rtas.c 29 KB

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