blob: e1b925bea205816f06075ec0ed9d8b51a612ffa5 [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 void irq_domain_check_hierarchy(struct irq_domain *domain);
27
Marc Zyngierb145dcc2015-10-13 12:51:36 +010028struct irqchip_fwid {
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020029 struct fwnode_handle fwnode;
30 unsigned int type;
31 char *name;
32 void *data;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010033};
34
35/**
36 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
37 * identifying an irq domain
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020038 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
39 * @name: Optional user provided domain name
40 * @id: Optional user provided id if name != NULL
41 * @data: Optional user-provided data
Marc Zyngierb145dcc2015-10-13 12:51:36 +010042 *
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020043 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
Marc Zyngierb145dcc2015-10-13 12:51:36 +010044 * fwnode_handle (or NULL on failure).
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020045 *
46 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
47 * solely to transport name information to irqdomain creation code. The
48 * node is not stored. For other types the pointer is kept in the irq
49 * domain struct.
Marc Zyngierb145dcc2015-10-13 12:51:36 +010050 */
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020051struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
52 const char *name, void *data)
Marc Zyngierb145dcc2015-10-13 12:51:36 +010053{
54 struct irqchip_fwid *fwid;
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020055 char *n;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010056
57 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010058
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020059 switch (type) {
60 case IRQCHIP_FWNODE_NAMED:
61 n = kasprintf(GFP_KERNEL, "%s", name);
62 break;
63 case IRQCHIP_FWNODE_NAMED_ID:
64 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
65 break;
66 default:
67 n = kasprintf(GFP_KERNEL, "irqchip@%p", data);
68 break;
69 }
70
71 if (!fwid || !n) {
Marc Zyngierb145dcc2015-10-13 12:51:36 +010072 kfree(fwid);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020073 kfree(n);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010074 return NULL;
75 }
76
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020077 fwid->type = type;
78 fwid->name = n;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010079 fwid->data = data;
80 fwid->fwnode.type = FWNODE_IRQCHIP;
81 return &fwid->fwnode;
82}
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020083EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010084
85/**
86 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
87 *
88 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
89 */
90void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
91{
92 struct irqchip_fwid *fwid;
93
Suravee Suthikulpanit75aba7b2015-12-10 08:55:28 -080094 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
Marc Zyngierb145dcc2015-10-13 12:51:36 +010095 return;
96
97 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
98 kfree(fwid->name);
99 kfree(fwid);
100}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000101EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
Marc Zyngierb145dcc2015-10-13 12:51:36 +0100102
Grant Likelycc79ca62012-02-16 01:37:49 -0700103/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100104 * __irq_domain_add() - Allocate a new irq_domain data structure
Punit Agrawal545d5d62016-05-31 13:56:48 +0100105 * @fwnode: firmware node for the interrupt controller
Grant Likelyfa40f372013-06-08 12:57:40 +0100106 * @size: Size of linear map; 0 for radix mapping only
Jiang Liua2579542014-05-27 16:07:37 +0800107 * @hwirq_max: Maximum number of interrupts supported by controller
Grant Likelyfa40f372013-06-08 12:57:40 +0100108 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
109 * direct mapping
Jiang Liuf8264e32014-11-06 22:20:14 +0800110 * @ops: domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -0700111 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700112 *
Jiang Liua2579542014-05-27 16:07:37 +0800113 * Allocates and initialize and irq_domain structure.
114 * Returns pointer to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -0700115 */
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100116struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
Grant Likelyddaf1442013-06-10 01:06:02 +0100117 irq_hw_number_t hwirq_max, int direct_max,
Grant Likelyfa40f372013-06-08 12:57:40 +0100118 const struct irq_domain_ops *ops,
119 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700120{
Punit Agrawal545d5d62016-05-31 13:56:48 +0100121 struct device_node *of_node = to_of_node(fwnode);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200122 struct irqchip_fwid *fwid;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700123 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700124
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200125 static atomic_t unknown_domains;
126
Grant Likelycef50752012-07-11 17:24:31 +0100127 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
128 GFP_KERNEL, of_node_to_nid(of_node));
Grant Likelya8db8cf2012-02-14 14:06:54 -0700129 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -0700130 return NULL;
131
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200132 if (fwnode && is_fwnode_irqchip(fwnode)) {
133 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
134
135 switch (fwid->type) {
136 case IRQCHIP_FWNODE_NAMED:
137 case IRQCHIP_FWNODE_NAMED_ID:
138 domain->name = kstrdup(fwid->name, GFP_KERNEL);
139 if (!domain->name) {
140 kfree(domain);
141 return NULL;
142 }
143 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
144 break;
145 default:
146 domain->fwnode = fwnode;
147 domain->name = fwid->name;
148 break;
149 }
150 } else if (of_node) {
151 char *name;
152
153 /*
154 * DT paths contain '/', which debugfs is legitimately
155 * unhappy about. Replace them with ':', which does
156 * the trick and is not as offensive as '\'...
157 */
158 name = kstrdup(of_node_full_name(of_node), GFP_KERNEL);
159 if (!name) {
160 kfree(domain);
161 return NULL;
162 }
163
164 strreplace(name, '/', ':');
165
166 domain->name = name;
167 domain->fwnode = fwnode;
168 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
169 }
170
171 if (!domain->name) {
172 if (fwnode) {
173 pr_err("Invalid fwnode type (%d) for irqdomain\n",
174 fwnode->type);
175 }
176 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
177 atomic_inc_return(&unknown_domains));
178 if (!domain->name) {
179 kfree(domain);
180 return NULL;
181 }
182 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
183 }
184
Marc Zyngierf1107112015-10-13 12:51:30 +0100185 of_node_get(of_node);
Marc Zyngierf1107112015-10-13 12:51:30 +0100186
Grant Likelycc79ca62012-02-16 01:37:49 -0700187 /* Fill structure */
Grant Likely1aa0dd92013-06-08 12:03:59 +0100188 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
Grant Likely68700652012-02-14 14:06:53 -0700189 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700190 domain->host_data = host_data;
Grant Likelyddaf1442013-06-10 01:06:02 +0100191 domain->hwirq_max = hwirq_max;
Grant Likely1aa0dd92013-06-08 12:03:59 +0100192 domain->revmap_size = size;
Grant Likelyfa40f372013-06-08 12:57:40 +0100193 domain->revmap_direct_max_irq = direct_max;
Jiang Liuf8264e32014-11-06 22:20:14 +0800194 irq_domain_check_hierarchy(domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700195
Grant Likelya8db8cf2012-02-14 14:06:54 -0700196 mutex_lock(&irq_domain_mutex);
197 list_add(&domain->link, &irq_domain_list);
198 mutex_unlock(&irq_domain_mutex);
Grant Likelyfa40f372013-06-08 12:57:40 +0100199
Grant Likely1aa0dd92013-06-08 12:03:59 +0100200 pr_debug("Added domain %s\n", domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +0100201 return domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700202}
Grant Likelyfa40f372013-06-08 12:57:40 +0100203EXPORT_SYMBOL_GPL(__irq_domain_add);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700204
Paul Mundt58ee99a2012-05-19 15:11:41 +0900205/**
206 * irq_domain_remove() - Remove an irq domain.
207 * @domain: domain to remove
208 *
209 * This routine is used to remove an irq domain. The caller must ensure
210 * that all mappings within the domain have been disposed of prior to
211 * use, depending on the revmap type.
212 */
213void irq_domain_remove(struct irq_domain *domain)
214{
215 mutex_lock(&irq_domain_mutex);
216
Matthew Wilcoxe9256ef2016-05-20 17:01:33 -0700217 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
Paul Mundt58ee99a2012-05-19 15:11:41 +0900218
219 list_del(&domain->link);
220
221 /*
222 * If the going away domain is the default one, reset it.
223 */
224 if (unlikely(irq_default_domain == domain))
225 irq_set_default_host(NULL);
226
227 mutex_unlock(&irq_domain_mutex);
228
Grant Likely1aa0dd92013-06-08 12:03:59 +0100229 pr_debug("Removed domain %s\n", domain->name);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900230
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100231 of_node_put(irq_domain_get_of_node(domain));
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200232 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
233 kfree(domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +0100234 kfree(domain);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900235}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900236EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900237
Grant Likelya8db8cf2012-02-14 14:06:54 -0700238/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100239 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
Mark Brown781d0f42012-07-05 12:19:19 +0100240 * @of_node: pointer to interrupt controller's device tree node.
241 * @size: total number of irqs in mapping
Linus Walleij94a63da2013-06-06 12:10:23 +0100242 * @first_irq: first number of irq block assigned to the domain,
Grant Likelyfa40f372013-06-08 12:57:40 +0100243 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
244 * pre-map all of the irqs in the domain to virqs starting at first_irq.
Jiang Liuf8264e32014-11-06 22:20:14 +0800245 * @ops: domain callbacks
Mark Brown781d0f42012-07-05 12:19:19 +0100246 * @host_data: Controller private data pointer
247 *
Grant Likelyfa40f372013-06-08 12:57:40 +0100248 * Allocates an irq_domain, and optionally if first_irq is positive then also
249 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
Mark Brown781d0f42012-07-05 12:19:19 +0100250 *
251 * This is intended to implement the expected behaviour for most
Grant Likelyfa40f372013-06-08 12:57:40 +0100252 * interrupt controllers. If device tree is used, then first_irq will be 0 and
253 * irqs get mapped dynamically on the fly. However, if the controller requires
254 * static virq assignments (non-DT boot) then it will set that up correctly.
Mark Brown781d0f42012-07-05 12:19:19 +0100255 */
256struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
257 unsigned int size,
258 unsigned int first_irq,
259 const struct irq_domain_ops *ops,
260 void *host_data)
261{
Grant Likelyfa40f372013-06-08 12:57:40 +0100262 struct irq_domain *domain;
Linus Walleij2854d162012-09-27 14:59:39 +0200263
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100264 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
Grant Likelyfa40f372013-06-08 12:57:40 +0100265 if (!domain)
266 return NULL;
267
268 if (first_irq > 0) {
Linus Walleij2854d162012-09-27 14:59:39 +0200269 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
Grant Likelyfa40f372013-06-08 12:57:40 +0100270 /* attempt to allocated irq_descs */
271 int rc = irq_alloc_descs(first_irq, first_irq, size,
272 of_node_to_nid(of_node));
273 if (rc < 0)
Linus Walleijd202b7b2012-11-27 01:20:32 +0100274 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
275 first_irq);
Grant Likelyfa40f372013-06-08 12:57:40 +0100276 }
Grant Likelyddaf1442013-06-10 01:06:02 +0100277 irq_domain_associate_many(domain, first_irq, 0, size);
Linus Walleij2854d162012-09-27 14:59:39 +0200278 }
279
Grant Likelyfa40f372013-06-08 12:57:40 +0100280 return domain;
Mark Brown781d0f42012-07-05 12:19:19 +0100281}
Arnd Bergmann346dbb72013-04-25 19:28:54 +0200282EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Mark Brown781d0f42012-07-05 12:19:19 +0100283
284/**
Grant Likelya8db8cf2012-02-14 14:06:54 -0700285 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
286 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700287 * @size: total number of irqs in legacy mapping
288 * @first_irq: first number of irq block assigned to the domain
289 * @first_hwirq: first hwirq number to use for the translation. Should normally
290 * be '0', but a positive integer can be used if the effective
291 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700292 * @ops: map/unmap domain callbacks
293 * @host_data: Controller private data pointer
294 *
295 * Note: the map() callback will be called before this function returns
296 * for all legacy interrupts except 0 (which is always the invalid irq for
297 * a legacy controller).
298 */
299struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700300 unsigned int size,
301 unsigned int first_irq,
302 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700303 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700304 void *host_data)
305{
Grant Likely1bc04f22012-02-14 14:06:55 -0700306 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700307
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100308 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
Grant Likelyddaf1442013-06-10 01:06:02 +0100309 first_hwirq + size, 0, ops, host_data);
Jiang Liuf8264e32014-11-06 22:20:14 +0800310 if (domain)
311 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
Grant Likely1bc04f22012-02-14 14:06:55 -0700312
Grant Likelya8db8cf2012-02-14 14:06:54 -0700313 return domain;
314}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900315EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700316
Grant Likelya8db8cf2012-02-14 14:06:54 -0700317/**
Marc Zyngier651e8b52016-04-11 09:57:51 +0100318 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
319 * @fwspec: FW specifier for an interrupt
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100320 * @bus_token: domain-specific data
Grant Likelycc79ca62012-02-16 01:37:49 -0700321 */
Marc Zyngier651e8b52016-04-11 09:57:51 +0100322struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
Marc Zyngier130b8c62015-10-13 12:51:31 +0100323 enum irq_domain_bus_token bus_token)
Grant Likelycc79ca62012-02-16 01:37:49 -0700324{
325 struct irq_domain *h, *found = NULL;
Marc Zyngier651e8b52016-04-11 09:57:51 +0100326 struct fwnode_handle *fwnode = fwspec->fwnode;
Grant Likelya18dc812012-01-26 12:12:14 -0700327 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700328
329 /* We might want to match the legacy controller last since
330 * it might potentially be set to match all interrupts in
331 * the absence of a device node. This isn't a problem so far
332 * yet though...
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100333 *
334 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
335 * values must generate an exact match for the domain to be
336 * selected.
Grant Likelycc79ca62012-02-16 01:37:49 -0700337 */
338 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700339 list_for_each_entry(h, &irq_domain_list, link) {
Marc Zyngier651e8b52016-04-11 09:57:51 +0100340 if (h->ops->select && fwspec->param_count)
341 rc = h->ops->select(h, fwspec, bus_token);
342 else if (h->ops->match)
Marc Zyngier130b8c62015-10-13 12:51:31 +0100343 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
Grant Likelya18dc812012-01-26 12:12:14 -0700344 else
Marc Zyngier130b8c62015-10-13 12:51:31 +0100345 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100346 ((bus_token == DOMAIN_BUS_ANY) ||
347 (h->bus_token == bus_token)));
Grant Likelya18dc812012-01-26 12:12:14 -0700348
349 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700350 found = h;
351 break;
352 }
Grant Likelya18dc812012-01-26 12:12:14 -0700353 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700354 mutex_unlock(&irq_domain_mutex);
355 return found;
356}
Marc Zyngier651e8b52016-04-11 09:57:51 +0100357EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
Grant Likelycc79ca62012-02-16 01:37:49 -0700358
359/**
Eric Augerc7b41f02017-01-19 20:57:59 +0000360 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
361 * IRQ remapping
362 *
363 * Return: false if any MSI irq domain does not support IRQ remapping,
364 * true otherwise (including if there is no MSI irq domain)
365 */
366bool irq_domain_check_msi_remap(void)
367{
368 struct irq_domain *h;
369 bool ret = true;
370
371 mutex_lock(&irq_domain_mutex);
372 list_for_each_entry(h, &irq_domain_list, link) {
373 if (irq_domain_is_msi(h) &&
374 !irq_domain_hierarchical_is_msi_remap(h)) {
375 ret = false;
376 break;
377 }
378 }
379 mutex_unlock(&irq_domain_mutex);
380 return ret;
381}
382EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
383
384/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700385 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700386 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700387 *
388 * For convenience, it's possible to set a "default" domain that will be used
389 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
390 * platforms that want to manipulate a few hard coded interrupt numbers that
391 * aren't properly represented in the device-tree.
392 */
Grant Likely68700652012-02-14 14:06:53 -0700393void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700394{
Paul Mundt54a90582012-05-19 15:11:47 +0900395 pr_debug("Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700396
Grant Likely68700652012-02-14 14:06:53 -0700397 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700398}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900399EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700400
Jiang Liu43a77592014-06-09 16:20:05 +0800401void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
Grant Likely913af202012-06-03 22:04:35 -0700402{
Grant Likelyddaf1442013-06-10 01:06:02 +0100403 struct irq_data *irq_data = irq_get_irq_data(irq);
404 irq_hw_number_t hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700405
Grant Likelyddaf1442013-06-10 01:06:02 +0100406 if (WARN(!irq_data || irq_data->domain != domain,
407 "virq%i doesn't exist; cannot disassociate\n", irq))
408 return;
Grant Likely913af202012-06-03 22:04:35 -0700409
Grant Likelyddaf1442013-06-10 01:06:02 +0100410 hwirq = irq_data->hwirq;
411 irq_set_status_flags(irq, IRQ_NOREQUEST);
Grant Likely913af202012-06-03 22:04:35 -0700412
Grant Likelyddaf1442013-06-10 01:06:02 +0100413 /* remove chip and handler */
414 irq_set_chip_and_handler(irq, NULL, NULL);
Grant Likely913af202012-06-03 22:04:35 -0700415
Grant Likelyddaf1442013-06-10 01:06:02 +0100416 /* Make sure it's completed */
417 synchronize_irq(irq);
Grant Likely913af202012-06-03 22:04:35 -0700418
Grant Likelyddaf1442013-06-10 01:06:02 +0100419 /* Tell the PIC about it */
420 if (domain->ops->unmap)
421 domain->ops->unmap(domain, irq);
422 smp_mb();
Grant Likely913af202012-06-03 22:04:35 -0700423
Grant Likelyddaf1442013-06-10 01:06:02 +0100424 irq_data->domain = NULL;
425 irq_data->hwirq = 0;
Grant Likely913af202012-06-03 22:04:35 -0700426
Grant Likelyddaf1442013-06-10 01:06:02 +0100427 /* Clear reverse map for this hwirq */
428 if (hwirq < domain->revmap_size) {
429 domain->linear_revmap[hwirq] = 0;
430 } else {
431 mutex_lock(&revmap_trees_mutex);
432 radix_tree_delete(&domain->revmap_tree, hwirq);
433 mutex_unlock(&revmap_trees_mutex);
Grant Likely913af202012-06-03 22:04:35 -0700434 }
435}
436
Grant Likelyddaf1442013-06-10 01:06:02 +0100437int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
438 irq_hw_number_t hwirq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700439{
Grant Likelyddaf1442013-06-10 01:06:02 +0100440 struct irq_data *irq_data = irq_get_irq_data(virq);
441 int ret;
442
443 if (WARN(hwirq >= domain->hwirq_max,
444 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
445 return -EINVAL;
446 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
447 return -EINVAL;
448 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
449 return -EINVAL;
450
451 mutex_lock(&irq_domain_mutex);
452 irq_data->hwirq = hwirq;
453 irq_data->domain = domain;
454 if (domain->ops->map) {
455 ret = domain->ops->map(domain, virq, hwirq);
456 if (ret != 0) {
457 /*
458 * If map() returns -EPERM, this interrupt is protected
459 * by the firmware or some other service and shall not
460 * be mapped. Don't bother telling the user about it.
461 */
462 if (ret != -EPERM) {
463 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
464 domain->name, hwirq, virq, ret);
465 }
466 irq_data->domain = NULL;
467 irq_data->hwirq = 0;
468 mutex_unlock(&irq_domain_mutex);
469 return ret;
470 }
471
472 /* If not already assigned, give the domain the chip's name */
473 if (!domain->name && irq_data->chip)
474 domain->name = irq_data->chip->name;
475 }
476
477 if (hwirq < domain->revmap_size) {
478 domain->linear_revmap[hwirq] = virq;
479 } else {
480 mutex_lock(&revmap_trees_mutex);
481 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
482 mutex_unlock(&revmap_trees_mutex);
483 }
484 mutex_unlock(&irq_domain_mutex);
485
486 irq_clear_status_flags(virq, IRQ_NOREQUEST);
487
488 return 0;
489}
490EXPORT_SYMBOL_GPL(irq_domain_associate);
491
492void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
493 irq_hw_number_t hwirq_base, int count)
494{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100495 struct device_node *of_node;
Grant Likelyddaf1442013-06-10 01:06:02 +0100496 int i;
Grant Likelycc79ca62012-02-16 01:37:49 -0700497
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100498 of_node = irq_domain_get_of_node(domain);
Grant Likely98aa4682012-06-17 16:17:04 -0600499 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100500 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600501
502 for (i = 0; i < count; i++) {
Grant Likelyddaf1442013-06-10 01:06:02 +0100503 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
Grant Likelycc79ca62012-02-16 01:37:49 -0700504 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700505}
Grant Likely98aa4682012-06-17 16:17:04 -0600506EXPORT_SYMBOL_GPL(irq_domain_associate_many);
Grant Likelycc79ca62012-02-16 01:37:49 -0700507
508/**
509 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700510 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700511 *
512 * This routine is used for irq controllers which can choose the hardware
513 * interrupt numbers they generate. In such a case it's simplest to use
Grant Likely1aa0dd92013-06-08 12:03:59 +0100514 * the linux irq as the hardware interrupt number. It still uses the linear
515 * or radix tree to store the mapping, but the irq controller can optimize
516 * the revmap path by using the hwirq directly.
Grant Likelycc79ca62012-02-16 01:37:49 -0700517 */
Grant Likely68700652012-02-14 14:06:53 -0700518unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700519{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100520 struct device_node *of_node;
Grant Likelycc79ca62012-02-16 01:37:49 -0700521 unsigned int virq;
522
Grant Likely68700652012-02-14 14:06:53 -0700523 if (domain == NULL)
524 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700525
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100526 of_node = irq_domain_get_of_node(domain);
527 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
Grant Likely03848372012-02-14 14:06:52 -0700528 if (!virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900529 pr_debug("create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700530 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700531 }
Grant Likely1aa0dd92013-06-08 12:03:59 +0100532 if (virq >= domain->revmap_direct_max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700533 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely1aa0dd92013-06-08 12:03:59 +0100534 domain->revmap_direct_max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700535 irq_free_desc(virq);
536 return 0;
537 }
Paul Mundt54a90582012-05-19 15:11:47 +0900538 pr_debug("create_direct obtained virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700539
Grant Likely98aa4682012-06-17 16:17:04 -0600540 if (irq_domain_associate(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700541 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700542 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700543 }
544
545 return virq;
546}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900547EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700548
549/**
550 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700551 * @domain: domain owning this hardware interrupt or NULL for default domain
552 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700553 *
554 * Only one mapping per hardware interrupt is permitted. Returns a linux
555 * irq number.
556 * If the sense/trigger is to be specified, set_irq_type() should be called
557 * on the number returned from that call.
558 */
Grant Likely68700652012-02-14 14:06:53 -0700559unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700560 irq_hw_number_t hwirq)
561{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100562 struct device_node *of_node;
David Daney5b7526e2012-04-05 16:52:13 -0700563 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700564
Paul Mundt54a90582012-05-19 15:11:47 +0900565 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700566
Grant Likely68700652012-02-14 14:06:53 -0700567 /* Look for default domain if nececssary */
568 if (domain == NULL)
569 domain = irq_default_domain;
570 if (domain == NULL) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800571 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700572 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700573 }
Paul Mundt54a90582012-05-19 15:11:47 +0900574 pr_debug("-> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700575
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100576 of_node = irq_domain_get_of_node(domain);
577
Grant Likelycc79ca62012-02-16 01:37:49 -0700578 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700579 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700580 if (virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900581 pr_debug("-> existing mapping on virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700582 return virq;
583 }
584
Grant Likely1bc04f22012-02-14 14:06:55 -0700585 /* Allocate a virtual interrupt number */
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900586 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
David Daney5b7526e2012-04-05 16:52:13 -0700587 if (virq <= 0) {
Paul Mundt54a90582012-05-19 15:11:47 +0900588 pr_debug("-> virq allocation failed\n");
Grant Likely1bc04f22012-02-14 14:06:55 -0700589 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700590 }
591
Grant Likely98aa4682012-06-17 16:17:04 -0600592 if (irq_domain_associate(domain, virq, hwirq)) {
Grant Likely73255702012-06-03 22:04:35 -0700593 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700594 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700595 }
596
Paul Mundt54a90582012-05-19 15:11:47 +0900597 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100598 hwirq, of_node_full_name(of_node), virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700599
600 return virq;
601}
602EXPORT_SYMBOL_GPL(irq_create_mapping);
603
Grant Likely98aa4682012-06-17 16:17:04 -0600604/**
605 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
606 * @domain: domain owning the interrupt range
607 * @irq_base: beginning of linux IRQ range
608 * @hwirq_base: beginning of hardware IRQ range
609 * @count: Number of interrupts to map
610 *
611 * This routine is used for allocating and mapping a range of hardware
612 * irqs to linux irqs where the linux irq numbers are at pre-defined
613 * locations. For use by controllers that already have static mappings
614 * to insert in to the domain.
615 *
616 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
617 * domain insertion.
618 *
619 * 0 is returned upon success, while any failure to establish a static
620 * mapping is treated as an error.
621 */
622int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
623 irq_hw_number_t hwirq_base, int count)
624{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100625 struct device_node *of_node;
Grant Likely98aa4682012-06-17 16:17:04 -0600626 int ret;
627
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100628 of_node = irq_domain_get_of_node(domain);
Grant Likely98aa4682012-06-17 16:17:04 -0600629 ret = irq_alloc_descs(irq_base, irq_base, count,
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100630 of_node_to_nid(of_node));
Grant Likely98aa4682012-06-17 16:17:04 -0600631 if (unlikely(ret < 0))
632 return ret;
633
Grant Likelyddaf1442013-06-10 01:06:02 +0100634 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600635 return 0;
636}
637EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
638
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100639static int irq_domain_translate(struct irq_domain *d,
640 struct irq_fwspec *fwspec,
641 irq_hw_number_t *hwirq, unsigned int *type)
642{
643#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
644 if (d->ops->translate)
645 return d->ops->translate(d, fwspec, hwirq, type);
646#endif
647 if (d->ops->xlate)
648 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
649 fwspec->param, fwspec->param_count,
650 hwirq, type);
651
652 /* If domain has no translation, then we assume interrupt line */
653 *hwirq = fwspec->param[0];
654 return 0;
655}
656
657static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
658 struct irq_fwspec *fwspec)
659{
660 int i;
661
662 fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
663 fwspec->param_count = irq_data->args_count;
664
665 for (i = 0; i < irq_data->args_count; i++)
666 fwspec->param[i] = irq_data->args[i];
667}
668
Marc Zyngierc0131f02015-10-13 12:51:34 +0100669unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
Grant Likelycc79ca62012-02-16 01:37:49 -0700670{
Grant Likely68700652012-02-14 14:06:53 -0700671 struct irq_domain *domain;
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100672 struct irq_data *irq_data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700673 irq_hw_number_t hwirq;
674 unsigned int type = IRQ_TYPE_NONE;
Jiang Liuf8264e32014-11-06 22:20:14 +0800675 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700676
Marc Zyngier530cbe12016-01-26 13:52:25 +0000677 if (fwspec->fwnode) {
Marc Zyngier651e8b52016-04-11 09:57:51 +0100678 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
Marc Zyngier530cbe12016-01-26 13:52:25 +0000679 if (!domain)
Marc Zyngier651e8b52016-04-11 09:57:51 +0100680 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
Marc Zyngier530cbe12016-01-26 13:52:25 +0000681 } else {
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100682 domain = irq_default_domain;
Marc Zyngier530cbe12016-01-26 13:52:25 +0000683 }
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100684
Grant Likely68700652012-02-14 14:06:53 -0700685 if (!domain) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800686 pr_warn("no irq domain found for %s !\n",
Marc Zyngierc0131f02015-10-13 12:51:34 +0100687 of_node_full_name(to_of_node(fwspec->fwnode)));
Grant Likely03848372012-02-14 14:06:52 -0700688 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700689 }
690
Marc Zyngierc0131f02015-10-13 12:51:34 +0100691 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100692 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700693
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100694 /*
695 * WARN if the irqchip returns a type with bits
696 * outside the sense mask set and clear these bits.
697 */
698 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
699 type &= IRQ_TYPE_SENSE_MASK;
700
701 /*
702 * If we've already configured this interrupt,
703 * don't do it again, or hell will break loose.
704 */
705 virq = irq_find_mapping(domain, hwirq);
706 if (virq) {
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800707 /*
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100708 * If the trigger type is not specified or matches the
709 * current trigger type then we are done so return the
710 * interrupt number.
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800711 */
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100712 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800713 return virq;
714
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100715 /*
716 * If the trigger type has not been set yet, then set
717 * it now and return the interrupt number.
718 */
719 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100720 irq_data = irq_get_irq_data(virq);
721 if (!irq_data)
722 return 0;
723
724 irqd_set_trigger_type(irq_data, type);
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100725 return virq;
726 }
727
728 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
729 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
730 return 0;
731 }
732
733 if (irq_domain_is_hierarchy(domain)) {
Marc Zyngierc0131f02015-10-13 12:51:34 +0100734 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800735 if (virq <= 0)
736 return 0;
737 } else {
738 /* Create mapping */
739 virq = irq_create_mapping(domain, hwirq);
740 if (!virq)
741 return virq;
742 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700743
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100744 irq_data = irq_get_irq_data(virq);
745 if (!irq_data) {
746 if (irq_domain_is_hierarchy(domain))
747 irq_domain_free_irqs(virq, 1);
748 else
749 irq_dispose_mapping(virq);
750 return 0;
751 }
752
753 /* Store trigger type */
754 irqd_set_trigger_type(irq_data, type);
755
Grant Likelycc79ca62012-02-16 01:37:49 -0700756 return virq;
757}
Marc Zyngierc0131f02015-10-13 12:51:34 +0100758EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
759
760unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
761{
762 struct irq_fwspec fwspec;
763
764 of_phandle_args_to_fwspec(irq_data, &fwspec);
765 return irq_create_fwspec_mapping(&fwspec);
766}
Grant Likelycc79ca62012-02-16 01:37:49 -0700767EXPORT_SYMBOL_GPL(irq_create_of_mapping);
768
769/**
770 * irq_dispose_mapping() - Unmap an interrupt
771 * @virq: linux irq number of the interrupt to unmap
772 */
773void irq_dispose_mapping(unsigned int virq)
774{
775 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700776 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700777
Grant Likely03848372012-02-14 14:06:52 -0700778 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700779 return;
780
Grant Likely68700652012-02-14 14:06:53 -0700781 domain = irq_data->domain;
782 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700783 return;
784
Jon Hunterd16dcd3d2016-06-21 10:23:22 +0100785 if (irq_domain_is_hierarchy(domain)) {
786 irq_domain_free_irqs(virq, 1);
787 } else {
788 irq_domain_disassociate(domain, virq);
789 irq_free_desc(virq);
790 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700791}
792EXPORT_SYMBOL_GPL(irq_dispose_mapping);
793
794/**
795 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700796 * @domain: domain owning this hardware interrupt
797 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700798 */
Grant Likely68700652012-02-14 14:06:53 -0700799unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700800 irq_hw_number_t hwirq)
801{
Grant Likely4c0946c2012-06-03 22:04:39 -0700802 struct irq_data *data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700803
Grant Likely68700652012-02-14 14:06:53 -0700804 /* Look for default domain if nececssary */
805 if (domain == NULL)
806 domain = irq_default_domain;
807 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700808 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700809
Grant Likely1aa0dd92013-06-08 12:03:59 +0100810 if (hwirq < domain->revmap_direct_max_irq) {
Jiang Liuf8264e32014-11-06 22:20:14 +0800811 data = irq_domain_get_irq_data(domain, hwirq);
812 if (data && data->hwirq == hwirq)
Grant Likely4c0946c2012-06-03 22:04:39 -0700813 return hwirq;
Grant Likely4c0946c2012-06-03 22:04:39 -0700814 }
815
Grant Likelyd3dcb432013-06-10 12:19:17 +0100816 /* Check if the hwirq is in the linear revmap. */
817 if (hwirq < domain->revmap_size)
818 return domain->linear_revmap[hwirq];
819
820 rcu_read_lock();
821 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
822 rcu_read_unlock();
823 return data ? data->irq : 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700824}
825EXPORT_SYMBOL_GPL(irq_find_mapping);
826
Grant Likely092b2fb2012-03-29 14:10:30 -0600827#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Marc Zyngierfe17a422017-05-12 12:55:35 +0100828static void virq_debug_show_one(struct seq_file *m, struct irq_desc *desc)
829{
830 struct irq_domain *domain;
831 struct irq_data *data;
832
833 domain = desc->irq_data.domain;
834 data = &desc->irq_data;
835
836 while (domain) {
837 unsigned int irq = data->irq;
838 unsigned long hwirq = data->hwirq;
839 struct irq_chip *chip;
840 bool direct;
841
842 if (data == &desc->irq_data)
843 seq_printf(m, "%5d ", irq);
844 else
845 seq_printf(m, "%5d+ ", irq);
846 seq_printf(m, "0x%05lx ", hwirq);
847
848 chip = irq_data_get_irq_chip(data);
849 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
850
851 seq_printf(m, data ? "0x%p " : " %p ",
852 irq_data_get_irq_chip_data(data));
853
854 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
855 direct = (irq == hwirq) && (irq < domain->revmap_direct_max_irq);
856 seq_printf(m, "%6s%-8s ",
857 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
858 direct ? "(DIRECT)" : "");
859 seq_printf(m, "%s\n", domain->name);
860#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
861 domain = domain->parent;
862 data = data->parent_data;
863#else
864 domain = NULL;
865#endif
866 }
867}
868
Grant Likelycc79ca62012-02-16 01:37:49 -0700869static int virq_debug_show(struct seq_file *m, void *private)
870{
871 unsigned long flags;
872 struct irq_desc *desc;
Grant Likely1400ea82013-06-06 22:20:44 +0100873 struct irq_domain *domain;
874 struct radix_tree_iter iter;
Marc Zyngierfe17a422017-05-12 12:55:35 +0100875 void **slot;
Grant Likelycc79ca62012-02-16 01:37:49 -0700876 int i;
877
Grant Likely1400ea82013-06-06 22:20:44 +0100878 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
879 "name", "mapped", "linear-max", "direct-max", "devtree-node");
880 mutex_lock(&irq_domain_mutex);
881 list_for_each_entry(domain, &irq_domain_list, link) {
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100882 struct device_node *of_node;
Marc Zyngier2370c002017-05-12 12:55:36 +0100883 const char *name;
884
Grant Likely1400ea82013-06-06 22:20:44 +0100885 int count = 0;
Marc Zyngier2370c002017-05-12 12:55:36 +0100886
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100887 of_node = irq_domain_get_of_node(domain);
Marc Zyngier2370c002017-05-12 12:55:36 +0100888 if (of_node)
889 name = of_node_full_name(of_node);
890 else if (is_fwnode_irqchip(domain->fwnode))
891 name = container_of(domain->fwnode, struct irqchip_fwid,
892 fwnode)->name;
893 else
894 name = "";
895
Grant Likely1400ea82013-06-06 22:20:44 +0100896 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
897 count++;
898 seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
899 domain == irq_default_domain ? '*' : ' ', domain->name,
900 domain->revmap_size + count, domain->revmap_size,
901 domain->revmap_direct_max_irq,
Marc Zyngier2370c002017-05-12 12:55:36 +0100902 name);
Grant Likely1400ea82013-06-06 22:20:44 +0100903 }
904 mutex_unlock(&irq_domain_mutex);
905
906 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600907 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
Grant Likely1400ea82013-06-06 22:20:44 +0100908 "active", "type", "domain");
Grant Likelycc79ca62012-02-16 01:37:49 -0700909
910 for (i = 1; i < nr_irqs; i++) {
911 desc = irq_to_desc(i);
912 if (!desc)
913 continue;
914
915 raw_spin_lock_irqsave(&desc->lock, flags);
Marc Zyngierfe17a422017-05-12 12:55:35 +0100916 virq_debug_show_one(m, desc);
Grant Likelycc79ca62012-02-16 01:37:49 -0700917 raw_spin_unlock_irqrestore(&desc->lock, flags);
918 }
919
920 return 0;
921}
922
923static int virq_debug_open(struct inode *inode, struct file *file)
924{
925 return single_open(file, virq_debug_show, inode->i_private);
926}
927
928static const struct file_operations virq_debug_fops = {
929 .open = virq_debug_open,
930 .read = seq_read,
931 .llseek = seq_lseek,
932 .release = single_release,
933};
934
935static int __init irq_debugfs_init(void)
936{
Grant Likely092b2fb2012-03-29 14:10:30 -0600937 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -0700938 NULL, &virq_debug_fops) == NULL)
939 return -ENOMEM;
940
941 return 0;
942}
943__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -0600944#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -0700945
Grant Likely16b2e6e2012-01-26 11:26:52 -0700946/**
947 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
948 *
949 * Device Tree IRQ specifier translation function which works with one cell
950 * bindings where the cell value maps directly to the hwirq number.
951 */
952int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
953 const u32 *intspec, unsigned int intsize,
954 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600955{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700956 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600957 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600958 *out_hwirq = intspec[0];
959 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600960 return 0;
961}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700962EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
963
964/**
965 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
966 *
967 * Device Tree IRQ specifier translation function which works with two cell
968 * bindings where the cell values map directly to the hwirq number
969 * and linux irq flags.
970 */
971int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
972 const u32 *intspec, unsigned int intsize,
973 irq_hw_number_t *out_hwirq, unsigned int *out_type)
974{
975 if (WARN_ON(intsize < 2))
976 return -EINVAL;
977 *out_hwirq = intspec[0];
978 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
979 return 0;
980}
981EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
982
983/**
984 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
985 *
986 * Device Tree IRQ specifier translation function which works with either one
987 * or two cell bindings where the cell values map directly to the hwirq number
988 * and linux irq flags.
989 *
990 * Note: don't use this function unless your interrupt controller explicitly
991 * supports both one and two cell bindings. For the majority of controllers
992 * the _onecell() or _twocell() variants above should be used.
993 */
994int irq_domain_xlate_onetwocell(struct irq_domain *d,
995 struct device_node *ctrlr,
996 const u32 *intspec, unsigned int intsize,
997 unsigned long *out_hwirq, unsigned int *out_type)
998{
999 if (WARN_ON(intsize < 1))
1000 return -EINVAL;
1001 *out_hwirq = intspec[0];
Sebastian Frias0c228912016-08-02 10:52:45 +02001002 if (intsize > 1)
1003 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1004 else
1005 *out_type = IRQ_TYPE_NONE;
Grant Likely16b2e6e2012-01-26 11:26:52 -07001006 return 0;
1007}
1008EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -06001009
Grant Likelya18dc812012-01-26 12:12:14 -07001010const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely16b2e6e2012-01-26 11:26:52 -07001011 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -07001012};
1013EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Jiang Liuf8264e32014-11-06 22:20:14 +08001014
Qais Yousefac0a0cd2015-12-08 13:20:18 +00001015int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001016 int node, const struct cpumask *affinity)
Jiang Liuf8264e32014-11-06 22:20:14 +08001017{
1018 unsigned int hint;
1019
1020 if (virq >= 0) {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001021 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1022 affinity);
Jiang Liuf8264e32014-11-06 22:20:14 +08001023 } else {
1024 hint = hwirq % nr_irqs;
1025 if (hint == 0)
1026 hint++;
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001027 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1028 affinity);
1029 if (virq <= 0 && hint > 1) {
1030 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1031 affinity);
1032 }
Jiang Liuf8264e32014-11-06 22:20:14 +08001033 }
1034
1035 return virq;
1036}
1037
1038#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
Jiang Liuafb7da82014-11-15 22:24:02 +08001039/**
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001040 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
Jiang Liuafb7da82014-11-15 22:24:02 +08001041 * @parent: Parent irq domain to associate with the new domain
1042 * @flags: Irq domain flags associated to the domain
1043 * @size: Size of the domain. See below
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001044 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liuafb7da82014-11-15 22:24:02 +08001045 * @ops: Pointer to the interrupt domain callbacks
1046 * @host_data: Controller private data pointer
1047 *
1048 * If @size is 0 a tree domain is created, otherwise a linear domain.
1049 *
1050 * If successful the parent is associated to the new domain and the
1051 * domain flags are set.
1052 * Returns pointer to IRQ domain, or NULL on failure.
1053 */
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001054struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
Jiang Liuafb7da82014-11-15 22:24:02 +08001055 unsigned int flags,
1056 unsigned int size,
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001057 struct fwnode_handle *fwnode,
Jiang Liuafb7da82014-11-15 22:24:02 +08001058 const struct irq_domain_ops *ops,
1059 void *host_data)
1060{
1061 struct irq_domain *domain;
1062
1063 if (size)
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001064 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
Jiang Liuafb7da82014-11-15 22:24:02 +08001065 else
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001066 domain = irq_domain_create_tree(fwnode, ops, host_data);
Jiang Liuafb7da82014-11-15 22:24:02 +08001067 if (domain) {
1068 domain->parent = parent;
1069 domain->flags |= flags;
1070 }
1071
1072 return domain;
1073}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001074EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
Jiang Liuafb7da82014-11-15 22:24:02 +08001075
Jiang Liuf8264e32014-11-06 22:20:14 +08001076static void irq_domain_insert_irq(int virq)
1077{
1078 struct irq_data *data;
1079
1080 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1081 struct irq_domain *domain = data->domain;
1082 irq_hw_number_t hwirq = data->hwirq;
1083
1084 if (hwirq < domain->revmap_size) {
1085 domain->linear_revmap[hwirq] = virq;
1086 } else {
1087 mutex_lock(&revmap_trees_mutex);
1088 radix_tree_insert(&domain->revmap_tree, hwirq, data);
1089 mutex_unlock(&revmap_trees_mutex);
1090 }
1091
1092 /* If not already assigned, give the domain the chip's name */
1093 if (!domain->name && data->chip)
1094 domain->name = data->chip->name;
1095 }
1096
1097 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1098}
1099
1100static void irq_domain_remove_irq(int virq)
1101{
1102 struct irq_data *data;
1103
1104 irq_set_status_flags(virq, IRQ_NOREQUEST);
1105 irq_set_chip_and_handler(virq, NULL, NULL);
1106 synchronize_irq(virq);
1107 smp_mb();
1108
1109 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1110 struct irq_domain *domain = data->domain;
1111 irq_hw_number_t hwirq = data->hwirq;
1112
1113 if (hwirq < domain->revmap_size) {
1114 domain->linear_revmap[hwirq] = 0;
1115 } else {
1116 mutex_lock(&revmap_trees_mutex);
1117 radix_tree_delete(&domain->revmap_tree, hwirq);
1118 mutex_unlock(&revmap_trees_mutex);
1119 }
1120 }
1121}
1122
1123static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1124 struct irq_data *child)
1125{
1126 struct irq_data *irq_data;
1127
Jiang Liu67830112015-06-01 16:05:13 +08001128 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1129 irq_data_get_node(child));
Jiang Liuf8264e32014-11-06 22:20:14 +08001130 if (irq_data) {
1131 child->parent_data = irq_data;
1132 irq_data->irq = child->irq;
Jiang Liu0d0b4c82015-06-01 16:05:12 +08001133 irq_data->common = child->common;
Jiang Liuf8264e32014-11-06 22:20:14 +08001134 irq_data->domain = domain;
1135 }
1136
1137 return irq_data;
1138}
1139
1140static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1141{
1142 struct irq_data *irq_data, *tmp;
1143 int i;
1144
1145 for (i = 0; i < nr_irqs; i++) {
1146 irq_data = irq_get_irq_data(virq + i);
1147 tmp = irq_data->parent_data;
1148 irq_data->parent_data = NULL;
1149 irq_data->domain = NULL;
1150
1151 while (tmp) {
1152 irq_data = tmp;
1153 tmp = tmp->parent_data;
1154 kfree(irq_data);
1155 }
1156 }
1157}
1158
1159static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1160 unsigned int virq, unsigned int nr_irqs)
1161{
1162 struct irq_data *irq_data;
1163 struct irq_domain *parent;
1164 int i;
1165
1166 /* The outermost irq_data is embedded in struct irq_desc */
1167 for (i = 0; i < nr_irqs; i++) {
1168 irq_data = irq_get_irq_data(virq + i);
1169 irq_data->domain = domain;
1170
1171 for (parent = domain->parent; parent; parent = parent->parent) {
1172 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1173 if (!irq_data) {
1174 irq_domain_free_irq_data(virq, i + 1);
1175 return -ENOMEM;
1176 }
1177 }
1178 }
1179
1180 return 0;
1181}
1182
1183/**
1184 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1185 * @domain: domain to match
1186 * @virq: IRQ number to get irq_data
1187 */
1188struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1189 unsigned int virq)
1190{
1191 struct irq_data *irq_data;
1192
1193 for (irq_data = irq_get_irq_data(virq); irq_data;
1194 irq_data = irq_data->parent_data)
1195 if (irq_data->domain == domain)
1196 return irq_data;
1197
1198 return NULL;
1199}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001200EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001201
1202/**
1203 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1204 * @domain: Interrupt domain to match
1205 * @virq: IRQ number
1206 * @hwirq: The hwirq number
1207 * @chip: The associated interrupt chip
1208 * @chip_data: The associated chip data
1209 */
1210int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1211 irq_hw_number_t hwirq, struct irq_chip *chip,
1212 void *chip_data)
1213{
1214 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1215
1216 if (!irq_data)
1217 return -ENOENT;
1218
1219 irq_data->hwirq = hwirq;
1220 irq_data->chip = chip ? chip : &no_irq_chip;
1221 irq_data->chip_data = chip_data;
1222
1223 return 0;
1224}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001225EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
Jiang Liuf8264e32014-11-06 22:20:14 +08001226
1227/**
Jiang Liu1b537702014-11-09 23:10:24 +08001228 * irq_domain_set_info - Set the complete data for a @virq in @domain
1229 * @domain: Interrupt domain to match
1230 * @virq: IRQ number
1231 * @hwirq: The hardware interrupt number
1232 * @chip: The associated interrupt chip
1233 * @chip_data: The associated interrupt chip data
1234 * @handler: The interrupt flow handler
1235 * @handler_data: The interrupt flow handler data
1236 * @handler_name: The interrupt handler name
1237 */
1238void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1239 irq_hw_number_t hwirq, struct irq_chip *chip,
1240 void *chip_data, irq_flow_handler_t handler,
1241 void *handler_data, const char *handler_name)
1242{
1243 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1244 __irq_set_handler(virq, handler, 0, handler_name);
1245 irq_set_handler_data(virq, handler_data);
1246}
Keith Busch64bce3e2016-01-12 13:18:07 -07001247EXPORT_SYMBOL(irq_domain_set_info);
Jiang Liu1b537702014-11-09 23:10:24 +08001248
1249/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001250 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1251 * @irq_data: The pointer to irq_data
1252 */
1253void irq_domain_reset_irq_data(struct irq_data *irq_data)
1254{
1255 irq_data->hwirq = 0;
1256 irq_data->chip = &no_irq_chip;
1257 irq_data->chip_data = NULL;
1258}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001259EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001260
1261/**
1262 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1263 * @domain: Interrupt domain to match
1264 * @virq: IRQ number to start with
1265 * @nr_irqs: The number of irqs to free
1266 */
1267void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1268 unsigned int nr_irqs)
1269{
1270 struct irq_data *irq_data;
1271 int i;
1272
1273 for (i = 0; i < nr_irqs; i++) {
1274 irq_data = irq_domain_get_irq_data(domain, virq + i);
1275 if (irq_data)
1276 irq_domain_reset_irq_data(irq_data);
1277 }
1278 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1279}
Axel Lin63cc7872016-03-17 12:00:31 +08001280EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
Jiang Liuf8264e32014-11-06 22:20:14 +08001281
1282/**
1283 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1284 * @domain: Interrupt domain to match
1285 * @virq: IRQ number to start with
1286 * @nr_irqs: The number of irqs to free
1287 */
1288void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1289 unsigned int nr_irqs)
1290{
1291 int i;
1292
1293 for (i = 0; i < nr_irqs; i++) {
1294 irq_set_handler_data(virq + i, NULL);
1295 irq_set_handler(virq + i, NULL);
1296 }
1297 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1298}
1299
Jiang Liu36d72732014-11-15 22:24:01 +08001300static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
1301{
1302 return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
1303}
1304
1305static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
1306 unsigned int irq_base,
1307 unsigned int nr_irqs)
1308{
1309 domain->ops->free(domain, irq_base, nr_irqs);
1310 if (irq_domain_is_auto_recursive(domain)) {
1311 BUG_ON(!domain->parent);
1312 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1313 nr_irqs);
1314 }
1315}
1316
Marc Zyngierc4665952015-11-23 08:26:04 +00001317int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
1318 unsigned int irq_base,
1319 unsigned int nr_irqs, void *arg)
Jiang Liu36d72732014-11-15 22:24:01 +08001320{
1321 int ret = 0;
1322 struct irq_domain *parent = domain->parent;
1323 bool recursive = irq_domain_is_auto_recursive(domain);
1324
1325 BUG_ON(recursive && !parent);
1326 if (recursive)
1327 ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
1328 nr_irqs, arg);
Alexander Popova1b7b1a2016-07-03 03:24:08 +03001329 if (ret < 0)
1330 return ret;
1331
1332 ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
Jiang Liu36d72732014-11-15 22:24:01 +08001333 if (ret < 0 && recursive)
1334 irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
1335
1336 return ret;
1337}
1338
Jiang Liuf8264e32014-11-06 22:20:14 +08001339/**
1340 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1341 * @domain: domain to allocate from
1342 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
1343 * @nr_irqs: number of IRQs to allocate
1344 * @node: NUMA node id for memory allocation
1345 * @arg: domain specific argument
1346 * @realloc: IRQ descriptors have already been allocated if true
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001347 * @affinity: Optional irq affinity mask for multiqueue devices
Jiang Liuf8264e32014-11-06 22:20:14 +08001348 *
1349 * Allocate IRQ numbers and initialized all data structures to support
1350 * hierarchy IRQ domains.
1351 * Parameter @realloc is mainly to support legacy IRQs.
1352 * Returns error code or allocated IRQ number
1353 *
1354 * The whole process to setup an IRQ has been split into two steps.
1355 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1356 * descriptor and required hardware resources. The second step,
1357 * irq_domain_activate_irq(), is to program hardwares with preallocated
1358 * resources. In this way, it's easier to rollback when failing to
1359 * allocate resources.
1360 */
1361int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1362 unsigned int nr_irqs, int node, void *arg,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001363 bool realloc, const struct cpumask *affinity)
Jiang Liuf8264e32014-11-06 22:20:14 +08001364{
1365 int i, ret, virq;
1366
1367 if (domain == NULL) {
1368 domain = irq_default_domain;
1369 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1370 return -EINVAL;
1371 }
1372
1373 if (!domain->ops->alloc) {
1374 pr_debug("domain->ops->alloc() is NULL\n");
1375 return -ENOSYS;
1376 }
1377
1378 if (realloc && irq_base >= 0) {
1379 virq = irq_base;
1380 } else {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001381 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1382 affinity);
Jiang Liuf8264e32014-11-06 22:20:14 +08001383 if (virq < 0) {
1384 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1385 irq_base, nr_irqs);
1386 return virq;
1387 }
1388 }
1389
1390 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1391 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1392 ret = -ENOMEM;
1393 goto out_free_desc;
1394 }
1395
1396 mutex_lock(&irq_domain_mutex);
Jiang Liu36d72732014-11-15 22:24:01 +08001397 ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
Jiang Liuf8264e32014-11-06 22:20:14 +08001398 if (ret < 0) {
1399 mutex_unlock(&irq_domain_mutex);
1400 goto out_free_irq_data;
1401 }
1402 for (i = 0; i < nr_irqs; i++)
1403 irq_domain_insert_irq(virq + i);
1404 mutex_unlock(&irq_domain_mutex);
1405
1406 return virq;
1407
1408out_free_irq_data:
1409 irq_domain_free_irq_data(virq, nr_irqs);
1410out_free_desc:
1411 irq_free_descs(virq, nr_irqs);
1412 return ret;
1413}
1414
1415/**
1416 * irq_domain_free_irqs - Free IRQ number and associated data structures
1417 * @virq: base IRQ number
1418 * @nr_irqs: number of IRQs to free
1419 */
1420void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1421{
1422 struct irq_data *data = irq_get_irq_data(virq);
1423 int i;
1424
1425 if (WARN(!data || !data->domain || !data->domain->ops->free,
1426 "NULL pointer, cannot free irq\n"))
1427 return;
1428
1429 mutex_lock(&irq_domain_mutex);
1430 for (i = 0; i < nr_irqs; i++)
1431 irq_domain_remove_irq(virq + i);
Jiang Liu36d72732014-11-15 22:24:01 +08001432 irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
Jiang Liuf8264e32014-11-06 22:20:14 +08001433 mutex_unlock(&irq_domain_mutex);
1434
1435 irq_domain_free_irq_data(virq, nr_irqs);
1436 irq_free_descs(virq, nr_irqs);
1437}
1438
1439/**
Jiang Liu36d72732014-11-15 22:24:01 +08001440 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1441 * @irq_base: Base IRQ number
1442 * @nr_irqs: Number of IRQs to allocate
1443 * @arg: Allocation data (arch/domain specific)
1444 *
1445 * Check whether the domain has been setup recursive. If not allocate
1446 * through the parent domain.
1447 */
1448int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1449 unsigned int irq_base, unsigned int nr_irqs,
1450 void *arg)
1451{
1452 /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
1453 if (irq_domain_is_auto_recursive(domain))
1454 return 0;
1455
1456 domain = domain->parent;
1457 if (domain)
1458 return irq_domain_alloc_irqs_recursive(domain, irq_base,
1459 nr_irqs, arg);
1460 return -ENOSYS;
1461}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001462EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
Jiang Liu36d72732014-11-15 22:24:01 +08001463
1464/**
1465 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1466 * @irq_base: Base IRQ number
1467 * @nr_irqs: Number of IRQs to free
1468 *
1469 * Check whether the domain has been setup recursive. If not free
1470 * through the parent domain.
1471 */
1472void irq_domain_free_irqs_parent(struct irq_domain *domain,
1473 unsigned int irq_base, unsigned int nr_irqs)
1474{
1475 /* irq_domain_free_irqs_recursive() will call parent's free */
1476 if (!irq_domain_is_auto_recursive(domain) && domain->parent)
1477 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1478 nr_irqs);
1479}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001480EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
Jiang Liu36d72732014-11-15 22:24:01 +08001481
Marc Zyngier08d85f32017-01-17 16:00:48 +00001482static void __irq_domain_activate_irq(struct irq_data *irq_data)
1483{
1484 if (irq_data && irq_data->domain) {
1485 struct irq_domain *domain = irq_data->domain;
1486
1487 if (irq_data->parent_data)
1488 __irq_domain_activate_irq(irq_data->parent_data);
1489 if (domain->ops->activate)
1490 domain->ops->activate(domain, irq_data);
1491 }
1492}
1493
1494static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1495{
1496 if (irq_data && irq_data->domain) {
1497 struct irq_domain *domain = irq_data->domain;
1498
1499 if (domain->ops->deactivate)
1500 domain->ops->deactivate(domain, irq_data);
1501 if (irq_data->parent_data)
1502 __irq_domain_deactivate_irq(irq_data->parent_data);
1503 }
1504}
1505
Jiang Liu36d72732014-11-15 22:24:01 +08001506/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001507 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1508 * interrupt
1509 * @irq_data: outermost irq_data associated with interrupt
1510 *
1511 * This is the second step to call domain_ops->activate to program interrupt
1512 * controllers, so the interrupt could actually get delivered.
1513 */
1514void irq_domain_activate_irq(struct irq_data *irq_data)
1515{
Marc Zyngier08d85f32017-01-17 16:00:48 +00001516 if (!irqd_is_activated(irq_data)) {
1517 __irq_domain_activate_irq(irq_data);
1518 irqd_set_activated(irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001519 }
1520}
1521
1522/**
1523 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1524 * deactivate interrupt
1525 * @irq_data: outermost irq_data associated with interrupt
1526 *
1527 * It calls domain_ops->deactivate to program interrupt controllers to disable
1528 * interrupt delivery.
1529 */
1530void irq_domain_deactivate_irq(struct irq_data *irq_data)
1531{
Marc Zyngier08d85f32017-01-17 16:00:48 +00001532 if (irqd_is_activated(irq_data)) {
1533 __irq_domain_deactivate_irq(irq_data);
1534 irqd_clr_activated(irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001535 }
1536}
1537
1538static void irq_domain_check_hierarchy(struct irq_domain *domain)
1539{
1540 /* Hierarchy irq_domains must implement callback alloc() */
1541 if (domain->ops->alloc)
1542 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1543}
Eric Auger631a9632017-01-19 20:57:57 +00001544
1545/**
1546 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1547 * parent has MSI remapping support
1548 * @domain: domain pointer
1549 */
1550bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1551{
1552 for (; domain; domain = domain->parent) {
1553 if (irq_domain_is_msi_remap(domain))
1554 return true;
1555 }
1556 return false;
1557}
Jiang Liuf8264e32014-11-06 22:20:14 +08001558#else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1559/**
1560 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1561 * @domain: domain to match
1562 * @virq: IRQ number to get irq_data
1563 */
1564struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1565 unsigned int virq)
1566{
1567 struct irq_data *irq_data = irq_get_irq_data(virq);
1568
1569 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1570}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001571EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001572
Stefan Agner5f22f5c2015-05-16 11:44:13 +02001573/**
1574 * irq_domain_set_info - Set the complete data for a @virq in @domain
1575 * @domain: Interrupt domain to match
1576 * @virq: IRQ number
1577 * @hwirq: The hardware interrupt number
1578 * @chip: The associated interrupt chip
1579 * @chip_data: The associated interrupt chip data
1580 * @handler: The interrupt flow handler
1581 * @handler_data: The interrupt flow handler data
1582 * @handler_name: The interrupt handler name
1583 */
1584void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1585 irq_hw_number_t hwirq, struct irq_chip *chip,
1586 void *chip_data, irq_flow_handler_t handler,
1587 void *handler_data, const char *handler_name)
1588{
1589 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1590 irq_set_chip_data(virq, chip_data);
1591 irq_set_handler_data(virq, handler_data);
1592}
1593
Jiang Liuf8264e32014-11-06 22:20:14 +08001594static void irq_domain_check_hierarchy(struct irq_domain *domain)
1595{
1596}
1597#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */