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>
23 #include <asm/semaphore.h>
24 #include <asm/machdep.h>
26 #include <asm/param.h>
27 #include <asm/system.h>
28 #include <asm/delay.h>
29 #include <asm/uaccess.h>
32 #include <asm/systemcfg.h>
35 struct rtas_t rtas = {
36 .lock = SPIN_LOCK_UNLOCKED
41 DEFINE_SPINLOCK(rtas_data_buf_lock);
42 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
43 unsigned long rtas_rmo_buf;
46 * call_rtas_display_status and call_rtas_display_status_delay
47 * are designed only for very early low-level debugging, which
48 * is why the token is hard-coded to 10.
50 void call_rtas_display_status(unsigned char c)
52 struct rtas_args *args = &rtas.args;
57 spin_lock_irqsave(&rtas.lock, s);
62 args->rets = (rtas_arg_t *)&(args->args[1]);
63 args->args[0] = (int)c;
65 enter_rtas(__pa(args));
67 spin_unlock_irqrestore(&rtas.lock, s);
70 void call_rtas_display_status_delay(unsigned char c)
72 static int pending_newline = 0; /* did last write end with unprinted newline? */
73 static int width = 16;
77 call_rtas_display_status(' ');
82 if (pending_newline) {
83 call_rtas_display_status('\r');
84 call_rtas_display_status('\n');
88 call_rtas_display_status(c);
94 void rtas_progress(char *s, unsigned short hex)
96 struct device_node *root;
99 static int display_character, set_indicator;
100 static int display_width, display_lines, *row_width, form_feed;
101 static DEFINE_SPINLOCK(progress_lock);
102 static int current_line;
103 static int pending_newline = 0; /* did last write end with unprinted newline? */
108 if (display_width == 0) {
109 display_width = 0x10;
110 if ((root = find_path_device("/rtas"))) {
111 if ((p = (unsigned int *)get_property(root,
112 "ibm,display-line-length", NULL)))
114 if ((p = (unsigned int *)get_property(root,
115 "ibm,form-feed", NULL)))
117 if ((p = (unsigned int *)get_property(root,
118 "ibm,display-number-of-lines", NULL)))
120 row_width = (unsigned int *)get_property(root,
121 "ibm,display-truncation-length", NULL);
123 display_character = rtas_token("display-character");
124 set_indicator = rtas_token("set-indicator");
127 if (display_character == RTAS_UNKNOWN_SERVICE) {
128 /* use hex display if available */
129 if (set_indicator != RTAS_UNKNOWN_SERVICE)
130 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
134 spin_lock(&progress_lock);
137 * Last write ended with newline, but we didn't print it since
138 * it would just clear the bottom line of output. Print it now
141 * If no newline is pending and form feed is supported, clear the
142 * display with a form feed; otherwise, print a CR to start output
143 * at the beginning of the line.
145 if (pending_newline) {
146 rtas_call(display_character, 1, 1, NULL, '\r');
147 rtas_call(display_character, 1, 1, NULL, '\n');
152 rtas_call(display_character, 1, 1, NULL,
155 rtas_call(display_character, 1, 1, NULL, '\r');
159 width = row_width[current_line];
161 width = display_width;
164 if (*os == '\n' || *os == '\r') {
165 /* If newline is the last character, save it
166 * until next call to avoid bumping up the
169 if (*os == '\n' && !os[1]) {
172 if (current_line > display_lines-1)
173 current_line = display_lines-1;
174 spin_unlock(&progress_lock);
178 /* RTAS wants CR-LF, not just LF */
181 rtas_call(display_character, 1, 1, NULL, '\r');
182 rtas_call(display_character, 1, 1, NULL, '\n');
184 /* CR might be used to re-draw a line, so we'll
185 * leave it alone and not add LF.
187 rtas_call(display_character, 1, 1, NULL, *os);
191 width = row_width[current_line];
193 width = display_width;
196 rtas_call(display_character, 1, 1, NULL, *os);
201 /* if we overwrite the screen length */
203 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
207 spin_unlock(&progress_lock);
210 int rtas_token(const char *service)
213 if (rtas.dev == NULL)
214 return RTAS_UNKNOWN_SERVICE;
215 tokp = (int *) get_property(rtas.dev, service, NULL);
216 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
219 #ifdef CONFIG_RTAS_ERROR_LOGGING
221 * Return the firmware-specified size of the error log buffer
222 * for all rtas calls that require an error buffer argument.
223 * This includes 'check-exception' and 'rtas-last-error'.
225 int rtas_get_error_log_max(void)
227 static int rtas_error_log_max;
228 if (rtas_error_log_max)
229 return rtas_error_log_max;
231 rtas_error_log_max = rtas_token ("rtas-error-log-max");
232 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
233 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
234 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
236 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
238 return rtas_error_log_max;
240 EXPORT_SYMBOL(rtas_get_error_log_max);
243 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
244 int rtas_last_error_token;
246 /** Return a copy of the detailed error text associated with the
247 * most recent failed call to rtas. Because the error text
248 * might go stale if there are any other intervening rtas calls,
249 * this routine must be called atomically with whatever produced
250 * the error (i.e. with rtas.lock still held from the previous call).
252 static char *__fetch_rtas_last_error(char *altbuf)
254 struct rtas_args err_args, save_args;
258 if (rtas_last_error_token == -1)
261 bufsz = rtas_get_error_log_max();
263 err_args.token = rtas_last_error_token;
266 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
267 err_args.args[1] = bufsz;
268 err_args.args[2] = 0;
270 save_args = rtas.args;
271 rtas.args = err_args;
273 enter_rtas(__pa(&rtas.args));
275 err_args = rtas.args;
276 rtas.args = save_args;
278 /* Log the error in the unlikely case that there was one. */
279 if (unlikely(err_args.args[2] == 0)) {
285 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
288 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
294 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
296 #else /* CONFIG_RTAS_ERROR_LOGGING */
297 #define __fetch_rtas_last_error(x) NULL
298 #define get_errorlog_buffer() NULL
301 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
306 struct rtas_args *rtas_args;
307 char *buff_copy = NULL;
310 if (token == RTAS_UNKNOWN_SERVICE)
313 /* Gotta do something different here, use global lock for now... */
314 spin_lock_irqsave(&rtas.lock, s);
315 rtas_args = &rtas.args;
317 rtas_args->token = token;
318 rtas_args->nargs = nargs;
319 rtas_args->nret = nret;
320 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
321 va_start(list, outputs);
322 for (i = 0; i < nargs; ++i)
323 rtas_args->args[i] = va_arg(list, rtas_arg_t);
326 for (i = 0; i < nret; ++i)
327 rtas_args->rets[i] = 0;
329 enter_rtas(__pa(rtas_args));
331 /* A -1 return code indicates that the last command couldn't
332 be completed due to a hardware error. */
333 if (rtas_args->rets[0] == -1)
334 buff_copy = __fetch_rtas_last_error(NULL);
336 if (nret > 1 && outputs != NULL)
337 for (i = 0; i < nret-1; ++i)
338 outputs[i] = rtas_args->rets[i+1];
339 ret = (nret > 0)? rtas_args->rets[0]: 0;
341 /* Gotta do something different here, use global lock for now... */
342 spin_unlock_irqrestore(&rtas.lock, s);
345 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
352 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
353 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
355 unsigned int rtas_extended_busy_delay_time(int status)
357 int order = status - 9900;
361 order = 0; /* RTC depends on this for -2 clock busy */
363 order = 5; /* bound */
365 /* Use microseconds for reasonable accuracy */
366 for (ms = 1; order > 0; order--)
372 int rtas_error_rc(int rtas_rc)
377 case -1: /* Hardware Error */
380 case -3: /* Bad indicator/domain/etc */
383 case -9000: /* Isolation error */
386 case -9001: /* Outstanding TCE/PTE */
389 case -9002: /* No usable slot */
393 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
394 __FUNCTION__, rtas_rc);
401 int rtas_get_power_level(int powerdomain, int *level)
403 int token = rtas_token("get-power-level");
406 if (token == RTAS_UNKNOWN_SERVICE)
409 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
413 return rtas_error_rc(rc);
417 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
419 int token = rtas_token("set-power-level");
420 unsigned int wait_time;
423 if (token == RTAS_UNKNOWN_SERVICE)
427 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
430 else if (rtas_is_extended_busy(rc)) {
431 wait_time = rtas_extended_busy_delay_time(rc);
432 udelay(wait_time * 1000);
438 return rtas_error_rc(rc);
442 int rtas_get_sensor(int sensor, int index, int *state)
444 int token = rtas_token("get-sensor-state");
445 unsigned int wait_time;
448 if (token == RTAS_UNKNOWN_SERVICE)
452 rc = rtas_call(token, 2, 2, state, sensor, index);
455 else if (rtas_is_extended_busy(rc)) {
456 wait_time = rtas_extended_busy_delay_time(rc);
457 udelay(wait_time * 1000);
463 return rtas_error_rc(rc);
467 int rtas_set_indicator(int indicator, int index, int new_value)
469 int token = rtas_token("set-indicator");
470 unsigned int wait_time;
473 if (token == RTAS_UNKNOWN_SERVICE)
477 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
480 else if (rtas_is_extended_busy(rc)) {
481 wait_time = rtas_extended_busy_delay_time(rc);
482 udelay(wait_time * 1000);
489 return rtas_error_rc(rc);
493 void rtas_restart(char *cmd)
495 printk("RTAS system-reboot returned %d\n",
496 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
500 void rtas_power_off(void)
502 /* allow power on only with power button press */
503 printk("RTAS power-off returned %d\n",
504 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
513 /* Must be in the RMO region, so we place it here */
514 static char rtas_os_term_buf[2048];
516 void rtas_os_term(char *str)
520 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
523 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
526 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
527 __pa(rtas_os_term_buf));
529 if (status == RTAS_BUSY)
531 else if (status != 0)
532 printk(KERN_EMERG "ibm,os-term call failed %d\n",
534 } while (status == RTAS_BUSY);
538 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
540 struct rtas_args args;
542 char *buff_copy, *errbuf = NULL;
545 if (!capable(CAP_SYS_ADMIN))
548 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
552 if (nargs > ARRAY_SIZE(args.args)
553 || args.nret > ARRAY_SIZE(args.args)
554 || nargs + args.nret > ARRAY_SIZE(args.args))
558 if (copy_from_user(args.args, uargs->args,
559 nargs * sizeof(rtas_arg_t)) != 0)
562 buff_copy = get_errorlog_buffer();
564 spin_lock_irqsave(&rtas.lock, flags);
567 enter_rtas(__pa(&rtas.args));
570 args.rets = &args.args[nargs];
572 /* A -1 return code indicates that the last command couldn't
573 be completed due to a hardware error. */
574 if (args.rets[0] == -1)
575 errbuf = __fetch_rtas_last_error(buff_copy);
577 spin_unlock_irqrestore(&rtas.lock, flags);
581 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
586 if (copy_to_user(uargs->args + nargs,
588 args.nret * sizeof(rtas_arg_t)) != 0)
595 /* This version can't take the spinlock, because it never returns */
597 struct rtas_args rtas_stop_self_args = {
598 /* The token is initialized for real in setup_system() */
599 .token = RTAS_UNKNOWN_SERVICE,
602 .rets = &rtas_stop_self_args.args[0],
605 void rtas_stop_self(void)
607 struct rtas_args *rtas_args = &rtas_stop_self_args;
611 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
613 printk("cpu %u (hwid %u) Ready to die...\n",
614 smp_processor_id(), hard_smp_processor_id());
615 enter_rtas(__pa(rtas_args));
617 panic("Alas, I survived.\n");
622 * Call early during boot, before mem init or bootmem, to retreive the RTAS
623 * informations from the device-tree and allocate the RMO buffer for userland
626 void __init rtas_initialize(void)
628 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
630 /* Get RTAS dev node and fill up our "rtas" structure with infos
633 rtas.dev = of_find_node_by_name(NULL, "rtas");
638 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
639 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
640 if (basep != NULL && sizep != NULL) {
643 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
644 if (entryp == NULL) /* Ugh */
645 rtas.entry = rtas.base;
647 rtas.entry = *entryp;
654 /* If RTAS was found, allocate the RMO buffer for it and look for
655 * the stop-self token if any
658 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
659 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
661 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
663 #ifdef CONFIG_HOTPLUG_CPU
664 rtas_stop_self_args.token = rtas_token("stop-self");
665 #endif /* CONFIG_HOTPLUG_CPU */
666 #ifdef CONFIG_RTAS_ERROR_LOGGING
667 rtas_last_error_token = rtas_token("rtas-last-error");
672 EXPORT_SYMBOL(rtas_token);
673 EXPORT_SYMBOL(rtas_call);
674 EXPORT_SYMBOL(rtas_data_buf);
675 EXPORT_SYMBOL(rtas_data_buf_lock);
676 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
677 EXPORT_SYMBOL(rtas_get_sensor);
678 EXPORT_SYMBOL(rtas_get_power_level);
679 EXPORT_SYMBOL(rtas_set_power_level);
680 EXPORT_SYMBOL(rtas_set_indicator);