irq-hip04.c 11 KB

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