Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 1 | #define pr_fmt(fmt) "irq: " fmt |
| 2 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 3 | #include <linux/debugfs.h> |
| 4 | #include <linux/hardirq.h> |
| 5 | #include <linux/interrupt.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 6 | #include <linux/irq.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 7 | #include <linux/irqdesc.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 8 | #include <linux/irqdomain.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/mutex.h> |
| 11 | #include <linux/of.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 12 | #include <linux/of_address.h> |
Rashika Kheria | 64be38a | 2014-02-27 17:10:12 +0530 | [diff] [blame] | 13 | #include <linux/of_irq.h> |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 14 | #include <linux/topology.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 15 | #include <linux/seq_file.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 16 | #include <linux/slab.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 17 | #include <linux/smp.h> |
| 18 | #include <linux/fs.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 19 | |
| 20 | static LIST_HEAD(irq_domain_list); |
| 21 | static DEFINE_MUTEX(irq_domain_mutex); |
| 22 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 23 | static DEFINE_MUTEX(revmap_trees_mutex); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 24 | static struct irq_domain *irq_default_domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 25 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 26 | static int irq_domain_alloc_descs(int virq, unsigned int nr_irqs, |
| 27 | irq_hw_number_t hwirq, int node); |
| 28 | static void irq_domain_check_hierarchy(struct irq_domain *domain); |
| 29 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 30 | /** |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 31 | * __irq_domain_add() - Allocate a new irq_domain data structure |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 32 | * @of_node: optional device-tree node of the interrupt controller |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 33 | * @size: Size of linear map; 0 for radix mapping only |
Jiang Liu | a257954 | 2014-05-27 16:07:37 +0800 | [diff] [blame] | 34 | * @hwirq_max: Maximum number of interrupts supported by controller |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 35 | * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no |
| 36 | * direct mapping |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 37 | * @ops: domain callbacks |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 38 | * @host_data: Controller private data pointer |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 39 | * |
Jiang Liu | a257954 | 2014-05-27 16:07:37 +0800 | [diff] [blame] | 40 | * Allocates and initialize and irq_domain structure. |
| 41 | * Returns pointer to IRQ domain, or NULL on failure. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 42 | */ |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 43 | struct irq_domain *__irq_domain_add(struct device_node *of_node, int size, |
| 44 | irq_hw_number_t hwirq_max, int direct_max, |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 45 | const struct irq_domain_ops *ops, |
| 46 | void *host_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 47 | { |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 48 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 49 | |
Grant Likely | cef5075 | 2012-07-11 17:24:31 +0100 | [diff] [blame] | 50 | domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size), |
| 51 | GFP_KERNEL, of_node_to_nid(of_node)); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 52 | if (WARN_ON(!domain)) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 53 | return NULL; |
| 54 | |
| 55 | /* Fill structure */ |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 56 | INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 57 | domain->ops = ops; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 58 | domain->host_data = host_data; |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 59 | domain->of_node = of_node_get(of_node); |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 60 | domain->hwirq_max = hwirq_max; |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 61 | domain->revmap_size = size; |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 62 | domain->revmap_direct_max_irq = direct_max; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 63 | irq_domain_check_hierarchy(domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 64 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 65 | mutex_lock(&irq_domain_mutex); |
| 66 | list_add(&domain->link, &irq_domain_list); |
| 67 | mutex_unlock(&irq_domain_mutex); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 68 | |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 69 | pr_debug("Added domain %s\n", domain->name); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 70 | return domain; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 71 | } |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(__irq_domain_add); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 73 | |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 74 | /** |
| 75 | * irq_domain_remove() - Remove an irq domain. |
| 76 | * @domain: domain to remove |
| 77 | * |
| 78 | * This routine is used to remove an irq domain. The caller must ensure |
| 79 | * that all mappings within the domain have been disposed of prior to |
| 80 | * use, depending on the revmap type. |
| 81 | */ |
| 82 | void irq_domain_remove(struct irq_domain *domain) |
| 83 | { |
| 84 | mutex_lock(&irq_domain_mutex); |
| 85 | |
Grant Likely | cef5075 | 2012-07-11 17:24:31 +0100 | [diff] [blame] | 86 | /* |
| 87 | * radix_tree_delete() takes care of destroying the root |
| 88 | * node when all entries are removed. Shout if there are |
| 89 | * any mappings left. |
| 90 | */ |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 91 | WARN_ON(domain->revmap_tree.height); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 92 | |
| 93 | list_del(&domain->link); |
| 94 | |
| 95 | /* |
| 96 | * If the going away domain is the default one, reset it. |
| 97 | */ |
| 98 | if (unlikely(irq_default_domain == domain)) |
| 99 | irq_set_default_host(NULL); |
| 100 | |
| 101 | mutex_unlock(&irq_domain_mutex); |
| 102 | |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 103 | pr_debug("Removed domain %s\n", domain->name); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 104 | |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 105 | of_node_put(domain->of_node); |
| 106 | kfree(domain); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 107 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(irq_domain_remove); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 109 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 110 | /** |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 111 | * 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] | 112 | * @of_node: pointer to interrupt controller's device tree node. |
| 113 | * @size: total number of irqs in mapping |
Linus Walleij | 94a63da | 2013-06-06 12:10:23 +0100 | [diff] [blame] | 114 | * @first_irq: first number of irq block assigned to the domain, |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 115 | * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then |
| 116 | * pre-map all of the irqs in the domain to virqs starting at first_irq. |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 117 | * @ops: domain callbacks |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 118 | * @host_data: Controller private data pointer |
| 119 | * |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 120 | * Allocates an irq_domain, and optionally if first_irq is positive then also |
| 121 | * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq. |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 122 | * |
| 123 | * This is intended to implement the expected behaviour for most |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 124 | * interrupt controllers. If device tree is used, then first_irq will be 0 and |
| 125 | * irqs get mapped dynamically on the fly. However, if the controller requires |
| 126 | * static virq assignments (non-DT boot) then it will set that up correctly. |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 127 | */ |
| 128 | struct irq_domain *irq_domain_add_simple(struct device_node *of_node, |
| 129 | unsigned int size, |
| 130 | unsigned int first_irq, |
| 131 | const struct irq_domain_ops *ops, |
| 132 | void *host_data) |
| 133 | { |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 134 | struct irq_domain *domain; |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 135 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 136 | domain = __irq_domain_add(of_node, size, size, 0, ops, host_data); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 137 | if (!domain) |
| 138 | return NULL; |
| 139 | |
| 140 | if (first_irq > 0) { |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 141 | if (IS_ENABLED(CONFIG_SPARSE_IRQ)) { |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 142 | /* attempt to allocated irq_descs */ |
| 143 | int rc = irq_alloc_descs(first_irq, first_irq, size, |
| 144 | of_node_to_nid(of_node)); |
| 145 | if (rc < 0) |
Linus Walleij | d202b7b | 2012-11-27 01:20:32 +0100 | [diff] [blame] | 146 | pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n", |
| 147 | first_irq); |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 148 | } |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 149 | irq_domain_associate_many(domain, first_irq, 0, size); |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Grant Likely | fa40f37 | 2013-06-08 12:57:40 +0100 | [diff] [blame] | 152 | return domain; |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 153 | } |
Arnd Bergmann | 346dbb7 | 2013-04-25 19:28:54 +0200 | [diff] [blame] | 154 | EXPORT_SYMBOL_GPL(irq_domain_add_simple); |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 155 | |
| 156 | /** |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 157 | * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain. |
| 158 | * @of_node: pointer to interrupt controller's device tree node. |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 159 | * @size: total number of irqs in legacy mapping |
| 160 | * @first_irq: first number of irq block assigned to the domain |
| 161 | * @first_hwirq: first hwirq number to use for the translation. Should normally |
| 162 | * be '0', but a positive integer can be used if the effective |
| 163 | * hwirqs numbering does not begin at zero. |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 164 | * @ops: map/unmap domain callbacks |
| 165 | * @host_data: Controller private data pointer |
| 166 | * |
| 167 | * Note: the map() callback will be called before this function returns |
| 168 | * for all legacy interrupts except 0 (which is always the invalid irq for |
| 169 | * a legacy controller). |
| 170 | */ |
| 171 | struct irq_domain *irq_domain_add_legacy(struct device_node *of_node, |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 172 | unsigned int size, |
| 173 | unsigned int first_irq, |
| 174 | irq_hw_number_t first_hwirq, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 175 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 176 | void *host_data) |
| 177 | { |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 178 | struct irq_domain *domain; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 179 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 180 | domain = __irq_domain_add(of_node, first_hwirq + size, |
| 181 | first_hwirq + size, 0, ops, host_data); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 182 | if (domain) |
| 183 | irq_domain_associate_many(domain, first_irq, first_hwirq, size); |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 184 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 185 | return domain; |
| 186 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 187 | EXPORT_SYMBOL_GPL(irq_domain_add_legacy); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 188 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 189 | /** |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 190 | * irq_find_matching_host() - Locates a domain for a given device node |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 191 | * @node: device-tree node of the interrupt controller |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 192 | * @bus_token: domain-specific data |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 193 | */ |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 194 | struct irq_domain *irq_find_matching_host(struct device_node *node, |
| 195 | enum irq_domain_bus_token bus_token) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 196 | { |
| 197 | struct irq_domain *h, *found = NULL; |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 198 | int rc; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 199 | |
| 200 | /* We might want to match the legacy controller last since |
| 201 | * it might potentially be set to match all interrupts in |
| 202 | * the absence of a device node. This isn't a problem so far |
| 203 | * yet though... |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 204 | * |
| 205 | * bus_token == DOMAIN_BUS_ANY matches any domain, any other |
| 206 | * values must generate an exact match for the domain to be |
| 207 | * selected. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 208 | */ |
| 209 | mutex_lock(&irq_domain_mutex); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 210 | list_for_each_entry(h, &irq_domain_list, link) { |
| 211 | if (h->ops->match) |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 212 | rc = h->ops->match(h, node, bus_token); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 213 | else |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 214 | rc = ((h->of_node != NULL) && (h->of_node == node) && |
| 215 | ((bus_token == DOMAIN_BUS_ANY) || |
| 216 | (h->bus_token == bus_token))); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 217 | |
| 218 | if (rc) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 219 | found = h; |
| 220 | break; |
| 221 | } |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 222 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 223 | mutex_unlock(&irq_domain_mutex); |
| 224 | return found; |
| 225 | } |
Marc Zyngier | ad3aedf | 2015-07-28 14:46:08 +0100 | [diff] [blame^] | 226 | EXPORT_SYMBOL_GPL(irq_find_matching_host); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * irq_set_default_host() - Set a "default" irq domain |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 230 | * @domain: default domain pointer |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 231 | * |
| 232 | * For convenience, it's possible to set a "default" domain that will be used |
| 233 | * whenever NULL is passed to irq_create_mapping(). It makes life easier for |
| 234 | * platforms that want to manipulate a few hard coded interrupt numbers that |
| 235 | * aren't properly represented in the device-tree. |
| 236 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 237 | void irq_set_default_host(struct irq_domain *domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 238 | { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 239 | pr_debug("Default domain set to @0x%p\n", domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 240 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 241 | irq_default_domain = domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 242 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 243 | EXPORT_SYMBOL_GPL(irq_set_default_host); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 244 | |
Jiang Liu | 43a7759 | 2014-06-09 16:20:05 +0800 | [diff] [blame] | 245 | void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq) |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 246 | { |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 247 | struct irq_data *irq_data = irq_get_irq_data(irq); |
| 248 | irq_hw_number_t hwirq; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 249 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 250 | if (WARN(!irq_data || irq_data->domain != domain, |
| 251 | "virq%i doesn't exist; cannot disassociate\n", irq)) |
| 252 | return; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 253 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 254 | hwirq = irq_data->hwirq; |
| 255 | irq_set_status_flags(irq, IRQ_NOREQUEST); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 256 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 257 | /* remove chip and handler */ |
| 258 | irq_set_chip_and_handler(irq, NULL, NULL); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 259 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 260 | /* Make sure it's completed */ |
| 261 | synchronize_irq(irq); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 262 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 263 | /* Tell the PIC about it */ |
| 264 | if (domain->ops->unmap) |
| 265 | domain->ops->unmap(domain, irq); |
| 266 | smp_mb(); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 267 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 268 | irq_data->domain = NULL; |
| 269 | irq_data->hwirq = 0; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 270 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 271 | /* Clear reverse map for this hwirq */ |
| 272 | if (hwirq < domain->revmap_size) { |
| 273 | domain->linear_revmap[hwirq] = 0; |
| 274 | } else { |
| 275 | mutex_lock(&revmap_trees_mutex); |
| 276 | radix_tree_delete(&domain->revmap_tree, hwirq); |
| 277 | mutex_unlock(&revmap_trees_mutex); |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 281 | int irq_domain_associate(struct irq_domain *domain, unsigned int virq, |
| 282 | irq_hw_number_t hwirq) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 283 | { |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 284 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 285 | int ret; |
| 286 | |
| 287 | if (WARN(hwirq >= domain->hwirq_max, |
| 288 | "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name)) |
| 289 | return -EINVAL; |
| 290 | if (WARN(!irq_data, "error: virq%i is not allocated", virq)) |
| 291 | return -EINVAL; |
| 292 | if (WARN(irq_data->domain, "error: virq%i is already associated", virq)) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | mutex_lock(&irq_domain_mutex); |
| 296 | irq_data->hwirq = hwirq; |
| 297 | irq_data->domain = domain; |
| 298 | if (domain->ops->map) { |
| 299 | ret = domain->ops->map(domain, virq, hwirq); |
| 300 | if (ret != 0) { |
| 301 | /* |
| 302 | * If map() returns -EPERM, this interrupt is protected |
| 303 | * by the firmware or some other service and shall not |
| 304 | * be mapped. Don't bother telling the user about it. |
| 305 | */ |
| 306 | if (ret != -EPERM) { |
| 307 | pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n", |
| 308 | domain->name, hwirq, virq, ret); |
| 309 | } |
| 310 | irq_data->domain = NULL; |
| 311 | irq_data->hwirq = 0; |
| 312 | mutex_unlock(&irq_domain_mutex); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | /* If not already assigned, give the domain the chip's name */ |
| 317 | if (!domain->name && irq_data->chip) |
| 318 | domain->name = irq_data->chip->name; |
| 319 | } |
| 320 | |
| 321 | if (hwirq < domain->revmap_size) { |
| 322 | domain->linear_revmap[hwirq] = virq; |
| 323 | } else { |
| 324 | mutex_lock(&revmap_trees_mutex); |
| 325 | radix_tree_insert(&domain->revmap_tree, hwirq, irq_data); |
| 326 | mutex_unlock(&revmap_trees_mutex); |
| 327 | } |
| 328 | mutex_unlock(&irq_domain_mutex); |
| 329 | |
| 330 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(irq_domain_associate); |
| 335 | |
| 336 | void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base, |
| 337 | irq_hw_number_t hwirq_base, int count) |
| 338 | { |
| 339 | int i; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 340 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 341 | pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__, |
| 342 | of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count); |
| 343 | |
| 344 | for (i = 0; i < count; i++) { |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 345 | irq_domain_associate(domain, irq_base + i, hwirq_base + i); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 346 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 347 | } |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(irq_domain_associate_many); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * irq_create_direct_mapping() - Allocate an irq for direct mapping |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 352 | * @domain: domain to allocate the irq for or NULL for default domain |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 353 | * |
| 354 | * This routine is used for irq controllers which can choose the hardware |
| 355 | * 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] | 356 | * the linux irq as the hardware interrupt number. It still uses the linear |
| 357 | * or radix tree to store the mapping, but the irq controller can optimize |
| 358 | * the revmap path by using the hwirq directly. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 359 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 360 | unsigned int irq_create_direct_mapping(struct irq_domain *domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 361 | { |
| 362 | unsigned int virq; |
| 363 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 364 | if (domain == NULL) |
| 365 | domain = irq_default_domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 366 | |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 367 | virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node)); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 368 | if (!virq) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 369 | pr_debug("create_direct virq allocation failed\n"); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 370 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 371 | } |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 372 | if (virq >= domain->revmap_direct_max_irq) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 373 | pr_err("ERROR: no free irqs available below %i maximum\n", |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 374 | domain->revmap_direct_max_irq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 375 | irq_free_desc(virq); |
| 376 | return 0; |
| 377 | } |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 378 | pr_debug("create_direct obtained virq %d\n", virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 379 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 380 | if (irq_domain_associate(domain, virq, virq)) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 381 | irq_free_desc(virq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 382 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return virq; |
| 386 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 387 | EXPORT_SYMBOL_GPL(irq_create_direct_mapping); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * irq_create_mapping() - Map a hardware interrupt into linux irq space |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 391 | * @domain: domain owning this hardware interrupt or NULL for default domain |
| 392 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 393 | * |
| 394 | * Only one mapping per hardware interrupt is permitted. Returns a linux |
| 395 | * irq number. |
| 396 | * If the sense/trigger is to be specified, set_irq_type() should be called |
| 397 | * on the number returned from that call. |
| 398 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 399 | unsigned int irq_create_mapping(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 400 | irq_hw_number_t hwirq) |
| 401 | { |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 402 | int virq; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 403 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 404 | pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 405 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 406 | /* Look for default domain if nececssary */ |
| 407 | if (domain == NULL) |
| 408 | domain = irq_default_domain; |
| 409 | if (domain == NULL) { |
Kefeng Wang | 798f0fd | 2013-06-06 19:20:27 +0800 | [diff] [blame] | 410 | WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 411 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 412 | } |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 413 | pr_debug("-> using domain @%p\n", domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 414 | |
| 415 | /* Check if mapping already exists */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 416 | virq = irq_find_mapping(domain, hwirq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 417 | if (virq) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 418 | pr_debug("-> existing mapping on virq %d\n", virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 419 | return virq; |
| 420 | } |
| 421 | |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 422 | /* Allocate a virtual interrupt number */ |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 423 | virq = irq_domain_alloc_descs(-1, 1, hwirq, |
| 424 | of_node_to_nid(domain->of_node)); |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 425 | if (virq <= 0) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 426 | pr_debug("-> virq allocation failed\n"); |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 427 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 430 | if (irq_domain_associate(domain, virq, hwirq)) { |
Grant Likely | 7325570 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 431 | irq_free_desc(virq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 432 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 435 | pr_debug("irq %lu on domain %s mapped to virtual irq %u\n", |
Grant Likely | efd68e7 | 2012-06-03 22:04:33 -0700 | [diff] [blame] | 436 | hwirq, of_node_full_name(domain->of_node), virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 437 | |
| 438 | return virq; |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(irq_create_mapping); |
| 441 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 442 | /** |
| 443 | * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs |
| 444 | * @domain: domain owning the interrupt range |
| 445 | * @irq_base: beginning of linux IRQ range |
| 446 | * @hwirq_base: beginning of hardware IRQ range |
| 447 | * @count: Number of interrupts to map |
| 448 | * |
| 449 | * This routine is used for allocating and mapping a range of hardware |
| 450 | * irqs to linux irqs where the linux irq numbers are at pre-defined |
| 451 | * locations. For use by controllers that already have static mappings |
| 452 | * to insert in to the domain. |
| 453 | * |
| 454 | * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time |
| 455 | * domain insertion. |
| 456 | * |
| 457 | * 0 is returned upon success, while any failure to establish a static |
| 458 | * mapping is treated as an error. |
| 459 | */ |
| 460 | int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base, |
| 461 | irq_hw_number_t hwirq_base, int count) |
| 462 | { |
| 463 | int ret; |
| 464 | |
| 465 | ret = irq_alloc_descs(irq_base, irq_base, count, |
| 466 | of_node_to_nid(domain->of_node)); |
| 467 | if (unlikely(ret < 0)) |
| 468 | return ret; |
| 469 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 470 | irq_domain_associate_many(domain, irq_base, hwirq_base, count); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(irq_create_strict_mappings); |
| 474 | |
Grant Likely | e6d30ab | 2013-09-15 16:55:53 +0100 | [diff] [blame] | 475 | unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 476 | { |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 477 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 478 | irq_hw_number_t hwirq; |
| 479 | unsigned int type = IRQ_TYPE_NONE; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 480 | int virq; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 481 | |
Grant Likely | e6d30ab | 2013-09-15 16:55:53 +0100 | [diff] [blame] | 482 | domain = irq_data->np ? irq_find_host(irq_data->np) : irq_default_domain; |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 483 | if (!domain) { |
Kefeng Wang | 798f0fd | 2013-06-06 19:20:27 +0800 | [diff] [blame] | 484 | pr_warn("no irq domain found for %s !\n", |
Grant Likely | e6d30ab | 2013-09-15 16:55:53 +0100 | [diff] [blame] | 485 | of_node_full_name(irq_data->np)); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 486 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 489 | /* If domain has no translation, then we assume interrupt line */ |
| 490 | if (domain->ops->xlate == NULL) |
Grant Likely | e6d30ab | 2013-09-15 16:55:53 +0100 | [diff] [blame] | 491 | hwirq = irq_data->args[0]; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 492 | else { |
Grant Likely | e6d30ab | 2013-09-15 16:55:53 +0100 | [diff] [blame] | 493 | if (domain->ops->xlate(domain, irq_data->np, irq_data->args, |
| 494 | irq_data->args_count, &hwirq, &type)) |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 495 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Yingjoe Chen | 0cc01ab | 2014-11-06 22:20:15 +0800 | [diff] [blame] | 498 | if (irq_domain_is_hierarchy(domain)) { |
| 499 | /* |
| 500 | * If we've already configured this interrupt, |
| 501 | * don't do it again, or hell will break loose. |
| 502 | */ |
| 503 | virq = irq_find_mapping(domain, hwirq); |
| 504 | if (virq) |
| 505 | return virq; |
| 506 | |
| 507 | virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, irq_data); |
| 508 | if (virq <= 0) |
| 509 | return 0; |
| 510 | } else { |
| 511 | /* Create mapping */ |
| 512 | virq = irq_create_mapping(domain, hwirq); |
| 513 | if (!virq) |
| 514 | return virq; |
| 515 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 516 | |
| 517 | /* Set type if specified and different than the current one */ |
| 518 | if (type != IRQ_TYPE_NONE && |
Javier Martinez Canillas | fbab62c5 | 2013-06-14 18:40:49 +0200 | [diff] [blame] | 519 | type != irq_get_trigger_type(virq)) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 520 | irq_set_irq_type(virq, type); |
| 521 | return virq; |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 524 | |
| 525 | /** |
| 526 | * irq_dispose_mapping() - Unmap an interrupt |
| 527 | * @virq: linux irq number of the interrupt to unmap |
| 528 | */ |
| 529 | void irq_dispose_mapping(unsigned int virq) |
| 530 | { |
| 531 | struct irq_data *irq_data = irq_get_irq_data(virq); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 532 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 533 | |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 534 | if (!virq || !irq_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 535 | return; |
| 536 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 537 | domain = irq_data->domain; |
| 538 | if (WARN_ON(domain == NULL)) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 539 | return; |
| 540 | |
Grant Likely | ddaf144 | 2013-06-10 01:06:02 +0100 | [diff] [blame] | 541 | irq_domain_disassociate(domain, virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 542 | irq_free_desc(virq); |
| 543 | } |
| 544 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
| 545 | |
| 546 | /** |
| 547 | * irq_find_mapping() - Find a linux irq from an hw irq number. |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 548 | * @domain: domain owning this hardware interrupt |
| 549 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 550 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 551 | unsigned int irq_find_mapping(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 552 | irq_hw_number_t hwirq) |
| 553 | { |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 554 | struct irq_data *data; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 555 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 556 | /* Look for default domain if nececssary */ |
| 557 | if (domain == NULL) |
| 558 | domain = irq_default_domain; |
| 559 | if (domain == NULL) |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 560 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 561 | |
Grant Likely | 1aa0dd9 | 2013-06-08 12:03:59 +0100 | [diff] [blame] | 562 | if (hwirq < domain->revmap_direct_max_irq) { |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 563 | data = irq_domain_get_irq_data(domain, hwirq); |
| 564 | if (data && data->hwirq == hwirq) |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 565 | return hwirq; |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Grant Likely | d3dcb43 | 2013-06-10 12:19:17 +0100 | [diff] [blame] | 568 | /* Check if the hwirq is in the linear revmap. */ |
| 569 | if (hwirq < domain->revmap_size) |
| 570 | return domain->linear_revmap[hwirq]; |
| 571 | |
| 572 | rcu_read_lock(); |
| 573 | data = radix_tree_lookup(&domain->revmap_tree, hwirq); |
| 574 | rcu_read_unlock(); |
| 575 | return data ? data->irq : 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 576 | } |
| 577 | EXPORT_SYMBOL_GPL(irq_find_mapping); |
| 578 | |
Grant Likely | 092b2fb | 2012-03-29 14:10:30 -0600 | [diff] [blame] | 579 | #ifdef CONFIG_IRQ_DOMAIN_DEBUG |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 580 | static int virq_debug_show(struct seq_file *m, void *private) |
| 581 | { |
| 582 | unsigned long flags; |
| 583 | struct irq_desc *desc; |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 584 | struct irq_domain *domain; |
| 585 | struct radix_tree_iter iter; |
| 586 | void *data, **slot; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 587 | int i; |
| 588 | |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 589 | seq_printf(m, " %-16s %-6s %-10s %-10s %s\n", |
| 590 | "name", "mapped", "linear-max", "direct-max", "devtree-node"); |
| 591 | mutex_lock(&irq_domain_mutex); |
| 592 | list_for_each_entry(domain, &irq_domain_list, link) { |
| 593 | int count = 0; |
| 594 | radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0) |
| 595 | count++; |
| 596 | seq_printf(m, "%c%-16s %6u %10u %10u %s\n", |
| 597 | domain == irq_default_domain ? '*' : ' ', domain->name, |
| 598 | domain->revmap_size + count, domain->revmap_size, |
| 599 | domain->revmap_direct_max_irq, |
| 600 | domain->of_node ? of_node_full_name(domain->of_node) : ""); |
| 601 | } |
| 602 | mutex_unlock(&irq_domain_mutex); |
| 603 | |
| 604 | seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq", |
Grant Likely | 5269a9a | 2012-04-12 14:42:15 -0600 | [diff] [blame] | 605 | "chip name", (int)(2 * sizeof(void *) + 2), "chip data", |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 606 | "active", "type", "domain"); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 607 | |
| 608 | for (i = 1; i < nr_irqs; i++) { |
| 609 | desc = irq_to_desc(i); |
| 610 | if (!desc) |
| 611 | continue; |
| 612 | |
| 613 | raw_spin_lock_irqsave(&desc->lock, flags); |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 614 | domain = desc->irq_data.domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 615 | |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 616 | if (domain) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 617 | struct irq_chip *chip; |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 618 | int hwirq = desc->irq_data.hwirq; |
| 619 | bool direct; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 620 | |
| 621 | seq_printf(m, "%5d ", i); |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 622 | seq_printf(m, "0x%05x ", hwirq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 623 | |
| 624 | chip = irq_desc_get_chip(desc); |
Grant Likely | 0bb4afb | 2013-06-06 14:23:30 +0100 | [diff] [blame] | 625 | seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none"); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 626 | |
| 627 | data = irq_desc_get_chip_data(desc); |
Grant Likely | 15e06bf | 2012-04-11 00:26:25 -0600 | [diff] [blame] | 628 | seq_printf(m, data ? "0x%p " : " %p ", data); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 629 | |
Grant Likely | 1400ea8 | 2013-06-06 22:20:44 +0100 | [diff] [blame] | 630 | seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' '); |
| 631 | direct = (i == hwirq) && (i < domain->revmap_direct_max_irq); |
| 632 | seq_printf(m, "%6s%-8s ", |
| 633 | (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX", |
| 634 | direct ? "(DIRECT)" : ""); |
Grant Likely | 0bb4afb | 2013-06-06 14:23:30 +0100 | [diff] [blame] | 635 | seq_printf(m, "%s\n", desc->irq_data.domain->name); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static int virq_debug_open(struct inode *inode, struct file *file) |
| 645 | { |
| 646 | return single_open(file, virq_debug_show, inode->i_private); |
| 647 | } |
| 648 | |
| 649 | static const struct file_operations virq_debug_fops = { |
| 650 | .open = virq_debug_open, |
| 651 | .read = seq_read, |
| 652 | .llseek = seq_lseek, |
| 653 | .release = single_release, |
| 654 | }; |
| 655 | |
| 656 | static int __init irq_debugfs_init(void) |
| 657 | { |
Grant Likely | 092b2fb | 2012-03-29 14:10:30 -0600 | [diff] [blame] | 658 | if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 659 | NULL, &virq_debug_fops) == NULL) |
| 660 | return -ENOMEM; |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | __initcall(irq_debugfs_init); |
Grant Likely | 092b2fb | 2012-03-29 14:10:30 -0600 | [diff] [blame] | 665 | #endif /* CONFIG_IRQ_DOMAIN_DEBUG */ |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 666 | |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 667 | /** |
| 668 | * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings |
| 669 | * |
| 670 | * Device Tree IRQ specifier translation function which works with one cell |
| 671 | * bindings where the cell value maps directly to the hwirq number. |
| 672 | */ |
| 673 | int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr, |
| 674 | const u32 *intspec, unsigned int intsize, |
| 675 | unsigned long *out_hwirq, unsigned int *out_type) |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 676 | { |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 677 | if (WARN_ON(intsize < 1)) |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 678 | return -EINVAL; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 679 | *out_hwirq = intspec[0]; |
| 680 | *out_type = IRQ_TYPE_NONE; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 681 | return 0; |
| 682 | } |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 683 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell); |
| 684 | |
| 685 | /** |
| 686 | * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings |
| 687 | * |
| 688 | * Device Tree IRQ specifier translation function which works with two cell |
| 689 | * bindings where the cell values map directly to the hwirq number |
| 690 | * and linux irq flags. |
| 691 | */ |
| 692 | int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr, |
| 693 | const u32 *intspec, unsigned int intsize, |
| 694 | irq_hw_number_t *out_hwirq, unsigned int *out_type) |
| 695 | { |
| 696 | if (WARN_ON(intsize < 2)) |
| 697 | return -EINVAL; |
| 698 | *out_hwirq = intspec[0]; |
| 699 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 700 | return 0; |
| 701 | } |
| 702 | EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell); |
| 703 | |
| 704 | /** |
| 705 | * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings |
| 706 | * |
| 707 | * Device Tree IRQ specifier translation function which works with either one |
| 708 | * or two cell bindings where the cell values map directly to the hwirq number |
| 709 | * and linux irq flags. |
| 710 | * |
| 711 | * Note: don't use this function unless your interrupt controller explicitly |
| 712 | * supports both one and two cell bindings. For the majority of controllers |
| 713 | * the _onecell() or _twocell() variants above should be used. |
| 714 | */ |
| 715 | int irq_domain_xlate_onetwocell(struct irq_domain *d, |
| 716 | struct device_node *ctrlr, |
| 717 | const u32 *intspec, unsigned int intsize, |
| 718 | unsigned long *out_hwirq, unsigned int *out_type) |
| 719 | { |
| 720 | if (WARN_ON(intsize < 1)) |
| 721 | return -EINVAL; |
| 722 | *out_hwirq = intspec[0]; |
| 723 | *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE; |
| 724 | return 0; |
| 725 | } |
| 726 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell); |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 727 | |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 728 | const struct irq_domain_ops irq_domain_simple_ops = { |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 729 | .xlate = irq_domain_xlate_onetwocell, |
Grant Likely | 7529495 | 2012-02-14 14:06:57 -0700 | [diff] [blame] | 730 | }; |
| 731 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 732 | |
| 733 | static int irq_domain_alloc_descs(int virq, unsigned int cnt, |
| 734 | irq_hw_number_t hwirq, int node) |
| 735 | { |
| 736 | unsigned int hint; |
| 737 | |
| 738 | if (virq >= 0) { |
| 739 | virq = irq_alloc_descs(virq, virq, cnt, node); |
| 740 | } else { |
| 741 | hint = hwirq % nr_irqs; |
| 742 | if (hint == 0) |
| 743 | hint++; |
| 744 | virq = irq_alloc_descs_from(hint, cnt, node); |
| 745 | if (virq <= 0 && hint > 1) |
| 746 | virq = irq_alloc_descs_from(1, cnt, node); |
| 747 | } |
| 748 | |
| 749 | return virq; |
| 750 | } |
| 751 | |
| 752 | #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY |
Jiang Liu | afb7da8 | 2014-11-15 22:24:02 +0800 | [diff] [blame] | 753 | /** |
| 754 | * irq_domain_add_hierarchy - Add a irqdomain into the hierarchy |
| 755 | * @parent: Parent irq domain to associate with the new domain |
| 756 | * @flags: Irq domain flags associated to the domain |
| 757 | * @size: Size of the domain. See below |
| 758 | * @node: Optional device-tree node of the interrupt controller |
| 759 | * @ops: Pointer to the interrupt domain callbacks |
| 760 | * @host_data: Controller private data pointer |
| 761 | * |
| 762 | * If @size is 0 a tree domain is created, otherwise a linear domain. |
| 763 | * |
| 764 | * If successful the parent is associated to the new domain and the |
| 765 | * domain flags are set. |
| 766 | * Returns pointer to IRQ domain, or NULL on failure. |
| 767 | */ |
| 768 | struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent, |
| 769 | unsigned int flags, |
| 770 | unsigned int size, |
| 771 | struct device_node *node, |
| 772 | const struct irq_domain_ops *ops, |
| 773 | void *host_data) |
| 774 | { |
| 775 | struct irq_domain *domain; |
| 776 | |
| 777 | if (size) |
| 778 | domain = irq_domain_add_linear(node, size, ops, host_data); |
| 779 | else |
| 780 | domain = irq_domain_add_tree(node, ops, host_data); |
| 781 | if (domain) { |
| 782 | domain->parent = parent; |
| 783 | domain->flags |= flags; |
| 784 | } |
| 785 | |
| 786 | return domain; |
| 787 | } |
| 788 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 789 | static void irq_domain_insert_irq(int virq) |
| 790 | { |
| 791 | struct irq_data *data; |
| 792 | |
| 793 | for (data = irq_get_irq_data(virq); data; data = data->parent_data) { |
| 794 | struct irq_domain *domain = data->domain; |
| 795 | irq_hw_number_t hwirq = data->hwirq; |
| 796 | |
| 797 | if (hwirq < domain->revmap_size) { |
| 798 | domain->linear_revmap[hwirq] = virq; |
| 799 | } else { |
| 800 | mutex_lock(&revmap_trees_mutex); |
| 801 | radix_tree_insert(&domain->revmap_tree, hwirq, data); |
| 802 | mutex_unlock(&revmap_trees_mutex); |
| 803 | } |
| 804 | |
| 805 | /* If not already assigned, give the domain the chip's name */ |
| 806 | if (!domain->name && data->chip) |
| 807 | domain->name = data->chip->name; |
| 808 | } |
| 809 | |
| 810 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
| 811 | } |
| 812 | |
| 813 | static void irq_domain_remove_irq(int virq) |
| 814 | { |
| 815 | struct irq_data *data; |
| 816 | |
| 817 | irq_set_status_flags(virq, IRQ_NOREQUEST); |
| 818 | irq_set_chip_and_handler(virq, NULL, NULL); |
| 819 | synchronize_irq(virq); |
| 820 | smp_mb(); |
| 821 | |
| 822 | for (data = irq_get_irq_data(virq); data; data = data->parent_data) { |
| 823 | struct irq_domain *domain = data->domain; |
| 824 | irq_hw_number_t hwirq = data->hwirq; |
| 825 | |
| 826 | if (hwirq < domain->revmap_size) { |
| 827 | domain->linear_revmap[hwirq] = 0; |
| 828 | } else { |
| 829 | mutex_lock(&revmap_trees_mutex); |
| 830 | radix_tree_delete(&domain->revmap_tree, hwirq); |
| 831 | mutex_unlock(&revmap_trees_mutex); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain, |
| 837 | struct irq_data *child) |
| 838 | { |
| 839 | struct irq_data *irq_data; |
| 840 | |
Jiang Liu | 6783011 | 2015-06-01 16:05:13 +0800 | [diff] [blame] | 841 | irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL, |
| 842 | irq_data_get_node(child)); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 843 | if (irq_data) { |
| 844 | child->parent_data = irq_data; |
| 845 | irq_data->irq = child->irq; |
Jiang Liu | 0d0b4c8 | 2015-06-01 16:05:12 +0800 | [diff] [blame] | 846 | irq_data->common = child->common; |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 847 | irq_data->node = child->node; |
| 848 | irq_data->domain = domain; |
| 849 | } |
| 850 | |
| 851 | return irq_data; |
| 852 | } |
| 853 | |
| 854 | static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs) |
| 855 | { |
| 856 | struct irq_data *irq_data, *tmp; |
| 857 | int i; |
| 858 | |
| 859 | for (i = 0; i < nr_irqs; i++) { |
| 860 | irq_data = irq_get_irq_data(virq + i); |
| 861 | tmp = irq_data->parent_data; |
| 862 | irq_data->parent_data = NULL; |
| 863 | irq_data->domain = NULL; |
| 864 | |
| 865 | while (tmp) { |
| 866 | irq_data = tmp; |
| 867 | tmp = tmp->parent_data; |
| 868 | kfree(irq_data); |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | static int irq_domain_alloc_irq_data(struct irq_domain *domain, |
| 874 | unsigned int virq, unsigned int nr_irqs) |
| 875 | { |
| 876 | struct irq_data *irq_data; |
| 877 | struct irq_domain *parent; |
| 878 | int i; |
| 879 | |
| 880 | /* The outermost irq_data is embedded in struct irq_desc */ |
| 881 | for (i = 0; i < nr_irqs; i++) { |
| 882 | irq_data = irq_get_irq_data(virq + i); |
| 883 | irq_data->domain = domain; |
| 884 | |
| 885 | for (parent = domain->parent; parent; parent = parent->parent) { |
| 886 | irq_data = irq_domain_insert_irq_data(parent, irq_data); |
| 887 | if (!irq_data) { |
| 888 | irq_domain_free_irq_data(virq, i + 1); |
| 889 | return -ENOMEM; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain |
| 899 | * @domain: domain to match |
| 900 | * @virq: IRQ number to get irq_data |
| 901 | */ |
| 902 | struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain, |
| 903 | unsigned int virq) |
| 904 | { |
| 905 | struct irq_data *irq_data; |
| 906 | |
| 907 | for (irq_data = irq_get_irq_data(virq); irq_data; |
| 908 | irq_data = irq_data->parent_data) |
| 909 | if (irq_data->domain == domain) |
| 910 | return irq_data; |
| 911 | |
| 912 | return NULL; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain |
| 917 | * @domain: Interrupt domain to match |
| 918 | * @virq: IRQ number |
| 919 | * @hwirq: The hwirq number |
| 920 | * @chip: The associated interrupt chip |
| 921 | * @chip_data: The associated chip data |
| 922 | */ |
| 923 | int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq, |
| 924 | irq_hw_number_t hwirq, struct irq_chip *chip, |
| 925 | void *chip_data) |
| 926 | { |
| 927 | struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq); |
| 928 | |
| 929 | if (!irq_data) |
| 930 | return -ENOENT; |
| 931 | |
| 932 | irq_data->hwirq = hwirq; |
| 933 | irq_data->chip = chip ? chip : &no_irq_chip; |
| 934 | irq_data->chip_data = chip_data; |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | /** |
Jiang Liu | 1b53770 | 2014-11-09 23:10:24 +0800 | [diff] [blame] | 940 | * irq_domain_set_info - Set the complete data for a @virq in @domain |
| 941 | * @domain: Interrupt domain to match |
| 942 | * @virq: IRQ number |
| 943 | * @hwirq: The hardware interrupt number |
| 944 | * @chip: The associated interrupt chip |
| 945 | * @chip_data: The associated interrupt chip data |
| 946 | * @handler: The interrupt flow handler |
| 947 | * @handler_data: The interrupt flow handler data |
| 948 | * @handler_name: The interrupt handler name |
| 949 | */ |
| 950 | void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, |
| 951 | irq_hw_number_t hwirq, struct irq_chip *chip, |
| 952 | void *chip_data, irq_flow_handler_t handler, |
| 953 | void *handler_data, const char *handler_name) |
| 954 | { |
| 955 | irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data); |
| 956 | __irq_set_handler(virq, handler, 0, handler_name); |
| 957 | irq_set_handler_data(virq, handler_data); |
| 958 | } |
| 959 | |
| 960 | /** |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 961 | * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data |
| 962 | * @irq_data: The pointer to irq_data |
| 963 | */ |
| 964 | void irq_domain_reset_irq_data(struct irq_data *irq_data) |
| 965 | { |
| 966 | irq_data->hwirq = 0; |
| 967 | irq_data->chip = &no_irq_chip; |
| 968 | irq_data->chip_data = NULL; |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * irq_domain_free_irqs_common - Clear irq_data and free the parent |
| 973 | * @domain: Interrupt domain to match |
| 974 | * @virq: IRQ number to start with |
| 975 | * @nr_irqs: The number of irqs to free |
| 976 | */ |
| 977 | void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq, |
| 978 | unsigned int nr_irqs) |
| 979 | { |
| 980 | struct irq_data *irq_data; |
| 981 | int i; |
| 982 | |
| 983 | for (i = 0; i < nr_irqs; i++) { |
| 984 | irq_data = irq_domain_get_irq_data(domain, virq + i); |
| 985 | if (irq_data) |
| 986 | irq_domain_reset_irq_data(irq_data); |
| 987 | } |
| 988 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent |
| 993 | * @domain: Interrupt domain to match |
| 994 | * @virq: IRQ number to start with |
| 995 | * @nr_irqs: The number of irqs to free |
| 996 | */ |
| 997 | void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq, |
| 998 | unsigned int nr_irqs) |
| 999 | { |
| 1000 | int i; |
| 1001 | |
| 1002 | for (i = 0; i < nr_irqs; i++) { |
| 1003 | irq_set_handler_data(virq + i, NULL); |
| 1004 | irq_set_handler(virq + i, NULL); |
| 1005 | } |
| 1006 | irq_domain_free_irqs_common(domain, virq, nr_irqs); |
| 1007 | } |
| 1008 | |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1009 | static bool irq_domain_is_auto_recursive(struct irq_domain *domain) |
| 1010 | { |
| 1011 | return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE; |
| 1012 | } |
| 1013 | |
| 1014 | static void irq_domain_free_irqs_recursive(struct irq_domain *domain, |
| 1015 | unsigned int irq_base, |
| 1016 | unsigned int nr_irqs) |
| 1017 | { |
| 1018 | domain->ops->free(domain, irq_base, nr_irqs); |
| 1019 | if (irq_domain_is_auto_recursive(domain)) { |
| 1020 | BUG_ON(!domain->parent); |
| 1021 | irq_domain_free_irqs_recursive(domain->parent, irq_base, |
| 1022 | nr_irqs); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | static int irq_domain_alloc_irqs_recursive(struct irq_domain *domain, |
| 1027 | unsigned int irq_base, |
| 1028 | unsigned int nr_irqs, void *arg) |
| 1029 | { |
| 1030 | int ret = 0; |
| 1031 | struct irq_domain *parent = domain->parent; |
| 1032 | bool recursive = irq_domain_is_auto_recursive(domain); |
| 1033 | |
| 1034 | BUG_ON(recursive && !parent); |
| 1035 | if (recursive) |
| 1036 | ret = irq_domain_alloc_irqs_recursive(parent, irq_base, |
| 1037 | nr_irqs, arg); |
| 1038 | if (ret >= 0) |
| 1039 | ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg); |
| 1040 | if (ret < 0 && recursive) |
| 1041 | irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs); |
| 1042 | |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1046 | /** |
| 1047 | * __irq_domain_alloc_irqs - Allocate IRQs from domain |
| 1048 | * @domain: domain to allocate from |
| 1049 | * @irq_base: allocate specified IRQ nubmer if irq_base >= 0 |
| 1050 | * @nr_irqs: number of IRQs to allocate |
| 1051 | * @node: NUMA node id for memory allocation |
| 1052 | * @arg: domain specific argument |
| 1053 | * @realloc: IRQ descriptors have already been allocated if true |
| 1054 | * |
| 1055 | * Allocate IRQ numbers and initialized all data structures to support |
| 1056 | * hierarchy IRQ domains. |
| 1057 | * Parameter @realloc is mainly to support legacy IRQs. |
| 1058 | * Returns error code or allocated IRQ number |
| 1059 | * |
| 1060 | * The whole process to setup an IRQ has been split into two steps. |
| 1061 | * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ |
| 1062 | * descriptor and required hardware resources. The second step, |
| 1063 | * irq_domain_activate_irq(), is to program hardwares with preallocated |
| 1064 | * resources. In this way, it's easier to rollback when failing to |
| 1065 | * allocate resources. |
| 1066 | */ |
| 1067 | int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base, |
| 1068 | unsigned int nr_irqs, int node, void *arg, |
| 1069 | bool realloc) |
| 1070 | { |
| 1071 | int i, ret, virq; |
| 1072 | |
| 1073 | if (domain == NULL) { |
| 1074 | domain = irq_default_domain; |
| 1075 | if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n")) |
| 1076 | return -EINVAL; |
| 1077 | } |
| 1078 | |
| 1079 | if (!domain->ops->alloc) { |
| 1080 | pr_debug("domain->ops->alloc() is NULL\n"); |
| 1081 | return -ENOSYS; |
| 1082 | } |
| 1083 | |
| 1084 | if (realloc && irq_base >= 0) { |
| 1085 | virq = irq_base; |
| 1086 | } else { |
| 1087 | virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node); |
| 1088 | if (virq < 0) { |
| 1089 | pr_debug("cannot allocate IRQ(base %d, count %d)\n", |
| 1090 | irq_base, nr_irqs); |
| 1091 | return virq; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) { |
| 1096 | pr_debug("cannot allocate memory for IRQ%d\n", virq); |
| 1097 | ret = -ENOMEM; |
| 1098 | goto out_free_desc; |
| 1099 | } |
| 1100 | |
| 1101 | mutex_lock(&irq_domain_mutex); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1102 | ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1103 | if (ret < 0) { |
| 1104 | mutex_unlock(&irq_domain_mutex); |
| 1105 | goto out_free_irq_data; |
| 1106 | } |
| 1107 | for (i = 0; i < nr_irqs; i++) |
| 1108 | irq_domain_insert_irq(virq + i); |
| 1109 | mutex_unlock(&irq_domain_mutex); |
| 1110 | |
| 1111 | return virq; |
| 1112 | |
| 1113 | out_free_irq_data: |
| 1114 | irq_domain_free_irq_data(virq, nr_irqs); |
| 1115 | out_free_desc: |
| 1116 | irq_free_descs(virq, nr_irqs); |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * irq_domain_free_irqs - Free IRQ number and associated data structures |
| 1122 | * @virq: base IRQ number |
| 1123 | * @nr_irqs: number of IRQs to free |
| 1124 | */ |
| 1125 | void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs) |
| 1126 | { |
| 1127 | struct irq_data *data = irq_get_irq_data(virq); |
| 1128 | int i; |
| 1129 | |
| 1130 | if (WARN(!data || !data->domain || !data->domain->ops->free, |
| 1131 | "NULL pointer, cannot free irq\n")) |
| 1132 | return; |
| 1133 | |
| 1134 | mutex_lock(&irq_domain_mutex); |
| 1135 | for (i = 0; i < nr_irqs; i++) |
| 1136 | irq_domain_remove_irq(virq + i); |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1137 | irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs); |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1138 | mutex_unlock(&irq_domain_mutex); |
| 1139 | |
| 1140 | irq_domain_free_irq_data(virq, nr_irqs); |
| 1141 | irq_free_descs(virq, nr_irqs); |
| 1142 | } |
| 1143 | |
| 1144 | /** |
Jiang Liu | 36d7273 | 2014-11-15 22:24:01 +0800 | [diff] [blame] | 1145 | * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain |
| 1146 | * @irq_base: Base IRQ number |
| 1147 | * @nr_irqs: Number of IRQs to allocate |
| 1148 | * @arg: Allocation data (arch/domain specific) |
| 1149 | * |
| 1150 | * Check whether the domain has been setup recursive. If not allocate |
| 1151 | * through the parent domain. |
| 1152 | */ |
| 1153 | int irq_domain_alloc_irqs_parent(struct irq_domain *domain, |
| 1154 | unsigned int irq_base, unsigned int nr_irqs, |
| 1155 | void *arg) |
| 1156 | { |
| 1157 | /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */ |
| 1158 | if (irq_domain_is_auto_recursive(domain)) |
| 1159 | return 0; |
| 1160 | |
| 1161 | domain = domain->parent; |
| 1162 | if (domain) |
| 1163 | return irq_domain_alloc_irqs_recursive(domain, irq_base, |
| 1164 | nr_irqs, arg); |
| 1165 | return -ENOSYS; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * irq_domain_free_irqs_parent - Free interrupts from parent domain |
| 1170 | * @irq_base: Base IRQ number |
| 1171 | * @nr_irqs: Number of IRQs to free |
| 1172 | * |
| 1173 | * Check whether the domain has been setup recursive. If not free |
| 1174 | * through the parent domain. |
| 1175 | */ |
| 1176 | void irq_domain_free_irqs_parent(struct irq_domain *domain, |
| 1177 | unsigned int irq_base, unsigned int nr_irqs) |
| 1178 | { |
| 1179 | /* irq_domain_free_irqs_recursive() will call parent's free */ |
| 1180 | if (!irq_domain_is_auto_recursive(domain) && domain->parent) |
| 1181 | irq_domain_free_irqs_recursive(domain->parent, irq_base, |
| 1182 | nr_irqs); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1186 | * irq_domain_activate_irq - Call domain_ops->activate recursively to activate |
| 1187 | * interrupt |
| 1188 | * @irq_data: outermost irq_data associated with interrupt |
| 1189 | * |
| 1190 | * This is the second step to call domain_ops->activate to program interrupt |
| 1191 | * controllers, so the interrupt could actually get delivered. |
| 1192 | */ |
| 1193 | void irq_domain_activate_irq(struct irq_data *irq_data) |
| 1194 | { |
| 1195 | if (irq_data && irq_data->domain) { |
| 1196 | struct irq_domain *domain = irq_data->domain; |
| 1197 | |
| 1198 | if (irq_data->parent_data) |
| 1199 | irq_domain_activate_irq(irq_data->parent_data); |
| 1200 | if (domain->ops->activate) |
| 1201 | domain->ops->activate(domain, irq_data); |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /** |
| 1206 | * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to |
| 1207 | * deactivate interrupt |
| 1208 | * @irq_data: outermost irq_data associated with interrupt |
| 1209 | * |
| 1210 | * It calls domain_ops->deactivate to program interrupt controllers to disable |
| 1211 | * interrupt delivery. |
| 1212 | */ |
| 1213 | void irq_domain_deactivate_irq(struct irq_data *irq_data) |
| 1214 | { |
| 1215 | if (irq_data && irq_data->domain) { |
| 1216 | struct irq_domain *domain = irq_data->domain; |
| 1217 | |
| 1218 | if (domain->ops->deactivate) |
| 1219 | domain->ops->deactivate(domain, irq_data); |
| 1220 | if (irq_data->parent_data) |
| 1221 | irq_domain_deactivate_irq(irq_data->parent_data); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | static void irq_domain_check_hierarchy(struct irq_domain *domain) |
| 1226 | { |
| 1227 | /* Hierarchy irq_domains must implement callback alloc() */ |
| 1228 | if (domain->ops->alloc) |
| 1229 | domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY; |
| 1230 | } |
| 1231 | #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */ |
| 1232 | /** |
| 1233 | * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain |
| 1234 | * @domain: domain to match |
| 1235 | * @virq: IRQ number to get irq_data |
| 1236 | */ |
| 1237 | struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain, |
| 1238 | unsigned int virq) |
| 1239 | { |
| 1240 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 1241 | |
| 1242 | return (irq_data && irq_data->domain == domain) ? irq_data : NULL; |
| 1243 | } |
| 1244 | |
Stefan Agner | 5f22f5c | 2015-05-16 11:44:13 +0200 | [diff] [blame] | 1245 | /** |
| 1246 | * irq_domain_set_info - Set the complete data for a @virq in @domain |
| 1247 | * @domain: Interrupt domain to match |
| 1248 | * @virq: IRQ number |
| 1249 | * @hwirq: The hardware interrupt number |
| 1250 | * @chip: The associated interrupt chip |
| 1251 | * @chip_data: The associated interrupt chip data |
| 1252 | * @handler: The interrupt flow handler |
| 1253 | * @handler_data: The interrupt flow handler data |
| 1254 | * @handler_name: The interrupt handler name |
| 1255 | */ |
| 1256 | void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, |
| 1257 | irq_hw_number_t hwirq, struct irq_chip *chip, |
| 1258 | void *chip_data, irq_flow_handler_t handler, |
| 1259 | void *handler_data, const char *handler_name) |
| 1260 | { |
| 1261 | irq_set_chip_and_handler_name(virq, chip, handler, handler_name); |
| 1262 | irq_set_chip_data(virq, chip_data); |
| 1263 | irq_set_handler_data(virq, handler_data); |
| 1264 | } |
| 1265 | |
Jiang Liu | f8264e3 | 2014-11-06 22:20:14 +0800 | [diff] [blame] | 1266 | static void irq_domain_check_hierarchy(struct irq_domain *domain) |
| 1267 | { |
| 1268 | } |
| 1269 | #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ |