powerpc/book3s: Queue up and process delayed MCE events.
[linux-2.6-block.git] / arch / powerpc / platforms / powernv / opal.c
CommitLineData
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 */
29struct kobject *opal_kobj;
30
14a43e69
BH
31struct opal {
32 u64 base;
33 u64 entry;
34} opal;
35
36static struct device_node *opal_node;
37static DEFINE_SPINLOCK(opal_write_lock);
ed79ba9e 38extern u64 opal_mc_secondary_handler[];
73ed148a
BH
39static unsigned int *opal_irqs;
40static unsigned int opal_irq_count;
1bc98de2
GS
41static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
42static DEFINE_SPINLOCK(opal_notifier_lock);
43static uint64_t last_notified_mask = 0x0ul;
44static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
14a43e69
BH
45
46int __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
84static 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
109early_initcall(opal_register_exception_handlers);
110
1bc98de2
GS
111int 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
123static 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
144void 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
155void 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
168void opal_notifier_disable(void)
169{
170 atomic_set(&opal_notifier_hold, 1);
171}
172
14a43e69
BH
173int 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
190int 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
258int opal_machine_check(struct pt_regs *regs)
259{
36df96f8 260 struct machine_check_event evt;
ed79ba9e 261
36df96f8
MS
262 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
263 return 0;
ed79ba9e
BH
264
265 /* Print things out */
36df96f8 266 if (evt.version != MCE_V1) {
ed79ba9e
BH
267 pr_err("Machine Check Exception, Unknown event version %d !\n",
268 evt.version);
269 return 0;
270 }
b5ff4211 271 machine_check_print_event_info(&evt);
ed79ba9e 272
36df96f8 273 return evt.severity == MCE_SEV_FATAL ? 0 : 1;
ed79ba9e
BH
274}
275
a125e092
BH
276static irqreturn_t opal_interrupt(int irq, void *data)
277{
5e4da530 278 __be64 events;
a125e092
BH
279
280 opal_handle_interrupt(virq_to_hw(irq), &events);
281
1bc98de2 282 opal_do_notifier(events);
a125e092
BH
283
284 return IRQ_HANDLED;
285}
286
6f68b5e2
VH
287static int opal_sysfs_init(void)
288{
289 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
290 if (!opal_kobj) {
291 pr_warn("kobject_create_and_add opal failed\n");
292 return -ENOMEM;
293 }
294
295 return 0;
296}
297
14a43e69
BH
298static int __init opal_init(void)
299{
300 struct device_node *np, *consoles;
1cc79bc8 301 const __be32 *irqs;
a125e092 302 int rc, i, irqlen;
14a43e69
BH
303
304 opal_node = of_find_node_by_path("/ibm,opal");
305 if (!opal_node) {
306 pr_warn("opal: Node not found\n");
307 return -ENODEV;
308 }
2db29d28
BH
309
310 /* Register OPAL consoles if any ports */
14a43e69
BH
311 if (firmware_has_feature(FW_FEATURE_OPALv2))
312 consoles = of_find_node_by_path("/ibm,opal/consoles");
313 else
314 consoles = of_node_get(opal_node);
2db29d28
BH
315 if (consoles) {
316 for_each_child_of_node(consoles, np) {
317 if (strcmp(np->name, "serial"))
318 continue;
319 of_platform_device_create(np, NULL, NULL);
320 }
321 of_node_put(consoles);
14a43e69 322 }
a125e092
BH
323
324 /* Find all OPAL interrupts and request them */
325 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
326 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
327 irqs ? (irqlen / 4) : 0);
73ed148a
BH
328 opal_irq_count = irqlen / 4;
329 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
a125e092
BH
330 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
331 unsigned int hwirq = be32_to_cpup(irqs);
332 unsigned int irq = irq_create_mapping(NULL, hwirq);
333 if (irq == NO_IRQ) {
334 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
335 continue;
336 }
337 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
338 if (rc)
339 pr_warning("opal: Error %d requesting irq %d"
340 " (0x%x)\n", rc, irq, hwirq);
73ed148a 341 opal_irqs[i] = irq;
a125e092 342 }
6f68b5e2
VH
343
344 /* Create "opal" kobject under /sys/firmware */
345 rc = opal_sysfs_init();
50bd6153
VH
346 if (rc == 0) {
347 /* Setup code update interface */
348 opal_flash_init();
349 }
6f68b5e2 350
14a43e69
BH
351 return 0;
352}
353subsys_initcall(opal_init);
73ed148a
BH
354
355void opal_shutdown(void)
356{
357 unsigned int i;
358
359 for (i = 0; i < opal_irq_count; i++) {
360 if (opal_irqs[i])
b0d436c7 361 free_irq(opal_irqs[i], NULL);
73ed148a
BH
362 opal_irqs[i] = 0;
363 }
364}