rtas.c 18 KB

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