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