opal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #undef DEBUG
  12. #include <linux/types.h>
  13. #include <linux/of.h>
  14. #include <linux/of_fdt.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/notifier.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/kobject.h>
  21. #include <linux/delay.h>
  22. #include <asm/opal.h>
  23. #include <asm/firmware.h>
  24. #include <asm/mce.h>
  25. #include "powernv.h"
  26. /* /sys/firmware/opal */
  27. struct kobject *opal_kobj;
  28. struct opal {
  29. u64 base;
  30. u64 entry;
  31. } opal;
  32. static struct device_node *opal_node;
  33. static DEFINE_SPINLOCK(opal_write_lock);
  34. extern u64 opal_mc_secondary_handler[];
  35. static unsigned int *opal_irqs;
  36. static unsigned int opal_irq_count;
  37. static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
  38. static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
  39. static DEFINE_SPINLOCK(opal_notifier_lock);
  40. static uint64_t last_notified_mask = 0x0ul;
  41. static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
  42. int __init early_init_dt_scan_opal(unsigned long node,
  43. const char *uname, int depth, void *data)
  44. {
  45. const void *basep, *entryp;
  46. unsigned long basesz, entrysz;
  47. if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
  48. return 0;
  49. basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
  50. entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
  51. if (!basep || !entryp)
  52. return 1;
  53. opal.base = of_read_number(basep, basesz/4);
  54. opal.entry = of_read_number(entryp, entrysz/4);
  55. pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
  56. opal.base, basep, basesz);
  57. pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
  58. opal.entry, entryp, entrysz);
  59. powerpc_firmware_features |= FW_FEATURE_OPAL;
  60. if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
  61. powerpc_firmware_features |= FW_FEATURE_OPALv2;
  62. powerpc_firmware_features |= FW_FEATURE_OPALv3;
  63. printk("OPAL V3 detected !\n");
  64. } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
  65. powerpc_firmware_features |= FW_FEATURE_OPALv2;
  66. printk("OPAL V2 detected !\n");
  67. } else {
  68. printk("OPAL V1 detected !\n");
  69. }
  70. return 1;
  71. }
  72. static int __init opal_register_exception_handlers(void)
  73. {
  74. #ifdef __BIG_ENDIAN__
  75. u64 glue;
  76. if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
  77. return -ENODEV;
  78. /* Hookup some exception handlers except machine check. We use the
  79. * fwnmi area at 0x7000 to provide the glue space to OPAL
  80. */
  81. glue = 0x7000;
  82. opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
  83. 0, glue);
  84. glue += 128;
  85. opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
  86. #endif
  87. return 0;
  88. }
  89. early_initcall(opal_register_exception_handlers);
  90. int opal_notifier_register(struct notifier_block *nb)
  91. {
  92. if (!nb) {
  93. pr_warning("%s: Invalid argument (%p)\n",
  94. __func__, nb);
  95. return -EINVAL;
  96. }
  97. atomic_notifier_chain_register(&opal_notifier_head, nb);
  98. return 0;
  99. }
  100. static void opal_do_notifier(uint64_t events)
  101. {
  102. unsigned long flags;
  103. uint64_t changed_mask;
  104. if (atomic_read(&opal_notifier_hold))
  105. return;
  106. spin_lock_irqsave(&opal_notifier_lock, flags);
  107. changed_mask = last_notified_mask ^ events;
  108. last_notified_mask = events;
  109. spin_unlock_irqrestore(&opal_notifier_lock, flags);
  110. /*
  111. * We feed with the event bits and changed bits for
  112. * enough information to the callback.
  113. */
  114. atomic_notifier_call_chain(&opal_notifier_head,
  115. events, (void *)changed_mask);
  116. }
  117. void opal_notifier_update_evt(uint64_t evt_mask,
  118. uint64_t evt_val)
  119. {
  120. unsigned long flags;
  121. spin_lock_irqsave(&opal_notifier_lock, flags);
  122. last_notified_mask &= ~evt_mask;
  123. last_notified_mask |= evt_val;
  124. spin_unlock_irqrestore(&opal_notifier_lock, flags);
  125. }
  126. void opal_notifier_enable(void)
  127. {
  128. int64_t rc;
  129. uint64_t evt = 0;
  130. atomic_set(&opal_notifier_hold, 0);
  131. /* Process pending events */
  132. rc = opal_poll_events(&evt);
  133. if (rc == OPAL_SUCCESS && evt)
  134. opal_do_notifier(evt);
  135. }
  136. void opal_notifier_disable(void)
  137. {
  138. atomic_set(&opal_notifier_hold, 1);
  139. }
  140. /*
  141. * Opal message notifier based on message type. Allow subscribers to get
  142. * notified for specific messgae type.
  143. */
  144. int opal_message_notifier_register(enum OpalMessageType msg_type,
  145. struct notifier_block *nb)
  146. {
  147. if (!nb) {
  148. pr_warning("%s: Invalid argument (%p)\n",
  149. __func__, nb);
  150. return -EINVAL;
  151. }
  152. if (msg_type > OPAL_MSG_TYPE_MAX) {
  153. pr_warning("%s: Invalid message type argument (%d)\n",
  154. __func__, msg_type);
  155. return -EINVAL;
  156. }
  157. return atomic_notifier_chain_register(
  158. &opal_msg_notifier_head[msg_type], nb);
  159. }
  160. static void opal_message_do_notify(uint32_t msg_type, void *msg)
  161. {
  162. /* notify subscribers */
  163. atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
  164. msg_type, msg);
  165. }
  166. static void opal_handle_message(void)
  167. {
  168. s64 ret;
  169. /*
  170. * TODO: pre-allocate a message buffer depending on opal-msg-size
  171. * value in /proc/device-tree.
  172. */
  173. static struct opal_msg msg;
  174. ret = opal_get_msg(__pa(&msg), sizeof(msg));
  175. /* No opal message pending. */
  176. if (ret == OPAL_RESOURCE)
  177. return;
  178. /* check for errors. */
  179. if (ret) {
  180. pr_warning("%s: Failed to retrive opal message, err=%lld\n",
  181. __func__, ret);
  182. return;
  183. }
  184. /* Sanity check */
  185. if (msg.msg_type > OPAL_MSG_TYPE_MAX) {
  186. pr_warning("%s: Unknown message type: %u\n",
  187. __func__, msg.msg_type);
  188. return;
  189. }
  190. opal_message_do_notify(msg.msg_type, (void *)&msg);
  191. }
  192. static int opal_message_notify(struct notifier_block *nb,
  193. unsigned long events, void *change)
  194. {
  195. if (events & OPAL_EVENT_MSG_PENDING)
  196. opal_handle_message();
  197. return 0;
  198. }
  199. static struct notifier_block opal_message_nb = {
  200. .notifier_call = opal_message_notify,
  201. .next = NULL,
  202. .priority = 0,
  203. };
  204. static int __init opal_message_init(void)
  205. {
  206. int ret, i;
  207. for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
  208. ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
  209. ret = opal_notifier_register(&opal_message_nb);
  210. if (ret) {
  211. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  212. __func__, ret);
  213. return ret;
  214. }
  215. return 0;
  216. }
  217. early_initcall(opal_message_init);
  218. int opal_get_chars(uint32_t vtermno, char *buf, int count)
  219. {
  220. s64 rc;
  221. __be64 evt, len;
  222. if (!opal.entry)
  223. return -ENODEV;
  224. opal_poll_events(&evt);
  225. if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
  226. return 0;
  227. len = cpu_to_be64(count);
  228. rc = opal_console_read(vtermno, &len, buf);
  229. if (rc == OPAL_SUCCESS)
  230. return be64_to_cpu(len);
  231. return 0;
  232. }
  233. int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
  234. {
  235. int written = 0;
  236. __be64 olen;
  237. s64 len, rc;
  238. unsigned long flags;
  239. __be64 evt;
  240. if (!opal.entry)
  241. return -ENODEV;
  242. /* We want put_chars to be atomic to avoid mangling of hvsi
  243. * packets. To do that, we first test for room and return
  244. * -EAGAIN if there isn't enough.
  245. *
  246. * Unfortunately, opal_console_write_buffer_space() doesn't
  247. * appear to work on opal v1, so we just assume there is
  248. * enough room and be done with it
  249. */
  250. spin_lock_irqsave(&opal_write_lock, flags);
  251. if (firmware_has_feature(FW_FEATURE_OPALv2)) {
  252. rc = opal_console_write_buffer_space(vtermno, &olen);
  253. len = be64_to_cpu(olen);
  254. if (rc || len < total_len) {
  255. spin_unlock_irqrestore(&opal_write_lock, flags);
  256. /* Closed -> drop characters */
  257. if (rc)
  258. return total_len;
  259. opal_poll_events(NULL);
  260. return -EAGAIN;
  261. }
  262. }
  263. /* We still try to handle partial completions, though they
  264. * should no longer happen.
  265. */
  266. rc = OPAL_BUSY;
  267. while(total_len > 0 && (rc == OPAL_BUSY ||
  268. rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
  269. olen = cpu_to_be64(total_len);
  270. rc = opal_console_write(vtermno, &olen, data);
  271. len = be64_to_cpu(olen);
  272. /* Closed or other error drop */
  273. if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
  274. rc != OPAL_BUSY_EVENT) {
  275. written = total_len;
  276. break;
  277. }
  278. if (rc == OPAL_SUCCESS) {
  279. total_len -= len;
  280. data += len;
  281. written += len;
  282. }
  283. /* This is a bit nasty but we need that for the console to
  284. * flush when there aren't any interrupts. We will clean
  285. * things a bit later to limit that to synchronous path
  286. * such as the kernel console and xmon/udbg
  287. */
  288. do
  289. opal_poll_events(&evt);
  290. while(rc == OPAL_SUCCESS &&
  291. (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
  292. }
  293. spin_unlock_irqrestore(&opal_write_lock, flags);
  294. return written;
  295. }
  296. static int opal_recover_mce(struct pt_regs *regs,
  297. struct machine_check_event *evt)
  298. {
  299. int recovered = 0;
  300. uint64_t ea = get_mce_fault_addr(evt);
  301. if (!(regs->msr & MSR_RI)) {
  302. /* If MSR_RI isn't set, we cannot recover */
  303. recovered = 0;
  304. } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
  305. /* Platform corrected itself */
  306. recovered = 1;
  307. } else if (ea && !is_kernel_addr(ea)) {
  308. /*
  309. * Faulting address is not in kernel text. We should be fine.
  310. * We need to find which process uses this address.
  311. * For now, kill the task if we have received exception when
  312. * in userspace.
  313. *
  314. * TODO: Queue up this address for hwpoisioning later.
  315. */
  316. if (user_mode(regs) && !is_global_init(current)) {
  317. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  318. recovered = 1;
  319. } else
  320. recovered = 0;
  321. } else if (user_mode(regs) && !is_global_init(current) &&
  322. evt->severity == MCE_SEV_ERROR_SYNC) {
  323. /*
  324. * If we have received a synchronous error when in userspace
  325. * kill the task.
  326. */
  327. _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
  328. recovered = 1;
  329. }
  330. return recovered;
  331. }
  332. int opal_machine_check(struct pt_regs *regs)
  333. {
  334. struct machine_check_event evt;
  335. if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
  336. return 0;
  337. /* Print things out */
  338. if (evt.version != MCE_V1) {
  339. pr_err("Machine Check Exception, Unknown event version %d !\n",
  340. evt.version);
  341. return 0;
  342. }
  343. machine_check_print_event_info(&evt);
  344. if (opal_recover_mce(regs, &evt))
  345. return 1;
  346. return 0;
  347. }
  348. static irqreturn_t opal_interrupt(int irq, void *data)
  349. {
  350. __be64 events;
  351. opal_handle_interrupt(virq_to_hw(irq), &events);
  352. opal_do_notifier(events);
  353. return IRQ_HANDLED;
  354. }
  355. static int opal_sysfs_init(void)
  356. {
  357. opal_kobj = kobject_create_and_add("opal", firmware_kobj);
  358. if (!opal_kobj) {
  359. pr_warn("kobject_create_and_add opal failed\n");
  360. return -ENOMEM;
  361. }
  362. return 0;
  363. }
  364. static int __init opal_init(void)
  365. {
  366. struct device_node *np, *consoles;
  367. const __be32 *irqs;
  368. int rc, i, irqlen;
  369. opal_node = of_find_node_by_path("/ibm,opal");
  370. if (!opal_node) {
  371. pr_warn("opal: Node not found\n");
  372. return -ENODEV;
  373. }
  374. /* Register OPAL consoles if any ports */
  375. if (firmware_has_feature(FW_FEATURE_OPALv2))
  376. consoles = of_find_node_by_path("/ibm,opal/consoles");
  377. else
  378. consoles = of_node_get(opal_node);
  379. if (consoles) {
  380. for_each_child_of_node(consoles, np) {
  381. if (strcmp(np->name, "serial"))
  382. continue;
  383. of_platform_device_create(np, NULL, NULL);
  384. }
  385. of_node_put(consoles);
  386. }
  387. /* Find all OPAL interrupts and request them */
  388. irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
  389. pr_debug("opal: Found %d interrupts reserved for OPAL\n",
  390. irqs ? (irqlen / 4) : 0);
  391. opal_irq_count = irqlen / 4;
  392. opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
  393. for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
  394. unsigned int hwirq = be32_to_cpup(irqs);
  395. unsigned int irq = irq_create_mapping(NULL, hwirq);
  396. if (irq == NO_IRQ) {
  397. pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
  398. continue;
  399. }
  400. rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
  401. if (rc)
  402. pr_warning("opal: Error %d requesting irq %d"
  403. " (0x%x)\n", rc, irq, hwirq);
  404. opal_irqs[i] = irq;
  405. }
  406. /* Create "opal" kobject under /sys/firmware */
  407. rc = opal_sysfs_init();
  408. if (rc == 0) {
  409. /* Setup code update interface */
  410. opal_flash_init();
  411. }
  412. return 0;
  413. }
  414. subsys_initcall(opal_init);
  415. void opal_shutdown(void)
  416. {
  417. unsigned int i;
  418. long rc = OPAL_BUSY;
  419. /* First free interrupts, which will also mask them */
  420. for (i = 0; i < opal_irq_count; i++) {
  421. if (opal_irqs[i])
  422. free_irq(opal_irqs[i], NULL);
  423. opal_irqs[i] = 0;
  424. }
  425. /*
  426. * Then sync with OPAL which ensure anything that can
  427. * potentially write to our memory has completed such
  428. * as an ongoing dump retrieval
  429. */
  430. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  431. rc = opal_sync_host_reboot();
  432. if (rc == OPAL_BUSY)
  433. opal_poll_events(NULL);
  434. else
  435. mdelay(10);
  436. }
  437. }