gic.c 8.6 KB

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