blob: b56c243aaee93907741d59aee9090dcd7e000ec7 [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>
16#include <linux/of_platform.h>
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +000017#include <linux/interrupt.h>
Gavin Shan1bc98de2013-06-20 18:13:22 +080018#include <linux/notifier.h>
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100019#include <linux/slab.h>
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053020#include <linux/kobject.h>
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000021#include <asm/opal.h>
22#include <asm/firmware.h>
23
24#include "powernv.h"
25
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053026/* /sys/firmware/opal */
27struct kobject *opal_kobj;
28
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000029struct opal {
30 u64 base;
31 u64 entry;
32} opal;
33
34static struct device_node *opal_node;
35static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000036extern u64 opal_mc_secondary_handler[];
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100037static unsigned int *opal_irqs;
38static unsigned int opal_irq_count;
Gavin Shan1bc98de2013-06-20 18:13:22 +080039static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
40static DEFINE_SPINLOCK(opal_notifier_lock);
41static uint64_t last_notified_mask = 0x0ul;
42static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000043
44int __init early_init_dt_scan_opal(unsigned long node,
45 const char *uname, int depth, void *data)
46{
47 const void *basep, *entryp;
48 unsigned long basesz, entrysz;
49
50 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
51 return 0;
52
53 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
54 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
55
56 if (!basep || !entryp)
57 return 1;
58
59 opal.base = of_read_number(basep, basesz/4);
60 opal.entry = of_read_number(entryp, entrysz/4);
61
62 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
63 opal.base, basep, basesz);
64 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
65 opal.entry, entryp, entrysz);
66
67 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +100068 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
69 powerpc_firmware_features |= FW_FEATURE_OPALv2;
70 powerpc_firmware_features |= FW_FEATURE_OPALv3;
71 printk("OPAL V3 detected !\n");
72 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000073 powerpc_firmware_features |= FW_FEATURE_OPALv2;
74 printk("OPAL V2 detected !\n");
75 } else {
76 printk("OPAL V1 detected !\n");
77 }
78
Jeremy Kerrc4463b32013-05-01 22:31:50 +000079 return 1;
80}
81
82static int __init opal_register_exception_handlers(void)
83{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +100084#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +000085 u64 glue;
86
87 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
88 return -ENODEV;
89
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000090 /* Hookup some exception handlers. We use the fwnmi area at 0x7000
91 * to provide the glue space to OPAL
92 */
93 glue = 0x7000;
94 opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
95 __pa(opal_mc_secondary_handler[0]),
96 glue);
97 glue += 128;
98 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
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000171int opal_get_chars(uint32_t vtermno, char *buf, int count)
172{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000173 s64 rc;
174 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000175
176 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000177 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000178 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000179 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000180 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000181 len = cpu_to_be64(count);
182 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000183 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000184 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000185 return 0;
186}
187
188int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
189{
190 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000191 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000192 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000193 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000194 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000195
196 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000197 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000198
199 /* We want put_chars to be atomic to avoid mangling of hvsi
200 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000201 * -EAGAIN if there isn't enough.
202 *
203 * Unfortunately, opal_console_write_buffer_space() doesn't
204 * appear to work on opal v1, so we just assume there is
205 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000206 */
207 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000208 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000209 rc = opal_console_write_buffer_space(vtermno, &olen);
210 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000211 if (rc || len < total_len) {
212 spin_unlock_irqrestore(&opal_write_lock, flags);
213 /* Closed -> drop characters */
214 if (rc)
215 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000216 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000217 return -EAGAIN;
218 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000219 }
220
221 /* We still try to handle partial completions, though they
222 * should no longer happen.
223 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000224 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000225 while(total_len > 0 && (rc == OPAL_BUSY ||
226 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000227 olen = cpu_to_be64(total_len);
228 rc = opal_console_write(vtermno, &olen, data);
229 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000230
231 /* Closed or other error drop */
232 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
233 rc != OPAL_BUSY_EVENT) {
234 written = total_len;
235 break;
236 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000237 if (rc == OPAL_SUCCESS) {
238 total_len -= len;
239 data += len;
240 written += len;
241 }
242 /* This is a bit nasty but we need that for the console to
243 * flush when there aren't any interrupts. We will clean
244 * things a bit later to limit that to synchronous path
245 * such as the kernel console and xmon/udbg
246 */
247 do
248 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000249 while(rc == OPAL_SUCCESS &&
250 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000251 }
252 spin_unlock_irqrestore(&opal_write_lock, flags);
253 return written;
254}
255
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000256int opal_machine_check(struct pt_regs *regs)
257{
258 struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
259 struct opal_machine_check_event evt;
260 const char *level, *sevstr, *subtype;
261 static const char *opal_mc_ue_types[] = {
262 "Indeterminate",
263 "Instruction fetch",
264 "Page table walk ifetch",
265 "Load/Store",
266 "Page table walk Load/Store",
267 };
268 static const char *opal_mc_slb_types[] = {
269 "Indeterminate",
270 "Parity",
271 "Multihit",
272 };
273 static const char *opal_mc_erat_types[] = {
274 "Indeterminate",
275 "Parity",
276 "Multihit",
277 };
278 static const char *opal_mc_tlb_types[] = {
279 "Indeterminate",
280 "Parity",
281 "Multihit",
282 };
283
284 /* Copy the event structure and release the original */
285 evt = *opal_evt;
286 opal_evt->in_use = 0;
287
288 /* Print things out */
289 if (evt.version != OpalMCE_V1) {
290 pr_err("Machine Check Exception, Unknown event version %d !\n",
291 evt.version);
292 return 0;
293 }
294 switch(evt.severity) {
295 case OpalMCE_SEV_NO_ERROR:
296 level = KERN_INFO;
297 sevstr = "Harmless";
298 break;
299 case OpalMCE_SEV_WARNING:
300 level = KERN_WARNING;
301 sevstr = "";
302 break;
303 case OpalMCE_SEV_ERROR_SYNC:
304 level = KERN_ERR;
305 sevstr = "Severe";
306 break;
307 case OpalMCE_SEV_FATAL:
308 default:
309 level = KERN_ERR;
310 sevstr = "Fatal";
311 break;
312 }
313
314 printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
315 evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
316 "Recovered" : "[Not recovered");
317 printk("%s Initiator: %s\n", level,
318 evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
319 switch(evt.error_type) {
320 case OpalMCE_ERROR_TYPE_UE:
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;
333 case OpalMCE_ERROR_TYPE_SLB:
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;
343 case OpalMCE_ERROR_TYPE_ERAT:
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;
353 case OpalMCE_ERROR_TYPE_TLB:
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:
364 case OpalMCE_ERROR_TYPE_UNKNOWN:
365 printk("%s Error type: Unknown\n", level);
366 break;
367 }
368 return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
369}
370
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000371static irqreturn_t opal_interrupt(int irq, void *data)
372{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000373 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000374
375 opal_handle_interrupt(virq_to_hw(irq), &events);
376
Gavin Shan1bc98de2013-06-20 18:13:22 +0800377 opal_do_notifier(events);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000378
379 return IRQ_HANDLED;
380}
381
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530382static 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
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000393static int __init opal_init(void)
394{
395 struct device_node *np, *consoles;
Alistair Popple1cc79bc2013-09-23 12:04:54 +1000396 const __be32 *irqs;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000397 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000398
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 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000404
405 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000406 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);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000410 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);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000417 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000418
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);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000423 opal_irq_count = irqlen / 4;
424 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000425 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);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000436 opal_irqs[i] = irq;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000437 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530438
439 /* Create "opal" kobject under /sys/firmware */
440 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530441 if (rc == 0) {
442 /* Setup code update interface */
443 opal_flash_init();
444 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530445
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000446 return 0;
447}
448subsys_initcall(opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000449
450void opal_shutdown(void)
451{
452 unsigned int i;
453
454 for (i = 0; i < opal_irq_count; i++) {
455 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000456 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000457 opal_irqs[i] = 0;
458 }
459}