| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 20 | #include <linux/capability.h> | 
| Benjamin Herrenschmidt | 21fe330 | 2005-11-07 16:41:59 +1100 | [diff] [blame] | 21 | #include <linux/delay.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <asm/prom.h> | 
|  | 24 | #include <asm/rtas.h> | 
| Michael Ellerman | 31a7f67 | 2006-01-31 17:17:47 +1100 | [diff] [blame] | 25 | #include <asm/hvcall.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/semaphore.h> | 
|  | 27 | #include <asm/machdep.h> | 
| Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 28 | #include <asm/firmware.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/page.h> | 
|  | 30 | #include <asm/param.h> | 
|  | 31 | #include <asm/system.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/delay.h> | 
|  | 33 | #include <asm/uaccess.h> | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 34 | #include <asm/lmb.h> | 
| Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 35 | #include <asm/udbg.h> | 
| Arnd Bergmann | a7f3184 | 2006-03-23 00:00:08 +0100 | [diff] [blame] | 36 | #include <asm/syscalls.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 38 | struct rtas_t rtas = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | .lock = SPIN_LOCK_UNLOCKED | 
|  | 40 | }; | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 41 | EXPORT_SYMBOL(rtas); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 |  | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 43 | struct rtas_suspend_me_data { | 
|  | 44 | long waiting; | 
|  | 45 | struct rtas_args *args; | 
|  | 46 | }; | 
|  | 47 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | DEFINE_SPINLOCK(rtas_data_buf_lock); | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 49 | EXPORT_SYMBOL(rtas_data_buf_lock); | 
|  | 50 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 51 | char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned; | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 52 | EXPORT_SYMBOL(rtas_data_buf); | 
|  | 53 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | unsigned long rtas_rmo_buf; | 
|  | 55 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 56 | /* | 
| Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 57 | * If non-NULL, this gets called when the kernel terminates. | 
|  | 58 | * This is done like this so rtas_flash can be a module. | 
|  | 59 | */ | 
|  | 60 | void (*rtas_flash_term_hook)(int); | 
|  | 61 | EXPORT_SYMBOL(rtas_flash_term_hook); | 
|  | 62 |  | 
|  | 63 | /* | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 64 | * call_rtas_display_status and call_rtas_display_status_delay | 
|  | 65 | * are designed only for very early low-level debugging, which | 
|  | 66 | * is why the token is hard-coded to 10. | 
|  | 67 | */ | 
| Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 68 | static void call_rtas_display_status(char c) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { | 
|  | 70 | struct rtas_args *args = &rtas.args; | 
|  | 71 | unsigned long s; | 
|  | 72 |  | 
|  | 73 | if (!rtas.base) | 
|  | 74 | return; | 
|  | 75 | spin_lock_irqsave(&rtas.lock, s); | 
|  | 76 |  | 
|  | 77 | args->token = 10; | 
|  | 78 | args->nargs = 1; | 
|  | 79 | args->nret  = 1; | 
|  | 80 | args->rets  = (rtas_arg_t *)&(args->args[1]); | 
| Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 81 | args->args[0] = (unsigned char)c; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 |  | 
|  | 83 | enter_rtas(__pa(args)); | 
|  | 84 |  | 
|  | 85 | spin_unlock_irqrestore(&rtas.lock, s); | 
|  | 86 | } | 
|  | 87 |  | 
| Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 88 | static void call_rtas_display_status_delay(char c) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { | 
|  | 90 | static int pending_newline = 0;  /* did last write end with unprinted newline? */ | 
|  | 91 | static int width = 16; | 
|  | 92 |  | 
|  | 93 | if (c == '\n') { | 
|  | 94 | while (width-- > 0) | 
|  | 95 | call_rtas_display_status(' '); | 
|  | 96 | width = 16; | 
| Benjamin Herrenschmidt | 21fe330 | 2005-11-07 16:41:59 +1100 | [diff] [blame] | 97 | mdelay(500); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | pending_newline = 1; | 
|  | 99 | } else { | 
|  | 100 | if (pending_newline) { | 
|  | 101 | call_rtas_display_status('\r'); | 
|  | 102 | call_rtas_display_status('\n'); | 
|  | 103 | } | 
|  | 104 | pending_newline = 0; | 
|  | 105 | if (width--) { | 
|  | 106 | call_rtas_display_status(c); | 
|  | 107 | udelay(10000); | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
| Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 112 | void __init udbg_init_rtas_panel(void) | 
| Michael Ellerman | 296167a | 2006-01-11 11:54:09 +1100 | [diff] [blame] | 113 | { | 
|  | 114 | udbg_putc = call_rtas_display_status_delay; | 
|  | 115 | } | 
|  | 116 |  | 
| Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 117 | #ifdef CONFIG_UDBG_RTAS_CONSOLE | 
|  | 118 |  | 
|  | 119 | /* If you think you're dying before early_init_dt_scan_rtas() does its | 
|  | 120 | * work, you can hard code the token values for your firmware here and | 
|  | 121 | * hardcode rtas.base/entry etc. | 
|  | 122 | */ | 
|  | 123 | static unsigned int rtas_putchar_token = RTAS_UNKNOWN_SERVICE; | 
|  | 124 | static unsigned int rtas_getchar_token = RTAS_UNKNOWN_SERVICE; | 
|  | 125 |  | 
|  | 126 | static void udbg_rtascon_putc(char c) | 
|  | 127 | { | 
|  | 128 | int tries; | 
|  | 129 |  | 
|  | 130 | if (!rtas.base) | 
|  | 131 | return; | 
|  | 132 |  | 
|  | 133 | /* Add CRs before LFs */ | 
|  | 134 | if (c == '\n') | 
|  | 135 | udbg_rtascon_putc('\r'); | 
|  | 136 |  | 
|  | 137 | /* if there is more than one character to be displayed, wait a bit */ | 
|  | 138 | for (tries = 0; tries < 16; tries++) { | 
|  | 139 | if (rtas_call(rtas_putchar_token, 1, 1, NULL, c) == 0) | 
|  | 140 | break; | 
|  | 141 | udelay(1000); | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | static int udbg_rtascon_getc_poll(void) | 
|  | 146 | { | 
|  | 147 | int c; | 
|  | 148 |  | 
|  | 149 | if (!rtas.base) | 
|  | 150 | return -1; | 
|  | 151 |  | 
|  | 152 | if (rtas_call(rtas_getchar_token, 0, 2, &c)) | 
|  | 153 | return -1; | 
|  | 154 |  | 
|  | 155 | return c; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | static int udbg_rtascon_getc(void) | 
|  | 159 | { | 
|  | 160 | int c; | 
|  | 161 |  | 
|  | 162 | while ((c = udbg_rtascon_getc_poll()) == -1) | 
|  | 163 | ; | 
|  | 164 |  | 
|  | 165 | return c; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 |  | 
|  | 169 | void __init udbg_init_rtas_console(void) | 
|  | 170 | { | 
|  | 171 | udbg_putc = udbg_rtascon_putc; | 
|  | 172 | udbg_getc = udbg_rtascon_getc; | 
|  | 173 | udbg_getc_poll = udbg_rtascon_getc_poll; | 
|  | 174 | } | 
|  | 175 | #endif /* CONFIG_UDBG_RTAS_CONSOLE */ | 
|  | 176 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 177 | void rtas_progress(char *s, unsigned short hex) | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 178 | { | 
|  | 179 | struct device_node *root; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 180 | int width; | 
|  | 181 | const int *p; | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 182 | char *os; | 
|  | 183 | static int display_character, set_indicator; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 184 | static int display_width, display_lines, form_feed; | 
| Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 185 | static const int *row_width; | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 186 | static DEFINE_SPINLOCK(progress_lock); | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 187 | static int current_line; | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 188 | static int pending_newline = 0;  /* did last write end with unprinted newline? */ | 
|  | 189 |  | 
|  | 190 | if (!rtas.base) | 
|  | 191 | return; | 
|  | 192 |  | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 193 | if (display_width == 0) { | 
|  | 194 | display_width = 0x10; | 
|  | 195 | if ((root = find_path_device("/rtas"))) { | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 196 | if ((p = get_property(root, | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 197 | "ibm,display-line-length", NULL))) | 
|  | 198 | display_width = *p; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 199 | if ((p = get_property(root, | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 200 | "ibm,form-feed", NULL))) | 
|  | 201 | form_feed = *p; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 202 | if ((p = get_property(root, | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 203 | "ibm,display-number-of-lines", NULL))) | 
|  | 204 | display_lines = *p; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 205 | row_width = get_property(root, | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 206 | "ibm,display-truncation-length", NULL); | 
|  | 207 | } | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 208 | display_character = rtas_token("display-character"); | 
|  | 209 | set_indicator = rtas_token("set-indicator"); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | if (display_character == RTAS_UNKNOWN_SERVICE) { | 
|  | 213 | /* use hex display if available */ | 
|  | 214 | if (set_indicator != RTAS_UNKNOWN_SERVICE) | 
|  | 215 | rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex); | 
|  | 216 | return; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | spin_lock(&progress_lock); | 
|  | 220 |  | 
|  | 221 | /* | 
|  | 222 | * Last write ended with newline, but we didn't print it since | 
|  | 223 | * it would just clear the bottom line of output. Print it now | 
|  | 224 | * instead. | 
|  | 225 | * | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 226 | * If no newline is pending and form feed is supported, clear the | 
|  | 227 | * display with a form feed; otherwise, print a CR to start output | 
|  | 228 | * at the beginning of the line. | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 229 | */ | 
|  | 230 | if (pending_newline) { | 
|  | 231 | rtas_call(display_character, 1, 1, NULL, '\r'); | 
|  | 232 | rtas_call(display_character, 1, 1, NULL, '\n'); | 
|  | 233 | pending_newline = 0; | 
|  | 234 | } else { | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 235 | current_line = 0; | 
|  | 236 | if (form_feed) | 
|  | 237 | rtas_call(display_character, 1, 1, NULL, | 
|  | 238 | (char)form_feed); | 
|  | 239 | else | 
|  | 240 | rtas_call(display_character, 1, 1, NULL, '\r'); | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 243 | if (row_width) | 
|  | 244 | width = row_width[current_line]; | 
|  | 245 | else | 
|  | 246 | width = display_width; | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 247 | os = s; | 
|  | 248 | while (*os) { | 
|  | 249 | if (*os == '\n' || *os == '\r') { | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 250 | /* If newline is the last character, save it | 
|  | 251 | * until next call to avoid bumping up the | 
|  | 252 | * display output. | 
|  | 253 | */ | 
|  | 254 | if (*os == '\n' && !os[1]) { | 
|  | 255 | pending_newline = 1; | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 256 | current_line++; | 
|  | 257 | if (current_line > display_lines-1) | 
|  | 258 | current_line = display_lines-1; | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 259 | spin_unlock(&progress_lock); | 
|  | 260 | return; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | /* RTAS wants CR-LF, not just LF */ | 
|  | 264 |  | 
|  | 265 | if (*os == '\n') { | 
|  | 266 | rtas_call(display_character, 1, 1, NULL, '\r'); | 
|  | 267 | rtas_call(display_character, 1, 1, NULL, '\n'); | 
|  | 268 | } else { | 
|  | 269 | /* CR might be used to re-draw a line, so we'll | 
|  | 270 | * leave it alone and not add LF. | 
|  | 271 | */ | 
|  | 272 | rtas_call(display_character, 1, 1, NULL, *os); | 
|  | 273 | } | 
|  | 274 |  | 
| Mike Strosaker | 8f586b2 | 2005-06-23 16:09:41 +1000 | [diff] [blame] | 275 | if (row_width) | 
|  | 276 | width = row_width[current_line]; | 
|  | 277 | else | 
|  | 278 | width = display_width; | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 279 | } else { | 
|  | 280 | width--; | 
|  | 281 | rtas_call(display_character, 1, 1, NULL, *os); | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | os++; | 
|  | 285 |  | 
|  | 286 | /* if we overwrite the screen length */ | 
|  | 287 | if (width <= 0) | 
|  | 288 | while ((*os != 0) && (*os != '\n') && (*os != '\r')) | 
|  | 289 | os++; | 
|  | 290 | } | 
|  | 291 |  | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 292 | spin_unlock(&progress_lock); | 
|  | 293 | } | 
| Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 294 | EXPORT_SYMBOL(rtas_progress);		/* needed by rtas_flash module */ | 
| Arnd Bergmann | 6566c6f | 2005-06-23 09:43:28 +1000 | [diff] [blame] | 295 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 296 | int rtas_token(const char *service) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 298 | const int *tokp; | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 299 | if (rtas.dev == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return RTAS_UNKNOWN_SERVICE; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 301 | tokp = get_property(rtas.dev, service, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return tokp ? *tokp : RTAS_UNKNOWN_SERVICE; | 
|  | 303 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 304 | EXPORT_SYMBOL(rtas_token); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 |  | 
| Nathan Lynch | f2d6d2d | 2006-12-06 18:50:45 -0600 | [diff] [blame] | 306 | int rtas_service_present(const char *service) | 
|  | 307 | { | 
|  | 308 | return rtas_token(service) != RTAS_UNKNOWN_SERVICE; | 
|  | 309 | } | 
|  | 310 | EXPORT_SYMBOL(rtas_service_present); | 
|  | 311 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 312 | #ifdef CONFIG_RTAS_ERROR_LOGGING | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /* | 
|  | 314 | * Return the firmware-specified size of the error log buffer | 
|  | 315 | *  for all rtas calls that require an error buffer argument. | 
|  | 316 | *  This includes 'check-exception' and 'rtas-last-error'. | 
|  | 317 | */ | 
|  | 318 | int rtas_get_error_log_max(void) | 
|  | 319 | { | 
|  | 320 | static int rtas_error_log_max; | 
|  | 321 | if (rtas_error_log_max) | 
|  | 322 | return rtas_error_log_max; | 
|  | 323 |  | 
|  | 324 | rtas_error_log_max = rtas_token ("rtas-error-log-max"); | 
|  | 325 | if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) || | 
|  | 326 | (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) { | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 327 | printk (KERN_WARNING "RTAS: bad log buffer size %d\n", | 
|  | 328 | rtas_error_log_max); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | rtas_error_log_max = RTAS_ERROR_LOG_MAX; | 
|  | 330 | } | 
|  | 331 | return rtas_error_log_max; | 
|  | 332 | } | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 333 | EXPORT_SYMBOL(rtas_get_error_log_max); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 |  | 
|  | 335 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 336 | char rtas_err_buf[RTAS_ERROR_LOG_MAX]; | 
|  | 337 | int rtas_last_error_token; | 
|  | 338 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | /** Return a copy of the detailed error text associated with the | 
|  | 340 | *  most recent failed call to rtas.  Because the error text | 
|  | 341 | *  might go stale if there are any other intervening rtas calls, | 
|  | 342 | *  this routine must be called atomically with whatever produced | 
|  | 343 | *  the error (i.e. with rtas.lock still held from the previous call). | 
|  | 344 | */ | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 345 | static char *__fetch_rtas_last_error(char *altbuf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { | 
|  | 347 | struct rtas_args err_args, save_args; | 
|  | 348 | u32 bufsz; | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 349 | char *buf = NULL; | 
|  | 350 |  | 
|  | 351 | if (rtas_last_error_token == -1) | 
|  | 352 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 |  | 
|  | 354 | bufsz = rtas_get_error_log_max(); | 
|  | 355 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 356 | err_args.token = rtas_last_error_token; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | err_args.nargs = 2; | 
|  | 358 | err_args.nret = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf); | 
|  | 360 | err_args.args[1] = bufsz; | 
|  | 361 | err_args.args[2] = 0; | 
|  | 362 |  | 
|  | 363 | save_args = rtas.args; | 
|  | 364 | rtas.args = err_args; | 
|  | 365 |  | 
|  | 366 | enter_rtas(__pa(&rtas.args)); | 
|  | 367 |  | 
|  | 368 | err_args = rtas.args; | 
|  | 369 | rtas.args = save_args; | 
|  | 370 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 371 | /* Log the error in the unlikely case that there was one. */ | 
|  | 372 | if (unlikely(err_args.args[2] == 0)) { | 
|  | 373 | if (altbuf) { | 
|  | 374 | buf = altbuf; | 
|  | 375 | } else { | 
|  | 376 | buf = rtas_err_buf; | 
|  | 377 | if (mem_init_done) | 
|  | 378 | buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC); | 
|  | 379 | } | 
|  | 380 | if (buf) | 
|  | 381 | memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX); | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | return buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 387 | #define get_errorlog_buffer()	kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL) | 
|  | 388 |  | 
|  | 389 | #else /* CONFIG_RTAS_ERROR_LOGGING */ | 
|  | 390 | #define __fetch_rtas_last_error(x)	NULL | 
|  | 391 | #define get_errorlog_buffer()		NULL | 
|  | 392 | #endif | 
|  | 393 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int rtas_call(int token, int nargs, int nret, int *outputs, ...) | 
|  | 395 | { | 
|  | 396 | va_list list; | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 397 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | unsigned long s; | 
|  | 399 | struct rtas_args *rtas_args; | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 400 | char *buff_copy = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | int ret; | 
|  | 402 |  | 
| Michael Ellerman | 24da3dd | 2006-06-23 18:20:10 +1000 | [diff] [blame] | 403 | if (!rtas.entry || token == RTAS_UNKNOWN_SERVICE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return -1; | 
|  | 405 |  | 
|  | 406 | /* Gotta do something different here, use global lock for now... */ | 
|  | 407 | spin_lock_irqsave(&rtas.lock, s); | 
|  | 408 | rtas_args = &rtas.args; | 
|  | 409 |  | 
|  | 410 | rtas_args->token = token; | 
|  | 411 | rtas_args->nargs = nargs; | 
|  | 412 | rtas_args->nret  = nret; | 
|  | 413 | rtas_args->rets  = (rtas_arg_t *)&(rtas_args->args[nargs]); | 
|  | 414 | va_start(list, outputs); | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 415 | for (i = 0; i < nargs; ++i) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | rtas_args->args[i] = va_arg(list, rtas_arg_t); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | va_end(list); | 
|  | 418 |  | 
|  | 419 | for (i = 0; i < nret; ++i) | 
|  | 420 | rtas_args->rets[i] = 0; | 
|  | 421 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | enter_rtas(__pa(rtas_args)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 |  | 
|  | 424 | /* A -1 return code indicates that the last command couldn't | 
|  | 425 | be completed due to a hardware error. */ | 
|  | 426 | if (rtas_args->rets[0] == -1) | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 427 | buff_copy = __fetch_rtas_last_error(NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 |  | 
|  | 429 | if (nret > 1 && outputs != NULL) | 
|  | 430 | for (i = 0; i < nret-1; ++i) | 
|  | 431 | outputs[i] = rtas_args->rets[i+1]; | 
|  | 432 | ret = (nret > 0)? rtas_args->rets[0]: 0; | 
|  | 433 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* Gotta do something different here, use global lock for now... */ | 
|  | 435 | spin_unlock_irqrestore(&rtas.lock, s); | 
|  | 436 |  | 
|  | 437 | if (buff_copy) { | 
|  | 438 | log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0); | 
|  | 439 | if (mem_init_done) | 
|  | 440 | kfree(buff_copy); | 
|  | 441 | } | 
|  | 442 | return ret; | 
|  | 443 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 444 | EXPORT_SYMBOL(rtas_call); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 446 | /* For RTAS_BUSY (-2), delay for 1 millisecond.  For an extended busy status | 
|  | 447 | * code of 990n, perform the hinted delay of 10^n (last digit) milliseconds. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | */ | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 449 | unsigned int rtas_busy_delay_time(int status) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 451 | int order; | 
|  | 452 | unsigned int ms = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 454 | if (status == RTAS_BUSY) { | 
|  | 455 | ms = 1; | 
|  | 456 | } else if (status >= 9900 && status <= 9905) { | 
|  | 457 | order = status - 9900; | 
|  | 458 | for (ms = 1; order > 0; order--) | 
|  | 459 | ms *= 10; | 
|  | 460 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 462 | return ms; | 
|  | 463 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 464 | EXPORT_SYMBOL(rtas_busy_delay_time); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 466 | /* For an RTAS busy status code, perform the hinted delay. */ | 
|  | 467 | unsigned int rtas_busy_delay(int status) | 
|  | 468 | { | 
|  | 469 | unsigned int ms; | 
|  | 470 |  | 
|  | 471 | might_sleep(); | 
|  | 472 | ms = rtas_busy_delay_time(status); | 
|  | 473 | if (ms) | 
|  | 474 | msleep(ms); | 
|  | 475 |  | 
|  | 476 | return ms; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 478 | EXPORT_SYMBOL(rtas_busy_delay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 |  | 
|  | 480 | int rtas_error_rc(int rtas_rc) | 
|  | 481 | { | 
|  | 482 | int rc; | 
|  | 483 |  | 
|  | 484 | switch (rtas_rc) { | 
|  | 485 | case -1: 		/* Hardware Error */ | 
|  | 486 | rc = -EIO; | 
|  | 487 | break; | 
|  | 488 | case -3:		/* Bad indicator/domain/etc */ | 
|  | 489 | rc = -EINVAL; | 
|  | 490 | break; | 
|  | 491 | case -9000:		/* Isolation error */ | 
|  | 492 | rc = -EFAULT; | 
|  | 493 | break; | 
|  | 494 | case -9001:		/* Outstanding TCE/PTE */ | 
|  | 495 | rc = -EEXIST; | 
|  | 496 | break; | 
|  | 497 | case -9002:		/* No usable slot */ | 
|  | 498 | rc = -ENODEV; | 
|  | 499 | break; | 
|  | 500 | default: | 
|  | 501 | printk(KERN_ERR "%s: unexpected RTAS error %d\n", | 
|  | 502 | __FUNCTION__, rtas_rc); | 
|  | 503 | rc = -ERANGE; | 
|  | 504 | break; | 
|  | 505 | } | 
|  | 506 | return rc; | 
|  | 507 | } | 
|  | 508 |  | 
|  | 509 | int rtas_get_power_level(int powerdomain, int *level) | 
|  | 510 | { | 
|  | 511 | int token = rtas_token("get-power-level"); | 
|  | 512 | int rc; | 
|  | 513 |  | 
|  | 514 | if (token == RTAS_UNKNOWN_SERVICE) | 
|  | 515 | return -ENOENT; | 
|  | 516 |  | 
|  | 517 | while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY) | 
|  | 518 | udelay(1); | 
|  | 519 |  | 
|  | 520 | if (rc < 0) | 
|  | 521 | return rtas_error_rc(rc); | 
|  | 522 | return rc; | 
|  | 523 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 524 | EXPORT_SYMBOL(rtas_get_power_level); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 |  | 
|  | 526 | int rtas_set_power_level(int powerdomain, int level, int *setlevel) | 
|  | 527 | { | 
|  | 528 | int token = rtas_token("set-power-level"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | int rc; | 
|  | 530 |  | 
|  | 531 | if (token == RTAS_UNKNOWN_SERVICE) | 
|  | 532 | return -ENOENT; | 
|  | 533 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 534 | do { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | rc = rtas_call(token, 2, 2, setlevel, powerdomain, level); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 536 | } while (rtas_busy_delay(rc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 |  | 
|  | 538 | if (rc < 0) | 
|  | 539 | return rtas_error_rc(rc); | 
|  | 540 | return rc; | 
|  | 541 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 542 | EXPORT_SYMBOL(rtas_set_power_level); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 |  | 
|  | 544 | int rtas_get_sensor(int sensor, int index, int *state) | 
|  | 545 | { | 
|  | 546 | int token = rtas_token("get-sensor-state"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | int rc; | 
|  | 548 |  | 
|  | 549 | if (token == RTAS_UNKNOWN_SERVICE) | 
|  | 550 | return -ENOENT; | 
|  | 551 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 552 | do { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | rc = rtas_call(token, 2, 2, state, sensor, index); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 554 | } while (rtas_busy_delay(rc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 |  | 
|  | 556 | if (rc < 0) | 
|  | 557 | return rtas_error_rc(rc); | 
|  | 558 | return rc; | 
|  | 559 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 560 | EXPORT_SYMBOL(rtas_get_sensor); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 |  | 
|  | 562 | int rtas_set_indicator(int indicator, int index, int new_value) | 
|  | 563 | { | 
|  | 564 | int token = rtas_token("set-indicator"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | int rc; | 
|  | 566 |  | 
|  | 567 | if (token == RTAS_UNKNOWN_SERVICE) | 
|  | 568 | return -ENOENT; | 
|  | 569 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 570 | do { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 572 | } while (rtas_busy_delay(rc)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 |  | 
|  | 574 | if (rc < 0) | 
|  | 575 | return rtas_error_rc(rc); | 
|  | 576 | return rc; | 
|  | 577 | } | 
| Michael Ellerman | ab3ab74 | 2006-06-23 18:20:11 +1000 | [diff] [blame] | 578 | EXPORT_SYMBOL(rtas_set_indicator); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 |  | 
| Haren Myneni | 81b73dd | 2006-07-27 14:29:00 -0700 | [diff] [blame] | 580 | /* | 
|  | 581 | * Ignoring RTAS extended delay | 
|  | 582 | */ | 
|  | 583 | int rtas_set_indicator_fast(int indicator, int index, int new_value) | 
|  | 584 | { | 
|  | 585 | int rc; | 
|  | 586 | int token = rtas_token("set-indicator"); | 
|  | 587 |  | 
|  | 588 | if (token == RTAS_UNKNOWN_SERVICE) | 
|  | 589 | return -ENOENT; | 
|  | 590 |  | 
|  | 591 | rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value); | 
|  | 592 |  | 
|  | 593 | WARN_ON(rc == -2 || (rc >= 9900 && rc <= 9905)); | 
|  | 594 |  | 
|  | 595 | if (rc < 0) | 
|  | 596 | return rtas_error_rc(rc); | 
|  | 597 |  | 
|  | 598 | return rc; | 
|  | 599 | } | 
|  | 600 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 601 | void rtas_restart(char *cmd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { | 
| Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 603 | if (rtas_flash_term_hook) | 
|  | 604 | rtas_flash_term_hook(SYS_RESTART); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | printk("RTAS system-reboot returned %d\n", | 
|  | 606 | rtas_call(rtas_token("system-reboot"), 0, 1, NULL)); | 
|  | 607 | for (;;); | 
|  | 608 | } | 
|  | 609 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 610 | void rtas_power_off(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { | 
| Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 612 | if (rtas_flash_term_hook) | 
|  | 613 | rtas_flash_term_hook(SYS_POWER_OFF); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | /* allow power on only with power button press */ | 
|  | 615 | printk("RTAS power-off returned %d\n", | 
|  | 616 | rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1)); | 
|  | 617 | for (;;); | 
|  | 618 | } | 
|  | 619 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 620 | void rtas_halt(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { | 
| Paul Mackerras | f4fcbbe | 2005-11-03 14:41:19 +1100 | [diff] [blame] | 622 | if (rtas_flash_term_hook) | 
|  | 623 | rtas_flash_term_hook(SYS_HALT); | 
|  | 624 | /* allow power on only with power button press */ | 
|  | 625 | printk("RTAS power-off returned %d\n", | 
|  | 626 | rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1)); | 
|  | 627 | for (;;); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } | 
|  | 629 |  | 
|  | 630 | /* Must be in the RMO region, so we place it here */ | 
|  | 631 | static char rtas_os_term_buf[2048]; | 
|  | 632 |  | 
|  | 633 | void rtas_os_term(char *str) | 
|  | 634 | { | 
|  | 635 | int status; | 
|  | 636 |  | 
| Olaf Hering | 39ed2fe | 2006-08-21 18:11:32 +0200 | [diff] [blame] | 637 | if (panic_timeout) | 
|  | 638 | return; | 
|  | 639 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term")) | 
|  | 641 | return; | 
|  | 642 |  | 
|  | 643 | snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str); | 
|  | 644 |  | 
|  | 645 | do { | 
|  | 646 | status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL, | 
|  | 647 | __pa(rtas_os_term_buf)); | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 648 | } while (rtas_busy_delay(status)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 |  | 
| John Rose | 507279d | 2006-06-05 16:31:48 -0500 | [diff] [blame] | 650 | if (status != 0) | 
|  | 651 | printk(KERN_EMERG "ibm,os-term call failed %d\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } | 
|  | 654 |  | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 655 | static int ibm_suspend_me_token = RTAS_UNKNOWN_SERVICE; | 
|  | 656 | #ifdef CONFIG_PPC_PSERIES | 
|  | 657 | static void rtas_percpu_suspend_me(void *info) | 
|  | 658 | { | 
| Dave C Boutcher | 82a4df7 | 2006-02-03 01:18:39 -0600 | [diff] [blame] | 659 | int i; | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 660 | long rc; | 
|  | 661 | long flags; | 
|  | 662 | struct rtas_suspend_me_data *data = | 
|  | 663 | (struct rtas_suspend_me_data *)info; | 
|  | 664 |  | 
|  | 665 | /* | 
|  | 666 | * We use "waiting" to indicate our state.  As long | 
|  | 667 | * as it is >0, we are still trying to all join up. | 
|  | 668 | * If it goes to 0, we have successfully joined up and | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 669 | * one thread got H_CONTINUE.  If any error happens, | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 670 | * we set it to <0. | 
|  | 671 | */ | 
|  | 672 | local_irq_save(flags); | 
|  | 673 | do { | 
|  | 674 | rc = plpar_hcall_norets(H_JOIN); | 
|  | 675 | smp_rmb(); | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 676 | } while (rc == H_SUCCESS && data->waiting > 0); | 
|  | 677 | if (rc == H_SUCCESS) | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 678 | goto out; | 
|  | 679 |  | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 680 | if (rc == H_CONTINUE) { | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 681 | data->waiting = 0; | 
| Dave C Boutcher | c4cb8ec | 2006-02-03 01:18:46 -0600 | [diff] [blame] | 682 | data->args->args[data->args->nargs] = | 
|  | 683 | rtas_call(ibm_suspend_me_token, 0, 1, NULL); | 
| KAMEZAWA Hiroyuki | 0e55195 | 2006-03-28 14:50:51 -0800 | [diff] [blame] | 684 | for_each_possible_cpu(i) | 
| Dave C Boutcher | 82a4df7 | 2006-02-03 01:18:39 -0600 | [diff] [blame] | 685 | plpar_hcall_norets(H_PROD,i); | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 686 | } else { | 
|  | 687 | data->waiting = -EBUSY; | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 688 | printk(KERN_ERR "Error on H_JOIN hypervisor call\n"); | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 689 | } | 
|  | 690 |  | 
|  | 691 | out: | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 692 | local_irq_restore(flags); | 
|  | 693 | return; | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | static int rtas_ibm_suspend_me(struct rtas_args *args) | 
|  | 697 | { | 
|  | 698 | int i; | 
| Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 699 | long state; | 
|  | 700 | long rc; | 
| Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 701 | unsigned long retbuf[PLPAR_HCALL_BUFSIZE]; | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 702 | struct rtas_suspend_me_data data; | 
|  | 703 |  | 
| Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 704 | /* Make sure the state is valid */ | 
| Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 705 | rc = plpar_hcall(H_VASI_STATE, retbuf, | 
|  | 706 | ((u64)args->args[0] << 32) | args->args[1]); | 
|  | 707 |  | 
|  | 708 | state = retbuf[0]; | 
| Dave C Boutcher | 368a6ba | 2006-06-12 19:49:20 -0500 | [diff] [blame] | 709 |  | 
|  | 710 | if (rc) { | 
|  | 711 | printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc); | 
|  | 712 | return rc; | 
|  | 713 | } else if (state == H_VASI_ENABLED) { | 
|  | 714 | args->args[args->nargs] = RTAS_NOT_SUSPENDABLE; | 
|  | 715 | return 0; | 
|  | 716 | } else if (state != H_VASI_SUSPENDING) { | 
|  | 717 | printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n", | 
|  | 718 | state); | 
|  | 719 | args->args[args->nargs] = -1; | 
|  | 720 | return 0; | 
|  | 721 | } | 
|  | 722 |  | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 723 | data.waiting = 1; | 
|  | 724 | data.args = args; | 
|  | 725 |  | 
|  | 726 | /* Call function on all CPUs.  One of us will make the | 
|  | 727 | * rtas call | 
|  | 728 | */ | 
|  | 729 | if (on_each_cpu(rtas_percpu_suspend_me, &data, 1, 0)) | 
|  | 730 | data.waiting = -EINVAL; | 
|  | 731 |  | 
|  | 732 | if (data.waiting != 0) | 
|  | 733 | printk(KERN_ERR "Error doing global join\n"); | 
|  | 734 |  | 
|  | 735 | /* Prod each CPU.  This won't hurt, and will wake | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 736 | * anyone we successfully put to sleep with H_JOIN. | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 737 | */ | 
| KAMEZAWA Hiroyuki | 0e55195 | 2006-03-28 14:50:51 -0800 | [diff] [blame] | 738 | for_each_possible_cpu(i) | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 739 | plpar_hcall_norets(H_PROD, i); | 
|  | 740 |  | 
|  | 741 | return data.waiting; | 
|  | 742 | } | 
|  | 743 | #else /* CONFIG_PPC_PSERIES */ | 
|  | 744 | static int rtas_ibm_suspend_me(struct rtas_args *args) | 
|  | 745 | { | 
|  | 746 | return -ENOSYS; | 
|  | 747 | } | 
|  | 748 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 |  | 
|  | 750 | asmlinkage int ppc_rtas(struct rtas_args __user *uargs) | 
|  | 751 | { | 
|  | 752 | struct rtas_args args; | 
|  | 753 | unsigned long flags; | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 754 | char *buff_copy, *errbuf = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | int nargs; | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 756 | int rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 |  | 
|  | 758 | if (!capable(CAP_SYS_ADMIN)) | 
|  | 759 | return -EPERM; | 
|  | 760 |  | 
|  | 761 | if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0) | 
|  | 762 | return -EFAULT; | 
|  | 763 |  | 
|  | 764 | nargs = args.nargs; | 
|  | 765 | if (nargs > ARRAY_SIZE(args.args) | 
|  | 766 | || args.nret > ARRAY_SIZE(args.args) | 
|  | 767 | || nargs + args.nret > ARRAY_SIZE(args.args)) | 
|  | 768 | return -EINVAL; | 
|  | 769 |  | 
|  | 770 | /* Copy in args. */ | 
|  | 771 | if (copy_from_user(args.args, uargs->args, | 
|  | 772 | nargs * sizeof(rtas_arg_t)) != 0) | 
|  | 773 | return -EFAULT; | 
|  | 774 |  | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 775 | if (args.token == RTAS_UNKNOWN_SERVICE) | 
|  | 776 | return -EINVAL; | 
|  | 777 |  | 
|  | 778 | /* Need to handle ibm,suspend_me call specially */ | 
|  | 779 | if (args.token == ibm_suspend_me_token) { | 
|  | 780 | rc = rtas_ibm_suspend_me(&args); | 
|  | 781 | if (rc) | 
|  | 782 | return rc; | 
|  | 783 | goto copy_return; | 
|  | 784 | } | 
|  | 785 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 786 | buff_copy = get_errorlog_buffer(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 |  | 
|  | 788 | spin_lock_irqsave(&rtas.lock, flags); | 
|  | 789 |  | 
|  | 790 | rtas.args = args; | 
|  | 791 | enter_rtas(__pa(&rtas.args)); | 
|  | 792 | args = rtas.args; | 
|  | 793 |  | 
|  | 794 | args.rets = &args.args[nargs]; | 
|  | 795 |  | 
|  | 796 | /* A -1 return code indicates that the last command couldn't | 
|  | 797 | be completed due to a hardware error. */ | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 798 | if (args.rets[0] == -1) | 
|  | 799 | errbuf = __fetch_rtas_last_error(buff_copy); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 |  | 
|  | 801 | spin_unlock_irqrestore(&rtas.lock, flags); | 
|  | 802 |  | 
|  | 803 | if (buff_copy) { | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 804 | if (errbuf) | 
|  | 805 | log_error(errbuf, ERR_TYPE_RTAS_LOG, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | kfree(buff_copy); | 
|  | 807 | } | 
|  | 808 |  | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 809 | copy_return: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | /* Copy out args. */ | 
|  | 811 | if (copy_to_user(uargs->args + nargs, | 
|  | 812 | args.args + nargs, | 
|  | 813 | args.nret * sizeof(rtas_arg_t)) != 0) | 
|  | 814 | return -EFAULT; | 
|  | 815 |  | 
|  | 816 | return 0; | 
|  | 817 | } | 
|  | 818 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | /* | 
| Adrian Bunk | 943ffb5 | 2006-01-10 00:10:13 +0100 | [diff] [blame] | 820 | * Call early during boot, before mem init or bootmem, to retrieve the RTAS | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | * informations from the device-tree and allocate the RMO buffer for userland | 
|  | 822 | * accesses. | 
|  | 823 | */ | 
|  | 824 | void __init rtas_initialize(void) | 
|  | 825 | { | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 826 | unsigned long rtas_region = RTAS_INSTANTIATE_MAX; | 
|  | 827 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | /* Get RTAS dev node and fill up our "rtas" structure with infos | 
|  | 829 | * about it. | 
|  | 830 | */ | 
|  | 831 | rtas.dev = of_find_node_by_name(NULL, "rtas"); | 
|  | 832 | if (rtas.dev) { | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 833 | const u32 *basep, *entryp, *sizep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 |  | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 835 | basep = get_property(rtas.dev, "linux,rtas-base", NULL); | 
|  | 836 | sizep = get_property(rtas.dev, "rtas-size", NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | if (basep != NULL && sizep != NULL) { | 
|  | 838 | rtas.base = *basep; | 
|  | 839 | rtas.size = *sizep; | 
| Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 840 | entryp = get_property(rtas.dev, | 
|  | 841 | "linux,rtas-entry", NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | if (entryp == NULL) /* Ugh */ | 
|  | 843 | rtas.entry = rtas.base; | 
|  | 844 | else | 
|  | 845 | rtas.entry = *entryp; | 
|  | 846 | } else | 
|  | 847 | rtas.dev = NULL; | 
|  | 848 | } | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 849 | if (!rtas.dev) | 
|  | 850 | return; | 
|  | 851 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | /* If RTAS was found, allocate the RMO buffer for it and look for | 
|  | 853 | * the stop-self token if any | 
|  | 854 | */ | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 855 | #ifdef CONFIG_PPC64 | 
| Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 856 | if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) { | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 857 | rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX); | 
| Dave C Boutcher | 91dc182 | 2006-01-13 18:39:24 -0600 | [diff] [blame] | 858 | ibm_suspend_me_token = rtas_token("ibm,suspend-me"); | 
|  | 859 | } | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 860 | #endif | 
|  | 861 | rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 |  | 
| Paul Mackerras | 033ef33 | 2005-10-26 17:05:24 +1000 | [diff] [blame] | 863 | #ifdef CONFIG_RTAS_ERROR_LOGGING | 
|  | 864 | rtas_last_error_token = rtas_token("rtas-last-error"); | 
|  | 865 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } | 
| Michael Ellerman | 458148c | 2006-06-23 18:20:13 +1000 | [diff] [blame] | 867 |  | 
|  | 868 | int __init early_init_dt_scan_rtas(unsigned long node, | 
|  | 869 | const char *uname, int depth, void *data) | 
|  | 870 | { | 
|  | 871 | u32 *basep, *entryp, *sizep; | 
|  | 872 |  | 
|  | 873 | if (depth != 1 || strcmp(uname, "rtas") != 0) | 
|  | 874 | return 0; | 
|  | 875 |  | 
|  | 876 | basep  = of_get_flat_dt_prop(node, "linux,rtas-base", NULL); | 
|  | 877 | entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL); | 
|  | 878 | sizep  = of_get_flat_dt_prop(node, "rtas-size", NULL); | 
|  | 879 |  | 
|  | 880 | if (basep && entryp && sizep) { | 
|  | 881 | rtas.base = *basep; | 
|  | 882 | rtas.entry = *entryp; | 
|  | 883 | rtas.size = *sizep; | 
|  | 884 | } | 
|  | 885 |  | 
| Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 886 | #ifdef CONFIG_UDBG_RTAS_CONSOLE | 
|  | 887 | basep = of_get_flat_dt_prop(node, "put-term-char", NULL); | 
|  | 888 | if (basep) | 
|  | 889 | rtas_putchar_token = *basep; | 
|  | 890 |  | 
|  | 891 | basep = of_get_flat_dt_prop(node, "get-term-char", NULL); | 
|  | 892 | if (basep) | 
|  | 893 | rtas_getchar_token = *basep; | 
| Michael Neuling | 9a2ded5 | 2006-08-16 23:12:14 -0500 | [diff] [blame] | 894 |  | 
|  | 895 | if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE && | 
|  | 896 | rtas_getchar_token != RTAS_UNKNOWN_SERVICE) | 
|  | 897 | udbg_init_rtas_console(); | 
|  | 898 |  | 
| Michael Ellerman | cc46bb9 | 2006-06-23 18:20:16 +1000 | [diff] [blame] | 899 | #endif | 
|  | 900 |  | 
| Michael Ellerman | 458148c | 2006-06-23 18:20:13 +1000 | [diff] [blame] | 901 | /* break now */ | 
|  | 902 | return 1; | 
|  | 903 | } |