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
42 struct rtas_suspend_me_data {
44 struct rtas_args *args;
49 DEFINE_SPINLOCK(rtas_data_buf_lock);
50 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
51 unsigned long rtas_rmo_buf;
54 * If non-NULL, this gets called when the kernel terminates.
55 * This is done like this so rtas_flash can be a module.
57 void (*rtas_flash_term_hook)(int);
58 EXPORT_SYMBOL(rtas_flash_term_hook);
61 * call_rtas_display_status and call_rtas_display_status_delay
62 * are designed only for very early low-level debugging, which
63 * is why the token is hard-coded to 10.
65 static void call_rtas_display_status(char c)
67 struct rtas_args *args = &rtas.args;
72 spin_lock_irqsave(&rtas.lock, s);
77 args->rets = (rtas_arg_t *)&(args->args[1]);
78 args->args[0] = (unsigned char)c;
80 enter_rtas(__pa(args));
82 spin_unlock_irqrestore(&rtas.lock, s);
85 static void call_rtas_display_status_delay(char c)
87 static int pending_newline = 0; /* did last write end with unprinted newline? */
88 static int width = 16;
92 call_rtas_display_status(' ');
97 if (pending_newline) {
98 call_rtas_display_status('\r');
99 call_rtas_display_status('\n');
103 call_rtas_display_status(c);
109 void __init udbg_init_rtas(void)
111 udbg_putc = call_rtas_display_status_delay;
114 void rtas_progress(char *s, unsigned short hex)
116 struct device_node *root;
119 static int display_character, set_indicator;
120 static int display_width, display_lines, *row_width, form_feed;
121 static DEFINE_SPINLOCK(progress_lock);
122 static int current_line;
123 static int pending_newline = 0; /* did last write end with unprinted newline? */
128 if (display_width == 0) {
129 display_width = 0x10;
130 if ((root = find_path_device("/rtas"))) {
131 if ((p = (unsigned int *)get_property(root,
132 "ibm,display-line-length", NULL)))
134 if ((p = (unsigned int *)get_property(root,
135 "ibm,form-feed", NULL)))
137 if ((p = (unsigned int *)get_property(root,
138 "ibm,display-number-of-lines", NULL)))
140 row_width = (unsigned int *)get_property(root,
141 "ibm,display-truncation-length", NULL);
143 display_character = rtas_token("display-character");
144 set_indicator = rtas_token("set-indicator");
147 if (display_character == RTAS_UNKNOWN_SERVICE) {
148 /* use hex display if available */
149 if (set_indicator != RTAS_UNKNOWN_SERVICE)
150 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
154 spin_lock(&progress_lock);
157 * Last write ended with newline, but we didn't print it since
158 * it would just clear the bottom line of output. Print it now
161 * If no newline is pending and form feed is supported, clear the
162 * display with a form feed; otherwise, print a CR to start output
163 * at the beginning of the line.
165 if (pending_newline) {
166 rtas_call(display_character, 1, 1, NULL, '\r');
167 rtas_call(display_character, 1, 1, NULL, '\n');
172 rtas_call(display_character, 1, 1, NULL,
175 rtas_call(display_character, 1, 1, NULL, '\r');
179 width = row_width[current_line];
181 width = display_width;
184 if (*os == '\n' || *os == '\r') {
185 /* If newline is the last character, save it
186 * until next call to avoid bumping up the
189 if (*os == '\n' && !os[1]) {
192 if (current_line > display_lines-1)
193 current_line = display_lines-1;
194 spin_unlock(&progress_lock);
198 /* RTAS wants CR-LF, not just LF */
201 rtas_call(display_character, 1, 1, NULL, '\r');
202 rtas_call(display_character, 1, 1, NULL, '\n');
204 /* CR might be used to re-draw a line, so we'll
205 * leave it alone and not add LF.
207 rtas_call(display_character, 1, 1, NULL, *os);
211 width = row_width[current_line];
213 width = display_width;
216 rtas_call(display_character, 1, 1, NULL, *os);
221 /* if we overwrite the screen length */
223 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
227 spin_unlock(&progress_lock);
229 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
231 int rtas_token(const char *service)
234 if (rtas.dev == NULL)
235 return RTAS_UNKNOWN_SERVICE;
236 tokp = (int *) get_property(rtas.dev, service, NULL);
237 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
240 #ifdef CONFIG_RTAS_ERROR_LOGGING
242 * Return the firmware-specified size of the error log buffer
243 * for all rtas calls that require an error buffer argument.
244 * This includes 'check-exception' and 'rtas-last-error'.
246 int rtas_get_error_log_max(void)
248 static int rtas_error_log_max;
249 if (rtas_error_log_max)
250 return rtas_error_log_max;
252 rtas_error_log_max = rtas_token ("rtas-error-log-max");
253 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
254 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
255 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
257 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
259 return rtas_error_log_max;
261 EXPORT_SYMBOL(rtas_get_error_log_max);
264 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
265 int rtas_last_error_token;
267 /** Return a copy of the detailed error text associated with the
268 * most recent failed call to rtas. Because the error text
269 * might go stale if there are any other intervening rtas calls,
270 * this routine must be called atomically with whatever produced
271 * the error (i.e. with rtas.lock still held from the previous call).
273 static char *__fetch_rtas_last_error(char *altbuf)
275 struct rtas_args err_args, save_args;
279 if (rtas_last_error_token == -1)
282 bufsz = rtas_get_error_log_max();
284 err_args.token = rtas_last_error_token;
287 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
288 err_args.args[1] = bufsz;
289 err_args.args[2] = 0;
291 save_args = rtas.args;
292 rtas.args = err_args;
294 enter_rtas(__pa(&rtas.args));
296 err_args = rtas.args;
297 rtas.args = save_args;
299 /* Log the error in the unlikely case that there was one. */
300 if (unlikely(err_args.args[2] == 0)) {
306 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
309 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
315 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
317 #else /* CONFIG_RTAS_ERROR_LOGGING */
318 #define __fetch_rtas_last_error(x) NULL
319 #define get_errorlog_buffer() NULL
322 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
327 struct rtas_args *rtas_args;
328 char *buff_copy = NULL;
331 if (token == RTAS_UNKNOWN_SERVICE)
334 /* Gotta do something different here, use global lock for now... */
335 spin_lock_irqsave(&rtas.lock, s);
336 rtas_args = &rtas.args;
338 rtas_args->token = token;
339 rtas_args->nargs = nargs;
340 rtas_args->nret = nret;
341 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
342 va_start(list, outputs);
343 for (i = 0; i < nargs; ++i)
344 rtas_args->args[i] = va_arg(list, rtas_arg_t);
347 for (i = 0; i < nret; ++i)
348 rtas_args->rets[i] = 0;
350 enter_rtas(__pa(rtas_args));
352 /* A -1 return code indicates that the last command couldn't
353 be completed due to a hardware error. */
354 if (rtas_args->rets[0] == -1)
355 buff_copy = __fetch_rtas_last_error(NULL);
357 if (nret > 1 && outputs != NULL)
358 for (i = 0; i < nret-1; ++i)
359 outputs[i] = rtas_args->rets[i+1];
360 ret = (nret > 0)? rtas_args->rets[0]: 0;
362 /* Gotta do something different here, use global lock for now... */
363 spin_unlock_irqrestore(&rtas.lock, s);
366 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
373 /* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
374 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
376 unsigned int rtas_busy_delay_time(int status)
381 if (status == RTAS_BUSY) {
383 } else if (status >= 9900 && status <= 9905) {
384 order = status - 9900;
385 for (ms = 1; order > 0; order--)
392 /* For an RTAS busy status code, perform the hinted delay. */
393 unsigned int rtas_busy_delay(int status)
398 ms = rtas_busy_delay_time(status);
405 int rtas_error_rc(int rtas_rc)
410 case -1: /* Hardware Error */
413 case -3: /* Bad indicator/domain/etc */
416 case -9000: /* Isolation error */
419 case -9001: /* Outstanding TCE/PTE */
422 case -9002: /* No usable slot */
426 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
427 __FUNCTION__, rtas_rc);
434 int rtas_get_power_level(int powerdomain, int *level)
436 int token = rtas_token("get-power-level");
439 if (token == RTAS_UNKNOWN_SERVICE)
442 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
446 return rtas_error_rc(rc);
450 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
452 int token = rtas_token("set-power-level");
455 if (token == RTAS_UNKNOWN_SERVICE)
459 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
460 } while (rtas_busy_delay(rc));
463 return rtas_error_rc(rc);
467 int rtas_get_sensor(int sensor, int index, int *state)
469 int token = rtas_token("get-sensor-state");
472 if (token == RTAS_UNKNOWN_SERVICE)
476 rc = rtas_call(token, 2, 2, state, sensor, index);
477 } while (rtas_busy_delay(rc));
480 return rtas_error_rc(rc);
484 int rtas_set_indicator(int indicator, int index, int new_value)
486 int token = rtas_token("set-indicator");
489 if (token == RTAS_UNKNOWN_SERVICE)
493 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
494 } while (rtas_busy_delay(rc));
497 return rtas_error_rc(rc);
501 void rtas_restart(char *cmd)
503 if (rtas_flash_term_hook)
504 rtas_flash_term_hook(SYS_RESTART);
505 printk("RTAS system-reboot returned %d\n",
506 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
510 void rtas_power_off(void)
512 if (rtas_flash_term_hook)
513 rtas_flash_term_hook(SYS_POWER_OFF);
514 /* allow power on only with power button press */
515 printk("RTAS power-off returned %d\n",
516 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
522 if (rtas_flash_term_hook)
523 rtas_flash_term_hook(SYS_HALT);
524 /* allow power on only with power button press */
525 printk("RTAS power-off returned %d\n",
526 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
530 /* Must be in the RMO region, so we place it here */
531 static char rtas_os_term_buf[2048];
533 void rtas_os_term(char *str)
537 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
540 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
543 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
544 __pa(rtas_os_term_buf));
545 } while (rtas_busy_delay(status));
548 printk(KERN_EMERG "ibm,os-term call failed %d\n",
552 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
553 #ifdef CONFIG_PPC_PSERIES
554 static void rtas_percpu_suspend_me(void *info)
559 struct rtas_suspend_me_data *data =
560 (struct rtas_suspend_me_data *)info;
563 * We use "waiting" to indicate our state. As long
564 * as it is >0, we are still trying to all join up.
565 * If it goes to 0, we have successfully joined up and
566 * one thread got H_CONTINUE. If any error happens,
569 local_irq_save(flags);
571 rc = plpar_hcall_norets(H_JOIN);
573 } while (rc == H_SUCCESS && data->waiting > 0);
577 if (rc == H_CONTINUE) {
579 data->args->args[data->args->nargs] =
580 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
581 for_each_possible_cpu(i)
582 plpar_hcall_norets(H_PROD,i);
584 data->waiting = -EBUSY;
585 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
589 local_irq_restore(flags);
593 static int rtas_ibm_suspend_me(struct rtas_args *args)
597 struct rtas_suspend_me_data data;
602 /* Call function on all CPUs. One of us will make the
605 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
606 data.waiting = -EINVAL;
608 if (data.waiting != 0)
609 printk(KERN_ERR "Error doing global join\n");
611 /* Prod each CPU. This won't hurt, and will wake
612 * anyone we successfully put to sleep with H_JOIN.
614 for_each_possible_cpu(i)
615 plpar_hcall_norets(H_PROD, i);
619 #else /* CONFIG_PPC_PSERIES */
620 static int rtas_ibm_suspend_me(struct rtas_args *args)
626 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
628 struct rtas_args args;
630 char *buff_copy, *errbuf = NULL;
634 if (!capable(CAP_SYS_ADMIN))
637 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
641 if (nargs > ARRAY_SIZE(args.args)
642 || args.nret > ARRAY_SIZE(args.args)
643 || nargs + args.nret > ARRAY_SIZE(args.args))
647 if (copy_from_user(args.args, uargs->args,
648 nargs * sizeof(rtas_arg_t)) != 0)
651 if (args.token == RTAS_UNKNOWN_SERVICE)
654 /* Need to handle ibm,suspend_me call specially */
655 if (args.token == ibm_suspend_me_token) {
656 rc = rtas_ibm_suspend_me(&args);
662 buff_copy = get_errorlog_buffer();
664 spin_lock_irqsave(&rtas.lock, flags);
667 enter_rtas(__pa(&rtas.args));
670 args.rets = &args.args[nargs];
672 /* A -1 return code indicates that the last command couldn't
673 be completed due to a hardware error. */
674 if (args.rets[0] == -1)
675 errbuf = __fetch_rtas_last_error(buff_copy);
677 spin_unlock_irqrestore(&rtas.lock, flags);
681 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
687 if (copy_to_user(uargs->args + nargs,
689 args.nret * sizeof(rtas_arg_t)) != 0)
695 /* This version can't take the spinlock, because it never returns */
697 struct rtas_args rtas_stop_self_args = {
698 /* The token is initialized for real in setup_system() */
699 .token = RTAS_UNKNOWN_SERVICE,
702 .rets = &rtas_stop_self_args.args[0],
705 void rtas_stop_self(void)
707 struct rtas_args *rtas_args = &rtas_stop_self_args;
711 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
713 printk("cpu %u (hwid %u) Ready to die...\n",
714 smp_processor_id(), hard_smp_processor_id());
715 enter_rtas(__pa(rtas_args));
717 panic("Alas, I survived.\n");
721 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
722 * informations from the device-tree and allocate the RMO buffer for userland
725 void __init rtas_initialize(void)
727 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
729 /* Get RTAS dev node and fill up our "rtas" structure with infos
732 rtas.dev = of_find_node_by_name(NULL, "rtas");
737 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
738 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
739 if (basep != NULL && sizep != NULL) {
742 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
743 if (entryp == NULL) /* Ugh */
744 rtas.entry = rtas.base;
746 rtas.entry = *entryp;
753 /* If RTAS was found, allocate the RMO buffer for it and look for
754 * the stop-self token if any
757 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
758 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
759 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
762 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
764 #ifdef CONFIG_HOTPLUG_CPU
765 rtas_stop_self_args.token = rtas_token("stop-self");
766 #endif /* CONFIG_HOTPLUG_CPU */
767 #ifdef CONFIG_RTAS_ERROR_LOGGING
768 rtas_last_error_token = rtas_token("rtas-last-error");
773 EXPORT_SYMBOL(rtas_token);
774 EXPORT_SYMBOL(rtas_call);
775 EXPORT_SYMBOL(rtas_data_buf);
776 EXPORT_SYMBOL(rtas_data_buf_lock);
777 EXPORT_SYMBOL(rtas_busy_delay_time);
778 EXPORT_SYMBOL(rtas_get_sensor);
779 EXPORT_SYMBOL(rtas_get_power_level);
780 EXPORT_SYMBOL(rtas_set_power_level);
781 EXPORT_SYMBOL(rtas_set_indicator);