opal.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
  85. powerpc_firmware_features |= FW_FEATURE_OPAL;
  86. pr_info("OPAL detected !\n");
  87. } else {
  88. panic("OPAL != V3 detected, no longer supported.\n");
  89. }
  90. /* Reinit all cores with the right endian */
  91. opal_reinit_cores();
  92. /* Restore some bits */
  93. if (cur_cpu_spec->cpu_restore)
  94. cur_cpu_spec->cpu_restore();
  95. return 1;
  96. }
  97. int __init early_init_dt_scan_recoverable_ranges(unsigned long node,
  98. const char *uname, int depth, void *data)
  99. {
  100. int i, psize, size;
  101. const __be32 *prop;
  102. if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
  103. return 0;
  104. prop = of_get_flat_dt_prop(node, "mcheck-recoverable-ranges", &psize);
  105. if (!prop)
  106. return 1;
  107. pr_debug("Found machine check recoverable ranges.\n");
  108. /*
  109. * Calculate number of available entries.
  110. *
  111. * Each recoverable address range entry is (start address, len,
  112. * recovery address), 2 cells each for start and recovery address,
  113. * 1 cell for len, totalling 5 cells per entry.
  114. */
  115. mc_recoverable_range_len = psize / (sizeof(*prop) * 5);
  116. /* Sanity check */
  117. if (!mc_recoverable_range_len)
  118. return 1;
  119. /* Size required to hold all the entries. */
  120. size = mc_recoverable_range_len *
  121. sizeof(struct mcheck_recoverable_range);
  122. /*
  123. * Allocate a buffer to hold the MC recoverable ranges. We would be
  124. * accessing them in real mode, hence it needs to be within
  125. * RMO region.
  126. */
  127. mc_recoverable_range =__va(memblock_alloc_base(size, __alignof__(u64),
  128. ppc64_rma_size));
  129. memset(mc_recoverable_range, 0, size);
  130. for (i = 0; i < mc_recoverable_range_len; i++) {
  131. mc_recoverable_range[i].start_addr =
  132. of_read_number(prop + (i * 5) + 0, 2);
  133. mc_recoverable_range[i].end_addr =
  134. mc_recoverable_range[i].start_addr +
  135. of_read_number(prop + (i * 5) + 2, 1);
  136. mc_recoverable_range[i].recover_addr =
  137. of_read_number(prop + (i * 5) + 3, 2);
  138. pr_debug("Machine check recoverable range: %llx..%llx: %llx\n",
  139. mc_recoverable_range[i].start_addr,
  140. mc_recoverable_range[i].end_addr,
  141. mc_recoverable_range[i].recover_addr);
  142. }
  143. return 1;
  144. }
  145. static int __init opal_register_exception_handlers(void)
  146. {
  147. #ifdef __BIG_ENDIAN__
  148. u64 glue;
  149. if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
  150. return -ENODEV;
  151. /* Hookup some exception handlers except machine check. We use the
  152. * fwnmi area at 0x7000 to provide the glue space to OPAL
  153. */
  154. glue = 0x7000;
  155. /*
  156. * Check if we are running on newer firmware that exports
  157. * OPAL_HANDLE_HMI token. If yes, then don't ask OPAL to patch
  158. * the HMI interrupt and we catch it directly in Linux.
  159. *
  160. * For older firmware (i.e currently released POWER8 System Firmware
  161. * as of today <= SV810_087), we fallback to old behavior and let OPAL
  162. * patch the HMI vector and handle it inside OPAL firmware.
  163. *
  164. * For newer firmware (in development/yet to be released) we will
  165. * start catching/handling HMI directly in Linux.
  166. */
  167. if (!opal_check_token(OPAL_HANDLE_HMI)) {
  168. pr_info("Old firmware detected, OPAL handles HMIs.\n");
  169. opal_register_exception_handler(
  170. OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
  171. 0, glue);
  172. glue += 128;
  173. }
  174. opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
  175. #endif
  176. return 0;
  177. }
  178. machine_early_initcall(powernv, opal_register_exception_handlers);
  179. /*
  180. * Opal message notifier based on message type. Allow subscribers to get
  181. * notified for specific messgae type.
  182. */
  183. int opal_message_notifier_register(enum opal_msg_type msg_type,
  184. struct notifier_block *nb)
  185. {
  186. if (!nb || msg_type >= OPAL_MSG_TYPE_MAX) {
  187. pr_warning("%s: Invalid arguments, msg_type:%d\n",
  188. __func__, msg_type);
  189. return -EINVAL;
  190. }
  191. return atomic_notifier_chain_register(
  192. &opal_msg_notifier_head[msg_type], nb);
  193. }
  194. EXPORT_SYMBOL_GPL(opal_message_notifier_register);
  195. int opal_message_notifier_unregister(enum opal_msg_type msg_type,
  196. struct notifier_block *nb)
  197. {
  198. return atomic_notifier_chain_unregister(
  199. &opal_msg_notifier_head[msg_type], nb);
  200. }
  201. EXPORT_SYMBOL_GPL(opal_message_notifier_unregister);
  202. static void opal_message_do_notify(uint32_t msg_type, void *msg)
  203. {
  204. /* notify subscribers */
  205. atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
  206. msg_type, msg);
  207. }
  208. static void opal_handle_message(void)
  209. {
  210. s64 ret;
  211. /*
  212. * TODO: pre-allocate a message buffer depending on opal-msg-size
  213. * value in /proc/device-tree.
  214. */
  215. static struct opal_msg msg;
  216. u32 type;
  217. ret = opal_get_msg(__pa(&msg), sizeof(msg));
  218. /* No opal message pending. */
  219. if (ret == OPAL_RESOURCE)
  220. return;
  221. /* check for errors. */
  222. if (ret) {
  223. pr_warning("%s: Failed to retrieve opal message, err=%lld\n",
  224. __func__, ret);
  225. return;
  226. }
  227. type = be32_to_cpu(msg.msg_type);
  228. /* Sanity check */
  229. if (type >= OPAL_MSG_TYPE_MAX) {
  230. pr_warn_once("%s: Unknown message type: %u\n", __func__, type);
  231. return;
  232. }
  233. opal_message_do_notify(type, (void *)&msg);
  234. }
  235. static irqreturn_t opal_message_notify(int irq, void *data)
  236. {
  237. opal_handle_message();
  238. return IRQ_HANDLED;
  239. }
  240. static int __init opal_message_init(void)
  241. {
  242. int ret, i, irq;
  243. for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
  244. ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
  245. irq = opal_event_request(ilog2(OPAL_EVENT_MSG_PENDING));
  246. if (!irq) {
  247. pr_err("%s: Can't register OPAL event irq (%d)\n",
  248. __func__, irq);
  249. return irq;
  250. }
  251. ret = request_irq(irq, opal_message_notify,
  252. IRQ_TYPE_LEVEL_HIGH, "opal-msg", NULL);
  253. if (ret) {
  254. pr_err("%s: Can't request OPAL event irq (%d)\n",
  255. __func__, ret);
  256. return ret;
  257. }
  258. return 0;
  259. }
  260. int opal_get_chars(uint32_t vtermno, char *buf, int count)
  261. {
  262. s64 rc;
  263. __be64 evt, len;
  264. if (!opal.entry)
  265. return -ENODEV;
  266. opal_poll_events(&evt);
  267. if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
  268. return 0;
  269. len = cpu_to_be64(count);
  270. rc = opal_console_read(vtermno, &len, buf);
  271. if (rc == OPAL_SUCCESS)
  272. return be64_to_cpu(len);
  273. return 0;
  274. }
  275. int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
  276. {
  277. int written = 0;
  278. __be64 olen;
  279. s64 len, rc;
  280. unsigned long flags;
  281. __be64 evt;
  282. if (!opal.entry)
  283. return -ENODEV;
  284. /* We want put_chars to be atomic to avoid mangling of hvsi
  285. * packets. To do that, we first test for room and return
  286. * -EAGAIN if there isn't enough.
  287. *
  288. * Unfortunately, opal_console_write_buffer_space() doesn't
  289. * appear to work on opal v1, so we just assume there is
  290. * enough room and be done with it
  291. */
  292. spin_lock_irqsave(&opal_write_lock, flags);
  293. rc = opal_console_write_buffer_space(vtermno, &olen);
  294. len = be64_to_cpu(olen);
  295. if (rc || len < total_len) {
  296. spin_unlock_irqrestore(&opal_write_lock, flags);
  297. /* Closed -> drop characters */
  298. if (rc)
  299. return total_len;
  300. opal_poll_events(NULL);
  301. return -EAGAIN;
  302. }
  303. /* We still try to handle partial completions, though they
  304. * should no longer happen.
  305. */
  306. rc = OPAL_BUSY;
  307. while(total_len > 0 && (rc == OPAL_BUSY ||
  308. rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
  309. olen = cpu_to_be64(total_len);
  310. rc = opal_console_write(vtermno, &olen, data);
  311. len = be64_to_cpu(olen);
  312. /* Closed or other error drop */
  313. if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
  314. rc != OPAL_BUSY_EVENT) {
  315. written += total_len;
  316. break;
  317. }
  318. if (rc == OPAL_SUCCESS) {
  319. total_len -= len;
  320. data += len;
  321. written += len;
  322. }
  323. /* This is a bit nasty but we need that for the console to
  324. * flush when there aren't any interrupts. We will clean
  325. * things a bit later to limit that to synchronous path
  326. * such as the kernel console and xmon/udbg
  327. */
  328. do
  329. opal_poll_events(&evt);
  330. while(rc == OPAL_SUCCESS &&
  331. (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
  332. }
  333. spin_unlock_irqrestore(&opal_write_lock, flags);
  334. return written;
  335. }
  336. static int opal_recover_mce(struct pt_regs *regs,
  337. struct machine_check_event *evt)
  338. {
  339. int recovered = 0;
  340. uint64_t ea = get_mce_fault_addr(evt);
  341. if (!(regs->msr & MSR_RI)) {
  342. /* If MSR_RI isn't set, we cannot recover */
  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. __be64 events;
  558. set_freezable();
  559. do {
  560. try_to_freeze();
  561. opal_poll_events(&events);
  562. opal_handle_events(be64_to_cpu(events));
  563. msleep_interruptible(opal_heartbeat);
  564. } while (!kthread_should_stop());
  565. return 0;
  566. }
  567. static void opal_init_heartbeat(void)
  568. {
  569. /* Old firwmware, we assume the HVC heartbeat is sufficient */
  570. if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
  571. &opal_heartbeat) != 0)
  572. opal_heartbeat = 0;
  573. if (opal_heartbeat)
  574. kthread_run(kopald, NULL, "kopald");
  575. }
  576. static int __init opal_init(void)
  577. {
  578. struct device_node *np, *consoles, *leds;
  579. int rc;
  580. opal_node = of_find_node_by_path("/ibm,opal");
  581. if (!opal_node) {
  582. pr_warn("Device node not found\n");
  583. return -ENODEV;
  584. }
  585. /* Register OPAL consoles if any ports */
  586. consoles = of_find_node_by_path("/ibm,opal/consoles");
  587. if (consoles) {
  588. for_each_child_of_node(consoles, np) {
  589. if (strcmp(np->name, "serial"))
  590. continue;
  591. of_platform_device_create(np, NULL, NULL);
  592. }
  593. of_node_put(consoles);
  594. }
  595. /* Initialise OPAL messaging system */
  596. opal_message_init();
  597. /* Initialise OPAL asynchronous completion interface */
  598. opal_async_comp_init();
  599. /* Initialise OPAL sensor interface */
  600. opal_sensor_init();
  601. /* Initialise OPAL hypervisor maintainence interrupt handling */
  602. opal_hmi_handler_init();
  603. /* Create i2c platform devices */
  604. opal_i2c_create_devs();
  605. /* Setup a heatbeat thread if requested by OPAL */
  606. opal_init_heartbeat();
  607. /* Create leds platform devices */
  608. leds = of_find_node_by_path("/ibm,opal/leds");
  609. if (leds) {
  610. of_platform_device_create(leds, "opal_leds", NULL);
  611. of_node_put(leds);
  612. }
  613. /* Create "opal" kobject under /sys/firmware */
  614. rc = opal_sysfs_init();
  615. if (rc == 0) {
  616. /* Export symbol map to userspace */
  617. opal_export_symmap();
  618. /* Setup dump region interface */
  619. opal_dump_region_init();
  620. /* Setup error log interface */
  621. rc = opal_elog_init();
  622. /* Setup code update interface */
  623. opal_flash_update_init();
  624. /* Setup platform dump extract interface */
  625. opal_platform_dump_init();
  626. /* Setup system parameters interface */
  627. opal_sys_param_init();
  628. /* Setup message log interface. */
  629. opal_msglog_init();
  630. }
  631. /* Initialize platform devices: IPMI backend, PRD & flash interface */
  632. opal_pdev_init(opal_node, "ibm,opal-ipmi");
  633. opal_pdev_init(opal_node, "ibm,opal-flash");
  634. opal_pdev_init(opal_node, "ibm,opal-prd");
  635. /* Initialise OPAL kmsg dumper for flushing console on panic */
  636. opal_kmsg_init();
  637. return 0;
  638. }
  639. machine_subsys_initcall(powernv, opal_init);
  640. void opal_shutdown(void)
  641. {
  642. long rc = OPAL_BUSY;
  643. opal_event_shutdown();
  644. /*
  645. * Then sync with OPAL which ensure anything that can
  646. * potentially write to our memory has completed such
  647. * as an ongoing dump retrieval
  648. */
  649. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  650. rc = opal_sync_host_reboot();
  651. if (rc == OPAL_BUSY)
  652. opal_poll_events(NULL);
  653. else
  654. mdelay(10);
  655. }
  656. /* Unregister memory dump region */
  657. if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
  658. opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
  659. }
  660. /* Export this so that test modules can use it */
  661. EXPORT_SYMBOL_GPL(opal_invalid_call);
  662. EXPORT_SYMBOL_GPL(opal_xscom_read);
  663. EXPORT_SYMBOL_GPL(opal_xscom_write);
  664. EXPORT_SYMBOL_GPL(opal_ipmi_send);
  665. EXPORT_SYMBOL_GPL(opal_ipmi_recv);
  666. EXPORT_SYMBOL_GPL(opal_flash_read);
  667. EXPORT_SYMBOL_GPL(opal_flash_write);
  668. EXPORT_SYMBOL_GPL(opal_flash_erase);
  669. EXPORT_SYMBOL_GPL(opal_prd_msg);
  670. /* Convert a region of vmalloc memory to an opal sg list */
  671. struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
  672. unsigned long vmalloc_size)
  673. {
  674. struct opal_sg_list *sg, *first = NULL;
  675. unsigned long i = 0;
  676. sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
  677. if (!sg)
  678. goto nomem;
  679. first = sg;
  680. while (vmalloc_size > 0) {
  681. uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
  682. uint64_t length = min(vmalloc_size, PAGE_SIZE);
  683. sg->entry[i].data = cpu_to_be64(data);
  684. sg->entry[i].length = cpu_to_be64(length);
  685. i++;
  686. if (i >= SG_ENTRIES_PER_NODE) {
  687. struct opal_sg_list *next;
  688. next = kzalloc(PAGE_SIZE, GFP_KERNEL);
  689. if (!next)
  690. goto nomem;
  691. sg->length = cpu_to_be64(
  692. i * sizeof(struct opal_sg_entry) + 16);
  693. i = 0;
  694. sg->next = cpu_to_be64(__pa(next));
  695. sg = next;
  696. }
  697. vmalloc_addr += length;
  698. vmalloc_size -= length;
  699. }
  700. sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
  701. return first;
  702. nomem:
  703. pr_err("%s : Failed to allocate memory\n", __func__);
  704. opal_free_sg_list(first);
  705. return NULL;
  706. }
  707. void opal_free_sg_list(struct opal_sg_list *sg)
  708. {
  709. while (sg) {
  710. uint64_t next = be64_to_cpu(sg->next);
  711. kfree(sg);
  712. if (next)
  713. sg = __va(next);
  714. else
  715. sg = NULL;
  716. }
  717. }
  718. int opal_error_code(int rc)
  719. {
  720. switch (rc) {
  721. case OPAL_SUCCESS: return 0;
  722. case OPAL_PARAMETER: return -EINVAL;
  723. case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
  724. case OPAL_BUSY_EVENT: return -EBUSY;
  725. case OPAL_NO_MEM: return -ENOMEM;
  726. case OPAL_PERMISSION: return -EPERM;
  727. case OPAL_UNSUPPORTED: return -EIO;
  728. case OPAL_HARDWARE: return -EIO;
  729. case OPAL_INTERNAL_ERROR: return -EIO;
  730. default:
  731. pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
  732. return -EIO;
  733. }
  734. }
  735. EXPORT_SYMBOL_GPL(opal_poll_events);
  736. EXPORT_SYMBOL_GPL(opal_rtc_read);
  737. EXPORT_SYMBOL_GPL(opal_rtc_write);
  738. EXPORT_SYMBOL_GPL(opal_tpo_read);
  739. EXPORT_SYMBOL_GPL(opal_tpo_write);
  740. EXPORT_SYMBOL_GPL(opal_i2c_request);
  741. /* Export these symbols for PowerNV LED class driver */
  742. EXPORT_SYMBOL_GPL(opal_leds_get_ind);
  743. EXPORT_SYMBOL_GPL(opal_leds_set_ind);