rtas.c 21 KB

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