blob: b50f737574ae1370f98912826abfc0d5a0cf3ee0 [file] [log] [blame]
Paul Mundt54a90582012-05-19 15:11:47 +09001#define pr_fmt(fmt) "irq: " fmt
2
Marc Zyngierc5c601c2017-07-07 09:39:59 +01003#include <linux/acpi.h>
Grant Likelycc79ca62012-02-16 01:37:49 -07004#include <linux/debugfs.h>
5#include <linux/hardirq.h>
6#include <linux/interrupt.h>
Grant Likely08a543a2011-07-26 03:19:06 -06007#include <linux/irq.h>
Grant Likelycc79ca62012-02-16 01:37:49 -07008#include <linux/irqdesc.h>
Grant Likely08a543a2011-07-26 03:19:06 -06009#include <linux/irqdomain.h>
10#include <linux/module.h>
11#include <linux/mutex.h>
12#include <linux/of.h>
Grant Likely7e713302011-07-26 03:19:06 -060013#include <linux/of_address.h>
Rashika Kheria64be38a2014-02-27 17:10:12 +053014#include <linux/of_irq.h>
Paul Mundt5ca4db62012-06-03 22:04:34 -070015#include <linux/topology.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070016#include <linux/seq_file.h>
Grant Likely7e713302011-07-26 03:19:06 -060017#include <linux/slab.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070018#include <linux/smp.h>
19#include <linux/fs.h>
Grant Likely08a543a2011-07-26 03:19:06 -060020
21static LIST_HEAD(irq_domain_list);
22static DEFINE_MUTEX(irq_domain_mutex);
23
Grant Likelycc79ca62012-02-16 01:37:49 -070024static DEFINE_MUTEX(revmap_trees_mutex);
Grant Likely68700652012-02-14 14:06:53 -070025static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070026
Jiang Liuf8264e32014-11-06 22:20:14 +080027static void irq_domain_check_hierarchy(struct irq_domain *domain);
28
Marc Zyngierb145dcc2015-10-13 12:51:36 +010029struct irqchip_fwid {
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020030 struct fwnode_handle fwnode;
31 unsigned int type;
32 char *name;
Thomas Gleixner087cdfb2017-06-20 01:37:17 +020033 void *data;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010034};
35
Thomas Gleixner087cdfb2017-06-20 01:37:17 +020036#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
37static void debugfs_add_domain_dir(struct irq_domain *d);
38static void debugfs_remove_domain_dir(struct irq_domain *d);
39#else
40static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
41static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
42#endif
43
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030044const struct fwnode_operations irqchip_fwnode_ops;
Arnd Bergmannb6eb66f2017-07-26 02:19:35 +020045EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030046
Marc Zyngierb145dcc2015-10-13 12:51:36 +010047/**
48 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
49 * identifying an irq domain
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020050 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
51 * @name: Optional user provided domain name
52 * @id: Optional user provided id if name != NULL
53 * @data: Optional user-provided data
Marc Zyngierb145dcc2015-10-13 12:51:36 +010054 *
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020055 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
Marc Zyngierb145dcc2015-10-13 12:51:36 +010056 * fwnode_handle (or NULL on failure).
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020057 *
58 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
59 * solely to transport name information to irqdomain creation code. The
60 * node is not stored. For other types the pointer is kept in the irq
61 * domain struct.
Marc Zyngierb145dcc2015-10-13 12:51:36 +010062 */
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020063struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
64 const char *name, void *data)
Marc Zyngierb145dcc2015-10-13 12:51:36 +010065{
66 struct irqchip_fwid *fwid;
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020067 char *n;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010068
69 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010070
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020071 switch (type) {
72 case IRQCHIP_FWNODE_NAMED:
73 n = kasprintf(GFP_KERNEL, "%s", name);
74 break;
75 case IRQCHIP_FWNODE_NAMED_ID:
76 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
77 break;
78 default:
79 n = kasprintf(GFP_KERNEL, "irqchip@%p", data);
80 break;
81 }
82
83 if (!fwid || !n) {
Marc Zyngierb145dcc2015-10-13 12:51:36 +010084 kfree(fwid);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020085 kfree(n);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010086 return NULL;
87 }
88
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020089 fwid->type = type;
90 fwid->name = n;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010091 fwid->data = data;
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030092 fwid->fwnode.ops = &irqchip_fwnode_ops;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010093 return &fwid->fwnode;
94}
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020095EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010096
97/**
98 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
99 *
100 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
101 */
102void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
103{
104 struct irqchip_fwid *fwid;
105
Suravee Suthikulpanit75aba7b2015-12-10 08:55:28 -0800106 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
Marc Zyngierb145dcc2015-10-13 12:51:36 +0100107 return;
108
109 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
110 kfree(fwid->name);
111 kfree(fwid);
112}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000113EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
Marc Zyngierb145dcc2015-10-13 12:51:36 +0100114
Grant Likelycc79ca62012-02-16 01:37:49 -0700115/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100116 * __irq_domain_add() - Allocate a new irq_domain data structure
Punit Agrawal545d5d62016-05-31 13:56:48 +0100117 * @fwnode: firmware node for the interrupt controller
Grant Likelyfa40f372013-06-08 12:57:40 +0100118 * @size: Size of linear map; 0 for radix mapping only
Jiang Liua2579542014-05-27 16:07:37 +0800119 * @hwirq_max: Maximum number of interrupts supported by controller
Grant Likelyfa40f372013-06-08 12:57:40 +0100120 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
121 * direct mapping
Jiang Liuf8264e32014-11-06 22:20:14 +0800122 * @ops: domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -0700123 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700124 *
Jiang Liua2579542014-05-27 16:07:37 +0800125 * Allocates and initialize and irq_domain structure.
126 * Returns pointer to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -0700127 */
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100128struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
Grant Likelyddaf1442013-06-10 01:06:02 +0100129 irq_hw_number_t hwirq_max, int direct_max,
Grant Likelyfa40f372013-06-08 12:57:40 +0100130 const struct irq_domain_ops *ops,
131 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700132{
Punit Agrawal545d5d62016-05-31 13:56:48 +0100133 struct device_node *of_node = to_of_node(fwnode);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200134 struct irqchip_fwid *fwid;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700135 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700136
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200137 static atomic_t unknown_domains;
138
Grant Likelycef50752012-07-11 17:24:31 +0100139 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
140 GFP_KERNEL, of_node_to_nid(of_node));
Grant Likelya8db8cf2012-02-14 14:06:54 -0700141 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -0700142 return NULL;
143
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200144 if (fwnode && is_fwnode_irqchip(fwnode)) {
145 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
146
147 switch (fwid->type) {
148 case IRQCHIP_FWNODE_NAMED:
149 case IRQCHIP_FWNODE_NAMED_ID:
150 domain->name = kstrdup(fwid->name, GFP_KERNEL);
151 if (!domain->name) {
152 kfree(domain);
153 return NULL;
154 }
155 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
156 break;
157 default:
158 domain->fwnode = fwnode;
159 domain->name = fwid->name;
160 break;
161 }
Marc Zyngierc5c601c2017-07-07 09:39:59 +0100162#ifdef CONFIG_ACPI
163 } else if (is_acpi_device_node(fwnode)) {
164 struct acpi_buffer buf = {
165 .length = ACPI_ALLOCATE_BUFFER,
166 };
167 acpi_handle handle;
168
169 handle = acpi_device_handle(to_acpi_device_node(fwnode));
170 if (acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf) == AE_OK) {
171 domain->name = buf.pointer;
172 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
173 }
174
175 domain->fwnode = fwnode;
176#endif
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200177 } else if (of_node) {
178 char *name;
179
180 /*
181 * DT paths contain '/', which debugfs is legitimately
182 * unhappy about. Replace them with ':', which does
183 * the trick and is not as offensive as '\'...
184 */
185 name = kstrdup(of_node_full_name(of_node), GFP_KERNEL);
186 if (!name) {
187 kfree(domain);
188 return NULL;
189 }
190
191 strreplace(name, '/', ':');
192
193 domain->name = name;
194 domain->fwnode = fwnode;
195 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
196 }
197
198 if (!domain->name) {
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300199 if (fwnode)
200 pr_err("Invalid fwnode type for irqdomain\n");
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200201 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
202 atomic_inc_return(&unknown_domains));
203 if (!domain->name) {
204 kfree(domain);
205 return NULL;
206 }
207 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
208 }
209
Marc Zyngierf1107112015-10-13 12:51:30 +0100210 of_node_get(of_node);
Marc Zyngierf1107112015-10-13 12:51:30 +0100211
Grant Likelycc79ca62012-02-16 01:37:49 -0700212 /* Fill structure */
Grant Likely1aa0dd92013-06-08 12:03:59 +0100213 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
Grant Likely68700652012-02-14 14:06:53 -0700214 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700215 domain->host_data = host_data;
Grant Likelyddaf1442013-06-10 01:06:02 +0100216 domain->hwirq_max = hwirq_max;
Grant Likely1aa0dd92013-06-08 12:03:59 +0100217 domain->revmap_size = size;
Grant Likelyfa40f372013-06-08 12:57:40 +0100218 domain->revmap_direct_max_irq = direct_max;
Jiang Liuf8264e32014-11-06 22:20:14 +0800219 irq_domain_check_hierarchy(domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700220
Grant Likelya8db8cf2012-02-14 14:06:54 -0700221 mutex_lock(&irq_domain_mutex);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +0200222 debugfs_add_domain_dir(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700223 list_add(&domain->link, &irq_domain_list);
224 mutex_unlock(&irq_domain_mutex);
Grant Likelyfa40f372013-06-08 12:57:40 +0100225
Grant Likely1aa0dd92013-06-08 12:03:59 +0100226 pr_debug("Added domain %s\n", domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +0100227 return domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700228}
Grant Likelyfa40f372013-06-08 12:57:40 +0100229EXPORT_SYMBOL_GPL(__irq_domain_add);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700230
Paul Mundt58ee99a2012-05-19 15:11:41 +0900231/**
232 * irq_domain_remove() - Remove an irq domain.
233 * @domain: domain to remove
234 *
235 * This routine is used to remove an irq domain. The caller must ensure
236 * that all mappings within the domain have been disposed of prior to
237 * use, depending on the revmap type.
238 */
239void irq_domain_remove(struct irq_domain *domain)
240{
241 mutex_lock(&irq_domain_mutex);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +0200242 debugfs_remove_domain_dir(domain);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900243
Matthew Wilcoxe9256ef2016-05-20 17:01:33 -0700244 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
Paul Mundt58ee99a2012-05-19 15:11:41 +0900245
246 list_del(&domain->link);
247
248 /*
249 * If the going away domain is the default one, reset it.
250 */
251 if (unlikely(irq_default_domain == domain))
252 irq_set_default_host(NULL);
253
254 mutex_unlock(&irq_domain_mutex);
255
Grant Likely1aa0dd92013-06-08 12:03:59 +0100256 pr_debug("Removed domain %s\n", domain->name);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900257
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100258 of_node_put(irq_domain_get_of_node(domain));
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200259 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
260 kfree(domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +0100261 kfree(domain);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900262}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900263EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900264
Marc Zyngier61d0a002017-06-22 11:34:57 +0100265void irq_domain_update_bus_token(struct irq_domain *domain,
266 enum irq_domain_bus_token bus_token)
267{
268 char *name;
269
270 if (domain->bus_token == bus_token)
271 return;
272
273 mutex_lock(&irq_domain_mutex);
274
275 domain->bus_token = bus_token;
276
277 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
278 if (!name) {
279 mutex_unlock(&irq_domain_mutex);
280 return;
281 }
282
283 debugfs_remove_domain_dir(domain);
284
285 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
286 kfree(domain->name);
287 else
288 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
289
290 domain->name = name;
291 debugfs_add_domain_dir(domain);
292
293 mutex_unlock(&irq_domain_mutex);
294}
295
Grant Likelya8db8cf2012-02-14 14:06:54 -0700296/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100297 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
Mark Brown781d0f42012-07-05 12:19:19 +0100298 * @of_node: pointer to interrupt controller's device tree node.
299 * @size: total number of irqs in mapping
Linus Walleij94a63da2013-06-06 12:10:23 +0100300 * @first_irq: first number of irq block assigned to the domain,
Grant Likelyfa40f372013-06-08 12:57:40 +0100301 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
302 * pre-map all of the irqs in the domain to virqs starting at first_irq.
Jiang Liuf8264e32014-11-06 22:20:14 +0800303 * @ops: domain callbacks
Mark Brown781d0f42012-07-05 12:19:19 +0100304 * @host_data: Controller private data pointer
305 *
Grant Likelyfa40f372013-06-08 12:57:40 +0100306 * Allocates an irq_domain, and optionally if first_irq is positive then also
307 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
Mark Brown781d0f42012-07-05 12:19:19 +0100308 *
309 * This is intended to implement the expected behaviour for most
Grant Likelyfa40f372013-06-08 12:57:40 +0100310 * interrupt controllers. If device tree is used, then first_irq will be 0 and
311 * irqs get mapped dynamically on the fly. However, if the controller requires
312 * static virq assignments (non-DT boot) then it will set that up correctly.
Mark Brown781d0f42012-07-05 12:19:19 +0100313 */
314struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
315 unsigned int size,
316 unsigned int first_irq,
317 const struct irq_domain_ops *ops,
318 void *host_data)
319{
Grant Likelyfa40f372013-06-08 12:57:40 +0100320 struct irq_domain *domain;
Linus Walleij2854d162012-09-27 14:59:39 +0200321
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100322 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
Grant Likelyfa40f372013-06-08 12:57:40 +0100323 if (!domain)
324 return NULL;
325
326 if (first_irq > 0) {
Linus Walleij2854d162012-09-27 14:59:39 +0200327 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
Grant Likelyfa40f372013-06-08 12:57:40 +0100328 /* attempt to allocated irq_descs */
329 int rc = irq_alloc_descs(first_irq, first_irq, size,
330 of_node_to_nid(of_node));
331 if (rc < 0)
Linus Walleijd202b7b2012-11-27 01:20:32 +0100332 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
333 first_irq);
Grant Likelyfa40f372013-06-08 12:57:40 +0100334 }
Grant Likelyddaf1442013-06-10 01:06:02 +0100335 irq_domain_associate_many(domain, first_irq, 0, size);
Linus Walleij2854d162012-09-27 14:59:39 +0200336 }
337
Grant Likelyfa40f372013-06-08 12:57:40 +0100338 return domain;
Mark Brown781d0f42012-07-05 12:19:19 +0100339}
Arnd Bergmann346dbb72013-04-25 19:28:54 +0200340EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Mark Brown781d0f42012-07-05 12:19:19 +0100341
342/**
Grant Likelya8db8cf2012-02-14 14:06:54 -0700343 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
344 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700345 * @size: total number of irqs in legacy mapping
346 * @first_irq: first number of irq block assigned to the domain
347 * @first_hwirq: first hwirq number to use for the translation. Should normally
348 * be '0', but a positive integer can be used if the effective
349 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700350 * @ops: map/unmap domain callbacks
351 * @host_data: Controller private data pointer
352 *
353 * Note: the map() callback will be called before this function returns
354 * for all legacy interrupts except 0 (which is always the invalid irq for
355 * a legacy controller).
356 */
357struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700358 unsigned int size,
359 unsigned int first_irq,
360 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700361 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700362 void *host_data)
363{
Grant Likely1bc04f22012-02-14 14:06:55 -0700364 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700365
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100366 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
Grant Likelyddaf1442013-06-10 01:06:02 +0100367 first_hwirq + size, 0, ops, host_data);
Jiang Liuf8264e32014-11-06 22:20:14 +0800368 if (domain)
369 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
Grant Likely1bc04f22012-02-14 14:06:55 -0700370
Grant Likelya8db8cf2012-02-14 14:06:54 -0700371 return domain;
372}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900373EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700374
Grant Likelya8db8cf2012-02-14 14:06:54 -0700375/**
Marc Zyngier651e8b52016-04-11 09:57:51 +0100376 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
377 * @fwspec: FW specifier for an interrupt
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100378 * @bus_token: domain-specific data
Grant Likelycc79ca62012-02-16 01:37:49 -0700379 */
Marc Zyngier651e8b52016-04-11 09:57:51 +0100380struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
Marc Zyngier130b8c62015-10-13 12:51:31 +0100381 enum irq_domain_bus_token bus_token)
Grant Likelycc79ca62012-02-16 01:37:49 -0700382{
383 struct irq_domain *h, *found = NULL;
Marc Zyngier651e8b52016-04-11 09:57:51 +0100384 struct fwnode_handle *fwnode = fwspec->fwnode;
Grant Likelya18dc812012-01-26 12:12:14 -0700385 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700386
387 /* We might want to match the legacy controller last since
388 * it might potentially be set to match all interrupts in
389 * the absence of a device node. This isn't a problem so far
390 * yet though...
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100391 *
392 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
393 * values must generate an exact match for the domain to be
394 * selected.
Grant Likelycc79ca62012-02-16 01:37:49 -0700395 */
396 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700397 list_for_each_entry(h, &irq_domain_list, link) {
Marc Zyngier651e8b52016-04-11 09:57:51 +0100398 if (h->ops->select && fwspec->param_count)
399 rc = h->ops->select(h, fwspec, bus_token);
400 else if (h->ops->match)
Marc Zyngier130b8c62015-10-13 12:51:31 +0100401 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
Grant Likelya18dc812012-01-26 12:12:14 -0700402 else
Marc Zyngier130b8c62015-10-13 12:51:31 +0100403 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100404 ((bus_token == DOMAIN_BUS_ANY) ||
405 (h->bus_token == bus_token)));
Grant Likelya18dc812012-01-26 12:12:14 -0700406
407 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700408 found = h;
409 break;
410 }
Grant Likelya18dc812012-01-26 12:12:14 -0700411 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700412 mutex_unlock(&irq_domain_mutex);
413 return found;
414}
Marc Zyngier651e8b52016-04-11 09:57:51 +0100415EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
Grant Likelycc79ca62012-02-16 01:37:49 -0700416
417/**
Eric Augerc7b41f02017-01-19 20:57:59 +0000418 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
419 * IRQ remapping
420 *
421 * Return: false if any MSI irq domain does not support IRQ remapping,
422 * true otherwise (including if there is no MSI irq domain)
423 */
424bool irq_domain_check_msi_remap(void)
425{
426 struct irq_domain *h;
427 bool ret = true;
428
429 mutex_lock(&irq_domain_mutex);
430 list_for_each_entry(h, &irq_domain_list, link) {
431 if (irq_domain_is_msi(h) &&
432 !irq_domain_hierarchical_is_msi_remap(h)) {
433 ret = false;
434 break;
435 }
436 }
437 mutex_unlock(&irq_domain_mutex);
438 return ret;
439}
440EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
441
442/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700443 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700444 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700445 *
446 * For convenience, it's possible to set a "default" domain that will be used
447 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
448 * platforms that want to manipulate a few hard coded interrupt numbers that
449 * aren't properly represented in the device-tree.
450 */
Grant Likely68700652012-02-14 14:06:53 -0700451void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700452{
Paul Mundt54a90582012-05-19 15:11:47 +0900453 pr_debug("Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700454
Grant Likely68700652012-02-14 14:06:53 -0700455 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700456}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900457EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700458
David Daneyb526adf2017-08-17 17:53:32 -0700459static void irq_domain_clear_mapping(struct irq_domain *domain,
460 irq_hw_number_t hwirq)
461{
462 if (hwirq < domain->revmap_size) {
463 domain->linear_revmap[hwirq] = 0;
464 } else {
465 mutex_lock(&revmap_trees_mutex);
466 radix_tree_delete(&domain->revmap_tree, hwirq);
467 mutex_unlock(&revmap_trees_mutex);
468 }
469}
470
471static void irq_domain_set_mapping(struct irq_domain *domain,
472 irq_hw_number_t hwirq,
473 struct irq_data *irq_data)
474{
475 if (hwirq < domain->revmap_size) {
476 domain->linear_revmap[hwirq] = irq_data->irq;
477 } else {
478 mutex_lock(&revmap_trees_mutex);
479 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
480 mutex_unlock(&revmap_trees_mutex);
481 }
482}
483
Jiang Liu43a77592014-06-09 16:20:05 +0800484void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
Grant Likely913af202012-06-03 22:04:35 -0700485{
Grant Likelyddaf1442013-06-10 01:06:02 +0100486 struct irq_data *irq_data = irq_get_irq_data(irq);
487 irq_hw_number_t hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700488
Grant Likelyddaf1442013-06-10 01:06:02 +0100489 if (WARN(!irq_data || irq_data->domain != domain,
490 "virq%i doesn't exist; cannot disassociate\n", irq))
491 return;
Grant Likely913af202012-06-03 22:04:35 -0700492
Grant Likelyddaf1442013-06-10 01:06:02 +0100493 hwirq = irq_data->hwirq;
494 irq_set_status_flags(irq, IRQ_NOREQUEST);
Grant Likely913af202012-06-03 22:04:35 -0700495
Grant Likelyddaf1442013-06-10 01:06:02 +0100496 /* remove chip and handler */
497 irq_set_chip_and_handler(irq, NULL, NULL);
Grant Likely913af202012-06-03 22:04:35 -0700498
Grant Likelyddaf1442013-06-10 01:06:02 +0100499 /* Make sure it's completed */
500 synchronize_irq(irq);
Grant Likely913af202012-06-03 22:04:35 -0700501
Grant Likelyddaf1442013-06-10 01:06:02 +0100502 /* Tell the PIC about it */
503 if (domain->ops->unmap)
504 domain->ops->unmap(domain, irq);
505 smp_mb();
Grant Likely913af202012-06-03 22:04:35 -0700506
Grant Likelyddaf1442013-06-10 01:06:02 +0100507 irq_data->domain = NULL;
508 irq_data->hwirq = 0;
Thomas Gleixner9dc6be32017-06-20 01:37:16 +0200509 domain->mapcount--;
Grant Likely913af202012-06-03 22:04:35 -0700510
Grant Likelyddaf1442013-06-10 01:06:02 +0100511 /* Clear reverse map for this hwirq */
David Daneyb526adf2017-08-17 17:53:32 -0700512 irq_domain_clear_mapping(domain, hwirq);
Grant Likely913af202012-06-03 22:04:35 -0700513}
514
Grant Likelyddaf1442013-06-10 01:06:02 +0100515int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
516 irq_hw_number_t hwirq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700517{
Grant Likelyddaf1442013-06-10 01:06:02 +0100518 struct irq_data *irq_data = irq_get_irq_data(virq);
519 int ret;
520
521 if (WARN(hwirq >= domain->hwirq_max,
522 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
523 return -EINVAL;
524 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
525 return -EINVAL;
526 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
527 return -EINVAL;
528
529 mutex_lock(&irq_domain_mutex);
530 irq_data->hwirq = hwirq;
531 irq_data->domain = domain;
532 if (domain->ops->map) {
533 ret = domain->ops->map(domain, virq, hwirq);
534 if (ret != 0) {
535 /*
536 * If map() returns -EPERM, this interrupt is protected
537 * by the firmware or some other service and shall not
538 * be mapped. Don't bother telling the user about it.
539 */
540 if (ret != -EPERM) {
541 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
542 domain->name, hwirq, virq, ret);
543 }
544 irq_data->domain = NULL;
545 irq_data->hwirq = 0;
546 mutex_unlock(&irq_domain_mutex);
547 return ret;
548 }
549
550 /* If not already assigned, give the domain the chip's name */
551 if (!domain->name && irq_data->chip)
552 domain->name = irq_data->chip->name;
553 }
554
Thomas Gleixner9dc6be32017-06-20 01:37:16 +0200555 domain->mapcount++;
David Daneyb526adf2017-08-17 17:53:32 -0700556 irq_domain_set_mapping(domain, hwirq, irq_data);
Grant Likelyddaf1442013-06-10 01:06:02 +0100557 mutex_unlock(&irq_domain_mutex);
558
559 irq_clear_status_flags(virq, IRQ_NOREQUEST);
560
561 return 0;
562}
563EXPORT_SYMBOL_GPL(irq_domain_associate);
564
565void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
566 irq_hw_number_t hwirq_base, int count)
567{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100568 struct device_node *of_node;
Grant Likelyddaf1442013-06-10 01:06:02 +0100569 int i;
Grant Likelycc79ca62012-02-16 01:37:49 -0700570
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100571 of_node = irq_domain_get_of_node(domain);
Grant Likely98aa4682012-06-17 16:17:04 -0600572 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100573 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600574
575 for (i = 0; i < count; i++) {
Grant Likelyddaf1442013-06-10 01:06:02 +0100576 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
Grant Likelycc79ca62012-02-16 01:37:49 -0700577 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700578}
Grant Likely98aa4682012-06-17 16:17:04 -0600579EXPORT_SYMBOL_GPL(irq_domain_associate_many);
Grant Likelycc79ca62012-02-16 01:37:49 -0700580
581/**
582 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700583 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700584 *
585 * This routine is used for irq controllers which can choose the hardware
586 * interrupt numbers they generate. In such a case it's simplest to use
Grant Likely1aa0dd92013-06-08 12:03:59 +0100587 * the linux irq as the hardware interrupt number. It still uses the linear
588 * or radix tree to store the mapping, but the irq controller can optimize
589 * the revmap path by using the hwirq directly.
Grant Likelycc79ca62012-02-16 01:37:49 -0700590 */
Grant Likely68700652012-02-14 14:06:53 -0700591unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700592{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100593 struct device_node *of_node;
Grant Likelycc79ca62012-02-16 01:37:49 -0700594 unsigned int virq;
595
Grant Likely68700652012-02-14 14:06:53 -0700596 if (domain == NULL)
597 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700598
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100599 of_node = irq_domain_get_of_node(domain);
600 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
Grant Likely03848372012-02-14 14:06:52 -0700601 if (!virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900602 pr_debug("create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700603 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700604 }
Grant Likely1aa0dd92013-06-08 12:03:59 +0100605 if (virq >= domain->revmap_direct_max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700606 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely1aa0dd92013-06-08 12:03:59 +0100607 domain->revmap_direct_max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700608 irq_free_desc(virq);
609 return 0;
610 }
Paul Mundt54a90582012-05-19 15:11:47 +0900611 pr_debug("create_direct obtained virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700612
Grant Likely98aa4682012-06-17 16:17:04 -0600613 if (irq_domain_associate(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700614 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700615 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700616 }
617
618 return virq;
619}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900620EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700621
622/**
623 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700624 * @domain: domain owning this hardware interrupt or NULL for default domain
625 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700626 *
627 * Only one mapping per hardware interrupt is permitted. Returns a linux
628 * irq number.
629 * If the sense/trigger is to be specified, set_irq_type() should be called
630 * on the number returned from that call.
631 */
Grant Likely68700652012-02-14 14:06:53 -0700632unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700633 irq_hw_number_t hwirq)
634{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100635 struct device_node *of_node;
David Daney5b7526e2012-04-05 16:52:13 -0700636 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700637
Paul Mundt54a90582012-05-19 15:11:47 +0900638 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700639
Grant Likely68700652012-02-14 14:06:53 -0700640 /* Look for default domain if nececssary */
641 if (domain == NULL)
642 domain = irq_default_domain;
643 if (domain == NULL) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800644 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700645 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700646 }
Paul Mundt54a90582012-05-19 15:11:47 +0900647 pr_debug("-> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700648
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100649 of_node = irq_domain_get_of_node(domain);
650
Grant Likelycc79ca62012-02-16 01:37:49 -0700651 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700652 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700653 if (virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900654 pr_debug("-> existing mapping on virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700655 return virq;
656 }
657
Grant Likely1bc04f22012-02-14 14:06:55 -0700658 /* Allocate a virtual interrupt number */
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900659 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
David Daney5b7526e2012-04-05 16:52:13 -0700660 if (virq <= 0) {
Paul Mundt54a90582012-05-19 15:11:47 +0900661 pr_debug("-> virq allocation failed\n");
Grant Likely1bc04f22012-02-14 14:06:55 -0700662 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700663 }
664
Grant Likely98aa4682012-06-17 16:17:04 -0600665 if (irq_domain_associate(domain, virq, hwirq)) {
Grant Likely73255702012-06-03 22:04:35 -0700666 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700667 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700668 }
669
Paul Mundt54a90582012-05-19 15:11:47 +0900670 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100671 hwirq, of_node_full_name(of_node), virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700672
673 return virq;
674}
675EXPORT_SYMBOL_GPL(irq_create_mapping);
676
Grant Likely98aa4682012-06-17 16:17:04 -0600677/**
678 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
679 * @domain: domain owning the interrupt range
680 * @irq_base: beginning of linux IRQ range
681 * @hwirq_base: beginning of hardware IRQ range
682 * @count: Number of interrupts to map
683 *
684 * This routine is used for allocating and mapping a range of hardware
685 * irqs to linux irqs where the linux irq numbers are at pre-defined
686 * locations. For use by controllers that already have static mappings
687 * to insert in to the domain.
688 *
689 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
690 * domain insertion.
691 *
692 * 0 is returned upon success, while any failure to establish a static
693 * mapping is treated as an error.
694 */
695int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
696 irq_hw_number_t hwirq_base, int count)
697{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100698 struct device_node *of_node;
Grant Likely98aa4682012-06-17 16:17:04 -0600699 int ret;
700
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100701 of_node = irq_domain_get_of_node(domain);
Grant Likely98aa4682012-06-17 16:17:04 -0600702 ret = irq_alloc_descs(irq_base, irq_base, count,
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100703 of_node_to_nid(of_node));
Grant Likely98aa4682012-06-17 16:17:04 -0600704 if (unlikely(ret < 0))
705 return ret;
706
Grant Likelyddaf1442013-06-10 01:06:02 +0100707 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600708 return 0;
709}
710EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
711
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100712static int irq_domain_translate(struct irq_domain *d,
713 struct irq_fwspec *fwspec,
714 irq_hw_number_t *hwirq, unsigned int *type)
715{
716#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
717 if (d->ops->translate)
718 return d->ops->translate(d, fwspec, hwirq, type);
719#endif
720 if (d->ops->xlate)
721 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
722 fwspec->param, fwspec->param_count,
723 hwirq, type);
724
725 /* If domain has no translation, then we assume interrupt line */
726 *hwirq = fwspec->param[0];
727 return 0;
728}
729
730static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
731 struct irq_fwspec *fwspec)
732{
733 int i;
734
735 fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
736 fwspec->param_count = irq_data->args_count;
737
738 for (i = 0; i < irq_data->args_count; i++)
739 fwspec->param[i] = irq_data->args[i];
740}
741
Marc Zyngierc0131f02015-10-13 12:51:34 +0100742unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
Grant Likelycc79ca62012-02-16 01:37:49 -0700743{
Grant Likely68700652012-02-14 14:06:53 -0700744 struct irq_domain *domain;
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100745 struct irq_data *irq_data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700746 irq_hw_number_t hwirq;
747 unsigned int type = IRQ_TYPE_NONE;
Jiang Liuf8264e32014-11-06 22:20:14 +0800748 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700749
Marc Zyngier530cbe12016-01-26 13:52:25 +0000750 if (fwspec->fwnode) {
Marc Zyngier651e8b52016-04-11 09:57:51 +0100751 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
Marc Zyngier530cbe12016-01-26 13:52:25 +0000752 if (!domain)
Marc Zyngier651e8b52016-04-11 09:57:51 +0100753 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
Marc Zyngier530cbe12016-01-26 13:52:25 +0000754 } else {
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100755 domain = irq_default_domain;
Marc Zyngier530cbe12016-01-26 13:52:25 +0000756 }
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100757
Grant Likely68700652012-02-14 14:06:53 -0700758 if (!domain) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800759 pr_warn("no irq domain found for %s !\n",
Marc Zyngierc0131f02015-10-13 12:51:34 +0100760 of_node_full_name(to_of_node(fwspec->fwnode)));
Grant Likely03848372012-02-14 14:06:52 -0700761 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700762 }
763
Marc Zyngierc0131f02015-10-13 12:51:34 +0100764 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100765 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700766
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100767 /*
768 * WARN if the irqchip returns a type with bits
769 * outside the sense mask set and clear these bits.
770 */
771 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
772 type &= IRQ_TYPE_SENSE_MASK;
773
774 /*
775 * If we've already configured this interrupt,
776 * don't do it again, or hell will break loose.
777 */
778 virq = irq_find_mapping(domain, hwirq);
779 if (virq) {
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800780 /*
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100781 * If the trigger type is not specified or matches the
782 * current trigger type then we are done so return the
783 * interrupt number.
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800784 */
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100785 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800786 return virq;
787
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100788 /*
789 * If the trigger type has not been set yet, then set
790 * it now and return the interrupt number.
791 */
792 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100793 irq_data = irq_get_irq_data(virq);
794 if (!irq_data)
795 return 0;
796
797 irqd_set_trigger_type(irq_data, type);
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100798 return virq;
799 }
800
801 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
802 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
803 return 0;
804 }
805
806 if (irq_domain_is_hierarchy(domain)) {
Marc Zyngierc0131f02015-10-13 12:51:34 +0100807 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800808 if (virq <= 0)
809 return 0;
810 } else {
811 /* Create mapping */
812 virq = irq_create_mapping(domain, hwirq);
813 if (!virq)
814 return virq;
815 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700816
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100817 irq_data = irq_get_irq_data(virq);
818 if (!irq_data) {
819 if (irq_domain_is_hierarchy(domain))
820 irq_domain_free_irqs(virq, 1);
821 else
822 irq_dispose_mapping(virq);
823 return 0;
824 }
825
826 /* Store trigger type */
827 irqd_set_trigger_type(irq_data, type);
828
Grant Likelycc79ca62012-02-16 01:37:49 -0700829 return virq;
830}
Marc Zyngierc0131f02015-10-13 12:51:34 +0100831EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
832
833unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
834{
835 struct irq_fwspec fwspec;
836
837 of_phandle_args_to_fwspec(irq_data, &fwspec);
838 return irq_create_fwspec_mapping(&fwspec);
839}
Grant Likelycc79ca62012-02-16 01:37:49 -0700840EXPORT_SYMBOL_GPL(irq_create_of_mapping);
841
842/**
843 * irq_dispose_mapping() - Unmap an interrupt
844 * @virq: linux irq number of the interrupt to unmap
845 */
846void irq_dispose_mapping(unsigned int virq)
847{
848 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700849 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700850
Grant Likely03848372012-02-14 14:06:52 -0700851 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700852 return;
853
Grant Likely68700652012-02-14 14:06:53 -0700854 domain = irq_data->domain;
855 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700856 return;
857
Jon Hunterd16dcd3d2016-06-21 10:23:22 +0100858 if (irq_domain_is_hierarchy(domain)) {
859 irq_domain_free_irqs(virq, 1);
860 } else {
861 irq_domain_disassociate(domain, virq);
862 irq_free_desc(virq);
863 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700864}
865EXPORT_SYMBOL_GPL(irq_dispose_mapping);
866
867/**
868 * irq_find_mapping() - Find a linux irq from an hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700869 * @domain: domain owning this hardware interrupt
870 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700871 */
Grant Likely68700652012-02-14 14:06:53 -0700872unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700873 irq_hw_number_t hwirq)
874{
Grant Likely4c0946c2012-06-03 22:04:39 -0700875 struct irq_data *data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700876
Grant Likely68700652012-02-14 14:06:53 -0700877 /* Look for default domain if nececssary */
878 if (domain == NULL)
879 domain = irq_default_domain;
880 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700881 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700882
Grant Likely1aa0dd92013-06-08 12:03:59 +0100883 if (hwirq < domain->revmap_direct_max_irq) {
Jiang Liuf8264e32014-11-06 22:20:14 +0800884 data = irq_domain_get_irq_data(domain, hwirq);
885 if (data && data->hwirq == hwirq)
Grant Likely4c0946c2012-06-03 22:04:39 -0700886 return hwirq;
Grant Likely4c0946c2012-06-03 22:04:39 -0700887 }
888
Grant Likelyd3dcb432013-06-10 12:19:17 +0100889 /* Check if the hwirq is in the linear revmap. */
890 if (hwirq < domain->revmap_size)
891 return domain->linear_revmap[hwirq];
892
893 rcu_read_lock();
894 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
895 rcu_read_unlock();
896 return data ? data->irq : 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700897}
898EXPORT_SYMBOL_GPL(irq_find_mapping);
899
Grant Likely092b2fb2012-03-29 14:10:30 -0600900#ifdef CONFIG_IRQ_DOMAIN_DEBUG
Marc Zyngierfe17a422017-05-12 12:55:35 +0100901static void virq_debug_show_one(struct seq_file *m, struct irq_desc *desc)
902{
903 struct irq_domain *domain;
904 struct irq_data *data;
905
906 domain = desc->irq_data.domain;
907 data = &desc->irq_data;
908
909 while (domain) {
910 unsigned int irq = data->irq;
911 unsigned long hwirq = data->hwirq;
912 struct irq_chip *chip;
913 bool direct;
914
915 if (data == &desc->irq_data)
916 seq_printf(m, "%5d ", irq);
917 else
918 seq_printf(m, "%5d+ ", irq);
919 seq_printf(m, "0x%05lx ", hwirq);
920
921 chip = irq_data_get_irq_chip(data);
922 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
923
924 seq_printf(m, data ? "0x%p " : " %p ",
925 irq_data_get_irq_chip_data(data));
926
927 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
928 direct = (irq == hwirq) && (irq < domain->revmap_direct_max_irq);
929 seq_printf(m, "%6s%-8s ",
930 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
931 direct ? "(DIRECT)" : "");
932 seq_printf(m, "%s\n", domain->name);
933#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
934 domain = domain->parent;
935 data = data->parent_data;
936#else
937 domain = NULL;
938#endif
939 }
940}
941
Grant Likelycc79ca62012-02-16 01:37:49 -0700942static int virq_debug_show(struct seq_file *m, void *private)
943{
944 unsigned long flags;
945 struct irq_desc *desc;
Grant Likely1400ea82013-06-06 22:20:44 +0100946 struct irq_domain *domain;
947 struct radix_tree_iter iter;
Marc Zyngierfe17a422017-05-12 12:55:35 +0100948 void **slot;
Grant Likelycc79ca62012-02-16 01:37:49 -0700949 int i;
950
Grant Likely1400ea82013-06-06 22:20:44 +0100951 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
952 "name", "mapped", "linear-max", "direct-max", "devtree-node");
953 mutex_lock(&irq_domain_mutex);
954 list_for_each_entry(domain, &irq_domain_list, link) {
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100955 struct device_node *of_node;
Marc Zyngier2370c002017-05-12 12:55:36 +0100956 const char *name;
957
Grant Likely1400ea82013-06-06 22:20:44 +0100958 int count = 0;
Marc Zyngier2370c002017-05-12 12:55:36 +0100959
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100960 of_node = irq_domain_get_of_node(domain);
Marc Zyngier2370c002017-05-12 12:55:36 +0100961 if (of_node)
962 name = of_node_full_name(of_node);
963 else if (is_fwnode_irqchip(domain->fwnode))
964 name = container_of(domain->fwnode, struct irqchip_fwid,
965 fwnode)->name;
966 else
967 name = "";
968
Grant Likely1400ea82013-06-06 22:20:44 +0100969 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
970 count++;
971 seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
972 domain == irq_default_domain ? '*' : ' ', domain->name,
973 domain->revmap_size + count, domain->revmap_size,
974 domain->revmap_direct_max_irq,
Marc Zyngier2370c002017-05-12 12:55:36 +0100975 name);
Grant Likely1400ea82013-06-06 22:20:44 +0100976 }
977 mutex_unlock(&irq_domain_mutex);
978
979 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
Grant Likely5269a9a2012-04-12 14:42:15 -0600980 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
Grant Likely1400ea82013-06-06 22:20:44 +0100981 "active", "type", "domain");
Grant Likelycc79ca62012-02-16 01:37:49 -0700982
983 for (i = 1; i < nr_irqs; i++) {
984 desc = irq_to_desc(i);
985 if (!desc)
986 continue;
987
988 raw_spin_lock_irqsave(&desc->lock, flags);
Marc Zyngierfe17a422017-05-12 12:55:35 +0100989 virq_debug_show_one(m, desc);
Grant Likelycc79ca62012-02-16 01:37:49 -0700990 raw_spin_unlock_irqrestore(&desc->lock, flags);
991 }
992
993 return 0;
994}
995
996static int virq_debug_open(struct inode *inode, struct file *file)
997{
998 return single_open(file, virq_debug_show, inode->i_private);
999}
1000
1001static const struct file_operations virq_debug_fops = {
1002 .open = virq_debug_open,
1003 .read = seq_read,
1004 .llseek = seq_lseek,
1005 .release = single_release,
1006};
1007
1008static int __init irq_debugfs_init(void)
1009{
Grant Likely092b2fb2012-03-29 14:10:30 -06001010 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
Grant Likelycc79ca62012-02-16 01:37:49 -07001011 NULL, &virq_debug_fops) == NULL)
1012 return -ENOMEM;
1013
1014 return 0;
1015}
1016__initcall(irq_debugfs_init);
Grant Likely092b2fb2012-03-29 14:10:30 -06001017#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
Grant Likelycc79ca62012-02-16 01:37:49 -07001018
Grant Likely16b2e6e2012-01-26 11:26:52 -07001019/**
1020 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
1021 *
1022 * Device Tree IRQ specifier translation function which works with one cell
1023 * bindings where the cell value maps directly to the hwirq number.
1024 */
1025int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
1026 const u32 *intspec, unsigned int intsize,
1027 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -06001028{
Grant Likely16b2e6e2012-01-26 11:26:52 -07001029 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -06001030 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -06001031 *out_hwirq = intspec[0];
1032 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -06001033 return 0;
1034}
Grant Likely16b2e6e2012-01-26 11:26:52 -07001035EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
1036
1037/**
1038 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
1039 *
1040 * Device Tree IRQ specifier translation function which works with two cell
1041 * bindings where the cell values map directly to the hwirq number
1042 * and linux irq flags.
1043 */
1044int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1045 const u32 *intspec, unsigned int intsize,
1046 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1047{
1048 if (WARN_ON(intsize < 2))
1049 return -EINVAL;
1050 *out_hwirq = intspec[0];
1051 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1052 return 0;
1053}
1054EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1055
1056/**
1057 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1058 *
1059 * Device Tree IRQ specifier translation function which works with either one
1060 * or two cell bindings where the cell values map directly to the hwirq number
1061 * and linux irq flags.
1062 *
1063 * Note: don't use this function unless your interrupt controller explicitly
1064 * supports both one and two cell bindings. For the majority of controllers
1065 * the _onecell() or _twocell() variants above should be used.
1066 */
1067int irq_domain_xlate_onetwocell(struct irq_domain *d,
1068 struct device_node *ctrlr,
1069 const u32 *intspec, unsigned int intsize,
1070 unsigned long *out_hwirq, unsigned int *out_type)
1071{
1072 if (WARN_ON(intsize < 1))
1073 return -EINVAL;
1074 *out_hwirq = intspec[0];
Sebastian Frias0c228912016-08-02 10:52:45 +02001075 if (intsize > 1)
1076 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1077 else
1078 *out_type = IRQ_TYPE_NONE;
Grant Likely16b2e6e2012-01-26 11:26:52 -07001079 return 0;
1080}
1081EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -06001082
Grant Likelya18dc812012-01-26 12:12:14 -07001083const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely16b2e6e2012-01-26 11:26:52 -07001084 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -07001085};
1086EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Jiang Liuf8264e32014-11-06 22:20:14 +08001087
Qais Yousefac0a0cd2015-12-08 13:20:18 +00001088int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001089 int node, const struct cpumask *affinity)
Jiang Liuf8264e32014-11-06 22:20:14 +08001090{
1091 unsigned int hint;
1092
1093 if (virq >= 0) {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001094 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1095 affinity);
Jiang Liuf8264e32014-11-06 22:20:14 +08001096 } else {
1097 hint = hwirq % nr_irqs;
1098 if (hint == 0)
1099 hint++;
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001100 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1101 affinity);
1102 if (virq <= 0 && hint > 1) {
1103 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1104 affinity);
1105 }
Jiang Liuf8264e32014-11-06 22:20:14 +08001106 }
1107
1108 return virq;
1109}
1110
1111#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
Jiang Liuafb7da82014-11-15 22:24:02 +08001112/**
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001113 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
Jiang Liuafb7da82014-11-15 22:24:02 +08001114 * @parent: Parent irq domain to associate with the new domain
1115 * @flags: Irq domain flags associated to the domain
1116 * @size: Size of the domain. See below
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001117 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liuafb7da82014-11-15 22:24:02 +08001118 * @ops: Pointer to the interrupt domain callbacks
1119 * @host_data: Controller private data pointer
1120 *
1121 * If @size is 0 a tree domain is created, otherwise a linear domain.
1122 *
1123 * If successful the parent is associated to the new domain and the
1124 * domain flags are set.
1125 * Returns pointer to IRQ domain, or NULL on failure.
1126 */
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001127struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
Jiang Liuafb7da82014-11-15 22:24:02 +08001128 unsigned int flags,
1129 unsigned int size,
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001130 struct fwnode_handle *fwnode,
Jiang Liuafb7da82014-11-15 22:24:02 +08001131 const struct irq_domain_ops *ops,
1132 void *host_data)
1133{
1134 struct irq_domain *domain;
1135
1136 if (size)
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001137 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
Jiang Liuafb7da82014-11-15 22:24:02 +08001138 else
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001139 domain = irq_domain_create_tree(fwnode, ops, host_data);
Jiang Liuafb7da82014-11-15 22:24:02 +08001140 if (domain) {
1141 domain->parent = parent;
1142 domain->flags |= flags;
1143 }
1144
1145 return domain;
1146}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001147EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
Jiang Liuafb7da82014-11-15 22:24:02 +08001148
Jiang Liuf8264e32014-11-06 22:20:14 +08001149static void irq_domain_insert_irq(int virq)
1150{
1151 struct irq_data *data;
1152
1153 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1154 struct irq_domain *domain = data->domain;
Jiang Liuf8264e32014-11-06 22:20:14 +08001155
Thomas Gleixner9dc6be32017-06-20 01:37:16 +02001156 domain->mapcount++;
David Daneyb526adf2017-08-17 17:53:32 -07001157 irq_domain_set_mapping(domain, data->hwirq, data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001158
1159 /* If not already assigned, give the domain the chip's name */
1160 if (!domain->name && data->chip)
1161 domain->name = data->chip->name;
1162 }
1163
1164 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1165}
1166
1167static void irq_domain_remove_irq(int virq)
1168{
1169 struct irq_data *data;
1170
1171 irq_set_status_flags(virq, IRQ_NOREQUEST);
1172 irq_set_chip_and_handler(virq, NULL, NULL);
1173 synchronize_irq(virq);
1174 smp_mb();
1175
1176 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1177 struct irq_domain *domain = data->domain;
1178 irq_hw_number_t hwirq = data->hwirq;
1179
Thomas Gleixner9dc6be32017-06-20 01:37:16 +02001180 domain->mapcount--;
David Daneyb526adf2017-08-17 17:53:32 -07001181 irq_domain_clear_mapping(domain, hwirq);
Jiang Liuf8264e32014-11-06 22:20:14 +08001182 }
1183}
1184
1185static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1186 struct irq_data *child)
1187{
1188 struct irq_data *irq_data;
1189
Jiang Liu67830112015-06-01 16:05:13 +08001190 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1191 irq_data_get_node(child));
Jiang Liuf8264e32014-11-06 22:20:14 +08001192 if (irq_data) {
1193 child->parent_data = irq_data;
1194 irq_data->irq = child->irq;
Jiang Liu0d0b4c82015-06-01 16:05:12 +08001195 irq_data->common = child->common;
Jiang Liuf8264e32014-11-06 22:20:14 +08001196 irq_data->domain = domain;
1197 }
1198
1199 return irq_data;
1200}
1201
1202static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1203{
1204 struct irq_data *irq_data, *tmp;
1205 int i;
1206
1207 for (i = 0; i < nr_irqs; i++) {
1208 irq_data = irq_get_irq_data(virq + i);
1209 tmp = irq_data->parent_data;
1210 irq_data->parent_data = NULL;
1211 irq_data->domain = NULL;
1212
1213 while (tmp) {
1214 irq_data = tmp;
1215 tmp = tmp->parent_data;
1216 kfree(irq_data);
1217 }
1218 }
1219}
1220
1221static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1222 unsigned int virq, unsigned int nr_irqs)
1223{
1224 struct irq_data *irq_data;
1225 struct irq_domain *parent;
1226 int i;
1227
1228 /* The outermost irq_data is embedded in struct irq_desc */
1229 for (i = 0; i < nr_irqs; i++) {
1230 irq_data = irq_get_irq_data(virq + i);
1231 irq_data->domain = domain;
1232
1233 for (parent = domain->parent; parent; parent = parent->parent) {
1234 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1235 if (!irq_data) {
1236 irq_domain_free_irq_data(virq, i + 1);
1237 return -ENOMEM;
1238 }
1239 }
1240 }
1241
1242 return 0;
1243}
1244
1245/**
1246 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1247 * @domain: domain to match
1248 * @virq: IRQ number to get irq_data
1249 */
1250struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1251 unsigned int virq)
1252{
1253 struct irq_data *irq_data;
1254
1255 for (irq_data = irq_get_irq_data(virq); irq_data;
1256 irq_data = irq_data->parent_data)
1257 if (irq_data->domain == domain)
1258 return irq_data;
1259
1260 return NULL;
1261}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001262EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001263
1264/**
1265 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1266 * @domain: Interrupt domain to match
1267 * @virq: IRQ number
1268 * @hwirq: The hwirq number
1269 * @chip: The associated interrupt chip
1270 * @chip_data: The associated chip data
1271 */
1272int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1273 irq_hw_number_t hwirq, struct irq_chip *chip,
1274 void *chip_data)
1275{
1276 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1277
1278 if (!irq_data)
1279 return -ENOENT;
1280
1281 irq_data->hwirq = hwirq;
1282 irq_data->chip = chip ? chip : &no_irq_chip;
1283 irq_data->chip_data = chip_data;
1284
1285 return 0;
1286}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001287EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
Jiang Liuf8264e32014-11-06 22:20:14 +08001288
1289/**
Jiang Liu1b537702014-11-09 23:10:24 +08001290 * irq_domain_set_info - Set the complete data for a @virq in @domain
1291 * @domain: Interrupt domain to match
1292 * @virq: IRQ number
1293 * @hwirq: The hardware interrupt number
1294 * @chip: The associated interrupt chip
1295 * @chip_data: The associated interrupt chip data
1296 * @handler: The interrupt flow handler
1297 * @handler_data: The interrupt flow handler data
1298 * @handler_name: The interrupt handler name
1299 */
1300void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1301 irq_hw_number_t hwirq, struct irq_chip *chip,
1302 void *chip_data, irq_flow_handler_t handler,
1303 void *handler_data, const char *handler_name)
1304{
1305 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1306 __irq_set_handler(virq, handler, 0, handler_name);
1307 irq_set_handler_data(virq, handler_data);
1308}
Keith Busch64bce3e2016-01-12 13:18:07 -07001309EXPORT_SYMBOL(irq_domain_set_info);
Jiang Liu1b537702014-11-09 23:10:24 +08001310
1311/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001312 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1313 * @irq_data: The pointer to irq_data
1314 */
1315void irq_domain_reset_irq_data(struct irq_data *irq_data)
1316{
1317 irq_data->hwirq = 0;
1318 irq_data->chip = &no_irq_chip;
1319 irq_data->chip_data = NULL;
1320}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001321EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001322
1323/**
1324 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1325 * @domain: Interrupt domain to match
1326 * @virq: IRQ number to start with
1327 * @nr_irqs: The number of irqs to free
1328 */
1329void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1330 unsigned int nr_irqs)
1331{
1332 struct irq_data *irq_data;
1333 int i;
1334
1335 for (i = 0; i < nr_irqs; i++) {
1336 irq_data = irq_domain_get_irq_data(domain, virq + i);
1337 if (irq_data)
1338 irq_domain_reset_irq_data(irq_data);
1339 }
1340 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1341}
Axel Lin63cc7872016-03-17 12:00:31 +08001342EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
Jiang Liuf8264e32014-11-06 22:20:14 +08001343
1344/**
1345 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1346 * @domain: Interrupt domain to match
1347 * @virq: IRQ number to start with
1348 * @nr_irqs: The number of irqs to free
1349 */
1350void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1351 unsigned int nr_irqs)
1352{
1353 int i;
1354
1355 for (i = 0; i < nr_irqs; i++) {
1356 irq_set_handler_data(virq + i, NULL);
1357 irq_set_handler(virq + i, NULL);
1358 }
1359 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1360}
1361
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001362static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
Jiang Liu36d72732014-11-15 22:24:01 +08001363 unsigned int irq_base,
1364 unsigned int nr_irqs)
1365{
David Daney0d12ec02017-08-17 17:53:33 -07001366 if (domain->ops->free)
1367 domain->ops->free(domain, irq_base, nr_irqs);
Jiang Liu36d72732014-11-15 22:24:01 +08001368}
1369
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001370int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
Marc Zyngierc4665952015-11-23 08:26:04 +00001371 unsigned int irq_base,
1372 unsigned int nr_irqs, void *arg)
Jiang Liu36d72732014-11-15 22:24:01 +08001373{
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001374 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
Jiang Liu36d72732014-11-15 22:24:01 +08001375}
1376
Jiang Liuf8264e32014-11-06 22:20:14 +08001377/**
1378 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1379 * @domain: domain to allocate from
1380 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
1381 * @nr_irqs: number of IRQs to allocate
1382 * @node: NUMA node id for memory allocation
1383 * @arg: domain specific argument
1384 * @realloc: IRQ descriptors have already been allocated if true
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001385 * @affinity: Optional irq affinity mask for multiqueue devices
Jiang Liuf8264e32014-11-06 22:20:14 +08001386 *
1387 * Allocate IRQ numbers and initialized all data structures to support
1388 * hierarchy IRQ domains.
1389 * Parameter @realloc is mainly to support legacy IRQs.
1390 * Returns error code or allocated IRQ number
1391 *
1392 * The whole process to setup an IRQ has been split into two steps.
1393 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1394 * descriptor and required hardware resources. The second step,
1395 * irq_domain_activate_irq(), is to program hardwares with preallocated
1396 * resources. In this way, it's easier to rollback when failing to
1397 * allocate resources.
1398 */
1399int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1400 unsigned int nr_irqs, int node, void *arg,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001401 bool realloc, const struct cpumask *affinity)
Jiang Liuf8264e32014-11-06 22:20:14 +08001402{
1403 int i, ret, virq;
1404
1405 if (domain == NULL) {
1406 domain = irq_default_domain;
1407 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1408 return -EINVAL;
1409 }
1410
1411 if (!domain->ops->alloc) {
1412 pr_debug("domain->ops->alloc() is NULL\n");
1413 return -ENOSYS;
1414 }
1415
1416 if (realloc && irq_base >= 0) {
1417 virq = irq_base;
1418 } else {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001419 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1420 affinity);
Jiang Liuf8264e32014-11-06 22:20:14 +08001421 if (virq < 0) {
1422 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1423 irq_base, nr_irqs);
1424 return virq;
1425 }
1426 }
1427
1428 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1429 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1430 ret = -ENOMEM;
1431 goto out_free_desc;
1432 }
1433
1434 mutex_lock(&irq_domain_mutex);
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001435 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
Jiang Liuf8264e32014-11-06 22:20:14 +08001436 if (ret < 0) {
1437 mutex_unlock(&irq_domain_mutex);
1438 goto out_free_irq_data;
1439 }
1440 for (i = 0; i < nr_irqs; i++)
1441 irq_domain_insert_irq(virq + i);
1442 mutex_unlock(&irq_domain_mutex);
1443
1444 return virq;
1445
1446out_free_irq_data:
1447 irq_domain_free_irq_data(virq, nr_irqs);
1448out_free_desc:
1449 irq_free_descs(virq, nr_irqs);
1450 return ret;
1451}
1452
David Daney495c38d2017-08-17 17:53:34 -07001453/* The irq_data was moved, fix the revmap to refer to the new location */
1454static void irq_domain_fix_revmap(struct irq_data *d)
1455{
1456 void **slot;
1457
1458 if (d->hwirq < d->domain->revmap_size)
1459 return; /* Not using radix tree. */
1460
1461 /* Fix up the revmap. */
1462 mutex_lock(&revmap_trees_mutex);
1463 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1464 if (slot)
1465 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1466 mutex_unlock(&revmap_trees_mutex);
1467}
1468
1469/**
1470 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1471 * @domain: Domain to push.
1472 * @virq: Irq to push the domain in to.
1473 * @arg: Passed to the irq_domain_ops alloc() function.
1474 *
1475 * For an already existing irqdomain hierarchy, as might be obtained
1476 * via a call to pci_enable_msix(), add an additional domain to the
1477 * head of the processing chain. Must be called before request_irq()
1478 * has been called.
1479 */
1480int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1481{
1482 struct irq_data *child_irq_data;
1483 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1484 struct irq_desc *desc;
1485 int rv = 0;
1486
1487 /*
1488 * Check that no action has been set, which indicates the virq
1489 * is in a state where this function doesn't have to deal with
1490 * races between interrupt handling and maintaining the
1491 * hierarchy. This will catch gross misuse. Attempting to
1492 * make the check race free would require holding locks across
1493 * calls to struct irq_domain_ops->alloc(), which could lead
1494 * to deadlock, so we just do a simple check before starting.
1495 */
1496 desc = irq_to_desc(virq);
1497 if (!desc)
1498 return -EINVAL;
1499 if (WARN_ON(desc->action))
1500 return -EBUSY;
1501
1502 if (domain == NULL)
1503 return -EINVAL;
1504
1505 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1506 return -EINVAL;
1507
Dan Carpenter20c4d492017-08-25 15:14:09 +03001508 if (!root_irq_data)
David Daney495c38d2017-08-17 17:53:34 -07001509 return -EINVAL;
1510
Dan Carpenter20c4d492017-08-25 15:14:09 +03001511 if (domain->parent != root_irq_data->domain)
David Daney495c38d2017-08-17 17:53:34 -07001512 return -EINVAL;
1513
1514 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1515 irq_data_get_node(root_irq_data));
1516 if (!child_irq_data)
1517 return -ENOMEM;
1518
1519 mutex_lock(&irq_domain_mutex);
1520
1521 /* Copy the original irq_data. */
1522 *child_irq_data = *root_irq_data;
1523
1524 /*
1525 * Overwrite the root_irq_data, which is embedded in struct
1526 * irq_desc, with values for this domain.
1527 */
1528 root_irq_data->parent_data = child_irq_data;
1529 root_irq_data->domain = domain;
1530 root_irq_data->mask = 0;
1531 root_irq_data->hwirq = 0;
1532 root_irq_data->chip = NULL;
1533 root_irq_data->chip_data = NULL;
1534
1535 /* May (probably does) set hwirq, chip, etc. */
1536 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1537 if (rv) {
1538 /* Restore the original irq_data. */
1539 *root_irq_data = *child_irq_data;
1540 goto error;
1541 }
1542
1543 irq_domain_fix_revmap(child_irq_data);
1544 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1545
1546error:
1547 mutex_unlock(&irq_domain_mutex);
1548
1549 return rv;
1550}
1551EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1552
1553/**
1554 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1555 * @domain: Domain to remove.
1556 * @virq: Irq to remove the domain from.
1557 *
1558 * Undo the effects of a call to irq_domain_push_irq(). Must be
1559 * called either before request_irq() or after free_irq().
1560 */
1561int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1562{
1563 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1564 struct irq_data *child_irq_data;
1565 struct irq_data *tmp_irq_data;
1566 struct irq_desc *desc;
1567
1568 /*
1569 * Check that no action is set, which indicates the virq is in
1570 * a state where this function doesn't have to deal with races
1571 * between interrupt handling and maintaining the hierarchy.
1572 * This will catch gross misuse. Attempting to make the check
1573 * race free would require holding locks across calls to
1574 * struct irq_domain_ops->free(), which could lead to
1575 * deadlock, so we just do a simple check before starting.
1576 */
1577 desc = irq_to_desc(virq);
1578 if (!desc)
1579 return -EINVAL;
1580 if (WARN_ON(desc->action))
1581 return -EBUSY;
1582
1583 if (domain == NULL)
1584 return -EINVAL;
1585
1586 if (!root_irq_data)
1587 return -EINVAL;
1588
1589 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1590
1591 /* We can only "pop" if this domain is at the top of the list */
1592 if (WARN_ON(root_irq_data != tmp_irq_data))
1593 return -EINVAL;
1594
1595 if (WARN_ON(root_irq_data->domain != domain))
1596 return -EINVAL;
1597
1598 child_irq_data = root_irq_data->parent_data;
1599 if (WARN_ON(!child_irq_data))
1600 return -EINVAL;
1601
1602 mutex_lock(&irq_domain_mutex);
1603
1604 root_irq_data->parent_data = NULL;
1605
1606 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1607 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1608
1609 /* Restore the original irq_data. */
1610 *root_irq_data = *child_irq_data;
1611
1612 irq_domain_fix_revmap(root_irq_data);
1613
1614 mutex_unlock(&irq_domain_mutex);
1615
1616 kfree(child_irq_data);
1617
1618 return 0;
1619}
1620EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1621
Jiang Liuf8264e32014-11-06 22:20:14 +08001622/**
1623 * irq_domain_free_irqs - Free IRQ number and associated data structures
1624 * @virq: base IRQ number
1625 * @nr_irqs: number of IRQs to free
1626 */
1627void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1628{
1629 struct irq_data *data = irq_get_irq_data(virq);
1630 int i;
1631
1632 if (WARN(!data || !data->domain || !data->domain->ops->free,
1633 "NULL pointer, cannot free irq\n"))
1634 return;
1635
1636 mutex_lock(&irq_domain_mutex);
1637 for (i = 0; i < nr_irqs; i++)
1638 irq_domain_remove_irq(virq + i);
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001639 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
Jiang Liuf8264e32014-11-06 22:20:14 +08001640 mutex_unlock(&irq_domain_mutex);
1641
1642 irq_domain_free_irq_data(virq, nr_irqs);
1643 irq_free_descs(virq, nr_irqs);
1644}
1645
1646/**
Jiang Liu36d72732014-11-15 22:24:01 +08001647 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1648 * @irq_base: Base IRQ number
1649 * @nr_irqs: Number of IRQs to allocate
1650 * @arg: Allocation data (arch/domain specific)
1651 *
1652 * Check whether the domain has been setup recursive. If not allocate
1653 * through the parent domain.
1654 */
1655int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1656 unsigned int irq_base, unsigned int nr_irqs,
1657 void *arg)
1658{
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001659 if (!domain->parent)
1660 return -ENOSYS;
Jiang Liu36d72732014-11-15 22:24:01 +08001661
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001662 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1663 nr_irqs, arg);
Jiang Liu36d72732014-11-15 22:24:01 +08001664}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001665EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
Jiang Liu36d72732014-11-15 22:24:01 +08001666
1667/**
1668 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1669 * @irq_base: Base IRQ number
1670 * @nr_irqs: Number of IRQs to free
1671 *
1672 * Check whether the domain has been setup recursive. If not free
1673 * through the parent domain.
1674 */
1675void irq_domain_free_irqs_parent(struct irq_domain *domain,
1676 unsigned int irq_base, unsigned int nr_irqs)
1677{
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001678 if (!domain->parent)
1679 return;
1680
1681 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
Jiang Liu36d72732014-11-15 22:24:01 +08001682}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001683EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
Jiang Liu36d72732014-11-15 22:24:01 +08001684
Marc Zyngier08d85f32017-01-17 16:00:48 +00001685static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1686{
1687 if (irq_data && irq_data->domain) {
1688 struct irq_domain *domain = irq_data->domain;
1689
1690 if (domain->ops->deactivate)
1691 domain->ops->deactivate(domain, irq_data);
1692 if (irq_data->parent_data)
1693 __irq_domain_deactivate_irq(irq_data->parent_data);
1694 }
1695}
1696
Thomas Gleixner42e1cc22017-09-13 23:29:12 +02001697static int __irq_domain_activate_irq(struct irq_data *irqd, bool early)
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001698{
1699 int ret = 0;
1700
1701 if (irqd && irqd->domain) {
1702 struct irq_domain *domain = irqd->domain;
1703
1704 if (irqd->parent_data)
Thomas Gleixner42e1cc22017-09-13 23:29:12 +02001705 ret = __irq_domain_activate_irq(irqd->parent_data,
1706 early);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001707 if (!ret && domain->ops->activate) {
Thomas Gleixner42e1cc22017-09-13 23:29:12 +02001708 ret = domain->ops->activate(domain, irqd, early);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001709 /* Rollback in case of error */
1710 if (ret && irqd->parent_data)
1711 __irq_domain_deactivate_irq(irqd->parent_data);
1712 }
1713 }
1714 return ret;
1715}
1716
Jiang Liu36d72732014-11-15 22:24:01 +08001717/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001718 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1719 * interrupt
1720 * @irq_data: outermost irq_data associated with interrupt
1721 *
1722 * This is the second step to call domain_ops->activate to program interrupt
1723 * controllers, so the interrupt could actually get delivered.
1724 */
Thomas Gleixner42e1cc22017-09-13 23:29:12 +02001725int irq_domain_activate_irq(struct irq_data *irq_data, bool early)
Jiang Liuf8264e32014-11-06 22:20:14 +08001726{
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001727 int ret = 0;
1728
1729 if (!irqd_is_activated(irq_data))
Thomas Gleixner42e1cc22017-09-13 23:29:12 +02001730 ret = __irq_domain_activate_irq(irq_data, early);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001731 if (!ret)
Marc Zyngier08d85f32017-01-17 16:00:48 +00001732 irqd_set_activated(irq_data);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001733 return ret;
Jiang Liuf8264e32014-11-06 22:20:14 +08001734}
1735
1736/**
1737 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1738 * deactivate interrupt
1739 * @irq_data: outermost irq_data associated with interrupt
1740 *
1741 * It calls domain_ops->deactivate to program interrupt controllers to disable
1742 * interrupt delivery.
1743 */
1744void irq_domain_deactivate_irq(struct irq_data *irq_data)
1745{
Marc Zyngier08d85f32017-01-17 16:00:48 +00001746 if (irqd_is_activated(irq_data)) {
1747 __irq_domain_deactivate_irq(irq_data);
1748 irqd_clr_activated(irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001749 }
1750}
1751
1752static void irq_domain_check_hierarchy(struct irq_domain *domain)
1753{
1754 /* Hierarchy irq_domains must implement callback alloc() */
1755 if (domain->ops->alloc)
1756 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1757}
Eric Auger631a9632017-01-19 20:57:57 +00001758
1759/**
1760 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1761 * parent has MSI remapping support
1762 * @domain: domain pointer
1763 */
1764bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1765{
1766 for (; domain; domain = domain->parent) {
1767 if (irq_domain_is_msi_remap(domain))
1768 return true;
1769 }
1770 return false;
1771}
Jiang Liuf8264e32014-11-06 22:20:14 +08001772#else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1773/**
1774 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1775 * @domain: domain to match
1776 * @virq: IRQ number to get irq_data
1777 */
1778struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1779 unsigned int virq)
1780{
1781 struct irq_data *irq_data = irq_get_irq_data(virq);
1782
1783 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1784}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001785EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001786
Stefan Agner5f22f5c2015-05-16 11:44:13 +02001787/**
1788 * irq_domain_set_info - Set the complete data for a @virq in @domain
1789 * @domain: Interrupt domain to match
1790 * @virq: IRQ number
1791 * @hwirq: The hardware interrupt number
1792 * @chip: The associated interrupt chip
1793 * @chip_data: The associated interrupt chip data
1794 * @handler: The interrupt flow handler
1795 * @handler_data: The interrupt flow handler data
1796 * @handler_name: The interrupt handler name
1797 */
1798void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1799 irq_hw_number_t hwirq, struct irq_chip *chip,
1800 void *chip_data, irq_flow_handler_t handler,
1801 void *handler_data, const char *handler_name)
1802{
1803 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1804 irq_set_chip_data(virq, chip_data);
1805 irq_set_handler_data(virq, handler_data);
1806}
1807
Jiang Liuf8264e32014-11-06 22:20:14 +08001808static void irq_domain_check_hierarchy(struct irq_domain *domain)
1809{
1810}
1811#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001812
1813#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1814static struct dentry *domain_dir;
1815
1816static void
1817irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1818{
1819 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1820 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1821 d->revmap_size + d->revmap_direct_max_irq);
1822 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1823 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
Thomas Gleixnerc3e72392017-09-13 23:29:06 +02001824 if (d->ops && d->ops->debug_show)
1825 d->ops->debug_show(m, d, NULL, ind + 1);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001826#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1827 if (!d->parent)
1828 return;
1829 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1830 irq_domain_debug_show_one(m, d->parent, ind + 4);
1831#endif
1832}
1833
1834static int irq_domain_debug_show(struct seq_file *m, void *p)
1835{
1836 struct irq_domain *d = m->private;
1837
1838 /* Default domain? Might be NULL */
1839 if (!d) {
1840 if (!irq_default_domain)
1841 return 0;
1842 d = irq_default_domain;
1843 }
1844 irq_domain_debug_show_one(m, d, 0);
1845 return 0;
1846}
1847
1848static int irq_domain_debug_open(struct inode *inode, struct file *file)
1849{
1850 return single_open(file, irq_domain_debug_show, inode->i_private);
1851}
1852
1853static const struct file_operations dfs_domain_ops = {
1854 .open = irq_domain_debug_open,
1855 .read = seq_read,
1856 .llseek = seq_lseek,
1857 .release = single_release,
1858};
1859
1860static void debugfs_add_domain_dir(struct irq_domain *d)
1861{
1862 if (!d->name || !domain_dir || d->debugfs_file)
1863 return;
1864 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
1865 &dfs_domain_ops);
1866}
1867
1868static void debugfs_remove_domain_dir(struct irq_domain *d)
1869{
Thomas Gleixnerf610c9d2017-07-07 08:57:57 +02001870 debugfs_remove(d->debugfs_file);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001871}
1872
1873void __init irq_domain_debugfs_init(struct dentry *root)
1874{
1875 struct irq_domain *d;
1876
1877 domain_dir = debugfs_create_dir("domains", root);
1878 if (!domain_dir)
1879 return;
1880
1881 debugfs_create_file("default", 0444, domain_dir, NULL, &dfs_domain_ops);
1882 mutex_lock(&irq_domain_mutex);
1883 list_for_each_entry(d, &irq_domain_list, link)
1884 debugfs_add_domain_dir(d);
1885 mutex_unlock(&irq_domain_mutex);
1886}
1887#endif