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>
32 #include <asm/systemcfg.h>
33 #include <asm/ppcdebug.h>
35 struct flash_block_list_header rtas_firmware_flash_list
= {0, NULL
};
37 struct rtas_t rtas
= {
38 .lock
= SPIN_LOCK_UNLOCKED
43 char rtas_err_buf
[RTAS_ERROR_LOG_MAX
];
45 DEFINE_SPINLOCK(rtas_data_buf_lock
);
46 char rtas_data_buf
[RTAS_DATA_BUF_SIZE
]__page_aligned
;
47 unsigned long rtas_rmo_buf
;
50 call_rtas_display_status(unsigned char c
)
52 struct rtas_args
*args
= &rtas
.args
;
57 spin_lock_irqsave(&rtas
.lock
, s
);
62 args
->rets
= (rtas_arg_t
*)&(args
->args
[1]);
63 args
->args
[0] = (int)c
;
65 enter_rtas(__pa(args
));
67 spin_unlock_irqrestore(&rtas
.lock
, s
);
71 call_rtas_display_status_delay(unsigned char c
)
73 static int pending_newline
= 0; /* did last write end with unprinted newline? */
74 static int width
= 16;
78 call_rtas_display_status(' ');
83 if (pending_newline
) {
84 call_rtas_display_status('\r');
85 call_rtas_display_status('\n');
89 call_rtas_display_status(c
);
96 rtas_progress(char *s
, unsigned short hex
)
98 struct device_node
*root
;
101 static int display_character
, set_indicator
;
102 static int display_width
, display_lines
, *row_width
, form_feed
;
103 static DEFINE_SPINLOCK(progress_lock
);
104 static int current_line
;
105 static int pending_newline
= 0; /* did last write end with unprinted newline? */
110 if (display_width
== 0) {
111 display_width
= 0x10;
112 if ((root
= find_path_device("/rtas"))) {
113 if ((p
= (unsigned int *)get_property(root
,
114 "ibm,display-line-length", NULL
)))
116 if ((p
= (unsigned int *)get_property(root
,
117 "ibm,form-feed", NULL
)))
119 if ((p
= (unsigned int *)get_property(root
,
120 "ibm,display-number-of-lines", NULL
)))
122 row_width
= (unsigned int *)get_property(root
,
123 "ibm,display-truncation-length", NULL
);
125 display_character
= rtas_token("display-character");
126 set_indicator
= rtas_token("set-indicator");
129 if (display_character
== RTAS_UNKNOWN_SERVICE
) {
130 /* use hex display if available */
131 if (set_indicator
!= RTAS_UNKNOWN_SERVICE
)
132 rtas_call(set_indicator
, 3, 1, NULL
, 6, 0, hex
);
136 spin_lock(&progress_lock
);
139 * Last write ended with newline, but we didn't print it since
140 * it would just clear the bottom line of output. Print it now
143 * If no newline is pending and form feed is supported, clear the
144 * display with a form feed; otherwise, print a CR to start output
145 * at the beginning of the line.
147 if (pending_newline
) {
148 rtas_call(display_character
, 1, 1, NULL
, '\r');
149 rtas_call(display_character
, 1, 1, NULL
, '\n');
154 rtas_call(display_character
, 1, 1, NULL
,
157 rtas_call(display_character
, 1, 1, NULL
, '\r');
161 width
= row_width
[current_line
];
163 width
= display_width
;
166 if (*os
== '\n' || *os
== '\r') {
167 /* If newline is the last character, save it
168 * until next call to avoid bumping up the
171 if (*os
== '\n' && !os
[1]) {
174 if (current_line
> display_lines
-1)
175 current_line
= display_lines
-1;
176 spin_unlock(&progress_lock
);
180 /* RTAS wants CR-LF, not just LF */
183 rtas_call(display_character
, 1, 1, NULL
, '\r');
184 rtas_call(display_character
, 1, 1, NULL
, '\n');
186 /* CR might be used to re-draw a line, so we'll
187 * leave it alone and not add LF.
189 rtas_call(display_character
, 1, 1, NULL
, *os
);
193 width
= row_width
[current_line
];
195 width
= display_width
;
198 rtas_call(display_character
, 1, 1, NULL
, *os
);
203 /* if we overwrite the screen length */
205 while ((*os
!= 0) && (*os
!= '\n') && (*os
!= '\r'))
209 spin_unlock(&progress_lock
);
213 rtas_token(const char *service
)
216 if (rtas
.dev
== NULL
) {
217 PPCDBG(PPCDBG_RTAS
,"\tNo rtas device in device-tree...\n");
218 return RTAS_UNKNOWN_SERVICE
;
220 tokp
= (int *) get_property(rtas
.dev
, service
, NULL
);
221 return tokp
? *tokp
: RTAS_UNKNOWN_SERVICE
;
225 * Return the firmware-specified size of the error log buffer
226 * for all rtas calls that require an error buffer argument.
227 * This includes 'check-exception' and 'rtas-last-error'.
229 int rtas_get_error_log_max(void)
231 static int rtas_error_log_max
;
232 if (rtas_error_log_max
)
233 return rtas_error_log_max
;
235 rtas_error_log_max
= rtas_token ("rtas-error-log-max");
236 if ((rtas_error_log_max
== RTAS_UNKNOWN_SERVICE
) ||
237 (rtas_error_log_max
> RTAS_ERROR_LOG_MAX
)) {
238 printk (KERN_WARNING
"RTAS: bad log buffer size %d\n", rtas_error_log_max
);
239 rtas_error_log_max
= RTAS_ERROR_LOG_MAX
;
241 return rtas_error_log_max
;
245 /** Return a copy of the detailed error text associated with the
246 * most recent failed call to rtas. Because the error text
247 * might go stale if there are any other intervening rtas calls,
248 * this routine must be called atomically with whatever produced
249 * the error (i.e. with rtas.lock still held from the previous call).
252 __fetch_rtas_last_error(void)
254 struct rtas_args err_args
, save_args
;
257 bufsz
= rtas_get_error_log_max();
259 err_args
.token
= rtas_token("rtas-last-error");
263 err_args
.args
[0] = (rtas_arg_t
)__pa(rtas_err_buf
);
264 err_args
.args
[1] = bufsz
;
265 err_args
.args
[2] = 0;
267 save_args
= rtas
.args
;
268 rtas
.args
= err_args
;
270 enter_rtas(__pa(&rtas
.args
));
272 err_args
= rtas
.args
;
273 rtas
.args
= save_args
;
275 return err_args
.args
[2];
278 int rtas_call(int token
, int nargs
, int nret
, int *outputs
, ...)
283 struct rtas_args
*rtas_args
;
284 char * buff_copy
= NULL
;
287 PPCDBG(PPCDBG_RTAS
, "Entering rtas_call\n");
288 PPCDBG(PPCDBG_RTAS
, "\ttoken = 0x%x\n", token
);
289 PPCDBG(PPCDBG_RTAS
, "\tnargs = %d\n", nargs
);
290 PPCDBG(PPCDBG_RTAS
, "\tnret = %d\n", nret
);
291 PPCDBG(PPCDBG_RTAS
, "\t&outputs = 0x%lx\n", outputs
);
292 if (token
== RTAS_UNKNOWN_SERVICE
)
295 /* Gotta do something different here, use global lock for now... */
296 spin_lock_irqsave(&rtas
.lock
, s
);
297 rtas_args
= &rtas
.args
;
299 rtas_args
->token
= token
;
300 rtas_args
->nargs
= nargs
;
301 rtas_args
->nret
= nret
;
302 rtas_args
->rets
= (rtas_arg_t
*)&(rtas_args
->args
[nargs
]);
303 va_start(list
, outputs
);
304 for (i
= 0; i
< nargs
; ++i
) {
305 rtas_args
->args
[i
] = va_arg(list
, rtas_arg_t
);
306 PPCDBG(PPCDBG_RTAS
, "\tnarg[%d] = 0x%x\n", i
, rtas_args
->args
[i
]);
310 for (i
= 0; i
< nret
; ++i
)
311 rtas_args
->rets
[i
] = 0;
313 PPCDBG(PPCDBG_RTAS
, "\tentering rtas with 0x%lx\n",
315 enter_rtas(__pa(rtas_args
));
316 PPCDBG(PPCDBG_RTAS
, "\treturned from rtas ...\n");
318 /* A -1 return code indicates that the last command couldn't
319 be completed due to a hardware error. */
320 if (rtas_args
->rets
[0] == -1)
321 logit
= (__fetch_rtas_last_error() == 0);
323 ifppcdebug(PPCDBG_RTAS
) {
324 for(i
=0; i
< nret
;i
++)
325 udbg_printf("\tnret[%d] = 0x%lx\n", i
, (ulong
)rtas_args
->rets
[i
]);
328 if (nret
> 1 && outputs
!= NULL
)
329 for (i
= 0; i
< nret
-1; ++i
)
330 outputs
[i
] = rtas_args
->rets
[i
+1];
331 ret
= (nret
> 0)? rtas_args
->rets
[0]: 0;
333 /* Log the error in the unlikely case that there was one. */
334 if (unlikely(logit
)) {
335 buff_copy
= rtas_err_buf
;
337 buff_copy
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_ATOMIC
);
339 memcpy(buff_copy
, rtas_err_buf
,
344 /* Gotta do something different here, use global lock for now... */
345 spin_unlock_irqrestore(&rtas
.lock
, s
);
348 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
355 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
356 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
359 rtas_extended_busy_delay_time(int status
)
361 int order
= status
- 9900;
365 order
= 0; /* RTC depends on this for -2 clock busy */
367 order
= 5; /* bound */
369 /* Use microseconds for reasonable accuracy */
370 for (ms
=1; order
> 0; order
--)
376 int rtas_error_rc(int rtas_rc
)
381 case -1: /* Hardware Error */
384 case -3: /* Bad indicator/domain/etc */
387 case -9000: /* Isolation error */
390 case -9001: /* Outstanding TCE/PTE */
393 case -9002: /* No usable slot */
397 printk(KERN_ERR
"%s: unexpected RTAS error %d\n",
398 __FUNCTION__
, rtas_rc
);
405 int rtas_get_power_level(int powerdomain
, int *level
)
407 int token
= rtas_token("get-power-level");
410 if (token
== RTAS_UNKNOWN_SERVICE
)
413 while ((rc
= rtas_call(token
, 1, 2, level
, powerdomain
)) == RTAS_BUSY
)
417 return rtas_error_rc(rc
);
421 int rtas_set_power_level(int powerdomain
, int level
, int *setlevel
)
423 int token
= rtas_token("set-power-level");
424 unsigned int wait_time
;
427 if (token
== RTAS_UNKNOWN_SERVICE
)
431 rc
= rtas_call(token
, 2, 2, setlevel
, powerdomain
, level
);
434 else if (rtas_is_extended_busy(rc
)) {
435 wait_time
= rtas_extended_busy_delay_time(rc
);
436 udelay(wait_time
* 1000);
442 return rtas_error_rc(rc
);
446 int rtas_get_sensor(int sensor
, int index
, int *state
)
448 int token
= rtas_token("get-sensor-state");
449 unsigned int wait_time
;
452 if (token
== RTAS_UNKNOWN_SERVICE
)
456 rc
= rtas_call(token
, 2, 2, state
, sensor
, index
);
459 else if (rtas_is_extended_busy(rc
)) {
460 wait_time
= rtas_extended_busy_delay_time(rc
);
461 udelay(wait_time
* 1000);
467 return rtas_error_rc(rc
);
471 int rtas_set_indicator(int indicator
, int index
, int new_value
)
473 int token
= rtas_token("set-indicator");
474 unsigned int wait_time
;
477 if (token
== RTAS_UNKNOWN_SERVICE
)
481 rc
= rtas_call(token
, 3, 1, NULL
, indicator
, index
, new_value
);
484 else if (rtas_is_extended_busy(rc
)) {
485 wait_time
= rtas_extended_busy_delay_time(rc
);
486 udelay(wait_time
* 1000);
493 return rtas_error_rc(rc
);
497 #define FLASH_BLOCK_LIST_VERSION (1UL)
499 rtas_flash_firmware(void)
501 unsigned long image_size
;
502 struct flash_block_list
*f
, *next
, *flist
;
503 unsigned long rtas_block_list
;
504 int i
, status
, update_token
;
506 update_token
= rtas_token("ibm,update-flash-64-and-reboot");
507 if (update_token
== RTAS_UNKNOWN_SERVICE
) {
508 printk(KERN_ALERT
"FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
509 printk(KERN_ALERT
"FLASH: firmware will not be flashed\n");
513 /* NOTE: the "first" block list is a global var with no data
514 * blocks in the kernel data segment. We do this because
515 * we want to ensure this block_list addr is under 4GB.
517 rtas_firmware_flash_list
.num_blocks
= 0;
518 flist
= (struct flash_block_list
*)&rtas_firmware_flash_list
;
519 rtas_block_list
= virt_to_abs(flist
);
520 if (rtas_block_list
>= 4UL*1024*1024*1024) {
521 printk(KERN_ALERT
"FLASH: kernel bug...flash list header addr above 4GB\n");
525 printk(KERN_ALERT
"FLASH: preparing saved firmware image for flash\n");
526 /* Update the block_list in place. */
528 for (f
= flist
; f
; f
= next
) {
529 /* Translate data addrs to absolute */
530 for (i
= 0; i
< f
->num_blocks
; i
++) {
531 f
->blocks
[i
].data
= (char *)virt_to_abs(f
->blocks
[i
].data
);
532 image_size
+= f
->blocks
[i
].length
;
535 /* Don't translate NULL pointer for last entry */
537 f
->next
= (struct flash_block_list
*)virt_to_abs(f
->next
);
540 /* make num_blocks into the version/length field */
541 f
->num_blocks
= (FLASH_BLOCK_LIST_VERSION
<< 56) | ((f
->num_blocks
+1)*16);
544 printk(KERN_ALERT
"FLASH: flash image is %ld bytes\n", image_size
);
545 printk(KERN_ALERT
"FLASH: performing flash and reboot\n");
546 rtas_progress("Flashing \n", 0x0);
547 rtas_progress("Please Wait... ", 0x0);
548 printk(KERN_ALERT
"FLASH: this will take several minutes. Do not power off!\n");
549 status
= rtas_call(update_token
, 1, 1, NULL
, rtas_block_list
);
550 switch (status
) { /* should only get "bad" status */
552 printk(KERN_ALERT
"FLASH: success\n");
555 printk(KERN_ALERT
"FLASH: hardware error. Firmware may not be not flashed\n");
558 printk(KERN_ALERT
"FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
561 printk(KERN_ALERT
"FLASH: flash failed when partially complete. System may not reboot\n");
564 printk(KERN_ALERT
"FLASH: unknown flash return code %d\n", status
);
569 void rtas_flash_bypass_warning(void)
571 printk(KERN_ALERT
"FLASH: firmware flash requires a reboot\n");
572 printk(KERN_ALERT
"FLASH: the firmware image will NOT be flashed\n");
577 rtas_restart(char *cmd
)
579 if (rtas_firmware_flash_list
.next
)
580 rtas_flash_firmware();
582 printk("RTAS system-reboot returned %d\n",
583 rtas_call(rtas_token("system-reboot"), 0, 1, NULL
));
590 if (rtas_firmware_flash_list
.next
)
591 rtas_flash_bypass_warning();
592 /* allow power on only with power button press */
593 printk("RTAS power-off returned %d\n",
594 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
601 if (rtas_firmware_flash_list
.next
)
602 rtas_flash_bypass_warning();
606 /* Must be in the RMO region, so we place it here */
607 static char rtas_os_term_buf
[2048];
609 void rtas_os_term(char *str
)
613 if (RTAS_UNKNOWN_SERVICE
== rtas_token("ibm,os-term"))
616 snprintf(rtas_os_term_buf
, 2048, "OS panic: %s", str
);
619 status
= rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL
,
620 __pa(rtas_os_term_buf
));
622 if (status
== RTAS_BUSY
)
624 else if (status
!= 0)
625 printk(KERN_EMERG
"ibm,os-term call failed %d\n",
627 } while (status
== RTAS_BUSY
);
631 asmlinkage
int ppc_rtas(struct rtas_args __user
*uargs
)
633 struct rtas_args args
;
639 if (!capable(CAP_SYS_ADMIN
))
642 if (copy_from_user(&args
, uargs
, 3 * sizeof(u32
)) != 0)
646 if (nargs
> ARRAY_SIZE(args
.args
)
647 || args
.nret
> ARRAY_SIZE(args
.args
)
648 || nargs
+ args
.nret
> ARRAY_SIZE(args
.args
))
652 if (copy_from_user(args
.args
, uargs
->args
,
653 nargs
* sizeof(rtas_arg_t
)) != 0)
656 buff_copy
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_KERNEL
);
658 spin_lock_irqsave(&rtas
.lock
, flags
);
661 enter_rtas(__pa(&rtas
.args
));
664 args
.rets
= &args
.args
[nargs
];
666 /* A -1 return code indicates that the last command couldn't
667 be completed due to a hardware error. */
668 if (args
.rets
[0] == -1) {
669 err_rc
= __fetch_rtas_last_error();
670 if ((err_rc
== 0) && buff_copy
) {
671 memcpy(buff_copy
, rtas_err_buf
, RTAS_ERROR_LOG_MAX
);
675 spin_unlock_irqrestore(&rtas
.lock
, flags
);
678 if ((args
.rets
[0] == -1) && (err_rc
== 0)) {
679 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
685 if (copy_to_user(uargs
->args
+ nargs
,
687 args
.nret
* sizeof(rtas_arg_t
)) != 0)
693 /* This version can't take the spinlock, because it never returns */
695 struct rtas_args rtas_stop_self_args
= {
696 /* The token is initialized for real in setup_system() */
697 .token
= RTAS_UNKNOWN_SERVICE
,
700 .rets
= &rtas_stop_self_args
.args
[0],
703 void rtas_stop_self(void)
705 struct rtas_args
*rtas_args
= &rtas_stop_self_args
;
709 BUG_ON(rtas_args
->token
== RTAS_UNKNOWN_SERVICE
);
711 printk("cpu %u (hwid %u) Ready to die...\n",
712 smp_processor_id(), hard_smp_processor_id());
713 enter_rtas(__pa(rtas_args
));
715 panic("Alas, I survived.\n");
719 * Call early during boot, before mem init or bootmem, to retreive the RTAS
720 * informations from the device-tree and allocate the RMO buffer for userland
723 void __init
rtas_initialize(void)
725 /* Get RTAS dev node and fill up our "rtas" structure with infos
728 rtas
.dev
= of_find_node_by_name(NULL
, "rtas");
733 basep
= (u32
*)get_property(rtas
.dev
, "linux,rtas-base", NULL
);
734 sizep
= (u32
*)get_property(rtas
.dev
, "rtas-size", NULL
);
735 if (basep
!= NULL
&& sizep
!= NULL
) {
738 entryp
= (u32
*)get_property(rtas
.dev
, "linux,rtas-entry", NULL
);
739 if (entryp
== NULL
) /* Ugh */
740 rtas
.entry
= rtas
.base
;
742 rtas
.entry
= *entryp
;
746 /* If RTAS was found, allocate the RMO buffer for it and look for
747 * the stop-self token if any
750 unsigned long rtas_region
= RTAS_INSTANTIATE_MAX
;
751 if (systemcfg
->platform
== PLATFORM_PSERIES_LPAR
)
752 rtas_region
= min(lmb
.rmo_size
, RTAS_INSTANTIATE_MAX
);
754 rtas_rmo_buf
= lmb_alloc_base(RTAS_RMOBUF_MAX
, PAGE_SIZE
,
757 #ifdef CONFIG_HOTPLUG_CPU
758 rtas_stop_self_args
.token
= rtas_token("stop-self");
759 #endif /* CONFIG_HOTPLUG_CPU */
765 EXPORT_SYMBOL(rtas_firmware_flash_list
);
766 EXPORT_SYMBOL(rtas_token
);
767 EXPORT_SYMBOL(rtas_call
);
768 EXPORT_SYMBOL(rtas_data_buf
);
769 EXPORT_SYMBOL(rtas_data_buf_lock
);
770 EXPORT_SYMBOL(rtas_extended_busy_delay_time
);
771 EXPORT_SYMBOL(rtas_get_sensor
);
772 EXPORT_SYMBOL(rtas_get_power_level
);
773 EXPORT_SYMBOL(rtas_set_power_level
);
774 EXPORT_SYMBOL(rtas_set_indicator
);
775 EXPORT_SYMBOL(rtas_get_error_log_max
);