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