irqdomain.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "irq: " fmt
  3. #include <linux/acpi.h>
  4. #include <linux/debugfs.h>
  5. #include <linux/hardirq.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqdesc.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/topology.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/slab.h>
  18. #include <linux/smp.h>
  19. #include <linux/fs.h>
  20. static LIST_HEAD(irq_domain_list);
  21. static DEFINE_MUTEX(irq_domain_mutex);
  22. static struct irq_domain *irq_default_domain;
  23. static void irq_domain_check_hierarchy(struct irq_domain *domain);
  24. struct irqchip_fwid {
  25. struct fwnode_handle fwnode;
  26. unsigned int type;
  27. char *name;
  28. phys_addr_t *pa;
  29. };
  30. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  31. static void debugfs_add_domain_dir(struct irq_domain *d);
  32. static void debugfs_remove_domain_dir(struct irq_domain *d);
  33. #else
  34. static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
  35. static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
  36. #endif
  37. const struct fwnode_operations irqchip_fwnode_ops;
  38. EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
  39. /**
  40. * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
  41. * identifying an irq domain
  42. * @type: Type of irqchip_fwnode. See linux/irqdomain.h
  43. * @id: Optional user provided id if name != NULL
  44. * @name: Optional user provided domain name
  45. * @pa: Optional user-provided physical address
  46. *
  47. * Allocate a struct irqchip_fwid, and return a poiner to the embedded
  48. * fwnode_handle (or NULL on failure).
  49. *
  50. * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
  51. * solely to transport name information to irqdomain creation code. The
  52. * node is not stored. For other types the pointer is kept in the irq
  53. * domain struct.
  54. */
  55. struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
  56. const char *name,
  57. phys_addr_t *pa)
  58. {
  59. struct irqchip_fwid *fwid;
  60. char *n;
  61. fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
  62. switch (type) {
  63. case IRQCHIP_FWNODE_NAMED:
  64. n = kasprintf(GFP_KERNEL, "%s", name);
  65. break;
  66. case IRQCHIP_FWNODE_NAMED_ID:
  67. n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
  68. break;
  69. default:
  70. n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
  71. break;
  72. }
  73. if (!fwid || !n) {
  74. kfree(fwid);
  75. kfree(n);
  76. return NULL;
  77. }
  78. fwid->type = type;
  79. fwid->name = n;
  80. fwid->pa = pa;
  81. fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
  82. return &fwid->fwnode;
  83. }
  84. EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
  85. /**
  86. * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
  87. *
  88. * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
  89. */
  90. void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
  91. {
  92. struct irqchip_fwid *fwid;
  93. if (WARN_ON(!is_fwnode_irqchip(fwnode)))
  94. return;
  95. fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
  96. kfree(fwid->name);
  97. kfree(fwid);
  98. }
  99. EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
  100. /**
  101. * __irq_domain_add() - Allocate a new irq_domain data structure
  102. * @fwnode: firmware node for the interrupt controller
  103. * @size: Size of linear map; 0 for radix mapping only
  104. * @hwirq_max: Maximum number of interrupts supported by controller
  105. * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
  106. * direct mapping
  107. * @ops: domain callbacks
  108. * @host_data: Controller private data pointer
  109. *
  110. * Allocates and initializes an irq_domain structure.
  111. * Returns pointer to IRQ domain, or NULL on failure.
  112. */
  113. struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
  114. irq_hw_number_t hwirq_max, int direct_max,
  115. const struct irq_domain_ops *ops,
  116. void *host_data)
  117. {
  118. struct irqchip_fwid *fwid;
  119. struct irq_domain *domain;
  120. static atomic_t unknown_domains;
  121. domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
  122. GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
  123. if (!domain)
  124. return NULL;
  125. if (is_fwnode_irqchip(fwnode)) {
  126. fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
  127. switch (fwid->type) {
  128. case IRQCHIP_FWNODE_NAMED:
  129. case IRQCHIP_FWNODE_NAMED_ID:
  130. domain->fwnode = fwnode;
  131. domain->name = kstrdup(fwid->name, GFP_KERNEL);
  132. if (!domain->name) {
  133. kfree(domain);
  134. return NULL;
  135. }
  136. domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
  137. break;
  138. default:
  139. domain->fwnode = fwnode;
  140. domain->name = fwid->name;
  141. break;
  142. }
  143. } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
  144. is_software_node(fwnode)) {
  145. char *name;
  146. /*
  147. * fwnode paths contain '/', which debugfs is legitimately
  148. * unhappy about. Replace them with ':', which does
  149. * the trick and is not as offensive as '\'...
  150. */
  151. name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
  152. if (!name) {
  153. kfree(domain);
  154. return NULL;
  155. }
  156. strreplace(name, '/', ':');
  157. domain->name = name;
  158. domain->fwnode = fwnode;
  159. domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
  160. }
  161. if (!domain->name) {
  162. if (fwnode)
  163. pr_err("Invalid fwnode type for irqdomain\n");
  164. domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
  165. atomic_inc_return(&unknown_domains));
  166. if (!domain->name) {
  167. kfree(domain);
  168. return NULL;
  169. }
  170. domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
  171. }
  172. fwnode_handle_get(fwnode);
  173. fwnode_dev_initialized(fwnode, true);
  174. /* Fill structure */
  175. INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
  176. mutex_init(&domain->revmap_tree_mutex);
  177. domain->ops = ops;
  178. domain->host_data = host_data;
  179. domain->hwirq_max = hwirq_max;
  180. domain->revmap_size = size;
  181. domain->revmap_direct_max_irq = direct_max;
  182. irq_domain_check_hierarchy(domain);
  183. mutex_lock(&irq_domain_mutex);
  184. debugfs_add_domain_dir(domain);
  185. list_add(&domain->link, &irq_domain_list);
  186. mutex_unlock(&irq_domain_mutex);
  187. pr_debug("Added domain %s\n", domain->name);
  188. return domain;
  189. }
  190. EXPORT_SYMBOL_GPL(__irq_domain_add);
  191. /**
  192. * irq_domain_remove() - Remove an irq domain.
  193. * @domain: domain to remove
  194. *
  195. * This routine is used to remove an irq domain. The caller must ensure
  196. * that all mappings within the domain have been disposed of prior to
  197. * use, depending on the revmap type.
  198. */
  199. void irq_domain_remove(struct irq_domain *domain)
  200. {
  201. mutex_lock(&irq_domain_mutex);
  202. debugfs_remove_domain_dir(domain);
  203. WARN_ON(!radix_tree_empty(&domain->revmap_tree));
  204. list_del(&domain->link);
  205. /*
  206. * If the going away domain is the default one, reset it.
  207. */
  208. if (unlikely(irq_default_domain == domain))
  209. irq_set_default_host(NULL);
  210. mutex_unlock(&irq_domain_mutex);
  211. pr_debug("Removed domain %s\n", domain->name);
  212. fwnode_dev_initialized(domain->fwnode, false);
  213. fwnode_handle_put(domain->fwnode);
  214. if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
  215. kfree(domain->name);
  216. kfree(domain);
  217. }
  218. EXPORT_SYMBOL_GPL(irq_domain_remove);
  219. void irq_domain_update_bus_token(struct irq_domain *domain,
  220. enum irq_domain_bus_token bus_token)
  221. {
  222. char *name;
  223. if (domain->bus_token == bus_token)
  224. return;
  225. mutex_lock(&irq_domain_mutex);
  226. domain->bus_token = bus_token;
  227. name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
  228. if (!name) {
  229. mutex_unlock(&irq_domain_mutex);
  230. return;
  231. }
  232. debugfs_remove_domain_dir(domain);
  233. if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
  234. kfree(domain->name);
  235. else
  236. domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
  237. domain->name = name;
  238. debugfs_add_domain_dir(domain);
  239. mutex_unlock(&irq_domain_mutex);
  240. }
  241. EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
  242. /**
  243. * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
  244. * @of_node: pointer to interrupt controller's device tree node.
  245. * @size: total number of irqs in mapping
  246. * @first_irq: first number of irq block assigned to the domain,
  247. * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
  248. * pre-map all of the irqs in the domain to virqs starting at first_irq.
  249. * @ops: domain callbacks
  250. * @host_data: Controller private data pointer
  251. *
  252. * Allocates an irq_domain, and optionally if first_irq is positive then also
  253. * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
  254. *
  255. * This is intended to implement the expected behaviour for most
  256. * interrupt controllers. If device tree is used, then first_irq will be 0 and
  257. * irqs get mapped dynamically on the fly. However, if the controller requires
  258. * static virq assignments (non-DT boot) then it will set that up correctly.
  259. */
  260. struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
  261. unsigned int size,
  262. unsigned int first_irq,
  263. const struct irq_domain_ops *ops,
  264. void *host_data)
  265. {
  266. struct irq_domain *domain;
  267. domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
  268. if (!domain)
  269. return NULL;
  270. if (first_irq > 0) {
  271. if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
  272. /* attempt to allocated irq_descs */
  273. int rc = irq_alloc_descs(first_irq, first_irq, size,
  274. of_node_to_nid(of_node));
  275. if (rc < 0)
  276. pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  277. first_irq);
  278. }
  279. irq_domain_associate_many(domain, first_irq, 0, size);
  280. }
  281. return domain;
  282. }
  283. EXPORT_SYMBOL_GPL(irq_domain_add_simple);
  284. /**
  285. * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
  286. * @of_node: pointer to interrupt controller's device tree node.
  287. * @size: total number of irqs in legacy mapping
  288. * @first_irq: first number of irq block assigned to the domain
  289. * @first_hwirq: first hwirq number to use for the translation. Should normally
  290. * be '0', but a positive integer can be used if the effective
  291. * hwirqs numbering does not begin at zero.
  292. * @ops: map/unmap domain callbacks
  293. * @host_data: Controller private data pointer
  294. *
  295. * Note: the map() callback will be called before this function returns
  296. * for all legacy interrupts except 0 (which is always the invalid irq for
  297. * a legacy controller).
  298. */
  299. struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
  300. unsigned int size,
  301. unsigned int first_irq,
  302. irq_hw_number_t first_hwirq,
  303. const struct irq_domain_ops *ops,
  304. void *host_data)
  305. {
  306. struct irq_domain *domain;
  307. domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
  308. first_hwirq + size, 0, ops, host_data);
  309. if (domain)
  310. irq_domain_associate_many(domain, first_irq, first_hwirq, size);
  311. return domain;
  312. }
  313. EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
  314. /**
  315. * irq_find_matching_fwspec() - Locates a domain for a given fwspec
  316. * @fwspec: FW specifier for an interrupt
  317. * @bus_token: domain-specific data
  318. */
  319. struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
  320. enum irq_domain_bus_token bus_token)
  321. {
  322. struct irq_domain *h, *found = NULL;
  323. struct fwnode_handle *fwnode = fwspec->fwnode;
  324. int rc;
  325. /* We might want to match the legacy controller last since
  326. * it might potentially be set to match all interrupts in
  327. * the absence of a device node. This isn't a problem so far
  328. * yet though...
  329. *
  330. * bus_token == DOMAIN_BUS_ANY matches any domain, any other
  331. * values must generate an exact match for the domain to be
  332. * selected.
  333. */
  334. mutex_lock(&irq_domain_mutex);
  335. list_for_each_entry(h, &irq_domain_list, link) {
  336. if (h->ops->select && fwspec->param_count)
  337. rc = h->ops->select(h, fwspec, bus_token);
  338. else if (h->ops->match)
  339. rc = h->ops->match(h, to_of_node(fwnode), bus_token);
  340. else
  341. rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
  342. ((bus_token == DOMAIN_BUS_ANY) ||
  343. (h->bus_token == bus_token)));
  344. if (rc) {
  345. found = h;
  346. break;
  347. }
  348. }
  349. mutex_unlock(&irq_domain_mutex);
  350. return found;
  351. }
  352. EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
  353. /**
  354. * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
  355. * IRQ remapping
  356. *
  357. * Return: false if any MSI irq domain does not support IRQ remapping,
  358. * true otherwise (including if there is no MSI irq domain)
  359. */
  360. bool irq_domain_check_msi_remap(void)
  361. {
  362. struct irq_domain *h;
  363. bool ret = true;
  364. mutex_lock(&irq_domain_mutex);
  365. list_for_each_entry(h, &irq_domain_list, link) {
  366. if (irq_domain_is_msi(h) &&
  367. !irq_domain_hierarchical_is_msi_remap(h)) {
  368. ret = false;
  369. break;
  370. }
  371. }
  372. mutex_unlock(&irq_domain_mutex);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
  376. /**
  377. * irq_set_default_host() - Set a "default" irq domain
  378. * @domain: default domain pointer
  379. *
  380. * For convenience, it's possible to set a "default" domain that will be used
  381. * whenever NULL is passed to irq_create_mapping(). It makes life easier for
  382. * platforms that want to manipulate a few hard coded interrupt numbers that
  383. * aren't properly represented in the device-tree.
  384. */
  385. void irq_set_default_host(struct irq_domain *domain)
  386. {
  387. pr_debug("Default domain set to @0x%p\n", domain);
  388. irq_default_domain = domain;
  389. }
  390. EXPORT_SYMBOL_GPL(irq_set_default_host);
  391. /**
  392. * irq_get_default_host() - Retrieve the "default" irq domain
  393. *
  394. * Returns: the default domain, if any.
  395. *
  396. * Modern code should never use this. This should only be used on
  397. * systems that cannot implement a firmware->fwnode mapping (which
  398. * both DT and ACPI provide).
  399. */
  400. struct irq_domain *irq_get_default_host(void)
  401. {
  402. return irq_default_domain;
  403. }
  404. static void irq_domain_clear_mapping(struct irq_domain *domain,
  405. irq_hw_number_t hwirq)
  406. {
  407. if (hwirq < domain->revmap_size) {
  408. domain->linear_revmap[hwirq] = 0;
  409. } else {
  410. mutex_lock(&domain->revmap_tree_mutex);
  411. radix_tree_delete(&domain->revmap_tree, hwirq);
  412. mutex_unlock(&domain->revmap_tree_mutex);
  413. }
  414. }
  415. static void irq_domain_set_mapping(struct irq_domain *domain,
  416. irq_hw_number_t hwirq,
  417. struct irq_data *irq_data)
  418. {
  419. if (hwirq < domain->revmap_size) {
  420. domain->linear_revmap[hwirq] = irq_data->irq;
  421. } else {
  422. mutex_lock(&domain->revmap_tree_mutex);
  423. radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
  424. mutex_unlock(&domain->revmap_tree_mutex);
  425. }
  426. }
  427. void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
  428. {
  429. struct irq_data *irq_data = irq_get_irq_data(irq);
  430. irq_hw_number_t hwirq;
  431. if (WARN(!irq_data || irq_data->domain != domain,
  432. "virq%i doesn't exist; cannot disassociate\n", irq))
  433. return;
  434. hwirq = irq_data->hwirq;
  435. irq_set_status_flags(irq, IRQ_NOREQUEST);
  436. /* remove chip and handler */
  437. irq_set_chip_and_handler(irq, NULL, NULL);
  438. /* Make sure it's completed */
  439. synchronize_irq(irq);
  440. /* Tell the PIC about it */
  441. if (domain->ops->unmap)
  442. domain->ops->unmap(domain, irq);
  443. smp_mb();
  444. irq_data->domain = NULL;
  445. irq_data->hwirq = 0;
  446. domain->mapcount--;
  447. /* Clear reverse map for this hwirq */
  448. irq_domain_clear_mapping(domain, hwirq);
  449. }
  450. int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
  451. irq_hw_number_t hwirq)
  452. {
  453. struct irq_data *irq_data = irq_get_irq_data(virq);
  454. int ret;
  455. if (WARN(hwirq >= domain->hwirq_max,
  456. "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
  457. return -EINVAL;
  458. if (WARN(!irq_data, "error: virq%i is not allocated", virq))
  459. return -EINVAL;
  460. if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
  461. return -EINVAL;
  462. mutex_lock(&irq_domain_mutex);
  463. irq_data->hwirq = hwirq;
  464. irq_data->domain = domain;
  465. if (domain->ops->map) {
  466. ret = domain->ops->map(domain, virq, hwirq);
  467. if (ret != 0) {
  468. /*
  469. * If map() returns -EPERM, this interrupt is protected
  470. * by the firmware or some other service and shall not
  471. * be mapped. Don't bother telling the user about it.
  472. */
  473. if (ret != -EPERM) {
  474. pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
  475. domain->name, hwirq, virq, ret);
  476. }
  477. irq_data->domain = NULL;
  478. irq_data->hwirq = 0;
  479. mutex_unlock(&irq_domain_mutex);
  480. return ret;
  481. }
  482. /* If not already assigned, give the domain the chip's name */
  483. if (!domain->name && irq_data->chip)
  484. domain->name = irq_data->chip->name;
  485. }
  486. domain->mapcount++;
  487. irq_domain_set_mapping(domain, hwirq, irq_data);
  488. mutex_unlock(&irq_domain_mutex);
  489. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  490. return 0;
  491. }
  492. EXPORT_SYMBOL_GPL(irq_domain_associate);
  493. void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
  494. irq_hw_number_t hwirq_base, int count)
  495. {
  496. struct device_node *of_node;
  497. int i;
  498. of_node = irq_domain_get_of_node(domain);
  499. pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
  500. of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
  501. for (i = 0; i < count; i++) {
  502. irq_domain_associate(domain, irq_base + i, hwirq_base + i);
  503. }
  504. }
  505. EXPORT_SYMBOL_GPL(irq_domain_associate_many);
  506. /**
  507. * irq_create_direct_mapping() - Allocate an irq for direct mapping
  508. * @domain: domain to allocate the irq for or NULL for default domain
  509. *
  510. * This routine is used for irq controllers which can choose the hardware
  511. * interrupt numbers they generate. In such a case it's simplest to use
  512. * the linux irq as the hardware interrupt number. It still uses the linear
  513. * or radix tree to store the mapping, but the irq controller can optimize
  514. * the revmap path by using the hwirq directly.
  515. */
  516. unsigned int irq_create_direct_mapping(struct irq_domain *domain)
  517. {
  518. struct device_node *of_node;
  519. unsigned int virq;
  520. if (domain == NULL)
  521. domain = irq_default_domain;
  522. of_node = irq_domain_get_of_node(domain);
  523. virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
  524. if (!virq) {
  525. pr_debug("create_direct virq allocation failed\n");
  526. return 0;
  527. }
  528. if (virq >= domain->revmap_direct_max_irq) {
  529. pr_err("ERROR: no free irqs available below %i maximum\n",
  530. domain->revmap_direct_max_irq);
  531. irq_free_desc(virq);
  532. return 0;
  533. }
  534. pr_debug("create_direct obtained virq %d\n", virq);
  535. if (irq_domain_associate(domain, virq, virq)) {
  536. irq_free_desc(virq);
  537. return 0;
  538. }
  539. return virq;
  540. }
  541. EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
  542. /**
  543. * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
  544. * @domain: domain owning this hardware interrupt or NULL for default domain
  545. * @hwirq: hardware irq number in that domain space
  546. * @affinity: irq affinity
  547. *
  548. * Only one mapping per hardware interrupt is permitted. Returns a linux
  549. * irq number.
  550. * If the sense/trigger is to be specified, set_irq_type() should be called
  551. * on the number returned from that call.
  552. */
  553. unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
  554. irq_hw_number_t hwirq,
  555. const struct irq_affinity_desc *affinity)
  556. {
  557. struct device_node *of_node;
  558. int virq;
  559. pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
  560. /* Look for default domain if nececssary */
  561. if (domain == NULL)
  562. domain = irq_default_domain;
  563. if (domain == NULL) {
  564. WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
  565. return 0;
  566. }
  567. pr_debug("-> using domain @%p\n", domain);
  568. of_node = irq_domain_get_of_node(domain);
  569. /* Check if mapping already exists */
  570. virq = irq_find_mapping(domain, hwirq);
  571. if (virq) {
  572. pr_debug("-> existing mapping on virq %d\n", virq);
  573. return virq;
  574. }
  575. /* Allocate a virtual interrupt number */
  576. virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
  577. affinity);
  578. if (virq <= 0) {
  579. pr_debug("-> virq allocation failed\n");
  580. return 0;
  581. }
  582. if (irq_domain_associate(domain, virq, hwirq)) {
  583. irq_free_desc(virq);
  584. return 0;
  585. }
  586. pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
  587. hwirq, of_node_full_name(of_node), virq);
  588. return virq;
  589. }
  590. EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
  591. /**
  592. * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
  593. * @domain: domain owning the interrupt range
  594. * @irq_base: beginning of linux IRQ range
  595. * @hwirq_base: beginning of hardware IRQ range
  596. * @count: Number of interrupts to map
  597. *
  598. * This routine is used for allocating and mapping a range of hardware
  599. * irqs to linux irqs where the linux irq numbers are at pre-defined
  600. * locations. For use by controllers that already have static mappings
  601. * to insert in to the domain.
  602. *
  603. * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
  604. * domain insertion.
  605. *
  606. * 0 is returned upon success, while any failure to establish a static
  607. * mapping is treated as an error.
  608. */
  609. int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
  610. irq_hw_number_t hwirq_base, int count)
  611. {
  612. struct device_node *of_node;
  613. int ret;
  614. of_node = irq_domain_get_of_node(domain);
  615. ret = irq_alloc_descs(irq_base, irq_base, count,
  616. of_node_to_nid(of_node));
  617. if (unlikely(ret < 0))
  618. return ret;
  619. irq_domain_associate_many(domain, irq_base, hwirq_base, count);
  620. return 0;
  621. }
  622. EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
  623. static int irq_domain_translate(struct irq_domain *d,
  624. struct irq_fwspec *fwspec,
  625. irq_hw_number_t *hwirq, unsigned int *type)
  626. {
  627. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  628. if (d->ops->translate)
  629. return d->ops->translate(d, fwspec, hwirq, type);
  630. #endif
  631. if (d->ops->xlate)
  632. return d->ops->xlate(d, to_of_node(fwspec->fwnode),
  633. fwspec->param, fwspec->param_count,
  634. hwirq, type);
  635. /* If domain has no translation, then we assume interrupt line */
  636. *hwirq = fwspec->param[0];
  637. return 0;
  638. }
  639. static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
  640. unsigned int count,
  641. struct irq_fwspec *fwspec)
  642. {
  643. int i;
  644. fwspec->fwnode = np ? &np->fwnode : NULL;
  645. fwspec->param_count = count;
  646. for (i = 0; i < count; i++)
  647. fwspec->param[i] = args[i];
  648. }
  649. unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
  650. {
  651. struct irq_domain *domain;
  652. struct irq_data *irq_data;
  653. irq_hw_number_t hwirq;
  654. unsigned int type = IRQ_TYPE_NONE;
  655. int virq;
  656. if (fwspec->fwnode) {
  657. domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
  658. if (!domain)
  659. domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
  660. } else {
  661. domain = irq_default_domain;
  662. }
  663. if (!domain) {
  664. pr_warn("no irq domain found for %s !\n",
  665. of_node_full_name(to_of_node(fwspec->fwnode)));
  666. return 0;
  667. }
  668. if (irq_domain_translate(domain, fwspec, &hwirq, &type))
  669. return 0;
  670. /*
  671. * WARN if the irqchip returns a type with bits
  672. * outside the sense mask set and clear these bits.
  673. */
  674. if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
  675. type &= IRQ_TYPE_SENSE_MASK;
  676. /*
  677. * If we've already configured this interrupt,
  678. * don't do it again, or hell will break loose.
  679. */
  680. virq = irq_find_mapping(domain, hwirq);
  681. if (virq) {
  682. /*
  683. * If the trigger type is not specified or matches the
  684. * current trigger type then we are done so return the
  685. * interrupt number.
  686. */
  687. if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
  688. return virq;
  689. /*
  690. * If the trigger type has not been set yet, then set
  691. * it now and return the interrupt number.
  692. */
  693. if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
  694. irq_data = irq_get_irq_data(virq);
  695. if (!irq_data)
  696. return 0;
  697. irqd_set_trigger_type(irq_data, type);
  698. return virq;
  699. }
  700. pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
  701. hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
  702. return 0;
  703. }
  704. if (irq_domain_is_hierarchy(domain)) {
  705. virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
  706. if (virq <= 0)
  707. return 0;
  708. } else {
  709. /* Create mapping */
  710. virq = irq_create_mapping(domain, hwirq);
  711. if (!virq)
  712. return virq;
  713. }
  714. irq_data = irq_get_irq_data(virq);
  715. if (!irq_data) {
  716. if (irq_domain_is_hierarchy(domain))
  717. irq_domain_free_irqs(virq, 1);
  718. else
  719. irq_dispose_mapping(virq);
  720. return 0;
  721. }
  722. /* Store trigger type */
  723. irqd_set_trigger_type(irq_data, type);
  724. return virq;
  725. }
  726. EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
  727. unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
  728. {
  729. struct irq_fwspec fwspec;
  730. of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
  731. irq_data->args_count, &fwspec);
  732. return irq_create_fwspec_mapping(&fwspec);
  733. }
  734. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  735. /**
  736. * irq_dispose_mapping() - Unmap an interrupt
  737. * @virq: linux irq number of the interrupt to unmap
  738. */
  739. void irq_dispose_mapping(unsigned int virq)
  740. {
  741. struct irq_data *irq_data = irq_get_irq_data(virq);
  742. struct irq_domain *domain;
  743. if (!virq || !irq_data)
  744. return;
  745. domain = irq_data->domain;
  746. if (WARN_ON(domain == NULL))
  747. return;
  748. if (irq_domain_is_hierarchy(domain)) {
  749. irq_domain_free_irqs(virq, 1);
  750. } else {
  751. irq_domain_disassociate(domain, virq);
  752. irq_free_desc(virq);
  753. }
  754. }
  755. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  756. /**
  757. * irq_find_mapping() - Find a linux irq from a hw irq number.
  758. * @domain: domain owning this hardware interrupt
  759. * @hwirq: hardware irq number in that domain space
  760. */
  761. unsigned int irq_find_mapping(struct irq_domain *domain,
  762. irq_hw_number_t hwirq)
  763. {
  764. struct irq_data *data;
  765. /* Look for default domain if nececssary */
  766. if (domain == NULL)
  767. domain = irq_default_domain;
  768. if (domain == NULL)
  769. return 0;
  770. if (hwirq < domain->revmap_direct_max_irq) {
  771. data = irq_domain_get_irq_data(domain, hwirq);
  772. if (data && data->hwirq == hwirq)
  773. return hwirq;
  774. }
  775. /* Check if the hwirq is in the linear revmap. */
  776. if (hwirq < domain->revmap_size)
  777. return domain->linear_revmap[hwirq];
  778. rcu_read_lock();
  779. data = radix_tree_lookup(&domain->revmap_tree, hwirq);
  780. rcu_read_unlock();
  781. return data ? data->irq : 0;
  782. }
  783. EXPORT_SYMBOL_GPL(irq_find_mapping);
  784. /**
  785. * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
  786. *
  787. * Device Tree IRQ specifier translation function which works with one cell
  788. * bindings where the cell value maps directly to the hwirq number.
  789. */
  790. int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
  791. const u32 *intspec, unsigned int intsize,
  792. unsigned long *out_hwirq, unsigned int *out_type)
  793. {
  794. if (WARN_ON(intsize < 1))
  795. return -EINVAL;
  796. *out_hwirq = intspec[0];
  797. *out_type = IRQ_TYPE_NONE;
  798. return 0;
  799. }
  800. EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
  801. /**
  802. * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
  803. *
  804. * Device Tree IRQ specifier translation function which works with two cell
  805. * bindings where the cell values map directly to the hwirq number
  806. * and linux irq flags.
  807. */
  808. int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
  809. const u32 *intspec, unsigned int intsize,
  810. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  811. {
  812. struct irq_fwspec fwspec;
  813. of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
  814. return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
  815. }
  816. EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
  817. /**
  818. * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
  819. *
  820. * Device Tree IRQ specifier translation function which works with either one
  821. * or two cell bindings where the cell values map directly to the hwirq number
  822. * and linux irq flags.
  823. *
  824. * Note: don't use this function unless your interrupt controller explicitly
  825. * supports both one and two cell bindings. For the majority of controllers
  826. * the _onecell() or _twocell() variants above should be used.
  827. */
  828. int irq_domain_xlate_onetwocell(struct irq_domain *d,
  829. struct device_node *ctrlr,
  830. const u32 *intspec, unsigned int intsize,
  831. unsigned long *out_hwirq, unsigned int *out_type)
  832. {
  833. if (WARN_ON(intsize < 1))
  834. return -EINVAL;
  835. *out_hwirq = intspec[0];
  836. if (intsize > 1)
  837. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  838. else
  839. *out_type = IRQ_TYPE_NONE;
  840. return 0;
  841. }
  842. EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
  843. const struct irq_domain_ops irq_domain_simple_ops = {
  844. .xlate = irq_domain_xlate_onetwocell,
  845. };
  846. EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
  847. /**
  848. * irq_domain_translate_onecell() - Generic translate for direct one cell
  849. * bindings
  850. */
  851. int irq_domain_translate_onecell(struct irq_domain *d,
  852. struct irq_fwspec *fwspec,
  853. unsigned long *out_hwirq,
  854. unsigned int *out_type)
  855. {
  856. if (WARN_ON(fwspec->param_count < 1))
  857. return -EINVAL;
  858. *out_hwirq = fwspec->param[0];
  859. *out_type = IRQ_TYPE_NONE;
  860. return 0;
  861. }
  862. EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
  863. /**
  864. * irq_domain_translate_twocell() - Generic translate for direct two cell
  865. * bindings
  866. *
  867. * Device Tree IRQ specifier translation function which works with two cell
  868. * bindings where the cell values map directly to the hwirq number
  869. * and linux irq flags.
  870. */
  871. int irq_domain_translate_twocell(struct irq_domain *d,
  872. struct irq_fwspec *fwspec,
  873. unsigned long *out_hwirq,
  874. unsigned int *out_type)
  875. {
  876. if (WARN_ON(fwspec->param_count < 2))
  877. return -EINVAL;
  878. *out_hwirq = fwspec->param[0];
  879. *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  880. return 0;
  881. }
  882. EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
  883. int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
  884. int node, const struct irq_affinity_desc *affinity)
  885. {
  886. unsigned int hint;
  887. if (virq >= 0) {
  888. virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
  889. affinity);
  890. } else {
  891. hint = hwirq % nr_irqs;
  892. if (hint == 0)
  893. hint++;
  894. virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
  895. affinity);
  896. if (virq <= 0 && hint > 1) {
  897. virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
  898. affinity);
  899. }
  900. }
  901. return virq;
  902. }
  903. /**
  904. * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
  905. * @irq_data: The pointer to irq_data
  906. */
  907. void irq_domain_reset_irq_data(struct irq_data *irq_data)
  908. {
  909. irq_data->hwirq = 0;
  910. irq_data->chip = &no_irq_chip;
  911. irq_data->chip_data = NULL;
  912. }
  913. EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
  914. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  915. /**
  916. * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
  917. * @parent: Parent irq domain to associate with the new domain
  918. * @flags: Irq domain flags associated to the domain
  919. * @size: Size of the domain. See below
  920. * @fwnode: Optional fwnode of the interrupt controller
  921. * @ops: Pointer to the interrupt domain callbacks
  922. * @host_data: Controller private data pointer
  923. *
  924. * If @size is 0 a tree domain is created, otherwise a linear domain.
  925. *
  926. * If successful the parent is associated to the new domain and the
  927. * domain flags are set.
  928. * Returns pointer to IRQ domain, or NULL on failure.
  929. */
  930. struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
  931. unsigned int flags,
  932. unsigned int size,
  933. struct fwnode_handle *fwnode,
  934. const struct irq_domain_ops *ops,
  935. void *host_data)
  936. {
  937. struct irq_domain *domain;
  938. if (size)
  939. domain = irq_domain_create_linear(fwnode, size, ops, host_data);
  940. else
  941. domain = irq_domain_create_tree(fwnode, ops, host_data);
  942. if (domain) {
  943. domain->parent = parent;
  944. domain->flags |= flags;
  945. }
  946. return domain;
  947. }
  948. EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
  949. static void irq_domain_insert_irq(int virq)
  950. {
  951. struct irq_data *data;
  952. for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
  953. struct irq_domain *domain = data->domain;
  954. domain->mapcount++;
  955. irq_domain_set_mapping(domain, data->hwirq, data);
  956. /* If not already assigned, give the domain the chip's name */
  957. if (!domain->name && data->chip)
  958. domain->name = data->chip->name;
  959. }
  960. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  961. }
  962. static void irq_domain_remove_irq(int virq)
  963. {
  964. struct irq_data *data;
  965. irq_set_status_flags(virq, IRQ_NOREQUEST);
  966. irq_set_chip_and_handler(virq, NULL, NULL);
  967. synchronize_irq(virq);
  968. smp_mb();
  969. for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
  970. struct irq_domain *domain = data->domain;
  971. irq_hw_number_t hwirq = data->hwirq;
  972. domain->mapcount--;
  973. irq_domain_clear_mapping(domain, hwirq);
  974. }
  975. }
  976. static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
  977. struct irq_data *child)
  978. {
  979. struct irq_data *irq_data;
  980. irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
  981. irq_data_get_node(child));
  982. if (irq_data) {
  983. child->parent_data = irq_data;
  984. irq_data->irq = child->irq;
  985. irq_data->common = child->common;
  986. irq_data->domain = domain;
  987. }
  988. return irq_data;
  989. }
  990. static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
  991. {
  992. struct irq_data *tmp;
  993. while (irq_data) {
  994. tmp = irq_data;
  995. irq_data = irq_data->parent_data;
  996. kfree(tmp);
  997. }
  998. }
  999. static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
  1000. {
  1001. struct irq_data *irq_data, *tmp;
  1002. int i;
  1003. for (i = 0; i < nr_irqs; i++) {
  1004. irq_data = irq_get_irq_data(virq + i);
  1005. tmp = irq_data->parent_data;
  1006. irq_data->parent_data = NULL;
  1007. irq_data->domain = NULL;
  1008. __irq_domain_free_hierarchy(tmp);
  1009. }
  1010. }
  1011. /**
  1012. * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
  1013. * @domain: IRQ domain from which the hierarchy is to be disconnected
  1014. * @virq: IRQ number where the hierarchy is to be trimmed
  1015. *
  1016. * Marks the @virq level belonging to @domain as disconnected.
  1017. * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
  1018. * to @domain.
  1019. *
  1020. * Its only use is to be able to trim levels of hierarchy that do not
  1021. * have any real meaning for this interrupt, and that the driver marks
  1022. * as such from its .alloc() callback.
  1023. */
  1024. int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
  1025. unsigned int virq)
  1026. {
  1027. struct irq_data *irqd;
  1028. irqd = irq_domain_get_irq_data(domain, virq);
  1029. if (!irqd)
  1030. return -EINVAL;
  1031. irqd->chip = ERR_PTR(-ENOTCONN);
  1032. return 0;
  1033. }
  1034. EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
  1035. static int irq_domain_trim_hierarchy(unsigned int virq)
  1036. {
  1037. struct irq_data *tail, *irqd, *irq_data;
  1038. irq_data = irq_get_irq_data(virq);
  1039. tail = NULL;
  1040. /* The first entry must have a valid irqchip */
  1041. if (!irq_data->chip || IS_ERR(irq_data->chip))
  1042. return -EINVAL;
  1043. /*
  1044. * Validate that the irq_data chain is sane in the presence of
  1045. * a hierarchy trimming marker.
  1046. */
  1047. for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
  1048. /* Can't have a valid irqchip after a trim marker */
  1049. if (irqd->chip && tail)
  1050. return -EINVAL;
  1051. /* Can't have an empty irqchip before a trim marker */
  1052. if (!irqd->chip && !tail)
  1053. return -EINVAL;
  1054. if (IS_ERR(irqd->chip)) {
  1055. /* Only -ENOTCONN is a valid trim marker */
  1056. if (PTR_ERR(irqd->chip) != -ENOTCONN)
  1057. return -EINVAL;
  1058. tail = irq_data;
  1059. }
  1060. }
  1061. /* No trim marker, nothing to do */
  1062. if (!tail)
  1063. return 0;
  1064. pr_info("IRQ%d: trimming hierarchy from %s\n",
  1065. virq, tail->parent_data->domain->name);
  1066. /* Sever the inner part of the hierarchy... */
  1067. irqd = tail;
  1068. tail = tail->parent_data;
  1069. irqd->parent_data = NULL;
  1070. __irq_domain_free_hierarchy(tail);
  1071. return 0;
  1072. }
  1073. static int irq_domain_alloc_irq_data(struct irq_domain *domain,
  1074. unsigned int virq, unsigned int nr_irqs)
  1075. {
  1076. struct irq_data *irq_data;
  1077. struct irq_domain *parent;
  1078. int i;
  1079. /* The outermost irq_data is embedded in struct irq_desc */
  1080. for (i = 0; i < nr_irqs; i++) {
  1081. irq_data = irq_get_irq_data(virq + i);
  1082. irq_data->domain = domain;
  1083. for (parent = domain->parent; parent; parent = parent->parent) {
  1084. irq_data = irq_domain_insert_irq_data(parent, irq_data);
  1085. if (!irq_data) {
  1086. irq_domain_free_irq_data(virq, i + 1);
  1087. return -ENOMEM;
  1088. }
  1089. }
  1090. }
  1091. return 0;
  1092. }
  1093. /**
  1094. * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
  1095. * @domain: domain to match
  1096. * @virq: IRQ number to get irq_data
  1097. */
  1098. struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
  1099. unsigned int virq)
  1100. {
  1101. struct irq_data *irq_data;
  1102. for (irq_data = irq_get_irq_data(virq); irq_data;
  1103. irq_data = irq_data->parent_data)
  1104. if (irq_data->domain == domain)
  1105. return irq_data;
  1106. return NULL;
  1107. }
  1108. EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
  1109. /**
  1110. * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
  1111. * @domain: Interrupt domain to match
  1112. * @virq: IRQ number
  1113. * @hwirq: The hwirq number
  1114. * @chip: The associated interrupt chip
  1115. * @chip_data: The associated chip data
  1116. */
  1117. int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
  1118. irq_hw_number_t hwirq, struct irq_chip *chip,
  1119. void *chip_data)
  1120. {
  1121. struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
  1122. if (!irq_data)
  1123. return -ENOENT;
  1124. irq_data->hwirq = hwirq;
  1125. irq_data->chip = chip ? chip : &no_irq_chip;
  1126. irq_data->chip_data = chip_data;
  1127. return 0;
  1128. }
  1129. EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
  1130. /**
  1131. * irq_domain_set_info - Set the complete data for a @virq in @domain
  1132. * @domain: Interrupt domain to match
  1133. * @virq: IRQ number
  1134. * @hwirq: The hardware interrupt number
  1135. * @chip: The associated interrupt chip
  1136. * @chip_data: The associated interrupt chip data
  1137. * @handler: The interrupt flow handler
  1138. * @handler_data: The interrupt flow handler data
  1139. * @handler_name: The interrupt handler name
  1140. */
  1141. void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
  1142. irq_hw_number_t hwirq, struct irq_chip *chip,
  1143. void *chip_data, irq_flow_handler_t handler,
  1144. void *handler_data, const char *handler_name)
  1145. {
  1146. irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
  1147. __irq_set_handler(virq, handler, 0, handler_name);
  1148. irq_set_handler_data(virq, handler_data);
  1149. }
  1150. EXPORT_SYMBOL(irq_domain_set_info);
  1151. /**
  1152. * irq_domain_free_irqs_common - Clear irq_data and free the parent
  1153. * @domain: Interrupt domain to match
  1154. * @virq: IRQ number to start with
  1155. * @nr_irqs: The number of irqs to free
  1156. */
  1157. void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
  1158. unsigned int nr_irqs)
  1159. {
  1160. struct irq_data *irq_data;
  1161. int i;
  1162. for (i = 0; i < nr_irqs; i++) {
  1163. irq_data = irq_domain_get_irq_data(domain, virq + i);
  1164. if (irq_data)
  1165. irq_domain_reset_irq_data(irq_data);
  1166. }
  1167. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  1168. }
  1169. EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
  1170. /**
  1171. * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
  1172. * @domain: Interrupt domain to match
  1173. * @virq: IRQ number to start with
  1174. * @nr_irqs: The number of irqs to free
  1175. */
  1176. void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
  1177. unsigned int nr_irqs)
  1178. {
  1179. int i;
  1180. for (i = 0; i < nr_irqs; i++) {
  1181. irq_set_handler_data(virq + i, NULL);
  1182. irq_set_handler(virq + i, NULL);
  1183. }
  1184. irq_domain_free_irqs_common(domain, virq, nr_irqs);
  1185. }
  1186. static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
  1187. unsigned int irq_base,
  1188. unsigned int nr_irqs)
  1189. {
  1190. unsigned int i;
  1191. if (!domain->ops->free)
  1192. return;
  1193. for (i = 0; i < nr_irqs; i++) {
  1194. if (irq_domain_get_irq_data(domain, irq_base + i))
  1195. domain->ops->free(domain, irq_base + i, 1);
  1196. }
  1197. }
  1198. int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
  1199. unsigned int irq_base,
  1200. unsigned int nr_irqs, void *arg)
  1201. {
  1202. if (!domain->ops->alloc) {
  1203. pr_debug("domain->ops->alloc() is NULL\n");
  1204. return -ENOSYS;
  1205. }
  1206. return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
  1207. }
  1208. /**
  1209. * __irq_domain_alloc_irqs - Allocate IRQs from domain
  1210. * @domain: domain to allocate from
  1211. * @irq_base: allocate specified IRQ number if irq_base >= 0
  1212. * @nr_irqs: number of IRQs to allocate
  1213. * @node: NUMA node id for memory allocation
  1214. * @arg: domain specific argument
  1215. * @realloc: IRQ descriptors have already been allocated if true
  1216. * @affinity: Optional irq affinity mask for multiqueue devices
  1217. *
  1218. * Allocate IRQ numbers and initialized all data structures to support
  1219. * hierarchy IRQ domains.
  1220. * Parameter @realloc is mainly to support legacy IRQs.
  1221. * Returns error code or allocated IRQ number
  1222. *
  1223. * The whole process to setup an IRQ has been split into two steps.
  1224. * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
  1225. * descriptor and required hardware resources. The second step,
  1226. * irq_domain_activate_irq(), is to program hardwares with preallocated
  1227. * resources. In this way, it's easier to rollback when failing to
  1228. * allocate resources.
  1229. */
  1230. int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
  1231. unsigned int nr_irqs, int node, void *arg,
  1232. bool realloc, const struct irq_affinity_desc *affinity)
  1233. {
  1234. int i, ret, virq;
  1235. if (domain == NULL) {
  1236. domain = irq_default_domain;
  1237. if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
  1238. return -EINVAL;
  1239. }
  1240. if (realloc && irq_base >= 0) {
  1241. virq = irq_base;
  1242. } else {
  1243. virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
  1244. affinity);
  1245. if (virq < 0) {
  1246. pr_debug("cannot allocate IRQ(base %d, count %d)\n",
  1247. irq_base, nr_irqs);
  1248. return virq;
  1249. }
  1250. }
  1251. if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
  1252. pr_debug("cannot allocate memory for IRQ%d\n", virq);
  1253. ret = -ENOMEM;
  1254. goto out_free_desc;
  1255. }
  1256. mutex_lock(&irq_domain_mutex);
  1257. ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
  1258. if (ret < 0) {
  1259. mutex_unlock(&irq_domain_mutex);
  1260. goto out_free_irq_data;
  1261. }
  1262. for (i = 0; i < nr_irqs; i++) {
  1263. ret = irq_domain_trim_hierarchy(virq + i);
  1264. if (ret) {
  1265. mutex_unlock(&irq_domain_mutex);
  1266. goto out_free_irq_data;
  1267. }
  1268. }
  1269. for (i = 0; i < nr_irqs; i++)
  1270. irq_domain_insert_irq(virq + i);
  1271. mutex_unlock(&irq_domain_mutex);
  1272. return virq;
  1273. out_free_irq_data:
  1274. irq_domain_free_irq_data(virq, nr_irqs);
  1275. out_free_desc:
  1276. irq_free_descs(virq, nr_irqs);
  1277. return ret;
  1278. }
  1279. /* The irq_data was moved, fix the revmap to refer to the new location */
  1280. static void irq_domain_fix_revmap(struct irq_data *d)
  1281. {
  1282. void __rcu **slot;
  1283. if (d->hwirq < d->domain->revmap_size)
  1284. return; /* Not using radix tree. */
  1285. /* Fix up the revmap. */
  1286. mutex_lock(&d->domain->revmap_tree_mutex);
  1287. slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
  1288. if (slot)
  1289. radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
  1290. mutex_unlock(&d->domain->revmap_tree_mutex);
  1291. }
  1292. /**
  1293. * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
  1294. * @domain: Domain to push.
  1295. * @virq: Irq to push the domain in to.
  1296. * @arg: Passed to the irq_domain_ops alloc() function.
  1297. *
  1298. * For an already existing irqdomain hierarchy, as might be obtained
  1299. * via a call to pci_enable_msix(), add an additional domain to the
  1300. * head of the processing chain. Must be called before request_irq()
  1301. * has been called.
  1302. */
  1303. int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
  1304. {
  1305. struct irq_data *child_irq_data;
  1306. struct irq_data *root_irq_data = irq_get_irq_data(virq);
  1307. struct irq_desc *desc;
  1308. int rv = 0;
  1309. /*
  1310. * Check that no action has been set, which indicates the virq
  1311. * is in a state where this function doesn't have to deal with
  1312. * races between interrupt handling and maintaining the
  1313. * hierarchy. This will catch gross misuse. Attempting to
  1314. * make the check race free would require holding locks across
  1315. * calls to struct irq_domain_ops->alloc(), which could lead
  1316. * to deadlock, so we just do a simple check before starting.
  1317. */
  1318. desc = irq_to_desc(virq);
  1319. if (!desc)
  1320. return -EINVAL;
  1321. if (WARN_ON(desc->action))
  1322. return -EBUSY;
  1323. if (domain == NULL)
  1324. return -EINVAL;
  1325. if (WARN_ON(!irq_domain_is_hierarchy(domain)))
  1326. return -EINVAL;
  1327. if (!root_irq_data)
  1328. return -EINVAL;
  1329. if (domain->parent != root_irq_data->domain)
  1330. return -EINVAL;
  1331. child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
  1332. irq_data_get_node(root_irq_data));
  1333. if (!child_irq_data)
  1334. return -ENOMEM;
  1335. mutex_lock(&irq_domain_mutex);
  1336. /* Copy the original irq_data. */
  1337. *child_irq_data = *root_irq_data;
  1338. /*
  1339. * Overwrite the root_irq_data, which is embedded in struct
  1340. * irq_desc, with values for this domain.
  1341. */
  1342. root_irq_data->parent_data = child_irq_data;
  1343. root_irq_data->domain = domain;
  1344. root_irq_data->mask = 0;
  1345. root_irq_data->hwirq = 0;
  1346. root_irq_data->chip = NULL;
  1347. root_irq_data->chip_data = NULL;
  1348. /* May (probably does) set hwirq, chip, etc. */
  1349. rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
  1350. if (rv) {
  1351. /* Restore the original irq_data. */
  1352. *root_irq_data = *child_irq_data;
  1353. kfree(child_irq_data);
  1354. goto error;
  1355. }
  1356. irq_domain_fix_revmap(child_irq_data);
  1357. irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
  1358. error:
  1359. mutex_unlock(&irq_domain_mutex);
  1360. return rv;
  1361. }
  1362. EXPORT_SYMBOL_GPL(irq_domain_push_irq);
  1363. /**
  1364. * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
  1365. * @domain: Domain to remove.
  1366. * @virq: Irq to remove the domain from.
  1367. *
  1368. * Undo the effects of a call to irq_domain_push_irq(). Must be
  1369. * called either before request_irq() or after free_irq().
  1370. */
  1371. int irq_domain_pop_irq(struct irq_domain *domain, int virq)
  1372. {
  1373. struct irq_data *root_irq_data = irq_get_irq_data(virq);
  1374. struct irq_data *child_irq_data;
  1375. struct irq_data *tmp_irq_data;
  1376. struct irq_desc *desc;
  1377. /*
  1378. * Check that no action is set, which indicates the virq is in
  1379. * a state where this function doesn't have to deal with races
  1380. * between interrupt handling and maintaining the hierarchy.
  1381. * This will catch gross misuse. Attempting to make the check
  1382. * race free would require holding locks across calls to
  1383. * struct irq_domain_ops->free(), which could lead to
  1384. * deadlock, so we just do a simple check before starting.
  1385. */
  1386. desc = irq_to_desc(virq);
  1387. if (!desc)
  1388. return -EINVAL;
  1389. if (WARN_ON(desc->action))
  1390. return -EBUSY;
  1391. if (domain == NULL)
  1392. return -EINVAL;
  1393. if (!root_irq_data)
  1394. return -EINVAL;
  1395. tmp_irq_data = irq_domain_get_irq_data(domain, virq);
  1396. /* We can only "pop" if this domain is at the top of the list */
  1397. if (WARN_ON(root_irq_data != tmp_irq_data))
  1398. return -EINVAL;
  1399. if (WARN_ON(root_irq_data->domain != domain))
  1400. return -EINVAL;
  1401. child_irq_data = root_irq_data->parent_data;
  1402. if (WARN_ON(!child_irq_data))
  1403. return -EINVAL;
  1404. mutex_lock(&irq_domain_mutex);
  1405. root_irq_data->parent_data = NULL;
  1406. irq_domain_clear_mapping(domain, root_irq_data->hwirq);
  1407. irq_domain_free_irqs_hierarchy(domain, virq, 1);
  1408. /* Restore the original irq_data. */
  1409. *root_irq_data = *child_irq_data;
  1410. irq_domain_fix_revmap(root_irq_data);
  1411. mutex_unlock(&irq_domain_mutex);
  1412. kfree(child_irq_data);
  1413. return 0;
  1414. }
  1415. EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
  1416. /**
  1417. * irq_domain_free_irqs - Free IRQ number and associated data structures
  1418. * @virq: base IRQ number
  1419. * @nr_irqs: number of IRQs to free
  1420. */
  1421. void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
  1422. {
  1423. struct irq_data *data = irq_get_irq_data(virq);
  1424. int i;
  1425. if (WARN(!data || !data->domain || !data->domain->ops->free,
  1426. "NULL pointer, cannot free irq\n"))
  1427. return;
  1428. mutex_lock(&irq_domain_mutex);
  1429. for (i = 0; i < nr_irqs; i++)
  1430. irq_domain_remove_irq(virq + i);
  1431. irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
  1432. mutex_unlock(&irq_domain_mutex);
  1433. irq_domain_free_irq_data(virq, nr_irqs);
  1434. irq_free_descs(virq, nr_irqs);
  1435. }
  1436. /**
  1437. * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
  1438. * @irq_base: Base IRQ number
  1439. * @nr_irqs: Number of IRQs to allocate
  1440. * @arg: Allocation data (arch/domain specific)
  1441. *
  1442. * Check whether the domain has been setup recursive. If not allocate
  1443. * through the parent domain.
  1444. */
  1445. int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
  1446. unsigned int irq_base, unsigned int nr_irqs,
  1447. void *arg)
  1448. {
  1449. if (!domain->parent)
  1450. return -ENOSYS;
  1451. return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
  1452. nr_irqs, arg);
  1453. }
  1454. EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
  1455. /**
  1456. * irq_domain_free_irqs_parent - Free interrupts from parent domain
  1457. * @irq_base: Base IRQ number
  1458. * @nr_irqs: Number of IRQs to free
  1459. *
  1460. * Check whether the domain has been setup recursive. If not free
  1461. * through the parent domain.
  1462. */
  1463. void irq_domain_free_irqs_parent(struct irq_domain *domain,
  1464. unsigned int irq_base, unsigned int nr_irqs)
  1465. {
  1466. if (!domain->parent)
  1467. return;
  1468. irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
  1469. }
  1470. EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
  1471. static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
  1472. {
  1473. if (irq_data && irq_data->domain) {
  1474. struct irq_domain *domain = irq_data->domain;
  1475. if (domain->ops->deactivate)
  1476. domain->ops->deactivate(domain, irq_data);
  1477. if (irq_data->parent_data)
  1478. __irq_domain_deactivate_irq(irq_data->parent_data);
  1479. }
  1480. }
  1481. static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
  1482. {
  1483. int ret = 0;
  1484. if (irqd && irqd->domain) {
  1485. struct irq_domain *domain = irqd->domain;
  1486. if (irqd->parent_data)
  1487. ret = __irq_domain_activate_irq(irqd->parent_data,
  1488. reserve);
  1489. if (!ret && domain->ops->activate) {
  1490. ret = domain->ops->activate(domain, irqd, reserve);
  1491. /* Rollback in case of error */
  1492. if (ret && irqd->parent_data)
  1493. __irq_domain_deactivate_irq(irqd->parent_data);
  1494. }
  1495. }
  1496. return ret;
  1497. }
  1498. /**
  1499. * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
  1500. * interrupt
  1501. * @irq_data: Outermost irq_data associated with interrupt
  1502. * @reserve: If set only reserve an interrupt vector instead of assigning one
  1503. *
  1504. * This is the second step to call domain_ops->activate to program interrupt
  1505. * controllers, so the interrupt could actually get delivered.
  1506. */
  1507. int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
  1508. {
  1509. int ret = 0;
  1510. if (!irqd_is_activated(irq_data))
  1511. ret = __irq_domain_activate_irq(irq_data, reserve);
  1512. if (!ret)
  1513. irqd_set_activated(irq_data);
  1514. return ret;
  1515. }
  1516. /**
  1517. * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
  1518. * deactivate interrupt
  1519. * @irq_data: outermost irq_data associated with interrupt
  1520. *
  1521. * It calls domain_ops->deactivate to program interrupt controllers to disable
  1522. * interrupt delivery.
  1523. */
  1524. void irq_domain_deactivate_irq(struct irq_data *irq_data)
  1525. {
  1526. if (irqd_is_activated(irq_data)) {
  1527. __irq_domain_deactivate_irq(irq_data);
  1528. irqd_clr_activated(irq_data);
  1529. }
  1530. }
  1531. static void irq_domain_check_hierarchy(struct irq_domain *domain)
  1532. {
  1533. /* Hierarchy irq_domains must implement callback alloc() */
  1534. if (domain->ops->alloc)
  1535. domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
  1536. }
  1537. /**
  1538. * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
  1539. * parent has MSI remapping support
  1540. * @domain: domain pointer
  1541. */
  1542. bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
  1543. {
  1544. for (; domain; domain = domain->parent) {
  1545. if (irq_domain_is_msi_remap(domain))
  1546. return true;
  1547. }
  1548. return false;
  1549. }
  1550. #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  1551. /**
  1552. * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
  1553. * @domain: domain to match
  1554. * @virq: IRQ number to get irq_data
  1555. */
  1556. struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
  1557. unsigned int virq)
  1558. {
  1559. struct irq_data *irq_data = irq_get_irq_data(virq);
  1560. return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
  1561. }
  1562. EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
  1563. /**
  1564. * irq_domain_set_info - Set the complete data for a @virq in @domain
  1565. * @domain: Interrupt domain to match
  1566. * @virq: IRQ number
  1567. * @hwirq: The hardware interrupt number
  1568. * @chip: The associated interrupt chip
  1569. * @chip_data: The associated interrupt chip data
  1570. * @handler: The interrupt flow handler
  1571. * @handler_data: The interrupt flow handler data
  1572. * @handler_name: The interrupt handler name
  1573. */
  1574. void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
  1575. irq_hw_number_t hwirq, struct irq_chip *chip,
  1576. void *chip_data, irq_flow_handler_t handler,
  1577. void *handler_data, const char *handler_name)
  1578. {
  1579. irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
  1580. irq_set_chip_data(virq, chip_data);
  1581. irq_set_handler_data(virq, handler_data);
  1582. }
  1583. static void irq_domain_check_hierarchy(struct irq_domain *domain)
  1584. {
  1585. }
  1586. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  1587. #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
  1588. static struct dentry *domain_dir;
  1589. static void
  1590. irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
  1591. {
  1592. seq_printf(m, "%*sname: %s\n", ind, "", d->name);
  1593. seq_printf(m, "%*ssize: %u\n", ind + 1, "",
  1594. d->revmap_size + d->revmap_direct_max_irq);
  1595. seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
  1596. seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
  1597. if (d->ops && d->ops->debug_show)
  1598. d->ops->debug_show(m, d, NULL, ind + 1);
  1599. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  1600. if (!d->parent)
  1601. return;
  1602. seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
  1603. irq_domain_debug_show_one(m, d->parent, ind + 4);
  1604. #endif
  1605. }
  1606. static int irq_domain_debug_show(struct seq_file *m, void *p)
  1607. {
  1608. struct irq_domain *d = m->private;
  1609. /* Default domain? Might be NULL */
  1610. if (!d) {
  1611. if (!irq_default_domain)
  1612. return 0;
  1613. d = irq_default_domain;
  1614. }
  1615. irq_domain_debug_show_one(m, d, 0);
  1616. return 0;
  1617. }
  1618. DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
  1619. static void debugfs_add_domain_dir(struct irq_domain *d)
  1620. {
  1621. if (!d->name || !domain_dir || d->debugfs_file)
  1622. return;
  1623. d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
  1624. &irq_domain_debug_fops);
  1625. }
  1626. static void debugfs_remove_domain_dir(struct irq_domain *d)
  1627. {
  1628. debugfs_remove(d->debugfs_file);
  1629. d->debugfs_file = NULL;
  1630. }
  1631. void __init irq_domain_debugfs_init(struct dentry *root)
  1632. {
  1633. struct irq_domain *d;
  1634. domain_dir = debugfs_create_dir("domains", root);
  1635. debugfs_create_file("default", 0444, domain_dir, NULL,
  1636. &irq_domain_debug_fops);
  1637. mutex_lock(&irq_domain_mutex);
  1638. list_for_each_entry(d, &irq_domain_list, link)
  1639. debugfs_add_domain_dir(d);
  1640. mutex_unlock(&irq_domain_mutex);
  1641. }
  1642. #endif