rtas.c 33 KB

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