rtas.c 21 KB

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