| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <common/debug.h> |
| 8 |
#include <cdefs.h> |
| 9 |
#include <drivers/arm/smmu_v3.h> |
| 10 |
#include <drivers/delay_timer.h> |
| 11 |
#include <lib/mmio.h> |
| 12 |
#include <arch_features.h> |
| 13 |
|
| 14 |
|
| 15 |
#define SMMU_POLL_TIMEOUT_US U(1000) |
| 16 |
|
| 17 |
static int smmuv3_poll(uintptr_t smmu_reg, uint32_t mask, |
| 18 |
uint32_t value) |
| 19 |
{ |
| 20 |
uint32_t reg_val; |
| 21 |
uint64_t timeout; |
| 22 |
|
| 23 |
|
| 24 |
timeout = timeout_init_us(SMMU_POLL_TIMEOUT_US); |
| 25 |
do { |
| 26 |
reg_val = mmio_read_32(smmu_reg); |
| 27 |
if ((reg_val & mask) == value) |
| 28 |
return 0; |
| 29 |
} while (!timeout_elapsed(timeout)); |
| 30 |
|
| 31 |
ERROR("Timeout polling SMMUv3 register @%p\n", (void *)smmu_reg); |
| 32 |
ERROR("Read value 0x%x, expected 0x%x\n", reg_val, |
| 33 |
value == 0U ? reg_val & ~mask : reg_val | mask); |
| 34 |
return -1; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
int __init smmuv3_security_init(uintptr_t smmu_base) |
| 42 |
{ |
| 43 |
|
| 44 |
if (smmuv3_poll(smmu_base + SMMU_GBPA, SMMU_GBPA_UPDATE, 0U) != 0U) |
| 45 |
return -1; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
mmio_setbits_32(smmu_base + SMMU_GBPA, |
| 52 |
SMMU_GBPA_UPDATE | SMMU_GBPA_ABORT); |
| 53 |
|
| 54 |
if (smmuv3_poll(smmu_base + SMMU_GBPA, SMMU_GBPA_UPDATE, 0U) != 0U) |
| 55 |
return -1; |
| 56 |
|
| 57 |
|
| 58 |
if ((mmio_read_32(smmu_base + SMMU_S_IDR1) & |
| 59 |
SMMU_S_IDR1_SECURE_IMPL) == 0U) |
| 60 |
return 0; |
| 61 |
|
| 62 |
|
| 63 |
if (smmuv3_poll(smmu_base + SMMU_S_GBPA, SMMU_S_GBPA_UPDATE, 0U) != 0U) |
| 64 |
return -1; |
| 65 |
|
| 66 |
mmio_setbits_32(smmu_base + SMMU_S_GBPA, |
| 67 |
SMMU_S_GBPA_UPDATE | SMMU_S_GBPA_ABORT); |
| 68 |
|
| 69 |
return smmuv3_poll(smmu_base + SMMU_S_GBPA, SMMU_S_GBPA_UPDATE, 0U); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
int __init smmuv3_init(uintptr_t smmu_base) |
| 74 |
{ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
to |
| 88 |
|
| 89 |
|
| 90 |
mmio_write_32(smmu_base + SMMU_S_INIT, SMMU_S_INIT_INV_ALL); |
| 91 |
|
| 92 |
|
| 93 |
if (smmuv3_poll(smmu_base + SMMU_S_INIT, |
| 94 |
SMMU_S_INIT_INV_ALL, 0U) != 0) { |
| 95 |
return -1; |
| 96 |
} |
| 97 |
|
| 98 |
#if ENABLE_RME |
| 99 |
|
| 100 |
if (is_feat_rme_present()) { |
| 101 |
if ((mmio_read_32(smmu_base + SMMU_ROOT_IDR0) & |
| 102 |
SMMU_ROOT_IDR0_ROOT_IMPL) == 0U) { |
| 103 |
WARN("Skip SMMU GPC configuration.\n"); |
| 104 |
} else { |
| 105 |
uint64_t gpccr_el3 = read_gpccr_el3(); |
| 106 |
uint64_t gptbr_el3 = read_gptbr_el3(); |
| 107 |
|
| 108 |
|
| 109 |
gpccr_el3 &= ~(1UL << 16); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
mmio_write_32(smmu_base + SMMU_ROOT_GPT_BASE_CFG, |
| 116 |
gpccr_el3); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
mmio_write_64(smmu_base + SMMU_ROOT_GPT_BASE, |
| 124 |
gptbr_el3 << 12); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
mmio_setbits_32(smmu_base + SMMU_ROOT_CR0, |
| 133 |
SMMU_ROOT_CR0_GPCEN | |
| 134 |
SMMU_ROOT_CR0_ACCESSEN); |
| 135 |
|
| 136 |
|
| 137 |
if (smmuv3_poll(smmu_base + SMMU_ROOT_CR0ACK, |
| 138 |
SMMU_ROOT_CR0_GPCEN | |
| 139 |
SMMU_ROOT_CR0_ACCESSEN, |
| 140 |
SMMU_ROOT_CR0_GPCEN | |
| 141 |
SMMU_ROOT_CR0_ACCESSEN) != 0) { |
| 142 |
WARN("Failed enabling SMMU GPC.\n"); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#endif |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
int smmuv3_ns_set_abort_all(uintptr_t smmu_base) |
| 159 |
{ |
| 160 |
|
| 161 |
if (smmuv3_poll(smmu_base + SMMU_GBPA, SMMU_GBPA_UPDATE, 0U) != 0U) { |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
mmio_setbits_32(smmu_base + SMMU_GBPA, SMMU_GBPA_UPDATE | SMMU_GBPA_ABORT); |
| 170 |
if (smmuv3_poll(smmu_base + SMMU_GBPA, SMMU_GBPA_UPDATE, 0U) != 0U) { |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
mmio_clrbits_32(smmu_base + SMMU_CR0, SMMU_CR0_SMMUEN); |
| 176 |
if (smmuv3_poll(smmu_base + SMMU_CR0ACK, SMMU_CR0_SMMUEN, 0U) != 0U) { |
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |