blob: 7a184a0ff18371c717ed705d2a2f021b65eec8f3 [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);
Mahesh Salgaonkar24366362013-11-18 15:35:58 +053043static struct atomic_notifier_head opal_msg_notifier_head[OPAL_MSG_TYPE_MAX];
Gavin Shan1bc98de2013-06-20 18:13:22 +080044static DEFINE_SPINLOCK(opal_notifier_lock);
45static uint64_t last_notified_mask = 0x0ul;
46static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000047
48int __init early_init_dt_scan_opal(unsigned long node,
49 const char *uname, int depth, void *data)
50{
51 const void *basep, *entryp;
52 unsigned long basesz, entrysz;
53
54 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
55 return 0;
56
57 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
58 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
59
60 if (!basep || !entryp)
61 return 1;
62
63 opal.base = of_read_number(basep, basesz/4);
64 opal.entry = of_read_number(entryp, entrysz/4);
65
66 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
67 opal.base, basep, basesz);
68 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
69 opal.entry, entryp, entrysz);
70
71 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +100072 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
73 powerpc_firmware_features |= FW_FEATURE_OPALv2;
74 powerpc_firmware_features |= FW_FEATURE_OPALv3;
75 printk("OPAL V3 detected !\n");
76 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000077 powerpc_firmware_features |= FW_FEATURE_OPALv2;
78 printk("OPAL V2 detected !\n");
79 } else {
80 printk("OPAL V1 detected !\n");
81 }
82
Jeremy Kerrc4463b32013-05-01 22:31:50 +000083 return 1;
84}
85
86static int __init opal_register_exception_handlers(void)
87{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +100088#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +000089 u64 glue;
90
91 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
92 return -ENODEV;
93
Mahesh Salgaonkar28446de2013-10-30 20:05:58 +053094 /* Hookup some exception handlers except machine check. We use the
95 * fwnmi area at 0x7000 to provide the glue space to OPAL
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000096 */
97 glue = 0x7000;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000098 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
99 0, glue);
100 glue += 128;
101 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000102#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000103
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000104 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000105}
106
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000107early_initcall(opal_register_exception_handlers);
108
Gavin Shan1bc98de2013-06-20 18:13:22 +0800109int opal_notifier_register(struct notifier_block *nb)
110{
111 if (!nb) {
112 pr_warning("%s: Invalid argument (%p)\n",
113 __func__, nb);
114 return -EINVAL;
115 }
116
117 atomic_notifier_chain_register(&opal_notifier_head, nb);
118 return 0;
119}
120
121static void opal_do_notifier(uint64_t events)
122{
123 unsigned long flags;
124 uint64_t changed_mask;
125
126 if (atomic_read(&opal_notifier_hold))
127 return;
128
129 spin_lock_irqsave(&opal_notifier_lock, flags);
130 changed_mask = last_notified_mask ^ events;
131 last_notified_mask = events;
132 spin_unlock_irqrestore(&opal_notifier_lock, flags);
133
134 /*
135 * We feed with the event bits and changed bits for
136 * enough information to the callback.
137 */
138 atomic_notifier_call_chain(&opal_notifier_head,
139 events, (void *)changed_mask);
140}
141
142void opal_notifier_update_evt(uint64_t evt_mask,
143 uint64_t evt_val)
144{
145 unsigned long flags;
146
147 spin_lock_irqsave(&opal_notifier_lock, flags);
148 last_notified_mask &= ~evt_mask;
149 last_notified_mask |= evt_val;
150 spin_unlock_irqrestore(&opal_notifier_lock, flags);
151}
152
153void opal_notifier_enable(void)
154{
155 int64_t rc;
156 uint64_t evt = 0;
157
158 atomic_set(&opal_notifier_hold, 0);
159
160 /* Process pending events */
161 rc = opal_poll_events(&evt);
162 if (rc == OPAL_SUCCESS && evt)
163 opal_do_notifier(evt);
164}
165
166void opal_notifier_disable(void)
167{
168 atomic_set(&opal_notifier_hold, 1);
169}
170
Mahesh Salgaonkar24366362013-11-18 15:35:58 +0530171/*
172 * Opal message notifier based on message type. Allow subscribers to get
173 * notified for specific messgae type.
174 */
175int opal_message_notifier_register(enum OpalMessageType msg_type,
176 struct notifier_block *nb)
177{
178 if (!nb) {
179 pr_warning("%s: Invalid argument (%p)\n",
180 __func__, nb);
181 return -EINVAL;
182 }
183 if (msg_type > OPAL_MSG_TYPE_MAX) {
184 pr_warning("%s: Invalid message type argument (%d)\n",
185 __func__, msg_type);
186 return -EINVAL;
187 }
188 return atomic_notifier_chain_register(
189 &opal_msg_notifier_head[msg_type], nb);
190}
191
192static void opal_message_do_notify(uint32_t msg_type, void *msg)
193{
194 /* notify subscribers */
195 atomic_notifier_call_chain(&opal_msg_notifier_head[msg_type],
196 msg_type, msg);
197}
198
199static void opal_handle_message(void)
200{
201 s64 ret;
202 /*
203 * TODO: pre-allocate a message buffer depending on opal-msg-size
204 * value in /proc/device-tree.
205 */
206 static struct opal_msg msg;
207
208 ret = opal_get_msg(__pa(&msg), sizeof(msg));
209 /* No opal message pending. */
210 if (ret == OPAL_RESOURCE)
211 return;
212
213 /* check for errors. */
214 if (ret) {
215 pr_warning("%s: Failed to retrive opal message, err=%lld\n",
216 __func__, ret);
217 return;
218 }
219
220 /* Sanity check */
221 if (msg.msg_type > OPAL_MSG_TYPE_MAX) {
222 pr_warning("%s: Unknown message type: %u\n",
223 __func__, msg.msg_type);
224 return;
225 }
226 opal_message_do_notify(msg.msg_type, (void *)&msg);
227}
228
229static int opal_message_notify(struct notifier_block *nb,
230 unsigned long events, void *change)
231{
232 if (events & OPAL_EVENT_MSG_PENDING)
233 opal_handle_message();
234 return 0;
235}
236
237static struct notifier_block opal_message_nb = {
238 .notifier_call = opal_message_notify,
239 .next = NULL,
240 .priority = 0,
241};
242
243static int __init opal_message_init(void)
244{
245 int ret, i;
246
247 for (i = 0; i < OPAL_MSG_TYPE_MAX; i++)
248 ATOMIC_INIT_NOTIFIER_HEAD(&opal_msg_notifier_head[i]);
249
250 ret = opal_notifier_register(&opal_message_nb);
251 if (ret) {
252 pr_err("%s: Can't register OPAL event notifier (%d)\n",
253 __func__, ret);
254 return ret;
255 }
256 return 0;
257}
258early_initcall(opal_message_init);
259
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000260int opal_get_chars(uint32_t vtermno, char *buf, int count)
261{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000262 s64 rc;
263 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000264
265 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000266 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000267 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000268 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000269 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000270 len = cpu_to_be64(count);
271 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000272 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000273 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000274 return 0;
275}
276
277int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
278{
279 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000280 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000281 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000282 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000283 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000284
285 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000286 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000287
288 /* We want put_chars to be atomic to avoid mangling of hvsi
289 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000290 * -EAGAIN if there isn't enough.
291 *
292 * Unfortunately, opal_console_write_buffer_space() doesn't
293 * appear to work on opal v1, so we just assume there is
294 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000295 */
296 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000297 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000298 rc = opal_console_write_buffer_space(vtermno, &olen);
299 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000300 if (rc || len < total_len) {
301 spin_unlock_irqrestore(&opal_write_lock, flags);
302 /* Closed -> drop characters */
303 if (rc)
304 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000305 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000306 return -EAGAIN;
307 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000308 }
309
310 /* We still try to handle partial completions, though they
311 * should no longer happen.
312 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000313 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000314 while(total_len > 0 && (rc == OPAL_BUSY ||
315 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000316 olen = cpu_to_be64(total_len);
317 rc = opal_console_write(vtermno, &olen, data);
318 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000319
320 /* Closed or other error drop */
321 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
322 rc != OPAL_BUSY_EVENT) {
323 written = total_len;
324 break;
325 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000326 if (rc == OPAL_SUCCESS) {
327 total_len -= len;
328 data += len;
329 written += len;
330 }
331 /* This is a bit nasty but we need that for the console to
332 * flush when there aren't any interrupts. We will clean
333 * things a bit later to limit that to synchronous path
334 * such as the kernel console and xmon/udbg
335 */
336 do
337 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000338 while(rc == OPAL_SUCCESS &&
339 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000340 }
341 spin_unlock_irqrestore(&opal_write_lock, flags);
342 return written;
343}
344
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530345static int opal_recover_mce(struct pt_regs *regs,
346 struct machine_check_event *evt)
347{
348 int recovered = 0;
349 uint64_t ea = get_mce_fault_addr(evt);
350
351 if (!(regs->msr & MSR_RI)) {
352 /* If MSR_RI isn't set, we cannot recover */
353 recovered = 0;
354 } else if (evt->disposition == MCE_DISPOSITION_RECOVERED) {
355 /* Platform corrected itself */
356 recovered = 1;
357 } else if (ea && !is_kernel_addr(ea)) {
358 /*
359 * Faulting address is not in kernel text. We should be fine.
360 * We need to find which process uses this address.
361 * For now, kill the task if we have received exception when
362 * in userspace.
363 *
364 * TODO: Queue up this address for hwpoisioning later.
365 */
366 if (user_mode(regs) && !is_global_init(current)) {
367 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
368 recovered = 1;
369 } else
370 recovered = 0;
371 } else if (user_mode(regs) && !is_global_init(current) &&
372 evt->severity == MCE_SEV_ERROR_SYNC) {
373 /*
374 * If we have received a synchronous error when in userspace
375 * kill the task.
376 */
377 _exception(SIGBUS, regs, BUS_MCEERR_AR, regs->nip);
378 recovered = 1;
379 }
380 return recovered;
381}
382
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000383int opal_machine_check(struct pt_regs *regs)
384{
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530385 struct machine_check_event evt;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000386
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530387 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
388 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000389
390 /* Print things out */
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530391 if (evt.version != MCE_V1) {
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000392 pr_err("Machine Check Exception, Unknown event version %d !\n",
393 evt.version);
394 return 0;
395 }
Mahesh Salgaonkarb5ff4212013-10-30 20:05:49 +0530396 machine_check_print_event_info(&evt);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000397
Mahesh Salgaonkarb63a0ff2013-10-30 20:06:13 +0530398 if (opal_recover_mce(regs, &evt))
399 return 1;
400 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000401}
402
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000403static irqreturn_t opal_interrupt(int irq, void *data)
404{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000405 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000406
407 opal_handle_interrupt(virq_to_hw(irq), &events);
408
Gavin Shan1bc98de2013-06-20 18:13:22 +0800409 opal_do_notifier(events);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000410
411 return IRQ_HANDLED;
412}
413
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530414static int opal_sysfs_init(void)
415{
416 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
417 if (!opal_kobj) {
418 pr_warn("kobject_create_and_add opal failed\n");
419 return -ENOMEM;
420 }
421
422 return 0;
423}
424
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000425static int __init opal_init(void)
426{
427 struct device_node *np, *consoles;
Alistair Popple1cc79bc2013-09-23 12:04:54 +1000428 const __be32 *irqs;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000429 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000430
431 opal_node = of_find_node_by_path("/ibm,opal");
432 if (!opal_node) {
433 pr_warn("opal: Node not found\n");
434 return -ENODEV;
435 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000436
437 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000438 if (firmware_has_feature(FW_FEATURE_OPALv2))
439 consoles = of_find_node_by_path("/ibm,opal/consoles");
440 else
441 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000442 if (consoles) {
443 for_each_child_of_node(consoles, np) {
444 if (strcmp(np->name, "serial"))
445 continue;
446 of_platform_device_create(np, NULL, NULL);
447 }
448 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000449 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000450
451 /* Find all OPAL interrupts and request them */
452 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
453 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
454 irqs ? (irqlen / 4) : 0);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000455 opal_irq_count = irqlen / 4;
456 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000457 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
458 unsigned int hwirq = be32_to_cpup(irqs);
459 unsigned int irq = irq_create_mapping(NULL, hwirq);
460 if (irq == NO_IRQ) {
461 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
462 continue;
463 }
464 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
465 if (rc)
466 pr_warning("opal: Error %d requesting irq %d"
467 " (0x%x)\n", rc, irq, hwirq);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000468 opal_irqs[i] = irq;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000469 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530470
471 /* Create "opal" kobject under /sys/firmware */
472 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530473 if (rc == 0) {
474 /* Setup code update interface */
475 opal_flash_init();
476 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530477
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000478 return 0;
479}
480subsys_initcall(opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000481
482void opal_shutdown(void)
483{
484 unsigned int i;
485
486 for (i = 0; i < opal_irq_count; i++) {
487 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000488 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000489 opal_irqs[i] = 0;
490 }
491}