opal.c 25 KB

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