opal.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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_phys_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. static int __opal_put_chars(uint32_t vtermno, const char *data, int total_len, bool atomic)
  291. {
  292. unsigned long flags = 0 /* shut up gcc */;
  293. int written;
  294. __be64 olen;
  295. s64 rc;
  296. if (!opal.entry)
  297. return -ENODEV;
  298. if (atomic)
  299. spin_lock_irqsave(&opal_write_lock, flags);
  300. rc = opal_console_write_buffer_space(vtermno, &olen);
  301. if (rc || be64_to_cpu(olen) < total_len) {
  302. /* Closed -> drop characters */
  303. if (rc)
  304. written = total_len;
  305. else
  306. written = -EAGAIN;
  307. goto out;
  308. }
  309. /* Should not get a partial write here because space is available. */
  310. olen = cpu_to_be64(total_len);
  311. rc = opal_console_write(vtermno, &olen, data);
  312. if (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  313. if (rc == OPAL_BUSY_EVENT)
  314. opal_poll_events(NULL);
  315. written = -EAGAIN;
  316. goto out;
  317. }
  318. /* Closed or other error drop */
  319. if (rc != OPAL_SUCCESS) {
  320. written = opal_error_code(rc);
  321. goto out;
  322. }
  323. written = be64_to_cpu(olen);
  324. if (written < total_len) {
  325. if (atomic) {
  326. /* Should not happen */
  327. pr_warn("atomic console write returned partial "
  328. "len=%d written=%d\n", total_len, written);
  329. }
  330. if (!written)
  331. written = -EAGAIN;
  332. }
  333. out:
  334. if (atomic)
  335. spin_unlock_irqrestore(&opal_write_lock, flags);
  336. return written;
  337. }
  338. int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
  339. {
  340. return __opal_put_chars(vtermno, data, total_len, false);
  341. }
  342. /*
  343. * opal_put_chars_atomic will not perform partial-writes. Data will be
  344. * atomically written to the terminal or not at all. This is not strictly
  345. * true at the moment because console space can race with OPAL's console
  346. * writes.
  347. */
  348. int opal_put_chars_atomic(uint32_t vtermno, const char *data, int total_len)
  349. {
  350. return __opal_put_chars(vtermno, data, total_len, true);
  351. }
  352. static s64 __opal_flush_console(uint32_t vtermno)
  353. {
  354. s64 rc;
  355. if (!opal_check_token(OPAL_CONSOLE_FLUSH)) {
  356. __be64 evt;
  357. /*
  358. * If OPAL_CONSOLE_FLUSH is not implemented in the firmware,
  359. * the console can still be flushed by calling the polling
  360. * function while it has OPAL_EVENT_CONSOLE_OUTPUT events.
  361. */
  362. WARN_ONCE(1, "opal: OPAL_CONSOLE_FLUSH missing.\n");
  363. opal_poll_events(&evt);
  364. if (!(be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT))
  365. return OPAL_SUCCESS;
  366. return OPAL_BUSY;
  367. } else {
  368. rc = opal_console_flush(vtermno);
  369. if (rc == OPAL_BUSY_EVENT) {
  370. opal_poll_events(NULL);
  371. rc = OPAL_BUSY;
  372. }
  373. return rc;
  374. }
  375. }
  376. /*
  377. * opal_flush_console spins until the console is flushed
  378. */
  379. int opal_flush_console(uint32_t vtermno)
  380. {
  381. for (;;) {
  382. s64 rc = __opal_flush_console(vtermno);
  383. if (rc == OPAL_BUSY || rc == OPAL_PARTIAL) {
  384. mdelay(1);
  385. continue;
  386. }
  387. return opal_error_code(rc);
  388. }
  389. }
  390. /*
  391. * opal_flush_chars is an hvc interface that sleeps until the console is
  392. * flushed if wait, otherwise it will return -EBUSY if the console has data,
  393. * -EAGAIN if it has data and some of it was flushed.
  394. */
  395. int opal_flush_chars(uint32_t vtermno, bool wait)
  396. {
  397. for (;;) {
  398. s64 rc = __opal_flush_console(vtermno);
  399. if (rc == OPAL_BUSY || rc == OPAL_PARTIAL) {
  400. if (wait) {
  401. msleep(OPAL_BUSY_DELAY_MS);
  402. continue;
  403. }
  404. if (rc == OPAL_PARTIAL)
  405. return -EAGAIN;
  406. }
  407. return opal_error_code(rc);
  408. }
  409. }
  410. static int opal_recover_mce(struct pt_regs *regs,
  411. struct machine_check_event *evt)
  412. {
  413. int recovered = 0;
  414. if (!(regs->msr & MSR_RI)) {
  415. /* If MSR_RI isn't set, we cannot recover */
  416. pr_err("Machine check interrupt unrecoverable: MSR(RI=0)\n");
  417. recovered = 0;
  418. } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
  419. /* Platform corrected itself */
  420. recovered = 1;
  421. } else if (evt->severity == MCE_SEV_FATAL) {
  422. /* Fatal machine check */
  423. pr_err("Machine check interrupt is fatal\n");
  424. recovered = 0;
  425. }
  426. if (!recovered && evt->severity == MCE_SEV_ERROR_SYNC) {
  427. /*
  428. * Try to kill processes if we get a synchronous machine check
  429. * (e.g., one caused by execution of this instruction). This
  430. * will devolve into a panic if we try to kill init or are in
  431. * an interrupt etc.
  432. *
  433. * TODO: Queue up this address for hwpoisioning later.
  434. * TODO: This is not quite right for d-side machine
  435. * checks ->nip is not necessarily the important
  436. * address.
  437. */
  438. if ((user_mode(regs))) {
  439. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  440. recovered = 1;
  441. } else if (die_will_crash()) {
  442. /*
  443. * die() would kill the kernel, so better to go via
  444. * the platform reboot code that will log the
  445. * machine check.
  446. */
  447. recovered = 0;
  448. } else {
  449. die("Machine check", regs, SIGBUS);
  450. recovered = 1;
  451. }
  452. }
  453. return recovered;
  454. }
  455. void __noreturn pnv_platform_error_reboot(struct pt_regs *regs, const char *msg)
  456. {
  457. panic_flush_kmsg_start();
  458. pr_emerg("Hardware platform error: %s\n", msg);
  459. if (regs)
  460. show_regs(regs);
  461. smp_send_stop();
  462. panic_flush_kmsg_end();
  463. /*
  464. * Don't bother to shut things down because this will
  465. * xstop the system.
  466. */
  467. if (opal_cec_reboot2(OPAL_REBOOT_PLATFORM_ERROR, msg)
  468. == OPAL_UNSUPPORTED) {
  469. pr_emerg("Reboot type %d not supported for %s\n",
  470. OPAL_REBOOT_PLATFORM_ERROR, msg);
  471. }
  472. /*
  473. * We reached here. There can be three possibilities:
  474. * 1. We are running on a firmware level that do not support
  475. * opal_cec_reboot2()
  476. * 2. We are running on a firmware level that do not support
  477. * OPAL_REBOOT_PLATFORM_ERROR reboot type.
  478. * 3. We are running on FSP based system that does not need
  479. * opal to trigger checkstop explicitly for error analysis.
  480. * The FSP PRD component would have already got notified
  481. * about this error through other channels.
  482. * 4. We are running on a newer skiboot that by default does
  483. * not cause a checkstop, drops us back to the kernel to
  484. * extract context and state at the time of the error.
  485. */
  486. panic(msg);
  487. }
  488. int opal_machine_check(struct pt_regs *regs)
  489. {
  490. struct machine_check_event evt;
  491. if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
  492. return 0;
  493. /* Print things out */
  494. if (evt.version != MCE_V1) {
  495. pr_err("Machine Check Exception, Unknown event version %d !\n",
  496. evt.version);
  497. return 0;
  498. }
  499. machine_check_print_event_info(&evt, user_mode(regs));
  500. if (opal_recover_mce(regs, &evt))
  501. return 1;
  502. pnv_platform_error_reboot(regs, "Unrecoverable Machine Check exception");
  503. }
  504. /* Early hmi handler called in real mode. */
  505. int opal_hmi_exception_early(struct pt_regs *regs)
  506. {
  507. s64 rc;
  508. /*
  509. * call opal hmi handler. Pass paca address as token.
  510. * The return value OPAL_SUCCESS is an indication that there is
  511. * an HMI event generated waiting to pull by Linux.
  512. */
  513. rc = opal_handle_hmi();
  514. if (rc == OPAL_SUCCESS) {
  515. local_paca->hmi_event_available = 1;
  516. return 1;
  517. }
  518. return 0;
  519. }
  520. /* HMI exception handler called in virtual mode during check_irq_replay. */
  521. int opal_handle_hmi_exception(struct pt_regs *regs)
  522. {
  523. /*
  524. * Check if HMI event is available.
  525. * if Yes, then wake kopald to process them.
  526. */
  527. if (!local_paca->hmi_event_available)
  528. return 0;
  529. local_paca->hmi_event_available = 0;
  530. opal_wake_poller();
  531. return 1;
  532. }
  533. static uint64_t find_recovery_address(uint64_t nip)
  534. {
  535. int i;
  536. for (i = 0; i < mc_recoverable_range_len; i++)
  537. if ((nip >= mc_recoverable_range[i].start_addr) &&
  538. (nip < mc_recoverable_range[i].end_addr))
  539. return mc_recoverable_range[i].recover_addr;
  540. return 0;
  541. }
  542. bool opal_mce_check_early_recovery(struct pt_regs *regs)
  543. {
  544. uint64_t recover_addr = 0;
  545. if (!opal.base || !opal.size)
  546. goto out;
  547. if ((regs->nip >= opal.base) &&
  548. (regs->nip < (opal.base + opal.size)))
  549. recover_addr = find_recovery_address(regs->nip);
  550. /*
  551. * Setup regs->nip to rfi into fixup address.
  552. */
  553. if (recover_addr)
  554. regs->nip = recover_addr;
  555. out:
  556. return !!recover_addr;
  557. }
  558. static int opal_sysfs_init(void)
  559. {
  560. opal_kobj = kobject_create_and_add("opal", firmware_kobj);
  561. if (!opal_kobj) {
  562. pr_warn("kobject_create_and_add opal failed\n");
  563. return -ENOMEM;
  564. }
  565. return 0;
  566. }
  567. static ssize_t symbol_map_read(struct file *fp, struct kobject *kobj,
  568. struct bin_attribute *bin_attr,
  569. char *buf, loff_t off, size_t count)
  570. {
  571. return memory_read_from_buffer(buf, count, &off, bin_attr->private,
  572. bin_attr->size);
  573. }
  574. static BIN_ATTR_RO(symbol_map, 0);
  575. static void opal_export_symmap(void)
  576. {
  577. const __be64 *syms;
  578. unsigned int size;
  579. struct device_node *fw;
  580. int rc;
  581. fw = of_find_node_by_path("/ibm,opal/firmware");
  582. if (!fw)
  583. return;
  584. syms = of_get_property(fw, "symbol-map", &size);
  585. if (!syms || size != 2 * sizeof(__be64))
  586. return;
  587. /* Setup attributes */
  588. bin_attr_symbol_map.private = __va(be64_to_cpu(syms[0]));
  589. bin_attr_symbol_map.size = be64_to_cpu(syms[1]);
  590. rc = sysfs_create_bin_file(opal_kobj, &bin_attr_symbol_map);
  591. if (rc)
  592. pr_warn("Error %d creating OPAL symbols file\n", rc);
  593. }
  594. static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
  595. struct bin_attribute *bin_attr, char *buf,
  596. loff_t off, size_t count)
  597. {
  598. return memory_read_from_buffer(buf, count, &off, bin_attr->private,
  599. bin_attr->size);
  600. }
  601. /*
  602. * opal_export_attrs: creates a sysfs node for each property listed in
  603. * the device-tree under /ibm,opal/firmware/exports/
  604. * All new sysfs nodes are created under /opal/exports/.
  605. * This allows for reserved memory regions (e.g. HDAT) to be read.
  606. * The new sysfs nodes are only readable by root.
  607. */
  608. static void opal_export_attrs(void)
  609. {
  610. struct bin_attribute *attr;
  611. struct device_node *np;
  612. struct property *prop;
  613. struct kobject *kobj;
  614. u64 vals[2];
  615. int rc;
  616. np = of_find_node_by_path("/ibm,opal/firmware/exports");
  617. if (!np)
  618. return;
  619. /* Create new 'exports' directory - /sys/firmware/opal/exports */
  620. kobj = kobject_create_and_add("exports", opal_kobj);
  621. if (!kobj) {
  622. pr_warn("kobject_create_and_add() of exports failed\n");
  623. return;
  624. }
  625. for_each_property_of_node(np, prop) {
  626. if (!strcmp(prop->name, "name") || !strcmp(prop->name, "phandle"))
  627. continue;
  628. if (of_property_read_u64_array(np, prop->name, &vals[0], 2))
  629. continue;
  630. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  631. if (attr == NULL) {
  632. pr_warn("Failed kmalloc for bin_attribute!");
  633. continue;
  634. }
  635. sysfs_bin_attr_init(attr);
  636. attr->attr.name = kstrdup(prop->name, GFP_KERNEL);
  637. attr->attr.mode = 0400;
  638. attr->read = export_attr_read;
  639. attr->private = __va(vals[0]);
  640. attr->size = vals[1];
  641. if (attr->attr.name == NULL) {
  642. pr_warn("Failed kstrdup for bin_attribute attr.name");
  643. kfree(attr);
  644. continue;
  645. }
  646. rc = sysfs_create_bin_file(kobj, attr);
  647. if (rc) {
  648. pr_warn("Error %d creating OPAL sysfs exports/%s file\n",
  649. rc, prop->name);
  650. kfree(attr->attr.name);
  651. kfree(attr);
  652. }
  653. }
  654. of_node_put(np);
  655. }
  656. static void __init opal_dump_region_init(void)
  657. {
  658. void *addr;
  659. uint64_t size;
  660. int rc;
  661. if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))
  662. return;
  663. /* Register kernel log buffer */
  664. addr = log_buf_addr_get();
  665. if (addr == NULL)
  666. return;
  667. size = log_buf_len_get();
  668. if (size == 0)
  669. return;
  670. rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
  671. __pa(addr), size);
  672. /* Don't warn if this is just an older OPAL that doesn't
  673. * know about that call
  674. */
  675. if (rc && rc != OPAL_UNSUPPORTED)
  676. pr_warn("DUMP: Failed to register kernel log buffer. "
  677. "rc = %d\n", rc);
  678. }
  679. static void opal_pdev_init(const char *compatible)
  680. {
  681. struct device_node *np;
  682. for_each_compatible_node(np, NULL, compatible)
  683. of_platform_device_create(np, NULL, NULL);
  684. }
  685. static void __init opal_imc_init_dev(void)
  686. {
  687. struct device_node *np;
  688. np = of_find_compatible_node(NULL, NULL, IMC_DTB_COMPAT);
  689. if (np)
  690. of_platform_device_create(np, NULL, NULL);
  691. }
  692. static int kopald(void *unused)
  693. {
  694. unsigned long timeout = msecs_to_jiffies(opal_heartbeat) + 1;
  695. set_freezable();
  696. do {
  697. try_to_freeze();
  698. opal_handle_events();
  699. set_current_state(TASK_INTERRUPTIBLE);
  700. if (opal_have_pending_events())
  701. __set_current_state(TASK_RUNNING);
  702. else
  703. schedule_timeout(timeout);
  704. } while (!kthread_should_stop());
  705. return 0;
  706. }
  707. void opal_wake_poller(void)
  708. {
  709. if (kopald_tsk)
  710. wake_up_process(kopald_tsk);
  711. }
  712. static void opal_init_heartbeat(void)
  713. {
  714. /* Old firwmware, we assume the HVC heartbeat is sufficient */
  715. if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
  716. &opal_heartbeat) != 0)
  717. opal_heartbeat = 0;
  718. if (opal_heartbeat)
  719. kopald_tsk = kthread_run(kopald, NULL, "kopald");
  720. }
  721. static int __init opal_init(void)
  722. {
  723. struct device_node *np, *consoles, *leds;
  724. int rc;
  725. opal_node = of_find_node_by_path("/ibm,opal");
  726. if (!opal_node) {
  727. pr_warn("Device node not found\n");
  728. return -ENODEV;
  729. }
  730. /* Register OPAL consoles if any ports */
  731. consoles = of_find_node_by_path("/ibm,opal/consoles");
  732. if (consoles) {
  733. for_each_child_of_node(consoles, np) {
  734. if (strcmp(np->name, "serial"))
  735. continue;
  736. of_platform_device_create(np, NULL, NULL);
  737. }
  738. of_node_put(consoles);
  739. }
  740. /* Initialise OPAL messaging system */
  741. opal_message_init();
  742. /* Initialise OPAL asynchronous completion interface */
  743. opal_async_comp_init();
  744. /* Initialise OPAL sensor interface */
  745. opal_sensor_init();
  746. /* Initialise OPAL hypervisor maintainence interrupt handling */
  747. opal_hmi_handler_init();
  748. /* Create i2c platform devices */
  749. opal_pdev_init("ibm,opal-i2c");
  750. /* Handle non-volatile memory devices */
  751. opal_pdev_init("pmem-region");
  752. /* Setup a heatbeat thread if requested by OPAL */
  753. opal_init_heartbeat();
  754. /* Detect In-Memory Collection counters and create devices*/
  755. opal_imc_init_dev();
  756. /* Create leds platform devices */
  757. leds = of_find_node_by_path("/ibm,opal/leds");
  758. if (leds) {
  759. of_platform_device_create(leds, "opal_leds", NULL);
  760. of_node_put(leds);
  761. }
  762. /* Initialise OPAL message log interface */
  763. opal_msglog_init();
  764. /* Create "opal" kobject under /sys/firmware */
  765. rc = opal_sysfs_init();
  766. if (rc == 0) {
  767. /* Export symbol map to userspace */
  768. opal_export_symmap();
  769. /* Setup dump region interface */
  770. opal_dump_region_init();
  771. /* Setup error log interface */
  772. rc = opal_elog_init();
  773. /* Setup code update interface */
  774. opal_flash_update_init();
  775. /* Setup platform dump extract interface */
  776. opal_platform_dump_init();
  777. /* Setup system parameters interface */
  778. opal_sys_param_init();
  779. /* Setup message log sysfs interface. */
  780. opal_msglog_sysfs_init();
  781. }
  782. /* Export all properties */
  783. opal_export_attrs();
  784. /* Initialize platform devices: IPMI backend, PRD & flash interface */
  785. opal_pdev_init("ibm,opal-ipmi");
  786. opal_pdev_init("ibm,opal-flash");
  787. opal_pdev_init("ibm,opal-prd");
  788. /* Initialise platform device: oppanel interface */
  789. opal_pdev_init("ibm,opal-oppanel");
  790. /* Initialise OPAL kmsg dumper for flushing console on panic */
  791. opal_kmsg_init();
  792. /* Initialise OPAL powercap interface */
  793. opal_powercap_init();
  794. /* Initialise OPAL Power-Shifting-Ratio interface */
  795. opal_psr_init();
  796. /* Initialise OPAL sensor groups */
  797. opal_sensor_groups_init();
  798. return 0;
  799. }
  800. machine_subsys_initcall(powernv, opal_init);
  801. void opal_shutdown(void)
  802. {
  803. long rc = OPAL_BUSY;
  804. opal_event_shutdown();
  805. /*
  806. * Then sync with OPAL which ensure anything that can
  807. * potentially write to our memory has completed such
  808. * as an ongoing dump retrieval
  809. */
  810. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  811. rc = opal_sync_host_reboot();
  812. if (rc == OPAL_BUSY)
  813. opal_poll_events(NULL);
  814. else
  815. mdelay(10);
  816. }
  817. /* Unregister memory dump region */
  818. if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
  819. opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
  820. }
  821. /* Export this so that test modules can use it */
  822. EXPORT_SYMBOL_GPL(opal_invalid_call);
  823. EXPORT_SYMBOL_GPL(opal_xscom_read);
  824. EXPORT_SYMBOL_GPL(opal_xscom_write);
  825. EXPORT_SYMBOL_GPL(opal_ipmi_send);
  826. EXPORT_SYMBOL_GPL(opal_ipmi_recv);
  827. EXPORT_SYMBOL_GPL(opal_flash_read);
  828. EXPORT_SYMBOL_GPL(opal_flash_write);
  829. EXPORT_SYMBOL_GPL(opal_flash_erase);
  830. EXPORT_SYMBOL_GPL(opal_prd_msg);
  831. EXPORT_SYMBOL_GPL(opal_check_token);
  832. /* Convert a region of vmalloc memory to an opal sg list */
  833. struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
  834. unsigned long vmalloc_size)
  835. {
  836. struct opal_sg_list *sg, *first = NULL;
  837. unsigned long i = 0;
  838. sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
  839. if (!sg)
  840. goto nomem;
  841. first = sg;
  842. while (vmalloc_size > 0) {
  843. uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
  844. uint64_t length = min(vmalloc_size, PAGE_SIZE);
  845. sg->entry[i].data = cpu_to_be64(data);
  846. sg->entry[i].length = cpu_to_be64(length);
  847. i++;
  848. if (i >= SG_ENTRIES_PER_NODE) {
  849. struct opal_sg_list *next;
  850. next = kzalloc(PAGE_SIZE, GFP_KERNEL);
  851. if (!next)
  852. goto nomem;
  853. sg->length = cpu_to_be64(
  854. i * sizeof(struct opal_sg_entry) + 16);
  855. i = 0;
  856. sg->next = cpu_to_be64(__pa(next));
  857. sg = next;
  858. }
  859. vmalloc_addr += length;
  860. vmalloc_size -= length;
  861. }
  862. sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
  863. return first;
  864. nomem:
  865. pr_err("%s : Failed to allocate memory\n", __func__);
  866. opal_free_sg_list(first);
  867. return NULL;
  868. }
  869. void opal_free_sg_list(struct opal_sg_list *sg)
  870. {
  871. while (sg) {
  872. uint64_t next = be64_to_cpu(sg->next);
  873. kfree(sg);
  874. if (next)
  875. sg = __va(next);
  876. else
  877. sg = NULL;
  878. }
  879. }
  880. int opal_error_code(int rc)
  881. {
  882. switch (rc) {
  883. case OPAL_SUCCESS: return 0;
  884. case OPAL_PARAMETER: return -EINVAL;
  885. case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
  886. case OPAL_BUSY:
  887. case OPAL_BUSY_EVENT: return -EBUSY;
  888. case OPAL_NO_MEM: return -ENOMEM;
  889. case OPAL_PERMISSION: return -EPERM;
  890. case OPAL_UNSUPPORTED: return -EIO;
  891. case OPAL_HARDWARE: return -EIO;
  892. case OPAL_INTERNAL_ERROR: return -EIO;
  893. case OPAL_TIMEOUT: return -ETIMEDOUT;
  894. default:
  895. pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
  896. return -EIO;
  897. }
  898. }
  899. void powernv_set_nmmu_ptcr(unsigned long ptcr)
  900. {
  901. int rc;
  902. if (firmware_has_feature(FW_FEATURE_OPAL)) {
  903. rc = opal_nmmu_set_ptcr(-1UL, ptcr);
  904. if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
  905. pr_warn("%s: Unable to set nest mmu ptcr\n", __func__);
  906. }
  907. }
  908. EXPORT_SYMBOL_GPL(opal_poll_events);
  909. EXPORT_SYMBOL_GPL(opal_rtc_read);
  910. EXPORT_SYMBOL_GPL(opal_rtc_write);
  911. EXPORT_SYMBOL_GPL(opal_tpo_read);
  912. EXPORT_SYMBOL_GPL(opal_tpo_write);
  913. EXPORT_SYMBOL_GPL(opal_i2c_request);
  914. /* Export these symbols for PowerNV LED class driver */
  915. EXPORT_SYMBOL_GPL(opal_leds_get_ind);
  916. EXPORT_SYMBOL_GPL(opal_leds_set_ind);
  917. /* Export this symbol for PowerNV Operator Panel class driver */
  918. EXPORT_SYMBOL_GPL(opal_write_oppanel_async);
  919. /* Export this for KVM */
  920. EXPORT_SYMBOL_GPL(opal_int_set_mfrr);
  921. EXPORT_SYMBOL_GPL(opal_int_eoi);
  922. EXPORT_SYMBOL_GPL(opal_error_code);
  923. /* Export the below symbol for NX compression */
  924. EXPORT_SYMBOL(opal_nx_coproc_init);