irq-hip04.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Hisilicon HiP04 INTC
  3. *
  4. * Copyright (C) 2002-2014 ARM Limited.
  5. * Copyright (c) 2013-2014 Hisilicon Ltd.
  6. * Copyright (c) 2013-2014 Linaro Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Interrupt architecture for the HIP04 INTC:
  13. *
  14. * o There is one Interrupt Distributor, which receives interrupts
  15. * from system devices and sends them to the Interrupt Controllers.
  16. *
  17. * o There is one CPU Interface per CPU, which sends interrupts sent
  18. * by the Distributor, and interrupts generated locally, to the
  19. * associated CPU. The base address of the CPU interface is usually
  20. * aliased so that the same address points to different chips depending
  21. * on the CPU it is accessed from.
  22. *
  23. * Note that IRQs 0-31 are special - they are local to each CPU.
  24. * As such, the enable set/clear, pending set/clear and active bit
  25. * registers are banked per-cpu for these sources.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/err.h>
  30. #include <linux/module.h>
  31. #include <linux/list.h>
  32. #include <linux/smp.h>
  33. #include <linux/cpu.h>
  34. #include <linux/cpu_pm.h>
  35. #include <linux/cpumask.h>
  36. #include <linux/io.h>
  37. #include <linux/of.h>
  38. #include <linux/of_address.h>
  39. #include <linux/of_irq.h>
  40. #include <linux/irqdomain.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/slab.h>
  43. #include <linux/irqchip.h>
  44. #include <linux/irqchip/arm-gic.h>
  45. #include <asm/irq.h>
  46. #include <asm/exception.h>
  47. #include <asm/smp_plat.h>
  48. #include "irq-gic-common.h"
  49. #define HIP04_MAX_IRQS 510
  50. struct hip04_irq_data {
  51. void __iomem *dist_base;
  52. void __iomem *cpu_base;
  53. struct irq_domain *domain;
  54. unsigned int nr_irqs;
  55. };
  56. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  57. /*
  58. * The GIC mapping of CPU interfaces does not necessarily match
  59. * the logical CPU numbering. Let's use a mapping as returned
  60. * by the GIC itself.
  61. */
  62. #define NR_HIP04_CPU_IF 16
  63. static u16 hip04_cpu_map[NR_HIP04_CPU_IF] __read_mostly;
  64. static struct hip04_irq_data hip04_data __read_mostly;
  65. static inline void __iomem *hip04_dist_base(struct irq_data *d)
  66. {
  67. struct hip04_irq_data *hip04_data = irq_data_get_irq_chip_data(d);
  68. return hip04_data->dist_base;
  69. }
  70. static inline void __iomem *hip04_cpu_base(struct irq_data *d)
  71. {
  72. struct hip04_irq_data *hip04_data = irq_data_get_irq_chip_data(d);
  73. return hip04_data->cpu_base;
  74. }
  75. static inline unsigned int hip04_irq(struct irq_data *d)
  76. {
  77. return d->hwirq;
  78. }
  79. /*
  80. * Routines to acknowledge, disable and enable interrupts
  81. */
  82. static void hip04_mask_irq(struct irq_data *d)
  83. {
  84. u32 mask = 1 << (hip04_irq(d) % 32);
  85. raw_spin_lock(&irq_controller_lock);
  86. writel_relaxed(mask, hip04_dist_base(d) + GIC_DIST_ENABLE_CLEAR +
  87. (hip04_irq(d) / 32) * 4);
  88. raw_spin_unlock(&irq_controller_lock);
  89. }
  90. static void hip04_unmask_irq(struct irq_data *d)
  91. {
  92. u32 mask = 1 << (hip04_irq(d) % 32);
  93. raw_spin_lock(&irq_controller_lock);
  94. writel_relaxed(mask, hip04_dist_base(d) + GIC_DIST_ENABLE_SET +
  95. (hip04_irq(d) / 32) * 4);
  96. raw_spin_unlock(&irq_controller_lock);
  97. }
  98. static void hip04_eoi_irq(struct irq_data *d)
  99. {
  100. writel_relaxed(hip04_irq(d), hip04_cpu_base(d) + GIC_CPU_EOI);
  101. }
  102. static int hip04_irq_set_type(struct irq_data *d, unsigned int type)
  103. {
  104. void __iomem *base = hip04_dist_base(d);
  105. unsigned int irq = hip04_irq(d);
  106. int ret;
  107. /* Interrupt configuration for SGIs can't be changed */
  108. if (irq < 16)
  109. return -EINVAL;
  110. /* SPIs have restrictions on the supported types */
  111. if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  112. type != IRQ_TYPE_EDGE_RISING)
  113. return -EINVAL;
  114. raw_spin_lock(&irq_controller_lock);
  115. ret = gic_configure_irq(irq, type, base, NULL);
  116. raw_spin_unlock(&irq_controller_lock);
  117. return ret;
  118. }
  119. #ifdef CONFIG_SMP
  120. static int hip04_irq_set_affinity(struct irq_data *d,
  121. const struct cpumask *mask_val,
  122. bool force)
  123. {
  124. void __iomem *reg;
  125. unsigned int cpu, shift = (hip04_irq(d) % 2) * 16;
  126. u32 val, mask, bit;
  127. if (!force)
  128. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  129. else
  130. cpu = cpumask_first(mask_val);
  131. if (cpu >= NR_HIP04_CPU_IF || cpu >= nr_cpu_ids)
  132. return -EINVAL;
  133. raw_spin_lock(&irq_controller_lock);
  134. reg = hip04_dist_base(d) + GIC_DIST_TARGET + ((hip04_irq(d) * 2) & ~3);
  135. mask = 0xffff << shift;
  136. bit = hip04_cpu_map[cpu] << shift;
  137. val = readl_relaxed(reg) & ~mask;
  138. writel_relaxed(val | bit, reg);
  139. raw_spin_unlock(&irq_controller_lock);
  140. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  141. return IRQ_SET_MASK_OK;
  142. }
  143. #endif
  144. static void __exception_irq_entry hip04_handle_irq(struct pt_regs *regs)
  145. {
  146. u32 irqstat, irqnr;
  147. void __iomem *cpu_base = hip04_data.cpu_base;
  148. do {
  149. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  150. irqnr = irqstat & GICC_IAR_INT_ID_MASK;
  151. if (likely(irqnr > 15 && irqnr <= HIP04_MAX_IRQS)) {
  152. handle_domain_irq(hip04_data.domain, irqnr, regs);
  153. continue;
  154. }
  155. if (irqnr < 16) {
  156. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  157. #ifdef CONFIG_SMP
  158. handle_IPI(irqnr, regs);
  159. #endif
  160. continue;
  161. }
  162. break;
  163. } while (1);
  164. }
  165. static struct irq_chip hip04_irq_chip = {
  166. .name = "HIP04 INTC",
  167. .irq_mask = hip04_mask_irq,
  168. .irq_unmask = hip04_unmask_irq,
  169. .irq_eoi = hip04_eoi_irq,
  170. .irq_set_type = hip04_irq_set_type,
  171. #ifdef CONFIG_SMP
  172. .irq_set_affinity = hip04_irq_set_affinity,
  173. #endif
  174. .flags = IRQCHIP_SET_TYPE_MASKED |
  175. IRQCHIP_SKIP_SET_WAKE |
  176. IRQCHIP_MASK_ON_SUSPEND,
  177. };
  178. static u16 hip04_get_cpumask(struct hip04_irq_data *intc)
  179. {
  180. void __iomem *base = intc->dist_base;
  181. u32 mask, i;
  182. for (i = mask = 0; i < 32; i += 2) {
  183. mask = readl_relaxed(base + GIC_DIST_TARGET + i * 2);
  184. mask |= mask >> 16;
  185. if (mask)
  186. break;
  187. }
  188. if (!mask)
  189. pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
  190. return mask;
  191. }
  192. static void __init hip04_irq_dist_init(struct hip04_irq_data *intc)
  193. {
  194. unsigned int i;
  195. u32 cpumask;
  196. unsigned int nr_irqs = intc->nr_irqs;
  197. void __iomem *base = intc->dist_base;
  198. writel_relaxed(0, base + GIC_DIST_CTRL);
  199. /*
  200. * Set all global interrupts to this CPU only.
  201. */
  202. cpumask = hip04_get_cpumask(intc);
  203. cpumask |= cpumask << 16;
  204. for (i = 32; i < nr_irqs; i += 2)
  205. writel_relaxed(cpumask, base + GIC_DIST_TARGET + ((i * 2) & ~3));
  206. gic_dist_config(base, nr_irqs, NULL);
  207. writel_relaxed(1, base + GIC_DIST_CTRL);
  208. }
  209. static void hip04_irq_cpu_init(struct hip04_irq_data *intc)
  210. {
  211. void __iomem *dist_base = intc->dist_base;
  212. void __iomem *base = intc->cpu_base;
  213. unsigned int cpu_mask, cpu = smp_processor_id();
  214. int i;
  215. /*
  216. * Get what the GIC says our CPU mask is.
  217. */
  218. BUG_ON(cpu >= NR_HIP04_CPU_IF);
  219. cpu_mask = hip04_get_cpumask(intc);
  220. hip04_cpu_map[cpu] = cpu_mask;
  221. /*
  222. * Clear our mask from the other map entries in case they're
  223. * still undefined.
  224. */
  225. for (i = 0; i < NR_HIP04_CPU_IF; i++)
  226. if (i != cpu)
  227. hip04_cpu_map[i] &= ~cpu_mask;
  228. gic_cpu_config(dist_base, NULL);
  229. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  230. writel_relaxed(1, base + GIC_CPU_CTRL);
  231. }
  232. #ifdef CONFIG_SMP
  233. static void hip04_raise_softirq(const struct cpumask *mask, unsigned int irq)
  234. {
  235. int cpu;
  236. unsigned long flags, map = 0;
  237. raw_spin_lock_irqsave(&irq_controller_lock, flags);
  238. /* Convert our logical CPU mask into a physical one. */
  239. for_each_cpu(cpu, mask)
  240. map |= hip04_cpu_map[cpu];
  241. /*
  242. * Ensure that stores to Normal memory are visible to the
  243. * other CPUs before they observe us issuing the IPI.
  244. */
  245. dmb(ishst);
  246. /* this always happens on GIC0 */
  247. writel_relaxed(map << 8 | irq, hip04_data.dist_base + GIC_DIST_SOFTINT);
  248. raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
  249. }
  250. #endif
  251. static int hip04_irq_domain_map(struct irq_domain *d, unsigned int irq,
  252. irq_hw_number_t hw)
  253. {
  254. if (hw < 32) {
  255. irq_set_percpu_devid(irq);
  256. irq_set_chip_and_handler(irq, &hip04_irq_chip,
  257. handle_percpu_devid_irq);
  258. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  259. } else {
  260. irq_set_chip_and_handler(irq, &hip04_irq_chip,
  261. handle_fasteoi_irq);
  262. irq_set_probe(irq);
  263. irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq)));
  264. }
  265. irq_set_chip_data(irq, d->host_data);
  266. return 0;
  267. }
  268. static int hip04_irq_domain_xlate(struct irq_domain *d,
  269. struct device_node *controller,
  270. const u32 *intspec, unsigned int intsize,
  271. unsigned long *out_hwirq,
  272. unsigned int *out_type)
  273. {
  274. unsigned long ret = 0;
  275. if (irq_domain_get_of_node(d) != controller)
  276. return -EINVAL;
  277. if (intsize < 3)
  278. return -EINVAL;
  279. /* Get the interrupt number and add 16 to skip over SGIs */
  280. *out_hwirq = intspec[1] + 16;
  281. /* For SPIs, we need to add 16 more to get the irq ID number */
  282. if (!intspec[0])
  283. *out_hwirq += 16;
  284. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  285. return ret;
  286. }
  287. static int hip04_irq_starting_cpu(unsigned int cpu)
  288. {
  289. hip04_irq_cpu_init(&hip04_data);
  290. return 0;
  291. }
  292. static const struct irq_domain_ops hip04_irq_domain_ops = {
  293. .map = hip04_irq_domain_map,
  294. .xlate = hip04_irq_domain_xlate,
  295. };
  296. static int __init
  297. hip04_of_init(struct device_node *node, struct device_node *parent)
  298. {
  299. irq_hw_number_t hwirq_base = 16;
  300. int nr_irqs, irq_base, i;
  301. if (WARN_ON(!node))
  302. return -ENODEV;
  303. hip04_data.dist_base = of_iomap(node, 0);
  304. WARN(!hip04_data.dist_base, "fail to map hip04 intc dist registers\n");
  305. hip04_data.cpu_base = of_iomap(node, 1);
  306. WARN(!hip04_data.cpu_base, "unable to map hip04 intc cpu registers\n");
  307. /*
  308. * Initialize the CPU interface map to all CPUs.
  309. * It will be refined as each CPU probes its ID.
  310. */
  311. for (i = 0; i < NR_HIP04_CPU_IF; i++)
  312. hip04_cpu_map[i] = 0xffff;
  313. /*
  314. * Find out how many interrupts are supported.
  315. * The HIP04 INTC only supports up to 510 interrupt sources.
  316. */
  317. nr_irqs = readl_relaxed(hip04_data.dist_base + GIC_DIST_CTR) & 0x1f;
  318. nr_irqs = (nr_irqs + 1) * 32;
  319. if (nr_irqs > HIP04_MAX_IRQS)
  320. nr_irqs = HIP04_MAX_IRQS;
  321. hip04_data.nr_irqs = nr_irqs;
  322. nr_irqs -= hwirq_base; /* calculate # of irqs to allocate */
  323. irq_base = irq_alloc_descs(-1, hwirq_base, nr_irqs, numa_node_id());
  324. if (irq_base < 0) {
  325. pr_err("failed to allocate IRQ numbers\n");
  326. return -EINVAL;
  327. }
  328. hip04_data.domain = irq_domain_add_legacy(node, nr_irqs, irq_base,
  329. hwirq_base,
  330. &hip04_irq_domain_ops,
  331. &hip04_data);
  332. if (WARN_ON(!hip04_data.domain))
  333. return -EINVAL;
  334. #ifdef CONFIG_SMP
  335. set_smp_cross_call(hip04_raise_softirq);
  336. #endif
  337. set_handle_irq(hip04_handle_irq);
  338. hip04_irq_dist_init(&hip04_data);
  339. cpuhp_setup_state(CPUHP_AP_IRQ_HIP04_STARTING, "irqchip/hip04:starting",
  340. hip04_irq_starting_cpu, NULL);
  341. return 0;
  342. }
  343. IRQCHIP_DECLARE(hip04_intc, "hisilicon,hip04-intc", hip04_of_init);