1 /*
2  * Copyright 2010 2011 Mark Nelson and Tseng-Hui (Frank) Lin, IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU General Public License
6  *  as published by the Free Software Foundation; either version
7  *  2 of the License, or (at your option) any later version.
8  */
9 
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/irq.h>
14 #include <linux/interrupt.h>
15 #include <linux/of.h>
16 #include <linux/list.h>
17 #include <linux/notifier.h>
18 
19 #include <asm/machdep.h>
20 #include <asm/rtas.h>
21 #include <asm/irq.h>
22 #include <asm/io_event_irq.h>
23 
24 #include "pseries.h"
25 
26 /*
27  * IO event interrupt is a mechanism provided by RTAS to return
28  * information about hardware error and non-error events. Device
29  * drivers can register their event handlers to receive events.
30  * Device drivers are expected to use atomic_notifier_chain_register()
31  * and atomic_notifier_chain_unregister() to register and unregister
32  * their event handlers. Since multiple IO event types and scopes
33  * share an IO event interrupt, the event handlers are called one
34  * by one until the IO event is claimed by one of the handlers.
35  * The event handlers are expected to return NOTIFY_OK if the
36  * event is handled by the event handler or NOTIFY_DONE if the
37  * event does not belong to the handler.
38  *
39  * Usage:
40  *
41  * Notifier function:
42  * #include <asm/io_event_irq.h>
43  * int event_handler(struct notifier_block *nb, unsigned long val, void *data) {
44  * 	p = (struct pseries_io_event_sect_data *) data;
45  * 	if (! is_my_event(p->scope, p->event_type)) return NOTIFY_DONE;
46  * 		:
47  * 		:
48  * 	return NOTIFY_OK;
49  * }
50  * struct notifier_block event_nb = {
51  * 	.notifier_call = event_handler,
52  * }
53  *
54  * Registration:
55  * atomic_notifier_chain_register(&pseries_ioei_notifier_list, &event_nb);
56  *
57  * Unregistration:
58  * atomic_notifier_chain_unregister(&pseries_ioei_notifier_list, &event_nb);
59  */
60 
61 ATOMIC_NOTIFIER_HEAD(pseries_ioei_notifier_list);
62 EXPORT_SYMBOL_GPL(pseries_ioei_notifier_list);
63 
64 static int ioei_check_exception_token;
65 
66 static char ioei_rtas_buf[RTAS_DATA_BUF_SIZE] __cacheline_aligned;
67 
68 /**
69  * Find the data portion of an IO Event section from event log.
70  * @elog: RTAS error/event log.
71  *
72  * Return:
73  * 	pointer to a valid IO event section data. NULL if not found.
74  */
75 static struct pseries_io_event * ioei_find_event(struct rtas_error_log *elog)
76 {
77 	struct pseries_errorlog *sect;
78 
79 	/* We should only ever get called for io-event interrupts, but if
80 	 * we do get called for another type then something went wrong so
81 	 * make some noise about it.
82 	 * RTAS_TYPE_IO only exists in extended event log version 6 or later.
83 	 * No need to check event log version.
84 	 */
85 	if (unlikely(rtas_error_type(elog) != RTAS_TYPE_IO)) {
86 		printk_once(KERN_WARNING"io_event_irq: Unexpected event type %d",
87 			    rtas_error_type(elog));
88 		return NULL;
89 	}
90 
91 	sect = get_pseries_errorlog(elog, PSERIES_ELOG_SECT_ID_IO_EVENT);
92 	if (unlikely(!sect)) {
93 		printk_once(KERN_WARNING "io_event_irq: RTAS extended event "
94 			    "log does not contain an IO Event section. "
95 			    "Could be a bug in system firmware!\n");
96 		return NULL;
97 	}
98 	return (struct pseries_io_event *) &sect->data;
99 }
100 
101 /*
102  * PAPR:
103  * - check-exception returns the first found error or event and clear that
104  *   error or event so it is reported once.
105  * - Each interrupt returns one event. If a plateform chooses to report
106  *   multiple events through a single interrupt, it must ensure that the
107  *   interrupt remains asserted until check-exception has been used to
108  *   process all out-standing events for that interrupt.
109  *
110  * Implementation notes:
111  * - Events must be processed in the order they are returned. Hence,
112  *   sequential in nature.
113  * - The owner of an event is determined by combinations of scope,
114  *   event type, and sub-type. There is no easy way to pre-sort clients
115  *   by scope or event type alone. For example, Torrent ISR route change
116  *   event is reported with scope 0x00 (Not Applicatable) rather than
117  *   0x3B (Torrent-hub). It is better to let the clients to identify
118  *   who owns the event.
119  */
120 
121 static irqreturn_t ioei_interrupt(int irq, void *dev_id)
122 {
123 	struct pseries_io_event *event;
124 	int rtas_rc;
125 
126 	for (;;) {
127 		rtas_rc = rtas_call(ioei_check_exception_token, 6, 1, NULL,
128 				    RTAS_VECTOR_EXTERNAL_INTERRUPT,
129 				    virq_to_hw(irq),
130 				    RTAS_IO_EVENTS, 1 /* Time Critical */,
131 				    __pa(ioei_rtas_buf),
132 				    RTAS_DATA_BUF_SIZE);
133 		if (rtas_rc != 0)
134 			break;
135 
136 		event = ioei_find_event((struct rtas_error_log *)ioei_rtas_buf);
137 		if (!event)
138 			continue;
139 
140 		atomic_notifier_call_chain(&pseries_ioei_notifier_list,
141 					   0, event);
142 	}
143 	return IRQ_HANDLED;
144 }
145 
146 static int __init ioei_init(void)
147 {
148 	struct device_node *np;
149 
150 	ioei_check_exception_token = rtas_token("check-exception");
151 	if (ioei_check_exception_token == RTAS_UNKNOWN_SERVICE)
152 		return -ENODEV;
153 
154 	np = of_find_node_by_path("/event-sources/ibm,io-events");
155 	if (np) {
156 		request_event_sources_irqs(np, ioei_interrupt, "IO_EVENT");
157 		pr_info("IBM I/O event interrupts enabled\n");
158 		of_node_put(np);
159 	} else {
160 		return -ENODEV;
161 	}
162 	return 0;
163 }
164 machine_subsys_initcall(pseries, ioei_init);
165 
166