Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | */ |
| 5 | |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 6 | #include <cpu_func.h> |
| 7 | #include <dm.h> |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 8 | #include <iommu.h> |
| 9 | #include <lmb.h> |
| 10 | #include <memalign.h> |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | |
| 13 | #define DART_PARAMS2 0x0004 |
| 14 | #define DART_PARAMS2_BYPASS_SUPPORT BIT(0) |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 15 | |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 16 | #define DART_T8020_TLB_CMD 0x0020 |
| 17 | #define DART_T8020_TLB_CMD_FLUSH BIT(20) |
| 18 | #define DART_T8020_TLB_CMD_BUSY BIT(2) |
| 19 | #define DART_T8020_TLB_SIDMASK 0x0034 |
| 20 | #define DART_T8020_ERROR 0x0040 |
| 21 | #define DART_T8020_ERROR_ADDR_LO 0x0050 |
| 22 | #define DART_T8020_ERROR_ADDR_HI 0x0054 |
| 23 | #define DART_T8020_CONFIG 0x0060 |
| 24 | #define DART_T8020_CONFIG_LOCK BIT(15) |
| 25 | #define DART_T8020_SID_ENABLE 0x00fc |
| 26 | #define DART_T8020_TCR_BASE 0x0100 |
| 27 | #define DART_T8020_TCR_TRANSLATE_ENABLE BIT(7) |
| 28 | #define DART_T8020_TCR_BYPASS_DART BIT(8) |
| 29 | #define DART_T8020_TCR_BYPASS_DAPF BIT(12) |
| 30 | #define DART_T8020_TTBR_BASE 0x0200 |
| 31 | #define DART_T8020_TTBR_VALID BIT(31) |
| 32 | |
| 33 | #define DART_T8110_PARAMS4 0x000c |
| 34 | #define DART_T8110_PARAMS4_NSID_MASK (0x1ff << 0) |
| 35 | #define DART_T8110_TLB_CMD 0x0080 |
| 36 | #define DART_T8110_TLB_CMD_BUSY BIT(31) |
| 37 | #define DART_T8110_TLB_CMD_FLUSH_ALL BIT(8) |
| 38 | #define DART_T8110_ERROR 0x0100 |
| 39 | #define DART_T8110_ERROR_MASK 0x0104 |
| 40 | #define DART_T8110_ERROR_ADDR_LO 0x0170 |
| 41 | #define DART_T8110_ERROR_ADDR_HI 0x0174 |
| 42 | #define DART_T8110_PROTECT 0x0200 |
| 43 | #define DART_T8110_PROTECT_TTBR_TCR BIT(0) |
| 44 | #define DART_T8110_SID_ENABLE_BASE 0x0c00 |
| 45 | #define DART_T8110_TCR_BASE 0x1000 |
Janne Grunau | 06c61d5 | 2022-07-01 00:06:16 +0200 | [diff] [blame] | 46 | #define DART_T8110_TCR_BYPASS_DAPF BIT(2) |
| 47 | #define DART_T8110_TCR_BYPASS_DART BIT(1) |
| 48 | #define DART_T8110_TCR_TRANSLATE_ENABLE BIT(0) |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 49 | #define DART_T8110_TTBR_BASE 0x1400 |
| 50 | #define DART_T8110_TTBR_VALID BIT(0) |
| 51 | |
| 52 | #define DART_SID_ENABLE(priv, idx) \ |
| 53 | ((priv)->sid_enable_base + 4 * (idx)) |
| 54 | #define DART_TCR(priv, sid) ((priv)->tcr_base + 4 * (sid)) |
| 55 | #define DART_TTBR(priv, sid, idx) \ |
| 56 | ((priv)->ttbr_base + 4 * (priv)->nttbr * (sid) + 4 * (idx)) |
| 57 | #define DART_TTBR_SHIFT 12 |
| 58 | |
| 59 | #define DART_ALL_STREAMS(priv) ((1U << (priv)->nsid) - 1) |
| 60 | |
| 61 | #define DART_PAGE_SIZE SZ_16K |
| 62 | #define DART_PAGE_MASK (DART_PAGE_SIZE - 1) |
| 63 | |
| 64 | #define DART_L1_TABLE 0x3 |
| 65 | #define DART_L2_INVAL 0 |
| 66 | #define DART_L2_VALID BIT(0) |
| 67 | #define DART_L2_FULL_PAGE BIT(1) |
| 68 | #define DART_L2_START(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 52) |
| 69 | #define DART_L2_END(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 40) |
| 70 | |
| 71 | struct apple_dart_priv { |
| 72 | void *base; |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 73 | u64 *l1, *l2; |
| 74 | int bypass, shift; |
| 75 | |
| 76 | dma_addr_t dvabase; |
| 77 | dma_addr_t dvaend; |
| 78 | |
| 79 | int nsid; |
| 80 | int nttbr; |
| 81 | int sid_enable_base; |
| 82 | int tcr_base; |
| 83 | u32 tcr_translate_enable; |
| 84 | u32 tcr_bypass; |
| 85 | int ttbr_base; |
| 86 | u32 ttbr_valid; |
| 87 | void (*flush_tlb)(struct apple_dart_priv *priv); |
| 88 | }; |
| 89 | |
| 90 | static void apple_dart_t8020_flush_tlb(struct apple_dart_priv *priv) |
| 91 | { |
| 92 | dsb(); |
| 93 | |
| 94 | writel(DART_ALL_STREAMS(priv), priv->base + DART_T8020_TLB_SIDMASK); |
| 95 | writel(DART_T8020_TLB_CMD_FLUSH, priv->base + DART_T8020_TLB_CMD); |
| 96 | while (readl(priv->base + DART_T8020_TLB_CMD) & |
| 97 | DART_T8020_TLB_CMD_BUSY) |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | static void apple_dart_t8110_flush_tlb(struct apple_dart_priv *priv) |
| 102 | { |
| 103 | dsb(); |
| 104 | |
| 105 | writel(DART_T8110_TLB_CMD_FLUSH_ALL, |
| 106 | priv->base + DART_T8110_TLB_CMD_FLUSH_ALL); |
| 107 | while (readl(priv->base + DART_T8110_TLB_CMD) & |
| 108 | DART_T8110_TLB_CMD_BUSY) |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | static dma_addr_t apple_dart_map(struct udevice *dev, void *addr, size_t size) |
| 113 | { |
| 114 | struct apple_dart_priv *priv = dev_get_priv(dev); |
| 115 | phys_addr_t paddr, dva; |
| 116 | phys_size_t psize, off; |
| 117 | int i, idx; |
| 118 | |
| 119 | if (priv->bypass) |
| 120 | return (phys_addr_t)addr; |
| 121 | |
| 122 | paddr = ALIGN_DOWN((phys_addr_t)addr, DART_PAGE_SIZE); |
| 123 | off = (phys_addr_t)addr - paddr; |
| 124 | psize = ALIGN(size + off, DART_PAGE_SIZE); |
| 125 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 126 | dva = lmb_alloc(psize, DART_PAGE_SIZE); |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 127 | |
| 128 | idx = dva / DART_PAGE_SIZE; |
| 129 | for (i = 0; i < psize / DART_PAGE_SIZE; i++) { |
| 130 | priv->l2[idx + i] = (paddr >> priv->shift) | DART_L2_VALID | |
| 131 | DART_L2_START(0LL) | DART_L2_END(~0LL); |
| 132 | paddr += DART_PAGE_SIZE; |
| 133 | } |
| 134 | flush_dcache_range((unsigned long)&priv->l2[idx], |
| 135 | (unsigned long)&priv->l2[idx + i]); |
| 136 | priv->flush_tlb(priv); |
| 137 | |
| 138 | return dva + off; |
| 139 | } |
| 140 | |
| 141 | static void apple_dart_unmap(struct udevice *dev, dma_addr_t addr, size_t size) |
| 142 | { |
| 143 | struct apple_dart_priv *priv = dev_get_priv(dev); |
| 144 | phys_addr_t dva; |
| 145 | phys_size_t psize; |
| 146 | int i, idx; |
| 147 | |
| 148 | if (priv->bypass) |
| 149 | return; |
| 150 | |
| 151 | dva = ALIGN_DOWN(addr, DART_PAGE_SIZE); |
| 152 | psize = size + (addr - dva); |
| 153 | psize = ALIGN(psize, DART_PAGE_SIZE); |
| 154 | |
| 155 | idx = dva / DART_PAGE_SIZE; |
| 156 | for (i = 0; i < psize / DART_PAGE_SIZE; i++) |
| 157 | priv->l2[idx + i] = DART_L2_INVAL; |
| 158 | flush_dcache_range((unsigned long)&priv->l2[idx], |
| 159 | (unsigned long)&priv->l2[idx + i]); |
| 160 | priv->flush_tlb(priv); |
| 161 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 162 | lmb_free(dva, psize); |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static struct iommu_ops apple_dart_ops = { |
| 166 | .map = apple_dart_map, |
| 167 | .unmap = apple_dart_unmap, |
| 168 | }; |
Janne Grunau | 06c61d5 | 2022-07-01 00:06:16 +0200 | [diff] [blame] | 169 | |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 170 | static int apple_dart_probe(struct udevice *dev) |
| 171 | { |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 172 | struct apple_dart_priv *priv = dev_get_priv(dev); |
| 173 | dma_addr_t addr; |
| 174 | phys_addr_t l2; |
| 175 | int ntte, nl1, nl2; |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 176 | int sid, i; |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 177 | u32 params2, params4; |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 178 | |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 179 | priv->base = dev_read_addr_ptr(dev); |
| 180 | if (!priv->base) |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 181 | return -EINVAL; |
| 182 | |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 183 | if (device_is_compatible(dev, "apple,t8110-dart")) { |
| 184 | params4 = readl(priv->base + DART_T8110_PARAMS4); |
| 185 | priv->nsid = params4 & DART_T8110_PARAMS4_NSID_MASK; |
| 186 | priv->nttbr = 1; |
| 187 | priv->sid_enable_base = DART_T8110_SID_ENABLE_BASE; |
| 188 | priv->tcr_base = DART_T8110_TCR_BASE; |
| 189 | priv->tcr_translate_enable = DART_T8110_TCR_TRANSLATE_ENABLE; |
| 190 | priv->tcr_bypass = |
| 191 | DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART; |
| 192 | priv->ttbr_base = DART_T8110_TTBR_BASE; |
| 193 | priv->ttbr_valid = DART_T8110_TTBR_VALID; |
| 194 | priv->flush_tlb = apple_dart_t8110_flush_tlb; |
| 195 | } else { |
| 196 | priv->nsid = 16; |
| 197 | priv->nttbr = 4; |
| 198 | priv->sid_enable_base = DART_T8020_SID_ENABLE; |
| 199 | priv->tcr_base = DART_T8020_TCR_BASE; |
| 200 | priv->tcr_translate_enable = DART_T8020_TCR_TRANSLATE_ENABLE; |
| 201 | priv->tcr_bypass = |
| 202 | DART_T8020_TCR_BYPASS_DAPF | DART_T8020_TCR_BYPASS_DART; |
| 203 | priv->ttbr_base = DART_T8020_TTBR_BASE; |
| 204 | priv->ttbr_valid = DART_T8020_TTBR_VALID; |
| 205 | priv->flush_tlb = apple_dart_t8020_flush_tlb; |
| 206 | } |
| 207 | |
| 208 | if (device_is_compatible(dev, "apple,t6000-dart") || |
| 209 | device_is_compatible(dev, "apple,t8110-dart")) |
| 210 | priv->shift = 4; |
Janne Grunau | 06c61d5 | 2022-07-01 00:06:16 +0200 | [diff] [blame] | 211 | |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 212 | priv->dvabase = DART_PAGE_SIZE; |
| 213 | priv->dvaend = SZ_4G - DART_PAGE_SIZE; |
| 214 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 215 | lmb_add(priv->dvabase, priv->dvaend - priv->dvabase); |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 216 | |
| 217 | /* Disable translations. */ |
| 218 | for (sid = 0; sid < priv->nsid; sid++) |
| 219 | writel(0, priv->base + DART_TCR(priv, sid)); |
| 220 | |
| 221 | /* Remove page tables. */ |
| 222 | for (sid = 0; sid < priv->nsid; sid++) { |
| 223 | for (i = 0; i < priv->nttbr; i++) |
| 224 | writel(0, priv->base + DART_TTBR(priv, sid, i)); |
| 225 | } |
| 226 | priv->flush_tlb(priv); |
| 227 | |
| 228 | params2 = readl(priv->base + DART_PARAMS2); |
| 229 | if (params2 & DART_PARAMS2_BYPASS_SUPPORT) { |
| 230 | for (sid = 0; sid < priv->nsid; sid++) { |
| 231 | writel(priv->tcr_bypass, |
| 232 | priv->base + DART_TCR(priv, sid)); |
Janne Grunau | 06c61d5 | 2022-07-01 00:06:16 +0200 | [diff] [blame] | 233 | } |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 234 | priv->bypass = 1; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | ntte = DIV_ROUND_UP(priv->dvaend, DART_PAGE_SIZE); |
| 239 | nl2 = DIV_ROUND_UP(ntte, DART_PAGE_SIZE / sizeof(u64)); |
| 240 | nl1 = DIV_ROUND_UP(nl2, DART_PAGE_SIZE / sizeof(u64)); |
| 241 | |
| 242 | priv->l2 = memalign(DART_PAGE_SIZE, nl2 * DART_PAGE_SIZE); |
| 243 | memset(priv->l2, 0, nl2 * DART_PAGE_SIZE); |
| 244 | flush_dcache_range((unsigned long)priv->l2, |
| 245 | (unsigned long)priv->l2 + nl2 * DART_PAGE_SIZE); |
| 246 | |
| 247 | priv->l1 = memalign(DART_PAGE_SIZE, nl1 * DART_PAGE_SIZE); |
| 248 | memset(priv->l1, 0, nl1 * DART_PAGE_SIZE); |
| 249 | l2 = (phys_addr_t)priv->l2; |
| 250 | for (i = 0; i < nl2; i++) { |
| 251 | priv->l1[i] = (l2 >> priv->shift) | DART_L1_TABLE; |
| 252 | l2 += DART_PAGE_SIZE; |
| 253 | } |
| 254 | flush_dcache_range((unsigned long)priv->l1, |
| 255 | (unsigned long)priv->l1 + nl1 * DART_PAGE_SIZE); |
| 256 | |
| 257 | /* Install page tables. */ |
| 258 | for (sid = 0; sid < priv->nsid; sid++) { |
| 259 | addr = (phys_addr_t)priv->l1; |
| 260 | for (i = 0; i < nl1; i++) { |
| 261 | writel(addr >> DART_TTBR_SHIFT | priv->ttbr_valid, |
| 262 | priv->base + DART_TTBR(priv, sid, i)); |
| 263 | addr += DART_PAGE_SIZE; |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 264 | } |
| 265 | } |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 266 | priv->flush_tlb(priv); |
| 267 | |
| 268 | /* Enable all streams. */ |
| 269 | for (i = 0; i < priv->nsid / 32; i++) |
| 270 | writel(~0, priv->base + DART_SID_ENABLE(priv, i)); |
| 271 | |
| 272 | /* Enable translations. */ |
| 273 | for (sid = 0; sid < priv->nsid; sid++) { |
| 274 | writel(priv->tcr_translate_enable, |
| 275 | priv->base + DART_TCR(priv, sid)); |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int apple_dart_remove(struct udevice *dev) |
| 282 | { |
| 283 | struct apple_dart_priv *priv = dev_get_priv(dev); |
| 284 | int sid, i; |
| 285 | |
| 286 | /* Disable translations. */ |
| 287 | for (sid = 0; sid < priv->nsid; sid++) |
| 288 | writel(0, priv->base + DART_TCR(priv, sid)); |
| 289 | |
| 290 | /* Remove page tables. */ |
| 291 | for (sid = 0; sid < priv->nsid; sid++) { |
| 292 | for (i = 0; i < priv->nttbr; i++) |
| 293 | writel(0, priv->base + DART_TTBR(priv, sid, i)); |
| 294 | } |
| 295 | priv->flush_tlb(priv); |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static const struct udevice_id apple_dart_ids[] = { |
| 301 | { .compatible = "apple,t8103-dart" }, |
Janne Grunau | 622a058 | 2022-02-08 22:27:49 +0100 | [diff] [blame] | 302 | { .compatible = "apple,t6000-dart" }, |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 303 | { .compatible = "apple,t8110-dart" }, |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 304 | { /* sentinel */ } |
| 305 | }; |
| 306 | |
| 307 | U_BOOT_DRIVER(apple_dart) = { |
| 308 | .name = "apple_dart", |
| 309 | .id = UCLASS_IOMMU, |
| 310 | .of_match = apple_dart_ids, |
Mark Kettenis | cedd24c | 2023-01-21 20:27:54 +0100 | [diff] [blame] | 311 | .priv_auto = sizeof(struct apple_dart_priv), |
| 312 | .ops = &apple_dart_ops, |
| 313 | .probe = apple_dart_probe, |
| 314 | .remove = apple_dart_remove, |
| 315 | .flags = DM_FLAG_OS_PREPARE |
Mark Kettenis | 357a256 | 2021-10-23 16:58:05 +0200 | [diff] [blame] | 316 | }; |