blob: 214780798289212eda799ebcbdde8025602e7c53 [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;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000180 int width;
181 const int *p;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000182 char *os;
183 static int display_character, set_indicator;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000184 static int display_width, display_lines, form_feed;
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100185 static const int *row_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000186 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000187 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000188 static int pending_newline = 0; /* did last write end with unprinted newline? */
189
190 if (!rtas.base)
191 return;
192
Mike Strosaker8f586b22005-06-23 16:09:41 +1000193 if (display_width == 0) {
194 display_width = 0x10;
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000195 if ((root = of_find_node_by_path("/rtas"))) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000196 if ((p = of_get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000197 "ibm,display-line-length", NULL)))
198 display_width = *p;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000199 if ((p = of_get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000200 "ibm,form-feed", NULL)))
201 form_feed = *p;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000202 if ((p = of_get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000203 "ibm,display-number-of-lines", NULL)))
204 display_lines = *p;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000205 row_width = of_get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000206 "ibm,display-truncation-length", NULL);
Stephen Rothwell8c8dc322007-04-24 13:50:55 +1000207 of_node_put(root);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000208 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000209 display_character = rtas_token("display-character");
210 set_indicator = rtas_token("set-indicator");
211 }
212
213 if (display_character == RTAS_UNKNOWN_SERVICE) {
214 /* use hex display if available */
215 if (set_indicator != RTAS_UNKNOWN_SERVICE)
216 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
217 return;
218 }
219
220 spin_lock(&progress_lock);
221
222 /*
223 * Last write ended with newline, but we didn't print it since
224 * it would just clear the bottom line of output. Print it now
225 * instead.
226 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000227 * If no newline is pending and form feed is supported, clear the
228 * display with a form feed; otherwise, print a CR to start output
229 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000230 */
231 if (pending_newline) {
232 rtas_call(display_character, 1, 1, NULL, '\r');
233 rtas_call(display_character, 1, 1, NULL, '\n');
234 pending_newline = 0;
235 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000236 current_line = 0;
237 if (form_feed)
238 rtas_call(display_character, 1, 1, NULL,
239 (char)form_feed);
240 else
241 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000242 }
243
Mike Strosaker8f586b22005-06-23 16:09:41 +1000244 if (row_width)
245 width = row_width[current_line];
246 else
247 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000248 os = s;
249 while (*os) {
250 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000251 /* If newline is the last character, save it
252 * until next call to avoid bumping up the
253 * display output.
254 */
255 if (*os == '\n' && !os[1]) {
256 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000257 current_line++;
258 if (current_line > display_lines-1)
259 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000260 spin_unlock(&progress_lock);
261 return;
262 }
263
264 /* RTAS wants CR-LF, not just LF */
265
266 if (*os == '\n') {
267 rtas_call(display_character, 1, 1, NULL, '\r');
268 rtas_call(display_character, 1, 1, NULL, '\n');
269 } else {
270 /* CR might be used to re-draw a line, so we'll
271 * leave it alone and not add LF.
272 */
273 rtas_call(display_character, 1, 1, NULL, *os);
274 }
275
Mike Strosaker8f586b22005-06-23 16:09:41 +1000276 if (row_width)
277 width = row_width[current_line];
278 else
279 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000280 } else {
281 width--;
282 rtas_call(display_character, 1, 1, NULL, *os);
283 }
284
285 os++;
286
287 /* if we overwrite the screen length */
288 if (width <= 0)
289 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
290 os++;
291 }
292
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000293 spin_unlock(&progress_lock);
294}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100295EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000296
Paul Mackerras033ef332005-10-26 17:05:24 +1000297int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000299 const int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000300 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return RTAS_UNKNOWN_SERVICE;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000302 tokp = of_get_property(rtas.dev, service, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
304}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000305EXPORT_SYMBOL(rtas_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Nathan Lynchf2d6d2d2006-12-06 18:50:45 -0600307int rtas_service_present(const char *service)
308{
309 return rtas_token(service) != RTAS_UNKNOWN_SERVICE;
310}
311EXPORT_SYMBOL(rtas_service_present);
312
Paul Mackerras033ef332005-10-26 17:05:24 +1000313#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*
315 * Return the firmware-specified size of the error log buffer
316 * for all rtas calls that require an error buffer argument.
317 * This includes 'check-exception' and 'rtas-last-error'.
318 */
319int rtas_get_error_log_max(void)
320{
321 static int rtas_error_log_max;
322 if (rtas_error_log_max)
323 return rtas_error_log_max;
324
325 rtas_error_log_max = rtas_token ("rtas-error-log-max");
326 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
327 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000328 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
329 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
331 }
332 return rtas_error_log_max;
333}
Paul Mackerras033ef332005-10-26 17:05:24 +1000334EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336
Paul Mackerras033ef332005-10-26 17:05:24 +1000337char rtas_err_buf[RTAS_ERROR_LOG_MAX];
338int rtas_last_error_token;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/** Return a copy of the detailed error text associated with the
341 * most recent failed call to rtas. Because the error text
342 * might go stale if there are any other intervening rtas calls,
343 * this routine must be called atomically with whatever produced
344 * the error (i.e. with rtas.lock still held from the previous call).
345 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000346static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct rtas_args err_args, save_args;
349 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000350 char *buf = NULL;
351
352 if (rtas_last_error_token == -1)
353 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 bufsz = rtas_get_error_log_max();
356
Paul Mackerras033ef332005-10-26 17:05:24 +1000357 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 err_args.nargs = 2;
359 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
361 err_args.args[1] = bufsz;
362 err_args.args[2] = 0;
363
364 save_args = rtas.args;
365 rtas.args = err_args;
366
367 enter_rtas(__pa(&rtas.args));
368
369 err_args = rtas.args;
370 rtas.args = save_args;
371
Paul Mackerras033ef332005-10-26 17:05:24 +1000372 /* Log the error in the unlikely case that there was one. */
373 if (unlikely(err_args.args[2] == 0)) {
374 if (altbuf) {
375 buf = altbuf;
376 } else {
377 buf = rtas_err_buf;
378 if (mem_init_done)
379 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
380 }
381 if (buf)
382 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
383 }
384
385 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Paul Mackerras033ef332005-10-26 17:05:24 +1000388#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
389
390#else /* CONFIG_RTAS_ERROR_LOGGING */
391#define __fetch_rtas_last_error(x) NULL
392#define get_errorlog_buffer() NULL
393#endif
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395int rtas_call(int token, int nargs, int nret, int *outputs, ...)
396{
397 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000398 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 unsigned long s;
400 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000401 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int ret;
403
Michael Ellerman24da3dd2006-06-23 18:20:10 +1000404 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -1;
406
407 /* Gotta do something different here, use global lock for now... */
408 spin_lock_irqsave(&rtas.lock, s);
409 rtas_args = &rtas.args;
410
411 rtas_args->token = token;
412 rtas_args->nargs = nargs;
413 rtas_args->nret = nret;
414 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
415 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000416 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 va_end(list);
419
420 for (i = 0; i < nret; ++i)
421 rtas_args->rets[i] = 0;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* A -1 return code indicates that the last command couldn't
426 be completed due to a hardware error. */
427 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000428 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (nret > 1 && outputs != NULL)
431 for (i = 0; i < nret-1; ++i)
432 outputs[i] = rtas_args->rets[i+1];
433 ret = (nret > 0)? rtas_args->rets[0]: 0;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* Gotta do something different here, use global lock for now... */
436 spin_unlock_irqrestore(&rtas.lock, s);
437
438 if (buff_copy) {
439 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
440 if (mem_init_done)
441 kfree(buff_copy);
442 }
443 return ret;
444}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000445EXPORT_SYMBOL(rtas_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
John Rose507279d2006-06-05 16:31:48 -0500447/* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
448 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
John Rose507279d2006-06-05 16:31:48 -0500450unsigned int rtas_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
John Rose507279d2006-06-05 16:31:48 -0500452 int order;
453 unsigned int ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
John Rose507279d2006-06-05 16:31:48 -0500455 if (status == RTAS_BUSY) {
456 ms = 1;
457 } else if (status >= 9900 && status <= 9905) {
458 order = status - 9900;
459 for (ms = 1; order > 0; order--)
460 ms *= 10;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
John Rose507279d2006-06-05 16:31:48 -0500463 return ms;
464}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000465EXPORT_SYMBOL(rtas_busy_delay_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
John Rose507279d2006-06-05 16:31:48 -0500467/* For an RTAS busy status code, perform the hinted delay. */
468unsigned int rtas_busy_delay(int status)
469{
470 unsigned int ms;
471
472 might_sleep();
473 ms = rtas_busy_delay_time(status);
474 if (ms)
475 msleep(ms);
476
477 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000479EXPORT_SYMBOL(rtas_busy_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481int rtas_error_rc(int rtas_rc)
482{
483 int rc;
484
485 switch (rtas_rc) {
486 case -1: /* Hardware Error */
487 rc = -EIO;
488 break;
489 case -3: /* Bad indicator/domain/etc */
490 rc = -EINVAL;
491 break;
492 case -9000: /* Isolation error */
493 rc = -EFAULT;
494 break;
495 case -9001: /* Outstanding TCE/PTE */
496 rc = -EEXIST;
497 break;
498 case -9002: /* No usable slot */
499 rc = -ENODEV;
500 break;
501 default:
502 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
503 __FUNCTION__, rtas_rc);
504 rc = -ERANGE;
505 break;
506 }
507 return rc;
508}
509
510int rtas_get_power_level(int powerdomain, int *level)
511{
512 int token = rtas_token("get-power-level");
513 int rc;
514
515 if (token == RTAS_UNKNOWN_SERVICE)
516 return -ENOENT;
517
518 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
519 udelay(1);
520
521 if (rc < 0)
522 return rtas_error_rc(rc);
523 return rc;
524}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000525EXPORT_SYMBOL(rtas_get_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527int rtas_set_power_level(int powerdomain, int level, int *setlevel)
528{
529 int token = rtas_token("set-power-level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int rc;
531
532 if (token == RTAS_UNKNOWN_SERVICE)
533 return -ENOENT;
534
John Rose507279d2006-06-05 16:31:48 -0500535 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
John Rose507279d2006-06-05 16:31:48 -0500537 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (rc < 0)
540 return rtas_error_rc(rc);
541 return rc;
542}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000543EXPORT_SYMBOL(rtas_set_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545int rtas_get_sensor(int sensor, int index, int *state)
546{
547 int token = rtas_token("get-sensor-state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int rc;
549
550 if (token == RTAS_UNKNOWN_SERVICE)
551 return -ENOENT;
552
John Rose507279d2006-06-05 16:31:48 -0500553 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 rc = rtas_call(token, 2, 2, state, sensor, index);
John Rose507279d2006-06-05 16:31:48 -0500555 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (rc < 0)
558 return rtas_error_rc(rc);
559 return rc;
560}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000561EXPORT_SYMBOL(rtas_get_sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563int rtas_set_indicator(int indicator, int index, int new_value)
564{
565 int token = rtas_token("set-indicator");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int rc;
567
568 if (token == RTAS_UNKNOWN_SERVICE)
569 return -ENOENT;
570
John Rose507279d2006-06-05 16:31:48 -0500571 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
John Rose507279d2006-06-05 16:31:48 -0500573 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (rc < 0)
576 return rtas_error_rc(rc);
577 return rc;
578}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000579EXPORT_SYMBOL(rtas_set_indicator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Haren Myneni81b73dd2006-07-27 14:29:00 -0700581/*
582 * Ignoring RTAS extended delay
583 */
584int rtas_set_indicator_fast(int indicator, int index, int new_value)
585{
586 int rc;
587 int token = rtas_token("set-indicator");
588
589 if (token == RTAS_UNKNOWN_SERVICE)
590 return -ENOENT;
591
592 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
593
594 WARN_ON(rc == -2 || (rc >= 9900 && rc <= 9905));
595
596 if (rc < 0)
597 return rtas_error_rc(rc);
598
599 return rc;
600}
601
Paul Mackerras033ef332005-10-26 17:05:24 +1000602void rtas_restart(char *cmd)
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_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 printk("RTAS system-reboot returned %d\n",
607 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
608 for (;;);
609}
610
Paul Mackerras033ef332005-10-26 17:05:24 +1000611void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100613 if (rtas_flash_term_hook)
614 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* allow power on only with power button press */
616 printk("RTAS power-off returned %d\n",
617 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
618 for (;;);
619}
620
Paul Mackerras033ef332005-10-26 17:05:24 +1000621void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100623 if (rtas_flash_term_hook)
624 rtas_flash_term_hook(SYS_HALT);
625 /* allow power on only with power button press */
626 printk("RTAS power-off returned %d\n",
627 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
628 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/* Must be in the RMO region, so we place it here */
632static char rtas_os_term_buf[2048];
633
634void rtas_os_term(char *str)
635{
636 int status;
637
Olaf Hering39ed2fe2006-08-21 18:11:32 +0200638 if (panic_timeout)
639 return;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
642 return;
643
644 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
645
646 do {
647 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
648 __pa(rtas_os_term_buf));
John Rose507279d2006-06-05 16:31:48 -0500649 } while (rtas_busy_delay(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
John Rose507279d2006-06-05 16:31:48 -0500651 if (status != 0)
652 printk(KERN_EMERG "ibm,os-term call failed %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600656static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
657#ifdef CONFIG_PPC_PSERIES
658static void rtas_percpu_suspend_me(void *info)
659{
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600660 int i;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600661 long rc;
662 long flags;
663 struct rtas_suspend_me_data *data =
664 (struct rtas_suspend_me_data *)info;
665
666 /*
667 * We use "waiting" to indicate our state. As long
668 * as it is >0, we are still trying to all join up.
669 * If it goes to 0, we have successfully joined up and
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200670 * one thread got H_CONTINUE. If any error happens,
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600671 * we set it to <0.
672 */
673 local_irq_save(flags);
674 do {
675 rc = plpar_hcall_norets(H_JOIN);
676 smp_rmb();
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200677 } while (rc == H_SUCCESS && data->waiting > 0);
678 if (rc == H_SUCCESS)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600679 goto out;
680
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200681 if (rc == H_CONTINUE) {
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600682 data->waiting = 0;
Dave C Boutcherc4cb8ec2006-02-03 01:18:46 -0600683 data->args->args[data->args->nargs] =
684 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800685 for_each_possible_cpu(i)
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600686 plpar_hcall_norets(H_PROD,i);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600687 } else {
688 data->waiting = -EBUSY;
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200689 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600690 }
691
692out:
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600693 local_irq_restore(flags);
694 return;
695}
696
697static int rtas_ibm_suspend_me(struct rtas_args *args)
698{
699 int i;
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500700 long state;
701 long rc;
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000702 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600703 struct rtas_suspend_me_data data;
704
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500705 /* Make sure the state is valid */
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000706 rc = plpar_hcall(H_VASI_STATE, retbuf,
707 ((u64)args->args[0] << 32) | args->args[1]);
708
709 state = retbuf[0];
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500710
711 if (rc) {
712 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
713 return rc;
714 } else if (state == H_VASI_ENABLED) {
715 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
716 return 0;
717 } else if (state != H_VASI_SUSPENDING) {
718 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
719 state);
720 args->args[args->nargs] = -1;
721 return 0;
722 }
723
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600724 data.waiting = 1;
725 data.args = args;
726
727 /* Call function on all CPUs. One of us will make the
728 * rtas call
729 */
730 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
731 data.waiting = -EINVAL;
732
733 if (data.waiting != 0)
734 printk(KERN_ERR "Error doing global join\n");
735
736 /* Prod each CPU. This won't hurt, and will wake
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200737 * anyone we successfully put to sleep with H_JOIN.
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600738 */
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800739 for_each_possible_cpu(i)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600740 plpar_hcall_norets(H_PROD, i);
741
742 return data.waiting;
743}
744#else /* CONFIG_PPC_PSERIES */
745static int rtas_ibm_suspend_me(struct rtas_args *args)
746{
747 return -ENOSYS;
748}
749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
752{
753 struct rtas_args args;
754 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000755 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int nargs;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600757 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (!capable(CAP_SYS_ADMIN))
760 return -EPERM;
761
762 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
763 return -EFAULT;
764
765 nargs = args.nargs;
766 if (nargs > ARRAY_SIZE(args.args)
767 || args.nret > ARRAY_SIZE(args.args)
768 || nargs + args.nret > ARRAY_SIZE(args.args))
769 return -EINVAL;
770
771 /* Copy in args. */
772 if (copy_from_user(args.args, uargs->args,
773 nargs * sizeof(rtas_arg_t)) != 0)
774 return -EFAULT;
775
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600776 if (args.token == RTAS_UNKNOWN_SERVICE)
777 return -EINVAL;
778
779 /* Need to handle ibm,suspend_me call specially */
780 if (args.token == ibm_suspend_me_token) {
781 rc = rtas_ibm_suspend_me(&args);
782 if (rc)
783 return rc;
784 goto copy_return;
785 }
786
Paul Mackerras033ef332005-10-26 17:05:24 +1000787 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 spin_lock_irqsave(&rtas.lock, flags);
790
791 rtas.args = args;
792 enter_rtas(__pa(&rtas.args));
793 args = rtas.args;
794
795 args.rets = &args.args[nargs];
796
797 /* A -1 return code indicates that the last command couldn't
798 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000799 if (args.rets[0] == -1)
800 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 spin_unlock_irqrestore(&rtas.lock, flags);
803
804 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000805 if (errbuf)
806 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 kfree(buff_copy);
808 }
809
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600810 copy_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* Copy out args. */
812 if (copy_to_user(uargs->args + nargs,
813 args.args + nargs,
814 args.nret * sizeof(rtas_arg_t)) != 0)
815 return -EFAULT;
816
817 return 0;
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100821 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * informations from the device-tree and allocate the RMO buffer for userland
823 * accesses.
824 */
825void __init rtas_initialize(void)
826{
Paul Mackerras033ef332005-10-26 17:05:24 +1000827 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Get RTAS dev node and fill up our "rtas" structure with infos
830 * about it.
831 */
832 rtas.dev = of_find_node_by_name(NULL, "rtas");
833 if (rtas.dev) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000834 const u32 *basep, *entryp, *sizep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000836 basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
837 sizep = of_get_property(rtas.dev, "rtas-size", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (basep != NULL && sizep != NULL) {
839 rtas.base = *basep;
840 rtas.size = *sizep;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000841 entryp = of_get_property(rtas.dev,
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000842 "linux,rtas-entry", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (entryp == NULL) /* Ugh */
844 rtas.entry = rtas.base;
845 else
846 rtas.entry = *entryp;
847 } else
848 rtas.dev = NULL;
849 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000850 if (!rtas.dev)
851 return;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* If RTAS was found, allocate the RMO buffer for it and look for
854 * the stop-self token if any
855 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000856#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100857 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000858 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600859 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
860 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000861#endif
862 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Paul Mackerras033ef332005-10-26 17:05:24 +1000864#ifdef CONFIG_RTAS_ERROR_LOGGING
865 rtas_last_error_token = rtas_token("rtas-last-error");
866#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
Michael Ellerman458148c2006-06-23 18:20:13 +1000868
869int __init early_init_dt_scan_rtas(unsigned long node,
870 const char *uname, int depth, void *data)
871{
872 u32 *basep, *entryp, *sizep;
873
874 if (depth != 1 || strcmp(uname, "rtas") != 0)
875 return 0;
876
877 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
878 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
879 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
880
881 if (basep && entryp && sizep) {
882 rtas.base = *basep;
883 rtas.entry = *entryp;
884 rtas.size = *sizep;
885 }
886
Michael Ellermancc46bb92006-06-23 18:20:16 +1000887#ifdef CONFIG_UDBG_RTAS_CONSOLE
888 basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
889 if (basep)
890 rtas_putchar_token = *basep;
891
892 basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
893 if (basep)
894 rtas_getchar_token = *basep;
Michael Neuling9a2ded52006-08-16 23:12:14 -0500895
896 if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
897 rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
898 udbg_init_rtas_console();
899
Michael Ellermancc46bb92006-06-23 18:20:16 +1000900#endif
901
Michael Ellerman458148c2006-06-23 18:20:13 +1000902 /* break now */
903 return 1;
904}