opal.c 8.8 KB

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