rtas.c 24 KB

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