irqdomain.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. #define pr_fmt(fmt) "irq: " fmt
  2. #include <linux/debugfs.h>
  3. #include <linux/hardirq.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/irq.h>
  6. #include <linux/irqdesc.h>
  7. #include <linux/irqdomain.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/topology.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/slab.h>
  16. #include <linux/smp.h>
  17. #include <linux/fs.h>
  18. static LIST_HEAD(irq_domain_list);
  19. static DEFINE_MUTEX(irq_domain_mutex);
  20. static DEFINE_MUTEX(revmap_trees_mutex);
  21. static struct irq_domain *irq_default_domain;
  22. static int irq_domain_alloc_descs(int virq, unsigned int nr_irqs,
  23. irq_hw_number_t hwirq, int node);
  24. static void irq_domain_check_hierarchy(struct irq_domain *domain);
  25. struct irqchip_fwid {
  26. struct fwnode_handle fwnode;
  27. char *name;
  28. void *data;
  29. };
  30. /**
  31. * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
  32. * identifying an irq domain
  33. * @data: optional user-provided data
  34. *
  35. * Allocate a struct device_node, and return a poiner to the embedded
  36. * fwnode_handle (or NULL on failure).
  37. */
  38. struct fwnode_handle *irq_domain_alloc_fwnode(void *data)
  39. {
  40. struct irqchip_fwid *fwid;
  41. char *name;
  42. fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
  43. name = kasprintf(GFP_KERNEL, "irqchip@%p", data);
  44. if (!fwid || !name) {
  45. kfree(fwid);
  46. kfree(name);
  47. return NULL;
  48. }
  49. fwid->name = name;
  50. fwid->data = data;
  51. fwid->fwnode.type = FWNODE_IRQCHIP;
  52. return &fwid->fwnode;
  53. }
  54. EXPORT_SYMBOL_GPL(irq_domain_alloc_fwnode);
  55. /**
  56. * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
  57. *
  58. * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
  59. */
  60. void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
  61. {
  62. struct irqchip_fwid *fwid;
  63. if (WARN_ON(!is_fwnode_irqchip(fwnode)))
  64. return;
  65. fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
  66. kfree(fwid->name);
  67. kfree(fwid);
  68. }
  69. EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
  70. /**
  71. * __irq_domain_add() - Allocate a new irq_domain data structure
  72. * @of_node: optional device-tree node of the interrupt controller
  73. * @size: Size of linear map; 0 for radix mapping only
  74. * @hwirq_max: Maximum number of interrupts supported by controller
  75. * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
  76. * direct mapping
  77. * @ops: domain callbacks
  78. * @host_data: Controller private data pointer
  79. *
  80. * Allocates and initialize and irq_domain structure.
  81. * Returns pointer to IRQ domain, or NULL on failure.
  82. */
  83. struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
  84. irq_hw_number_t hwirq_max, int direct_max,
  85. const struct irq_domain_ops *ops,
  86. void *host_data)
  87. {
  88. struct irq_domain *domain;
  89. struct device_node *of_node;
  90. of_node = to_of_node(fwnode);
  91. domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
  92. GFP_KERNEL, of_node_to_nid(of_node));
  93. if (WARN_ON(!domain))
  94. return NULL;
  95. of_node_get(of_node);
  96. /* Fill structure */
  97. INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
  98. domain->ops = ops;
  99. domain->host_data = host_data;
  100. domain->fwnode = fwnode;
  101. domain->hwirq_max = hwirq_max;
  102. domain->revmap_size = size;
  103. domain->revmap_direct_max_irq = direct_max;
  104. irq_domain_check_hierarchy(domain);
  105. mutex_lock(&irq_domain_mutex);
  106. list_add(&domain->link, &irq_domain_list);
  107. mutex_unlock(&irq_domain_mutex);
  108. pr_debug("Added domain %s\n", domain->name);
  109. return domain;
  110. }
  111. EXPORT_SYMBOL_GPL(__irq_domain_add);
  112. /**
  113. * irq_domain_remove() - Remove an irq domain.
  114. * @domain: domain to remove
  115. *
  116. * This routine is used to remove an irq domain. The caller must ensure
  117. * that all mappings within the domain have been disposed of prior to
  118. * use, depending on the revmap type.
  119. */
  120. void irq_domain_remove(struct irq_domain *domain)
  121. {
  122. mutex_lock(&irq_domain_mutex);
  123. /*
  124. * radix_tree_delete() takes care of destroying the root
  125. * node when all entries are removed. Shout if there are
  126. * any mappings left.
  127. */
  128. WARN_ON(domain->revmap_tree.height);
  129. list_del(&domain->link);
  130. /*
  131. * If the going away domain is the default one, reset it.
  132. */
  133. if (unlikely(irq_default_domain == domain))
  134. irq_set_default_host(NULL);
  135. mutex_unlock(&irq_domain_mutex);
  136. pr_debug("Removed domain %s\n", domain->name);
  137. of_node_put(irq_domain_get_of_node(domain));
  138. kfree(domain);
  139. }
  140. EXPORT_SYMBOL_GPL(irq_domain_remove);
  141. /**
  142. * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
  143. * @of_node: pointer to interrupt controller's device tree node.
  144. * @size: total number of irqs in mapping
  145. * @first_irq: first number of irq block assigned to the domain,
  146. * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
  147. * pre-map all of the irqs in the domain to virqs starting at first_irq.
  148. * @ops: domain callbacks
  149. * @host_data: Controller private data pointer
  150. *
  151. * Allocates an irq_domain, and optionally if first_irq is positive then also
  152. * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
  153. *
  154. * This is intended to implement the expected behaviour for most
  155. * interrupt controllers. If device tree is used, then first_irq will be 0 and
  156. * irqs get mapped dynamically on the fly. However, if the controller requires
  157. * static virq assignments (non-DT boot) then it will set that up correctly.
  158. */
  159. struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
  160. unsigned int size,
  161. unsigned int first_irq,
  162. const struct irq_domain_ops *ops,
  163. void *host_data)
  164. {
  165. struct irq_domain *domain;
  166. domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
  167. if (!domain)
  168. return NULL;
  169. if (first_irq > 0) {
  170. if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
  171. /* attempt to allocated irq_descs */
  172. int rc = irq_alloc_descs(first_irq, first_irq, size,
  173. of_node_to_nid(of_node));
  174. if (rc < 0)
  175. pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  176. first_irq);
  177. }
  178. irq_domain_associate_many(domain, first_irq, 0, size);
  179. }
  180. return domain;
  181. }
  182. EXPORT_SYMBOL_GPL(irq_domain_add_simple);
  183. /**
  184. * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
  185. * @of_node: pointer to interrupt controller's device tree node.
  186. * @size: total number of irqs in legacy mapping
  187. * @first_irq: first number of irq block assigned to the domain
  188. * @first_hwirq: first hwirq number to use for the translation. Should normally
  189. * be '0', but a positive integer can be used if the effective
  190. * hwirqs numbering does not begin at zero.
  191. * @ops: map/unmap domain callbacks
  192. * @host_data: Controller private data pointer
  193. *
  194. * Note: the map() callback will be called before this function returns
  195. * for all legacy interrupts except 0 (which is always the invalid irq for
  196. * a legacy controller).
  197. */
  198. struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
  199. unsigned int size,
  200. unsigned int first_irq,
  201. irq_hw_number_t first_hwirq,
  202. const struct irq_domain_ops *ops,
  203. void *host_data)
  204. {
  205. struct irq_domain *domain;
  206. domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
  207. first_hwirq + size, 0, ops, host_data);
  208. if (domain)
  209. irq_domain_associate_many(domain, first_irq, first_hwirq, size);
  210. return domain;
  211. }
  212. EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
  213. /**
  214. * irq_find_matching_fwnode() - Locates a domain for a given fwnode
  215. * @fwnode: FW descriptor of the interrupt controller
  216. * @bus_token: domain-specific data
  217. */
  218. struct irq_domain *irq_find_matching_fwnode(struct fwnode_handle *fwnode,
  219. enum irq_domain_bus_token bus_token)
  220. {
  221. struct irq_domain *h, *found = NULL;
  222. int rc;
  223. /* We might want to match the legacy controller last since
  224. * it might potentially be set to match all interrupts in
  225. * the absence of a device node. This isn't a problem so far
  226. * yet though...
  227. *
  228. * bus_token == DOMAIN_BUS_ANY matches any domain, any other
  229. * values must generate an exact match for the domain to be
  230. * selected.
  231. */
  232. mutex_lock(&irq_domain_mutex);
  233. list_for_each_entry(h, &irq_domain_list, link) {
  234. if (h->ops->match)
  235. rc = h->ops->match(h, to_of_node(fwnode), bus_token);
  236. else
  237. rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
  238. ((bus_token == DOMAIN_BUS_ANY) ||
  239. (h->bus_token == bus_token)));
  240. if (rc) {
  241. found = h;
  242. break;
  243. }
  244. }
  245. mutex_unlock(&irq_domain_mutex);
  246. return found;
  247. }
  248. EXPORT_SYMBOL_GPL(irq_find_matching_fwnode);
  249. /**
  250. * irq_set_default_host() - Set a "default" irq domain
  251. * @domain: default domain pointer
  252. *
  253. * For convenience, it's possible to set a "default" domain that will be used
  254. * whenever NULL is passed to irq_create_mapping(). It makes life easier for
  255. * platforms that want to manipulate a few hard coded interrupt numbers that
  256. * aren't properly represented in the device-tree.
  257. */
  258. void irq_set_default_host(struct irq_domain *domain)
  259. {
  260. pr_debug("Default domain set to @0x%p\n", domain);
  261. irq_default_domain = domain;
  262. }
  263. EXPORT_SYMBOL_GPL(irq_set_default_host);
  264. void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
  265. {
  266. struct irq_data *irq_data = irq_get_irq_data(irq);
  267. irq_hw_number_t hwirq;
  268. if (WARN(!irq_data || irq_data->domain != domain,
  269. "virq%i doesn't exist; cannot disassociate\n", irq))
  270. return;
  271. hwirq = irq_data->hwirq;
  272. irq_set_status_flags(irq, IRQ_NOREQUEST);
  273. /* remove chip and handler */
  274. irq_set_chip_and_handler(irq, NULL, NULL);
  275. /* Make sure it's completed */
  276. synchronize_irq(irq);
  277. /* Tell the PIC about it */
  278. if (domain->ops->unmap)
  279. domain->ops->unmap(domain, irq);
  280. smp_mb();
  281. irq_data->domain = NULL;
  282. irq_data->hwirq = 0;
  283. /* Clear reverse map for this hwirq */
  284. if (hwirq < domain->revmap_size) {
  285. domain->linear_revmap[hwirq] = 0;
  286. } else {
  287. mutex_lock(&revmap_trees_mutex);
  288. radix_tree_delete(&domain->revmap_tree, hwirq);
  289. mutex_unlock(&revmap_trees_mutex);
  290. }
  291. }
  292. int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
  293. irq_hw_number_t hwirq)
  294. {
  295. struct irq_data *irq_data = irq_get_irq_data(virq);
  296. int ret;
  297. if (WARN(hwirq >= domain->hwirq_max,
  298. "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
  299. return -EINVAL;
  300. if (WARN(!irq_data, "error: virq%i is not allocated", virq))
  301. return -EINVAL;
  302. if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
  303. return -EINVAL;
  304. mutex_lock(&irq_domain_mutex);
  305. irq_data->hwirq = hwirq;
  306. irq_data->domain = domain;
  307. if (domain->ops->map) {
  308. ret = domain->ops->map(domain, virq, hwirq);
  309. if (ret != 0) {
  310. /*
  311. * If map() returns -EPERM, this interrupt is protected
  312. * by the firmware or some other service and shall not
  313. * be mapped. Don't bother telling the user about it.
  314. */
  315. if (ret != -EPERM) {
  316. pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
  317. domain->name, hwirq, virq, ret);
  318. }
  319. irq_data->domain = NULL;
  320. irq_data->hwirq = 0;
  321. mutex_unlock(&irq_domain_mutex);
  322. return ret;
  323. }
  324. /* If not already assigned, give the domain the chip's name */
  325. if (!domain->name && irq_data->chip)
  326. domain->name = irq_data->chip->name;
  327. }
  328. if (hwirq < domain->revmap_size) {
  329. domain->linear_revmap[hwirq] = virq;
  330. } else {
  331. mutex_lock(&revmap_trees_mutex);
  332. radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
  333. mutex_unlock(&revmap_trees_mutex);
  334. }
  335. mutex_unlock(&irq_domain_mutex);
  336. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  337. return 0;
  338. }
  339. EXPORT_SYMBOL_GPL(irq_domain_associate);
  340. void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
  341. irq_hw_number_t hwirq_base, int count)
  342. {
  343. struct device_node *of_node;
  344. int i;
  345. of_node = irq_domain_get_of_node(domain);
  346. pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
  347. of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
  348. for (i = 0; i < count; i++) {
  349. irq_domain_associate(domain, irq_base + i, hwirq_base + i);
  350. }
  351. }
  352. EXPORT_SYMBOL_GPL(irq_domain_associate_many);
  353. /**
  354. * irq_create_direct_mapping() - Allocate an irq for direct mapping
  355. * @domain: domain to allocate the irq for or NULL for default domain
  356. *
  357. * This routine is used for irq controllers which can choose the hardware
  358. * interrupt numbers they generate. In such a case it's simplest to use
  359. * the linux irq as the hardware interrupt number. It still uses the linear
  360. * or radix tree to store the mapping, but the irq controller can optimize
  361. * the revmap path by using the hwirq directly.
  362. */
  363. unsigned int irq_create_direct_mapping(struct irq_domain *domain)
  364. {
  365. struct device_node *of_node;
  366. unsigned int virq;
  367. if (domain == NULL)
  368. domain = irq_default_domain;
  369. of_node = irq_domain_get_of_node(domain);
  370. virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
  371. if (!virq) {
  372. pr_debug("create_direct virq allocation failed\n");
  373. return 0;
  374. }
  375. if (virq >= domain->revmap_direct_max_irq) {
  376. pr_err("ERROR: no free irqs available below %i maximum\n",
  377. domain->revmap_direct_max_irq);
  378. irq_free_desc(virq);
  379. return 0;
  380. }
  381. pr_debug("create_direct obtained virq %d\n", virq);
  382. if (irq_domain_associate(domain, virq, virq)) {
  383. irq_free_desc(virq);
  384. return 0;
  385. }
  386. return virq;
  387. }
  388. EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
  389. /**
  390. * irq_create_mapping() - Map a hardware interrupt into linux irq space
  391. * @domain: domain owning this hardware interrupt or NULL for default domain
  392. * @hwirq: hardware irq number in that domain space
  393. *
  394. * Only one mapping per hardware interrupt is permitted. Returns a linux
  395. * irq number.
  396. * If the sense/trigger is to be specified, set_irq_type() should be called
  397. * on the number returned from that call.
  398. */
  399. unsigned int irq_create_mapping(struct irq_domain *domain,
  400. irq_hw_number_t hwirq)
  401. {
  402. struct device_node *of_node;
  403. int virq;
  404. pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
  405. /* Look for default domain if nececssary */
  406. if (domain == NULL)
  407. domain = irq_default_domain;
  408. if (domain == NULL) {
  409. WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
  410. return 0;
  411. }
  412. pr_debug("-> using domain @%p\n", domain);
  413. of_node = irq_domain_get_of_node(domain);
  414. /* Check if mapping already exists */
  415. virq = irq_find_mapping(domain, hwirq);
  416. if (virq) {
  417. pr_debug("-> existing mapping on virq %d\n", virq);
  418. return virq;
  419. }
  420. /* Allocate a virtual interrupt number */
  421. virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node));
  422. if (virq <= 0) {
  423. pr_debug("-> virq allocation failed\n");
  424. return 0;
  425. }
  426. if (irq_domain_associate(domain, virq, hwirq)) {
  427. irq_free_desc(virq);
  428. return 0;
  429. }
  430. pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
  431. hwirq, of_node_full_name(of_node), virq);
  432. return virq;
  433. }
  434. EXPORT_SYMBOL_GPL(irq_create_mapping);
  435. /**
  436. * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
  437. * @domain: domain owning the interrupt range
  438. * @irq_base: beginning of linux IRQ range
  439. * @hwirq_base: beginning of hardware IRQ range
  440. * @count: Number of interrupts to map
  441. *
  442. * This routine is used for allocating and mapping a range of hardware
  443. * irqs to linux irqs where the linux irq numbers are at pre-defined
  444. * locations. For use by controllers that already have static mappings
  445. * to insert in to the domain.
  446. *
  447. * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
  448. * domain insertion.
  449. *
  450. * 0 is returned upon success, while any failure to establish a static
  451. * mapping is treated as an error.
  452. */
  453. int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
  454. irq_hw_number_t hwirq_base, int count)
  455. {
  456. struct device_node *of_node;
  457. int ret;
  458. of_node = irq_domain_get_of_node(domain);
  459. ret = irq_alloc_descs(irq_base, irq_base, count,
  460. of_node_to_nid(of_node));
  461. if (unlikely(ret < 0))
  462. return ret;
  463. irq_domain_associate_many(domain, irq_base, hwirq_base, count);
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
  467. static int irq_domain_translate(struct irq_domain *d,
  468. struct irq_fwspec *fwspec,
  469. irq_hw_number_t *hwirq, unsigned int *type)
  470. {
  471. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  472. if (d->ops->translate)
  473. return d->ops->translate(d, fwspec, hwirq, type);
  474. #endif
  475. if (d->ops->xlate)
  476. return d->ops->xlate(d, to_of_node(fwspec->fwnode),
  477. fwspec->param, fwspec->param_count,
  478. hwirq, type);
  479. /* If domain has no translation, then we assume interrupt line */
  480. *hwirq = fwspec->param[0];
  481. return 0;
  482. }
  483. static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
  484. struct irq_fwspec *fwspec)
  485. {
  486. int i;
  487. fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
  488. fwspec->param_count = irq_data->args_count;
  489. for (i = 0; i < irq_data->args_count; i++)
  490. fwspec->param[i] = irq_data->args[i];
  491. }
  492. unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
  493. {
  494. struct irq_domain *domain;
  495. irq_hw_number_t hwirq;
  496. unsigned int type = IRQ_TYPE_NONE;
  497. int virq;
  498. if (fwspec->fwnode) {
  499. domain = irq_find_matching_fwnode(fwspec->fwnode,
  500. DOMAIN_BUS_WIRED);
  501. if (!domain)
  502. domain = irq_find_matching_fwnode(fwspec->fwnode,
  503. DOMAIN_BUS_ANY);
  504. } else {
  505. domain = irq_default_domain;
  506. }
  507. if (!domain) {
  508. pr_warn("no irq domain found for %s !\n",
  509. of_node_full_name(to_of_node(fwspec->fwnode)));
  510. return 0;
  511. }
  512. if (irq_domain_translate(domain, fwspec, &hwirq, &type))
  513. return 0;
  514. if (irq_domain_is_hierarchy(domain)) {
  515. /*
  516. * If we've already configured this interrupt,
  517. * don't do it again, or hell will break loose.
  518. */
  519. virq = irq_find_mapping(domain, hwirq);
  520. if (virq)
  521. return virq;
  522. virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
  523. if (virq <= 0)
  524. return 0;
  525. } else {
  526. /* Create mapping */
  527. virq = irq_create_mapping(domain, hwirq);
  528. if (!virq)
  529. return virq;
  530. }
  531. /* Set type if specified and different than the current one */
  532. if (type != IRQ_TYPE_NONE &&
  533. type != irq_get_trigger_type(virq))
  534. irq_set_irq_type(virq, type);
  535. return virq;
  536. }
  537. EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
  538. unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
  539. {
  540. struct irq_fwspec fwspec;
  541. of_phandle_args_to_fwspec(irq_data, &fwspec);
  542. return irq_create_fwspec_mapping(&fwspec);
  543. }
  544. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  545. /**
  546. * irq_dispose_mapping() - Unmap an interrupt
  547. * @virq: linux irq number of the interrupt to unmap
  548. */
  549. void irq_dispose_mapping(unsigned int virq)
  550. {
  551. struct irq_data *irq_data = irq_get_irq_data(virq);
  552. struct irq_domain *domain;
  553. if (!virq || !irq_data)
  554. return;
  555. domain = irq_data->domain;
  556. if (WARN_ON(domain == NULL))
  557. return;
  558. irq_domain_disassociate(domain, virq);
  559. irq_free_desc(virq);
  560. }
  561. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  562. /**
  563. * irq_find_mapping() - Find a linux irq from an hw irq number.
  564. * @domain: domain owning this hardware interrupt
  565. * @hwirq: hardware irq number in that domain space
  566. */
  567. unsigned int irq_find_mapping(struct irq_domain *domain,
  568. irq_hw_number_t hwirq)
  569. {
  570. struct irq_data *data;
  571. /* Look for default domain if nececssary */
  572. if (domain == NULL)
  573. domain = irq_default_domain;
  574. if (domain == NULL)
  575. return 0;
  576. if (hwirq < domain->revmap_direct_max_irq) {
  577. data = irq_domain_get_irq_data(domain, hwirq);
  578. if (data && data->hwirq == hwirq)
  579. return hwirq;
  580. }
  581. /* Check if the hwirq is in the linear revmap. */
  582. if (hwirq < domain->revmap_size)
  583. return domain->linear_revmap[hwirq];
  584. rcu_read_lock();
  585. data = radix_tree_lookup(&domain->revmap_tree, hwirq);
  586. rcu_read_unlock();
  587. return data ? data->irq : 0;
  588. }
  589. EXPORT_SYMBOL_GPL(irq_find_mapping);
  590. #ifdef CONFIG_IRQ_DOMAIN_DEBUG
  591. static int virq_debug_show(struct seq_file *m, void *private)
  592. {
  593. unsigned long flags;
  594. struct irq_desc *desc;
  595. struct irq_domain *domain;
  596. struct radix_tree_iter iter;
  597. void *data, **slot;
  598. int i;
  599. seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
  600. "name", "mapped", "linear-max", "direct-max", "devtree-node");
  601. mutex_lock(&irq_domain_mutex);
  602. list_for_each_entry(domain, &irq_domain_list, link) {
  603. struct device_node *of_node;
  604. int count = 0;
  605. of_node = irq_domain_get_of_node(domain);
  606. radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
  607. count++;
  608. seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
  609. domain == irq_default_domain ? '*' : ' ', domain->name,
  610. domain->revmap_size + count, domain->revmap_size,
  611. domain->revmap_direct_max_irq,
  612. of_node ? of_node_full_name(of_node) : "");
  613. }
  614. mutex_unlock(&irq_domain_mutex);
  615. seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
  616. "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
  617. "active", "type", "domain");
  618. for (i = 1; i < nr_irqs; i++) {
  619. desc = irq_to_desc(i);
  620. if (!desc)
  621. continue;
  622. raw_spin_lock_irqsave(&desc->lock, flags);
  623. domain = desc->irq_data.domain;
  624. if (domain) {
  625. struct irq_chip *chip;
  626. int hwirq = desc->irq_data.hwirq;
  627. bool direct;
  628. seq_printf(m, "%5d ", i);
  629. seq_printf(m, "0x%05x ", hwirq);
  630. chip = irq_desc_get_chip(desc);
  631. seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
  632. data = irq_desc_get_chip_data(desc);
  633. seq_printf(m, data ? "0x%p " : " %p ", data);
  634. seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
  635. direct = (i == hwirq) && (i < domain->revmap_direct_max_irq);
  636. seq_printf(m, "%6s%-8s ",
  637. (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
  638. direct ? "(DIRECT)" : "");
  639. seq_printf(m, "%s\n", desc->irq_data.domain->name);
  640. }
  641. raw_spin_unlock_irqrestore(&desc->lock, flags);
  642. }
  643. return 0;
  644. }
  645. static int virq_debug_open(struct inode *inode, struct file *file)
  646. {
  647. return single_open(file, virq_debug_show, inode->i_private);
  648. }
  649. static const struct file_operations virq_debug_fops = {
  650. .open = virq_debug_open,
  651. .read = seq_read,
  652. .llseek = seq_lseek,
  653. .release = single_release,
  654. };
  655. static int __init irq_debugfs_init(void)
  656. {
  657. if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
  658. NULL, &virq_debug_fops) == NULL)
  659. return -ENOMEM;
  660. return 0;
  661. }
  662. __initcall(irq_debugfs_init);
  663. #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
  664. /**
  665. * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
  666. *
  667. * Device Tree IRQ specifier translation function which works with one cell
  668. * bindings where the cell value maps directly to the hwirq number.
  669. */
  670. int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
  671. const u32 *intspec, unsigned int intsize,
  672. unsigned long *out_hwirq, unsigned int *out_type)
  673. {
  674. if (WARN_ON(intsize < 1))
  675. return -EINVAL;
  676. *out_hwirq = intspec[0];
  677. *out_type = IRQ_TYPE_NONE;
  678. return 0;
  679. }
  680. EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
  681. /**
  682. * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
  683. *
  684. * Device Tree IRQ specifier translation function which works with two cell
  685. * bindings where the cell values map directly to the hwirq number
  686. * and linux irq flags.
  687. */
  688. int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
  689. const u32 *intspec, unsigned int intsize,
  690. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  691. {
  692. if (WARN_ON(intsize < 2))
  693. return -EINVAL;
  694. *out_hwirq = intspec[0];
  695. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  696. return 0;
  697. }
  698. EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
  699. /**
  700. * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
  701. *
  702. * Device Tree IRQ specifier translation function which works with either one
  703. * or two cell bindings where the cell values map directly to the hwirq number
  704. * and linux irq flags.
  705. *
  706. * Note: don't use this function unless your interrupt controller explicitly
  707. * supports both one and two cell bindings. For the majority of controllers
  708. * the _onecell() or _twocell() variants above should be used.
  709. */
  710. int irq_domain_xlate_onetwocell(struct irq_domain *d,
  711. struct device_node *ctrlr,
  712. const u32 *intspec, unsigned int intsize,
  713. unsigned long *out_hwirq, unsigned int *out_type)
  714. {
  715. if (WARN_ON(intsize < 1))
  716. return -EINVAL;
  717. *out_hwirq = intspec[0];
  718. *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
  719. return 0;
  720. }
  721. EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
  722. const struct irq_domain_ops irq_domain_simple_ops = {
  723. .xlate = irq_domain_xlate_onetwocell,
  724. };
  725. EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
  726. static int irq_domain_alloc_descs(int virq, unsigned int cnt,
  727. irq_hw_number_t hwirq, int node)
  728. {
  729. unsigned int hint;
  730. if (virq >= 0) {
  731. virq = irq_alloc_descs(virq, virq, cnt, node);
  732. } else {
  733. hint = hwirq % nr_irqs;
  734. if (hint == 0)
  735. hint++;
  736. virq = irq_alloc_descs_from(hint, cnt, node);
  737. if (virq <= 0 && hint > 1)
  738. virq = irq_alloc_descs_from(1, cnt, node);
  739. }
  740. return virq;
  741. }
  742. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  743. /**
  744. * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
  745. * @parent: Parent irq domain to associate with the new domain
  746. * @flags: Irq domain flags associated to the domain
  747. * @size: Size of the domain. See below
  748. * @fwnode: Optional fwnode of the interrupt controller
  749. * @ops: Pointer to the interrupt domain callbacks
  750. * @host_data: Controller private data pointer
  751. *
  752. * If @size is 0 a tree domain is created, otherwise a linear domain.
  753. *
  754. * If successful the parent is associated to the new domain and the
  755. * domain flags are set.
  756. * Returns pointer to IRQ domain, or NULL on failure.
  757. */
  758. struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
  759. unsigned int flags,
  760. unsigned int size,
  761. struct fwnode_handle *fwnode,
  762. const struct irq_domain_ops *ops,
  763. void *host_data)
  764. {
  765. struct irq_domain *domain;
  766. if (size)
  767. domain = irq_domain_create_linear(fwnode, size, ops, host_data);
  768. else
  769. domain = irq_domain_create_tree(fwnode, ops, host_data);
  770. if (domain) {
  771. domain->parent = parent;
  772. domain->flags |= flags;
  773. }
  774. return domain;
  775. }
  776. static void irq_domain_insert_irq(int virq)
  777. {
  778. struct irq_data *data;
  779. for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
  780. struct irq_domain *domain = data->domain;
  781. irq_hw_number_t hwirq = data->hwirq;
  782. if (hwirq < domain->revmap_size) {
  783. domain->linear_revmap[hwirq] = virq;
  784. } else {
  785. mutex_lock(&revmap_trees_mutex);
  786. radix_tree_insert(&domain->revmap_tree, hwirq, data);
  787. mutex_unlock(&revmap_trees_mutex);
  788. }
  789. /* If not already assigned, give the domain the chip's name */
  790. if (!domain->name && data->chip)
  791. domain->name = data->chip->name;
  792. }
  793. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  794. }
  795. static void irq_domain_remove_irq(int virq)
  796. {
  797. struct irq_data *data;
  798. irq_set_status_flags(virq, IRQ_NOREQUEST);
  799. irq_set_chip_and_handler(virq, NULL, NULL);
  800. synchronize_irq(virq);
  801. smp_mb();
  802. for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
  803. struct irq_domain *domain = data->domain;
  804. irq_hw_number_t hwirq = data->hwirq;
  805. if (hwirq < domain->revmap_size) {
  806. domain->linear_revmap[hwirq] = 0;
  807. } else {
  808. mutex_lock(&revmap_trees_mutex);
  809. radix_tree_delete(&domain->revmap_tree, hwirq);
  810. mutex_unlock(&revmap_trees_mutex);
  811. }
  812. }
  813. }
  814. static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
  815. struct irq_data *child)
  816. {
  817. struct irq_data *irq_data;
  818. irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
  819. irq_data_get_node(child));
  820. if (irq_data) {
  821. child->parent_data = irq_data;
  822. irq_data->irq = child->irq;
  823. irq_data->common = child->common;
  824. irq_data->domain = domain;
  825. }
  826. return irq_data;
  827. }
  828. static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
  829. {
  830. struct irq_data *irq_data, *tmp;
  831. int i;
  832. for (i = 0; i < nr_irqs; i++) {
  833. irq_data = irq_get_irq_data(virq + i);
  834. tmp = irq_data->parent_data;
  835. irq_data->parent_data = NULL;
  836. irq_data->domain = NULL;
  837. while (tmp) {
  838. irq_data = tmp;
  839. tmp = tmp->parent_data;
  840. kfree(irq_data);
  841. }
  842. }
  843. }
  844. static int irq_domain_alloc_irq_data(struct irq_domain *domain,
  845. unsigned int virq, unsigned int nr_irqs)
  846. {
  847. struct irq_data *irq_data;
  848. struct irq_domain *parent;
  849. int i;
  850. /* The outermost irq_data is embedded in struct irq_desc */
  851. for (i = 0; i < nr_irqs; i++) {
  852. irq_data = irq_get_irq_data(virq + i);
  853. irq_data->domain = domain;
  854. for (parent = domain->parent; parent; parent = parent->parent) {
  855. irq_data = irq_domain_insert_irq_data(parent, irq_data);
  856. if (!irq_data) {
  857. irq_domain_free_irq_data(virq, i + 1);
  858. return -ENOMEM;
  859. }
  860. }
  861. }
  862. return 0;
  863. }
  864. /**
  865. * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
  866. * @domain: domain to match
  867. * @virq: IRQ number to get irq_data
  868. */
  869. struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
  870. unsigned int virq)
  871. {
  872. struct irq_data *irq_data;
  873. for (irq_data = irq_get_irq_data(virq); irq_data;
  874. irq_data = irq_data->parent_data)
  875. if (irq_data->domain == domain)
  876. return irq_data;
  877. return NULL;
  878. }
  879. EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
  880. /**
  881. * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
  882. * @domain: Interrupt domain to match
  883. * @virq: IRQ number
  884. * @hwirq: The hwirq number
  885. * @chip: The associated interrupt chip
  886. * @chip_data: The associated chip data
  887. */
  888. int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
  889. irq_hw_number_t hwirq, struct irq_chip *chip,
  890. void *chip_data)
  891. {
  892. struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
  893. if (!irq_data)
  894. return -ENOENT;
  895. irq_data->hwirq = hwirq;
  896. irq_data->chip = chip ? chip : &no_irq_chip;
  897. irq_data->chip_data = chip_data;
  898. return 0;
  899. }
  900. /**
  901. * irq_domain_set_info - Set the complete data for a @virq in @domain
  902. * @domain: Interrupt domain to match
  903. * @virq: IRQ number
  904. * @hwirq: The hardware interrupt number
  905. * @chip: The associated interrupt chip
  906. * @chip_data: The associated interrupt chip data
  907. * @handler: The interrupt flow handler
  908. * @handler_data: The interrupt flow handler data
  909. * @handler_name: The interrupt handler name
  910. */
  911. void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
  912. irq_hw_number_t hwirq, struct irq_chip *chip,
  913. void *chip_data, irq_flow_handler_t handler,
  914. void *handler_data, const char *handler_name)
  915. {
  916. irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
  917. __irq_set_handler(virq, handler, 0, handler_name);
  918. irq_set_handler_data(virq, handler_data);
  919. }
  920. EXPORT_SYMBOL(irq_domain_set_info);
  921. /**
  922. * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
  923. * @irq_data: The pointer to irq_data
  924. */
  925. void irq_domain_reset_irq_data(struct irq_data *irq_data)
  926. {
  927. irq_data->hwirq = 0;
  928. irq_data->chip = &no_irq_chip;
  929. irq_data->chip_data = NULL;
  930. }
  931. /**
  932. * irq_domain_free_irqs_common - Clear irq_data and free the parent
  933. * @domain: Interrupt domain to match
  934. * @virq: IRQ number to start with
  935. * @nr_irqs: The number of irqs to free
  936. */
  937. void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
  938. unsigned int nr_irqs)
  939. {
  940. struct irq_data *irq_data;
  941. int i;
  942. for (i = 0; i < nr_irqs; i++) {
  943. irq_data = irq_domain_get_irq_data(domain, virq + i);
  944. if (irq_data)
  945. irq_domain_reset_irq_data(irq_data);
  946. }
  947. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  948. }
  949. /**
  950. * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
  951. * @domain: Interrupt domain to match
  952. * @virq: IRQ number to start with
  953. * @nr_irqs: The number of irqs to free
  954. */
  955. void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
  956. unsigned int nr_irqs)
  957. {
  958. int i;
  959. for (i = 0; i < nr_irqs; i++) {
  960. irq_set_handler_data(virq + i, NULL);
  961. irq_set_handler(virq + i, NULL);
  962. }
  963. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  964. }
  965. static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
  966. {
  967. return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
  968. }
  969. static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
  970. unsigned int irq_base,
  971. unsigned int nr_irqs)
  972. {
  973. domain->ops->free(domain, irq_base, nr_irqs);
  974. if (irq_domain_is_auto_recursive(domain)) {
  975. BUG_ON(!domain->parent);
  976. irq_domain_free_irqs_recursive(domain->parent, irq_base,
  977. nr_irqs);
  978. }
  979. }
  980. int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
  981. unsigned int irq_base,
  982. unsigned int nr_irqs, void *arg)
  983. {
  984. int ret = 0;
  985. struct irq_domain *parent = domain->parent;
  986. bool recursive = irq_domain_is_auto_recursive(domain);
  987. BUG_ON(recursive && !parent);
  988. if (recursive)
  989. ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
  990. nr_irqs, arg);
  991. if (ret >= 0)
  992. ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
  993. if (ret < 0 && recursive)
  994. irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
  995. return ret;
  996. }
  997. /**
  998. * __irq_domain_alloc_irqs - Allocate IRQs from domain
  999. * @domain: domain to allocate from
  1000. * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
  1001. * @nr_irqs: number of IRQs to allocate
  1002. * @node: NUMA node id for memory allocation
  1003. * @arg: domain specific argument
  1004. * @realloc: IRQ descriptors have already been allocated if true
  1005. *
  1006. * Allocate IRQ numbers and initialized all data structures to support
  1007. * hierarchy IRQ domains.
  1008. * Parameter @realloc is mainly to support legacy IRQs.
  1009. * Returns error code or allocated IRQ number
  1010. *
  1011. * The whole process to setup an IRQ has been split into two steps.
  1012. * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
  1013. * descriptor and required hardware resources. The second step,
  1014. * irq_domain_activate_irq(), is to program hardwares with preallocated
  1015. * resources. In this way, it's easier to rollback when failing to
  1016. * allocate resources.
  1017. */
  1018. int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
  1019. unsigned int nr_irqs, int node, void *arg,
  1020. bool realloc)
  1021. {
  1022. int i, ret, virq;
  1023. if (domain == NULL) {
  1024. domain = irq_default_domain;
  1025. if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
  1026. return -EINVAL;
  1027. }
  1028. if (!domain->ops->alloc) {
  1029. pr_debug("domain->ops->alloc() is NULL\n");
  1030. return -ENOSYS;
  1031. }
  1032. if (realloc && irq_base >= 0) {
  1033. virq = irq_base;
  1034. } else {
  1035. virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node);
  1036. if (virq < 0) {
  1037. pr_debug("cannot allocate IRQ(base %d, count %d)\n",
  1038. irq_base, nr_irqs);
  1039. return virq;
  1040. }
  1041. }
  1042. if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
  1043. pr_debug("cannot allocate memory for IRQ%d\n", virq);
  1044. ret = -ENOMEM;
  1045. goto out_free_desc;
  1046. }
  1047. mutex_lock(&irq_domain_mutex);
  1048. ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
  1049. if (ret < 0) {
  1050. mutex_unlock(&irq_domain_mutex);
  1051. goto out_free_irq_data;
  1052. }
  1053. for (i = 0; i < nr_irqs; i++)
  1054. irq_domain_insert_irq(virq + i);
  1055. mutex_unlock(&irq_domain_mutex);
  1056. return virq;
  1057. out_free_irq_data:
  1058. irq_domain_free_irq_data(virq, nr_irqs);
  1059. out_free_desc:
  1060. irq_free_descs(virq, nr_irqs);
  1061. return ret;
  1062. }
  1063. /**
  1064. * irq_domain_free_irqs - Free IRQ number and associated data structures
  1065. * @virq: base IRQ number
  1066. * @nr_irqs: number of IRQs to free
  1067. */
  1068. void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
  1069. {
  1070. struct irq_data *data = irq_get_irq_data(virq);
  1071. int i;
  1072. if (WARN(!data || !data->domain || !data->domain->ops->free,
  1073. "NULL pointer, cannot free irq\n"))
  1074. return;
  1075. mutex_lock(&irq_domain_mutex);
  1076. for (i = 0; i < nr_irqs; i++)
  1077. irq_domain_remove_irq(virq + i);
  1078. irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
  1079. mutex_unlock(&irq_domain_mutex);
  1080. irq_domain_free_irq_data(virq, nr_irqs);
  1081. irq_free_descs(virq, nr_irqs);
  1082. }
  1083. /**
  1084. * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
  1085. * @irq_base: Base IRQ number
  1086. * @nr_irqs: Number of IRQs to allocate
  1087. * @arg: Allocation data (arch/domain specific)
  1088. *
  1089. * Check whether the domain has been setup recursive. If not allocate
  1090. * through the parent domain.
  1091. */
  1092. int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
  1093. unsigned int irq_base, unsigned int nr_irqs,
  1094. void *arg)
  1095. {
  1096. /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
  1097. if (irq_domain_is_auto_recursive(domain))
  1098. return 0;
  1099. domain = domain->parent;
  1100. if (domain)
  1101. return irq_domain_alloc_irqs_recursive(domain, irq_base,
  1102. nr_irqs, arg);
  1103. return -ENOSYS;
  1104. }
  1105. /**
  1106. * irq_domain_free_irqs_parent - Free interrupts from parent domain
  1107. * @irq_base: Base IRQ number
  1108. * @nr_irqs: Number of IRQs to free
  1109. *
  1110. * Check whether the domain has been setup recursive. If not free
  1111. * through the parent domain.
  1112. */
  1113. void irq_domain_free_irqs_parent(struct irq_domain *domain,
  1114. unsigned int irq_base, unsigned int nr_irqs)
  1115. {
  1116. /* irq_domain_free_irqs_recursive() will call parent's free */
  1117. if (!irq_domain_is_auto_recursive(domain) && domain->parent)
  1118. irq_domain_free_irqs_recursive(domain->parent, irq_base,
  1119. nr_irqs);
  1120. }
  1121. /**
  1122. * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
  1123. * interrupt
  1124. * @irq_data: outermost irq_data associated with interrupt
  1125. *
  1126. * This is the second step to call domain_ops->activate to program interrupt
  1127. * controllers, so the interrupt could actually get delivered.
  1128. */
  1129. void irq_domain_activate_irq(struct irq_data *irq_data)
  1130. {
  1131. if (irq_data && irq_data->domain) {
  1132. struct irq_domain *domain = irq_data->domain;
  1133. if (irq_data->parent_data)
  1134. irq_domain_activate_irq(irq_data->parent_data);
  1135. if (domain->ops->activate)
  1136. domain->ops->activate(domain, irq_data);
  1137. }
  1138. }
  1139. /**
  1140. * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
  1141. * deactivate interrupt
  1142. * @irq_data: outermost irq_data associated with interrupt
  1143. *
  1144. * It calls domain_ops->deactivate to program interrupt controllers to disable
  1145. * interrupt delivery.
  1146. */
  1147. void irq_domain_deactivate_irq(struct irq_data *irq_data)
  1148. {
  1149. if (irq_data && irq_data->domain) {
  1150. struct irq_domain *domain = irq_data->domain;
  1151. if (domain->ops->deactivate)
  1152. domain->ops->deactivate(domain, irq_data);
  1153. if (irq_data->parent_data)
  1154. irq_domain_deactivate_irq(irq_data->parent_data);
  1155. }
  1156. }
  1157. static void irq_domain_check_hierarchy(struct irq_domain *domain)
  1158. {
  1159. /* Hierarchy irq_domains must implement callback alloc() */
  1160. if (domain->ops->alloc)
  1161. domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
  1162. }
  1163. #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  1164. /**
  1165. * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
  1166. * @domain: domain to match
  1167. * @virq: IRQ number to get irq_data
  1168. */
  1169. struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
  1170. unsigned int virq)
  1171. {
  1172. struct irq_data *irq_data = irq_get_irq_data(virq);
  1173. return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
  1174. }
  1175. EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
  1176. /**
  1177. * irq_domain_set_info - Set the complete data for a @virq in @domain
  1178. * @domain: Interrupt domain to match
  1179. * @virq: IRQ number
  1180. * @hwirq: The hardware interrupt number
  1181. * @chip: The associated interrupt chip
  1182. * @chip_data: The associated interrupt chip data
  1183. * @handler: The interrupt flow handler
  1184. * @handler_data: The interrupt flow handler data
  1185. * @handler_name: The interrupt handler name
  1186. */
  1187. void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
  1188. irq_hw_number_t hwirq, struct irq_chip *chip,
  1189. void *chip_data, irq_flow_handler_t handler,
  1190. void *handler_data, const char *handler_name)
  1191. {
  1192. irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
  1193. irq_set_chip_data(virq, chip_data);
  1194. irq_set_handler_data(virq, handler_data);
  1195. }
  1196. static void irq_domain_check_hierarchy(struct irq_domain *domain)
  1197. {
  1198. }
  1199. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */