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/delay.h>
24 #include <asm/semaphore.h>
25 #include <asm/machdep.h>
27 #include <asm/param.h>
28 #include <asm/system.h>
29 #include <asm/delay.h>
30 #include <asm/uaccess.h>
33 #include <asm/systemcfg.h>
36 struct rtas_t rtas = {
37 .lock = SPIN_LOCK_UNLOCKED
42 DEFINE_SPINLOCK(rtas_data_buf_lock);
43 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
44 unsigned long rtas_rmo_buf;
47 * If non-NULL, this gets called when the kernel terminates.
48 * This is done like this so rtas_flash can be a module.
50 void (*rtas_flash_term_hook)(int);
51 EXPORT_SYMBOL(rtas_flash_term_hook);
54 * call_rtas_display_status and call_rtas_display_status_delay
55 * are designed only for very early low-level debugging, which
56 * is why the token is hard-coded to 10.
58 void call_rtas_display_status(unsigned char c)
60 struct rtas_args *args = &rtas.args;
65 spin_lock_irqsave(&rtas.lock, s);
70 args->rets = (rtas_arg_t *)&(args->args[1]);
71 args->args[0] = (int)c;
73 enter_rtas(__pa(args));
75 spin_unlock_irqrestore(&rtas.lock, s);
78 void call_rtas_display_status_delay(unsigned char c)
80 static int pending_newline = 0; /* did last write end with unprinted newline? */
81 static int width = 16;
85 call_rtas_display_status(' ');
90 if (pending_newline) {
91 call_rtas_display_status('\r');
92 call_rtas_display_status('\n');
96 call_rtas_display_status(c);
102 void rtas_progress(char *s, unsigned short hex)
104 struct device_node *root;
107 static int display_character, set_indicator;
108 static int display_width, display_lines, *row_width, form_feed;
109 static DEFINE_SPINLOCK(progress_lock);
110 static int current_line;
111 static int pending_newline = 0; /* did last write end with unprinted newline? */
116 if (display_width == 0) {
117 display_width = 0x10;
118 if ((root = find_path_device("/rtas"))) {
119 if ((p = (unsigned int *)get_property(root,
120 "ibm,display-line-length", NULL)))
122 if ((p = (unsigned int *)get_property(root,
123 "ibm,form-feed", NULL)))
125 if ((p = (unsigned int *)get_property(root,
126 "ibm,display-number-of-lines", NULL)))
128 row_width = (unsigned int *)get_property(root,
129 "ibm,display-truncation-length", NULL);
131 display_character = rtas_token("display-character");
132 set_indicator = rtas_token("set-indicator");
135 if (display_character == RTAS_UNKNOWN_SERVICE) {
136 /* use hex display if available */
137 if (set_indicator != RTAS_UNKNOWN_SERVICE)
138 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
142 spin_lock(&progress_lock);
145 * Last write ended with newline, but we didn't print it since
146 * it would just clear the bottom line of output. Print it now
149 * If no newline is pending and form feed is supported, clear the
150 * display with a form feed; otherwise, print a CR to start output
151 * at the beginning of the line.
153 if (pending_newline) {
154 rtas_call(display_character, 1, 1, NULL, '\r');
155 rtas_call(display_character, 1, 1, NULL, '\n');
160 rtas_call(display_character, 1, 1, NULL,
163 rtas_call(display_character, 1, 1, NULL, '\r');
167 width = row_width[current_line];
169 width = display_width;
172 if (*os == '\n' || *os == '\r') {
173 /* If newline is the last character, save it
174 * until next call to avoid bumping up the
177 if (*os == '\n' && !os[1]) {
180 if (current_line > display_lines-1)
181 current_line = display_lines-1;
182 spin_unlock(&progress_lock);
186 /* RTAS wants CR-LF, not just LF */
189 rtas_call(display_character, 1, 1, NULL, '\r');
190 rtas_call(display_character, 1, 1, NULL, '\n');
192 /* CR might be used to re-draw a line, so we'll
193 * leave it alone and not add LF.
195 rtas_call(display_character, 1, 1, NULL, *os);
199 width = row_width[current_line];
201 width = display_width;
204 rtas_call(display_character, 1, 1, NULL, *os);
209 /* if we overwrite the screen length */
211 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
215 spin_unlock(&progress_lock);
217 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
219 int rtas_token(const char *service)
222 if (rtas.dev == NULL)
223 return RTAS_UNKNOWN_SERVICE;
224 tokp = (int *) get_property(rtas.dev, service, NULL);
225 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
228 #ifdef CONFIG_RTAS_ERROR_LOGGING
230 * Return the firmware-specified size of the error log buffer
231 * for all rtas calls that require an error buffer argument.
232 * This includes 'check-exception' and 'rtas-last-error'.
234 int rtas_get_error_log_max(void)
236 static int rtas_error_log_max;
237 if (rtas_error_log_max)
238 return rtas_error_log_max;
240 rtas_error_log_max = rtas_token ("rtas-error-log-max");
241 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
242 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
243 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
245 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
247 return rtas_error_log_max;
249 EXPORT_SYMBOL(rtas_get_error_log_max);
252 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
253 int rtas_last_error_token;
255 /** Return a copy of the detailed error text associated with the
256 * most recent failed call to rtas. Because the error text
257 * might go stale if there are any other intervening rtas calls,
258 * this routine must be called atomically with whatever produced
259 * the error (i.e. with rtas.lock still held from the previous call).
261 static char *__fetch_rtas_last_error(char *altbuf)
263 struct rtas_args err_args, save_args;
267 if (rtas_last_error_token == -1)
270 bufsz = rtas_get_error_log_max();
272 err_args.token = rtas_last_error_token;
275 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
276 err_args.args[1] = bufsz;
277 err_args.args[2] = 0;
279 save_args = rtas.args;
280 rtas.args = err_args;
282 enter_rtas(__pa(&rtas.args));
284 err_args = rtas.args;
285 rtas.args = save_args;
287 /* Log the error in the unlikely case that there was one. */
288 if (unlikely(err_args.args[2] == 0)) {
294 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
297 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
303 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
305 #else /* CONFIG_RTAS_ERROR_LOGGING */
306 #define __fetch_rtas_last_error(x) NULL
307 #define get_errorlog_buffer() NULL
310 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
315 struct rtas_args *rtas_args;
316 char *buff_copy = NULL;
319 if (token == RTAS_UNKNOWN_SERVICE)
322 /* Gotta do something different here, use global lock for now... */
323 spin_lock_irqsave(&rtas.lock, s);
324 rtas_args = &rtas.args;
326 rtas_args->token = token;
327 rtas_args->nargs = nargs;
328 rtas_args->nret = nret;
329 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
330 va_start(list, outputs);
331 for (i = 0; i < nargs; ++i)
332 rtas_args->args[i] = va_arg(list, rtas_arg_t);
335 for (i = 0; i < nret; ++i)
336 rtas_args->rets[i] = 0;
338 enter_rtas(__pa(rtas_args));
340 /* A -1 return code indicates that the last command couldn't
341 be completed due to a hardware error. */
342 if (rtas_args->rets[0] == -1)
343 buff_copy = __fetch_rtas_last_error(NULL);
345 if (nret > 1 && outputs != NULL)
346 for (i = 0; i < nret-1; ++i)
347 outputs[i] = rtas_args->rets[i+1];
348 ret = (nret > 0)? rtas_args->rets[0]: 0;
350 /* Gotta do something different here, use global lock for now... */
351 spin_unlock_irqrestore(&rtas.lock, s);
354 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
361 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
362 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
364 unsigned int rtas_extended_busy_delay_time(int status)
366 int order = status - 9900;
370 order = 0; /* RTC depends on this for -2 clock busy */
372 order = 5; /* bound */
374 /* Use microseconds for reasonable accuracy */
375 for (ms = 1; order > 0; order--)
381 int rtas_error_rc(int rtas_rc)
386 case -1: /* Hardware Error */
389 case -3: /* Bad indicator/domain/etc */
392 case -9000: /* Isolation error */
395 case -9001: /* Outstanding TCE/PTE */
398 case -9002: /* No usable slot */
402 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
403 __FUNCTION__, rtas_rc);
410 int rtas_get_power_level(int powerdomain, int *level)
412 int token = rtas_token("get-power-level");
415 if (token == RTAS_UNKNOWN_SERVICE)
418 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
422 return rtas_error_rc(rc);
426 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
428 int token = rtas_token("set-power-level");
429 unsigned int wait_time;
432 if (token == RTAS_UNKNOWN_SERVICE)
436 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
439 else if (rtas_is_extended_busy(rc)) {
440 wait_time = rtas_extended_busy_delay_time(rc);
441 udelay(wait_time * 1000);
447 return rtas_error_rc(rc);
451 int rtas_get_sensor(int sensor, int index, int *state)
453 int token = rtas_token("get-sensor-state");
454 unsigned int wait_time;
457 if (token == RTAS_UNKNOWN_SERVICE)
461 rc = rtas_call(token, 2, 2, state, sensor, index);
464 else if (rtas_is_extended_busy(rc)) {
465 wait_time = rtas_extended_busy_delay_time(rc);
466 udelay(wait_time * 1000);
472 return rtas_error_rc(rc);
476 int rtas_set_indicator(int indicator, int index, int new_value)
478 int token = rtas_token("set-indicator");
479 unsigned int wait_time;
482 if (token == RTAS_UNKNOWN_SERVICE)
486 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
489 else if (rtas_is_extended_busy(rc)) {
490 wait_time = rtas_extended_busy_delay_time(rc);
491 udelay(wait_time * 1000);
498 return rtas_error_rc(rc);
502 void rtas_restart(char *cmd)
504 if (rtas_flash_term_hook)
505 rtas_flash_term_hook(SYS_RESTART);
506 printk("RTAS system-reboot returned %d\n",
507 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
511 void rtas_power_off(void)
513 if (rtas_flash_term_hook)
514 rtas_flash_term_hook(SYS_POWER_OFF);
515 /* allow power on only with power button press */
516 printk("RTAS power-off returned %d\n",
517 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
523 if (rtas_flash_term_hook)
524 rtas_flash_term_hook(SYS_HALT);
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));
531 /* Must be in the RMO region, so we place it here */
532 static char rtas_os_term_buf[2048];
534 void rtas_os_term(char *str)
538 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
541 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
544 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
545 __pa(rtas_os_term_buf));
547 if (status == RTAS_BUSY)
549 else if (status != 0)
550 printk(KERN_EMERG "ibm,os-term call failed %d\n",
552 } while (status == RTAS_BUSY);
556 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
558 struct rtas_args args;
560 char *buff_copy, *errbuf = NULL;
563 if (!capable(CAP_SYS_ADMIN))
566 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
570 if (nargs > ARRAY_SIZE(args.args)
571 || args.nret > ARRAY_SIZE(args.args)
572 || nargs + args.nret > ARRAY_SIZE(args.args))
576 if (copy_from_user(args.args, uargs->args,
577 nargs * sizeof(rtas_arg_t)) != 0)
580 buff_copy = get_errorlog_buffer();
582 spin_lock_irqsave(&rtas.lock, flags);
585 enter_rtas(__pa(&rtas.args));
588 args.rets = &args.args[nargs];
590 /* A -1 return code indicates that the last command couldn't
591 be completed due to a hardware error. */
592 if (args.rets[0] == -1)
593 errbuf = __fetch_rtas_last_error(buff_copy);
595 spin_unlock_irqrestore(&rtas.lock, flags);
599 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
604 if (copy_to_user(uargs->args + nargs,
606 args.nret * sizeof(rtas_arg_t)) != 0)
612 /* This version can't take the spinlock, because it never returns */
614 struct rtas_args rtas_stop_self_args = {
615 /* The token is initialized for real in setup_system() */
616 .token = RTAS_UNKNOWN_SERVICE,
619 .rets = &rtas_stop_self_args.args[0],
622 void rtas_stop_self(void)
624 struct rtas_args *rtas_args = &rtas_stop_self_args;
628 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
630 printk("cpu %u (hwid %u) Ready to die...\n",
631 smp_processor_id(), hard_smp_processor_id());
632 enter_rtas(__pa(rtas_args));
634 panic("Alas, I survived.\n");
638 * Call early during boot, before mem init or bootmem, to retreive the RTAS
639 * informations from the device-tree and allocate the RMO buffer for userland
642 void __init rtas_initialize(void)
644 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
646 /* Get RTAS dev node and fill up our "rtas" structure with infos
649 rtas.dev = of_find_node_by_name(NULL, "rtas");
654 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
655 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
656 if (basep != NULL && sizep != NULL) {
659 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
660 if (entryp == NULL) /* Ugh */
661 rtas.entry = rtas.base;
663 rtas.entry = *entryp;
670 /* If RTAS was found, allocate the RMO buffer for it and look for
671 * the stop-self token if any
674 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
675 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
677 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
679 #ifdef CONFIG_HOTPLUG_CPU
680 rtas_stop_self_args.token = rtas_token("stop-self");
681 #endif /* CONFIG_HOTPLUG_CPU */
682 #ifdef CONFIG_RTAS_ERROR_LOGGING
683 rtas_last_error_token = rtas_token("rtas-last-error");
688 EXPORT_SYMBOL(rtas_token);
689 EXPORT_SYMBOL(rtas_call);
690 EXPORT_SYMBOL(rtas_data_buf);
691 EXPORT_SYMBOL(rtas_data_buf_lock);
692 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
693 EXPORT_SYMBOL(rtas_get_sensor);
694 EXPORT_SYMBOL(rtas_get_power_level);
695 EXPORT_SYMBOL(rtas_set_power_level);
696 EXPORT_SYMBOL(rtas_set_indicator);