rtas.c 23 KB

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