irq-bcm7038-l1.c 8.8 KB

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