irqdomain.c 54 KB

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