rtas.c 22 KB

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