irqdomain.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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_domain;
  21. static int default_irq_domain_match(struct irq_domain *d, struct device_node *np)
  22. {
  23. return d->of_node != NULL && d->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 domain callbacks
  31. * @inval_irq: provide a hw number in that domain 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 *domain, *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. domain = kzalloc(size, GFP_KERNEL);
  55. if (domain == NULL)
  56. return NULL;
  57. /* Fill structure */
  58. domain->revmap_type = revmap_type;
  59. domain->inval_irq = inval_irq;
  60. domain->ops = ops;
  61. domain->of_node = of_node_get(of_node);
  62. if (domain->ops->match == NULL)
  63. domain->ops->match = default_irq_domain_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(domain->of_node);
  71. kfree(domain);
  72. return NULL;
  73. }
  74. }
  75. }
  76. list_add(&domain->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. domain->inval_irq = 0;
  83. /* setup us as the domain 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 = domain;
  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(domain, 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 *)(domain + 1);
  99. for (i = 0; i < revmap_arg; i++)
  100. rmap[i] = 0;
  101. domain->revmap_data.linear.size = revmap_arg;
  102. domain->revmap_data.linear.revmap = rmap;
  103. break;
  104. case IRQ_DOMAIN_MAP_TREE:
  105. INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
  106. break;
  107. default:
  108. break;
  109. }
  110. pr_debug("irq: Allocated domain of type %d @0x%p\n", revmap_type, domain);
  111. return domain;
  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. * @domain: default domain 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 *domain)
  145. {
  146. pr_debug("irq: Default domain set to @0x%p\n", domain);
  147. irq_default_domain = domain;
  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 *domain, 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 = domain;
  169. if (domain->ops->map(domain, 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. * @domain: domain to allocate the irq for or NULL for default domain
  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 *domain)
  187. {
  188. unsigned int virq;
  189. if (domain == NULL)
  190. domain = irq_default_domain;
  191. BUG_ON(domain == NULL);
  192. WARN_ON(domain->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(domain, 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. * @domain: domain owning this hardware interrupt or NULL for default domain
  214. * @hwirq: hardware irq number in that domain 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 *domain,
  222. irq_hw_number_t hwirq)
  223. {
  224. unsigned int virq, hint;
  225. pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
  226. /* Look for default domain if nececssary */
  227. if (domain == NULL)
  228. domain = irq_default_domain;
  229. if (domain == NULL) {
  230. printk(KERN_WARNING "irq_create_mapping called for"
  231. " NULL domain, hwirq=%lx\n", hwirq);
  232. WARN_ON(1);
  233. return 0;
  234. }
  235. pr_debug("irq: -> using domain @%p\n", domain);
  236. /* Check if mapping already exists */
  237. virq = irq_find_mapping(domain, 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 (domain->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(domain, virq, hwirq)) {
  263. if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
  264. irq_free_desc(virq);
  265. return 0;
  266. }
  267. pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
  268. hwirq, domain->of_node ? domain->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 *domain;
  276. irq_hw_number_t hwirq;
  277. unsigned int type = IRQ_TYPE_NONE;
  278. unsigned int virq;
  279. domain = controller ? irq_find_host(controller) : irq_default_domain;
  280. if (!domain) {
  281. printk(KERN_WARNING "irq: no irq domain found for %s !\n",
  282. controller->full_name);
  283. return 0;
  284. }
  285. /* If domain has no translation, then we assume interrupt line */
  286. if (domain->ops->xlate == NULL)
  287. hwirq = intspec[0];
  288. else {
  289. if (domain->ops->xlate(domain, controller, intspec, intsize,
  290. &hwirq, &type))
  291. return 0;
  292. }
  293. /* Create mapping */
  294. virq = irq_create_mapping(domain, hwirq);
  295. if (!virq)
  296. return virq;
  297. /* Set type if specified and different than the current one */
  298. if (type != IRQ_TYPE_NONE &&
  299. type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
  300. irq_set_irq_type(virq, type);
  301. return virq;
  302. }
  303. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  304. /**
  305. * irq_dispose_mapping() - Unmap an interrupt
  306. * @virq: linux irq number of the interrupt to unmap
  307. */
  308. void irq_dispose_mapping(unsigned int virq)
  309. {
  310. struct irq_data *irq_data = irq_get_irq_data(virq);
  311. struct irq_domain *domain;
  312. irq_hw_number_t hwirq;
  313. if (!virq || !irq_data)
  314. return;
  315. domain = irq_data->domain;
  316. if (WARN_ON(domain == NULL))
  317. return;
  318. /* Never unmap legacy interrupts */
  319. if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  320. return;
  321. irq_set_status_flags(virq, IRQ_NOREQUEST);
  322. /* remove chip and handler */
  323. irq_set_chip_and_handler(virq, NULL, NULL);
  324. /* Make sure it's completed */
  325. synchronize_irq(virq);
  326. /* Tell the PIC about it */
  327. if (domain->ops->unmap)
  328. domain->ops->unmap(domain, virq);
  329. smp_mb();
  330. /* Clear reverse map */
  331. hwirq = irq_data->hwirq;
  332. switch(domain->revmap_type) {
  333. case IRQ_DOMAIN_MAP_LINEAR:
  334. if (hwirq < domain->revmap_data.linear.size)
  335. domain->revmap_data.linear.revmap[hwirq] = 0;
  336. break;
  337. case IRQ_DOMAIN_MAP_TREE:
  338. mutex_lock(&revmap_trees_mutex);
  339. radix_tree_delete(&domain->revmap_data.tree, hwirq);
  340. mutex_unlock(&revmap_trees_mutex);
  341. break;
  342. }
  343. /* Destroy map */
  344. irq_data->hwirq = domain->inval_irq;
  345. irq_free_desc(virq);
  346. }
  347. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  348. /**
  349. * irq_find_mapping() - Find a linux irq from an hw irq number.
  350. * @domain: domain owning this hardware interrupt
  351. * @hwirq: hardware irq number in that domain space
  352. *
  353. * This is a slow path, for use by generic code. It's expected that an
  354. * irq controller implementation directly calls the appropriate low level
  355. * mapping function.
  356. */
  357. unsigned int irq_find_mapping(struct irq_domain *domain,
  358. irq_hw_number_t hwirq)
  359. {
  360. unsigned int i;
  361. unsigned int hint = hwirq % irq_virq_count;
  362. /* Look for default domain if nececssary */
  363. if (domain == NULL)
  364. domain = irq_default_domain;
  365. if (domain == NULL)
  366. return 0;
  367. /* legacy -> bail early */
  368. if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
  369. return hwirq;
  370. /* Slow path does a linear search of the map */
  371. if (hint == 0)
  372. hint = 1;
  373. i = hint;
  374. do {
  375. struct irq_data *data = irq_get_irq_data(i);
  376. if (data && (data->domain == domain) && (data->hwirq == hwirq))
  377. return i;
  378. i++;
  379. if (i >= irq_virq_count)
  380. i = 1;
  381. } while(i != hint);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(irq_find_mapping);
  385. /**
  386. * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
  387. * @domain: domain owning this hardware interrupt
  388. * @hwirq: hardware irq number in that domain space
  389. *
  390. * This is a fast path, for use by irq controller code that uses radix tree
  391. * revmaps
  392. */
  393. unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
  394. irq_hw_number_t hwirq)
  395. {
  396. struct irq_data *irq_data;
  397. if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
  398. return irq_find_mapping(domain, hwirq);
  399. /*
  400. * Freeing an irq can delete nodes along the path to
  401. * do the lookup via call_rcu.
  402. */
  403. rcu_read_lock();
  404. irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
  405. rcu_read_unlock();
  406. /*
  407. * If found in radix tree, then fine.
  408. * Else fallback to linear lookup - this should not happen in practice
  409. * as it means that we failed to insert the node in the radix tree.
  410. */
  411. return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
  412. }
  413. /**
  414. * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
  415. * @domain: domain owning this hardware interrupt
  416. * @virq: linux irq number
  417. * @hwirq: hardware irq number in that domain space
  418. *
  419. * This is for use by irq controllers that use a radix tree reverse
  420. * mapping for fast lookup.
  421. */
  422. void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
  423. irq_hw_number_t hwirq)
  424. {
  425. struct irq_data *irq_data = irq_get_irq_data(virq);
  426. if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
  427. return;
  428. if (virq) {
  429. mutex_lock(&revmap_trees_mutex);
  430. radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
  431. mutex_unlock(&revmap_trees_mutex);
  432. }
  433. }
  434. /**
  435. * irq_linear_revmap() - Find a linux irq from a hw irq number.
  436. * @domain: domain owning this hardware interrupt
  437. * @hwirq: hardware irq number in that domain space
  438. *
  439. * This is a fast path, for use by irq controller code that uses linear
  440. * revmaps. It does fallback to the slow path if the revmap doesn't exist
  441. * yet and will create the revmap entry with appropriate locking
  442. */
  443. unsigned int irq_linear_revmap(struct irq_domain *domain,
  444. irq_hw_number_t hwirq)
  445. {
  446. unsigned int *revmap;
  447. if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
  448. return irq_find_mapping(domain, hwirq);
  449. /* Check revmap bounds */
  450. if (unlikely(hwirq >= domain->revmap_data.linear.size))
  451. return irq_find_mapping(domain, hwirq);
  452. /* Check if revmap was allocated */
  453. revmap = domain->revmap_data.linear.revmap;
  454. if (unlikely(revmap == NULL))
  455. return irq_find_mapping(domain, hwirq);
  456. /* Fill up revmap with slow path if no mapping found */
  457. if (unlikely(!revmap[hwirq]))
  458. revmap[hwirq] = irq_find_mapping(domain, hwirq);
  459. return revmap[hwirq];
  460. }
  461. #ifdef CONFIG_VIRQ_DEBUG
  462. static int virq_debug_show(struct seq_file *m, void *private)
  463. {
  464. unsigned long flags;
  465. struct irq_desc *desc;
  466. const char *p;
  467. static const char none[] = "none";
  468. void *data;
  469. int i;
  470. seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
  471. "chip name", "chip data", "domain name");
  472. for (i = 1; i < nr_irqs; i++) {
  473. desc = irq_to_desc(i);
  474. if (!desc)
  475. continue;
  476. raw_spin_lock_irqsave(&desc->lock, flags);
  477. if (desc->action && desc->action->handler) {
  478. struct irq_chip *chip;
  479. seq_printf(m, "%5d ", i);
  480. seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
  481. chip = irq_desc_get_chip(desc);
  482. if (chip && chip->name)
  483. p = chip->name;
  484. else
  485. p = none;
  486. seq_printf(m, "%-15s ", p);
  487. data = irq_desc_get_chip_data(desc);
  488. seq_printf(m, "0x%16p ", data);
  489. if (desc->irq_data.domain->of_node)
  490. p = desc->irq_data.domain->of_node->full_name;
  491. else
  492. p = none;
  493. seq_printf(m, "%s\n", p);
  494. }
  495. raw_spin_unlock_irqrestore(&desc->lock, flags);
  496. }
  497. return 0;
  498. }
  499. static int virq_debug_open(struct inode *inode, struct file *file)
  500. {
  501. return single_open(file, virq_debug_show, inode->i_private);
  502. }
  503. static const struct file_operations virq_debug_fops = {
  504. .open = virq_debug_open,
  505. .read = seq_read,
  506. .llseek = seq_lseek,
  507. .release = single_release,
  508. };
  509. static int __init irq_debugfs_init(void)
  510. {
  511. if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
  512. NULL, &virq_debug_fops) == NULL)
  513. return -ENOMEM;
  514. return 0;
  515. }
  516. __initcall(irq_debugfs_init);
  517. #endif /* CONFIG_VIRQ_DEBUG */
  518. #else /* CONFIG_PPC */
  519. /**
  520. * irq_domain_add() - Register an irq_domain
  521. * @domain: ptr to initialized irq_domain structure
  522. *
  523. * Registers an irq_domain structure. The irq_domain must at a minimum be
  524. * initialized with an ops structure pointer, and either a ->to_irq hook or
  525. * a valid irq_base value. Everything else is optional.
  526. */
  527. void irq_domain_add(struct irq_domain *domain)
  528. {
  529. struct irq_data *d;
  530. int hwirq, irq;
  531. /*
  532. * This assumes that the irq_domain owner has already allocated
  533. * the irq_descs. This block will be removed when support for dynamic
  534. * allocation of irq_descs is added to irq_domain.
  535. */
  536. irq_domain_for_each_irq(domain, hwirq, irq) {
  537. d = irq_get_irq_data(irq);
  538. if (!d) {
  539. WARN(1, "error: assigning domain to non existant irq_desc");
  540. return;
  541. }
  542. if (d->domain) {
  543. /* things are broken; just report, don't clean up */
  544. WARN(1, "error: irq_desc already assigned to a domain");
  545. return;
  546. }
  547. d->domain = domain;
  548. d->hwirq = hwirq;
  549. }
  550. mutex_lock(&irq_domain_mutex);
  551. list_add(&domain->link, &irq_domain_list);
  552. mutex_unlock(&irq_domain_mutex);
  553. }
  554. /**
  555. * irq_domain_del() - Unregister an irq_domain
  556. * @domain: ptr to registered irq_domain.
  557. */
  558. void irq_domain_del(struct irq_domain *domain)
  559. {
  560. struct irq_data *d;
  561. int hwirq, irq;
  562. mutex_lock(&irq_domain_mutex);
  563. list_del(&domain->link);
  564. mutex_unlock(&irq_domain_mutex);
  565. /* Clear the irq_domain assignments */
  566. irq_domain_for_each_irq(domain, hwirq, irq) {
  567. d = irq_get_irq_data(irq);
  568. d->domain = NULL;
  569. }
  570. }
  571. #if defined(CONFIG_OF_IRQ)
  572. /**
  573. * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
  574. *
  575. * Used by the device tree interrupt mapping code to translate a device tree
  576. * interrupt specifier to a valid linux irq number. Returns either a valid
  577. * linux IRQ number or 0.
  578. *
  579. * When the caller no longer need the irq number returned by this function it
  580. * should arrange to call irq_dispose_mapping().
  581. */
  582. unsigned int irq_create_of_mapping(struct device_node *controller,
  583. const u32 *intspec, unsigned int intsize)
  584. {
  585. struct irq_domain *domain;
  586. unsigned long hwirq;
  587. unsigned int irq, type;
  588. int rc = -EINVAL;
  589. /* Find a domain which can translate the irq spec */
  590. mutex_lock(&irq_domain_mutex);
  591. list_for_each_entry(domain, &irq_domain_list, link) {
  592. if (!domain->ops->xlate)
  593. continue;
  594. rc = domain->ops->xlate(domain, controller,
  595. intspec, intsize, &hwirq, &type);
  596. if (rc == 0)
  597. break;
  598. }
  599. mutex_unlock(&irq_domain_mutex);
  600. if (rc != 0)
  601. return 0;
  602. irq = irq_domain_to_irq(domain, hwirq);
  603. if (type != IRQ_TYPE_NONE)
  604. irq_set_irq_type(irq, type);
  605. pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
  606. controller->full_name, (int)hwirq, irq, type);
  607. return irq;
  608. }
  609. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  610. /**
  611. * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
  612. * @irq: linux irq number to be discarded
  613. *
  614. * Calling this function indicates the caller no longer needs a reference to
  615. * the linux irq number returned by a prior call to irq_create_of_mapping().
  616. */
  617. void irq_dispose_mapping(unsigned int irq)
  618. {
  619. /*
  620. * nothing yet; will be filled when support for dynamic allocation of
  621. * irq_descs is added to irq_domain
  622. */
  623. }
  624. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  625. int irq_domain_simple_xlate(struct irq_domain *d,
  626. struct device_node *controller,
  627. const u32 *intspec, unsigned int intsize,
  628. unsigned long *out_hwirq, unsigned int *out_type)
  629. {
  630. if (d->of_node != controller)
  631. return -EINVAL;
  632. if (intsize < 1)
  633. return -EINVAL;
  634. if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
  635. (intspec[0] >= d->hwirq_base + d->nr_irq)))
  636. return -EINVAL;
  637. *out_hwirq = intspec[0];
  638. *out_type = IRQ_TYPE_NONE;
  639. if (intsize > 1)
  640. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  641. return 0;
  642. }
  643. /**
  644. * irq_domain_create_simple() - Set up a 'simple' translation range
  645. */
  646. void irq_domain_add_simple(struct device_node *controller, int irq_base)
  647. {
  648. struct irq_domain *domain;
  649. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  650. if (!domain) {
  651. WARN_ON(1);
  652. return;
  653. }
  654. domain->irq_base = irq_base;
  655. domain->of_node = of_node_get(controller);
  656. domain->ops = &irq_domain_simple_ops;
  657. irq_domain_add(domain);
  658. }
  659. EXPORT_SYMBOL_GPL(irq_domain_add_simple);
  660. void irq_domain_generate_simple(const struct of_device_id *match,
  661. u64 phys_base, unsigned int irq_start)
  662. {
  663. struct device_node *node;
  664. pr_debug("looking for phys_base=%llx, irq_start=%i\n",
  665. (unsigned long long) phys_base, (int) irq_start);
  666. node = of_find_matching_node_by_address(NULL, match, phys_base);
  667. if (node)
  668. irq_domain_add_simple(node, irq_start);
  669. }
  670. EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
  671. #endif /* CONFIG_OF_IRQ */
  672. struct irq_domain_ops irq_domain_simple_ops = {
  673. #ifdef CONFIG_OF_IRQ
  674. .xlate = irq_domain_simple_xlate,
  675. #endif /* CONFIG_OF_IRQ */
  676. };
  677. EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
  678. #endif /* !CONFIG_PPC */