blob: 31853d3167b56b3f6a91df2de41f527217d9dec4 [file] [log] [blame]
Grant Likely6469dfb2011-07-26 03:19:06 -06001#include <linux/irq.h>
2#include <linux/irqdomain.h>
3#include <linux/module.h>
4#include <linux/mutex.h>
5#include <linux/of.h>
Grant Likely971a0ee2011-07-26 03:19:06 -06006#include <linux/of_address.h>
7#include <linux/slab.h>
Grant Likely6469dfb2011-07-26 03:19:06 -06008
9static LIST_HEAD(irq_domain_list);
10static DEFINE_MUTEX(irq_domain_mutex);
11
12/**
13 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure
15 *
Michael Bohane6717fd2012-01-03 16:37:43 -080016 * Adds a irq_domain structure. The irq_domain must at a minimum be
Grant Likely6469dfb2011-07-26 03:19:06 -060017 * initialized with an ops structure pointer, and either a ->to_irq hook or
Michael Bohan6f4df412012-01-11 11:45:16 -080018 * a valid irq_base value. The irq range must be mutually exclusive with
19 * domains already registered. Everything else is optional.
Grant Likely6469dfb2011-07-26 03:19:06 -060020 */
Michael Bohan6f4df412012-01-11 11:45:16 -080021int irq_domain_add(struct irq_domain *domain)
Grant Likely6469dfb2011-07-26 03:19:06 -060022{
Michael Bohan6f4df412012-01-11 11:45:16 -080023 struct irq_domain *curr;
24 uint32_t d_highirq = domain->irq_base + domain->nr_irq - 1;
25
26 if (!domain->nr_irq)
27 return -EINVAL;
28
Michael Bohane6717fd2012-01-03 16:37:43 -080029 mutex_lock(&irq_domain_mutex);
Michael Bohan6f4df412012-01-11 11:45:16 -080030 /* insert in ascending order of domain->irq_base */
31 list_for_each_entry(curr, &irq_domain_list, list) {
32 uint32_t c_highirq = curr->irq_base + curr->nr_irq - 1;
33 if (domain->irq_base < curr->irq_base &&
34 d_highirq < curr->irq_base) {
35 break;
36 }
37 if (d_highirq <= c_highirq) {
38 mutex_unlock(&irq_domain_mutex);
39 return -EINVAL;
40 }
41 }
42 list_add_tail(&domain->list, &curr->list);
Michael Bohane6717fd2012-01-03 16:37:43 -080043 mutex_unlock(&irq_domain_mutex);
Michael Bohan6f4df412012-01-11 11:45:16 -080044
45 return 0;
Michael Bohane6717fd2012-01-03 16:37:43 -080046}
47
48/**
49 * irq_domain_register() - Register an entire irq_domain
50 * @domain: ptr to initialized irq_domain structure
51 *
52 * Registers the entire irq_domain. The irq_domain must at a minimum be
53 * initialized with an ops structure pointer, and either a ->to_irq hook or
54 * a valid irq_base value. Everything else is optional.
55 */
56void irq_domain_register(struct irq_domain *domain)
57{
Grant Likely6469dfb2011-07-26 03:19:06 -060058 struct irq_data *d;
Rob Herring60fe4602011-09-30 10:48:38 -050059 int hwirq, irq;
Grant Likely6469dfb2011-07-26 03:19:06 -060060
Rob Herring60fe4602011-09-30 10:48:38 -050061 irq_domain_for_each_irq(domain, hwirq, irq) {
62 d = irq_get_irq_data(irq);
Rob Herring36b2c362011-09-14 11:31:37 -050063 if (!d) {
64 WARN(1, "error: assigning domain to non existant irq_desc");
65 return;
66 }
67 if (d->domain) {
Grant Likely6469dfb2011-07-26 03:19:06 -060068 /* things are broken; just report, don't clean up */
69 WARN(1, "error: irq_desc already assigned to a domain");
70 return;
71 }
72 d->domain = domain;
73 d->hwirq = hwirq;
74 }
Grant Likely6469dfb2011-07-26 03:19:06 -060075}
76
77/**
Michael Bohane6717fd2012-01-03 16:37:43 -080078 * irq_domain_register_irq() - Register an irq_domain
79 * @domain: ptr to initialized irq_domain structure
80 * @hwirq: irq_domain hwirq to register
81 *
82 * Registers a specific hwirq within the irq_domain. The irq_domain
83 * must at a minimum be initialized with an ops structure pointer, and
84 * either a ->to_irq hook or a valid irq_base value. Everything else is
85 * optional.
86 */
87void irq_domain_register_irq(struct irq_domain *domain, int hwirq)
88{
89 struct irq_data *d;
90
91 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
92 if (!d) {
93 WARN(1, "error: assigning domain to non existant irq_desc");
94 return;
95 }
96 if (d->domain) {
97 /* things are broken; just report, don't clean up */
98 WARN(1, "error: irq_desc already assigned to a domain");
99 return;
100 }
101 d->domain = domain;
102 d->hwirq = hwirq;
103}
104
105/**
106 * irq_domain_del() - Removes a irq_domain from the system
Grant Likely6469dfb2011-07-26 03:19:06 -0600107 * @domain: ptr to registered irq_domain.
108 */
109void irq_domain_del(struct irq_domain *domain)
110{
Grant Likely6469dfb2011-07-26 03:19:06 -0600111 mutex_lock(&irq_domain_mutex);
112 list_del(&domain->list);
113 mutex_unlock(&irq_domain_mutex);
Michael Bohane6717fd2012-01-03 16:37:43 -0800114}
115
116/**
117 * irq_domain_unregister() - Unregister an irq_domain
118 * @domain: ptr to registered irq_domain.
119 */
120void irq_domain_unregister(struct irq_domain *domain)
121{
122 struct irq_data *d;
123 int hwirq, irq;
Grant Likely6469dfb2011-07-26 03:19:06 -0600124
125 /* Clear the irq_domain assignments */
Rob Herring60fe4602011-09-30 10:48:38 -0500126 irq_domain_for_each_irq(domain, hwirq, irq) {
127 d = irq_get_irq_data(irq);
Grant Likely6469dfb2011-07-26 03:19:06 -0600128 d->domain = NULL;
129 }
130}
131
Michael Bohane6717fd2012-01-03 16:37:43 -0800132/**
133 * irq_domain_unregister_irq() - Unregister a hwirq within a irq_domain
134 * @domain: ptr to registered irq_domain.
135 * @hwirq: irq_domain hwirq to unregister.
136 */
137void irq_domain_unregister_irq(struct irq_domain *domain, int hwirq)
138{
139 struct irq_data *d;
140
141 /* Clear the irq_domain assignment */
142 d = irq_get_irq_data(irq_domain_to_irq(domain, hwirq));
143 d->domain = NULL;
144}
145
Grant Likely6469dfb2011-07-26 03:19:06 -0600146#if defined(CONFIG_OF_IRQ)
147/**
148 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
149 *
150 * Used by the device tree interrupt mapping code to translate a device tree
151 * interrupt specifier to a valid linux irq number. Returns either a valid
152 * linux IRQ number or 0.
153 *
154 * When the caller no longer need the irq number returned by this function it
155 * should arrange to call irq_dispose_mapping().
156 */
157unsigned int irq_create_of_mapping(struct device_node *controller,
158 const u32 *intspec, unsigned int intsize)
159{
160 struct irq_domain *domain;
161 unsigned long hwirq;
162 unsigned int irq, type;
163 int rc = -EINVAL;
164
165 /* Find a domain which can translate the irq spec */
166 mutex_lock(&irq_domain_mutex);
167 list_for_each_entry(domain, &irq_domain_list, list) {
168 if (!domain->ops->dt_translate)
169 continue;
Michael Bohan6f4df412012-01-11 11:45:16 -0800170
Grant Likely6469dfb2011-07-26 03:19:06 -0600171 rc = domain->ops->dt_translate(domain, controller,
172 intspec, intsize, &hwirq, &type);
173 if (rc == 0)
174 break;
175 }
176 mutex_unlock(&irq_domain_mutex);
177
178 if (rc != 0)
179 return 0;
180
181 irq = irq_domain_to_irq(domain, hwirq);
182 if (type != IRQ_TYPE_NONE)
183 irq_set_irq_type(irq, type);
184 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
185 controller->full_name, (int)hwirq, irq, type);
186 return irq;
187}
188EXPORT_SYMBOL_GPL(irq_create_of_mapping);
189
190/**
191 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
192 * @irq: linux irq number to be discarded
193 *
194 * Calling this function indicates the caller no longer needs a reference to
195 * the linux irq number returned by a prior call to irq_create_of_mapping().
196 */
197void irq_dispose_mapping(unsigned int irq)
198{
199 /*
200 * nothing yet; will be filled when support for dynamic allocation of
201 * irq_descs is added to irq_domain
202 */
203}
204EXPORT_SYMBOL_GPL(irq_dispose_mapping);
Grant Likely971a0ee2011-07-26 03:19:06 -0600205
206int irq_domain_simple_dt_translate(struct irq_domain *d,
207 struct device_node *controller,
208 const u32 *intspec, unsigned int intsize,
209 unsigned long *out_hwirq, unsigned int *out_type)
210{
211 if (d->of_node != controller)
212 return -EINVAL;
213 if (intsize < 1)
214 return -EINVAL;
215
216 *out_hwirq = intspec[0];
217 *out_type = IRQ_TYPE_NONE;
218 if (intsize > 1)
219 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
220 return 0;
221}
222
223struct irq_domain_ops irq_domain_simple_ops = {
224 .dt_translate = irq_domain_simple_dt_translate,
225};
226EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
227
228/**
229 * irq_domain_create_simple() - Set up a 'simple' translation range
230 */
231void irq_domain_add_simple(struct device_node *controller, int irq_base)
232{
233 struct irq_domain *domain;
Michael Bohan6f4df412012-01-11 11:45:16 -0800234 int rc;
Grant Likely971a0ee2011-07-26 03:19:06 -0600235
236 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
237 if (!domain) {
238 WARN_ON(1);
239 return;
240 }
241
242 domain->irq_base = irq_base;
243 domain->of_node = of_node_get(controller);
244 domain->ops = &irq_domain_simple_ops;
Michael Bohan6f4df412012-01-11 11:45:16 -0800245 rc = irq_domain_add(domain);
246 if (rc) {
247 WARN(1, "Unable to create irq domain\n");
248 return;
249 }
Michael Bohane6717fd2012-01-03 16:37:43 -0800250 irq_domain_register(domain);
Grant Likely971a0ee2011-07-26 03:19:06 -0600251}
252EXPORT_SYMBOL_GPL(irq_domain_add_simple);
253
254void irq_domain_generate_simple(const struct of_device_id *match,
255 u64 phys_base, unsigned int irq_start)
256{
257 struct device_node *node;
258 pr_info("looking for phys_base=%llx, irq_start=%i\n",
259 (unsigned long long) phys_base, (int) irq_start);
260 node = of_find_matching_node_by_address(NULL, match, phys_base);
261 if (node)
262 irq_domain_add_simple(node, irq_start);
263 else
264 pr_info("no node found\n");
265}
266EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely6469dfb2011-07-26 03:19:06 -0600267#endif /* CONFIG_OF_IRQ */