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> | |
a9415644 | 20 | #include <linux/capability.h> |
21fe3301 | 21 | #include <linux/delay.h> |
1da177e4 LT |
22 | |
23 | #include <asm/prom.h> | |
24 | #include <asm/rtas.h> | |
25 | #include <asm/semaphore.h> | |
26 | #include <asm/machdep.h> | |
27 | #include <asm/page.h> | |
28 | #include <asm/param.h> | |
29 | #include <asm/system.h> | |
1da177e4 LT |
30 | #include <asm/delay.h> |
31 | #include <asm/uaccess.h> | |
033ef338 | 32 | #include <asm/lmb.h> |
296167ae | 33 | #include <asm/udbg.h> |
1da177e4 | 34 | |
033ef338 | 35 | struct rtas_t rtas = { |
1da177e4 LT |
36 | .lock = SPIN_LOCK_UNLOCKED |
37 | }; | |
38 | ||
91dc182c DB |
39 | struct rtas_suspend_me_data { |
40 | long waiting; | |
41 | struct rtas_args *args; | |
42 | }; | |
43 | ||
1da177e4 LT |
44 | EXPORT_SYMBOL(rtas); |
45 | ||
1da177e4 | 46 | DEFINE_SPINLOCK(rtas_data_buf_lock); |
033ef338 | 47 | char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned; |
1da177e4 LT |
48 | unsigned long rtas_rmo_buf; |
49 | ||
f4fcbbe9 PM |
50 | /* |
51 | * If non-NULL, this gets called when the kernel terminates. | |
52 | * This is done like this so rtas_flash can be a module. | |
53 | */ | |
54 | void (*rtas_flash_term_hook)(int); | |
55 | EXPORT_SYMBOL(rtas_flash_term_hook); | |
56 | ||
033ef338 PM |
57 | /* |
58 | * call_rtas_display_status and call_rtas_display_status_delay | |
59 | * are designed only for very early low-level debugging, which | |
60 | * is why the token is hard-coded to 10. | |
61 | */ | |
296167ae | 62 | static void call_rtas_display_status(char c) |
1da177e4 LT |
63 | { |
64 | struct rtas_args *args = &rtas.args; | |
65 | unsigned long s; | |
66 | ||
67 | if (!rtas.base) | |
68 | return; | |
69 | spin_lock_irqsave(&rtas.lock, s); | |
70 | ||
71 | args->token = 10; | |
72 | args->nargs = 1; | |
73 | args->nret = 1; | |
74 | args->rets = (rtas_arg_t *)&(args->args[1]); | |
296167ae | 75 | args->args[0] = (unsigned char)c; |
1da177e4 LT |
76 | |
77 | enter_rtas(__pa(args)); | |
78 | ||
79 | spin_unlock_irqrestore(&rtas.lock, s); | |
80 | } | |
81 | ||
296167ae | 82 | static void call_rtas_display_status_delay(char c) |
1da177e4 LT |
83 | { |
84 | static int pending_newline = 0; /* did last write end with unprinted newline? */ | |
85 | static int width = 16; | |
86 | ||
87 | if (c == '\n') { | |
88 | while (width-- > 0) | |
89 | call_rtas_display_status(' '); | |
90 | width = 16; | |
21fe3301 | 91 | mdelay(500); |
1da177e4 LT |
92 | pending_newline = 1; |
93 | } else { | |
94 | if (pending_newline) { | |
95 | call_rtas_display_status('\r'); | |
96 | call_rtas_display_status('\n'); | |
97 | } | |
98 | pending_newline = 0; | |
99 | if (width--) { | |
100 | call_rtas_display_status(c); | |
101 | udelay(10000); | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
296167ae ME |
106 | void __init udbg_init_rtas(void) |
107 | { | |
108 | udbg_putc = call_rtas_display_status_delay; | |
109 | } | |
110 | ||
033ef338 | 111 | void rtas_progress(char *s, unsigned short hex) |
6566c6f1 AB |
112 | { |
113 | struct device_node *root; | |
114 | int width, *p; | |
115 | char *os; | |
116 | static int display_character, set_indicator; | |
8f586b22 | 117 | static int display_width, display_lines, *row_width, form_feed; |
6566c6f1 | 118 | static DEFINE_SPINLOCK(progress_lock); |
8f586b22 | 119 | static int current_line; |
6566c6f1 AB |
120 | static int pending_newline = 0; /* did last write end with unprinted newline? */ |
121 | ||
122 | if (!rtas.base) | |
123 | return; | |
124 | ||
8f586b22 MS |
125 | if (display_width == 0) { |
126 | display_width = 0x10; | |
127 | if ((root = find_path_device("/rtas"))) { | |
128 | if ((p = (unsigned int *)get_property(root, | |
129 | "ibm,display-line-length", NULL))) | |
130 | display_width = *p; | |
131 | if ((p = (unsigned int *)get_property(root, | |
132 | "ibm,form-feed", NULL))) | |
133 | form_feed = *p; | |
134 | if ((p = (unsigned int *)get_property(root, | |
135 | "ibm,display-number-of-lines", NULL))) | |
136 | display_lines = *p; | |
137 | row_width = (unsigned int *)get_property(root, | |
138 | "ibm,display-truncation-length", NULL); | |
139 | } | |
6566c6f1 AB |
140 | display_character = rtas_token("display-character"); |
141 | set_indicator = rtas_token("set-indicator"); | |
142 | } | |
143 | ||
144 | if (display_character == RTAS_UNKNOWN_SERVICE) { | |
145 | /* use hex display if available */ | |
146 | if (set_indicator != RTAS_UNKNOWN_SERVICE) | |
147 | rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex); | |
148 | return; | |
149 | } | |
150 | ||
151 | spin_lock(&progress_lock); | |
152 | ||
153 | /* | |
154 | * Last write ended with newline, but we didn't print it since | |
155 | * it would just clear the bottom line of output. Print it now | |
156 | * instead. | |
157 | * | |
8f586b22 MS |
158 | * If no newline is pending and form feed is supported, clear the |
159 | * display with a form feed; otherwise, print a CR to start output | |
160 | * at the beginning of the line. | |
6566c6f1 AB |
161 | */ |
162 | if (pending_newline) { | |
163 | rtas_call(display_character, 1, 1, NULL, '\r'); | |
164 | rtas_call(display_character, 1, 1, NULL, '\n'); | |
165 | pending_newline = 0; | |
166 | } else { | |
8f586b22 MS |
167 | current_line = 0; |
168 | if (form_feed) | |
169 | rtas_call(display_character, 1, 1, NULL, | |
170 | (char)form_feed); | |
171 | else | |
172 | rtas_call(display_character, 1, 1, NULL, '\r'); | |
6566c6f1 AB |
173 | } |
174 | ||
8f586b22 MS |
175 | if (row_width) |
176 | width = row_width[current_line]; | |
177 | else | |
178 | width = display_width; | |
6566c6f1 AB |
179 | os = s; |
180 | while (*os) { | |
181 | if (*os == '\n' || *os == '\r') { | |
6566c6f1 AB |
182 | /* If newline is the last character, save it |
183 | * until next call to avoid bumping up the | |
184 | * display output. | |
185 | */ | |
186 | if (*os == '\n' && !os[1]) { | |
187 | pending_newline = 1; | |
8f586b22 MS |
188 | current_line++; |
189 | if (current_line > display_lines-1) | |
190 | current_line = display_lines-1; | |
6566c6f1 AB |
191 | spin_unlock(&progress_lock); |
192 | return; | |
193 | } | |
194 | ||
195 | /* RTAS wants CR-LF, not just LF */ | |
196 | ||
197 | if (*os == '\n') { | |
198 | rtas_call(display_character, 1, 1, NULL, '\r'); | |
199 | rtas_call(display_character, 1, 1, NULL, '\n'); | |
200 | } else { | |
201 | /* CR might be used to re-draw a line, so we'll | |
202 | * leave it alone and not add LF. | |
203 | */ | |
204 | rtas_call(display_character, 1, 1, NULL, *os); | |
205 | } | |
206 | ||
8f586b22 MS |
207 | if (row_width) |
208 | width = row_width[current_line]; | |
209 | else | |
210 | width = display_width; | |
6566c6f1 AB |
211 | } else { |
212 | width--; | |
213 | rtas_call(display_character, 1, 1, NULL, *os); | |
214 | } | |
215 | ||
216 | os++; | |
217 | ||
218 | /* if we overwrite the screen length */ | |
219 | if (width <= 0) | |
220 | while ((*os != 0) && (*os != '\n') && (*os != '\r')) | |
221 | os++; | |
222 | } | |
223 | ||
6566c6f1 AB |
224 | spin_unlock(&progress_lock); |
225 | } | |
f4fcbbe9 | 226 | EXPORT_SYMBOL(rtas_progress); /* needed by rtas_flash module */ |
6566c6f1 | 227 | |
033ef338 | 228 | int rtas_token(const char *service) |
1da177e4 LT |
229 | { |
230 | int *tokp; | |
033ef338 | 231 | if (rtas.dev == NULL) |
1da177e4 | 232 | return RTAS_UNKNOWN_SERVICE; |
1da177e4 LT |
233 | tokp = (int *) get_property(rtas.dev, service, NULL); |
234 | return tokp ? *tokp : RTAS_UNKNOWN_SERVICE; | |
235 | } | |
236 | ||
033ef338 | 237 | #ifdef CONFIG_RTAS_ERROR_LOGGING |
1da177e4 LT |
238 | /* |
239 | * Return the firmware-specified size of the error log buffer | |
240 | * for all rtas calls that require an error buffer argument. | |
241 | * This includes 'check-exception' and 'rtas-last-error'. | |
242 | */ | |
243 | int rtas_get_error_log_max(void) | |
244 | { | |
245 | static int rtas_error_log_max; | |
246 | if (rtas_error_log_max) | |
247 | return rtas_error_log_max; | |
248 | ||
249 | rtas_error_log_max = rtas_token ("rtas-error-log-max"); | |
250 | if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) || | |
251 | (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) { | |
033ef338 PM |
252 | printk (KERN_WARNING "RTAS: bad log buffer size %d\n", |
253 | rtas_error_log_max); | |
1da177e4 LT |
254 | rtas_error_log_max = RTAS_ERROR_LOG_MAX; |
255 | } | |
256 | return rtas_error_log_max; | |
257 | } | |
033ef338 | 258 | EXPORT_SYMBOL(rtas_get_error_log_max); |
1da177e4 LT |
259 | |
260 | ||
033ef338 PM |
261 | char rtas_err_buf[RTAS_ERROR_LOG_MAX]; |
262 | int rtas_last_error_token; | |
263 | ||
1da177e4 LT |
264 | /** Return a copy of the detailed error text associated with the |
265 | * most recent failed call to rtas. Because the error text | |
266 | * might go stale if there are any other intervening rtas calls, | |
267 | * this routine must be called atomically with whatever produced | |
268 | * the error (i.e. with rtas.lock still held from the previous call). | |
269 | */ | |
033ef338 | 270 | static char *__fetch_rtas_last_error(char *altbuf) |
1da177e4 LT |
271 | { |
272 | struct rtas_args err_args, save_args; | |
273 | u32 bufsz; | |
033ef338 PM |
274 | char *buf = NULL; |
275 | ||
276 | if (rtas_last_error_token == -1) | |
277 | return NULL; | |
1da177e4 LT |
278 | |
279 | bufsz = rtas_get_error_log_max(); | |
280 | ||
033ef338 | 281 | err_args.token = rtas_last_error_token; |
1da177e4 LT |
282 | err_args.nargs = 2; |
283 | err_args.nret = 1; | |
1da177e4 LT |
284 | err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf); |
285 | err_args.args[1] = bufsz; | |
286 | err_args.args[2] = 0; | |
287 | ||
288 | save_args = rtas.args; | |
289 | rtas.args = err_args; | |
290 | ||
291 | enter_rtas(__pa(&rtas.args)); | |
292 | ||
293 | err_args = rtas.args; | |
294 | rtas.args = save_args; | |
295 | ||
033ef338 PM |
296 | /* Log the error in the unlikely case that there was one. */ |
297 | if (unlikely(err_args.args[2] == 0)) { | |
298 | if (altbuf) { | |
299 | buf = altbuf; | |
300 | } else { | |
301 | buf = rtas_err_buf; | |
302 | if (mem_init_done) | |
303 | buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC); | |
304 | } | |
305 | if (buf) | |
306 | memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX); | |
307 | } | |
308 | ||
309 | return buf; | |
1da177e4 LT |
310 | } |
311 | ||
033ef338 PM |
312 | #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL) |
313 | ||
314 | #else /* CONFIG_RTAS_ERROR_LOGGING */ | |
315 | #define __fetch_rtas_last_error(x) NULL | |
316 | #define get_errorlog_buffer() NULL | |
317 | #endif | |
318 | ||
1da177e4 LT |
319 | int rtas_call(int token, int nargs, int nret, int *outputs, ...) |
320 | { | |
321 | va_list list; | |
033ef338 | 322 | int i; |
1da177e4 LT |
323 | unsigned long s; |
324 | struct rtas_args *rtas_args; | |
033ef338 | 325 | char *buff_copy = NULL; |
1da177e4 LT |
326 | int ret; |
327 | ||
1da177e4 LT |
328 | if (token == RTAS_UNKNOWN_SERVICE) |
329 | return -1; | |
330 | ||
331 | /* Gotta do something different here, use global lock for now... */ | |
332 | spin_lock_irqsave(&rtas.lock, s); | |
333 | rtas_args = &rtas.args; | |
334 | ||
335 | rtas_args->token = token; | |
336 | rtas_args->nargs = nargs; | |
337 | rtas_args->nret = nret; | |
338 | rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]); | |
339 | va_start(list, outputs); | |
033ef338 | 340 | for (i = 0; i < nargs; ++i) |
1da177e4 | 341 | rtas_args->args[i] = va_arg(list, rtas_arg_t); |
1da177e4 LT |
342 | va_end(list); |
343 | ||
344 | for (i = 0; i < nret; ++i) | |
345 | rtas_args->rets[i] = 0; | |
346 | ||
1da177e4 | 347 | enter_rtas(__pa(rtas_args)); |
1da177e4 LT |
348 | |
349 | /* A -1 return code indicates that the last command couldn't | |
350 | be completed due to a hardware error. */ | |
351 | if (rtas_args->rets[0] == -1) | |
033ef338 | 352 | buff_copy = __fetch_rtas_last_error(NULL); |
1da177e4 LT |
353 | |
354 | if (nret > 1 && outputs != NULL) | |
355 | for (i = 0; i < nret-1; ++i) | |
356 | outputs[i] = rtas_args->rets[i+1]; | |
357 | ret = (nret > 0)? rtas_args->rets[0]: 0; | |
358 | ||
1da177e4 LT |
359 | /* Gotta do something different here, use global lock for now... */ |
360 | spin_unlock_irqrestore(&rtas.lock, s); | |
361 | ||
362 | if (buff_copy) { | |
363 | log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0); | |
364 | if (mem_init_done) | |
365 | kfree(buff_copy); | |
366 | } | |
367 | return ret; | |
368 | } | |
369 | ||
370 | /* Given an RTAS status code of 990n compute the hinted delay of 10^n | |
371 | * (last digit) milliseconds. For now we bound at n=5 (100 sec). | |
372 | */ | |
033ef338 | 373 | unsigned int rtas_extended_busy_delay_time(int status) |
1da177e4 LT |
374 | { |
375 | int order = status - 9900; | |
376 | unsigned long ms; | |
377 | ||
378 | if (order < 0) | |
379 | order = 0; /* RTC depends on this for -2 clock busy */ | |
380 | else if (order > 5) | |
381 | order = 5; /* bound */ | |
382 | ||
383 | /* Use microseconds for reasonable accuracy */ | |
033ef338 | 384 | for (ms = 1; order > 0; order--) |
1da177e4 LT |
385 | ms *= 10; |
386 | ||
387 | return ms; | |
388 | } | |
389 | ||
390 | int rtas_error_rc(int rtas_rc) | |
391 | { | |
392 | int rc; | |
393 | ||
394 | switch (rtas_rc) { | |
395 | case -1: /* Hardware Error */ | |
396 | rc = -EIO; | |
397 | break; | |
398 | case -3: /* Bad indicator/domain/etc */ | |
399 | rc = -EINVAL; | |
400 | break; | |
401 | case -9000: /* Isolation error */ | |
402 | rc = -EFAULT; | |
403 | break; | |
404 | case -9001: /* Outstanding TCE/PTE */ | |
405 | rc = -EEXIST; | |
406 | break; | |
407 | case -9002: /* No usable slot */ | |
408 | rc = -ENODEV; | |
409 | break; | |
410 | default: | |
411 | printk(KERN_ERR "%s: unexpected RTAS error %d\n", | |
412 | __FUNCTION__, rtas_rc); | |
413 | rc = -ERANGE; | |
414 | break; | |
415 | } | |
416 | return rc; | |
417 | } | |
418 | ||
419 | int rtas_get_power_level(int powerdomain, int *level) | |
420 | { | |
421 | int token = rtas_token("get-power-level"); | |
422 | int rc; | |
423 | ||
424 | if (token == RTAS_UNKNOWN_SERVICE) | |
425 | return -ENOENT; | |
426 | ||
427 | while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY) | |
428 | udelay(1); | |
429 | ||
430 | if (rc < 0) | |
431 | return rtas_error_rc(rc); | |
432 | return rc; | |
433 | } | |
434 | ||
435 | int rtas_set_power_level(int powerdomain, int level, int *setlevel) | |
436 | { | |
437 | int token = rtas_token("set-power-level"); | |
438 | unsigned int wait_time; | |
439 | int rc; | |
440 | ||
441 | if (token == RTAS_UNKNOWN_SERVICE) | |
442 | return -ENOENT; | |
443 | ||
444 | while (1) { | |
445 | rc = rtas_call(token, 2, 2, setlevel, powerdomain, level); | |
446 | if (rc == RTAS_BUSY) | |
447 | udelay(1); | |
448 | else if (rtas_is_extended_busy(rc)) { | |
449 | wait_time = rtas_extended_busy_delay_time(rc); | |
450 | udelay(wait_time * 1000); | |
451 | } else | |
452 | break; | |
453 | } | |
454 | ||
455 | if (rc < 0) | |
456 | return rtas_error_rc(rc); | |
457 | return rc; | |
458 | } | |
459 | ||
460 | int rtas_get_sensor(int sensor, int index, int *state) | |
461 | { | |
462 | int token = rtas_token("get-sensor-state"); | |
463 | unsigned int wait_time; | |
464 | int rc; | |
465 | ||
466 | if (token == RTAS_UNKNOWN_SERVICE) | |
467 | return -ENOENT; | |
468 | ||
469 | while (1) { | |
470 | rc = rtas_call(token, 2, 2, state, sensor, index); | |
471 | if (rc == RTAS_BUSY) | |
472 | udelay(1); | |
473 | else if (rtas_is_extended_busy(rc)) { | |
474 | wait_time = rtas_extended_busy_delay_time(rc); | |
475 | udelay(wait_time * 1000); | |
476 | } else | |
477 | break; | |
478 | } | |
479 | ||
480 | if (rc < 0) | |
481 | return rtas_error_rc(rc); | |
482 | return rc; | |
483 | } | |
484 | ||
485 | int rtas_set_indicator(int indicator, int index, int new_value) | |
486 | { | |
487 | int token = rtas_token("set-indicator"); | |
488 | unsigned int wait_time; | |
489 | int rc; | |
490 | ||
491 | if (token == RTAS_UNKNOWN_SERVICE) | |
492 | return -ENOENT; | |
493 | ||
494 | while (1) { | |
495 | rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value); | |
496 | if (rc == RTAS_BUSY) | |
497 | udelay(1); | |
498 | else if (rtas_is_extended_busy(rc)) { | |
499 | wait_time = rtas_extended_busy_delay_time(rc); | |
500 | udelay(wait_time * 1000); | |
501 | } | |
502 | else | |
503 | break; | |
504 | } | |
505 | ||
506 | if (rc < 0) | |
507 | return rtas_error_rc(rc); | |
508 | return rc; | |
509 | } | |
510 | ||
033ef338 | 511 | void rtas_restart(char *cmd) |
1da177e4 | 512 | { |
f4fcbbe9 PM |
513 | if (rtas_flash_term_hook) |
514 | rtas_flash_term_hook(SYS_RESTART); | |
1da177e4 LT |
515 | printk("RTAS system-reboot returned %d\n", |
516 | rtas_call(rtas_token("system-reboot"), 0, 1, NULL)); | |
517 | for (;;); | |
518 | } | |
519 | ||
033ef338 | 520 | void rtas_power_off(void) |
1da177e4 | 521 | { |
f4fcbbe9 PM |
522 | if (rtas_flash_term_hook) |
523 | rtas_flash_term_hook(SYS_POWER_OFF); | |
1da177e4 LT |
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 (;;); | |
528 | } | |
529 | ||
033ef338 | 530 | void rtas_halt(void) |
1da177e4 | 531 | { |
f4fcbbe9 PM |
532 | if (rtas_flash_term_hook) |
533 | rtas_flash_term_hook(SYS_HALT); | |
534 | /* allow power on only with power button press */ | |
535 | printk("RTAS power-off returned %d\n", | |
536 | rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1)); | |
537 | for (;;); | |
1da177e4 LT |
538 | } |
539 | ||
540 | /* Must be in the RMO region, so we place it here */ | |
541 | static char rtas_os_term_buf[2048]; | |
542 | ||
543 | void rtas_os_term(char *str) | |
544 | { | |
545 | int status; | |
546 | ||
547 | if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term")) | |
548 | return; | |
549 | ||
550 | snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str); | |
551 | ||
552 | do { | |
553 | status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL, | |
554 | __pa(rtas_os_term_buf)); | |
555 | ||
556 | if (status == RTAS_BUSY) | |
557 | udelay(1); | |
558 | else if (status != 0) | |
559 | printk(KERN_EMERG "ibm,os-term call failed %d\n", | |
560 | status); | |
561 | } while (status == RTAS_BUSY); | |
562 | } | |
563 | ||
91dc182c DB |
564 | static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE; |
565 | #ifdef CONFIG_PPC_PSERIES | |
566 | static void rtas_percpu_suspend_me(void *info) | |
567 | { | |
568 | 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 | |
577 | * one thread got H_Continue. If any error happens, | |
578 | * we set it to <0. | |
579 | */ | |
580 | local_irq_save(flags); | |
581 | do { | |
582 | rc = plpar_hcall_norets(H_JOIN); | |
583 | smp_rmb(); | |
584 | } while (rc == H_Success && data->waiting > 0); | |
585 | if (rc == H_Success) | |
586 | goto out; | |
587 | ||
588 | if (rc == H_Continue) { | |
589 | data->waiting = 0; | |
590 | rtas_call(ibm_suspend_me_token, 0, 1, | |
591 | data->args->args); | |
592 | } else { | |
593 | data->waiting = -EBUSY; | |
594 | printk(KERN_ERR "Error on H_Join hypervisor call\n"); | |
595 | } | |
596 | ||
597 | out: | |
598 | /* before we restore interrupts, make sure we don't | |
599 | * generate a spurious soft lockup errors | |
600 | */ | |
601 | touch_softlockup_watchdog(); | |
602 | local_irq_restore(flags); | |
603 | return; | |
604 | } | |
605 | ||
606 | static int rtas_ibm_suspend_me(struct rtas_args *args) | |
607 | { | |
608 | int i; | |
609 | ||
610 | struct rtas_suspend_me_data data; | |
611 | ||
612 | data.waiting = 1; | |
613 | data.args = args; | |
614 | ||
615 | /* Call function on all CPUs. One of us will make the | |
616 | * rtas call | |
617 | */ | |
618 | if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0)) | |
619 | data.waiting = -EINVAL; | |
620 | ||
621 | if (data.waiting != 0) | |
622 | printk(KERN_ERR "Error doing global join\n"); | |
623 | ||
624 | /* Prod each CPU. This won't hurt, and will wake | |
625 | * anyone we successfully put to sleep with H_Join | |
626 | */ | |
627 | for_each_cpu(i) | |
628 | plpar_hcall_norets(H_PROD, i); | |
629 | ||
630 | return data.waiting; | |
631 | } | |
632 | #else /* CONFIG_PPC_PSERIES */ | |
633 | static int rtas_ibm_suspend_me(struct rtas_args *args) | |
634 | { | |
635 | return -ENOSYS; | |
636 | } | |
637 | #endif | |
1da177e4 LT |
638 | |
639 | asmlinkage int ppc_rtas(struct rtas_args __user *uargs) | |
640 | { | |
641 | struct rtas_args args; | |
642 | unsigned long flags; | |
033ef338 | 643 | char *buff_copy, *errbuf = NULL; |
1da177e4 | 644 | int nargs; |
91dc182c | 645 | int rc; |
1da177e4 LT |
646 | |
647 | if (!capable(CAP_SYS_ADMIN)) | |
648 | return -EPERM; | |
649 | ||
650 | if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0) | |
651 | return -EFAULT; | |
652 | ||
653 | nargs = args.nargs; | |
654 | if (nargs > ARRAY_SIZE(args.args) | |
655 | || args.nret > ARRAY_SIZE(args.args) | |
656 | || nargs + args.nret > ARRAY_SIZE(args.args)) | |
657 | return -EINVAL; | |
658 | ||
659 | /* Copy in args. */ | |
660 | if (copy_from_user(args.args, uargs->args, | |
661 | nargs * sizeof(rtas_arg_t)) != 0) | |
662 | return -EFAULT; | |
663 | ||
91dc182c DB |
664 | if (args.token == RTAS_UNKNOWN_SERVICE) |
665 | return -EINVAL; | |
666 | ||
667 | /* Need to handle ibm,suspend_me call specially */ | |
668 | if (args.token == ibm_suspend_me_token) { | |
669 | rc = rtas_ibm_suspend_me(&args); | |
670 | if (rc) | |
671 | return rc; | |
672 | goto copy_return; | |
673 | } | |
674 | ||
033ef338 | 675 | buff_copy = get_errorlog_buffer(); |
1da177e4 LT |
676 | |
677 | spin_lock_irqsave(&rtas.lock, flags); | |
678 | ||
679 | rtas.args = args; | |
680 | enter_rtas(__pa(&rtas.args)); | |
681 | args = rtas.args; | |
682 | ||
683 | args.rets = &args.args[nargs]; | |
684 | ||
685 | /* A -1 return code indicates that the last command couldn't | |
686 | be completed due to a hardware error. */ | |
033ef338 PM |
687 | if (args.rets[0] == -1) |
688 | errbuf = __fetch_rtas_last_error(buff_copy); | |
1da177e4 LT |
689 | |
690 | spin_unlock_irqrestore(&rtas.lock, flags); | |
691 | ||
692 | if (buff_copy) { | |
033ef338 PM |
693 | if (errbuf) |
694 | log_error(errbuf, ERR_TYPE_RTAS_LOG, 0); | |
1da177e4 LT |
695 | kfree(buff_copy); |
696 | } | |
697 | ||
91dc182c | 698 | copy_return: |
1da177e4 LT |
699 | /* Copy out args. */ |
700 | if (copy_to_user(uargs->args + nargs, | |
701 | args.args + nargs, | |
702 | args.nret * sizeof(rtas_arg_t)) != 0) | |
703 | return -EFAULT; | |
704 | ||
705 | return 0; | |
706 | } | |
707 | ||
708 | /* This version can't take the spinlock, because it never returns */ | |
709 | ||
710 | struct rtas_args rtas_stop_self_args = { | |
711 | /* The token is initialized for real in setup_system() */ | |
712 | .token = RTAS_UNKNOWN_SERVICE, | |
713 | .nargs = 0, | |
714 | .nret = 1, | |
715 | .rets = &rtas_stop_self_args.args[0], | |
716 | }; | |
717 | ||
718 | void rtas_stop_self(void) | |
719 | { | |
720 | struct rtas_args *rtas_args = &rtas_stop_self_args; | |
721 | ||
722 | local_irq_disable(); | |
723 | ||
724 | BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE); | |
725 | ||
726 | printk("cpu %u (hwid %u) Ready to die...\n", | |
727 | smp_processor_id(), hard_smp_processor_id()); | |
728 | enter_rtas(__pa(rtas_args)); | |
729 | ||
730 | panic("Alas, I survived.\n"); | |
731 | } | |
732 | ||
733 | /* | |
943ffb58 | 734 | * Call early during boot, before mem init or bootmem, to retrieve the RTAS |
1da177e4 LT |
735 | * informations from the device-tree and allocate the RMO buffer for userland |
736 | * accesses. | |
737 | */ | |
738 | void __init rtas_initialize(void) | |
739 | { | |
033ef338 PM |
740 | unsigned long rtas_region = RTAS_INSTANTIATE_MAX; |
741 | ||
1da177e4 LT |
742 | /* Get RTAS dev node and fill up our "rtas" structure with infos |
743 | * about it. | |
744 | */ | |
745 | rtas.dev = of_find_node_by_name(NULL, "rtas"); | |
746 | if (rtas.dev) { | |
747 | u32 *basep, *entryp; | |
748 | u32 *sizep; | |
749 | ||
750 | basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL); | |
751 | sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL); | |
752 | if (basep != NULL && sizep != NULL) { | |
753 | rtas.base = *basep; | |
754 | rtas.size = *sizep; | |
755 | entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL); | |
756 | if (entryp == NULL) /* Ugh */ | |
757 | rtas.entry = rtas.base; | |
758 | else | |
759 | rtas.entry = *entryp; | |
760 | } else | |
761 | rtas.dev = NULL; | |
762 | } | |
033ef338 PM |
763 | if (!rtas.dev) |
764 | return; | |
765 | ||
1da177e4 LT |
766 | /* If RTAS was found, allocate the RMO buffer for it and look for |
767 | * the stop-self token if any | |
768 | */ | |
033ef338 | 769 | #ifdef CONFIG_PPC64 |
91dc182c | 770 | if (_machine == PLATFORM_PSERIES_LPAR) { |
033ef338 | 771 | rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX); |
91dc182c DB |
772 | ibm_suspend_me_token = rtas_token("ibm,suspend-me"); |
773 | } | |
033ef338 PM |
774 | #endif |
775 | rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region); | |
1da177e4 LT |
776 | |
777 | #ifdef CONFIG_HOTPLUG_CPU | |
033ef338 | 778 | rtas_stop_self_args.token = rtas_token("stop-self"); |
1da177e4 | 779 | #endif /* CONFIG_HOTPLUG_CPU */ |
033ef338 PM |
780 | #ifdef CONFIG_RTAS_ERROR_LOGGING |
781 | rtas_last_error_token = rtas_token("rtas-last-error"); | |
782 | #endif | |
1da177e4 LT |
783 | } |
784 | ||
785 | ||
1da177e4 LT |
786 | EXPORT_SYMBOL(rtas_token); |
787 | EXPORT_SYMBOL(rtas_call); | |
788 | EXPORT_SYMBOL(rtas_data_buf); | |
789 | EXPORT_SYMBOL(rtas_data_buf_lock); | |
790 | EXPORT_SYMBOL(rtas_extended_busy_delay_time); | |
791 | EXPORT_SYMBOL(rtas_get_sensor); | |
792 | EXPORT_SYMBOL(rtas_get_power_level); | |
793 | EXPORT_SYMBOL(rtas_set_power_level); | |
794 | EXPORT_SYMBOL(rtas_set_indicator); |