blob: 0c6ed6d75b1794b5630a73054dfd00fbeb262d00 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Paul Mackerras033ef332005-10-26 17:05:24 +100036struct rtas_t rtas = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .lock = SPIN_LOCK_UNLOCKED
38};
39
Dave C Boutcher91dc1822006-01-13 18:39:24 -060040struct rtas_suspend_me_data {
41 long waiting;
42 struct rtas_args *args;
43};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045EXPORT_SYMBOL(rtas);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047DEFINE_SPINLOCK(rtas_data_buf_lock);
Paul Mackerras033ef332005-10-26 17:05:24 +100048char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049unsigned long rtas_rmo_buf;
50
Paul Mackerras033ef332005-10-26 17:05:24 +100051/*
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110052 * If non-NULL, this gets called when the kernel terminates.
53 * This is done like this so rtas_flash can be a module.
54 */
55void (*rtas_flash_term_hook)(int);
56EXPORT_SYMBOL(rtas_flash_term_hook);
57
58/*
Paul Mackerras033ef332005-10-26 17:05:24 +100059 * call_rtas_display_status and call_rtas_display_status_delay
60 * are designed only for very early low-level debugging, which
61 * is why the token is hard-coded to 10.
62 */
Michael Ellerman296167a2006-01-11 11:54:09 +110063static void call_rtas_display_status(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct rtas_args *args = &rtas.args;
66 unsigned long s;
67
68 if (!rtas.base)
69 return;
70 spin_lock_irqsave(&rtas.lock, s);
71
72 args->token = 10;
73 args->nargs = 1;
74 args->nret = 1;
75 args->rets = (rtas_arg_t *)&(args->args[1]);
Michael Ellerman296167a2006-01-11 11:54:09 +110076 args->args[0] = (unsigned char)c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 enter_rtas(__pa(args));
79
80 spin_unlock_irqrestore(&rtas.lock, s);
81}
82
Michael Ellerman296167a2006-01-11 11:54:09 +110083static void call_rtas_display_status_delay(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 static int pending_newline = 0; /* did last write end with unprinted newline? */
86 static int width = 16;
87
88 if (c == '\n') {
89 while (width-- > 0)
90 call_rtas_display_status(' ');
91 width = 16;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +110092 mdelay(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pending_newline = 1;
94 } else {
95 if (pending_newline) {
96 call_rtas_display_status('\r');
97 call_rtas_display_status('\n');
98 }
99 pending_newline = 0;
100 if (width--) {
101 call_rtas_display_status(c);
102 udelay(10000);
103 }
104 }
105}
106
Michael Ellerman296167a2006-01-11 11:54:09 +1100107void __init udbg_init_rtas(void)
108{
109 udbg_putc = call_rtas_display_status_delay;
110}
111
Paul Mackerras033ef332005-10-26 17:05:24 +1000112void rtas_progress(char *s, unsigned short hex)
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000113{
114 struct device_node *root;
115 int width, *p;
116 char *os;
117 static int display_character, set_indicator;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000118 static int display_width, display_lines, *row_width, form_feed;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000119 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000120 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000121 static int pending_newline = 0; /* did last write end with unprinted newline? */
122
123 if (!rtas.base)
124 return;
125
Mike Strosaker8f586b22005-06-23 16:09:41 +1000126 if (display_width == 0) {
127 display_width = 0x10;
128 if ((root = find_path_device("/rtas"))) {
129 if ((p = (unsigned int *)get_property(root,
130 "ibm,display-line-length", NULL)))
131 display_width = *p;
132 if ((p = (unsigned int *)get_property(root,
133 "ibm,form-feed", NULL)))
134 form_feed = *p;
135 if ((p = (unsigned int *)get_property(root,
136 "ibm,display-number-of-lines", NULL)))
137 display_lines = *p;
138 row_width = (unsigned int *)get_property(root,
139 "ibm,display-truncation-length", NULL);
140 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000141 display_character = rtas_token("display-character");
142 set_indicator = rtas_token("set-indicator");
143 }
144
145 if (display_character == RTAS_UNKNOWN_SERVICE) {
146 /* use hex display if available */
147 if (set_indicator != RTAS_UNKNOWN_SERVICE)
148 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
149 return;
150 }
151
152 spin_lock(&progress_lock);
153
154 /*
155 * Last write ended with newline, but we didn't print it since
156 * it would just clear the bottom line of output. Print it now
157 * instead.
158 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000159 * If no newline is pending and form feed is supported, clear the
160 * display with a form feed; otherwise, print a CR to start output
161 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000162 */
163 if (pending_newline) {
164 rtas_call(display_character, 1, 1, NULL, '\r');
165 rtas_call(display_character, 1, 1, NULL, '\n');
166 pending_newline = 0;
167 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000168 current_line = 0;
169 if (form_feed)
170 rtas_call(display_character, 1, 1, NULL,
171 (char)form_feed);
172 else
173 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000174 }
175
Mike Strosaker8f586b22005-06-23 16:09:41 +1000176 if (row_width)
177 width = row_width[current_line];
178 else
179 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000180 os = s;
181 while (*os) {
182 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000183 /* If newline is the last character, save it
184 * until next call to avoid bumping up the
185 * display output.
186 */
187 if (*os == '\n' && !os[1]) {
188 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000189 current_line++;
190 if (current_line > display_lines-1)
191 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000192 spin_unlock(&progress_lock);
193 return;
194 }
195
196 /* RTAS wants CR-LF, not just LF */
197
198 if (*os == '\n') {
199 rtas_call(display_character, 1, 1, NULL, '\r');
200 rtas_call(display_character, 1, 1, NULL, '\n');
201 } else {
202 /* CR might be used to re-draw a line, so we'll
203 * leave it alone and not add LF.
204 */
205 rtas_call(display_character, 1, 1, NULL, *os);
206 }
207
Mike Strosaker8f586b22005-06-23 16:09:41 +1000208 if (row_width)
209 width = row_width[current_line];
210 else
211 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000212 } else {
213 width--;
214 rtas_call(display_character, 1, 1, NULL, *os);
215 }
216
217 os++;
218
219 /* if we overwrite the screen length */
220 if (width <= 0)
221 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
222 os++;
223 }
224
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000225 spin_unlock(&progress_lock);
226}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100227EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000228
Paul Mackerras033ef332005-10-26 17:05:24 +1000229int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000232 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return RTAS_UNKNOWN_SERVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 tokp = (int *) get_property(rtas.dev, service, NULL);
235 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
236}
237
Paul Mackerras033ef332005-10-26 17:05:24 +1000238#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Return the firmware-specified size of the error log buffer
241 * for all rtas calls that require an error buffer argument.
242 * This includes 'check-exception' and 'rtas-last-error'.
243 */
244int rtas_get_error_log_max(void)
245{
246 static int rtas_error_log_max;
247 if (rtas_error_log_max)
248 return rtas_error_log_max;
249
250 rtas_error_log_max = rtas_token ("rtas-error-log-max");
251 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
252 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000253 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
254 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
256 }
257 return rtas_error_log_max;
258}
Paul Mackerras033ef332005-10-26 17:05:24 +1000259EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261
Paul Mackerras033ef332005-10-26 17:05:24 +1000262char rtas_err_buf[RTAS_ERROR_LOG_MAX];
263int rtas_last_error_token;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/** Return a copy of the detailed error text associated with the
266 * most recent failed call to rtas. Because the error text
267 * might go stale if there are any other intervening rtas calls,
268 * this routine must be called atomically with whatever produced
269 * the error (i.e. with rtas.lock still held from the previous call).
270 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000271static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct rtas_args err_args, save_args;
274 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000275 char *buf = NULL;
276
277 if (rtas_last_error_token == -1)
278 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 bufsz = rtas_get_error_log_max();
281
Paul Mackerras033ef332005-10-26 17:05:24 +1000282 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 err_args.nargs = 2;
284 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
286 err_args.args[1] = bufsz;
287 err_args.args[2] = 0;
288
289 save_args = rtas.args;
290 rtas.args = err_args;
291
292 enter_rtas(__pa(&rtas.args));
293
294 err_args = rtas.args;
295 rtas.args = save_args;
296
Paul Mackerras033ef332005-10-26 17:05:24 +1000297 /* Log the error in the unlikely case that there was one. */
298 if (unlikely(err_args.args[2] == 0)) {
299 if (altbuf) {
300 buf = altbuf;
301 } else {
302 buf = rtas_err_buf;
303 if (mem_init_done)
304 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
305 }
306 if (buf)
307 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
308 }
309
310 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Paul Mackerras033ef332005-10-26 17:05:24 +1000313#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
314
315#else /* CONFIG_RTAS_ERROR_LOGGING */
316#define __fetch_rtas_last_error(x) NULL
317#define get_errorlog_buffer() NULL
318#endif
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320int rtas_call(int token, int nargs, int nret, int *outputs, ...)
321{
322 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000323 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long s;
325 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000326 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int ret;
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (token == RTAS_UNKNOWN_SERVICE)
330 return -1;
331
332 /* Gotta do something different here, use global lock for now... */
333 spin_lock_irqsave(&rtas.lock, s);
334 rtas_args = &rtas.args;
335
336 rtas_args->token = token;
337 rtas_args->nargs = nargs;
338 rtas_args->nret = nret;
339 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
340 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000341 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 va_end(list);
344
345 for (i = 0; i < nret; ++i)
346 rtas_args->rets[i] = 0;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* A -1 return code indicates that the last command couldn't
351 be completed due to a hardware error. */
352 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000353 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (nret > 1 && outputs != NULL)
356 for (i = 0; i < nret-1; ++i)
357 outputs[i] = rtas_args->rets[i+1];
358 ret = (nret > 0)? rtas_args->rets[0]: 0;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Gotta do something different here, use global lock for now... */
361 spin_unlock_irqrestore(&rtas.lock, s);
362
363 if (buff_copy) {
364 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
365 if (mem_init_done)
366 kfree(buff_copy);
367 }
368 return ret;
369}
370
371/* Given an RTAS status code of 990n compute the hinted delay of 10^n
372 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
373 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000374unsigned int rtas_extended_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int order = status - 9900;
377 unsigned long ms;
378
379 if (order < 0)
380 order = 0; /* RTC depends on this for -2 clock busy */
381 else if (order > 5)
382 order = 5; /* bound */
383
384 /* Use microseconds for reasonable accuracy */
Paul Mackerras033ef332005-10-26 17:05:24 +1000385 for (ms = 1; order > 0; order--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ms *= 10;
387
388 return ms;
389}
390
391int rtas_error_rc(int rtas_rc)
392{
393 int rc;
394
395 switch (rtas_rc) {
396 case -1: /* Hardware Error */
397 rc = -EIO;
398 break;
399 case -3: /* Bad indicator/domain/etc */
400 rc = -EINVAL;
401 break;
402 case -9000: /* Isolation error */
403 rc = -EFAULT;
404 break;
405 case -9001: /* Outstanding TCE/PTE */
406 rc = -EEXIST;
407 break;
408 case -9002: /* No usable slot */
409 rc = -ENODEV;
410 break;
411 default:
412 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
413 __FUNCTION__, rtas_rc);
414 rc = -ERANGE;
415 break;
416 }
417 return rc;
418}
419
420int rtas_get_power_level(int powerdomain, int *level)
421{
422 int token = rtas_token("get-power-level");
423 int rc;
424
425 if (token == RTAS_UNKNOWN_SERVICE)
426 return -ENOENT;
427
428 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
429 udelay(1);
430
431 if (rc < 0)
432 return rtas_error_rc(rc);
433 return rc;
434}
435
436int rtas_set_power_level(int powerdomain, int level, int *setlevel)
437{
438 int token = rtas_token("set-power-level");
439 unsigned int wait_time;
440 int rc;
441
442 if (token == RTAS_UNKNOWN_SERVICE)
443 return -ENOENT;
444
445 while (1) {
446 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
447 if (rc == RTAS_BUSY)
448 udelay(1);
449 else if (rtas_is_extended_busy(rc)) {
450 wait_time = rtas_extended_busy_delay_time(rc);
451 udelay(wait_time * 1000);
452 } else
453 break;
454 }
455
456 if (rc < 0)
457 return rtas_error_rc(rc);
458 return rc;
459}
460
461int rtas_get_sensor(int sensor, int index, int *state)
462{
463 int token = rtas_token("get-sensor-state");
464 unsigned int wait_time;
465 int rc;
466
467 if (token == RTAS_UNKNOWN_SERVICE)
468 return -ENOENT;
469
470 while (1) {
471 rc = rtas_call(token, 2, 2, state, sensor, index);
472 if (rc == RTAS_BUSY)
473 udelay(1);
474 else if (rtas_is_extended_busy(rc)) {
475 wait_time = rtas_extended_busy_delay_time(rc);
476 udelay(wait_time * 1000);
477 } else
478 break;
479 }
480
481 if (rc < 0)
482 return rtas_error_rc(rc);
483 return rc;
484}
485
486int rtas_set_indicator(int indicator, int index, int new_value)
487{
488 int token = rtas_token("set-indicator");
489 unsigned int wait_time;
490 int rc;
491
492 if (token == RTAS_UNKNOWN_SERVICE)
493 return -ENOENT;
494
495 while (1) {
496 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
497 if (rc == RTAS_BUSY)
498 udelay(1);
499 else if (rtas_is_extended_busy(rc)) {
500 wait_time = rtas_extended_busy_delay_time(rc);
501 udelay(wait_time * 1000);
502 }
503 else
504 break;
505 }
506
507 if (rc < 0)
508 return rtas_error_rc(rc);
509 return rc;
510}
511
Paul Mackerras033ef332005-10-26 17:05:24 +1000512void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100514 if (rtas_flash_term_hook)
515 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 printk("RTAS system-reboot returned %d\n",
517 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
518 for (;;);
519}
520
Paul Mackerras033ef332005-10-26 17:05:24 +1000521void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100523 if (rtas_flash_term_hook)
524 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* allow power on only with power button press */
526 printk("RTAS power-off returned %d\n",
527 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
528 for (;;);
529}
530
Paul Mackerras033ef332005-10-26 17:05:24 +1000531void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100533 if (rtas_flash_term_hook)
534 rtas_flash_term_hook(SYS_HALT);
535 /* allow power on only with power button press */
536 printk("RTAS power-off returned %d\n",
537 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
538 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/* Must be in the RMO region, so we place it here */
542static char rtas_os_term_buf[2048];
543
544void rtas_os_term(char *str)
545{
546 int status;
547
548 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
549 return;
550
551 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
552
553 do {
554 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
555 __pa(rtas_os_term_buf));
556
557 if (status == RTAS_BUSY)
558 udelay(1);
559 else if (status != 0)
560 printk(KERN_EMERG "ibm,os-term call failed %d\n",
561 status);
562 } while (status == RTAS_BUSY);
563}
564
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600565static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
566#ifdef CONFIG_PPC_PSERIES
567static void rtas_percpu_suspend_me(void *info)
568{
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600569 int i;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600570 long rc;
571 long flags;
572 struct rtas_suspend_me_data *data =
573 (struct rtas_suspend_me_data *)info;
574
575 /*
576 * We use "waiting" to indicate our state. As long
577 * as it is >0, we are still trying to all join up.
578 * If it goes to 0, we have successfully joined up and
579 * one thread got H_Continue. If any error happens,
580 * we set it to <0.
581 */
582 local_irq_save(flags);
583 do {
584 rc = plpar_hcall_norets(H_JOIN);
585 smp_rmb();
586 } while (rc == H_Success && data->waiting > 0);
587 if (rc == H_Success)
588 goto out;
589
590 if (rc == H_Continue) {
591 data->waiting = 0;
Dave C Boutcherc4cb8ec2006-02-03 01:18:46 -0600592 data->args->args[data->args->nargs] =
593 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600594 for_each_cpu(i)
595 plpar_hcall_norets(H_PROD,i);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600596 } else {
597 data->waiting = -EBUSY;
598 printk(KERN_ERR "Error on H_Join hypervisor call\n");
599 }
600
601out:
602 /* before we restore interrupts, make sure we don't
603 * generate a spurious soft lockup errors
604 */
605 touch_softlockup_watchdog();
606 local_irq_restore(flags);
607 return;
608}
609
610static int rtas_ibm_suspend_me(struct rtas_args *args)
611{
612 int i;
613
614 struct rtas_suspend_me_data data;
615
616 data.waiting = 1;
617 data.args = args;
618
619 /* Call function on all CPUs. One of us will make the
620 * rtas call
621 */
622 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
623 data.waiting = -EINVAL;
624
625 if (data.waiting != 0)
626 printk(KERN_ERR "Error doing global join\n");
627
628 /* Prod each CPU. This won't hurt, and will wake
629 * anyone we successfully put to sleep with H_Join
630 */
631 for_each_cpu(i)
632 plpar_hcall_norets(H_PROD, i);
633
634 return data.waiting;
635}
636#else /* CONFIG_PPC_PSERIES */
637static int rtas_ibm_suspend_me(struct rtas_args *args)
638{
639 return -ENOSYS;
640}
641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
644{
645 struct rtas_args args;
646 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000647 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int nargs;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600649 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (!capable(CAP_SYS_ADMIN))
652 return -EPERM;
653
654 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
655 return -EFAULT;
656
657 nargs = args.nargs;
658 if (nargs > ARRAY_SIZE(args.args)
659 || args.nret > ARRAY_SIZE(args.args)
660 || nargs + args.nret > ARRAY_SIZE(args.args))
661 return -EINVAL;
662
663 /* Copy in args. */
664 if (copy_from_user(args.args, uargs->args,
665 nargs * sizeof(rtas_arg_t)) != 0)
666 return -EFAULT;
667
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600668 if (args.token == RTAS_UNKNOWN_SERVICE)
669 return -EINVAL;
670
671 /* Need to handle ibm,suspend_me call specially */
672 if (args.token == ibm_suspend_me_token) {
673 rc = rtas_ibm_suspend_me(&args);
674 if (rc)
675 return rc;
676 goto copy_return;
677 }
678
Paul Mackerras033ef332005-10-26 17:05:24 +1000679 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 spin_lock_irqsave(&rtas.lock, flags);
682
683 rtas.args = args;
684 enter_rtas(__pa(&rtas.args));
685 args = rtas.args;
686
687 args.rets = &args.args[nargs];
688
689 /* A -1 return code indicates that the last command couldn't
690 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000691 if (args.rets[0] == -1)
692 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 spin_unlock_irqrestore(&rtas.lock, flags);
695
696 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000697 if (errbuf)
698 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(buff_copy);
700 }
701
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600702 copy_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Copy out args. */
704 if (copy_to_user(uargs->args + nargs,
705 args.args + nargs,
706 args.nret * sizeof(rtas_arg_t)) != 0)
707 return -EFAULT;
708
709 return 0;
710}
711
712/* This version can't take the spinlock, because it never returns */
713
714struct rtas_args rtas_stop_self_args = {
715 /* The token is initialized for real in setup_system() */
716 .token = RTAS_UNKNOWN_SERVICE,
717 .nargs = 0,
718 .nret = 1,
719 .rets = &rtas_stop_self_args.args[0],
720};
721
722void rtas_stop_self(void)
723{
724 struct rtas_args *rtas_args = &rtas_stop_self_args;
725
726 local_irq_disable();
727
728 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
729
730 printk("cpu %u (hwid %u) Ready to die...\n",
731 smp_processor_id(), hard_smp_processor_id());
732 enter_rtas(__pa(rtas_args));
733
734 panic("Alas, I survived.\n");
735}
736
737/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100738 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * informations from the device-tree and allocate the RMO buffer for userland
740 * accesses.
741 */
742void __init rtas_initialize(void)
743{
Paul Mackerras033ef332005-10-26 17:05:24 +1000744 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* Get RTAS dev node and fill up our "rtas" structure with infos
747 * about it.
748 */
749 rtas.dev = of_find_node_by_name(NULL, "rtas");
750 if (rtas.dev) {
751 u32 *basep, *entryp;
752 u32 *sizep;
753
754 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
755 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
756 if (basep != NULL && sizep != NULL) {
757 rtas.base = *basep;
758 rtas.size = *sizep;
759 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
760 if (entryp == NULL) /* Ugh */
761 rtas.entry = rtas.base;
762 else
763 rtas.entry = *entryp;
764 } else
765 rtas.dev = NULL;
766 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000767 if (!rtas.dev)
768 return;
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* If RTAS was found, allocate the RMO buffer for it and look for
771 * the stop-self token if any
772 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000773#ifdef CONFIG_PPC64
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600774 if (_machine == PLATFORM_PSERIES_LPAR) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000775 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600776 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
777 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000778#endif
779 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000782 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000784#ifdef CONFIG_RTAS_ERROR_LOGGING
785 rtas_last_error_token = rtas_token("rtas-last-error");
786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790EXPORT_SYMBOL(rtas_token);
791EXPORT_SYMBOL(rtas_call);
792EXPORT_SYMBOL(rtas_data_buf);
793EXPORT_SYMBOL(rtas_data_buf_lock);
794EXPORT_SYMBOL(rtas_extended_busy_delay_time);
795EXPORT_SYMBOL(rtas_get_sensor);
796EXPORT_SYMBOL(rtas_get_power_level);
797EXPORT_SYMBOL(rtas_set_power_level);
798EXPORT_SYMBOL(rtas_set_indicator);