opal.c 11 KB

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