rtas.c 27 KB

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