rtas.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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. switch (state) {
  727. case DOWN:
  728. cpuret = cpu_down(cpu);
  729. break;
  730. case UP:
  731. cpuret = cpu_up(cpu);
  732. break;
  733. }
  734. if (cpuret) {
  735. pr_debug("%s: cpu_%s for cpu#%d returned %d.\n",
  736. __func__,
  737. ((state == UP) ? "up" : "down"),
  738. cpu, cpuret);
  739. if (!ret)
  740. ret = cpuret;
  741. if (state == UP) {
  742. /* clear bits for unchanged cpus, return */
  743. cpumask_shift_right(cpus, cpus, cpu);
  744. cpumask_shift_left(cpus, cpus, cpu);
  745. break;
  746. } else {
  747. /* clear bit for unchanged cpu, continue */
  748. cpumask_clear_cpu(cpu, cpus);
  749. }
  750. }
  751. }
  752. return ret;
  753. }
  754. #endif
  755. int rtas_online_cpus_mask(cpumask_var_t cpus)
  756. {
  757. int ret;
  758. ret = rtas_cpu_state_change_mask(UP, cpus);
  759. if (ret) {
  760. cpumask_var_t tmp_mask;
  761. if (!alloc_cpumask_var(&tmp_mask, GFP_KERNEL))
  762. return ret;
  763. /* Use tmp_mask to preserve cpus mask from first failure */
  764. cpumask_copy(tmp_mask, cpus);
  765. rtas_offline_cpus_mask(tmp_mask);
  766. free_cpumask_var(tmp_mask);
  767. }
  768. return ret;
  769. }
  770. EXPORT_SYMBOL(rtas_online_cpus_mask);
  771. int rtas_offline_cpus_mask(cpumask_var_t cpus)
  772. {
  773. return rtas_cpu_state_change_mask(DOWN, cpus);
  774. }
  775. EXPORT_SYMBOL(rtas_offline_cpus_mask);
  776. int rtas_ibm_suspend_me(u64 handle)
  777. {
  778. long state;
  779. long rc;
  780. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  781. struct rtas_suspend_me_data data;
  782. DECLARE_COMPLETION_ONSTACK(done);
  783. cpumask_var_t offline_mask;
  784. int cpuret;
  785. if (!rtas_service_present("ibm,suspend-me"))
  786. return -ENOSYS;
  787. /* Make sure the state is valid */
  788. rc = plpar_hcall(H_VASI_STATE, retbuf, handle);
  789. state = retbuf[0];
  790. if (rc) {
  791. printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned %ld\n",rc);
  792. return rc;
  793. } else if (state == H_VASI_ENABLED) {
  794. return -EAGAIN;
  795. } else if (state != H_VASI_SUSPENDING) {
  796. printk(KERN_ERR "rtas_ibm_suspend_me: vasi_state returned state %ld\n",
  797. state);
  798. return -EIO;
  799. }
  800. if (!alloc_cpumask_var(&offline_mask, GFP_KERNEL))
  801. return -ENOMEM;
  802. atomic_set(&data.working, 0);
  803. atomic_set(&data.done, 0);
  804. atomic_set(&data.error, 0);
  805. data.token = rtas_token("ibm,suspend-me");
  806. data.complete = &done;
  807. /* All present CPUs must be online */
  808. cpumask_andnot(offline_mask, cpu_present_mask, cpu_online_mask);
  809. cpuret = rtas_online_cpus_mask(offline_mask);
  810. if (cpuret) {
  811. pr_err("%s: Could not bring present CPUs online.\n", __func__);
  812. atomic_set(&data.error, cpuret);
  813. goto out;
  814. }
  815. cpu_hotplug_disable();
  816. /* Check if we raced with a CPU-Offline Operation */
  817. if (unlikely(!cpumask_equal(cpu_present_mask, cpu_online_mask))) {
  818. pr_err("%s: Raced against a concurrent CPU-Offline\n",
  819. __func__);
  820. atomic_set(&data.error, -EBUSY);
  821. goto out_hotplug_enable;
  822. }
  823. /* Call function on all CPUs. One of us will make the
  824. * rtas call
  825. */
  826. if (on_each_cpu(rtas_percpu_suspend_me, &data, 0))
  827. atomic_set(&data.error, -EINVAL);
  828. wait_for_completion(&done);
  829. if (atomic_read(&data.error) != 0)
  830. printk(KERN_ERR "Error doing global join\n");
  831. out_hotplug_enable:
  832. cpu_hotplug_enable();
  833. /* Take down CPUs not online prior to suspend */
  834. cpuret = rtas_offline_cpus_mask(offline_mask);
  835. if (cpuret)
  836. pr_warn("%s: Could not restore CPUs to offline state.\n",
  837. __func__);
  838. out:
  839. free_cpumask_var(offline_mask);
  840. return atomic_read(&data.error);
  841. }
  842. #else /* CONFIG_PPC_PSERIES */
  843. int rtas_ibm_suspend_me(u64 handle)
  844. {
  845. return -ENOSYS;
  846. }
  847. #endif
  848. /**
  849. * Find a specific pseries error log in an RTAS extended event log.
  850. * @log: RTAS error/event log
  851. * @section_id: two character section identifier
  852. *
  853. * Returns a pointer to the specified errorlog or NULL if not found.
  854. */
  855. struct pseries_errorlog *get_pseries_errorlog(struct rtas_error_log *log,
  856. uint16_t section_id)
  857. {
  858. struct rtas_ext_event_log_v6 *ext_log =
  859. (struct rtas_ext_event_log_v6 *)log->buffer;
  860. struct pseries_errorlog *sect;
  861. unsigned char *p, *log_end;
  862. uint32_t ext_log_length = rtas_error_extended_log_length(log);
  863. uint8_t log_format = rtas_ext_event_log_format(ext_log);
  864. uint32_t company_id = rtas_ext_event_company_id(ext_log);
  865. /* Check that we understand the format */
  866. if (ext_log_length < sizeof(struct rtas_ext_event_log_v6) ||
  867. log_format != RTAS_V6EXT_LOG_FORMAT_EVENT_LOG ||
  868. company_id != RTAS_V6EXT_COMPANY_ID_IBM)
  869. return NULL;
  870. log_end = log->buffer + ext_log_length;
  871. p = ext_log->vendor_log;
  872. while (p < log_end) {
  873. sect = (struct pseries_errorlog *)p;
  874. if (pseries_errorlog_id(sect) == section_id)
  875. return sect;
  876. p += pseries_errorlog_length(sect);
  877. }
  878. return NULL;
  879. }
  880. /* We assume to be passed big endian arguments */
  881. SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
  882. {
  883. struct rtas_args args;
  884. unsigned long flags;
  885. char *buff_copy, *errbuf = NULL;
  886. int nargs, nret, token;
  887. if (!capable(CAP_SYS_ADMIN))
  888. return -EPERM;
  889. if (!rtas.entry)
  890. return -EINVAL;
  891. if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
  892. return -EFAULT;
  893. nargs = be32_to_cpu(args.nargs);
  894. nret = be32_to_cpu(args.nret);
  895. token = be32_to_cpu(args.token);
  896. if (nargs >= ARRAY_SIZE(args.args)
  897. || nret > ARRAY_SIZE(args.args)
  898. || nargs + nret > ARRAY_SIZE(args.args))
  899. return -EINVAL;
  900. /* Copy in args. */
  901. if (copy_from_user(args.args, uargs->args,
  902. nargs * sizeof(rtas_arg_t)) != 0)
  903. return -EFAULT;
  904. if (token == RTAS_UNKNOWN_SERVICE)
  905. return -EINVAL;
  906. args.rets = &args.args[nargs];
  907. memset(args.rets, 0, nret * sizeof(rtas_arg_t));
  908. /* Need to handle ibm,suspend_me call specially */
  909. if (token == ibm_suspend_me_token) {
  910. /*
  911. * rtas_ibm_suspend_me assumes the streamid handle is in cpu
  912. * endian, or at least the hcall within it requires it.
  913. */
  914. int rc = 0;
  915. u64 handle = ((u64)be32_to_cpu(args.args[0]) << 32)
  916. | be32_to_cpu(args.args[1]);
  917. rc = rtas_ibm_suspend_me(handle);
  918. if (rc == -EAGAIN)
  919. args.rets[0] = cpu_to_be32(RTAS_NOT_SUSPENDABLE);
  920. else if (rc == -EIO)
  921. args.rets[0] = cpu_to_be32(-1);
  922. else if (rc)
  923. return rc;
  924. goto copy_return;
  925. }
  926. buff_copy = get_errorlog_buffer();
  927. flags = lock_rtas();
  928. rtas.args = args;
  929. enter_rtas(__pa(&rtas.args));
  930. args = rtas.args;
  931. /* A -1 return code indicates that the last command couldn't
  932. be completed due to a hardware error. */
  933. if (be32_to_cpu(args.rets[0]) == -1)
  934. errbuf = __fetch_rtas_last_error(buff_copy);
  935. unlock_rtas(flags);
  936. if (buff_copy) {
  937. if (errbuf)
  938. log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
  939. kfree(buff_copy);
  940. }
  941. copy_return:
  942. /* Copy out args. */
  943. if (copy_to_user(uargs->args + nargs,
  944. args.args + nargs,
  945. nret * sizeof(rtas_arg_t)) != 0)
  946. return -EFAULT;
  947. return 0;
  948. }
  949. /*
  950. * Call early during boot, before mem init, to retrieve the RTAS
  951. * information from the device-tree and allocate the RMO buffer for userland
  952. * accesses.
  953. */
  954. void __init rtas_initialize(void)
  955. {
  956. unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
  957. u32 base, size, entry;
  958. int no_base, no_size, no_entry;
  959. /* Get RTAS dev node and fill up our "rtas" structure with infos
  960. * about it.
  961. */
  962. rtas.dev = of_find_node_by_name(NULL, "rtas");
  963. if (!rtas.dev)
  964. return;
  965. no_base = of_property_read_u32(rtas.dev, "linux,rtas-base", &base);
  966. no_size = of_property_read_u32(rtas.dev, "rtas-size", &size);
  967. if (no_base || no_size) {
  968. of_node_put(rtas.dev);
  969. rtas.dev = NULL;
  970. return;
  971. }
  972. rtas.base = base;
  973. rtas.size = size;
  974. no_entry = of_property_read_u32(rtas.dev, "linux,rtas-entry", &entry);
  975. rtas.entry = no_entry ? rtas.base : entry;
  976. /* If RTAS was found, allocate the RMO buffer for it and look for
  977. * the stop-self token if any
  978. */
  979. #ifdef CONFIG_PPC64
  980. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  981. rtas_region = min(ppc64_rma_size, RTAS_INSTANTIATE_MAX);
  982. ibm_suspend_me_token = rtas_token("ibm,suspend-me");
  983. }
  984. #endif
  985. rtas_rmo_buf = memblock_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
  986. #ifdef CONFIG_RTAS_ERROR_LOGGING
  987. rtas_last_error_token = rtas_token("rtas-last-error");
  988. #endif
  989. }
  990. int __init early_init_dt_scan_rtas(unsigned long node,
  991. const char *uname, int depth, void *data)
  992. {
  993. const u32 *basep, *entryp, *sizep;
  994. if (depth != 1 || strcmp(uname, "rtas") != 0)
  995. return 0;
  996. basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
  997. entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  998. sizep = of_get_flat_dt_prop(node, "rtas-size", NULL);
  999. if (basep && entryp && sizep) {
  1000. rtas.base = *basep;
  1001. rtas.entry = *entryp;
  1002. rtas.size = *sizep;
  1003. }
  1004. #ifdef CONFIG_UDBG_RTAS_CONSOLE
  1005. basep = of_get_flat_dt_prop(node, "put-term-char", NULL);
  1006. if (basep)
  1007. rtas_putchar_token = *basep;
  1008. basep = of_get_flat_dt_prop(node, "get-term-char", NULL);
  1009. if (basep)
  1010. rtas_getchar_token = *basep;
  1011. if (rtas_putchar_token != RTAS_UNKNOWN_SERVICE &&
  1012. rtas_getchar_token != RTAS_UNKNOWN_SERVICE)
  1013. udbg_init_rtas_console();
  1014. #endif
  1015. /* break now */
  1016. return 1;
  1017. }
  1018. static arch_spinlock_t timebase_lock;
  1019. static u64 timebase = 0;
  1020. void rtas_give_timebase(void)
  1021. {
  1022. unsigned long flags;
  1023. local_irq_save(flags);
  1024. hard_irq_disable();
  1025. arch_spin_lock(&timebase_lock);
  1026. rtas_call(rtas_token("freeze-time-base"), 0, 1, NULL);
  1027. timebase = get_tb();
  1028. arch_spin_unlock(&timebase_lock);
  1029. while (timebase)
  1030. barrier();
  1031. rtas_call(rtas_token("thaw-time-base"), 0, 1, NULL);
  1032. local_irq_restore(flags);
  1033. }
  1034. void rtas_take_timebase(void)
  1035. {
  1036. while (!timebase)
  1037. barrier();
  1038. arch_spin_lock(&timebase_lock);
  1039. set_tb(timebase >> 32, timebase & 0xffffffff);
  1040. timebase = 0;
  1041. arch_spin_unlock(&timebase_lock);
  1042. }