blob: 061d8afd246ef471474636ab16b4fda1175cefb3 [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 Ellerman296167a2006-01-11 11:54:09 +1100112void __init udbg_init_rtas(void)
113{
114 udbg_putc = call_rtas_display_status_delay;
115}
116
Paul Mackerras033ef332005-10-26 17:05:24 +1000117void rtas_progress(char *s, unsigned short hex)
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000118{
119 struct device_node *root;
120 int width, *p;
121 char *os;
122 static int display_character, set_indicator;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000123 static int display_width, display_lines, *row_width, form_feed;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000124 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000125 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000126 static int pending_newline = 0; /* did last write end with unprinted newline? */
127
128 if (!rtas.base)
129 return;
130
Mike Strosaker8f586b22005-06-23 16:09:41 +1000131 if (display_width == 0) {
132 display_width = 0x10;
133 if ((root = find_path_device("/rtas"))) {
134 if ((p = (unsigned int *)get_property(root,
135 "ibm,display-line-length", NULL)))
136 display_width = *p;
137 if ((p = (unsigned int *)get_property(root,
138 "ibm,form-feed", NULL)))
139 form_feed = *p;
140 if ((p = (unsigned int *)get_property(root,
141 "ibm,display-number-of-lines", NULL)))
142 display_lines = *p;
143 row_width = (unsigned int *)get_property(root,
144 "ibm,display-truncation-length", NULL);
145 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000146 display_character = rtas_token("display-character");
147 set_indicator = rtas_token("set-indicator");
148 }
149
150 if (display_character == RTAS_UNKNOWN_SERVICE) {
151 /* use hex display if available */
152 if (set_indicator != RTAS_UNKNOWN_SERVICE)
153 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
154 return;
155 }
156
157 spin_lock(&progress_lock);
158
159 /*
160 * Last write ended with newline, but we didn't print it since
161 * it would just clear the bottom line of output. Print it now
162 * instead.
163 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000164 * If no newline is pending and form feed is supported, clear the
165 * display with a form feed; otherwise, print a CR to start output
166 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000167 */
168 if (pending_newline) {
169 rtas_call(display_character, 1, 1, NULL, '\r');
170 rtas_call(display_character, 1, 1, NULL, '\n');
171 pending_newline = 0;
172 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000173 current_line = 0;
174 if (form_feed)
175 rtas_call(display_character, 1, 1, NULL,
176 (char)form_feed);
177 else
178 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000179 }
180
Mike Strosaker8f586b22005-06-23 16:09:41 +1000181 if (row_width)
182 width = row_width[current_line];
183 else
184 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000185 os = s;
186 while (*os) {
187 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000188 /* If newline is the last character, save it
189 * until next call to avoid bumping up the
190 * display output.
191 */
192 if (*os == '\n' && !os[1]) {
193 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000194 current_line++;
195 if (current_line > display_lines-1)
196 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000197 spin_unlock(&progress_lock);
198 return;
199 }
200
201 /* RTAS wants CR-LF, not just LF */
202
203 if (*os == '\n') {
204 rtas_call(display_character, 1, 1, NULL, '\r');
205 rtas_call(display_character, 1, 1, NULL, '\n');
206 } else {
207 /* CR might be used to re-draw a line, so we'll
208 * leave it alone and not add LF.
209 */
210 rtas_call(display_character, 1, 1, NULL, *os);
211 }
212
Mike Strosaker8f586b22005-06-23 16:09:41 +1000213 if (row_width)
214 width = row_width[current_line];
215 else
216 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000217 } else {
218 width--;
219 rtas_call(display_character, 1, 1, NULL, *os);
220 }
221
222 os++;
223
224 /* if we overwrite the screen length */
225 if (width <= 0)
226 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
227 os++;
228 }
229
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000230 spin_unlock(&progress_lock);
231}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100232EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000233
Paul Mackerras033ef332005-10-26 17:05:24 +1000234int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000237 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return RTAS_UNKNOWN_SERVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 tokp = (int *) get_property(rtas.dev, service, NULL);
240 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
241}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000242EXPORT_SYMBOL(rtas_token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Paul Mackerras033ef332005-10-26 17:05:24 +1000244#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * Return the firmware-specified size of the error log buffer
247 * for all rtas calls that require an error buffer argument.
248 * This includes 'check-exception' and 'rtas-last-error'.
249 */
250int rtas_get_error_log_max(void)
251{
252 static int rtas_error_log_max;
253 if (rtas_error_log_max)
254 return rtas_error_log_max;
255
256 rtas_error_log_max = rtas_token ("rtas-error-log-max");
257 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
258 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000259 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
260 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
262 }
263 return rtas_error_log_max;
264}
Paul Mackerras033ef332005-10-26 17:05:24 +1000265EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267
Paul Mackerras033ef332005-10-26 17:05:24 +1000268char rtas_err_buf[RTAS_ERROR_LOG_MAX];
269int rtas_last_error_token;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/** Return a copy of the detailed error text associated with the
272 * most recent failed call to rtas. Because the error text
273 * might go stale if there are any other intervening rtas calls,
274 * this routine must be called atomically with whatever produced
275 * the error (i.e. with rtas.lock still held from the previous call).
276 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000277static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct rtas_args err_args, save_args;
280 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000281 char *buf = NULL;
282
283 if (rtas_last_error_token == -1)
284 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 bufsz = rtas_get_error_log_max();
287
Paul Mackerras033ef332005-10-26 17:05:24 +1000288 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 err_args.nargs = 2;
290 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
292 err_args.args[1] = bufsz;
293 err_args.args[2] = 0;
294
295 save_args = rtas.args;
296 rtas.args = err_args;
297
298 enter_rtas(__pa(&rtas.args));
299
300 err_args = rtas.args;
301 rtas.args = save_args;
302
Paul Mackerras033ef332005-10-26 17:05:24 +1000303 /* Log the error in the unlikely case that there was one. */
304 if (unlikely(err_args.args[2] == 0)) {
305 if (altbuf) {
306 buf = altbuf;
307 } else {
308 buf = rtas_err_buf;
309 if (mem_init_done)
310 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
311 }
312 if (buf)
313 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
314 }
315
316 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Paul Mackerras033ef332005-10-26 17:05:24 +1000319#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
320
321#else /* CONFIG_RTAS_ERROR_LOGGING */
322#define __fetch_rtas_last_error(x) NULL
323#define get_errorlog_buffer() NULL
324#endif
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326int rtas_call(int token, int nargs, int nret, int *outputs, ...)
327{
328 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000329 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned long s;
331 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000332 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int ret;
334
Michael Ellerman24da3dd2006-06-23 18:20:10 +1000335 if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -1;
337
338 /* Gotta do something different here, use global lock for now... */
339 spin_lock_irqsave(&rtas.lock, s);
340 rtas_args = &rtas.args;
341
342 rtas_args->token = token;
343 rtas_args->nargs = nargs;
344 rtas_args->nret = nret;
345 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
346 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000347 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 va_end(list);
350
351 for (i = 0; i < nret; ++i)
352 rtas_args->rets[i] = 0;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* A -1 return code indicates that the last command couldn't
357 be completed due to a hardware error. */
358 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000359 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (nret > 1 && outputs != NULL)
362 for (i = 0; i < nret-1; ++i)
363 outputs[i] = rtas_args->rets[i+1];
364 ret = (nret > 0)? rtas_args->rets[0]: 0;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Gotta do something different here, use global lock for now... */
367 spin_unlock_irqrestore(&rtas.lock, s);
368
369 if (buff_copy) {
370 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
371 if (mem_init_done)
372 kfree(buff_copy);
373 }
374 return ret;
375}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000376EXPORT_SYMBOL(rtas_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
John Rose507279d2006-06-05 16:31:48 -0500378/* For RTAS_BUSY (-2), delay for 1 millisecond. For an extended busy status
379 * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
John Rose507279d2006-06-05 16:31:48 -0500381unsigned int rtas_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
John Rose507279d2006-06-05 16:31:48 -0500383 int order;
384 unsigned int ms = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
John Rose507279d2006-06-05 16:31:48 -0500386 if (status == RTAS_BUSY) {
387 ms = 1;
388 } else if (status >= 9900 && status <= 9905) {
389 order = status - 9900;
390 for (ms = 1; order > 0; order--)
391 ms *= 10;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
John Rose507279d2006-06-05 16:31:48 -0500394 return ms;
395}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000396EXPORT_SYMBOL(rtas_busy_delay_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
John Rose507279d2006-06-05 16:31:48 -0500398/* For an RTAS busy status code, perform the hinted delay. */
399unsigned int rtas_busy_delay(int status)
400{
401 unsigned int ms;
402
403 might_sleep();
404 ms = rtas_busy_delay_time(status);
405 if (ms)
406 msleep(ms);
407
408 return ms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000410EXPORT_SYMBOL(rtas_busy_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412int rtas_error_rc(int rtas_rc)
413{
414 int rc;
415
416 switch (rtas_rc) {
417 case -1: /* Hardware Error */
418 rc = -EIO;
419 break;
420 case -3: /* Bad indicator/domain/etc */
421 rc = -EINVAL;
422 break;
423 case -9000: /* Isolation error */
424 rc = -EFAULT;
425 break;
426 case -9001: /* Outstanding TCE/PTE */
427 rc = -EEXIST;
428 break;
429 case -9002: /* No usable slot */
430 rc = -ENODEV;
431 break;
432 default:
433 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
434 __FUNCTION__, rtas_rc);
435 rc = -ERANGE;
436 break;
437 }
438 return rc;
439}
440
441int rtas_get_power_level(int powerdomain, int *level)
442{
443 int token = rtas_token("get-power-level");
444 int rc;
445
446 if (token == RTAS_UNKNOWN_SERVICE)
447 return -ENOENT;
448
449 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
450 udelay(1);
451
452 if (rc < 0)
453 return rtas_error_rc(rc);
454 return rc;
455}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000456EXPORT_SYMBOL(rtas_get_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458int rtas_set_power_level(int powerdomain, int level, int *setlevel)
459{
460 int token = rtas_token("set-power-level");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int rc;
462
463 if (token == RTAS_UNKNOWN_SERVICE)
464 return -ENOENT;
465
John Rose507279d2006-06-05 16:31:48 -0500466 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
John Rose507279d2006-06-05 16:31:48 -0500468 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (rc < 0)
471 return rtas_error_rc(rc);
472 return rc;
473}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000474EXPORT_SYMBOL(rtas_set_power_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476int rtas_get_sensor(int sensor, int index, int *state)
477{
478 int token = rtas_token("get-sensor-state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int rc;
480
481 if (token == RTAS_UNKNOWN_SERVICE)
482 return -ENOENT;
483
John Rose507279d2006-06-05 16:31:48 -0500484 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 rc = rtas_call(token, 2, 2, state, sensor, index);
John Rose507279d2006-06-05 16:31:48 -0500486 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (rc < 0)
489 return rtas_error_rc(rc);
490 return rc;
491}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000492EXPORT_SYMBOL(rtas_get_sensor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494int rtas_set_indicator(int indicator, int index, int new_value)
495{
496 int token = rtas_token("set-indicator");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int rc;
498
499 if (token == RTAS_UNKNOWN_SERVICE)
500 return -ENOENT;
501
John Rose507279d2006-06-05 16:31:48 -0500502 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
John Rose507279d2006-06-05 16:31:48 -0500504 } while (rtas_busy_delay(rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (rc < 0)
507 return rtas_error_rc(rc);
508 return rc;
509}
Michael Ellermanab3ab742006-06-23 18:20:11 +1000510EXPORT_SYMBOL(rtas_set_indicator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Paul Mackerras033ef332005-10-26 17:05:24 +1000512void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100514 if (rtas_flash_term_hook)
515 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 printk("RTAS system-reboot returned %d\n",
517 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
518 for (;;);
519}
520
Paul Mackerras033ef332005-10-26 17:05:24 +1000521void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100523 if (rtas_flash_term_hook)
524 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* allow power on only with power button press */
526 printk("RTAS power-off returned %d\n",
527 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
528 for (;;);
529}
530
Paul Mackerras033ef332005-10-26 17:05:24 +1000531void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100533 if (rtas_flash_term_hook)
534 rtas_flash_term_hook(SYS_HALT);
535 /* allow power on only with power button press */
536 printk("RTAS power-off returned %d\n",
537 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
538 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/* Must be in the RMO region, so we place it here */
542static char rtas_os_term_buf[2048];
543
544void rtas_os_term(char *str)
545{
546 int status;
547
548 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
549 return;
550
551 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
552
553 do {
554 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
555 __pa(rtas_os_term_buf));
John Rose507279d2006-06-05 16:31:48 -0500556 } while (rtas_busy_delay(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
John Rose507279d2006-06-05 16:31:48 -0500558 if (status != 0)
559 printk(KERN_EMERG "ibm,os-term call failed %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600563static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE;
564#ifdef CONFIG_PPC_PSERIES
565static void rtas_percpu_suspend_me(void *info)
566{
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600567 int i;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600568 long rc;
569 long flags;
570 struct rtas_suspend_me_data *data =
571 (struct rtas_suspend_me_data *)info;
572
573 /*
574 * We use "waiting" to indicate our state. As long
575 * as it is >0, we are still trying to all join up.
576 * If it goes to 0, we have successfully joined up and
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200577 * one thread got H_CONTINUE. If any error happens,
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600578 * we set it to <0.
579 */
580 local_irq_save(flags);
581 do {
582 rc = plpar_hcall_norets(H_JOIN);
583 smp_rmb();
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200584 } while (rc == H_SUCCESS && data->waiting > 0);
585 if (rc == H_SUCCESS)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600586 goto out;
587
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200588 if (rc == H_CONTINUE) {
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600589 data->waiting = 0;
Dave C Boutcherc4cb8ec2006-02-03 01:18:46 -0600590 data->args->args[data->args->nargs] =
591 rtas_call(ibm_suspend_me_token, 0, 1, NULL);
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800592 for_each_possible_cpu(i)
Dave C Boutcher82a4df72006-02-03 01:18:39 -0600593 plpar_hcall_norets(H_PROD,i);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600594 } else {
595 data->waiting = -EBUSY;
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200596 printk(KERN_ERR "Error on H_JOIN hypervisor call\n");
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600597 }
598
599out:
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600600 local_irq_restore(flags);
601 return;
602}
603
604static int rtas_ibm_suspend_me(struct rtas_args *args)
605{
606 int i;
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500607 long state;
608 long rc;
609 unsigned long dummy;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600610
611 struct rtas_suspend_me_data data;
612
Dave C Boutcher368a6ba2006-06-12 19:49:20 -0500613 /* Make sure the state is valid */
614 rc = plpar_hcall(H_VASI_STATE,
615 ((u64)args->args[0] << 32) | args->args[1],
616 0, 0, 0,
617 &state, &dummy, &dummy);
618
619 if (rc) {
620 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
621 return rc;
622 } else if (state == H_VASI_ENABLED) {
623 args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
624 return 0;
625 } else if (state != H_VASI_SUSPENDING) {
626 printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
627 state);
628 args->args[args->nargs] = -1;
629 return 0;
630 }
631
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600632 data.waiting = 1;
633 data.args = args;
634
635 /* Call function on all CPUs. One of us will make the
636 * rtas call
637 */
638 if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0))
639 data.waiting = -EINVAL;
640
641 if (data.waiting != 0)
642 printk(KERN_ERR "Error doing global join\n");
643
644 /* Prod each CPU. This won't hurt, and will wake
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200645 * anyone we successfully put to sleep with H_JOIN.
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600646 */
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800647 for_each_possible_cpu(i)
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600648 plpar_hcall_norets(H_PROD, i);
649
650 return data.waiting;
651}
652#else /* CONFIG_PPC_PSERIES */
653static int rtas_ibm_suspend_me(struct rtas_args *args)
654{
655 return -ENOSYS;
656}
657#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
660{
661 struct rtas_args args;
662 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000663 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int nargs;
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600665 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!capable(CAP_SYS_ADMIN))
668 return -EPERM;
669
670 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
671 return -EFAULT;
672
673 nargs = args.nargs;
674 if (nargs > ARRAY_SIZE(args.args)
675 || args.nret > ARRAY_SIZE(args.args)
676 || nargs + args.nret > ARRAY_SIZE(args.args))
677 return -EINVAL;
678
679 /* Copy in args. */
680 if (copy_from_user(args.args, uargs->args,
681 nargs * sizeof(rtas_arg_t)) != 0)
682 return -EFAULT;
683
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600684 if (args.token == RTAS_UNKNOWN_SERVICE)
685 return -EINVAL;
686
687 /* Need to handle ibm,suspend_me call specially */
688 if (args.token == ibm_suspend_me_token) {
689 rc = rtas_ibm_suspend_me(&args);
690 if (rc)
691 return rc;
692 goto copy_return;
693 }
694
Paul Mackerras033ef332005-10-26 17:05:24 +1000695 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 spin_lock_irqsave(&rtas.lock, flags);
698
699 rtas.args = args;
700 enter_rtas(__pa(&rtas.args));
701 args = rtas.args;
702
703 args.rets = &args.args[nargs];
704
705 /* A -1 return code indicates that the last command couldn't
706 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000707 if (args.rets[0] == -1)
708 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 spin_unlock_irqrestore(&rtas.lock, flags);
711
712 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000713 if (errbuf)
714 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 kfree(buff_copy);
716 }
717
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600718 copy_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* Copy out args. */
720 if (copy_to_user(uargs->args + nargs,
721 args.args + nargs,
722 args.nret * sizeof(rtas_arg_t)) != 0)
723 return -EFAULT;
724
725 return 0;
726}
727
728/* This version can't take the spinlock, because it never returns */
729
730struct rtas_args rtas_stop_self_args = {
731 /* The token is initialized for real in setup_system() */
732 .token = RTAS_UNKNOWN_SERVICE,
733 .nargs = 0,
734 .nret = 1,
735 .rets = &rtas_stop_self_args.args[0],
736};
737
738void rtas_stop_self(void)
739{
740 struct rtas_args *rtas_args = &rtas_stop_self_args;
741
742 local_irq_disable();
743
744 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
745
746 printk("cpu %u (hwid %u) Ready to die...\n",
747 smp_processor_id(), hard_smp_processor_id());
748 enter_rtas(__pa(rtas_args));
749
750 panic("Alas, I survived.\n");
751}
752
753/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100754 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * informations from the device-tree and allocate the RMO buffer for userland
756 * accesses.
757 */
758void __init rtas_initialize(void)
759{
Paul Mackerras033ef332005-10-26 17:05:24 +1000760 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* Get RTAS dev node and fill up our "rtas" structure with infos
763 * about it.
764 */
765 rtas.dev = of_find_node_by_name(NULL, "rtas");
766 if (rtas.dev) {
767 u32 *basep, *entryp;
768 u32 *sizep;
769
770 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
771 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
772 if (basep != NULL && sizep != NULL) {
773 rtas.base = *basep;
774 rtas.size = *sizep;
775 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
776 if (entryp == NULL) /* Ugh */
777 rtas.entry = rtas.base;
778 else
779 rtas.entry = *entryp;
780 } else
781 rtas.dev = NULL;
782 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000783 if (!rtas.dev)
784 return;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* If RTAS was found, allocate the RMO buffer for it and look for
787 * the stop-self token if any
788 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000789#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100790 if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000791 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
Dave C Boutcher91dc1822006-01-13 18:39:24 -0600792 ibm_suspend_me_token = rtas_token("ibm,suspend-me");
793 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000794#endif
795 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000798 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000800#ifdef CONFIG_RTAS_ERROR_LOGGING
801 rtas_last_error_token = rtas_token("rtas-last-error");
802#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
Michael Ellerman458148c2006-06-23 18:20:13 +1000804
805int __init early_init_dt_scan_rtas(unsigned long node,
806 const char *uname, int depth, void *data)
807{
808 u32 *basep, *entryp, *sizep;
809
810 if (depth != 1 || strcmp(uname, "rtas") != 0)
811 return 0;
812
813 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
814 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
815 sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
816
817 if (basep && entryp && sizep) {
818 rtas.base = *basep;
819 rtas.entry = *entryp;
820 rtas.size = *sizep;
821 }
822
823 /* break now */
824 return 1;
825}