rtas.c 19 KB

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