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> |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 13 | #include <linux/topology.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 15 | #include <linux/slab.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 16 | #include <linux/smp.h> |
| 17 | #include <linux/fs.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 18 | |
| 19 | static LIST_HEAD(irq_domain_list); |
| 20 | static DEFINE_MUTEX(irq_domain_mutex); |
| 21 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 22 | static DEFINE_MUTEX(revmap_trees_mutex); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 23 | static struct irq_domain *irq_default_domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 24 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 25 | /** |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 26 | * irq_domain_alloc() - Allocate a new irq_domain data structure |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 27 | * @of_node: optional device-tree node of the interrupt controller |
| 28 | * @revmap_type: type of reverse mapping to use |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 29 | * @ops: map/unmap domain callbacks |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 30 | * @host_data: Controller private data pointer |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 31 | * |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 32 | * Allocates and initialize and irq_domain structure. Caller is expected to |
| 33 | * register allocated irq_domain with irq_domain_register(). Returns pointer |
| 34 | * to IRQ domain, or NULL on failure. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 35 | */ |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 36 | static struct irq_domain *irq_domain_alloc(struct device_node *of_node, |
| 37 | unsigned int revmap_type, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 38 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 39 | void *host_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 40 | { |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 41 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 42 | |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 43 | domain = kzalloc_node(sizeof(*domain), GFP_KERNEL, |
| 44 | of_node_to_nid(of_node)); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 45 | if (WARN_ON(!domain)) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 46 | return NULL; |
| 47 | |
| 48 | /* Fill structure */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 49 | domain->revmap_type = revmap_type; |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 50 | domain->ops = ops; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 51 | domain->host_data = host_data; |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 52 | domain->of_node = of_node_get(of_node); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 53 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 54 | return domain; |
| 55 | } |
| 56 | |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 57 | static void irq_domain_free(struct irq_domain *domain) |
| 58 | { |
| 59 | of_node_put(domain->of_node); |
| 60 | kfree(domain); |
| 61 | } |
| 62 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 63 | static void irq_domain_add(struct irq_domain *domain) |
| 64 | { |
| 65 | mutex_lock(&irq_domain_mutex); |
| 66 | list_add(&domain->link, &irq_domain_list); |
| 67 | mutex_unlock(&irq_domain_mutex); |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 68 | pr_debug("Allocated domain of type %d @0x%p\n", |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 69 | domain->revmap_type, domain); |
| 70 | } |
| 71 | |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 72 | /** |
| 73 | * irq_domain_remove() - Remove an irq domain. |
| 74 | * @domain: domain to remove |
| 75 | * |
| 76 | * This routine is used to remove an irq domain. The caller must ensure |
| 77 | * that all mappings within the domain have been disposed of prior to |
| 78 | * use, depending on the revmap type. |
| 79 | */ |
| 80 | void irq_domain_remove(struct irq_domain *domain) |
| 81 | { |
| 82 | mutex_lock(&irq_domain_mutex); |
| 83 | |
| 84 | switch (domain->revmap_type) { |
| 85 | case IRQ_DOMAIN_MAP_LEGACY: |
| 86 | /* |
| 87 | * Legacy domains don't manage their own irq_desc |
| 88 | * allocations, we expect the caller to handle irq_desc |
| 89 | * freeing on their own. |
| 90 | */ |
| 91 | break; |
| 92 | case IRQ_DOMAIN_MAP_TREE: |
| 93 | /* |
| 94 | * radix_tree_delete() takes care of destroying the root |
| 95 | * node when all entries are removed. Shout if there are |
| 96 | * any mappings left. |
| 97 | */ |
| 98 | WARN_ON(domain->revmap_data.tree.height); |
| 99 | break; |
| 100 | case IRQ_DOMAIN_MAP_LINEAR: |
| 101 | kfree(domain->revmap_data.linear.revmap); |
| 102 | domain->revmap_data.linear.size = 0; |
| 103 | break; |
| 104 | case IRQ_DOMAIN_MAP_NOMAP: |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | list_del(&domain->link); |
| 109 | |
| 110 | /* |
| 111 | * If the going away domain is the default one, reset it. |
| 112 | */ |
| 113 | if (unlikely(irq_default_domain == domain)) |
| 114 | irq_set_default_host(NULL); |
| 115 | |
| 116 | mutex_unlock(&irq_domain_mutex); |
| 117 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 118 | pr_debug("Removed domain of type %d @0x%p\n", |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 119 | domain->revmap_type, domain); |
| 120 | |
| 121 | irq_domain_free(domain); |
| 122 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 123 | EXPORT_SYMBOL_GPL(irq_domain_remove); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 124 | |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 125 | static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain, |
| 126 | irq_hw_number_t hwirq) |
| 127 | { |
| 128 | irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq; |
| 129 | int size = domain->revmap_data.legacy.size; |
| 130 | |
| 131 | if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size)) |
| 132 | return 0; |
| 133 | return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq; |
| 134 | } |
| 135 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 136 | /** |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 137 | * irq_domain_add_simple() - Allocate and register a simple irq_domain. |
| 138 | * @of_node: pointer to interrupt controller's device tree node. |
| 139 | * @size: total number of irqs in mapping |
Linus Walleij | 94a63da | 2013-06-06 12:10:23 +0100 | [diff] [blame] | 140 | * @first_irq: first number of irq block assigned to the domain, |
| 141 | * pass zero to assign irqs on-the-fly. This will result in a |
| 142 | * linear IRQ domain so it is important to use irq_create_mapping() |
| 143 | * for each used IRQ, especially when SPARSE_IRQ is enabled. |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 144 | * @ops: map/unmap domain callbacks |
| 145 | * @host_data: Controller private data pointer |
| 146 | * |
| 147 | * Allocates a legacy irq_domain if irq_base is positive or a linear |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 148 | * domain otherwise. For the legacy domain, IRQ descriptors will also |
| 149 | * be allocated. |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 150 | * |
| 151 | * This is intended to implement the expected behaviour for most |
| 152 | * interrupt controllers which is that a linear mapping should |
| 153 | * normally be used unless the system requires a legacy mapping in |
| 154 | * order to support supplying interrupt numbers during non-DT |
| 155 | * registration of devices. |
| 156 | */ |
| 157 | struct irq_domain *irq_domain_add_simple(struct device_node *of_node, |
| 158 | unsigned int size, |
| 159 | unsigned int first_irq, |
| 160 | const struct irq_domain_ops *ops, |
| 161 | void *host_data) |
| 162 | { |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 163 | if (first_irq > 0) { |
| 164 | int irq_base; |
| 165 | |
| 166 | if (IS_ENABLED(CONFIG_SPARSE_IRQ)) { |
| 167 | /* |
| 168 | * Set the descriptor allocator to search for a |
| 169 | * 1-to-1 mapping, such as irq_alloc_desc_at(). |
| 170 | * Use of_node_to_nid() which is defined to |
| 171 | * numa_node_id() on platforms that have no custom |
| 172 | * implementation. |
| 173 | */ |
| 174 | irq_base = irq_alloc_descs(first_irq, first_irq, size, |
| 175 | of_node_to_nid(of_node)); |
| 176 | if (irq_base < 0) { |
Linus Walleij | d202b7b | 2012-11-27 01:20:32 +0100 | [diff] [blame] | 177 | pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n", |
| 178 | first_irq); |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 179 | irq_base = first_irq; |
| 180 | } |
| 181 | } else |
| 182 | irq_base = first_irq; |
| 183 | |
| 184 | return irq_domain_add_legacy(of_node, size, irq_base, 0, |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 185 | ops, host_data); |
Linus Walleij | 2854d16 | 2012-09-27 14:59:39 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* A linear domain is the default */ |
| 189 | return irq_domain_add_linear(of_node, size, ops, host_data); |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 190 | } |
Arnd Bergmann | 346dbb7 | 2013-04-25 19:28:54 +0200 | [diff] [blame] | 191 | EXPORT_SYMBOL_GPL(irq_domain_add_simple); |
Mark Brown | 781d0f4 | 2012-07-05 12:19:19 +0100 | [diff] [blame] | 192 | |
| 193 | /** |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 194 | * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain. |
| 195 | * @of_node: pointer to interrupt controller's device tree node. |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 196 | * @size: total number of irqs in legacy mapping |
| 197 | * @first_irq: first number of irq block assigned to the domain |
| 198 | * @first_hwirq: first hwirq number to use for the translation. Should normally |
| 199 | * be '0', but a positive integer can be used if the effective |
| 200 | * hwirqs numbering does not begin at zero. |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 201 | * @ops: map/unmap domain callbacks |
| 202 | * @host_data: Controller private data pointer |
| 203 | * |
| 204 | * Note: the map() callback will be called before this function returns |
| 205 | * for all legacy interrupts except 0 (which is always the invalid irq for |
| 206 | * a legacy controller). |
| 207 | */ |
| 208 | struct irq_domain *irq_domain_add_legacy(struct device_node *of_node, |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 209 | unsigned int size, |
| 210 | unsigned int first_irq, |
| 211 | irq_hw_number_t first_hwirq, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 212 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 213 | void *host_data) |
| 214 | { |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 215 | struct irq_domain *domain; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 216 | unsigned int i; |
| 217 | |
| 218 | domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data); |
| 219 | if (!domain) |
| 220 | return NULL; |
| 221 | |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 222 | domain->revmap_data.legacy.first_irq = first_irq; |
| 223 | domain->revmap_data.legacy.first_hwirq = first_hwirq; |
| 224 | domain->revmap_data.legacy.size = size; |
| 225 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 226 | mutex_lock(&irq_domain_mutex); |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 227 | /* Verify that all the irqs are available */ |
| 228 | for (i = 0; i < size; i++) { |
| 229 | int irq = first_irq + i; |
| 230 | struct irq_data *irq_data = irq_get_irq_data(irq); |
| 231 | |
| 232 | if (WARN_ON(!irq_data || irq_data->domain)) { |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 233 | mutex_unlock(&irq_domain_mutex); |
Paul Mundt | 58ee99a | 2012-05-19 15:11:41 +0900 | [diff] [blame] | 234 | irq_domain_free(domain); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 235 | return NULL; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 238 | |
| 239 | /* Claim all of the irqs before registering a legacy domain */ |
| 240 | for (i = 0; i < size; i++) { |
| 241 | struct irq_data *irq_data = irq_get_irq_data(first_irq + i); |
| 242 | irq_data->hwirq = first_hwirq + i; |
| 243 | irq_data->domain = domain; |
| 244 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 245 | mutex_unlock(&irq_domain_mutex); |
| 246 | |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 247 | for (i = 0; i < size; i++) { |
| 248 | int irq = first_irq + i; |
| 249 | int hwirq = first_hwirq + i; |
| 250 | |
| 251 | /* IRQ0 gets ignored */ |
| 252 | if (!irq) |
| 253 | continue; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 254 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 255 | /* Legacy flags are left to default at this point, |
| 256 | * one can then use irq_create_mapping() to |
| 257 | * explicitly change them |
| 258 | */ |
Grant Likely | aed9804 | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 259 | if (ops->map) |
| 260 | ops->map(domain, irq, hwirq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 261 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 262 | /* Clear norequest flags */ |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 263 | irq_clear_status_flags(irq, IRQ_NOREQUEST); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 264 | } |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 265 | |
| 266 | irq_domain_add(domain); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 267 | return domain; |
| 268 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 269 | EXPORT_SYMBOL_GPL(irq_domain_add_legacy); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 270 | |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 271 | /** |
Dong Aisheng | 22076c7 | 2012-06-20 17:00:30 +0800 | [diff] [blame] | 272 | * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain. |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 273 | * @of_node: pointer to interrupt controller's device tree node. |
Mark Brown | a87487e | 2012-05-19 12:15:35 +0100 | [diff] [blame] | 274 | * @size: Number of interrupts in the domain. |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 275 | * @ops: map/unmap domain callbacks |
| 276 | * @host_data: Controller private data pointer |
| 277 | */ |
| 278 | struct irq_domain *irq_domain_add_linear(struct device_node *of_node, |
| 279 | unsigned int size, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 280 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 281 | void *host_data) |
| 282 | { |
| 283 | struct irq_domain *domain; |
| 284 | unsigned int *revmap; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 285 | |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 286 | revmap = kzalloc_node(sizeof(*revmap) * size, GFP_KERNEL, |
| 287 | of_node_to_nid(of_node)); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 288 | if (WARN_ON(!revmap)) |
| 289 | return NULL; |
| 290 | |
| 291 | domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data); |
| 292 | if (!domain) { |
| 293 | kfree(revmap); |
| 294 | return NULL; |
| 295 | } |
| 296 | domain->revmap_data.linear.size = size; |
| 297 | domain->revmap_data.linear.revmap = revmap; |
| 298 | irq_domain_add(domain); |
| 299 | return domain; |
| 300 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 301 | EXPORT_SYMBOL_GPL(irq_domain_add_linear); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 302 | |
| 303 | struct irq_domain *irq_domain_add_nomap(struct device_node *of_node, |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 304 | unsigned int max_irq, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 305 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 306 | void *host_data) |
| 307 | { |
| 308 | struct irq_domain *domain = irq_domain_alloc(of_node, |
| 309 | IRQ_DOMAIN_MAP_NOMAP, ops, host_data); |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 310 | if (domain) { |
| 311 | domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0; |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 312 | irq_domain_add(domain); |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 313 | } |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 314 | return domain; |
| 315 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(irq_domain_add_nomap); |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * irq_domain_add_tree() |
| 320 | * @of_node: pointer to interrupt controller's device tree node. |
| 321 | * @ops: map/unmap domain callbacks |
| 322 | * |
| 323 | * Note: The radix tree will be allocated later during boot automatically |
| 324 | * (the reverse mapping will use the slow path until that happens). |
| 325 | */ |
| 326 | struct irq_domain *irq_domain_add_tree(struct device_node *of_node, |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 327 | const struct irq_domain_ops *ops, |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 328 | void *host_data) |
| 329 | { |
| 330 | struct irq_domain *domain = irq_domain_alloc(of_node, |
| 331 | IRQ_DOMAIN_MAP_TREE, ops, host_data); |
| 332 | if (domain) { |
| 333 | INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL); |
| 334 | irq_domain_add(domain); |
| 335 | } |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 336 | return domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 337 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 338 | EXPORT_SYMBOL_GPL(irq_domain_add_tree); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 339 | |
| 340 | /** |
| 341 | * irq_find_host() - Locates a domain for a given device node |
| 342 | * @node: device-tree node of the interrupt controller |
| 343 | */ |
| 344 | struct irq_domain *irq_find_host(struct device_node *node) |
| 345 | { |
| 346 | struct irq_domain *h, *found = NULL; |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 347 | int rc; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 348 | |
| 349 | /* We might want to match the legacy controller last since |
| 350 | * it might potentially be set to match all interrupts in |
| 351 | * the absence of a device node. This isn't a problem so far |
| 352 | * yet though... |
| 353 | */ |
| 354 | mutex_lock(&irq_domain_mutex); |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 355 | list_for_each_entry(h, &irq_domain_list, link) { |
| 356 | if (h->ops->match) |
| 357 | rc = h->ops->match(h, node); |
| 358 | else |
| 359 | rc = (h->of_node != NULL) && (h->of_node == node); |
| 360 | |
| 361 | if (rc) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 362 | found = h; |
| 363 | break; |
| 364 | } |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 365 | } |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 366 | mutex_unlock(&irq_domain_mutex); |
| 367 | return found; |
| 368 | } |
| 369 | EXPORT_SYMBOL_GPL(irq_find_host); |
| 370 | |
| 371 | /** |
| 372 | * irq_set_default_host() - Set a "default" irq domain |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 373 | * @domain: default domain pointer |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 374 | * |
| 375 | * For convenience, it's possible to set a "default" domain that will be used |
| 376 | * whenever NULL is passed to irq_create_mapping(). It makes life easier for |
| 377 | * platforms that want to manipulate a few hard coded interrupt numbers that |
| 378 | * aren't properly represented in the device-tree. |
| 379 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 380 | void irq_set_default_host(struct irq_domain *domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 381 | { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 382 | pr_debug("Default domain set to @0x%p\n", domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 383 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 384 | irq_default_domain = domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 385 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 386 | EXPORT_SYMBOL_GPL(irq_set_default_host); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 387 | |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 388 | static void irq_domain_disassociate_many(struct irq_domain *domain, |
| 389 | unsigned int irq_base, int count) |
| 390 | { |
| 391 | /* |
| 392 | * disassociate in reverse order; |
| 393 | * not strictly necessary, but nice for unwinding |
| 394 | */ |
| 395 | while (count--) { |
| 396 | int irq = irq_base + count; |
| 397 | struct irq_data *irq_data = irq_get_irq_data(irq); |
Chen Gang | 275e31b | 2013-05-14 19:02:45 +0800 | [diff] [blame] | 398 | irq_hw_number_t hwirq; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 399 | |
| 400 | if (WARN_ON(!irq_data || irq_data->domain != domain)) |
| 401 | continue; |
| 402 | |
Chen Gang | 275e31b | 2013-05-14 19:02:45 +0800 | [diff] [blame] | 403 | hwirq = irq_data->hwirq; |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 404 | irq_set_status_flags(irq, IRQ_NOREQUEST); |
| 405 | |
| 406 | /* remove chip and handler */ |
| 407 | irq_set_chip_and_handler(irq, NULL, NULL); |
| 408 | |
| 409 | /* Make sure it's completed */ |
| 410 | synchronize_irq(irq); |
| 411 | |
| 412 | /* Tell the PIC about it */ |
| 413 | if (domain->ops->unmap) |
| 414 | domain->ops->unmap(domain, irq); |
| 415 | smp_mb(); |
| 416 | |
| 417 | irq_data->domain = NULL; |
| 418 | irq_data->hwirq = 0; |
| 419 | |
| 420 | /* Clear reverse map */ |
| 421 | switch(domain->revmap_type) { |
| 422 | case IRQ_DOMAIN_MAP_LINEAR: |
| 423 | if (hwirq < domain->revmap_data.linear.size) |
| 424 | domain->revmap_data.linear.revmap[hwirq] = 0; |
| 425 | break; |
| 426 | case IRQ_DOMAIN_MAP_TREE: |
| 427 | mutex_lock(&revmap_trees_mutex); |
| 428 | radix_tree_delete(&domain->revmap_data.tree, hwirq); |
| 429 | mutex_unlock(&revmap_trees_mutex); |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 435 | int irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base, |
| 436 | irq_hw_number_t hwirq_base, int count) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 437 | { |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 438 | unsigned int virq = irq_base; |
| 439 | irq_hw_number_t hwirq = hwirq_base; |
Mark Brown | f5a1ad0 | 2012-07-20 10:33:19 +0100 | [diff] [blame] | 440 | int i, ret; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 441 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 442 | pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__, |
| 443 | of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count); |
| 444 | |
| 445 | for (i = 0; i < count; i++) { |
| 446 | struct irq_data *irq_data = irq_get_irq_data(virq + i); |
| 447 | |
| 448 | if (WARN(!irq_data, "error: irq_desc not allocated; " |
| 449 | "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i)) |
| 450 | return -EINVAL; |
| 451 | if (WARN(irq_data->domain, "error: irq_desc already associated; " |
| 452 | "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i)) |
| 453 | return -EINVAL; |
| 454 | }; |
| 455 | |
| 456 | for (i = 0; i < count; i++, virq++, hwirq++) { |
| 457 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 458 | |
| 459 | irq_data->hwirq = hwirq; |
| 460 | irq_data->domain = domain; |
Mark Brown | f5a1ad0 | 2012-07-20 10:33:19 +0100 | [diff] [blame] | 461 | if (domain->ops->map) { |
| 462 | ret = domain->ops->map(domain, virq, hwirq); |
| 463 | if (ret != 0) { |
Benjamin Herrenschmidt | 5fe0c1f | 2013-05-06 11:37:43 +1000 | [diff] [blame] | 464 | /* |
| 465 | * If map() returns -EPERM, this interrupt is protected |
| 466 | * by the firmware or some other service and shall not |
Grant Likely | 5e1cda5 | 2013-05-29 03:10:53 +0100 | [diff] [blame^] | 467 | * be mapped. Don't bother telling the user about it. |
Benjamin Herrenschmidt | 5fe0c1f | 2013-05-06 11:37:43 +1000 | [diff] [blame] | 468 | */ |
| 469 | if (ret != -EPERM) { |
Grant Likely | 5e1cda5 | 2013-05-29 03:10:53 +0100 | [diff] [blame^] | 470 | pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n", |
| 471 | of_node_full_name(domain->of_node), hwirq, virq, ret); |
Benjamin Herrenschmidt | 5fe0c1f | 2013-05-06 11:37:43 +1000 | [diff] [blame] | 472 | } |
Mark Brown | f5a1ad0 | 2012-07-20 10:33:19 +0100 | [diff] [blame] | 473 | irq_data->domain = NULL; |
| 474 | irq_data->hwirq = 0; |
Grant Likely | 5e1cda5 | 2013-05-29 03:10:53 +0100 | [diff] [blame^] | 475 | continue; |
Mark Brown | f5a1ad0 | 2012-07-20 10:33:19 +0100 | [diff] [blame] | 476 | } |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | switch (domain->revmap_type) { |
| 480 | case IRQ_DOMAIN_MAP_LINEAR: |
| 481 | if (hwirq < domain->revmap_data.linear.size) |
| 482 | domain->revmap_data.linear.revmap[hwirq] = virq; |
| 483 | break; |
| 484 | case IRQ_DOMAIN_MAP_TREE: |
| 485 | mutex_lock(&revmap_trees_mutex); |
Grant Likely | d6b0d1f | 2012-06-03 22:04:37 -0700 | [diff] [blame] | 486 | radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data); |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 487 | mutex_unlock(&revmap_trees_mutex); |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 494 | return 0; |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 495 | |
| 496 | err_unmap: |
| 497 | irq_domain_disassociate_many(domain, irq_base, i); |
| 498 | return -EINVAL; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 499 | } |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 500 | EXPORT_SYMBOL_GPL(irq_domain_associate_many); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * irq_create_direct_mapping() - Allocate an irq for direct mapping |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 504 | * @domain: domain to allocate the irq for or NULL for default domain |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 505 | * |
| 506 | * This routine is used for irq controllers which can choose the hardware |
| 507 | * interrupt numbers they generate. In such a case it's simplest to use |
| 508 | * the linux irq as the hardware interrupt number. |
| 509 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 510 | unsigned int irq_create_direct_mapping(struct irq_domain *domain) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 511 | { |
| 512 | unsigned int virq; |
| 513 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 514 | if (domain == NULL) |
| 515 | domain = irq_default_domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 516 | |
Grant Likely | 9844a55 | 2012-06-03 22:04:38 -0700 | [diff] [blame] | 517 | if (WARN_ON(!domain || domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP)) |
| 518 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 519 | |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 520 | 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] | 521 | if (!virq) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 522 | pr_debug("create_direct virq allocation failed\n"); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 523 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 524 | } |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 525 | if (virq >= domain->revmap_data.nomap.max_irq) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 526 | pr_err("ERROR: no free irqs available below %i maximum\n", |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 527 | domain->revmap_data.nomap.max_irq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 528 | irq_free_desc(virq); |
| 529 | return 0; |
| 530 | } |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 531 | pr_debug("create_direct obtained virq %d\n", virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 532 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 533 | if (irq_domain_associate(domain, virq, virq)) { |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 534 | irq_free_desc(virq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 535 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | return virq; |
| 539 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 540 | EXPORT_SYMBOL_GPL(irq_create_direct_mapping); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 541 | |
| 542 | /** |
| 543 | * irq_create_mapping() - Map a hardware interrupt into linux irq space |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 544 | * @domain: domain owning this hardware interrupt or NULL for default domain |
| 545 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 546 | * |
| 547 | * Only one mapping per hardware interrupt is permitted. Returns a linux |
| 548 | * irq number. |
| 549 | * If the sense/trigger is to be specified, set_irq_type() should be called |
| 550 | * on the number returned from that call. |
| 551 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 552 | unsigned int irq_create_mapping(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 553 | irq_hw_number_t hwirq) |
| 554 | { |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 555 | unsigned int hint; |
| 556 | int virq; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 557 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 558 | pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 559 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 560 | /* Look for default domain if nececssary */ |
| 561 | if (domain == NULL) |
| 562 | domain = irq_default_domain; |
| 563 | if (domain == NULL) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 564 | pr_warning("irq_create_mapping called for" |
| 565 | " NULL domain, hwirq=%lx\n", hwirq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 566 | WARN_ON(1); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 567 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 568 | } |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 569 | pr_debug("-> using domain @%p\n", domain); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 570 | |
| 571 | /* Check if mapping already exists */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 572 | virq = irq_find_mapping(domain, hwirq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 573 | if (virq) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 574 | pr_debug("-> existing mapping on virq %d\n", virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 575 | return virq; |
| 576 | } |
| 577 | |
| 578 | /* Get a virtual interrupt number */ |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 579 | if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) |
| 580 | return irq_domain_legacy_revmap(domain, hwirq); |
| 581 | |
| 582 | /* Allocate a virtual interrupt number */ |
Grant Likely | 6fa6c8e2 | 2012-02-15 15:06:08 -0700 | [diff] [blame] | 583 | hint = hwirq % nr_irqs; |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 584 | if (hint == 0) |
| 585 | hint++; |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 586 | virq = irq_alloc_desc_from(hint, of_node_to_nid(domain->of_node)); |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 587 | if (virq <= 0) |
Paul Mundt | 5ca4db6 | 2012-06-03 22:04:34 -0700 | [diff] [blame] | 588 | virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node)); |
David Daney | 5b7526e | 2012-04-05 16:52:13 -0700 | [diff] [blame] | 589 | if (virq <= 0) { |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 590 | pr_debug("-> virq allocation failed\n"); |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 591 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 594 | if (irq_domain_associate(domain, virq, hwirq)) { |
Grant Likely | 7325570 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 595 | irq_free_desc(virq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 596 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 599 | 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] | 600 | hwirq, of_node_full_name(domain->of_node), virq); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 601 | |
| 602 | return virq; |
| 603 | } |
| 604 | EXPORT_SYMBOL_GPL(irq_create_mapping); |
| 605 | |
Grant Likely | 98aa468 | 2012-06-17 16:17:04 -0600 | [diff] [blame] | 606 | /** |
| 607 | * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs |
| 608 | * @domain: domain owning the interrupt range |
| 609 | * @irq_base: beginning of linux IRQ range |
| 610 | * @hwirq_base: beginning of hardware IRQ range |
| 611 | * @count: Number of interrupts to map |
| 612 | * |
| 613 | * This routine is used for allocating and mapping a range of hardware |
| 614 | * irqs to linux irqs where the linux irq numbers are at pre-defined |
| 615 | * locations. For use by controllers that already have static mappings |
| 616 | * to insert in to the domain. |
| 617 | * |
| 618 | * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time |
| 619 | * domain insertion. |
| 620 | * |
| 621 | * 0 is returned upon success, while any failure to establish a static |
| 622 | * mapping is treated as an error. |
| 623 | */ |
| 624 | int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base, |
| 625 | irq_hw_number_t hwirq_base, int count) |
| 626 | { |
| 627 | int ret; |
| 628 | |
| 629 | ret = irq_alloc_descs(irq_base, irq_base, count, |
| 630 | of_node_to_nid(domain->of_node)); |
| 631 | if (unlikely(ret < 0)) |
| 632 | return ret; |
| 633 | |
| 634 | ret = irq_domain_associate_many(domain, irq_base, hwirq_base, count); |
| 635 | if (unlikely(ret < 0)) { |
| 636 | irq_free_descs(irq_base, count); |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | EXPORT_SYMBOL_GPL(irq_create_strict_mappings); |
| 643 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 644 | unsigned int irq_create_of_mapping(struct device_node *controller, |
| 645 | const u32 *intspec, unsigned int intsize) |
| 646 | { |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 647 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 648 | irq_hw_number_t hwirq; |
| 649 | unsigned int type = IRQ_TYPE_NONE; |
| 650 | unsigned int virq; |
| 651 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 652 | domain = controller ? irq_find_host(controller) : irq_default_domain; |
| 653 | if (!domain) { |
Grant Likely | abd2363 | 2012-02-24 08:07:06 -0700 | [diff] [blame] | 654 | #ifdef CONFIG_MIPS |
| 655 | /* |
| 656 | * Workaround to avoid breaking interrupt controller drivers |
| 657 | * that don't yet register an irq_domain. This is temporary |
| 658 | * code. ~~~gcl, Feb 24, 2012 |
| 659 | * |
| 660 | * Scheduled for removal in Linux v3.6. That should be enough |
| 661 | * time. |
| 662 | */ |
| 663 | if (intsize > 0) |
| 664 | return intspec[0]; |
| 665 | #endif |
Paul Mundt | 54a9058 | 2012-05-19 15:11:47 +0900 | [diff] [blame] | 666 | pr_warning("no irq domain found for %s !\n", |
Grant Likely | efd68e7 | 2012-06-03 22:04:33 -0700 | [diff] [blame] | 667 | of_node_full_name(controller)); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 668 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 671 | /* If domain has no translation, then we assume interrupt line */ |
| 672 | if (domain->ops->xlate == NULL) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 673 | hwirq = intspec[0]; |
| 674 | else { |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 675 | if (domain->ops->xlate(domain, controller, intspec, intsize, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 676 | &hwirq, &type)) |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 677 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* Create mapping */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 681 | virq = irq_create_mapping(domain, hwirq); |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 682 | if (!virq) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 683 | return virq; |
| 684 | |
| 685 | /* Set type if specified and different than the current one */ |
| 686 | if (type != IRQ_TYPE_NONE && |
| 687 | type != (irqd_get_trigger_type(irq_get_irq_data(virq)))) |
| 688 | irq_set_irq_type(virq, type); |
| 689 | return virq; |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 692 | |
| 693 | /** |
| 694 | * irq_dispose_mapping() - Unmap an interrupt |
| 695 | * @virq: linux irq number of the interrupt to unmap |
| 696 | */ |
| 697 | void irq_dispose_mapping(unsigned int virq) |
| 698 | { |
| 699 | struct irq_data *irq_data = irq_get_irq_data(virq); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 700 | struct irq_domain *domain; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 701 | |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 702 | if (!virq || !irq_data) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 703 | return; |
| 704 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 705 | domain = irq_data->domain; |
| 706 | if (WARN_ON(domain == NULL)) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 707 | return; |
| 708 | |
| 709 | /* Never unmap legacy interrupts */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 710 | if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 711 | return; |
| 712 | |
Grant Likely | 913af20 | 2012-06-03 22:04:35 -0700 | [diff] [blame] | 713 | irq_domain_disassociate_many(domain, virq, 1); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 714 | irq_free_desc(virq); |
| 715 | } |
| 716 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
| 717 | |
| 718 | /** |
| 719 | * irq_find_mapping() - Find a linux irq from an hw irq number. |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 720 | * @domain: domain owning this hardware interrupt |
| 721 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 722 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 723 | unsigned int irq_find_mapping(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 724 | irq_hw_number_t hwirq) |
| 725 | { |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 726 | struct irq_data *data; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 727 | |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 728 | /* Look for default domain if nececssary */ |
| 729 | if (domain == NULL) |
| 730 | domain = irq_default_domain; |
| 731 | if (domain == NULL) |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 732 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 733 | |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 734 | switch (domain->revmap_type) { |
| 735 | case IRQ_DOMAIN_MAP_LEGACY: |
Grant Likely | 1bc04f2 | 2012-02-14 14:06:55 -0700 | [diff] [blame] | 736 | return irq_domain_legacy_revmap(domain, hwirq); |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 737 | case IRQ_DOMAIN_MAP_LINEAR: |
| 738 | return irq_linear_revmap(domain, hwirq); |
| 739 | case IRQ_DOMAIN_MAP_TREE: |
| 740 | rcu_read_lock(); |
| 741 | data = radix_tree_lookup(&domain->revmap_data.tree, hwirq); |
| 742 | rcu_read_unlock(); |
| 743 | if (data) |
| 744 | return data->irq; |
| 745 | break; |
| 746 | case IRQ_DOMAIN_MAP_NOMAP: |
| 747 | data = irq_get_irq_data(hwirq); |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 748 | if (data && (data->domain == domain) && (data->hwirq == hwirq)) |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 749 | return hwirq; |
| 750 | break; |
| 751 | } |
| 752 | |
Grant Likely | 0384837 | 2012-02-14 14:06:52 -0700 | [diff] [blame] | 753 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 754 | } |
| 755 | EXPORT_SYMBOL_GPL(irq_find_mapping); |
| 756 | |
| 757 | /** |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 758 | * irq_linear_revmap() - Find a linux irq from a hw irq number. |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 759 | * @domain: domain owning this hardware interrupt |
| 760 | * @hwirq: hardware irq number in that domain space |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 761 | * |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 762 | * This is a fast path that can be called directly by irq controller code to |
| 763 | * save a handful of instructions. |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 764 | */ |
Grant Likely | 6870065 | 2012-02-14 14:06:53 -0700 | [diff] [blame] | 765 | unsigned int irq_linear_revmap(struct irq_domain *domain, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 766 | irq_hw_number_t hwirq) |
| 767 | { |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 768 | BUG_ON(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 769 | |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 770 | /* Check revmap bounds; complain if exceeded */ |
| 771 | if (WARN_ON(hwirq >= domain->revmap_data.linear.size)) |
| 772 | return 0; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 773 | |
Grant Likely | 4c0946c | 2012-06-03 22:04:39 -0700 | [diff] [blame] | 774 | return domain->revmap_data.linear.revmap[hwirq]; |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 775 | } |
Paul Mundt | ecd84eb | 2012-05-19 15:11:42 +0900 | [diff] [blame] | 776 | EXPORT_SYMBOL_GPL(irq_linear_revmap); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 777 | |
Grant Likely | 092b2fb | 2012-03-29 14:10:30 -0600 | [diff] [blame] | 778 | #ifdef CONFIG_IRQ_DOMAIN_DEBUG |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 779 | static int virq_debug_show(struct seq_file *m, void *private) |
| 780 | { |
| 781 | unsigned long flags; |
| 782 | struct irq_desc *desc; |
| 783 | const char *p; |
| 784 | static const char none[] = "none"; |
| 785 | void *data; |
| 786 | int i; |
| 787 | |
Grant Likely | 15e06bf | 2012-04-11 00:26:25 -0600 | [diff] [blame] | 788 | seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq", |
Grant Likely | 5269a9a | 2012-04-12 14:42:15 -0600 | [diff] [blame] | 789 | "chip name", (int)(2 * sizeof(void *) + 2), "chip data", |
| 790 | "domain name"); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 791 | |
| 792 | for (i = 1; i < nr_irqs; i++) { |
| 793 | desc = irq_to_desc(i); |
| 794 | if (!desc) |
| 795 | continue; |
| 796 | |
| 797 | raw_spin_lock_irqsave(&desc->lock, flags); |
| 798 | |
| 799 | if (desc->action && desc->action->handler) { |
| 800 | struct irq_chip *chip; |
| 801 | |
| 802 | seq_printf(m, "%5d ", i); |
| 803 | seq_printf(m, "0x%05lx ", desc->irq_data.hwirq); |
| 804 | |
| 805 | chip = irq_desc_get_chip(desc); |
| 806 | if (chip && chip->name) |
| 807 | p = chip->name; |
| 808 | else |
| 809 | p = none; |
| 810 | seq_printf(m, "%-15s ", p); |
| 811 | |
| 812 | data = irq_desc_get_chip_data(desc); |
Grant Likely | 15e06bf | 2012-04-11 00:26:25 -0600 | [diff] [blame] | 813 | seq_printf(m, data ? "0x%p " : " %p ", data); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 814 | |
Grant Likely | efd68e7 | 2012-06-03 22:04:33 -0700 | [diff] [blame] | 815 | if (desc->irq_data.domain) |
| 816 | p = of_node_full_name(desc->irq_data.domain->of_node); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 817 | else |
| 818 | p = none; |
| 819 | seq_printf(m, "%s\n", p); |
| 820 | } |
| 821 | |
| 822 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
| 823 | } |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int virq_debug_open(struct inode *inode, struct file *file) |
| 829 | { |
| 830 | return single_open(file, virq_debug_show, inode->i_private); |
| 831 | } |
| 832 | |
| 833 | static const struct file_operations virq_debug_fops = { |
| 834 | .open = virq_debug_open, |
| 835 | .read = seq_read, |
| 836 | .llseek = seq_lseek, |
| 837 | .release = single_release, |
| 838 | }; |
| 839 | |
| 840 | static int __init irq_debugfs_init(void) |
| 841 | { |
Grant Likely | 092b2fb | 2012-03-29 14:10:30 -0600 | [diff] [blame] | 842 | if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL, |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 843 | NULL, &virq_debug_fops) == NULL) |
| 844 | return -ENOMEM; |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | __initcall(irq_debugfs_init); |
Grant Likely | 092b2fb | 2012-03-29 14:10:30 -0600 | [diff] [blame] | 849 | #endif /* CONFIG_IRQ_DOMAIN_DEBUG */ |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame] | 850 | |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 851 | /** |
| 852 | * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings |
| 853 | * |
| 854 | * Device Tree IRQ specifier translation function which works with one cell |
| 855 | * bindings where the cell value maps directly to the hwirq number. |
| 856 | */ |
| 857 | int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr, |
| 858 | const u32 *intspec, unsigned int intsize, |
| 859 | unsigned long *out_hwirq, unsigned int *out_type) |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 860 | { |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 861 | if (WARN_ON(intsize < 1)) |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 862 | return -EINVAL; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 863 | *out_hwirq = intspec[0]; |
| 864 | *out_type = IRQ_TYPE_NONE; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 865 | return 0; |
| 866 | } |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 867 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell); |
| 868 | |
| 869 | /** |
| 870 | * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings |
| 871 | * |
| 872 | * Device Tree IRQ specifier translation function which works with two cell |
| 873 | * bindings where the cell values map directly to the hwirq number |
| 874 | * and linux irq flags. |
| 875 | */ |
| 876 | int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr, |
| 877 | const u32 *intspec, unsigned int intsize, |
| 878 | irq_hw_number_t *out_hwirq, unsigned int *out_type) |
| 879 | { |
| 880 | if (WARN_ON(intsize < 2)) |
| 881 | return -EINVAL; |
| 882 | *out_hwirq = intspec[0]; |
| 883 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 884 | return 0; |
| 885 | } |
| 886 | EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell); |
| 887 | |
| 888 | /** |
| 889 | * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings |
| 890 | * |
| 891 | * Device Tree IRQ specifier translation function which works with either one |
| 892 | * or two cell bindings where the cell values map directly to the hwirq number |
| 893 | * and linux irq flags. |
| 894 | * |
| 895 | * Note: don't use this function unless your interrupt controller explicitly |
| 896 | * supports both one and two cell bindings. For the majority of controllers |
| 897 | * the _onecell() or _twocell() variants above should be used. |
| 898 | */ |
| 899 | int irq_domain_xlate_onetwocell(struct irq_domain *d, |
| 900 | struct device_node *ctrlr, |
| 901 | const u32 *intspec, unsigned int intsize, |
| 902 | unsigned long *out_hwirq, unsigned int *out_type) |
| 903 | { |
| 904 | if (WARN_ON(intsize < 1)) |
| 905 | return -EINVAL; |
| 906 | *out_hwirq = intspec[0]; |
| 907 | *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE; |
| 908 | return 0; |
| 909 | } |
| 910 | EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell); |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 911 | |
Grant Likely | a18dc81 | 2012-01-26 12:12:14 -0700 | [diff] [blame] | 912 | const struct irq_domain_ops irq_domain_simple_ops = { |
Grant Likely | 16b2e6e | 2012-01-26 11:26:52 -0700 | [diff] [blame] | 913 | .xlate = irq_domain_xlate_onetwocell, |
Grant Likely | 7529495 | 2012-02-14 14:06:57 -0700 | [diff] [blame] | 914 | }; |
| 915 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); |
| 916 | |
| 917 | #ifdef CONFIG_OF_IRQ |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 918 | void irq_domain_generate_simple(const struct of_device_id *match, |
| 919 | u64 phys_base, unsigned int irq_start) |
| 920 | { |
| 921 | struct device_node *node; |
Grant Likely | e1964c5 | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 922 | pr_debug("looking for phys_base=%llx, irq_start=%i\n", |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 923 | (unsigned long long) phys_base, (int) irq_start); |
| 924 | node = of_find_matching_node_by_address(NULL, match, phys_base); |
| 925 | if (node) |
Grant Likely | 6b783f7 | 2012-01-10 17:09:30 -0700 | [diff] [blame] | 926 | irq_domain_add_legacy(node, 32, irq_start, 0, |
| 927 | &irq_domain_simple_ops, NULL); |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 928 | } |
| 929 | EXPORT_SYMBOL_GPL(irq_domain_generate_simple); |
Grant Likely | 7529495 | 2012-02-14 14:06:57 -0700 | [diff] [blame] | 930 | #endif |