rtas.c 19 KB

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