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