blob: 0aa96d348ab0c7d3610d99563b6c5dfd8d3e4790 [file] [log] [blame]
Grant Likely08a543a2011-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 Likely7e713302011-07-26 03:19:06 -06006#include <linux/of_address.h>
7#include <linux/slab.h>
Grant Likelya8db8cf2012-02-14 14:06:54 -07008
Grant Likely08a543a2011-07-26 03:19:06 -06009static LIST_HEAD(irq_domain_list);
10static DEFINE_MUTEX(irq_domain_mutex);
11
Grant Likelycc79ca62012-02-16 01:37:49 -070012/**
Grant Likely6469dfb2011-07-26 03:19:06 -060013 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure
Grant Likelycc79ca62012-02-16 01:37:49 -070015 *
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 Likelycc79ca62012-02-16 01:37:49 -070020 */
Michael Bohan6f4df412012-01-11 11:45:16 -080021int irq_domain_add(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -070022{
Michael Bohan6f4df412012-01-11 11:45:16 -080023 struct irq_domain *curr;
24 uint32_t d_highirq = domain->irq_base + domain->nr_irq - 1;
Grant Likelycc79ca62012-02-16 01:37:49 -070025
Michael Bohan6f4df412012-01-11 11:45:16 -080026 if (!domain->nr_irq)
27 return -EINVAL;
Grant Likely1bc04f22012-02-14 14:06:55 -070028
Grant Likelycc79ca62012-02-16 01:37:49 -070029 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) {
Grant Likelycc79ca62012-02-16 01:37:49 -070035 break;
36 }
Michael Bohan6f4df412012-01-11 11:45:16 -080037 if (d_highirq <= c_highirq) {
38 mutex_unlock(&irq_domain_mutex);
39 return -EINVAL;
40 }
Grant Likelya18dc812012-01-26 12:12:14 -070041 }
Michael Bohan6f4df412012-01-11 11:45:16 -080042 list_add_tail(&domain->list, &curr->list);
Grant Likelycc79ca62012-02-16 01:37:49 -070043 mutex_unlock(&irq_domain_mutex);
Grant Likelycc79ca62012-02-16 01:37:49 -070044
45 return 0;
46}
47
48/**
Michael Bohane6717fd2012-01-03 16:37:43 -080049 * irq_domain_register() - Register an entire irq_domain
50 * @domain: ptr to initialized irq_domain structure
Grant Likelycc79ca62012-02-16 01:37:49 -070051 *
Michael Bohane6717fd2012-01-03 16:37:43 -080052 * 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.
Grant Likelycc79ca62012-02-16 01:37:49 -070055 */
Michael Bohane6717fd2012-01-03 16:37:43 -080056void irq_domain_register(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -070057{
Grant Likely6469dfb2011-07-26 03:19:06 -060058 struct irq_data *d;
Rob Herring60fe4602011-09-30 10:48:38 -050059 int hwirq, irq;
Grant Likelycc79ca62012-02-16 01:37:49 -070060
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;
Grant Likelycc79ca62012-02-16 01:37:49 -070074 }
Grant Likelycc79ca62012-02-16 01:37:49 -070075}
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
Grant Likelycc79ca62012-02-16 01:37:49 -070081 *
Michael Bohane6717fd2012-01-03 16:37:43 -080082 * 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.
Grant Likelycc79ca62012-02-16 01:37:49 -070086 */
Michael Bohane6717fd2012-01-03 16:37:43 -080087void irq_domain_register_irq(struct irq_domain *domain, int hwirq)
Grant Likelycc79ca62012-02-16 01:37:49 -070088{
Michael Bohane6717fd2012-01-03 16:37:43 -080089 struct irq_data *d;
Grant Likelycc79ca62012-02-16 01:37:49 -070090
Michael Bohane6717fd2012-01-03 16:37:43 -080091 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;
Grant Likelycc79ca62012-02-16 01:37:49 -070095 }
Michael Bohane6717fd2012-01-03 16:37:43 -080096 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;
Grant Likelycc79ca62012-02-16 01:37:49 -0700100 }
Michael Bohane6717fd2012-01-03 16:37:43 -0800101 d->domain = domain;
102 d->hwirq = hwirq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700103}
Grant Likelycc79ca62012-02-16 01:37:49 -0700104
Michael Bohane6717fd2012-01-03 16:37:43 -0800105/**
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
Michael Bohancf68d7a2012-01-11 17:53:05 -0800146/**
147 * irq_domain_find_free_range() - Find an available irq range
148 * @from: lowest logical irq number to request from
149 * @cnt: number of interrupts to search for
150 *
151 * Finds an available logical irq range from the domains specified
152 * on the system. The from parameter can be used to allocate a range
153 * at least as great as the specified irq number.
154 */
155int irq_domain_find_free_range(unsigned int from, unsigned int cnt)
156{
157 struct irq_domain *curr, *prev = NULL;
158
159 if (list_empty(&irq_domain_list))
160 return from;
161
162 list_for_each_entry(curr, &irq_domain_list, list) {
163 if (prev == NULL) {
164 if ((from + cnt - 1) < curr->irq_base)
165 return from;
166 } else {
167 uint32_t p_next_irq = prev->irq_base + prev->nr_irq;
168 uint32_t start_irq;
169 if (from >= curr->irq_base)
170 continue;
171 if (from < p_next_irq)
172 start_irq = p_next_irq;
173 else
174 start_irq = from;
175 if ((curr->irq_base - start_irq) >= cnt)
176 return p_next_irq;
177 }
178 prev = curr;
179 }
180 curr = list_entry(curr->list.prev, struct irq_domain, list);
181
182 return from > curr->irq_base + curr->nr_irq ?
183 from : curr->irq_base + curr->nr_irq;
184}
185
Grant Likely6469dfb2011-07-26 03:19:06 -0600186#if defined(CONFIG_OF_IRQ)
187/**
188 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
189 *
190 * Used by the device tree interrupt mapping code to translate a device tree
191 * interrupt specifier to a valid linux irq number. Returns either a valid
192 * linux IRQ number or 0.
193 *
194 * When the caller no longer need the irq number returned by this function it
195 * should arrange to call irq_dispose_mapping().
196 */
Grant Likelycc79ca62012-02-16 01:37:49 -0700197unsigned int irq_create_of_mapping(struct device_node *controller,
198 const u32 *intspec, unsigned int intsize)
199{
Grant Likely68700652012-02-14 14:06:53 -0700200 struct irq_domain *domain;
Grant Likely6469dfb2011-07-26 03:19:06 -0600201 unsigned long hwirq;
202 unsigned int irq, type;
203 int rc = -EINVAL;
Grant Likelycc79ca62012-02-16 01:37:49 -0700204
Grant Likely6469dfb2011-07-26 03:19:06 -0600205 /* Find a domain which can translate the irq spec */
206 mutex_lock(&irq_domain_mutex);
207 list_for_each_entry(domain, &irq_domain_list, list) {
208 if (!domain->ops->dt_translate)
209 continue;
Michael Bohan6f4df412012-01-11 11:45:16 -0800210
Grant Likely6469dfb2011-07-26 03:19:06 -0600211 rc = domain->ops->dt_translate(domain, controller,
212 intspec, intsize, &hwirq, &type);
213 if (rc == 0)
214 break;
215 }
216 mutex_unlock(&irq_domain_mutex);
217
218 if (rc != 0)
Grant Likely03848372012-02-14 14:06:52 -0700219 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700220
Grant Likely6469dfb2011-07-26 03:19:06 -0600221 irq = irq_domain_to_irq(domain, hwirq);
222 if (type != IRQ_TYPE_NONE)
223 irq_set_irq_type(irq, type);
224 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
225 controller->full_name, (int)hwirq, irq, type);
226 return irq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700227}
228EXPORT_SYMBOL_GPL(irq_create_of_mapping);
229
230/**
Grant Likely6469dfb2011-07-26 03:19:06 -0600231 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
232 * @irq: linux irq number to be discarded
233 *
234 * Calling this function indicates the caller no longer needs a reference to
235 * the linux irq number returned by a prior call to irq_create_of_mapping().
Grant Likelycc79ca62012-02-16 01:37:49 -0700236 */
Grant Likely6469dfb2011-07-26 03:19:06 -0600237void irq_dispose_mapping(unsigned int irq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700238{
Grant Likely6469dfb2011-07-26 03:19:06 -0600239 /*
240 * nothing yet; will be filled when support for dynamic allocation of
241 * irq_descs is added to irq_domain
242 */
Grant Likelycc79ca62012-02-16 01:37:49 -0700243}
244EXPORT_SYMBOL_GPL(irq_dispose_mapping);
245
Grant Likely971a0ee2011-07-26 03:19:06 -0600246int irq_domain_simple_dt_translate(struct irq_domain *d,
247 struct device_node *controller,
248 const u32 *intspec, unsigned int intsize,
249 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likelycc79ca62012-02-16 01:37:49 -0700250{
Grant Likely971a0ee2011-07-26 03:19:06 -0600251 if (d->of_node != controller)
Grant Likely7e713302011-07-26 03:19:06 -0600252 return -EINVAL;
Grant Likely971a0ee2011-07-26 03:19:06 -0600253 if (intsize < 1)
254 return -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700255 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
256 (intspec[0] >= d->hwirq_base + d->nr_irq)))
257 return -EINVAL;
Grant Likely971a0ee2011-07-26 03:19:06 -0600258
Grant Likely7e713302011-07-26 03:19:06 -0600259 *out_hwirq = intspec[0];
260 *out_type = IRQ_TYPE_NONE;
Grant Likely971a0ee2011-07-26 03:19:06 -0600261 if (intsize > 1)
262 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
Grant Likely7e713302011-07-26 03:19:06 -0600263 return 0;
264}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700265
266/**
Grant Likely971a0ee2011-07-26 03:19:06 -0600267 * irq_domain_create_simple() - Set up a 'simple' translation range
Grant Likely16b2e6e2012-01-26 11:26:52 -0700268 */
Grant Likely971a0ee2011-07-26 03:19:06 -0600269void irq_domain_add_simple(struct device_node *controller, int irq_base)
Grant Likely16b2e6e2012-01-26 11:26:52 -0700270{
Grant Likely971a0ee2011-07-26 03:19:06 -0600271 struct irq_domain *domain;
Michael Bohan6f4df412012-01-11 11:45:16 -0800272 int rc;
Grant Likely971a0ee2011-07-26 03:19:06 -0600273
274 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
275 if (!domain) {
276 WARN_ON(1);
277 return;
278 }
279
280 domain->irq_base = irq_base;
281 domain->of_node = of_node_get(controller);
282 domain->ops = &irq_domain_simple_ops;
Michael Bohan6f4df412012-01-11 11:45:16 -0800283 rc = irq_domain_add(domain);
284 if (rc) {
285 WARN(1, "Unable to create irq domain\n");
286 return;
287 }
Michael Bohane6717fd2012-01-03 16:37:43 -0800288 irq_domain_register(domain);
Grant Likely16b2e6e2012-01-26 11:26:52 -0700289}
Grant Likely971a0ee2011-07-26 03:19:06 -0600290EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Grant Likely16b2e6e2012-01-26 11:26:52 -0700291
Grant Likely7e713302011-07-26 03:19:06 -0600292void irq_domain_generate_simple(const struct of_device_id *match,
293 u64 phys_base, unsigned int irq_start)
294{
295 struct device_node *node;
Grant Likely971a0ee2011-07-26 03:19:06 -0600296 pr_info("looking for phys_base=%llx, irq_start=%i\n",
Grant Likely7e713302011-07-26 03:19:06 -0600297 (unsigned long long) phys_base, (int) irq_start);
298 node = of_find_matching_node_by_address(NULL, match, phys_base);
299 if (node)
Grant Likely971a0ee2011-07-26 03:19:06 -0600300 irq_domain_add_simple(node, irq_start);
301 else
302 pr_info("no node found\n");
Grant Likely7e713302011-07-26 03:19:06 -0600303}
304EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
Grant Likely6469dfb2011-07-26 03:19:06 -0600305#endif /* CONFIG_OF_IRQ */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700306
307struct irq_domain_ops irq_domain_simple_ops = {
308#ifdef CONFIG_OF_IRQ
309 .dt_translate = irq_domain_simple_dt_translate,
310#endif /* CONFIG_OF_IRQ */
311};
312EXPORT_SYMBOL_GPL(irq_domain_simple_ops);