rtas.c 23 KB

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