irqdomain.c 21 KB

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