irq-hip04.c 11 KB

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