opal.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. 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 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 struct bin_attribute symbol_map_attr = {
  575. .attr = {.name = "symbol_map", .mode = 0400},
  576. .read = symbol_map_read
  577. };
  578. static void opal_export_symmap(void)
  579. {
  580. const __be64 *syms;
  581. unsigned int size;
  582. struct device_node *fw;
  583. int rc;
  584. fw = of_find_node_by_path("/ibm,opal/firmware");
  585. if (!fw)
  586. return;
  587. syms = of_get_property(fw, "symbol-map", &size);
  588. if (!syms || size != 2 * sizeof(__be64))
  589. return;
  590. /* Setup attributes */
  591. symbol_map_attr.private = __va(be64_to_cpu(syms[0]));
  592. symbol_map_attr.size = be64_to_cpu(syms[1]);
  593. rc = sysfs_create_bin_file(opal_kobj, &symbol_map_attr);
  594. if (rc)
  595. pr_warn("Error %d creating OPAL symbols file\n", rc);
  596. }
  597. static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
  598. struct bin_attribute *bin_attr, char *buf,
  599. loff_t off, size_t count)
  600. {
  601. return memory_read_from_buffer(buf, count, &off, bin_attr->private,
  602. bin_attr->size);
  603. }
  604. /*
  605. * opal_export_attrs: creates a sysfs node for each property listed in
  606. * the device-tree under /ibm,opal/firmware/exports/
  607. * All new sysfs nodes are created under /opal/exports/.
  608. * This allows for reserved memory regions (e.g. HDAT) to be read.
  609. * The new sysfs nodes are only readable by root.
  610. */
  611. static void opal_export_attrs(void)
  612. {
  613. struct bin_attribute *attr;
  614. struct device_node *np;
  615. struct property *prop;
  616. struct kobject *kobj;
  617. u64 vals[2];
  618. int rc;
  619. np = of_find_node_by_path("/ibm,opal/firmware/exports");
  620. if (!np)
  621. return;
  622. /* Create new 'exports' directory - /sys/firmware/opal/exports */
  623. kobj = kobject_create_and_add("exports", opal_kobj);
  624. if (!kobj) {
  625. pr_warn("kobject_create_and_add() of exports failed\n");
  626. return;
  627. }
  628. for_each_property_of_node(np, prop) {
  629. if (!strcmp(prop->name, "name") || !strcmp(prop->name, "phandle"))
  630. continue;
  631. if (of_property_read_u64_array(np, prop->name, &vals[0], 2))
  632. continue;
  633. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  634. if (attr == NULL) {
  635. pr_warn("Failed kmalloc for bin_attribute!");
  636. continue;
  637. }
  638. sysfs_bin_attr_init(attr);
  639. attr->attr.name = kstrdup(prop->name, GFP_KERNEL);
  640. attr->attr.mode = 0400;
  641. attr->read = export_attr_read;
  642. attr->private = __va(vals[0]);
  643. attr->size = vals[1];
  644. if (attr->attr.name == NULL) {
  645. pr_warn("Failed kstrdup for bin_attribute attr.name");
  646. kfree(attr);
  647. continue;
  648. }
  649. rc = sysfs_create_bin_file(kobj, attr);
  650. if (rc) {
  651. pr_warn("Error %d creating OPAL sysfs exports/%s file\n",
  652. rc, prop->name);
  653. kfree(attr->attr.name);
  654. kfree(attr);
  655. }
  656. }
  657. of_node_put(np);
  658. }
  659. static void __init opal_dump_region_init(void)
  660. {
  661. void *addr;
  662. uint64_t size;
  663. int rc;
  664. if (!opal_check_token(OPAL_REGISTER_DUMP_REGION))
  665. return;
  666. /* Register kernel log buffer */
  667. addr = log_buf_addr_get();
  668. if (addr == NULL)
  669. return;
  670. size = log_buf_len_get();
  671. if (size == 0)
  672. return;
  673. rc = opal_register_dump_region(OPAL_DUMP_REGION_LOG_BUF,
  674. __pa(addr), size);
  675. /* Don't warn if this is just an older OPAL that doesn't
  676. * know about that call
  677. */
  678. if (rc && rc != OPAL_UNSUPPORTED)
  679. pr_warn("DUMP: Failed to register kernel log buffer. "
  680. "rc = %d\n", rc);
  681. }
  682. static void opal_pdev_init(const char *compatible)
  683. {
  684. struct device_node *np;
  685. for_each_compatible_node(np, NULL, compatible)
  686. of_platform_device_create(np, NULL, NULL);
  687. }
  688. static void __init opal_imc_init_dev(void)
  689. {
  690. struct device_node *np;
  691. np = of_find_compatible_node(NULL, NULL, IMC_DTB_COMPAT);
  692. if (np)
  693. of_platform_device_create(np, NULL, NULL);
  694. }
  695. static int kopald(void *unused)
  696. {
  697. unsigned long timeout = msecs_to_jiffies(opal_heartbeat) + 1;
  698. set_freezable();
  699. do {
  700. try_to_freeze();
  701. opal_handle_events();
  702. set_current_state(TASK_INTERRUPTIBLE);
  703. if (opal_have_pending_events())
  704. __set_current_state(TASK_RUNNING);
  705. else
  706. schedule_timeout(timeout);
  707. } while (!kthread_should_stop());
  708. return 0;
  709. }
  710. void opal_wake_poller(void)
  711. {
  712. if (kopald_tsk)
  713. wake_up_process(kopald_tsk);
  714. }
  715. static void opal_init_heartbeat(void)
  716. {
  717. /* Old firwmware, we assume the HVC heartbeat is sufficient */
  718. if (of_property_read_u32(opal_node, "ibm,heartbeat-ms",
  719. &opal_heartbeat) != 0)
  720. opal_heartbeat = 0;
  721. if (opal_heartbeat)
  722. kopald_tsk = kthread_run(kopald, NULL, "kopald");
  723. }
  724. static int __init opal_init(void)
  725. {
  726. struct device_node *np, *consoles, *leds;
  727. int rc;
  728. opal_node = of_find_node_by_path("/ibm,opal");
  729. if (!opal_node) {
  730. pr_warn("Device node not found\n");
  731. return -ENODEV;
  732. }
  733. /* Register OPAL consoles if any ports */
  734. consoles = of_find_node_by_path("/ibm,opal/consoles");
  735. if (consoles) {
  736. for_each_child_of_node(consoles, np) {
  737. if (strcmp(np->name, "serial"))
  738. continue;
  739. of_platform_device_create(np, NULL, NULL);
  740. }
  741. of_node_put(consoles);
  742. }
  743. /* Initialise OPAL messaging system */
  744. opal_message_init();
  745. /* Initialise OPAL asynchronous completion interface */
  746. opal_async_comp_init();
  747. /* Initialise OPAL sensor interface */
  748. opal_sensor_init();
  749. /* Initialise OPAL hypervisor maintainence interrupt handling */
  750. opal_hmi_handler_init();
  751. /* Create i2c platform devices */
  752. opal_pdev_init("ibm,opal-i2c");
  753. /* Handle non-volatile memory devices */
  754. opal_pdev_init("pmem-region");
  755. /* Setup a heatbeat thread if requested by OPAL */
  756. opal_init_heartbeat();
  757. /* Detect In-Memory Collection counters and create devices*/
  758. opal_imc_init_dev();
  759. /* Create leds platform devices */
  760. leds = of_find_node_by_path("/ibm,opal/leds");
  761. if (leds) {
  762. of_platform_device_create(leds, "opal_leds", NULL);
  763. of_node_put(leds);
  764. }
  765. /* Initialise OPAL message log interface */
  766. opal_msglog_init();
  767. /* Create "opal" kobject under /sys/firmware */
  768. rc = opal_sysfs_init();
  769. if (rc == 0) {
  770. /* Export symbol map to userspace */
  771. opal_export_symmap();
  772. /* Setup dump region interface */
  773. opal_dump_region_init();
  774. /* Setup error log interface */
  775. rc = opal_elog_init();
  776. /* Setup code update interface */
  777. opal_flash_update_init();
  778. /* Setup platform dump extract interface */
  779. opal_platform_dump_init();
  780. /* Setup system parameters interface */
  781. opal_sys_param_init();
  782. /* Setup message log sysfs interface. */
  783. opal_msglog_sysfs_init();
  784. }
  785. /* Export all properties */
  786. opal_export_attrs();
  787. /* Initialize platform devices: IPMI backend, PRD & flash interface */
  788. opal_pdev_init("ibm,opal-ipmi");
  789. opal_pdev_init("ibm,opal-flash");
  790. opal_pdev_init("ibm,opal-prd");
  791. /* Initialise platform device: oppanel interface */
  792. opal_pdev_init("ibm,opal-oppanel");
  793. /* Initialise OPAL kmsg dumper for flushing console on panic */
  794. opal_kmsg_init();
  795. /* Initialise OPAL powercap interface */
  796. opal_powercap_init();
  797. /* Initialise OPAL Power-Shifting-Ratio interface */
  798. opal_psr_init();
  799. /* Initialise OPAL sensor groups */
  800. opal_sensor_groups_init();
  801. return 0;
  802. }
  803. machine_subsys_initcall(powernv, opal_init);
  804. void opal_shutdown(void)
  805. {
  806. long rc = OPAL_BUSY;
  807. opal_event_shutdown();
  808. /*
  809. * Then sync with OPAL which ensure anything that can
  810. * potentially write to our memory has completed such
  811. * as an ongoing dump retrieval
  812. */
  813. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  814. rc = opal_sync_host_reboot();
  815. if (rc == OPAL_BUSY)
  816. opal_poll_events(NULL);
  817. else
  818. mdelay(10);
  819. }
  820. /* Unregister memory dump region */
  821. if (opal_check_token(OPAL_UNREGISTER_DUMP_REGION))
  822. opal_unregister_dump_region(OPAL_DUMP_REGION_LOG_BUF);
  823. }
  824. /* Export this so that test modules can use it */
  825. EXPORT_SYMBOL_GPL(opal_invalid_call);
  826. EXPORT_SYMBOL_GPL(opal_xscom_read);
  827. EXPORT_SYMBOL_GPL(opal_xscom_write);
  828. EXPORT_SYMBOL_GPL(opal_ipmi_send);
  829. EXPORT_SYMBOL_GPL(opal_ipmi_recv);
  830. EXPORT_SYMBOL_GPL(opal_flash_read);
  831. EXPORT_SYMBOL_GPL(opal_flash_write);
  832. EXPORT_SYMBOL_GPL(opal_flash_erase);
  833. EXPORT_SYMBOL_GPL(opal_prd_msg);
  834. EXPORT_SYMBOL_GPL(opal_check_token);
  835. /* Convert a region of vmalloc memory to an opal sg list */
  836. struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,
  837. unsigned long vmalloc_size)
  838. {
  839. struct opal_sg_list *sg, *first = NULL;
  840. unsigned long i = 0;
  841. sg = kzalloc(PAGE_SIZE, GFP_KERNEL);
  842. if (!sg)
  843. goto nomem;
  844. first = sg;
  845. while (vmalloc_size > 0) {
  846. uint64_t data = vmalloc_to_pfn(vmalloc_addr) << PAGE_SHIFT;
  847. uint64_t length = min(vmalloc_size, PAGE_SIZE);
  848. sg->entry[i].data = cpu_to_be64(data);
  849. sg->entry[i].length = cpu_to_be64(length);
  850. i++;
  851. if (i >= SG_ENTRIES_PER_NODE) {
  852. struct opal_sg_list *next;
  853. next = kzalloc(PAGE_SIZE, GFP_KERNEL);
  854. if (!next)
  855. goto nomem;
  856. sg->length = cpu_to_be64(
  857. i * sizeof(struct opal_sg_entry) + 16);
  858. i = 0;
  859. sg->next = cpu_to_be64(__pa(next));
  860. sg = next;
  861. }
  862. vmalloc_addr += length;
  863. vmalloc_size -= length;
  864. }
  865. sg->length = cpu_to_be64(i * sizeof(struct opal_sg_entry) + 16);
  866. return first;
  867. nomem:
  868. pr_err("%s : Failed to allocate memory\n", __func__);
  869. opal_free_sg_list(first);
  870. return NULL;
  871. }
  872. void opal_free_sg_list(struct opal_sg_list *sg)
  873. {
  874. while (sg) {
  875. uint64_t next = be64_to_cpu(sg->next);
  876. kfree(sg);
  877. if (next)
  878. sg = __va(next);
  879. else
  880. sg = NULL;
  881. }
  882. }
  883. int opal_error_code(int rc)
  884. {
  885. switch (rc) {
  886. case OPAL_SUCCESS: return 0;
  887. case OPAL_PARAMETER: return -EINVAL;
  888. case OPAL_ASYNC_COMPLETION: return -EINPROGRESS;
  889. case OPAL_BUSY:
  890. case OPAL_BUSY_EVENT: return -EBUSY;
  891. case OPAL_NO_MEM: return -ENOMEM;
  892. case OPAL_PERMISSION: return -EPERM;
  893. case OPAL_UNSUPPORTED: return -EIO;
  894. case OPAL_HARDWARE: return -EIO;
  895. case OPAL_INTERNAL_ERROR: return -EIO;
  896. case OPAL_TIMEOUT: return -ETIMEDOUT;
  897. default:
  898. pr_err("%s: unexpected OPAL error %d\n", __func__, rc);
  899. return -EIO;
  900. }
  901. }
  902. void powernv_set_nmmu_ptcr(unsigned long ptcr)
  903. {
  904. int rc;
  905. if (firmware_has_feature(FW_FEATURE_OPAL)) {
  906. rc = opal_nmmu_set_ptcr(-1UL, ptcr);
  907. if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
  908. pr_warn("%s: Unable to set nest mmu ptcr\n", __func__);
  909. }
  910. }
  911. EXPORT_SYMBOL_GPL(opal_poll_events);
  912. EXPORT_SYMBOL_GPL(opal_rtc_read);
  913. EXPORT_SYMBOL_GPL(opal_rtc_write);
  914. EXPORT_SYMBOL_GPL(opal_tpo_read);
  915. EXPORT_SYMBOL_GPL(opal_tpo_write);
  916. EXPORT_SYMBOL_GPL(opal_i2c_request);
  917. /* Export these symbols for PowerNV LED class driver */
  918. EXPORT_SYMBOL_GPL(opal_leds_get_ind);
  919. EXPORT_SYMBOL_GPL(opal_leds_set_ind);
  920. /* Export this symbol for PowerNV Operator Panel class driver */
  921. EXPORT_SYMBOL_GPL(opal_write_oppanel_async);
  922. /* Export this for KVM */
  923. EXPORT_SYMBOL_GPL(opal_int_set_mfrr);
  924. EXPORT_SYMBOL_GPL(opal_int_eoi);
  925. EXPORT_SYMBOL_GPL(opal_error_code);
  926. /* Export the below symbol for NX compression */
  927. EXPORT_SYMBOL(opal_nx_coproc_init);