irqdomain.c 38 KB

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