2 * PowerNV OPAL high level interfaces
4 * Copyright 2011 IBM Corp.
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.
14 #include <linux/types.h>
16 #include <linux/of_platform.h>
17 #include <linux/interrupt.h>
19 #include <asm/firmware.h>
28 static struct device_node *opal_node;
29 static DEFINE_SPINLOCK(opal_write_lock);
30 extern u64 opal_mc_secondary_handler[];
32 int __init early_init_dt_scan_opal(unsigned long node,
33 const char *uname, int depth, void *data)
35 const void *basep, *entryp;
36 unsigned long basesz, entrysz;
38 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
41 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
42 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
44 if (!basep || !entryp)
47 opal.base = of_read_number(basep, basesz/4);
48 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);
55 powerpc_firmware_features |= FW_FEATURE_OPAL;
56 if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
57 powerpc_firmware_features |= FW_FEATURE_OPALv2;
58 printk("OPAL V2 detected !\n");
60 printk("OPAL V1 detected !\n");
66 static int __init opal_register_exception_handlers(void)
70 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
73 /* Hookup some exception handlers. We use the fwnmi area at 0x7000
74 * to provide the glue space to OPAL
77 opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
78 __pa(opal_mc_secondary_handler[0]),
81 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
84 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
89 early_initcall(opal_register_exception_handlers);
91 int opal_get_chars(uint32_t vtermno, char *buf, int count)
98 opal_poll_events(&evt);
99 if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0)
102 rc = opal_console_read(vtermno, &len, buf);
103 if (rc == OPAL_SUCCESS)
108 int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
118 /* We want put_chars to be atomic to avoid mangling of hvsi
119 * packets. To do that, we first test for room and return
120 * -EAGAIN if there isn't enough.
122 * Unfortunately, opal_console_write_buffer_space() doesn't
123 * appear to work on opal v1, so we just assume there is
124 * enough room and be done with it
126 spin_lock_irqsave(&opal_write_lock, flags);
127 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
128 rc = opal_console_write_buffer_space(vtermno, &len);
129 if (rc || len < total_len) {
130 spin_unlock_irqrestore(&opal_write_lock, flags);
131 /* Closed -> drop characters */
134 opal_poll_events(&evt);
139 /* We still try to handle partial completions, though they
140 * should no longer happen.
143 while(total_len > 0 && (rc == OPAL_BUSY ||
144 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
146 rc = opal_console_write(vtermno, &len, data);
147 if (rc == OPAL_SUCCESS) {
152 /* This is a bit nasty but we need that for the console to
153 * flush when there aren't any interrupts. We will clean
154 * things a bit later to limit that to synchronous path
155 * such as the kernel console and xmon/udbg
158 opal_poll_events(&evt);
159 while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
161 spin_unlock_irqrestore(&opal_write_lock, flags);
165 int opal_machine_check(struct pt_regs *regs)
167 struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
168 struct opal_machine_check_event evt;
169 const char *level, *sevstr, *subtype;
170 static const char *opal_mc_ue_types[] = {
173 "Page table walk ifetch",
175 "Page table walk Load/Store",
177 static const char *opal_mc_slb_types[] = {
182 static const char *opal_mc_erat_types[] = {
187 static const char *opal_mc_tlb_types[] = {
193 /* Copy the event structure and release the original */
195 opal_evt->in_use = 0;
197 /* Print things out */
198 if (evt.version != OpalMCE_V1) {
199 pr_err("Machine Check Exception, Unknown event version %d !\n",
203 switch(evt.severity) {
204 case OpalMCE_SEV_NO_ERROR:
208 case OpalMCE_SEV_WARNING:
209 level = KERN_WARNING;
212 case OpalMCE_SEV_ERROR_SYNC:
216 case OpalMCE_SEV_FATAL:
223 printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
224 evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
225 "Recovered" : "[Not recovered");
226 printk("%s Initiator: %s\n", level,
227 evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
228 switch(evt.error_type) {
229 case OpalMCE_ERROR_TYPE_UE:
230 subtype = evt.u.ue_error.ue_error_type <
231 ARRAY_SIZE(opal_mc_ue_types) ?
232 opal_mc_ue_types[evt.u.ue_error.ue_error_type]
234 printk("%s Error type: UE [%s]\n", level, subtype);
235 if (evt.u.ue_error.effective_address_provided)
236 printk("%s Effective address: %016llx\n",
237 level, evt.u.ue_error.effective_address);
238 if (evt.u.ue_error.physical_address_provided)
239 printk("%s Physial address: %016llx\n",
240 level, evt.u.ue_error.physical_address);
242 case OpalMCE_ERROR_TYPE_SLB:
243 subtype = evt.u.slb_error.slb_error_type <
244 ARRAY_SIZE(opal_mc_slb_types) ?
245 opal_mc_slb_types[evt.u.slb_error.slb_error_type]
247 printk("%s Error type: SLB [%s]\n", level, subtype);
248 if (evt.u.slb_error.effective_address_provided)
249 printk("%s Effective address: %016llx\n",
250 level, evt.u.slb_error.effective_address);
252 case OpalMCE_ERROR_TYPE_ERAT:
253 subtype = evt.u.erat_error.erat_error_type <
254 ARRAY_SIZE(opal_mc_erat_types) ?
255 opal_mc_erat_types[evt.u.erat_error.erat_error_type]
257 printk("%s Error type: ERAT [%s]\n", level, subtype);
258 if (evt.u.erat_error.effective_address_provided)
259 printk("%s Effective address: %016llx\n",
260 level, evt.u.erat_error.effective_address);
262 case OpalMCE_ERROR_TYPE_TLB:
263 subtype = evt.u.tlb_error.tlb_error_type <
264 ARRAY_SIZE(opal_mc_tlb_types) ?
265 opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
267 printk("%s Error type: TLB [%s]\n", level, subtype);
268 if (evt.u.tlb_error.effective_address_provided)
269 printk("%s Effective address: %016llx\n",
270 level, evt.u.tlb_error.effective_address);
273 case OpalMCE_ERROR_TYPE_UNKNOWN:
274 printk("%s Error type: Unknown\n", level);
277 return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
280 static irqreturn_t opal_interrupt(int irq, void *data)
284 opal_handle_interrupt(virq_to_hw(irq), &events);
286 /* XXX TODO: Do something with the events */
291 static int __init opal_init(void)
293 struct device_node *np, *consoles;
297 opal_node = of_find_node_by_path("/ibm,opal");
299 pr_warn("opal: Node not found\n");
302 if (firmware_has_feature(FW_FEATURE_OPALv2))
303 consoles = of_find_node_by_path("/ibm,opal/consoles");
305 consoles = of_node_get(opal_node);
307 /* Register serial ports */
308 for_each_child_of_node(consoles, np) {
309 if (strcmp(np->name, "serial"))
311 of_platform_device_create(np, NULL, NULL);
313 of_node_put(consoles);
315 /* Find all OPAL interrupts and request them */
316 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
317 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
318 irqs ? (irqlen / 4) : 0);
319 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
320 unsigned int hwirq = be32_to_cpup(irqs);
321 unsigned int irq = irq_create_mapping(NULL, hwirq);
323 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
326 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
328 pr_warning("opal: Error %d requesting irq %d"
329 " (0x%x)\n", rc, irq, hwirq);
333 subsys_initcall(opal_init);