blob: 1c798cd553722f6f043a066c299100be89c10f96 [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>
24
25#include "powernv.h"
26
Vasant Hegde6f68b5e2013-08-27 15:09:52 +053027/* /sys/firmware/opal */
28struct kobject *opal_kobj;
29
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000030struct opal {
31 u64 base;
32 u64 entry;
33} opal;
34
35static struct device_node *opal_node;
36static DEFINE_SPINLOCK(opal_write_lock);
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000037extern u64 opal_mc_secondary_handler[];
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +100038static unsigned int *opal_irqs;
39static unsigned int opal_irq_count;
Gavin Shan1bc98de2013-06-20 18:13:22 +080040static ATOMIC_NOTIFIER_HEAD(opal_notifier_head);
41static DEFINE_SPINLOCK(opal_notifier_lock);
42static uint64_t last_notified_mask = 0x0ul;
43static atomic_t opal_notifier_hold = ATOMIC_INIT(0);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000044
45int __init early_init_dt_scan_opal(unsigned long node,
46 const char *uname, int depth, void *data)
47{
48 const void *basep, *entryp;
49 unsigned long basesz, entrysz;
50
51 if (depth != 1 || strcmp(uname, "ibm,opal") != 0)
52 return 0;
53
54 basep = of_get_flat_dt_prop(node, "opal-base-address", &basesz);
55 entryp = of_get_flat_dt_prop(node, "opal-entry-address", &entrysz);
56
57 if (!basep || !entryp)
58 return 1;
59
60 opal.base = of_read_number(basep, basesz/4);
61 opal.entry = of_read_number(entryp, entrysz/4);
62
63 pr_debug("OPAL Base = 0x%llx (basep=%p basesz=%ld)\n",
64 opal.base, basep, basesz);
65 pr_debug("OPAL Entry = 0x%llx (entryp=%p basesz=%ld)\n",
66 opal.entry, entryp, entrysz);
67
68 powerpc_firmware_features |= FW_FEATURE_OPAL;
Benjamin Herrenschmidt75b93da2013-05-14 15:10:02 +100069 if (of_flat_dt_is_compatible(node, "ibm,opal-v3")) {
70 powerpc_firmware_features |= FW_FEATURE_OPALv2;
71 powerpc_firmware_features |= FW_FEATURE_OPALv3;
72 printk("OPAL V3 detected !\n");
73 } else if (of_flat_dt_is_compatible(node, "ibm,opal-v2")) {
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +000074 powerpc_firmware_features |= FW_FEATURE_OPALv2;
75 printk("OPAL V2 detected !\n");
76 } else {
77 printk("OPAL V1 detected !\n");
78 }
79
Jeremy Kerrc4463b32013-05-01 22:31:50 +000080 return 1;
81}
82
83static int __init opal_register_exception_handlers(void)
84{
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +100085#ifdef __BIG_ENDIAN__
Jeremy Kerrc4463b32013-05-01 22:31:50 +000086 u64 glue;
87
88 if (!(powerpc_firmware_features & FW_FEATURE_OPAL))
89 return -ENODEV;
90
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +000091 /* Hookup some exception handlers. We use the fwnmi area at 0x7000
92 * to provide the glue space to OPAL
93 */
94 glue = 0x7000;
95 opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER,
96 __pa(opal_mc_secondary_handler[0]),
97 glue);
98 glue += 128;
99 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER,
100 0, glue);
101 glue += 128;
102 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER, 0, glue);
Benjamin Herrenschmidt29186092013-09-23 12:05:04 +1000103#endif
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000104
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000105 return 0;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000106}
107
Jeremy Kerrc4463b32013-05-01 22:31:50 +0000108early_initcall(opal_register_exception_handlers);
109
Gavin Shan1bc98de2013-06-20 18:13:22 +0800110int opal_notifier_register(struct notifier_block *nb)
111{
112 if (!nb) {
113 pr_warning("%s: Invalid argument (%p)\n",
114 __func__, nb);
115 return -EINVAL;
116 }
117
118 atomic_notifier_chain_register(&opal_notifier_head, nb);
119 return 0;
120}
121
122static void opal_do_notifier(uint64_t events)
123{
124 unsigned long flags;
125 uint64_t changed_mask;
126
127 if (atomic_read(&opal_notifier_hold))
128 return;
129
130 spin_lock_irqsave(&opal_notifier_lock, flags);
131 changed_mask = last_notified_mask ^ events;
132 last_notified_mask = events;
133 spin_unlock_irqrestore(&opal_notifier_lock, flags);
134
135 /*
136 * We feed with the event bits and changed bits for
137 * enough information to the callback.
138 */
139 atomic_notifier_call_chain(&opal_notifier_head,
140 events, (void *)changed_mask);
141}
142
143void opal_notifier_update_evt(uint64_t evt_mask,
144 uint64_t evt_val)
145{
146 unsigned long flags;
147
148 spin_lock_irqsave(&opal_notifier_lock, flags);
149 last_notified_mask &= ~evt_mask;
150 last_notified_mask |= evt_val;
151 spin_unlock_irqrestore(&opal_notifier_lock, flags);
152}
153
154void opal_notifier_enable(void)
155{
156 int64_t rc;
157 uint64_t evt = 0;
158
159 atomic_set(&opal_notifier_hold, 0);
160
161 /* Process pending events */
162 rc = opal_poll_events(&evt);
163 if (rc == OPAL_SUCCESS && evt)
164 opal_do_notifier(evt);
165}
166
167void opal_notifier_disable(void)
168{
169 atomic_set(&opal_notifier_hold, 1);
170}
171
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000172int opal_get_chars(uint32_t vtermno, char *buf, int count)
173{
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000174 s64 rc;
175 __be64 evt, len;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000176
177 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000178 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000179 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000180 if ((be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_INPUT) == 0)
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000181 return 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000182 len = cpu_to_be64(count);
183 rc = opal_console_read(vtermno, &len, buf);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000184 if (rc == OPAL_SUCCESS)
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000185 return be64_to_cpu(len);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000186 return 0;
187}
188
189int opal_put_chars(uint32_t vtermno, const char *data, int total_len)
190{
191 int written = 0;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000192 __be64 olen;
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000193 s64 len, rc;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000194 unsigned long flags;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000195 __be64 evt;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000196
197 if (!opal.entry)
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000198 return -ENODEV;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000199
200 /* We want put_chars to be atomic to avoid mangling of hvsi
201 * packets. To do that, we first test for room and return
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000202 * -EAGAIN if there isn't enough.
203 *
204 * Unfortunately, opal_console_write_buffer_space() doesn't
205 * appear to work on opal v1, so we just assume there is
206 * enough room and be done with it
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000207 */
208 spin_lock_irqsave(&opal_write_lock, flags);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000209 if (firmware_has_feature(FW_FEATURE_OPALv2)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000210 rc = opal_console_write_buffer_space(vtermno, &olen);
211 len = be64_to_cpu(olen);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000212 if (rc || len < total_len) {
213 spin_unlock_irqrestore(&opal_write_lock, flags);
214 /* Closed -> drop characters */
215 if (rc)
216 return total_len;
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000217 opal_poll_events(NULL);
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000218 return -EAGAIN;
219 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000220 }
221
222 /* We still try to handle partial completions, though they
223 * should no longer happen.
224 */
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000225 rc = OPAL_BUSY;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000226 while(total_len > 0 && (rc == OPAL_BUSY ||
227 rc == OPAL_BUSY_EVENT || rc == OPAL_SUCCESS)) {
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000228 olen = cpu_to_be64(total_len);
229 rc = opal_console_write(vtermno, &olen, data);
230 len = be64_to_cpu(olen);
Benjamin Herrenschmidt1de14552013-05-08 14:14:26 +1000231
232 /* Closed or other error drop */
233 if (rc != OPAL_SUCCESS && rc != OPAL_BUSY &&
234 rc != OPAL_BUSY_EVENT) {
235 written = total_len;
236 break;
237 }
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000238 if (rc == OPAL_SUCCESS) {
239 total_len -= len;
240 data += len;
241 written += len;
242 }
243 /* This is a bit nasty but we need that for the console to
244 * flush when there aren't any interrupts. We will clean
245 * things a bit later to limit that to synchronous path
246 * such as the kernel console and xmon/udbg
247 */
248 do
249 opal_poll_events(&evt);
Benjamin Herrenschmidt4f893632013-09-23 12:05:02 +1000250 while(rc == OPAL_SUCCESS &&
251 (be64_to_cpu(evt) & OPAL_EVENT_CONSOLE_OUTPUT));
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000252 }
253 spin_unlock_irqrestore(&opal_write_lock, flags);
254 return written;
255}
256
Benjamin Herrenschmidted79ba92011-09-19 17:45:04 +0000257int opal_machine_check(struct pt_regs *regs)
258{
259 struct opal_machine_check_event *opal_evt = get_paca()->opal_mc_evt;
260 struct opal_machine_check_event evt;
261 const char *level, *sevstr, *subtype;
262 static const char *opal_mc_ue_types[] = {
263 "Indeterminate",
264 "Instruction fetch",
265 "Page table walk ifetch",
266 "Load/Store",
267 "Page table walk Load/Store",
268 };
269 static const char *opal_mc_slb_types[] = {
270 "Indeterminate",
271 "Parity",
272 "Multihit",
273 };
274 static const char *opal_mc_erat_types[] = {
275 "Indeterminate",
276 "Parity",
277 "Multihit",
278 };
279 static const char *opal_mc_tlb_types[] = {
280 "Indeterminate",
281 "Parity",
282 "Multihit",
283 };
284
285 /* Copy the event structure and release the original */
286 evt = *opal_evt;
287 opal_evt->in_use = 0;
288
289 /* Print things out */
290 if (evt.version != OpalMCE_V1) {
291 pr_err("Machine Check Exception, Unknown event version %d !\n",
292 evt.version);
293 return 0;
294 }
295 switch(evt.severity) {
296 case OpalMCE_SEV_NO_ERROR:
297 level = KERN_INFO;
298 sevstr = "Harmless";
299 break;
300 case OpalMCE_SEV_WARNING:
301 level = KERN_WARNING;
302 sevstr = "";
303 break;
304 case OpalMCE_SEV_ERROR_SYNC:
305 level = KERN_ERR;
306 sevstr = "Severe";
307 break;
308 case OpalMCE_SEV_FATAL:
309 default:
310 level = KERN_ERR;
311 sevstr = "Fatal";
312 break;
313 }
314
315 printk("%s%s Machine check interrupt [%s]\n", level, sevstr,
316 evt.disposition == OpalMCE_DISPOSITION_RECOVERED ?
317 "Recovered" : "[Not recovered");
318 printk("%s Initiator: %s\n", level,
319 evt.initiator == OpalMCE_INITIATOR_CPU ? "CPU" : "Unknown");
320 switch(evt.error_type) {
321 case OpalMCE_ERROR_TYPE_UE:
322 subtype = evt.u.ue_error.ue_error_type <
323 ARRAY_SIZE(opal_mc_ue_types) ?
324 opal_mc_ue_types[evt.u.ue_error.ue_error_type]
325 : "Unknown";
326 printk("%s Error type: UE [%s]\n", level, subtype);
327 if (evt.u.ue_error.effective_address_provided)
328 printk("%s Effective address: %016llx\n",
329 level, evt.u.ue_error.effective_address);
330 if (evt.u.ue_error.physical_address_provided)
331 printk("%s Physial address: %016llx\n",
332 level, evt.u.ue_error.physical_address);
333 break;
334 case OpalMCE_ERROR_TYPE_SLB:
335 subtype = evt.u.slb_error.slb_error_type <
336 ARRAY_SIZE(opal_mc_slb_types) ?
337 opal_mc_slb_types[evt.u.slb_error.slb_error_type]
338 : "Unknown";
339 printk("%s Error type: SLB [%s]\n", level, subtype);
340 if (evt.u.slb_error.effective_address_provided)
341 printk("%s Effective address: %016llx\n",
342 level, evt.u.slb_error.effective_address);
343 break;
344 case OpalMCE_ERROR_TYPE_ERAT:
345 subtype = evt.u.erat_error.erat_error_type <
346 ARRAY_SIZE(opal_mc_erat_types) ?
347 opal_mc_erat_types[evt.u.erat_error.erat_error_type]
348 : "Unknown";
349 printk("%s Error type: ERAT [%s]\n", level, subtype);
350 if (evt.u.erat_error.effective_address_provided)
351 printk("%s Effective address: %016llx\n",
352 level, evt.u.erat_error.effective_address);
353 break;
354 case OpalMCE_ERROR_TYPE_TLB:
355 subtype = evt.u.tlb_error.tlb_error_type <
356 ARRAY_SIZE(opal_mc_tlb_types) ?
357 opal_mc_tlb_types[evt.u.tlb_error.tlb_error_type]
358 : "Unknown";
359 printk("%s Error type: TLB [%s]\n", level, subtype);
360 if (evt.u.tlb_error.effective_address_provided)
361 printk("%s Effective address: %016llx\n",
362 level, evt.u.tlb_error.effective_address);
363 break;
364 default:
365 case OpalMCE_ERROR_TYPE_UNKNOWN:
366 printk("%s Error type: Unknown\n", level);
367 break;
368 }
369 return evt.severity == OpalMCE_SEV_FATAL ? 0 : 1;
370}
371
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000372static irqreturn_t opal_interrupt(int irq, void *data)
373{
Anton Blanchard5e4da532013-09-23 12:05:06 +1000374 __be64 events;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000375
376 opal_handle_interrupt(virq_to_hw(irq), &events);
377
Gavin Shan1bc98de2013-06-20 18:13:22 +0800378 opal_do_notifier(events);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000379
380 return IRQ_HANDLED;
381}
382
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530383static int opal_sysfs_init(void)
384{
385 opal_kobj = kobject_create_and_add("opal", firmware_kobj);
386 if (!opal_kobj) {
387 pr_warn("kobject_create_and_add opal failed\n");
388 return -ENOMEM;
389 }
390
391 return 0;
392}
393
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000394static int __init opal_init(void)
395{
396 struct device_node *np, *consoles;
Alistair Popple1cc79bc2013-09-23 12:04:54 +1000397 const __be32 *irqs;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000398 int rc, i, irqlen;
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000399
400 opal_node = of_find_node_by_path("/ibm,opal");
401 if (!opal_node) {
402 pr_warn("opal: Node not found\n");
403 return -ENODEV;
404 }
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000405
406 /* Register OPAL consoles if any ports */
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000407 if (firmware_has_feature(FW_FEATURE_OPALv2))
408 consoles = of_find_node_by_path("/ibm,opal/consoles");
409 else
410 consoles = of_node_get(opal_node);
Benjamin Herrenschmidt2db29d22013-07-15 13:03:14 +1000411 if (consoles) {
412 for_each_child_of_node(consoles, np) {
413 if (strcmp(np->name, "serial"))
414 continue;
415 of_platform_device_create(np, NULL, NULL);
416 }
417 of_node_put(consoles);
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000418 }
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000419
420 /* Find all OPAL interrupts and request them */
421 irqs = of_get_property(opal_node, "opal-interrupts", &irqlen);
422 pr_debug("opal: Found %d interrupts reserved for OPAL\n",
423 irqs ? (irqlen / 4) : 0);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000424 opal_irq_count = irqlen / 4;
425 opal_irqs = kzalloc(opal_irq_count * sizeof(unsigned int), GFP_KERNEL);
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000426 for (i = 0; irqs && i < (irqlen / 4); i++, irqs++) {
427 unsigned int hwirq = be32_to_cpup(irqs);
428 unsigned int irq = irq_create_mapping(NULL, hwirq);
429 if (irq == NO_IRQ) {
430 pr_warning("opal: Failed to map irq 0x%x\n", hwirq);
431 continue;
432 }
433 rc = request_irq(irq, opal_interrupt, 0, "opal", NULL);
434 if (rc)
435 pr_warning("opal: Error %d requesting irq %d"
436 " (0x%x)\n", rc, irq, hwirq);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000437 opal_irqs[i] = irq;
Benjamin Herrenschmidta125e092011-09-19 17:45:03 +0000438 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530439
440 /* Create "opal" kobject under /sys/firmware */
441 rc = opal_sysfs_init();
Vasant Hegde50bd6152013-10-24 16:04:58 +0530442 if (rc == 0) {
443 /* Setup code update interface */
444 opal_flash_init();
445 }
Vasant Hegde6f68b5e2013-08-27 15:09:52 +0530446
Benjamin Herrenschmidt14a43e62011-09-19 17:44:57 +0000447 return 0;
448}
449subsys_initcall(opal_init);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000450
451void opal_shutdown(void)
452{
453 unsigned int i;
454
455 for (i = 0; i < opal_irq_count; i++) {
456 if (opal_irqs[i])
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000457 free_irq(opal_irqs[i], NULL);
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000458 opal_irqs[i] = 0;
459 }
460}