irqdomain.c 40 KB

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