aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc/kernel/rtas.c
diff options
context:
space:
mode:
authorPaul Mackerras <paulus@samba.org>2005-10-26 03:05:24 -0400
committerPaul Mackerras <paulus@samba.org>2005-10-26 03:05:24 -0400
commit033ef338b6e007dc081c6282a4f2a9dd761f8cd2 (patch)
tree3c77fad71c3d9ba04ddcdaea33063aaf7520ddb0 /arch/powerpc/kernel/rtas.c
parentf9bd170a87948a9e077149b70fb192c563770fdf (diff)
powerpc: Merge rtas.c into arch/powerpc/kernel
This splits arch/ppc64/kernel/rtas.c into arch/powerpc/kernel/rtas.c, which contains generic RTAS functions useful on any CHRP platform, and arch/powerpc/platforms/pseries/rtas-fw.[ch], which contain some pSeries-specific firmware flashing bits. The parts of rtas.c that are to do with pSeries-specific error logging are protected by a new CONFIG_RTAS_ERROR_LOGGING symbol. The inclusion of rtas.o is controlled by the CONFIG_PPC_RTAS symbol, and the relevant platforms select that. Signed-off-by: Paul Mackerras <paulus@samba.org>
Diffstat (limited to 'arch/powerpc/kernel/rtas.c')
-rw-r--r--arch/powerpc/kernel/rtas.c680
1 files changed, 680 insertions, 0 deletions
diff --git a/arch/powerpc/kernel/rtas.c b/arch/powerpc/kernel/rtas.c
new file mode 100644
index 000000000000..4d22eeeeb91d
--- /dev/null
+++ b/arch/powerpc/kernel/rtas.c
@@ -0,0 +1,680 @@
1/*
2 *
3 * Procedures for interfacing to the RTAS on CHRP machines.
4 *
5 * Peter Bergner, IBM March 2001.
6 * Copyright (C) 2001 IBM.
7 *
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.
12 */
13
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
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
34
35struct rtas_t rtas = {
36 .lock = SPIN_LOCK_UNLOCKED
37};
38
39EXPORT_SYMBOL(rtas);
40
41DEFINE_SPINLOCK(rtas_data_buf_lock);
42char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
43unsigned long rtas_rmo_buf;
44
45/*
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.
49 */
50void call_rtas_display_status(unsigned char c)
51{
52 struct rtas_args *args = &rtas.args;
53 unsigned long s;
54
55 if (!rtas.base)
56 return;
57 spin_lock_irqsave(&rtas.lock, s);
58
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;
64
65 enter_rtas(__pa(args));
66
67 spin_unlock_irqrestore(&rtas.lock, s);
68}
69
70void call_rtas_display_status_delay(unsigned char c)
71{
72 static int pending_newline = 0; /* did last write end with unprinted newline? */
73 static int width = 16;
74
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');
85 }
86 pending_newline = 0;
87 if (width--) {
88 call_rtas_display_status(c);
89 udelay(10000);
90 }
91 }
92}
93
94void rtas_progress(char *s, unsigned short hex)
95{
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? */
104
105 if (!rtas.base)
106 return;
107
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);
122 }
123 display_character = rtas_token("display-character");
124 set_indicator = rtas_token("set-indicator");
125 }
126
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;
132 }
133
134 spin_lock(&progress_lock);
135
136 /*
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.
140 *
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.
144 */
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');
156 }
157
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.
168 */
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;
176 }
177
178 /* RTAS wants CR-LF, not just LF */
179
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.
186 */
187 rtas_call(display_character, 1, 1, NULL, *os);
188 }
189
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);
197 }
198
199 os++;
200
201 /* if we overwrite the screen length */
202 if (width <= 0)
203 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
204 os++;
205 }
206
207 spin_unlock(&progress_lock);
208}
209
210int rtas_token(const char *service)
211{
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;
217}
218
219#ifdef CONFIG_RTAS_ERROR_LOGGING
220/*
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'.
224 */
225int rtas_get_error_log_max(void)
226{
227 static int rtas_error_log_max;
228 if (rtas_error_log_max)
229 return rtas_error_log_max;
230
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;
237 }
238 return rtas_error_log_max;
239}
240EXPORT_SYMBOL(rtas_get_error_log_max);
241
242
243char rtas_err_buf[RTAS_ERROR_LOG_MAX];
244int rtas_last_error_token;
245
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).
251 */
252static char *__fetch_rtas_last_error(char *altbuf)
253{
254 struct rtas_args err_args, save_args;
255 u32 bufsz;
256 char *buf = NULL;
257
258 if (rtas_last_error_token == -1)
259 return NULL;
260
261 bufsz = rtas_get_error_log_max();
262
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;
269
270 save_args = rtas.args;
271 rtas.args = err_args;
272
273 enter_rtas(__pa(&rtas.args));
274
275 err_args = rtas.args;
276 rtas.args = save_args;
277
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);
286 }
287 if (buf)
288 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
289 }
290
291 return buf;
292}
293
294#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
295
296#else /* CONFIG_RTAS_ERROR_LOGGING */
297#define __fetch_rtas_last_error(x) NULL
298#define get_errorlog_buffer() NULL
299#endif
300
301int rtas_call(int token, int nargs, int nret, int *outputs, ...)
302{
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;
309
310 if (token == RTAS_UNKNOWN_SERVICE)
311 return -1;
312
313 /* Gotta do something different here, use global lock for now... */
314 spin_lock_irqsave(&rtas.lock, s);
315 rtas_args = &rtas.args;
316
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);
325
326 for (i = 0; i < nret; ++i)
327 rtas_args->rets[i] = 0;
328
329 enter_rtas(__pa(rtas_args));
330
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);
335
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;
340
341 /* Gotta do something different here, use global lock for now... */
342 spin_unlock_irqrestore(&rtas.lock, s);
343
344 if (buff_copy) {
345 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
346 if (mem_init_done)
347 kfree(buff_copy);
348 }
349 return ret;
350}
351
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).
354 */
355unsigned int rtas_extended_busy_delay_time(int status)
356{
357 int order = status - 9900;
358 unsigned long ms;
359
360 if (order < 0)
361 order = 0; /* RTC depends on this for -2 clock busy */
362 else if (order > 5)
363 order = 5; /* bound */
364
365 /* Use microseconds for reasonable accuracy */
366 for (ms = 1; order > 0; order--)
367 ms *= 10;
368
369 return ms;
370}
371
372int rtas_error_rc(int rtas_rc)
373{
374 int rc;
375
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;
397 }
398 return rc;
399}
400
401int rtas_get_power_level(int powerdomain, int *level)
402{
403 int token = rtas_token("get-power-level");
404 int rc;
405
406 if (token == RTAS_UNKNOWN_SERVICE)
407 return -ENOENT;
408
409 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
410 udelay(1);
411
412 if (rc < 0)
413 return rtas_error_rc(rc);
414 return rc;
415}
416
417int rtas_set_power_level(int powerdomain, int level, int *setlevel)
418{
419 int token = rtas_token("set-power-level");
420 unsigned int wait_time;
421 int rc;
422
423 if (token == RTAS_UNKNOWN_SERVICE)
424 return -ENOENT;
425
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;
435 }
436
437 if (rc < 0)
438 return rtas_error_rc(rc);
439 return rc;
440}
441
442int rtas_get_sensor(int sensor, int index, int *state)
443{
444 int token = rtas_token("get-sensor-state");
445 unsigned int wait_time;
446 int rc;
447
448 if (token == RTAS_UNKNOWN_SERVICE)
449 return -ENOENT;
450
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;
460 }
461
462 if (rc < 0)
463 return rtas_error_rc(rc);
464 return rc;
465}
466
467int rtas_set_indicator(int indicator, int index, int new_value)
468{
469 int token = rtas_token("set-indicator");
470 unsigned int wait_time;
471 int rc;
472
473 if (token == RTAS_UNKNOWN_SERVICE)
474 return -ENOENT;
475
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);
483 }
484 else
485 break;
486 }
487
488 if (rc < 0)
489 return rtas_error_rc(rc);
490 return rc;
491}
492
493void rtas_restart(char *cmd)
494{
495 printk("RTAS system-reboot returned %d\n",
496 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
497 for (;;);
498}
499
500void rtas_power_off(void)
501{
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 (;;);
506}
507
508void rtas_halt(void)
509{
510 rtas_power_off();
511}
512
513/* Must be in the RMO region, so we place it here */
514static char rtas_os_term_buf[2048];
515
516void rtas_os_term(char *str)
517{
518 int status;
519
520 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
521 return;
522
523 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
524
525 do {
526 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
527 __pa(rtas_os_term_buf));
528
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);
535}
536
537
538asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
539{
540 struct rtas_args args;
541 unsigned long flags;
542 char *buff_copy, *errbuf = NULL;
543 int nargs;
544
545 if (!capable(CAP_SYS_ADMIN))
546 return -EPERM;
547
548 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
549 return -EFAULT;
550
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;
556
557 /* Copy in args. */
558 if (copy_from_user(args.args, uargs->args,
559 nargs * sizeof(rtas_arg_t)) != 0)
560 return -EFAULT;
561
562 buff_copy = get_errorlog_buffer();
563
564 spin_lock_irqsave(&rtas.lock, flags);
565
566 rtas.args = args;
567 enter_rtas(__pa(&rtas.args));
568 args = rtas.args;
569
570 args.rets = &args.args[nargs];
571
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);
576
577 spin_unlock_irqrestore(&rtas.lock, flags);
578
579 if (buff_copy) {
580 if (errbuf)
581 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
582 kfree(buff_copy);
583 }
584
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;
590
591 return 0;
592}
593
594#ifdef CONFIG_SMP
595/* This version can't take the spinlock, because it never returns */
596
597struct 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],
603};
604
605void rtas_stop_self(void)
606{
607 struct rtas_args *rtas_args = &rtas_stop_self_args;
608
609 local_irq_disable();
610
611 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
612
613 printk("cpu %u (hwid %u) Ready to die...\n",
614 smp_processor_id(), hard_smp_processor_id());
615 enter_rtas(__pa(rtas_args));
616
617 panic("Alas, I survived.\n");
618}
619#endif
620
621/*
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.
625 */
626void __init rtas_initialize(void)
627{
628 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
629
630 /* Get RTAS dev node and fill up our "rtas" structure with infos
631 * about it.
632 */
633 rtas.dev = of_find_node_by_name(NULL, "rtas");
634 if (rtas.dev) {
635 u32 *basep, *entryp;
636 u32 *sizep;
637
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;
650 }
651 if (!rtas.dev)
652 return;
653
654 /* If RTAS was found, allocate the RMO buffer for it and look for
655 * the stop-self token if any
656 */
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);
662
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
669}
670
671
672EXPORT_SYMBOL(rtas_token);
673EXPORT_SYMBOL(rtas_call);
674EXPORT_SYMBOL(rtas_data_buf);
675EXPORT_SYMBOL(rtas_data_buf_lock);
676EXPORT_SYMBOL(rtas_extended_busy_delay_time);
677EXPORT_SYMBOL(rtas_get_sensor);
678EXPORT_SYMBOL(rtas_get_power_level);
679EXPORT_SYMBOL(rtas_set_power_level);
680EXPORT_SYMBOL(rtas_set_indicator);