]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
21fe3301 | 20 | #include <linux/delay.h> |
1da177e4 LT |
21 | |
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> | |
1da177e4 LT |
29 | #include <asm/delay.h> |
30 | #include <asm/uaccess.h> | |
033ef338 | 31 | #include <asm/lmb.h> |
296167ae | 32 | #include <asm/udbg.h> |
1da177e4 | 33 | |
033ef338 | 34 | struct rtas_t rtas = { |
1da177e4 LT |
35 | .lock = SPIN_LOCK_UNLOCKED |
36 | }; | |
37 | ||
38 | EXPORT_SYMBOL(rtas); | |
39 | ||
1da177e4 | 40 | DEFINE_SPINLOCK(rtas_data_buf_lock); |
033ef338 | 41 | char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned; |
1da177e4 LT |
42 | unsigned long rtas_rmo_buf; |
43 | ||
f4fcbbe9 PM |
44 | /* |
45 | * If non-NULL, this gets called when the kernel terminates. | |
46 | * This is done like this so rtas_flash can be a module. | |
47 | */ | |
48 | void (*rtas_flash_term_hook)(int); | |
49 | EXPORT_SYMBOL(rtas_flash_term_hook); | |
50 | ||
033ef338 PM |
51 | /* |
52 | * 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 | */ | |
296167ae | 56 | static void call_rtas_display_status(char c) |
1da177e4 LT |
57 | { |
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]); | |
296167ae | 69 | args->args[0] = (unsigned char)c; |
1da177e4 LT |
70 | |
71 | enter_rtas(__pa(args)); | |
72 | ||
73 | spin_unlock_irqrestore(&rtas.lock, s); | |
74 | } | |
75 | ||
296167ae | 76 | static void call_rtas_display_status_delay(char c) |
1da177e4 LT |
77 | { |
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; | |
21fe3301 | 85 | mdelay(500); |
1da177e4 LT |
86 | 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 | ||
296167ae ME |
100 | void __init udbg_init_rtas(void) |
101 | { | |
102 | udbg_putc = call_rtas_display_status_delay; | |
103 | } | |
104 | ||
033ef338 | 105 | void rtas_progress(char *s, unsigned short hex) |
6566c6f1 AB |
106 | { |
107 | struct device_node *root; | |
108 | int width, *p; | |
109 | char *os; | |
110 | static int display_character, set_indicator; | |
8f586b22 | 111 | static int display_width, display_lines, *row_width, form_feed; |
6566c6f1 | 112 | static DEFINE_SPINLOCK(progress_lock); |
8f586b22 | 113 | static int current_line; |
6566c6f1 AB |
114 | static int pending_newline = 0; /* did last write end with unprinted newline? */ |
115 | ||
116 | if (!rtas.base) | |
117 | return; | |
118 | ||
8f586b22 MS |
119 | 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 | } | |
6566c6f1 AB |
134 | 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 | * | |
8f586b22 MS |
152 | * 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. | |
6566c6f1 AB |
155 | */ |
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 { | |
8f586b22 MS |
161 | 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'); | |
6566c6f1 AB |
167 | } |
168 | ||
8f586b22 MS |
169 | if (row_width) |
170 | width = row_width[current_line]; | |
171 | else | |
172 | width = display_width; | |
6566c6f1 AB |
173 | os = s; |
174 | while (*os) { | |
175 | if (*os == '\n' || *os == '\r') { | |
6566c6f1 AB |
176 | /* 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; | |
8f586b22 MS |
182 | current_line++; |
183 | if (current_line > display_lines-1) | |
184 | current_line = display_lines-1; | |
6566c6f1 AB |
185 | 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 | ||
8f586b22 MS |
201 | if (row_width) |
202 | width = row_width[current_line]; | |
203 | else | |
204 | width = display_width; | |
6566c6f1 AB |
205 | } 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 | ||
6566c6f1 AB |
218 | spin_unlock(&progress_lock); |
219 | } | |
f4fcbbe9 | 220 | EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */ |
6566c6f1 | 221 | |
033ef338 | 222 | int rtas_token(const char *service) |
1da177e4 LT |
223 | { |
224 | int *tokp; | |
033ef338 | 225 | if (rtas.dev == NULL) |
1da177e4 | 226 | return RTAS_UNKNOWN_SERVICE; |
1da177e4 LT |
227 | tokp = (int *) get_property(rtas.dev, service, NULL); |
228 | return tokp ? *tokp : RTAS_UNKNOWN_SERVICE; | |
229 | } | |
230 | ||
033ef338 | 231 | #ifdef CONFIG_RTAS_ERROR_LOGGING |
1da177e4 LT |
232 | /* |
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 | */ | |
237 | int 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)) { | |
033ef338 PM |
246 | printk (KERN_WARNING "RTAS: bad log buffer size %d\n", |
247 | rtas_error_log_max); | |
1da177e4 LT |
248 | rtas_error_log_max = RTAS_ERROR_LOG_MAX; |
249 | } | |
250 | return rtas_error_log_max; | |
251 | } | |
033ef338 | 252 | EXPORT_SYMBOL(rtas_get_error_log_max); |
1da177e4 LT |
253 | |
254 | ||
033ef338 PM |
255 | char rtas_err_buf[RTAS_ERROR_LOG_MAX]; |
256 | int rtas_last_error_token; | |
257 | ||
1da177e4 LT |
258 | /** 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 | */ | |
033ef338 | 264 | static char *__fetch_rtas_last_error(char *altbuf) |
1da177e4 LT |
265 | { |
266 | struct rtas_args err_args, save_args; | |
267 | u32 bufsz; | |
033ef338 PM |
268 | char *buf = NULL; |
269 | ||
270 | if (rtas_last_error_token == -1) | |
271 | return NULL; | |
1da177e4 LT |
272 | |
273 | bufsz = rtas_get_error_log_max(); | |
274 | ||
033ef338 | 275 | err_args.token = rtas_last_error_token; |
1da177e4 LT |
276 | err_args.nargs = 2; |
277 | err_args.nret = 1; | |
1da177e4 LT |
278 | 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 | ||
033ef338 PM |
290 | /* 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; | |
1da177e4 LT |
304 | } |
305 | ||
033ef338 PM |
306 | #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 | ||
1da177e4 LT |
313 | int rtas_call(int token, int nargs, int nret, int *outputs, ...) |
314 | { | |
315 | va_list list; | |
033ef338 | 316 | int i; |
1da177e4 LT |
317 | unsigned long s; |
318 | struct rtas_args *rtas_args; | |
033ef338 | 319 | char *buff_copy = NULL; |
1da177e4 LT |
320 | int ret; |
321 | ||
1da177e4 LT |
322 | 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); | |
033ef338 | 334 | for (i = 0; i < nargs; ++i) |
1da177e4 | 335 | rtas_args->args[i] = va_arg(list, rtas_arg_t); |
1da177e4 LT |
336 | va_end(list); |
337 | ||
338 | for (i = 0; i < nret; ++i) | |
339 | rtas_args->rets[i] = 0; | |
340 | ||
1da177e4 | 341 | enter_rtas(__pa(rtas_args)); |
1da177e4 LT |
342 | |
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) | |
033ef338 | 346 | buff_copy = __fetch_rtas_last_error(NULL); |
1da177e4 LT |
347 | |
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 | ||
1da177e4 LT |
353 | /* 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 | */ | |
033ef338 | 367 | unsigned int rtas_extended_busy_delay_time(int status) |
1da177e4 LT |
368 | { |
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 */ | |
033ef338 | 378 | for (ms = 1; order > 0; order--) |
1da177e4 LT |
379 | ms *= 10; |
380 | ||
381 | return ms; | |
382 | } | |
383 | ||
384 | int 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 | ||
413 | int 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 | ||
429 | int 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 | ||
454 | int 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 | ||
479 | int 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 | ||
033ef338 | 505 | void rtas_restart(char *cmd) |
1da177e4 | 506 | { |
f4fcbbe9 PM |
507 | if (rtas_flash_term_hook) |
508 | rtas_flash_term_hook(SYS_RESTART); | |
1da177e4 LT |
509 | printk("RTAS system-reboot returned %d\n", |
510 | rtas_call(rtas_token("system-reboot"), 0, 1, NULL)); | |
511 | for (;;); | |
512 | } | |
513 | ||
033ef338 | 514 | void rtas_power_off(void) |
1da177e4 | 515 | { |
f4fcbbe9 PM |
516 | if (rtas_flash_term_hook) |
517 | rtas_flash_term_hook(SYS_POWER_OFF); | |
1da177e4 LT |
518 | /* 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 | ||
033ef338 | 524 | void rtas_halt(void) |
1da177e4 | 525 | { |
f4fcbbe9 PM |
526 | 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 (;;); | |
1da177e4 LT |
532 | } |
533 | ||
534 | /* Must be in the RMO region, so we place it here */ | |
535 | static char rtas_os_term_buf[2048]; | |
536 | ||
537 | void 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 | ||
559 | asmlinkage int ppc_rtas(struct rtas_args __user *uargs) | |
560 | { | |
561 | struct rtas_args args; | |
562 | unsigned long flags; | |
033ef338 | 563 | char *buff_copy, *errbuf = NULL; |
1da177e4 | 564 | int nargs; |
1da177e4 LT |
565 | |
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 | ||
033ef338 | 583 | buff_copy = get_errorlog_buffer(); |
1da177e4 LT |
584 | |
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. */ | |
033ef338 PM |
595 | if (args.rets[0] == -1) |
596 | errbuf = __fetch_rtas_last_error(buff_copy); | |
1da177e4 LT |
597 | |
598 | spin_unlock_irqrestore(&rtas.lock, flags); | |
599 | ||
600 | if (buff_copy) { | |
033ef338 PM |
601 | if (errbuf) |
602 | log_error(errbuf, ERR_TYPE_RTAS_LOG, 0); | |
1da177e4 LT |
603 | 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 | ||
617 | struct 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 | ||
625 | void 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 | /* | |
943ffb58 | 641 | * Call early during boot, before mem init or bootmem, to retrieve the RTAS |
1da177e4 LT |
642 | * informations from the device-tree and allocate the RMO buffer for userland |
643 | * accesses. | |
644 | */ | |
645 | void __init rtas_initialize(void) | |
646 | { | |
033ef338 PM |
647 | unsigned long rtas_region = RTAS_INSTANTIATE_MAX; |
648 | ||
1da177e4 LT |
649 | /* 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 | } | |
033ef338 PM |
670 | if (!rtas.dev) |
671 | return; | |
672 | ||
1da177e4 LT |
673 | /* If RTAS was found, allocate the RMO buffer for it and look for |
674 | * the stop-self token if any | |
675 | */ | |
033ef338 | 676 | #ifdef CONFIG_PPC64 |
799d6046 | 677 | if (_machine == PLATFORM_PSERIES_LPAR) |
033ef338 PM |
678 | 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); | |
1da177e4 LT |
681 | |
682 | #ifdef CONFIG_HOTPLUG_CPU | |
033ef338 | 683 | rtas_stop_self_args.token = rtas_token("stop-self"); |
1da177e4 | 684 | #endif /* CONFIG_HOTPLUG_CPU */ |
033ef338 PM |
685 | #ifdef CONFIG_RTAS_ERROR_LOGGING |
686 | rtas_last_error_token = rtas_token("rtas-last-error"); | |
687 | #endif | |
1da177e4 LT |
688 | } |
689 | ||
690 | ||
1da177e4 LT |
691 | EXPORT_SYMBOL(rtas_token); |
692 | EXPORT_SYMBOL(rtas_call); | |
693 | EXPORT_SYMBOL(rtas_data_buf); | |
694 | EXPORT_SYMBOL(rtas_data_buf_lock); | |
695 | EXPORT_SYMBOL(rtas_extended_busy_delay_time); | |
696 | EXPORT_SYMBOL(rtas_get_sensor); | |
697 | EXPORT_SYMBOL(rtas_get_power_level); | |
698 | EXPORT_SYMBOL(rtas_set_power_level); | |
699 | EXPORT_SYMBOL(rtas_set_indicator); |