opal.c 12 KB

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