irqdomain.c 41 KB

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