blob: b7fc2d884950c2fa31e935ca7628ace355167610 [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>
20
21#include <asm/prom.h>
22#include <asm/rtas.h>
23#include <asm/semaphore.h>
24#include <asm/machdep.h>
25#include <asm/page.h>
26#include <asm/param.h>
27#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/delay.h>
29#include <asm/uaccess.h>
Paul Mackerras033ef332005-10-26 17:05:24 +100030#include <asm/lmb.h>
31#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/systemcfg.h>
Paul Mackerras033ef332005-10-26 17:05:24 +100033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Paul Mackerras033ef332005-10-26 17:05:24 +100035struct rtas_t rtas = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .lock = SPIN_LOCK_UNLOCKED
37};
38
39EXPORT_SYMBOL(rtas);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041DEFINE_SPINLOCK(rtas_data_buf_lock);
Paul Mackerras033ef332005-10-26 17:05:24 +100042char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043unsigned long rtas_rmo_buf;
44
Paul Mackerras033ef332005-10-26 17:05:24 +100045/*
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +110046 * If non-NULL, this gets called when the kernel terminates.
47 * This is done like this so rtas_flash can be a module.
48 */
49void (*rtas_flash_term_hook)(int);
50EXPORT_SYMBOL(rtas_flash_term_hook);
51
52/*
Paul Mackerras033ef332005-10-26 17:05:24 +100053 * call_rtas_display_status and call_rtas_display_status_delay
54 * are designed only for very early low-level debugging, which
55 * is why the token is hard-coded to 10.
56 */
57void call_rtas_display_status(unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct rtas_args *args = &rtas.args;
60 unsigned long s;
61
62 if (!rtas.base)
63 return;
64 spin_lock_irqsave(&rtas.lock, s);
65
66 args->token = 10;
67 args->nargs = 1;
68 args->nret = 1;
69 args->rets = (rtas_arg_t *)&(args->args[1]);
70 args->args[0] = (int)c;
71
72 enter_rtas(__pa(args));
73
74 spin_unlock_irqrestore(&rtas.lock, s);
75}
76
Paul Mackerras033ef332005-10-26 17:05:24 +100077void call_rtas_display_status_delay(unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 static int pending_newline = 0; /* did last write end with unprinted newline? */
80 static int width = 16;
81
82 if (c == '\n') {
83 while (width-- > 0)
84 call_rtas_display_status(' ');
85 width = 16;
86 udelay(500000);
87 pending_newline = 1;
88 } else {
89 if (pending_newline) {
90 call_rtas_display_status('\r');
91 call_rtas_display_status('\n');
92 }
93 pending_newline = 0;
94 if (width--) {
95 call_rtas_display_status(c);
96 udelay(10000);
97 }
98 }
99}
100
Paul Mackerras033ef332005-10-26 17:05:24 +1000101void rtas_progress(char *s, unsigned short hex)
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000102{
103 struct device_node *root;
104 int width, *p;
105 char *os;
106 static int display_character, set_indicator;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000107 static int display_width, display_lines, *row_width, form_feed;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000108 static DEFINE_SPINLOCK(progress_lock);
Mike Strosaker8f586b22005-06-23 16:09:41 +1000109 static int current_line;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000110 static int pending_newline = 0; /* did last write end with unprinted newline? */
111
112 if (!rtas.base)
113 return;
114
Mike Strosaker8f586b22005-06-23 16:09:41 +1000115 if (display_width == 0) {
116 display_width = 0x10;
117 if ((root = find_path_device("/rtas"))) {
118 if ((p = (unsigned int *)get_property(root,
119 "ibm,display-line-length", NULL)))
120 display_width = *p;
121 if ((p = (unsigned int *)get_property(root,
122 "ibm,form-feed", NULL)))
123 form_feed = *p;
124 if ((p = (unsigned int *)get_property(root,
125 "ibm,display-number-of-lines", NULL)))
126 display_lines = *p;
127 row_width = (unsigned int *)get_property(root,
128 "ibm,display-truncation-length", NULL);
129 }
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000130 display_character = rtas_token("display-character");
131 set_indicator = rtas_token("set-indicator");
132 }
133
134 if (display_character == RTAS_UNKNOWN_SERVICE) {
135 /* use hex display if available */
136 if (set_indicator != RTAS_UNKNOWN_SERVICE)
137 rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
138 return;
139 }
140
141 spin_lock(&progress_lock);
142
143 /*
144 * Last write ended with newline, but we didn't print it since
145 * it would just clear the bottom line of output. Print it now
146 * instead.
147 *
Mike Strosaker8f586b22005-06-23 16:09:41 +1000148 * If no newline is pending and form feed is supported, clear the
149 * display with a form feed; otherwise, print a CR to start output
150 * at the beginning of the line.
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000151 */
152 if (pending_newline) {
153 rtas_call(display_character, 1, 1, NULL, '\r');
154 rtas_call(display_character, 1, 1, NULL, '\n');
155 pending_newline = 0;
156 } else {
Mike Strosaker8f586b22005-06-23 16:09:41 +1000157 current_line = 0;
158 if (form_feed)
159 rtas_call(display_character, 1, 1, NULL,
160 (char)form_feed);
161 else
162 rtas_call(display_character, 1, 1, NULL, '\r');
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000163 }
164
Mike Strosaker8f586b22005-06-23 16:09:41 +1000165 if (row_width)
166 width = row_width[current_line];
167 else
168 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000169 os = s;
170 while (*os) {
171 if (*os == '\n' || *os == '\r') {
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000172 /* If newline is the last character, save it
173 * until next call to avoid bumping up the
174 * display output.
175 */
176 if (*os == '\n' && !os[1]) {
177 pending_newline = 1;
Mike Strosaker8f586b22005-06-23 16:09:41 +1000178 current_line++;
179 if (current_line > display_lines-1)
180 current_line = display_lines-1;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000181 spin_unlock(&progress_lock);
182 return;
183 }
184
185 /* RTAS wants CR-LF, not just LF */
186
187 if (*os == '\n') {
188 rtas_call(display_character, 1, 1, NULL, '\r');
189 rtas_call(display_character, 1, 1, NULL, '\n');
190 } else {
191 /* CR might be used to re-draw a line, so we'll
192 * leave it alone and not add LF.
193 */
194 rtas_call(display_character, 1, 1, NULL, *os);
195 }
196
Mike Strosaker8f586b22005-06-23 16:09:41 +1000197 if (row_width)
198 width = row_width[current_line];
199 else
200 width = display_width;
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000201 } else {
202 width--;
203 rtas_call(display_character, 1, 1, NULL, *os);
204 }
205
206 os++;
207
208 /* if we overwrite the screen length */
209 if (width <= 0)
210 while ((*os != 0) && (*os != '\n') && (*os != '\r'))
211 os++;
212 }
213
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000214 spin_unlock(&progress_lock);
215}
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100216EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */
Arnd Bergmann6566c6f2005-06-23 09:43:28 +1000217
Paul Mackerras033ef332005-10-26 17:05:24 +1000218int rtas_token(const char *service)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int *tokp;
Paul Mackerras033ef332005-10-26 17:05:24 +1000221 if (rtas.dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return RTAS_UNKNOWN_SERVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 tokp = (int *) get_property(rtas.dev, service, NULL);
224 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
225}
226
Paul Mackerras033ef332005-10-26 17:05:24 +1000227#ifdef CONFIG_RTAS_ERROR_LOGGING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * Return the firmware-specified size of the error log buffer
230 * for all rtas calls that require an error buffer argument.
231 * This includes 'check-exception' and 'rtas-last-error'.
232 */
233int rtas_get_error_log_max(void)
234{
235 static int rtas_error_log_max;
236 if (rtas_error_log_max)
237 return rtas_error_log_max;
238
239 rtas_error_log_max = rtas_token ("rtas-error-log-max");
240 if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
241 (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000242 printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
243 rtas_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
245 }
246 return rtas_error_log_max;
247}
Paul Mackerras033ef332005-10-26 17:05:24 +1000248EXPORT_SYMBOL(rtas_get_error_log_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250
Paul Mackerras033ef332005-10-26 17:05:24 +1000251char rtas_err_buf[RTAS_ERROR_LOG_MAX];
252int rtas_last_error_token;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/** Return a copy of the detailed error text associated with the
255 * most recent failed call to rtas. Because the error text
256 * might go stale if there are any other intervening rtas calls,
257 * this routine must be called atomically with whatever produced
258 * the error (i.e. with rtas.lock still held from the previous call).
259 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000260static char *__fetch_rtas_last_error(char *altbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct rtas_args err_args, save_args;
263 u32 bufsz;
Paul Mackerras033ef332005-10-26 17:05:24 +1000264 char *buf = NULL;
265
266 if (rtas_last_error_token == -1)
267 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 bufsz = rtas_get_error_log_max();
270
Paul Mackerras033ef332005-10-26 17:05:24 +1000271 err_args.token = rtas_last_error_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 err_args.nargs = 2;
273 err_args.nret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
275 err_args.args[1] = bufsz;
276 err_args.args[2] = 0;
277
278 save_args = rtas.args;
279 rtas.args = err_args;
280
281 enter_rtas(__pa(&rtas.args));
282
283 err_args = rtas.args;
284 rtas.args = save_args;
285
Paul Mackerras033ef332005-10-26 17:05:24 +1000286 /* Log the error in the unlikely case that there was one. */
287 if (unlikely(err_args.args[2] == 0)) {
288 if (altbuf) {
289 buf = altbuf;
290 } else {
291 buf = rtas_err_buf;
292 if (mem_init_done)
293 buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
294 }
295 if (buf)
296 memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
297 }
298
299 return buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Paul Mackerras033ef332005-10-26 17:05:24 +1000302#define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
303
304#else /* CONFIG_RTAS_ERROR_LOGGING */
305#define __fetch_rtas_last_error(x) NULL
306#define get_errorlog_buffer() NULL
307#endif
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309int rtas_call(int token, int nargs, int nret, int *outputs, ...)
310{
311 va_list list;
Paul Mackerras033ef332005-10-26 17:05:24 +1000312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned long s;
314 struct rtas_args *rtas_args;
Paul Mackerras033ef332005-10-26 17:05:24 +1000315 char *buff_copy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int ret;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (token == RTAS_UNKNOWN_SERVICE)
319 return -1;
320
321 /* Gotta do something different here, use global lock for now... */
322 spin_lock_irqsave(&rtas.lock, s);
323 rtas_args = &rtas.args;
324
325 rtas_args->token = token;
326 rtas_args->nargs = nargs;
327 rtas_args->nret = nret;
328 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
329 va_start(list, outputs);
Paul Mackerras033ef332005-10-26 17:05:24 +1000330 for (i = 0; i < nargs; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 rtas_args->args[i] = va_arg(list, rtas_arg_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 va_end(list);
333
334 for (i = 0; i < nret; ++i)
335 rtas_args->rets[i] = 0;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 enter_rtas(__pa(rtas_args));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* A -1 return code indicates that the last command couldn't
340 be completed due to a hardware error. */
341 if (rtas_args->rets[0] == -1)
Paul Mackerras033ef332005-10-26 17:05:24 +1000342 buff_copy = __fetch_rtas_last_error(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (nret > 1 && outputs != NULL)
345 for (i = 0; i < nret-1; ++i)
346 outputs[i] = rtas_args->rets[i+1];
347 ret = (nret > 0)? rtas_args->rets[0]: 0;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Gotta do something different here, use global lock for now... */
350 spin_unlock_irqrestore(&rtas.lock, s);
351
352 if (buff_copy) {
353 log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
354 if (mem_init_done)
355 kfree(buff_copy);
356 }
357 return ret;
358}
359
360/* Given an RTAS status code of 990n compute the hinted delay of 10^n
361 * (last digit) milliseconds. For now we bound at n=5 (100 sec).
362 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000363unsigned int rtas_extended_busy_delay_time(int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 int order = status - 9900;
366 unsigned long ms;
367
368 if (order < 0)
369 order = 0; /* RTC depends on this for -2 clock busy */
370 else if (order > 5)
371 order = 5; /* bound */
372
373 /* Use microseconds for reasonable accuracy */
Paul Mackerras033ef332005-10-26 17:05:24 +1000374 for (ms = 1; order > 0; order--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ms *= 10;
376
377 return ms;
378}
379
380int rtas_error_rc(int rtas_rc)
381{
382 int rc;
383
384 switch (rtas_rc) {
385 case -1: /* Hardware Error */
386 rc = -EIO;
387 break;
388 case -3: /* Bad indicator/domain/etc */
389 rc = -EINVAL;
390 break;
391 case -9000: /* Isolation error */
392 rc = -EFAULT;
393 break;
394 case -9001: /* Outstanding TCE/PTE */
395 rc = -EEXIST;
396 break;
397 case -9002: /* No usable slot */
398 rc = -ENODEV;
399 break;
400 default:
401 printk(KERN_ERR "%s: unexpected RTAS error %d\n",
402 __FUNCTION__, rtas_rc);
403 rc = -ERANGE;
404 break;
405 }
406 return rc;
407}
408
409int rtas_get_power_level(int powerdomain, int *level)
410{
411 int token = rtas_token("get-power-level");
412 int rc;
413
414 if (token == RTAS_UNKNOWN_SERVICE)
415 return -ENOENT;
416
417 while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
418 udelay(1);
419
420 if (rc < 0)
421 return rtas_error_rc(rc);
422 return rc;
423}
424
425int rtas_set_power_level(int powerdomain, int level, int *setlevel)
426{
427 int token = rtas_token("set-power-level");
428 unsigned int wait_time;
429 int rc;
430
431 if (token == RTAS_UNKNOWN_SERVICE)
432 return -ENOENT;
433
434 while (1) {
435 rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
436 if (rc == RTAS_BUSY)
437 udelay(1);
438 else if (rtas_is_extended_busy(rc)) {
439 wait_time = rtas_extended_busy_delay_time(rc);
440 udelay(wait_time * 1000);
441 } else
442 break;
443 }
444
445 if (rc < 0)
446 return rtas_error_rc(rc);
447 return rc;
448}
449
450int rtas_get_sensor(int sensor, int index, int *state)
451{
452 int token = rtas_token("get-sensor-state");
453 unsigned int wait_time;
454 int rc;
455
456 if (token == RTAS_UNKNOWN_SERVICE)
457 return -ENOENT;
458
459 while (1) {
460 rc = rtas_call(token, 2, 2, state, sensor, index);
461 if (rc == RTAS_BUSY)
462 udelay(1);
463 else if (rtas_is_extended_busy(rc)) {
464 wait_time = rtas_extended_busy_delay_time(rc);
465 udelay(wait_time * 1000);
466 } else
467 break;
468 }
469
470 if (rc < 0)
471 return rtas_error_rc(rc);
472 return rc;
473}
474
475int rtas_set_indicator(int indicator, int index, int new_value)
476{
477 int token = rtas_token("set-indicator");
478 unsigned int wait_time;
479 int rc;
480
481 if (token == RTAS_UNKNOWN_SERVICE)
482 return -ENOENT;
483
484 while (1) {
485 rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
486 if (rc == RTAS_BUSY)
487 udelay(1);
488 else if (rtas_is_extended_busy(rc)) {
489 wait_time = rtas_extended_busy_delay_time(rc);
490 udelay(wait_time * 1000);
491 }
492 else
493 break;
494 }
495
496 if (rc < 0)
497 return rtas_error_rc(rc);
498 return rc;
499}
500
Paul Mackerras033ef332005-10-26 17:05:24 +1000501void rtas_restart(char *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100503 if (rtas_flash_term_hook)
504 rtas_flash_term_hook(SYS_RESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk("RTAS system-reboot returned %d\n",
506 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
507 for (;;);
508}
509
Paul Mackerras033ef332005-10-26 17:05:24 +1000510void rtas_power_off(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100512 if (rtas_flash_term_hook)
513 rtas_flash_term_hook(SYS_POWER_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* allow power on only with power button press */
515 printk("RTAS power-off returned %d\n",
516 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
517 for (;;);
518}
519
Paul Mackerras033ef332005-10-26 17:05:24 +1000520void rtas_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +1100522 if (rtas_flash_term_hook)
523 rtas_flash_term_hook(SYS_HALT);
524 /* allow power on only with power button press */
525 printk("RTAS power-off returned %d\n",
526 rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
527 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/* Must be in the RMO region, so we place it here */
531static char rtas_os_term_buf[2048];
532
533void rtas_os_term(char *str)
534{
535 int status;
536
537 if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
538 return;
539
540 snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
541
542 do {
543 status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
544 __pa(rtas_os_term_buf));
545
546 if (status == RTAS_BUSY)
547 udelay(1);
548 else if (status != 0)
549 printk(KERN_EMERG "ibm,os-term call failed %d\n",
550 status);
551 } while (status == RTAS_BUSY);
552}
553
554
555asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
556{
557 struct rtas_args args;
558 unsigned long flags;
Paul Mackerras033ef332005-10-26 17:05:24 +1000559 char *buff_copy, *errbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (!capable(CAP_SYS_ADMIN))
563 return -EPERM;
564
565 if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
566 return -EFAULT;
567
568 nargs = args.nargs;
569 if (nargs > ARRAY_SIZE(args.args)
570 || args.nret > ARRAY_SIZE(args.args)
571 || nargs + args.nret > ARRAY_SIZE(args.args))
572 return -EINVAL;
573
574 /* Copy in args. */
575 if (copy_from_user(args.args, uargs->args,
576 nargs * sizeof(rtas_arg_t)) != 0)
577 return -EFAULT;
578
Paul Mackerras033ef332005-10-26 17:05:24 +1000579 buff_copy = get_errorlog_buffer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 spin_lock_irqsave(&rtas.lock, flags);
582
583 rtas.args = args;
584 enter_rtas(__pa(&rtas.args));
585 args = rtas.args;
586
587 args.rets = &args.args[nargs];
588
589 /* A -1 return code indicates that the last command couldn't
590 be completed due to a hardware error. */
Paul Mackerras033ef332005-10-26 17:05:24 +1000591 if (args.rets[0] == -1)
592 errbuf = __fetch_rtas_last_error(buff_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 spin_unlock_irqrestore(&rtas.lock, flags);
595
596 if (buff_copy) {
Paul Mackerras033ef332005-10-26 17:05:24 +1000597 if (errbuf)
598 log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree(buff_copy);
600 }
601
602 /* Copy out args. */
603 if (copy_to_user(uargs->args + nargs,
604 args.args + nargs,
605 args.nret * sizeof(rtas_arg_t)) != 0)
606 return -EFAULT;
607
608 return 0;
609}
610
Paul Mackerras033ef332005-10-26 17:05:24 +1000611#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/* This version can't take the spinlock, because it never returns */
613
614struct rtas_args rtas_stop_self_args = {
615 /* The token is initialized for real in setup_system() */
616 .token = RTAS_UNKNOWN_SERVICE,
617 .nargs = 0,
618 .nret = 1,
619 .rets = &rtas_stop_self_args.args[0],
620};
621
622void rtas_stop_self(void)
623{
624 struct rtas_args *rtas_args = &rtas_stop_self_args;
625
626 local_irq_disable();
627
628 BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
629
630 printk("cpu %u (hwid %u) Ready to die...\n",
631 smp_processor_id(), hard_smp_processor_id());
632 enter_rtas(__pa(rtas_args));
633
634 panic("Alas, I survived.\n");
635}
Paul Mackerras033ef332005-10-26 17:05:24 +1000636#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
639 * Call early during boot, before mem init or bootmem, to retreive the RTAS
640 * informations from the device-tree and allocate the RMO buffer for userland
641 * accesses.
642 */
643void __init rtas_initialize(void)
644{
Paul Mackerras033ef332005-10-26 17:05:24 +1000645 unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* Get RTAS dev node and fill up our "rtas" structure with infos
648 * about it.
649 */
650 rtas.dev = of_find_node_by_name(NULL, "rtas");
651 if (rtas.dev) {
652 u32 *basep, *entryp;
653 u32 *sizep;
654
655 basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
656 sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
657 if (basep != NULL && sizep != NULL) {
658 rtas.base = *basep;
659 rtas.size = *sizep;
660 entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
661 if (entryp == NULL) /* Ugh */
662 rtas.entry = rtas.base;
663 else
664 rtas.entry = *entryp;
665 } else
666 rtas.dev = NULL;
667 }
Paul Mackerras033ef332005-10-26 17:05:24 +1000668 if (!rtas.dev)
669 return;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* If RTAS was found, allocate the RMO buffer for it and look for
672 * the stop-self token if any
673 */
Paul Mackerras033ef332005-10-26 17:05:24 +1000674#ifdef CONFIG_PPC64
675 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
676 rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
677#endif
678 rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680#ifdef CONFIG_HOTPLUG_CPU
Paul Mackerras033ef332005-10-26 17:05:24 +1000681 rtas_stop_self_args.token = rtas_token("stop-self");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#endif /* CONFIG_HOTPLUG_CPU */
Paul Mackerras033ef332005-10-26 17:05:24 +1000683#ifdef CONFIG_RTAS_ERROR_LOGGING
684 rtas_last_error_token = rtas_token("rtas-last-error");
685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689EXPORT_SYMBOL(rtas_token);
690EXPORT_SYMBOL(rtas_call);
691EXPORT_SYMBOL(rtas_data_buf);
692EXPORT_SYMBOL(rtas_data_buf_lock);
693EXPORT_SYMBOL(rtas_extended_busy_delay_time);
694EXPORT_SYMBOL(rtas_get_sensor);
695EXPORT_SYMBOL(rtas_get_power_level);
696EXPORT_SYMBOL(rtas_set_power_level);
697EXPORT_SYMBOL(rtas_set_indicator);