gic.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * linux/arch/arm/common/gic.c
  3. *
  4. * Copyright (C) 2002 ARM Limited, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Interrupt architecture for the GIC:
  11. *
  12. * o There is one Interrupt Distributor, which receives interrupts
  13. * from system devices and sends them to the Interrupt Controllers.
  14. *
  15. * o There is one CPU Interface per CPU, which sends interrupts sent
  16. * by the Distributor, and interrupts generated locally, to the
  17. * associated CPU. The base address of the CPU interface is usually
  18. * aliased so that the same address points to different chips depending
  19. * on the CPU it is accessed from.
  20. *
  21. * Note that IRQs 0-31 are special - they are local to each CPU.
  22. * As such, the enable set/clear, pending set/clear and active bit
  23. * registers are banked per-cpu for these sources.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/list.h>
  28. #include <linux/smp.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/io.h>
  31. #include <asm/irq.h>
  32. #include <asm/mach/irq.h>
  33. #include <asm/hardware/gic.h>
  34. static DEFINE_SPINLOCK(irq_controller_lock);
  35. /* Address of GIC 0 CPU interface */
  36. void __iomem *gic_cpu_base_addr __read_mostly;
  37. struct gic_chip_data {
  38. unsigned int irq_offset;
  39. void __iomem *dist_base;
  40. void __iomem *cpu_base;
  41. };
  42. /*
  43. * Supported arch specific GIC irq extension.
  44. * Default make them NULL.
  45. */
  46. struct irq_chip gic_arch_extn = {
  47. .irq_eoi = NULL,
  48. .irq_mask = NULL,
  49. .irq_unmask = NULL,
  50. .irq_retrigger = NULL,
  51. .irq_set_type = NULL,
  52. .irq_set_wake = NULL,
  53. };
  54. #ifndef MAX_GIC_NR
  55. #define MAX_GIC_NR 1
  56. #endif
  57. static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
  58. static inline void __iomem *gic_dist_base(struct irq_data *d)
  59. {
  60. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  61. return gic_data->dist_base;
  62. }
  63. static inline void __iomem *gic_cpu_base(struct irq_data *d)
  64. {
  65. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  66. return gic_data->cpu_base;
  67. }
  68. static inline unsigned int gic_irq(struct irq_data *d)
  69. {
  70. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  71. return d->irq - gic_data->irq_offset;
  72. }
  73. /*
  74. * Routines to acknowledge, disable and enable interrupts
  75. */
  76. static void gic_mask_irq(struct irq_data *d)
  77. {
  78. u32 mask = 1 << (d->irq % 32);
  79. spin_lock(&irq_controller_lock);
  80. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
  81. if (gic_arch_extn.irq_mask)
  82. gic_arch_extn.irq_mask(d);
  83. spin_unlock(&irq_controller_lock);
  84. }
  85. static void gic_unmask_irq(struct irq_data *d)
  86. {
  87. u32 mask = 1 << (d->irq % 32);
  88. spin_lock(&irq_controller_lock);
  89. if (gic_arch_extn.irq_unmask)
  90. gic_arch_extn.irq_unmask(d);
  91. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
  92. spin_unlock(&irq_controller_lock);
  93. }
  94. static void gic_eoi_irq(struct irq_data *d)
  95. {
  96. if (gic_arch_extn.irq_eoi) {
  97. spin_lock(&irq_controller_lock);
  98. gic_arch_extn.irq_eoi(d);
  99. spin_unlock(&irq_controller_lock);
  100. }
  101. writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
  102. }
  103. static int gic_set_type(struct irq_data *d, unsigned int type)
  104. {
  105. void __iomem *base = gic_dist_base(d);
  106. unsigned int gicirq = gic_irq(d);
  107. u32 enablemask = 1 << (gicirq % 32);
  108. u32 enableoff = (gicirq / 32) * 4;
  109. u32 confmask = 0x2 << ((gicirq % 16) * 2);
  110. u32 confoff = (gicirq / 16) * 4;
  111. bool enabled = false;
  112. u32 val;
  113. /* Interrupt configuration for SGIs can't be changed */
  114. if (gicirq < 16)
  115. return -EINVAL;
  116. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  117. return -EINVAL;
  118. spin_lock(&irq_controller_lock);
  119. if (gic_arch_extn.irq_set_type)
  120. gic_arch_extn.irq_set_type(d, type);
  121. val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
  122. if (type == IRQ_TYPE_LEVEL_HIGH)
  123. val &= ~confmask;
  124. else if (type == IRQ_TYPE_EDGE_RISING)
  125. val |= confmask;
  126. /*
  127. * As recommended by the spec, disable the interrupt before changing
  128. * the configuration
  129. */
  130. if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
  131. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
  132. enabled = true;
  133. }
  134. writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
  135. if (enabled)
  136. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
  137. spin_unlock(&irq_controller_lock);
  138. return 0;
  139. }
  140. static int gic_retrigger(struct irq_data *d)
  141. {
  142. if (gic_arch_extn.irq_retrigger)
  143. return gic_arch_extn.irq_retrigger(d);
  144. return -ENXIO;
  145. }
  146. #ifdef CONFIG_SMP
  147. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  148. bool force)
  149. {
  150. void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
  151. unsigned int shift = (d->irq % 4) * 8;
  152. unsigned int cpu = cpumask_first(mask_val);
  153. u32 val, mask, bit;
  154. if (cpu >= 8)
  155. return -EINVAL;
  156. mask = 0xff << shift;
  157. bit = 1 << (cpu + shift);
  158. spin_lock(&irq_controller_lock);
  159. d->node = cpu;
  160. val = readl_relaxed(reg) & ~mask;
  161. writel_relaxed(val | bit, reg);
  162. spin_unlock(&irq_controller_lock);
  163. return 0;
  164. }
  165. #endif
  166. #ifdef CONFIG_PM
  167. static int gic_set_wake(struct irq_data *d, unsigned int on)
  168. {
  169. int ret = -ENXIO;
  170. if (gic_arch_extn.irq_set_wake)
  171. ret = gic_arch_extn.irq_set_wake(d, on);
  172. return ret;
  173. }
  174. #else
  175. #define gic_set_wake NULL
  176. #endif
  177. static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
  178. {
  179. struct gic_chip_data *chip_data = irq_get_handler_data(irq);
  180. struct irq_chip *chip = irq_get_chip(irq);
  181. unsigned int cascade_irq, gic_irq;
  182. unsigned long status;
  183. chained_irq_enter(chip, desc);
  184. spin_lock(&irq_controller_lock);
  185. status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
  186. spin_unlock(&irq_controller_lock);
  187. gic_irq = (status & 0x3ff);
  188. if (gic_irq == 1023)
  189. goto out;
  190. cascade_irq = gic_irq + chip_data->irq_offset;
  191. if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
  192. do_bad_IRQ(cascade_irq, desc);
  193. else
  194. generic_handle_irq(cascade_irq);
  195. out:
  196. chained_irq_exit(chip, desc);
  197. }
  198. static struct irq_chip gic_chip = {
  199. .name = "GIC",
  200. .irq_mask = gic_mask_irq,
  201. .irq_unmask = gic_unmask_irq,
  202. .irq_eoi = gic_eoi_irq,
  203. .irq_set_type = gic_set_type,
  204. .irq_retrigger = gic_retrigger,
  205. #ifdef CONFIG_SMP
  206. .irq_set_affinity = gic_set_affinity,
  207. #endif
  208. .irq_set_wake = gic_set_wake,
  209. };
  210. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  211. {
  212. if (gic_nr >= MAX_GIC_NR)
  213. BUG();
  214. if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
  215. BUG();
  216. irq_set_chained_handler(irq, gic_handle_cascade_irq);
  217. }
  218. static void __init gic_dist_init(struct gic_chip_data *gic,
  219. unsigned int irq_start)
  220. {
  221. unsigned int gic_irqs, irq_limit, i;
  222. void __iomem *base = gic->dist_base;
  223. u32 cpumask = 1 << smp_processor_id();
  224. cpumask |= cpumask << 8;
  225. cpumask |= cpumask << 16;
  226. writel_relaxed(0, base + GIC_DIST_CTRL);
  227. /*
  228. * Find out how many interrupts are supported.
  229. * The GIC only supports up to 1020 interrupt sources.
  230. */
  231. gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f;
  232. gic_irqs = (gic_irqs + 1) * 32;
  233. if (gic_irqs > 1020)
  234. gic_irqs = 1020;
  235. /*
  236. * Set all global interrupts to be level triggered, active low.
  237. */
  238. for (i = 32; i < gic_irqs; i += 16)
  239. writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  240. /*
  241. * Set all global interrupts to this CPU only.
  242. */
  243. for (i = 32; i < gic_irqs; i += 4)
  244. writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  245. /*
  246. * Set priority on all global interrupts.
  247. */
  248. for (i = 32; i < gic_irqs; i += 4)
  249. writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  250. /*
  251. * Disable all interrupts. Leave the PPI and SGIs alone
  252. * as these enables are banked registers.
  253. */
  254. for (i = 32; i < gic_irqs; i += 32)
  255. writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  256. /*
  257. * Limit number of interrupts registered to the platform maximum
  258. */
  259. irq_limit = gic->irq_offset + gic_irqs;
  260. if (WARN_ON(irq_limit > NR_IRQS))
  261. irq_limit = NR_IRQS;
  262. /*
  263. * Setup the Linux IRQ subsystem.
  264. */
  265. for (i = irq_start; i < irq_limit; i++) {
  266. irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
  267. irq_set_chip_data(i, gic);
  268. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  269. }
  270. writel_relaxed(1, base + GIC_DIST_CTRL);
  271. }
  272. static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
  273. {
  274. void __iomem *dist_base = gic->dist_base;
  275. void __iomem *base = gic->cpu_base;
  276. int i;
  277. /*
  278. * Deal with the banked PPI and SGI interrupts - disable all
  279. * PPI interrupts, ensure all SGI interrupts are enabled.
  280. */
  281. writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
  282. writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
  283. /*
  284. * Set priority on PPI and SGI interrupts
  285. */
  286. for (i = 0; i < 32; i += 4)
  287. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
  288. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  289. writel_relaxed(1, base + GIC_CPU_CTRL);
  290. }
  291. void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
  292. void __iomem *dist_base, void __iomem *cpu_base)
  293. {
  294. struct gic_chip_data *gic;
  295. BUG_ON(gic_nr >= MAX_GIC_NR);
  296. gic = &gic_data[gic_nr];
  297. gic->dist_base = dist_base;
  298. gic->cpu_base = cpu_base;
  299. gic->irq_offset = (irq_start - 1) & ~31;
  300. if (gic_nr == 0)
  301. gic_cpu_base_addr = cpu_base;
  302. gic_dist_init(gic, irq_start);
  303. gic_cpu_init(gic);
  304. }
  305. void __cpuinit gic_secondary_init(unsigned int gic_nr)
  306. {
  307. BUG_ON(gic_nr >= MAX_GIC_NR);
  308. gic_cpu_init(&gic_data[gic_nr]);
  309. }
  310. void __cpuinit gic_enable_ppi(unsigned int irq)
  311. {
  312. unsigned long flags;
  313. local_irq_save(flags);
  314. irq_set_status_flags(irq, IRQ_NOPROBE);
  315. gic_unmask_irq(irq_get_irq_data(irq));
  316. local_irq_restore(flags);
  317. }
  318. #ifdef CONFIG_SMP
  319. void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  320. {
  321. unsigned long map = *cpus_addr(*mask);
  322. /*
  323. * Ensure that stores to Normal memory are visible to the
  324. * other CPUs before issuing the IPI.
  325. */
  326. dsb();
  327. /* this always happens on GIC0 */
  328. writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
  329. }
  330. #endif