irqdomain.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. #include <linux/debugfs.h>
  2. #include <linux/hardirq.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/irqdesc.h>
  6. #include <linux/irqdomain.h>
  7. #include <linux/module.h>
  8. #include <linux/mutex.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/smp.h>
  14. #include <linux/fs.h>
  15. #define IRQ_DOMAIN_MAP_LEGACY 0 /* driver allocated fixed range of irqs.
  16. * ie. legacy 8259, gets irqs 1..15 */
  17. #define IRQ_DOMAIN_MAP_NOMAP 1 /* no fast reverse mapping */
  18. #define IRQ_DOMAIN_MAP_LINEAR 2 /* linear map of interrupts */
  19. #define IRQ_DOMAIN_MAP_TREE 3 /* radix tree */
  20. static LIST_HEAD(irq_domain_list);
  21. static DEFINE_MUTEX(irq_domain_mutex);
  22. static DEFINE_MUTEX(revmap_trees_mutex);
  23. static unsigned int irq_virq_count = NR_IRQS;
  24. static struct irq_domain *irq_default_domain;
  25. /**
  26. * irq_domain_alloc() - Allocate a new irq_domain data structure
  27. * @of_node: optional device-tree node of the interrupt controller
  28. * @revmap_type: type of reverse mapping to use
  29. * @ops: map/unmap domain callbacks
  30. * @host_data: Controller private data pointer
  31. *
  32. * Allocates and initialize and irq_domain structure. Caller is expected to
  33. * register allocated irq_domain with irq_domain_register(). Returns pointer
  34. * to IRQ domain, or NULL on failure.
  35. */
  36. static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
  37. unsigned int revmap_type,
  38. const struct irq_domain_ops *ops,
  39. void *host_data)
  40. {
  41. struct irq_domain *domain;
  42. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  43. if (WARN_ON(!domain))
  44. return NULL;
  45. /* Fill structure */
  46. domain->revmap_type = revmap_type;
  47. domain->ops = ops;
  48. domain->host_data = host_data;
  49. domain->of_node = of_node_get(of_node);
  50. return domain;
  51. }
  52. static void irq_domain_add(struct irq_domain *domain)
  53. {
  54. mutex_lock(&irq_domain_mutex);
  55. list_add(&domain->link, &irq_domain_list);
  56. mutex_unlock(&irq_domain_mutex);
  57. pr_debug("irq: Allocated domain of type %d @0x%p\n",
  58. domain->revmap_type, domain);
  59. }
  60. static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
  61. irq_hw_number_t hwirq)
  62. {
  63. irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
  64. int size = domain->revmap_data.legacy.size;
  65. if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
  66. return 0;
  67. return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
  68. }
  69. /**
  70. * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
  71. * @of_node: pointer to interrupt controller's device tree node.
  72. * @size: total number of irqs in legacy mapping
  73. * @first_irq: first number of irq block assigned to the domain
  74. * @first_hwirq: first hwirq number to use for the translation. Should normally
  75. * be '0', but a positive integer can be used if the effective
  76. * hwirqs numbering does not begin at zero.
  77. * @ops: map/unmap domain callbacks
  78. * @host_data: Controller private data pointer
  79. *
  80. * Note: the map() callback will be called before this function returns
  81. * for all legacy interrupts except 0 (which is always the invalid irq for
  82. * a legacy controller).
  83. */
  84. struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
  85. unsigned int size,
  86. unsigned int first_irq,
  87. irq_hw_number_t first_hwirq,
  88. const struct irq_domain_ops *ops,
  89. void *host_data)
  90. {
  91. struct irq_domain *domain;
  92. unsigned int i;
  93. domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
  94. if (!domain)
  95. return NULL;
  96. domain->revmap_data.legacy.first_irq = first_irq;
  97. domain->revmap_data.legacy.first_hwirq = first_hwirq;
  98. domain->revmap_data.legacy.size = size;
  99. mutex_lock(&irq_domain_mutex);
  100. /* Verify that all the irqs are available */
  101. for (i = 0; i < size; i++) {
  102. int irq = first_irq + i;
  103. struct irq_data *irq_data = irq_get_irq_data(irq);
  104. if (WARN_ON(!irq_data || irq_data->domain)) {
  105. mutex_unlock(&irq_domain_mutex);
  106. of_node_put(domain->of_node);
  107. kfree(domain);
  108. return NULL;
  109. }
  110. }
  111. /* Claim all of the irqs before registering a legacy domain */
  112. for (i = 0; i < size; i++) {
  113. struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
  114. irq_data->hwirq = first_hwirq + i;
  115. irq_data->domain = domain;
  116. }
  117. mutex_unlock(&irq_domain_mutex);
  118. for (i = 0; i < size; i++) {
  119. int irq = first_irq + i;
  120. int hwirq = first_hwirq + i;
  121. /* IRQ0 gets ignored */
  122. if (!irq)
  123. continue;
  124. /* Legacy flags are left to default at this point,
  125. * one can then use irq_create_mapping() to
  126. * explicitly change them
  127. */
  128. ops->map(domain, irq, hwirq);
  129. /* Clear norequest flags */
  130. irq_clear_status_flags(irq, IRQ_NOREQUEST);
  131. }
  132. irq_domain_add(domain);
  133. return domain;
  134. }
  135. /**
  136. * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
  137. * @of_node: pointer to interrupt controller's device tree node.
  138. * @ops: map/unmap domain callbacks
  139. * @host_data: Controller private data pointer
  140. */
  141. struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
  142. unsigned int size,
  143. const struct irq_domain_ops *ops,
  144. void *host_data)
  145. {
  146. struct irq_domain *domain;
  147. unsigned int *revmap;
  148. revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
  149. if (WARN_ON(!revmap))
  150. return NULL;
  151. domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
  152. if (!domain) {
  153. kfree(revmap);
  154. return NULL;
  155. }
  156. domain->revmap_data.linear.size = size;
  157. domain->revmap_data.linear.revmap = revmap;
  158. irq_domain_add(domain);
  159. return domain;
  160. }
  161. struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
  162. const struct irq_domain_ops *ops,
  163. void *host_data)
  164. {
  165. struct irq_domain *domain = irq_domain_alloc(of_node,
  166. IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
  167. if (domain)
  168. irq_domain_add(domain);
  169. return domain;
  170. }
  171. /**
  172. * irq_domain_add_tree()
  173. * @of_node: pointer to interrupt controller's device tree node.
  174. * @ops: map/unmap domain callbacks
  175. *
  176. * Note: The radix tree will be allocated later during boot automatically
  177. * (the reverse mapping will use the slow path until that happens).
  178. */
  179. struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
  180. const struct irq_domain_ops *ops,
  181. void *host_data)
  182. {
  183. struct irq_domain *domain = irq_domain_alloc(of_node,
  184. IRQ_DOMAIN_MAP_TREE, ops, host_data);
  185. if (domain) {
  186. INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
  187. irq_domain_add(domain);
  188. }
  189. return domain;
  190. }
  191. /**
  192. * irq_find_host() - Locates a domain for a given device node
  193. * @node: device-tree node of the interrupt controller
  194. */
  195. struct irq_domain *irq_find_host(struct device_node *node)
  196. {
  197. struct irq_domain *h, *found = NULL;
  198. int rc;
  199. /* We might want to match the legacy controller last since
  200. * it might potentially be set to match all interrupts in
  201. * the absence of a device node. This isn't a problem so far
  202. * yet though...
  203. */
  204. mutex_lock(&irq_domain_mutex);
  205. list_for_each_entry(h, &irq_domain_list, link) {
  206. if (h->ops->match)
  207. rc = h->ops->match(h, node);
  208. else
  209. rc = (h->of_node != NULL) && (h->of_node == node);
  210. if (rc) {
  211. found = h;
  212. break;
  213. }
  214. }
  215. mutex_unlock(&irq_domain_mutex);
  216. return found;
  217. }
  218. EXPORT_SYMBOL_GPL(irq_find_host);
  219. /**
  220. * irq_set_default_host() - Set a "default" irq domain
  221. * @domain: default domain pointer
  222. *
  223. * For convenience, it's possible to set a "default" domain that will be used
  224. * whenever NULL is passed to irq_create_mapping(). It makes life easier for
  225. * platforms that want to manipulate a few hard coded interrupt numbers that
  226. * aren't properly represented in the device-tree.
  227. */
  228. void irq_set_default_host(struct irq_domain *domain)
  229. {
  230. pr_debug("irq: Default domain set to @0x%p\n", domain);
  231. irq_default_domain = domain;
  232. }
  233. /**
  234. * irq_set_virq_count() - Set the maximum number of linux irqs
  235. * @count: number of linux irqs, capped with NR_IRQS
  236. *
  237. * This is mainly for use by platforms like iSeries who want to program
  238. * the virtual irq number in the controller to avoid the reverse mapping
  239. */
  240. void irq_set_virq_count(unsigned int count)
  241. {
  242. pr_debug("irq: Trying to set virq count to %d\n", count);
  243. BUG_ON(count < NUM_ISA_INTERRUPTS);
  244. if (count < NR_IRQS)
  245. irq_virq_count = count;
  246. }
  247. static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
  248. irq_hw_number_t hwirq)
  249. {
  250. struct irq_data *irq_data = irq_get_irq_data(virq);
  251. irq_data->hwirq = hwirq;
  252. irq_data->domain = domain;
  253. if (domain->ops->map(domain, virq, hwirq)) {
  254. pr_debug("irq: -> mapping failed, freeing\n");
  255. irq_data->domain = NULL;
  256. irq_data->hwirq = 0;
  257. return -1;
  258. }
  259. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  260. return 0;
  261. }
  262. /**
  263. * irq_create_direct_mapping() - Allocate an irq for direct mapping
  264. * @domain: domain to allocate the irq for or NULL for default domain
  265. *
  266. * This routine is used for irq controllers which can choose the hardware
  267. * interrupt numbers they generate. In such a case it's simplest to use
  268. * the linux irq as the hardware interrupt number.
  269. */
  270. unsigned int irq_create_direct_mapping(struct irq_domain *domain)
  271. {
  272. unsigned int virq;
  273. if (domain == NULL)
  274. domain = irq_default_domain;
  275. BUG_ON(domain == NULL);
  276. WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
  277. virq = irq_alloc_desc_from(1, 0);
  278. if (!virq) {
  279. pr_debug("irq: create_direct virq allocation failed\n");
  280. return 0;
  281. }
  282. if (virq >= irq_virq_count) {
  283. pr_err("ERROR: no free irqs available below %i maximum\n",
  284. irq_virq_count);
  285. irq_free_desc(virq);
  286. return 0;
  287. }
  288. pr_debug("irq: create_direct obtained virq %d\n", virq);
  289. if (irq_setup_virq(domain, virq, virq)) {
  290. irq_free_desc(virq);
  291. return 0;
  292. }
  293. return virq;
  294. }
  295. /**
  296. * irq_create_mapping() - Map a hardware interrupt into linux irq space
  297. * @domain: domain owning this hardware interrupt or NULL for default domain
  298. * @hwirq: hardware irq number in that domain space
  299. *
  300. * Only one mapping per hardware interrupt is permitted. Returns a linux
  301. * irq number.
  302. * If the sense/trigger is to be specified, set_irq_type() should be called
  303. * on the number returned from that call.
  304. */
  305. unsigned int irq_create_mapping(struct irq_domain *domain,
  306. irq_hw_number_t hwirq)
  307. {
  308. unsigned int virq, hint;
  309. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
  310. /* Look for default domain if nececssary */
  311. if (domain == NULL)
  312. domain = irq_default_domain;
  313. if (domain == NULL) {
  314. printk(KERN_WARNING "irq_create_mapping called for"
  315. " NULL domain, hwirq=%lx\n", hwirq);
  316. WARN_ON(1);
  317. return 0;
  318. }
  319. pr_debug("irq: -> using domain @%p\n", domain);
  320. /* Check if mapping already exists */
  321. virq = irq_find_mapping(domain, hwirq);
  322. if (virq) {
  323. pr_debug("irq: -> existing mapping on virq %d\n", virq);
  324. return virq;
  325. }
  326. /* Get a virtual interrupt number */
  327. if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  328. return irq_domain_legacy_revmap(domain, hwirq);
  329. /* Allocate a virtual interrupt number */
  330. hint = hwirq % irq_virq_count;
  331. if (hint == 0)
  332. hint++;
  333. virq = irq_alloc_desc_from(hint, 0);
  334. if (!virq)
  335. virq = irq_alloc_desc_from(1, 0);
  336. if (!virq) {
  337. pr_debug("irq: -> virq allocation failed\n");
  338. return 0;
  339. }
  340. if (irq_setup_virq(domain, virq, hwirq)) {
  341. if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
  342. irq_free_desc(virq);
  343. return 0;
  344. }
  345. pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
  346. hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
  347. return virq;
  348. }
  349. EXPORT_SYMBOL_GPL(irq_create_mapping);
  350. unsigned int irq_create_of_mapping(struct device_node *controller,
  351. const u32 *intspec, unsigned int intsize)
  352. {
  353. struct irq_domain *domain;
  354. irq_hw_number_t hwirq;
  355. unsigned int type = IRQ_TYPE_NONE;
  356. unsigned int virq;
  357. domain = controller ? irq_find_host(controller) : irq_default_domain;
  358. if (!domain) {
  359. #ifdef CONFIG_MIPS
  360. /*
  361. * Workaround to avoid breaking interrupt controller drivers
  362. * that don't yet register an irq_domain. This is temporary
  363. * code. ~~~gcl, Feb 24, 2012
  364. *
  365. * Scheduled for removal in Linux v3.6. That should be enough
  366. * time.
  367. */
  368. if (intsize > 0)
  369. return intspec[0];
  370. #endif
  371. printk(KERN_WARNING "irq: no irq domain found for %s !\n",
  372. controller->full_name);
  373. return 0;
  374. }
  375. /* If domain has no translation, then we assume interrupt line */
  376. if (domain->ops->xlate == NULL)
  377. hwirq = intspec[0];
  378. else {
  379. if (domain->ops->xlate(domain, controller, intspec, intsize,
  380. &hwirq, &type))
  381. return 0;
  382. }
  383. /* Create mapping */
  384. virq = irq_create_mapping(domain, hwirq);
  385. if (!virq)
  386. return virq;
  387. /* Set type if specified and different than the current one */
  388. if (type != IRQ_TYPE_NONE &&
  389. type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
  390. irq_set_irq_type(virq, type);
  391. return virq;
  392. }
  393. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  394. /**
  395. * irq_dispose_mapping() - Unmap an interrupt
  396. * @virq: linux irq number of the interrupt to unmap
  397. */
  398. void irq_dispose_mapping(unsigned int virq)
  399. {
  400. struct irq_data *irq_data = irq_get_irq_data(virq);
  401. struct irq_domain *domain;
  402. irq_hw_number_t hwirq;
  403. if (!virq || !irq_data)
  404. return;
  405. domain = irq_data->domain;
  406. if (WARN_ON(domain == NULL))
  407. return;
  408. /* Never unmap legacy interrupts */
  409. if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  410. return;
  411. irq_set_status_flags(virq, IRQ_NOREQUEST);
  412. /* remove chip and handler */
  413. irq_set_chip_and_handler(virq, NULL, NULL);
  414. /* Make sure it's completed */
  415. synchronize_irq(virq);
  416. /* Tell the PIC about it */
  417. if (domain->ops->unmap)
  418. domain->ops->unmap(domain, virq);
  419. smp_mb();
  420. /* Clear reverse map */
  421. hwirq = irq_data->hwirq;
  422. switch(domain->revmap_type) {
  423. case IRQ_DOMAIN_MAP_LINEAR:
  424. if (hwirq < domain->revmap_data.linear.size)
  425. domain->revmap_data.linear.revmap[hwirq] = 0;
  426. break;
  427. case IRQ_DOMAIN_MAP_TREE:
  428. mutex_lock(&revmap_trees_mutex);
  429. radix_tree_delete(&domain->revmap_data.tree, hwirq);
  430. mutex_unlock(&revmap_trees_mutex);
  431. break;
  432. }
  433. irq_free_desc(virq);
  434. }
  435. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  436. /**
  437. * irq_find_mapping() - Find a linux irq from an hw irq number.
  438. * @domain: domain owning this hardware interrupt
  439. * @hwirq: hardware irq number in that domain space
  440. *
  441. * This is a slow path, for use by generic code. It's expected that an
  442. * irq controller implementation directly calls the appropriate low level
  443. * mapping function.
  444. */
  445. unsigned int irq_find_mapping(struct irq_domain *domain,
  446. irq_hw_number_t hwirq)
  447. {
  448. unsigned int i;
  449. unsigned int hint = hwirq % irq_virq_count;
  450. /* Look for default domain if nececssary */
  451. if (domain == NULL)
  452. domain = irq_default_domain;
  453. if (domain == NULL)
  454. return 0;
  455. /* legacy -> bail early */
  456. if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  457. return irq_domain_legacy_revmap(domain, hwirq);
  458. /* Slow path does a linear search of the map */
  459. if (hint == 0)
  460. hint = 1;
  461. i = hint;
  462. do {
  463. struct irq_data *data = irq_get_irq_data(i);
  464. if (data && (data->domain == domain) && (data->hwirq == hwirq))
  465. return i;
  466. i++;
  467. if (i >= irq_virq_count)
  468. i = 1;
  469. } while(i != hint);
  470. return 0;
  471. }
  472. EXPORT_SYMBOL_GPL(irq_find_mapping);
  473. /**
  474. * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
  475. * @domain: domain owning this hardware interrupt
  476. * @hwirq: hardware irq number in that domain space
  477. *
  478. * This is a fast path, for use by irq controller code that uses radix tree
  479. * revmaps
  480. */
  481. unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
  482. irq_hw_number_t hwirq)
  483. {
  484. struct irq_data *irq_data;
  485. if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
  486. return irq_find_mapping(domain, hwirq);
  487. /*
  488. * Freeing an irq can delete nodes along the path to
  489. * do the lookup via call_rcu.
  490. */
  491. rcu_read_lock();
  492. irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
  493. rcu_read_unlock();
  494. /*
  495. * If found in radix tree, then fine.
  496. * Else fallback to linear lookup - this should not happen in practice
  497. * as it means that we failed to insert the node in the radix tree.
  498. */
  499. return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
  500. }
  501. /**
  502. * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
  503. * @domain: domain owning this hardware interrupt
  504. * @virq: linux irq number
  505. * @hwirq: hardware irq number in that domain space
  506. *
  507. * This is for use by irq controllers that use a radix tree reverse
  508. * mapping for fast lookup.
  509. */
  510. void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
  511. irq_hw_number_t hwirq)
  512. {
  513. struct irq_data *irq_data = irq_get_irq_data(virq);
  514. if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
  515. return;
  516. if (virq) {
  517. mutex_lock(&revmap_trees_mutex);
  518. radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
  519. mutex_unlock(&revmap_trees_mutex);
  520. }
  521. }
  522. /**
  523. * irq_linear_revmap() - Find a linux irq from a hw irq number.
  524. * @domain: domain owning this hardware interrupt
  525. * @hwirq: hardware irq number in that domain space
  526. *
  527. * This is a fast path, for use by irq controller code that uses linear
  528. * revmaps. It does fallback to the slow path if the revmap doesn't exist
  529. * yet and will create the revmap entry with appropriate locking
  530. */
  531. unsigned int irq_linear_revmap(struct irq_domain *domain,
  532. irq_hw_number_t hwirq)
  533. {
  534. unsigned int *revmap;
  535. if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
  536. return irq_find_mapping(domain, hwirq);
  537. /* Check revmap bounds */
  538. if (unlikely(hwirq >= domain->revmap_data.linear.size))
  539. return irq_find_mapping(domain, hwirq);
  540. /* Check if revmap was allocated */
  541. revmap = domain->revmap_data.linear.revmap;
  542. if (unlikely(revmap == NULL))
  543. return irq_find_mapping(domain, hwirq);
  544. /* Fill up revmap with slow path if no mapping found */
  545. if (unlikely(!revmap[hwirq]))
  546. revmap[hwirq] = irq_find_mapping(domain, hwirq);
  547. return revmap[hwirq];
  548. }
  549. #ifdef CONFIG_IRQ_DOMAIN_DEBUG
  550. static int virq_debug_show(struct seq_file *m, void *private)
  551. {
  552. unsigned long flags;
  553. struct irq_desc *desc;
  554. const char *p;
  555. static const char none[] = "none";
  556. void *data;
  557. int i;
  558. seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
  559. "chip name", "chip data", "domain name");
  560. for (i = 1; i < nr_irqs; i++) {
  561. desc = irq_to_desc(i);
  562. if (!desc)
  563. continue;
  564. raw_spin_lock_irqsave(&desc->lock, flags);
  565. if (desc->action && desc->action->handler) {
  566. struct irq_chip *chip;
  567. seq_printf(m, "%5d ", i);
  568. seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
  569. chip = irq_desc_get_chip(desc);
  570. if (chip && chip->name)
  571. p = chip->name;
  572. else
  573. p = none;
  574. seq_printf(m, "%-15s ", p);
  575. data = irq_desc_get_chip_data(desc);
  576. seq_printf(m, "0x%16p ", data);
  577. if (desc->irq_data.domain && desc->irq_data.domain->of_node)
  578. p = desc->irq_data.domain->of_node->full_name;
  579. else
  580. p = none;
  581. seq_printf(m, "%s\n", p);
  582. }
  583. raw_spin_unlock_irqrestore(&desc->lock, flags);
  584. }
  585. return 0;
  586. }
  587. static int virq_debug_open(struct inode *inode, struct file *file)
  588. {
  589. return single_open(file, virq_debug_show, inode->i_private);
  590. }
  591. static const struct file_operations virq_debug_fops = {
  592. .open = virq_debug_open,
  593. .read = seq_read,
  594. .llseek = seq_lseek,
  595. .release = single_release,
  596. };
  597. static int __init irq_debugfs_init(void)
  598. {
  599. if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
  600. NULL, &virq_debug_fops) == NULL)
  601. return -ENOMEM;
  602. return 0;
  603. }
  604. __initcall(irq_debugfs_init);
  605. #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
  606. int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
  607. irq_hw_number_t hwirq)
  608. {
  609. return 0;
  610. }
  611. /**
  612. * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
  613. *
  614. * Device Tree IRQ specifier translation function which works with one cell
  615. * bindings where the cell value maps directly to the hwirq number.
  616. */
  617. int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
  618. const u32 *intspec, unsigned int intsize,
  619. unsigned long *out_hwirq, unsigned int *out_type)
  620. {
  621. if (WARN_ON(intsize < 1))
  622. return -EINVAL;
  623. *out_hwirq = intspec[0];
  624. *out_type = IRQ_TYPE_NONE;
  625. return 0;
  626. }
  627. EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
  628. /**
  629. * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
  630. *
  631. * Device Tree IRQ specifier translation function which works with two cell
  632. * bindings where the cell values map directly to the hwirq number
  633. * and linux irq flags.
  634. */
  635. int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
  636. const u32 *intspec, unsigned int intsize,
  637. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  638. {
  639. if (WARN_ON(intsize < 2))
  640. return -EINVAL;
  641. *out_hwirq = intspec[0];
  642. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  643. return 0;
  644. }
  645. EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
  646. /**
  647. * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
  648. *
  649. * Device Tree IRQ specifier translation function which works with either one
  650. * or two cell bindings where the cell values map directly to the hwirq number
  651. * and linux irq flags.
  652. *
  653. * Note: don't use this function unless your interrupt controller explicitly
  654. * supports both one and two cell bindings. For the majority of controllers
  655. * the _onecell() or _twocell() variants above should be used.
  656. */
  657. int irq_domain_xlate_onetwocell(struct irq_domain *d,
  658. struct device_node *ctrlr,
  659. const u32 *intspec, unsigned int intsize,
  660. unsigned long *out_hwirq, unsigned int *out_type)
  661. {
  662. if (WARN_ON(intsize < 1))
  663. return -EINVAL;
  664. *out_hwirq = intspec[0];
  665. *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
  666. return 0;
  667. }
  668. EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
  669. const struct irq_domain_ops irq_domain_simple_ops = {
  670. .map = irq_domain_simple_map,
  671. .xlate = irq_domain_xlate_onetwocell,
  672. };
  673. EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
  674. #ifdef CONFIG_OF_IRQ
  675. void irq_domain_generate_simple(const struct of_device_id *match,
  676. u64 phys_base, unsigned int irq_start)
  677. {
  678. struct device_node *node;
  679. pr_debug("looking for phys_base=%llx, irq_start=%i\n",
  680. (unsigned long long) phys_base, (int) irq_start);
  681. node = of_find_matching_node_by_address(NULL, match, phys_base);
  682. if (node)
  683. irq_domain_add_legacy(node, 32, irq_start, 0,
  684. &irq_domain_simple_ops, NULL);
  685. }
  686. EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
  687. #endif