irq-bcm6345-l1.c 9.3 KB

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