irqdomain.c 38 KB

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