irq-bcm7038-l1.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. irq_data_update_effective_affinity(d, cpumask_of(first_cpu));
  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. if (of_property_read_bool(dn, "brcm,irq-can-wake"))
  245. enable_irq_wake(parent_irq);
  246. irq_set_chained_handler_and_data(parent_irq, bcm7038_l1_irq_handle,
  247. intc);
  248. return 0;
  249. }
  250. static struct irq_chip bcm7038_l1_irq_chip = {
  251. .name = "bcm7038-l1",
  252. .irq_mask = bcm7038_l1_mask,
  253. .irq_unmask = bcm7038_l1_unmask,
  254. .irq_set_affinity = bcm7038_l1_set_affinity,
  255. #ifdef CONFIG_SMP
  256. .irq_cpu_offline = bcm7038_l1_cpu_offline,
  257. #endif
  258. };
  259. static int bcm7038_l1_map(struct irq_domain *d, unsigned int virq,
  260. irq_hw_number_t hw_irq)
  261. {
  262. irq_set_chip_and_handler(virq, &bcm7038_l1_irq_chip, handle_level_irq);
  263. irq_set_chip_data(virq, d->host_data);
  264. irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq)));
  265. return 0;
  266. }
  267. static const struct irq_domain_ops bcm7038_l1_domain_ops = {
  268. .xlate = irq_domain_xlate_onecell,
  269. .map = bcm7038_l1_map,
  270. };
  271. int __init bcm7038_l1_of_init(struct device_node *dn,
  272. struct device_node *parent)
  273. {
  274. struct bcm7038_l1_chip *intc;
  275. int idx, ret;
  276. intc = kzalloc(sizeof(*intc), GFP_KERNEL);
  277. if (!intc)
  278. return -ENOMEM;
  279. raw_spin_lock_init(&intc->lock);
  280. for_each_possible_cpu(idx) {
  281. ret = bcm7038_l1_init_one(dn, idx, intc);
  282. if (ret < 0) {
  283. if (idx)
  284. break;
  285. pr_err("failed to remap intc L1 registers\n");
  286. goto out_free;
  287. }
  288. }
  289. intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
  290. &bcm7038_l1_domain_ops,
  291. intc);
  292. if (!intc->domain) {
  293. ret = -ENOMEM;
  294. goto out_unmap;
  295. }
  296. return 0;
  297. out_unmap:
  298. for_each_possible_cpu(idx) {
  299. struct bcm7038_l1_cpu *cpu = intc->cpus[idx];
  300. if (cpu) {
  301. if (cpu->map_base)
  302. iounmap(cpu->map_base);
  303. kfree(cpu);
  304. }
  305. }
  306. out_free:
  307. kfree(intc);
  308. return ret;
  309. }
  310. IRQCHIP_DECLARE(bcm7038_l1, "brcm,bcm7038-l1-intc", bcm7038_l1_of_init);