rtas.c 29 KB

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