mt_gic_v3.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2020-2024, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include "../drivers/arm/gic/v3/gicv3_private.h"
  10. #include <bl31/interrupt_mgmt.h>
  11. #include <common/bl_common.h>
  12. #include <common/debug.h>
  13. #include <lib/mtk_init/mtk_init.h>
  14. #include <mt_gic_v3.h>
  15. #include <mtk_plat_common.h>
  16. #include <plat/common/platform.h>
  17. #include <plat_private.h>
  18. #include <platform_def.h>
  19. #define SGI_MASK 0xffff
  20. uintptr_t rdistif_base_addrs[PLATFORM_CORE_COUNT];
  21. static uint32_t rdist_has_saved[PLATFORM_CORE_COUNT];
  22. /* we save and restore the GICv3 context on system suspend */
  23. gicv3_dist_ctx_t dist_ctx;
  24. static const interrupt_prop_t mtk_interrupt_props[] = {
  25. PLAT_MTK_G1S_IRQ_PROPS(INTR_GROUP1S)
  26. };
  27. static unsigned int mt_mpidr_to_core_pos(u_register_t mpidr)
  28. {
  29. return plat_core_pos_by_mpidr(mpidr);
  30. }
  31. gicv3_driver_data_t mt_gicv3_data = {
  32. .gicd_base = MT_GIC_BASE,
  33. .gicr_base = MT_GIC_RDIST_BASE,
  34. .interrupt_props = mtk_interrupt_props,
  35. .interrupt_props_num = ARRAY_SIZE(mtk_interrupt_props),
  36. .rdistif_num = PLATFORM_CORE_COUNT,
  37. .rdistif_base_addrs = rdistif_base_addrs,
  38. .mpidr_to_core_pos = mt_mpidr_to_core_pos,
  39. };
  40. struct gic_chip_data {
  41. /* All cores share the same configuration */
  42. unsigned int saved_ctlr;
  43. unsigned int saved_group;
  44. unsigned int saved_enable;
  45. unsigned int saved_conf0;
  46. unsigned int saved_conf1;
  47. unsigned int saved_grpmod;
  48. unsigned int saved_ispendr;
  49. unsigned int saved_isactiver;
  50. unsigned int saved_nsacr;
  51. /* Per-core sgi */
  52. unsigned int saved_sgi[PLATFORM_CORE_COUNT];
  53. /* Per-core priority */
  54. unsigned int saved_prio[PLATFORM_CORE_COUNT][GICR_NUM_REGS(IPRIORITYR)];
  55. };
  56. static struct gic_chip_data gic_data;
  57. void mt_gic_driver_init(void)
  58. {
  59. gicv3_driver_init(&mt_gicv3_data);
  60. }
  61. void mt_gic_set_pending(uint32_t irq)
  62. {
  63. gicv3_set_interrupt_pending(irq, plat_my_core_pos());
  64. }
  65. void mt_gic_distif_save(void)
  66. {
  67. gicv3_distif_save(&dist_ctx);
  68. }
  69. void mt_gic_distif_restore(void)
  70. {
  71. gicv3_distif_init_restore(&dist_ctx);
  72. }
  73. void mt_gic_rdistif_init(void)
  74. {
  75. unsigned int proc_num;
  76. unsigned int index;
  77. uintptr_t gicr_base;
  78. proc_num = plat_my_core_pos();
  79. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  80. /* set all SGI/PPI as non-secure GROUP1 by default */
  81. mmio_write_32(gicr_base + GICR_IGROUPR0, ~0U);
  82. mmio_write_32(gicr_base + GICR_IGRPMODR0, 0x0);
  83. /* setup the default PPI/SGI priorities */
  84. for (index = 0; index < TOTAL_PCPU_INTR_NUM; index += 4U)
  85. gicr_write_ipriorityr(gicr_base, index,
  86. GICD_IPRIORITYR_DEF_VAL);
  87. }
  88. void mt_gic_rdistif_save(void)
  89. {
  90. unsigned int i, proc_num;
  91. uintptr_t gicr_base;
  92. proc_num = plat_my_core_pos();
  93. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  94. /*
  95. * Wait for any write to GICR_CTLR to complete before trying to save any
  96. * state.
  97. */
  98. gicr_wait_for_pending_write(gicr_base);
  99. gic_data.saved_ctlr = mmio_read_32(gicr_base + GICR_CTLR);
  100. gic_data.saved_group = mmio_read_32(gicr_base + GICR_IGROUPR0);
  101. gic_data.saved_enable = mmio_read_32(gicr_base + GICR_ISENABLER0);
  102. gic_data.saved_conf0 = mmio_read_32(gicr_base + GICR_ICFGR0);
  103. gic_data.saved_conf1 = mmio_read_32(gicr_base + GICR_ICFGR1);
  104. gic_data.saved_grpmod = mmio_read_32(gicr_base + GICR_IGRPMODR0);
  105. gic_data.saved_ispendr = mmio_read_32(gicr_base + GICR_ISPENDR0);
  106. gic_data.saved_isactiver = mmio_read_32(gicr_base + GICR_ISACTIVER0);
  107. gic_data.saved_nsacr = mmio_read_32(gicr_base + GICR_NSACR);
  108. for (i = 0U; i < 8U; ++i)
  109. gic_data.saved_prio[proc_num][i] = gicr_ipriorityr_read(gicr_base, i);
  110. rdist_has_saved[proc_num] = 1;
  111. }
  112. void mt_gic_rdistif_restore(void)
  113. {
  114. unsigned int i, proc_num;
  115. uintptr_t gicr_base;
  116. proc_num = plat_my_core_pos();
  117. if (rdist_has_saved[proc_num] == 1) {
  118. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  119. mmio_write_32(gicr_base + GICR_IGROUPR0, gic_data.saved_group);
  120. mmio_write_32(gicr_base + GICR_IGRPMODR0, gic_data.saved_grpmod);
  121. mmio_write_32(gicr_base + GICR_NSACR, gic_data.saved_nsacr);
  122. mmio_write_32(gicr_base + GICR_ICFGR0, gic_data.saved_conf0);
  123. mmio_write_32(gicr_base + GICR_ICFGR1, gic_data.saved_conf1);
  124. for (i = 0U; i < 8U; ++i)
  125. gicr_ipriorityr_write(gicr_base, i, gic_data.saved_prio[proc_num][i]);
  126. mmio_write_32(gicr_base + GICR_ISPENDR0, gic_data.saved_ispendr);
  127. mmio_write_32(gicr_base + GICR_ISACTIVER0, gic_data.saved_isactiver);
  128. mmio_write_32(gicr_base + GICR_ISENABLER0, gic_data.saved_enable);
  129. mmio_write_32(gicr_base + GICR_CTLR, gic_data.saved_ctlr);
  130. gicr_wait_for_pending_write(gicr_base);
  131. }
  132. }
  133. void mt_gic_rdistif_restore_all(void)
  134. {
  135. unsigned int i, proc_num;
  136. uintptr_t gicr_base;
  137. for (proc_num = 0; proc_num < PLATFORM_CORE_COUNT; proc_num++) {
  138. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  139. mmio_write_32(gicr_base + GICR_IGROUPR0, gic_data.saved_group);
  140. mmio_write_32(gicr_base + GICR_IGRPMODR0, gic_data.saved_grpmod);
  141. mmio_write_32(gicr_base + GICR_NSACR, gic_data.saved_nsacr);
  142. mmio_write_32(gicr_base + GICR_ICFGR0, gic_data.saved_conf0);
  143. mmio_write_32(gicr_base + GICR_ICFGR1, gic_data.saved_conf1);
  144. for (i = 0U; i < 8U; ++i)
  145. gicr_ipriorityr_write(gicr_base, i, gic_data.saved_prio[proc_num][i]);
  146. mmio_write_32(gicr_base + GICR_ISPENDR0, gic_data.saved_ispendr);
  147. mmio_write_32(gicr_base + GICR_ISACTIVER0, gic_data.saved_isactiver);
  148. mmio_write_32(gicr_base + GICR_ISENABLER0, gic_data.saved_enable);
  149. mmio_write_32(gicr_base + GICR_CTLR, gic_data.saved_ctlr);
  150. gicr_wait_for_pending_write(gicr_base);
  151. }
  152. }
  153. void gic_sgi_save_all(void)
  154. {
  155. unsigned int proc_num;
  156. uintptr_t gicr_base;
  157. for (proc_num = 0; proc_num < PLATFORM_CORE_COUNT; proc_num++) {
  158. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  159. gic_data.saved_sgi[proc_num] =
  160. mmio_read_32(gicr_base + GICR_ISPENDR0) & SGI_MASK;
  161. }
  162. }
  163. void gic_sgi_restore_all(void)
  164. {
  165. unsigned int proc_num;
  166. uintptr_t gicr_base;
  167. for (proc_num = 0; proc_num < PLATFORM_CORE_COUNT; proc_num++) {
  168. gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
  169. mmio_write_32(gicr_base + GICR_ICPENDR0, SGI_MASK);
  170. mmio_write_32(gicr_base + GICR_ISPENDR0,
  171. gic_data.saved_sgi[proc_num] & SGI_MASK);
  172. }
  173. }
  174. void mt_gic_init(void)
  175. {
  176. gicv3_distif_init();
  177. gicv3_rdistif_init(plat_my_core_pos());
  178. gicv3_cpuif_enable(plat_my_core_pos());
  179. }
  180. uint32_t mt_irq_get_pending(uint32_t irq)
  181. {
  182. uint32_t val;
  183. val = mmio_read_32(BASE_GICD_BASE + GICD_ISPENDR +
  184. irq / 32 * 4);
  185. val = (val >> (irq % 32)) & 1U;
  186. return val;
  187. }
  188. void mt_irq_set_pending(uint32_t irq)
  189. {
  190. uint32_t bit = 1U << (irq % 32);
  191. mmio_write_32(BASE_GICD_BASE + GICD_ISPENDR +
  192. irq / 32 * 4, bit);
  193. }
  194. int mt_gic_one_init(void)
  195. {
  196. INFO("[%s] GIC initialization\n", __func__);
  197. /* Initialize the GIC driver, CPU and distributor interfaces */
  198. mt_gic_driver_init();
  199. mt_gic_init();
  200. return 0;
  201. }
  202. MTK_PLAT_SETUP_0_INIT(mt_gic_one_init);