opal.c 9.5 KB

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