opal.c 8.6 KB

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