irq-bcm7038-l1.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Broadcom BCM7038 style Level 1 interrupt controller driver
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  5. * Author: Kevin Cernekee
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/bitops.h>
  13. #include <linux/kconfig.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/irq.h>
  20. #include <linux/irqdomain.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/smp.h>
  29. #include <linux/types.h>
  30. #include <linux/irqchip.h>
  31. #include <linux/irqchip/chained_irq.h>
  32. #define IRQS_PER_WORD 32
  33. #define REG_BYTES_PER_IRQ_WORD (sizeof(u32) * 4)
  34. #define MAX_WORDS 8
  35. struct bcm7038_l1_cpu;
  36. struct bcm7038_l1_chip {
  37. raw_spinlock_t lock;
  38. unsigned int n_words;
  39. struct irq_domain *domain;
  40. struct bcm7038_l1_cpu *cpus[NR_CPUS];
  41. u8 affinity[MAX_WORDS * IRQS_PER_WORD];
  42. };
  43. struct bcm7038_l1_cpu {
  44. void __iomem *map_base;
  45. u32 mask_cache[0];
  46. };
  47. /*
  48. * STATUS/MASK_STATUS/MASK_SET/MASK_CLEAR are packed one right after another:
  49. *
  50. * 7038:
  51. * 0x1000_1400: W0_STATUS
  52. * 0x1000_1404: W1_STATUS
  53. * 0x1000_1408: W0_MASK_STATUS
  54. * 0x1000_140c: W1_MASK_STATUS
  55. * 0x1000_1410: W0_MASK_SET
  56. * 0x1000_1414: W1_MASK_SET
  57. * 0x1000_1418: W0_MASK_CLEAR
  58. * 0x1000_141c: W1_MASK_CLEAR
  59. *
  60. * 7445:
  61. * 0xf03e_1500: W0_STATUS
  62. * 0xf03e_1504: W1_STATUS
  63. * 0xf03e_1508: W2_STATUS
  64. * 0xf03e_150c: W3_STATUS
  65. * 0xf03e_1510: W4_STATUS
  66. * 0xf03e_1514: W0_MASK_STATUS
  67. * 0xf03e_1518: W1_MASK_STATUS
  68. * [...]
  69. */
  70. static inline unsigned int reg_status(struct bcm7038_l1_chip *intc,
  71. unsigned int word)
  72. {
  73. return (0 * intc->n_words + word) * sizeof(u32);
  74. }
  75. static inline unsigned int reg_mask_status(struct bcm7038_l1_chip *intc,
  76. unsigned int word)
  77. {
  78. return (1 * intc->n_words + word) * sizeof(u32);
  79. }
  80. static inline unsigned int reg_mask_set(struct bcm7038_l1_chip *intc,
  81. unsigned int word)
  82. {
  83. return (2 * intc->n_words + word) * sizeof(u32);
  84. }
  85. static inline unsigned int reg_mask_clr(struct bcm7038_l1_chip *intc,
  86. unsigned int word)
  87. {
  88. return (3 * intc->n_words + word) * sizeof(u32);
  89. }
  90. static inline u32 l1_readl(void __iomem *reg)
  91. {
  92. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  93. return ioread32be(reg);
  94. else
  95. return readl(reg);
  96. }
  97. static inline void l1_writel(u32 val, void __iomem *reg)
  98. {
  99. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  100. iowrite32be(val, reg);
  101. else
  102. writel(val, reg);
  103. }
  104. static void bcm7038_l1_irq_handle(struct irq_desc *desc)
  105. {
  106. struct bcm7038_l1_chip *intc = irq_desc_get_handler_data(desc);
  107. struct bcm7038_l1_cpu *cpu;
  108. struct irq_chip *chip = irq_desc_get_chip(desc);
  109. unsigned int idx;
  110. #ifdef CONFIG_SMP
  111. cpu = intc->cpus[cpu_logical_map(smp_processor_id())];
  112. #else
  113. cpu = intc->cpus[0];
  114. #endif
  115. chained_irq_enter(chip, desc);
  116. for (idx = 0; idx < intc->n_words; idx++) {
  117. int base = idx * IRQS_PER_WORD;
  118. unsigned long pending, flags;
  119. int hwirq;
  120. raw_spin_lock_irqsave(&intc->lock, flags);
  121. pending = l1_readl(cpu->map_base + reg_status(intc, idx)) &
  122. ~cpu->mask_cache[idx];
  123. raw_spin_unlock_irqrestore(&intc->lock, flags);
  124. for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
  125. generic_handle_irq(irq_find_mapping(intc->domain,
  126. base + hwirq));
  127. }
  128. }
  129. chained_irq_exit(chip, desc);
  130. }
  131. static void __bcm7038_l1_unmask(struct irq_data *d, unsigned int cpu_idx)
  132. {
  133. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  134. u32 word = d->hwirq / IRQS_PER_WORD;
  135. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  136. intc->cpus[cpu_idx]->mask_cache[word] &= ~mask;
  137. l1_writel(mask, intc->cpus[cpu_idx]->map_base +
  138. reg_mask_clr(intc, word));
  139. }
  140. static void __bcm7038_l1_mask(struct irq_data *d, unsigned int cpu_idx)
  141. {
  142. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  143. u32 word = d->hwirq / IRQS_PER_WORD;
  144. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  145. intc->cpus[cpu_idx]->mask_cache[word] |= mask;
  146. l1_writel(mask, intc->cpus[cpu_idx]->map_base +
  147. reg_mask_set(intc, word));
  148. }
  149. static void bcm7038_l1_unmask(struct irq_data *d)
  150. {
  151. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  152. unsigned long flags;
  153. raw_spin_lock_irqsave(&intc->lock, flags);
  154. __bcm7038_l1_unmask(d, intc->affinity[d->hwirq]);
  155. raw_spin_unlock_irqrestore(&intc->lock, flags);
  156. }
  157. static void bcm7038_l1_mask(struct irq_data *d)
  158. {
  159. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  160. unsigned long flags;
  161. raw_spin_lock_irqsave(&intc->lock, flags);
  162. __bcm7038_l1_mask(d, intc->affinity[d->hwirq]);
  163. raw_spin_unlock_irqrestore(&intc->lock, flags);
  164. }
  165. static int bcm7038_l1_set_affinity(struct irq_data *d,
  166. const struct cpumask *dest,
  167. bool force)
  168. {
  169. struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
  170. unsigned long flags;
  171. irq_hw_number_t hw = d->hwirq;
  172. u32 word = hw / IRQS_PER_WORD;
  173. u32 mask = BIT(hw % IRQS_PER_WORD);
  174. unsigned int first_cpu = cpumask_any_and(dest, cpu_online_mask);
  175. bool was_disabled;
  176. raw_spin_lock_irqsave(&intc->lock, flags);
  177. was_disabled = !!(intc->cpus[intc->affinity[hw]]->mask_cache[word] &
  178. mask);
  179. __bcm7038_l1_mask(d, intc->affinity[hw]);
  180. intc->affinity[hw] = first_cpu;
  181. if (!was_disabled)
  182. __bcm7038_l1_unmask(d, first_cpu);
  183. raw_spin_unlock_irqrestore(&intc->lock, flags);
  184. return 0;
  185. }
  186. static int __init bcm7038_l1_init_one(struct device_node *dn,
  187. unsigned int idx,
  188. struct bcm7038_l1_chip *intc)
  189. {
  190. struct resource res;
  191. resource_size_t sz;
  192. struct bcm7038_l1_cpu *cpu;
  193. unsigned int i, n_words, parent_irq;
  194. if (of_address_to_resource(dn, idx, &res))
  195. return -EINVAL;
  196. sz = resource_size(&res);
  197. n_words = sz / REG_BYTES_PER_IRQ_WORD;
  198. if (n_words > MAX_WORDS)
  199. return -EINVAL;
  200. else if (!intc->n_words)
  201. intc->n_words = n_words;
  202. else if (intc->n_words != n_words)
  203. return -EINVAL;
  204. cpu = intc->cpus[idx] = kzalloc(sizeof(*cpu) + n_words * sizeof(u32),
  205. GFP_KERNEL);
  206. if (!cpu)
  207. return -ENOMEM;
  208. cpu->map_base = ioremap(res.start, sz);
  209. if (!cpu->map_base)
  210. return -ENOMEM;
  211. for (i = 0; i < n_words; i++) {
  212. l1_writel(0xffffffff, cpu->map_base + reg_mask_set(intc, i));
  213. cpu->mask_cache[i] = 0xffffffff;
  214. }
  215. parent_irq = irq_of_parse_and_map(dn, idx);
  216. if (!parent_irq) {
  217. pr_err("failed to map parent interrupt %d\n", parent_irq);
  218. return -EINVAL;
  219. }
  220. irq_set_chained_handler_and_data(parent_irq, bcm7038_l1_irq_handle,
  221. intc);
  222. return 0;
  223. }
  224. static struct irq_chip bcm7038_l1_irq_chip = {
  225. .name = "bcm7038-l1",
  226. .irq_mask = bcm7038_l1_mask,
  227. .irq_unmask = bcm7038_l1_unmask,
  228. .irq_set_affinity = bcm7038_l1_set_affinity,
  229. };
  230. static int bcm7038_l1_map(struct irq_domain *d, unsigned int virq,
  231. irq_hw_number_t hw_irq)
  232. {
  233. irq_set_chip_and_handler(virq, &bcm7038_l1_irq_chip, handle_level_irq);
  234. irq_set_chip_data(virq, d->host_data);
  235. return 0;
  236. }
  237. static const struct irq_domain_ops bcm7038_l1_domain_ops = {
  238. .xlate = irq_domain_xlate_onecell,
  239. .map = bcm7038_l1_map,
  240. };
  241. int __init bcm7038_l1_of_init(struct device_node *dn,
  242. struct device_node *parent)
  243. {
  244. struct bcm7038_l1_chip *intc;
  245. int idx, ret;
  246. intc = kzalloc(sizeof(*intc), GFP_KERNEL);
  247. if (!intc)
  248. return -ENOMEM;
  249. raw_spin_lock_init(&intc->lock);
  250. for_each_possible_cpu(idx) {
  251. ret = bcm7038_l1_init_one(dn, idx, intc);
  252. if (ret < 0) {
  253. if (idx)
  254. break;
  255. pr_err("failed to remap intc L1 registers\n");
  256. goto out_free;
  257. }
  258. }
  259. intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
  260. &bcm7038_l1_domain_ops,
  261. intc);
  262. if (!intc->domain) {
  263. ret = -ENOMEM;
  264. goto out_unmap;
  265. }
  266. pr_info("registered BCM7038 L1 intc (mem: 0x%p, IRQs: %d)\n",
  267. intc->cpus[0]->map_base, IRQS_PER_WORD * intc->n_words);
  268. return 0;
  269. out_unmap:
  270. for_each_possible_cpu(idx) {
  271. struct bcm7038_l1_cpu *cpu = intc->cpus[idx];
  272. if (cpu) {
  273. if (cpu->map_base)
  274. iounmap(cpu->map_base);
  275. kfree(cpu);
  276. }
  277. }
  278. out_free:
  279. kfree(intc);
  280. return ret;
  281. }
  282. IRQCHIP_DECLARE(bcm7038_l1, "brcm,bcm7038-l1-intc", bcm7038_l1_of_init);