opal.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /* Closed or other error drop */
  124. if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
  125. rc != OPAL_BUSY_EVENT) {
  126. written = total_len;
  127. break;
  128. }
  129. if (rc == OPAL_SUCCESS) {
  130. total_len -= len;
  131. data += len;
  132. written += len;
  133. }
  134. /* This is a bit nasty but we need that for the console to
  135. * flush when there aren't any interrupts. We will clean
  136. * things a bit later to limit that to synchronous path
  137. * such as the kernel console and xmon/udbg
  138. */
  139. do
  140. opal_poll_events(&evt);
  141. while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
  142. }
  143. spin_unlock_irqrestore(&opal_write_lock, flags);
  144. return written;
  145. }
  146. int opal_machine_check(struct pt_regs *regs)
  147. {
  148. struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
  149. struct opal_machine_check_event evt;
  150. const char *level, *sevstr, *subtype;
  151. static const char *opal_mc_ue_types[] = {
  152. "Indeterminate",
  153. "Instruction fetch",
  154. "Page table walk ifetch",
  155. "Load/Store",
  156. "Page table walk Load/Store",
  157. };
  158. static const char *opal_mc_slb_types[] = {
  159. "Indeterminate",
  160. "Parity",
  161. "Multihit",
  162. };
  163. static const char *opal_mc_erat_types[] = {
  164. "Indeterminate",
  165. "Parity",
  166. "Multihit",
  167. };
  168. static const char *opal_mc_tlb_types[] = {
  169. "Indeterminate",
  170. "Parity",
  171. "Multihit",
  172. };
  173. /* Copy the event structure and release the original */
  174. evt = *opal_evt;
  175. opal_evt->in_use = 0;
  176. /* Print things out */
  177. if (evt.version != OpalMCE_V1) {
  178. pr_err("Machine Check Exception, Unknown event version %d !\n",
  179. evt.version);
  180. return 0;
  181. }
  182. switch(evt.severity) {
  183. case OpalMCE_SEV_NO_ERROR:
  184. level = KERN_INFO;
  185. sevstr = "Harmless";
  186. break;
  187. case OpalMCE_SEV_WARNING:
  188. level = KERN_WARNING;
  189. sevstr = "";
  190. break;
  191. case OpalMCE_SEV_ERROR_SYNC:
  192. level = KERN_ERR;
  193. sevstr = "Severe";
  194. break;
  195. case OpalMCE_SEV_FATAL:
  196. default:
  197. level = KERN_ERR;
  198. sevstr = "Fatal";
  199. break;
  200. }
  201. printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
  202. evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
  203. "Recovered" : "[Not recovered");
  204. printk("%s Initiator: %s\n", level,
  205. evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
  206. switch(evt.error_type) {
  207. case OpalMCE_ERROR_TYPE_UE:
  208. subtype = evt.u.ue_error.ue_error_type <
  209. ARRAY_SIZE(opal_mc_ue_types) ?
  210. opal_mc_ue_types[evt.u.ue_error.ue_error_type]
  211. : "Unknown";
  212. printk("%s Error type: UE [%s]\n", level, subtype);
  213. if (evt.u.ue_error.effective_address_provided)
  214. printk("%s Effective address: %016llx\n",
  215. level, evt.u.ue_error.effective_address);
  216. if (evt.u.ue_error.physical_address_provided)
  217. printk("%s Physial address: %016llx\n",
  218. level, evt.u.ue_error.physical_address);
  219. break;
  220. case OpalMCE_ERROR_TYPE_SLB:
  221. subtype = evt.u.slb_error.slb_error_type <
  222. ARRAY_SIZE(opal_mc_slb_types) ?
  223. opal_mc_slb_types[evt.u.slb_error.slb_error_type]
  224. : "Unknown";
  225. printk("%s Error type: SLB [%s]\n", level, subtype);
  226. if (evt.u.slb_error.effective_address_provided)
  227. printk("%s Effective address: %016llx\n",
  228. level, evt.u.slb_error.effective_address);
  229. break;
  230. case OpalMCE_ERROR_TYPE_ERAT:
  231. subtype = evt.u.erat_error.erat_error_type <
  232. ARRAY_SIZE(opal_mc_erat_types) ?
  233. opal_mc_erat_types[evt.u.erat_error.erat_error_type]
  234. : "Unknown";
  235. printk("%s Error type: ERAT [%s]\n", level, subtype);
  236. if (evt.u.erat_error.effective_address_provided)
  237. printk("%s Effective address: %016llx\n",
  238. level, evt.u.erat_error.effective_address);
  239. break;
  240. case OpalMCE_ERROR_TYPE_TLB:
  241. subtype = evt.u.tlb_error.tlb_error_type <
  242. ARRAY_SIZE(opal_mc_tlb_types) ?
  243. opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
  244. : "Unknown";
  245. printk("%s Error type: TLB [%s]\n", level, subtype);
  246. if (evt.u.tlb_error.effective_address_provided)
  247. printk("%s Effective address: %016llx\n",
  248. level, evt.u.tlb_error.effective_address);
  249. break;
  250. default:
  251. case OpalMCE_ERROR_TYPE_UNKNOWN:
  252. printk("%s Error type: Unknown\n", level);
  253. break;
  254. }
  255. return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
  256. }
  257. static irqreturn_t opal_interrupt(int irq, void *data)
  258. {
  259. uint64_t events;
  260. opal_handle_interrupt(virq_to_hw(irq), &events);
  261. /* XXX TODO: Do something with the events */
  262. return IRQ_HANDLED;
  263. }
  264. static int __init opal_init(void)
  265. {
  266. struct device_node *np, *consoles;
  267. const u32 *irqs;
  268. int rc, i, irqlen;
  269. opal_node = of_find_node_by_path("/ibm,opal");
  270. if (!opal_node) {
  271. pr_warn("opal: Node not found\n");
  272. return -ENODEV;
  273. }
  274. if (firmware_has_feature(FW_FEATURE_OPALv2))
  275. consoles = of_find_node_by_path("/ibm,opal/consoles");
  276. else
  277. consoles = of_node_get(opal_node);
  278. /* Register serial ports */
  279. for_each_child_of_node(consoles, np) {
  280. if (strcmp(np->name, "serial"))
  281. continue;
  282. of_platform_device_create(np, NULL, NULL);
  283. }
  284. of_node_put(consoles);
  285. /* Find all OPAL interrupts and request them */
  286. irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
  287. pr_debug("opal: Found %d interrupts reserved for OPAL\n",
  288. irqs ? (irqlen / 4) : 0);
  289. for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
  290. unsigned int hwirq = be32_to_cpup(irqs);
  291. unsigned int irq = irq_create_mapping(NULL, hwirq);
  292. if (irq == NO_IRQ) {
  293. pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
  294. continue;
  295. }
  296. rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
  297. if (rc)
  298. pr_warning("opal: Error %d requesting irq %d"
  299. " (0x%x)\n", rc, irq, hwirq);
  300. }
  301. return 0;
  302. }
  303. subsys_initcall(opal_init);