opal.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * PowerNV OPAL high level interfaces
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) "opal: " fmt
  12. #include <linux/printk.h>
  13. #include <linux/types.h>
  14. #include <linux/of.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/notifier.h>
  19. #include <linux/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/kobject.h>
  22. #include <linux/delay.h>
  23. #include <linux/memblock.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <asm/machdep.h>
  27. #include <asm/opal.h>
  28. #include <asm/firmware.h>
  29. #include <asm/mce.h>
  30. #include "powernv.h"
  31. /* /sys/firmware/opal */
  32. struct kobject *opal_kobj;
  33. struct opal {
  34. u64 base;
  35. u64 entry;
  36. u64 size;
  37. } opal;
  38. struct mcheck_recoverable_range {
  39. u64 start_addr;
  40. u64 end_addr;
  41. u64 recover_addr;
  42. };
  43. static struct mcheck_recoverable_range *mc_recoverable_range;
  44. static int mc_recoverable_range_len;
  45. struct device_node *opal_node;
  46. static DEFINE_SPINLOCK(opal_write_lock);
  47. static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
  48. static uint32_t opal_heartbeat;
  49. static struct task_struct *kopald_tsk;
  50. void opal_configure_cores(void)
  51. {
  52. /* Do the actual re-init, This will clobber all FPRs, VRs, etc...
  53. *
  54. * It will preserve non volatile GPRs and HSPRG0/1. It will
  55. * also restore HIDs and other SPRs to their original value
  56. * but it might clobber a bunch.
  57. */
  58. #ifdef __BIG_ENDIAN__
  59. opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
  60. #else
  61. opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_LE);
  62. #endif
  63. /* Restore some bits */
  64. if (cur_cpu_spec->cpu_restore)
  65. cur_cpu_spec->cpu_restore();
  66. }
  67. int __init early_init_dt_scan_opal(unsigned long node,
  68. const char *uname, int depth, void *data)
  69. {
  70. const void *basep, *entryp, *sizep;
  71. int basesz, entrysz, runtimesz;
  72. if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
  73. return 0;
  74. basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
  75. entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
  76. sizep = of_get_flat_dt_prop(node, "opal-runtime-size", &runtimesz);
  77. if (!basep || !entryp || !sizep)
  78. return 1;
  79. opal.base = of_read_number(basep, basesz/4);
  80. opal.entry = of_read_number(entryp, entrysz/4);
  81. opal.size = of_read_number(sizep, runtimesz/4);
  82. pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%d)\n",
  83. opal.base, basep, basesz);
  84. pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%d)\n",
  85. opal.entry, entryp, entrysz);
  86. pr_debug("OPAL Entry = 0x%llx (sizep=%p runtimesz=%d)\n",
  87. opal.size, sizep, runtimesz);
  88. if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
  89. powerpc_firmware_features |= FW_FEATURE_OPAL;
  90. pr_info("OPAL detected !\n");
  91. } else {
  92. panic("OPAL != V3 detected, no longer supported.\n");
  93. }
  94. return 1;
  95. }
  96. int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
  97. const char *uname, int depth, void *data)
  98. {
  99. int i, psize, size;
  100. const __be32 *prop;
  101. if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
  102. return 0;
  103. prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
  104. if (!prop)
  105. return 1;
  106. pr_debug("Found machine check recoverable ranges.\n");
  107. /*
  108. * Calculate number of available entries.
  109. *
  110. * Each recoverable address range entry is (start address, len,
  111. * recovery address), 2 cells each for start and recovery address,
  112. * 1 cell for len, totalling 5 cells per entry.
  113. */
  114. mc_recoverable_range_len = psize / (sizeof(*prop) * 5);
  115. /* Sanity check */
  116. if (!mc_recoverable_range_len)
  117. return 1;
  118. /* Size required to hold all the entries. */
  119. size = mc_recoverable_range_len *
  120. sizeof(struct mcheck_recoverable_range);
  121. /*
  122. * Allocate a buffer to hold the MC recoverable ranges. We would be
  123. * accessing them in real mode, hence it needs to be within
  124. * RMO region.
  125. */
  126. mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64),
  127. ppc64_rma_size));
  128. memset(mc_recoverable_range, 0, size);
  129. for (i = 0; i < mc_recoverable_range_len; i++) {
  130. mc_recoverable_range[i].start_addr =
  131. of_read_number(prop + (i * 5) + 0, 2);
  132. mc_recoverable_range[i].end_addr =
  133. mc_recoverable_range[i].start_addr +
  134. of_read_number(prop + (i * 5) + 2, 1);
  135. mc_recoverable_range[i].recover_addr =
  136. of_read_number(prop + (i * 5) + 3, 2);
  137. pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
  138. mc_recoverable_range[i].start_addr,
  139. mc_recoverable_range[i].end_addr,
  140. mc_recoverable_range[i].recover_addr);
  141. }
  142. return 1;
  143. }
  144. static int __init opal_register_exception_handlers(void)
  145. {
  146. #ifdef __BIG_ENDIAN__
  147. u64 glue;
  148. if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
  149. return -ENODEV;
  150. /* Hookup some exception handlers except machine check. We use the
  151. * fwnmi area at 0x7000 to provide the glue space to OPAL
  152. */
  153. glue = 0x7000;
  154. /*
  155. * Check if we are running on newer firmware that exports
  156. * OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to patch
  157. * the HMI interrupt and we catch it directly in Linux.
  158. *
  159. * For older firmware (i.e currently released POWER8 System Firmware
  160. * as of today <= SV810_087), we fallback to old behavior and let OPAL
  161. * patch the HMI vector and handle it inside OPAL firmware.
  162. *
  163. * For newer firmware (in development/yet to be released) we will
  164. * start catching/handling HMI directly in Linux.
  165. */
  166. if (!opal_check_token(OPAL_HANDLE_HMI)) {
  167. pr_info("Old firmware detected, OPAL handles HMIs.\n");
  168. opal_register_exception_handler(
  169. OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
  170. 0, glue);
  171. glue += 128;
  172. }
  173. opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
  174. #endif
  175. return 0;
  176. }
  177. machine_early_initcall(powernv, opal_register_exception_handlers);
  178. /*
  179. * Opal message notifier based on message type. Allow subscribers to get
  180. * notified for specific messgae type.
  181. */
  182. int opal_message_notifier_register(enum opal_msg_type msg_type,
  183. struct notifier_block *nb)
  184. {
  185. if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {
  186. pr_warning("%s: Invalid arguments, msg_type:%d\n",
  187. __func__, msg_type);
  188. return -EINVAL;
  189. }
  190. return atomic_notifier_chain_register(
  191. &opal_msg_notifier_head[msg_type], nb);
  192. }
  193. EXPORT_SYMBOL_GPL(opal_message_notifier_register);
  194. int opal_message_notifier_unregister(enum opal_msg_type msg_type,
  195. struct notifier_block *nb)
  196. {
  197. return atomic_notifier_chain_unregister(
  198. &opal_msg_notifier_head[msg_type], nb);
  199. }
  200. EXPORT_SYMBOL_GPL(opal_message_notifier_unregister);
  201. static void opal_message_do_notify(uint32_t msg_type, void *msg)
  202. {
  203. /* notify subscribers */
  204. atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
  205. msg_type, msg);
  206. }
  207. static void opal_handle_message(void)
  208. {
  209. s64 ret;
  210. /*
  211. * TODO: pre-allocate a message buffer depending on opal-msg-size
  212. * value in /proc/device-tree.
  213. */
  214. static struct opal_msg msg;
  215. u32 type;
  216. ret = opal_get_msg(__pa(&msg), sizeof(msg));
  217. /* No opal message pending. */
  218. if (ret == OPAL_RESOURCE)
  219. return;
  220. /* check for errors. */
  221. if (ret) {
  222. pr_warning("%s: Failed to retrieve opal message, err=%lld\n",
  223. __func__, ret);
  224. return;
  225. }
  226. type = be32_to_cpu(msg.msg_type);
  227. /* Sanity check */
  228. if (type >= OPAL_MSG_TYPE_MAX) {
  229. pr_warn_once("%s: Unknown message type: %u\n", __func__, type);
  230. return;
  231. }
  232. opal_message_do_notify(type, (void *)&msg);
  233. }
  234. static irqreturn_t opal_message_notify(int irq, void *data)
  235. {
  236. opal_handle_message();
  237. return IRQ_HANDLED;
  238. }
  239. static int __init opal_message_init(void)
  240. {
  241. int ret, i, irq;
  242. for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
  243. ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
  244. irq = opal_event_request(ilog2(OPAL_EVENT_MSG_PENDING));
  245. if (!irq) {
  246. pr_err("%s: Can't register OPAL event irq (%d)\n",
  247. __func__, irq);
  248. return irq;
  249. }
  250. ret = request_irq(irq, opal_message_notify,
  251. IRQ_TYPE_LEVEL_HIGH, "opal-msg", NULL);
  252. if (ret) {
  253. pr_err("%s: Can't request OPAL event irq (%d)\n",
  254. __func__, ret);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. int opal_get_chars(uint32_t vtermno, char *buf, int count)
  260. {
  261. s64 rc;
  262. __be64 evt, len;
  263. if (!opal.entry)
  264. return -ENODEV;
  265. opal_poll_events(&evt);
  266. if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
  267. return 0;
  268. len = cpu_to_be64(count);
  269. rc = opal_console_read(vtermno, &len, buf);
  270. if (rc == OPAL_SUCCESS)
  271. return be64_to_cpu(len);
  272. return 0;
  273. }
  274. int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
  275. {
  276. int written = 0;
  277. __be64 olen;
  278. s64 len, rc;
  279. unsigned long flags;
  280. __be64 evt;
  281. if (!opal.entry)
  282. return -ENODEV;
  283. /* We want put_chars to be atomic to avoid mangling of hvsi
  284. * packets. To do that, we first test for room and return
  285. * -EAGAIN if there isn't enough.
  286. *
  287. * Unfortunately, opal_console_write_buffer_space() doesn't
  288. * appear to work on opal v1, so we just assume there is
  289. * enough room and be done with it
  290. */
  291. spin_lock_irqsave(&opal_write_lock, flags);
  292. rc = opal_console_write_buffer_space(vtermno, &olen);
  293. len = be64_to_cpu(olen);
  294. if (rc || len < total_len) {
  295. spin_unlock_irqrestore(&opal_write_lock, flags);
  296. /* Closed -> drop characters */
  297. if (rc)
  298. return total_len;
  299. opal_poll_events(NULL);
  300. return -EAGAIN;
  301. }
  302. /* We still try to handle partial completions, though they
  303. * should no longer happen.
  304. */
  305. rc = OPAL_BUSY;
  306. while(total_len > 0 && (rc == OPAL_BUSY ||
  307. rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
  308. olen = cpu_to_be64(total_len);
  309. rc = opal_console_write(vtermno, &olen, data);
  310. len = be64_to_cpu(olen);
  311. /* Closed or other error drop */
  312. if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
  313. rc != OPAL_BUSY_EVENT) {
  314. written = total_len;
  315. break;
  316. }
  317. if (rc == OPAL_SUCCESS) {
  318. total_len -= len;
  319. data += len;
  320. written += len;
  321. }
  322. /* This is a bit nasty but we need that for the console to
  323. * flush when there aren't any interrupts. We will clean
  324. * things a bit later to limit that to synchronous path
  325. * such as the kernel console and xmon/udbg
  326. */
  327. do
  328. opal_poll_events(&evt);
  329. while(rc == OPAL_SUCCESS &&
  330. (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
  331. }
  332. spin_unlock_irqrestore(&opal_write_lock, flags);
  333. return written;
  334. }
  335. static int opal_recover_mce(struct pt_regs *regs,
  336. struct machine_check_event *evt)
  337. {
  338. int recovered = 0;
  339. uint64_t ea = get_mce_fault_addr(evt);
  340. if (!(regs->msr & MSR_RI)) {
  341. /* If MSR_RI isn't set, we cannot recover */
  342. pr_err("Machine check interrupt unrecoverable: MSR(RI=0)\n");
  343. recovered = 0;
  344. } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
  345. /* Platform corrected itself */
  346. recovered = 1;
  347. } else if (ea && !is_kernel_addr(ea)) {
  348. /*
  349. * Faulting address is not in kernel text. We should be fine.
  350. * We need to find which process uses this address.
  351. * For now, kill the task if we have received exception when
  352. * in userspace.
  353. *
  354. * TODO: Queue up this address for hwpoisioning later.
  355. */
  356. if (user_mode(regs) && !is_global_init(current)) {
  357. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  358. recovered = 1;
  359. } else
  360. recovered = 0;
  361. } else if (user_mode(regs) && !is_global_init(current) &&
  362. evt->severity == MCE_SEV_ERROR_SYNC) {
  363. /*
  364. * If we have received a synchronous error when in userspace
  365. * kill the task.
  366. */
  367. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  368. recovered = 1;
  369. }
  370. return recovered;
  371. }
  372. int opal_machine_check(struct pt_regs *regs)
  373. {
  374. struct machine_check_event evt;
  375. int ret;
  376. if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
  377. return 0;
  378. /* Print things out */
  379. if (evt.version != MCE_V1) {
  380. pr_err("Machine Check Exception, Unknown event version %d !\n",
  381. evt.version);
  382. return 0;
  383. }
  384. machine_check_print_event_info(&evt);
  385. if (opal_recover_mce(regs, &evt))
  386. return 1;
  387. /*
  388. * Unrecovered machine check, we are heading to panic path.
  389. *
  390. * We may have hit this MCE in very early stage of kernel
  391. * initialization even before opal-prd has started running. If
  392. * this is the case then this MCE error may go un-noticed or
  393. * un-analyzed if we go down panic path. We need to inform
  394. * BMC/OCC about this error so that they can collect relevant
  395. * data for error analysis before rebooting.
  396. * Use opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR) to do so.
  397. * This function may not return on BMC based system.
  398. */
  399. ret = opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR,
  400. "Unrecoverable Machine Check exception");
  401. if (ret == OPAL_UNSUPPORTED) {
  402. pr_emerg("Reboot type %d not supported\n",
  403. OPAL_REBOOT_PLATFORM_ERROR);
  404. }
  405. /*
  406. * We reached here. There can be three possibilities:
  407. * 1. We are running on a firmware level that do not support
  408. * opal_cec_reboot2()
  409. * 2. We are running on a firmware level that do not support
  410. * OPAL_REBOOT_PLATFORM_ERROR reboot type.
  411. * 3. We are running on FSP based system that does not need opal
  412. * to trigger checkstop explicitly for error analysis. The FSP
  413. * PRD component would have already got notified about this
  414. * error through other channels.
  415. *
  416. * If hardware marked this as an unrecoverable MCE, we are
  417. * going to panic anyway. Even if it didn't, it's not safe to
  418. * continue at this point, so we should explicitly panic.
  419. */
  420. panic("PowerNV Unrecovered Machine Check");
  421. return 0;
  422. }
  423. /* Early hmi handler called in real mode. */
  424. int opal_hmi_exception_early(struct pt_regs *regs)
  425. {
  426. s64 rc;
  427. /*
  428. * call opal hmi handler. Pass paca address as token.
  429. * The return value OPAL_SUCCESS is an indication that there is
  430. * an HMI event generated waiting to pull by Linux.
  431. */
  432. rc = opal_handle_hmi();
  433. if (rc == OPAL_SUCCESS) {
  434. local_paca->hmi_event_available = 1;
  435. return 1;
  436. }
  437. return 0;
  438. }
  439. /* HMI exception handler called in virtual mode during check_irq_replay. */
  440. int opal_handle_hmi_exception(struct pt_regs *regs)
  441. {
  442. s64 rc;
  443. __be64 evt = 0;
  444. /*
  445. * Check if HMI event is available.
  446. * if Yes, then call opal_poll_events to pull opal messages and
  447. * process them.
  448. */
  449. if (!local_paca->hmi_event_available)
  450. return 0;
  451. local_paca->hmi_event_available = 0;
  452. rc = opal_poll_events(&evt);
  453. if (rc == OPAL_SUCCESS && evt)
  454. opal_handle_events(be64_to_cpu(evt));
  455. return 1;
  456. }
  457. static uint64_t find_recovery_address(uint64_t nip)
  458. {
  459. int i;
  460. for (i = 0; i < mc_recoverable_range_len; i++)
  461. if ((nip >= mc_recoverable_range[i].start_addr) &&
  462. (nip < mc_recoverable_range[i].end_addr))
  463. return mc_recoverable_range[i].recover_addr;
  464. return 0;
  465. }
  466. bool opal_mce_check_early_recovery(struct pt_regs *regs)
  467. {
  468. uint64_t recover_addr = 0;
  469. if (!opal.base || !opal.size)
  470. goto out;
  471. if ((regs->nip >= opal.base) &&
  472. (regs->nip < (opal.base + opal.size)))
  473. recover_addr = find_recovery_address(regs->nip);
  474. /*
  475. * Setup regs->nip to rfi into fixup address.
  476. */
  477. if (recover_addr)
  478. regs->nip = recover_addr;
  479. out:
  480. return !!recover_addr;
  481. }
  482. static int opal_sysfs_init(void)
  483. {
  484. opal_kobj = kobject_create_and_add("opal", firmware_kobj);
  485. if (!opal_kobj) {
  486. pr_warn("kobject_create_and_add opal failed\n");
  487. return -ENOMEM;
  488. }
  489. return 0;
  490. }
  491. static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj,
  492. struct bin_attribute *bin_attr,
  493. char *buf, loff_t off, size_t count)
  494. {
  495. return memory_read_from_buffer(buf, count, &off, bin_attr->private,
  496. bin_attr->size);
  497. }
  498. static BIN_ATTR_RO(symbol_map, 0);
  499. static void opal_export_symmap(void)
  500. {
  501. const __be64 *syms;
  502. unsigned int size;
  503. struct device_node *fw;
  504. int rc;
  505. fw = of_find_node_by_path("/ibm,opal/firmware");
  506. if (!fw)
  507. return;
  508. syms = of_get_property(fw, "symbol-map", &size);
  509. if (!syms || size != 2 * sizeof(__be64))
  510. return;
  511. /* Setup attributes */
  512. bin_attr_symbol_map.private = __va(be64_to_cpu(syms[0]));
  513. bin_attr_symbol_map.size = be64_to_cpu(syms[1]);
  514. rc = sysfs_create_bin_file(opal_kobj, &bin_attr_symbol_map);
  515. if (rc)
  516. pr_warn("Error %d creating OPAL symbols file\n", rc);
  517. }
  518. static void __init opal_dump_region_init(void)
  519. {
  520. void *addr;
  521. uint64_t size;
  522. int rc;
  523. if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))
  524. return;
  525. /* Register kernel log buffer */
  526. addr = log_buf_addr_get();
  527. if (addr == NULL)
  528. return;
  529. size = log_buf_len_get();
  530. if (size == 0)
  531. return;
  532. rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
  533. __pa(addr), size);
  534. /* Don't warn if this is just an older OPAL that doesn't
  535. * know about that call
  536. */
  537. if (rc && rc != OPAL_UNSUPPORTED)
  538. pr_warn("DUMP: Failed to register kernel log buffer. "
  539. "rc = %d\n", rc);
  540. }
  541. static void opal_pdev_init(struct device_node *opal_node,
  542. const char *compatible)
  543. {
  544. struct device_node *np;
  545. for_each_child_of_node(opal_node, np)
  546. if (of_device_is_compatible(np, compatible))
  547. of_platform_device_create(np, NULL, NULL);
  548. }
  549. static void opal_i2c_create_devs(void)
  550. {
  551. struct device_node *np;
  552. for_each_compatible_node(np, NULL, "ibm,opal-i2c")
  553. of_platform_device_create(np, NULL, NULL);
  554. }
  555. static int kopald(void *unused)
  556. {
  557. unsigned long timeout = msecs_to_jiffies(opal_heartbeat) + 1;
  558. __be64 events;
  559. set_freezable();
  560. do {
  561. try_to_freeze();
  562. opal_poll_events(&events);
  563. opal_handle_events(be64_to_cpu(events));
  564. schedule_timeout_interruptible(timeout);
  565. } while (!kthread_should_stop());
  566. return 0;
  567. }
  568. void opal_wake_poller(void)
  569. {
  570. if (kopald_tsk)
  571. wake_up_process(kopald_tsk);
  572. }
  573. static void opal_init_heartbeat(void)
  574. {
  575. /* Old firwmware, we assume the HVC heartbeat is sufficient */
  576. if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
  577. &opal_heartbeat) != 0)
  578. opal_heartbeat = 0;
  579. if (opal_heartbeat)
  580. kopald_tsk = kthread_run(kopald, NULL, "kopald");
  581. }
  582. static int __init opal_init(void)
  583. {
  584. struct device_node *np, *consoles, *leds;
  585. int rc;
  586. opal_node = of_find_node_by_path("/ibm,opal");
  587. if (!opal_node) {
  588. pr_warn("Device node not found\n");
  589. return -ENODEV;
  590. }
  591. /* Register OPAL consoles if any ports */
  592. consoles = of_find_node_by_path("/ibm,opal/consoles");
  593. if (consoles) {
  594. for_each_child_of_node(consoles, np) {
  595. if (strcmp(np->name, "serial"))
  596. continue;
  597. of_platform_device_create(np, NULL, NULL);
  598. }
  599. of_node_put(consoles);
  600. }
  601. /* Initialise OPAL messaging system */
  602. opal_message_init();
  603. /* Initialise OPAL asynchronous completion interface */
  604. opal_async_comp_init();
  605. /* Initialise OPAL sensor interface */
  606. opal_sensor_init();
  607. /* Initialise OPAL hypervisor maintainence interrupt handling */
  608. opal_hmi_handler_init();
  609. /* Create i2c platform devices */
  610. opal_i2c_create_devs();
  611. /* Setup a heatbeat thread if requested by OPAL */
  612. opal_init_heartbeat();
  613. /* Create leds platform devices */
  614. leds = of_find_node_by_path("/ibm,opal/leds");
  615. if (leds) {
  616. of_platform_device_create(leds, "opal_leds", NULL);
  617. of_node_put(leds);
  618. }
  619. /* Initialise OPAL message log interface */
  620. opal_msglog_init();
  621. /* Create "opal" kobject under /sys/firmware */
  622. rc = opal_sysfs_init();
  623. if (rc == 0) {
  624. /* Export symbol map to userspace */
  625. opal_export_symmap();
  626. /* Setup dump region interface */
  627. opal_dump_region_init();
  628. /* Setup error log interface */
  629. rc = opal_elog_init();
  630. /* Setup code update interface */
  631. opal_flash_update_init();
  632. /* Setup platform dump extract interface */
  633. opal_platform_dump_init();
  634. /* Setup system parameters interface */
  635. opal_sys_param_init();
  636. /* Setup message log sysfs interface. */
  637. opal_msglog_sysfs_init();
  638. }
  639. /* Initialize platform devices: IPMI backend, PRD & flash interface */
  640. opal_pdev_init(opal_node, "ibm,opal-ipmi");
  641. opal_pdev_init(opal_node, "ibm,opal-flash");
  642. opal_pdev_init(opal_node, "ibm,opal-prd");
  643. /* Initialise platform device: oppanel interface */
  644. opal_pdev_init(opal_node, "ibm,opal-oppanel");
  645. /* Initialise OPAL kmsg dumper for flushing console on panic */
  646. opal_kmsg_init();
  647. return 0;
  648. }
  649. machine_subsys_initcall(powernv, opal_init);
  650. void opal_shutdown(void)
  651. {
  652. long rc = OPAL_BUSY;
  653. opal_event_shutdown();
  654. /*
  655. * Then sync with OPAL which ensure anything that can
  656. * potentially write to our memory has completed such
  657. * as an ongoing dump retrieval
  658. */
  659. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  660. rc = opal_sync_host_reboot();
  661. if (rc == OPAL_BUSY)
  662. opal_poll_events(NULL);
  663. else
  664. mdelay(10);
  665. }
  666. /* Unregister memory dump region */
  667. if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
  668. opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
  669. }
  670. /* Export this so that test modules can use it */
  671. EXPORT_SYMBOL_GPL(opal_invalid_call);
  672. EXPORT_SYMBOL_GPL(opal_xscom_read);
  673. EXPORT_SYMBOL_GPL(opal_xscom_write);
  674. EXPORT_SYMBOL_GPL(opal_ipmi_send);
  675. EXPORT_SYMBOL_GPL(opal_ipmi_recv);
  676. EXPORT_SYMBOL_GPL(opal_flash_read);
  677. EXPORT_SYMBOL_GPL(opal_flash_write);
  678. EXPORT_SYMBOL_GPL(opal_flash_erase);
  679. EXPORT_SYMBOL_GPL(opal_prd_msg);
  680. /* Convert a region of vmalloc memory to an opal sg list */
  681. struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
  682. unsigned long vmalloc_size)
  683. {
  684. struct opal_sg_list *sg, *first = NULL;
  685. unsigned long i = 0;
  686. sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
  687. if (!sg)
  688. goto nomem;
  689. first = sg;
  690. while (vmalloc_size > 0) {
  691. uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
  692. uint64_t length = min(vmalloc_size, PAGE_SIZE);
  693. sg->entry[i].data = cpu_to_be64(data);
  694. sg->entry[i].length = cpu_to_be64(length);
  695. i++;
  696. if (i >= SG_ENTRIES_PER_NODE) {
  697. struct opal_sg_list *next;
  698. next = kzalloc(PAGE_SIZE, GFP_KERNEL);
  699. if (!next)
  700. goto nomem;
  701. sg->length = cpu_to_be64(
  702. i * sizeof(struct opal_sg_entry) + 16);
  703. i = 0;
  704. sg->next = cpu_to_be64(__pa(next));
  705. sg = next;
  706. }
  707. vmalloc_addr += length;
  708. vmalloc_size -= length;
  709. }
  710. sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
  711. return first;
  712. nomem:
  713. pr_err("%s : Failed to allocate memory\n", __func__);
  714. opal_free_sg_list(first);
  715. return NULL;
  716. }
  717. void opal_free_sg_list(struct opal_sg_list *sg)
  718. {
  719. while (sg) {
  720. uint64_t next = be64_to_cpu(sg->next);
  721. kfree(sg);
  722. if (next)
  723. sg = __va(next);
  724. else
  725. sg = NULL;
  726. }
  727. }
  728. int opal_error_code(int rc)
  729. {
  730. switch (rc) {
  731. case OPAL_SUCCESS: return 0;
  732. case OPAL_PARAMETER: return -EINVAL;
  733. case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
  734. case OPAL_BUSY_EVENT: return -EBUSY;
  735. case OPAL_NO_MEM: return -ENOMEM;
  736. case OPAL_PERMISSION: return -EPERM;
  737. case OPAL_UNSUPPORTED: return -EIO;
  738. case OPAL_HARDWARE: return -EIO;
  739. case OPAL_INTERNAL_ERROR: return -EIO;
  740. default:
  741. pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
  742. return -EIO;
  743. }
  744. }
  745. EXPORT_SYMBOL_GPL(opal_poll_events);
  746. EXPORT_SYMBOL_GPL(opal_rtc_read);
  747. EXPORT_SYMBOL_GPL(opal_rtc_write);
  748. EXPORT_SYMBOL_GPL(opal_tpo_read);
  749. EXPORT_SYMBOL_GPL(opal_tpo_write);
  750. EXPORT_SYMBOL_GPL(opal_i2c_request);
  751. /* Export these symbols for PowerNV LED class driver */
  752. EXPORT_SYMBOL_GPL(opal_leds_get_ind);
  753. EXPORT_SYMBOL_GPL(opal_leds_set_ind);
  754. /* Export this symbol for PowerNV Operator Panel class driver */
  755. EXPORT_SYMBOL_GPL(opal_write_oppanel_async);