irqdomain.c 40 KB

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