rtas.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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/export.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/delay.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/udbg.h>
  37. #include <asm/syscalls.h>
  38. #include <asm/smp.h>
  39. #include <linux/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 && need_resched())
  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. printk(KERN_DEBUG "calling ibm,suspend-me on cpu %i\n", smp_processor_id());
  599. while (rc == H_MULTI_THREADS_ACTIVE && !atomic_read(&data->done) &&
  600. !atomic_read(&data->error))
  601. rc = rtas_call(data->token, 0, 1, NULL);
  602. if (rc || atomic_read(&data->error)) {
  603. printk(KERN_DEBUG "ibm,suspend-me returned %d\n", rc);
  604. slb_set_size(slb_size);
  605. }
  606. if (atomic_read(&data->error))
  607. rc = atomic_read(&data->error);
  608. atomic_set(&data->error, rc);
  609. pSeries_coalesce_init();
  610. if (wake_when_done) {
  611. atomic_set(&data->done, 1);
  612. for_each_online_cpu(cpu)
  613. plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
  614. }
  615. if (atomic_dec_return(&data->working) == 0)
  616. complete(data->complete);
  617. return rc;
  618. }
  619. int rtas_suspend_last_cpu(struct rtas_suspend_me_data *data)
  620. {
  621. atomic_inc(&data->working);
  622. return __rtas_suspend_last_cpu(data, 0);
  623. }
  624. static int __rtas_suspend_cpu(struct rtas_suspend_me_data *data, int wake_when_done)
  625. {
  626. long rc = H_SUCCESS;
  627. unsigned long msr_save;
  628. int cpu;
  629. atomic_inc(&data->working);
  630. /* really need to ensure MSR.EE is off for H_JOIN */
  631. msr_save = mfmsr();
  632. mtmsr(msr_save & ~(MSR_EE));
  633. while (rc == H_SUCCESS && !atomic_read(&data->done) && !atomic_read(&data->error))
  634. rc = plpar_hcall_norets(H_JOIN);
  635. mtmsr(msr_save);
  636. if (rc == H_SUCCESS) {
  637. /* This cpu was prodded and the suspend is complete. */
  638. goto out;
  639. } else if (rc == H_CONTINUE) {
  640. /* All other cpus are in H_JOIN, this cpu does
  641. * the suspend.
  642. */
  643. return __rtas_suspend_last_cpu(data, wake_when_done);
  644. } else {
  645. printk(KERN_ERR "H_JOIN on cpu %i failed with rc = %ld\n",
  646. smp_processor_id(), rc);
  647. atomic_set(&data->error, rc);
  648. }
  649. if (wake_when_done) {
  650. atomic_set(&data->done, 1);
  651. /* This cpu did the suspend or got an error; in either case,
  652. * we need to prod all other other cpus out of join state.
  653. * Extra prods are harmless.
  654. */
  655. for_each_online_cpu(cpu)
  656. plpar_hcall_norets(H_PROD, get_hard_smp_processor_id(cpu));
  657. }
  658. out:
  659. if (atomic_dec_return(&data->working) == 0)
  660. complete(data->complete);
  661. return rc;
  662. }
  663. int rtas_suspend_cpu(struct rtas_suspend_me_data *data)
  664. {
  665. return __rtas_suspend_cpu(data, 0);
  666. }
  667. static void rtas_percpu_suspend_me(void *info)
  668. {
  669. __rtas_suspend_cpu((struct rtas_suspend_me_data *)info, 1);
  670. }
  671. int rtas_ibm_suspend_me(struct rtas_args *args)
  672. {
  673. long state;
  674. long rc;
  675. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  676. struct rtas_suspend_me_data data;
  677. DECLARE_COMPLETION_ONSTACK(done);
  678. if (!rtas_service_present("ibm,suspend-me"))
  679. return -ENOSYS;
  680. /* Make sure the state is valid */
  681. rc = plpar_hcall(H_VASI_STATE, retbuf,
  682. ((u64)args->args[0] << 32) | args->args[1]);
  683. state = retbuf[0];
  684. if (rc) {
  685. printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
  686. return rc;
  687. } else if (state == H_VASI_ENABLED) {
  688. args->args[args->nargs] = RTAS_NOT_SUSPENDABLE;
  689. return 0;
  690. } else if (state != H_VASI_SUSPENDING) {
  691. printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
  692. state);
  693. args->args[args->nargs] = -1;
  694. return 0;
  695. }
  696. atomic_set(&data.working, 0);
  697. atomic_set(&data.done, 0);
  698. atomic_set(&data.error, 0);
  699. data.token = rtas_token("ibm,suspend-me");
  700. data.complete = &done;
  701. stop_topology_update();
  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. start_topology_update();
  711. return atomic_read(&data.error);
  712. }
  713. #else /* CONFIG_PPC_PSERIES */
  714. int rtas_ibm_suspend_me(struct rtas_args *args)
  715. {
  716. return -ENOSYS;
  717. }
  718. #endif
  719. /**
  720. * Find a specific pseries error log in an RTAS extended event log.
  721. * @log: RTAS error/event log
  722. * @section_id: two character section identifier
  723. *
  724. * Returns a pointer to the specified errorlog or NULL if not found.
  725. */
  726. struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
  727. uint16_t section_id)
  728. {
  729. struct rtas_ext_event_log_v6 *ext_log =
  730. (struct rtas_ext_event_log_v6 *)log->buffer;
  731. struct pseries_errorlog *sect;
  732. unsigned char *p, *log_end;
  733. /* Check that we understand the format */
  734. if (log->extended_log_length < sizeof(struct rtas_ext_event_log_v6) ||
  735. ext_log->log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
  736. ext_log->company_id != RTAS_V6EXT_COMPANY_ID_IBM)
  737. return NULL;
  738. log_end = log->buffer + log->extended_log_length;
  739. p = ext_log->vendor_log;
  740. while (p < log_end) {
  741. sect = (struct pseries_errorlog *)p;
  742. if (sect->id == section_id)
  743. return sect;
  744. p += sect->length;
  745. }
  746. return NULL;
  747. }
  748. asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
  749. {
  750. struct rtas_args args;
  751. unsigned long flags;
  752. char *buff_copy, *errbuf = NULL;
  753. int nargs;
  754. int rc;
  755. if (!capable(CAP_SYS_ADMIN))
  756. return -EPERM;
  757. if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
  758. return -EFAULT;
  759. nargs = args.nargs;
  760. if (nargs > ARRAY_SIZE(args.args)
  761. || args.nret > ARRAY_SIZE(args.args)
  762. || nargs + args.nret > ARRAY_SIZE(args.args))
  763. return -EINVAL;
  764. /* Copy in args. */
  765. if (copy_from_user(args.args, uargs->args,
  766. nargs * sizeof(rtas_arg_t)) != 0)
  767. return -EFAULT;
  768. if (args.token == RTAS_UNKNOWN_SERVICE)
  769. return -EINVAL;
  770. args.rets = &args.args[nargs];
  771. memset(args.rets, 0, args.nret * sizeof(rtas_arg_t));
  772. /* Need to handle ibm,suspend_me call specially */
  773. if (args.token == ibm_suspend_me_token) {
  774. rc = rtas_ibm_suspend_me(&args);
  775. if (rc)
  776. return rc;
  777. goto copy_return;
  778. }
  779. buff_copy = get_errorlog_buffer();
  780. flags = lock_rtas();
  781. rtas.args = args;
  782. enter_rtas(__pa(&rtas.args));
  783. args = rtas.args;
  784. /* A -1 return code indicates that the last command couldn't
  785. be completed due to a hardware error. */
  786. if (args.rets[0] == -1)
  787. errbuf = __fetch_rtas_last_error(buff_copy);
  788. unlock_rtas(flags);
  789. if (buff_copy) {
  790. if (errbuf)
  791. log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
  792. kfree(buff_copy);
  793. }
  794. copy_return:
  795. /* Copy out args. */
  796. if (copy_to_user(uargs->args + nargs,
  797. args.args + nargs,
  798. args.nret * sizeof(rtas_arg_t)) != 0)
  799. return -EFAULT;
  800. return 0;
  801. }
  802. /*
  803. * Call early during boot, before mem init or bootmem, to retrieve the RTAS
  804. * informations from the device-tree and allocate the RMO buffer for userland
  805. * accesses.
  806. */
  807. void __init rtas_initialize(void)
  808. {
  809. unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
  810. /* Get RTAS dev node and fill up our "rtas" structure with infos
  811. * about it.
  812. */
  813. rtas.dev = of_find_node_by_name(NULL, "rtas");
  814. if (rtas.dev) {
  815. const u32 *basep, *entryp, *sizep;
  816. basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
  817. sizep = of_get_property(rtas.dev, "rtas-size", NULL);
  818. if (basep != NULL && sizep != NULL) {
  819. rtas.base = *basep;
  820. rtas.size = *sizep;
  821. entryp = of_get_property(rtas.dev,
  822. "linux,rtas-entry", NULL);
  823. if (entryp == NULL) /* Ugh */
  824. rtas.entry = rtas.base;
  825. else
  826. rtas.entry = *entryp;
  827. } else
  828. rtas.dev = NULL;
  829. }
  830. if (!rtas.dev)
  831. return;
  832. /* If RTAS was found, allocate the RMO buffer for it and look for
  833. * the stop-self token if any
  834. */
  835. #ifdef CONFIG_PPC64
  836. if (machine_is(pseries) && firmware_has_feature(FW_FEATURE_LPAR)) {
  837. rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
  838. ibm_suspend_me_token = rtas_token("ibm,suspend-me");
  839. }
  840. #endif
  841. rtas_rmo_buf = memblock_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
  842. #ifdef CONFIG_RTAS_ERROR_LOGGING
  843. rtas_last_error_token = rtas_token("rtas-last-error");
  844. #endif
  845. }
  846. int __init early_init_dt_scan_rtas(unsigned long node,
  847. const char *uname, int depth, void *data)
  848. {
  849. u32 *basep, *entryp, *sizep;
  850. if (depth != 1 || strcmp(uname, "rtas") != 0)
  851. return 0;
  852. basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
  853. entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  854. sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
  855. if (basep && entryp && sizep) {
  856. rtas.base = *basep;
  857. rtas.entry = *entryp;
  858. rtas.size = *sizep;
  859. }
  860. #ifdef CONFIG_UDBG_RTAS_CONSOLE
  861. basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
  862. if (basep)
  863. rtas_putchar_token = *basep;
  864. basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
  865. if (basep)
  866. rtas_getchar_token = *basep;
  867. if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
  868. rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
  869. udbg_init_rtas_console();
  870. #endif
  871. /* break now */
  872. return 1;
  873. }
  874. static arch_spinlock_t timebase_lock;
  875. static u64 timebase = 0;
  876. void __cpuinit rtas_give_timebase(void)
  877. {
  878. unsigned long flags;
  879. local_irq_save(flags);
  880. hard_irq_disable();
  881. arch_spin_lock(&timebase_lock);
  882. rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
  883. timebase = get_tb();
  884. arch_spin_unlock(&timebase_lock);
  885. while (timebase)
  886. barrier();
  887. rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
  888. local_irq_restore(flags);
  889. }
  890. void __cpuinit rtas_take_timebase(void)
  891. {
  892. while (!timebase)
  893. barrier();
  894. arch_spin_lock(&timebase_lock);
  895. set_tb(timebase >> 32, timebase & 0xffffffff);
  896. timebase = 0;
  897. arch_spin_unlock(&timebase_lock);
  898. }