irqdomain.c 51 KB

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