rtas.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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/module.h>
  18. #include <linux/init.h>
  19. #include <asm/prom.h>
  20. #include <asm/rtas.h>
  21. #include <asm/semaphore.h>
  22. #include <asm/machdep.h>
  23. #include <asm/page.h>
  24. #include <asm/param.h>
  25. #include <asm/system.h>
  26. #include <asm/delay.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/lmb.h>
  29. #ifdef CONFIG_PPC64
  30. #include <asm/systemcfg.h>
  31. #endif
  32. struct rtas_t rtas = {
  33. .lock = SPIN_LOCK_UNLOCKED
  34. };
  35. EXPORT_SYMBOL(rtas);
  36. DEFINE_SPINLOCK(rtas_data_buf_lock);
  37. char rtas_data_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
  38. unsigned long rtas_rmo_buf;
  39. /*
  40. * call_rtas_display_status and call_rtas_display_status_delay
  41. * are designed only for very early low-level debugging, which
  42. * is why the token is hard-coded to 10.
  43. */
  44. void call_rtas_display_status(unsigned char c)
  45. {
  46. struct rtas_args *args = &rtas.args;
  47. unsigned long s;
  48. if (!rtas.base)
  49. return;
  50. spin_lock_irqsave(&rtas.lock, s);
  51. args->token = 10;
  52. args->nargs = 1;
  53. args->nret = 1;
  54. args->rets = (rtas_arg_t *)&(args->args[1]);
  55. args->args[0] = (int)c;
  56. enter_rtas(__pa(args));
  57. spin_unlock_irqrestore(&rtas.lock, s);
  58. }
  59. void call_rtas_display_status_delay(unsigned char c)
  60. {
  61. static int pending_newline = 0; /* did last write end with unprinted newline? */
  62. static int width = 16;
  63. if (c == '\n') {
  64. while (width-- > 0)
  65. call_rtas_display_status(' ');
  66. width = 16;
  67. udelay(500000);
  68. pending_newline = 1;
  69. } else {
  70. if (pending_newline) {
  71. call_rtas_display_status('\r');
  72. call_rtas_display_status('\n');
  73. }
  74. pending_newline = 0;
  75. if (width--) {
  76. call_rtas_display_status(c);
  77. udelay(10000);
  78. }
  79. }
  80. }
  81. void rtas_progress(char *s, unsigned short hex)
  82. {
  83. struct device_node *root;
  84. int width, *p;
  85. char *os;
  86. static int display_character, set_indicator;
  87. static int display_width, display_lines, *row_width, form_feed;
  88. static DEFINE_SPINLOCK(progress_lock);
  89. static int current_line;
  90. static int pending_newline = 0; /* did last write end with unprinted newline? */
  91. if (!rtas.base)
  92. return;
  93. if (display_width == 0) {
  94. display_width = 0x10;
  95. if ((root = find_path_device("/rtas"))) {
  96. if ((p = (unsigned int *)get_property(root,
  97. "ibm,display-line-length", NULL)))
  98. display_width = *p;
  99. if ((p = (unsigned int *)get_property(root,
  100. "ibm,form-feed", NULL)))
  101. form_feed = *p;
  102. if ((p = (unsigned int *)get_property(root,
  103. "ibm,display-number-of-lines", NULL)))
  104. display_lines = *p;
  105. row_width = (unsigned int *)get_property(root,
  106. "ibm,display-truncation-length", NULL);
  107. }
  108. display_character = rtas_token("display-character");
  109. set_indicator = rtas_token("set-indicator");
  110. }
  111. if (display_character == RTAS_UNKNOWN_SERVICE) {
  112. /* use hex display if available */
  113. if (set_indicator != RTAS_UNKNOWN_SERVICE)
  114. rtas_call(set_indicator, 3, 1, NULL, 6, 0, hex);
  115. return;
  116. }
  117. spin_lock(&progress_lock);
  118. /*
  119. * Last write ended with newline, but we didn't print it since
  120. * it would just clear the bottom line of output. Print it now
  121. * instead.
  122. *
  123. * If no newline is pending and form feed is supported, clear the
  124. * display with a form feed; otherwise, print a CR to start output
  125. * at the beginning of the line.
  126. */
  127. if (pending_newline) {
  128. rtas_call(display_character, 1, 1, NULL, '\r');
  129. rtas_call(display_character, 1, 1, NULL, '\n');
  130. pending_newline = 0;
  131. } else {
  132. current_line = 0;
  133. if (form_feed)
  134. rtas_call(display_character, 1, 1, NULL,
  135. (char)form_feed);
  136. else
  137. rtas_call(display_character, 1, 1, NULL, '\r');
  138. }
  139. if (row_width)
  140. width = row_width[current_line];
  141. else
  142. width = display_width;
  143. os = s;
  144. while (*os) {
  145. if (*os == '\n' || *os == '\r') {
  146. /* If newline is the last character, save it
  147. * until next call to avoid bumping up the
  148. * display output.
  149. */
  150. if (*os == '\n' && !os[1]) {
  151. pending_newline = 1;
  152. current_line++;
  153. if (current_line > display_lines-1)
  154. current_line = display_lines-1;
  155. spin_unlock(&progress_lock);
  156. return;
  157. }
  158. /* RTAS wants CR-LF, not just LF */
  159. if (*os == '\n') {
  160. rtas_call(display_character, 1, 1, NULL, '\r');
  161. rtas_call(display_character, 1, 1, NULL, '\n');
  162. } else {
  163. /* CR might be used to re-draw a line, so we'll
  164. * leave it alone and not add LF.
  165. */
  166. rtas_call(display_character, 1, 1, NULL, *os);
  167. }
  168. if (row_width)
  169. width = row_width[current_line];
  170. else
  171. width = display_width;
  172. } else {
  173. width--;
  174. rtas_call(display_character, 1, 1, NULL, *os);
  175. }
  176. os++;
  177. /* if we overwrite the screen length */
  178. if (width <= 0)
  179. while ((*os != 0) && (*os != '\n') && (*os != '\r'))
  180. os++;
  181. }
  182. spin_unlock(&progress_lock);
  183. }
  184. int rtas_token(const char *service)
  185. {
  186. int *tokp;
  187. if (rtas.dev == NULL)
  188. return RTAS_UNKNOWN_SERVICE;
  189. tokp = (int *) get_property(rtas.dev, service, NULL);
  190. return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
  191. }
  192. #ifdef CONFIG_RTAS_ERROR_LOGGING
  193. /*
  194. * Return the firmware-specified size of the error log buffer
  195. * for all rtas calls that require an error buffer argument.
  196. * This includes 'check-exception' and 'rtas-last-error'.
  197. */
  198. int rtas_get_error_log_max(void)
  199. {
  200. static int rtas_error_log_max;
  201. if (rtas_error_log_max)
  202. return rtas_error_log_max;
  203. rtas_error_log_max = rtas_token ("rtas-error-log-max");
  204. if ((rtas_error_log_max == RTAS_UNKNOWN_SERVICE) ||
  205. (rtas_error_log_max > RTAS_ERROR_LOG_MAX)) {
  206. printk (KERN_WARNING "RTAS: bad log buffer size %d\n",
  207. rtas_error_log_max);
  208. rtas_error_log_max = RTAS_ERROR_LOG_MAX;
  209. }
  210. return rtas_error_log_max;
  211. }
  212. EXPORT_SYMBOL(rtas_get_error_log_max);
  213. char rtas_err_buf[RTAS_ERROR_LOG_MAX];
  214. int rtas_last_error_token;
  215. /** Return a copy of the detailed error text associated with the
  216. * most recent failed call to rtas. Because the error text
  217. * might go stale if there are any other intervening rtas calls,
  218. * this routine must be called atomically with whatever produced
  219. * the error (i.e. with rtas.lock still held from the previous call).
  220. */
  221. static char *__fetch_rtas_last_error(char *altbuf)
  222. {
  223. struct rtas_args err_args, save_args;
  224. u32 bufsz;
  225. char *buf = NULL;
  226. if (rtas_last_error_token == -1)
  227. return NULL;
  228. bufsz = rtas_get_error_log_max();
  229. err_args.token = rtas_last_error_token;
  230. err_args.nargs = 2;
  231. err_args.nret = 1;
  232. err_args.args[0] = (rtas_arg_t)__pa(rtas_err_buf);
  233. err_args.args[1] = bufsz;
  234. err_args.args[2] = 0;
  235. save_args = rtas.args;
  236. rtas.args = err_args;
  237. enter_rtas(__pa(&rtas.args));
  238. err_args = rtas.args;
  239. rtas.args = save_args;
  240. /* Log the error in the unlikely case that there was one. */
  241. if (unlikely(err_args.args[2] == 0)) {
  242. if (altbuf) {
  243. buf = altbuf;
  244. } else {
  245. buf = rtas_err_buf;
  246. if (mem_init_done)
  247. buf = kmalloc(RTAS_ERROR_LOG_MAX, GFP_ATOMIC);
  248. }
  249. if (buf)
  250. memcpy(buf, rtas_err_buf, RTAS_ERROR_LOG_MAX);
  251. }
  252. return buf;
  253. }
  254. #define get_errorlog_buffer() kmalloc(RTAS_ERROR_LOG_MAX, GFP_KERNEL)
  255. #else /* CONFIG_RTAS_ERROR_LOGGING */
  256. #define __fetch_rtas_last_error(x) NULL
  257. #define get_errorlog_buffer() NULL
  258. #endif
  259. int rtas_call(int token, int nargs, int nret, int *outputs, ...)
  260. {
  261. va_list list;
  262. int i;
  263. unsigned long s;
  264. struct rtas_args *rtas_args;
  265. char *buff_copy = NULL;
  266. int ret;
  267. if (token == RTAS_UNKNOWN_SERVICE)
  268. return -1;
  269. /* Gotta do something different here, use global lock for now... */
  270. spin_lock_irqsave(&rtas.lock, s);
  271. rtas_args = &rtas.args;
  272. rtas_args->token = token;
  273. rtas_args->nargs = nargs;
  274. rtas_args->nret = nret;
  275. rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
  276. va_start(list, outputs);
  277. for (i = 0; i < nargs; ++i)
  278. rtas_args->args[i] = va_arg(list, rtas_arg_t);
  279. va_end(list);
  280. for (i = 0; i < nret; ++i)
  281. rtas_args->rets[i] = 0;
  282. enter_rtas(__pa(rtas_args));
  283. /* A -1 return code indicates that the last command couldn't
  284. be completed due to a hardware error. */
  285. if (rtas_args->rets[0] == -1)
  286. buff_copy = __fetch_rtas_last_error(NULL);
  287. if (nret > 1 && outputs != NULL)
  288. for (i = 0; i < nret-1; ++i)
  289. outputs[i] = rtas_args->rets[i+1];
  290. ret = (nret > 0)? rtas_args->rets[0]: 0;
  291. /* Gotta do something different here, use global lock for now... */
  292. spin_unlock_irqrestore(&rtas.lock, s);
  293. if (buff_copy) {
  294. log_error(buff_copy, ERR_TYPE_RTAS_LOG, 0);
  295. if (mem_init_done)
  296. kfree(buff_copy);
  297. }
  298. return ret;
  299. }
  300. /* Given an RTAS status code of 990n compute the hinted delay of 10^n
  301. * (last digit) milliseconds. For now we bound at n=5 (100 sec).
  302. */
  303. unsigned int rtas_extended_busy_delay_time(int status)
  304. {
  305. int order = status - 9900;
  306. unsigned long ms;
  307. if (order < 0)
  308. order = 0; /* RTC depends on this for -2 clock busy */
  309. else if (order > 5)
  310. order = 5; /* bound */
  311. /* Use microseconds for reasonable accuracy */
  312. for (ms = 1; order > 0; order--)
  313. ms *= 10;
  314. return ms;
  315. }
  316. int rtas_error_rc(int rtas_rc)
  317. {
  318. int rc;
  319. switch (rtas_rc) {
  320. case -1: /* Hardware Error */
  321. rc = -EIO;
  322. break;
  323. case -3: /* Bad indicator/domain/etc */
  324. rc = -EINVAL;
  325. break;
  326. case -9000: /* Isolation error */
  327. rc = -EFAULT;
  328. break;
  329. case -9001: /* Outstanding TCE/PTE */
  330. rc = -EEXIST;
  331. break;
  332. case -9002: /* No usable slot */
  333. rc = -ENODEV;
  334. break;
  335. default:
  336. printk(KERN_ERR "%s: unexpected RTAS error %d\n",
  337. __FUNCTION__, rtas_rc);
  338. rc = -ERANGE;
  339. break;
  340. }
  341. return rc;
  342. }
  343. int rtas_get_power_level(int powerdomain, int *level)
  344. {
  345. int token = rtas_token("get-power-level");
  346. int rc;
  347. if (token == RTAS_UNKNOWN_SERVICE)
  348. return -ENOENT;
  349. while ((rc = rtas_call(token, 1, 2, level, powerdomain)) == RTAS_BUSY)
  350. udelay(1);
  351. if (rc < 0)
  352. return rtas_error_rc(rc);
  353. return rc;
  354. }
  355. int rtas_set_power_level(int powerdomain, int level, int *setlevel)
  356. {
  357. int token = rtas_token("set-power-level");
  358. unsigned int wait_time;
  359. int rc;
  360. if (token == RTAS_UNKNOWN_SERVICE)
  361. return -ENOENT;
  362. while (1) {
  363. rc = rtas_call(token, 2, 2, setlevel, powerdomain, level);
  364. if (rc == RTAS_BUSY)
  365. udelay(1);
  366. else if (rtas_is_extended_busy(rc)) {
  367. wait_time = rtas_extended_busy_delay_time(rc);
  368. udelay(wait_time * 1000);
  369. } else
  370. break;
  371. }
  372. if (rc < 0)
  373. return rtas_error_rc(rc);
  374. return rc;
  375. }
  376. int rtas_get_sensor(int sensor, int index, int *state)
  377. {
  378. int token = rtas_token("get-sensor-state");
  379. unsigned int wait_time;
  380. int rc;
  381. if (token == RTAS_UNKNOWN_SERVICE)
  382. return -ENOENT;
  383. while (1) {
  384. rc = rtas_call(token, 2, 2, state, sensor, index);
  385. if (rc == RTAS_BUSY)
  386. udelay(1);
  387. else if (rtas_is_extended_busy(rc)) {
  388. wait_time = rtas_extended_busy_delay_time(rc);
  389. udelay(wait_time * 1000);
  390. } else
  391. break;
  392. }
  393. if (rc < 0)
  394. return rtas_error_rc(rc);
  395. return rc;
  396. }
  397. int rtas_set_indicator(int indicator, int index, int new_value)
  398. {
  399. int token = rtas_token("set-indicator");
  400. unsigned int wait_time;
  401. int rc;
  402. if (token == RTAS_UNKNOWN_SERVICE)
  403. return -ENOENT;
  404. while (1) {
  405. rc = rtas_call(token, 3, 1, NULL, indicator, index, new_value);
  406. if (rc == RTAS_BUSY)
  407. udelay(1);
  408. else if (rtas_is_extended_busy(rc)) {
  409. wait_time = rtas_extended_busy_delay_time(rc);
  410. udelay(wait_time * 1000);
  411. }
  412. else
  413. break;
  414. }
  415. if (rc < 0)
  416. return rtas_error_rc(rc);
  417. return rc;
  418. }
  419. void rtas_restart(char *cmd)
  420. {
  421. printk("RTAS system-reboot returned %d\n",
  422. rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
  423. for (;;);
  424. }
  425. void rtas_power_off(void)
  426. {
  427. /* allow power on only with power button press */
  428. printk("RTAS power-off returned %d\n",
  429. rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1));
  430. for (;;);
  431. }
  432. void rtas_halt(void)
  433. {
  434. rtas_power_off();
  435. }
  436. /* Must be in the RMO region, so we place it here */
  437. static char rtas_os_term_buf[2048];
  438. void rtas_os_term(char *str)
  439. {
  440. int status;
  441. if (RTAS_UNKNOWN_SERVICE == rtas_token("ibm,os-term"))
  442. return;
  443. snprintf(rtas_os_term_buf, 2048, "OS panic: %s", str);
  444. do {
  445. status = rtas_call(rtas_token("ibm,os-term"), 1, 1, NULL,
  446. __pa(rtas_os_term_buf));
  447. if (status == RTAS_BUSY)
  448. udelay(1);
  449. else if (status != 0)
  450. printk(KERN_EMERG "ibm,os-term call failed %d\n",
  451. status);
  452. } while (status == RTAS_BUSY);
  453. }
  454. asmlinkage int ppc_rtas(struct rtas_args __user *uargs)
  455. {
  456. struct rtas_args args;
  457. unsigned long flags;
  458. char *buff_copy, *errbuf = NULL;
  459. int nargs;
  460. if (!capable(CAP_SYS_ADMIN))
  461. return -EPERM;
  462. if (copy_from_user(&args, uargs, 3 * sizeof(u32)) != 0)
  463. return -EFAULT;
  464. nargs = args.nargs;
  465. if (nargs > ARRAY_SIZE(args.args)
  466. || args.nret > ARRAY_SIZE(args.args)
  467. || nargs + args.nret > ARRAY_SIZE(args.args))
  468. return -EINVAL;
  469. /* Copy in args. */
  470. if (copy_from_user(args.args, uargs->args,
  471. nargs * sizeof(rtas_arg_t)) != 0)
  472. return -EFAULT;
  473. buff_copy = get_errorlog_buffer();
  474. spin_lock_irqsave(&rtas.lock, flags);
  475. rtas.args = args;
  476. enter_rtas(__pa(&rtas.args));
  477. args = rtas.args;
  478. args.rets = &args.args[nargs];
  479. /* A -1 return code indicates that the last command couldn't
  480. be completed due to a hardware error. */
  481. if (args.rets[0] == -1)
  482. errbuf = __fetch_rtas_last_error(buff_copy);
  483. spin_unlock_irqrestore(&rtas.lock, flags);
  484. if (buff_copy) {
  485. if (errbuf)
  486. log_error(errbuf, ERR_TYPE_RTAS_LOG, 0);
  487. kfree(buff_copy);
  488. }
  489. /* Copy out args. */
  490. if (copy_to_user(uargs->args + nargs,
  491. args.args + nargs,
  492. args.nret * sizeof(rtas_arg_t)) != 0)
  493. return -EFAULT;
  494. return 0;
  495. }
  496. #ifdef CONFIG_SMP
  497. /* This version can't take the spinlock, because it never returns */
  498. struct rtas_args rtas_stop_self_args = {
  499. /* The token is initialized for real in setup_system() */
  500. .token = RTAS_UNKNOWN_SERVICE,
  501. .nargs = 0,
  502. .nret = 1,
  503. .rets = &rtas_stop_self_args.args[0],
  504. };
  505. void rtas_stop_self(void)
  506. {
  507. struct rtas_args *rtas_args = &rtas_stop_self_args;
  508. local_irq_disable();
  509. BUG_ON(rtas_args->token == RTAS_UNKNOWN_SERVICE);
  510. printk("cpu %u (hwid %u) Ready to die...\n",
  511. smp_processor_id(), hard_smp_processor_id());
  512. enter_rtas(__pa(rtas_args));
  513. panic("Alas, I survived.\n");
  514. }
  515. #endif
  516. /*
  517. * Call early during boot, before mem init or bootmem, to retreive the RTAS
  518. * informations from the device-tree and allocate the RMO buffer for userland
  519. * accesses.
  520. */
  521. void __init rtas_initialize(void)
  522. {
  523. unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
  524. /* Get RTAS dev node and fill up our "rtas" structure with infos
  525. * about it.
  526. */
  527. rtas.dev = of_find_node_by_name(NULL, "rtas");
  528. if (rtas.dev) {
  529. u32 *basep, *entryp;
  530. u32 *sizep;
  531. basep = (u32 *)get_property(rtas.dev, "linux,rtas-base", NULL);
  532. sizep = (u32 *)get_property(rtas.dev, "rtas-size", NULL);
  533. if (basep != NULL && sizep != NULL) {
  534. rtas.base = *basep;
  535. rtas.size = *sizep;
  536. entryp = (u32 *)get_property(rtas.dev, "linux,rtas-entry", NULL);
  537. if (entryp == NULL) /* Ugh */
  538. rtas.entry = rtas.base;
  539. else
  540. rtas.entry = *entryp;
  541. } else
  542. rtas.dev = NULL;
  543. }
  544. if (!rtas.dev)
  545. return;
  546. /* If RTAS was found, allocate the RMO buffer for it and look for
  547. * the stop-self token if any
  548. */
  549. #ifdef CONFIG_PPC64
  550. if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
  551. rtas_region = min(lmb.rmo_size, RTAS_INSTANTIATE_MAX);
  552. #endif
  553. rtas_rmo_buf = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE, rtas_region);
  554. #ifdef CONFIG_HOTPLUG_CPU
  555. rtas_stop_self_args.token = rtas_token("stop-self");
  556. #endif /* CONFIG_HOTPLUG_CPU */
  557. #ifdef CONFIG_RTAS_ERROR_LOGGING
  558. rtas_last_error_token = rtas_token("rtas-last-error");
  559. #endif
  560. }
  561. EXPORT_SYMBOL(rtas_token);
  562. EXPORT_SYMBOL(rtas_call);
  563. EXPORT_SYMBOL(rtas_data_buf);
  564. EXPORT_SYMBOL(rtas_data_buf_lock);
  565. EXPORT_SYMBOL(rtas_extended_busy_delay_time);
  566. EXPORT_SYMBOL(rtas_get_sensor);
  567. EXPORT_SYMBOL(rtas_get_power_level);
  568. EXPORT_SYMBOL(rtas_set_power_level);
  569. EXPORT_SYMBOL(rtas_set_indicator);