blob: 14353b8789ddebabe7bc653d2a43ac90b4abe902 [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;
185 const static 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;
195 if ((root = find_path_device("/rtas"))) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000196 if ((p = get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000197 "ibm,display-line-length", NULL)))
198 display_width = *p;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000199 if ((p = get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000200 "ibm,form-feed", NULL)))
201 form_feed = *p;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000202 if ((p = get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000203 "ibm,display-number-of-lines", NULL)))
204 display_lines = *p;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000205 row_width = get_property(root,
Mike Strosaker8f586b22005-06-23 16:09:41 +1000206 "ibm,display-truncation-length", NULL);
207 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000208 display_character = rtas_token("display-character");
209 set_indicator = rtas_token("set-indicator");
210 }
211
212 if (display_character == RTAS_UNKNOWN_SERVICE) {
213 /* use hex display if available */
214 if (set_indicator != RTAS_UNKNOWN_SERVICE)
215 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
216 return;
217 }
218
219 spin_lock(&progress_lock);
220
221 /*
222 * Last write ended with newline, but we didn't print it since
223 * it would just clear the bottom line of output. Print it now
224 * instead.
225 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000226 * If no newline is pending and form feed is supported, clear the
227 * display with a form feed; otherwise, print a CR to start output
228 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000229 */
230 if (pending_newline) {
231 rtas_call(display_character, 1, 1, NULL, '\r');
232 rtas_call(display_character, 1, 1, NULL, '\n');
233 pending_newline = 0;
234 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000235 current_line = 0;
236 if (form_feed)
237 rtas_call(display_character, 1, 1, NULL,
238 (char)form_feed);
239 else
240 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000241 }
242
Mike Strosaker8f586b22005-06-23 16:09:41 +1000243 if (row_width)
244 width = row_width[current_line];
245 else
246 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000247 os = s;
248 while (*os) {
249 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000250 /* If newline is the last character, save it
251 * until next call to avoid bumping up the
252 * display output.
253 */
254 if (*os == '\n' && !os[1]) {
255 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000256 current_line++;
257 if (current_line > display_lines-1)
258 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000259 spin_unlock(&progress_lock);
260 return;
261 }
262
263 /* RTAS wants CR-LF, not just LF */
264
265 if (*os == '\n') {
266 rtas_call(display_character, 1, 1, NULL, '\r');
267 rtas_call(display_character, 1, 1, NULL, '\n');
268 } else {
269 /* CR might be used to re-draw a line, so we'll
270 * leave it alone and not add LF.
271 */
272 rtas_call(display_character, 1, 1, NULL, *os);
273 }
274
Mike Strosaker8f586b22005-06-23 16:09:41 +1000275 if (row_width)
276 width = row_width[current_line];
277 else
278 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000279 } else {
280 width--;
281 rtas_call(display_character, 1, 1, NULL, *os);
282 }
283
284 os++;
285
286 /* if we overwrite the screen length */
287 if (width <= 0)
288 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
289 os++;
290 }
291
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000292 spin_unlock(&progress_lock);
293}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100294EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000295
Paul Mackerras033ef332005-10-26 17:05:24 +1000296int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000298 const int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000299 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return RTAS_UNKNOWN_SERVICE;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000301 tokp = get_property(rtas.dev, service, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
303}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000304EXPORT_SYMBOL(rtas_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Paul Mackerras033ef332005-10-26 17:05:24 +1000306#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * Return the firmware-specified size of the error log buffer
309 * for all rtas calls that require an error buffer argument.
310 * This includes 'check-exception' and 'rtas-last-error'.
311 */
312int rtas_get_error_log_max(void)
313{
314 static int rtas_error_log_max;
315 if (rtas_error_log_max)
316 return rtas_error_log_max;
317
318 rtas_error_log_max = rtas_token ("rtas-error-log-max");
319 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
320 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000321 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
322 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
324 }
325 return rtas_error_log_max;
326}
Paul Mackerras033ef332005-10-26 17:05:24 +1000327EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329
Paul Mackerras033ef332005-10-26 17:05:24 +1000330char rtas_err_buf[RTAS_ERROR_LOG_MAX];
331int rtas_last_error_token;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/** Return a copy of the detailed error text associated with the
334 * most recent failed call to rtas. Because the error text
335 * might go stale if there are any other intervening rtas calls,
336 * this routine must be called atomically with whatever produced
337 * the error (i.e. with rtas.lock still held from the previous call).
338 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000339static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct rtas_args err_args, save_args;
342 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000343 char *buf = NULL;
344
345 if (rtas_last_error_token == -1)
346 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 bufsz = rtas_get_error_log_max();
349
Paul Mackerras033ef332005-10-26 17:05:24 +1000350 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 err_args.nargs = 2;
352 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
354 err_args.args[1] = bufsz;
355 err_args.args[2] = 0;
356
357 save_args = rtas.args;
358 rtas.args = err_args;
359
360 enter_rtas(__pa(&rtas.args));
361
362 err_args = rtas.args;
363 rtas.args = save_args;
364
Paul Mackerras033ef332005-10-26 17:05:24 +1000365 /* Log the error in the unlikely case that there was one. */
366 if (unlikely(err_args.args[2] == 0)) {
367 if (altbuf) {
368 buf = altbuf;
369 } else {
370 buf = rtas_err_buf;
371 if (mem_init_done)
372 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
373 }
374 if (buf)
375 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
376 }
377
378 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Paul Mackerras033ef332005-10-26 17:05:24 +1000381#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
382
383#else /* CONFIG_RTAS_ERROR_LOGGING */
384#define __fetch_rtas_last_error(x) NULL
385#define get_errorlog_buffer() NULL
386#endif
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388int rtas_call(int token, int nargs, int nret, int *outputs, ...)
389{
390 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000391 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 unsigned long s;
393 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000394 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int ret;
396
Michael Ellerman24da3dd2006-06-23 18:20:10 +1000397 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -1;
399
400 /* Gotta do something different here, use global lock for now... */
401 spin_lock_irqsave(&rtas.lock, s);
402 rtas_args = &rtas.args;
403
404 rtas_args->token = token;
405 rtas_args->nargs = nargs;
406 rtas_args->nret = nret;
407 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
408 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000409 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 va_end(list);
412
413 for (i = 0; i < nret; ++i)
414 rtas_args->rets[i] = 0;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* A -1 return code indicates that the last command couldn't
419 be completed due to a hardware error. */
420 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000421 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (nret > 1 && outputs != NULL)
424 for (i = 0; i < nret-1; ++i)
425 outputs[i] = rtas_args->rets[i+1];
426 ret = (nret > 0)? rtas_args->rets[0]: 0;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Gotta do something different here, use global lock for now... */
429 spin_unlock_irqrestore(&rtas.lock, s);
430
431 if (buff_copy) {
432 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
433 if (mem_init_done)
434 kfree(buff_copy);
435 }
436 return ret;
437}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000438EXPORT_SYMBOL(rtas_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
John Rose507279d2006-06-05 16:31:48 -0500440/* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
441 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
John Rose507279d2006-06-05 16:31:48 -0500443unsigned int rtas_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
John Rose507279d2006-06-05 16:31:48 -0500445 int order;
446 unsigned int ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
John Rose507279d2006-06-05 16:31:48 -0500448 if (status == RTAS_BUSY) {
449 ms = 1;
450 } else if (status >= 9900 && status <= 9905) {
451 order = status - 9900;
452 for (ms = 1; order > 0; order--)
453 ms *= 10;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
John Rose507279d2006-06-05 16:31:48 -0500456 return ms;
457}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000458EXPORT_SYMBOL(rtas_busy_delay_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
John Rose507279d2006-06-05 16:31:48 -0500460/* For an RTAS busy status code, perform the hinted delay. */
461unsigned int rtas_busy_delay(int status)
462{
463 unsigned int ms;
464
465 might_sleep();
466 ms = rtas_busy_delay_time(status);
467 if (ms)
468 msleep(ms);
469
470 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000472EXPORT_SYMBOL(rtas_busy_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474int rtas_error_rc(int rtas_rc)
475{
476 int rc;
477
478 switch (rtas_rc) {
479 case -1: /* Hardware Error */
480 rc = -EIO;
481 break;
482 case -3: /* Bad indicator/domain/etc */
483 rc = -EINVAL;
484 break;
485 case -9000: /* Isolation error */
486 rc = -EFAULT;
487 break;
488 case -9001: /* Outstanding TCE/PTE */
489 rc = -EEXIST;
490 break;
491 case -9002: /* No usable slot */
492 rc = -ENODEV;
493 break;
494 default:
495 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
496 __FUNCTION__, rtas_rc);
497 rc = -ERANGE;
498 break;
499 }
500 return rc;
501}
502
503int rtas_get_power_level(int powerdomain, int *level)
504{
505 int token = rtas_token("get-power-level");
506 int rc;
507
508 if (token == RTAS_UNKNOWN_SERVICE)
509 return -ENOENT;
510
511 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
512 udelay(1);
513
514 if (rc < 0)
515 return rtas_error_rc(rc);
516 return rc;
517}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000518EXPORT_SYMBOL(rtas_get_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520int rtas_set_power_level(int powerdomain, int level, int *setlevel)
521{
522 int token = rtas_token("set-power-level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int rc;
524
525 if (token == RTAS_UNKNOWN_SERVICE)
526 return -ENOENT;
527
John Rose507279d2006-06-05 16:31:48 -0500528 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
John Rose507279d2006-06-05 16:31:48 -0500530 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (rc < 0)
533 return rtas_error_rc(rc);
534 return rc;
535}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000536EXPORT_SYMBOL(rtas_set_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538int rtas_get_sensor(int sensor, int index, int *state)
539{
540 int token = rtas_token("get-sensor-state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int rc;
542
543 if (token == RTAS_UNKNOWN_SERVICE)
544 return -ENOENT;
545
John Rose507279d2006-06-05 16:31:48 -0500546 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 rc = rtas_call(token, 2, 2, state, sensor, index);
John Rose507279d2006-06-05 16:31:48 -0500548 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (rc < 0)
551 return rtas_error_rc(rc);
552 return rc;
553}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000554EXPORT_SYMBOL(rtas_get_sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556int rtas_set_indicator(int indicator, int index, int new_value)
557{
558 int token = rtas_token("set-indicator");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int rc;
560
561 if (token == RTAS_UNKNOWN_SERVICE)
562 return -ENOENT;
563
John Rose507279d2006-06-05 16:31:48 -0500564 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
John Rose507279d2006-06-05 16:31:48 -0500566 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (rc < 0)
569 return rtas_error_rc(rc);
570 return rc;
571}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000572EXPORT_SYMBOL(rtas_set_indicator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Paul Mackerras033ef332005-10-26 17:05:24 +1000574void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100576 if (rtas_flash_term_hook)
577 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 printk("RTAS system-reboot returned %d\n",
579 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
580 for (;;);
581}
582
Paul Mackerras033ef332005-10-26 17:05:24 +1000583void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100585 if (rtas_flash_term_hook)
586 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* allow power on only with power button press */
588 printk("RTAS power-off returned %d\n",
589 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
590 for (;;);
591}
592
Paul Mackerras033ef332005-10-26 17:05:24 +1000593void rtas_halt(void)
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_HALT);
597 /* allow power on only with power button press */
598 printk("RTAS power-off returned %d\n",
599 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
600 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/* Must be in the RMO region, so we place it here */
604static char rtas_os_term_buf[2048];
605
606void rtas_os_term(char *str)
607{
608 int status;
609
610 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
611 return;
612
613 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
614
615 do {
616 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
617 __pa(rtas_os_term_buf));
John Rose507279d2006-06-05 16:31:48 -0500618 } while (rtas_busy_delay(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
John Rose507279d2006-06-05 16:31:48 -0500620 if (status != 0)
621 printk(KERN_EMERG "ibm,os-term call failed %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600625static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
626#ifdef CONFIG_PPC_PSERIES
627static void rtas_percpu_suspend_me(void *info)
628{
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600629 int i;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600630 long rc;
631 long flags;
632 struct rtas_suspend_me_data *data =
633 (struct rtas_suspend_me_data *)info;
634
635 /*
636 * We use "waiting" to indicate our state. As long
637 * as it is >0, we are still trying to all join up.
638 * If it goes to 0, we have successfully joined up and
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200639 * one thread got H_CONTINUE. If any error happens,
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600640 * we set it to <0.
641 */
642 local_irq_save(flags);
643 do {
644 rc = plpar_hcall_norets(H_JOIN);
645 smp_rmb();
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200646 } while (rc == H_SUCCESS && data->waiting > 0);
647 if (rc == H_SUCCESS)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600648 goto out;
649
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200650 if (rc == H_CONTINUE) {
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600651 data->waiting = 0;
Dave C Boutcherc4cb8ec2006-02-03 01:18:46 -0600652 data->args->args[data->args->nargs] =
653 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800654 for_each_possible_cpu(i)
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600655 plpar_hcall_norets(H_PROD,i);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600656 } else {
657 data->waiting = -EBUSY;
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200658 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600659 }
660
661out:
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600662 local_irq_restore(flags);
663 return;
664}
665
666static int rtas_ibm_suspend_me(struct rtas_args *args)
667{
668 int i;
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500669 long state;
670 long rc;
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000671 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600672 struct rtas_suspend_me_data data;
673
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500674 /* Make sure the state is valid */
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000675 rc = plpar_hcall(H_VASI_STATE, retbuf,
676 ((u64)args->args[0] << 32) | args->args[1]);
677
678 state = retbuf[0];
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500679
680 if (rc) {
681 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
682 return rc;
683 } else if (state == H_VASI_ENABLED) {
684 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
685 return 0;
686 } else if (state != H_VASI_SUSPENDING) {
687 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
688 state);
689 args->args[args->nargs] = -1;
690 return 0;
691 }
692
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600693 data.waiting = 1;
694 data.args = args;
695
696 /* Call function on all CPUs. One of us will make the
697 * rtas call
698 */
699 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
700 data.waiting = -EINVAL;
701
702 if (data.waiting != 0)
703 printk(KERN_ERR "Error doing global join\n");
704
705 /* Prod each CPU. This won't hurt, and will wake
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200706 * anyone we successfully put to sleep with H_JOIN.
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600707 */
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800708 for_each_possible_cpu(i)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600709 plpar_hcall_norets(H_PROD, i);
710
711 return data.waiting;
712}
713#else /* CONFIG_PPC_PSERIES */
714static int rtas_ibm_suspend_me(struct rtas_args *args)
715{
716 return -ENOSYS;
717}
718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
721{
722 struct rtas_args args;
723 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000724 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int nargs;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600726 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (!capable(CAP_SYS_ADMIN))
729 return -EPERM;
730
731 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
732 return -EFAULT;
733
734 nargs = args.nargs;
735 if (nargs > ARRAY_SIZE(args.args)
736 || args.nret > ARRAY_SIZE(args.args)
737 || nargs + args.nret > ARRAY_SIZE(args.args))
738 return -EINVAL;
739
740 /* Copy in args. */
741 if (copy_from_user(args.args, uargs->args,
742 nargs * sizeof(rtas_arg_t)) != 0)
743 return -EFAULT;
744
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600745 if (args.token == RTAS_UNKNOWN_SERVICE)
746 return -EINVAL;
747
748 /* Need to handle ibm,suspend_me call specially */
749 if (args.token == ibm_suspend_me_token) {
750 rc = rtas_ibm_suspend_me(&args);
751 if (rc)
752 return rc;
753 goto copy_return;
754 }
755
Paul Mackerras033ef332005-10-26 17:05:24 +1000756 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 spin_lock_irqsave(&rtas.lock, flags);
759
760 rtas.args = args;
761 enter_rtas(__pa(&rtas.args));
762 args = rtas.args;
763
764 args.rets = &args.args[nargs];
765
766 /* A -1 return code indicates that the last command couldn't
767 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000768 if (args.rets[0] == -1)
769 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 spin_unlock_irqrestore(&rtas.lock, flags);
772
773 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000774 if (errbuf)
775 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 kfree(buff_copy);
777 }
778
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600779 copy_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Copy out args. */
781 if (copy_to_user(uargs->args + nargs,
782 args.args + nargs,
783 args.nret * sizeof(rtas_arg_t)) != 0)
784 return -EFAULT;
785
786 return 0;
787}
788
789/* This version can't take the spinlock, because it never returns */
790
791struct rtas_args rtas_stop_self_args = {
792 /* The token is initialized for real in setup_system() */
793 .token = RTAS_UNKNOWN_SERVICE,
794 .nargs = 0,
795 .nret = 1,
796 .rets = &rtas_stop_self_args.args[0],
797};
798
799void rtas_stop_self(void)
800{
801 struct rtas_args *rtas_args = &rtas_stop_self_args;
802
803 local_irq_disable();
804
805 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
806
807 printk("cpu %u (hwid %u) Ready to die...\n",
808 smp_processor_id(), hard_smp_processor_id());
809 enter_rtas(__pa(rtas_args));
810
811 panic("Alas, I survived.\n");
812}
813
814/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100815 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * informations from the device-tree and allocate the RMO buffer for userland
817 * accesses.
818 */
819void __init rtas_initialize(void)
820{
Paul Mackerras033ef332005-10-26 17:05:24 +1000821 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Get RTAS dev node and fill up our "rtas" structure with infos
824 * about it.
825 */
826 rtas.dev = of_find_node_by_name(NULL, "rtas");
827 if (rtas.dev) {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000828 const u32 *basep, *entryp, *sizep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000830 basep = get_property(rtas.dev, "linux,rtas-base", NULL);
831 sizep = get_property(rtas.dev, "rtas-size", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (basep != NULL && sizep != NULL) {
833 rtas.base = *basep;
834 rtas.size = *sizep;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000835 entryp = get_property(rtas.dev,
836 "linux,rtas-entry", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (entryp == NULL) /* Ugh */
838 rtas.entry = rtas.base;
839 else
840 rtas.entry = *entryp;
841 } else
842 rtas.dev = NULL;
843 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000844 if (!rtas.dev)
845 return;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* If RTAS was found, allocate the RMO buffer for it and look for
848 * the stop-self token if any
849 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000850#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100851 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000852 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600853 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
854 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000855#endif
856 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000859 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000861#ifdef CONFIG_RTAS_ERROR_LOGGING
862 rtas_last_error_token = rtas_token("rtas-last-error");
863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Michael Ellerman458148c2006-06-23 18:20:13 +1000865
866int __init early_init_dt_scan_rtas(unsigned long node,
867 const char *uname, int depth, void *data)
868{
869 u32 *basep, *entryp, *sizep;
870
871 if (depth != 1 || strcmp(uname, "rtas") != 0)
872 return 0;
873
874 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
875 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
876 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
877
878 if (basep && entryp && sizep) {
879 rtas.base = *basep;
880 rtas.entry = *entryp;
881 rtas.size = *sizep;
882 }
883
Michael Ellermancc46bb92006-06-23 18:20:16 +1000884#ifdef CONFIG_UDBG_RTAS_CONSOLE
885 basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
886 if (basep)
887 rtas_putchar_token = *basep;
888
889 basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
890 if (basep)
891 rtas_getchar_token = *basep;
892#endif
893
Michael Ellerman458148c2006-06-23 18:20:13 +1000894 /* break now */
895 return 1;
896}