rtas.c 24 KB

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