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(void)
114 udbg_putc = call_rtas_display_status_delay;
117 void rtas_progress(char *s, unsigned short hex)
119 struct device_node *root;
122 static int display_character, set_indicator;
123 static int display_width, display_lines, *row_width, form_feed;
124 static DEFINE_SPINLOCK(progress_lock);
125 static int current_line;
126 static int pending_newline = 0; /* did last write end with unprinted newline? */
131 if (display_width == 0) {
132 display_width = 0x10;
133 if ((root = find_path_device("/rtas"))) {
134 if ((p = (unsigned int *)get_property(root,
135 "ibm,display-line-length", NULL)))
137 if ((p = (unsigned int *)get_property(root,
138 "ibm,form-feed", NULL)))
140 if ((p = (unsigned int *)get_property(root,
141 "ibm,display-number-of-lines", NULL)))
143 row_width = (unsigned int *)get_property(root,
144 "ibm,display-truncation-length", NULL);
146 display_character = rtas_token("display-character");
147 set_indicator = rtas_token("set-indicator");
150 if (display_character == RTAS_UNKNOWN_SERVICE) {
151 /* use hex display if available */
152 if (set_indicator != RTAS_UNKNOWN_SERVICE)
153 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
157 spin_lock(&progress_lock);
160 * Last write ended with newline, but we didn't print it since
161 * it would just clear the bottom line of output. Print it now
164 * If no newline is pending and form feed is supported, clear the
165 * display with a form feed; otherwise, print a CR to start output
166 * at the beginning of the line.
168 if (pending_newline) {
169 rtas_call(display_character, 1, 1, NULL, '\r');
170 rtas_call(display_character, 1, 1, NULL, '\n');
175 rtas_call(display_character, 1, 1, NULL,
178 rtas_call(display_character, 1, 1, NULL, '\r');
182 width = row_width[current_line];
184 width = display_width;
187 if (*os == '\n' || *os == '\r') {
188 /* If newline is the last character, save it
189 * until next call to avoid bumping up the
192 if (*os == '\n' && !os[1]) {
195 if (current_line > display_lines-1)
196 current_line = display_lines-1;
197 spin_unlock(&progress_lock);
201 /* RTAS wants CR-LF, not just LF */
204 rtas_call(display_character, 1, 1, NULL, '\r');
205 rtas_call(display_character, 1, 1, NULL, '\n');
207 /* CR might be used to re-draw a line, so we'll
208 * leave it alone and not add LF.
210 rtas_call(display_character, 1, 1, NULL, *os);
214 width = row_width[current_line];
216 width = display_width;
219 rtas_call(display_character, 1, 1, NULL, *os);
224 /* if we overwrite the screen length */
226 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
230 spin_unlock(&progress_lock);
232 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
234 int rtas_token(const char *service)
237 if (rtas.dev == NULL)
238 return RTAS_UNKNOWN_SERVICE;
239 tokp = (int *) get_property(rtas.dev, service, NULL);
240 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
242 EXPORT_SYMBOL(rtas_token);
244 #ifdef CONFIG_RTAS_ERROR_LOGGING
246 * Return the firmware-specified size of the error log buffer
247 * for all rtas calls that require an error buffer argument.
248 * This includes 'check-exception' and 'rtas-last-error'.
250 int rtas_get_error_log_max(void)
252 static int rtas_error_log_max;
253 if (rtas_error_log_max)
254 return rtas_error_log_max;
256 rtas_error_log_max = rtas_token ("rtas-error-log-max");
257 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
258 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
259 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
261 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
263 return rtas_error_log_max;
265 EXPORT_SYMBOL(rtas_get_error_log_max);
268 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
269 int rtas_last_error_token;
271 /** Return a copy of the detailed error text associated with the
272 * most recent failed call to rtas. Because the error text
273 * might go stale if there are any other intervening rtas calls,
274 * this routine must be called atomically with whatever produced
275 * the error (i.e. with rtas.lock still held from the previous call).
277 static char *__fetch_rtas_last_error(char *altbuf)
279 struct rtas_args err_args, save_args;
283 if (rtas_last_error_token == -1)
286 bufsz = rtas_get_error_log_max();
288 err_args.token = rtas_last_error_token;
291 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
292 err_args.args[1] = bufsz;
293 err_args.args[2] = 0;
295 save_args = rtas.args;
296 rtas.args = err_args;
298 enter_rtas(__pa(&rtas.args));
300 err_args = rtas.args;
301 rtas.args = save_args;
303 /* Log the error in the unlikely case that there was one. */
304 if (unlikely(err_args.args[2] == 0)) {
310 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
313 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
319 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
321 #else /* CONFIG_RTAS_ERROR_LOGGING */
322 #define __fetch_rtas_last_error(x) NULL
323 #define get_errorlog_buffer() NULL
326 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
331 struct rtas_args *rtas_args;
332 char *buff_copy = NULL;
335 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
338 /* Gotta do something different here, use global lock for now... */
339 spin_lock_irqsave(&rtas.lock, s);
340 rtas_args = &rtas.args;
342 rtas_args->token = token;
343 rtas_args->nargs = nargs;
344 rtas_args->nret = nret;
345 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
346 va_start(list, outputs);
347 for (i = 0; i < nargs; ++i)
348 rtas_args->args[i] = va_arg(list, rtas_arg_t);
351 for (i = 0; i < nret; ++i)
352 rtas_args->rets[i] = 0;
354 enter_rtas(__pa(rtas_args));
356 /* A -1 return code indicates that the last command couldn't
357 be completed due to a hardware error. */
358 if (rtas_args->rets[0] == -1)
359 buff_copy = __fetch_rtas_last_error(NULL);
361 if (nret > 1 && outputs != NULL)
362 for (i = 0; i < nret-1; ++i)
363 outputs[i] = rtas_args->rets[i+1];
364 ret = (nret > 0)? rtas_args->rets[0]: 0;
366 /* Gotta do something different here, use global lock for now... */
367 spin_unlock_irqrestore(&rtas.lock, s);
370 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
376 EXPORT_SYMBOL(rtas_call);
378 /* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
379 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
381 unsigned int rtas_busy_delay_time(int status)
386 if (status == RTAS_BUSY) {
388 } else if (status >= 9900 && status <= 9905) {
389 order = status - 9900;
390 for (ms = 1; order > 0; order--)
396 EXPORT_SYMBOL(rtas_busy_delay_time);
398 /* For an RTAS busy status code, perform the hinted delay. */
399 unsigned int rtas_busy_delay(int status)
404 ms = rtas_busy_delay_time(status);
410 EXPORT_SYMBOL(rtas_busy_delay);
412 int rtas_error_rc(int rtas_rc)
417 case -1: /* Hardware Error */
420 case -3: /* Bad indicator/domain/etc */
423 case -9000: /* Isolation error */
426 case -9001: /* Outstanding TCE/PTE */
429 case -9002: /* No usable slot */
433 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
434 __FUNCTION__, rtas_rc);
441 int rtas_get_power_level(int powerdomain, int *level)
443 int token = rtas_token("get-power-level");
446 if (token == RTAS_UNKNOWN_SERVICE)
449 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
453 return rtas_error_rc(rc);
456 EXPORT_SYMBOL(rtas_get_power_level);
458 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
460 int token = rtas_token("set-power-level");
463 if (token == RTAS_UNKNOWN_SERVICE)
467 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
468 } while (rtas_busy_delay(rc));
471 return rtas_error_rc(rc);
474 EXPORT_SYMBOL(rtas_set_power_level);
476 int rtas_get_sensor(int sensor, int index, int *state)
478 int token = rtas_token("get-sensor-state");
481 if (token == RTAS_UNKNOWN_SERVICE)
485 rc = rtas_call(token, 2, 2, state, sensor, index);
486 } while (rtas_busy_delay(rc));
489 return rtas_error_rc(rc);
492 EXPORT_SYMBOL(rtas_get_sensor);
494 int rtas_set_indicator(int indicator, int index, int new_value)
496 int token = rtas_token("set-indicator");
499 if (token == RTAS_UNKNOWN_SERVICE)
503 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
504 } while (rtas_busy_delay(rc));
507 return rtas_error_rc(rc);
510 EXPORT_SYMBOL(rtas_set_indicator);
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));
556 } while (rtas_busy_delay(status));
559 printk(KERN_EMERG "ibm,os-term call failed %d\n",
563 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
564 #ifdef CONFIG_PPC_PSERIES
565 static void rtas_percpu_suspend_me(void *info)
570 struct rtas_suspend_me_data *data =
571 (struct rtas_suspend_me_data *)info;
574 * We use "waiting" to indicate our state. As long
575 * as it is >0, we are still trying to all join up.
576 * If it goes to 0, we have successfully joined up and
577 * one thread got H_CONTINUE. If any error happens,
580 local_irq_save(flags);
582 rc = plpar_hcall_norets(H_JOIN);
584 } while (rc == H_SUCCESS && data->waiting > 0);
588 if (rc == H_CONTINUE) {
590 data->args->args[data->args->nargs] =
591 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
592 for_each_possible_cpu(i)
593 plpar_hcall_norets(H_PROD,i);
595 data->waiting = -EBUSY;
596 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
600 local_irq_restore(flags);
604 static int rtas_ibm_suspend_me(struct rtas_args *args)
611 struct rtas_suspend_me_data data;
613 /* Make sure the state is valid */
614 rc = plpar_hcall(H_VASI_STATE,
615 ((u64)args->args[0] << 32) | args->args[1],
617 &state, &dummy, &dummy);
620 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
622 } else if (state == H_VASI_ENABLED) {
623 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
625 } else if (state != H_VASI_SUSPENDING) {
626 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
628 args->args[args->nargs] = -1;
635 /* Call function on all CPUs. One of us will make the
638 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
639 data.waiting = -EINVAL;
641 if (data.waiting != 0)
642 printk(KERN_ERR "Error doing global join\n");
644 /* Prod each CPU. This won't hurt, and will wake
645 * anyone we successfully put to sleep with H_JOIN.
647 for_each_possible_cpu(i)
648 plpar_hcall_norets(H_PROD, i);
652 #else /* CONFIG_PPC_PSERIES */
653 static int rtas_ibm_suspend_me(struct rtas_args *args)
659 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
661 struct rtas_args args;
663 char *buff_copy, *errbuf = NULL;
667 if (!capable(CAP_SYS_ADMIN))
670 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
674 if (nargs > ARRAY_SIZE(args.args)
675 || args.nret > ARRAY_SIZE(args.args)
676 || nargs + args.nret > ARRAY_SIZE(args.args))
680 if (copy_from_user(args.args, uargs->args,
681 nargs * sizeof(rtas_arg_t)) != 0)
684 if (args.token == RTAS_UNKNOWN_SERVICE)
687 /* Need to handle ibm,suspend_me call specially */
688 if (args.token == ibm_suspend_me_token) {
689 rc = rtas_ibm_suspend_me(&args);
695 buff_copy = get_errorlog_buffer();
697 spin_lock_irqsave(&rtas.lock, flags);
700 enter_rtas(__pa(&rtas.args));
703 args.rets = &args.args[nargs];
705 /* A -1 return code indicates that the last command couldn't
706 be completed due to a hardware error. */
707 if (args.rets[0] == -1)
708 errbuf = __fetch_rtas_last_error(buff_copy);
710 spin_unlock_irqrestore(&rtas.lock, flags);
714 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
720 if (copy_to_user(uargs->args + nargs,
722 args.nret * sizeof(rtas_arg_t)) != 0)
728 /* This version can't take the spinlock, because it never returns */
730 struct rtas_args rtas_stop_self_args = {
731 /* The token is initialized for real in setup_system() */
732 .token = RTAS_UNKNOWN_SERVICE,
735 .rets = &rtas_stop_self_args.args[0],
738 void rtas_stop_self(void)
740 struct rtas_args *rtas_args = &rtas_stop_self_args;
744 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
746 printk("cpu %u (hwid %u) Ready to die...\n",
747 smp_processor_id(), hard_smp_processor_id());
748 enter_rtas(__pa(rtas_args));
750 panic("Alas, I survived.\n");
754 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
755 * informations from the device-tree and allocate the RMO buffer for userland
758 void __init rtas_initialize(void)
760 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
762 /* Get RTAS dev node and fill up our "rtas" structure with infos
765 rtas.dev = of_find_node_by_name(NULL, "rtas");
770 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
771 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
772 if (basep != NULL && sizep != NULL) {
775 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
776 if (entryp == NULL) /* Ugh */
777 rtas.entry = rtas.base;
779 rtas.entry = *entryp;
786 /* If RTAS was found, allocate the RMO buffer for it and look for
787 * the stop-self token if any
790 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
791 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
792 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
795 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
797 #ifdef CONFIG_HOTPLUG_CPU
798 rtas_stop_self_args.token = rtas_token("stop-self");
799 #endif /* CONFIG_HOTPLUG_CPU */
800 #ifdef CONFIG_RTAS_ERROR_LOGGING
801 rtas_last_error_token = rtas_token("rtas-last-error");