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>
34 struct flash_block_list_header rtas_firmware_flash_list
= {0, NULL
};
36 struct rtas_t rtas
= {
37 .lock
= SPIN_LOCK_UNLOCKED
42 char rtas_err_buf
[RTAS_ERROR_LOG_MAX
];
44 DEFINE_SPINLOCK(rtas_data_buf_lock
);
45 char rtas_data_buf
[RTAS_DATA_BUF_SIZE
]__page_aligned
;
46 unsigned long rtas_rmo_buf
;
49 call_rtas_display_status(unsigned char c
)
51 struct rtas_args
*args
= &rtas
.args
;
56 spin_lock_irqsave(&rtas
.lock
, s
);
61 args
->rets
= (rtas_arg_t
*)&(args
->args
[1]);
62 args
->args
[0] = (int)c
;
64 enter_rtas(__pa(args
));
66 spin_unlock_irqrestore(&rtas
.lock
, s
);
70 call_rtas_display_status_delay(unsigned char c
)
72 static int pending_newline
= 0; /* did last write end with unprinted newline? */
73 static int width
= 16;
77 call_rtas_display_status(' ');
82 if (pending_newline
) {
83 call_rtas_display_status('\r');
84 call_rtas_display_status('\n');
88 call_rtas_display_status(c
);
95 rtas_token(const char *service
)
98 if (rtas
.dev
== NULL
) {
99 PPCDBG(PPCDBG_RTAS
,"\tNo rtas device in device-tree...\n");
100 return RTAS_UNKNOWN_SERVICE
;
102 tokp
= (int *) get_property(rtas
.dev
, service
, NULL
);
103 return tokp
? *tokp
: RTAS_UNKNOWN_SERVICE
;
107 * Return the firmware-specified size of the error log buffer
108 * for all rtas calls that require an error buffer argument.
109 * This includes 'check-exception' and 'rtas-last-error'.
111 int rtas_get_error_log_max(void)
113 static int rtas_error_log_max
;
114 if (rtas_error_log_max
)
115 return rtas_error_log_max
;
117 rtas_error_log_max
= rtas_token ("rtas-error-log-max");
118 if ((rtas_error_log_max
== RTAS_UNKNOWN_SERVICE
) ||
119 (rtas_error_log_max
> RTAS_ERROR_LOG_MAX
)) {
120 printk (KERN_WARNING
"RTAS: bad log buffer size %d\n", rtas_error_log_max
);
121 rtas_error_log_max
= RTAS_ERROR_LOG_MAX
;
123 return rtas_error_log_max
;
127 /** Return a copy of the detailed error text associated with the
128 * most recent failed call to rtas. Because the error text
129 * might go stale if there are any other intervening rtas calls,
130 * this routine must be called atomically with whatever produced
131 * the error (i.e. with rtas.lock still held from the previous call).
134 __fetch_rtas_last_error(void)
136 struct rtas_args err_args
, save_args
;
139 bufsz
= rtas_get_error_log_max();
141 err_args
.token
= rtas_token("rtas-last-error");
145 err_args
.args
[0] = (rtas_arg_t
)__pa(rtas_err_buf
);
146 err_args
.args
[1] = bufsz
;
147 err_args
.args
[2] = 0;
149 save_args
= rtas
.args
;
150 rtas
.args
= err_args
;
152 enter_rtas(__pa(&rtas
.args
));
154 err_args
= rtas
.args
;
155 rtas
.args
= save_args
;
157 return err_args
.args
[2];
160 int rtas_call(int token
, int nargs
, int nret
, int *outputs
, ...)
165 struct rtas_args
*rtas_args
;
166 char * buff_copy
= NULL
;
169 PPCDBG(PPCDBG_RTAS
, "Entering rtas_call\n");
170 PPCDBG(PPCDBG_RTAS
, "\ttoken = 0x%x\n", token
);
171 PPCDBG(PPCDBG_RTAS
, "\tnargs = %d\n", nargs
);
172 PPCDBG(PPCDBG_RTAS
, "\tnret = %d\n", nret
);
173 PPCDBG(PPCDBG_RTAS
, "\t&outputs = 0x%lx\n", outputs
);
174 if (token
== RTAS_UNKNOWN_SERVICE
)
177 /* Gotta do something different here, use global lock for now... */
178 spin_lock_irqsave(&rtas
.lock
, s
);
179 rtas_args
= &rtas
.args
;
181 rtas_args
->token
= token
;
182 rtas_args
->nargs
= nargs
;
183 rtas_args
->nret
= nret
;
184 rtas_args
->rets
= (rtas_arg_t
*)&(rtas_args
->args
[nargs
]);
185 va_start(list
, outputs
);
186 for (i
= 0; i
< nargs
; ++i
) {
187 rtas_args
->args
[i
] = va_arg(list
, rtas_arg_t
);
188 PPCDBG(PPCDBG_RTAS
, "\tnarg[%d] = 0x%x\n", i
, rtas_args
->args
[i
]);
192 for (i
= 0; i
< nret
; ++i
)
193 rtas_args
->rets
[i
] = 0;
195 PPCDBG(PPCDBG_RTAS
, "\tentering rtas with 0x%lx\n",
197 enter_rtas(__pa(rtas_args
));
198 PPCDBG(PPCDBG_RTAS
, "\treturned from rtas ...\n");
200 /* A -1 return code indicates that the last command couldn't
201 be completed due to a hardware error. */
202 if (rtas_args
->rets
[0] == -1)
203 logit
= (__fetch_rtas_last_error() == 0);
205 ifppcdebug(PPCDBG_RTAS
) {
206 for(i
=0; i
< nret
;i
++)
207 udbg_printf("\tnret[%d] = 0x%lx\n", i
, (ulong
)rtas_args
->rets
[i
]);
210 if (nret
> 1 && outputs
!= NULL
)
211 for (i
= 0; i
< nret
-1; ++i
)
212 outputs
[i
] = rtas_args
->rets
[i
+1];
213 ret
= (nret
> 0)? rtas_args
->rets
[0]: 0;
215 /* Log the error in the unlikely case that there was one. */
216 if (unlikely(logit
)) {
217 buff_copy
= rtas_err_buf
;
219 buff_copy
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_ATOMIC
);
221 memcpy(buff_copy
, rtas_err_buf
,
226 /* Gotta do something different here, use global lock for now... */
227 spin_unlock_irqrestore(&rtas
.lock
, s
);
230 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
237 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
238 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
241 rtas_extended_busy_delay_time(int status
)
243 int order
= status
- 9900;
247 order
= 0; /* RTC depends on this for -2 clock busy */
249 order
= 5; /* bound */
251 /* Use microseconds for reasonable accuracy */
252 for (ms
=1; order
> 0; order
--)
258 int rtas_error_rc(int rtas_rc
)
263 case -1: /* Hardware Error */
266 case -3: /* Bad indicator/domain/etc */
269 case -9000: /* Isolation error */
272 case -9001: /* Outstanding TCE/PTE */
275 case -9002: /* No usable slot */
279 printk(KERN_ERR
"%s: unexpected RTAS error %d\n",
280 __FUNCTION__
, rtas_rc
);
287 int rtas_get_power_level(int powerdomain
, int *level
)
289 int token
= rtas_token("get-power-level");
292 if (token
== RTAS_UNKNOWN_SERVICE
)
295 while ((rc
= rtas_call(token
, 1, 2, level
, powerdomain
)) == RTAS_BUSY
)
299 return rtas_error_rc(rc
);
303 int rtas_set_power_level(int powerdomain
, int level
, int *setlevel
)
305 int token
= rtas_token("set-power-level");
306 unsigned int wait_time
;
309 if (token
== RTAS_UNKNOWN_SERVICE
)
313 rc
= rtas_call(token
, 2, 2, setlevel
, powerdomain
, level
);
316 else if (rtas_is_extended_busy(rc
)) {
317 wait_time
= rtas_extended_busy_delay_time(rc
);
318 udelay(wait_time
* 1000);
324 return rtas_error_rc(rc
);
328 int rtas_get_sensor(int sensor
, int index
, int *state
)
330 int token
= rtas_token("get-sensor-state");
331 unsigned int wait_time
;
334 if (token
== RTAS_UNKNOWN_SERVICE
)
338 rc
= rtas_call(token
, 2, 2, state
, sensor
, index
);
341 else if (rtas_is_extended_busy(rc
)) {
342 wait_time
= rtas_extended_busy_delay_time(rc
);
343 udelay(wait_time
* 1000);
349 return rtas_error_rc(rc
);
353 int rtas_set_indicator(int indicator
, int index
, int new_value
)
355 int token
= rtas_token("set-indicator");
356 unsigned int wait_time
;
359 if (token
== RTAS_UNKNOWN_SERVICE
)
363 rc
= rtas_call(token
, 3, 1, NULL
, indicator
, index
, new_value
);
366 else if (rtas_is_extended_busy(rc
)) {
367 wait_time
= rtas_extended_busy_delay_time(rc
);
368 udelay(wait_time
* 1000);
375 return rtas_error_rc(rc
);
379 #define FLASH_BLOCK_LIST_VERSION (1UL)
381 rtas_flash_firmware(void)
383 unsigned long image_size
;
384 struct flash_block_list
*f
, *next
, *flist
;
385 unsigned long rtas_block_list
;
386 int i
, status
, update_token
;
388 update_token
= rtas_token("ibm,update-flash-64-and-reboot");
389 if (update_token
== RTAS_UNKNOWN_SERVICE
) {
390 printk(KERN_ALERT
"FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
391 printk(KERN_ALERT
"FLASH: firmware will not be flashed\n");
395 /* NOTE: the "first" block list is a global var with no data
396 * blocks in the kernel data segment. We do this because
397 * we want to ensure this block_list addr is under 4GB.
399 rtas_firmware_flash_list
.num_blocks
= 0;
400 flist
= (struct flash_block_list
*)&rtas_firmware_flash_list
;
401 rtas_block_list
= virt_to_abs(flist
);
402 if (rtas_block_list
>= 4UL*1024*1024*1024) {
403 printk(KERN_ALERT
"FLASH: kernel bug...flash list header addr above 4GB\n");
407 printk(KERN_ALERT
"FLASH: preparing saved firmware image for flash\n");
408 /* Update the block_list in place. */
410 for (f
= flist
; f
; f
= next
) {
411 /* Translate data addrs to absolute */
412 for (i
= 0; i
< f
->num_blocks
; i
++) {
413 f
->blocks
[i
].data
= (char *)virt_to_abs(f
->blocks
[i
].data
);
414 image_size
+= f
->blocks
[i
].length
;
417 /* Don't translate NULL pointer for last entry */
419 f
->next
= (struct flash_block_list
*)virt_to_abs(f
->next
);
422 /* make num_blocks into the version/length field */
423 f
->num_blocks
= (FLASH_BLOCK_LIST_VERSION
<< 56) | ((f
->num_blocks
+1)*16);
426 printk(KERN_ALERT
"FLASH: flash image is %ld bytes\n", image_size
);
427 printk(KERN_ALERT
"FLASH: performing flash and reboot\n");
428 ppc_md
.progress("Flashing \n", 0x0);
429 ppc_md
.progress("Please Wait... ", 0x0);
430 printk(KERN_ALERT
"FLASH: this will take several minutes. Do not power off!\n");
431 status
= rtas_call(update_token
, 1, 1, NULL
, rtas_block_list
);
432 switch (status
) { /* should only get "bad" status */
434 printk(KERN_ALERT
"FLASH: success\n");
437 printk(KERN_ALERT
"FLASH: hardware error. Firmware may not be not flashed\n");
440 printk(KERN_ALERT
"FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
443 printk(KERN_ALERT
"FLASH: flash failed when partially complete. System may not reboot\n");
446 printk(KERN_ALERT
"FLASH: unknown flash return code %d\n", status
);
451 void rtas_flash_bypass_warning(void)
453 printk(KERN_ALERT
"FLASH: firmware flash requires a reboot\n");
454 printk(KERN_ALERT
"FLASH: the firmware image will NOT be flashed\n");
459 rtas_restart(char *cmd
)
461 if (rtas_firmware_flash_list
.next
)
462 rtas_flash_firmware();
464 printk("RTAS system-reboot returned %d\n",
465 rtas_call(rtas_token("system-reboot"), 0, 1, NULL
));
472 if (rtas_firmware_flash_list
.next
)
473 rtas_flash_bypass_warning();
474 /* allow power on only with power button press */
475 printk("RTAS power-off returned %d\n",
476 rtas_call(rtas_token("power-off"), 2, 1, NULL
, -1, -1));
483 if (rtas_firmware_flash_list
.next
)
484 rtas_flash_bypass_warning();
488 /* Must be in the RMO region, so we place it here */
489 static char rtas_os_term_buf
[2048];
491 void rtas_os_term(char *str
)
495 if (RTAS_UNKNOWN_SERVICE
== rtas_token("ibm,os-term"))
498 snprintf(rtas_os_term_buf
, 2048, "OS panic: %s", str
);
501 status
= rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL
,
502 __pa(rtas_os_term_buf
));
504 if (status
== RTAS_BUSY
)
506 else if (status
!= 0)
507 printk(KERN_EMERG
"ibm,os-term call failed %d\n",
509 } while (status
== RTAS_BUSY
);
513 asmlinkage
int ppc_rtas(struct rtas_args __user
*uargs
)
515 struct rtas_args args
;
521 if (!capable(CAP_SYS_ADMIN
))
524 if (copy_from_user(&args
, uargs
, 3 * sizeof(u32
)) != 0)
528 if (nargs
> ARRAY_SIZE(args
.args
)
529 || args
.nret
> ARRAY_SIZE(args
.args
)
530 || nargs
+ args
.nret
> ARRAY_SIZE(args
.args
))
534 if (copy_from_user(args
.args
, uargs
->args
,
535 nargs
* sizeof(rtas_arg_t
)) != 0)
538 buff_copy
= kmalloc(RTAS_ERROR_LOG_MAX
, GFP_KERNEL
);
540 spin_lock_irqsave(&rtas
.lock
, flags
);
543 enter_rtas(__pa(&rtas
.args
));
546 args
.rets
= &args
.args
[nargs
];
548 /* A -1 return code indicates that the last command couldn't
549 be completed due to a hardware error. */
550 if (args
.rets
[0] == -1) {
551 err_rc
= __fetch_rtas_last_error();
552 if ((err_rc
== 0) && buff_copy
) {
553 memcpy(buff_copy
, rtas_err_buf
, RTAS_ERROR_LOG_MAX
);
557 spin_unlock_irqrestore(&rtas
.lock
, flags
);
560 if ((args
.rets
[0] == -1) && (err_rc
== 0)) {
561 log_error(buff_copy
, ERR_TYPE_RTAS_LOG
, 0);
567 if (copy_to_user(uargs
->args
+ nargs
,
569 args
.nret
* sizeof(rtas_arg_t
)) != 0)
575 /* This version can't take the spinlock, because it never returns */
577 struct rtas_args rtas_stop_self_args
= {
578 /* The token is initialized for real in setup_system() */
579 .token
= RTAS_UNKNOWN_SERVICE
,
582 .rets
= &rtas_stop_self_args
.args
[0],
585 void rtas_stop_self(void)
587 struct rtas_args
*rtas_args
= &rtas_stop_self_args
;
591 BUG_ON(rtas_args
->token
== RTAS_UNKNOWN_SERVICE
);
593 printk("cpu %u (hwid %u) Ready to die...\n",
594 smp_processor_id(), hard_smp_processor_id());
595 enter_rtas(__pa(rtas_args
));
597 panic("Alas, I survived.\n");
601 * Call early during boot, before mem init or bootmem, to retreive the RTAS
602 * informations from the device-tree and allocate the RMO buffer for userland
605 void __init
rtas_initialize(void)
607 /* Get RTAS dev node and fill up our "rtas" structure with infos
610 rtas
.dev
= of_find_node_by_name(NULL
, "rtas");
615 basep
= (u32
*)get_property(rtas
.dev
, "linux,rtas-base", NULL
);
616 sizep
= (u32
*)get_property(rtas
.dev
, "rtas-size", NULL
);
617 if (basep
!= NULL
&& sizep
!= NULL
) {
620 entryp
= (u32
*)get_property(rtas
.dev
, "linux,rtas-entry", NULL
);
621 if (entryp
== NULL
) /* Ugh */
622 rtas
.entry
= rtas
.base
;
624 rtas
.entry
= *entryp
;
628 /* If RTAS was found, allocate the RMO buffer for it and look for
629 * the stop-self token if any
632 unsigned long rtas_region
= RTAS_INSTANTIATE_MAX
;
633 if (systemcfg
->platform
== PLATFORM_PSERIES_LPAR
)
634 rtas_region
= min(lmb
.rmo_size
, RTAS_INSTANTIATE_MAX
);
636 rtas_rmo_buf
= lmb_alloc_base(RTAS_RMOBUF_MAX
, PAGE_SIZE
,
639 #ifdef CONFIG_HOTPLUG_CPU
640 rtas_stop_self_args
.token
= rtas_token("stop-self");
641 #endif /* CONFIG_HOTPLUG_CPU */
647 EXPORT_SYMBOL(rtas_firmware_flash_list
);
648 EXPORT_SYMBOL(rtas_token
);
649 EXPORT_SYMBOL(rtas_call
);
650 EXPORT_SYMBOL(rtas_data_buf
);
651 EXPORT_SYMBOL(rtas_data_buf_lock
);
652 EXPORT_SYMBOL(rtas_extended_busy_delay_time
);
653 EXPORT_SYMBOL(rtas_get_sensor
);
654 EXPORT_SYMBOL(rtas_get_power_level
);
655 EXPORT_SYMBOL(rtas_set_power_level
);
656 EXPORT_SYMBOL(rtas_set_indicator
);
657 EXPORT_SYMBOL(rtas_get_error_log_max
);