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_progress(char *s, unsigned short hex)
97 struct device_node *root;
100 static int display_character, set_indicator;
101 static int max_width;
102 static DEFINE_SPINLOCK(progress_lock);
103 static int pending_newline = 0; /* did last write end with unprinted newline? */
108 if (max_width == 0) {
109 if ((root = find_path_device("/rtas")) &&
110 (p = (unsigned int *)get_property(root,
111 "ibm,display-line-length",
116 display_character = rtas_token("display-character");
117 set_indicator = rtas_token("set-indicator");
120 if (display_character == RTAS_UNKNOWN_SERVICE) {
121 /* use hex display if available */
122 if (set_indicator != RTAS_UNKNOWN_SERVICE)
123 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
127 spin_lock(&progress_lock);
130 * Last write ended with newline, but we didn't print it since
131 * it would just clear the bottom line of output. Print it now
134 * If no newline is pending, print a CR to start output at the
135 * beginning of the line.
137 if (pending_newline) {
138 rtas_call(display_character, 1, 1, NULL, '\r');
139 rtas_call(display_character, 1, 1, NULL, '\n');
142 rtas_call(display_character, 1, 1, NULL, '\r');
148 if (*os == '\n' || *os == '\r') {
149 /* Blank to end of line. */
151 rtas_call(display_character, 1, 1, NULL, ' ');
153 /* If newline is the last character, save it
154 * until next call to avoid bumping up the
157 if (*os == '\n' && !os[1]) {
159 spin_unlock(&progress_lock);
163 /* RTAS wants CR-LF, not just LF */
166 rtas_call(display_character, 1, 1, NULL, '\r');
167 rtas_call(display_character, 1, 1, NULL, '\n');
169 /* CR might be used to re-draw a line, so we'll
170 * leave it alone and not add LF.
172 rtas_call(display_character, 1, 1, NULL, *os);
178 rtas_call(display_character, 1, 1, NULL, *os);
183 /* if we overwrite the screen length */
185 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
189 /* Blank to end of line. */
191 rtas_call(display_character, 1, 1, NULL, ' ');
193 spin_unlock(&progress_lock);
197 rtas_token(const char *service)
200 if (rtas.dev == NULL) {
201 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
202 return RTAS_UNKNOWN_SERVICE;
204 tokp = (int *) get_property(rtas.dev, service, NULL);
205 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
209 * Return the firmware-specified size of the error log buffer
210 * for all rtas calls that require an error buffer argument.
211 * This includes 'check-exception' and 'rtas-last-error'.
213 int rtas_get_error_log_max(void)
215 static int rtas_error_log_max;
216 if (rtas_error_log_max)
217 return rtas_error_log_max;
219 rtas_error_log_max = rtas_token ("rtas-error-log-max");
220 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
221 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
222 printk (KERN_WARNING "RTAS: bad log buffer size %d\n", rtas_error_log_max);
223 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
225 return rtas_error_log_max;
229 /** Return a copy of the detailed error text associated with the
230 * most recent failed call to rtas. Because the error text
231 * might go stale if there are any other intervening rtas calls,
232 * this routine must be called atomically with whatever produced
233 * the error (i.e. with rtas.lock still held from the previous call).
236 __fetch_rtas_last_error(void)
238 struct rtas_args err_args, save_args;
241 bufsz = rtas_get_error_log_max();
243 err_args.token = rtas_token("rtas-last-error");
247 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
248 err_args.args[1] = bufsz;
249 err_args.args[2] = 0;
251 save_args = rtas.args;
252 rtas.args = err_args;
254 enter_rtas(__pa(&rtas.args));
256 err_args = rtas.args;
257 rtas.args = save_args;
259 return err_args.args[2];
262 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
267 struct rtas_args *rtas_args;
268 char * buff_copy = NULL;
271 PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
272 PPCDBG(PPCDBG_RTAS, "\ttoken = 0x%x\n", token);
273 PPCDBG(PPCDBG_RTAS, "\tnargs = %d\n", nargs);
274 PPCDBG(PPCDBG_RTAS, "\tnret = %d\n", nret);
275 PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
276 if (token == RTAS_UNKNOWN_SERVICE)
279 /* Gotta do something different here, use global lock for now... */
280 spin_lock_irqsave(&rtas.lock, s);
281 rtas_args = &rtas.args;
283 rtas_args->token = token;
284 rtas_args->nargs = nargs;
285 rtas_args->nret = nret;
286 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
287 va_start(list, outputs);
288 for (i = 0; i < nargs; ++i) {
289 rtas_args->args[i] = va_arg(list, rtas_arg_t);
290 PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%x\n", i, rtas_args->args[i]);
294 for (i = 0; i < nret; ++i)
295 rtas_args->rets[i] = 0;
297 PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
299 enter_rtas(__pa(rtas_args));
300 PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
302 /* A -1 return code indicates that the last command couldn't
303 be completed due to a hardware error. */
304 if (rtas_args->rets[0] == -1)
305 logit = (__fetch_rtas_last_error() == 0);
307 ifppcdebug(PPCDBG_RTAS) {
308 for(i=0; i < nret ;i++)
309 udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
312 if (nret > 1 && outputs != NULL)
313 for (i = 0; i < nret-1; ++i)
314 outputs[i] = rtas_args->rets[i+1];
315 ret = (nret > 0)? rtas_args->rets[0]: 0;
317 /* Log the error in the unlikely case that there was one. */
318 if (unlikely(logit)) {
319 buff_copy = rtas_err_buf;
321 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
323 memcpy(buff_copy, rtas_err_buf,
328 /* Gotta do something different here, use global lock for now... */
329 spin_unlock_irqrestore(&rtas.lock, s);
332 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
339 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
340 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
343 rtas_extended_busy_delay_time(int status)
345 int order = status - 9900;
349 order = 0; /* RTC depends on this for -2 clock busy */
351 order = 5; /* bound */
353 /* Use microseconds for reasonable accuracy */
354 for (ms=1; order > 0; order--)
360 int rtas_error_rc(int rtas_rc)
365 case -1: /* Hardware Error */
368 case -3: /* Bad indicator/domain/etc */
371 case -9000: /* Isolation error */
374 case -9001: /* Outstanding TCE/PTE */
377 case -9002: /* No usable slot */
381 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
382 __FUNCTION__, rtas_rc);
389 int rtas_get_power_level(int powerdomain, int *level)
391 int token = rtas_token("get-power-level");
394 if (token == RTAS_UNKNOWN_SERVICE)
397 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
401 return rtas_error_rc(rc);
405 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
407 int token = rtas_token("set-power-level");
408 unsigned int wait_time;
411 if (token == RTAS_UNKNOWN_SERVICE)
415 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
418 else if (rtas_is_extended_busy(rc)) {
419 wait_time = rtas_extended_busy_delay_time(rc);
420 udelay(wait_time * 1000);
426 return rtas_error_rc(rc);
430 int rtas_get_sensor(int sensor, int index, int *state)
432 int token = rtas_token("get-sensor-state");
433 unsigned int wait_time;
436 if (token == RTAS_UNKNOWN_SERVICE)
440 rc = rtas_call(token, 2, 2, state, sensor, index);
443 else if (rtas_is_extended_busy(rc)) {
444 wait_time = rtas_extended_busy_delay_time(rc);
445 udelay(wait_time * 1000);
451 return rtas_error_rc(rc);
455 int rtas_set_indicator(int indicator, int index, int new_value)
457 int token = rtas_token("set-indicator");
458 unsigned int wait_time;
461 if (token == RTAS_UNKNOWN_SERVICE)
465 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
468 else if (rtas_is_extended_busy(rc)) {
469 wait_time = rtas_extended_busy_delay_time(rc);
470 udelay(wait_time * 1000);
477 return rtas_error_rc(rc);
481 #define FLASH_BLOCK_LIST_VERSION (1UL)
483 rtas_flash_firmware(void)
485 unsigned long image_size;
486 struct flash_block_list *f, *next, *flist;
487 unsigned long rtas_block_list;
488 int i, status, update_token;
490 update_token = rtas_token("ibm,update-flash-64-and-reboot");
491 if (update_token == RTAS_UNKNOWN_SERVICE) {
492 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
493 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
497 /* NOTE: the "first" block list is a global var with no data
498 * blocks in the kernel data segment. We do this because
499 * we want to ensure this block_list addr is under 4GB.
501 rtas_firmware_flash_list.num_blocks = 0;
502 flist = (struct flash_block_list *)&rtas_firmware_flash_list;
503 rtas_block_list = virt_to_abs(flist);
504 if (rtas_block_list >= 4UL*1024*1024*1024) {
505 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
509 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
510 /* Update the block_list in place. */
512 for (f = flist; f; f = next) {
513 /* Translate data addrs to absolute */
514 for (i = 0; i < f->num_blocks; i++) {
515 f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
516 image_size += f->blocks[i].length;
519 /* Don't translate NULL pointer for last entry */
521 f->next = (struct flash_block_list *)virt_to_abs(f->next);
524 /* make num_blocks into the version/length field */
525 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
528 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
529 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
530 rtas_progress("Flashing \n", 0x0);
531 rtas_progress("Please Wait... ", 0x0);
532 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
533 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
534 switch (status) { /* should only get "bad" status */
536 printk(KERN_ALERT "FLASH: success\n");
539 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
542 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
545 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
548 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
553 void rtas_flash_bypass_warning(void)
555 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
556 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
561 rtas_restart(char *cmd)
563 if (rtas_firmware_flash_list.next)
564 rtas_flash_firmware();
566 printk("RTAS system-reboot returned %d\n",
567 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
574 if (rtas_firmware_flash_list.next)
575 rtas_flash_bypass_warning();
576 /* allow power on only with power button press */
577 printk("RTAS power-off returned %d\n",
578 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
585 if (rtas_firmware_flash_list.next)
586 rtas_flash_bypass_warning();
590 /* Must be in the RMO region, so we place it here */
591 static char rtas_os_term_buf[2048];
593 void rtas_os_term(char *str)
597 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
600 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
603 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
604 __pa(rtas_os_term_buf));
606 if (status == RTAS_BUSY)
608 else if (status != 0)
609 printk(KERN_EMERG "ibm,os-term call failed %d\n",
611 } while (status == RTAS_BUSY);
615 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
617 struct rtas_args args;
623 if (!capable(CAP_SYS_ADMIN))
626 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
630 if (nargs > ARRAY_SIZE(args.args)
631 || args.nret > ARRAY_SIZE(args.args)
632 || nargs + args.nret > ARRAY_SIZE(args.args))
636 if (copy_from_user(args.args, uargs->args,
637 nargs * sizeof(rtas_arg_t)) != 0)
640 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL);
642 spin_lock_irqsave(&rtas.lock, flags);
645 enter_rtas(__pa(&rtas.args));
648 args.rets = &args.args[nargs];
650 /* A -1 return code indicates that the last command couldn't
651 be completed due to a hardware error. */
652 if (args.rets[0] == -1) {
653 err_rc = __fetch_rtas_last_error();
654 if ((err_rc == 0) && buff_copy) {
655 memcpy(buff_copy, rtas_err_buf, RTAS_ERROR_LOG_MAX);
659 spin_unlock_irqrestore(&rtas.lock, flags);
662 if ((args.rets[0] == -1) && (err_rc == 0)) {
663 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
669 if (copy_to_user(uargs->args + nargs,
671 args.nret * sizeof(rtas_arg_t)) != 0)
677 /* This version can't take the spinlock, because it never returns */
679 struct rtas_args rtas_stop_self_args = {
680 /* The token is initialized for real in setup_system() */
681 .token = RTAS_UNKNOWN_SERVICE,
684 .rets = &rtas_stop_self_args.args[0],
687 void rtas_stop_self(void)
689 struct rtas_args *rtas_args = &rtas_stop_self_args;
693 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
695 printk("cpu %u (hwid %u) Ready to die...\n",
696 smp_processor_id(), hard_smp_processor_id());
697 enter_rtas(__pa(rtas_args));
699 panic("Alas, I survived.\n");
703 * Call early during boot, before mem init or bootmem, to retreive the RTAS
704 * informations from the device-tree and allocate the RMO buffer for userland
707 void __init rtas_initialize(void)
709 /* Get RTAS dev node and fill up our "rtas" structure with infos
712 rtas.dev = of_find_node_by_name(NULL, "rtas");
717 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
718 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
719 if (basep != NULL && sizep != NULL) {
722 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
723 if (entryp == NULL) /* Ugh */
724 rtas.entry = rtas.base;
726 rtas.entry = *entryp;
730 /* If RTAS was found, allocate the RMO buffer for it and look for
731 * the stop-self token if any
734 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
735 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
736 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
738 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
741 #ifdef CONFIG_HOTPLUG_CPU
742 rtas_stop_self_args.token = rtas_token("stop-self");
743 #endif /* CONFIG_HOTPLUG_CPU */
749 EXPORT_SYMBOL(rtas_firmware_flash_list);
750 EXPORT_SYMBOL(rtas_token);
751 EXPORT_SYMBOL(rtas_call);
752 EXPORT_SYMBOL(rtas_data_buf);
753 EXPORT_SYMBOL(rtas_data_buf_lock);
754 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
755 EXPORT_SYMBOL(rtas_get_sensor);
756 EXPORT_SYMBOL(rtas_get_power_level);
757 EXPORT_SYMBOL(rtas_set_power_level);
758 EXPORT_SYMBOL(rtas_set_indicator);
759 EXPORT_SYMBOL(rtas_get_error_log_max);