rtas.c 21 KB

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