rtas.c 29 KB

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