rtas.c 24 KB

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