opal.c 11 KB

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