1e3873444SGrant Likely /* 2e3873444SGrant Likely * Derived from arch/i386/kernel/irq.c 3e3873444SGrant Likely * Copyright (C) 1992 Linus Torvalds 4e3873444SGrant Likely * Adapted from arch/i386 by Gary Thomas 5e3873444SGrant Likely * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 6e3873444SGrant Likely * Updated and modified by Cort Dougan <cort@fsmlabs.com> 7e3873444SGrant Likely * Copyright (C) 1996-2001 Cort Dougan 8e3873444SGrant Likely * Adapted for Power Macintosh by Paul Mackerras 9e3873444SGrant Likely * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au) 10e3873444SGrant Likely * 11e3873444SGrant Likely * This program is free software; you can redistribute it and/or 12e3873444SGrant Likely * modify it under the terms of the GNU General Public License 13e3873444SGrant Likely * as published by the Free Software Foundation; either version 14e3873444SGrant Likely * 2 of the License, or (at your option) any later version. 15e3873444SGrant Likely * 16e3873444SGrant Likely * This file contains the code used to make IRQ descriptions in the 17e3873444SGrant Likely * device tree to actual irq numbers on an interrupt controller 18e3873444SGrant Likely * driver. 19e3873444SGrant Likely */ 20e3873444SGrant Likely 21c706c239SMarc Zyngier #include <linux/device.h> 22e3873444SGrant Likely #include <linux/errno.h> 23c71a54b0SRob Herring #include <linux/list.h> 24e3873444SGrant Likely #include <linux/module.h> 25e3873444SGrant Likely #include <linux/of.h> 26e3873444SGrant Likely #include <linux/of_irq.h> 27e3873444SGrant Likely #include <linux/string.h> 28c71a54b0SRob Herring #include <linux/slab.h> 29e3873444SGrant Likely 30e3873444SGrant Likely /** 31e3873444SGrant Likely * irq_of_parse_and_map - Parse and map an interrupt into linux virq space 32d84ff46aSYijing Wang * @dev: Device node of the device whose interrupt is to be mapped 33e3873444SGrant Likely * @index: Index of the interrupt to map 34e3873444SGrant Likely * 350c02c800SGrant Likely * This function is a wrapper that chains of_irq_parse_one() and 36e3873444SGrant Likely * irq_create_of_mapping() to make things easier to callers 37e3873444SGrant Likely */ 38e3873444SGrant Likely unsigned int irq_of_parse_and_map(struct device_node *dev, int index) 39e3873444SGrant Likely { 40530210c7SGrant Likely struct of_phandle_args oirq; 41e3873444SGrant Likely 420c02c800SGrant Likely if (of_irq_parse_one(dev, index, &oirq)) 4377a7300aSAnton Vorontsov return 0; 44e3873444SGrant Likely 45e6d30ab1SGrant Likely return irq_create_of_mapping(&oirq); 46e3873444SGrant Likely } 47e3873444SGrant Likely EXPORT_SYMBOL_GPL(irq_of_parse_and_map); 487dc2e113SGrant Likely 497dc2e113SGrant Likely /** 507dc2e113SGrant Likely * of_irq_find_parent - Given a device node, find its interrupt parent node 517dc2e113SGrant Likely * @child: pointer to device node 527dc2e113SGrant Likely * 537dc2e113SGrant Likely * Returns a pointer to the interrupt parent node, or NULL if the interrupt 547dc2e113SGrant Likely * parent could not be determined. 557dc2e113SGrant Likely */ 56*4c3141e0SCarlo Caione struct device_node *of_irq_find_parent(struct device_node *child) 577dc2e113SGrant Likely { 58b4bbb029SLinus Torvalds struct device_node *p; 599a6b2e58SGrant Likely const __be32 *parp; 607dc2e113SGrant Likely 61b4bbb029SLinus Torvalds if (!of_node_get(child)) 627dc2e113SGrant Likely return NULL; 637dc2e113SGrant Likely 647dc2e113SGrant Likely do { 65b4bbb029SLinus Torvalds parp = of_get_property(child, "interrupt-parent", NULL); 667dc2e113SGrant Likely if (parp == NULL) 67b4bbb029SLinus Torvalds p = of_get_parent(child); 687dc2e113SGrant Likely else { 697dc2e113SGrant Likely if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) 707dc2e113SGrant Likely p = of_node_get(of_irq_dflt_pic); 717dc2e113SGrant Likely else 729a6b2e58SGrant Likely p = of_find_node_by_phandle(be32_to_cpup(parp)); 737dc2e113SGrant Likely } 74b4bbb029SLinus Torvalds of_node_put(child); 75b4bbb029SLinus Torvalds child = p; 767dc2e113SGrant Likely } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL); 777dc2e113SGrant Likely 78b4bbb029SLinus Torvalds return p; 797dc2e113SGrant Likely } 80*4c3141e0SCarlo Caione EXPORT_SYMBOL_GPL(of_irq_find_parent); 817dc2e113SGrant Likely 827dc2e113SGrant Likely /** 830c02c800SGrant Likely * of_irq_parse_raw - Low level interrupt tree parsing 847dc2e113SGrant Likely * @parent: the device interrupt parent 8523616132SGrant Likely * @addr: address specifier (start of "reg" property of the device) in be32 format 8623616132SGrant Likely * @out_irq: structure of_irq updated by this function 877dc2e113SGrant Likely * 887dc2e113SGrant Likely * Returns 0 on success and a negative number on error 897dc2e113SGrant Likely * 907dc2e113SGrant Likely * This function is a low-level interrupt tree walking function. It 917dc2e113SGrant Likely * can be used to do a partial walk with synthetized reg and interrupts 927dc2e113SGrant Likely * properties, for example when resolving PCI interrupts when no device 9323616132SGrant Likely * node exist for the parent. It takes an interrupt specifier structure as 9423616132SGrant Likely * input, walks the tree looking for any interrupt-map properties, translates 9523616132SGrant Likely * the specifier for each map, and then returns the translated map. 967dc2e113SGrant Likely */ 9723616132SGrant Likely int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq) 987dc2e113SGrant Likely { 997dc2e113SGrant Likely struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL; 100355e62f5SGrant Likely __be32 initial_match_array[MAX_PHANDLE_ARGS]; 10123616132SGrant Likely const __be32 *match_array = initial_match_array; 102355e62f5SGrant Likely const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 }; 1037dc2e113SGrant Likely u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0; 1047dc2e113SGrant Likely int imaplen, match, i; 1057dc2e113SGrant Likely 106624cfca5SGrant Likely #ifdef DEBUG 107624cfca5SGrant Likely of_print_phandle_args("of_irq_parse_raw: ", out_irq); 108624cfca5SGrant Likely #endif 1097dc2e113SGrant Likely 11023616132SGrant Likely ipar = of_node_get(out_irq->np); 1117dc2e113SGrant Likely 1127dc2e113SGrant Likely /* First get the #interrupt-cells property of the current cursor 1137dc2e113SGrant Likely * that tells us how to interpret the passed-in intspec. If there 1147dc2e113SGrant Likely * is none, we are nice and just walk up the tree 1157dc2e113SGrant Likely */ 1167dc2e113SGrant Likely do { 1177dc2e113SGrant Likely tmp = of_get_property(ipar, "#interrupt-cells", NULL); 1187dc2e113SGrant Likely if (tmp != NULL) { 119a7c194b0SRob Herring intsize = be32_to_cpu(*tmp); 1207dc2e113SGrant Likely break; 1217dc2e113SGrant Likely } 1227dc2e113SGrant Likely tnode = ipar; 1237dc2e113SGrant Likely ipar = of_irq_find_parent(ipar); 1247dc2e113SGrant Likely of_node_put(tnode); 1257dc2e113SGrant Likely } while (ipar); 1267dc2e113SGrant Likely if (ipar == NULL) { 1277dc2e113SGrant Likely pr_debug(" -> no parent found !\n"); 1287dc2e113SGrant Likely goto fail; 1297dc2e113SGrant Likely } 1307dc2e113SGrant Likely 1310c02c800SGrant Likely pr_debug("of_irq_parse_raw: ipar=%s, size=%d\n", of_node_full_name(ipar), intsize); 1327dc2e113SGrant Likely 13323616132SGrant Likely if (out_irq->args_count != intsize) 1347dc2e113SGrant Likely return -EINVAL; 1357dc2e113SGrant Likely 1367dc2e113SGrant Likely /* Look for this #address-cells. We have to implement the old linux 1377dc2e113SGrant Likely * trick of looking for the parent here as some device-trees rely on it 1387dc2e113SGrant Likely */ 1397dc2e113SGrant Likely old = of_node_get(ipar); 1407dc2e113SGrant Likely do { 1417dc2e113SGrant Likely tmp = of_get_property(old, "#address-cells", NULL); 1427dc2e113SGrant Likely tnode = of_get_parent(old); 1437dc2e113SGrant Likely of_node_put(old); 1447dc2e113SGrant Likely old = tnode; 1457dc2e113SGrant Likely } while (old && tmp == NULL); 1467dc2e113SGrant Likely of_node_put(old); 1477dc2e113SGrant Likely old = NULL; 148a7c194b0SRob Herring addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp); 1497dc2e113SGrant Likely 1507dc2e113SGrant Likely pr_debug(" -> addrsize=%d\n", addrsize); 1517dc2e113SGrant Likely 152355e62f5SGrant Likely /* Range check so that the temporary buffer doesn't overflow */ 153355e62f5SGrant Likely if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) 154355e62f5SGrant Likely goto fail; 155355e62f5SGrant Likely 15623616132SGrant Likely /* Precalculate the match array - this simplifies match loop */ 15723616132SGrant Likely for (i = 0; i < addrsize; i++) 15878119fd1SGrant Likely initial_match_array[i] = addr ? addr[i] : 0; 15923616132SGrant Likely for (i = 0; i < intsize; i++) 16023616132SGrant Likely initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]); 16123616132SGrant Likely 1627dc2e113SGrant Likely /* Now start the actual "proper" walk of the interrupt tree */ 1637dc2e113SGrant Likely while (ipar != NULL) { 1647dc2e113SGrant Likely /* Now check if cursor is an interrupt-controller and if it is 1657dc2e113SGrant Likely * then we are done 1667dc2e113SGrant Likely */ 1677dc2e113SGrant Likely if (of_get_property(ipar, "interrupt-controller", NULL) != 1687dc2e113SGrant Likely NULL) { 1697dc2e113SGrant Likely pr_debug(" -> got it !\n"); 1707dc2e113SGrant Likely return 0; 1717dc2e113SGrant Likely } 1727dc2e113SGrant Likely 17378119fd1SGrant Likely /* 17478119fd1SGrant Likely * interrupt-map parsing does not work without a reg 17578119fd1SGrant Likely * property when #address-cells != 0 17678119fd1SGrant Likely */ 17778119fd1SGrant Likely if (addrsize && !addr) { 17878119fd1SGrant Likely pr_debug(" -> no reg passed in when needed !\n"); 17978119fd1SGrant Likely goto fail; 18078119fd1SGrant Likely } 18178119fd1SGrant Likely 1827dc2e113SGrant Likely /* Now look for an interrupt-map */ 1837dc2e113SGrant Likely imap = of_get_property(ipar, "interrupt-map", &imaplen); 1847dc2e113SGrant Likely /* No interrupt map, check for an interrupt parent */ 1857dc2e113SGrant Likely if (imap == NULL) { 1867dc2e113SGrant Likely pr_debug(" -> no map, getting parent\n"); 1877dc2e113SGrant Likely newpar = of_irq_find_parent(ipar); 1887dc2e113SGrant Likely goto skiplevel; 1897dc2e113SGrant Likely } 1907dc2e113SGrant Likely imaplen /= sizeof(u32); 1917dc2e113SGrant Likely 1927dc2e113SGrant Likely /* Look for a mask */ 1937dc2e113SGrant Likely imask = of_get_property(ipar, "interrupt-map-mask", NULL); 19423616132SGrant Likely if (!imask) 19523616132SGrant Likely imask = dummy_imask; 1967dc2e113SGrant Likely 1977dc2e113SGrant Likely /* Parse interrupt-map */ 1987dc2e113SGrant Likely match = 0; 1997dc2e113SGrant Likely while (imaplen > (addrsize + intsize + 1) && !match) { 2007dc2e113SGrant Likely /* Compare specifiers */ 2017dc2e113SGrant Likely match = 1; 20223616132SGrant Likely for (i = 0; i < (addrsize + intsize); i++, imaplen--) 20374dac2edSTomasz Figa match &= !((match_array[i] ^ *imap++) & imask[i]); 2047dc2e113SGrant Likely 2057dc2e113SGrant Likely pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen); 2067dc2e113SGrant Likely 2077dc2e113SGrant Likely /* Get the interrupt parent */ 2087dc2e113SGrant Likely if (of_irq_workarounds & OF_IMAP_NO_PHANDLE) 2097dc2e113SGrant Likely newpar = of_node_get(of_irq_dflt_pic); 2107dc2e113SGrant Likely else 2119a6b2e58SGrant Likely newpar = of_find_node_by_phandle(be32_to_cpup(imap)); 2127dc2e113SGrant Likely imap++; 2137dc2e113SGrant Likely --imaplen; 2147dc2e113SGrant Likely 2157dc2e113SGrant Likely /* Check if not found */ 2167dc2e113SGrant Likely if (newpar == NULL) { 2177dc2e113SGrant Likely pr_debug(" -> imap parent not found !\n"); 2187dc2e113SGrant Likely goto fail; 2197dc2e113SGrant Likely } 2207dc2e113SGrant Likely 2211ca56e7dSPeter Crosthwaite if (!of_device_is_available(newpar)) 2221ca56e7dSPeter Crosthwaite match = 0; 2231ca56e7dSPeter Crosthwaite 2247dc2e113SGrant Likely /* Get #interrupt-cells and #address-cells of new 2257dc2e113SGrant Likely * parent 2267dc2e113SGrant Likely */ 2277dc2e113SGrant Likely tmp = of_get_property(newpar, "#interrupt-cells", NULL); 2287dc2e113SGrant Likely if (tmp == NULL) { 2297dc2e113SGrant Likely pr_debug(" -> parent lacks #interrupt-cells!\n"); 2307dc2e113SGrant Likely goto fail; 2317dc2e113SGrant Likely } 232a7c194b0SRob Herring newintsize = be32_to_cpu(*tmp); 2337dc2e113SGrant Likely tmp = of_get_property(newpar, "#address-cells", NULL); 234a7c194b0SRob Herring newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp); 2357dc2e113SGrant Likely 2367dc2e113SGrant Likely pr_debug(" -> newintsize=%d, newaddrsize=%d\n", 2377dc2e113SGrant Likely newintsize, newaddrsize); 2387dc2e113SGrant Likely 2397dc2e113SGrant Likely /* Check for malformed properties */ 240355e62f5SGrant Likely if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS)) 241355e62f5SGrant Likely goto fail; 2427dc2e113SGrant Likely if (imaplen < (newaddrsize + newintsize)) 2437dc2e113SGrant Likely goto fail; 2447dc2e113SGrant Likely 2457dc2e113SGrant Likely imap += newaddrsize + newintsize; 2467dc2e113SGrant Likely imaplen -= newaddrsize + newintsize; 2477dc2e113SGrant Likely 2487dc2e113SGrant Likely pr_debug(" -> imaplen=%d\n", imaplen); 2497dc2e113SGrant Likely } 2507dc2e113SGrant Likely if (!match) 2517dc2e113SGrant Likely goto fail; 2527dc2e113SGrant Likely 25323616132SGrant Likely /* 25423616132SGrant Likely * Successfully parsed an interrrupt-map translation; copy new 25523616132SGrant Likely * interrupt specifier into the out_irq structure 25623616132SGrant Likely */ 25723616132SGrant Likely match_array = imap - newaddrsize - newintsize; 25823616132SGrant Likely for (i = 0; i < newintsize; i++) 25923616132SGrant Likely out_irq->args[i] = be32_to_cpup(imap - newintsize + i); 26023616132SGrant Likely out_irq->args_count = intsize = newintsize; 2617dc2e113SGrant Likely addrsize = newaddrsize; 2627dc2e113SGrant Likely 2637dc2e113SGrant Likely skiplevel: 2647dc2e113SGrant Likely /* Iterate again with new parent */ 265d23b2516SJeremy Linton out_irq->np = newpar; 26674a7f084SGrant Likely pr_debug(" -> new parent: %s\n", of_node_full_name(newpar)); 2677dc2e113SGrant Likely of_node_put(ipar); 2687dc2e113SGrant Likely ipar = newpar; 2697dc2e113SGrant Likely newpar = NULL; 2707dc2e113SGrant Likely } 2717dc2e113SGrant Likely fail: 2727dc2e113SGrant Likely of_node_put(ipar); 2737dc2e113SGrant Likely of_node_put(newpar); 2747dc2e113SGrant Likely 2757dc2e113SGrant Likely return -EINVAL; 2767dc2e113SGrant Likely } 2770c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_raw); 2787dc2e113SGrant Likely 2797dc2e113SGrant Likely /** 2800c02c800SGrant Likely * of_irq_parse_one - Resolve an interrupt for a device 2817dc2e113SGrant Likely * @device: the device whose interrupt is to be resolved 2827dc2e113SGrant Likely * @index: index of the interrupt to resolve 2837dc2e113SGrant Likely * @out_irq: structure of_irq filled by this function 2847dc2e113SGrant Likely * 28523616132SGrant Likely * This function resolves an interrupt for a node by walking the interrupt tree, 28623616132SGrant Likely * finding which interrupt controller node it is attached to, and returning the 28723616132SGrant Likely * interrupt specifier that can be used to retrieve a Linux IRQ number. 2887dc2e113SGrant Likely */ 289530210c7SGrant Likely int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq) 2907dc2e113SGrant Likely { 2917dc2e113SGrant Likely struct device_node *p; 292d2f71839SGrant Likely const __be32 *intspec, *tmp, *addr; 2937dc2e113SGrant Likely u32 intsize, intlen; 294d7c14605SLaurent Pinchart int i, res; 2957dc2e113SGrant Likely 2960c02c800SGrant Likely pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index); 2977dc2e113SGrant Likely 2987dc2e113SGrant Likely /* OldWorld mac stuff is "special", handle out of line */ 2997dc2e113SGrant Likely if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC) 3000c02c800SGrant Likely return of_irq_parse_oldworld(device, index, out_irq); 3017dc2e113SGrant Likely 30279d97015SGrant Likely /* Get the reg property (if any) */ 30379d97015SGrant Likely addr = of_get_property(device, "reg", NULL); 30479d97015SGrant Likely 305a9ecdc0fSFlorian Fainelli /* Try the new-style interrupts-extended first */ 30679d97015SGrant Likely res = of_parse_phandle_with_args(device, "interrupts-extended", 30779d97015SGrant Likely "#interrupt-cells", index, out_irq); 308a9ecdc0fSFlorian Fainelli if (!res) 30979d97015SGrant Likely return of_irq_parse_raw(addr, out_irq); 310a9ecdc0fSFlorian Fainelli 311a9ecdc0fSFlorian Fainelli /* Get the interrupts property */ 312a9ecdc0fSFlorian Fainelli intspec = of_get_property(device, "interrupts", &intlen); 313a9ecdc0fSFlorian Fainelli if (intspec == NULL) 314a9ecdc0fSFlorian Fainelli return -EINVAL; 315a9ecdc0fSFlorian Fainelli 316d2f71839SGrant Likely intlen /= sizeof(*intspec); 3177dc2e113SGrant Likely 318d2f71839SGrant Likely pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen); 3197dc2e113SGrant Likely 3207dc2e113SGrant Likely /* Look for the interrupt parent. */ 3217dc2e113SGrant Likely p = of_irq_find_parent(device); 3227dc2e113SGrant Likely if (p == NULL) 3237dc2e113SGrant Likely return -EINVAL; 3247dc2e113SGrant Likely 3257dc2e113SGrant Likely /* Get size of interrupt specifier */ 3267dc2e113SGrant Likely tmp = of_get_property(p, "#interrupt-cells", NULL); 327d7c14605SLaurent Pinchart if (tmp == NULL) { 328d7c14605SLaurent Pinchart res = -EINVAL; 3297dc2e113SGrant Likely goto out; 330d7c14605SLaurent Pinchart } 331a7c194b0SRob Herring intsize = be32_to_cpu(*tmp); 3327dc2e113SGrant Likely 3337dc2e113SGrant Likely pr_debug(" intsize=%d intlen=%d\n", intsize, intlen); 3347dc2e113SGrant Likely 3357dc2e113SGrant Likely /* Check index */ 336d7c14605SLaurent Pinchart if ((index + 1) * intsize > intlen) { 337d7c14605SLaurent Pinchart res = -EINVAL; 3387dc2e113SGrant Likely goto out; 339d7c14605SLaurent Pinchart } 3407dc2e113SGrant Likely 34123616132SGrant Likely /* Copy intspec into irq structure */ 34223616132SGrant Likely intspec += index * intsize; 34323616132SGrant Likely out_irq->np = p; 34423616132SGrant Likely out_irq->args_count = intsize; 34523616132SGrant Likely for (i = 0; i < intsize; i++) 34623616132SGrant Likely out_irq->args[i] = be32_to_cpup(intspec++); 34723616132SGrant Likely 34823616132SGrant Likely /* Check if there are any interrupt-map translations to process */ 34923616132SGrant Likely res = of_irq_parse_raw(addr, out_irq); 3507dc2e113SGrant Likely out: 3517dc2e113SGrant Likely of_node_put(p); 3527dc2e113SGrant Likely return res; 3537dc2e113SGrant Likely } 3540c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_one); 3557dc2e113SGrant Likely 3567dc2e113SGrant Likely /** 3577dc2e113SGrant Likely * of_irq_to_resource - Decode a node's IRQ and return it as a resource 3587dc2e113SGrant Likely * @dev: pointer to device tree node 3597dc2e113SGrant Likely * @index: zero-based index of the irq 3607dc2e113SGrant Likely * @r: pointer to resource structure to return result into. 3617dc2e113SGrant Likely */ 3627dc2e113SGrant Likely int of_irq_to_resource(struct device_node *dev, int index, struct resource *r) 3637dc2e113SGrant Likely { 3647dc2e113SGrant Likely int irq = irq_of_parse_and_map(dev, index); 3657dc2e113SGrant Likely 3667dc2e113SGrant Likely /* Only dereference the resource if both the 3677dc2e113SGrant Likely * resource and the irq are valid. */ 36877a7300aSAnton Vorontsov if (r && irq) { 369661db794SBenoit Cousson const char *name = NULL; 370661db794SBenoit Cousson 371cf9e2368SSebastian Andrzej Siewior memset(r, 0, sizeof(*r)); 372661db794SBenoit Cousson /* 373ae107d06SGeert Uytterhoeven * Get optional "interrupt-names" property to add a name 374661db794SBenoit Cousson * to the resource. 375661db794SBenoit Cousson */ 376661db794SBenoit Cousson of_property_read_string_index(dev, "interrupt-names", index, 377661db794SBenoit Cousson &name); 378661db794SBenoit Cousson 3797dc2e113SGrant Likely r->start = r->end = irq; 3804a43d686STomasz Figa r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq)); 3818804827bSGrant Likely r->name = name ? name : of_node_full_name(dev); 3827dc2e113SGrant Likely } 3837dc2e113SGrant Likely 3847dc2e113SGrant Likely return irq; 3857dc2e113SGrant Likely } 3867dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_to_resource); 38752f6537cSAndres Salomon 38852f6537cSAndres Salomon /** 3899ec36cafSRob Herring * of_irq_get - Decode a node's IRQ and return it as a Linux irq number 3909ec36cafSRob Herring * @dev: pointer to device tree node 3919ec36cafSRob Herring * @index: zero-based index of the irq 3929ec36cafSRob Herring * 3939ec36cafSRob Herring * Returns Linux irq number on success, or -EPROBE_DEFER if the irq domain 3949ec36cafSRob Herring * is not yet created. 3959ec36cafSRob Herring * 3969ec36cafSRob Herring */ 3979ec36cafSRob Herring int of_irq_get(struct device_node *dev, int index) 3989ec36cafSRob Herring { 3999ec36cafSRob Herring int rc; 4009ec36cafSRob Herring struct of_phandle_args oirq; 4019ec36cafSRob Herring struct irq_domain *domain; 4029ec36cafSRob Herring 4039ec36cafSRob Herring rc = of_irq_parse_one(dev, index, &oirq); 4049ec36cafSRob Herring if (rc) 4059ec36cafSRob Herring return rc; 4069ec36cafSRob Herring 4079ec36cafSRob Herring domain = irq_find_host(oirq.np); 4089ec36cafSRob Herring if (!domain) 4099ec36cafSRob Herring return -EPROBE_DEFER; 4109ec36cafSRob Herring 4119ec36cafSRob Herring return irq_create_of_mapping(&oirq); 4129ec36cafSRob Herring } 4139eb08fb3SLaurent Pinchart EXPORT_SYMBOL_GPL(of_irq_get); 4149ec36cafSRob Herring 4159ec36cafSRob Herring /** 416ad69674eSGrygorii Strashko * of_irq_get_byname - Decode a node's IRQ and return it as a Linux irq number 417ad69674eSGrygorii Strashko * @dev: pointer to device tree node 418ad69674eSGrygorii Strashko * @name: irq name 419ad69674eSGrygorii Strashko * 420ad69674eSGrygorii Strashko * Returns Linux irq number on success, or -EPROBE_DEFER if the irq domain 421ad69674eSGrygorii Strashko * is not yet created, or error code in case of any other failure. 422ad69674eSGrygorii Strashko */ 423ad69674eSGrygorii Strashko int of_irq_get_byname(struct device_node *dev, const char *name) 424ad69674eSGrygorii Strashko { 425ad69674eSGrygorii Strashko int index; 426ad69674eSGrygorii Strashko 427ad69674eSGrygorii Strashko if (unlikely(!name)) 428ad69674eSGrygorii Strashko return -EINVAL; 429ad69674eSGrygorii Strashko 430ad69674eSGrygorii Strashko index = of_property_match_string(dev, "interrupt-names", name); 431ad69674eSGrygorii Strashko if (index < 0) 432ad69674eSGrygorii Strashko return index; 433ad69674eSGrygorii Strashko 434ad69674eSGrygorii Strashko return of_irq_get(dev, index); 435ad69674eSGrygorii Strashko } 4366602c452SDmitry Torokhov EXPORT_SYMBOL_GPL(of_irq_get_byname); 437ad69674eSGrygorii Strashko 438ad69674eSGrygorii Strashko /** 43952f6537cSAndres Salomon * of_irq_count - Count the number of IRQs a node uses 44052f6537cSAndres Salomon * @dev: pointer to device tree node 44152f6537cSAndres Salomon */ 44252f6537cSAndres Salomon int of_irq_count(struct device_node *dev) 44352f6537cSAndres Salomon { 4443da52787SThierry Reding struct of_phandle_args irq; 44552f6537cSAndres Salomon int nr = 0; 44652f6537cSAndres Salomon 4473da52787SThierry Reding while (of_irq_parse_one(dev, nr, &irq) == 0) 44852f6537cSAndres Salomon nr++; 44952f6537cSAndres Salomon 45052f6537cSAndres Salomon return nr; 45152f6537cSAndres Salomon } 45252f6537cSAndres Salomon 45352f6537cSAndres Salomon /** 45452f6537cSAndres Salomon * of_irq_to_resource_table - Fill in resource table with node's IRQ info 45552f6537cSAndres Salomon * @dev: pointer to device tree node 45652f6537cSAndres Salomon * @res: array of resources to fill in 45752f6537cSAndres Salomon * @nr_irqs: the number of IRQs (and upper bound for num of @res elements) 45852f6537cSAndres Salomon * 45952f6537cSAndres Salomon * Returns the size of the filled in table (up to @nr_irqs). 46052f6537cSAndres Salomon */ 46152f6537cSAndres Salomon int of_irq_to_resource_table(struct device_node *dev, struct resource *res, 46252f6537cSAndres Salomon int nr_irqs) 46352f6537cSAndres Salomon { 46452f6537cSAndres Salomon int i; 46552f6537cSAndres Salomon 46652f6537cSAndres Salomon for (i = 0; i < nr_irqs; i++, res++) 46777a7300aSAnton Vorontsov if (!of_irq_to_resource(dev, i, res)) 46852f6537cSAndres Salomon break; 46952f6537cSAndres Salomon 47052f6537cSAndres Salomon return i; 47152f6537cSAndres Salomon } 472a4f8bf22SJohn Crispin EXPORT_SYMBOL_GPL(of_irq_to_resource_table); 473c71a54b0SRob Herring 47448a9b733SGeert Uytterhoeven struct of_intc_desc { 475c71a54b0SRob Herring struct list_head list; 476c71a54b0SRob Herring struct device_node *dev; 477c71a54b0SRob Herring struct device_node *interrupt_parent; 478c71a54b0SRob Herring }; 479c71a54b0SRob Herring 480c71a54b0SRob Herring /** 481c71a54b0SRob Herring * of_irq_init - Scan and init matching interrupt controllers in DT 482c71a54b0SRob Herring * @matches: 0 terminated array of nodes to match and init function to call 483c71a54b0SRob Herring * 484c71a54b0SRob Herring * This function scans the device tree for matching interrupt controller nodes, 485c71a54b0SRob Herring * and calls their initialization functions in order with parents first. 486c71a54b0SRob Herring */ 487c71a54b0SRob Herring void __init of_irq_init(const struct of_device_id *matches) 488c71a54b0SRob Herring { 489c71a54b0SRob Herring struct device_node *np, *parent = NULL; 49048a9b733SGeert Uytterhoeven struct of_intc_desc *desc, *temp_desc; 491c71a54b0SRob Herring struct list_head intc_desc_list, intc_parent_list; 492c71a54b0SRob Herring 493c71a54b0SRob Herring INIT_LIST_HEAD(&intc_desc_list); 494c71a54b0SRob Herring INIT_LIST_HEAD(&intc_parent_list); 495c71a54b0SRob Herring 496c71a54b0SRob Herring for_each_matching_node(np, matches) { 497bf49be02SPeter Crosthwaite if (!of_find_property(np, "interrupt-controller", NULL) || 498bf49be02SPeter Crosthwaite !of_device_is_available(np)) 499c71a54b0SRob Herring continue; 500c71a54b0SRob Herring /* 50148a9b733SGeert Uytterhoeven * Here, we allocate and populate an of_intc_desc with the node 502c71a54b0SRob Herring * pointer, interrupt-parent device_node etc. 503c71a54b0SRob Herring */ 504c71a54b0SRob Herring desc = kzalloc(sizeof(*desc), GFP_KERNEL); 5058363ccb9SJulia Lawall if (WARN_ON(!desc)) { 5068363ccb9SJulia Lawall of_node_put(np); 507c71a54b0SRob Herring goto err; 5088363ccb9SJulia Lawall } 509c71a54b0SRob Herring 5108363ccb9SJulia Lawall desc->dev = of_node_get(np); 511c71a54b0SRob Herring desc->interrupt_parent = of_irq_find_parent(np); 512d7fb6d0aSRob Herring if (desc->interrupt_parent == np) 513d7fb6d0aSRob Herring desc->interrupt_parent = NULL; 514c71a54b0SRob Herring list_add_tail(&desc->list, &intc_desc_list); 515c71a54b0SRob Herring } 516c71a54b0SRob Herring 517c71a54b0SRob Herring /* 518c71a54b0SRob Herring * The root irq controller is the one without an interrupt-parent. 519c71a54b0SRob Herring * That one goes first, followed by the controllers that reference it, 520c71a54b0SRob Herring * followed by the ones that reference the 2nd level controllers, etc. 521c71a54b0SRob Herring */ 522c71a54b0SRob Herring while (!list_empty(&intc_desc_list)) { 523c71a54b0SRob Herring /* 524c71a54b0SRob Herring * Process all controllers with the current 'parent'. 525c71a54b0SRob Herring * First pass will be looking for NULL as the parent. 526c71a54b0SRob Herring * The assumption is that NULL parent means a root controller. 527c71a54b0SRob Herring */ 528c71a54b0SRob Herring list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) { 529c71a54b0SRob Herring const struct of_device_id *match; 530c71a54b0SRob Herring int ret; 531c71a54b0SRob Herring of_irq_init_cb_t irq_init_cb; 532c71a54b0SRob Herring 533c71a54b0SRob Herring if (desc->interrupt_parent != parent) 534c71a54b0SRob Herring continue; 535c71a54b0SRob Herring 536c71a54b0SRob Herring list_del(&desc->list); 537c71a54b0SRob Herring match = of_match_node(matches, desc->dev); 538c71a54b0SRob Herring if (WARN(!match->data, 539c71a54b0SRob Herring "of_irq_init: no init function for %s\n", 540c71a54b0SRob Herring match->compatible)) { 541c71a54b0SRob Herring kfree(desc); 542c71a54b0SRob Herring continue; 543c71a54b0SRob Herring } 544c71a54b0SRob Herring 545c71a54b0SRob Herring pr_debug("of_irq_init: init %s @ %p, parent %p\n", 546c71a54b0SRob Herring match->compatible, 547c71a54b0SRob Herring desc->dev, desc->interrupt_parent); 548d2e41518SKim Phillips irq_init_cb = (of_irq_init_cb_t)match->data; 549c71a54b0SRob Herring ret = irq_init_cb(desc->dev, desc->interrupt_parent); 550c71a54b0SRob Herring if (ret) { 551c71a54b0SRob Herring kfree(desc); 552c71a54b0SRob Herring continue; 553c71a54b0SRob Herring } 554c71a54b0SRob Herring 555c71a54b0SRob Herring /* 556c71a54b0SRob Herring * This one is now set up; add it to the parent list so 557c71a54b0SRob Herring * its children can get processed in a subsequent pass. 558c71a54b0SRob Herring */ 559c71a54b0SRob Herring list_add_tail(&desc->list, &intc_parent_list); 560c71a54b0SRob Herring } 561c71a54b0SRob Herring 562c71a54b0SRob Herring /* Get the next pending parent that might have children */ 563c0cdfaa0SAxel Lin desc = list_first_entry_or_null(&intc_parent_list, 564c0cdfaa0SAxel Lin typeof(*desc), list); 565c0cdfaa0SAxel Lin if (!desc) { 566c71a54b0SRob Herring pr_err("of_irq_init: children remain, but no parents\n"); 567c71a54b0SRob Herring break; 568c71a54b0SRob Herring } 569c71a54b0SRob Herring list_del(&desc->list); 570c71a54b0SRob Herring parent = desc->dev; 571c71a54b0SRob Herring kfree(desc); 572c71a54b0SRob Herring } 573c71a54b0SRob Herring 574c71a54b0SRob Herring list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) { 575c71a54b0SRob Herring list_del(&desc->list); 576c71a54b0SRob Herring kfree(desc); 577c71a54b0SRob Herring } 578c71a54b0SRob Herring err: 579c71a54b0SRob Herring list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) { 580c71a54b0SRob Herring list_del(&desc->list); 5818363ccb9SJulia Lawall of_node_put(desc->dev); 582c71a54b0SRob Herring kfree(desc); 583c71a54b0SRob Herring } 584c71a54b0SRob Herring } 585c706c239SMarc Zyngier 586a251b263SMarc Zyngier static u32 __of_msi_map_rid(struct device *dev, struct device_node **np, 587a251b263SMarc Zyngier u32 rid_in) 5888db02d8bSDavid Daney { 5898db02d8bSDavid Daney struct device *parent_dev; 5908db02d8bSDavid Daney struct device_node *msi_controller_node; 591a251b263SMarc Zyngier struct device_node *msi_np = *np; 5928db02d8bSDavid Daney u32 map_mask, masked_rid, rid_base, msi_base, rid_len, phandle; 5938db02d8bSDavid Daney int msi_map_len; 5948db02d8bSDavid Daney bool matched; 5958db02d8bSDavid Daney u32 rid_out = rid_in; 5968db02d8bSDavid Daney const __be32 *msi_map = NULL; 5978db02d8bSDavid Daney 5988db02d8bSDavid Daney /* 5998db02d8bSDavid Daney * Walk up the device parent links looking for one with a 6008db02d8bSDavid Daney * "msi-map" property. 6018db02d8bSDavid Daney */ 6028db02d8bSDavid Daney for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent) { 6038db02d8bSDavid Daney if (!parent_dev->of_node) 6048db02d8bSDavid Daney continue; 6058db02d8bSDavid Daney 6068db02d8bSDavid Daney msi_map = of_get_property(parent_dev->of_node, 6078db02d8bSDavid Daney "msi-map", &msi_map_len); 6088db02d8bSDavid Daney if (!msi_map) 6098db02d8bSDavid Daney continue; 6108db02d8bSDavid Daney 6118db02d8bSDavid Daney if (msi_map_len % (4 * sizeof(__be32))) { 6128db02d8bSDavid Daney dev_err(parent_dev, "Error: Bad msi-map length: %d\n", 6138db02d8bSDavid Daney msi_map_len); 6148db02d8bSDavid Daney return rid_out; 6158db02d8bSDavid Daney } 6168db02d8bSDavid Daney /* We have a good parent_dev and msi_map, let's use them. */ 6178db02d8bSDavid Daney break; 6188db02d8bSDavid Daney } 6198db02d8bSDavid Daney if (!msi_map) 6208db02d8bSDavid Daney return rid_out; 6218db02d8bSDavid Daney 6228db02d8bSDavid Daney /* The default is to select all bits. */ 6238db02d8bSDavid Daney map_mask = 0xffffffff; 6248db02d8bSDavid Daney 6258db02d8bSDavid Daney /* 6268db02d8bSDavid Daney * Can be overridden by "msi-map-mask" property. If 6278db02d8bSDavid Daney * of_property_read_u32() fails, the default is used. 6288db02d8bSDavid Daney */ 6298db02d8bSDavid Daney of_property_read_u32(parent_dev->of_node, "msi-map-mask", &map_mask); 6308db02d8bSDavid Daney 6318db02d8bSDavid Daney masked_rid = map_mask & rid_in; 6328db02d8bSDavid Daney matched = false; 6338db02d8bSDavid Daney while (!matched && msi_map_len >= 4 * sizeof(__be32)) { 6348db02d8bSDavid Daney rid_base = be32_to_cpup(msi_map + 0); 6358db02d8bSDavid Daney phandle = be32_to_cpup(msi_map + 1); 6368db02d8bSDavid Daney msi_base = be32_to_cpup(msi_map + 2); 6378db02d8bSDavid Daney rid_len = be32_to_cpup(msi_map + 3); 6388db02d8bSDavid Daney 6398db02d8bSDavid Daney msi_controller_node = of_find_node_by_phandle(phandle); 6408db02d8bSDavid Daney 641a251b263SMarc Zyngier matched = (masked_rid >= rid_base && 642a251b263SMarc Zyngier masked_rid < rid_base + rid_len); 643a251b263SMarc Zyngier if (msi_np) 644a251b263SMarc Zyngier matched &= msi_np == msi_controller_node; 645a251b263SMarc Zyngier 646a251b263SMarc Zyngier if (matched && !msi_np) { 647a251b263SMarc Zyngier *np = msi_np = msi_controller_node; 648a251b263SMarc Zyngier break; 649a251b263SMarc Zyngier } 6508db02d8bSDavid Daney 6518db02d8bSDavid Daney of_node_put(msi_controller_node); 6528db02d8bSDavid Daney msi_map_len -= 4 * sizeof(__be32); 6538db02d8bSDavid Daney msi_map += 4; 6548db02d8bSDavid Daney } 6558db02d8bSDavid Daney if (!matched) 6568db02d8bSDavid Daney return rid_out; 6578db02d8bSDavid Daney 6588db02d8bSDavid Daney rid_out = masked_rid + msi_base; 6598db02d8bSDavid Daney dev_dbg(dev, 6608db02d8bSDavid Daney "msi-map at: %s, using mask %08x, rid-base: %08x, msi-base: %08x, length: %08x, rid: %08x -> %08x\n", 6618db02d8bSDavid Daney dev_name(parent_dev), map_mask, rid_base, msi_base, 6628db02d8bSDavid Daney rid_len, rid_in, rid_out); 6638db02d8bSDavid Daney 6648db02d8bSDavid Daney return rid_out; 6658db02d8bSDavid Daney } 66648ae34fbSMarc Zyngier 667a251b263SMarc Zyngier /** 668a251b263SMarc Zyngier * of_msi_map_rid - Map a MSI requester ID for a device. 669a251b263SMarc Zyngier * @dev: device for which the mapping is to be done. 670a251b263SMarc Zyngier * @msi_np: device node of the expected msi controller. 671a251b263SMarc Zyngier * @rid_in: unmapped MSI requester ID for the device. 672a251b263SMarc Zyngier * 673a251b263SMarc Zyngier * Walk up the device hierarchy looking for devices with a "msi-map" 674a251b263SMarc Zyngier * property. If found, apply the mapping to @rid_in. 675a251b263SMarc Zyngier * 676a251b263SMarc Zyngier * Returns the mapped MSI requester ID. 677a251b263SMarc Zyngier */ 678a251b263SMarc Zyngier u32 of_msi_map_rid(struct device *dev, struct device_node *msi_np, u32 rid_in) 679a251b263SMarc Zyngier { 680a251b263SMarc Zyngier return __of_msi_map_rid(dev, &msi_np, rid_in); 681a251b263SMarc Zyngier } 682a251b263SMarc Zyngier 68348ae34fbSMarc Zyngier static struct irq_domain *__of_get_msi_domain(struct device_node *np, 68448ae34fbSMarc Zyngier enum irq_domain_bus_token token) 68548ae34fbSMarc Zyngier { 68648ae34fbSMarc Zyngier struct irq_domain *d; 68748ae34fbSMarc Zyngier 68848ae34fbSMarc Zyngier d = irq_find_matching_host(np, token); 68948ae34fbSMarc Zyngier if (!d) 69048ae34fbSMarc Zyngier d = irq_find_host(np); 69148ae34fbSMarc Zyngier 69248ae34fbSMarc Zyngier return d; 69348ae34fbSMarc Zyngier } 69448ae34fbSMarc Zyngier 69548ae34fbSMarc Zyngier /** 69682b9b424SMarc Zyngier * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain 69782b9b424SMarc Zyngier * @dev: device for which the mapping is to be done. 69882b9b424SMarc Zyngier * @rid: Requester ID for the device. 69982b9b424SMarc Zyngier * 70082b9b424SMarc Zyngier * Walk up the device hierarchy looking for devices with a "msi-map" 70182b9b424SMarc Zyngier * property. 70282b9b424SMarc Zyngier * 70382b9b424SMarc Zyngier * Returns: the MSI domain for this device (or NULL on failure) 70482b9b424SMarc Zyngier */ 70582b9b424SMarc Zyngier struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 rid) 70682b9b424SMarc Zyngier { 70782b9b424SMarc Zyngier struct device_node *np = NULL; 70882b9b424SMarc Zyngier 70982b9b424SMarc Zyngier __of_msi_map_rid(dev, &np, rid); 71082b9b424SMarc Zyngier return __of_get_msi_domain(np, DOMAIN_BUS_PCI_MSI); 71182b9b424SMarc Zyngier } 71282b9b424SMarc Zyngier 71382b9b424SMarc Zyngier /** 71448ae34fbSMarc Zyngier * of_msi_get_domain - Use msi-parent to find the relevant MSI domain 71548ae34fbSMarc Zyngier * @dev: device for which the domain is requested 71648ae34fbSMarc Zyngier * @np: device node for @dev 71748ae34fbSMarc Zyngier * @token: bus type for this domain 71848ae34fbSMarc Zyngier * 71948ae34fbSMarc Zyngier * Parse the msi-parent property (both the simple and the complex 72048ae34fbSMarc Zyngier * versions), and returns the corresponding MSI domain. 72148ae34fbSMarc Zyngier * 72248ae34fbSMarc Zyngier * Returns: the MSI domain for this device (or NULL on failure). 72348ae34fbSMarc Zyngier */ 72448ae34fbSMarc Zyngier struct irq_domain *of_msi_get_domain(struct device *dev, 72548ae34fbSMarc Zyngier struct device_node *np, 72648ae34fbSMarc Zyngier enum irq_domain_bus_token token) 72748ae34fbSMarc Zyngier { 72848ae34fbSMarc Zyngier struct device_node *msi_np; 72948ae34fbSMarc Zyngier struct irq_domain *d; 73048ae34fbSMarc Zyngier 73148ae34fbSMarc Zyngier /* Check for a single msi-parent property */ 73248ae34fbSMarc Zyngier msi_np = of_parse_phandle(np, "msi-parent", 0); 73348ae34fbSMarc Zyngier if (msi_np && !of_property_read_bool(msi_np, "#msi-cells")) { 73448ae34fbSMarc Zyngier d = __of_get_msi_domain(msi_np, token); 73548ae34fbSMarc Zyngier if (!d) 73648ae34fbSMarc Zyngier of_node_put(msi_np); 73748ae34fbSMarc Zyngier return d; 73848ae34fbSMarc Zyngier } 73948ae34fbSMarc Zyngier 74048ae34fbSMarc Zyngier if (token == DOMAIN_BUS_PLATFORM_MSI) { 74148ae34fbSMarc Zyngier /* Check for the complex msi-parent version */ 74248ae34fbSMarc Zyngier struct of_phandle_args args; 74348ae34fbSMarc Zyngier int index = 0; 74448ae34fbSMarc Zyngier 74548ae34fbSMarc Zyngier while (!of_parse_phandle_with_args(np, "msi-parent", 74648ae34fbSMarc Zyngier "#msi-cells", 74748ae34fbSMarc Zyngier index, &args)) { 74848ae34fbSMarc Zyngier d = __of_get_msi_domain(args.np, token); 74948ae34fbSMarc Zyngier if (d) 75048ae34fbSMarc Zyngier return d; 75148ae34fbSMarc Zyngier 75248ae34fbSMarc Zyngier of_node_put(args.np); 75348ae34fbSMarc Zyngier index++; 75448ae34fbSMarc Zyngier } 75548ae34fbSMarc Zyngier } 75648ae34fbSMarc Zyngier 75748ae34fbSMarc Zyngier return NULL; 75848ae34fbSMarc Zyngier } 75961c08240SMarc Zyngier 76061c08240SMarc Zyngier /** 76161c08240SMarc Zyngier * of_msi_configure - Set the msi_domain field of a device 76261c08240SMarc Zyngier * @dev: device structure to associate with an MSI irq domain 76361c08240SMarc Zyngier * @np: device node for that device 76461c08240SMarc Zyngier */ 76561c08240SMarc Zyngier void of_msi_configure(struct device *dev, struct device_node *np) 76661c08240SMarc Zyngier { 76761c08240SMarc Zyngier dev_set_msi_domain(dev, 76861c08240SMarc Zyngier of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI)); 76961c08240SMarc Zyngier } 770