blob: 142d818a31a6169c119ef00b498d6c0dee01c3d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Randy Dunlapa9415642006-01-11 12:17:48 -080020#include <linux/capability.h>
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +110021#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/prom.h>
24#include <asm/rtas.h>
Michael Ellerman31a7f672006-01-31 17:17:47 +110025#include <asm/hvcall.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/semaphore.h>
27#include <asm/machdep.h>
28#include <asm/page.h>
29#include <asm/param.h>
30#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/delay.h>
32#include <asm/uaccess.h>
Paul Mackerras033ef332005-10-26 17:05:24 +100033#include <asm/lmb.h>
Michael Ellerman296167a2006-01-11 11:54:09 +110034#include <asm/udbg.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010035#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Paul Mackerras033ef332005-10-26 17:05:24 +100037struct rtas_t rtas = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 .lock = SPIN_LOCK_UNLOCKED
39};
40
Dave C Boutcher91dc1822006-01-13 18:39:24 -060041struct rtas_suspend_me_data {
42 long waiting;
43 struct rtas_args *args;
44};
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046EXPORT_SYMBOL(rtas);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048DEFINE_SPINLOCK(rtas_data_buf_lock);
Paul Mackerras033ef332005-10-26 17:05:24 +100049char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050unsigned long rtas_rmo_buf;
51
Paul Mackerras033ef332005-10-26 17:05:24 +100052/*
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110053 * If non-NULL, this gets called when the kernel terminates.
54 * This is done like this so rtas_flash can be a module.
55 */
56void (*rtas_flash_term_hook)(int);
57EXPORT_SYMBOL(rtas_flash_term_hook);
58
59/*
Paul Mackerras033ef332005-10-26 17:05:24 +100060 * 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.
63 */
Michael Ellerman296167a2006-01-11 11:54:09 +110064static void call_rtas_display_status(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct rtas_args *args = &rtas.args;
67 unsigned long s;
68
69 if (!rtas.base)
70 return;
71 spin_lock_irqsave(&rtas.lock, s);
72
73 args->token = 10;
74 args->nargs = 1;
75 args->nret = 1;
76 args->rets = (rtas_arg_t *)&(args->args[1]);
Michael Ellerman296167a2006-01-11 11:54:09 +110077 args->args[0] = (unsigned char)c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 enter_rtas(__pa(args));
80
81 spin_unlock_irqrestore(&rtas.lock, s);
82}
83
Michael Ellerman296167a2006-01-11 11:54:09 +110084static void call_rtas_display_status_delay(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 static int pending_newline = 0; /* did last write end with unprinted newline? */
87 static int width = 16;
88
89 if (c == '\n') {
90 while (width-- > 0)
91 call_rtas_display_status(' ');
92 width = 16;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +110093 mdelay(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pending_newline = 1;
95 } else {
96 if (pending_newline) {
97 call_rtas_display_status('\r');
98 call_rtas_display_status('\n');
99 }
100 pending_newline = 0;
101 if (width--) {
102 call_rtas_display_status(c);
103 udelay(10000);
104 }
105 }
106}
107
Michael Ellerman296167a2006-01-11 11:54:09 +1100108void __init udbg_init_rtas(void)
109{
110 udbg_putc = call_rtas_display_status_delay;
111}
112
Paul Mackerras033ef332005-10-26 17:05:24 +1000113void rtas_progress(char *s, unsigned short hex)
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000114{
115 struct device_node *root;
116 int width, *p;
117 char *os;
118 static int display_character, set_indicator;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000119 static int display_width, display_lines, *row_width, form_feed;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000120 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000121 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000122 static int pending_newline = 0; /* did last write end with unprinted newline? */
123
124 if (!rtas.base)
125 return;
126
Mike Strosaker8f586b22005-06-23 16:09:41 +1000127 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);
141 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000142 display_character = rtas_token("display-character");
143 set_indicator = rtas_token("set-indicator");
144 }
145
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;
151 }
152
153 spin_lock(&progress_lock);
154
155 /*
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.
159 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000160 * 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.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000163 */
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 {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000169 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');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000175 }
176
Mike Strosaker8f586b22005-06-23 16:09:41 +1000177 if (row_width)
178 width = row_width[current_line];
179 else
180 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000181 os = s;
182 while (*os) {
183 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000184 /* If newline is the last character, save it
185 * until next call to avoid bumping up the
186 * display output.
187 */
188 if (*os == '\n' && !os[1]) {
189 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000190 current_line++;
191 if (current_line > display_lines-1)
192 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000193 spin_unlock(&progress_lock);
194 return;
195 }
196
197 /* RTAS wants CR-LF, not just LF */
198
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.
205 */
206 rtas_call(display_character, 1, 1, NULL, *os);
207 }
208
Mike Strosaker8f586b22005-06-23 16:09:41 +1000209 if (row_width)
210 width = row_width[current_line];
211 else
212 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000213 } else {
214 width--;
215 rtas_call(display_character, 1, 1, NULL, *os);
216 }
217
218 os++;
219
220 /* if we overwrite the screen length */
221 if (width <= 0)
222 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
223 os++;
224 }
225
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000226 spin_unlock(&progress_lock);
227}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100228EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000229
Paul Mackerras033ef332005-10-26 17:05:24 +1000230int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000233 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return RTAS_UNKNOWN_SERVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 tokp = (int *) get_property(rtas.dev, service, NULL);
236 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
237}
238
Paul Mackerras033ef332005-10-26 17:05:24 +1000239#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
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'.
244 */
245int rtas_get_error_log_max(void)
246{
247 static int rtas_error_log_max;
248 if (rtas_error_log_max)
249 return rtas_error_log_max;
250
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)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000254 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
255 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
257 }
258 return rtas_error_log_max;
259}
Paul Mackerras033ef332005-10-26 17:05:24 +1000260EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262
Paul Mackerras033ef332005-10-26 17:05:24 +1000263char rtas_err_buf[RTAS_ERROR_LOG_MAX];
264int rtas_last_error_token;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/** 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).
271 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000272static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct rtas_args err_args, save_args;
275 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000276 char *buf = NULL;
277
278 if (rtas_last_error_token == -1)
279 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 bufsz = rtas_get_error_log_max();
282
Paul Mackerras033ef332005-10-26 17:05:24 +1000283 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 err_args.nargs = 2;
285 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
287 err_args.args[1] = bufsz;
288 err_args.args[2] = 0;
289
290 save_args = rtas.args;
291 rtas.args = err_args;
292
293 enter_rtas(__pa(&rtas.args));
294
295 err_args = rtas.args;
296 rtas.args = save_args;
297
Paul Mackerras033ef332005-10-26 17:05:24 +1000298 /* 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);
306 }
307 if (buf)
308 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
309 }
310
311 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Paul Mackerras033ef332005-10-26 17:05:24 +1000314#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
315
316#else /* CONFIG_RTAS_ERROR_LOGGING */
317#define __fetch_rtas_last_error(x) NULL
318#define get_errorlog_buffer() NULL
319#endif
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321int rtas_call(int token, int nargs, int nret, int *outputs, ...)
322{
323 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000324 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned long s;
326 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000327 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 int ret;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (token == RTAS_UNKNOWN_SERVICE)
331 return -1;
332
333 /* Gotta do something different here, use global lock for now... */
334 spin_lock_irqsave(&rtas.lock, s);
335 rtas_args = &rtas.args;
336
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);
Paul Mackerras033ef332005-10-26 17:05:24 +1000342 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 va_end(list);
345
346 for (i = 0; i < nret; ++i)
347 rtas_args->rets[i] = 0;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
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)
Paul Mackerras033ef332005-10-26 17:05:24 +1000354 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
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;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Gotta do something different here, use global lock for now... */
362 spin_unlock_irqrestore(&rtas.lock, s);
363
364 if (buff_copy) {
365 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
366 if (mem_init_done)
367 kfree(buff_copy);
368 }
369 return ret;
370}
371
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).
374 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000375unsigned int rtas_extended_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 int order = status - 9900;
378 unsigned long ms;
379
380 if (order < 0)
381 order = 0; /* RTC depends on this for -2 clock busy */
382 else if (order > 5)
383 order = 5; /* bound */
384
385 /* Use microseconds for reasonable accuracy */
Paul Mackerras033ef332005-10-26 17:05:24 +1000386 for (ms = 1; order > 0; order--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ms *= 10;
388
389 return ms;
390}
391
392int rtas_error_rc(int rtas_rc)
393{
394 int rc;
395
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;
417 }
418 return rc;
419}
420
421int rtas_get_power_level(int powerdomain, int *level)
422{
423 int token = rtas_token("get-power-level");
424 int rc;
425
426 if (token == RTAS_UNKNOWN_SERVICE)
427 return -ENOENT;
428
429 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
430 udelay(1);
431
432 if (rc < 0)
433 return rtas_error_rc(rc);
434 return rc;
435}
436
437int rtas_set_power_level(int powerdomain, int level, int *setlevel)
438{
439 int token = rtas_token("set-power-level");
440 unsigned int wait_time;
441 int rc;
442
443 if (token == RTAS_UNKNOWN_SERVICE)
444 return -ENOENT;
445
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;
455 }
456
457 if (rc < 0)
458 return rtas_error_rc(rc);
459 return rc;
460}
461
462int rtas_get_sensor(int sensor, int index, int *state)
463{
464 int token = rtas_token("get-sensor-state");
465 unsigned int wait_time;
466 int rc;
467
468 if (token == RTAS_UNKNOWN_SERVICE)
469 return -ENOENT;
470
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;
480 }
481
482 if (rc < 0)
483 return rtas_error_rc(rc);
484 return rc;
485}
486
487int rtas_set_indicator(int indicator, int index, int new_value)
488{
489 int token = rtas_token("set-indicator");
490 unsigned int wait_time;
491 int rc;
492
493 if (token == RTAS_UNKNOWN_SERVICE)
494 return -ENOENT;
495
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);
503 }
504 else
505 break;
506 }
507
508 if (rc < 0)
509 return rtas_error_rc(rc);
510 return rc;
511}
512
Paul Mackerras033ef332005-10-26 17:05:24 +1000513void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100515 if (rtas_flash_term_hook)
516 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 printk("RTAS system-reboot returned %d\n",
518 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
519 for (;;);
520}
521
Paul Mackerras033ef332005-10-26 17:05:24 +1000522void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100524 if (rtas_flash_term_hook)
525 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* 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 (;;);
530}
531
Paul Mackerras033ef332005-10-26 17:05:24 +1000532void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100534 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 (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/* Must be in the RMO region, so we place it here */
543static char rtas_os_term_buf[2048];
544
545void rtas_os_term(char *str)
546{
547 int status;
548
549 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
550 return;
551
552 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
553
554 do {
555 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
556 __pa(rtas_os_term_buf));
557
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);
564}
565
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600566static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
567#ifdef CONFIG_PPC_PSERIES
568static void rtas_percpu_suspend_me(void *info)
569{
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600570 int i;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600571 long rc;
572 long flags;
573 struct rtas_suspend_me_data *data =
574 (struct rtas_suspend_me_data *)info;
575
576 /*
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.
582 */
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;
590
591 if (rc == H_Continue) {
592 data->waiting = 0;
Dave C Boutcherc4cb8ec2006-02-03 01:18:46 -0600593 data->args->args[data->args->nargs] =
594 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600595 for_each_cpu(i)
596 plpar_hcall_norets(H_PROD,i);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600597 } else {
598 data->waiting = -EBUSY;
599 printk(KERN_ERR "Error on H_Join hypervisor call\n");
600 }
601
602out:
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600603 local_irq_restore(flags);
604 return;
605}
606
607static int rtas_ibm_suspend_me(struct rtas_args *args)
608{
609 int i;
610
611 struct rtas_suspend_me_data data;
612
613 data.waiting = 1;
614 data.args = args;
615
616 /* Call function on all CPUs. One of us will make the
617 * rtas call
618 */
619 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
620 data.waiting = -EINVAL;
621
622 if (data.waiting != 0)
623 printk(KERN_ERR "Error doing global join\n");
624
625 /* Prod each CPU. This won't hurt, and will wake
626 * anyone we successfully put to sleep with H_Join
627 */
628 for_each_cpu(i)
629 plpar_hcall_norets(H_PROD, i);
630
631 return data.waiting;
632}
633#else /* CONFIG_PPC_PSERIES */
634static int rtas_ibm_suspend_me(struct rtas_args *args)
635{
636 return -ENOSYS;
637}
638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
641{
642 struct rtas_args args;
643 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000644 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int nargs;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600646 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (!capable(CAP_SYS_ADMIN))
649 return -EPERM;
650
651 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
652 return -EFAULT;
653
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;
659
660 /* Copy in args. */
661 if (copy_from_user(args.args, uargs->args,
662 nargs * sizeof(rtas_arg_t)) != 0)
663 return -EFAULT;
664
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600665 if (args.token == RTAS_UNKNOWN_SERVICE)
666 return -EINVAL;
667
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;
674 }
675
Paul Mackerras033ef332005-10-26 17:05:24 +1000676 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 spin_lock_irqsave(&rtas.lock, flags);
679
680 rtas.args = args;
681 enter_rtas(__pa(&rtas.args));
682 args = rtas.args;
683
684 args.rets = &args.args[nargs];
685
686 /* A -1 return code indicates that the last command couldn't
687 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000688 if (args.rets[0] == -1)
689 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 spin_unlock_irqrestore(&rtas.lock, flags);
692
693 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000694 if (errbuf)
695 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 kfree(buff_copy);
697 }
698
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600699 copy_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* 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;
705
706 return 0;
707}
708
709/* This version can't take the spinlock, because it never returns */
710
711struct 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],
717};
718
719void rtas_stop_self(void)
720{
721 struct rtas_args *rtas_args = &rtas_stop_self_args;
722
723 local_irq_disable();
724
725 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
726
727 printk("cpu %u (hwid %u) Ready to die...\n",
728 smp_processor_id(), hard_smp_processor_id());
729 enter_rtas(__pa(rtas_args));
730
731 panic("Alas, I survived.\n");
732}
733
734/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100735 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * informations from the device-tree and allocate the RMO buffer for userland
737 * accesses.
738 */
739void __init rtas_initialize(void)
740{
Paul Mackerras033ef332005-10-26 17:05:24 +1000741 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* Get RTAS dev node and fill up our "rtas" structure with infos
744 * about it.
745 */
746 rtas.dev = of_find_node_by_name(NULL, "rtas");
747 if (rtas.dev) {
748 u32 *basep, *entryp;
749 u32 *sizep;
750
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;
763 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000764 if (!rtas.dev)
765 return;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* If RTAS was found, allocate the RMO buffer for it and look for
768 * the stop-self token if any
769 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000770#ifdef CONFIG_PPC64
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600771 if (_machine == PLATFORM_PSERIES_LPAR) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000772 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600773 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
774 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000775#endif
776 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000779 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000781#ifdef CONFIG_RTAS_ERROR_LOGGING
782 rtas_last_error_token = rtas_token("rtas-last-error");
783#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787EXPORT_SYMBOL(rtas_token);
788EXPORT_SYMBOL(rtas_call);
789EXPORT_SYMBOL(rtas_data_buf);
790EXPORT_SYMBOL(rtas_data_buf_lock);
791EXPORT_SYMBOL(rtas_extended_busy_delay_time);
792EXPORT_SYMBOL(rtas_get_sensor);
793EXPORT_SYMBOL(rtas_get_power_level);
794EXPORT_SYMBOL(rtas_set_power_level);
795EXPORT_SYMBOL(rtas_set_indicator);