irqdomain.c 38 KB

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