rtas.c 34 KB

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