rtas.c 32 KB

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