Commit | Line | Data |
---|---|---|
14a43e69 BH |
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 | ||
12 | #undef DEBUG | |
13 | ||
14 | #include <linux/types.h> | |
15 | #include <linux/of.h> | |
26a2056e | 16 | #include <linux/of_fdt.h> |
14a43e69 | 17 | #include <linux/of_platform.h> |
a125e092 | 18 | #include <linux/interrupt.h> |
1bc98de2 | 19 | #include <linux/notifier.h> |
73ed148a | 20 | #include <linux/slab.h> |
6f68b5e2 | 21 | #include <linux/kobject.h> |
14a43e69 BH |
22 | #include <asm/opal.h> |
23 | #include <asm/firmware.h> | |
36df96f8 | 24 | #include <asm/mce.h> |
14a43e69 BH |
25 | |
26 | #include "powernv.h" | |
27 | ||
6f68b5e2 VH |
28 | /* /sys/firmware/opal */ |
29 | struct kobject *opal_kobj; | |
30 | ||
14a43e69 BH |
31 | struct opal { |
32 | u64 base; | |
33 | u64 entry; | |
34 | } opal; | |
35 | ||
36 | static struct device_node *opal_node; | |
37 | static DEFINE_SPINLOCK(opal_write_lock); | |
ed79ba9e | 38 | extern u64 opal_mc_secondary_handler[]; |
73ed148a BH |
39 | static unsigned int *opal_irqs; |
40 | static unsigned int opal_irq_count; | |
1bc98de2 GS |
41 | static ATOMIC_NOTIFIER_HEAD(opal_notifier_head); |
42 | static DEFINE_SPINLOCK(opal_notifier_lock); | |
43 | static uint64_t last_notified_mask = 0x0ul; | |
44 | static atomic_t opal_notifier_hold = ATOMIC_INIT(0); | |
14a43e69 BH |
45 | |
46 | int __init early_init_dt_scan_opal(unsigned long node, | |
47 | const char *uname, int depth, void *data) | |
48 | { | |
49 | const void *basep, *entryp; | |
50 | unsigned long basesz, entrysz; | |
51 | ||
52 | if (depth != 1 || strcmp(uname, "ibm,opal") != 0) | |
53 | return 0; | |
54 | ||
55 | basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz); | |
56 | entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz); | |
57 | ||
58 | if (!basep || !entryp) | |
59 | return 1; | |
60 | ||
61 | opal.base = of_read_number(basep, basesz/4); | |
62 | opal.entry = of_read_number(entryp, entrysz/4); | |
63 | ||
64 | pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n", | |
65 | opal.base, basep, basesz); | |
66 | pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n", | |
67 | opal.entry, entryp, entrysz); | |
68 | ||
69 | powerpc_firmware_features |= FW_FEATURE_OPAL; | |
75b93da4 BH |
70 | if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) { |
71 | powerpc_firmware_features |= FW_FEATURE_OPALv2; | |
72 | powerpc_firmware_features |= FW_FEATURE_OPALv3; | |
73 | printk("OPAL V3 detected !\n"); | |
74 | } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) { | |
14a43e69 BH |
75 | powerpc_firmware_features |= FW_FEATURE_OPALv2; |
76 | printk("OPAL V2 detected !\n"); | |
77 | } else { | |
78 | printk("OPAL V1 detected !\n"); | |
79 | } | |
80 | ||
c4463b37 JK |
81 | return 1; |
82 | } | |
83 | ||
84 | static int __init opal_register_exception_handlers(void) | |
85 | { | |
29186097 | 86 | #ifdef __BIG_ENDIAN__ |
c4463b37 JK |
87 | u64 glue; |
88 | ||
89 | if (!(powerpc_firmware_features & FW_FEATURE_OPAL)) | |
90 | return -ENODEV; | |
91 | ||
ed79ba9e BH |
92 | /* Hookup some exception handlers. We use the fwnmi area at 0x7000 |
93 | * to provide the glue space to OPAL | |
94 | */ | |
95 | glue = 0x7000; | |
96 | opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER, | |
97 | __pa(opal_mc_secondary_handler[0]), | |
98 | glue); | |
99 | glue += 128; | |
100 | opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER, | |
101 | 0, glue); | |
102 | glue += 128; | |
103 | opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue); | |
29186097 | 104 | #endif |
ed79ba9e | 105 | |
c4463b37 | 106 | return 0; |
14a43e69 BH |
107 | } |
108 | ||
c4463b37 JK |
109 | early_initcall(opal_register_exception_handlers); |
110 | ||
1bc98de2 GS |
111 | int opal_notifier_register(struct notifier_block *nb) |
112 | { | |
113 | if (!nb) { | |
114 | pr_warning("%s: Invalid argument (%p)\n", | |
115 | __func__, nb); | |
116 | return -EINVAL; | |
117 | } | |
118 | ||
119 | atomic_notifier_chain_register(&opal_notifier_head, nb); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | static void opal_do_notifier(uint64_t events) | |
124 | { | |
125 | unsigned long flags; | |
126 | uint64_t changed_mask; | |
127 | ||
128 | if (atomic_read(&opal_notifier_hold)) | |
129 | return; | |
130 | ||
131 | spin_lock_irqsave(&opal_notifier_lock, flags); | |
132 | changed_mask = last_notified_mask ^ events; | |
133 | last_notified_mask = events; | |
134 | spin_unlock_irqrestore(&opal_notifier_lock, flags); | |
135 | ||
136 | /* | |
137 | * We feed with the event bits and changed bits for | |
138 | * enough information to the callback. | |
139 | */ | |
140 | atomic_notifier_call_chain(&opal_notifier_head, | |
141 | events, (void *)changed_mask); | |
142 | } | |
143 | ||
144 | void opal_notifier_update_evt(uint64_t evt_mask, | |
145 | uint64_t evt_val) | |
146 | { | |
147 | unsigned long flags; | |
148 | ||
149 | spin_lock_irqsave(&opal_notifier_lock, flags); | |
150 | last_notified_mask &= ~evt_mask; | |
151 | last_notified_mask |= evt_val; | |
152 | spin_unlock_irqrestore(&opal_notifier_lock, flags); | |
153 | } | |
154 | ||
155 | void opal_notifier_enable(void) | |
156 | { | |
157 | int64_t rc; | |
158 | uint64_t evt = 0; | |
159 | ||
160 | atomic_set(&opal_notifier_hold, 0); | |
161 | ||
162 | /* Process pending events */ | |
163 | rc = opal_poll_events(&evt); | |
164 | if (rc == OPAL_SUCCESS && evt) | |
165 | opal_do_notifier(evt); | |
166 | } | |
167 | ||
168 | void opal_notifier_disable(void) | |
169 | { | |
170 | atomic_set(&opal_notifier_hold, 1); | |
171 | } | |
172 | ||
14a43e69 BH |
173 | int opal_get_chars(uint32_t vtermno, char *buf, int count) |
174 | { | |
4f89363b BH |
175 | s64 rc; |
176 | __be64 evt, len; | |
14a43e69 BH |
177 | |
178 | if (!opal.entry) | |
daea1175 | 179 | return -ENODEV; |
14a43e69 | 180 | opal_poll_events(&evt); |
4f89363b | 181 | if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0) |
14a43e69 | 182 | return 0; |
4f89363b BH |
183 | len = cpu_to_be64(count); |
184 | rc = opal_console_read(vtermno, &len, buf); | |
14a43e69 | 185 | if (rc == OPAL_SUCCESS) |
4f89363b | 186 | return be64_to_cpu(len); |
14a43e69 BH |
187 | return 0; |
188 | } | |
189 | ||
190 | int opal_put_chars(uint32_t vtermno, const char *data, int total_len) | |
191 | { | |
192 | int written = 0; | |
4f89363b | 193 | __be64 olen; |
daea1175 | 194 | s64 len, rc; |
14a43e69 | 195 | unsigned long flags; |
4f89363b | 196 | __be64 evt; |
14a43e69 BH |
197 | |
198 | if (!opal.entry) | |
daea1175 | 199 | return -ENODEV; |
14a43e69 BH |
200 | |
201 | /* We want put_chars to be atomic to avoid mangling of hvsi | |
202 | * packets. To do that, we first test for room and return | |
daea1175 BH |
203 | * -EAGAIN if there isn't enough. |
204 | * | |
205 | * Unfortunately, opal_console_write_buffer_space() doesn't | |
206 | * appear to work on opal v1, so we just assume there is | |
207 | * enough room and be done with it | |
14a43e69 BH |
208 | */ |
209 | spin_lock_irqsave(&opal_write_lock, flags); | |
daea1175 | 210 | if (firmware_has_feature(FW_FEATURE_OPALv2)) { |
4f89363b BH |
211 | rc = opal_console_write_buffer_space(vtermno, &olen); |
212 | len = be64_to_cpu(olen); | |
daea1175 BH |
213 | if (rc || len < total_len) { |
214 | spin_unlock_irqrestore(&opal_write_lock, flags); | |
215 | /* Closed -> drop characters */ | |
216 | if (rc) | |
217 | return total_len; | |
4f89363b | 218 | opal_poll_events(NULL); |
daea1175 BH |
219 | return -EAGAIN; |
220 | } | |
14a43e69 BH |
221 | } |
222 | ||
223 | /* We still try to handle partial completions, though they | |
224 | * should no longer happen. | |
225 | */ | |
daea1175 | 226 | rc = OPAL_BUSY; |
14a43e69 BH |
227 | while(total_len > 0 && (rc == OPAL_BUSY || |
228 | rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) { | |
4f89363b BH |
229 | olen = cpu_to_be64(total_len); |
230 | rc = opal_console_write(vtermno, &olen, data); | |
231 | len = be64_to_cpu(olen); | |
1de1455f BH |
232 | |
233 | /* Closed or other error drop */ | |
234 | if (rc != OPAL_SUCCESS && rc != OPAL_BUSY && | |
235 | rc != OPAL_BUSY_EVENT) { | |
236 | written = total_len; | |
237 | break; | |
238 | } | |
14a43e69 BH |
239 | if (rc == OPAL_SUCCESS) { |
240 | total_len -= len; | |
241 | data += len; | |
242 | written += len; | |
243 | } | |
244 | /* This is a bit nasty but we need that for the console to | |
245 | * flush when there aren't any interrupts. We will clean | |
246 | * things a bit later to limit that to synchronous path | |
247 | * such as the kernel console and xmon/udbg | |
248 | */ | |
249 | do | |
250 | opal_poll_events(&evt); | |
4f89363b BH |
251 | while(rc == OPAL_SUCCESS && |
252 | (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT)); | |
14a43e69 BH |
253 | } |
254 | spin_unlock_irqrestore(&opal_write_lock, flags); | |
255 | return written; | |
256 | } | |
257 | ||
ed79ba9e BH |
258 | int opal_machine_check(struct pt_regs *regs) |
259 | { | |
36df96f8 | 260 | struct machine_check_event evt; |
ed79ba9e BH |
261 | const char *level, *sevstr, *subtype; |
262 | static const char *opal_mc_ue_types[] = { | |
263 | "Indeterminate", | |
264 | "Instruction fetch", | |
265 | "Page table walk ifetch", | |
266 | "Load/Store", | |
267 | "Page table walk Load/Store", | |
268 | }; | |
269 | static const char *opal_mc_slb_types[] = { | |
270 | "Indeterminate", | |
271 | "Parity", | |
272 | "Multihit", | |
273 | }; | |
274 | static const char *opal_mc_erat_types[] = { | |
275 | "Indeterminate", | |
276 | "Parity", | |
277 | "Multihit", | |
278 | }; | |
279 | static const char *opal_mc_tlb_types[] = { | |
280 | "Indeterminate", | |
281 | "Parity", | |
282 | "Multihit", | |
283 | }; | |
284 | ||
36df96f8 MS |
285 | if (!get_mce_event(&evt, MCE_EVENT_RELEASE)) |
286 | return 0; | |
ed79ba9e BH |
287 | |
288 | /* Print things out */ | |
36df96f8 | 289 | if (evt.version != MCE_V1) { |
ed79ba9e BH |
290 | pr_err("Machine Check Exception, Unknown event version %d !\n", |
291 | evt.version); | |
292 | return 0; | |
293 | } | |
294 | switch(evt.severity) { | |
36df96f8 | 295 | case MCE_SEV_NO_ERROR: |
ed79ba9e BH |
296 | level = KERN_INFO; |
297 | sevstr = "Harmless"; | |
298 | break; | |
36df96f8 | 299 | case MCE_SEV_WARNING: |
ed79ba9e BH |
300 | level = KERN_WARNING; |
301 | sevstr = ""; | |
302 | break; | |
36df96f8 | 303 | case MCE_SEV_ERROR_SYNC: |
ed79ba9e BH |
304 | level = KERN_ERR; |
305 | sevstr = "Severe"; | |
306 | break; | |
36df96f8 | 307 | case MCE_SEV_FATAL: |
ed79ba9e BH |
308 | default: |
309 | level = KERN_ERR; | |
310 | sevstr = "Fatal"; | |
311 | break; | |
312 | } | |
313 | ||
314 | printk("%s%s Machine check interrupt [%s]\n", level, sevstr, | |
36df96f8 | 315 | evt.disposition == MCE_DISPOSITION_RECOVERED ? |
ed79ba9e BH |
316 | "Recovered" : "[Not recovered"); |
317 | printk("%s Initiator: %s\n", level, | |
36df96f8 | 318 | evt.initiator == MCE_INITIATOR_CPU ? "CPU" : "Unknown"); |
ed79ba9e | 319 | switch(evt.error_type) { |
36df96f8 | 320 | case MCE_ERROR_TYPE_UE: |
ed79ba9e BH |
321 | subtype = evt.u.ue_error.ue_error_type < |
322 | ARRAY_SIZE(opal_mc_ue_types) ? | |
323 | opal_mc_ue_types[evt.u.ue_error.ue_error_type] | |
324 | : "Unknown"; | |
325 | printk("%s Error type: UE [%s]\n", level, subtype); | |
326 | if (evt.u.ue_error.effective_address_provided) | |
327 | printk("%s Effective address: %016llx\n", | |
328 | level, evt.u.ue_error.effective_address); | |
329 | if (evt.u.ue_error.physical_address_provided) | |
330 | printk("%s Physial address: %016llx\n", | |
331 | level, evt.u.ue_error.physical_address); | |
332 | break; | |
36df96f8 | 333 | case MCE_ERROR_TYPE_SLB: |
ed79ba9e BH |
334 | subtype = evt.u.slb_error.slb_error_type < |
335 | ARRAY_SIZE(opal_mc_slb_types) ? | |
336 | opal_mc_slb_types[evt.u.slb_error.slb_error_type] | |
337 | : "Unknown"; | |
338 | printk("%s Error type: SLB [%s]\n", level, subtype); | |
339 | if (evt.u.slb_error.effective_address_provided) | |
340 | printk("%s Effective address: %016llx\n", | |
341 | level, evt.u.slb_error.effective_address); | |
342 | break; | |
36df96f8 | 343 | case MCE_ERROR_TYPE_ERAT: |
ed79ba9e BH |
344 | subtype = evt.u.erat_error.erat_error_type < |
345 | ARRAY_SIZE(opal_mc_erat_types) ? | |
346 | opal_mc_erat_types[evt.u.erat_error.erat_error_type] | |
347 | : "Unknown"; | |
348 | printk("%s Error type: ERAT [%s]\n", level, subtype); | |
349 | if (evt.u.erat_error.effective_address_provided) | |
350 | printk("%s Effective address: %016llx\n", | |
351 | level, evt.u.erat_error.effective_address); | |
352 | break; | |
36df96f8 | 353 | case MCE_ERROR_TYPE_TLB: |
ed79ba9e BH |
354 | subtype = evt.u.tlb_error.tlb_error_type < |
355 | ARRAY_SIZE(opal_mc_tlb_types) ? | |
356 | opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type] | |
357 | : "Unknown"; | |
358 | printk("%s Error type: TLB [%s]\n", level, subtype); | |
359 | if (evt.u.tlb_error.effective_address_provided) | |
360 | printk("%s Effective address: %016llx\n", | |
361 | level, evt.u.tlb_error.effective_address); | |
362 | break; | |
363 | default: | |
36df96f8 | 364 | case MCE_ERROR_TYPE_UNKNOWN: |
ed79ba9e BH |
365 | printk("%s Error type: Unknown\n", level); |
366 | break; | |
367 | } | |
36df96f8 | 368 | return evt.severity == MCE_SEV_FATAL ? 0 : 1; |
ed79ba9e BH |
369 | } |
370 | ||
a125e092 BH |
371 | static irqreturn_t opal_interrupt(int irq, void *data) |
372 | { | |
5e4da530 | 373 | __be64 events; |
a125e092 BH |
374 | |
375 | opal_handle_interrupt(virq_to_hw(irq), &events); | |
376 | ||
1bc98de2 | 377 | opal_do_notifier(events); |
a125e092 BH |
378 | |
379 | return IRQ_HANDLED; | |
380 | } | |
381 | ||
6f68b5e2 VH |
382 | static int opal_sysfs_init(void) |
383 | { | |
384 | opal_kobj = kobject_create_and_add("opal", firmware_kobj); | |
385 | if (!opal_kobj) { | |
386 | pr_warn("kobject_create_and_add opal failed\n"); | |
387 | return -ENOMEM; | |
388 | } | |
389 | ||
390 | return 0; | |
391 | } | |
392 | ||
14a43e69 BH |
393 | static int __init opal_init(void) |
394 | { | |
395 | struct device_node *np, *consoles; | |
1cc79bc8 | 396 | const __be32 *irqs; |
a125e092 | 397 | int rc, i, irqlen; |
14a43e69 BH |
398 | |
399 | opal_node = of_find_node_by_path("/ibm,opal"); | |
400 | if (!opal_node) { | |
401 | pr_warn("opal: Node not found\n"); | |
402 | return -ENODEV; | |
403 | } | |
2db29d28 BH |
404 | |
405 | /* Register OPAL consoles if any ports */ | |
14a43e69 BH |
406 | if (firmware_has_feature(FW_FEATURE_OPALv2)) |
407 | consoles = of_find_node_by_path("/ibm,opal/consoles"); | |
408 | else | |
409 | consoles = of_node_get(opal_node); | |
2db29d28 BH |
410 | if (consoles) { |
411 | for_each_child_of_node(consoles, np) { | |
412 | if (strcmp(np->name, "serial")) | |
413 | continue; | |
414 | of_platform_device_create(np, NULL, NULL); | |
415 | } | |
416 | of_node_put(consoles); | |
14a43e69 | 417 | } |
a125e092 BH |
418 | |
419 | /* Find all OPAL interrupts and request them */ | |
420 | irqs = of_get_property(opal_node, "opal-interrupts", &irqlen); | |
421 | pr_debug("opal: Found %d interrupts reserved for OPAL\n", | |
422 | irqs ? (irqlen / 4) : 0); | |
73ed148a BH |
423 | opal_irq_count = irqlen / 4; |
424 | opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL); | |
a125e092 BH |
425 | for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) { |
426 | unsigned int hwirq = be32_to_cpup(irqs); | |
427 | unsigned int irq = irq_create_mapping(NULL, hwirq); | |
428 | if (irq == NO_IRQ) { | |
429 | pr_warning("opal: Failed to map irq 0x%x\n", hwirq); | |
430 | continue; | |
431 | } | |
432 | rc = request_irq(irq, opal_interrupt, 0, "opal", NULL); | |
433 | if (rc) | |
434 | pr_warning("opal: Error %d requesting irq %d" | |
435 | " (0x%x)\n", rc, irq, hwirq); | |
73ed148a | 436 | opal_irqs[i] = irq; |
a125e092 | 437 | } |
6f68b5e2 VH |
438 | |
439 | /* Create "opal" kobject under /sys/firmware */ | |
440 | rc = opal_sysfs_init(); | |
50bd6153 VH |
441 | if (rc == 0) { |
442 | /* Setup code update interface */ | |
443 | opal_flash_init(); | |
444 | } | |
6f68b5e2 | 445 | |
14a43e69 BH |
446 | return 0; |
447 | } | |
448 | subsys_initcall(opal_init); | |
73ed148a BH |
449 | |
450 | void opal_shutdown(void) | |
451 | { | |
452 | unsigned int i; | |
453 | ||
454 | for (i = 0; i < opal_irq_count; i++) { | |
455 | if (opal_irqs[i]) | |
b0d436c7 | 456 | free_irq(opal_irqs[i], NULL); |
73ed148a BH |
457 | opal_irqs[i] = 0; |
458 | } | |
459 | } |