gic.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.
  18. *
  19. * Note that IRQs 0-31 are special - they are local to each CPU.
  20. * As such, the enable set/clear, pending set/clear and active bit
  21. * registers are banked per-cpu for these sources.
  22. */
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/list.h>
  26. #include <linux/smp.h>
  27. #include <linux/cpumask.h>
  28. #include <asm/irq.h>
  29. #include <asm/io.h>
  30. #include <asm/mach/irq.h>
  31. #include <asm/hardware/gic.h>
  32. static void __iomem *gic_dist_base;
  33. static void __iomem *gic_cpu_base;
  34. /*
  35. * Routines to acknowledge, disable and enable interrupts
  36. *
  37. * Linux assumes that when we're done with an interrupt we need to
  38. * unmask it, in the same way we need to unmask an interrupt when
  39. * we first enable it.
  40. *
  41. * The GIC has a seperate notion of "end of interrupt" to re-enable
  42. * an interrupt after handling, in order to support hardware
  43. * prioritisation.
  44. *
  45. * We can make the GIC behave in the way that Linux expects by making
  46. * our "acknowledge" routine disable the interrupt, then mark it as
  47. * complete.
  48. */
  49. static void gic_ack_irq(unsigned int irq)
  50. {
  51. u32 mask = 1 << (irq % 32);
  52. writel(mask, gic_dist_base + GIC_DIST_ENABLE_CLEAR + (irq / 32) * 4);
  53. writel(irq, gic_cpu_base + GIC_CPU_EOI);
  54. }
  55. static void gic_mask_irq(unsigned int irq)
  56. {
  57. u32 mask = 1 << (irq % 32);
  58. writel(mask, gic_dist_base + GIC_DIST_ENABLE_CLEAR + (irq / 32) * 4);
  59. }
  60. static void gic_unmask_irq(unsigned int irq)
  61. {
  62. u32 mask = 1 << (irq % 32);
  63. writel(mask, gic_dist_base + GIC_DIST_ENABLE_SET + (irq / 32) * 4);
  64. }
  65. static void gic_set_cpu(struct irqdesc *desc, unsigned int irq, unsigned int cpu)
  66. {
  67. void __iomem *reg = gic_dist_base + GIC_DIST_TARGET + (irq & ~3);
  68. unsigned int shift = (irq % 4) * 8;
  69. u32 val;
  70. val = readl(reg) & ~(0xff << shift);
  71. val |= 1 << (cpu + shift);
  72. writel(val, reg);
  73. }
  74. static struct irqchip gic_chip = {
  75. .ack = gic_ack_irq,
  76. .mask = gic_mask_irq,
  77. .unmask = gic_unmask_irq,
  78. #ifdef CONFIG_SMP
  79. .set_cpu = gic_set_cpu,
  80. #endif
  81. };
  82. void __init gic_dist_init(void __iomem *base)
  83. {
  84. unsigned int max_irq, i;
  85. u32 cpumask = 1 << smp_processor_id();
  86. cpumask |= cpumask << 8;
  87. cpumask |= cpumask << 16;
  88. gic_dist_base = base;
  89. writel(0, base + GIC_DIST_CTRL);
  90. /*
  91. * Find out how many interrupts are supported.
  92. */
  93. max_irq = readl(base + GIC_DIST_CTR) & 0x1f;
  94. max_irq = (max_irq + 1) * 32;
  95. /*
  96. * The GIC only supports up to 1020 interrupt sources.
  97. * Limit this to either the architected maximum, or the
  98. * platform maximum.
  99. */
  100. if (max_irq > max(1020, NR_IRQS))
  101. max_irq = max(1020, NR_IRQS);
  102. /*
  103. * Set all global interrupts to be level triggered, active low.
  104. */
  105. for (i = 32; i < max_irq; i += 16)
  106. writel(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  107. /*
  108. * Set all global interrupts to this CPU only.
  109. */
  110. for (i = 32; i < max_irq; i += 4)
  111. writel(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  112. /*
  113. * Set priority on all interrupts.
  114. */
  115. for (i = 0; i < max_irq; i += 4)
  116. writel(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  117. /*
  118. * Disable all interrupts.
  119. */
  120. for (i = 0; i < max_irq; i += 32)
  121. writel(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  122. /*
  123. * Setup the Linux IRQ subsystem.
  124. */
  125. for (i = 29; i < max_irq; i++) {
  126. set_irq_chip(i, &gic_chip);
  127. set_irq_handler(i, do_level_IRQ);
  128. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  129. }
  130. writel(1, base + GIC_DIST_CTRL);
  131. }
  132. void __cpuinit gic_cpu_init(void __iomem *base)
  133. {
  134. gic_cpu_base = base;
  135. writel(0xf0, base + GIC_CPU_PRIMASK);
  136. writel(1, base + GIC_CPU_CTRL);
  137. }
  138. #ifdef CONFIG_SMP
  139. void gic_raise_softirq(cpumask_t cpumask, unsigned int irq)
  140. {
  141. unsigned long map = *cpus_addr(cpumask);
  142. writel(map << 16 | irq, gic_dist_base + GIC_DIST_SOFTINT);
  143. }
  144. #endif