blob: 01e74cbc67e95fca9e93acce5de9a333eddd26ec [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>
Rob Herring26a20562013-09-26 07:40:04 -050016#include <linux/of_fdt.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000017#include <linux/of_platform.h>
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +000018#include <linux/interrupt.h>
Gavin Shan1bc98de2013-06-20 18:13:22 +080019#include <linux/notifier.h>
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100020#include <linux/slab.h>
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +053021#include <linux/sched.h>
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053022#include <linux/kobject.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000023#include <asm/opal.h>
24#include <asm/firmware.h>
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +053025#include <asm/mce.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000026
27#include "powernv.h"
28
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053029/* /sys/firmware/opal */
30struct kobject *opal_kobj;
31
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000032struct opal {
33 u64 base;
34 u64 entry;
35} opal;
36
37static struct device_node *opal_node;
38static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000039extern u64 opal_mc_secondary_handler[];
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100040static unsigned int *opal_irqs;
41static unsigned int opal_irq_count;
Gavin Shan1bc98de2013-06-20 18:13:22 +080042static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
43static DEFINE_SPINLOCK(opal_notifier_lock);
44static uint64_t last_notified_mask = 0x0ul;
45static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000046
47int __init early_init_dt_scan_opal(unsigned long node,
48 const char *uname, int depth, void *data)
49{
50 const void *basep, *entryp;
51 unsigned long basesz, entrysz;
52
53 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
54 return 0;
55
56 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
57 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
58
59 if (!basep || !entryp)
60 return 1;
61
62 opal.base = of_read_number(basep, basesz/4);
63 opal.entry = of_read_number(entryp, entrysz/4);
64
65 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
66 opal.base, basep, basesz);
67 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
68 opal.entry, entryp, entrysz);
69
70 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +100071 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
72 powerpc_firmware_features |= FW_FEATURE_OPALv2;
73 powerpc_firmware_features |= FW_FEATURE_OPALv3;
74 printk("OPAL V3 detected !\n");
75 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000076 powerpc_firmware_features |= FW_FEATURE_OPALv2;
77 printk("OPAL V2 detected !\n");
78 } else {
79 printk("OPAL V1 detected !\n");
80 }
81
Jeremy Kerrc4463b32013-05-01 22:31:50 +000082 return 1;
83}
84
85static int __init opal_register_exception_handlers(void)
86{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +100087#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +000088 u64 glue;
89
90 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
91 return -ENODEV;
92
Mahesh Salgaonkar28446de2013-10-30 20:05:58 +053093 /* Hookup some exception handlers except machine check. We use the
94 * fwnmi area at 0x7000 to provide the glue space to OPAL
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000095 */
96 glue = 0x7000;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000097 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
98 0, glue);
99 glue += 128;
100 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000101#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000102
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000103 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000104}
105
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000106early_initcall(opal_register_exception_handlers);
107
Gavin Shan1bc98de2013-06-20 18:13:22 +0800108int opal_notifier_register(struct notifier_block *nb)
109{
110 if (!nb) {
111 pr_warning("%s: Invalid argument (%p)\n",
112 __func__, nb);
113 return -EINVAL;
114 }
115
116 atomic_notifier_chain_register(&opal_notifier_head, nb);
117 return 0;
118}
119
120static void opal_do_notifier(uint64_t events)
121{
122 unsigned long flags;
123 uint64_t changed_mask;
124
125 if (atomic_read(&opal_notifier_hold))
126 return;
127
128 spin_lock_irqsave(&opal_notifier_lock, flags);
129 changed_mask = last_notified_mask ^ events;
130 last_notified_mask = events;
131 spin_unlock_irqrestore(&opal_notifier_lock, flags);
132
133 /*
134 * We feed with the event bits and changed bits for
135 * enough information to the callback.
136 */
137 atomic_notifier_call_chain(&opal_notifier_head,
138 events, (void *)changed_mask);
139}
140
141void opal_notifier_update_evt(uint64_t evt_mask,
142 uint64_t evt_val)
143{
144 unsigned long flags;
145
146 spin_lock_irqsave(&opal_notifier_lock, flags);
147 last_notified_mask &= ~evt_mask;
148 last_notified_mask |= evt_val;
149 spin_unlock_irqrestore(&opal_notifier_lock, flags);
150}
151
152void opal_notifier_enable(void)
153{
154 int64_t rc;
155 uint64_t evt = 0;
156
157 atomic_set(&opal_notifier_hold, 0);
158
159 /* Process pending events */
160 rc = opal_poll_events(&evt);
161 if (rc == OPAL_SUCCESS && evt)
162 opal_do_notifier(evt);
163}
164
165void opal_notifier_disable(void)
166{
167 atomic_set(&opal_notifier_hold, 1);
168}
169
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000170int opal_get_chars(uint32_t vtermno, char *buf, int count)
171{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000172 s64 rc;
173 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000174
175 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000176 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000177 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000178 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000179 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000180 len = cpu_to_be64(count);
181 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000182 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000183 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000184 return 0;
185}
186
187int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
188{
189 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000190 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000191 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000192 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000193 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000194
195 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000196 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000197
198 /* We want put_chars to be atomic to avoid mangling of hvsi
199 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000200 * -EAGAIN if there isn't enough.
201 *
202 * Unfortunately, opal_console_write_buffer_space() doesn't
203 * appear to work on opal v1, so we just assume there is
204 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000205 */
206 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000207 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000208 rc = opal_console_write_buffer_space(vtermno, &olen);
209 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000210 if (rc || len < total_len) {
211 spin_unlock_irqrestore(&opal_write_lock, flags);
212 /* Closed -> drop characters */
213 if (rc)
214 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000215 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000216 return -EAGAIN;
217 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000218 }
219
220 /* We still try to handle partial completions, though they
221 * should no longer happen.
222 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000223 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000224 while(total_len > 0 && (rc == OPAL_BUSY ||
225 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000226 olen = cpu_to_be64(total_len);
227 rc = opal_console_write(vtermno, &olen, data);
228 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000229
230 /* Closed or other error drop */
231 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
232 rc != OPAL_BUSY_EVENT) {
233 written = total_len;
234 break;
235 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000236 if (rc == OPAL_SUCCESS) {
237 total_len -= len;
238 data += len;
239 written += len;
240 }
241 /* This is a bit nasty but we need that for the console to
242 * flush when there aren't any interrupts. We will clean
243 * things a bit later to limit that to synchronous path
244 * such as the kernel console and xmon/udbg
245 */
246 do
247 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000248 while(rc == OPAL_SUCCESS &&
249 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000250 }
251 spin_unlock_irqrestore(&opal_write_lock, flags);
252 return written;
253}
254
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530255static int opal_recover_mce(struct pt_regs *regs,
256 struct machine_check_event *evt)
257{
258 int recovered = 0;
259 uint64_t ea = get_mce_fault_addr(evt);
260
261 if (!(regs->msr & MSR_RI)) {
262 /* If MSR_RI isn't set, we cannot recover */
263 recovered = 0;
264 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
265 /* Platform corrected itself */
266 recovered = 1;
267 } else if (ea && !is_kernel_addr(ea)) {
268 /*
269 * Faulting address is not in kernel text. We should be fine.
270 * We need to find which process uses this address.
271 * For now, kill the task if we have received exception when
272 * in userspace.
273 *
274 * TODO: Queue up this address for hwpoisioning later.
275 */
276 if (user_mode(regs) && !is_global_init(current)) {
277 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
278 recovered = 1;
279 } else
280 recovered = 0;
281 } else if (user_mode(regs) && !is_global_init(current) &&
282 evt->severity == MCE_SEV_ERROR_SYNC) {
283 /*
284 * If we have received a synchronous error when in userspace
285 * kill the task.
286 */
287 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
288 recovered = 1;
289 }
290 return recovered;
291}
292
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000293int opal_machine_check(struct pt_regs *regs)
294{
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530295 struct machine_check_event evt;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000296
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530297 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
298 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000299
300 /* Print things out */
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530301 if (evt.version != MCE_V1) {
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000302 pr_err("Machine Check Exception, Unknown event version %d !\n",
303 evt.version);
304 return 0;
305 }
Mahesh Salgaonkarb5ff4212013-10-30 20:05:49 +0530306 machine_check_print_event_info(&evt);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000307
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530308 if (opal_recover_mce(regs, &evt))
309 return 1;
310 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000311}
312
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000313static irqreturn_t opal_interrupt(int irq, void *data)
314{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000315 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000316
317 opal_handle_interrupt(virq_to_hw(irq), &events);
318
Gavin Shan1bc98de2013-06-20 18:13:22 +0800319 opal_do_notifier(events);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000320
321 return IRQ_HANDLED;
322}
323
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530324static int opal_sysfs_init(void)
325{
326 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
327 if (!opal_kobj) {
328 pr_warn("kobject_create_and_add opal failed\n");
329 return -ENOMEM;
330 }
331
332 return 0;
333}
334
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000335static int __init opal_init(void)
336{
337 struct device_node *np, *consoles;
Alistair Popple1cc79bc2013-09-23 12:04:54 +1000338 const __be32 *irqs;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000339 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000340
341 opal_node = of_find_node_by_path("/ibm,opal");
342 if (!opal_node) {
343 pr_warn("opal: Node not found\n");
344 return -ENODEV;
345 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000346
347 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000348 if (firmware_has_feature(FW_FEATURE_OPALv2))
349 consoles = of_find_node_by_path("/ibm,opal/consoles");
350 else
351 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000352 if (consoles) {
353 for_each_child_of_node(consoles, np) {
354 if (strcmp(np->name, "serial"))
355 continue;
356 of_platform_device_create(np, NULL, NULL);
357 }
358 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000359 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000360
361 /* Find all OPAL interrupts and request them */
362 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
363 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
364 irqs ? (irqlen / 4) : 0);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000365 opal_irq_count = irqlen / 4;
366 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000367 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
368 unsigned int hwirq = be32_to_cpup(irqs);
369 unsigned int irq = irq_create_mapping(NULL, hwirq);
370 if (irq == NO_IRQ) {
371 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
372 continue;
373 }
374 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
375 if (rc)
376 pr_warning("opal: Error %d requesting irq %d"
377 " (0x%x)\n", rc, irq, hwirq);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000378 opal_irqs[i] = irq;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000379 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530380
381 /* Create "opal" kobject under /sys/firmware */
382 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530383 if (rc == 0) {
384 /* Setup code update interface */
385 opal_flash_init();
386 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530387
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000388 return 0;
389}
390subsys_initcall(opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000391
392void opal_shutdown(void)
393{
394 unsigned int i;
395
396 for (i = 0; i < opal_irq_count; i++) {
397 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000398 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000399 opal_irqs[i] = 0;
400 }
401}