gic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. struct gic_chip_data {
  36. unsigned int irq_offset;
  37. void __iomem *dist_base;
  38. void __iomem *cpu_base;
  39. };
  40. #ifndef MAX_GIC_NR
  41. #define MAX_GIC_NR 1
  42. #endif
  43. static struct gic_chip_data gic_data[MAX_GIC_NR];
  44. static inline void __iomem *gic_dist_base(unsigned int irq)
  45. {
  46. struct gic_chip_data *gic_data = get_irq_chip_data(irq);
  47. return gic_data->dist_base;
  48. }
  49. static inline void __iomem *gic_cpu_base(unsigned int irq)
  50. {
  51. struct gic_chip_data *gic_data = get_irq_chip_data(irq);
  52. return gic_data->cpu_base;
  53. }
  54. static inline unsigned int gic_irq(unsigned int irq)
  55. {
  56. struct gic_chip_data *gic_data = get_irq_chip_data(irq);
  57. return irq - gic_data->irq_offset;
  58. }
  59. /*
  60. * Routines to acknowledge, disable and enable interrupts
  61. */
  62. static void gic_ack_irq(unsigned int irq)
  63. {
  64. spin_lock(&irq_controller_lock);
  65. writel(gic_irq(irq), gic_cpu_base(irq) + GIC_CPU_EOI);
  66. spin_unlock(&irq_controller_lock);
  67. }
  68. static void gic_mask_irq(unsigned int irq)
  69. {
  70. u32 mask = 1 << (irq % 32);
  71. spin_lock(&irq_controller_lock);
  72. writel(mask, gic_dist_base(irq) + GIC_DIST_ENABLE_CLEAR + (gic_irq(irq) / 32) * 4);
  73. spin_unlock(&irq_controller_lock);
  74. }
  75. static void gic_unmask_irq(unsigned int irq)
  76. {
  77. u32 mask = 1 << (irq % 32);
  78. spin_lock(&irq_controller_lock);
  79. writel(mask, gic_dist_base(irq) + GIC_DIST_ENABLE_SET + (gic_irq(irq) / 32) * 4);
  80. spin_unlock(&irq_controller_lock);
  81. }
  82. static int gic_set_type(unsigned int irq, unsigned int type)
  83. {
  84. void __iomem *base = gic_dist_base(irq);
  85. unsigned int gicirq = gic_irq(irq);
  86. u32 enablemask = 1 << (gicirq % 32);
  87. u32 enableoff = (gicirq / 32) * 4;
  88. u32 confmask = 0x2 << ((gicirq % 16) * 2);
  89. u32 confoff = (gicirq / 16) * 4;
  90. bool enabled = false;
  91. u32 val;
  92. /* Interrupt configuration for SGIs can't be changed */
  93. if (gicirq < 16)
  94. return -EINVAL;
  95. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  96. return -EINVAL;
  97. spin_lock(&irq_controller_lock);
  98. val = readl(base + GIC_DIST_CONFIG + confoff);
  99. if (type == IRQ_TYPE_LEVEL_HIGH)
  100. val &= ~confmask;
  101. else if (type == IRQ_TYPE_EDGE_RISING)
  102. val |= confmask;
  103. /*
  104. * As recommended by the spec, disable the interrupt before changing
  105. * the configuration
  106. */
  107. if (readl(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
  108. writel(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
  109. enabled = true;
  110. }
  111. writel(val, base + GIC_DIST_CONFIG + confoff);
  112. if (enabled)
  113. writel(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
  114. spin_unlock(&irq_controller_lock);
  115. return 0;
  116. }
  117. #ifdef CONFIG_SMP
  118. static int gic_set_cpu(unsigned int irq, const struct cpumask *mask_val)
  119. {
  120. void __iomem *reg = gic_dist_base(irq) + GIC_DIST_TARGET + (gic_irq(irq) & ~3);
  121. unsigned int shift = (irq % 4) * 8;
  122. unsigned int cpu = cpumask_first(mask_val);
  123. u32 val;
  124. spin_lock(&irq_controller_lock);
  125. irq_desc[irq].node = cpu;
  126. val = readl(reg) & ~(0xff << shift);
  127. val |= 1 << (cpu + shift);
  128. writel(val, reg);
  129. spin_unlock(&irq_controller_lock);
  130. return 0;
  131. }
  132. #endif
  133. static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
  134. {
  135. struct gic_chip_data *chip_data = get_irq_data(irq);
  136. struct irq_chip *chip = get_irq_chip(irq);
  137. unsigned int cascade_irq, gic_irq;
  138. unsigned long status;
  139. /* primary controller ack'ing */
  140. chip->ack(irq);
  141. spin_lock(&irq_controller_lock);
  142. status = readl(chip_data->cpu_base + GIC_CPU_INTACK);
  143. spin_unlock(&irq_controller_lock);
  144. gic_irq = (status & 0x3ff);
  145. if (gic_irq == 1023)
  146. goto out;
  147. cascade_irq = gic_irq + chip_data->irq_offset;
  148. if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
  149. do_bad_IRQ(cascade_irq, desc);
  150. else
  151. generic_handle_irq(cascade_irq);
  152. out:
  153. /* primary controller unmasking */
  154. chip->unmask(irq);
  155. }
  156. static struct irq_chip gic_chip = {
  157. .name = "GIC",
  158. .ack = gic_ack_irq,
  159. .mask = gic_mask_irq,
  160. .unmask = gic_unmask_irq,
  161. .set_type = gic_set_type,
  162. #ifdef CONFIG_SMP
  163. .set_affinity = gic_set_cpu,
  164. #endif
  165. };
  166. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  167. {
  168. if (gic_nr >= MAX_GIC_NR)
  169. BUG();
  170. if (set_irq_data(irq, &gic_data[gic_nr]) != 0)
  171. BUG();
  172. set_irq_chained_handler(irq, gic_handle_cascade_irq);
  173. }
  174. void __init gic_dist_init(unsigned int gic_nr, void __iomem *base,
  175. unsigned int irq_start)
  176. {
  177. unsigned int max_irq, i;
  178. u32 cpumask = 1 << smp_processor_id();
  179. if (gic_nr >= MAX_GIC_NR)
  180. BUG();
  181. cpumask |= cpumask << 8;
  182. cpumask |= cpumask << 16;
  183. gic_data[gic_nr].dist_base = base;
  184. gic_data[gic_nr].irq_offset = (irq_start - 1) & ~31;
  185. writel(0, base + GIC_DIST_CTRL);
  186. /*
  187. * Find out how many interrupts are supported.
  188. */
  189. max_irq = readl(base + GIC_DIST_CTR) & 0x1f;
  190. max_irq = (max_irq + 1) * 32;
  191. /*
  192. * The GIC only supports up to 1020 interrupt sources.
  193. * Limit this to either the architected maximum, or the
  194. * platform maximum.
  195. */
  196. if (max_irq > max(1020, NR_IRQS))
  197. max_irq = max(1020, NR_IRQS);
  198. /*
  199. * Set all global interrupts to be level triggered, active low.
  200. */
  201. for (i = 32; i < max_irq; i += 16)
  202. writel(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  203. /*
  204. * Set all global interrupts to this CPU only.
  205. */
  206. for (i = 32; i < max_irq; i += 4)
  207. writel(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  208. /*
  209. * Set priority on all global interrupts.
  210. */
  211. for (i = 32; i < max_irq; i += 4)
  212. writel(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  213. /*
  214. * Disable all interrupts. Leave the PPI and SGIs alone
  215. * as these enables are banked registers.
  216. */
  217. for (i = 32; i < max_irq; i += 32)
  218. writel(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  219. /*
  220. * Setup the Linux IRQ subsystem.
  221. */
  222. for (i = irq_start; i < gic_data[gic_nr].irq_offset + max_irq; i++) {
  223. set_irq_chip(i, &gic_chip);
  224. set_irq_chip_data(i, &gic_data[gic_nr]);
  225. set_irq_handler(i, handle_level_irq);
  226. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  227. }
  228. writel(1, base + GIC_DIST_CTRL);
  229. }
  230. void __cpuinit gic_cpu_init(unsigned int gic_nr, void __iomem *base)
  231. {
  232. void __iomem *dist_base;
  233. int i;
  234. if (gic_nr >= MAX_GIC_NR)
  235. BUG();
  236. dist_base = gic_data[gic_nr].dist_base;
  237. BUG_ON(!dist_base);
  238. gic_data[gic_nr].cpu_base = base;
  239. /*
  240. * Deal with the banked PPI and SGI interrupts - disable all
  241. * PPI interrupts, ensure all SGI interrupts are enabled.
  242. */
  243. writel(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
  244. writel(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
  245. /*
  246. * Set priority on PPI and SGI interrupts
  247. */
  248. for (i = 0; i < 32; i += 4)
  249. writel(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
  250. writel(0xf0, base + GIC_CPU_PRIMASK);
  251. writel(1, base + GIC_CPU_CTRL);
  252. }
  253. #ifdef CONFIG_SMP
  254. void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  255. {
  256. unsigned long map = *cpus_addr(*mask);
  257. /* this always happens on GIC0 */
  258. writel(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
  259. }
  260. #endif