blob: f348bd49487c1e986c378c2f0e52abec6f63cd37 [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>
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053021#include <linux/kobject.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000022#include <asm/opal.h>
23#include <asm/firmware.h>
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +053024#include <asm/mce.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000025
26#include "powernv.h"
27
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053028/* /sys/firmware/opal */
29struct kobject *opal_kobj;
30
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000031struct opal {
32 u64 base;
33 u64 entry;
34} opal;
35
36static struct device_node *opal_node;
37static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000038extern u64 opal_mc_secondary_handler[];
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100039static unsigned int *opal_irqs;
40static unsigned int opal_irq_count;
Gavin Shan1bc98de2013-06-20 18:13:22 +080041static 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);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000045
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;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +100070 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")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000075 powerpc_firmware_features |= FW_FEATURE_OPALv2;
76 printk("OPAL V2 detected !\n");
77 } else {
78 printk("OPAL V1 detected !\n");
79 }
80
Jeremy Kerrc4463b32013-05-01 22:31:50 +000081 return 1;
82}
83
84static int __init opal_register_exception_handlers(void)
85{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +100086#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +000087 u64 glue;
88
89 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
90 return -ENODEV;
91
Mahesh Salgaonkar28446de2013-10-30 20:05:58 +053092 /* Hookup some exception handlers except machine check. We use the
93 * fwnmi area at 0x7000 to provide the glue space to OPAL
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000094 */
95 glue = 0x7000;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000096 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
97 0, glue);
98 glue += 128;
99 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000100#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000101
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000102 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000103}
104
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000105early_initcall(opal_register_exception_handlers);
106
Gavin Shan1bc98de2013-06-20 18:13:22 +0800107int opal_notifier_register(struct notifier_block *nb)
108{
109 if (!nb) {
110 pr_warning("%s: Invalid argument (%p)\n",
111 __func__, nb);
112 return -EINVAL;
113 }
114
115 atomic_notifier_chain_register(&opal_notifier_head, nb);
116 return 0;
117}
118
119static void opal_do_notifier(uint64_t events)
120{
121 unsigned long flags;
122 uint64_t changed_mask;
123
124 if (atomic_read(&opal_notifier_hold))
125 return;
126
127 spin_lock_irqsave(&opal_notifier_lock, flags);
128 changed_mask = last_notified_mask ^ events;
129 last_notified_mask = events;
130 spin_unlock_irqrestore(&opal_notifier_lock, flags);
131
132 /*
133 * We feed with the event bits and changed bits for
134 * enough information to the callback.
135 */
136 atomic_notifier_call_chain(&opal_notifier_head,
137 events, (void *)changed_mask);
138}
139
140void opal_notifier_update_evt(uint64_t evt_mask,
141 uint64_t evt_val)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&opal_notifier_lock, flags);
146 last_notified_mask &= ~evt_mask;
147 last_notified_mask |= evt_val;
148 spin_unlock_irqrestore(&opal_notifier_lock, flags);
149}
150
151void opal_notifier_enable(void)
152{
153 int64_t rc;
154 uint64_t evt = 0;
155
156 atomic_set(&opal_notifier_hold, 0);
157
158 /* Process pending events */
159 rc = opal_poll_events(&evt);
160 if (rc == OPAL_SUCCESS && evt)
161 opal_do_notifier(evt);
162}
163
164void opal_notifier_disable(void)
165{
166 atomic_set(&opal_notifier_hold, 1);
167}
168
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000169int opal_get_chars(uint32_t vtermno, char *buf, int count)
170{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000171 s64 rc;
172 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000173
174 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000175 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000176 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000177 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000178 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000179 len = cpu_to_be64(count);
180 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000181 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000182 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000183 return 0;
184}
185
186int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
187{
188 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000189 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000190 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000191 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000192 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000193
194 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000195 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000196
197 /* We want put_chars to be atomic to avoid mangling of hvsi
198 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000199 * -EAGAIN if there isn't enough.
200 *
201 * Unfortunately, opal_console_write_buffer_space() doesn't
202 * appear to work on opal v1, so we just assume there is
203 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000204 */
205 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000206 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000207 rc = opal_console_write_buffer_space(vtermno, &olen);
208 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000209 if (rc || len < total_len) {
210 spin_unlock_irqrestore(&opal_write_lock, flags);
211 /* Closed -> drop characters */
212 if (rc)
213 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000214 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000215 return -EAGAIN;
216 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000217 }
218
219 /* We still try to handle partial completions, though they
220 * should no longer happen.
221 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000222 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000223 while(total_len > 0 && (rc == OPAL_BUSY ||
224 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000225 olen = cpu_to_be64(total_len);
226 rc = opal_console_write(vtermno, &olen, data);
227 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000228
229 /* Closed or other error drop */
230 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
231 rc != OPAL_BUSY_EVENT) {
232 written = total_len;
233 break;
234 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000235 if (rc == OPAL_SUCCESS) {
236 total_len -= len;
237 data += len;
238 written += len;
239 }
240 /* This is a bit nasty but we need that for the console to
241 * flush when there aren't any interrupts. We will clean
242 * things a bit later to limit that to synchronous path
243 * such as the kernel console and xmon/udbg
244 */
245 do
246 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000247 while(rc == OPAL_SUCCESS &&
248 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000249 }
250 spin_unlock_irqrestore(&opal_write_lock, flags);
251 return written;
252}
253
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000254int opal_machine_check(struct pt_regs *regs)
255{
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530256 struct machine_check_event evt;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000257
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530258 if (!get_mce_event(&evt, MCE_EVENT_RELEASE))
259 return 0;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000260
261 /* Print things out */
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530262 if (evt.version != MCE_V1) {
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000263 pr_err("Machine Check Exception, Unknown event version %d !\n",
264 evt.version);
265 return 0;
266 }
Mahesh Salgaonkarb5ff4212013-10-30 20:05:49 +0530267 machine_check_print_event_info(&evt);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000268
Mahesh Salgaonkar36df96f2013-10-30 20:05:40 +0530269 return evt.severity == MCE_SEV_FATAL ? 0 : 1;
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000270}
271
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000272static irqreturn_t opal_interrupt(int irq, void *data)
273{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000274 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000275
276 opal_handle_interrupt(virq_to_hw(irq), &events);
277
Gavin Shan1bc98de2013-06-20 18:13:22 +0800278 opal_do_notifier(events);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000279
280 return IRQ_HANDLED;
281}
282
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530283static int opal_sysfs_init(void)
284{
285 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
286 if (!opal_kobj) {
287 pr_warn("kobject_create_and_add opal failed\n");
288 return -ENOMEM;
289 }
290
291 return 0;
292}
293
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000294static int __init opal_init(void)
295{
296 struct device_node *np, *consoles;
Alistair Popple1cc79bc2013-09-23 12:04:54 +1000297 const __be32 *irqs;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000298 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000299
300 opal_node = of_find_node_by_path("/ibm,opal");
301 if (!opal_node) {
302 pr_warn("opal: Node not found\n");
303 return -ENODEV;
304 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000305
306 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000307 if (firmware_has_feature(FW_FEATURE_OPALv2))
308 consoles = of_find_node_by_path("/ibm,opal/consoles");
309 else
310 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000311 if (consoles) {
312 for_each_child_of_node(consoles, np) {
313 if (strcmp(np->name, "serial"))
314 continue;
315 of_platform_device_create(np, NULL, NULL);
316 }
317 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000318 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000319
320 /* Find all OPAL interrupts and request them */
321 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
322 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
323 irqs ? (irqlen / 4) : 0);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000324 opal_irq_count = irqlen / 4;
325 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000326 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
327 unsigned int hwirq = be32_to_cpup(irqs);
328 unsigned int irq = irq_create_mapping(NULL, hwirq);
329 if (irq == NO_IRQ) {
330 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
331 continue;
332 }
333 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
334 if (rc)
335 pr_warning("opal: Error %d requesting irq %d"
336 " (0x%x)\n", rc, irq, hwirq);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000337 opal_irqs[i] = irq;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000338 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530339
340 /* Create "opal" kobject under /sys/firmware */
341 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530342 if (rc == 0) {
343 /* Setup code update interface */
344 opal_flash_init();
345 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530346
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000347 return 0;
348}
349subsys_initcall(opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000350
351void opal_shutdown(void)
352{
353 unsigned int i;
354
355 for (i = 0; i < opal_irq_count; i++) {
356 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000357 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000358 opal_irqs[i] = 0;
359 }
360}