rtas.c 22 KB

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