blob: 77f1e06d208d2984bf0b12d79f2d6733c8c53b5f [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>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110028#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/page.h>
30#include <asm/param.h>
31#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/delay.h>
33#include <asm/uaccess.h>
Paul Mackerras033ef332005-10-26 17:05:24 +100034#include <asm/lmb.h>
Michael Ellerman296167a2006-01-11 11:54:09 +110035#include <asm/udbg.h>
Arnd Bergmanna7f31842006-03-23 00:00:08 +010036#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Paul Mackerras033ef332005-10-26 17:05:24 +100038struct rtas_t rtas = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 .lock = SPIN_LOCK_UNLOCKED
40};
Michael Ellermanab3ab742006-06-23 18:20:11 +100041EXPORT_SYMBOL(rtas);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Dave C Boutcher91dc1822006-01-13 18:39:24 -060043struct rtas_suspend_me_data {
44 long waiting;
45 struct rtas_args *args;
46};
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048DEFINE_SPINLOCK(rtas_data_buf_lock);
Michael Ellermanab3ab742006-06-23 18:20:11 +100049EXPORT_SYMBOL(rtas_data_buf_lock);
50
Paul Mackerras033ef332005-10-26 17:05:24 +100051char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
Michael Ellermanab3ab742006-06-23 18:20:11 +100052EXPORT_SYMBOL(rtas_data_buf);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054unsigned long rtas_rmo_buf;
55
Paul Mackerras033ef332005-10-26 17:05:24 +100056/*
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110057 * If non-NULL, this gets called when the kernel terminates.
58 * This is done like this so rtas_flash can be a module.
59 */
60void (*rtas_flash_term_hook)(int);
61EXPORT_SYMBOL(rtas_flash_term_hook);
62
63/*
Paul Mackerras033ef332005-10-26 17:05:24 +100064 * call_rtas_display_status and call_rtas_display_status_delay
65 * are designed only for very early low-level debugging, which
66 * is why the token is hard-coded to 10.
67 */
Michael Ellerman296167a2006-01-11 11:54:09 +110068static void call_rtas_display_status(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct rtas_args *args = &rtas.args;
71 unsigned long s;
72
73 if (!rtas.base)
74 return;
75 spin_lock_irqsave(&rtas.lock, s);
76
77 args->token = 10;
78 args->nargs = 1;
79 args->nret = 1;
80 args->rets = (rtas_arg_t *)&(args->args[1]);
Michael Ellerman296167a2006-01-11 11:54:09 +110081 args->args[0] = (unsigned char)c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 enter_rtas(__pa(args));
84
85 spin_unlock_irqrestore(&rtas.lock, s);
86}
87
Michael Ellerman296167a2006-01-11 11:54:09 +110088static void call_rtas_display_status_delay(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 static int pending_newline = 0; /* did last write end with unprinted newline? */
91 static int width = 16;
92
93 if (c == '\n') {
94 while (width-- > 0)
95 call_rtas_display_status(' ');
96 width = 16;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +110097 mdelay(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 pending_newline = 1;
99 } else {
100 if (pending_newline) {
101 call_rtas_display_status('\r');
102 call_rtas_display_status('\n');
103 }
104 pending_newline = 0;
105 if (width--) {
106 call_rtas_display_status(c);
107 udelay(10000);
108 }
109 }
110}
111
Michael Ellermancc46bb92006-06-23 18:20:16 +1000112void __init udbg_init_rtas_panel(void)
Michael Ellerman296167a2006-01-11 11:54:09 +1100113{
114 udbg_putc = call_rtas_display_status_delay;
115}
116
Michael Ellermancc46bb92006-06-23 18:20:16 +1000117#ifdef CONFIG_UDBG_RTAS_CONSOLE
118
119/* If you think you're dying before early_init_dt_scan_rtas() does its
120 * work, you can hard code the token values for your firmware here and
121 * hardcode rtas.base/entry etc.
122 */
123static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE;
124static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE;
125
126static void udbg_rtascon_putc(char c)
127{
128 int tries;
129
130 if (!rtas.base)
131 return;
132
133 /* Add CRs before LFs */
134 if (c == '\n')
135 udbg_rtascon_putc('\r');
136
137 /* if there is more than one character to be displayed, wait a bit */
138 for (tries = 0; tries < 16; tries++) {
139 if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0)
140 break;
141 udelay(1000);
142 }
143}
144
145static int udbg_rtascon_getc_poll(void)
146{
147 int c;
148
149 if (!rtas.base)
150 return -1;
151
152 if (rtas_call(rtas_getchar_token, 0, 2, &c))
153 return -1;
154
155 return c;
156}
157
158static int udbg_rtascon_getc(void)
159{
160 int c;
161
162 while ((c = udbg_rtascon_getc_poll()) == -1)
163 ;
164
165 return c;
166}
167
168
169void __init udbg_init_rtas_console(void)
170{
171 udbg_putc = udbg_rtascon_putc;
172 udbg_getc = udbg_rtascon_getc;
173 udbg_getc_poll = udbg_rtascon_getc_poll;
174}
175#endif /* CONFIG_UDBG_RTAS_CONSOLE */
176
Paul Mackerras033ef332005-10-26 17:05:24 +1000177void rtas_progress(char *s, unsigned short hex)
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000178{
179 struct device_node *root;
180 int width, *p;
181 char *os;
182 static int display_character, set_indicator;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000183 static int display_width, display_lines, *row_width, form_feed;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000184 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000185 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000186 static int pending_newline = 0; /* did last write end with unprinted newline? */
187
188 if (!rtas.base)
189 return;
190
Mike Strosaker8f586b22005-06-23 16:09:41 +1000191 if (display_width == 0) {
192 display_width = 0x10;
193 if ((root = find_path_device("/rtas"))) {
194 if ((p = (unsigned int *)get_property(root,
195 "ibm,display-line-length", NULL)))
196 display_width = *p;
197 if ((p = (unsigned int *)get_property(root,
198 "ibm,form-feed", NULL)))
199 form_feed = *p;
200 if ((p = (unsigned int *)get_property(root,
201 "ibm,display-number-of-lines", NULL)))
202 display_lines = *p;
203 row_width = (unsigned int *)get_property(root,
204 "ibm,display-truncation-length", NULL);
205 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000206 display_character = rtas_token("display-character");
207 set_indicator = rtas_token("set-indicator");
208 }
209
210 if (display_character == RTAS_UNKNOWN_SERVICE) {
211 /* use hex display if available */
212 if (set_indicator != RTAS_UNKNOWN_SERVICE)
213 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
214 return;
215 }
216
217 spin_lock(&progress_lock);
218
219 /*
220 * Last write ended with newline, but we didn't print it since
221 * it would just clear the bottom line of output. Print it now
222 * instead.
223 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000224 * If no newline is pending and form feed is supported, clear the
225 * display with a form feed; otherwise, print a CR to start output
226 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000227 */
228 if (pending_newline) {
229 rtas_call(display_character, 1, 1, NULL, '\r');
230 rtas_call(display_character, 1, 1, NULL, '\n');
231 pending_newline = 0;
232 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000233 current_line = 0;
234 if (form_feed)
235 rtas_call(display_character, 1, 1, NULL,
236 (char)form_feed);
237 else
238 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000239 }
240
Mike Strosaker8f586b22005-06-23 16:09:41 +1000241 if (row_width)
242 width = row_width[current_line];
243 else
244 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000245 os = s;
246 while (*os) {
247 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000248 /* If newline is the last character, save it
249 * until next call to avoid bumping up the
250 * display output.
251 */
252 if (*os == '\n' && !os[1]) {
253 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000254 current_line++;
255 if (current_line > display_lines-1)
256 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000257 spin_unlock(&progress_lock);
258 return;
259 }
260
261 /* RTAS wants CR-LF, not just LF */
262
263 if (*os == '\n') {
264 rtas_call(display_character, 1, 1, NULL, '\r');
265 rtas_call(display_character, 1, 1, NULL, '\n');
266 } else {
267 /* CR might be used to re-draw a line, so we'll
268 * leave it alone and not add LF.
269 */
270 rtas_call(display_character, 1, 1, NULL, *os);
271 }
272
Mike Strosaker8f586b22005-06-23 16:09:41 +1000273 if (row_width)
274 width = row_width[current_line];
275 else
276 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000277 } else {
278 width--;
279 rtas_call(display_character, 1, 1, NULL, *os);
280 }
281
282 os++;
283
284 /* if we overwrite the screen length */
285 if (width <= 0)
286 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
287 os++;
288 }
289
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000290 spin_unlock(&progress_lock);
291}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100292EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000293
Paul Mackerras033ef332005-10-26 17:05:24 +1000294int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000297 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return RTAS_UNKNOWN_SERVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 tokp = (int *) get_property(rtas.dev, service, NULL);
300 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
301}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000302EXPORT_SYMBOL(rtas_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Paul Mackerras033ef332005-10-26 17:05:24 +1000304#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
306 * Return the firmware-specified size of the error log buffer
307 * for all rtas calls that require an error buffer argument.
308 * This includes 'check-exception' and 'rtas-last-error'.
309 */
310int rtas_get_error_log_max(void)
311{
312 static int rtas_error_log_max;
313 if (rtas_error_log_max)
314 return rtas_error_log_max;
315
316 rtas_error_log_max = rtas_token ("rtas-error-log-max");
317 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
318 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000319 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
320 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
322 }
323 return rtas_error_log_max;
324}
Paul Mackerras033ef332005-10-26 17:05:24 +1000325EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327
Paul Mackerras033ef332005-10-26 17:05:24 +1000328char rtas_err_buf[RTAS_ERROR_LOG_MAX];
329int rtas_last_error_token;
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/** Return a copy of the detailed error text associated with the
332 * most recent failed call to rtas. Because the error text
333 * might go stale if there are any other intervening rtas calls,
334 * this routine must be called atomically with whatever produced
335 * the error (i.e. with rtas.lock still held from the previous call).
336 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000337static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct rtas_args err_args, save_args;
340 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000341 char *buf = NULL;
342
343 if (rtas_last_error_token == -1)
344 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 bufsz = rtas_get_error_log_max();
347
Paul Mackerras033ef332005-10-26 17:05:24 +1000348 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 err_args.nargs = 2;
350 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
352 err_args.args[1] = bufsz;
353 err_args.args[2] = 0;
354
355 save_args = rtas.args;
356 rtas.args = err_args;
357
358 enter_rtas(__pa(&rtas.args));
359
360 err_args = rtas.args;
361 rtas.args = save_args;
362
Paul Mackerras033ef332005-10-26 17:05:24 +1000363 /* Log the error in the unlikely case that there was one. */
364 if (unlikely(err_args.args[2] == 0)) {
365 if (altbuf) {
366 buf = altbuf;
367 } else {
368 buf = rtas_err_buf;
369 if (mem_init_done)
370 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
371 }
372 if (buf)
373 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
374 }
375
376 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Paul Mackerras033ef332005-10-26 17:05:24 +1000379#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
380
381#else /* CONFIG_RTAS_ERROR_LOGGING */
382#define __fetch_rtas_last_error(x) NULL
383#define get_errorlog_buffer() NULL
384#endif
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386int rtas_call(int token, int nargs, int nret, int *outputs, ...)
387{
388 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000389 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned long s;
391 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000392 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int ret;
394
Michael Ellerman24da3dd2006-06-23 18:20:10 +1000395 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -1;
397
398 /* Gotta do something different here, use global lock for now... */
399 spin_lock_irqsave(&rtas.lock, s);
400 rtas_args = &rtas.args;
401
402 rtas_args->token = token;
403 rtas_args->nargs = nargs;
404 rtas_args->nret = nret;
405 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
406 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000407 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 va_end(list);
410
411 for (i = 0; i < nret; ++i)
412 rtas_args->rets[i] = 0;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* A -1 return code indicates that the last command couldn't
417 be completed due to a hardware error. */
418 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000419 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (nret > 1 && outputs != NULL)
422 for (i = 0; i < nret-1; ++i)
423 outputs[i] = rtas_args->rets[i+1];
424 ret = (nret > 0)? rtas_args->rets[0]: 0;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Gotta do something different here, use global lock for now... */
427 spin_unlock_irqrestore(&rtas.lock, s);
428
429 if (buff_copy) {
430 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
431 if (mem_init_done)
432 kfree(buff_copy);
433 }
434 return ret;
435}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000436EXPORT_SYMBOL(rtas_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
John Rose507279d2006-06-05 16:31:48 -0500438/* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
439 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
John Rose507279d2006-06-05 16:31:48 -0500441unsigned int rtas_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
John Rose507279d2006-06-05 16:31:48 -0500443 int order;
444 unsigned int ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
John Rose507279d2006-06-05 16:31:48 -0500446 if (status == RTAS_BUSY) {
447 ms = 1;
448 } else if (status >= 9900 && status <= 9905) {
449 order = status - 9900;
450 for (ms = 1; order > 0; order--)
451 ms *= 10;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
John Rose507279d2006-06-05 16:31:48 -0500454 return ms;
455}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000456EXPORT_SYMBOL(rtas_busy_delay_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
John Rose507279d2006-06-05 16:31:48 -0500458/* For an RTAS busy status code, perform the hinted delay. */
459unsigned int rtas_busy_delay(int status)
460{
461 unsigned int ms;
462
463 might_sleep();
464 ms = rtas_busy_delay_time(status);
465 if (ms)
466 msleep(ms);
467
468 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000470EXPORT_SYMBOL(rtas_busy_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472int rtas_error_rc(int rtas_rc)
473{
474 int rc;
475
476 switch (rtas_rc) {
477 case -1: /* Hardware Error */
478 rc = -EIO;
479 break;
480 case -3: /* Bad indicator/domain/etc */
481 rc = -EINVAL;
482 break;
483 case -9000: /* Isolation error */
484 rc = -EFAULT;
485 break;
486 case -9001: /* Outstanding TCE/PTE */
487 rc = -EEXIST;
488 break;
489 case -9002: /* No usable slot */
490 rc = -ENODEV;
491 break;
492 default:
493 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
494 __FUNCTION__, rtas_rc);
495 rc = -ERANGE;
496 break;
497 }
498 return rc;
499}
500
501int rtas_get_power_level(int powerdomain, int *level)
502{
503 int token = rtas_token("get-power-level");
504 int rc;
505
506 if (token == RTAS_UNKNOWN_SERVICE)
507 return -ENOENT;
508
509 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
510 udelay(1);
511
512 if (rc < 0)
513 return rtas_error_rc(rc);
514 return rc;
515}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000516EXPORT_SYMBOL(rtas_get_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518int rtas_set_power_level(int powerdomain, int level, int *setlevel)
519{
520 int token = rtas_token("set-power-level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int rc;
522
523 if (token == RTAS_UNKNOWN_SERVICE)
524 return -ENOENT;
525
John Rose507279d2006-06-05 16:31:48 -0500526 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
John Rose507279d2006-06-05 16:31:48 -0500528 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (rc < 0)
531 return rtas_error_rc(rc);
532 return rc;
533}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000534EXPORT_SYMBOL(rtas_set_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536int rtas_get_sensor(int sensor, int index, int *state)
537{
538 int token = rtas_token("get-sensor-state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int rc;
540
541 if (token == RTAS_UNKNOWN_SERVICE)
542 return -ENOENT;
543
John Rose507279d2006-06-05 16:31:48 -0500544 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 rc = rtas_call(token, 2, 2, state, sensor, index);
John Rose507279d2006-06-05 16:31:48 -0500546 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (rc < 0)
549 return rtas_error_rc(rc);
550 return rc;
551}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000552EXPORT_SYMBOL(rtas_get_sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554int rtas_set_indicator(int indicator, int index, int new_value)
555{
556 int token = rtas_token("set-indicator");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int rc;
558
559 if (token == RTAS_UNKNOWN_SERVICE)
560 return -ENOENT;
561
John Rose507279d2006-06-05 16:31:48 -0500562 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
John Rose507279d2006-06-05 16:31:48 -0500564 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (rc < 0)
567 return rtas_error_rc(rc);
568 return rc;
569}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000570EXPORT_SYMBOL(rtas_set_indicator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Haren Myneni81b73dd2006-07-27 14:29:00 -0700572/*
573 * Ignoring RTAS extended delay
574 */
575int rtas_set_indicator_fast(int indicator, int index, int new_value)
576{
577 int rc;
578 int token = rtas_token("set-indicator");
579
580 if (token == RTAS_UNKNOWN_SERVICE)
581 return -ENOENT;
582
583 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
584
585 WARN_ON(rc == -2 || (rc >= 9900 && rc <= 9905));
586
587 if (rc < 0)
588 return rtas_error_rc(rc);
589
590 return rc;
591}
592
Paul Mackerras033ef332005-10-26 17:05:24 +1000593void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100595 if (rtas_flash_term_hook)
596 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 printk("RTAS system-reboot returned %d\n",
598 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
599 for (;;);
600}
601
Paul Mackerras033ef332005-10-26 17:05:24 +1000602void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100604 if (rtas_flash_term_hook)
605 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* allow power on only with power button press */
607 printk("RTAS power-off returned %d\n",
608 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
609 for (;;);
610}
611
Paul Mackerras033ef332005-10-26 17:05:24 +1000612void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100614 if (rtas_flash_term_hook)
615 rtas_flash_term_hook(SYS_HALT);
616 /* allow power on only with power button press */
617 printk("RTAS power-off returned %d\n",
618 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
619 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622/* Must be in the RMO region, so we place it here */
623static char rtas_os_term_buf[2048];
624
625void rtas_os_term(char *str)
626{
627 int status;
628
629 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
630 return;
631
632 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
633
634 do {
635 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
636 __pa(rtas_os_term_buf));
John Rose507279d2006-06-05 16:31:48 -0500637 } while (rtas_busy_delay(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
John Rose507279d2006-06-05 16:31:48 -0500639 if (status != 0)
640 printk(KERN_EMERG "ibm,os-term call failed %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600644static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
645#ifdef CONFIG_PPC_PSERIES
646static void rtas_percpu_suspend_me(void *info)
647{
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600648 int i;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600649 long rc;
650 long flags;
651 struct rtas_suspend_me_data *data =
652 (struct rtas_suspend_me_data *)info;
653
654 /*
655 * We use "waiting" to indicate our state. As long
656 * as it is >0, we are still trying to all join up.
657 * If it goes to 0, we have successfully joined up and
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200658 * one thread got H_CONTINUE. If any error happens,
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600659 * we set it to <0.
660 */
661 local_irq_save(flags);
662 do {
663 rc = plpar_hcall_norets(H_JOIN);
664 smp_rmb();
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200665 } while (rc == H_SUCCESS && data->waiting > 0);
666 if (rc == H_SUCCESS)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600667 goto out;
668
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200669 if (rc == H_CONTINUE) {
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600670 data->waiting = 0;
Dave C Boutcherc4cb8ec2006-02-03 01:18:46 -0600671 data->args->args[data->args->nargs] =
672 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800673 for_each_possible_cpu(i)
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600674 plpar_hcall_norets(H_PROD,i);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600675 } else {
676 data->waiting = -EBUSY;
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200677 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600678 }
679
680out:
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600681 local_irq_restore(flags);
682 return;
683}
684
685static int rtas_ibm_suspend_me(struct rtas_args *args)
686{
687 int i;
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500688 long state;
689 long rc;
690 unsigned long dummy;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600691
692 struct rtas_suspend_me_data data;
693
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500694 /* Make sure the state is valid */
695 rc = plpar_hcall(H_VASI_STATE,
696 ((u64)args->args[0] << 32) | args->args[1],
697 0, 0, 0,
698 &state, &dummy, &dummy);
699
700 if (rc) {
701 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
702 return rc;
703 } else if (state == H_VASI_ENABLED) {
704 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
705 return 0;
706 } else if (state != H_VASI_SUSPENDING) {
707 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
708 state);
709 args->args[args->nargs] = -1;
710 return 0;
711 }
712
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600713 data.waiting = 1;
714 data.args = args;
715
716 /* Call function on all CPUs. One of us will make the
717 * rtas call
718 */
719 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
720 data.waiting = -EINVAL;
721
722 if (data.waiting != 0)
723 printk(KERN_ERR "Error doing global join\n");
724
725 /* Prod each CPU. This won't hurt, and will wake
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200726 * anyone we successfully put to sleep with H_JOIN.
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600727 */
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800728 for_each_possible_cpu(i)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600729 plpar_hcall_norets(H_PROD, i);
730
731 return data.waiting;
732}
733#else /* CONFIG_PPC_PSERIES */
734static int rtas_ibm_suspend_me(struct rtas_args *args)
735{
736 return -ENOSYS;
737}
738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
741{
742 struct rtas_args args;
743 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000744 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int nargs;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600746 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (!capable(CAP_SYS_ADMIN))
749 return -EPERM;
750
751 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
752 return -EFAULT;
753
754 nargs = args.nargs;
755 if (nargs > ARRAY_SIZE(args.args)
756 || args.nret > ARRAY_SIZE(args.args)
757 || nargs + args.nret > ARRAY_SIZE(args.args))
758 return -EINVAL;
759
760 /* Copy in args. */
761 if (copy_from_user(args.args, uargs->args,
762 nargs * sizeof(rtas_arg_t)) != 0)
763 return -EFAULT;
764
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600765 if (args.token == RTAS_UNKNOWN_SERVICE)
766 return -EINVAL;
767
768 /* Need to handle ibm,suspend_me call specially */
769 if (args.token == ibm_suspend_me_token) {
770 rc = rtas_ibm_suspend_me(&args);
771 if (rc)
772 return rc;
773 goto copy_return;
774 }
775
Paul Mackerras033ef332005-10-26 17:05:24 +1000776 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 spin_lock_irqsave(&rtas.lock, flags);
779
780 rtas.args = args;
781 enter_rtas(__pa(&rtas.args));
782 args = rtas.args;
783
784 args.rets = &args.args[nargs];
785
786 /* A -1 return code indicates that the last command couldn't
787 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000788 if (args.rets[0] == -1)
789 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 spin_unlock_irqrestore(&rtas.lock, flags);
792
793 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000794 if (errbuf)
795 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 kfree(buff_copy);
797 }
798
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600799 copy_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Copy out args. */
801 if (copy_to_user(uargs->args + nargs,
802 args.args + nargs,
803 args.nret * sizeof(rtas_arg_t)) != 0)
804 return -EFAULT;
805
806 return 0;
807}
808
809/* This version can't take the spinlock, because it never returns */
810
811struct rtas_args rtas_stop_self_args = {
812 /* The token is initialized for real in setup_system() */
813 .token = RTAS_UNKNOWN_SERVICE,
814 .nargs = 0,
815 .nret = 1,
816 .rets = &rtas_stop_self_args.args[0],
817};
818
819void rtas_stop_self(void)
820{
821 struct rtas_args *rtas_args = &rtas_stop_self_args;
822
823 local_irq_disable();
824
825 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
826
827 printk("cpu %u (hwid %u) Ready to die...\n",
828 smp_processor_id(), hard_smp_processor_id());
829 enter_rtas(__pa(rtas_args));
830
831 panic("Alas, I survived.\n");
832}
833
834/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100835 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * informations from the device-tree and allocate the RMO buffer for userland
837 * accesses.
838 */
839void __init rtas_initialize(void)
840{
Paul Mackerras033ef332005-10-26 17:05:24 +1000841 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Get RTAS dev node and fill up our "rtas" structure with infos
844 * about it.
845 */
846 rtas.dev = of_find_node_by_name(NULL, "rtas");
847 if (rtas.dev) {
848 u32 *basep, *entryp;
849 u32 *sizep;
850
851 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
852 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
853 if (basep != NULL && sizep != NULL) {
854 rtas.base = *basep;
855 rtas.size = *sizep;
856 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
857 if (entryp == NULL) /* Ugh */
858 rtas.entry = rtas.base;
859 else
860 rtas.entry = *entryp;
861 } else
862 rtas.dev = NULL;
863 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000864 if (!rtas.dev)
865 return;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* If RTAS was found, allocate the RMO buffer for it and look for
868 * the stop-self token if any
869 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000870#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100871 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000872 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600873 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
874 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000875#endif
876 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000879 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000881#ifdef CONFIG_RTAS_ERROR_LOGGING
882 rtas_last_error_token = rtas_token("rtas-last-error");
883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Michael Ellerman458148c2006-06-23 18:20:13 +1000885
886int __init early_init_dt_scan_rtas(unsigned long node,
887 const char *uname, int depth, void *data)
888{
889 u32 *basep, *entryp, *sizep;
890
891 if (depth != 1 || strcmp(uname, "rtas") != 0)
892 return 0;
893
894 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
895 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
896 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
897
898 if (basep && entryp && sizep) {
899 rtas.base = *basep;
900 rtas.entry = *entryp;
901 rtas.size = *sizep;
902 }
903
Michael Ellermancc46bb92006-06-23 18:20:16 +1000904#ifdef CONFIG_UDBG_RTAS_CONSOLE
905 basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
906 if (basep)
907 rtas_putchar_token = *basep;
908
909 basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
910 if (basep)
911 rtas_getchar_token = *basep;
912#endif
913
Michael Ellerman458148c2006-06-23 18:20:13 +1000914 /* break now */
915 return 1;
916}