irq-bcm7038-l1.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #ifdef CONFIG_SMP
  187. static void bcm7038_l1_cpu_offline(struct irq_data *d)
  188. {
  189. struct cpumask *mask = irq_data_get_affinity_mask(d);
  190. int cpu = smp_processor_id();
  191. cpumask_t new_affinity;
  192. /* This CPU was not on the affinity mask */
  193. if (!cpumask_test_cpu(cpu, mask))
  194. return;
  195. if (cpumask_weight(mask) > 1) {
  196. /*
  197. * Multiple CPU affinity, remove this CPU from the affinity
  198. * mask
  199. */
  200. cpumask_copy(&new_affinity, mask);
  201. cpumask_clear_cpu(cpu, &new_affinity);
  202. } else {
  203. /* Only CPU, put on the lowest online CPU */
  204. cpumask_clear(&new_affinity);
  205. cpumask_set_cpu(cpumask_first(cpu_online_mask), &new_affinity);
  206. }
  207. irq_set_affinity_locked(d, &new_affinity, false);
  208. }
  209. #endif
  210. static int __init bcm7038_l1_init_one(struct device_node *dn,
  211. unsigned int idx,
  212. struct bcm7038_l1_chip *intc)
  213. {
  214. struct resource res;
  215. resource_size_t sz;
  216. struct bcm7038_l1_cpu *cpu;
  217. unsigned int i, n_words, parent_irq;
  218. if (of_address_to_resource(dn, idx, &res))
  219. return -EINVAL;
  220. sz = resource_size(&res);
  221. n_words = sz / REG_BYTES_PER_IRQ_WORD;
  222. if (n_words > MAX_WORDS)
  223. return -EINVAL;
  224. else if (!intc->n_words)
  225. intc->n_words = n_words;
  226. else if (intc->n_words != n_words)
  227. return -EINVAL;
  228. cpu = intc->cpus[idx] = kzalloc(sizeof(*cpu) + n_words * sizeof(u32),
  229. GFP_KERNEL);
  230. if (!cpu)
  231. return -ENOMEM;
  232. cpu->map_base = ioremap(res.start, sz);
  233. if (!cpu->map_base)
  234. return -ENOMEM;
  235. for (i = 0; i < n_words; i++) {
  236. l1_writel(0xffffffff, cpu->map_base + reg_mask_set(intc, i));
  237. cpu->mask_cache[i] = 0xffffffff;
  238. }
  239. parent_irq = irq_of_parse_and_map(dn, idx);
  240. if (!parent_irq) {
  241. pr_err("failed to map parent interrupt %d\n", parent_irq);
  242. return -EINVAL;
  243. }
  244. irq_set_chained_handler_and_data(parent_irq, bcm7038_l1_irq_handle,
  245. intc);
  246. return 0;
  247. }
  248. static struct irq_chip bcm7038_l1_irq_chip = {
  249. .name = "bcm7038-l1",
  250. .irq_mask = bcm7038_l1_mask,
  251. .irq_unmask = bcm7038_l1_unmask,
  252. .irq_set_affinity = bcm7038_l1_set_affinity,
  253. #ifdef CONFIG_SMP
  254. .irq_cpu_offline = bcm7038_l1_cpu_offline,
  255. #endif
  256. };
  257. static int bcm7038_l1_map(struct irq_domain *d, unsigned int virq,
  258. irq_hw_number_t hw_irq)
  259. {
  260. irq_set_chip_and_handler(virq, &bcm7038_l1_irq_chip, handle_level_irq);
  261. irq_set_chip_data(virq, d->host_data);
  262. return 0;
  263. }
  264. static const struct irq_domain_ops bcm7038_l1_domain_ops = {
  265. .xlate = irq_domain_xlate_onecell,
  266. .map = bcm7038_l1_map,
  267. };
  268. int __init bcm7038_l1_of_init(struct device_node *dn,
  269. struct device_node *parent)
  270. {
  271. struct bcm7038_l1_chip *intc;
  272. int idx, ret;
  273. intc = kzalloc(sizeof(*intc), GFP_KERNEL);
  274. if (!intc)
  275. return -ENOMEM;
  276. raw_spin_lock_init(&intc->lock);
  277. for_each_possible_cpu(idx) {
  278. ret = bcm7038_l1_init_one(dn, idx, intc);
  279. if (ret < 0) {
  280. if (idx)
  281. break;
  282. pr_err("failed to remap intc L1 registers\n");
  283. goto out_free;
  284. }
  285. }
  286. intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
  287. &bcm7038_l1_domain_ops,
  288. intc);
  289. if (!intc->domain) {
  290. ret = -ENOMEM;
  291. goto out_unmap;
  292. }
  293. pr_info("registered BCM7038 L1 intc (mem: 0x%p, IRQs: %d)\n",
  294. intc->cpus[0]->map_base, IRQS_PER_WORD * intc->n_words);
  295. return 0;
  296. out_unmap:
  297. for_each_possible_cpu(idx) {
  298. struct bcm7038_l1_cpu *cpu = intc->cpus[idx];
  299. if (cpu) {
  300. if (cpu->map_base)
  301. iounmap(cpu->map_base);
  302. kfree(cpu);
  303. }
  304. }
  305. out_free:
  306. kfree(intc);
  307. return ret;
  308. }
  309. IRQCHIP_DECLARE(bcm7038_l1, "brcm,bcm7038-l1-intc", bcm7038_l1_of_init);