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 display_width, display_lines, *row_width, form_feed;
102 static DEFINE_SPINLOCK(progress_lock);
103 static int current_line;
104 static int pending_newline = 0; /* did last write end with unprinted newline? */
109 if (display_width == 0) {
110 display_width = 0x10;
111 if ((root = find_path_device("/rtas"))) {
112 if ((p = (unsigned int *)get_property(root,
113 "ibm,display-line-length", NULL)))
115 if ((p = (unsigned int *)get_property(root,
116 "ibm,form-feed", NULL)))
118 if ((p = (unsigned int *)get_property(root,
119 "ibm,display-number-of-lines", NULL)))
121 row_width = (unsigned int *)get_property(root,
122 "ibm,display-truncation-length", NULL);
124 display_character = rtas_token("display-character");
125 set_indicator = rtas_token("set-indicator");
128 if (display_character == RTAS_UNKNOWN_SERVICE) {
129 /* use hex display if available */
130 if (set_indicator != RTAS_UNKNOWN_SERVICE)
131 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
135 spin_lock(&progress_lock);
138 * Last write ended with newline, but we didn't print it since
139 * it would just clear the bottom line of output. Print it now
142 * If no newline is pending and form feed is supported, clear the
143 * display with a form feed; otherwise, print a CR to start output
144 * at the beginning of the line.
146 if (pending_newline) {
147 rtas_call(display_character, 1, 1, NULL, '\r');
148 rtas_call(display_character, 1, 1, NULL, '\n');
153 rtas_call(display_character, 1, 1, NULL,
156 rtas_call(display_character, 1, 1, NULL, '\r');
160 width = row_width[current_line];
162 width = display_width;
165 if (*os == '\n' || *os == '\r') {
166 /* If newline is the last character, save it
167 * until next call to avoid bumping up the
170 if (*os == '\n' && !os[1]) {
173 if (current_line > display_lines-1)
174 current_line = display_lines-1;
175 spin_unlock(&progress_lock);
179 /* RTAS wants CR-LF, not just LF */
182 rtas_call(display_character, 1, 1, NULL, '\r');
183 rtas_call(display_character, 1, 1, NULL, '\n');
185 /* CR might be used to re-draw a line, so we'll
186 * leave it alone and not add LF.
188 rtas_call(display_character, 1, 1, NULL, *os);
192 width = row_width[current_line];
194 width = display_width;
197 rtas_call(display_character, 1, 1, NULL, *os);
202 /* if we overwrite the screen length */
204 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
208 spin_unlock(&progress_lock);
212 rtas_token(const char *service)
215 if (rtas.dev == NULL) {
216 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
217 return RTAS_UNKNOWN_SERVICE;
219 tokp = (int *) get_property(rtas.dev, service, NULL);
220 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
224 * Return the firmware-specified size of the error log buffer
225 * for all rtas calls that require an error buffer argument.
226 * This includes 'check-exception' and 'rtas-last-error'.
228 int rtas_get_error_log_max(void)
230 static int rtas_error_log_max;
231 if (rtas_error_log_max)
232 return rtas_error_log_max;
234 rtas_error_log_max = rtas_token ("rtas-error-log-max");
235 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
236 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
237 printk (KERN_WARNING "RTAS: bad log buffer size %d\n", rtas_error_log_max);
238 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
240 return rtas_error_log_max;
244 /** Return a copy of the detailed error text associated with the
245 * most recent failed call to rtas. Because the error text
246 * might go stale if there are any other intervening rtas calls,
247 * this routine must be called atomically with whatever produced
248 * the error (i.e. with rtas.lock still held from the previous call).
251 __fetch_rtas_last_error(void)
253 struct rtas_args err_args, save_args;
256 bufsz = rtas_get_error_log_max();
258 err_args.token = rtas_token("rtas-last-error");
262 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
263 err_args.args[1] = bufsz;
264 err_args.args[2] = 0;
266 save_args = rtas.args;
267 rtas.args = err_args;
269 enter_rtas(__pa(&rtas.args));
271 err_args = rtas.args;
272 rtas.args = save_args;
274 return err_args.args[2];
277 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
282 struct rtas_args *rtas_args;
283 char * buff_copy = NULL;
286 PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
287 PPCDBG(PPCDBG_RTAS, "\ttoken = 0x%x\n", token);
288 PPCDBG(PPCDBG_RTAS, "\tnargs = %d\n", nargs);
289 PPCDBG(PPCDBG_RTAS, "\tnret = %d\n", nret);
290 PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
291 if (token == RTAS_UNKNOWN_SERVICE)
294 /* Gotta do something different here, use global lock for now... */
295 spin_lock_irqsave(&rtas.lock, s);
296 rtas_args = &rtas.args;
298 rtas_args->token = token;
299 rtas_args->nargs = nargs;
300 rtas_args->nret = nret;
301 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
302 va_start(list, outputs);
303 for (i = 0; i < nargs; ++i) {
304 rtas_args->args[i] = va_arg(list, rtas_arg_t);
305 PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%x\n", i, rtas_args->args[i]);
309 for (i = 0; i < nret; ++i)
310 rtas_args->rets[i] = 0;
312 PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
314 enter_rtas(__pa(rtas_args));
315 PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
317 /* A -1 return code indicates that the last command couldn't
318 be completed due to a hardware error. */
319 if (rtas_args->rets[0] == -1)
320 logit = (__fetch_rtas_last_error() == 0);
322 ifppcdebug(PPCDBG_RTAS) {
323 for(i=0; i < nret ;i++)
324 udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
327 if (nret > 1 && outputs != NULL)
328 for (i = 0; i < nret-1; ++i)
329 outputs[i] = rtas_args->rets[i+1];
330 ret = (nret > 0)? rtas_args->rets[0]: 0;
332 /* Log the error in the unlikely case that there was one. */
333 if (unlikely(logit)) {
334 buff_copy = rtas_err_buf;
336 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
338 memcpy(buff_copy, rtas_err_buf,
343 /* Gotta do something different here, use global lock for now... */
344 spin_unlock_irqrestore(&rtas.lock, s);
347 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
354 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
355 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
358 rtas_extended_busy_delay_time(int status)
360 int order = status - 9900;
364 order = 0; /* RTC depends on this for -2 clock busy */
366 order = 5; /* bound */
368 /* Use microseconds for reasonable accuracy */
369 for (ms=1; order > 0; order--)
375 int rtas_error_rc(int rtas_rc)
380 case -1: /* Hardware Error */
383 case -3: /* Bad indicator/domain/etc */
386 case -9000: /* Isolation error */
389 case -9001: /* Outstanding TCE/PTE */
392 case -9002: /* No usable slot */
396 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
397 __FUNCTION__, rtas_rc);
404 int rtas_get_power_level(int powerdomain, int *level)
406 int token = rtas_token("get-power-level");
409 if (token == RTAS_UNKNOWN_SERVICE)
412 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
416 return rtas_error_rc(rc);
420 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
422 int token = rtas_token("set-power-level");
423 unsigned int wait_time;
426 if (token == RTAS_UNKNOWN_SERVICE)
430 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
433 else if (rtas_is_extended_busy(rc)) {
434 wait_time = rtas_extended_busy_delay_time(rc);
435 udelay(wait_time * 1000);
441 return rtas_error_rc(rc);
445 int rtas_get_sensor(int sensor, int index, int *state)
447 int token = rtas_token("get-sensor-state");
448 unsigned int wait_time;
451 if (token == RTAS_UNKNOWN_SERVICE)
455 rc = rtas_call(token, 2, 2, state, sensor, index);
458 else if (rtas_is_extended_busy(rc)) {
459 wait_time = rtas_extended_busy_delay_time(rc);
460 udelay(wait_time * 1000);
466 return rtas_error_rc(rc);
470 int rtas_set_indicator(int indicator, int index, int new_value)
472 int token = rtas_token("set-indicator");
473 unsigned int wait_time;
476 if (token == RTAS_UNKNOWN_SERVICE)
480 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
483 else if (rtas_is_extended_busy(rc)) {
484 wait_time = rtas_extended_busy_delay_time(rc);
485 udelay(wait_time * 1000);
492 return rtas_error_rc(rc);
496 #define FLASH_BLOCK_LIST_VERSION (1UL)
498 rtas_flash_firmware(void)
500 unsigned long image_size;
501 struct flash_block_list *f, *next, *flist;
502 unsigned long rtas_block_list;
503 int i, status, update_token;
505 update_token = rtas_token("ibm,update-flash-64-and-reboot");
506 if (update_token == RTAS_UNKNOWN_SERVICE) {
507 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
508 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
512 /* NOTE: the "first" block list is a global var with no data
513 * blocks in the kernel data segment. We do this because
514 * we want to ensure this block_list addr is under 4GB.
516 rtas_firmware_flash_list.num_blocks = 0;
517 flist = (struct flash_block_list *)&rtas_firmware_flash_list;
518 rtas_block_list = virt_to_abs(flist);
519 if (rtas_block_list >= 4UL*1024*1024*1024) {
520 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
524 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
525 /* Update the block_list in place. */
527 for (f = flist; f; f = next) {
528 /* Translate data addrs to absolute */
529 for (i = 0; i < f->num_blocks; i++) {
530 f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
531 image_size += f->blocks[i].length;
534 /* Don't translate NULL pointer for last entry */
536 f->next = (struct flash_block_list *)virt_to_abs(f->next);
539 /* make num_blocks into the version/length field */
540 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
543 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
544 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
545 rtas_progress("Flashing \n", 0x0);
546 rtas_progress("Please Wait... ", 0x0);
547 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
548 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
549 switch (status) { /* should only get "bad" status */
551 printk(KERN_ALERT "FLASH: success\n");
554 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
557 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
560 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
563 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
568 void rtas_flash_bypass_warning(void)
570 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
571 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
576 rtas_restart(char *cmd)
578 if (rtas_firmware_flash_list.next)
579 rtas_flash_firmware();
581 printk("RTAS system-reboot returned %d\n",
582 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
589 if (rtas_firmware_flash_list.next)
590 rtas_flash_bypass_warning();
591 /* allow power on only with power button press */
592 printk("RTAS power-off returned %d\n",
593 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
600 if (rtas_firmware_flash_list.next)
601 rtas_flash_bypass_warning();
605 /* Must be in the RMO region, so we place it here */
606 static char rtas_os_term_buf[2048];
608 void rtas_os_term(char *str)
612 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
615 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
618 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
619 __pa(rtas_os_term_buf));
621 if (status == RTAS_BUSY)
623 else if (status != 0)
624 printk(KERN_EMERG "ibm,os-term call failed %d\n",
626 } while (status == RTAS_BUSY);
630 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
632 struct rtas_args args;
638 if (!capable(CAP_SYS_ADMIN))
641 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
645 if (nargs > ARRAY_SIZE(args.args)
646 || args.nret > ARRAY_SIZE(args.args)
647 || nargs + args.nret > ARRAY_SIZE(args.args))
651 if (copy_from_user(args.args, uargs->args,
652 nargs * sizeof(rtas_arg_t)) != 0)
655 buff_copy = kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL);
657 spin_lock_irqsave(&rtas.lock, flags);
660 enter_rtas(__pa(&rtas.args));
663 args.rets = &args.args[nargs];
665 /* A -1 return code indicates that the last command couldn't
666 be completed due to a hardware error. */
667 if (args.rets[0] == -1) {
668 err_rc = __fetch_rtas_last_error();
669 if ((err_rc == 0) && buff_copy) {
670 memcpy(buff_copy, rtas_err_buf, RTAS_ERROR_LOG_MAX);
674 spin_unlock_irqrestore(&rtas.lock, flags);
677 if ((args.rets[0] == -1) && (err_rc == 0)) {
678 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
684 if (copy_to_user(uargs->args + nargs,
686 args.nret * sizeof(rtas_arg_t)) != 0)
692 /* This version can't take the spinlock, because it never returns */
694 struct rtas_args rtas_stop_self_args = {
695 /* The token is initialized for real in setup_system() */
696 .token = RTAS_UNKNOWN_SERVICE,
699 .rets = &rtas_stop_self_args.args[0],
702 void rtas_stop_self(void)
704 struct rtas_args *rtas_args = &rtas_stop_self_args;
708 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
710 printk("cpu %u (hwid %u) Ready to die...\n",
711 smp_processor_id(), hard_smp_processor_id());
712 enter_rtas(__pa(rtas_args));
714 panic("Alas, I survived.\n");
718 * Call early during boot, before mem init or bootmem, to retreive the RTAS
719 * informations from the device-tree and allocate the RMO buffer for userland
722 void __init rtas_initialize(void)
724 /* Get RTAS dev node and fill up our "rtas" structure with infos
727 rtas.dev = of_find_node_by_name(NULL, "rtas");
732 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
733 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
734 if (basep != NULL && sizep != NULL) {
737 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
738 if (entryp == NULL) /* Ugh */
739 rtas.entry = rtas.base;
741 rtas.entry = *entryp;
745 /* If RTAS was found, allocate the RMO buffer for it and look for
746 * the stop-self token if any
749 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
750 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
751 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
753 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
756 #ifdef CONFIG_HOTPLUG_CPU
757 rtas_stop_self_args.token = rtas_token("stop-self");
758 #endif /* CONFIG_HOTPLUG_CPU */
764 EXPORT_SYMBOL(rtas_firmware_flash_list);
765 EXPORT_SYMBOL(rtas_token);
766 EXPORT_SYMBOL(rtas_call);
767 EXPORT_SYMBOL(rtas_data_buf);
768 EXPORT_SYMBOL(rtas_data_buf_lock);
769 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
770 EXPORT_SYMBOL(rtas_get_sensor);
771 EXPORT_SYMBOL(rtas_get_power_level);
772 EXPORT_SYMBOL(rtas_set_power_level);
773 EXPORT_SYMBOL(rtas_set_indicator);
774 EXPORT_SYMBOL(rtas_get_error_log_max);