blob: 68bcd2824bc6352ed8507b9dc4d66312d852fbe1 [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>
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +110020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/prom.h>
23#include <asm/rtas.h>
24#include <asm/semaphore.h>
25#include <asm/machdep.h>
26#include <asm/page.h>
27#include <asm/param.h>
28#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/delay.h>
30#include <asm/uaccess.h>
Paul Mackerras033ef332005-10-26 17:05:24 +100031#include <asm/lmb.h>
Michael Ellerman296167a2006-01-11 11:54:09 +110032#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Paul Mackerras033ef332005-10-26 17:05:24 +100034struct rtas_t rtas = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .lock = SPIN_LOCK_UNLOCKED
36};
37
38EXPORT_SYMBOL(rtas);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040DEFINE_SPINLOCK(rtas_data_buf_lock);
Paul Mackerras033ef332005-10-26 17:05:24 +100041char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042unsigned long rtas_rmo_buf;
43
Paul Mackerras033ef332005-10-26 17:05:24 +100044/*
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110045 * If non-NULL, this gets called when the kernel terminates.
46 * This is done like this so rtas_flash can be a module.
47 */
48void (*rtas_flash_term_hook)(int);
49EXPORT_SYMBOL(rtas_flash_term_hook);
50
51/*
Paul Mackerras033ef332005-10-26 17:05:24 +100052 * call_rtas_display_status and call_rtas_display_status_delay
53 * are designed only for very early low-level debugging, which
54 * is why the token is hard-coded to 10.
55 */
Michael Ellerman296167a2006-01-11 11:54:09 +110056static void call_rtas_display_status(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct rtas_args *args = &rtas.args;
59 unsigned long s;
60
61 if (!rtas.base)
62 return;
63 spin_lock_irqsave(&rtas.lock, s);
64
65 args->token = 10;
66 args->nargs = 1;
67 args->nret = 1;
68 args->rets = (rtas_arg_t *)&(args->args[1]);
Michael Ellerman296167a2006-01-11 11:54:09 +110069 args->args[0] = (unsigned char)c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 enter_rtas(__pa(args));
72
73 spin_unlock_irqrestore(&rtas.lock, s);
74}
75
Michael Ellerman296167a2006-01-11 11:54:09 +110076static void call_rtas_display_status_delay(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 static int pending_newline = 0; /* did last write end with unprinted newline? */
79 static int width = 16;
80
81 if (c == '\n') {
82 while (width-- > 0)
83 call_rtas_display_status(' ');
84 width = 16;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +110085 mdelay(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 pending_newline = 1;
87 } else {
88 if (pending_newline) {
89 call_rtas_display_status('\r');
90 call_rtas_display_status('\n');
91 }
92 pending_newline = 0;
93 if (width--) {
94 call_rtas_display_status(c);
95 udelay(10000);
96 }
97 }
98}
99
Michael Ellerman296167a2006-01-11 11:54:09 +1100100void __init udbg_init_rtas(void)
101{
102 udbg_putc = call_rtas_display_status_delay;
103}
104
Paul Mackerras033ef332005-10-26 17:05:24 +1000105void rtas_progress(char *s, unsigned short hex)
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000106{
107 struct device_node *root;
108 int width, *p;
109 char *os;
110 static int display_character, set_indicator;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000111 static int display_width, display_lines, *row_width, form_feed;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000112 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000113 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000114 static int pending_newline = 0; /* did last write end with unprinted newline? */
115
116 if (!rtas.base)
117 return;
118
Mike Strosaker8f586b22005-06-23 16:09:41 +1000119 if (display_width == 0) {
120 display_width = 0x10;
121 if ((root = find_path_device("/rtas"))) {
122 if ((p = (unsigned int *)get_property(root,
123 "ibm,display-line-length", NULL)))
124 display_width = *p;
125 if ((p = (unsigned int *)get_property(root,
126 "ibm,form-feed", NULL)))
127 form_feed = *p;
128 if ((p = (unsigned int *)get_property(root,
129 "ibm,display-number-of-lines", NULL)))
130 display_lines = *p;
131 row_width = (unsigned int *)get_property(root,
132 "ibm,display-truncation-length", NULL);
133 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000134 display_character = rtas_token("display-character");
135 set_indicator = rtas_token("set-indicator");
136 }
137
138 if (display_character == RTAS_UNKNOWN_SERVICE) {
139 /* use hex display if available */
140 if (set_indicator != RTAS_UNKNOWN_SERVICE)
141 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
142 return;
143 }
144
145 spin_lock(&progress_lock);
146
147 /*
148 * Last write ended with newline, but we didn't print it since
149 * it would just clear the bottom line of output. Print it now
150 * instead.
151 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000152 * If no newline is pending and form feed is supported, clear the
153 * display with a form feed; otherwise, print a CR to start output
154 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000155 */
156 if (pending_newline) {
157 rtas_call(display_character, 1, 1, NULL, '\r');
158 rtas_call(display_character, 1, 1, NULL, '\n');
159 pending_newline = 0;
160 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000161 current_line = 0;
162 if (form_feed)
163 rtas_call(display_character, 1, 1, NULL,
164 (char)form_feed);
165 else
166 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000167 }
168
Mike Strosaker8f586b22005-06-23 16:09:41 +1000169 if (row_width)
170 width = row_width[current_line];
171 else
172 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000173 os = s;
174 while (*os) {
175 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000176 /* If newline is the last character, save it
177 * until next call to avoid bumping up the
178 * display output.
179 */
180 if (*os == '\n' && !os[1]) {
181 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000182 current_line++;
183 if (current_line > display_lines-1)
184 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000185 spin_unlock(&progress_lock);
186 return;
187 }
188
189 /* RTAS wants CR-LF, not just LF */
190
191 if (*os == '\n') {
192 rtas_call(display_character, 1, 1, NULL, '\r');
193 rtas_call(display_character, 1, 1, NULL, '\n');
194 } else {
195 /* CR might be used to re-draw a line, so we'll
196 * leave it alone and not add LF.
197 */
198 rtas_call(display_character, 1, 1, NULL, *os);
199 }
200
Mike Strosaker8f586b22005-06-23 16:09:41 +1000201 if (row_width)
202 width = row_width[current_line];
203 else
204 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000205 } else {
206 width--;
207 rtas_call(display_character, 1, 1, NULL, *os);
208 }
209
210 os++;
211
212 /* if we overwrite the screen length */
213 if (width <= 0)
214 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
215 os++;
216 }
217
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000218 spin_unlock(&progress_lock);
219}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100220EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000221
Paul Mackerras033ef332005-10-26 17:05:24 +1000222int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000225 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return RTAS_UNKNOWN_SERVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 tokp = (int *) get_property(rtas.dev, service, NULL);
228 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
229}
230
Paul Mackerras033ef332005-10-26 17:05:24 +1000231#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/*
233 * Return the firmware-specified size of the error log buffer
234 * for all rtas calls that require an error buffer argument.
235 * This includes 'check-exception' and 'rtas-last-error'.
236 */
237int rtas_get_error_log_max(void)
238{
239 static int rtas_error_log_max;
240 if (rtas_error_log_max)
241 return rtas_error_log_max;
242
243 rtas_error_log_max = rtas_token ("rtas-error-log-max");
244 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
245 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000246 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
247 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
249 }
250 return rtas_error_log_max;
251}
Paul Mackerras033ef332005-10-26 17:05:24 +1000252EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254
Paul Mackerras033ef332005-10-26 17:05:24 +1000255char rtas_err_buf[RTAS_ERROR_LOG_MAX];
256int rtas_last_error_token;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/** Return a copy of the detailed error text associated with the
259 * most recent failed call to rtas. Because the error text
260 * might go stale if there are any other intervening rtas calls,
261 * this routine must be called atomically with whatever produced
262 * the error (i.e. with rtas.lock still held from the previous call).
263 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000264static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct rtas_args err_args, save_args;
267 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000268 char *buf = NULL;
269
270 if (rtas_last_error_token == -1)
271 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 bufsz = rtas_get_error_log_max();
274
Paul Mackerras033ef332005-10-26 17:05:24 +1000275 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 err_args.nargs = 2;
277 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
279 err_args.args[1] = bufsz;
280 err_args.args[2] = 0;
281
282 save_args = rtas.args;
283 rtas.args = err_args;
284
285 enter_rtas(__pa(&rtas.args));
286
287 err_args = rtas.args;
288 rtas.args = save_args;
289
Paul Mackerras033ef332005-10-26 17:05:24 +1000290 /* Log the error in the unlikely case that there was one. */
291 if (unlikely(err_args.args[2] == 0)) {
292 if (altbuf) {
293 buf = altbuf;
294 } else {
295 buf = rtas_err_buf;
296 if (mem_init_done)
297 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
298 }
299 if (buf)
300 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
301 }
302
303 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Paul Mackerras033ef332005-10-26 17:05:24 +1000306#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
307
308#else /* CONFIG_RTAS_ERROR_LOGGING */
309#define __fetch_rtas_last_error(x) NULL
310#define get_errorlog_buffer() NULL
311#endif
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313int rtas_call(int token, int nargs, int nret, int *outputs, ...)
314{
315 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000316 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long s;
318 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000319 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int ret;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (token == RTAS_UNKNOWN_SERVICE)
323 return -1;
324
325 /* Gotta do something different here, use global lock for now... */
326 spin_lock_irqsave(&rtas.lock, s);
327 rtas_args = &rtas.args;
328
329 rtas_args->token = token;
330 rtas_args->nargs = nargs;
331 rtas_args->nret = nret;
332 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
333 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000334 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 va_end(list);
337
338 for (i = 0; i < nret; ++i)
339 rtas_args->rets[i] = 0;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* A -1 return code indicates that the last command couldn't
344 be completed due to a hardware error. */
345 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000346 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (nret > 1 && outputs != NULL)
349 for (i = 0; i < nret-1; ++i)
350 outputs[i] = rtas_args->rets[i+1];
351 ret = (nret > 0)? rtas_args->rets[0]: 0;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Gotta do something different here, use global lock for now... */
354 spin_unlock_irqrestore(&rtas.lock, s);
355
356 if (buff_copy) {
357 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
358 if (mem_init_done)
359 kfree(buff_copy);
360 }
361 return ret;
362}
363
364/* Given an RTAS status code of 990n compute the hinted delay of 10^n
365 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
366 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000367unsigned int rtas_extended_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int order = status - 9900;
370 unsigned long ms;
371
372 if (order < 0)
373 order = 0; /* RTC depends on this for -2 clock busy */
374 else if (order > 5)
375 order = 5; /* bound */
376
377 /* Use microseconds for reasonable accuracy */
Paul Mackerras033ef332005-10-26 17:05:24 +1000378 for (ms = 1; order > 0; order--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 ms *= 10;
380
381 return ms;
382}
383
384int rtas_error_rc(int rtas_rc)
385{
386 int rc;
387
388 switch (rtas_rc) {
389 case -1: /* Hardware Error */
390 rc = -EIO;
391 break;
392 case -3: /* Bad indicator/domain/etc */
393 rc = -EINVAL;
394 break;
395 case -9000: /* Isolation error */
396 rc = -EFAULT;
397 break;
398 case -9001: /* Outstanding TCE/PTE */
399 rc = -EEXIST;
400 break;
401 case -9002: /* No usable slot */
402 rc = -ENODEV;
403 break;
404 default:
405 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
406 __FUNCTION__, rtas_rc);
407 rc = -ERANGE;
408 break;
409 }
410 return rc;
411}
412
413int rtas_get_power_level(int powerdomain, int *level)
414{
415 int token = rtas_token("get-power-level");
416 int rc;
417
418 if (token == RTAS_UNKNOWN_SERVICE)
419 return -ENOENT;
420
421 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
422 udelay(1);
423
424 if (rc < 0)
425 return rtas_error_rc(rc);
426 return rc;
427}
428
429int rtas_set_power_level(int powerdomain, int level, int *setlevel)
430{
431 int token = rtas_token("set-power-level");
432 unsigned int wait_time;
433 int rc;
434
435 if (token == RTAS_UNKNOWN_SERVICE)
436 return -ENOENT;
437
438 while (1) {
439 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
440 if (rc == RTAS_BUSY)
441 udelay(1);
442 else if (rtas_is_extended_busy(rc)) {
443 wait_time = rtas_extended_busy_delay_time(rc);
444 udelay(wait_time * 1000);
445 } else
446 break;
447 }
448
449 if (rc < 0)
450 return rtas_error_rc(rc);
451 return rc;
452}
453
454int rtas_get_sensor(int sensor, int index, int *state)
455{
456 int token = rtas_token("get-sensor-state");
457 unsigned int wait_time;
458 int rc;
459
460 if (token == RTAS_UNKNOWN_SERVICE)
461 return -ENOENT;
462
463 while (1) {
464 rc = rtas_call(token, 2, 2, state, sensor, index);
465 if (rc == RTAS_BUSY)
466 udelay(1);
467 else if (rtas_is_extended_busy(rc)) {
468 wait_time = rtas_extended_busy_delay_time(rc);
469 udelay(wait_time * 1000);
470 } else
471 break;
472 }
473
474 if (rc < 0)
475 return rtas_error_rc(rc);
476 return rc;
477}
478
479int rtas_set_indicator(int indicator, int index, int new_value)
480{
481 int token = rtas_token("set-indicator");
482 unsigned int wait_time;
483 int rc;
484
485 if (token == RTAS_UNKNOWN_SERVICE)
486 return -ENOENT;
487
488 while (1) {
489 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
490 if (rc == RTAS_BUSY)
491 udelay(1);
492 else if (rtas_is_extended_busy(rc)) {
493 wait_time = rtas_extended_busy_delay_time(rc);
494 udelay(wait_time * 1000);
495 }
496 else
497 break;
498 }
499
500 if (rc < 0)
501 return rtas_error_rc(rc);
502 return rc;
503}
504
Paul Mackerras033ef332005-10-26 17:05:24 +1000505void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100507 if (rtas_flash_term_hook)
508 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 printk("RTAS system-reboot returned %d\n",
510 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
511 for (;;);
512}
513
Paul Mackerras033ef332005-10-26 17:05:24 +1000514void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100516 if (rtas_flash_term_hook)
517 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* allow power on only with power button press */
519 printk("RTAS power-off returned %d\n",
520 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
521 for (;;);
522}
523
Paul Mackerras033ef332005-10-26 17:05:24 +1000524void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100526 if (rtas_flash_term_hook)
527 rtas_flash_term_hook(SYS_HALT);
528 /* allow power on only with power button press */
529 printk("RTAS power-off returned %d\n",
530 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
531 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/* Must be in the RMO region, so we place it here */
535static char rtas_os_term_buf[2048];
536
537void rtas_os_term(char *str)
538{
539 int status;
540
541 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
542 return;
543
544 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
545
546 do {
547 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
548 __pa(rtas_os_term_buf));
549
550 if (status == RTAS_BUSY)
551 udelay(1);
552 else if (status != 0)
553 printk(KERN_EMERG "ibm,os-term call failed %d\n",
554 status);
555 } while (status == RTAS_BUSY);
556}
557
558
559asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
560{
561 struct rtas_args args;
562 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000563 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 if (!capable(CAP_SYS_ADMIN))
567 return -EPERM;
568
569 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
570 return -EFAULT;
571
572 nargs = args.nargs;
573 if (nargs > ARRAY_SIZE(args.args)
574 || args.nret > ARRAY_SIZE(args.args)
575 || nargs + args.nret > ARRAY_SIZE(args.args))
576 return -EINVAL;
577
578 /* Copy in args. */
579 if (copy_from_user(args.args, uargs->args,
580 nargs * sizeof(rtas_arg_t)) != 0)
581 return -EFAULT;
582
Paul Mackerras033ef332005-10-26 17:05:24 +1000583 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 spin_lock_irqsave(&rtas.lock, flags);
586
587 rtas.args = args;
588 enter_rtas(__pa(&rtas.args));
589 args = rtas.args;
590
591 args.rets = &args.args[nargs];
592
593 /* A -1 return code indicates that the last command couldn't
594 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000595 if (args.rets[0] == -1)
596 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 spin_unlock_irqrestore(&rtas.lock, flags);
599
600 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000601 if (errbuf)
602 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 kfree(buff_copy);
604 }
605
606 /* Copy out args. */
607 if (copy_to_user(uargs->args + nargs,
608 args.args + nargs,
609 args.nret * sizeof(rtas_arg_t)) != 0)
610 return -EFAULT;
611
612 return 0;
613}
614
615/* This version can't take the spinlock, because it never returns */
616
617struct rtas_args rtas_stop_self_args = {
618 /* The token is initialized for real in setup_system() */
619 .token = RTAS_UNKNOWN_SERVICE,
620 .nargs = 0,
621 .nret = 1,
622 .rets = &rtas_stop_self_args.args[0],
623};
624
625void rtas_stop_self(void)
626{
627 struct rtas_args *rtas_args = &rtas_stop_self_args;
628
629 local_irq_disable();
630
631 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
632
633 printk("cpu %u (hwid %u) Ready to die...\n",
634 smp_processor_id(), hard_smp_processor_id());
635 enter_rtas(__pa(rtas_args));
636
637 panic("Alas, I survived.\n");
638}
639
640/*
Adrian Bunk943ffb52006-01-10 00:10:13 +0100641 * Call early during boot, before mem init or bootmem, to retrieve the RTAS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * informations from the device-tree and allocate the RMO buffer for userland
643 * accesses.
644 */
645void __init rtas_initialize(void)
646{
Paul Mackerras033ef332005-10-26 17:05:24 +1000647 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* Get RTAS dev node and fill up our "rtas" structure with infos
650 * about it.
651 */
652 rtas.dev = of_find_node_by_name(NULL, "rtas");
653 if (rtas.dev) {
654 u32 *basep, *entryp;
655 u32 *sizep;
656
657 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
658 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
659 if (basep != NULL && sizep != NULL) {
660 rtas.base = *basep;
661 rtas.size = *sizep;
662 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
663 if (entryp == NULL) /* Ugh */
664 rtas.entry = rtas.base;
665 else
666 rtas.entry = *entryp;
667 } else
668 rtas.dev = NULL;
669 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000670 if (!rtas.dev)
671 return;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* If RTAS was found, allocate the RMO buffer for it and look for
674 * the stop-self token if any
675 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000676#ifdef CONFIG_PPC64
Paul Mackerras799d6042005-11-10 13:37:51 +1100677 if (_machine == PLATFORM_PSERIES_LPAR)
Paul Mackerras033ef332005-10-26 17:05:24 +1000678 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
679#endif
680 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000683 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000685#ifdef CONFIG_RTAS_ERROR_LOGGING
686 rtas_last_error_token = rtas_token("rtas-last-error");
687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691EXPORT_SYMBOL(rtas_token);
692EXPORT_SYMBOL(rtas_call);
693EXPORT_SYMBOL(rtas_data_buf);
694EXPORT_SYMBOL(rtas_data_buf_lock);
695EXPORT_SYMBOL(rtas_extended_busy_delay_time);
696EXPORT_SYMBOL(rtas_get_sensor);
697EXPORT_SYMBOL(rtas_get_power_level);
698EXPORT_SYMBOL(rtas_set_power_level);
699EXPORT_SYMBOL(rtas_set_indicator);