irq-crossbar.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * drivers/irqchip/irq-crossbar.c
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. * Author: Sricharan R <r.sricharan@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/slab.h>
  18. #include "irqchip.h"
  19. #define IRQ_FREE -1
  20. #define IRQ_RESERVED -2
  21. #define IRQ_SKIP -3
  22. #define GIC_IRQ_START 32
  23. /**
  24. * struct crossbar_device - crossbar device description
  25. * @lock: spinlock serializing access to @irq_map
  26. * @int_max: maximum number of supported interrupts
  27. * @safe_map: safe default value to initialize the crossbar
  28. * @max_crossbar_sources: Maximum number of crossbar sources
  29. * @irq_map: array of interrupts to crossbar number mapping
  30. * @crossbar_base: crossbar base address
  31. * @register_offsets: offsets for each irq number
  32. * @write: register write function pointer
  33. */
  34. struct crossbar_device {
  35. raw_spinlock_t lock;
  36. uint int_max;
  37. uint safe_map;
  38. uint max_crossbar_sources;
  39. uint *irq_map;
  40. void __iomem *crossbar_base;
  41. int *register_offsets;
  42. void (*write)(int, int);
  43. };
  44. static struct crossbar_device *cb;
  45. static void crossbar_writel(int irq_no, int cb_no)
  46. {
  47. writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  48. }
  49. static void crossbar_writew(int irq_no, int cb_no)
  50. {
  51. writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  52. }
  53. static void crossbar_writeb(int irq_no, int cb_no)
  54. {
  55. writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  56. }
  57. static struct irq_chip crossbar_chip = {
  58. .name = "CBAR",
  59. .irq_eoi = irq_chip_eoi_parent,
  60. .irq_mask = irq_chip_mask_parent,
  61. .irq_unmask = irq_chip_unmask_parent,
  62. .irq_retrigger = irq_chip_retrigger_hierarchy,
  63. .irq_set_wake = irq_chip_set_wake_parent,
  64. #ifdef CONFIG_SMP
  65. .irq_set_affinity = irq_chip_set_affinity_parent,
  66. #endif
  67. };
  68. static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
  69. irq_hw_number_t hwirq)
  70. {
  71. struct of_phandle_args args;
  72. int i;
  73. int err;
  74. raw_spin_lock(&cb->lock);
  75. for (i = cb->int_max - 1; i >= 0; i--) {
  76. if (cb->irq_map[i] == IRQ_FREE) {
  77. cb->irq_map[i] = hwirq;
  78. break;
  79. }
  80. }
  81. raw_spin_unlock(&cb->lock);
  82. if (i < 0)
  83. return -ENODEV;
  84. args.np = domain->parent->of_node;
  85. args.args_count = 3;
  86. args.args[0] = 0; /* SPI */
  87. args.args[1] = i;
  88. args.args[2] = IRQ_TYPE_LEVEL_HIGH;
  89. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
  90. if (err)
  91. cb->irq_map[i] = IRQ_FREE;
  92. else
  93. cb->write(i, hwirq);
  94. return err;
  95. }
  96. static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
  97. unsigned int nr_irqs, void *data)
  98. {
  99. struct of_phandle_args *args = data;
  100. irq_hw_number_t hwirq;
  101. int i;
  102. if (args->args_count != 3)
  103. return -EINVAL; /* Not GIC compliant */
  104. if (args->args[0] != 0)
  105. return -EINVAL; /* No PPI should point to this domain */
  106. hwirq = args->args[1];
  107. if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
  108. return -EINVAL; /* Can't deal with this */
  109. for (i = 0; i < nr_irqs; i++) {
  110. int err = allocate_gic_irq(d, virq + i, hwirq + i);
  111. if (err)
  112. return err;
  113. irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
  114. &crossbar_chip, NULL);
  115. }
  116. return 0;
  117. }
  118. /**
  119. * crossbar_domain_free - unmap/free a crossbar<->irq connection
  120. * @domain: domain of irq to unmap
  121. * @virq: virq number
  122. * @nr_irqs: number of irqs to free
  123. *
  124. * We do not maintain a use count of total number of map/unmap
  125. * calls for a particular irq to find out if a irq can be really
  126. * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
  127. * after which irq is anyways unusable. So an explicit map has to be called
  128. * after that.
  129. */
  130. static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
  131. unsigned int nr_irqs)
  132. {
  133. int i;
  134. raw_spin_lock(&cb->lock);
  135. for (i = 0; i < nr_irqs; i++) {
  136. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  137. irq_domain_reset_irq_data(d);
  138. cb->irq_map[d->hwirq] = IRQ_FREE;
  139. cb->write(d->hwirq, cb->safe_map);
  140. }
  141. raw_spin_unlock(&cb->lock);
  142. }
  143. static int crossbar_domain_xlate(struct irq_domain *d,
  144. struct device_node *controller,
  145. const u32 *intspec, unsigned int intsize,
  146. unsigned long *out_hwirq,
  147. unsigned int *out_type)
  148. {
  149. if (d->of_node != controller)
  150. return -EINVAL; /* Shouldn't happen, really... */
  151. if (intsize != 3)
  152. return -EINVAL; /* Not GIC compliant */
  153. if (intspec[0] != 0)
  154. return -EINVAL; /* No PPI should point to this domain */
  155. *out_hwirq = intspec[1];
  156. *out_type = intspec[2];
  157. return 0;
  158. }
  159. static const struct irq_domain_ops crossbar_domain_ops = {
  160. .alloc = crossbar_domain_alloc,
  161. .free = crossbar_domain_free,
  162. .xlate = crossbar_domain_xlate,
  163. };
  164. static int __init crossbar_of_init(struct device_node *node)
  165. {
  166. int i, size, max = 0, reserved = 0, entry;
  167. const __be32 *irqsr;
  168. int ret = -ENOMEM;
  169. cb = kzalloc(sizeof(*cb), GFP_KERNEL);
  170. if (!cb)
  171. return ret;
  172. cb->crossbar_base = of_iomap(node, 0);
  173. if (!cb->crossbar_base)
  174. goto err_cb;
  175. of_property_read_u32(node, "ti,max-crossbar-sources",
  176. &cb->max_crossbar_sources);
  177. if (!cb->max_crossbar_sources) {
  178. pr_err("missing 'ti,max-crossbar-sources' property\n");
  179. ret = -EINVAL;
  180. goto err_base;
  181. }
  182. of_property_read_u32(node, "ti,max-irqs", &max);
  183. if (!max) {
  184. pr_err("missing 'ti,max-irqs' property\n");
  185. ret = -EINVAL;
  186. goto err_base;
  187. }
  188. cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
  189. if (!cb->irq_map)
  190. goto err_base;
  191. cb->int_max = max;
  192. for (i = 0; i < max; i++)
  193. cb->irq_map[i] = IRQ_FREE;
  194. /* Get and mark reserved irqs */
  195. irqsr = of_get_property(node, "ti,irqs-reserved", &size);
  196. if (irqsr) {
  197. size /= sizeof(__be32);
  198. for (i = 0; i < size; i++) {
  199. of_property_read_u32_index(node,
  200. "ti,irqs-reserved",
  201. i, &entry);
  202. if (entry >= max) {
  203. pr_err("Invalid reserved entry\n");
  204. ret = -EINVAL;
  205. goto err_irq_map;
  206. }
  207. cb->irq_map[entry] = IRQ_RESERVED;
  208. }
  209. }
  210. /* Skip irqs hardwired to bypass the crossbar */
  211. irqsr = of_get_property(node, "ti,irqs-skip", &size);
  212. if (irqsr) {
  213. size /= sizeof(__be32);
  214. for (i = 0; i < size; i++) {
  215. of_property_read_u32_index(node,
  216. "ti,irqs-skip",
  217. i, &entry);
  218. if (entry >= max) {
  219. pr_err("Invalid skip entry\n");
  220. ret = -EINVAL;
  221. goto err_irq_map;
  222. }
  223. cb->irq_map[entry] = IRQ_SKIP;
  224. }
  225. }
  226. cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
  227. if (!cb->register_offsets)
  228. goto err_irq_map;
  229. of_property_read_u32(node, "ti,reg-size", &size);
  230. switch (size) {
  231. case 1:
  232. cb->write = crossbar_writeb;
  233. break;
  234. case 2:
  235. cb->write = crossbar_writew;
  236. break;
  237. case 4:
  238. cb->write = crossbar_writel;
  239. break;
  240. default:
  241. pr_err("Invalid reg-size property\n");
  242. ret = -EINVAL;
  243. goto err_reg_offset;
  244. break;
  245. }
  246. /*
  247. * Register offsets are not linear because of the
  248. * reserved irqs. so find and store the offsets once.
  249. */
  250. for (i = 0; i < max; i++) {
  251. if (cb->irq_map[i] == IRQ_RESERVED)
  252. continue;
  253. cb->register_offsets[i] = reserved;
  254. reserved += size;
  255. }
  256. of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
  257. /* Initialize the crossbar with safe map to start with */
  258. for (i = 0; i < max; i++) {
  259. if (cb->irq_map[i] == IRQ_RESERVED ||
  260. cb->irq_map[i] == IRQ_SKIP)
  261. continue;
  262. cb->write(i, cb->safe_map);
  263. }
  264. raw_spin_lock_init(&cb->lock);
  265. return 0;
  266. err_reg_offset:
  267. kfree(cb->register_offsets);
  268. err_irq_map:
  269. kfree(cb->irq_map);
  270. err_base:
  271. iounmap(cb->crossbar_base);
  272. err_cb:
  273. kfree(cb);
  274. cb = NULL;
  275. return ret;
  276. }
  277. static int __init irqcrossbar_init(struct device_node *node,
  278. struct device_node *parent)
  279. {
  280. struct irq_domain *parent_domain, *domain;
  281. int err;
  282. if (!parent) {
  283. pr_err("%s: no parent, giving up\n", node->full_name);
  284. return -ENODEV;
  285. }
  286. parent_domain = irq_find_host(parent);
  287. if (!parent_domain) {
  288. pr_err("%s: unable to obtain parent domain\n", node->full_name);
  289. return -ENXIO;
  290. }
  291. err = crossbar_of_init(node);
  292. if (err)
  293. return err;
  294. domain = irq_domain_add_hierarchy(parent_domain, 0,
  295. cb->max_crossbar_sources,
  296. node, &crossbar_domain_ops,
  297. NULL);
  298. if (!domain) {
  299. pr_err("%s: failed to allocated domain\n", node->full_name);
  300. return -ENOMEM;
  301. }
  302. return 0;
  303. }
  304. IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);