irqdomain.c 59 KB

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