irq-hip04.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/arm-gic.h>
  44. #include <asm/irq.h>
  45. #include <asm/exception.h>
  46. #include <asm/smp_plat.h>
  47. #include "irq-gic-common.h"
  48. #include "irqchip.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. /* Interrupt configuration for SGIs can't be changed */
  107. if (irq < 16)
  108. return -EINVAL;
  109. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  110. return -EINVAL;
  111. raw_spin_lock(&irq_controller_lock);
  112. gic_configure_irq(irq, type, base, NULL);
  113. raw_spin_unlock(&irq_controller_lock);
  114. return 0;
  115. }
  116. #ifdef CONFIG_SMP
  117. static int hip04_irq_set_affinity(struct irq_data *d,
  118. const struct cpumask *mask_val,
  119. bool force)
  120. {
  121. void __iomem *reg;
  122. unsigned int cpu, shift = (hip04_irq(d) % 2) * 16;
  123. u32 val, mask, bit;
  124. if (!force)
  125. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  126. else
  127. cpu = cpumask_first(mask_val);
  128. if (cpu >= NR_HIP04_CPU_IF || cpu >= nr_cpu_ids)
  129. return -EINVAL;
  130. raw_spin_lock(&irq_controller_lock);
  131. reg = hip04_dist_base(d) + GIC_DIST_TARGET + ((hip04_irq(d) * 2) & ~3);
  132. mask = 0xffff << shift;
  133. bit = hip04_cpu_map[cpu] << shift;
  134. val = readl_relaxed(reg) & ~mask;
  135. writel_relaxed(val | bit, reg);
  136. raw_spin_unlock(&irq_controller_lock);
  137. return IRQ_SET_MASK_OK;
  138. }
  139. #endif
  140. static void __exception_irq_entry hip04_handle_irq(struct pt_regs *regs)
  141. {
  142. u32 irqstat, irqnr;
  143. void __iomem *cpu_base = hip04_data.cpu_base;
  144. do {
  145. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  146. irqnr = irqstat & GICC_IAR_INT_ID_MASK;
  147. if (likely(irqnr > 15 && irqnr <= HIP04_MAX_IRQS)) {
  148. handle_domain_irq(hip04_data.domain, irqnr, regs);
  149. continue;
  150. }
  151. if (irqnr < 16) {
  152. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  153. #ifdef CONFIG_SMP
  154. handle_IPI(irqnr, regs);
  155. #endif
  156. continue;
  157. }
  158. break;
  159. } while (1);
  160. }
  161. static struct irq_chip hip04_irq_chip = {
  162. .name = "HIP04 INTC",
  163. .irq_mask = hip04_mask_irq,
  164. .irq_unmask = hip04_unmask_irq,
  165. .irq_eoi = hip04_eoi_irq,
  166. .irq_set_type = hip04_irq_set_type,
  167. #ifdef CONFIG_SMP
  168. .irq_set_affinity = hip04_irq_set_affinity,
  169. #endif
  170. };
  171. static u16 hip04_get_cpumask(struct hip04_irq_data *intc)
  172. {
  173. void __iomem *base = intc->dist_base;
  174. u32 mask, i;
  175. for (i = mask = 0; i < 32; i += 2) {
  176. mask = readl_relaxed(base + GIC_DIST_TARGET + i * 2);
  177. mask |= mask >> 16;
  178. if (mask)
  179. break;
  180. }
  181. if (!mask)
  182. pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
  183. return mask;
  184. }
  185. static void __init hip04_irq_dist_init(struct hip04_irq_data *intc)
  186. {
  187. unsigned int i;
  188. u32 cpumask;
  189. unsigned int nr_irqs = intc->nr_irqs;
  190. void __iomem *base = intc->dist_base;
  191. writel_relaxed(0, base + GIC_DIST_CTRL);
  192. /*
  193. * Set all global interrupts to this CPU only.
  194. */
  195. cpumask = hip04_get_cpumask(intc);
  196. cpumask |= cpumask << 16;
  197. for (i = 32; i < nr_irqs; i += 2)
  198. writel_relaxed(cpumask, base + GIC_DIST_TARGET + ((i * 2) & ~3));
  199. gic_dist_config(base, nr_irqs, NULL);
  200. writel_relaxed(1, base + GIC_DIST_CTRL);
  201. }
  202. static void hip04_irq_cpu_init(struct hip04_irq_data *intc)
  203. {
  204. void __iomem *dist_base = intc->dist_base;
  205. void __iomem *base = intc->cpu_base;
  206. unsigned int cpu_mask, cpu = smp_processor_id();
  207. int i;
  208. /*
  209. * Get what the GIC says our CPU mask is.
  210. */
  211. BUG_ON(cpu >= NR_HIP04_CPU_IF);
  212. cpu_mask = hip04_get_cpumask(intc);
  213. hip04_cpu_map[cpu] = cpu_mask;
  214. /*
  215. * Clear our mask from the other map entries in case they're
  216. * still undefined.
  217. */
  218. for (i = 0; i < NR_HIP04_CPU_IF; i++)
  219. if (i != cpu)
  220. hip04_cpu_map[i] &= ~cpu_mask;
  221. gic_cpu_config(dist_base, NULL);
  222. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  223. writel_relaxed(1, base + GIC_CPU_CTRL);
  224. }
  225. #ifdef CONFIG_SMP
  226. static void hip04_raise_softirq(const struct cpumask *mask, unsigned int irq)
  227. {
  228. int cpu;
  229. unsigned long flags, map = 0;
  230. raw_spin_lock_irqsave(&irq_controller_lock, flags);
  231. /* Convert our logical CPU mask into a physical one. */
  232. for_each_cpu(cpu, mask)
  233. map |= hip04_cpu_map[cpu];
  234. /*
  235. * Ensure that stores to Normal memory are visible to the
  236. * other CPUs before they observe us issuing the IPI.
  237. */
  238. dmb(ishst);
  239. /* this always happens on GIC0 */
  240. writel_relaxed(map << 8 | irq, hip04_data.dist_base + GIC_DIST_SOFTINT);
  241. raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
  242. }
  243. #endif
  244. static int hip04_irq_domain_map(struct irq_domain *d, unsigned int irq,
  245. irq_hw_number_t hw)
  246. {
  247. if (hw < 32) {
  248. irq_set_percpu_devid(irq);
  249. irq_set_chip_and_handler(irq, &hip04_irq_chip,
  250. handle_percpu_devid_irq);
  251. set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
  252. } else {
  253. irq_set_chip_and_handler(irq, &hip04_irq_chip,
  254. handle_fasteoi_irq);
  255. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  256. }
  257. irq_set_chip_data(irq, d->host_data);
  258. return 0;
  259. }
  260. static int hip04_irq_domain_xlate(struct irq_domain *d,
  261. struct device_node *controller,
  262. const u32 *intspec, unsigned int intsize,
  263. unsigned long *out_hwirq,
  264. unsigned int *out_type)
  265. {
  266. unsigned long ret = 0;
  267. if (d->of_node != controller)
  268. return -EINVAL;
  269. if (intsize < 3)
  270. return -EINVAL;
  271. /* Get the interrupt number and add 16 to skip over SGIs */
  272. *out_hwirq = intspec[1] + 16;
  273. /* For SPIs, we need to add 16 more to get the irq ID number */
  274. if (!intspec[0])
  275. *out_hwirq += 16;
  276. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  277. return ret;
  278. }
  279. #ifdef CONFIG_SMP
  280. static int hip04_irq_secondary_init(struct notifier_block *nfb,
  281. unsigned long action,
  282. void *hcpu)
  283. {
  284. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  285. hip04_irq_cpu_init(&hip04_data);
  286. return NOTIFY_OK;
  287. }
  288. /*
  289. * Notifier for enabling the INTC CPU interface. Set an arbitrarily high
  290. * priority because the GIC needs to be up before the ARM generic timers.
  291. */
  292. static struct notifier_block hip04_irq_cpu_notifier = {
  293. .notifier_call = hip04_irq_secondary_init,
  294. .priority = 100,
  295. };
  296. #endif
  297. static const struct irq_domain_ops hip04_irq_domain_ops = {
  298. .map = hip04_irq_domain_map,
  299. .xlate = hip04_irq_domain_xlate,
  300. };
  301. static int __init
  302. hip04_of_init(struct device_node *node, struct device_node *parent)
  303. {
  304. irq_hw_number_t hwirq_base = 16;
  305. int nr_irqs, irq_base, i;
  306. if (WARN_ON(!node))
  307. return -ENODEV;
  308. hip04_data.dist_base = of_iomap(node, 0);
  309. WARN(!hip04_data.dist_base, "fail to map hip04 intc dist registers\n");
  310. hip04_data.cpu_base = of_iomap(node, 1);
  311. WARN(!hip04_data.cpu_base, "unable to map hip04 intc cpu registers\n");
  312. /*
  313. * Initialize the CPU interface map to all CPUs.
  314. * It will be refined as each CPU probes its ID.
  315. */
  316. for (i = 0; i < NR_HIP04_CPU_IF; i++)
  317. hip04_cpu_map[i] = 0xffff;
  318. /*
  319. * Find out how many interrupts are supported.
  320. * The HIP04 INTC only supports up to 510 interrupt sources.
  321. */
  322. nr_irqs = readl_relaxed(hip04_data.dist_base + GIC_DIST_CTR) & 0x1f;
  323. nr_irqs = (nr_irqs + 1) * 32;
  324. if (nr_irqs > HIP04_MAX_IRQS)
  325. nr_irqs = HIP04_MAX_IRQS;
  326. hip04_data.nr_irqs = nr_irqs;
  327. nr_irqs -= hwirq_base; /* calculate # of irqs to allocate */
  328. irq_base = irq_alloc_descs(-1, hwirq_base, nr_irqs, numa_node_id());
  329. if (IS_ERR_VALUE(irq_base)) {
  330. pr_err("failed to allocate IRQ numbers\n");
  331. return -EINVAL;
  332. }
  333. hip04_data.domain = irq_domain_add_legacy(node, nr_irqs, irq_base,
  334. hwirq_base,
  335. &hip04_irq_domain_ops,
  336. &hip04_data);
  337. if (WARN_ON(!hip04_data.domain))
  338. return -EINVAL;
  339. #ifdef CONFIG_SMP
  340. set_smp_cross_call(hip04_raise_softirq);
  341. register_cpu_notifier(&hip04_irq_cpu_notifier);
  342. #endif
  343. set_handle_irq(hip04_handle_irq);
  344. hip04_irq_dist_init(&hip04_data);
  345. hip04_irq_cpu_init(&hip04_data);
  346. return 0;
  347. }
  348. IRQCHIP_DECLARE(hip04_intc, "hisilicon,hip04-intc", hip04_of_init);