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>
20 #include <linux/capability.h>
21 #include <linux/delay.h>
25 #include <asm/hvcall.h>
26 #include <asm/semaphore.h>
27 #include <asm/machdep.h>
29 #include <asm/param.h>
30 #include <asm/system.h>
31 #include <asm/delay.h>
32 #include <asm/uaccess.h>
36 struct rtas_t rtas = {
37 .lock = SPIN_LOCK_UNLOCKED
40 struct rtas_suspend_me_data {
42 struct rtas_args *args;
47 DEFINE_SPINLOCK(rtas_data_buf_lock);
48 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
49 unsigned long rtas_rmo_buf;
52 * If non-NULL, this gets called when the kernel terminates.
53 * This is done like this so rtas_flash can be a module.
55 void (*rtas_flash_term_hook)(int);
56 EXPORT_SYMBOL(rtas_flash_term_hook);
59 * call_rtas_display_status and call_rtas_display_status_delay
60 * are designed only for very early low-level debugging, which
61 * is why the token is hard-coded to 10.
63 static void call_rtas_display_status(char c)
65 struct rtas_args *args = &rtas.args;
70 spin_lock_irqsave(&rtas.lock, s);
75 args->rets = (rtas_arg_t *)&(args->args[1]);
76 args->args[0] = (unsigned char)c;
78 enter_rtas(__pa(args));
80 spin_unlock_irqrestore(&rtas.lock, s);
83 static void call_rtas_display_status_delay(char c)
85 static int pending_newline = 0; /* did last write end with unprinted newline? */
86 static int width = 16;
90 call_rtas_display_status(' ');
95 if (pending_newline) {
96 call_rtas_display_status('\r');
97 call_rtas_display_status('\n');
101 call_rtas_display_status(c);
107 void __init udbg_init_rtas(void)
109 udbg_putc = call_rtas_display_status_delay;
112 void rtas_progress(char *s, unsigned short hex)
114 struct device_node *root;
117 static int display_character, set_indicator;
118 static int display_width, display_lines, *row_width, form_feed;
119 static DEFINE_SPINLOCK(progress_lock);
120 static int current_line;
121 static int pending_newline = 0; /* did last write end with unprinted newline? */
126 if (display_width == 0) {
127 display_width = 0x10;
128 if ((root = find_path_device("/rtas"))) {
129 if ((p = (unsigned int *)get_property(root,
130 "ibm,display-line-length", NULL)))
132 if ((p = (unsigned int *)get_property(root,
133 "ibm,form-feed", NULL)))
135 if ((p = (unsigned int *)get_property(root,
136 "ibm,display-number-of-lines", NULL)))
138 row_width = (unsigned int *)get_property(root,
139 "ibm,display-truncation-length", NULL);
141 display_character = rtas_token("display-character");
142 set_indicator = rtas_token("set-indicator");
145 if (display_character == RTAS_UNKNOWN_SERVICE) {
146 /* use hex display if available */
147 if (set_indicator != RTAS_UNKNOWN_SERVICE)
148 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
152 spin_lock(&progress_lock);
155 * Last write ended with newline, but we didn't print it since
156 * it would just clear the bottom line of output. Print it now
159 * If no newline is pending and form feed is supported, clear the
160 * display with a form feed; otherwise, print a CR to start output
161 * at the beginning of the line.
163 if (pending_newline) {
164 rtas_call(display_character, 1, 1, NULL, '\r');
165 rtas_call(display_character, 1, 1, NULL, '\n');
170 rtas_call(display_character, 1, 1, NULL,
173 rtas_call(display_character, 1, 1, NULL, '\r');
177 width = row_width[current_line];
179 width = display_width;
182 if (*os == '\n' || *os == '\r') {
183 /* If newline is the last character, save it
184 * until next call to avoid bumping up the
187 if (*os == '\n' && !os[1]) {
190 if (current_line > display_lines-1)
191 current_line = display_lines-1;
192 spin_unlock(&progress_lock);
196 /* RTAS wants CR-LF, not just LF */
199 rtas_call(display_character, 1, 1, NULL, '\r');
200 rtas_call(display_character, 1, 1, NULL, '\n');
202 /* CR might be used to re-draw a line, so we'll
203 * leave it alone and not add LF.
205 rtas_call(display_character, 1, 1, NULL, *os);
209 width = row_width[current_line];
211 width = display_width;
214 rtas_call(display_character, 1, 1, NULL, *os);
219 /* if we overwrite the screen length */
221 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
225 spin_unlock(&progress_lock);
227 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
229 int rtas_token(const char *service)
232 if (rtas.dev == NULL)
233 return RTAS_UNKNOWN_SERVICE;
234 tokp = (int *) get_property(rtas.dev, service, NULL);
235 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
238 #ifdef CONFIG_RTAS_ERROR_LOGGING
240 * Return the firmware-specified size of the error log buffer
241 * for all rtas calls that require an error buffer argument.
242 * This includes 'check-exception' and 'rtas-last-error'.
244 int rtas_get_error_log_max(void)
246 static int rtas_error_log_max;
247 if (rtas_error_log_max)
248 return rtas_error_log_max;
250 rtas_error_log_max = rtas_token ("rtas-error-log-max");
251 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
252 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
253 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
255 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
257 return rtas_error_log_max;
259 EXPORT_SYMBOL(rtas_get_error_log_max);
262 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
263 int rtas_last_error_token;
265 /** Return a copy of the detailed error text associated with the
266 * most recent failed call to rtas. Because the error text
267 * might go stale if there are any other intervening rtas calls,
268 * this routine must be called atomically with whatever produced
269 * the error (i.e. with rtas.lock still held from the previous call).
271 static char *__fetch_rtas_last_error(char *altbuf)
273 struct rtas_args err_args, save_args;
277 if (rtas_last_error_token == -1)
280 bufsz = rtas_get_error_log_max();
282 err_args.token = rtas_last_error_token;
285 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
286 err_args.args[1] = bufsz;
287 err_args.args[2] = 0;
289 save_args = rtas.args;
290 rtas.args = err_args;
292 enter_rtas(__pa(&rtas.args));
294 err_args = rtas.args;
295 rtas.args = save_args;
297 /* Log the error in the unlikely case that there was one. */
298 if (unlikely(err_args.args[2] == 0)) {
304 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
307 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
313 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
315 #else /* CONFIG_RTAS_ERROR_LOGGING */
316 #define __fetch_rtas_last_error(x) NULL
317 #define get_errorlog_buffer() NULL
320 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
325 struct rtas_args *rtas_args;
326 char *buff_copy = NULL;
329 if (token == RTAS_UNKNOWN_SERVICE)
332 /* Gotta do something different here, use global lock for now... */
333 spin_lock_irqsave(&rtas.lock, s);
334 rtas_args = &rtas.args;
336 rtas_args->token = token;
337 rtas_args->nargs = nargs;
338 rtas_args->nret = nret;
339 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
340 va_start(list, outputs);
341 for (i = 0; i < nargs; ++i)
342 rtas_args->args[i] = va_arg(list, rtas_arg_t);
345 for (i = 0; i < nret; ++i)
346 rtas_args->rets[i] = 0;
348 enter_rtas(__pa(rtas_args));
350 /* A -1 return code indicates that the last command couldn't
351 be completed due to a hardware error. */
352 if (rtas_args->rets[0] == -1)
353 buff_copy = __fetch_rtas_last_error(NULL);
355 if (nret > 1 && outputs != NULL)
356 for (i = 0; i < nret-1; ++i)
357 outputs[i] = rtas_args->rets[i+1];
358 ret = (nret > 0)? rtas_args->rets[0]: 0;
360 /* Gotta do something different here, use global lock for now... */
361 spin_unlock_irqrestore(&rtas.lock, s);
364 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
371 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
372 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
374 unsigned int rtas_extended_busy_delay_time(int status)
376 int order = status - 9900;
380 order = 0; /* RTC depends on this for -2 clock busy */
382 order = 5; /* bound */
384 /* Use microseconds for reasonable accuracy */
385 for (ms = 1; order > 0; order--)
391 int rtas_error_rc(int rtas_rc)
396 case -1: /* Hardware Error */
399 case -3: /* Bad indicator/domain/etc */
402 case -9000: /* Isolation error */
405 case -9001: /* Outstanding TCE/PTE */
408 case -9002: /* No usable slot */
412 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
413 __FUNCTION__, rtas_rc);
420 int rtas_get_power_level(int powerdomain, int *level)
422 int token = rtas_token("get-power-level");
425 if (token == RTAS_UNKNOWN_SERVICE)
428 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
432 return rtas_error_rc(rc);
436 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
438 int token = rtas_token("set-power-level");
439 unsigned int wait_time;
442 if (token == RTAS_UNKNOWN_SERVICE)
446 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
449 else if (rtas_is_extended_busy(rc)) {
450 wait_time = rtas_extended_busy_delay_time(rc);
451 udelay(wait_time * 1000);
457 return rtas_error_rc(rc);
461 int rtas_get_sensor(int sensor, int index, int *state)
463 int token = rtas_token("get-sensor-state");
464 unsigned int wait_time;
467 if (token == RTAS_UNKNOWN_SERVICE)
471 rc = rtas_call(token, 2, 2, state, sensor, index);
474 else if (rtas_is_extended_busy(rc)) {
475 wait_time = rtas_extended_busy_delay_time(rc);
476 udelay(wait_time * 1000);
482 return rtas_error_rc(rc);
486 int rtas_set_indicator(int indicator, int index, int new_value)
488 int token = rtas_token("set-indicator");
489 unsigned int wait_time;
492 if (token == RTAS_UNKNOWN_SERVICE)
496 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
499 else if (rtas_is_extended_busy(rc)) {
500 wait_time = rtas_extended_busy_delay_time(rc);
501 udelay(wait_time * 1000);
508 return rtas_error_rc(rc);
512 void rtas_restart(char *cmd)
514 if (rtas_flash_term_hook)
515 rtas_flash_term_hook(SYS_RESTART);
516 printk("RTAS system-reboot returned %d\n",
517 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
521 void rtas_power_off(void)
523 if (rtas_flash_term_hook)
524 rtas_flash_term_hook(SYS_POWER_OFF);
525 /* allow power on only with power button press */
526 printk("RTAS power-off returned %d\n",
527 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
533 if (rtas_flash_term_hook)
534 rtas_flash_term_hook(SYS_HALT);
535 /* allow power on only with power button press */
536 printk("RTAS power-off returned %d\n",
537 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
541 /* Must be in the RMO region, so we place it here */
542 static char rtas_os_term_buf[2048];
544 void rtas_os_term(char *str)
548 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
551 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
554 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
555 __pa(rtas_os_term_buf));
557 if (status == RTAS_BUSY)
559 else if (status != 0)
560 printk(KERN_EMERG "ibm,os-term call failed %d\n",
562 } while (status == RTAS_BUSY);
565 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
566 #ifdef CONFIG_PPC_PSERIES
567 static void rtas_percpu_suspend_me(void *info)
572 struct rtas_suspend_me_data *data =
573 (struct rtas_suspend_me_data *)info;
576 * We use "waiting" to indicate our state. As long
577 * as it is >0, we are still trying to all join up.
578 * If it goes to 0, we have successfully joined up and
579 * one thread got H_Continue. If any error happens,
582 local_irq_save(flags);
584 rc = plpar_hcall_norets(H_JOIN);
586 } while (rc == H_Success && data->waiting > 0);
590 if (rc == H_Continue) {
592 data->args->args[data->args->nargs] =
593 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
595 plpar_hcall_norets(H_PROD,i);
597 data->waiting = -EBUSY;
598 printk(KERN_ERR "Error on H_Join hypervisor call\n");
602 local_irq_restore(flags);
606 static int rtas_ibm_suspend_me(struct rtas_args *args)
610 struct rtas_suspend_me_data data;
615 /* Call function on all CPUs. One of us will make the
618 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
619 data.waiting = -EINVAL;
621 if (data.waiting != 0)
622 printk(KERN_ERR "Error doing global join\n");
624 /* Prod each CPU. This won't hurt, and will wake
625 * anyone we successfully put to sleep with H_Join
628 plpar_hcall_norets(H_PROD, i);
632 #else /* CONFIG_PPC_PSERIES */
633 static int rtas_ibm_suspend_me(struct rtas_args *args)
639 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
641 struct rtas_args args;
643 char *buff_copy, *errbuf = NULL;
647 if (!capable(CAP_SYS_ADMIN))
650 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
654 if (nargs > ARRAY_SIZE(args.args)
655 || args.nret > ARRAY_SIZE(args.args)
656 || nargs + args.nret > ARRAY_SIZE(args.args))
660 if (copy_from_user(args.args, uargs->args,
661 nargs * sizeof(rtas_arg_t)) != 0)
664 if (args.token == RTAS_UNKNOWN_SERVICE)
667 /* Need to handle ibm,suspend_me call specially */
668 if (args.token == ibm_suspend_me_token) {
669 rc = rtas_ibm_suspend_me(&args);
675 buff_copy = get_errorlog_buffer();
677 spin_lock_irqsave(&rtas.lock, flags);
680 enter_rtas(__pa(&rtas.args));
683 args.rets = &args.args[nargs];
685 /* A -1 return code indicates that the last command couldn't
686 be completed due to a hardware error. */
687 if (args.rets[0] == -1)
688 errbuf = __fetch_rtas_last_error(buff_copy);
690 spin_unlock_irqrestore(&rtas.lock, flags);
694 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
700 if (copy_to_user(uargs->args + nargs,
702 args.nret * sizeof(rtas_arg_t)) != 0)
708 /* This version can't take the spinlock, because it never returns */
710 struct rtas_args rtas_stop_self_args = {
711 /* The token is initialized for real in setup_system() */
712 .token = RTAS_UNKNOWN_SERVICE,
715 .rets = &rtas_stop_self_args.args[0],
718 void rtas_stop_self(void)
720 struct rtas_args *rtas_args = &rtas_stop_self_args;
724 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
726 printk("cpu %u (hwid %u) Ready to die...\n",
727 smp_processor_id(), hard_smp_processor_id());
728 enter_rtas(__pa(rtas_args));
730 panic("Alas, I survived.\n");
734 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
735 * informations from the device-tree and allocate the RMO buffer for userland
738 void __init rtas_initialize(void)
740 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
742 /* Get RTAS dev node and fill up our "rtas" structure with infos
745 rtas.dev = of_find_node_by_name(NULL, "rtas");
750 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
751 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
752 if (basep != NULL && sizep != NULL) {
755 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
756 if (entryp == NULL) /* Ugh */
757 rtas.entry = rtas.base;
759 rtas.entry = *entryp;
766 /* If RTAS was found, allocate the RMO buffer for it and look for
767 * the stop-self token if any
770 if (_machine == PLATFORM_PSERIES_LPAR) {
771 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
772 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
775 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
777 #ifdef CONFIG_HOTPLUG_CPU
778 rtas_stop_self_args.token = rtas_token("stop-self");
779 #endif /* CONFIG_HOTPLUG_CPU */
780 #ifdef CONFIG_RTAS_ERROR_LOGGING
781 rtas_last_error_token = rtas_token("rtas-last-error");
786 EXPORT_SYMBOL(rtas_token);
787 EXPORT_SYMBOL(rtas_call);
788 EXPORT_SYMBOL(rtas_data_buf);
789 EXPORT_SYMBOL(rtas_data_buf_lock);
790 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
791 EXPORT_SYMBOL(rtas_get_sensor);
792 EXPORT_SYMBOL(rtas_get_power_level);
793 EXPORT_SYMBOL(rtas_set_power_level);
794 EXPORT_SYMBOL(rtas_set_indicator);