rtas.c 23 KB

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