developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 1 | /* |
developer | 508dc02 | 2019-06-25 15:33:48 +0800 | [diff] [blame] | 2 | * Copyright (c) 2019, MediaTek Inc. All rights reserved. |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
developer | 508dc02 | 2019-06-25 15:33:48 +0800 | [diff] [blame] | 7 | #include <assert.h> |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 8 | #include <common/bl_common.h> |
| 9 | #include <common/debug.h> |
| 10 | #include <drivers/arm/gicv3.h> |
| 11 | #include <bl31/interrupt_mgmt.h> |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 12 | #include <mt_gic_v3.h> |
| 13 | #include <mtk_plat_common.h> |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 14 | #include "../drivers/arm/gic/v3/gicv3_private.h" |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 15 | #include "plat_private.h" |
| 16 | #include <plat/common/platform.h> |
| 17 | #include <platform_def.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdio.h> |
| 20 | |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 21 | uintptr_t rdistif_base_addrs[PLATFORM_CORE_COUNT]; |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 22 | static uint32_t rdist_has_saved[PLATFORM_CORE_COUNT]; |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 23 | |
developer | 508dc02 | 2019-06-25 15:33:48 +0800 | [diff] [blame] | 24 | /* we save and restore the GICv3 context on system suspend */ |
developer | 508dc02 | 2019-06-25 15:33:48 +0800 | [diff] [blame] | 25 | gicv3_dist_ctx_t dist_ctx; |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 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 | |
| 32 | gicv3_driver_data_t mt_gicv3_data = { |
| 33 | .gicd_base = MT_GIC_BASE, |
| 34 | .gicr_base = MT_GIC_RDIST_BASE, |
| 35 | .rdistif_num = PLATFORM_CORE_COUNT, |
| 36 | .rdistif_base_addrs = rdistif_base_addrs, |
| 37 | .mpidr_to_core_pos = mt_mpidr_to_core_pos, |
| 38 | }; |
| 39 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 40 | struct gic_chip_data { |
| 41 | unsigned int saved_group; |
| 42 | unsigned int saved_enable; |
| 43 | unsigned int saved_conf0; |
| 44 | unsigned int saved_conf1; |
| 45 | unsigned int saved_grpmod; |
| 46 | }; |
| 47 | |
| 48 | static struct gic_chip_data gic_data; |
| 49 | |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 50 | void clear_sec_pol_ctl_en(void) |
| 51 | { |
| 52 | unsigned int i; |
| 53 | |
| 54 | /* total 19 polarity ctrl registers */ |
| 55 | for (i = 0; i <= NR_INT_POL_CTL - 1; i++) { |
| 56 | mmio_write_32((SEC_POL_CTL_EN0 + (i * 4)), 0); |
| 57 | } |
| 58 | dsb(); |
| 59 | } |
| 60 | |
| 61 | void mt_gic_driver_init(void) |
| 62 | { |
| 63 | gicv3_driver_init(&mt_gicv3_data); |
| 64 | } |
| 65 | |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 66 | void mt_gic_set_pending(uint32_t irq) |
| 67 | { |
| 68 | gicv3_set_interrupt_pending(irq, plat_my_core_pos()); |
| 69 | } |
| 70 | |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 71 | void mt_gic_cpuif_enable(void) |
| 72 | { |
| 73 | gicv3_cpuif_enable(plat_my_core_pos()); |
| 74 | } |
| 75 | |
| 76 | void mt_gic_cpuif_disable(void) |
| 77 | { |
| 78 | gicv3_cpuif_disable(plat_my_core_pos()); |
| 79 | } |
| 80 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 81 | void mt_gic_rdistif_init(void) |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 82 | { |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 83 | unsigned int proc_num; |
| 84 | unsigned int index; |
| 85 | uintptr_t gicr_base; |
| 86 | |
| 87 | proc_num = plat_my_core_pos(); |
| 88 | gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num]; |
| 89 | |
| 90 | /* set all SGI/PPI as non-secure GROUP1 by default */ |
| 91 | mmio_write_32(gicr_base + GICR_IGROUPR0, ~0U); |
| 92 | mmio_write_32(gicr_base + GICR_IGRPMODR0, 0x0); |
| 93 | |
| 94 | /* setup the default PPI/SGI priorities */ |
| 95 | for (index = 0; index < TOTAL_PCPU_INTR_NUM; index += 4U) |
| 96 | gicr_write_ipriorityr(gicr_base, index, |
| 97 | GICD_IPRIORITYR_DEF_VAL); |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 98 | } |
| 99 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 100 | void mt_gic_distif_save(void) |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 101 | { |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 102 | gicv3_distif_save(&dist_ctx); |
| 103 | } |
| 104 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 105 | void mt_gic_distif_restore(void) |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 106 | { |
| 107 | gicv3_distif_init_restore(&dist_ctx); |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 108 | } |
| 109 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 110 | void mt_gic_rdistif_save(void) |
| 111 | { |
| 112 | unsigned int proc_num; |
| 113 | uintptr_t gicr_base; |
| 114 | |
| 115 | proc_num = plat_my_core_pos(); |
| 116 | gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num]; |
| 117 | |
| 118 | gic_data.saved_group = mmio_read_32(gicr_base + GICR_IGROUPR0); |
| 119 | gic_data.saved_enable = mmio_read_32(gicr_base + GICR_ISENABLER0); |
| 120 | gic_data.saved_conf0 = mmio_read_32(gicr_base + GICR_ICFGR0); |
| 121 | gic_data.saved_conf1 = mmio_read_32(gicr_base + GICR_ICFGR1); |
| 122 | gic_data.saved_grpmod = mmio_read_32(gicr_base + GICR_IGRPMODR0); |
| 123 | |
| 124 | rdist_has_saved[proc_num] = 1; |
| 125 | } |
| 126 | |
| 127 | void mt_gic_rdistif_restore(void) |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 128 | { |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 129 | unsigned int proc_num; |
| 130 | uintptr_t gicr_base; |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 131 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 132 | proc_num = plat_my_core_pos(); |
| 133 | if (rdist_has_saved[proc_num] == 1) { |
| 134 | gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num]; |
| 135 | mmio_write_32(gicr_base + GICR_IGROUPR0, gic_data.saved_group); |
| 136 | mmio_write_32(gicr_base + GICR_ISENABLER0, gic_data.saved_enable); |
| 137 | mmio_write_32(gicr_base + GICR_ICFGR0, gic_data.saved_conf0); |
| 138 | mmio_write_32(gicr_base + GICR_ICFGR1, gic_data.saved_conf1); |
| 139 | mmio_write_32(gicr_base + GICR_IGRPMODR0, gic_data.saved_grpmod); |
| 140 | } |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 141 | } |
| 142 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 143 | void mt_gic_sync_dcm_enable(void) |
| 144 | { |
| 145 | mmio_clrsetbits_32(GIC_SYNC_DCM, GIC_SYNC_DCM_MASK, GIC_SYNC_DCM_ON); |
| 146 | } |
| 147 | |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 148 | void mt_gic_sync_dcm_disable(void) |
| 149 | { |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 150 | mmio_clrsetbits_32(GIC_SYNC_DCM, GIC_SYNC_DCM_MASK, GIC_SYNC_DCM_OFF); |
| 151 | } |
| 152 | |
| 153 | void mt_gic_init(void) |
| 154 | { |
| 155 | gicv3_distif_init(); |
| 156 | gicv3_cpuif_enable(plat_my_core_pos()); |
| 157 | mt_gic_rdistif_init(); |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 158 | |
developer | e2f1332 | 2019-10-04 10:47:11 +0800 | [diff] [blame] | 159 | clear_sec_pol_ctl_en(); |
developer | 3f3f1ab | 2019-05-02 22:26:22 +0800 | [diff] [blame] | 160 | } |