3 * Procedures for interfacing to the RTAS on CHRP machines.
5 * Peter Bergner, IBM March 2001.
6 * Copyright (C) 2001 IBM.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
23 #include <asm/semaphore.h>
24 #include <asm/machdep.h>
26 #include <asm/param.h>
27 #include <asm/system.h>
28 #include <asm/abs_addr.h>
30 #include <asm/delay.h>
31 #include <asm/uaccess.h>
33 struct flash_block_list_header rtas_firmware_flash_list
= {0, NULL
};
35 struct rtas_t rtas
= {
36 .lock
= SPIN_LOCK_UNLOCKED
41 char rtas_err_buf
[RTAS_ERROR_LOG_MAX
];
43 spinlock_t rtas_data_buf_lock
= SPIN_LOCK_UNLOCKED
;
44 char rtas_data_buf
[RTAS_DATA_BUF_SIZE
]__page_aligned
;
45 unsigned long rtas_rmo_buf
;
48 call_rtas_display_status(unsigned char c
)
50 struct rtas_args
*args
= &rtas
.args
;
55 spin_lock_irqsave(&rtas
.lock
, s
);
60 args
->rets
= (rtas_arg_t
*)&(args
->args
[1]);
61 args
->args
[0] = (int)c
;
63 enter_rtas(__pa(args
));
65 spin_unlock_irqrestore(&rtas
.lock
, s
);
69 call_rtas_display_status_delay(unsigned char c
)
71 static int pending_newline
= 0; /* did last write end with unprinted newline? */
72 static int width
= 16;
76 call_rtas_display_status(' ');
81 if (pending_newline
) {
82 call_rtas_display_status('\r');
83 call_rtas_display_status('\n');
87 call_rtas_display_status(c
);
94 rtas_token(const char *service
)
97 if (rtas
.dev
== NULL
) {
98 PPCDBG(PPCDBG_RTAS
,"\tNo rtas device in device-tree...\n");
99 return RTAS_UNKNOWN_SERVICE
;
101 tokp
= (int *) get_property(rtas
.dev
, service
, NULL
);
102 return tokp
? *tokp
: RTAS_UNKNOWN_SERVICE
;
105 /** Return a copy of the detailed error text associated with the
106 * most recent failed call to rtas. Because the error text
107 * might go stale if there are any other intervening rtas calls,
108 * this routine must be called atomically with whatever produced
109 * the error (i.e. with rtas.lock still held from the previous call).
112 __fetch_rtas_last_error(void)
114 struct rtas_args err_args
, save_args
;
117 bufsz
= rtas_token ("rtas-error-log-max");
118 if ((bufsz
== RTAS_UNKNOWN_SERVICE
) ||
119 (bufsz
> RTAS_ERROR_LOG_MAX
)) {
120 printk (KERN_WARNING
"RTAS: bad log buffer size %d\n", bufsz
);
121 bufsz
= RTAS_ERROR_LOG_MAX
;
124 err_args
.token
= rtas_token("rtas-last-error");
128 err_args
.args
[0] = (rtas_arg_t
)__pa(rtas_err_buf
);
129 err_args
.args
[1] = bufsz
;
130 err_args
.args
[2] = 0;
132 save_args
= rtas
.args
;
133 rtas
.args
= err_args
;
135 enter_rtas(__pa(&rtas
.args
));
137 err_args
= rtas
.args
;
138 rtas
.args
= save_args
;
140 return err_args
.args
[2];
143 int rtas_call(int token
, int nargs
, int nret
, int *outputs
, ...)
148 struct rtas_args
*rtas_args
;
149 char * buff_copy
= NULL
;
152 PPCDBG(PPCDBG_RTAS
, "Entering rtas_call\n");
153 PPCDBG(PPCDBG_RTAS
, "\ttoken = 0x%x\n", token
);
154 PPCDBG(PPCDBG_RTAS
, "\tnargs = %d\n", nargs
);
155 PPCDBG(PPCDBG_RTAS
, "\tnret = %d\n", nret
);
156 PPCDBG(PPCDBG_RTAS
, "\t&outputs = 0x%lx\n", outputs
);
157 if (token
== RTAS_UNKNOWN_SERVICE
)
160 /* Gotta do something different here, use global lock for now... */
161 spin_lock_irqsave(&rtas
.lock
, s
);
162 rtas_args
= &rtas
.args
;
164 rtas_args
->token
= token
;
165 rtas_args
->nargs
= nargs
;
166 rtas_args
->nret
= nret
;
167 rtas_args
->rets
= (rtas_arg_t
*)&(rtas_args
->args
[nargs
]);
168 va_start(list
, outputs
);
169 for (i
= 0; i
< nargs
; ++i
) {
170 rtas_args
->args
[i
] = va_arg(list
, rtas_arg_t
);
171 PPCDBG(PPCDBG_RTAS
, "\tnarg[%d] = 0x%x\n", i
, rtas_args
->args
[i
]);
175 for (i
= 0; i
< nret
; ++i
)
176 rtas_args
->rets
[i
] = 0;
178 PPCDBG(PPCDBG_RTAS
, "\tentering rtas with 0x%lx\n",
180 enter_rtas(__pa(rtas_args
));
181 PPCDBG(PPCDBG_RTAS
, "\treturned from rtas ...\n");
183 /* A -1 return code indicates that the last command couldn't
184 be completed due to a hardware error. */
185 if (rtas_args
->rets
[0] == -1)
186 logit
= (__fetch_rtas_last_error() == 0);
188 ifppcdebug(PPCDBG_RTAS
) {
189 for(i
=0; i
< nret
;i
++)
190 udbg_printf("\tnret[%d] = 0x%lx\n", i
, (ulong
)rtas_args
->rets
[i
]);
193 if (nret
> 1 && outputs
!= NULL
)
194 for (i
= 0; i
< nret
-1; ++i
)
195 outputs
[i
] = rtas_args
->rets
[i
+1];
196 ret
= (nret
> 0)? rtas_args
->rets
[0]: 0;
198 /* Log the error in the unlikely case that there was one. */
199 if (unlikely(logit
)) {
200 buff_copy
= rtas_err_buf
;
202 buff_copy
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_ATOMIC
);
204 memcpy(buff_copy
, rtas_err_buf
,
209 /* Gotta do something different here, use global lock for now... */
210 spin_unlock_irqrestore(&rtas
.lock
, s
);
213 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
220 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
221 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
224 rtas_extended_busy_delay_time(int status
)
226 int order
= status
- 9900;
230 order
= 0; /* RTC depends on this for -2 clock busy */
232 order
= 5; /* bound */
234 /* Use microseconds for reasonable accuracy */
235 for (ms
=1; order
> 0; order
--)
242 rtas_get_power_level(int powerdomain
, int *level
)
244 int token
= rtas_token("get-power-level");
247 if (token
== RTAS_UNKNOWN_SERVICE
)
248 return RTAS_UNKNOWN_OP
;
250 while ((rc
= rtas_call(token
, 1, 2, level
, powerdomain
)) == RTAS_BUSY
)
256 rtas_set_power_level(int powerdomain
, int level
, int *setlevel
)
258 int token
= rtas_token("set-power-level");
259 unsigned int wait_time
;
262 if (token
== RTAS_UNKNOWN_SERVICE
)
263 return RTAS_UNKNOWN_OP
;
266 rc
= rtas_call(token
, 2, 2, setlevel
, powerdomain
, level
);
269 else if (rtas_is_extended_busy(rc
)) {
270 wait_time
= rtas_extended_busy_delay_time(rc
);
271 udelay(wait_time
* 1000);
279 rtas_get_sensor(int sensor
, int index
, int *state
)
281 int token
= rtas_token("get-sensor-state");
282 unsigned int wait_time
;
285 if (token
== RTAS_UNKNOWN_SERVICE
)
286 return RTAS_UNKNOWN_OP
;
289 rc
= rtas_call(token
, 2, 2, state
, sensor
, index
);
292 else if (rtas_is_extended_busy(rc
)) {
293 wait_time
= rtas_extended_busy_delay_time(rc
);
294 udelay(wait_time
* 1000);
302 rtas_set_indicator(int indicator
, int index
, int new_value
)
304 int token
= rtas_token("set-indicator");
305 unsigned int wait_time
;
308 if (token
== RTAS_UNKNOWN_SERVICE
)
309 return RTAS_UNKNOWN_OP
;
312 rc
= rtas_call(token
, 3, 1, NULL
, indicator
, index
, new_value
);
315 else if (rtas_is_extended_busy(rc
)) {
316 wait_time
= rtas_extended_busy_delay_time(rc
);
317 udelay(wait_time
* 1000);
326 #define FLASH_BLOCK_LIST_VERSION (1UL)
328 rtas_flash_firmware(void)
330 unsigned long image_size
;
331 struct flash_block_list
*f
, *next
, *flist
;
332 unsigned long rtas_block_list
;
333 int i
, status
, update_token
;
335 update_token
= rtas_token("ibm,update-flash-64-and-reboot");
336 if (update_token
== RTAS_UNKNOWN_SERVICE
) {
337 printk(KERN_ALERT
"FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
338 printk(KERN_ALERT
"FLASH: firmware will not be flashed\n");
342 /* NOTE: the "first" block list is a global var with no data
343 * blocks in the kernel data segment. We do this because
344 * we want to ensure this block_list addr is under 4GB.
346 rtas_firmware_flash_list
.num_blocks
= 0;
347 flist
= (struct flash_block_list
*)&rtas_firmware_flash_list
;
348 rtas_block_list
= virt_to_abs(flist
);
349 if (rtas_block_list
>= 4UL*1024*1024*1024) {
350 printk(KERN_ALERT
"FLASH: kernel bug...flash list header addr above 4GB\n");
354 printk(KERN_ALERT
"FLASH: preparing saved firmware image for flash\n");
355 /* Update the block_list in place. */
357 for (f
= flist
; f
; f
= next
) {
358 /* Translate data addrs to absolute */
359 for (i
= 0; i
< f
->num_blocks
; i
++) {
360 f
->blocks
[i
].data
= (char *)virt_to_abs(f
->blocks
[i
].data
);
361 image_size
+= f
->blocks
[i
].length
;
364 /* Don't translate NULL pointer for last entry */
366 f
->next
= (struct flash_block_list
*)virt_to_abs(f
->next
);
369 /* make num_blocks into the version/length field */
370 f
->num_blocks
= (FLASH_BLOCK_LIST_VERSION
<< 56) | ((f
->num_blocks
+1)*16);
373 printk(KERN_ALERT
"FLASH: flash image is %ld bytes\n", image_size
);
374 printk(KERN_ALERT
"FLASH: performing flash and reboot\n");
375 ppc_md
.progress("Flashing \n", 0x0);
376 ppc_md
.progress("Please Wait... ", 0x0);
377 printk(KERN_ALERT
"FLASH: this will take several minutes. Do not power off!\n");
378 status
= rtas_call(update_token
, 1, 1, NULL
, rtas_block_list
);
379 switch (status
) { /* should only get "bad" status */
381 printk(KERN_ALERT
"FLASH: success\n");
384 printk(KERN_ALERT
"FLASH: hardware error. Firmware may not be not flashed\n");
387 printk(KERN_ALERT
"FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
390 printk(KERN_ALERT
"FLASH: flash failed when partially complete. System may not reboot\n");
393 printk(KERN_ALERT
"FLASH: unknown flash return code %d\n", status
);
398 void rtas_flash_bypass_warning(void)
400 printk(KERN_ALERT
"FLASH: firmware flash requires a reboot\n");
401 printk(KERN_ALERT
"FLASH: the firmware image will NOT be flashed\n");
406 rtas_restart(char *cmd
)
408 if (rtas_firmware_flash_list
.next
)
409 rtas_flash_firmware();
411 printk("RTAS system-reboot returned %d\n",
412 rtas_call(rtas_token("system-reboot"), 0, 1, NULL
));
419 if (rtas_firmware_flash_list
.next
)
420 rtas_flash_bypass_warning();
421 /* allow power on only with power button press */
422 printk("RTAS power-off returned %d\n",
423 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
430 if (rtas_firmware_flash_list
.next
)
431 rtas_flash_bypass_warning();
435 /* Must be in the RMO region, so we place it here */
436 static char rtas_os_term_buf
[2048];
438 void rtas_os_term(char *str
)
442 snprintf(rtas_os_term_buf
, 2048, "OS panic: %s", str
);
445 status
= rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL
,
446 __pa(rtas_os_term_buf
));
448 if (status
== RTAS_BUSY
)
450 else if (status
!= 0)
451 printk(KERN_EMERG
"ibm,os-term call failed %d\n",
453 } while (status
== RTAS_BUSY
);
457 asmlinkage
int ppc_rtas(struct rtas_args __user
*uargs
)
459 struct rtas_args args
;
465 if (!capable(CAP_SYS_ADMIN
))
468 if (copy_from_user(&args
, uargs
, 3 * sizeof(u32
)) != 0)
472 if (nargs
> ARRAY_SIZE(args
.args
)
473 || args
.nret
> ARRAY_SIZE(args
.args
)
474 || nargs
+ args
.nret
> ARRAY_SIZE(args
.args
))
478 if (copy_from_user(args
.args
, uargs
->args
,
479 nargs
* sizeof(rtas_arg_t
)) != 0)
482 buff_copy
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_KERNEL
);
484 spin_lock_irqsave(&rtas
.lock
, flags
);
487 enter_rtas(__pa(&rtas
.args
));
490 args
.rets
= &args
.args
[nargs
];
492 /* A -1 return code indicates that the last command couldn't
493 be completed due to a hardware error. */
494 if (args
.rets
[0] == -1) {
495 err_rc
= __fetch_rtas_last_error();
496 if ((err_rc
== 0) && buff_copy
) {
497 memcpy(buff_copy
, rtas_err_buf
, RTAS_ERROR_LOG_MAX
);
501 spin_unlock_irqrestore(&rtas
.lock
, flags
);
504 if ((args
.rets
[0] == -1) && (err_rc
== 0)) {
505 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
511 if (copy_to_user(uargs
->args
+ nargs
,
513 args
.nret
* sizeof(rtas_arg_t
)) != 0)
519 #ifdef CONFIG_HOTPLUG_CPU
520 /* This version can't take the spinlock, because it never returns */
522 struct rtas_args rtas_stop_self_args
= {
523 /* The token is initialized for real in setup_system() */
524 .token
= RTAS_UNKNOWN_SERVICE
,
527 .rets
= &rtas_stop_self_args
.args
[0],
530 void rtas_stop_self(void)
532 struct rtas_args
*rtas_args
= &rtas_stop_self_args
;
536 BUG_ON(rtas_args
->token
== RTAS_UNKNOWN_SERVICE
);
538 printk("cpu %u (hwid %u) Ready to die...\n",
539 smp_processor_id(), hard_smp_processor_id());
540 enter_rtas(__pa(rtas_args
));
542 panic("Alas, I survived.\n");
544 #endif /* CONFIG_HOTPLUG_CPU */
547 * Return the firmware-specified size of the error log buffer
548 * for all rtas calls that require an error buffer argument.
549 * This includes 'check-exception' and 'rtas-last-error'.
551 int rtas_get_error_log_max(void)
553 static int rtas_error_log_max
;
554 if (rtas_error_log_max
)
555 return rtas_error_log_max
;
557 rtas_error_log_max
= rtas_token ("rtas-error-log-max");
558 if ((rtas_error_log_max
== RTAS_UNKNOWN_SERVICE
) ||
559 (rtas_error_log_max
> RTAS_ERROR_LOG_MAX
)) {
560 printk (KERN_WARNING
"RTAS: bad log buffer size %d\n", rtas_error_log_max
);
561 rtas_error_log_max
= RTAS_ERROR_LOG_MAX
;
563 return rtas_error_log_max
;
567 * Call early during boot, before mem init or bootmem, to retreive the RTAS
568 * informations from the device-tree and allocate the RMO buffer for userland
571 void __init
rtas_initialize(void)
573 /* Get RTAS dev node and fill up our "rtas" structure with infos
576 rtas
.dev
= of_find_node_by_name(NULL
, "rtas");
581 basep
= (u64
*)get_property(of_chosen
, "linux,rtas-base", NULL
);
582 sizep
= (u32
*)get_property(of_chosen
, "linux,rtas-size", NULL
);
583 if (basep
!= NULL
&& sizep
!= NULL
) {
586 entryp
= (u64
*)get_property(of_chosen
, "linux,rtas-entry", NULL
);
587 if (entryp
== NULL
) /* Ugh */
588 rtas
.entry
= rtas
.base
;
590 rtas
.entry
= *entryp
;
594 /* If RTAS was found, allocate the RMO buffer for it and look for
595 * the stop-self token if any
598 unsigned long rtas_region
= RTAS_INSTANTIATE_MAX
;
599 if (systemcfg
->platform
== PLATFORM_PSERIES_LPAR
)
600 rtas_region
= min(lmb
.rmo_size
, RTAS_INSTANTIATE_MAX
);
602 rtas_rmo_buf
= lmb_alloc_base(RTAS_RMOBUF_MAX
, PAGE_SIZE
,
605 #ifdef CONFIG_HOTPLUG_CPU
606 rtas_stop_self_args
.token
= rtas_token("stop-self");
607 #endif /* CONFIG_HOTPLUG_CPU */
613 EXPORT_SYMBOL(rtas_firmware_flash_list
);
614 EXPORT_SYMBOL(rtas_token
);
615 EXPORT_SYMBOL(rtas_call
);
616 EXPORT_SYMBOL(rtas_data_buf
);
617 EXPORT_SYMBOL(rtas_data_buf_lock
);
618 EXPORT_SYMBOL(rtas_extended_busy_delay_time
);
619 EXPORT_SYMBOL(rtas_get_sensor
);
620 EXPORT_SYMBOL(rtas_get_power_level
);
621 EXPORT_SYMBOL(rtas_set_power_level
);
622 EXPORT_SYMBOL(rtas_set_indicator
);
623 EXPORT_SYMBOL(rtas_get_error_log_max
);