irq-hip04.c 11 KB

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