irqdomain.c 23 KB

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