Thomas Gleixner | 52a65ff | 2018-03-14 22:15:19 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 3 | #define pr_fmt(fmt) "irq: " fmt |
| 4 | |
Marc Zyngier | c5c601c | 2017-07-07 09:39:59 +0100 | [diff] [blame] | 5 | #include <linux/acpi.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 6 | #include <linux/debugfs.h> |
| 7 | #include <linux/hardirq.h> |
| 8 | #include <linux/interrupt.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 9 | #include <linux/irq.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 10 | #include <linux/irqdesc.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 11 | #include <linux/irqdomain.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/of.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 15 | #include <linux/of_address.h> |
Rashika Kheria | 64be38a | 2014-02-27 17:10:12 +0530 | [diff] [blame] | 16 | #include <linux/of_irq.h> |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 17 | #include <linux/topology.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 18 | #include <linux/seq_file.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 19 | #include <linux/slab.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 20 | #include <linux/smp.h> |
| 21 | #include <linux/fs.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 22 | |
| 23 | static LIST_HEAD(irq_domain_list); |
| 24 | static DEFINE_MUTEX(irq_domain_mutex); |
| 25 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 26 | static struct irq_domain *irq_default_domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 27 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 28 | static void irq_domain_check_hierarchy(struct irq_domain *domain); |
| 29 | |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 30 | struct irqchip_fwid { |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 31 | struct fwnode_handle fwnode; |
| 32 | unsigned int type; |
| 33 | char *name; |
Marc Zyngier | b977fcf | 2019-07-31 15:13:19 +0100 | [diff] [blame] | 34 | phys_addr_t *pa; |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 37 | #ifdef CONFIG_GENERIC_IRQ_DEBUGFS |
| 38 | static void debugfs_add_domain_dir(struct irq_domain *d); |
| 39 | static void debugfs_remove_domain_dir(struct irq_domain *d); |
| 40 | #else |
| 41 | static inline void debugfs_add_domain_dir(struct irq_domain *d) { } |
| 42 | static inline void debugfs_remove_domain_dir(struct irq_domain *d) { } |
| 43 | #endif |
| 44 | |
David Woodhouse | 2cbd5a4 | 2020-10-24 22:35:22 +0100 | [diff] [blame^] | 45 | static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode) |
| 46 | { |
| 47 | struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode); |
| 48 | |
| 49 | return fwid->name; |
| 50 | } |
| 51 | |
| 52 | const struct fwnode_operations irqchip_fwnode_ops = { |
| 53 | .get_name = irqchip_fwnode_get_name, |
| 54 | }; |
Arnd Bergmann | b6eb66f | 2017-07-26 02:19:35 +0200 | [diff] [blame] | 55 | EXPORT_SYMBOL_GPL(irqchip_fwnode_ops); |
Sakari Ailus | db3e50f | 2017-07-21 14:39:31 +0300 | [diff] [blame] | 56 | |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 57 | /** |
luanshi | b513df6 | 2020-03-03 09:48:45 +0800 | [diff] [blame] | 58 | * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 59 | * identifying an irq domain |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 60 | * @type: Type of irqchip_fwnode. See linux/irqdomain.h |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 61 | * @id: Optional user provided id if name != NULL |
luanshi | b513df6 | 2020-03-03 09:48:45 +0800 | [diff] [blame] | 62 | * @name: Optional user provided domain name |
Yi Wang | 0ed9ca25 | 2019-10-19 17:07:27 +0800 | [diff] [blame] | 63 | * @pa: Optional user-provided physical address |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 64 | * |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 65 | * Allocate a struct irqchip_fwid, and return a poiner to the embedded |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 66 | * fwnode_handle (or NULL on failure). |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 67 | * |
| 68 | * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are |
| 69 | * solely to transport name information to irqdomain creation code. The |
| 70 | * node is not stored. For other types the pointer is kept in the irq |
| 71 | * domain struct. |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 72 | */ |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 73 | struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id, |
Marc Zyngier | b977fcf | 2019-07-31 15:13:19 +0100 | [diff] [blame] | 74 | const char *name, |
| 75 | phys_addr_t *pa) |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 76 | { |
| 77 | struct irqchip_fwid *fwid; |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 78 | char *n; |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 79 | |
| 80 | fwid = kzalloc(sizeof(*fwid), GFP_KERNEL); |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 81 | |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 82 | switch (type) { |
| 83 | case IRQCHIP_FWNODE_NAMED: |
| 84 | n = kasprintf(GFP_KERNEL, "%s", name); |
| 85 | break; |
| 86 | case IRQCHIP_FWNODE_NAMED_ID: |
| 87 | n = kasprintf(GFP_KERNEL, "%s-%d", name, id); |
| 88 | break; |
| 89 | default: |
Marc Zyngier | b977fcf | 2019-07-31 15:13:19 +0100 | [diff] [blame] | 90 | n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa); |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | |
| 94 | if (!fwid || !n) { |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 95 | kfree(fwid); |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 96 | kfree(n); |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 100 | fwid->type = type; |
| 101 | fwid->name = n; |
Marc Zyngier | b977fcf | 2019-07-31 15:13:19 +0100 | [diff] [blame] | 102 | fwid->pa = pa; |
Sakari Ailus | db3e50f | 2017-07-21 14:39:31 +0300 | [diff] [blame] | 103 | fwid->fwnode.ops = &irqchip_fwnode_ops; |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 104 | return &fwid->fwnode; |
| 105 | } |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode); |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle |
| 110 | * |
| 111 | * Free a fwnode_handle allocated with irq_domain_alloc_fwnode. |
| 112 | */ |
| 113 | void irq_domain_free_fwnode(struct fwnode_handle *fwnode) |
| 114 | { |
| 115 | struct irqchip_fwid *fwid; |
| 116 | |
Suravee Suthikulpanit | 75aba7b | 2015-12-10 08:55:28 -0800 | [diff] [blame] | 117 | if (WARN_ON(!is_fwnode_irqchip(fwnode))) |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 118 | return; |
| 119 | |
| 120 | fwid = container_of(fwnode, struct irqchip_fwid, fwnode); |
| 121 | kfree(fwid->name); |
| 122 | kfree(fwid); |
| 123 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(irq_domain_free_fwnode); |
Marc Zyngier | b145dcc | 2015-10-13 12:51:36 +0100 | [diff] [blame] | 125 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 126 | /** |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 127 | * __irq_domain_add() - Allocate a new irq_domain data structure |
Punit Agrawal | 545d5d6 | 2016-05-31 13:56:48 +0100 | [diff] [blame] | 128 | * @fwnode: firmware node for the interrupt controller |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 129 | * @size: Size of linear map; 0 for radix mapping only |
Jiang Liu | a257954 | 2014-05-27 16:07:37 +0800 | [diff] [blame] | 130 | * @hwirq_max: Maximum number of interrupts supported by controller |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 131 | * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no |
| 132 | * direct mapping |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 133 | * @ops: domain callbacks |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 134 | * @host_data: Controller private data pointer |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 135 | * |
Zenghui Yu | 3a1d24c | 2019-07-06 04:41:12 +0000 | [diff] [blame] | 136 | * Allocates and initializes an irq_domain structure. |
Jiang Liu | a257954 | 2014-05-27 16:07:37 +0800 | [diff] [blame] | 137 | * Returns pointer to IRQ domain, or NULL on failure. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 138 | */ |
Marc Zyngier | 1bf4ddc | 2015-10-13 12:51:35 +0100 | [diff] [blame] | 139 | struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size, |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 140 | irq_hw_number_t hwirq_max, int direct_max, |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 141 | const struct irq_domain_ops *ops, |
| 142 | void *host_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 143 | { |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 144 | struct irqchip_fwid *fwid; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 145 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 146 | |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 147 | static atomic_t unknown_domains; |
| 148 | |
Grant Likely | cef5075 | 2012-07-11 17:24:31 +0100 | [diff] [blame] | 149 | domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size), |
Andy Shevchenko | 181e9d4 | 2020-05-20 19:49:25 +0300 | [diff] [blame] | 150 | GFP_KERNEL, of_node_to_nid(to_of_node(fwnode))); |
Geert Uytterhoeven | 43b98d8 | 2019-05-27 13:57:42 +0200 | [diff] [blame] | 151 | if (!domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 152 | return NULL; |
| 153 | |
Zenghui Yu | 45e9504 | 2020-07-16 16:39:05 +0800 | [diff] [blame] | 154 | if (is_fwnode_irqchip(fwnode)) { |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 155 | fwid = container_of(fwnode, struct irqchip_fwid, fwnode); |
| 156 | |
| 157 | switch (fwid->type) { |
| 158 | case IRQCHIP_FWNODE_NAMED: |
| 159 | case IRQCHIP_FWNODE_NAMED_ID: |
Dexuan Cui | 711419e | 2019-09-02 23:14:56 +0000 | [diff] [blame] | 160 | domain->fwnode = fwnode; |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 161 | domain->name = kstrdup(fwid->name, GFP_KERNEL); |
| 162 | if (!domain->name) { |
| 163 | kfree(domain); |
| 164 | return NULL; |
| 165 | } |
| 166 | domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; |
| 167 | break; |
| 168 | default: |
| 169 | domain->fwnode = fwnode; |
| 170 | domain->name = fwid->name; |
| 171 | break; |
| 172 | } |
Andy Shevchenko | 9ed78b0 | 2020-05-20 19:49:27 +0300 | [diff] [blame] | 173 | } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) || |
| 174 | is_software_node(fwnode)) { |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 175 | char *name; |
| 176 | |
| 177 | /* |
Andy Shevchenko | 181e9d4 | 2020-05-20 19:49:25 +0300 | [diff] [blame] | 178 | * fwnode paths contain '/', which debugfs is legitimately |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 179 | * unhappy about. Replace them with ':', which does |
| 180 | * the trick and is not as offensive as '\'... |
| 181 | */ |
Andy Shevchenko | 181e9d4 | 2020-05-20 19:49:25 +0300 | [diff] [blame] | 182 | name = kasprintf(GFP_KERNEL, "%pfw", fwnode); |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 183 | if (!name) { |
| 184 | kfree(domain); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | strreplace(name, '/', ':'); |
| 189 | |
| 190 | domain->name = name; |
| 191 | domain->fwnode = fwnode; |
| 192 | domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; |
| 193 | } |
| 194 | |
| 195 | if (!domain->name) { |
Sakari Ailus | db3e50f | 2017-07-21 14:39:31 +0300 | [diff] [blame] | 196 | if (fwnode) |
| 197 | pr_err("Invalid fwnode type for irqdomain\n"); |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 198 | domain->name = kasprintf(GFP_KERNEL, "unknown-%d", |
| 199 | atomic_inc_return(&unknown_domains)); |
| 200 | if (!domain->name) { |
| 201 | kfree(domain); |
| 202 | return NULL; |
| 203 | } |
| 204 | domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; |
| 205 | } |
| 206 | |
Andy Shevchenko | 181e9d4 | 2020-05-20 19:49:25 +0300 | [diff] [blame] | 207 | fwnode_handle_get(fwnode); |
Marc Zyngier | f110711 | 2015-10-13 12:51:30 +0100 | [diff] [blame] | 208 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 209 | /* Fill structure */ |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 210 | INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL); |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 211 | mutex_init(&domain->revmap_tree_mutex); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 212 | domain->ops = ops; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 213 | domain->host_data = host_data; |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 214 | domain->hwirq_max = hwirq_max; |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 215 | domain->revmap_size = size; |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 216 | domain->revmap_direct_max_irq = direct_max; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 217 | irq_domain_check_hierarchy(domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 218 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 219 | mutex_lock(&irq_domain_mutex); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 220 | debugfs_add_domain_dir(domain); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 221 | list_add(&domain->link, &irq_domain_list); |
| 222 | mutex_unlock(&irq_domain_mutex); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 223 | |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 224 | pr_debug("Added domain %s\n", domain->name); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 225 | return domain; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 226 | } |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(__irq_domain_add); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 228 | |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 229 | /** |
| 230 | * irq_domain_remove() - Remove an irq domain. |
| 231 | * @domain: domain to remove |
| 232 | * |
| 233 | * This routine is used to remove an irq domain. The caller must ensure |
| 234 | * that all mappings within the domain have been disposed of prior to |
| 235 | * use, depending on the revmap type. |
| 236 | */ |
| 237 | void irq_domain_remove(struct irq_domain *domain) |
| 238 | { |
| 239 | mutex_lock(&irq_domain_mutex); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 240 | debugfs_remove_domain_dir(domain); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 241 | |
Matthew Wilcox | e9256ef | 2016-05-20 17:01:33 -0700 | [diff] [blame] | 242 | WARN_ON(!radix_tree_empty(&domain->revmap_tree)); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 243 | |
| 244 | list_del(&domain->link); |
| 245 | |
| 246 | /* |
| 247 | * If the going away domain is the default one, reset it. |
| 248 | */ |
| 249 | if (unlikely(irq_default_domain == domain)) |
| 250 | irq_set_default_host(NULL); |
| 251 | |
| 252 | mutex_unlock(&irq_domain_mutex); |
| 253 | |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 254 | pr_debug("Removed domain %s\n", domain->name); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 255 | |
Andy Shevchenko | 181e9d4 | 2020-05-20 19:49:25 +0300 | [diff] [blame] | 256 | fwnode_handle_put(domain->fwnode); |
Thomas Gleixner | d59f661 | 2017-06-20 01:37:05 +0200 | [diff] [blame] | 257 | if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED) |
| 258 | kfree(domain->name); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 259 | kfree(domain); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 260 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 261 | EXPORT_SYMBOL_GPL(irq_domain_remove); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 262 | |
Marc Zyngier | 61d0a00 | 2017-06-22 11:34:57 +0100 | [diff] [blame] | 263 | void irq_domain_update_bus_token(struct irq_domain *domain, |
| 264 | enum irq_domain_bus_token bus_token) |
| 265 | { |
| 266 | char *name; |
| 267 | |
| 268 | if (domain->bus_token == bus_token) |
| 269 | return; |
| 270 | |
| 271 | mutex_lock(&irq_domain_mutex); |
| 272 | |
| 273 | domain->bus_token = bus_token; |
| 274 | |
| 275 | name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token); |
| 276 | if (!name) { |
| 277 | mutex_unlock(&irq_domain_mutex); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | debugfs_remove_domain_dir(domain); |
| 282 | |
| 283 | if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED) |
| 284 | kfree(domain->name); |
| 285 | else |
| 286 | domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; |
| 287 | |
| 288 | domain->name = name; |
| 289 | debugfs_add_domain_dir(domain); |
| 290 | |
| 291 | mutex_unlock(&irq_domain_mutex); |
| 292 | } |
John Stultz | 8a66792 | 2020-07-10 23:18:22 +0000 | [diff] [blame] | 293 | EXPORT_SYMBOL_GPL(irq_domain_update_bus_token); |
Marc Zyngier | 61d0a00 | 2017-06-22 11:34:57 +0100 | [diff] [blame] | 294 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 295 | /** |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 296 | * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 297 | * @of_node: pointer to interrupt controller's device tree node. |
| 298 | * @size: total number of irqs in mapping |
Linus Walleij | 94a63da | 2013-06-06 12:10:23 +0100 | [diff] [blame] | 299 | * @first_irq: first number of irq block assigned to the domain, |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 300 | * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then |
| 301 | * pre-map all of the irqs in the domain to virqs starting at first_irq. |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 302 | * @ops: domain callbacks |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 303 | * @host_data: Controller private data pointer |
| 304 | * |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 305 | * Allocates an irq_domain, and optionally if first_irq is positive then also |
| 306 | * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq. |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 307 | * |
| 308 | * This is intended to implement the expected behaviour for most |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 309 | * interrupt controllers. If device tree is used, then first_irq will be 0 and |
| 310 | * irqs get mapped dynamically on the fly. However, if the controller requires |
| 311 | * static virq assignments (non-DT boot) then it will set that up correctly. |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 312 | */ |
| 313 | struct irq_domain *irq_domain_add_simple(struct device_node *of_node, |
| 314 | unsigned int size, |
| 315 | unsigned int first_irq, |
| 316 | const struct irq_domain_ops *ops, |
| 317 | void *host_data) |
| 318 | { |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 319 | struct irq_domain *domain; |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 320 | |
Marc Zyngier | 1bf4ddc | 2015-10-13 12:51:35 +0100 | [diff] [blame] | 321 | domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 322 | if (!domain) |
| 323 | return NULL; |
| 324 | |
| 325 | if (first_irq > 0) { |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 326 | if (IS_ENABLED(CONFIG_SPARSE_IRQ)) { |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 327 | /* attempt to allocated irq_descs */ |
| 328 | int rc = irq_alloc_descs(first_irq, first_irq, size, |
| 329 | of_node_to_nid(of_node)); |
| 330 | if (rc < 0) |
Linus Walleij | d202b7b | 2012-11-27 01:20:32 +0100 | [diff] [blame] | 331 | pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n", |
| 332 | first_irq); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 333 | } |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 334 | irq_domain_associate_many(domain, first_irq, 0, size); |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 337 | return domain; |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 338 | } |
Arnd Bergmann | 346dbb7 | 2013-04-25 19:28:54 +0200 | [diff] [blame] | 339 | EXPORT_SYMBOL_GPL(irq_domain_add_simple); |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 340 | |
| 341 | /** |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 342 | * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain. |
| 343 | * @of_node: pointer to interrupt controller's device tree node. |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 344 | * @size: total number of irqs in legacy mapping |
| 345 | * @first_irq: first number of irq block assigned to the domain |
| 346 | * @first_hwirq: first hwirq number to use for the translation. Should normally |
| 347 | * be '0', but a positive integer can be used if the effective |
| 348 | * hwirqs numbering does not begin at zero. |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 349 | * @ops: map/unmap domain callbacks |
| 350 | * @host_data: Controller private data pointer |
| 351 | * |
| 352 | * Note: the map() callback will be called before this function returns |
| 353 | * for all legacy interrupts except 0 (which is always the invalid irq for |
| 354 | * a legacy controller). |
| 355 | */ |
| 356 | struct irq_domain *irq_domain_add_legacy(struct device_node *of_node, |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 357 | unsigned int size, |
| 358 | unsigned int first_irq, |
| 359 | irq_hw_number_t first_hwirq, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 360 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 361 | void *host_data) |
| 362 | { |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 363 | struct irq_domain *domain; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 364 | |
Marc Zyngier | 1bf4ddc | 2015-10-13 12:51:35 +0100 | [diff] [blame] | 365 | domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size, |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 366 | first_hwirq + size, 0, ops, host_data); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 367 | if (domain) |
| 368 | irq_domain_associate_many(domain, first_irq, first_hwirq, size); |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 369 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 370 | return domain; |
| 371 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 372 | EXPORT_SYMBOL_GPL(irq_domain_add_legacy); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 373 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 374 | /** |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 375 | * irq_find_matching_fwspec() - Locates a domain for a given fwspec |
| 376 | * @fwspec: FW specifier for an interrupt |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame] | 377 | * @bus_token: domain-specific data |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 378 | */ |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 379 | struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec, |
Marc Zyngier | 130b8c6 | 2015-10-13 12:51:31 +0100 | [diff] [blame] | 380 | enum irq_domain_bus_token bus_token) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 381 | { |
| 382 | struct irq_domain *h, *found = NULL; |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 383 | struct fwnode_handle *fwnode = fwspec->fwnode; |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 384 | int rc; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 385 | |
| 386 | /* We might want to match the legacy controller last since |
| 387 | * it might potentially be set to match all interrupts in |
| 388 | * the absence of a device node. This isn't a problem so far |
| 389 | * yet though... |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame] | 390 | * |
| 391 | * bus_token == DOMAIN_BUS_ANY matches any domain, any other |
| 392 | * values must generate an exact match for the domain to be |
| 393 | * selected. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 394 | */ |
| 395 | mutex_lock(&irq_domain_mutex); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 396 | list_for_each_entry(h, &irq_domain_list, link) { |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 397 | if (h->ops->select && fwspec->param_count) |
| 398 | rc = h->ops->select(h, fwspec, bus_token); |
| 399 | else if (h->ops->match) |
Marc Zyngier | 130b8c6 | 2015-10-13 12:51:31 +0100 | [diff] [blame] | 400 | rc = h->ops->match(h, to_of_node(fwnode), bus_token); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 401 | else |
Marc Zyngier | 130b8c6 | 2015-10-13 12:51:31 +0100 | [diff] [blame] | 402 | rc = ((fwnode != NULL) && (h->fwnode == fwnode) && |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame] | 403 | ((bus_token == DOMAIN_BUS_ANY) || |
| 404 | (h->bus_token == bus_token))); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 405 | |
| 406 | if (rc) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 407 | found = h; |
| 408 | break; |
| 409 | } |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 410 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 411 | mutex_unlock(&irq_domain_mutex); |
| 412 | return found; |
| 413 | } |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(irq_find_matching_fwspec); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 415 | |
| 416 | /** |
Eric Auger | c7b41f0 | 2017-01-19 20:57:59 +0000 | [diff] [blame] | 417 | * irq_domain_check_msi_remap - Check whether all MSI irq domains implement |
| 418 | * IRQ remapping |
| 419 | * |
| 420 | * Return: false if any MSI irq domain does not support IRQ remapping, |
| 421 | * true otherwise (including if there is no MSI irq domain) |
| 422 | */ |
| 423 | bool irq_domain_check_msi_remap(void) |
| 424 | { |
| 425 | struct irq_domain *h; |
| 426 | bool ret = true; |
| 427 | |
| 428 | mutex_lock(&irq_domain_mutex); |
| 429 | list_for_each_entry(h, &irq_domain_list, link) { |
| 430 | if (irq_domain_is_msi(h) && |
| 431 | !irq_domain_hierarchical_is_msi_remap(h)) { |
| 432 | ret = false; |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | mutex_unlock(&irq_domain_mutex); |
| 437 | return ret; |
| 438 | } |
| 439 | EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap); |
| 440 | |
| 441 | /** |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 442 | * irq_set_default_host() - Set a "default" irq domain |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 443 | * @domain: default domain pointer |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 444 | * |
| 445 | * For convenience, it's possible to set a "default" domain that will be used |
| 446 | * whenever NULL is passed to irq_create_mapping(). It makes life easier for |
| 447 | * platforms that want to manipulate a few hard coded interrupt numbers that |
| 448 | * aren't properly represented in the device-tree. |
| 449 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 450 | void irq_set_default_host(struct irq_domain *domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 451 | { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 452 | pr_debug("Default domain set to @0x%p\n", domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 453 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 454 | irq_default_domain = domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 455 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(irq_set_default_host); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 457 | |
Marc Zyngier | 9f199dd | 2019-02-20 08:59:23 +0000 | [diff] [blame] | 458 | /** |
| 459 | * irq_get_default_host() - Retrieve the "default" irq domain |
| 460 | * |
| 461 | * Returns: the default domain, if any. |
| 462 | * |
| 463 | * Modern code should never use this. This should only be used on |
| 464 | * systems that cannot implement a firmware->fwnode mapping (which |
| 465 | * both DT and ACPI provide). |
| 466 | */ |
| 467 | struct irq_domain *irq_get_default_host(void) |
| 468 | { |
| 469 | return irq_default_domain; |
| 470 | } |
| 471 | |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 472 | static void irq_domain_clear_mapping(struct irq_domain *domain, |
| 473 | irq_hw_number_t hwirq) |
| 474 | { |
| 475 | if (hwirq < domain->revmap_size) { |
| 476 | domain->linear_revmap[hwirq] = 0; |
| 477 | } else { |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 478 | mutex_lock(&domain->revmap_tree_mutex); |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 479 | radix_tree_delete(&domain->revmap_tree, hwirq); |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 480 | mutex_unlock(&domain->revmap_tree_mutex); |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | static void irq_domain_set_mapping(struct irq_domain *domain, |
| 485 | irq_hw_number_t hwirq, |
| 486 | struct irq_data *irq_data) |
| 487 | { |
| 488 | if (hwirq < domain->revmap_size) { |
| 489 | domain->linear_revmap[hwirq] = irq_data->irq; |
| 490 | } else { |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 491 | mutex_lock(&domain->revmap_tree_mutex); |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 492 | radix_tree_insert(&domain->revmap_tree, hwirq, irq_data); |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 493 | mutex_unlock(&domain->revmap_tree_mutex); |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Jiang Liu | 43a7759 | 2014-06-09 16:20:05 +0800 | [diff] [blame] | 497 | void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq) |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 498 | { |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 499 | struct irq_data *irq_data = irq_get_irq_data(irq); |
| 500 | irq_hw_number_t hwirq; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 501 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 502 | if (WARN(!irq_data || irq_data->domain != domain, |
| 503 | "virq%i doesn't exist; cannot disassociate\n", irq)) |
| 504 | return; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 505 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 506 | hwirq = irq_data->hwirq; |
| 507 | irq_set_status_flags(irq, IRQ_NOREQUEST); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 508 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 509 | /* remove chip and handler */ |
| 510 | irq_set_chip_and_handler(irq, NULL, NULL); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 511 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 512 | /* Make sure it's completed */ |
| 513 | synchronize_irq(irq); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 514 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 515 | /* Tell the PIC about it */ |
| 516 | if (domain->ops->unmap) |
| 517 | domain->ops->unmap(domain, irq); |
| 518 | smp_mb(); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 519 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 520 | irq_data->domain = NULL; |
| 521 | irq_data->hwirq = 0; |
Thomas Gleixner | 9dc6be3 | 2017-06-20 01:37:16 +0200 | [diff] [blame] | 522 | domain->mapcount--; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 523 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 524 | /* Clear reverse map for this hwirq */ |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 525 | irq_domain_clear_mapping(domain, hwirq); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 528 | int irq_domain_associate(struct irq_domain *domain, unsigned int virq, |
| 529 | irq_hw_number_t hwirq) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 530 | { |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 531 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 532 | int ret; |
| 533 | |
| 534 | if (WARN(hwirq >= domain->hwirq_max, |
| 535 | "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name)) |
| 536 | return -EINVAL; |
| 537 | if (WARN(!irq_data, "error: virq%i is not allocated", virq)) |
| 538 | return -EINVAL; |
| 539 | if (WARN(irq_data->domain, "error: virq%i is already associated", virq)) |
| 540 | return -EINVAL; |
| 541 | |
| 542 | mutex_lock(&irq_domain_mutex); |
| 543 | irq_data->hwirq = hwirq; |
| 544 | irq_data->domain = domain; |
| 545 | if (domain->ops->map) { |
| 546 | ret = domain->ops->map(domain, virq, hwirq); |
| 547 | if (ret != 0) { |
| 548 | /* |
| 549 | * If map() returns -EPERM, this interrupt is protected |
| 550 | * by the firmware or some other service and shall not |
| 551 | * be mapped. Don't bother telling the user about it. |
| 552 | */ |
| 553 | if (ret != -EPERM) { |
| 554 | pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n", |
| 555 | domain->name, hwirq, virq, ret); |
| 556 | } |
| 557 | irq_data->domain = NULL; |
| 558 | irq_data->hwirq = 0; |
| 559 | mutex_unlock(&irq_domain_mutex); |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | /* If not already assigned, give the domain the chip's name */ |
| 564 | if (!domain->name && irq_data->chip) |
| 565 | domain->name = irq_data->chip->name; |
| 566 | } |
| 567 | |
Thomas Gleixner | 9dc6be3 | 2017-06-20 01:37:16 +0200 | [diff] [blame] | 568 | domain->mapcount++; |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 569 | irq_domain_set_mapping(domain, hwirq, irq_data); |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 570 | mutex_unlock(&irq_domain_mutex); |
| 571 | |
| 572 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(irq_domain_associate); |
| 577 | |
| 578 | void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base, |
| 579 | irq_hw_number_t hwirq_base, int count) |
| 580 | { |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 581 | struct device_node *of_node; |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 582 | int i; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 583 | |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 584 | of_node = irq_domain_get_of_node(domain); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 585 | pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__, |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 586 | of_node_full_name(of_node), irq_base, (int)hwirq_base, count); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 587 | |
| 588 | for (i = 0; i < count; i++) { |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 589 | irq_domain_associate(domain, irq_base + i, hwirq_base + i); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 590 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 591 | } |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 592 | EXPORT_SYMBOL_GPL(irq_domain_associate_many); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 593 | |
| 594 | /** |
| 595 | * irq_create_direct_mapping() - Allocate an irq for direct mapping |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 596 | * @domain: domain to allocate the irq for or NULL for default domain |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 597 | * |
| 598 | * This routine is used for irq controllers which can choose the hardware |
| 599 | * interrupt numbers they generate. In such a case it's simplest to use |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 600 | * the linux irq as the hardware interrupt number. It still uses the linear |
| 601 | * or radix tree to store the mapping, but the irq controller can optimize |
| 602 | * the revmap path by using the hwirq directly. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 603 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 604 | unsigned int irq_create_direct_mapping(struct irq_domain *domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 605 | { |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 606 | struct device_node *of_node; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 607 | unsigned int virq; |
| 608 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 609 | if (domain == NULL) |
| 610 | domain = irq_default_domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 611 | |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 612 | of_node = irq_domain_get_of_node(domain); |
| 613 | virq = irq_alloc_desc_from(1, of_node_to_nid(of_node)); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 614 | if (!virq) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 615 | pr_debug("create_direct virq allocation failed\n"); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 616 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 617 | } |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 618 | if (virq >= domain->revmap_direct_max_irq) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 619 | pr_err("ERROR: no free irqs available below %i maximum\n", |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 620 | domain->revmap_direct_max_irq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 621 | irq_free_desc(virq); |
| 622 | return 0; |
| 623 | } |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 624 | pr_debug("create_direct obtained virq %d\n", virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 625 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 626 | if (irq_domain_associate(domain, virq, virq)) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 627 | irq_free_desc(virq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 628 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | return virq; |
| 632 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 633 | EXPORT_SYMBOL_GPL(irq_create_direct_mapping); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * irq_create_mapping() - Map a hardware interrupt into linux irq space |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 637 | * @domain: domain owning this hardware interrupt or NULL for default domain |
| 638 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 639 | * |
| 640 | * Only one mapping per hardware interrupt is permitted. Returns a linux |
| 641 | * irq number. |
| 642 | * If the sense/trigger is to be specified, set_irq_type() should be called |
| 643 | * on the number returned from that call. |
| 644 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 645 | unsigned int irq_create_mapping(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 646 | irq_hw_number_t hwirq) |
| 647 | { |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 648 | struct device_node *of_node; |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 649 | int virq; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 650 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 651 | pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 652 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 653 | /* Look for default domain if nececssary */ |
| 654 | if (domain == NULL) |
| 655 | domain = irq_default_domain; |
| 656 | if (domain == NULL) { |
Kefeng Wang | 798f0fd | 2013-06-06 19:20:27 +0800 | [diff] [blame] | 657 | WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 658 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 659 | } |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 660 | pr_debug("-> using domain @%p\n", domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 661 | |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 662 | of_node = irq_domain_get_of_node(domain); |
| 663 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 664 | /* Check if mapping already exists */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 665 | virq = irq_find_mapping(domain, hwirq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 666 | if (virq) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 667 | pr_debug("-> existing mapping on virq %d\n", virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 668 | return virq; |
| 669 | } |
| 670 | |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 671 | /* Allocate a virtual interrupt number */ |
Thomas Gleixner | 06ee6d5 | 2016-07-04 17:39:24 +0900 | [diff] [blame] | 672 | virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL); |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 673 | if (virq <= 0) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 674 | pr_debug("-> virq allocation failed\n"); |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 675 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 678 | if (irq_domain_associate(domain, virq, hwirq)) { |
Grant Likely | 7325570 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 679 | irq_free_desc(virq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 680 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 683 | pr_debug("irq %lu on domain %s mapped to virtual irq %u\n", |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 684 | hwirq, of_node_full_name(of_node), virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 685 | |
| 686 | return virq; |
| 687 | } |
| 688 | EXPORT_SYMBOL_GPL(irq_create_mapping); |
| 689 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 690 | /** |
| 691 | * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs |
| 692 | * @domain: domain owning the interrupt range |
| 693 | * @irq_base: beginning of linux IRQ range |
| 694 | * @hwirq_base: beginning of hardware IRQ range |
| 695 | * @count: Number of interrupts to map |
| 696 | * |
| 697 | * This routine is used for allocating and mapping a range of hardware |
| 698 | * irqs to linux irqs where the linux irq numbers are at pre-defined |
| 699 | * locations. For use by controllers that already have static mappings |
| 700 | * to insert in to the domain. |
| 701 | * |
| 702 | * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time |
| 703 | * domain insertion. |
| 704 | * |
| 705 | * 0 is returned upon success, while any failure to establish a static |
| 706 | * mapping is treated as an error. |
| 707 | */ |
| 708 | int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base, |
| 709 | irq_hw_number_t hwirq_base, int count) |
| 710 | { |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 711 | struct device_node *of_node; |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 712 | int ret; |
| 713 | |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 714 | of_node = irq_domain_get_of_node(domain); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 715 | ret = irq_alloc_descs(irq_base, irq_base, count, |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 716 | of_node_to_nid(of_node)); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 717 | if (unlikely(ret < 0)) |
| 718 | return ret; |
| 719 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 720 | irq_domain_associate_many(domain, irq_base, hwirq_base, count); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | EXPORT_SYMBOL_GPL(irq_create_strict_mappings); |
| 724 | |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 725 | static int irq_domain_translate(struct irq_domain *d, |
| 726 | struct irq_fwspec *fwspec, |
| 727 | irq_hw_number_t *hwirq, unsigned int *type) |
| 728 | { |
| 729 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
| 730 | if (d->ops->translate) |
| 731 | return d->ops->translate(d, fwspec, hwirq, type); |
| 732 | #endif |
| 733 | if (d->ops->xlate) |
| 734 | return d->ops->xlate(d, to_of_node(fwspec->fwnode), |
| 735 | fwspec->param, fwspec->param_count, |
| 736 | hwirq, type); |
| 737 | |
| 738 | /* If domain has no translation, then we assume interrupt line */ |
| 739 | *hwirq = fwspec->param[0]; |
| 740 | return 0; |
| 741 | } |
| 742 | |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 743 | static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args, |
| 744 | unsigned int count, |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 745 | struct irq_fwspec *fwspec) |
| 746 | { |
| 747 | int i; |
| 748 | |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 749 | fwspec->fwnode = np ? &np->fwnode : NULL; |
| 750 | fwspec->param_count = count; |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 751 | |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 752 | for (i = 0; i < count; i++) |
| 753 | fwspec->param[i] = args[i]; |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Marc Zyngier | c0131f0 | 2015-10-13 12:51:34 +0100 | [diff] [blame] | 756 | unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 757 | { |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 758 | struct irq_domain *domain; |
Jon Hunter | 1e2a7d7 | 2016-06-07 16:12:28 +0100 | [diff] [blame] | 759 | struct irq_data *irq_data; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 760 | irq_hw_number_t hwirq; |
| 761 | unsigned int type = IRQ_TYPE_NONE; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 762 | int virq; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 763 | |
Marc Zyngier | 530cbe1 | 2016-01-26 13:52:25 +0000 | [diff] [blame] | 764 | if (fwspec->fwnode) { |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 765 | domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED); |
Marc Zyngier | 530cbe1 | 2016-01-26 13:52:25 +0000 | [diff] [blame] | 766 | if (!domain) |
Marc Zyngier | 651e8b5 | 2016-04-11 09:57:51 +0100 | [diff] [blame] | 767 | domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY); |
Marc Zyngier | 530cbe1 | 2016-01-26 13:52:25 +0000 | [diff] [blame] | 768 | } else { |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 769 | domain = irq_default_domain; |
Marc Zyngier | 530cbe1 | 2016-01-26 13:52:25 +0000 | [diff] [blame] | 770 | } |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 771 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 772 | if (!domain) { |
Kefeng Wang | 798f0fd | 2013-06-06 19:20:27 +0800 | [diff] [blame] | 773 | pr_warn("no irq domain found for %s !\n", |
Marc Zyngier | c0131f0 | 2015-10-13 12:51:34 +0100 | [diff] [blame] | 774 | of_node_full_name(to_of_node(fwspec->fwnode))); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 775 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Marc Zyngier | c0131f0 | 2015-10-13 12:51:34 +0100 | [diff] [blame] | 778 | if (irq_domain_translate(domain, fwspec, &hwirq, &type)) |
Marc Zyngier | 11e4438e | 2015-10-13 12:51:32 +0100 | [diff] [blame] | 779 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 780 | |
Jon Hunter | b62b2cf | 2016-06-07 16:12:26 +0100 | [diff] [blame] | 781 | /* |
| 782 | * WARN if the irqchip returns a type with bits |
| 783 | * outside the sense mask set and clear these bits. |
| 784 | */ |
| 785 | if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK)) |
| 786 | type &= IRQ_TYPE_SENSE_MASK; |
| 787 | |
| 788 | /* |
| 789 | * If we've already configured this interrupt, |
| 790 | * don't do it again, or hell will break loose. |
| 791 | */ |
| 792 | virq = irq_find_mapping(domain, hwirq); |
| 793 | if (virq) { |
Yingjoe Chen | 0cc01ab | 2014-11-06 22:20:15 +0800 | [diff] [blame] | 794 | /* |
Jon Hunter | b62b2cf | 2016-06-07 16:12:26 +0100 | [diff] [blame] | 795 | * If the trigger type is not specified or matches the |
| 796 | * current trigger type then we are done so return the |
| 797 | * interrupt number. |
Yingjoe Chen | 0cc01ab | 2014-11-06 22:20:15 +0800 | [diff] [blame] | 798 | */ |
Jon Hunter | b62b2cf | 2016-06-07 16:12:26 +0100 | [diff] [blame] | 799 | if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq)) |
Yingjoe Chen | 0cc01ab | 2014-11-06 22:20:15 +0800 | [diff] [blame] | 800 | return virq; |
| 801 | |
Jon Hunter | b62b2cf | 2016-06-07 16:12:26 +0100 | [diff] [blame] | 802 | /* |
| 803 | * If the trigger type has not been set yet, then set |
| 804 | * it now and return the interrupt number. |
| 805 | */ |
| 806 | if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) { |
Jon Hunter | 1e2a7d7 | 2016-06-07 16:12:28 +0100 | [diff] [blame] | 807 | irq_data = irq_get_irq_data(virq); |
| 808 | if (!irq_data) |
| 809 | return 0; |
| 810 | |
| 811 | irqd_set_trigger_type(irq_data, type); |
Jon Hunter | b62b2cf | 2016-06-07 16:12:26 +0100 | [diff] [blame] | 812 | return virq; |
| 813 | } |
| 814 | |
| 815 | pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n", |
| 816 | hwirq, of_node_full_name(to_of_node(fwspec->fwnode))); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | if (irq_domain_is_hierarchy(domain)) { |
Marc Zyngier | c0131f0 | 2015-10-13 12:51:34 +0100 | [diff] [blame] | 821 | virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec); |
Yingjoe Chen | 0cc01ab | 2014-11-06 22:20:15 +0800 | [diff] [blame] | 822 | if (virq <= 0) |
| 823 | return 0; |
| 824 | } else { |
| 825 | /* Create mapping */ |
| 826 | virq = irq_create_mapping(domain, hwirq); |
| 827 | if (!virq) |
| 828 | return virq; |
| 829 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 830 | |
Jon Hunter | 1e2a7d7 | 2016-06-07 16:12:28 +0100 | [diff] [blame] | 831 | irq_data = irq_get_irq_data(virq); |
| 832 | if (!irq_data) { |
| 833 | if (irq_domain_is_hierarchy(domain)) |
| 834 | irq_domain_free_irqs(virq, 1); |
| 835 | else |
| 836 | irq_dispose_mapping(virq); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* Store trigger type */ |
| 841 | irqd_set_trigger_type(irq_data, type); |
| 842 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 843 | return virq; |
| 844 | } |
Marc Zyngier | c0131f0 | 2015-10-13 12:51:34 +0100 | [diff] [blame] | 845 | EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping); |
| 846 | |
| 847 | unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data) |
| 848 | { |
| 849 | struct irq_fwspec fwspec; |
| 850 | |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 851 | of_phandle_args_to_fwspec(irq_data->np, irq_data->args, |
| 852 | irq_data->args_count, &fwspec); |
| 853 | |
Marc Zyngier | c0131f0 | 2015-10-13 12:51:34 +0100 | [diff] [blame] | 854 | return irq_create_fwspec_mapping(&fwspec); |
| 855 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 856 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 857 | |
| 858 | /** |
| 859 | * irq_dispose_mapping() - Unmap an interrupt |
| 860 | * @virq: linux irq number of the interrupt to unmap |
| 861 | */ |
| 862 | void irq_dispose_mapping(unsigned int virq) |
| 863 | { |
| 864 | struct irq_data *irq_data = irq_get_irq_data(virq); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 865 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 866 | |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 867 | if (!virq || !irq_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 868 | return; |
| 869 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 870 | domain = irq_data->domain; |
| 871 | if (WARN_ON(domain == NULL)) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 872 | return; |
| 873 | |
Jon Hunter | d16dcd3d | 2016-06-21 10:23:22 +0100 | [diff] [blame] | 874 | if (irq_domain_is_hierarchy(domain)) { |
| 875 | irq_domain_free_irqs(virq, 1); |
| 876 | } else { |
| 877 | irq_domain_disassociate(domain, virq); |
| 878 | irq_free_desc(virq); |
| 879 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 880 | } |
| 881 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
| 882 | |
| 883 | /** |
Geert Uytterhoeven | b8d62f3 | 2018-10-08 13:17:26 +0200 | [diff] [blame] | 884 | * irq_find_mapping() - Find a linux irq from a hw irq number. |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 885 | * @domain: domain owning this hardware interrupt |
| 886 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 887 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 888 | unsigned int irq_find_mapping(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 889 | irq_hw_number_t hwirq) |
| 890 | { |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 891 | struct irq_data *data; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 892 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 893 | /* Look for default domain if nececssary */ |
| 894 | if (domain == NULL) |
| 895 | domain = irq_default_domain; |
| 896 | if (domain == NULL) |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 897 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 898 | |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 899 | if (hwirq < domain->revmap_direct_max_irq) { |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 900 | data = irq_domain_get_irq_data(domain, hwirq); |
| 901 | if (data && data->hwirq == hwirq) |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 902 | return hwirq; |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Grant Likely | d3dcb43 | 2013-06-10 12:19:17 +0100 | [diff] [blame] | 905 | /* Check if the hwirq is in the linear revmap. */ |
| 906 | if (hwirq < domain->revmap_size) |
| 907 | return domain->linear_revmap[hwirq]; |
| 908 | |
| 909 | rcu_read_lock(); |
| 910 | data = radix_tree_lookup(&domain->revmap_tree, hwirq); |
| 911 | rcu_read_unlock(); |
| 912 | return data ? data->irq : 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 913 | } |
| 914 | EXPORT_SYMBOL_GPL(irq_find_mapping); |
| 915 | |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 916 | /** |
| 917 | * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings |
| 918 | * |
| 919 | * Device Tree IRQ specifier translation function which works with one cell |
| 920 | * bindings where the cell value maps directly to the hwirq number. |
| 921 | */ |
| 922 | int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr, |
| 923 | const u32 *intspec, unsigned int intsize, |
| 924 | unsigned long *out_hwirq, unsigned int *out_type) |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 925 | { |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 926 | if (WARN_ON(intsize < 1)) |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 927 | return -EINVAL; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 928 | *out_hwirq = intspec[0]; |
| 929 | *out_type = IRQ_TYPE_NONE; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 930 | return 0; |
| 931 | } |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 932 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell); |
| 933 | |
| 934 | /** |
| 935 | * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings |
| 936 | * |
| 937 | * Device Tree IRQ specifier translation function which works with two cell |
| 938 | * bindings where the cell values map directly to the hwirq number |
| 939 | * and linux irq flags. |
| 940 | */ |
| 941 | int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr, |
| 942 | const u32 *intspec, unsigned int intsize, |
| 943 | irq_hw_number_t *out_hwirq, unsigned int *out_type) |
| 944 | { |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 945 | struct irq_fwspec fwspec; |
| 946 | |
| 947 | of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec); |
| 948 | return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type); |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 949 | } |
| 950 | EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell); |
| 951 | |
| 952 | /** |
| 953 | * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings |
| 954 | * |
| 955 | * Device Tree IRQ specifier translation function which works with either one |
| 956 | * or two cell bindings where the cell values map directly to the hwirq number |
| 957 | * and linux irq flags. |
| 958 | * |
| 959 | * Note: don't use this function unless your interrupt controller explicitly |
| 960 | * supports both one and two cell bindings. For the majority of controllers |
| 961 | * the _onecell() or _twocell() variants above should be used. |
| 962 | */ |
| 963 | int irq_domain_xlate_onetwocell(struct irq_domain *d, |
| 964 | struct device_node *ctrlr, |
| 965 | const u32 *intspec, unsigned int intsize, |
| 966 | unsigned long *out_hwirq, unsigned int *out_type) |
| 967 | { |
| 968 | if (WARN_ON(intsize < 1)) |
| 969 | return -EINVAL; |
| 970 | *out_hwirq = intspec[0]; |
Sebastian Frias | 0c22891 | 2016-08-02 10:52:45 +0200 | [diff] [blame] | 971 | if (intsize > 1) |
| 972 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 973 | else |
| 974 | *out_type = IRQ_TYPE_NONE; |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 975 | return 0; |
| 976 | } |
| 977 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell); |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 978 | |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 979 | const struct irq_domain_ops irq_domain_simple_ops = { |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 980 | .xlate = irq_domain_xlate_onetwocell, |
Grant Likely | 7529495 | 2012-02-14 14:06:57 -0700 | [diff] [blame] | 981 | }; |
| 982 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 983 | |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 984 | /** |
Yash Shah | b01ecce | 2019-12-10 16:41:09 +0530 | [diff] [blame] | 985 | * irq_domain_translate_onecell() - Generic translate for direct one cell |
| 986 | * bindings |
| 987 | */ |
| 988 | int irq_domain_translate_onecell(struct irq_domain *d, |
| 989 | struct irq_fwspec *fwspec, |
| 990 | unsigned long *out_hwirq, |
| 991 | unsigned int *out_type) |
| 992 | { |
| 993 | if (WARN_ON(fwspec->param_count < 1)) |
| 994 | return -EINVAL; |
| 995 | *out_hwirq = fwspec->param[0]; |
| 996 | *out_type = IRQ_TYPE_NONE; |
| 997 | return 0; |
| 998 | } |
| 999 | EXPORT_SYMBOL_GPL(irq_domain_translate_onecell); |
| 1000 | |
| 1001 | /** |
Brian Masney | b5c231d | 2019-02-07 21:16:22 -0500 | [diff] [blame] | 1002 | * irq_domain_translate_twocell() - Generic translate for direct two cell |
| 1003 | * bindings |
| 1004 | * |
| 1005 | * Device Tree IRQ specifier translation function which works with two cell |
| 1006 | * bindings where the cell values map directly to the hwirq number |
| 1007 | * and linux irq flags. |
| 1008 | */ |
| 1009 | int irq_domain_translate_twocell(struct irq_domain *d, |
| 1010 | struct irq_fwspec *fwspec, |
| 1011 | unsigned long *out_hwirq, |
| 1012 | unsigned int *out_type) |
| 1013 | { |
| 1014 | if (WARN_ON(fwspec->param_count < 2)) |
| 1015 | return -EINVAL; |
| 1016 | *out_hwirq = fwspec->param[0]; |
| 1017 | *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | EXPORT_SYMBOL_GPL(irq_domain_translate_twocell); |
| 1021 | |
Qais Yousef | ac0a0cd | 2015-12-08 13:20:18 +0000 | [diff] [blame] | 1022 | int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq, |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 1023 | int node, const struct irq_affinity_desc *affinity) |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1024 | { |
| 1025 | unsigned int hint; |
| 1026 | |
| 1027 | if (virq >= 0) { |
Thomas Gleixner | 06ee6d5 | 2016-07-04 17:39:24 +0900 | [diff] [blame] | 1028 | virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE, |
| 1029 | affinity); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1030 | } else { |
| 1031 | hint = hwirq % nr_irqs; |
| 1032 | if (hint == 0) |
| 1033 | hint++; |
Thomas Gleixner | 06ee6d5 | 2016-07-04 17:39:24 +0900 | [diff] [blame] | 1034 | virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE, |
| 1035 | affinity); |
| 1036 | if (virq <= 0 && hint > 1) { |
| 1037 | virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE, |
| 1038 | affinity); |
| 1039 | } |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | return virq; |
| 1043 | } |
| 1044 | |
Bartosz Golaszewski | 5c8f77a | 2020-05-14 10:39:00 +0200 | [diff] [blame] | 1045 | /** |
| 1046 | * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data |
| 1047 | * @irq_data: The pointer to irq_data |
| 1048 | */ |
| 1049 | void irq_domain_reset_irq_data(struct irq_data *irq_data) |
| 1050 | { |
| 1051 | irq_data->hwirq = 0; |
| 1052 | irq_data->chip = &no_irq_chip; |
| 1053 | irq_data->chip_data = NULL; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data); |
| 1056 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1057 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1058 | /** |
Marc Zyngier | 2a5e9a0 | 2015-10-13 12:51:43 +0100 | [diff] [blame] | 1059 | * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1060 | * @parent: Parent irq domain to associate with the new domain |
| 1061 | * @flags: Irq domain flags associated to the domain |
| 1062 | * @size: Size of the domain. See below |
Marc Zyngier | 2a5e9a0 | 2015-10-13 12:51:43 +0100 | [diff] [blame] | 1063 | * @fwnode: Optional fwnode of the interrupt controller |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1064 | * @ops: Pointer to the interrupt domain callbacks |
| 1065 | * @host_data: Controller private data pointer |
| 1066 | * |
| 1067 | * If @size is 0 a tree domain is created, otherwise a linear domain. |
| 1068 | * |
| 1069 | * If successful the parent is associated to the new domain and the |
| 1070 | * domain flags are set. |
| 1071 | * Returns pointer to IRQ domain, or NULL on failure. |
| 1072 | */ |
Marc Zyngier | 2a5e9a0 | 2015-10-13 12:51:43 +0100 | [diff] [blame] | 1073 | struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent, |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1074 | unsigned int flags, |
| 1075 | unsigned int size, |
Marc Zyngier | 2a5e9a0 | 2015-10-13 12:51:43 +0100 | [diff] [blame] | 1076 | struct fwnode_handle *fwnode, |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1077 | const struct irq_domain_ops *ops, |
| 1078 | void *host_data) |
| 1079 | { |
| 1080 | struct irq_domain *domain; |
| 1081 | |
| 1082 | if (size) |
Marc Zyngier | 2a5e9a0 | 2015-10-13 12:51:43 +0100 | [diff] [blame] | 1083 | domain = irq_domain_create_linear(fwnode, size, ops, host_data); |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1084 | else |
Marc Zyngier | 2a5e9a0 | 2015-10-13 12:51:43 +0100 | [diff] [blame] | 1085 | domain = irq_domain_create_tree(fwnode, ops, host_data); |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1086 | if (domain) { |
| 1087 | domain->parent = parent; |
| 1088 | domain->flags |= flags; |
| 1089 | } |
| 1090 | |
| 1091 | return domain; |
| 1092 | } |
Quan Nguyen | 52b2a05 | 2016-03-03 21:56:52 +0700 | [diff] [blame] | 1093 | EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy); |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 1094 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1095 | static void irq_domain_insert_irq(int virq) |
| 1096 | { |
| 1097 | struct irq_data *data; |
| 1098 | |
| 1099 | for (data = irq_get_irq_data(virq); data; data = data->parent_data) { |
| 1100 | struct irq_domain *domain = data->domain; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1101 | |
Thomas Gleixner | 9dc6be3 | 2017-06-20 01:37:16 +0200 | [diff] [blame] | 1102 | domain->mapcount++; |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 1103 | irq_domain_set_mapping(domain, data->hwirq, data); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1104 | |
| 1105 | /* If not already assigned, give the domain the chip's name */ |
| 1106 | if (!domain->name && data->chip) |
| 1107 | domain->name = data->chip->name; |
| 1108 | } |
| 1109 | |
| 1110 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
| 1111 | } |
| 1112 | |
| 1113 | static void irq_domain_remove_irq(int virq) |
| 1114 | { |
| 1115 | struct irq_data *data; |
| 1116 | |
| 1117 | irq_set_status_flags(virq, IRQ_NOREQUEST); |
| 1118 | irq_set_chip_and_handler(virq, NULL, NULL); |
| 1119 | synchronize_irq(virq); |
| 1120 | smp_mb(); |
| 1121 | |
| 1122 | for (data = irq_get_irq_data(virq); data; data = data->parent_data) { |
| 1123 | struct irq_domain *domain = data->domain; |
| 1124 | irq_hw_number_t hwirq = data->hwirq; |
| 1125 | |
Thomas Gleixner | 9dc6be3 | 2017-06-20 01:37:16 +0200 | [diff] [blame] | 1126 | domain->mapcount--; |
David Daney | b526adf | 2017-08-17 17:53:32 -0700 | [diff] [blame] | 1127 | irq_domain_clear_mapping(domain, hwirq); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain, |
| 1132 | struct irq_data *child) |
| 1133 | { |
| 1134 | struct irq_data *irq_data; |
| 1135 | |
Jiang Liu | 6783011 | 2015-06-01 16:05:13 +0800 | [diff] [blame] | 1136 | irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL, |
| 1137 | irq_data_get_node(child)); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1138 | if (irq_data) { |
| 1139 | child->parent_data = irq_data; |
| 1140 | irq_data->irq = child->irq; |
Jiang Liu | 0d0b4c8 | 2015-06-01 16:05:12 +0800 | [diff] [blame] | 1141 | irq_data->common = child->common; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1142 | irq_data->domain = domain; |
| 1143 | } |
| 1144 | |
| 1145 | return irq_data; |
| 1146 | } |
| 1147 | |
Marc Zyngier | 5556797 | 2020-10-06 10:10:20 +0100 | [diff] [blame] | 1148 | static void __irq_domain_free_hierarchy(struct irq_data *irq_data) |
| 1149 | { |
| 1150 | struct irq_data *tmp; |
| 1151 | |
| 1152 | while (irq_data) { |
| 1153 | tmp = irq_data; |
| 1154 | irq_data = irq_data->parent_data; |
| 1155 | kfree(tmp); |
| 1156 | } |
| 1157 | } |
| 1158 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1159 | static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs) |
| 1160 | { |
| 1161 | struct irq_data *irq_data, *tmp; |
| 1162 | int i; |
| 1163 | |
| 1164 | for (i = 0; i < nr_irqs; i++) { |
| 1165 | irq_data = irq_get_irq_data(virq + i); |
| 1166 | tmp = irq_data->parent_data; |
| 1167 | irq_data->parent_data = NULL; |
| 1168 | irq_data->domain = NULL; |
| 1169 | |
Marc Zyngier | 5556797 | 2020-10-06 10:10:20 +0100 | [diff] [blame] | 1170 | __irq_domain_free_hierarchy(tmp); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /** |
| 1175 | * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy |
| 1176 | * @domain: IRQ domain from which the hierarchy is to be disconnected |
| 1177 | * @virq: IRQ number where the hierarchy is to be trimmed |
| 1178 | * |
| 1179 | * Marks the @virq level belonging to @domain as disconnected. |
| 1180 | * Returns -EINVAL if @virq doesn't have a valid irq_data pointing |
| 1181 | * to @domain. |
| 1182 | * |
| 1183 | * Its only use is to be able to trim levels of hierarchy that do not |
| 1184 | * have any real meaning for this interrupt, and that the driver marks |
| 1185 | * as such from its .alloc() callback. |
| 1186 | */ |
| 1187 | int irq_domain_disconnect_hierarchy(struct irq_domain *domain, |
| 1188 | unsigned int virq) |
| 1189 | { |
| 1190 | struct irq_data *irqd; |
| 1191 | |
| 1192 | irqd = irq_domain_get_irq_data(domain, virq); |
| 1193 | if (!irqd) |
| 1194 | return -EINVAL; |
| 1195 | |
| 1196 | irqd->chip = ERR_PTR(-ENOTCONN); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | static int irq_domain_trim_hierarchy(unsigned int virq) |
| 1201 | { |
| 1202 | struct irq_data *tail, *irqd, *irq_data; |
| 1203 | |
| 1204 | irq_data = irq_get_irq_data(virq); |
| 1205 | tail = NULL; |
| 1206 | |
| 1207 | /* The first entry must have a valid irqchip */ |
| 1208 | if (!irq_data->chip || IS_ERR(irq_data->chip)) |
| 1209 | return -EINVAL; |
| 1210 | |
| 1211 | /* |
| 1212 | * Validate that the irq_data chain is sane in the presence of |
| 1213 | * a hierarchy trimming marker. |
| 1214 | */ |
| 1215 | for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) { |
| 1216 | /* Can't have a valid irqchip after a trim marker */ |
| 1217 | if (irqd->chip && tail) |
| 1218 | return -EINVAL; |
| 1219 | |
| 1220 | /* Can't have an empty irqchip before a trim marker */ |
| 1221 | if (!irqd->chip && !tail) |
| 1222 | return -EINVAL; |
| 1223 | |
| 1224 | if (IS_ERR(irqd->chip)) { |
| 1225 | /* Only -ENOTCONN is a valid trim marker */ |
| 1226 | if (PTR_ERR(irqd->chip) != -ENOTCONN) |
| 1227 | return -EINVAL; |
| 1228 | |
| 1229 | tail = irq_data; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1230 | } |
| 1231 | } |
Marc Zyngier | 5556797 | 2020-10-06 10:10:20 +0100 | [diff] [blame] | 1232 | |
| 1233 | /* No trim marker, nothing to do */ |
| 1234 | if (!tail) |
| 1235 | return 0; |
| 1236 | |
| 1237 | pr_info("IRQ%d: trimming hierarchy from %s\n", |
| 1238 | virq, tail->parent_data->domain->name); |
| 1239 | |
| 1240 | /* Sever the inner part of the hierarchy... */ |
| 1241 | irqd = tail; |
| 1242 | tail = tail->parent_data; |
| 1243 | irqd->parent_data = NULL; |
| 1244 | __irq_domain_free_hierarchy(tail); |
| 1245 | |
| 1246 | return 0; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | static int irq_domain_alloc_irq_data(struct irq_domain *domain, |
| 1250 | unsigned int virq, unsigned int nr_irqs) |
| 1251 | { |
| 1252 | struct irq_data *irq_data; |
| 1253 | struct irq_domain *parent; |
| 1254 | int i; |
| 1255 | |
| 1256 | /* The outermost irq_data is embedded in struct irq_desc */ |
| 1257 | for (i = 0; i < nr_irqs; i++) { |
| 1258 | irq_data = irq_get_irq_data(virq + i); |
| 1259 | irq_data->domain = domain; |
| 1260 | |
| 1261 | for (parent = domain->parent; parent; parent = parent->parent) { |
| 1262 | irq_data = irq_domain_insert_irq_data(parent, irq_data); |
| 1263 | if (!irq_data) { |
| 1264 | irq_domain_free_irq_data(virq, i + 1); |
| 1265 | return -ENOMEM; |
| 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain |
| 1275 | * @domain: domain to match |
| 1276 | * @virq: IRQ number to get irq_data |
| 1277 | */ |
| 1278 | struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain, |
| 1279 | unsigned int virq) |
| 1280 | { |
| 1281 | struct irq_data *irq_data; |
| 1282 | |
| 1283 | for (irq_data = irq_get_irq_data(virq); irq_data; |
| 1284 | irq_data = irq_data->parent_data) |
| 1285 | if (irq_data->domain == domain) |
| 1286 | return irq_data; |
| 1287 | |
| 1288 | return NULL; |
| 1289 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 1290 | EXPORT_SYMBOL_GPL(irq_domain_get_irq_data); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1291 | |
| 1292 | /** |
| 1293 | * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain |
| 1294 | * @domain: Interrupt domain to match |
| 1295 | * @virq: IRQ number |
| 1296 | * @hwirq: The hwirq number |
| 1297 | * @chip: The associated interrupt chip |
| 1298 | * @chip_data: The associated chip data |
| 1299 | */ |
| 1300 | int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq, |
| 1301 | irq_hw_number_t hwirq, struct irq_chip *chip, |
| 1302 | void *chip_data) |
| 1303 | { |
| 1304 | struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq); |
| 1305 | |
| 1306 | if (!irq_data) |
| 1307 | return -ENOENT; |
| 1308 | |
| 1309 | irq_data->hwirq = hwirq; |
| 1310 | irq_data->chip = chip ? chip : &no_irq_chip; |
| 1311 | irq_data->chip_data = chip_data; |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
Quan Nguyen | 52b2a05 | 2016-03-03 21:56:52 +0700 | [diff] [blame] | 1315 | EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1316 | |
| 1317 | /** |
Jiang Liu | 1b53770 | 2014-11-09 23:10:24 +0800 | [diff] [blame] | 1318 | * irq_domain_set_info - Set the complete data for a @virq in @domain |
| 1319 | * @domain: Interrupt domain to match |
| 1320 | * @virq: IRQ number |
| 1321 | * @hwirq: The hardware interrupt number |
| 1322 | * @chip: The associated interrupt chip |
| 1323 | * @chip_data: The associated interrupt chip data |
| 1324 | * @handler: The interrupt flow handler |
| 1325 | * @handler_data: The interrupt flow handler data |
| 1326 | * @handler_name: The interrupt handler name |
| 1327 | */ |
| 1328 | void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, |
| 1329 | irq_hw_number_t hwirq, struct irq_chip *chip, |
| 1330 | void *chip_data, irq_flow_handler_t handler, |
| 1331 | void *handler_data, const char *handler_name) |
| 1332 | { |
| 1333 | irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data); |
| 1334 | __irq_set_handler(virq, handler, 0, handler_name); |
| 1335 | irq_set_handler_data(virq, handler_data); |
| 1336 | } |
Keith Busch | 64bce3e | 2016-01-12 13:18:07 -0700 | [diff] [blame] | 1337 | EXPORT_SYMBOL(irq_domain_set_info); |
Jiang Liu | 1b53770 | 2014-11-09 23:10:24 +0800 | [diff] [blame] | 1338 | |
| 1339 | /** |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1340 | * irq_domain_free_irqs_common - Clear irq_data and free the parent |
| 1341 | * @domain: Interrupt domain to match |
| 1342 | * @virq: IRQ number to start with |
| 1343 | * @nr_irqs: The number of irqs to free |
| 1344 | */ |
| 1345 | void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq, |
| 1346 | unsigned int nr_irqs) |
| 1347 | { |
| 1348 | struct irq_data *irq_data; |
| 1349 | int i; |
| 1350 | |
| 1351 | for (i = 0; i < nr_irqs; i++) { |
| 1352 | irq_data = irq_domain_get_irq_data(domain, virq + i); |
| 1353 | if (irq_data) |
| 1354 | irq_domain_reset_irq_data(irq_data); |
| 1355 | } |
| 1356 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 1357 | } |
Axel Lin | 63cc787 | 2016-03-17 12:00:31 +0800 | [diff] [blame] | 1358 | EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1359 | |
| 1360 | /** |
| 1361 | * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent |
| 1362 | * @domain: Interrupt domain to match |
| 1363 | * @virq: IRQ number to start with |
| 1364 | * @nr_irqs: The number of irqs to free |
| 1365 | */ |
| 1366 | void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq, |
| 1367 | unsigned int nr_irqs) |
| 1368 | { |
| 1369 | int i; |
| 1370 | |
| 1371 | for (i = 0; i < nr_irqs; i++) { |
| 1372 | irq_set_handler_data(virq + i, NULL); |
| 1373 | irq_set_handler(virq + i, NULL); |
| 1374 | } |
| 1375 | irq_domain_free_irqs_common(domain, virq, nr_irqs); |
| 1376 | } |
| 1377 | |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1378 | static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain, |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1379 | unsigned int irq_base, |
| 1380 | unsigned int nr_irqs) |
| 1381 | { |
David Daney | 0d12ec0 | 2017-08-17 17:53:33 -0700 | [diff] [blame] | 1382 | if (domain->ops->free) |
| 1383 | domain->ops->free(domain, irq_base, nr_irqs); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1384 | } |
| 1385 | |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1386 | int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain, |
Marc Zyngier | c466595 | 2015-11-23 08:26:04 +0000 | [diff] [blame] | 1387 | unsigned int irq_base, |
| 1388 | unsigned int nr_irqs, void *arg) |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1389 | { |
Alexander Sverdlin | 87f2d1c | 2020-03-06 18:47:20 +0100 | [diff] [blame] | 1390 | if (!domain->ops->alloc) { |
| 1391 | pr_debug("domain->ops->alloc() is NULL\n"); |
| 1392 | return -ENOSYS; |
| 1393 | } |
| 1394 | |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1395 | return domain->ops->alloc(domain, irq_base, nr_irqs, arg); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1396 | } |
| 1397 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1398 | /** |
| 1399 | * __irq_domain_alloc_irqs - Allocate IRQs from domain |
| 1400 | * @domain: domain to allocate from |
Julien Grall | 08970ec | 2019-04-18 16:54:01 +0100 | [diff] [blame] | 1401 | * @irq_base: allocate specified IRQ number if irq_base >= 0 |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1402 | * @nr_irqs: number of IRQs to allocate |
| 1403 | * @node: NUMA node id for memory allocation |
| 1404 | * @arg: domain specific argument |
| 1405 | * @realloc: IRQ descriptors have already been allocated if true |
Thomas Gleixner | 06ee6d5 | 2016-07-04 17:39:24 +0900 | [diff] [blame] | 1406 | * @affinity: Optional irq affinity mask for multiqueue devices |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1407 | * |
| 1408 | * Allocate IRQ numbers and initialized all data structures to support |
| 1409 | * hierarchy IRQ domains. |
| 1410 | * Parameter @realloc is mainly to support legacy IRQs. |
| 1411 | * Returns error code or allocated IRQ number |
| 1412 | * |
| 1413 | * The whole process to setup an IRQ has been split into two steps. |
| 1414 | * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ |
| 1415 | * descriptor and required hardware resources. The second step, |
| 1416 | * irq_domain_activate_irq(), is to program hardwares with preallocated |
| 1417 | * resources. In this way, it's easier to rollback when failing to |
| 1418 | * allocate resources. |
| 1419 | */ |
| 1420 | int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base, |
| 1421 | unsigned int nr_irqs, int node, void *arg, |
Dou Liyang | bec0403 | 2018-12-04 23:51:20 +0800 | [diff] [blame] | 1422 | bool realloc, const struct irq_affinity_desc *affinity) |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1423 | { |
| 1424 | int i, ret, virq; |
| 1425 | |
| 1426 | if (domain == NULL) { |
| 1427 | domain = irq_default_domain; |
| 1428 | if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n")) |
| 1429 | return -EINVAL; |
| 1430 | } |
| 1431 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1432 | if (realloc && irq_base >= 0) { |
| 1433 | virq = irq_base; |
| 1434 | } else { |
Thomas Gleixner | 06ee6d5 | 2016-07-04 17:39:24 +0900 | [diff] [blame] | 1435 | virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node, |
| 1436 | affinity); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1437 | if (virq < 0) { |
| 1438 | pr_debug("cannot allocate IRQ(base %d, count %d)\n", |
| 1439 | irq_base, nr_irqs); |
| 1440 | return virq; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) { |
| 1445 | pr_debug("cannot allocate memory for IRQ%d\n", virq); |
| 1446 | ret = -ENOMEM; |
| 1447 | goto out_free_desc; |
| 1448 | } |
| 1449 | |
| 1450 | mutex_lock(&irq_domain_mutex); |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1451 | ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1452 | if (ret < 0) { |
| 1453 | mutex_unlock(&irq_domain_mutex); |
| 1454 | goto out_free_irq_data; |
| 1455 | } |
Marc Zyngier | 5556797 | 2020-10-06 10:10:20 +0100 | [diff] [blame] | 1456 | |
| 1457 | for (i = 0; i < nr_irqs; i++) { |
| 1458 | ret = irq_domain_trim_hierarchy(virq + i); |
| 1459 | if (ret) { |
| 1460 | mutex_unlock(&irq_domain_mutex); |
| 1461 | goto out_free_irq_data; |
| 1462 | } |
| 1463 | } |
| 1464 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1465 | for (i = 0; i < nr_irqs; i++) |
| 1466 | irq_domain_insert_irq(virq + i); |
| 1467 | mutex_unlock(&irq_domain_mutex); |
| 1468 | |
| 1469 | return virq; |
| 1470 | |
| 1471 | out_free_irq_data: |
| 1472 | irq_domain_free_irq_data(virq, nr_irqs); |
| 1473 | out_free_desc: |
| 1474 | irq_free_descs(virq, nr_irqs); |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1478 | /* The irq_data was moved, fix the revmap to refer to the new location */ |
| 1479 | static void irq_domain_fix_revmap(struct irq_data *d) |
| 1480 | { |
Masahiro Yamada | d03cc2d | 2017-09-22 21:20:41 +0900 | [diff] [blame] | 1481 | void __rcu **slot; |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1482 | |
| 1483 | if (d->hwirq < d->domain->revmap_size) |
| 1484 | return; /* Not using radix tree. */ |
| 1485 | |
| 1486 | /* Fix up the revmap. */ |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 1487 | mutex_lock(&d->domain->revmap_tree_mutex); |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1488 | slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq); |
| 1489 | if (slot) |
| 1490 | radix_tree_replace_slot(&d->domain->revmap_tree, slot, d); |
Masahiro Yamada | f1d7835 | 2017-10-05 10:44:54 +0900 | [diff] [blame] | 1491 | mutex_unlock(&d->domain->revmap_tree_mutex); |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * irq_domain_push_irq() - Push a domain in to the top of a hierarchy. |
| 1496 | * @domain: Domain to push. |
| 1497 | * @virq: Irq to push the domain in to. |
| 1498 | * @arg: Passed to the irq_domain_ops alloc() function. |
| 1499 | * |
| 1500 | * For an already existing irqdomain hierarchy, as might be obtained |
| 1501 | * via a call to pci_enable_msix(), add an additional domain to the |
| 1502 | * head of the processing chain. Must be called before request_irq() |
| 1503 | * has been called. |
| 1504 | */ |
| 1505 | int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg) |
| 1506 | { |
| 1507 | struct irq_data *child_irq_data; |
| 1508 | struct irq_data *root_irq_data = irq_get_irq_data(virq); |
| 1509 | struct irq_desc *desc; |
| 1510 | int rv = 0; |
| 1511 | |
| 1512 | /* |
| 1513 | * Check that no action has been set, which indicates the virq |
| 1514 | * is in a state where this function doesn't have to deal with |
| 1515 | * races between interrupt handling and maintaining the |
| 1516 | * hierarchy. This will catch gross misuse. Attempting to |
| 1517 | * make the check race free would require holding locks across |
| 1518 | * calls to struct irq_domain_ops->alloc(), which could lead |
| 1519 | * to deadlock, so we just do a simple check before starting. |
| 1520 | */ |
| 1521 | desc = irq_to_desc(virq); |
| 1522 | if (!desc) |
| 1523 | return -EINVAL; |
| 1524 | if (WARN_ON(desc->action)) |
| 1525 | return -EBUSY; |
| 1526 | |
| 1527 | if (domain == NULL) |
| 1528 | return -EINVAL; |
| 1529 | |
| 1530 | if (WARN_ON(!irq_domain_is_hierarchy(domain))) |
| 1531 | return -EINVAL; |
| 1532 | |
Dan Carpenter | 20c4d49 | 2017-08-25 15:14:09 +0300 | [diff] [blame] | 1533 | if (!root_irq_data) |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1534 | return -EINVAL; |
| 1535 | |
Dan Carpenter | 20c4d49 | 2017-08-25 15:14:09 +0300 | [diff] [blame] | 1536 | if (domain->parent != root_irq_data->domain) |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1537 | return -EINVAL; |
| 1538 | |
| 1539 | child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL, |
| 1540 | irq_data_get_node(root_irq_data)); |
| 1541 | if (!child_irq_data) |
| 1542 | return -ENOMEM; |
| 1543 | |
| 1544 | mutex_lock(&irq_domain_mutex); |
| 1545 | |
| 1546 | /* Copy the original irq_data. */ |
| 1547 | *child_irq_data = *root_irq_data; |
| 1548 | |
| 1549 | /* |
| 1550 | * Overwrite the root_irq_data, which is embedded in struct |
| 1551 | * irq_desc, with values for this domain. |
| 1552 | */ |
| 1553 | root_irq_data->parent_data = child_irq_data; |
| 1554 | root_irq_data->domain = domain; |
| 1555 | root_irq_data->mask = 0; |
| 1556 | root_irq_data->hwirq = 0; |
| 1557 | root_irq_data->chip = NULL; |
| 1558 | root_irq_data->chip_data = NULL; |
| 1559 | |
| 1560 | /* May (probably does) set hwirq, chip, etc. */ |
| 1561 | rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg); |
| 1562 | if (rv) { |
| 1563 | /* Restore the original irq_data. */ |
| 1564 | *root_irq_data = *child_irq_data; |
Kevin Hao | 0f394da | 2020-01-20 12:35:47 +0800 | [diff] [blame] | 1565 | kfree(child_irq_data); |
David Daney | 495c38d | 2017-08-17 17:53:34 -0700 | [diff] [blame] | 1566 | goto error; |
| 1567 | } |
| 1568 | |
| 1569 | irq_domain_fix_revmap(child_irq_data); |
| 1570 | irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data); |
| 1571 | |
| 1572 | error: |
| 1573 | mutex_unlock(&irq_domain_mutex); |
| 1574 | |
| 1575 | return rv; |
| 1576 | } |
| 1577 | EXPORT_SYMBOL_GPL(irq_domain_push_irq); |
| 1578 | |
| 1579 | /** |
| 1580 | * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy. |
| 1581 | * @domain: Domain to remove. |
| 1582 | * @virq: Irq to remove the domain from. |
| 1583 | * |
| 1584 | * Undo the effects of a call to irq_domain_push_irq(). Must be |
| 1585 | * called either before request_irq() or after free_irq(). |
| 1586 | */ |
| 1587 | int irq_domain_pop_irq(struct irq_domain *domain, int virq) |
| 1588 | { |
| 1589 | struct irq_data *root_irq_data = irq_get_irq_data(virq); |
| 1590 | struct irq_data *child_irq_data; |
| 1591 | struct irq_data *tmp_irq_data; |
| 1592 | struct irq_desc *desc; |
| 1593 | |
| 1594 | /* |
| 1595 | * Check that no action is set, which indicates the virq is in |
| 1596 | * a state where this function doesn't have to deal with races |
| 1597 | * between interrupt handling and maintaining the hierarchy. |
| 1598 | * This will catch gross misuse. Attempting to make the check |
| 1599 | * race free would require holding locks across calls to |
| 1600 | * struct irq_domain_ops->free(), which could lead to |
| 1601 | * deadlock, so we just do a simple check before starting. |
| 1602 | */ |
| 1603 | desc = irq_to_desc(virq); |
| 1604 | if (!desc) |
| 1605 | return -EINVAL; |
| 1606 | if (WARN_ON(desc->action)) |
| 1607 | return -EBUSY; |
| 1608 | |
| 1609 | if (domain == NULL) |
| 1610 | return -EINVAL; |
| 1611 | |
| 1612 | if (!root_irq_data) |
| 1613 | return -EINVAL; |
| 1614 | |
| 1615 | tmp_irq_data = irq_domain_get_irq_data(domain, virq); |
| 1616 | |
| 1617 | /* We can only "pop" if this domain is at the top of the list */ |
| 1618 | if (WARN_ON(root_irq_data != tmp_irq_data)) |
| 1619 | return -EINVAL; |
| 1620 | |
| 1621 | if (WARN_ON(root_irq_data->domain != domain)) |
| 1622 | return -EINVAL; |
| 1623 | |
| 1624 | child_irq_data = root_irq_data->parent_data; |
| 1625 | if (WARN_ON(!child_irq_data)) |
| 1626 | return -EINVAL; |
| 1627 | |
| 1628 | mutex_lock(&irq_domain_mutex); |
| 1629 | |
| 1630 | root_irq_data->parent_data = NULL; |
| 1631 | |
| 1632 | irq_domain_clear_mapping(domain, root_irq_data->hwirq); |
| 1633 | irq_domain_free_irqs_hierarchy(domain, virq, 1); |
| 1634 | |
| 1635 | /* Restore the original irq_data. */ |
| 1636 | *root_irq_data = *child_irq_data; |
| 1637 | |
| 1638 | irq_domain_fix_revmap(root_irq_data); |
| 1639 | |
| 1640 | mutex_unlock(&irq_domain_mutex); |
| 1641 | |
| 1642 | kfree(child_irq_data); |
| 1643 | |
| 1644 | return 0; |
| 1645 | } |
| 1646 | EXPORT_SYMBOL_GPL(irq_domain_pop_irq); |
| 1647 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1648 | /** |
| 1649 | * irq_domain_free_irqs - Free IRQ number and associated data structures |
| 1650 | * @virq: base IRQ number |
| 1651 | * @nr_irqs: number of IRQs to free |
| 1652 | */ |
| 1653 | void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs) |
| 1654 | { |
| 1655 | struct irq_data *data = irq_get_irq_data(virq); |
| 1656 | int i; |
| 1657 | |
| 1658 | if (WARN(!data || !data->domain || !data->domain->ops->free, |
| 1659 | "NULL pointer, cannot free irq\n")) |
| 1660 | return; |
| 1661 | |
| 1662 | mutex_lock(&irq_domain_mutex); |
| 1663 | for (i = 0; i < nr_irqs; i++) |
| 1664 | irq_domain_remove_irq(virq + i); |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1665 | irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1666 | mutex_unlock(&irq_domain_mutex); |
| 1667 | |
| 1668 | irq_domain_free_irq_data(virq, nr_irqs); |
| 1669 | irq_free_descs(virq, nr_irqs); |
| 1670 | } |
| 1671 | |
| 1672 | /** |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1673 | * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain |
| 1674 | * @irq_base: Base IRQ number |
| 1675 | * @nr_irqs: Number of IRQs to allocate |
| 1676 | * @arg: Allocation data (arch/domain specific) |
| 1677 | * |
| 1678 | * Check whether the domain has been setup recursive. If not allocate |
| 1679 | * through the parent domain. |
| 1680 | */ |
| 1681 | int irq_domain_alloc_irqs_parent(struct irq_domain *domain, |
| 1682 | unsigned int irq_base, unsigned int nr_irqs, |
| 1683 | void *arg) |
| 1684 | { |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1685 | if (!domain->parent) |
| 1686 | return -ENOSYS; |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1687 | |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1688 | return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base, |
| 1689 | nr_irqs, arg); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1690 | } |
Quan Nguyen | 52b2a05 | 2016-03-03 21:56:52 +0700 | [diff] [blame] | 1691 | EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1692 | |
| 1693 | /** |
| 1694 | * irq_domain_free_irqs_parent - Free interrupts from parent domain |
| 1695 | * @irq_base: Base IRQ number |
| 1696 | * @nr_irqs: Number of IRQs to free |
| 1697 | * |
| 1698 | * Check whether the domain has been setup recursive. If not free |
| 1699 | * through the parent domain. |
| 1700 | */ |
| 1701 | void irq_domain_free_irqs_parent(struct irq_domain *domain, |
| 1702 | unsigned int irq_base, unsigned int nr_irqs) |
| 1703 | { |
Marc Zyngier | 6a6544e | 2017-06-20 22:17:44 +0100 | [diff] [blame] | 1704 | if (!domain->parent) |
| 1705 | return; |
| 1706 | |
| 1707 | irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1708 | } |
Quan Nguyen | 52b2a05 | 2016-03-03 21:56:52 +0700 | [diff] [blame] | 1709 | EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1710 | |
Marc Zyngier | 08d85f3 | 2017-01-17 16:00:48 +0000 | [diff] [blame] | 1711 | static void __irq_domain_deactivate_irq(struct irq_data *irq_data) |
| 1712 | { |
| 1713 | if (irq_data && irq_data->domain) { |
| 1714 | struct irq_domain *domain = irq_data->domain; |
| 1715 | |
| 1716 | if (domain->ops->deactivate) |
| 1717 | domain->ops->deactivate(domain, irq_data); |
| 1718 | if (irq_data->parent_data) |
| 1719 | __irq_domain_deactivate_irq(irq_data->parent_data); |
| 1720 | } |
| 1721 | } |
| 1722 | |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 1723 | static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve) |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 1724 | { |
| 1725 | int ret = 0; |
| 1726 | |
| 1727 | if (irqd && irqd->domain) { |
| 1728 | struct irq_domain *domain = irqd->domain; |
| 1729 | |
| 1730 | if (irqd->parent_data) |
Thomas Gleixner | 42e1cc2 | 2017-09-13 23:29:12 +0200 | [diff] [blame] | 1731 | ret = __irq_domain_activate_irq(irqd->parent_data, |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 1732 | reserve); |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 1733 | if (!ret && domain->ops->activate) { |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 1734 | ret = domain->ops->activate(domain, irqd, reserve); |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 1735 | /* Rollback in case of error */ |
| 1736 | if (ret && irqd->parent_data) |
| 1737 | __irq_domain_deactivate_irq(irqd->parent_data); |
| 1738 | } |
| 1739 | } |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1743 | /** |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1744 | * irq_domain_activate_irq - Call domain_ops->activate recursively to activate |
| 1745 | * interrupt |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 1746 | * @irq_data: Outermost irq_data associated with interrupt |
| 1747 | * @reserve: If set only reserve an interrupt vector instead of assigning one |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1748 | * |
| 1749 | * This is the second step to call domain_ops->activate to program interrupt |
| 1750 | * controllers, so the interrupt could actually get delivered. |
| 1751 | */ |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 1752 | int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve) |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1753 | { |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 1754 | int ret = 0; |
| 1755 | |
| 1756 | if (!irqd_is_activated(irq_data)) |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 1757 | ret = __irq_domain_activate_irq(irq_data, reserve); |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 1758 | if (!ret) |
Marc Zyngier | 08d85f3 | 2017-01-17 16:00:48 +0000 | [diff] [blame] | 1759 | irqd_set_activated(irq_data); |
Thomas Gleixner | bb9b428 | 2017-09-13 23:29:11 +0200 | [diff] [blame] | 1760 | return ret; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to |
| 1765 | * deactivate interrupt |
| 1766 | * @irq_data: outermost irq_data associated with interrupt |
| 1767 | * |
| 1768 | * It calls domain_ops->deactivate to program interrupt controllers to disable |
| 1769 | * interrupt delivery. |
| 1770 | */ |
| 1771 | void irq_domain_deactivate_irq(struct irq_data *irq_data) |
| 1772 | { |
Marc Zyngier | 08d85f3 | 2017-01-17 16:00:48 +0000 | [diff] [blame] | 1773 | if (irqd_is_activated(irq_data)) { |
| 1774 | __irq_domain_deactivate_irq(irq_data); |
| 1775 | irqd_clr_activated(irq_data); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | static void irq_domain_check_hierarchy(struct irq_domain *domain) |
| 1780 | { |
| 1781 | /* Hierarchy irq_domains must implement callback alloc() */ |
| 1782 | if (domain->ops->alloc) |
| 1783 | domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY; |
| 1784 | } |
Eric Auger | 631a963 | 2017-01-19 20:57:57 +0000 | [diff] [blame] | 1785 | |
| 1786 | /** |
| 1787 | * irq_domain_hierarchical_is_msi_remap - Check if the domain or any |
| 1788 | * parent has MSI remapping support |
| 1789 | * @domain: domain pointer |
| 1790 | */ |
| 1791 | bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain) |
| 1792 | { |
| 1793 | for (; domain; domain = domain->parent) { |
| 1794 | if (irq_domain_is_msi_remap(domain)) |
| 1795 | return true; |
| 1796 | } |
| 1797 | return false; |
| 1798 | } |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1799 | #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */ |
| 1800 | /** |
| 1801 | * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain |
| 1802 | * @domain: domain to match |
| 1803 | * @virq: IRQ number to get irq_data |
| 1804 | */ |
| 1805 | struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain, |
| 1806 | unsigned int virq) |
| 1807 | { |
| 1808 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 1809 | |
| 1810 | return (irq_data && irq_data->domain == domain) ? irq_data : NULL; |
| 1811 | } |
Jake Oshins | a4289dc | 2015-12-10 17:52:59 +0000 | [diff] [blame] | 1812 | EXPORT_SYMBOL_GPL(irq_domain_get_irq_data); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1813 | |
Stefan Agner | 5f22f5c | 2015-05-16 11:44:13 +0200 | [diff] [blame] | 1814 | /** |
| 1815 | * irq_domain_set_info - Set the complete data for a @virq in @domain |
| 1816 | * @domain: Interrupt domain to match |
| 1817 | * @virq: IRQ number |
| 1818 | * @hwirq: The hardware interrupt number |
| 1819 | * @chip: The associated interrupt chip |
| 1820 | * @chip_data: The associated interrupt chip data |
| 1821 | * @handler: The interrupt flow handler |
| 1822 | * @handler_data: The interrupt flow handler data |
| 1823 | * @handler_name: The interrupt handler name |
| 1824 | */ |
| 1825 | void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, |
| 1826 | irq_hw_number_t hwirq, struct irq_chip *chip, |
| 1827 | void *chip_data, irq_flow_handler_t handler, |
| 1828 | void *handler_data, const char *handler_name) |
| 1829 | { |
| 1830 | irq_set_chip_and_handler_name(virq, chip, handler, handler_name); |
| 1831 | irq_set_chip_data(virq, chip_data); |
| 1832 | irq_set_handler_data(virq, handler_data); |
| 1833 | } |
| 1834 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1835 | static void irq_domain_check_hierarchy(struct irq_domain *domain) |
| 1836 | { |
| 1837 | } |
| 1838 | #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1839 | |
| 1840 | #ifdef CONFIG_GENERIC_IRQ_DEBUGFS |
| 1841 | static struct dentry *domain_dir; |
| 1842 | |
| 1843 | static void |
| 1844 | irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind) |
| 1845 | { |
| 1846 | seq_printf(m, "%*sname: %s\n", ind, "", d->name); |
| 1847 | seq_printf(m, "%*ssize: %u\n", ind + 1, "", |
| 1848 | d->revmap_size + d->revmap_direct_max_irq); |
| 1849 | seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount); |
| 1850 | seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags); |
Thomas Gleixner | c3e7239 | 2017-09-13 23:29:06 +0200 | [diff] [blame] | 1851 | if (d->ops && d->ops->debug_show) |
| 1852 | d->ops->debug_show(m, d, NULL, ind + 1); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1853 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
| 1854 | if (!d->parent) |
| 1855 | return; |
| 1856 | seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name); |
| 1857 | irq_domain_debug_show_one(m, d->parent, ind + 4); |
| 1858 | #endif |
| 1859 | } |
| 1860 | |
| 1861 | static int irq_domain_debug_show(struct seq_file *m, void *p) |
| 1862 | { |
| 1863 | struct irq_domain *d = m->private; |
| 1864 | |
| 1865 | /* Default domain? Might be NULL */ |
| 1866 | if (!d) { |
| 1867 | if (!irq_default_domain) |
| 1868 | return 0; |
| 1869 | d = irq_default_domain; |
| 1870 | } |
| 1871 | irq_domain_debug_show_one(m, d, 0); |
| 1872 | return 0; |
| 1873 | } |
Andy Shevchenko | 0b24a0b | 2018-02-14 17:47:35 +0200 | [diff] [blame] | 1874 | DEFINE_SHOW_ATTRIBUTE(irq_domain_debug); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1875 | |
| 1876 | static void debugfs_add_domain_dir(struct irq_domain *d) |
| 1877 | { |
| 1878 | if (!d->name || !domain_dir || d->debugfs_file) |
| 1879 | return; |
| 1880 | d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d, |
Andy Shevchenko | 0b24a0b | 2018-02-14 17:47:35 +0200 | [diff] [blame] | 1881 | &irq_domain_debug_fops); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | static void debugfs_remove_domain_dir(struct irq_domain *d) |
| 1885 | { |
Thomas Gleixner | f610c9d | 2017-07-07 08:57:57 +0200 | [diff] [blame] | 1886 | debugfs_remove(d->debugfs_file); |
Marc Zyngier | 513145e | 2018-10-01 11:05:21 +0100 | [diff] [blame] | 1887 | d->debugfs_file = NULL; |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | void __init irq_domain_debugfs_init(struct dentry *root) |
| 1891 | { |
| 1892 | struct irq_domain *d; |
| 1893 | |
| 1894 | domain_dir = debugfs_create_dir("domains", root); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1895 | |
Andy Shevchenko | 0b24a0b | 2018-02-14 17:47:35 +0200 | [diff] [blame] | 1896 | debugfs_create_file("default", 0444, domain_dir, NULL, |
| 1897 | &irq_domain_debug_fops); |
Thomas Gleixner | 087cdfb | 2017-06-20 01:37:17 +0200 | [diff] [blame] | 1898 | mutex_lock(&irq_domain_mutex); |
| 1899 | list_for_each_entry(d, &irq_domain_list, link) |
| 1900 | debugfs_add_domain_dir(d); |
| 1901 | mutex_unlock(&irq_domain_mutex); |
| 1902 | } |
| 1903 | #endif |