opal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/kobject.h>
  20. #include <asm/opal.h>
  21. #include <asm/firmware.h>
  22. #include "powernv.h"
  23. /* /sys/firmware/opal */
  24. struct kobject *opal_kobj;
  25. struct opal {
  26. u64 base;
  27. u64 entry;
  28. } opal;
  29. static struct device_node *opal_node;
  30. static DEFINE_SPINLOCK(opal_write_lock);
  31. extern u64 opal_mc_secondary_handler[];
  32. static unsigned int *opal_irqs;
  33. static unsigned int opal_irq_count;
  34. static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
  35. static DEFINE_SPINLOCK(opal_notifier_lock);
  36. static uint64_t last_notified_mask = 0x0ul;
  37. static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
  38. int __init early_init_dt_scan_opal(unsigned long node,
  39. const char *uname, int depth, void *data)
  40. {
  41. const void *basep, *entryp;
  42. unsigned long basesz, entrysz;
  43. if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
  44. return 0;
  45. basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
  46. entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
  47. if (!basep || !entryp)
  48. return 1;
  49. opal.base = of_read_number(basep, basesz/4);
  50. opal.entry = of_read_number(entryp, entrysz/4);
  51. pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
  52. opal.base, basep, basesz);
  53. pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
  54. opal.entry, entryp, entrysz);
  55. powerpc_firmware_features |= FW_FEATURE_OPAL;
  56. if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
  57. powerpc_firmware_features |= FW_FEATURE_OPALv2;
  58. powerpc_firmware_features |= FW_FEATURE_OPALv3;
  59. printk("OPAL V3 detected !\n");
  60. } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
  61. powerpc_firmware_features |= FW_FEATURE_OPALv2;
  62. printk("OPAL V2 detected !\n");
  63. } else {
  64. printk("OPAL V1 detected !\n");
  65. }
  66. return 1;
  67. }
  68. static int __init opal_register_exception_handlers(void)
  69. {
  70. #ifdef __BIG_ENDIAN__
  71. u64 glue;
  72. if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
  73. return -ENODEV;
  74. /* Hookup some exception handlers. We use the fwnmi area at 0x7000
  75. * to provide the glue space to OPAL
  76. */
  77. glue = 0x7000;
  78. opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
  79. __pa(opal_mc_secondary_handler[0]),
  80. glue);
  81. glue += 128;
  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. int opal_get_chars(uint32_t vtermno, char *buf, int count)
  141. {
  142. s64 rc;
  143. __be64 evt, len;
  144. if (!opal.entry)
  145. return -ENODEV;
  146. opal_poll_events(&evt);
  147. if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
  148. return 0;
  149. len = cpu_to_be64(count);
  150. rc = opal_console_read(vtermno, &len, buf);
  151. if (rc == OPAL_SUCCESS)
  152. return be64_to_cpu(len);
  153. return 0;
  154. }
  155. int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
  156. {
  157. int written = 0;
  158. __be64 olen;
  159. s64 len, rc;
  160. unsigned long flags;
  161. __be64 evt;
  162. if (!opal.entry)
  163. return -ENODEV;
  164. /* We want put_chars to be atomic to avoid mangling of hvsi
  165. * packets. To do that, we first test for room and return
  166. * -EAGAIN if there isn't enough.
  167. *
  168. * Unfortunately, opal_console_write_buffer_space() doesn't
  169. * appear to work on opal v1, so we just assume there is
  170. * enough room and be done with it
  171. */
  172. spin_lock_irqsave(&opal_write_lock, flags);
  173. if (firmware_has_feature(FW_FEATURE_OPALv2)) {
  174. rc = opal_console_write_buffer_space(vtermno, &olen);
  175. len = be64_to_cpu(olen);
  176. if (rc || len < total_len) {
  177. spin_unlock_irqrestore(&opal_write_lock, flags);
  178. /* Closed -> drop characters */
  179. if (rc)
  180. return total_len;
  181. opal_poll_events(NULL);
  182. return -EAGAIN;
  183. }
  184. }
  185. /* We still try to handle partial completions, though they
  186. * should no longer happen.
  187. */
  188. rc = OPAL_BUSY;
  189. while(total_len > 0 && (rc == OPAL_BUSY ||
  190. rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
  191. olen = cpu_to_be64(total_len);
  192. rc = opal_console_write(vtermno, &olen, data);
  193. len = be64_to_cpu(olen);
  194. /* Closed or other error drop */
  195. if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
  196. rc != OPAL_BUSY_EVENT) {
  197. written = total_len;
  198. break;
  199. }
  200. if (rc == OPAL_SUCCESS) {
  201. total_len -= len;
  202. data += len;
  203. written += len;
  204. }
  205. /* This is a bit nasty but we need that for the console to
  206. * flush when there aren't any interrupts. We will clean
  207. * things a bit later to limit that to synchronous path
  208. * such as the kernel console and xmon/udbg
  209. */
  210. do
  211. opal_poll_events(&evt);
  212. while(rc == OPAL_SUCCESS &&
  213. (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
  214. }
  215. spin_unlock_irqrestore(&opal_write_lock, flags);
  216. return written;
  217. }
  218. int opal_machine_check(struct pt_regs *regs)
  219. {
  220. struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
  221. struct opal_machine_check_event evt;
  222. const char *level, *sevstr, *subtype;
  223. static const char *opal_mc_ue_types[] = {
  224. "Indeterminate",
  225. "Instruction fetch",
  226. "Page table walk ifetch",
  227. "Load/Store",
  228. "Page table walk Load/Store",
  229. };
  230. static const char *opal_mc_slb_types[] = {
  231. "Indeterminate",
  232. "Parity",
  233. "Multihit",
  234. };
  235. static const char *opal_mc_erat_types[] = {
  236. "Indeterminate",
  237. "Parity",
  238. "Multihit",
  239. };
  240. static const char *opal_mc_tlb_types[] = {
  241. "Indeterminate",
  242. "Parity",
  243. "Multihit",
  244. };
  245. /* Copy the event structure and release the original */
  246. evt = *opal_evt;
  247. opal_evt->in_use = 0;
  248. /* Print things out */
  249. if (evt.version != OpalMCE_V1) {
  250. pr_err("Machine Check Exception, Unknown event version %d !\n",
  251. evt.version);
  252. return 0;
  253. }
  254. switch(evt.severity) {
  255. case OpalMCE_SEV_NO_ERROR:
  256. level = KERN_INFO;
  257. sevstr = "Harmless";
  258. break;
  259. case OpalMCE_SEV_WARNING:
  260. level = KERN_WARNING;
  261. sevstr = "";
  262. break;
  263. case OpalMCE_SEV_ERROR_SYNC:
  264. level = KERN_ERR;
  265. sevstr = "Severe";
  266. break;
  267. case OpalMCE_SEV_FATAL:
  268. default:
  269. level = KERN_ERR;
  270. sevstr = "Fatal";
  271. break;
  272. }
  273. printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
  274. evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
  275. "Recovered" : "[Not recovered");
  276. printk("%s Initiator: %s\n", level,
  277. evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
  278. switch(evt.error_type) {
  279. case OpalMCE_ERROR_TYPE_UE:
  280. subtype = evt.u.ue_error.ue_error_type <
  281. ARRAY_SIZE(opal_mc_ue_types) ?
  282. opal_mc_ue_types[evt.u.ue_error.ue_error_type]
  283. : "Unknown";
  284. printk("%s Error type: UE [%s]\n", level, subtype);
  285. if (evt.u.ue_error.effective_address_provided)
  286. printk("%s Effective address: %016llx\n",
  287. level, evt.u.ue_error.effective_address);
  288. if (evt.u.ue_error.physical_address_provided)
  289. printk("%s Physial address: %016llx\n",
  290. level, evt.u.ue_error.physical_address);
  291. break;
  292. case OpalMCE_ERROR_TYPE_SLB:
  293. subtype = evt.u.slb_error.slb_error_type <
  294. ARRAY_SIZE(opal_mc_slb_types) ?
  295. opal_mc_slb_types[evt.u.slb_error.slb_error_type]
  296. : "Unknown";
  297. printk("%s Error type: SLB [%s]\n", level, subtype);
  298. if (evt.u.slb_error.effective_address_provided)
  299. printk("%s Effective address: %016llx\n",
  300. level, evt.u.slb_error.effective_address);
  301. break;
  302. case OpalMCE_ERROR_TYPE_ERAT:
  303. subtype = evt.u.erat_error.erat_error_type <
  304. ARRAY_SIZE(opal_mc_erat_types) ?
  305. opal_mc_erat_types[evt.u.erat_error.erat_error_type]
  306. : "Unknown";
  307. printk("%s Error type: ERAT [%s]\n", level, subtype);
  308. if (evt.u.erat_error.effective_address_provided)
  309. printk("%s Effective address: %016llx\n",
  310. level, evt.u.erat_error.effective_address);
  311. break;
  312. case OpalMCE_ERROR_TYPE_TLB:
  313. subtype = evt.u.tlb_error.tlb_error_type <
  314. ARRAY_SIZE(opal_mc_tlb_types) ?
  315. opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
  316. : "Unknown";
  317. printk("%s Error type: TLB [%s]\n", level, subtype);
  318. if (evt.u.tlb_error.effective_address_provided)
  319. printk("%s Effective address: %016llx\n",
  320. level, evt.u.tlb_error.effective_address);
  321. break;
  322. default:
  323. case OpalMCE_ERROR_TYPE_UNKNOWN:
  324. printk("%s Error type: Unknown\n", level);
  325. break;
  326. }
  327. return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
  328. }
  329. static irqreturn_t opal_interrupt(int irq, void *data)
  330. {
  331. __be64 events;
  332. opal_handle_interrupt(virq_to_hw(irq), &events);
  333. opal_do_notifier(events);
  334. return IRQ_HANDLED;
  335. }
  336. static int opal_sysfs_init(void)
  337. {
  338. opal_kobj = kobject_create_and_add("opal", firmware_kobj);
  339. if (!opal_kobj) {
  340. pr_warn("kobject_create_and_add opal failed\n");
  341. return -ENOMEM;
  342. }
  343. return 0;
  344. }
  345. static int __init opal_init(void)
  346. {
  347. struct device_node *np, *consoles;
  348. const __be32 *irqs;
  349. int rc, i, irqlen;
  350. opal_node = of_find_node_by_path("/ibm,opal");
  351. if (!opal_node) {
  352. pr_warn("opal: Node not found\n");
  353. return -ENODEV;
  354. }
  355. /* Register OPAL consoles if any ports */
  356. if (firmware_has_feature(FW_FEATURE_OPALv2))
  357. consoles = of_find_node_by_path("/ibm,opal/consoles");
  358. else
  359. consoles = of_node_get(opal_node);
  360. if (consoles) {
  361. for_each_child_of_node(consoles, np) {
  362. if (strcmp(np->name, "serial"))
  363. continue;
  364. of_platform_device_create(np, NULL, NULL);
  365. }
  366. of_node_put(consoles);
  367. }
  368. /* Find all OPAL interrupts and request them */
  369. irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
  370. pr_debug("opal: Found %d interrupts reserved for OPAL\n",
  371. irqs ? (irqlen / 4) : 0);
  372. opal_irq_count = irqlen / 4;
  373. opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
  374. for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
  375. unsigned int hwirq = be32_to_cpup(irqs);
  376. unsigned int irq = irq_create_mapping(NULL, hwirq);
  377. if (irq == NO_IRQ) {
  378. pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
  379. continue;
  380. }
  381. rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
  382. if (rc)
  383. pr_warning("opal: Error %d requesting irq %d"
  384. " (0x%x)\n", rc, irq, hwirq);
  385. opal_irqs[i] = irq;
  386. }
  387. /* Create "opal" kobject under /sys/firmware */
  388. rc = opal_sysfs_init();
  389. if (rc == 0) {
  390. /* Setup code update interface */
  391. opal_flash_init();
  392. }
  393. return 0;
  394. }
  395. subsys_initcall(opal_init);
  396. void opal_shutdown(void)
  397. {
  398. unsigned int i;
  399. for (i = 0; i < opal_irq_count; i++) {
  400. if (opal_irqs[i])
  401. free_irq(opal_irqs[i], NULL);
  402. opal_irqs[i] = 0;
  403. }
  404. }