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