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