blob: 8c3577fef78c4ee50cd6912d1cc87202e7d17cf2 [file] [log] [blame]
Paul Mundt54a90582012-05-19 15:11:47 +09001#define pr_fmt(fmt) "irq: " fmt
2
Grant Likelycc79ca62012-02-16 01:37:49 -07003#include <linux/debugfs.h>
4#include <linux/hardirq.h>
5#include <linux/interrupt.h>
Grant Likely08a543a2011-07-26 03:19:06 -06006#include <linux/irq.h>
Grant Likelycc79ca62012-02-16 01:37:49 -07007#include <linux/irqdesc.h>
Grant Likely08a543a2011-07-26 03:19:06 -06008#include <linux/irqdomain.h>
9#include <linux/module.h>
10#include <linux/mutex.h>
11#include <linux/of.h>
Grant Likely7e713302011-07-26 03:19:06 -060012#include <linux/of_address.h>
Rashika Kheria64be38a2014-02-27 17:10:12 +053013#include <linux/of_irq.h>
Paul Mundt5ca4db62012-06-03 22:04:34 -070014#include <linux/topology.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070015#include <linux/seq_file.h>
Grant Likely7e713302011-07-26 03:19:06 -060016#include <linux/slab.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070017#include <linux/smp.h>
18#include <linux/fs.h>
Grant Likely08a543a2011-07-26 03:19:06 -060019
20static LIST_HEAD(irq_domain_list);
21static DEFINE_MUTEX(irq_domain_mutex);
22
Grant Likelycc79ca62012-02-16 01:37:49 -070023static DEFINE_MUTEX(revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -070024static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070025
Jiang Liuf8264e32014-11-06 22:20:14 +080026static int irq_domain_alloc_descs(int virq, unsigned int nr_irqs,
27 irq_hw_number_t hwirq, int node);
28static void irq_domain_check_hierarchy(struct irq_domain *domain);
29
Grant Likelycc79ca62012-02-16 01:37:49 -070030/**
Grant Likelyfa40f372013-06-08 12:57:40 +010031 * __irq_domain_add() - Allocate a new irq_domain data structure
Grant Likelycc79ca62012-02-16 01:37:49 -070032 * @of_node: optional device-tree node of the interrupt controller
Grant Likelyfa40f372013-06-08 12:57:40 +010033 * @size: Size of linear map; 0 for radix mapping only
Jiang Liua2579542014-05-27 16:07:37 +080034 * @hwirq_max: Maximum number of interrupts supported by controller
Grant Likelyfa40f372013-06-08 12:57:40 +010035 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
36 * direct mapping
Jiang Liuf8264e32014-11-06 22:20:14 +080037 * @ops: domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -070038 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -070039 *
Jiang Liua2579542014-05-27 16:07:37 +080040 * Allocates and initialize and irq_domain structure.
41 * Returns pointer to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -070042 */
Grant Likelyddaf1442013-06-10 01:06:02 +010043struct irq_domain *__irq_domain_add(struct device_node *of_node, int size,
44 irq_hw_number_t hwirq_max, int direct_max,
Grant Likelyfa40f372013-06-08 12:57:40 +010045 const struct irq_domain_ops *ops,
46 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -070047{
Grant Likelya8db8cf2012-02-14 14:06:54 -070048 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070049
Grant Likelycef50752012-07-11 17:24:31 +010050 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
51 GFP_KERNEL, of_node_to_nid(of_node));
Grant Likelya8db8cf2012-02-14 14:06:54 -070052 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -070053 return NULL;
54
55 /* Fill structure */
Grant Likely1aa0dd92013-06-08 12:03:59 +010056 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
Grant Likely68700652012-02-14 14:06:53 -070057 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -070058 domain->host_data = host_data;
Grant Likely68700652012-02-14 14:06:53 -070059 domain->of_node = of_node_get(of_node);
Grant Likelyddaf1442013-06-10 01:06:02 +010060 domain->hwirq_max = hwirq_max;
Grant Likely1aa0dd92013-06-08 12:03:59 +010061 domain->revmap_size = size;
Grant Likelyfa40f372013-06-08 12:57:40 +010062 domain->revmap_direct_max_irq = direct_max;
Jiang Liuf8264e32014-11-06 22:20:14 +080063 irq_domain_check_hierarchy(domain);
Grant Likelycc79ca62012-02-16 01:37:49 -070064
Grant Likelya8db8cf2012-02-14 14:06:54 -070065 mutex_lock(&irq_domain_mutex);
66 list_add(&domain->link, &irq_domain_list);
67 mutex_unlock(&irq_domain_mutex);
Grant Likelyfa40f372013-06-08 12:57:40 +010068
Grant Likely1aa0dd92013-06-08 12:03:59 +010069 pr_debug("Added domain %s\n", domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +010070 return domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -070071}
Grant Likelyfa40f372013-06-08 12:57:40 +010072EXPORT_SYMBOL_GPL(__irq_domain_add);
Grant Likelya8db8cf2012-02-14 14:06:54 -070073
Paul Mundt58ee99a2012-05-19 15:11:41 +090074/**
75 * irq_domain_remove() - Remove an irq domain.
76 * @domain: domain to remove
77 *
78 * This routine is used to remove an irq domain. The caller must ensure
79 * that all mappings within the domain have been disposed of prior to
80 * use, depending on the revmap type.
81 */
82void irq_domain_remove(struct irq_domain *domain)
83{
84 mutex_lock(&irq_domain_mutex);
85
Grant Likelycef50752012-07-11 17:24:31 +010086 /*
87 * radix_tree_delete() takes care of destroying the root
88 * node when all entries are removed. Shout if there are
89 * any mappings left.
90 */
Grant Likely1aa0dd92013-06-08 12:03:59 +010091 WARN_ON(domain->revmap_tree.height);
Paul Mundt58ee99a2012-05-19 15:11:41 +090092
93 list_del(&domain->link);
94
95 /*
96 * If the going away domain is the default one, reset it.
97 */
98 if (unlikely(irq_default_domain == domain))
99 irq_set_default_host(NULL);
100
101 mutex_unlock(&irq_domain_mutex);
102
Grant Likely1aa0dd92013-06-08 12:03:59 +0100103 pr_debug("Removed domain %s\n", domain->name);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900104
Grant Likelyfa40f372013-06-08 12:57:40 +0100105 of_node_put(domain->of_node);
106 kfree(domain);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900107}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900108EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900109
Grant Likelya8db8cf2012-02-14 14:06:54 -0700110/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100111 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
Mark Brown781d0f42012-07-05 12:19:19 +0100112 * @of_node: pointer to interrupt controller's device tree node.
113 * @size: total number of irqs in mapping
Linus Walleij94a63da2013-06-06 12:10:23 +0100114 * @first_irq: first number of irq block assigned to the domain,
Grant Likelyfa40f372013-06-08 12:57:40 +0100115 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
116 * pre-map all of the irqs in the domain to virqs starting at first_irq.
Jiang Liuf8264e32014-11-06 22:20:14 +0800117 * @ops: domain callbacks
Mark Brown781d0f42012-07-05 12:19:19 +0100118 * @host_data: Controller private data pointer
119 *
Grant Likelyfa40f372013-06-08 12:57:40 +0100120 * Allocates an irq_domain, and optionally if first_irq is positive then also
121 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
Mark Brown781d0f42012-07-05 12:19:19 +0100122 *
123 * This is intended to implement the expected behaviour for most
Grant Likelyfa40f372013-06-08 12:57:40 +0100124 * interrupt controllers. If device tree is used, then first_irq will be 0 and
125 * irqs get mapped dynamically on the fly. However, if the controller requires
126 * static virq assignments (non-DT boot) then it will set that up correctly.
Mark Brown781d0f42012-07-05 12:19:19 +0100127 */
128struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
129 unsigned int size,
130 unsigned int first_irq,
131 const struct irq_domain_ops *ops,
132 void *host_data)
133{
Grant Likelyfa40f372013-06-08 12:57:40 +0100134 struct irq_domain *domain;
Linus Walleij2854d162012-09-27 14:59:39 +0200135
Grant Likelyddaf1442013-06-10 01:06:02 +0100136 domain = __irq_domain_add(of_node, size, size, 0, ops, host_data);
Grant Likelyfa40f372013-06-08 12:57:40 +0100137 if (!domain)
138 return NULL;
139
140 if (first_irq > 0) {
Linus Walleij2854d162012-09-27 14:59:39 +0200141 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
Grant Likelyfa40f372013-06-08 12:57:40 +0100142 /* attempt to allocated irq_descs */
143 int rc = irq_alloc_descs(first_irq, first_irq, size,
144 of_node_to_nid(of_node));
145 if (rc < 0)
Linus Walleijd202b7b2012-11-27 01:20:32 +0100146 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
147 first_irq);
Grant Likelyfa40f372013-06-08 12:57:40 +0100148 }
Grant Likelyddaf1442013-06-10 01:06:02 +0100149 irq_domain_associate_many(domain, first_irq, 0, size);
Linus Walleij2854d162012-09-27 14:59:39 +0200150 }
151
Grant Likelyfa40f372013-06-08 12:57:40 +0100152 return domain;
Mark Brown781d0f42012-07-05 12:19:19 +0100153}
Arnd Bergmann346dbb72013-04-25 19:28:54 +0200154EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Mark Brown781d0f42012-07-05 12:19:19 +0100155
156/**
Grant Likelya8db8cf2012-02-14 14:06:54 -0700157 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
158 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700159 * @size: total number of irqs in legacy mapping
160 * @first_irq: first number of irq block assigned to the domain
161 * @first_hwirq: first hwirq number to use for the translation. Should normally
162 * be '0', but a positive integer can be used if the effective
163 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700164 * @ops: map/unmap domain callbacks
165 * @host_data: Controller private data pointer
166 *
167 * Note: the map() callback will be called before this function returns
168 * for all legacy interrupts except 0 (which is always the invalid irq for
169 * a legacy controller).
170 */
171struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700172 unsigned int size,
173 unsigned int first_irq,
174 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700175 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700176 void *host_data)
177{
Grant Likely1bc04f22012-02-14 14:06:55 -0700178 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700179
Grant Likelyddaf1442013-06-10 01:06:02 +0100180 domain = __irq_domain_add(of_node, first_hwirq + size,
181 first_hwirq + size, 0, ops, host_data);
Jiang Liuf8264e32014-11-06 22:20:14 +0800182 if (domain)
183 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
Grant Likely1bc04f22012-02-14 14:06:55 -0700184
Grant Likelya8db8cf2012-02-14 14:06:54 -0700185 return domain;
186}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900187EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700188
Grant Likelya8db8cf2012-02-14 14:06:54 -0700189/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700190 * irq_find_host() - Locates a domain for a given device node
191 * @node: device-tree node of the interrupt controller
192 */
193struct irq_domain *irq_find_host(struct device_node *node)
194{
195 struct irq_domain *h, *found = NULL;
Grant Likelya18dc812012-01-26 12:12:14 -0700196 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700197
198 /* We might want to match the legacy controller last since
199 * it might potentially be set to match all interrupts in
200 * the absence of a device node. This isn't a problem so far
201 * yet though...
202 */
203 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700204 list_for_each_entry(h, &irq_domain_list, link) {
205 if (h->ops->match)
206 rc = h->ops->match(h, node);
207 else
208 rc = (h->of_node != NULL) && (h->of_node == node);
209
210 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700211 found = h;
212 break;
213 }
Grant Likelya18dc812012-01-26 12:12:14 -0700214 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700215 mutex_unlock(&irq_domain_mutex);
216 return found;
217}
218EXPORT_SYMBOL_GPL(irq_find_host);
219
220/**
221 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700222 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700223 *
224 * For convenience, it's possible to set a "default" domain that will be used
225 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
226 * platforms that want to manipulate a few hard coded interrupt numbers that
227 * aren't properly represented in the device-tree.
228 */
Grant Likely68700652012-02-14 14:06:53 -0700229void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700230{
Paul Mundt54a90582012-05-19 15:11:47 +0900231 pr_debug("Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700232
Grant Likely68700652012-02-14 14:06:53 -0700233 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700234}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900235EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700236
Jiang Liu43a77592014-06-09 16:20:05 +0800237void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
Grant Likely913af202012-06-03 22:04:35 -0700238{
Grant Likelyddaf1442013-06-10 01:06:02 +0100239 struct irq_data *irq_data = irq_get_irq_data(irq);
240 irq_hw_number_t hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700241
Grant Likelyddaf1442013-06-10 01:06:02 +0100242 if (WARN(!irq_data || irq_data->domain != domain,
243 "virq%i doesn't exist; cannot disassociate\n", irq))
244 return;
Grant Likely913af202012-06-03 22:04:35 -0700245
Grant Likelyddaf1442013-06-10 01:06:02 +0100246 hwirq = irq_data->hwirq;
247 irq_set_status_flags(irq, IRQ_NOREQUEST);
Grant Likely913af202012-06-03 22:04:35 -0700248
Grant Likelyddaf1442013-06-10 01:06:02 +0100249 /* remove chip and handler */
250 irq_set_chip_and_handler(irq, NULL, NULL);
Grant Likely913af202012-06-03 22:04:35 -0700251
Grant Likelyddaf1442013-06-10 01:06:02 +0100252 /* Make sure it's completed */
253 synchronize_irq(irq);
Grant Likely913af202012-06-03 22:04:35 -0700254
Grant Likelyddaf1442013-06-10 01:06:02 +0100255 /* Tell the PIC about it */
256 if (domain->ops->unmap)
257 domain->ops->unmap(domain, irq);
258 smp_mb();
Grant Likely913af202012-06-03 22:04:35 -0700259
Grant Likelyddaf1442013-06-10 01:06:02 +0100260 irq_data->domain = NULL;
261 irq_data->hwirq = 0;
Grant Likely913af202012-06-03 22:04:35 -0700262
Grant Likelyddaf1442013-06-10 01:06:02 +0100263 /* Clear reverse map for this hwirq */
264 if (hwirq < domain->revmap_size) {
265 domain->linear_revmap[hwirq] = 0;
266 } else {
267 mutex_lock(&revmap_trees_mutex);
268 radix_tree_delete(&domain->revmap_tree, hwirq);
269 mutex_unlock(&revmap_trees_mutex);
Grant Likely913af202012-06-03 22:04:35 -0700270 }
271}
272
Grant Likelyddaf1442013-06-10 01:06:02 +0100273int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
274 irq_hw_number_t hwirq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700275{
Grant Likelyddaf1442013-06-10 01:06:02 +0100276 struct irq_data *irq_data = irq_get_irq_data(virq);
277 int ret;
278
279 if (WARN(hwirq >= domain->hwirq_max,
280 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
281 return -EINVAL;
282 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
283 return -EINVAL;
284 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
285 return -EINVAL;
286
287 mutex_lock(&irq_domain_mutex);
288 irq_data->hwirq = hwirq;
289 irq_data->domain = domain;
290 if (domain->ops->map) {
291 ret = domain->ops->map(domain, virq, hwirq);
292 if (ret != 0) {
293 /*
294 * If map() returns -EPERM, this interrupt is protected
295 * by the firmware or some other service and shall not
296 * be mapped. Don't bother telling the user about it.
297 */
298 if (ret != -EPERM) {
299 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
300 domain->name, hwirq, virq, ret);
301 }
302 irq_data->domain = NULL;
303 irq_data->hwirq = 0;
304 mutex_unlock(&irq_domain_mutex);
305 return ret;
306 }
307
308 /* If not already assigned, give the domain the chip's name */
309 if (!domain->name && irq_data->chip)
310 domain->name = irq_data->chip->name;
311 }
312
313 if (hwirq < domain->revmap_size) {
314 domain->linear_revmap[hwirq] = virq;
315 } else {
316 mutex_lock(&revmap_trees_mutex);
317 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
318 mutex_unlock(&revmap_trees_mutex);
319 }
320 mutex_unlock(&irq_domain_mutex);
321
322 irq_clear_status_flags(virq, IRQ_NOREQUEST);
323
324 return 0;
325}
326EXPORT_SYMBOL_GPL(irq_domain_associate);
327
328void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
329 irq_hw_number_t hwirq_base, int count)
330{
331 int i;
Grant Likelycc79ca62012-02-16 01:37:49 -0700332
Grant Likely98aa4682012-06-17 16:17:04 -0600333 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
334 of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count);
335
336 for (i = 0; i < count; i++) {
Grant Likelyddaf1442013-06-10 01:06:02 +0100337 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
Grant Likelycc79ca62012-02-16 01:37:49 -0700338 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700339}
Grant Likely98aa4682012-06-17 16:17:04 -0600340EXPORT_SYMBOL_GPL(irq_domain_associate_many);
Grant Likelycc79ca62012-02-16 01:37:49 -0700341
342/**
343 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700344 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700345 *
346 * This routine is used for irq controllers which can choose the hardware
347 * interrupt numbers they generate. In such a case it's simplest to use
Grant Likely1aa0dd92013-06-08 12:03:59 +0100348 * the linux irq as the hardware interrupt number. It still uses the linear
349 * or radix tree to store the mapping, but the irq controller can optimize
350 * the revmap path by using the hwirq directly.
Grant Likelycc79ca62012-02-16 01:37:49 -0700351 */
Grant Likely68700652012-02-14 14:06:53 -0700352unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700353{
354 unsigned int virq;
355
Grant Likely68700652012-02-14 14:06:53 -0700356 if (domain == NULL)
357 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700358
Paul Mundt5ca4db62012-06-03 22:04:34 -0700359 virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
Grant Likely03848372012-02-14 14:06:52 -0700360 if (!virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900361 pr_debug("create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700362 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700363 }
Grant Likely1aa0dd92013-06-08 12:03:59 +0100364 if (virq >= domain->revmap_direct_max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700365 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely1aa0dd92013-06-08 12:03:59 +0100366 domain->revmap_direct_max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700367 irq_free_desc(virq);
368 return 0;
369 }
Paul Mundt54a90582012-05-19 15:11:47 +0900370 pr_debug("create_direct obtained virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700371
Grant Likely98aa4682012-06-17 16:17:04 -0600372 if (irq_domain_associate(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700373 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700374 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700375 }
376
377 return virq;
378}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900379EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700380
381/**
382 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700383 * @domain: domain owning this hardware interrupt or NULL for default domain
384 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700385 *
386 * Only one mapping per hardware interrupt is permitted. Returns a linux
387 * irq number.
388 * If the sense/trigger is to be specified, set_irq_type() should be called
389 * on the number returned from that call.
390 */
Grant Likely68700652012-02-14 14:06:53 -0700391unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700392 irq_hw_number_t hwirq)
393{
David Daney5b7526e2012-04-05 16:52:13 -0700394 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700395
Paul Mundt54a90582012-05-19 15:11:47 +0900396 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700397
Grant Likely68700652012-02-14 14:06:53 -0700398 /* Look for default domain if nececssary */
399 if (domain == NULL)
400 domain = irq_default_domain;
401 if (domain == NULL) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800402 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700403 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700404 }
Paul Mundt54a90582012-05-19 15:11:47 +0900405 pr_debug("-> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700406
407 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700408 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700409 if (virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900410 pr_debug("-> existing mapping on virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700411 return virq;
412 }
413
Grant Likely1bc04f22012-02-14 14:06:55 -0700414 /* Allocate a virtual interrupt number */
Jiang Liuf8264e32014-11-06 22:20:14 +0800415 virq = irq_domain_alloc_descs(-1, 1, hwirq,
416 of_node_to_nid(domain->of_node));
David Daney5b7526e2012-04-05 16:52:13 -0700417 if (virq <= 0) {
Paul Mundt54a90582012-05-19 15:11:47 +0900418 pr_debug("-> virq allocation failed\n");
Grant Likely1bc04f22012-02-14 14:06:55 -0700419 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700420 }
421
Grant Likely98aa4682012-06-17 16:17:04 -0600422 if (irq_domain_associate(domain, virq, hwirq)) {
Grant Likely73255702012-06-03 22:04:35 -0700423 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700424 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700425 }
426
Paul Mundt54a90582012-05-19 15:11:47 +0900427 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
Grant Likelyefd68e72012-06-03 22:04:33 -0700428 hwirq, of_node_full_name(domain->of_node), virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700429
430 return virq;
431}
432EXPORT_SYMBOL_GPL(irq_create_mapping);
433
Grant Likely98aa4682012-06-17 16:17:04 -0600434/**
435 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
436 * @domain: domain owning the interrupt range
437 * @irq_base: beginning of linux IRQ range
438 * @hwirq_base: beginning of hardware IRQ range
439 * @count: Number of interrupts to map
440 *
441 * This routine is used for allocating and mapping a range of hardware
442 * irqs to linux irqs where the linux irq numbers are at pre-defined
443 * locations. For use by controllers that already have static mappings
444 * to insert in to the domain.
445 *
446 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
447 * domain insertion.
448 *
449 * 0 is returned upon success, while any failure to establish a static
450 * mapping is treated as an error.
451 */
452int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
453 irq_hw_number_t hwirq_base, int count)
454{
455 int ret;
456
457 ret = irq_alloc_descs(irq_base, irq_base, count,
458 of_node_to_nid(domain->of_node));
459 if (unlikely(ret < 0))
460 return ret;
461
Grant Likelyddaf1442013-06-10 01:06:02 +0100462 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600463 return 0;
464}
465EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
466
Grant Likelye6d30ab2013-09-15 16:55:53 +0100467unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700468{
Grant Likely68700652012-02-14 14:06:53 -0700469 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700470 irq_hw_number_t hwirq;
471 unsigned int type = IRQ_TYPE_NONE;
Jiang Liuf8264e32014-11-06 22:20:14 +0800472 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700473
Grant Likelye6d30ab2013-09-15 16:55:53 +0100474 domain = irq_data->np ? irq_find_host(irq_data->np) : irq_default_domain;
Grant Likely68700652012-02-14 14:06:53 -0700475 if (!domain) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800476 pr_warn("no irq domain found for %s !\n",
Grant Likelye6d30ab2013-09-15 16:55:53 +0100477 of_node_full_name(irq_data->np));
Grant Likely03848372012-02-14 14:06:52 -0700478 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700479 }
480
Grant Likely68700652012-02-14 14:06:53 -0700481 /* If domain has no translation, then we assume interrupt line */
482 if (domain->ops->xlate == NULL)
Grant Likelye6d30ab2013-09-15 16:55:53 +0100483 hwirq = irq_data->args[0];
Grant Likelycc79ca62012-02-16 01:37:49 -0700484 else {
Grant Likelye6d30ab2013-09-15 16:55:53 +0100485 if (domain->ops->xlate(domain, irq_data->np, irq_data->args,
486 irq_data->args_count, &hwirq, &type))
Grant Likely03848372012-02-14 14:06:52 -0700487 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700488 }
489
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800490 if (irq_domain_is_hierarchy(domain)) {
491 /*
492 * If we've already configured this interrupt,
493 * don't do it again, or hell will break loose.
494 */
495 virq = irq_find_mapping(domain, hwirq);
496 if (virq)
497 return virq;
498
499 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, irq_data);
500 if (virq <= 0)
501 return 0;
502 } else {
503 /* Create mapping */
504 virq = irq_create_mapping(domain, hwirq);
505 if (!virq)
506 return virq;
507 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700508
509 /* Set type if specified and different than the current one */
510 if (type != IRQ_TYPE_NONE &&
Javier Martinez Canillasfbab62c52013-06-14 18:40:49 +0200511 type != irq_get_trigger_type(virq))
Grant Likelycc79ca62012-02-16 01:37:49 -0700512 irq_set_irq_type(virq, type);
513 return virq;
514}
515EXPORT_SYMBOL_GPL(irq_create_of_mapping);
516
517/**
518 * irq_dispose_mapping() - Unmap an interrupt
519 * @virq: linux irq number of the interrupt to unmap
520 */
521void irq_dispose_mapping(unsigned int virq)
522{
523 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700524 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700525
Grant Likely03848372012-02-14 14:06:52 -0700526 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700527 return;
528
Grant Likely68700652012-02-14 14:06:53 -0700529 domain = irq_data->domain;
530 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700531 return;
532
Grant Likelyddaf1442013-06-10 01:06:02 +0100533 irq_domain_disassociate(domain, virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700534 irq_free_desc(virq);
535}
536EXPORT_SYMBOL_GPL(irq_dispose_mapping);
537
538/**
539 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700540 * @domain: domain owning this hardware interrupt
541 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700542 */
Grant Likely68700652012-02-14 14:06:53 -0700543unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700544 irq_hw_number_t hwirq)
545{
Grant Likely4c0946c2012-06-03 22:04:39 -0700546 struct irq_data *data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700547
Grant Likely68700652012-02-14 14:06:53 -0700548 /* Look for default domain if nececssary */
549 if (domain == NULL)
550 domain = irq_default_domain;
551 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700552 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700553
Grant Likely1aa0dd92013-06-08 12:03:59 +0100554 if (hwirq < domain->revmap_direct_max_irq) {
Jiang Liuf8264e32014-11-06 22:20:14 +0800555 data = irq_domain_get_irq_data(domain, hwirq);
556 if (data && data->hwirq == hwirq)
Grant Likely4c0946c2012-06-03 22:04:39 -0700557 return hwirq;
Grant Likely4c0946c2012-06-03 22:04:39 -0700558 }
559
Grant Likelyd3dcb432013-06-10 12:19:17 +0100560 /* Check if the hwirq is in the linear revmap. */
561 if (hwirq < domain->revmap_size)
562 return domain->linear_revmap[hwirq];
563
564 rcu_read_lock();
565 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
566 rcu_read_unlock();
567 return data ? data->irq : 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700568}
569EXPORT_SYMBOL_GPL(irq_find_mapping);
570
Grant Likely092b2fb2012-03-29 14:10:30 -0600571#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Grant Likelycc79ca62012-02-16 01:37:49 -0700572static int virq_debug_show(struct seq_file *m, void *private)
573{
574 unsigned long flags;
575 struct irq_desc *desc;
Grant Likely1400ea82013-06-06 22:20:44 +0100576 struct irq_domain *domain;
577 struct radix_tree_iter iter;
578 void *data, **slot;
Grant Likelycc79ca62012-02-16 01:37:49 -0700579 int i;
580
Grant Likely1400ea82013-06-06 22:20:44 +0100581 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
582 "name", "mapped", "linear-max", "direct-max", "devtree-node");
583 mutex_lock(&irq_domain_mutex);
584 list_for_each_entry(domain, &irq_domain_list, link) {
585 int count = 0;
586 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
587 count++;
588 seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
589 domain == irq_default_domain ? '*' : ' ', domain->name,
590 domain->revmap_size + count, domain->revmap_size,
591 domain->revmap_direct_max_irq,
592 domain->of_node ? of_node_full_name(domain->of_node) : "");
593 }
594 mutex_unlock(&irq_domain_mutex);
595
596 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600597 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
Grant Likely1400ea82013-06-06 22:20:44 +0100598 "active", "type", "domain");
Grant Likelycc79ca62012-02-16 01:37:49 -0700599
600 for (i = 1; i < nr_irqs; i++) {
601 desc = irq_to_desc(i);
602 if (!desc)
603 continue;
604
605 raw_spin_lock_irqsave(&desc->lock, flags);
Grant Likely1400ea82013-06-06 22:20:44 +0100606 domain = desc->irq_data.domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700607
Grant Likely1400ea82013-06-06 22:20:44 +0100608 if (domain) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700609 struct irq_chip *chip;
Grant Likely1400ea82013-06-06 22:20:44 +0100610 int hwirq = desc->irq_data.hwirq;
611 bool direct;
Grant Likelycc79ca62012-02-16 01:37:49 -0700612
613 seq_printf(m, "%5d ", i);
Grant Likely1400ea82013-06-06 22:20:44 +0100614 seq_printf(m, "0x%05x ", hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700615
616 chip = irq_desc_get_chip(desc);
Grant Likely0bb4afb2013-06-06 14:23:30 +0100617 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
Grant Likelycc79ca62012-02-16 01:37:49 -0700618
619 data = irq_desc_get_chip_data(desc);
Grant Likely15e06bf2012-04-11 00:26:25 -0600620 seq_printf(m, data ? "0x%p " : " %p ", data);
Grant Likelycc79ca62012-02-16 01:37:49 -0700621
Grant Likely1400ea82013-06-06 22:20:44 +0100622 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
623 direct = (i == hwirq) && (i < domain->revmap_direct_max_irq);
624 seq_printf(m, "%6s%-8s ",
625 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
626 direct ? "(DIRECT)" : "");
Grant Likely0bb4afb2013-06-06 14:23:30 +0100627 seq_printf(m, "%s\n", desc->irq_data.domain->name);
Grant Likelycc79ca62012-02-16 01:37:49 -0700628 }
629
630 raw_spin_unlock_irqrestore(&desc->lock, flags);
631 }
632
633 return 0;
634}
635
636static int virq_debug_open(struct inode *inode, struct file *file)
637{
638 return single_open(file, virq_debug_show, inode->i_private);
639}
640
641static const struct file_operations virq_debug_fops = {
642 .open = virq_debug_open,
643 .read = seq_read,
644 .llseek = seq_lseek,
645 .release = single_release,
646};
647
648static int __init irq_debugfs_init(void)
649{
Grant Likely092b2fb2012-03-29 14:10:30 -0600650 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -0700651 NULL, &virq_debug_fops) == NULL)
652 return -ENOMEM;
653
654 return 0;
655}
656__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -0600657#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -0700658
Grant Likely16b2e6e2012-01-26 11:26:52 -0700659/**
660 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
661 *
662 * Device Tree IRQ specifier translation function which works with one cell
663 * bindings where the cell value maps directly to the hwirq number.
664 */
665int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
666 const u32 *intspec, unsigned int intsize,
667 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600668{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700669 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600670 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600671 *out_hwirq = intspec[0];
672 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600673 return 0;
674}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700675EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
676
677/**
678 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
679 *
680 * Device Tree IRQ specifier translation function which works with two cell
681 * bindings where the cell values map directly to the hwirq number
682 * and linux irq flags.
683 */
684int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
685 const u32 *intspec, unsigned int intsize,
686 irq_hw_number_t *out_hwirq, unsigned int *out_type)
687{
688 if (WARN_ON(intsize < 2))
689 return -EINVAL;
690 *out_hwirq = intspec[0];
691 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
692 return 0;
693}
694EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
695
696/**
697 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
698 *
699 * Device Tree IRQ specifier translation function which works with either one
700 * or two cell bindings where the cell values map directly to the hwirq number
701 * and linux irq flags.
702 *
703 * Note: don't use this function unless your interrupt controller explicitly
704 * supports both one and two cell bindings. For the majority of controllers
705 * the _onecell() or _twocell() variants above should be used.
706 */
707int irq_domain_xlate_onetwocell(struct irq_domain *d,
708 struct device_node *ctrlr,
709 const u32 *intspec, unsigned int intsize,
710 unsigned long *out_hwirq, unsigned int *out_type)
711{
712 if (WARN_ON(intsize < 1))
713 return -EINVAL;
714 *out_hwirq = intspec[0];
715 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
716 return 0;
717}
718EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600719
Grant Likelya18dc812012-01-26 12:12:14 -0700720const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely16b2e6e2012-01-26 11:26:52 -0700721 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700722};
723EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Jiang Liuf8264e32014-11-06 22:20:14 +0800724
725static int irq_domain_alloc_descs(int virq, unsigned int cnt,
726 irq_hw_number_t hwirq, int node)
727{
728 unsigned int hint;
729
730 if (virq >= 0) {
731 virq = irq_alloc_descs(virq, virq, cnt, node);
732 } else {
733 hint = hwirq % nr_irqs;
734 if (hint == 0)
735 hint++;
736 virq = irq_alloc_descs_from(hint, cnt, node);
737 if (virq <= 0 && hint > 1)
738 virq = irq_alloc_descs_from(1, cnt, node);
739 }
740
741 return virq;
742}
743
744#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
Jiang Liuafb7da82014-11-15 22:24:02 +0800745/**
746 * irq_domain_add_hierarchy - Add a irqdomain into the hierarchy
747 * @parent: Parent irq domain to associate with the new domain
748 * @flags: Irq domain flags associated to the domain
749 * @size: Size of the domain. See below
750 * @node: Optional device-tree node of the interrupt controller
751 * @ops: Pointer to the interrupt domain callbacks
752 * @host_data: Controller private data pointer
753 *
754 * If @size is 0 a tree domain is created, otherwise a linear domain.
755 *
756 * If successful the parent is associated to the new domain and the
757 * domain flags are set.
758 * Returns pointer to IRQ domain, or NULL on failure.
759 */
760struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
761 unsigned int flags,
762 unsigned int size,
763 struct device_node *node,
764 const struct irq_domain_ops *ops,
765 void *host_data)
766{
767 struct irq_domain *domain;
768
769 if (size)
770 domain = irq_domain_add_linear(node, size, ops, host_data);
771 else
772 domain = irq_domain_add_tree(node, ops, host_data);
773 if (domain) {
774 domain->parent = parent;
775 domain->flags |= flags;
776 }
777
778 return domain;
779}
780
Jiang Liuf8264e32014-11-06 22:20:14 +0800781static void irq_domain_insert_irq(int virq)
782{
783 struct irq_data *data;
784
785 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
786 struct irq_domain *domain = data->domain;
787 irq_hw_number_t hwirq = data->hwirq;
788
789 if (hwirq < domain->revmap_size) {
790 domain->linear_revmap[hwirq] = virq;
791 } else {
792 mutex_lock(&revmap_trees_mutex);
793 radix_tree_insert(&domain->revmap_tree, hwirq, data);
794 mutex_unlock(&revmap_trees_mutex);
795 }
796
797 /* If not already assigned, give the domain the chip's name */
798 if (!domain->name && data->chip)
799 domain->name = data->chip->name;
800 }
801
802 irq_clear_status_flags(virq, IRQ_NOREQUEST);
803}
804
805static void irq_domain_remove_irq(int virq)
806{
807 struct irq_data *data;
808
809 irq_set_status_flags(virq, IRQ_NOREQUEST);
810 irq_set_chip_and_handler(virq, NULL, NULL);
811 synchronize_irq(virq);
812 smp_mb();
813
814 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
815 struct irq_domain *domain = data->domain;
816 irq_hw_number_t hwirq = data->hwirq;
817
818 if (hwirq < domain->revmap_size) {
819 domain->linear_revmap[hwirq] = 0;
820 } else {
821 mutex_lock(&revmap_trees_mutex);
822 radix_tree_delete(&domain->revmap_tree, hwirq);
823 mutex_unlock(&revmap_trees_mutex);
824 }
825 }
826}
827
828static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
829 struct irq_data *child)
830{
831 struct irq_data *irq_data;
832
Jiang Liu67830112015-06-01 16:05:13 +0800833 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
834 irq_data_get_node(child));
Jiang Liuf8264e32014-11-06 22:20:14 +0800835 if (irq_data) {
836 child->parent_data = irq_data;
837 irq_data->irq = child->irq;
Jiang Liu0d0b4c82015-06-01 16:05:12 +0800838 irq_data->common = child->common;
Jiang Liuf8264e32014-11-06 22:20:14 +0800839 irq_data->node = child->node;
840 irq_data->domain = domain;
841 }
842
843 return irq_data;
844}
845
846static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
847{
848 struct irq_data *irq_data, *tmp;
849 int i;
850
851 for (i = 0; i < nr_irqs; i++) {
852 irq_data = irq_get_irq_data(virq + i);
853 tmp = irq_data->parent_data;
854 irq_data->parent_data = NULL;
855 irq_data->domain = NULL;
856
857 while (tmp) {
858 irq_data = tmp;
859 tmp = tmp->parent_data;
860 kfree(irq_data);
861 }
862 }
863}
864
865static int irq_domain_alloc_irq_data(struct irq_domain *domain,
866 unsigned int virq, unsigned int nr_irqs)
867{
868 struct irq_data *irq_data;
869 struct irq_domain *parent;
870 int i;
871
872 /* The outermost irq_data is embedded in struct irq_desc */
873 for (i = 0; i < nr_irqs; i++) {
874 irq_data = irq_get_irq_data(virq + i);
875 irq_data->domain = domain;
876
877 for (parent = domain->parent; parent; parent = parent->parent) {
878 irq_data = irq_domain_insert_irq_data(parent, irq_data);
879 if (!irq_data) {
880 irq_domain_free_irq_data(virq, i + 1);
881 return -ENOMEM;
882 }
883 }
884 }
885
886 return 0;
887}
888
889/**
890 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
891 * @domain: domain to match
892 * @virq: IRQ number to get irq_data
893 */
894struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
895 unsigned int virq)
896{
897 struct irq_data *irq_data;
898
899 for (irq_data = irq_get_irq_data(virq); irq_data;
900 irq_data = irq_data->parent_data)
901 if (irq_data->domain == domain)
902 return irq_data;
903
904 return NULL;
905}
906
907/**
908 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
909 * @domain: Interrupt domain to match
910 * @virq: IRQ number
911 * @hwirq: The hwirq number
912 * @chip: The associated interrupt chip
913 * @chip_data: The associated chip data
914 */
915int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
916 irq_hw_number_t hwirq, struct irq_chip *chip,
917 void *chip_data)
918{
919 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
920
921 if (!irq_data)
922 return -ENOENT;
923
924 irq_data->hwirq = hwirq;
925 irq_data->chip = chip ? chip : &no_irq_chip;
926 irq_data->chip_data = chip_data;
927
928 return 0;
929}
930
931/**
Jiang Liu1b537702014-11-09 23:10:24 +0800932 * irq_domain_set_info - Set the complete data for a @virq in @domain
933 * @domain: Interrupt domain to match
934 * @virq: IRQ number
935 * @hwirq: The hardware interrupt number
936 * @chip: The associated interrupt chip
937 * @chip_data: The associated interrupt chip data
938 * @handler: The interrupt flow handler
939 * @handler_data: The interrupt flow handler data
940 * @handler_name: The interrupt handler name
941 */
942void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
943 irq_hw_number_t hwirq, struct irq_chip *chip,
944 void *chip_data, irq_flow_handler_t handler,
945 void *handler_data, const char *handler_name)
946{
947 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
948 __irq_set_handler(virq, handler, 0, handler_name);
949 irq_set_handler_data(virq, handler_data);
950}
951
952/**
Jiang Liuf8264e32014-11-06 22:20:14 +0800953 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
954 * @irq_data: The pointer to irq_data
955 */
956void irq_domain_reset_irq_data(struct irq_data *irq_data)
957{
958 irq_data->hwirq = 0;
959 irq_data->chip = &no_irq_chip;
960 irq_data->chip_data = NULL;
961}
962
963/**
964 * irq_domain_free_irqs_common - Clear irq_data and free the parent
965 * @domain: Interrupt domain to match
966 * @virq: IRQ number to start with
967 * @nr_irqs: The number of irqs to free
968 */
969void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
970 unsigned int nr_irqs)
971{
972 struct irq_data *irq_data;
973 int i;
974
975 for (i = 0; i < nr_irqs; i++) {
976 irq_data = irq_domain_get_irq_data(domain, virq + i);
977 if (irq_data)
978 irq_domain_reset_irq_data(irq_data);
979 }
980 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
981}
982
983/**
984 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
985 * @domain: Interrupt domain to match
986 * @virq: IRQ number to start with
987 * @nr_irqs: The number of irqs to free
988 */
989void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
990 unsigned int nr_irqs)
991{
992 int i;
993
994 for (i = 0; i < nr_irqs; i++) {
995 irq_set_handler_data(virq + i, NULL);
996 irq_set_handler(virq + i, NULL);
997 }
998 irq_domain_free_irqs_common(domain, virq, nr_irqs);
999}
1000
Jiang Liu36d72732014-11-15 22:24:01 +08001001static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
1002{
1003 return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
1004}
1005
1006static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
1007 unsigned int irq_base,
1008 unsigned int nr_irqs)
1009{
1010 domain->ops->free(domain, irq_base, nr_irqs);
1011 if (irq_domain_is_auto_recursive(domain)) {
1012 BUG_ON(!domain->parent);
1013 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1014 nr_irqs);
1015 }
1016}
1017
1018static int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
1019 unsigned int irq_base,
1020 unsigned int nr_irqs, void *arg)
1021{
1022 int ret = 0;
1023 struct irq_domain *parent = domain->parent;
1024 bool recursive = irq_domain_is_auto_recursive(domain);
1025
1026 BUG_ON(recursive && !parent);
1027 if (recursive)
1028 ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
1029 nr_irqs, arg);
1030 if (ret >= 0)
1031 ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1032 if (ret < 0 && recursive)
1033 irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
1034
1035 return ret;
1036}
1037
Jiang Liuf8264e32014-11-06 22:20:14 +08001038/**
1039 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1040 * @domain: domain to allocate from
1041 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
1042 * @nr_irqs: number of IRQs to allocate
1043 * @node: NUMA node id for memory allocation
1044 * @arg: domain specific argument
1045 * @realloc: IRQ descriptors have already been allocated if true
1046 *
1047 * Allocate IRQ numbers and initialized all data structures to support
1048 * hierarchy IRQ domains.
1049 * Parameter @realloc is mainly to support legacy IRQs.
1050 * Returns error code or allocated IRQ number
1051 *
1052 * The whole process to setup an IRQ has been split into two steps.
1053 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1054 * descriptor and required hardware resources. The second step,
1055 * irq_domain_activate_irq(), is to program hardwares with preallocated
1056 * resources. In this way, it's easier to rollback when failing to
1057 * allocate resources.
1058 */
1059int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1060 unsigned int nr_irqs, int node, void *arg,
1061 bool realloc)
1062{
1063 int i, ret, virq;
1064
1065 if (domain == NULL) {
1066 domain = irq_default_domain;
1067 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1068 return -EINVAL;
1069 }
1070
1071 if (!domain->ops->alloc) {
1072 pr_debug("domain->ops->alloc() is NULL\n");
1073 return -ENOSYS;
1074 }
1075
1076 if (realloc && irq_base >= 0) {
1077 virq = irq_base;
1078 } else {
1079 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node);
1080 if (virq < 0) {
1081 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1082 irq_base, nr_irqs);
1083 return virq;
1084 }
1085 }
1086
1087 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1088 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1089 ret = -ENOMEM;
1090 goto out_free_desc;
1091 }
1092
1093 mutex_lock(&irq_domain_mutex);
Jiang Liu36d72732014-11-15 22:24:01 +08001094 ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
Jiang Liuf8264e32014-11-06 22:20:14 +08001095 if (ret < 0) {
1096 mutex_unlock(&irq_domain_mutex);
1097 goto out_free_irq_data;
1098 }
1099 for (i = 0; i < nr_irqs; i++)
1100 irq_domain_insert_irq(virq + i);
1101 mutex_unlock(&irq_domain_mutex);
1102
1103 return virq;
1104
1105out_free_irq_data:
1106 irq_domain_free_irq_data(virq, nr_irqs);
1107out_free_desc:
1108 irq_free_descs(virq, nr_irqs);
1109 return ret;
1110}
1111
1112/**
1113 * irq_domain_free_irqs - Free IRQ number and associated data structures
1114 * @virq: base IRQ number
1115 * @nr_irqs: number of IRQs to free
1116 */
1117void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1118{
1119 struct irq_data *data = irq_get_irq_data(virq);
1120 int i;
1121
1122 if (WARN(!data || !data->domain || !data->domain->ops->free,
1123 "NULL pointer, cannot free irq\n"))
1124 return;
1125
1126 mutex_lock(&irq_domain_mutex);
1127 for (i = 0; i < nr_irqs; i++)
1128 irq_domain_remove_irq(virq + i);
Jiang Liu36d72732014-11-15 22:24:01 +08001129 irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
Jiang Liuf8264e32014-11-06 22:20:14 +08001130 mutex_unlock(&irq_domain_mutex);
1131
1132 irq_domain_free_irq_data(virq, nr_irqs);
1133 irq_free_descs(virq, nr_irqs);
1134}
1135
1136/**
Jiang Liu36d72732014-11-15 22:24:01 +08001137 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1138 * @irq_base: Base IRQ number
1139 * @nr_irqs: Number of IRQs to allocate
1140 * @arg: Allocation data (arch/domain specific)
1141 *
1142 * Check whether the domain has been setup recursive. If not allocate
1143 * through the parent domain.
1144 */
1145int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1146 unsigned int irq_base, unsigned int nr_irqs,
1147 void *arg)
1148{
1149 /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
1150 if (irq_domain_is_auto_recursive(domain))
1151 return 0;
1152
1153 domain = domain->parent;
1154 if (domain)
1155 return irq_domain_alloc_irqs_recursive(domain, irq_base,
1156 nr_irqs, arg);
1157 return -ENOSYS;
1158}
1159
1160/**
1161 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1162 * @irq_base: Base IRQ number
1163 * @nr_irqs: Number of IRQs to free
1164 *
1165 * Check whether the domain has been setup recursive. If not free
1166 * through the parent domain.
1167 */
1168void irq_domain_free_irqs_parent(struct irq_domain *domain,
1169 unsigned int irq_base, unsigned int nr_irqs)
1170{
1171 /* irq_domain_free_irqs_recursive() will call parent's free */
1172 if (!irq_domain_is_auto_recursive(domain) && domain->parent)
1173 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1174 nr_irqs);
1175}
1176
1177/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001178 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1179 * interrupt
1180 * @irq_data: outermost irq_data associated with interrupt
1181 *
1182 * This is the second step to call domain_ops->activate to program interrupt
1183 * controllers, so the interrupt could actually get delivered.
1184 */
1185void irq_domain_activate_irq(struct irq_data *irq_data)
1186{
1187 if (irq_data && irq_data->domain) {
1188 struct irq_domain *domain = irq_data->domain;
1189
1190 if (irq_data->parent_data)
1191 irq_domain_activate_irq(irq_data->parent_data);
1192 if (domain->ops->activate)
1193 domain->ops->activate(domain, irq_data);
1194 }
1195}
1196
1197/**
1198 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1199 * deactivate interrupt
1200 * @irq_data: outermost irq_data associated with interrupt
1201 *
1202 * It calls domain_ops->deactivate to program interrupt controllers to disable
1203 * interrupt delivery.
1204 */
1205void irq_domain_deactivate_irq(struct irq_data *irq_data)
1206{
1207 if (irq_data && irq_data->domain) {
1208 struct irq_domain *domain = irq_data->domain;
1209
1210 if (domain->ops->deactivate)
1211 domain->ops->deactivate(domain, irq_data);
1212 if (irq_data->parent_data)
1213 irq_domain_deactivate_irq(irq_data->parent_data);
1214 }
1215}
1216
1217static void irq_domain_check_hierarchy(struct irq_domain *domain)
1218{
1219 /* Hierarchy irq_domains must implement callback alloc() */
1220 if (domain->ops->alloc)
1221 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1222}
1223#else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1224/**
1225 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1226 * @domain: domain to match
1227 * @virq: IRQ number to get irq_data
1228 */
1229struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1230 unsigned int virq)
1231{
1232 struct irq_data *irq_data = irq_get_irq_data(virq);
1233
1234 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1235}
1236
Stefan Agner5f22f5c2015-05-16 11:44:13 +02001237/**
1238 * irq_domain_set_info - Set the complete data for a @virq in @domain
1239 * @domain: Interrupt domain to match
1240 * @virq: IRQ number
1241 * @hwirq: The hardware interrupt number
1242 * @chip: The associated interrupt chip
1243 * @chip_data: The associated interrupt chip data
1244 * @handler: The interrupt flow handler
1245 * @handler_data: The interrupt flow handler data
1246 * @handler_name: The interrupt handler name
1247 */
1248void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1249 irq_hw_number_t hwirq, struct irq_chip *chip,
1250 void *chip_data, irq_flow_handler_t handler,
1251 void *handler_data, const char *handler_name)
1252{
1253 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1254 irq_set_chip_data(virq, chip_data);
1255 irq_set_handler_data(virq, handler_data);
1256}
1257
Jiang Liuf8264e32014-11-06 22:20:14 +08001258static void irq_domain_check_hierarchy(struct irq_domain *domain)
1259{
1260}
1261#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */