2 * PowerNV OPAL high level interfaces
4 * Copyright 2011 IBM Corp.
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.
14 #include <linux/types.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_platform.h>
18 #include <linux/interrupt.h>
19 #include <linux/notifier.h>
20 #include <linux/slab.h>
21 #include <linux/kobject.h>
23 #include <asm/firmware.h>
27 /* /sys/firmware/opal */
28 struct kobject
*opal_kobj
;
35 static struct device_node
*opal_node
;
36 static DEFINE_SPINLOCK(opal_write_lock
);
37 extern u64 opal_mc_secondary_handler
[];
38 static unsigned int *opal_irqs
;
39 static unsigned int opal_irq_count
;
40 static ATOMIC_NOTIFIER_HEAD(opal_notifier_head
);
41 static DEFINE_SPINLOCK(opal_notifier_lock
);
42 static uint64_t last_notified_mask
= 0x0ul
;
43 static atomic_t opal_notifier_hold
= ATOMIC_INIT(0);
45 int __init
early_init_dt_scan_opal(unsigned long node
,
46 const char *uname
, int depth
, void *data
)
48 const void *basep
, *entryp
;
49 unsigned long basesz
, entrysz
;
51 if (depth
!= 1 || strcmp(uname
, "ibm,opal") != 0)
54 basep
= of_get_flat_dt_prop(node
, "opal-base-address", &basesz
);
55 entryp
= of_get_flat_dt_prop(node
, "opal-entry-address", &entrysz
);
57 if (!basep
|| !entryp
)
60 opal
.base
= of_read_number(basep
, basesz
/4);
61 opal
.entry
= of_read_number(entryp
, entrysz
/4);
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
);
68 powerpc_firmware_features
|= FW_FEATURE_OPAL
;
69 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")) {
74 powerpc_firmware_features
|= FW_FEATURE_OPALv2
;
75 printk("OPAL V2 detected !\n");
77 printk("OPAL V1 detected !\n");
83 static int __init
opal_register_exception_handlers(void)
88 if (!(powerpc_firmware_features
& FW_FEATURE_OPAL
))
91 /* Hookup some exception handlers. We use the fwnmi area at 0x7000
92 * to provide the glue space to OPAL
95 opal_register_exception_handler(OPAL_MACHINE_CHECK_HANDLER
,
96 __pa(opal_mc_secondary_handler
[0]),
99 opal_register_exception_handler(OPAL_HYPERVISOR_MAINTENANCE_HANDLER
,
102 opal_register_exception_handler(OPAL_SOFTPATCH_HANDLER
, 0, glue
);
108 early_initcall(opal_register_exception_handlers
);
110 int opal_notifier_register(struct notifier_block
*nb
)
113 pr_warning("%s: Invalid argument (%p)\n",
118 atomic_notifier_chain_register(&opal_notifier_head
, nb
);
122 static void opal_do_notifier(uint64_t events
)
125 uint64_t changed_mask
;
127 if (atomic_read(&opal_notifier_hold
))
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
);
136 * We feed with the event bits and changed bits for
137 * enough information to the callback.
139 atomic_notifier_call_chain(&opal_notifier_head
,
140 events
, (void *)changed_mask
);
143 void opal_notifier_update_evt(uint64_t evt_mask
,
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
);
154 void opal_notifier_enable(void)
159 atomic_set(&opal_notifier_hold
, 0);
161 /* Process pending events */
162 rc
= opal_poll_events(&evt
);
163 if (rc
== OPAL_SUCCESS
&& evt
)
164 opal_do_notifier(evt
);
167 void opal_notifier_disable(void)
169 atomic_set(&opal_notifier_hold
, 1);
172 int opal_get_chars(uint32_t vtermno
, char *buf
, int count
)
179 opal_poll_events(&evt
);
180 if ((be64_to_cpu(evt
) & OPAL_EVENT_CONSOLE_INPUT
) == 0)
182 len
= cpu_to_be64(count
);
183 rc
= opal_console_read(vtermno
, &len
, buf
);
184 if (rc
== OPAL_SUCCESS
)
185 return be64_to_cpu(len
);
189 int opal_put_chars(uint32_t vtermno
, const char *data
, int total_len
)
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
202 * -EAGAIN if there isn't enough.
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
208 spin_lock_irqsave(&opal_write_lock
, flags
);
209 if (firmware_has_feature(FW_FEATURE_OPALv2
)) {
210 rc
= opal_console_write_buffer_space(vtermno
, &olen
);
211 len
= be64_to_cpu(olen
);
212 if (rc
|| len
< total_len
) {
213 spin_unlock_irqrestore(&opal_write_lock
, flags
);
214 /* Closed -> drop characters */
217 opal_poll_events(NULL
);
222 /* We still try to handle partial completions, though they
223 * should no longer happen.
226 while(total_len
> 0 && (rc
== OPAL_BUSY
||
227 rc
== OPAL_BUSY_EVENT
|| rc
== OPAL_SUCCESS
)) {
228 olen
= cpu_to_be64(total_len
);
229 rc
= opal_console_write(vtermno
, &olen
, data
);
230 len
= be64_to_cpu(olen
);
232 /* Closed or other error drop */
233 if (rc
!= OPAL_SUCCESS
&& rc
!= OPAL_BUSY
&&
234 rc
!= OPAL_BUSY_EVENT
) {
238 if (rc
== OPAL_SUCCESS
) {
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
249 opal_poll_events(&evt
);
250 while(rc
== OPAL_SUCCESS
&&
251 (be64_to_cpu(evt
) & OPAL_EVENT_CONSOLE_OUTPUT
));
253 spin_unlock_irqrestore(&opal_write_lock
, flags
);
257 int opal_machine_check(struct pt_regs
*regs
)
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
[] = {
265 "Page table walk ifetch",
267 "Page table walk Load/Store",
269 static const char *opal_mc_slb_types
[] = {
274 static const char *opal_mc_erat_types
[] = {
279 static const char *opal_mc_tlb_types
[] = {
285 /* Copy the event structure and release the original */
287 opal_evt
->in_use
= 0;
289 /* Print things out */
290 if (evt
.version
!= OpalMCE_V1
) {
291 pr_err("Machine Check Exception, Unknown event version %d !\n",
295 switch(evt
.severity
) {
296 case OpalMCE_SEV_NO_ERROR
:
300 case OpalMCE_SEV_WARNING
:
301 level
= KERN_WARNING
;
304 case OpalMCE_SEV_ERROR_SYNC
:
308 case OpalMCE_SEV_FATAL
:
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
]
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
);
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
]
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
);
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
]
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
);
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
]
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
);
365 case OpalMCE_ERROR_TYPE_UNKNOWN
:
366 printk("%s Error type: Unknown\n", level
);
369 return evt
.severity
== OpalMCE_SEV_FATAL
? 0 : 1;
372 static irqreturn_t
opal_interrupt(int irq
, void *data
)
376 opal_handle_interrupt(virq_to_hw(irq
), &events
);
378 opal_do_notifier(events
);
383 static int opal_sysfs_init(void)
385 opal_kobj
= kobject_create_and_add("opal", firmware_kobj
);
387 pr_warn("kobject_create_and_add opal failed\n");
394 static int __init
opal_init(void)
396 struct device_node
*np
, *consoles
;
400 opal_node
= of_find_node_by_path("/ibm,opal");
402 pr_warn("opal: Node not found\n");
406 /* Register OPAL consoles if any ports */
407 if (firmware_has_feature(FW_FEATURE_OPALv2
))
408 consoles
= of_find_node_by_path("/ibm,opal/consoles");
410 consoles
= of_node_get(opal_node
);
412 for_each_child_of_node(consoles
, np
) {
413 if (strcmp(np
->name
, "serial"))
415 of_platform_device_create(np
, NULL
, NULL
);
417 of_node_put(consoles
);
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);
424 opal_irq_count
= irqlen
/ 4;
425 opal_irqs
= kzalloc(opal_irq_count
* sizeof(unsigned int), GFP_KERNEL
);
426 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
);
430 pr_warning("opal: Failed to map irq 0x%x\n", hwirq
);
433 rc
= request_irq(irq
, opal_interrupt
, 0, "opal", NULL
);
435 pr_warning("opal: Error %d requesting irq %d"
436 " (0x%x)\n", rc
, irq
, hwirq
);
440 /* Create "opal" kobject under /sys/firmware */
441 rc
= opal_sysfs_init();
443 /* Setup code update interface */
449 subsys_initcall(opal_init
);
451 void opal_shutdown(void)
455 for (i
= 0; i
< opal_irq_count
; i
++) {
457 free_irq(opal_irqs
[i
], NULL
);