[PATCH] capable/capability.h (arch/)
[linux-2.6/zen-sources.git] / arch / powerpc / kernel / rtas.c
blob4b9cfe4637b1fcff56cf698f15e250101d28ffe5
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>
20 #include <linux/capability.h>
21 #include <linux/delay.h>
23 #include <asm/prom.h>
24 #include <asm/rtas.h>
25 #include <asm/semaphore.h>
26 #include <asm/machdep.h>
27 #include <asm/page.h>
28 #include <asm/param.h>
29 #include <asm/system.h>
30 #include <asm/delay.h>
31 #include <asm/uaccess.h>
32 #include <asm/lmb.h>
33 #include <asm/udbg.h>
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 * If non-NULL, this gets called when the kernel terminates.
47 * This is done like this so rtas_flash can be a module.
49 void (*rtas_flash_term_hook)(int);
50 EXPORT_SYMBOL(rtas_flash_term_hook);
53 * call_rtas_display_status and call_rtas_display_status_delay
54 * are designed only for very early low-level debugging, which
55 * is why the token is hard-coded to 10.
57 static void call_rtas_display_status(char c)
59 struct rtas_args *args = &rtas.args;
60 unsigned long s;
62 if (!rtas.base)
63 return;
64 spin_lock_irqsave(&rtas.lock, s);
66 args->token = 10;
67 args->nargs = 1;
68 args->nret = 1;
69 args->rets = (rtas_arg_t *)&(args->args[1]);
70 args->args[0] = (unsigned char)c;
72 enter_rtas(__pa(args));
74 spin_unlock_irqrestore(&rtas.lock, s);
77 static void call_rtas_display_status_delay(char c)
79 static int pending_newline = 0; /* did last write end with unprinted newline? */
80 static int width = 16;
82 if (c == '\n') {
83 while (width-- > 0)
84 call_rtas_display_status(' ');
85 width = 16;
86 mdelay(500);
87 pending_newline = 1;
88 } else {
89 if (pending_newline) {
90 call_rtas_display_status('\r');
91 call_rtas_display_status('\n');
93 pending_newline = 0;
94 if (width--) {
95 call_rtas_display_status(c);
96 udelay(10000);
101 void __init udbg_init_rtas(void)
103 udbg_putc = call_rtas_display_status_delay;
106 void rtas_progress(char *s, unsigned short hex)
108 struct device_node *root;
109 int width, *p;
110 char *os;
111 static int display_character, set_indicator;
112 static int display_width, display_lines, *row_width, form_feed;
113 static DEFINE_SPINLOCK(progress_lock);
114 static int current_line;
115 static int pending_newline = 0; /* did last write end with unprinted newline? */
117 if (!rtas.base)
118 return;
120 if (display_width == 0) {
121 display_width = 0x10;
122 if ((root = find_path_device("/rtas"))) {
123 if ((p = (unsigned int *)get_property(root,
124 "ibm,display-line-length", NULL)))
125 display_width = *p;
126 if ((p = (unsigned int *)get_property(root,
127 "ibm,form-feed", NULL)))
128 form_feed = *p;
129 if ((p = (unsigned int *)get_property(root,
130 "ibm,display-number-of-lines", NULL)))
131 display_lines = *p;
132 row_width = (unsigned int *)get_property(root,
133 "ibm,display-truncation-length", NULL);
135 display_character = rtas_token("display-character");
136 set_indicator = rtas_token("set-indicator");
139 if (display_character == RTAS_UNKNOWN_SERVICE) {
140 /* use hex display if available */
141 if (set_indicator != RTAS_UNKNOWN_SERVICE)
142 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
143 return;
146 spin_lock(&progress_lock);
149 * Last write ended with newline, but we didn't print it since
150 * it would just clear the bottom line of output. Print it now
151 * instead.
153 * If no newline is pending and form feed is supported, clear the
154 * display with a form feed; otherwise, print a CR to start output
155 * at the beginning of the line.
157 if (pending_newline) {
158 rtas_call(display_character, 1, 1, NULL, '\r');
159 rtas_call(display_character, 1, 1, NULL, '\n');
160 pending_newline = 0;
161 } else {
162 current_line = 0;
163 if (form_feed)
164 rtas_call(display_character, 1, 1, NULL,
165 (char)form_feed);
166 else
167 rtas_call(display_character, 1, 1, NULL, '\r');
170 if (row_width)
171 width = row_width[current_line];
172 else
173 width = display_width;
174 os = s;
175 while (*os) {
176 if (*os == '\n' || *os == '\r') {
177 /* If newline is the last character, save it
178 * until next call to avoid bumping up the
179 * display output.
181 if (*os == '\n' && !os[1]) {
182 pending_newline = 1;
183 current_line++;
184 if (current_line > display_lines-1)
185 current_line = display_lines-1;
186 spin_unlock(&progress_lock);
187 return;
190 /* RTAS wants CR-LF, not just LF */
192 if (*os == '\n') {
193 rtas_call(display_character, 1, 1, NULL, '\r');
194 rtas_call(display_character, 1, 1, NULL, '\n');
195 } else {
196 /* CR might be used to re-draw a line, so we'll
197 * leave it alone and not add LF.
199 rtas_call(display_character, 1, 1, NULL, *os);
202 if (row_width)
203 width = row_width[current_line];
204 else
205 width = display_width;
206 } else {
207 width--;
208 rtas_call(display_character, 1, 1, NULL, *os);
211 os++;
213 /* if we overwrite the screen length */
214 if (width <= 0)
215 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
216 os++;
219 spin_unlock(&progress_lock);
221 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
223 int rtas_token(const char *service)
225 int *tokp;
226 if (rtas.dev == NULL)
227 return RTAS_UNKNOWN_SERVICE;
228 tokp = (int *) get_property(rtas.dev, service, NULL);
229 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
232 #ifdef CONFIG_RTAS_ERROR_LOGGING
234 * Return the firmware-specified size of the error log buffer
235 * for all rtas calls that require an error buffer argument.
236 * This includes 'check-exception' and 'rtas-last-error'.
238 int rtas_get_error_log_max(void)
240 static int rtas_error_log_max;
241 if (rtas_error_log_max)
242 return rtas_error_log_max;
244 rtas_error_log_max = rtas_token ("rtas-error-log-max");
245 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
246 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
247 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
248 rtas_error_log_max);
249 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
251 return rtas_error_log_max;
253 EXPORT_SYMBOL(rtas_get_error_log_max);
256 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
257 int rtas_last_error_token;
259 /** Return a copy of the detailed error text associated with the
260 * most recent failed call to rtas. Because the error text
261 * might go stale if there are any other intervening rtas calls,
262 * this routine must be called atomically with whatever produced
263 * the error (i.e. with rtas.lock still held from the previous call).
265 static char *__fetch_rtas_last_error(char *altbuf)
267 struct rtas_args err_args, save_args;
268 u32 bufsz;
269 char *buf = NULL;
271 if (rtas_last_error_token == -1)
272 return NULL;
274 bufsz = rtas_get_error_log_max();
276 err_args.token = rtas_last_error_token;
277 err_args.nargs = 2;
278 err_args.nret = 1;
279 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
280 err_args.args[1] = bufsz;
281 err_args.args[2] = 0;
283 save_args = rtas.args;
284 rtas.args = err_args;
286 enter_rtas(__pa(&rtas.args));
288 err_args = rtas.args;
289 rtas.args = save_args;
291 /* Log the error in the unlikely case that there was one. */
292 if (unlikely(err_args.args[2] == 0)) {
293 if (altbuf) {
294 buf = altbuf;
295 } else {
296 buf = rtas_err_buf;
297 if (mem_init_done)
298 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
300 if (buf)
301 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
304 return buf;
307 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
309 #else /* CONFIG_RTAS_ERROR_LOGGING */
310 #define __fetch_rtas_last_error(x) NULL
311 #define get_errorlog_buffer() NULL
312 #endif
314 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
316 va_list list;
317 int i;
318 unsigned long s;
319 struct rtas_args *rtas_args;
320 char *buff_copy = NULL;
321 int ret;
323 if (token == RTAS_UNKNOWN_SERVICE)
324 return -1;
326 /* Gotta do something different here, use global lock for now... */
327 spin_lock_irqsave(&rtas.lock, s);
328 rtas_args = &rtas.args;
330 rtas_args->token = token;
331 rtas_args->nargs = nargs;
332 rtas_args->nret = nret;
333 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
334 va_start(list, outputs);
335 for (i = 0; i < nargs; ++i)
336 rtas_args->args[i] = va_arg(list, rtas_arg_t);
337 va_end(list);
339 for (i = 0; i < nret; ++i)
340 rtas_args->rets[i] = 0;
342 enter_rtas(__pa(rtas_args));
344 /* A -1 return code indicates that the last command couldn't
345 be completed due to a hardware error. */
346 if (rtas_args->rets[0] == -1)
347 buff_copy = __fetch_rtas_last_error(NULL);
349 if (nret > 1 && outputs != NULL)
350 for (i = 0; i < nret-1; ++i)
351 outputs[i] = rtas_args->rets[i+1];
352 ret = (nret > 0)? rtas_args->rets[0]: 0;
354 /* Gotta do something different here, use global lock for now... */
355 spin_unlock_irqrestore(&rtas.lock, s);
357 if (buff_copy) {
358 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
359 if (mem_init_done)
360 kfree(buff_copy);
362 return ret;
365 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
366 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
368 unsigned int rtas_extended_busy_delay_time(int status)
370 int order = status - 9900;
371 unsigned long ms;
373 if (order < 0)
374 order = 0; /* RTC depends on this for -2 clock busy */
375 else if (order > 5)
376 order = 5; /* bound */
378 /* Use microseconds for reasonable accuracy */
379 for (ms = 1; order > 0; order--)
380 ms *= 10;
382 return ms;
385 int rtas_error_rc(int rtas_rc)
387 int rc;
389 switch (rtas_rc) {
390 case -1: /* Hardware Error */
391 rc = -EIO;
392 break;
393 case -3: /* Bad indicator/domain/etc */
394 rc = -EINVAL;
395 break;
396 case -9000: /* Isolation error */
397 rc = -EFAULT;
398 break;
399 case -9001: /* Outstanding TCE/PTE */
400 rc = -EEXIST;
401 break;
402 case -9002: /* No usable slot */
403 rc = -ENODEV;
404 break;
405 default:
406 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
407 __FUNCTION__, rtas_rc);
408 rc = -ERANGE;
409 break;
411 return rc;
414 int rtas_get_power_level(int powerdomain, int *level)
416 int token = rtas_token("get-power-level");
417 int rc;
419 if (token == RTAS_UNKNOWN_SERVICE)
420 return -ENOENT;
422 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
423 udelay(1);
425 if (rc < 0)
426 return rtas_error_rc(rc);
427 return rc;
430 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
432 int token = rtas_token("set-power-level");
433 unsigned int wait_time;
434 int rc;
436 if (token == RTAS_UNKNOWN_SERVICE)
437 return -ENOENT;
439 while (1) {
440 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
441 if (rc == RTAS_BUSY)
442 udelay(1);
443 else if (rtas_is_extended_busy(rc)) {
444 wait_time = rtas_extended_busy_delay_time(rc);
445 udelay(wait_time * 1000);
446 } else
447 break;
450 if (rc < 0)
451 return rtas_error_rc(rc);
452 return rc;
455 int rtas_get_sensor(int sensor, int index, int *state)
457 int token = rtas_token("get-sensor-state");
458 unsigned int wait_time;
459 int rc;
461 if (token == RTAS_UNKNOWN_SERVICE)
462 return -ENOENT;
464 while (1) {
465 rc = rtas_call(token, 2, 2, state, sensor, index);
466 if (rc == RTAS_BUSY)
467 udelay(1);
468 else if (rtas_is_extended_busy(rc)) {
469 wait_time = rtas_extended_busy_delay_time(rc);
470 udelay(wait_time * 1000);
471 } else
472 break;
475 if (rc < 0)
476 return rtas_error_rc(rc);
477 return rc;
480 int rtas_set_indicator(int indicator, int index, int new_value)
482 int token = rtas_token("set-indicator");
483 unsigned int wait_time;
484 int rc;
486 if (token == RTAS_UNKNOWN_SERVICE)
487 return -ENOENT;
489 while (1) {
490 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
491 if (rc == RTAS_BUSY)
492 udelay(1);
493 else if (rtas_is_extended_busy(rc)) {
494 wait_time = rtas_extended_busy_delay_time(rc);
495 udelay(wait_time * 1000);
497 else
498 break;
501 if (rc < 0)
502 return rtas_error_rc(rc);
503 return rc;
506 void rtas_restart(char *cmd)
508 if (rtas_flash_term_hook)
509 rtas_flash_term_hook(SYS_RESTART);
510 printk("RTAS system-reboot returned %d\n",
511 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
512 for (;;);
515 void rtas_power_off(void)
517 if (rtas_flash_term_hook)
518 rtas_flash_term_hook(SYS_POWER_OFF);
519 /* allow power on only with power button press */
520 printk("RTAS power-off returned %d\n",
521 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
522 for (;;);
525 void rtas_halt(void)
527 if (rtas_flash_term_hook)
528 rtas_flash_term_hook(SYS_HALT);
529 /* allow power on only with power button press */
530 printk("RTAS power-off returned %d\n",
531 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
532 for (;;);
535 /* Must be in the RMO region, so we place it here */
536 static char rtas_os_term_buf[2048];
538 void rtas_os_term(char *str)
540 int status;
542 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
543 return;
545 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
547 do {
548 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
549 __pa(rtas_os_term_buf));
551 if (status == RTAS_BUSY)
552 udelay(1);
553 else if (status != 0)
554 printk(KERN_EMERG "ibm,os-term call failed %d\n",
555 status);
556 } while (status == RTAS_BUSY);
560 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
562 struct rtas_args args;
563 unsigned long flags;
564 char *buff_copy, *errbuf = NULL;
565 int nargs;
567 if (!capable(CAP_SYS_ADMIN))
568 return -EPERM;
570 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
571 return -EFAULT;
573 nargs = args.nargs;
574 if (nargs > ARRAY_SIZE(args.args)
575 || args.nret > ARRAY_SIZE(args.args)
576 || nargs + args.nret > ARRAY_SIZE(args.args))
577 return -EINVAL;
579 /* Copy in args. */
580 if (copy_from_user(args.args, uargs->args,
581 nargs * sizeof(rtas_arg_t)) != 0)
582 return -EFAULT;
584 buff_copy = get_errorlog_buffer();
586 spin_lock_irqsave(&rtas.lock, flags);
588 rtas.args = args;
589 enter_rtas(__pa(&rtas.args));
590 args = rtas.args;
592 args.rets = &args.args[nargs];
594 /* A -1 return code indicates that the last command couldn't
595 be completed due to a hardware error. */
596 if (args.rets[0] == -1)
597 errbuf = __fetch_rtas_last_error(buff_copy);
599 spin_unlock_irqrestore(&rtas.lock, flags);
601 if (buff_copy) {
602 if (errbuf)
603 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
604 kfree(buff_copy);
607 /* Copy out args. */
608 if (copy_to_user(uargs->args + nargs,
609 args.args + nargs,
610 args.nret * sizeof(rtas_arg_t)) != 0)
611 return -EFAULT;
613 return 0;
616 /* This version can't take the spinlock, because it never returns */
618 struct rtas_args rtas_stop_self_args = {
619 /* The token is initialized for real in setup_system() */
620 .token = RTAS_UNKNOWN_SERVICE,
621 .nargs = 0,
622 .nret = 1,
623 .rets = &rtas_stop_self_args.args[0],
626 void rtas_stop_self(void)
628 struct rtas_args *rtas_args = &rtas_stop_self_args;
630 local_irq_disable();
632 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
634 printk("cpu %u (hwid %u) Ready to die...\n",
635 smp_processor_id(), hard_smp_processor_id());
636 enter_rtas(__pa(rtas_args));
638 panic("Alas, I survived.\n");
642 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
643 * informations from the device-tree and allocate the RMO buffer for userland
644 * accesses.
646 void __init rtas_initialize(void)
648 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
650 /* Get RTAS dev node and fill up our "rtas" structure with infos
651 * about it.
653 rtas.dev = of_find_node_by_name(NULL, "rtas");
654 if (rtas.dev) {
655 u32 *basep, *entryp;
656 u32 *sizep;
658 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
659 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
660 if (basep != NULL && sizep != NULL) {
661 rtas.base = *basep;
662 rtas.size = *sizep;
663 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
664 if (entryp == NULL) /* Ugh */
665 rtas.entry = rtas.base;
666 else
667 rtas.entry = *entryp;
668 } else
669 rtas.dev = NULL;
671 if (!rtas.dev)
672 return;
674 /* If RTAS was found, allocate the RMO buffer for it and look for
675 * the stop-self token if any
677 #ifdef CONFIG_PPC64
678 if (_machine == PLATFORM_PSERIES_LPAR)
679 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
680 #endif
681 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
683 #ifdef CONFIG_HOTPLUG_CPU
684 rtas_stop_self_args.token = rtas_token("stop-self");
685 #endif /* CONFIG_HOTPLUG_CPU */
686 #ifdef CONFIG_RTAS_ERROR_LOGGING
687 rtas_last_error_token = rtas_token("rtas-last-error");
688 #endif
692 EXPORT_SYMBOL(rtas_token);
693 EXPORT_SYMBOL(rtas_call);
694 EXPORT_SYMBOL(rtas_data_buf);
695 EXPORT_SYMBOL(rtas_data_buf_lock);
696 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
697 EXPORT_SYMBOL(rtas_get_sensor);
698 EXPORT_SYMBOL(rtas_get_power_level);
699 EXPORT_SYMBOL(rtas_set_power_level);
700 EXPORT_SYMBOL(rtas_set_indicator);