irq-bcm6345-l1.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom BCM6345 style Level 1 interrupt controller driver
  4. *
  5. * Copyright (C) 2014 Broadcom Corporation
  6. * Copyright 2015 Simon Arlott
  7. *
  8. * This is based on the BCM7038 (which supports SMP) but with a single
  9. * enable register instead of separate mask/set/clear registers.
  10. *
  11. * The BCM3380 has a similar mask/status register layout, but each pair
  12. * of words is at separate locations (and SMP is not supported).
  13. *
  14. * ENABLE/STATUS words are packed next to each other for each CPU:
  15. *
  16. * BCM6368:
  17. * 0x1000_0020: CPU0_W0_ENABLE
  18. * 0x1000_0024: CPU0_W1_ENABLE
  19. * 0x1000_0028: CPU0_W0_STATUS IRQs 31-63
  20. * 0x1000_002c: CPU0_W1_STATUS IRQs 0-31
  21. * 0x1000_0030: CPU1_W0_ENABLE
  22. * 0x1000_0034: CPU1_W1_ENABLE
  23. * 0x1000_0038: CPU1_W0_STATUS IRQs 31-63
  24. * 0x1000_003c: CPU1_W1_STATUS IRQs 0-31
  25. *
  26. * BCM63168:
  27. * 0x1000_0020: CPU0_W0_ENABLE
  28. * 0x1000_0024: CPU0_W1_ENABLE
  29. * 0x1000_0028: CPU0_W2_ENABLE
  30. * 0x1000_002c: CPU0_W3_ENABLE
  31. * 0x1000_0030: CPU0_W0_STATUS IRQs 96-127
  32. * 0x1000_0034: CPU0_W1_STATUS IRQs 64-95
  33. * 0x1000_0038: CPU0_W2_STATUS IRQs 32-63
  34. * 0x1000_003c: CPU0_W3_STATUS IRQs 0-31
  35. * 0x1000_0040: CPU1_W0_ENABLE
  36. * 0x1000_0044: CPU1_W1_ENABLE
  37. * 0x1000_0048: CPU1_W2_ENABLE
  38. * 0x1000_004c: CPU1_W3_ENABLE
  39. * 0x1000_0050: CPU1_W0_STATUS IRQs 96-127
  40. * 0x1000_0054: CPU1_W1_STATUS IRQs 64-95
  41. * 0x1000_0058: CPU1_W2_STATUS IRQs 32-63
  42. * 0x1000_005c: CPU1_W3_STATUS IRQs 0-31
  43. *
  44. * IRQs are numbered in CPU native endian order
  45. * (which is big-endian in these examples)
  46. */
  47. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  48. #include <linux/bitops.h>
  49. #include <linux/cpumask.h>
  50. #include <linux/kernel.h>
  51. #include <linux/init.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/io.h>
  54. #include <linux/ioport.h>
  55. #include <linux/irq.h>
  56. #include <linux/irqdomain.h>
  57. #include <linux/module.h>
  58. #include <linux/of.h>
  59. #include <linux/of_irq.h>
  60. #include <linux/of_address.h>
  61. #include <linux/of_platform.h>
  62. #include <linux/platform_device.h>
  63. #include <linux/slab.h>
  64. #include <linux/smp.h>
  65. #include <linux/types.h>
  66. #include <linux/irqchip.h>
  67. #include <linux/irqchip/chained_irq.h>
  68. #define IRQS_PER_WORD 32
  69. #define REG_BYTES_PER_IRQ_WORD (sizeof(u32) * 2)
  70. struct bcm6345_l1_cpu;
  71. struct bcm6345_l1_chip {
  72. raw_spinlock_t lock;
  73. unsigned int n_words;
  74. struct irq_domain *domain;
  75. struct cpumask cpumask;
  76. struct bcm6345_l1_cpu *cpus[NR_CPUS];
  77. };
  78. struct bcm6345_l1_cpu {
  79. struct bcm6345_l1_chip *intc;
  80. void __iomem *map_base;
  81. unsigned int parent_irq;
  82. u32 enable_cache[];
  83. };
  84. static inline unsigned int reg_enable(struct bcm6345_l1_chip *intc,
  85. unsigned int word)
  86. {
  87. #ifdef __BIG_ENDIAN
  88. return (1 * intc->n_words - word - 1) * sizeof(u32);
  89. #else
  90. return (0 * intc->n_words + word) * sizeof(u32);
  91. #endif
  92. }
  93. static inline unsigned int reg_status(struct bcm6345_l1_chip *intc,
  94. unsigned int word)
  95. {
  96. #ifdef __BIG_ENDIAN
  97. return (2 * intc->n_words - word - 1) * sizeof(u32);
  98. #else
  99. return (1 * intc->n_words + word) * sizeof(u32);
  100. #endif
  101. }
  102. static inline unsigned int cpu_for_irq(struct bcm6345_l1_chip *intc,
  103. struct irq_data *d)
  104. {
  105. return cpumask_first_and(&intc->cpumask, irq_data_get_affinity_mask(d));
  106. }
  107. static void bcm6345_l1_irq_handle(struct irq_desc *desc)
  108. {
  109. struct bcm6345_l1_cpu *cpu = irq_desc_get_handler_data(desc);
  110. struct bcm6345_l1_chip *intc = cpu->intc;
  111. struct irq_chip *chip = irq_desc_get_chip(desc);
  112. unsigned int idx;
  113. chained_irq_enter(chip, desc);
  114. for (idx = 0; idx < intc->n_words; idx++) {
  115. int base = idx * IRQS_PER_WORD;
  116. unsigned long pending;
  117. irq_hw_number_t hwirq;
  118. pending = __raw_readl(cpu->map_base + reg_status(intc, idx));
  119. pending &= __raw_readl(cpu->map_base + reg_enable(intc, idx));
  120. for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
  121. if (generic_handle_domain_irq(intc->domain, base + hwirq))
  122. spurious_interrupt();
  123. }
  124. }
  125. chained_irq_exit(chip, desc);
  126. }
  127. static inline void __bcm6345_l1_unmask(struct irq_data *d)
  128. {
  129. struct bcm6345_l1_chip *intc = irq_data_get_irq_chip_data(d);
  130. u32 word = d->hwirq / IRQS_PER_WORD;
  131. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  132. unsigned int cpu_idx = cpu_for_irq(intc, d);
  133. intc->cpus[cpu_idx]->enable_cache[word] |= mask;
  134. __raw_writel(intc->cpus[cpu_idx]->enable_cache[word],
  135. intc->cpus[cpu_idx]->map_base + reg_enable(intc, word));
  136. }
  137. static inline void __bcm6345_l1_mask(struct irq_data *d)
  138. {
  139. struct bcm6345_l1_chip *intc = irq_data_get_irq_chip_data(d);
  140. u32 word = d->hwirq / IRQS_PER_WORD;
  141. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  142. unsigned int cpu_idx = cpu_for_irq(intc, d);
  143. intc->cpus[cpu_idx]->enable_cache[word] &= ~mask;
  144. __raw_writel(intc->cpus[cpu_idx]->enable_cache[word],
  145. intc->cpus[cpu_idx]->map_base + reg_enable(intc, word));
  146. }
  147. static void bcm6345_l1_unmask(struct irq_data *d)
  148. {
  149. struct bcm6345_l1_chip *intc = irq_data_get_irq_chip_data(d);
  150. unsigned long flags;
  151. raw_spin_lock_irqsave(&intc->lock, flags);
  152. __bcm6345_l1_unmask(d);
  153. raw_spin_unlock_irqrestore(&intc->lock, flags);
  154. }
  155. static void bcm6345_l1_mask(struct irq_data *d)
  156. {
  157. struct bcm6345_l1_chip *intc = irq_data_get_irq_chip_data(d);
  158. unsigned long flags;
  159. raw_spin_lock_irqsave(&intc->lock, flags);
  160. __bcm6345_l1_mask(d);
  161. raw_spin_unlock_irqrestore(&intc->lock, flags);
  162. }
  163. static int bcm6345_l1_set_affinity(struct irq_data *d,
  164. const struct cpumask *dest,
  165. bool force)
  166. {
  167. struct bcm6345_l1_chip *intc = irq_data_get_irq_chip_data(d);
  168. u32 word = d->hwirq / IRQS_PER_WORD;
  169. u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
  170. unsigned int old_cpu = cpu_for_irq(intc, d);
  171. unsigned int new_cpu;
  172. struct cpumask valid;
  173. unsigned long flags;
  174. bool enabled;
  175. if (!cpumask_and(&valid, &intc->cpumask, dest))
  176. return -EINVAL;
  177. new_cpu = cpumask_any_and(&valid, cpu_online_mask);
  178. if (new_cpu >= nr_cpu_ids)
  179. return -EINVAL;
  180. dest = cpumask_of(new_cpu);
  181. raw_spin_lock_irqsave(&intc->lock, flags);
  182. if (old_cpu != new_cpu) {
  183. enabled = intc->cpus[old_cpu]->enable_cache[word] & mask;
  184. if (enabled)
  185. __bcm6345_l1_mask(d);
  186. irq_data_update_affinity(d, dest);
  187. if (enabled)
  188. __bcm6345_l1_unmask(d);
  189. } else {
  190. irq_data_update_affinity(d, dest);
  191. }
  192. raw_spin_unlock_irqrestore(&intc->lock, flags);
  193. irq_data_update_effective_affinity(d, cpumask_of(new_cpu));
  194. return IRQ_SET_MASK_OK_NOCOPY;
  195. }
  196. static int __init bcm6345_l1_init_one(struct device_node *dn,
  197. unsigned int idx,
  198. struct bcm6345_l1_chip *intc)
  199. {
  200. struct resource res;
  201. resource_size_t sz;
  202. struct bcm6345_l1_cpu *cpu;
  203. unsigned int i, n_words;
  204. if (of_address_to_resource(dn, idx, &res))
  205. return -EINVAL;
  206. sz = resource_size(&res);
  207. n_words = sz / REG_BYTES_PER_IRQ_WORD;
  208. if (!intc->n_words)
  209. intc->n_words = n_words;
  210. else if (intc->n_words != n_words)
  211. return -EINVAL;
  212. cpu = intc->cpus[idx] = kzalloc(sizeof(*cpu) + n_words * sizeof(u32),
  213. GFP_KERNEL);
  214. if (!cpu)
  215. return -ENOMEM;
  216. cpu->intc = intc;
  217. cpu->map_base = ioremap(res.start, sz);
  218. if (!cpu->map_base)
  219. return -ENOMEM;
  220. for (i = 0; i < n_words; i++) {
  221. cpu->enable_cache[i] = 0;
  222. __raw_writel(0, cpu->map_base + reg_enable(intc, i));
  223. }
  224. cpu->parent_irq = irq_of_parse_and_map(dn, idx);
  225. if (!cpu->parent_irq) {
  226. pr_err("failed to map parent interrupt %d\n", cpu->parent_irq);
  227. return -EINVAL;
  228. }
  229. irq_set_chained_handler_and_data(cpu->parent_irq,
  230. bcm6345_l1_irq_handle, cpu);
  231. return 0;
  232. }
  233. static struct irq_chip bcm6345_l1_irq_chip = {
  234. .name = "bcm6345-l1",
  235. .irq_mask = bcm6345_l1_mask,
  236. .irq_unmask = bcm6345_l1_unmask,
  237. .irq_set_affinity = bcm6345_l1_set_affinity,
  238. };
  239. static int bcm6345_l1_map(struct irq_domain *d, unsigned int virq,
  240. irq_hw_number_t hw_irq)
  241. {
  242. irq_set_chip_and_handler(virq,
  243. &bcm6345_l1_irq_chip, handle_percpu_irq);
  244. irq_set_chip_data(virq, d->host_data);
  245. irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq)));
  246. return 0;
  247. }
  248. static const struct irq_domain_ops bcm6345_l1_domain_ops = {
  249. .xlate = irq_domain_xlate_onecell,
  250. .map = bcm6345_l1_map,
  251. };
  252. static int __init bcm6345_l1_of_init(struct device_node *dn,
  253. struct device_node *parent)
  254. {
  255. struct bcm6345_l1_chip *intc;
  256. unsigned int idx;
  257. int ret;
  258. intc = kzalloc(sizeof(*intc), GFP_KERNEL);
  259. if (!intc)
  260. return -ENOMEM;
  261. for_each_possible_cpu(idx) {
  262. ret = bcm6345_l1_init_one(dn, idx, intc);
  263. if (ret)
  264. pr_err("failed to init intc L1 for cpu %d: %d\n",
  265. idx, ret);
  266. else
  267. cpumask_set_cpu(idx, &intc->cpumask);
  268. }
  269. if (cpumask_empty(&intc->cpumask)) {
  270. ret = -ENODEV;
  271. goto out_free;
  272. }
  273. raw_spin_lock_init(&intc->lock);
  274. intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
  275. &bcm6345_l1_domain_ops,
  276. intc);
  277. if (!intc->domain) {
  278. ret = -ENOMEM;
  279. goto out_unmap;
  280. }
  281. pr_info("registered BCM6345 L1 intc (IRQs: %d)\n",
  282. IRQS_PER_WORD * intc->n_words);
  283. for_each_cpu(idx, &intc->cpumask) {
  284. struct bcm6345_l1_cpu *cpu = intc->cpus[idx];
  285. pr_info(" CPU%u at MMIO 0x%p (irq = %d)\n", idx,
  286. cpu->map_base, cpu->parent_irq);
  287. }
  288. return 0;
  289. out_unmap:
  290. for_each_possible_cpu(idx) {
  291. struct bcm6345_l1_cpu *cpu = intc->cpus[idx];
  292. if (cpu) {
  293. if (cpu->map_base)
  294. iounmap(cpu->map_base);
  295. kfree(cpu);
  296. }
  297. }
  298. out_free:
  299. kfree(intc);
  300. return ret;
  301. }
  302. IRQCHIP_DECLARE(bcm6345_l1, "brcm,bcm6345-l1-intc", bcm6345_l1_of_init);