1
0

gic.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <assert.h>
  31. #include "gic.h"
  32. #include "gic_registers.h"
  33. #include "cortex_a.h"
  34. ////////////////////////////////////////////////////////////////////////////////
  35. // Prototypes
  36. ////////////////////////////////////////////////////////////////////////////////
  37. static inline gicd_t * gic_get_gicd(void);
  38. static inline gicc_t * gic_get_gicc(void);
  39. static inline uint32_t irq_get_register_offset(uint32_t irqID);
  40. static inline uint32_t irq_get_bit_offset(uint32_t irqID);
  41. static inline uint32_t irq_get_bit_mask(uint32_t irqID);
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // Code
  44. ////////////////////////////////////////////////////////////////////////////////
  45. static inline gicd_t * gic_get_gicd(void)
  46. {
  47. uint32_t base = get_arm_private_peripheral_base() + kGICDBaseOffset;
  48. return (gicd_t *)base;
  49. }
  50. static inline gicc_t * gic_get_gicc(void)
  51. {
  52. uint32_t base = get_arm_private_peripheral_base() + kGICCBaseOffset;
  53. return (gicc_t *)base;
  54. }
  55. static inline uint32_t irq_get_register_offset(uint32_t irqID)
  56. {
  57. return irqID / 32;
  58. }
  59. static inline uint32_t irq_get_bit_offset(uint32_t irqID)
  60. {
  61. return irqID & 0x1f;
  62. }
  63. static inline uint32_t irq_get_bit_mask(uint32_t irqID)
  64. {
  65. return 1 << irq_get_bit_offset(irqID);
  66. }
  67. void gic_enable(bool enableIt)
  68. {
  69. gicd_t * gicd = gic_get_gicd();
  70. if (enableIt)
  71. {
  72. // Enable both secure and non-secure.
  73. gicd->CTLR |= kBM_GICD_CTLR_EnableGrp0 | kBM_GICD_CTLR_EnableGrp1;
  74. }
  75. else
  76. {
  77. // Clear the enable bits.
  78. gicd->CTLR &= ~(kBM_GICD_CTLR_EnableGrp0 | kBM_GICD_CTLR_EnableGrp1);
  79. }
  80. }
  81. void gic_set_irq_security(uint32_t irqID, bool isSecure)
  82. {
  83. gicd_t * gicd = gic_get_gicd();
  84. uint32_t reg = irq_get_register_offset(irqID);
  85. uint32_t mask = irq_get_bit_mask(irqID);
  86. uint32_t value = gicd->IGROUPRn[reg];
  87. if (!isSecure)
  88. {
  89. value &= ~mask;
  90. }
  91. else
  92. {
  93. value |= mask;
  94. }
  95. gicd->IGROUPRn[reg] = value;
  96. }
  97. void gic_enable_irq(uint32_t irqID, bool isEnabled)
  98. {
  99. gicd_t * gicd = gic_get_gicd();
  100. uint32_t reg = irq_get_register_offset(irqID);
  101. uint32_t mask = irq_get_bit_mask(irqID);
  102. // Select set-enable or clear-enable register based on enable flag.
  103. if (isEnabled)
  104. {
  105. gicd->ISENABLERn[reg] = mask;
  106. }
  107. else
  108. {
  109. gicd->ICENABLERn[reg] = mask;
  110. }
  111. }
  112. void gic_set_irq_priority(uint32_t ID, uint32_t priority)
  113. {
  114. gicd_t * gicd = gic_get_gicd();
  115. // Update the priority register. The priority registers are byte accessible, and the register
  116. // struct has the priority registers as a byte array, so we can just index directly by the
  117. // interrupt ID.
  118. gicd->IPRIORITYRn[ID] = priority & 0xff;
  119. }
  120. void gic_set_cpu_target(uint32_t irqID, unsigned cpuNumber, bool enableIt)
  121. {
  122. // Make sure the CPU number is valid.
  123. assert(cpuNumber <= 7);
  124. gicd_t * gicd = gic_get_gicd();
  125. uint8_t cpuMask = 1 << cpuNumber;
  126. // Like the priority registers, the target registers are byte accessible, and the register
  127. // struct has the them as a byte array, so we can just index directly by the
  128. // interrupt ID.
  129. if (enableIt)
  130. {
  131. gicd->ITARGETSRn[irqID] |= (cpuMask & 0xff);
  132. }
  133. else
  134. {
  135. gicd->ITARGETSRn[irqID] &= ~(cpuMask & 0xff);
  136. }
  137. }
  138. void gic_send_sgi(uint32_t irqID, uint32_t target_list, uint32_t filter_list)
  139. {
  140. gicd_t * gicd = gic_get_gicd();
  141. gicd->SGIR = (filter_list << kBP_GICD_SGIR_TargetListFilter)
  142. | (target_list << kBP_GICD_SGIR_CPUTargetList)
  143. | (irqID & 0xf);
  144. }
  145. void gic_cpu_enable(bool enableIt)
  146. {
  147. gicc_t * gicc = gic_get_gicc();
  148. if (enableIt)
  149. {
  150. gicc->CTLR |= kBM_GICC_CTLR_EnableS | kBM_GICC_CTLR_EnableNS;
  151. }
  152. else
  153. {
  154. gicc->CTLR &= ~(kBM_GICC_CTLR_EnableS | kBM_GICC_CTLR_EnableNS);
  155. }
  156. }
  157. void gic_set_cpu_priority_mask(uint32_t priority)
  158. {
  159. gicc_t * gicc = gic_get_gicc();
  160. gicc->PMR = priority & 0xff;
  161. }
  162. uint32_t gic_read_irq_ack(void)
  163. {
  164. gicc_t * gicc = gic_get_gicc();
  165. return gicc->IAR;
  166. }
  167. void gic_write_end_of_irq(uint32_t irqID)
  168. {
  169. gicc_t * gicc = gic_get_gicc();
  170. gicc->EOIR = irqID;
  171. }
  172. void gic_init(void)
  173. {
  174. gicd_t * gicd = gic_get_gicd();
  175. // First disable the distributor.
  176. gic_enable(false);
  177. // Clear all pending interrupts.
  178. int i;
  179. for (i = 0; i < 32; ++i)
  180. {
  181. gicd->ICPENDRn[i] = 0xffffffff;
  182. }
  183. // Set all interrupts to secure.
  184. for (i = 0; i < 8; ++i)
  185. {
  186. gicd->IGROUPRn[i] = 0;
  187. }
  188. // Init the GIC CPU interface.
  189. gic_init_cpu();
  190. // Now enable the distributor.
  191. gic_enable(true);
  192. }
  193. void gic_init_cpu(void)
  194. {
  195. // Init the GIC CPU interface.
  196. gic_set_cpu_priority_mask(0xff);
  197. // Disable preemption.
  198. gicc_t * gicc = gic_get_gicc();
  199. gicc->BPR = 7;
  200. // Enable signaling the CPU.
  201. gic_cpu_enable(true);
  202. }
  203. ////////////////////////////////////////////////////////////////////////////////
  204. // EOF
  205. ////////////////////////////////////////////////////////////////////////////////