blob: ade4463226c612fd338b5cbaed1cb941d14881a9 [file] [log] [blame]
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +00001/*
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
12#undef DEBUG
13
14#include <linux/types.h>
15#include <linux/of.h>
16#include <linux/of_platform.h>
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +000017#include <linux/interrupt.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000018#include <asm/opal.h>
19#include <asm/firmware.h>
20
21#include "powernv.h"
22
23struct opal {
24 u64 base;
25 u64 entry;
26} opal;
27
28static struct device_node *opal_node;
29static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000030extern u64 opal_mc_secondary_handler[];
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000031
32int __init early_init_dt_scan_opal(unsigned long node,
33 const char *uname, int depth, void *data)
34{
35 const void *basep, *entryp;
36 unsigned long basesz, entrysz;
37
38 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
39 return 0;
40
41 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
42 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
43
44 if (!basep || !entryp)
45 return 1;
46
47 opal.base = of_read_number(basep, basesz/4);
48 opal.entry = of_read_number(entryp, entrysz/4);
49
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);
54
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");
59 } else {
60 printk("OPAL V1 detected !\n");
61 }
62
Jeremy Kerrc4463b32013-05-01 22:31:50 +000063 return 1;
64}
65
66static int __init opal_register_exception_handlers(void)
67{
68 u64 glue;
69
70 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
71 return -ENODEV;
72
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000073 /* Hookup some exception handlers. We use the fwnmi area at 0x7000
74 * to provide the glue space to OPAL
75 */
76 glue = 0x7000;
77 opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
78 __pa(opal_mc_secondary_handler[0]),
79 glue);
80 glue += 128;
81 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
82 0, glue);
83 glue += 128;
84 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
85
Jeremy Kerrc4463b32013-05-01 22:31:50 +000086 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000087}
88
Jeremy Kerrc4463b32013-05-01 22:31:50 +000089early_initcall(opal_register_exception_handlers);
90
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000091int opal_get_chars(uint32_t vtermno, char *buf, int count)
92{
93 s64 len, rc;
94 u64 evt;
95
96 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +000097 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000098 opal_poll_events(&evt);
99 if ((evt & OPAL_EVENT_CONSOLE_INPUT) == 0)
100 return 0;
101 len = count;
102 rc = opal_console_read(vtermno, &len, buf);
103 if (rc == OPAL_SUCCESS)
104 return len;
105 return 0;
106}
107
108int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
109{
110 int written = 0;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000111 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000112 unsigned long flags;
113 u64 evt;
114
115 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000116 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000117
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
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000120 * -EAGAIN if there isn't enough.
121 *
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
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000125 */
126 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000127 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 */
132 if (rc)
133 return total_len;
134 opal_poll_events(&evt);
135 return -EAGAIN;
136 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000137 }
138
139 /* We still try to handle partial completions, though they
140 * should no longer happen.
141 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000142 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000143 while(total_len > 0 && (rc == OPAL_BUSY ||
144 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
145 len = total_len;
146 rc = opal_console_write(vtermno, &len, data);
147 if (rc == OPAL_SUCCESS) {
148 total_len -= len;
149 data += len;
150 written += len;
151 }
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
156 */
157 do
158 opal_poll_events(&evt);
159 while(rc == OPAL_SUCCESS && (evt & OPAL_EVENT_CONSOLE_OUTPUT));
160 }
161 spin_unlock_irqrestore(&opal_write_lock, flags);
162 return written;
163}
164
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000165int opal_machine_check(struct pt_regs *regs)
166{
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[] = {
171 "Indeterminate",
172 "Instruction fetch",
173 "Page table walk ifetch",
174 "Load/Store",
175 "Page table walk Load/Store",
176 };
177 static const char *opal_mc_slb_types[] = {
178 "Indeterminate",
179 "Parity",
180 "Multihit",
181 };
182 static const char *opal_mc_erat_types[] = {
183 "Indeterminate",
184 "Parity",
185 "Multihit",
186 };
187 static const char *opal_mc_tlb_types[] = {
188 "Indeterminate",
189 "Parity",
190 "Multihit",
191 };
192
193 /* Copy the event structure and release the original */
194 evt = *opal_evt;
195 opal_evt->in_use = 0;
196
197 /* Print things out */
198 if (evt.version != OpalMCE_V1) {
199 pr_err("Machine Check Exception, Unknown event version %d !\n",
200 evt.version);
201 return 0;
202 }
203 switch(evt.severity) {
204 case OpalMCE_SEV_NO_ERROR:
205 level = KERN_INFO;
206 sevstr = "Harmless";
207 break;
208 case OpalMCE_SEV_WARNING:
209 level = KERN_WARNING;
210 sevstr = "";
211 break;
212 case OpalMCE_SEV_ERROR_SYNC:
213 level = KERN_ERR;
214 sevstr = "Severe";
215 break;
216 case OpalMCE_SEV_FATAL:
217 default:
218 level = KERN_ERR;
219 sevstr = "Fatal";
220 break;
221 }
222
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]
233 : "Unknown";
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);
241 break;
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]
246 : "Unknown";
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);
251 break;
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]
256 : "Unknown";
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);
261 break;
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]
266 : "Unknown";
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);
271 break;
272 default:
273 case OpalMCE_ERROR_TYPE_UNKNOWN:
274 printk("%s Error type: Unknown\n", level);
275 break;
276 }
277 return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
278}
279
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000280static irqreturn_t opal_interrupt(int irq, void *data)
281{
282 uint64_t events;
283
284 opal_handle_interrupt(virq_to_hw(irq), &events);
285
286 /* XXX TODO: Do something with the events */
287
288 return IRQ_HANDLED;
289}
290
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000291static int __init opal_init(void)
292{
293 struct device_node *np, *consoles;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000294 const u32 *irqs;
295 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000296
297 opal_node = of_find_node_by_path("/ibm,opal");
298 if (!opal_node) {
299 pr_warn("opal: Node not found\n");
300 return -ENODEV;
301 }
302 if (firmware_has_feature(FW_FEATURE_OPALv2))
303 consoles = of_find_node_by_path("/ibm,opal/consoles");
304 else
305 consoles = of_node_get(opal_node);
306
307 /* Register serial ports */
308 for_each_child_of_node(consoles, np) {
309 if (strcmp(np->name, "serial"))
310 continue;
311 of_platform_device_create(np, NULL, NULL);
312 }
313 of_node_put(consoles);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000314
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);
322 if (irq == NO_IRQ) {
323 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
324 continue;
325 }
326 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
327 if (rc)
328 pr_warning("opal: Error %d requesting irq %d"
329 " (0x%x)\n", rc, irq, hwirq);
330 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000331 return 0;
332}
333subsys_initcall(opal_init);