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>
28 #include <asm/firmware.h>
30 #include <asm/param.h>
31 #include <asm/system.h>
32 #include <asm/delay.h>
33 #include <asm/uaccess.h>
36 #include <asm/syscalls.h>
38 struct rtas_t rtas = {
39 .lock = SPIN_LOCK_UNLOCKED
43 struct rtas_suspend_me_data {
45 struct rtas_args *args;
48 DEFINE_SPINLOCK(rtas_data_buf_lock);
49 EXPORT_SYMBOL(rtas_data_buf_lock);
51 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
52 EXPORT_SYMBOL(rtas_data_buf);
54 unsigned long rtas_rmo_buf;
57 * If non-NULL, this gets called when the kernel terminates.
58 * This is done like this so rtas_flash can be a module.
60 void (*rtas_flash_term_hook)(int);
61 EXPORT_SYMBOL(rtas_flash_term_hook);
64 * call_rtas_display_status and call_rtas_display_status_delay
65 * are designed only for very early low-level debugging, which
66 * is why the token is hard-coded to 10.
68 static void call_rtas_display_status(char c)
70 struct rtas_args *args = &rtas.args;
75 spin_lock_irqsave(&rtas.lock, s);
80 args->rets = (rtas_arg_t *)&(args->args[1]);
81 args->args[0] = (unsigned char)c;
83 enter_rtas(__pa(args));
85 spin_unlock_irqrestore(&rtas.lock, s);
88 static void call_rtas_display_status_delay(char c)
90 static int pending_newline = 0; /* did last write end with unprinted newline? */
91 static int width = 16;
95 call_rtas_display_status(' ');
100 if (pending_newline) {
101 call_rtas_display_status('\r');
102 call_rtas_display_status('\n');
106 call_rtas_display_status(c);
112 void __init udbg_init_rtas_panel(void)
114 udbg_putc = call_rtas_display_status_delay;
117 #ifdef CONFIG_UDBG_RTAS_CONSOLE
119 /* If you think you're dying before early_init_dt_scan_rtas() does its
120 * work, you can hard code the token values for your firmware here and
121 * hardcode rtas.base/entry etc.
123 static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
124 static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
126 static void udbg_rtascon_putc(char c)
133 /* Add CRs before LFs */
135 udbg_rtascon_putc('\r');
137 /* if there is more than one character to be displayed, wait a bit */
138 for (tries = 0; tries < 16; tries++) {
139 if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
145 static int udbg_rtascon_getc_poll(void)
152 if (rtas_call(rtas_getchar_token, 0, 2, &c))
158 static int udbg_rtascon_getc(void)
162 while ((c = udbg_rtascon_getc_poll()) == -1)
169 void __init udbg_init_rtas_console(void)
171 udbg_putc = udbg_rtascon_putc;
172 udbg_getc = udbg_rtascon_getc;
173 udbg_getc_poll = udbg_rtascon_getc_poll;
175 #endif /* CONFIG_UDBG_RTAS_CONSOLE */
177 void rtas_progress(char *s, unsigned short hex)
179 struct device_node *root;
183 static int display_character, set_indicator;
184 static int display_width, display_lines, form_feed;
185 static const int *row_width;
186 static DEFINE_SPINLOCK(progress_lock);
187 static int current_line;
188 static int pending_newline = 0; /* did last write end with unprinted newline? */
193 if (display_width == 0) {
194 display_width = 0x10;
195 if ((root = of_find_node_by_path("/rtas"))) {
196 if ((p = of_get_property(root,
197 "ibm,display-line-length", NULL)))
199 if ((p = of_get_property(root,
200 "ibm,form-feed", NULL)))
202 if ((p = of_get_property(root,
203 "ibm,display-number-of-lines", NULL)))
205 row_width = of_get_property(root,
206 "ibm,display-truncation-length", NULL);
209 display_character = rtas_token("display-character");
210 set_indicator = rtas_token("set-indicator");
213 if (display_character == RTAS_UNKNOWN_SERVICE) {
214 /* use hex display if available */
215 if (set_indicator != RTAS_UNKNOWN_SERVICE)
216 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
220 spin_lock(&progress_lock);
223 * Last write ended with newline, but we didn't print it since
224 * it would just clear the bottom line of output. Print it now
227 * If no newline is pending and form feed is supported, clear the
228 * display with a form feed; otherwise, print a CR to start output
229 * at the beginning of the line.
231 if (pending_newline) {
232 rtas_call(display_character, 1, 1, NULL, '\r');
233 rtas_call(display_character, 1, 1, NULL, '\n');
238 rtas_call(display_character, 1, 1, NULL,
241 rtas_call(display_character, 1, 1, NULL, '\r');
245 width = row_width[current_line];
247 width = display_width;
250 if (*os == '\n' || *os == '\r') {
251 /* If newline is the last character, save it
252 * until next call to avoid bumping up the
255 if (*os == '\n' && !os[1]) {
258 if (current_line > display_lines-1)
259 current_line = display_lines-1;
260 spin_unlock(&progress_lock);
264 /* RTAS wants CR-LF, not just LF */
267 rtas_call(display_character, 1, 1, NULL, '\r');
268 rtas_call(display_character, 1, 1, NULL, '\n');
270 /* CR might be used to re-draw a line, so we'll
271 * leave it alone and not add LF.
273 rtas_call(display_character, 1, 1, NULL, *os);
277 width = row_width[current_line];
279 width = display_width;
282 rtas_call(display_character, 1, 1, NULL, *os);
287 /* if we overwrite the screen length */
289 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
293 spin_unlock(&progress_lock);
295 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
297 int rtas_token(const char *service)
300 if (rtas.dev == NULL)
301 return RTAS_UNKNOWN_SERVICE;
302 tokp = of_get_property(rtas.dev, service, NULL);
303 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
305 EXPORT_SYMBOL(rtas_token);
307 int rtas_service_present(const char *service)
309 return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
311 EXPORT_SYMBOL(rtas_service_present);
313 #ifdef CONFIG_RTAS_ERROR_LOGGING
315 * Return the firmware-specified size of the error log buffer
316 * for all rtas calls that require an error buffer argument.
317 * This includes 'check-exception' and 'rtas-last-error'.
319 int rtas_get_error_log_max(void)
321 static int rtas_error_log_max;
322 if (rtas_error_log_max)
323 return rtas_error_log_max;
325 rtas_error_log_max = rtas_token ("rtas-error-log-max");
326 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
327 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
328 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
330 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
332 return rtas_error_log_max;
334 EXPORT_SYMBOL(rtas_get_error_log_max);
337 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
338 int rtas_last_error_token;
340 /** Return a copy of the detailed error text associated with the
341 * most recent failed call to rtas. Because the error text
342 * might go stale if there are any other intervening rtas calls,
343 * this routine must be called atomically with whatever produced
344 * the error (i.e. with rtas.lock still held from the previous call).
346 static char *__fetch_rtas_last_error(char *altbuf)
348 struct rtas_args err_args, save_args;
352 if (rtas_last_error_token == -1)
355 bufsz = rtas_get_error_log_max();
357 err_args.token = rtas_last_error_token;
360 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
361 err_args.args[1] = bufsz;
362 err_args.args[2] = 0;
364 save_args = rtas.args;
365 rtas.args = err_args;
367 enter_rtas(__pa(&rtas.args));
369 err_args = rtas.args;
370 rtas.args = save_args;
372 /* Log the error in the unlikely case that there was one. */
373 if (unlikely(err_args.args[2] == 0)) {
379 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
382 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
388 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
390 #else /* CONFIG_RTAS_ERROR_LOGGING */
391 #define __fetch_rtas_last_error(x) NULL
392 #define get_errorlog_buffer() NULL
395 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
400 struct rtas_args *rtas_args;
401 char *buff_copy = NULL;
404 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
407 /* Gotta do something different here, use global lock for now... */
408 spin_lock_irqsave(&rtas.lock, s);
409 rtas_args = &rtas.args;
411 rtas_args->token = token;
412 rtas_args->nargs = nargs;
413 rtas_args->nret = nret;
414 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
415 va_start(list, outputs);
416 for (i = 0; i < nargs; ++i)
417 rtas_args->args[i] = va_arg(list, rtas_arg_t);
420 for (i = 0; i < nret; ++i)
421 rtas_args->rets[i] = 0;
423 enter_rtas(__pa(rtas_args));
425 /* A -1 return code indicates that the last command couldn't
426 be completed due to a hardware error. */
427 if (rtas_args->rets[0] == -1)
428 buff_copy = __fetch_rtas_last_error(NULL);
430 if (nret > 1 && outputs != NULL)
431 for (i = 0; i < nret-1; ++i)
432 outputs[i] = rtas_args->rets[i+1];
433 ret = (nret > 0)? rtas_args->rets[0]: 0;
435 /* Gotta do something different here, use global lock for now... */
436 spin_unlock_irqrestore(&rtas.lock, s);
439 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
445 EXPORT_SYMBOL(rtas_call);
447 /* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
448 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
450 unsigned int rtas_busy_delay_time(int status)
455 if (status == RTAS_BUSY) {
457 } else if (status >= 9900 && status <= 9905) {
458 order = status - 9900;
459 for (ms = 1; order > 0; order--)
465 EXPORT_SYMBOL(rtas_busy_delay_time);
467 /* For an RTAS busy status code, perform the hinted delay. */
468 unsigned int rtas_busy_delay(int status)
473 ms = rtas_busy_delay_time(status);
479 EXPORT_SYMBOL(rtas_busy_delay);
481 int rtas_error_rc(int rtas_rc)
486 case -1: /* Hardware Error */
489 case -3: /* Bad indicator/domain/etc */
492 case -9000: /* Isolation error */
495 case -9001: /* Outstanding TCE/PTE */
498 case -9002: /* No usable slot */
502 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
503 __FUNCTION__, rtas_rc);
510 int rtas_get_power_level(int powerdomain, int *level)
512 int token = rtas_token("get-power-level");
515 if (token == RTAS_UNKNOWN_SERVICE)
518 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
522 return rtas_error_rc(rc);
525 EXPORT_SYMBOL(rtas_get_power_level);
527 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
529 int token = rtas_token("set-power-level");
532 if (token == RTAS_UNKNOWN_SERVICE)
536 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
537 } while (rtas_busy_delay(rc));
540 return rtas_error_rc(rc);
543 EXPORT_SYMBOL(rtas_set_power_level);
545 int rtas_get_sensor(int sensor, int index, int *state)
547 int token = rtas_token("get-sensor-state");
550 if (token == RTAS_UNKNOWN_SERVICE)
554 rc = rtas_call(token, 2, 2, state, sensor, index);
555 } while (rtas_busy_delay(rc));
558 return rtas_error_rc(rc);
561 EXPORT_SYMBOL(rtas_get_sensor);
563 int rtas_set_indicator(int indicator, int index, int new_value)
565 int token = rtas_token("set-indicator");
568 if (token == RTAS_UNKNOWN_SERVICE)
572 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
573 } while (rtas_busy_delay(rc));
576 return rtas_error_rc(rc);
579 EXPORT_SYMBOL(rtas_set_indicator);
582 * Ignoring RTAS extended delay
584 int rtas_set_indicator_fast(int indicator, int index, int new_value)
587 int token = rtas_token("set-indicator");
589 if (token == RTAS_UNKNOWN_SERVICE)
592 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
594 WARN_ON(rc == -2 || (rc >= 9900 && rc <= 9905));
597 return rtas_error_rc(rc);
602 void rtas_restart(char *cmd)
604 if (rtas_flash_term_hook)
605 rtas_flash_term_hook(SYS_RESTART);
606 printk("RTAS system-reboot returned %d\n",
607 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
611 void rtas_power_off(void)
613 if (rtas_flash_term_hook)
614 rtas_flash_term_hook(SYS_POWER_OFF);
615 /* allow power on only with power button press */
616 printk("RTAS power-off returned %d\n",
617 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
623 if (rtas_flash_term_hook)
624 rtas_flash_term_hook(SYS_HALT);
625 /* allow power on only with power button press */
626 printk("RTAS power-off returned %d\n",
627 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
631 /* Must be in the RMO region, so we place it here */
632 static char rtas_os_term_buf[2048];
634 void rtas_os_term(char *str)
641 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
644 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
647 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
648 __pa(rtas_os_term_buf));
649 } while (rtas_busy_delay(status));
652 printk(KERN_EMERG "ibm,os-term call failed %d\n",
656 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
657 #ifdef CONFIG_PPC_PSERIES
658 static void rtas_percpu_suspend_me(void *info)
663 struct rtas_suspend_me_data *data =
664 (struct rtas_suspend_me_data *)info;
667 * We use "waiting" to indicate our state. As long
668 * as it is >0, we are still trying to all join up.
669 * If it goes to 0, we have successfully joined up and
670 * one thread got H_CONTINUE. If any error happens,
673 local_irq_save(flags);
675 rc = plpar_hcall_norets(H_JOIN);
677 } while (rc == H_SUCCESS && data->waiting > 0);
681 if (rc == H_CONTINUE) {
683 data->args->args[data->args->nargs] =
684 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
685 for_each_possible_cpu(i)
686 plpar_hcall_norets(H_PROD,i);
688 data->waiting = -EBUSY;
689 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
693 local_irq_restore(flags);
697 static int rtas_ibm_suspend_me(struct rtas_args *args)
702 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
703 struct rtas_suspend_me_data data;
705 /* Make sure the state is valid */
706 rc = plpar_hcall(H_VASI_STATE, retbuf,
707 ((u64)args->args[0] << 32) | args->args[1]);
712 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
714 } else if (state == H_VASI_ENABLED) {
715 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
717 } else if (state != H_VASI_SUSPENDING) {
718 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
720 args->args[args->nargs] = -1;
727 /* Call function on all CPUs. One of us will make the
730 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
731 data.waiting = -EINVAL;
733 if (data.waiting != 0)
734 printk(KERN_ERR "Error doing global join\n");
736 /* Prod each CPU. This won't hurt, and will wake
737 * anyone we successfully put to sleep with H_JOIN.
739 for_each_possible_cpu(i)
740 plpar_hcall_norets(H_PROD, i);
744 #else /* CONFIG_PPC_PSERIES */
745 static int rtas_ibm_suspend_me(struct rtas_args *args)
751 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
753 struct rtas_args args;
755 char *buff_copy, *errbuf = NULL;
759 if (!capable(CAP_SYS_ADMIN))
762 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
766 if (nargs > ARRAY_SIZE(args.args)
767 || args.nret > ARRAY_SIZE(args.args)
768 || nargs + args.nret > ARRAY_SIZE(args.args))
772 if (copy_from_user(args.args, uargs->args,
773 nargs * sizeof(rtas_arg_t)) != 0)
776 if (args.token == RTAS_UNKNOWN_SERVICE)
779 /* Need to handle ibm,suspend_me call specially */
780 if (args.token == ibm_suspend_me_token) {
781 rc = rtas_ibm_suspend_me(&args);
787 buff_copy = get_errorlog_buffer();
789 spin_lock_irqsave(&rtas.lock, flags);
792 enter_rtas(__pa(&rtas.args));
795 args.rets = &args.args[nargs];
797 /* A -1 return code indicates that the last command couldn't
798 be completed due to a hardware error. */
799 if (args.rets[0] == -1)
800 errbuf = __fetch_rtas_last_error(buff_copy);
802 spin_unlock_irqrestore(&rtas.lock, flags);
806 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
812 if (copy_to_user(uargs->args + nargs,
814 args.nret * sizeof(rtas_arg_t)) != 0)
821 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
822 * informations from the device-tree and allocate the RMO buffer for userland
825 void __init rtas_initialize(void)
827 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
829 /* Get RTAS dev node and fill up our "rtas" structure with infos
832 rtas.dev = of_find_node_by_name(NULL, "rtas");
834 const u32 *basep, *entryp, *sizep;
836 basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
837 sizep = of_get_property(rtas.dev, "rtas-size", NULL);
838 if (basep != NULL && sizep != NULL) {
841 entryp = of_get_property(rtas.dev,
842 "linux,rtas-entry", NULL);
843 if (entryp == NULL) /* Ugh */
844 rtas.entry = rtas.base;
846 rtas.entry = *entryp;
853 /* If RTAS was found, allocate the RMO buffer for it and look for
854 * the stop-self token if any
857 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
858 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
859 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
862 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
864 #ifdef CONFIG_RTAS_ERROR_LOGGING
865 rtas_last_error_token = rtas_token("rtas-last-error");
869 int __init early_init_dt_scan_rtas(unsigned long node,
870 const char *uname, int depth, void *data)
872 u32 *basep, *entryp, *sizep;
874 if (depth != 1 || strcmp(uname, "rtas") != 0)
877 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
878 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
879 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
881 if (basep && entryp && sizep) {
883 rtas.entry = *entryp;
887 #ifdef CONFIG_UDBG_RTAS_CONSOLE
888 basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
890 rtas_putchar_token = *basep;
892 basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
894 rtas_getchar_token = *basep;
896 if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
897 rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
898 udbg_init_rtas_console();