opal.c 22 KB

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