Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame^] | 1 | #include <linux/debugfs.h> |
| 2 | #include <linux/hardirq.h> |
| 3 | #include <linux/interrupt.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 4 | #include <linux/irq.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame^] | 5 | #include <linux/irqdesc.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 6 | #include <linux/irqdomain.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/mutex.h> |
| 9 | #include <linux/of.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 10 | #include <linux/of_address.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame^] | 11 | #include <linux/seq_file.h> |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 12 | #include <linux/slab.h> |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame^] | 13 | #include <linux/smp.h> |
| 14 | #include <linux/fs.h> |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 15 | |
| 16 | static LIST_HEAD(irq_domain_list); |
| 17 | static DEFINE_MUTEX(irq_domain_mutex); |
| 18 | |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame^] | 19 | #ifdef CONFIG_PPC |
| 20 | static DEFINE_MUTEX(revmap_trees_mutex); |
| 21 | static unsigned int irq_virq_count = NR_IRQS; |
| 22 | static struct irq_domain *irq_default_host; |
| 23 | |
| 24 | static int default_irq_host_match(struct irq_domain *h, struct device_node *np) |
| 25 | { |
| 26 | return h->of_node != NULL && h->of_node == np; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * irq_alloc_host() - Allocate a new irq_domain data structure |
| 31 | * @of_node: optional device-tree node of the interrupt controller |
| 32 | * @revmap_type: type of reverse mapping to use |
| 33 | * @revmap_arg: for IRQ_DOMAIN_MAP_LINEAR linear only: size of the map |
| 34 | * @ops: map/unmap host callbacks |
| 35 | * @inval_irq: provide a hw number in that host space that is always invalid |
| 36 | * |
| 37 | * Allocates and initialize and irq_domain structure. Note that in the case of |
| 38 | * IRQ_DOMAIN_MAP_LEGACY, the map() callback will be called before this returns |
| 39 | * for all legacy interrupts except 0 (which is always the invalid irq for |
| 40 | * a legacy controller). For a IRQ_DOMAIN_MAP_LINEAR, the map is allocated by |
| 41 | * this call as well. For a IRQ_DOMAIN_MAP_TREE, the radix tree will be |
| 42 | * allocated later during boot automatically (the reverse mapping will use the |
| 43 | * slow path until that happens). |
| 44 | */ |
| 45 | struct irq_domain *irq_alloc_host(struct device_node *of_node, |
| 46 | unsigned int revmap_type, |
| 47 | unsigned int revmap_arg, |
| 48 | struct irq_domain_ops *ops, |
| 49 | irq_hw_number_t inval_irq) |
| 50 | { |
| 51 | struct irq_domain *host, *h; |
| 52 | unsigned int size = sizeof(struct irq_domain); |
| 53 | unsigned int i; |
| 54 | unsigned int *rmap; |
| 55 | |
| 56 | /* Allocate structure and revmap table if using linear mapping */ |
| 57 | if (revmap_type == IRQ_DOMAIN_MAP_LINEAR) |
| 58 | size += revmap_arg * sizeof(unsigned int); |
| 59 | host = kzalloc(size, GFP_KERNEL); |
| 60 | if (host == NULL) |
| 61 | return NULL; |
| 62 | |
| 63 | /* Fill structure */ |
| 64 | host->revmap_type = revmap_type; |
| 65 | host->inval_irq = inval_irq; |
| 66 | host->ops = ops; |
| 67 | host->of_node = of_node_get(of_node); |
| 68 | |
| 69 | if (host->ops->match == NULL) |
| 70 | host->ops->match = default_irq_host_match; |
| 71 | |
| 72 | mutex_lock(&irq_domain_mutex); |
| 73 | /* Make sure only one legacy controller can be created */ |
| 74 | if (revmap_type == IRQ_DOMAIN_MAP_LEGACY) { |
| 75 | list_for_each_entry(h, &irq_domain_list, link) { |
| 76 | if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) { |
| 77 | mutex_unlock(&irq_domain_mutex); |
| 78 | of_node_put(host->of_node); |
| 79 | kfree(host); |
| 80 | return NULL; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | list_add(&host->link, &irq_domain_list); |
| 85 | mutex_unlock(&irq_domain_mutex); |
| 86 | |
| 87 | /* Additional setups per revmap type */ |
| 88 | switch(revmap_type) { |
| 89 | case IRQ_DOMAIN_MAP_LEGACY: |
| 90 | /* 0 is always the invalid number for legacy */ |
| 91 | host->inval_irq = 0; |
| 92 | /* setup us as the host for all legacy interrupts */ |
| 93 | for (i = 1; i < NUM_ISA_INTERRUPTS; i++) { |
| 94 | struct irq_data *irq_data = irq_get_irq_data(i); |
| 95 | irq_data->hwirq = i; |
| 96 | irq_data->domain = host; |
| 97 | |
| 98 | /* Legacy flags are left to default at this point, |
| 99 | * one can then use irq_create_mapping() to |
| 100 | * explicitly change them |
| 101 | */ |
| 102 | ops->map(host, i, i); |
| 103 | |
| 104 | /* Clear norequest flags */ |
| 105 | irq_clear_status_flags(i, IRQ_NOREQUEST); |
| 106 | } |
| 107 | break; |
| 108 | case IRQ_DOMAIN_MAP_LINEAR: |
| 109 | rmap = (unsigned int *)(host + 1); |
| 110 | for (i = 0; i < revmap_arg; i++) |
| 111 | rmap[i] = NO_IRQ; |
| 112 | host->revmap_data.linear.size = revmap_arg; |
| 113 | host->revmap_data.linear.revmap = rmap; |
| 114 | break; |
| 115 | case IRQ_DOMAIN_MAP_TREE: |
| 116 | INIT_RADIX_TREE(&host->revmap_data.tree, GFP_KERNEL); |
| 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host); |
| 123 | |
| 124 | return host; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * irq_find_host() - Locates a domain for a given device node |
| 129 | * @node: device-tree node of the interrupt controller |
| 130 | */ |
| 131 | struct irq_domain *irq_find_host(struct device_node *node) |
| 132 | { |
| 133 | struct irq_domain *h, *found = NULL; |
| 134 | |
| 135 | /* We might want to match the legacy controller last since |
| 136 | * it might potentially be set to match all interrupts in |
| 137 | * the absence of a device node. This isn't a problem so far |
| 138 | * yet though... |
| 139 | */ |
| 140 | mutex_lock(&irq_domain_mutex); |
| 141 | list_for_each_entry(h, &irq_domain_list, link) |
| 142 | if (h->ops->match(h, node)) { |
| 143 | found = h; |
| 144 | break; |
| 145 | } |
| 146 | mutex_unlock(&irq_domain_mutex); |
| 147 | return found; |
| 148 | } |
| 149 | EXPORT_SYMBOL_GPL(irq_find_host); |
| 150 | |
| 151 | /** |
| 152 | * irq_set_default_host() - Set a "default" irq domain |
| 153 | * @host: default host pointer |
| 154 | * |
| 155 | * For convenience, it's possible to set a "default" domain that will be used |
| 156 | * whenever NULL is passed to irq_create_mapping(). It makes life easier for |
| 157 | * platforms that want to manipulate a few hard coded interrupt numbers that |
| 158 | * aren't properly represented in the device-tree. |
| 159 | */ |
| 160 | void irq_set_default_host(struct irq_domain *host) |
| 161 | { |
| 162 | pr_debug("irq: Default host set to @0x%p\n", host); |
| 163 | |
| 164 | irq_default_host = host; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * irq_set_virq_count() - Set the maximum number of linux irqs |
| 169 | * @count: number of linux irqs, capped with NR_IRQS |
| 170 | * |
| 171 | * This is mainly for use by platforms like iSeries who want to program |
| 172 | * the virtual irq number in the controller to avoid the reverse mapping |
| 173 | */ |
| 174 | void irq_set_virq_count(unsigned int count) |
| 175 | { |
| 176 | pr_debug("irq: Trying to set virq count to %d\n", count); |
| 177 | |
| 178 | BUG_ON(count < NUM_ISA_INTERRUPTS); |
| 179 | if (count < NR_IRQS) |
| 180 | irq_virq_count = count; |
| 181 | } |
| 182 | |
| 183 | static int irq_setup_virq(struct irq_domain *host, unsigned int virq, |
| 184 | irq_hw_number_t hwirq) |
| 185 | { |
| 186 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 187 | |
| 188 | irq_data->hwirq = hwirq; |
| 189 | irq_data->domain = host; |
| 190 | if (host->ops->map(host, virq, hwirq)) { |
| 191 | pr_debug("irq: -> mapping failed, freeing\n"); |
| 192 | irq_data->domain = NULL; |
| 193 | irq_data->hwirq = 0; |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | irq_clear_status_flags(virq, IRQ_NOREQUEST); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * irq_create_direct_mapping() - Allocate an irq for direct mapping |
| 204 | * @host: domain to allocate the irq for or NULL for default host |
| 205 | * |
| 206 | * This routine is used for irq controllers which can choose the hardware |
| 207 | * interrupt numbers they generate. In such a case it's simplest to use |
| 208 | * the linux irq as the hardware interrupt number. |
| 209 | */ |
| 210 | unsigned int irq_create_direct_mapping(struct irq_domain *host) |
| 211 | { |
| 212 | unsigned int virq; |
| 213 | |
| 214 | if (host == NULL) |
| 215 | host = irq_default_host; |
| 216 | |
| 217 | BUG_ON(host == NULL); |
| 218 | WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_NOMAP); |
| 219 | |
| 220 | virq = irq_alloc_desc_from(1, 0); |
| 221 | if (virq == NO_IRQ) { |
| 222 | pr_debug("irq: create_direct virq allocation failed\n"); |
| 223 | return NO_IRQ; |
| 224 | } |
| 225 | if (virq >= irq_virq_count) { |
| 226 | pr_err("ERROR: no free irqs available below %i maximum\n", |
| 227 | irq_virq_count); |
| 228 | irq_free_desc(virq); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | pr_debug("irq: create_direct obtained virq %d\n", virq); |
| 233 | |
| 234 | if (irq_setup_virq(host, virq, virq)) { |
| 235 | irq_free_desc(virq); |
| 236 | return NO_IRQ; |
| 237 | } |
| 238 | |
| 239 | return virq; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * irq_create_mapping() - Map a hardware interrupt into linux irq space |
| 244 | * @host: host owning this hardware interrupt or NULL for default host |
| 245 | * @hwirq: hardware irq number in that host space |
| 246 | * |
| 247 | * Only one mapping per hardware interrupt is permitted. Returns a linux |
| 248 | * irq number. |
| 249 | * If the sense/trigger is to be specified, set_irq_type() should be called |
| 250 | * on the number returned from that call. |
| 251 | */ |
| 252 | unsigned int irq_create_mapping(struct irq_domain *host, |
| 253 | irq_hw_number_t hwirq) |
| 254 | { |
| 255 | unsigned int virq, hint; |
| 256 | |
| 257 | pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq); |
| 258 | |
| 259 | /* Look for default host if nececssary */ |
| 260 | if (host == NULL) |
| 261 | host = irq_default_host; |
| 262 | if (host == NULL) { |
| 263 | printk(KERN_WARNING "irq_create_mapping called for" |
| 264 | " NULL host, hwirq=%lx\n", hwirq); |
| 265 | WARN_ON(1); |
| 266 | return NO_IRQ; |
| 267 | } |
| 268 | pr_debug("irq: -> using host @%p\n", host); |
| 269 | |
| 270 | /* Check if mapping already exists */ |
| 271 | virq = irq_find_mapping(host, hwirq); |
| 272 | if (virq != NO_IRQ) { |
| 273 | pr_debug("irq: -> existing mapping on virq %d\n", virq); |
| 274 | return virq; |
| 275 | } |
| 276 | |
| 277 | /* Get a virtual interrupt number */ |
| 278 | if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY) { |
| 279 | /* Handle legacy */ |
| 280 | virq = (unsigned int)hwirq; |
| 281 | if (virq == 0 || virq >= NUM_ISA_INTERRUPTS) |
| 282 | return NO_IRQ; |
| 283 | return virq; |
| 284 | } else { |
| 285 | /* Allocate a virtual interrupt number */ |
| 286 | hint = hwirq % irq_virq_count; |
| 287 | if (hint == 0) |
| 288 | hint++; |
| 289 | virq = irq_alloc_desc_from(hint, 0); |
| 290 | if (!virq) |
| 291 | virq = irq_alloc_desc_from(1, 0); |
| 292 | if (virq == NO_IRQ) { |
| 293 | pr_debug("irq: -> virq allocation failed\n"); |
| 294 | return NO_IRQ; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (irq_setup_virq(host, virq, hwirq)) { |
| 299 | if (host->revmap_type != IRQ_DOMAIN_MAP_LEGACY) |
| 300 | irq_free_desc(virq); |
| 301 | return NO_IRQ; |
| 302 | } |
| 303 | |
| 304 | pr_debug("irq: irq %lu on host %s mapped to virtual irq %u\n", |
| 305 | hwirq, host->of_node ? host->of_node->full_name : "null", virq); |
| 306 | |
| 307 | return virq; |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(irq_create_mapping); |
| 310 | |
| 311 | unsigned int irq_create_of_mapping(struct device_node *controller, |
| 312 | const u32 *intspec, unsigned int intsize) |
| 313 | { |
| 314 | struct irq_domain *host; |
| 315 | irq_hw_number_t hwirq; |
| 316 | unsigned int type = IRQ_TYPE_NONE; |
| 317 | unsigned int virq; |
| 318 | |
| 319 | if (controller == NULL) |
| 320 | host = irq_default_host; |
| 321 | else |
| 322 | host = irq_find_host(controller); |
| 323 | if (host == NULL) { |
| 324 | printk(KERN_WARNING "irq: no irq host found for %s !\n", |
| 325 | controller->full_name); |
| 326 | return NO_IRQ; |
| 327 | } |
| 328 | |
| 329 | /* If host has no translation, then we assume interrupt line */ |
| 330 | if (host->ops->xlate == NULL) |
| 331 | hwirq = intspec[0]; |
| 332 | else { |
| 333 | if (host->ops->xlate(host, controller, intspec, intsize, |
| 334 | &hwirq, &type)) |
| 335 | return NO_IRQ; |
| 336 | } |
| 337 | |
| 338 | /* Create mapping */ |
| 339 | virq = irq_create_mapping(host, hwirq); |
| 340 | if (virq == NO_IRQ) |
| 341 | return virq; |
| 342 | |
| 343 | /* Set type if specified and different than the current one */ |
| 344 | if (type != IRQ_TYPE_NONE && |
| 345 | type != (irqd_get_trigger_type(irq_get_irq_data(virq)))) |
| 346 | irq_set_irq_type(virq, type); |
| 347 | return virq; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 350 | |
| 351 | /** |
| 352 | * irq_dispose_mapping() - Unmap an interrupt |
| 353 | * @virq: linux irq number of the interrupt to unmap |
| 354 | */ |
| 355 | void irq_dispose_mapping(unsigned int virq) |
| 356 | { |
| 357 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 358 | struct irq_domain *host; |
| 359 | irq_hw_number_t hwirq; |
| 360 | |
| 361 | if (virq == NO_IRQ || !irq_data) |
| 362 | return; |
| 363 | |
| 364 | host = irq_data->domain; |
| 365 | if (WARN_ON(host == NULL)) |
| 366 | return; |
| 367 | |
| 368 | /* Never unmap legacy interrupts */ |
| 369 | if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY) |
| 370 | return; |
| 371 | |
| 372 | irq_set_status_flags(virq, IRQ_NOREQUEST); |
| 373 | |
| 374 | /* remove chip and handler */ |
| 375 | irq_set_chip_and_handler(virq, NULL, NULL); |
| 376 | |
| 377 | /* Make sure it's completed */ |
| 378 | synchronize_irq(virq); |
| 379 | |
| 380 | /* Tell the PIC about it */ |
| 381 | if (host->ops->unmap) |
| 382 | host->ops->unmap(host, virq); |
| 383 | smp_mb(); |
| 384 | |
| 385 | /* Clear reverse map */ |
| 386 | hwirq = irq_data->hwirq; |
| 387 | switch(host->revmap_type) { |
| 388 | case IRQ_DOMAIN_MAP_LINEAR: |
| 389 | if (hwirq < host->revmap_data.linear.size) |
| 390 | host->revmap_data.linear.revmap[hwirq] = NO_IRQ; |
| 391 | break; |
| 392 | case IRQ_DOMAIN_MAP_TREE: |
| 393 | mutex_lock(&revmap_trees_mutex); |
| 394 | radix_tree_delete(&host->revmap_data.tree, hwirq); |
| 395 | mutex_unlock(&revmap_trees_mutex); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | /* Destroy map */ |
| 400 | irq_data->hwirq = host->inval_irq; |
| 401 | |
| 402 | irq_free_desc(virq); |
| 403 | } |
| 404 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
| 405 | |
| 406 | /** |
| 407 | * irq_find_mapping() - Find a linux irq from an hw irq number. |
| 408 | * @host: domain owning this hardware interrupt |
| 409 | * @hwirq: hardware irq number in that host space |
| 410 | * |
| 411 | * This is a slow path, for use by generic code. It's expected that an |
| 412 | * irq controller implementation directly calls the appropriate low level |
| 413 | * mapping function. |
| 414 | */ |
| 415 | unsigned int irq_find_mapping(struct irq_domain *host, |
| 416 | irq_hw_number_t hwirq) |
| 417 | { |
| 418 | unsigned int i; |
| 419 | unsigned int hint = hwirq % irq_virq_count; |
| 420 | |
| 421 | /* Look for default host if nececssary */ |
| 422 | if (host == NULL) |
| 423 | host = irq_default_host; |
| 424 | if (host == NULL) |
| 425 | return NO_IRQ; |
| 426 | |
| 427 | /* legacy -> bail early */ |
| 428 | if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY) |
| 429 | return hwirq; |
| 430 | |
| 431 | /* Slow path does a linear search of the map */ |
| 432 | if (hint == 0) |
| 433 | hint = 1; |
| 434 | i = hint; |
| 435 | do { |
| 436 | struct irq_data *data = irq_get_irq_data(i); |
| 437 | if (data && (data->domain == host) && (data->hwirq == hwirq)) |
| 438 | return i; |
| 439 | i++; |
| 440 | if (i >= irq_virq_count) |
| 441 | i = 1; |
| 442 | } while(i != hint); |
| 443 | return NO_IRQ; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(irq_find_mapping); |
| 446 | |
| 447 | /** |
| 448 | * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number. |
| 449 | * @host: host owning this hardware interrupt |
| 450 | * @hwirq: hardware irq number in that host space |
| 451 | * |
| 452 | * This is a fast path, for use by irq controller code that uses radix tree |
| 453 | * revmaps |
| 454 | */ |
| 455 | unsigned int irq_radix_revmap_lookup(struct irq_domain *host, |
| 456 | irq_hw_number_t hwirq) |
| 457 | { |
| 458 | struct irq_data *irq_data; |
| 459 | |
| 460 | if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_TREE)) |
| 461 | return irq_find_mapping(host, hwirq); |
| 462 | |
| 463 | /* |
| 464 | * Freeing an irq can delete nodes along the path to |
| 465 | * do the lookup via call_rcu. |
| 466 | */ |
| 467 | rcu_read_lock(); |
| 468 | irq_data = radix_tree_lookup(&host->revmap_data.tree, hwirq); |
| 469 | rcu_read_unlock(); |
| 470 | |
| 471 | /* |
| 472 | * If found in radix tree, then fine. |
| 473 | * Else fallback to linear lookup - this should not happen in practice |
| 474 | * as it means that we failed to insert the node in the radix tree. |
| 475 | */ |
| 476 | return irq_data ? irq_data->irq : irq_find_mapping(host, hwirq); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping. |
| 481 | * @host: host owning this hardware interrupt |
| 482 | * @virq: linux irq number |
| 483 | * @hwirq: hardware irq number in that host space |
| 484 | * |
| 485 | * This is for use by irq controllers that use a radix tree reverse |
| 486 | * mapping for fast lookup. |
| 487 | */ |
| 488 | void irq_radix_revmap_insert(struct irq_domain *host, unsigned int virq, |
| 489 | irq_hw_number_t hwirq) |
| 490 | { |
| 491 | struct irq_data *irq_data = irq_get_irq_data(virq); |
| 492 | |
| 493 | if (WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_TREE)) |
| 494 | return; |
| 495 | |
| 496 | if (virq != NO_IRQ) { |
| 497 | mutex_lock(&revmap_trees_mutex); |
| 498 | radix_tree_insert(&host->revmap_data.tree, hwirq, irq_data); |
| 499 | mutex_unlock(&revmap_trees_mutex); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * irq_linear_revmap() - Find a linux irq from a hw irq number. |
| 505 | * @host: host owning this hardware interrupt |
| 506 | * @hwirq: hardware irq number in that host space |
| 507 | * |
| 508 | * This is a fast path, for use by irq controller code that uses linear |
| 509 | * revmaps. It does fallback to the slow path if the revmap doesn't exist |
| 510 | * yet and will create the revmap entry with appropriate locking |
| 511 | */ |
| 512 | unsigned int irq_linear_revmap(struct irq_domain *host, |
| 513 | irq_hw_number_t hwirq) |
| 514 | { |
| 515 | unsigned int *revmap; |
| 516 | |
| 517 | if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_LINEAR)) |
| 518 | return irq_find_mapping(host, hwirq); |
| 519 | |
| 520 | /* Check revmap bounds */ |
| 521 | if (unlikely(hwirq >= host->revmap_data.linear.size)) |
| 522 | return irq_find_mapping(host, hwirq); |
| 523 | |
| 524 | /* Check if revmap was allocated */ |
| 525 | revmap = host->revmap_data.linear.revmap; |
| 526 | if (unlikely(revmap == NULL)) |
| 527 | return irq_find_mapping(host, hwirq); |
| 528 | |
| 529 | /* Fill up revmap with slow path if no mapping found */ |
| 530 | if (unlikely(revmap[hwirq] == NO_IRQ)) |
| 531 | revmap[hwirq] = irq_find_mapping(host, hwirq); |
| 532 | |
| 533 | return revmap[hwirq]; |
| 534 | } |
| 535 | |
| 536 | #ifdef CONFIG_VIRQ_DEBUG |
| 537 | static int virq_debug_show(struct seq_file *m, void *private) |
| 538 | { |
| 539 | unsigned long flags; |
| 540 | struct irq_desc *desc; |
| 541 | const char *p; |
| 542 | static const char none[] = "none"; |
| 543 | void *data; |
| 544 | int i; |
| 545 | |
| 546 | seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq", |
| 547 | "chip name", "chip data", "host name"); |
| 548 | |
| 549 | for (i = 1; i < nr_irqs; i++) { |
| 550 | desc = irq_to_desc(i); |
| 551 | if (!desc) |
| 552 | continue; |
| 553 | |
| 554 | raw_spin_lock_irqsave(&desc->lock, flags); |
| 555 | |
| 556 | if (desc->action && desc->action->handler) { |
| 557 | struct irq_chip *chip; |
| 558 | |
| 559 | seq_printf(m, "%5d ", i); |
| 560 | seq_printf(m, "0x%05lx ", desc->irq_data.hwirq); |
| 561 | |
| 562 | chip = irq_desc_get_chip(desc); |
| 563 | if (chip && chip->name) |
| 564 | p = chip->name; |
| 565 | else |
| 566 | p = none; |
| 567 | seq_printf(m, "%-15s ", p); |
| 568 | |
| 569 | data = irq_desc_get_chip_data(desc); |
| 570 | seq_printf(m, "0x%16p ", data); |
| 571 | |
| 572 | if (desc->irq_data.domain->of_node) |
| 573 | p = desc->irq_data.domain->of_node->full_name; |
| 574 | else |
| 575 | p = none; |
| 576 | seq_printf(m, "%s\n", p); |
| 577 | } |
| 578 | |
| 579 | raw_spin_unlock_irqrestore(&desc->lock, flags); |
| 580 | } |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static int virq_debug_open(struct inode *inode, struct file *file) |
| 586 | { |
| 587 | return single_open(file, virq_debug_show, inode->i_private); |
| 588 | } |
| 589 | |
| 590 | static const struct file_operations virq_debug_fops = { |
| 591 | .open = virq_debug_open, |
| 592 | .read = seq_read, |
| 593 | .llseek = seq_lseek, |
| 594 | .release = single_release, |
| 595 | }; |
| 596 | |
| 597 | static int __init irq_debugfs_init(void) |
| 598 | { |
| 599 | if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root, |
| 600 | NULL, &virq_debug_fops) == NULL) |
| 601 | return -ENOMEM; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | __initcall(irq_debugfs_init); |
| 606 | #endif /* CONFIG_VIRQ_DEBUG */ |
| 607 | |
| 608 | #else /* CONFIG_PPC */ |
| 609 | |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 610 | /** |
| 611 | * irq_domain_add() - Register an irq_domain |
| 612 | * @domain: ptr to initialized irq_domain structure |
| 613 | * |
| 614 | * Registers an irq_domain structure. The irq_domain must at a minimum be |
| 615 | * initialized with an ops structure pointer, and either a ->to_irq hook or |
| 616 | * a valid irq_base value. Everything else is optional. |
| 617 | */ |
| 618 | void irq_domain_add(struct irq_domain *domain) |
| 619 | { |
| 620 | struct irq_data *d; |
Rob Herring | 6d27430 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 621 | int hwirq, irq; |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * This assumes that the irq_domain owner has already allocated |
| 625 | * the irq_descs. This block will be removed when support for dynamic |
| 626 | * allocation of irq_descs is added to irq_domain. |
| 627 | */ |
Rob Herring | 6d27430 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 628 | irq_domain_for_each_irq(domain, hwirq, irq) { |
| 629 | d = irq_get_irq_data(irq); |
Rob Herring | eef24af | 2011-09-14 11:31:37 -0500 | [diff] [blame] | 630 | if (!d) { |
| 631 | WARN(1, "error: assigning domain to non existant irq_desc"); |
| 632 | return; |
| 633 | } |
| 634 | if (d->domain) { |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 635 | /* things are broken; just report, don't clean up */ |
| 636 | WARN(1, "error: irq_desc already assigned to a domain"); |
| 637 | return; |
| 638 | } |
| 639 | d->domain = domain; |
| 640 | d->hwirq = hwirq; |
| 641 | } |
| 642 | |
| 643 | mutex_lock(&irq_domain_mutex); |
Grant Likely | 7bb69ba | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 644 | list_add(&domain->link, &irq_domain_list); |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 645 | mutex_unlock(&irq_domain_mutex); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * irq_domain_del() - Unregister an irq_domain |
| 650 | * @domain: ptr to registered irq_domain. |
| 651 | */ |
| 652 | void irq_domain_del(struct irq_domain *domain) |
| 653 | { |
| 654 | struct irq_data *d; |
Rob Herring | 6d27430 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 655 | int hwirq, irq; |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 656 | |
| 657 | mutex_lock(&irq_domain_mutex); |
Grant Likely | 7bb69ba | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 658 | list_del(&domain->link); |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 659 | mutex_unlock(&irq_domain_mutex); |
| 660 | |
| 661 | /* Clear the irq_domain assignments */ |
Rob Herring | 6d27430 | 2011-09-30 10:48:38 -0500 | [diff] [blame] | 662 | irq_domain_for_each_irq(domain, hwirq, irq) { |
| 663 | d = irq_get_irq_data(irq); |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 664 | d->domain = NULL; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | #if defined(CONFIG_OF_IRQ) |
| 669 | /** |
| 670 | * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec |
| 671 | * |
| 672 | * Used by the device tree interrupt mapping code to translate a device tree |
| 673 | * interrupt specifier to a valid linux irq number. Returns either a valid |
| 674 | * linux IRQ number or 0. |
| 675 | * |
| 676 | * When the caller no longer need the irq number returned by this function it |
| 677 | * should arrange to call irq_dispose_mapping(). |
| 678 | */ |
| 679 | unsigned int irq_create_of_mapping(struct device_node *controller, |
| 680 | const u32 *intspec, unsigned int intsize) |
| 681 | { |
| 682 | struct irq_domain *domain; |
| 683 | unsigned long hwirq; |
| 684 | unsigned int irq, type; |
| 685 | int rc = -EINVAL; |
| 686 | |
| 687 | /* Find a domain which can translate the irq spec */ |
| 688 | mutex_lock(&irq_domain_mutex); |
Grant Likely | 7bb69ba | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 689 | list_for_each_entry(domain, &irq_domain_list, link) { |
| 690 | if (!domain->ops->xlate) |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 691 | continue; |
Grant Likely | 7bb69ba | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 692 | rc = domain->ops->xlate(domain, controller, |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 693 | intspec, intsize, &hwirq, &type); |
| 694 | if (rc == 0) |
| 695 | break; |
| 696 | } |
| 697 | mutex_unlock(&irq_domain_mutex); |
| 698 | |
| 699 | if (rc != 0) |
| 700 | return 0; |
| 701 | |
| 702 | irq = irq_domain_to_irq(domain, hwirq); |
| 703 | if (type != IRQ_TYPE_NONE) |
| 704 | irq_set_irq_type(irq, type); |
| 705 | pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n", |
| 706 | controller->full_name, (int)hwirq, irq, type); |
| 707 | return irq; |
| 708 | } |
| 709 | EXPORT_SYMBOL_GPL(irq_create_of_mapping); |
| 710 | |
| 711 | /** |
| 712 | * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping() |
| 713 | * @irq: linux irq number to be discarded |
| 714 | * |
| 715 | * Calling this function indicates the caller no longer needs a reference to |
| 716 | * the linux irq number returned by a prior call to irq_create_of_mapping(). |
| 717 | */ |
| 718 | void irq_dispose_mapping(unsigned int irq) |
| 719 | { |
| 720 | /* |
| 721 | * nothing yet; will be filled when support for dynamic allocation of |
| 722 | * irq_descs is added to irq_domain |
| 723 | */ |
| 724 | } |
| 725 | EXPORT_SYMBOL_GPL(irq_dispose_mapping); |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 726 | |
Grant Likely | 7bb69ba | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 727 | int irq_domain_simple_xlate(struct irq_domain *d, |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 728 | struct device_node *controller, |
| 729 | const u32 *intspec, unsigned int intsize, |
| 730 | unsigned long *out_hwirq, unsigned int *out_type) |
| 731 | { |
| 732 | if (d->of_node != controller) |
| 733 | return -EINVAL; |
| 734 | if (intsize < 1) |
| 735 | return -EINVAL; |
Rob Herring | 93797d8 | 2011-12-12 09:59:14 -0600 | [diff] [blame] | 736 | if (d->nr_irq && ((intspec[0] < d->hwirq_base) || |
| 737 | (intspec[0] >= d->hwirq_base + d->nr_irq))) |
| 738 | return -EINVAL; |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 739 | |
| 740 | *out_hwirq = intspec[0]; |
| 741 | *out_type = IRQ_TYPE_NONE; |
| 742 | if (intsize > 1) |
| 743 | *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; |
| 744 | return 0; |
| 745 | } |
| 746 | |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 747 | /** |
| 748 | * irq_domain_create_simple() - Set up a 'simple' translation range |
| 749 | */ |
| 750 | void irq_domain_add_simple(struct device_node *controller, int irq_base) |
| 751 | { |
| 752 | struct irq_domain *domain; |
| 753 | |
| 754 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 755 | if (!domain) { |
| 756 | WARN_ON(1); |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | domain->irq_base = irq_base; |
| 761 | domain->of_node = of_node_get(controller); |
| 762 | domain->ops = &irq_domain_simple_ops; |
| 763 | irq_domain_add(domain); |
| 764 | } |
| 765 | EXPORT_SYMBOL_GPL(irq_domain_add_simple); |
| 766 | |
| 767 | void irq_domain_generate_simple(const struct of_device_id *match, |
| 768 | u64 phys_base, unsigned int irq_start) |
| 769 | { |
| 770 | struct device_node *node; |
Grant Likely | e1964c5 | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 771 | pr_debug("looking for phys_base=%llx, irq_start=%i\n", |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 772 | (unsigned long long) phys_base, (int) irq_start); |
| 773 | node = of_find_matching_node_by_address(NULL, match, phys_base); |
| 774 | if (node) |
| 775 | irq_domain_add_simple(node, irq_start); |
Grant Likely | 7e71330 | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 776 | } |
| 777 | EXPORT_SYMBOL_GPL(irq_domain_generate_simple); |
Grant Likely | 08a543a | 2011-07-26 03:19:06 -0600 | [diff] [blame] | 778 | #endif /* CONFIG_OF_IRQ */ |
Jamie Iles | c87fb57 | 2011-12-14 23:43:16 +0100 | [diff] [blame] | 779 | |
| 780 | struct irq_domain_ops irq_domain_simple_ops = { |
| 781 | #ifdef CONFIG_OF_IRQ |
Grant Likely | 7bb69ba | 2012-02-14 14:06:48 -0700 | [diff] [blame] | 782 | .xlate = irq_domain_simple_xlate, |
Jamie Iles | c87fb57 | 2011-12-14 23:43:16 +0100 | [diff] [blame] | 783 | #endif /* CONFIG_OF_IRQ */ |
| 784 | }; |
| 785 | EXPORT_SYMBOL_GPL(irq_domain_simple_ops); |
Grant Likely | cc79ca6 | 2012-02-16 01:37:49 -0700 | [diff] [blame^] | 786 | |
| 787 | #endif /* !CONFIG_PPC */ |