powerpc: Merge rtas.c into arch/powerpc/kernel
[linux-2.6/btrfs-unstable.git] / arch / powerpc / kernel / rtas.c
blob4d22eeeeb91ddc6fc9fc85804a3c783ad47130ae
1 /*
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.
14 #include <stdarg.h>
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>
21 #include <asm/prom.h>
22 #include <asm/rtas.h>
23 #include <asm/semaphore.h>
24 #include <asm/machdep.h>
25 #include <asm/page.h>
26 #include <asm/param.h>
27 #include <asm/system.h>
28 #include <asm/delay.h>
29 #include <asm/uaccess.h>
30 #include <asm/lmb.h>
31 #ifdef CONFIG_PPC64
32 #include <asm/systemcfg.h>
33 #endif
35 struct rtas_t rtas = {
36 .lock = SPIN_LOCK_UNLOCKED
39 EXPORT_SYMBOL(rtas);
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;
53 unsigned long s;
55 if (!rtas.base)
56 return;
57 spin_lock_irqsave(&rtas.lock, s);
59 args->token = 10;
60 args->nargs = 1;
61 args->nret = 1;
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;
75 if (c == '\n') {
76 while (width-- > 0)
77 call_rtas_display_status(' ');
78 width = 16;
79 udelay(500000);
80 pending_newline = 1;
81 } else {
82 if (pending_newline) {
83 call_rtas_display_status('\r');
84 call_rtas_display_status('\n');
86 pending_newline = 0;
87 if (width--) {
88 call_rtas_display_status(c);
89 udelay(10000);
94 void rtas_progress(char *s, unsigned short hex)
96 struct device_node *root;
97 int width, *p;
98 char *os;
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? */
105 if (!rtas.base)
106 return;
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)))
113 display_width = *p;
114 if ((p = (unsigned int *)get_property(root,
115 "ibm,form-feed", NULL)))
116 form_feed = *p;
117 if ((p = (unsigned int *)get_property(root,
118 "ibm,display-number-of-lines", NULL)))
119 display_lines = *p;
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);
131 return;
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
139 * instead.
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');
148 pending_newline = 0;
149 } else {
150 current_line = 0;
151 if (form_feed)
152 rtas_call(display_character, 1, 1, NULL,
153 (char)form_feed);
154 else
155 rtas_call(display_character, 1, 1, NULL, '\r');
158 if (row_width)
159 width = row_width[current_line];
160 else
161 width = display_width;
162 os = s;
163 while (*os) {
164 if (*os == '\n' || *os == '\r') {
165 /* If newline is the last character, save it
166 * until next call to avoid bumping up the
167 * display output.
169 if (*os == '\n' && !os[1]) {
170 pending_newline = 1;
171 current_line++;
172 if (current_line > display_lines-1)
173 current_line = display_lines-1;
174 spin_unlock(&progress_lock);
175 return;
178 /* RTAS wants CR-LF, not just LF */
180 if (*os == '\n') {
181 rtas_call(display_character, 1, 1, NULL, '\r');
182 rtas_call(display_character, 1, 1, NULL, '\n');
183 } else {
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);
190 if (row_width)
191 width = row_width[current_line];
192 else
193 width = display_width;
194 } else {
195 width--;
196 rtas_call(display_character, 1, 1, NULL, *os);
199 os++;
201 /* if we overwrite the screen length */
202 if (width <= 0)
203 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
204 os++;
207 spin_unlock(&progress_lock);
210 int rtas_token(const char *service)
212 int *tokp;
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",
235 rtas_error_log_max);
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;
255 u32 bufsz;
256 char *buf = NULL;
258 if (rtas_last_error_token == -1)
259 return NULL;
261 bufsz = rtas_get_error_log_max();
263 err_args.token = rtas_last_error_token;
264 err_args.nargs = 2;
265 err_args.nret = 1;
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)) {
280 if (altbuf) {
281 buf = altbuf;
282 } else {
283 buf = rtas_err_buf;
284 if (mem_init_done)
285 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
287 if (buf)
288 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
291 return buf;
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
299 #endif
301 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
303 va_list list;
304 int i;
305 unsigned long s;
306 struct rtas_args *rtas_args;
307 char *buff_copy = NULL;
308 int ret;
310 if (token == RTAS_UNKNOWN_SERVICE)
311 return -1;
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);
324 va_end(list);
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);
344 if (buff_copy) {
345 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
346 if (mem_init_done)
347 kfree(buff_copy);
349 return ret;
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;
358 unsigned long ms;
360 if (order < 0)
361 order = 0; /* RTC depends on this for -2 clock busy */
362 else if (order > 5)
363 order = 5; /* bound */
365 /* Use microseconds for reasonable accuracy */
366 for (ms = 1; order > 0; order--)
367 ms *= 10;
369 return ms;
372 int rtas_error_rc(int rtas_rc)
374 int rc;
376 switch (rtas_rc) {
377 case -1: /* Hardware Error */
378 rc = -EIO;
379 break;
380 case -3: /* Bad indicator/domain/etc */
381 rc = -EINVAL;
382 break;
383 case -9000: /* Isolation error */
384 rc = -EFAULT;
385 break;
386 case -9001: /* Outstanding TCE/PTE */
387 rc = -EEXIST;
388 break;
389 case -9002: /* No usable slot */
390 rc = -ENODEV;
391 break;
392 default:
393 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
394 __FUNCTION__, rtas_rc);
395 rc = -ERANGE;
396 break;
398 return rc;
401 int rtas_get_power_level(int powerdomain, int *level)
403 int token = rtas_token("get-power-level");
404 int rc;
406 if (token == RTAS_UNKNOWN_SERVICE)
407 return -ENOENT;
409 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
410 udelay(1);
412 if (rc < 0)
413 return rtas_error_rc(rc);
414 return 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;
421 int rc;
423 if (token == RTAS_UNKNOWN_SERVICE)
424 return -ENOENT;
426 while (1) {
427 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
428 if (rc == RTAS_BUSY)
429 udelay(1);
430 else if (rtas_is_extended_busy(rc)) {
431 wait_time = rtas_extended_busy_delay_time(rc);
432 udelay(wait_time * 1000);
433 } else
434 break;
437 if (rc < 0)
438 return rtas_error_rc(rc);
439 return 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;
446 int rc;
448 if (token == RTAS_UNKNOWN_SERVICE)
449 return -ENOENT;
451 while (1) {
452 rc = rtas_call(token, 2, 2, state, sensor, index);
453 if (rc == RTAS_BUSY)
454 udelay(1);
455 else if (rtas_is_extended_busy(rc)) {
456 wait_time = rtas_extended_busy_delay_time(rc);
457 udelay(wait_time * 1000);
458 } else
459 break;
462 if (rc < 0)
463 return rtas_error_rc(rc);
464 return 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;
471 int rc;
473 if (token == RTAS_UNKNOWN_SERVICE)
474 return -ENOENT;
476 while (1) {
477 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
478 if (rc == RTAS_BUSY)
479 udelay(1);
480 else if (rtas_is_extended_busy(rc)) {
481 wait_time = rtas_extended_busy_delay_time(rc);
482 udelay(wait_time * 1000);
484 else
485 break;
488 if (rc < 0)
489 return rtas_error_rc(rc);
490 return 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));
497 for (;;);
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));
505 for (;;);
508 void rtas_halt(void)
510 rtas_power_off();
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)
518 int status;
520 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
521 return;
523 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
525 do {
526 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
527 __pa(rtas_os_term_buf));
529 if (status == RTAS_BUSY)
530 udelay(1);
531 else if (status != 0)
532 printk(KERN_EMERG "ibm,os-term call failed %d\n",
533 status);
534 } while (status == RTAS_BUSY);
538 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
540 struct rtas_args args;
541 unsigned long flags;
542 char *buff_copy, *errbuf = NULL;
543 int nargs;
545 if (!capable(CAP_SYS_ADMIN))
546 return -EPERM;
548 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
549 return -EFAULT;
551 nargs = args.nargs;
552 if (nargs > ARRAY_SIZE(args.args)
553 || args.nret > ARRAY_SIZE(args.args)
554 || nargs + args.nret > ARRAY_SIZE(args.args))
555 return -EINVAL;
557 /* Copy in args. */
558 if (copy_from_user(args.args, uargs->args,
559 nargs * sizeof(rtas_arg_t)) != 0)
560 return -EFAULT;
562 buff_copy = get_errorlog_buffer();
564 spin_lock_irqsave(&rtas.lock, flags);
566 rtas.args = args;
567 enter_rtas(__pa(&rtas.args));
568 args = 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);
579 if (buff_copy) {
580 if (errbuf)
581 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
582 kfree(buff_copy);
585 /* Copy out args. */
586 if (copy_to_user(uargs->args + nargs,
587 args.args + nargs,
588 args.nret * sizeof(rtas_arg_t)) != 0)
589 return -EFAULT;
591 return 0;
594 #ifdef CONFIG_SMP
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,
600 .nargs = 0,
601 .nret = 1,
602 .rets = &rtas_stop_self_args.args[0],
605 void rtas_stop_self(void)
607 struct rtas_args *rtas_args = &rtas_stop_self_args;
609 local_irq_disable();
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");
619 #endif
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
624 * accesses.
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
631 * about it.
633 rtas.dev = of_find_node_by_name(NULL, "rtas");
634 if (rtas.dev) {
635 u32 *basep, *entryp;
636 u32 *sizep;
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) {
641 rtas.base = *basep;
642 rtas.size = *sizep;
643 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
644 if (entryp == NULL) /* Ugh */
645 rtas.entry = rtas.base;
646 else
647 rtas.entry = *entryp;
648 } else
649 rtas.dev = NULL;
651 if (!rtas.dev)
652 return;
654 /* If RTAS was found, allocate the RMO buffer for it and look for
655 * the stop-self token if any
657 #ifdef CONFIG_PPC64
658 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
659 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
660 #endif
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");
668 #endif
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);