[PATCH] powerpc: declare arch syscalls in <asm/syscalls.h>
[linux-2.6/sactl.git] / arch / powerpc / kernel / rtas.c
blob142d818a31a6169c119ef00b498d6c0dee01c3d9
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/hvcall.h>
26 #include <asm/semaphore.h>
27 #include <asm/machdep.h>
28 #include <asm/page.h>
29 #include <asm/param.h>
30 #include <asm/system.h>
31 #include <asm/delay.h>
32 #include <asm/uaccess.h>
33 #include <asm/lmb.h>
34 #include <asm/udbg.h>
35 #include <asm/syscalls.h>
37 struct rtas_t rtas = {
38 .lock = SPIN_LOCK_UNLOCKED
41 struct rtas_suspend_me_data {
42 long waiting;
43 struct rtas_args *args;
46 EXPORT_SYMBOL(rtas);
48 DEFINE_SPINLOCK(rtas_data_buf_lock);
49 char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
50 unsigned long rtas_rmo_buf;
53 * If non-NULL, this gets called when the kernel terminates.
54 * This is done like this so rtas_flash can be a module.
56 void (*rtas_flash_term_hook)(int);
57 EXPORT_SYMBOL(rtas_flash_term_hook);
60 * call_rtas_display_status and call_rtas_display_status_delay
61 * are designed only for very early low-level debugging, which
62 * is why the token is hard-coded to 10.
64 static void call_rtas_display_status(char c)
66 struct rtas_args *args = &rtas.args;
67 unsigned long s;
69 if (!rtas.base)
70 return;
71 spin_lock_irqsave(&rtas.lock, s);
73 args->token = 10;
74 args->nargs = 1;
75 args->nret = 1;
76 args->rets = (rtas_arg_t *)&(args->args[1]);
77 args->args[0] = (unsigned char)c;
79 enter_rtas(__pa(args));
81 spin_unlock_irqrestore(&rtas.lock, s);
84 static void call_rtas_display_status_delay(char c)
86 static int pending_newline = 0; /* did last write end with unprinted newline? */
87 static int width = 16;
89 if (c == '\n') {
90 while (width-- > 0)
91 call_rtas_display_status(' ');
92 width = 16;
93 mdelay(500);
94 pending_newline = 1;
95 } else {
96 if (pending_newline) {
97 call_rtas_display_status('\r');
98 call_rtas_display_status('\n');
100 pending_newline = 0;
101 if (width--) {
102 call_rtas_display_status(c);
103 udelay(10000);
108 void __init udbg_init_rtas(void)
110 udbg_putc = call_rtas_display_status_delay;
113 void rtas_progress(char *s, unsigned short hex)
115 struct device_node *root;
116 int width, *p;
117 char *os;
118 static int display_character, set_indicator;
119 static int display_width, display_lines, *row_width, form_feed;
120 static DEFINE_SPINLOCK(progress_lock);
121 static int current_line;
122 static int pending_newline = 0; /* did last write end with unprinted newline? */
124 if (!rtas.base)
125 return;
127 if (display_width == 0) {
128 display_width = 0x10;
129 if ((root = find_path_device("/rtas"))) {
130 if ((p = (unsigned int *)get_property(root,
131 "ibm,display-line-length", NULL)))
132 display_width = *p;
133 if ((p = (unsigned int *)get_property(root,
134 "ibm,form-feed", NULL)))
135 form_feed = *p;
136 if ((p = (unsigned int *)get_property(root,
137 "ibm,display-number-of-lines", NULL)))
138 display_lines = *p;
139 row_width = (unsigned int *)get_property(root,
140 "ibm,display-truncation-length", NULL);
142 display_character = rtas_token("display-character");
143 set_indicator = rtas_token("set-indicator");
146 if (display_character == RTAS_UNKNOWN_SERVICE) {
147 /* use hex display if available */
148 if (set_indicator != RTAS_UNKNOWN_SERVICE)
149 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
150 return;
153 spin_lock(&progress_lock);
156 * Last write ended with newline, but we didn't print it since
157 * it would just clear the bottom line of output. Print it now
158 * instead.
160 * If no newline is pending and form feed is supported, clear the
161 * display with a form feed; otherwise, print a CR to start output
162 * at the beginning of the line.
164 if (pending_newline) {
165 rtas_call(display_character, 1, 1, NULL, '\r');
166 rtas_call(display_character, 1, 1, NULL, '\n');
167 pending_newline = 0;
168 } else {
169 current_line = 0;
170 if (form_feed)
171 rtas_call(display_character, 1, 1, NULL,
172 (char)form_feed);
173 else
174 rtas_call(display_character, 1, 1, NULL, '\r');
177 if (row_width)
178 width = row_width[current_line];
179 else
180 width = display_width;
181 os = s;
182 while (*os) {
183 if (*os == '\n' || *os == '\r') {
184 /* If newline is the last character, save it
185 * until next call to avoid bumping up the
186 * display output.
188 if (*os == '\n' && !os[1]) {
189 pending_newline = 1;
190 current_line++;
191 if (current_line > display_lines-1)
192 current_line = display_lines-1;
193 spin_unlock(&progress_lock);
194 return;
197 /* RTAS wants CR-LF, not just LF */
199 if (*os == '\n') {
200 rtas_call(display_character, 1, 1, NULL, '\r');
201 rtas_call(display_character, 1, 1, NULL, '\n');
202 } else {
203 /* CR might be used to re-draw a line, so we'll
204 * leave it alone and not add LF.
206 rtas_call(display_character, 1, 1, NULL, *os);
209 if (row_width)
210 width = row_width[current_line];
211 else
212 width = display_width;
213 } else {
214 width--;
215 rtas_call(display_character, 1, 1, NULL, *os);
218 os++;
220 /* if we overwrite the screen length */
221 if (width <= 0)
222 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
223 os++;
226 spin_unlock(&progress_lock);
228 EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
230 int rtas_token(const char *service)
232 int *tokp;
233 if (rtas.dev == NULL)
234 return RTAS_UNKNOWN_SERVICE;
235 tokp = (int *) get_property(rtas.dev, service, NULL);
236 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
239 #ifdef CONFIG_RTAS_ERROR_LOGGING
241 * Return the firmware-specified size of the error log buffer
242 * for all rtas calls that require an error buffer argument.
243 * This includes 'check-exception' and 'rtas-last-error'.
245 int rtas_get_error_log_max(void)
247 static int rtas_error_log_max;
248 if (rtas_error_log_max)
249 return rtas_error_log_max;
251 rtas_error_log_max = rtas_token ("rtas-error-log-max");
252 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
253 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
254 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
255 rtas_error_log_max);
256 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
258 return rtas_error_log_max;
260 EXPORT_SYMBOL(rtas_get_error_log_max);
263 char rtas_err_buf[RTAS_ERROR_LOG_MAX];
264 int rtas_last_error_token;
266 /** Return a copy of the detailed error text associated with the
267 * most recent failed call to rtas. Because the error text
268 * might go stale if there are any other intervening rtas calls,
269 * this routine must be called atomically with whatever produced
270 * the error (i.e. with rtas.lock still held from the previous call).
272 static char *__fetch_rtas_last_error(char *altbuf)
274 struct rtas_args err_args, save_args;
275 u32 bufsz;
276 char *buf = NULL;
278 if (rtas_last_error_token == -1)
279 return NULL;
281 bufsz = rtas_get_error_log_max();
283 err_args.token = rtas_last_error_token;
284 err_args.nargs = 2;
285 err_args.nret = 1;
286 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
287 err_args.args[1] = bufsz;
288 err_args.args[2] = 0;
290 save_args = rtas.args;
291 rtas.args = err_args;
293 enter_rtas(__pa(&rtas.args));
295 err_args = rtas.args;
296 rtas.args = save_args;
298 /* Log the error in the unlikely case that there was one. */
299 if (unlikely(err_args.args[2] == 0)) {
300 if (altbuf) {
301 buf = altbuf;
302 } else {
303 buf = rtas_err_buf;
304 if (mem_init_done)
305 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
307 if (buf)
308 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
311 return buf;
314 #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
316 #else /* CONFIG_RTAS_ERROR_LOGGING */
317 #define __fetch_rtas_last_error(x) NULL
318 #define get_errorlog_buffer() NULL
319 #endif
321 int rtas_call(int token, int nargs, int nret, int *outputs, ...)
323 va_list list;
324 int i;
325 unsigned long s;
326 struct rtas_args *rtas_args;
327 char *buff_copy = NULL;
328 int ret;
330 if (token == RTAS_UNKNOWN_SERVICE)
331 return -1;
333 /* Gotta do something different here, use global lock for now... */
334 spin_lock_irqsave(&rtas.lock, s);
335 rtas_args = &rtas.args;
337 rtas_args->token = token;
338 rtas_args->nargs = nargs;
339 rtas_args->nret = nret;
340 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
341 va_start(list, outputs);
342 for (i = 0; i < nargs; ++i)
343 rtas_args->args[i] = va_arg(list, rtas_arg_t);
344 va_end(list);
346 for (i = 0; i < nret; ++i)
347 rtas_args->rets[i] = 0;
349 enter_rtas(__pa(rtas_args));
351 /* A -1 return code indicates that the last command couldn't
352 be completed due to a hardware error. */
353 if (rtas_args->rets[0] == -1)
354 buff_copy = __fetch_rtas_last_error(NULL);
356 if (nret > 1 && outputs != NULL)
357 for (i = 0; i < nret-1; ++i)
358 outputs[i] = rtas_args->rets[i+1];
359 ret = (nret > 0)? rtas_args->rets[0]: 0;
361 /* Gotta do something different here, use global lock for now... */
362 spin_unlock_irqrestore(&rtas.lock, s);
364 if (buff_copy) {
365 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
366 if (mem_init_done)
367 kfree(buff_copy);
369 return ret;
372 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
373 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
375 unsigned int rtas_extended_busy_delay_time(int status)
377 int order = status - 9900;
378 unsigned long ms;
380 if (order < 0)
381 order = 0; /* RTC depends on this for -2 clock busy */
382 else if (order > 5)
383 order = 5; /* bound */
385 /* Use microseconds for reasonable accuracy */
386 for (ms = 1; order > 0; order--)
387 ms *= 10;
389 return ms;
392 int rtas_error_rc(int rtas_rc)
394 int rc;
396 switch (rtas_rc) {
397 case -1: /* Hardware Error */
398 rc = -EIO;
399 break;
400 case -3: /* Bad indicator/domain/etc */
401 rc = -EINVAL;
402 break;
403 case -9000: /* Isolation error */
404 rc = -EFAULT;
405 break;
406 case -9001: /* Outstanding TCE/PTE */
407 rc = -EEXIST;
408 break;
409 case -9002: /* No usable slot */
410 rc = -ENODEV;
411 break;
412 default:
413 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
414 __FUNCTION__, rtas_rc);
415 rc = -ERANGE;
416 break;
418 return rc;
421 int rtas_get_power_level(int powerdomain, int *level)
423 int token = rtas_token("get-power-level");
424 int rc;
426 if (token == RTAS_UNKNOWN_SERVICE)
427 return -ENOENT;
429 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
430 udelay(1);
432 if (rc < 0)
433 return rtas_error_rc(rc);
434 return rc;
437 int rtas_set_power_level(int powerdomain, int level, int *setlevel)
439 int token = rtas_token("set-power-level");
440 unsigned int wait_time;
441 int rc;
443 if (token == RTAS_UNKNOWN_SERVICE)
444 return -ENOENT;
446 while (1) {
447 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
448 if (rc == RTAS_BUSY)
449 udelay(1);
450 else if (rtas_is_extended_busy(rc)) {
451 wait_time = rtas_extended_busy_delay_time(rc);
452 udelay(wait_time * 1000);
453 } else
454 break;
457 if (rc < 0)
458 return rtas_error_rc(rc);
459 return rc;
462 int rtas_get_sensor(int sensor, int index, int *state)
464 int token = rtas_token("get-sensor-state");
465 unsigned int wait_time;
466 int rc;
468 if (token == RTAS_UNKNOWN_SERVICE)
469 return -ENOENT;
471 while (1) {
472 rc = rtas_call(token, 2, 2, state, sensor, index);
473 if (rc == RTAS_BUSY)
474 udelay(1);
475 else if (rtas_is_extended_busy(rc)) {
476 wait_time = rtas_extended_busy_delay_time(rc);
477 udelay(wait_time * 1000);
478 } else
479 break;
482 if (rc < 0)
483 return rtas_error_rc(rc);
484 return rc;
487 int rtas_set_indicator(int indicator, int index, int new_value)
489 int token = rtas_token("set-indicator");
490 unsigned int wait_time;
491 int rc;
493 if (token == RTAS_UNKNOWN_SERVICE)
494 return -ENOENT;
496 while (1) {
497 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
498 if (rc == RTAS_BUSY)
499 udelay(1);
500 else if (rtas_is_extended_busy(rc)) {
501 wait_time = rtas_extended_busy_delay_time(rc);
502 udelay(wait_time * 1000);
504 else
505 break;
508 if (rc < 0)
509 return rtas_error_rc(rc);
510 return rc;
513 void rtas_restart(char *cmd)
515 if (rtas_flash_term_hook)
516 rtas_flash_term_hook(SYS_RESTART);
517 printk("RTAS system-reboot returned %d\n",
518 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
519 for (;;);
522 void rtas_power_off(void)
524 if (rtas_flash_term_hook)
525 rtas_flash_term_hook(SYS_POWER_OFF);
526 /* allow power on only with power button press */
527 printk("RTAS power-off returned %d\n",
528 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
529 for (;;);
532 void rtas_halt(void)
534 if (rtas_flash_term_hook)
535 rtas_flash_term_hook(SYS_HALT);
536 /* allow power on only with power button press */
537 printk("RTAS power-off returned %d\n",
538 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
539 for (;;);
542 /* Must be in the RMO region, so we place it here */
543 static char rtas_os_term_buf[2048];
545 void rtas_os_term(char *str)
547 int status;
549 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
550 return;
552 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
554 do {
555 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
556 __pa(rtas_os_term_buf));
558 if (status == RTAS_BUSY)
559 udelay(1);
560 else if (status != 0)
561 printk(KERN_EMERG "ibm,os-term call failed %d\n",
562 status);
563 } while (status == RTAS_BUSY);
566 static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
567 #ifdef CONFIG_PPC_PSERIES
568 static void rtas_percpu_suspend_me(void *info)
570 int i;
571 long rc;
572 long flags;
573 struct rtas_suspend_me_data *data =
574 (struct rtas_suspend_me_data *)info;
577 * We use "waiting" to indicate our state. As long
578 * as it is >0, we are still trying to all join up.
579 * If it goes to 0, we have successfully joined up and
580 * one thread got H_Continue. If any error happens,
581 * we set it to <0.
583 local_irq_save(flags);
584 do {
585 rc = plpar_hcall_norets(H_JOIN);
586 smp_rmb();
587 } while (rc == H_Success && data->waiting > 0);
588 if (rc == H_Success)
589 goto out;
591 if (rc == H_Continue) {
592 data->waiting = 0;
593 data->args->args[data->args->nargs] =
594 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
595 for_each_cpu(i)
596 plpar_hcall_norets(H_PROD,i);
597 } else {
598 data->waiting = -EBUSY;
599 printk(KERN_ERR "Error on H_Join hypervisor call\n");
602 out:
603 local_irq_restore(flags);
604 return;
607 static int rtas_ibm_suspend_me(struct rtas_args *args)
609 int i;
611 struct rtas_suspend_me_data data;
613 data.waiting = 1;
614 data.args = args;
616 /* Call function on all CPUs. One of us will make the
617 * rtas call
619 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
620 data.waiting = -EINVAL;
622 if (data.waiting != 0)
623 printk(KERN_ERR "Error doing global join\n");
625 /* Prod each CPU. This won't hurt, and will wake
626 * anyone we successfully put to sleep with H_Join
628 for_each_cpu(i)
629 plpar_hcall_norets(H_PROD, i);
631 return data.waiting;
633 #else /* CONFIG_PPC_PSERIES */
634 static int rtas_ibm_suspend_me(struct rtas_args *args)
636 return -ENOSYS;
638 #endif
640 asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
642 struct rtas_args args;
643 unsigned long flags;
644 char *buff_copy, *errbuf = NULL;
645 int nargs;
646 int rc;
648 if (!capable(CAP_SYS_ADMIN))
649 return -EPERM;
651 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
652 return -EFAULT;
654 nargs = args.nargs;
655 if (nargs > ARRAY_SIZE(args.args)
656 || args.nret > ARRAY_SIZE(args.args)
657 || nargs + args.nret > ARRAY_SIZE(args.args))
658 return -EINVAL;
660 /* Copy in args. */
661 if (copy_from_user(args.args, uargs->args,
662 nargs * sizeof(rtas_arg_t)) != 0)
663 return -EFAULT;
665 if (args.token == RTAS_UNKNOWN_SERVICE)
666 return -EINVAL;
668 /* Need to handle ibm,suspend_me call specially */
669 if (args.token == ibm_suspend_me_token) {
670 rc = rtas_ibm_suspend_me(&args);
671 if (rc)
672 return rc;
673 goto copy_return;
676 buff_copy = get_errorlog_buffer();
678 spin_lock_irqsave(&rtas.lock, flags);
680 rtas.args = args;
681 enter_rtas(__pa(&rtas.args));
682 args = rtas.args;
684 args.rets = &args.args[nargs];
686 /* A -1 return code indicates that the last command couldn't
687 be completed due to a hardware error. */
688 if (args.rets[0] == -1)
689 errbuf = __fetch_rtas_last_error(buff_copy);
691 spin_unlock_irqrestore(&rtas.lock, flags);
693 if (buff_copy) {
694 if (errbuf)
695 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
696 kfree(buff_copy);
699 copy_return:
700 /* Copy out args. */
701 if (copy_to_user(uargs->args + nargs,
702 args.args + nargs,
703 args.nret * sizeof(rtas_arg_t)) != 0)
704 return -EFAULT;
706 return 0;
709 /* This version can't take the spinlock, because it never returns */
711 struct rtas_args rtas_stop_self_args = {
712 /* The token is initialized for real in setup_system() */
713 .token = RTAS_UNKNOWN_SERVICE,
714 .nargs = 0,
715 .nret = 1,
716 .rets = &rtas_stop_self_args.args[0],
719 void rtas_stop_self(void)
721 struct rtas_args *rtas_args = &rtas_stop_self_args;
723 local_irq_disable();
725 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
727 printk("cpu %u (hwid %u) Ready to die...\n",
728 smp_processor_id(), hard_smp_processor_id());
729 enter_rtas(__pa(rtas_args));
731 panic("Alas, I survived.\n");
735 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
736 * informations from the device-tree and allocate the RMO buffer for userland
737 * accesses.
739 void __init rtas_initialize(void)
741 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
743 /* Get RTAS dev node and fill up our "rtas" structure with infos
744 * about it.
746 rtas.dev = of_find_node_by_name(NULL, "rtas");
747 if (rtas.dev) {
748 u32 *basep, *entryp;
749 u32 *sizep;
751 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
752 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
753 if (basep != NULL && sizep != NULL) {
754 rtas.base = *basep;
755 rtas.size = *sizep;
756 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
757 if (entryp == NULL) /* Ugh */
758 rtas.entry = rtas.base;
759 else
760 rtas.entry = *entryp;
761 } else
762 rtas.dev = NULL;
764 if (!rtas.dev)
765 return;
767 /* If RTAS was found, allocate the RMO buffer for it and look for
768 * the stop-self token if any
770 #ifdef CONFIG_PPC64
771 if (_machine == PLATFORM_PSERIES_LPAR) {
772 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
773 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
775 #endif
776 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
778 #ifdef CONFIG_HOTPLUG_CPU
779 rtas_stop_self_args.token = rtas_token("stop-self");
780 #endif /* CONFIG_HOTPLUG_CPU */
781 #ifdef CONFIG_RTAS_ERROR_LOGGING
782 rtas_last_error_token = rtas_token("rtas-last-error");
783 #endif
787 EXPORT_SYMBOL(rtas_token);
788 EXPORT_SYMBOL(rtas_call);
789 EXPORT_SYMBOL(rtas_data_buf);
790 EXPORT_SYMBOL(rtas_data_buf_lock);
791 EXPORT_SYMBOL(rtas_extended_busy_delay_time);
792 EXPORT_SYMBOL(rtas_get_sensor);
793 EXPORT_SYMBOL(rtas_get_power_level);
794 EXPORT_SYMBOL(rtas_set_power_level);
795 EXPORT_SYMBOL(rtas_set_indicator);