1 1.5 jmcneill /* $NetBSD: apple_dart.c,v 1.5 2023/02/24 11:19:15 jmcneill Exp $ */ 2 1.4 skrll /* $OpenBSD: apldart.c,v 1.10 2022/02/27 17:36:52 kettenis Exp $ */ 3 1.1 jmcneill 4 1.1 jmcneill /*- 5 1.1 jmcneill * Copyright (c) 2021 Mark Kettenis <kettenis (at) openbsd.org> 6 1.1 jmcneill * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca> 7 1.1 jmcneill * 8 1.1 jmcneill * Permission to use, copy, modify, and distribute this software for any 9 1.1 jmcneill * purpose with or without fee is hereby granted, provided that the above 10 1.1 jmcneill * copyright notice and this permission notice appear in all copies. 11 1.1 jmcneill * 12 1.1 jmcneill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 1.1 jmcneill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 1.1 jmcneill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 1.1 jmcneill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 1.1 jmcneill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 1.1 jmcneill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 1.1 jmcneill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 1.1 jmcneill */ 20 1.1 jmcneill 21 1.1 jmcneill //#define APPLE_DART_DEBUG 22 1.1 jmcneill 23 1.1 jmcneill #include <sys/cdefs.h> 24 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: apple_dart.c,v 1.5 2023/02/24 11:19:15 jmcneill Exp $"); 25 1.1 jmcneill 26 1.1 jmcneill #include <sys/param.h> 27 1.1 jmcneill #include <sys/bus.h> 28 1.1 jmcneill #include <sys/device.h> 29 1.1 jmcneill #include <sys/intr.h> 30 1.1 jmcneill #include <sys/kernel.h> 31 1.1 jmcneill #include <sys/systm.h> 32 1.1 jmcneill #include <sys/kmem.h> 33 1.1 jmcneill #include <sys/vmem.h> 34 1.1 jmcneill 35 1.1 jmcneill #include <arm/cpufunc.h> 36 1.1 jmcneill 37 1.1 jmcneill #include <dev/fdt/fdtvar.h> 38 1.4 skrll /* 39 1.4 skrll * This driver largely ignores stream IDs and simply uses a single 40 1.4 skrll * translation table for all the devices that it serves. This is good 41 1.4 skrll * enough for the PCIe host bridge that serves the on-board devices on 42 1.4 skrll * the current generation Apple Silicon Macs as these only have a 43 1.4 skrll * single PCIe device behind each DART. 44 1.4 skrll */ 45 1.1 jmcneill 46 1.1 jmcneill /* 47 1.1 jmcneill * DART registers 48 1.1 jmcneill */ 49 1.4 skrll #define DART_PARAMS2 0x0004 50 1.4 skrll #define DART_PARAMS2_BYPASS_SUPPORT __BIT(0) 51 1.1 jmcneill #define DART_TLB_OP 0x0020 52 1.4 skrll #define DART_TLB_OP_BUSY __BIT(2) 53 1.4 skrll #define DART_TLB_OP_FLUSH __BIT(20) 54 1.1 jmcneill #define DART_TLB_OP_SIDMASK 0x0034 55 1.1 jmcneill #define DART_ERR_STATUS 0x0040 56 1.4 skrll #define DART_ERR_FLAG __BIT(31) 57 1.4 skrll #define DART_ERR_STREAM_MASK __BITS(27, 24) 58 1.4 skrll #define DART_ERR_CODE_MASK __BITS(11, 0) 59 1.4 skrll #define DART_ERR_READ_FAULT __BIT(4) 60 1.4 skrll #define DART_ERR_WRITE_FAULT __BIT(3) 61 1.4 skrll #define DART_ERR_NOPTE __BIT(2) 62 1.4 skrll #define DART_ERR_NOPMD __BIT(1) 63 1.4 skrll #define DART_ERR_NOTTBR __BIT(0) 64 1.1 jmcneill #define DART_ERR_ADDRL 0x0050 65 1.1 jmcneill #define DART_ERR_ADDRH 0x0054 66 1.4 skrll #define DART_CONFIG 0x0060 67 1.4 skrll #define DART_CONFIG_LOCK __BIT(15) 68 1.4 skrll #define DART_TCR(sid) (0x0100 + (sid) * 0x4) 69 1.4 skrll #define DART_TCR_TRANSLATE_ENABLE __BIT(7) 70 1.4 skrll #define DART_TCR_BYPASS_DART __BIT(8) 71 1.4 skrll #define DART_TCR_BYPASS_DAPF __BIT(12) 72 1.1 jmcneill #define DART_TTBR(sid, idx) (0x0200 + (sid) * 0x10 + (idx) * 0x4) 73 1.4 skrll #define DART_TTBR_VALID __BIT(31) 74 1.4 skrll #define DART_TTBR_SHIFT 12 75 1.4 skrll 76 1.4 skrll #define DART_NUM_STREAMS 16 77 1.4 skrll #define DART_ALL_STREAMS ((1 << DART_NUM_STREAMS) - 1) 78 1.1 jmcneill 79 1.1 jmcneill #define DART_APERTURE_START 0x00100000 80 1.1 jmcneill #define DART_APERTURE_SIZE 0x3fe00000 81 1.1 jmcneill #define DART_PAGE_SIZE 16384 82 1.1 jmcneill #define DART_PAGE_MASK (DART_PAGE_SIZE - 1) 83 1.1 jmcneill 84 1.4 skrll /* 85 1.4 skrll * Some hardware (e.g. bge(4)) will always use (aligned) 64-bit memory 86 1.4 skrll * access. To make sure this doesn't fault, round the subpage limits 87 1.4 skrll * down and up accordingly. 88 1.4 skrll */ 89 1.5 jmcneill #define DART_OFFSET_MASK 7 90 1.4 skrll 91 1.4 skrll #define DART_L1_TABLE 0x3 92 1.1 jmcneill #define DART_L2_INVAL 0x0 93 1.4 skrll #define DART_L2_VALID __BIT(0) 94 1.4 skrll #define DART_L2_FULL_PAGE __BIT(1) 95 1.4 skrll 96 1.4 skrll #define DART_L2_START_MASK __BITS(63, 52) 97 1.4 skrll #define DART_L2_END_MASK __BITS(51, 40) 98 1.4 skrll #define DART_L2_SUBPAGE(addr) __SHIFTOUT((addr), __BITS(13, 2)) 99 1.4 skrll #define DART_L2_START(addr) __SHIFTIN(DART_L2_SUBPAGE(addr), DART_L2_START_MASK) 100 1.4 skrll #define DART_L2_END(addr) __SHIFTIN(DART_L2_SUBPAGE(addr), DART_L2_END_MASK) 101 1.1 jmcneill 102 1.1 jmcneill #define DART_ROUND_PAGE(pa) (((pa) + DART_PAGE_MASK) & ~DART_PAGE_MASK) 103 1.1 jmcneill #define DART_TRUNC_PAGE(pa) ((pa) & ~DART_PAGE_MASK) 104 1.4 skrll #define DART_ROUND_OFFSET(pa) (((pa) + DART_OFFSET_MASK) & ~DART_OFFSET_MASK) 105 1.4 skrll #define DART_TRUNC_OFFSET(pa) ((pa) & ~DART_OFFSET_MASK) 106 1.1 jmcneill 107 1.1 jmcneill static const struct device_compatible_entry compat_data[] = { 108 1.1 jmcneill { .compat = "apple,dart-m1", .value = 16 }, 109 1.4 skrll { .compat = "apple,t8103-dart", .value = 16 }, 110 1.1 jmcneill DEVICE_COMPAT_EOL 111 1.1 jmcneill }; 112 1.1 jmcneill 113 1.1 jmcneill static struct arm32_dma_range apple_dart_dma_ranges[] = { 114 1.1 jmcneill [0] = { 115 1.1 jmcneill .dr_sysbase = 0, 116 1.1 jmcneill .dr_busbase = 0, 117 1.1 jmcneill .dr_len = UINTPTR_MAX, 118 1.1 jmcneill .dr_flags = _BUS_DMAMAP_COHERENT, 119 1.1 jmcneill } 120 1.1 jmcneill }; 121 1.1 jmcneill 122 1.1 jmcneill struct apple_dart_map_state { 123 1.1 jmcneill bus_addr_t ams_dva; 124 1.1 jmcneill bus_size_t ams_len; 125 1.1 jmcneill }; 126 1.1 jmcneill 127 1.1 jmcneill struct apple_dart_dma { 128 1.1 jmcneill bus_dmamap_t dma_map; 129 1.1 jmcneill bus_dma_segment_t dma_seg; 130 1.1 jmcneill bus_size_t dma_size; 131 1.1 jmcneill void *dma_kva; 132 1.1 jmcneill }; 133 1.1 jmcneill 134 1.1 jmcneill #define DART_DMA_MAP(_dma) ((_dma)->dma_map) 135 1.1 jmcneill #define DART_DMA_LEN(_dma) ((_dma)->dma_size) 136 1.1 jmcneill #define DART_DMA_DVA(_dma) ((_dma)->dma_map->dm_segs[0].ds_addr) 137 1.1 jmcneill #define DART_DMA_KVA(_dma) ((_dma)->dma_kva) 138 1.1 jmcneill 139 1.1 jmcneill struct apple_dart_softc { 140 1.1 jmcneill device_t sc_dev; 141 1.1 jmcneill int sc_phandle; 142 1.1 jmcneill bus_space_tag_t sc_bst; 143 1.1 jmcneill bus_space_handle_t sc_bsh; 144 1.1 jmcneill bus_dma_tag_t sc_dmat; 145 1.1 jmcneill 146 1.1 jmcneill uint64_t sc_sid_mask; 147 1.1 jmcneill u_int sc_nsid; 148 1.1 jmcneill 149 1.1 jmcneill vmem_t *sc_dvamap; 150 1.1 jmcneill 151 1.1 jmcneill struct apple_dart_dma *sc_l1; 152 1.1 jmcneill struct apple_dart_dma **sc_l2; 153 1.1 jmcneill u_int sc_nl2; 154 1.1 jmcneill 155 1.1 jmcneill struct arm32_bus_dma_tag sc_bus_dmat; 156 1.1 jmcneill }; 157 1.1 jmcneill 158 1.1 jmcneill #define DART_READ(sc, reg) \ 159 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 160 1.1 jmcneill #define DART_WRITE(sc, reg, val) \ 161 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 162 1.1 jmcneill 163 1.1 jmcneill static void 164 1.1 jmcneill apple_dart_flush_tlb(struct apple_dart_softc *sc) 165 1.1 jmcneill { 166 1.1 jmcneill dsb(sy); 167 1.1 jmcneill isb(); 168 1.1 jmcneill 169 1.1 jmcneill DART_WRITE(sc, DART_TLB_OP_SIDMASK, sc->sc_sid_mask); 170 1.1 jmcneill DART_WRITE(sc, DART_TLB_OP, DART_TLB_OP_FLUSH); 171 1.1 jmcneill while ((DART_READ(sc, DART_TLB_OP) & DART_TLB_OP_BUSY) != 0) { 172 1.1 jmcneill __asm volatile ("yield" ::: "memory"); 173 1.1 jmcneill } 174 1.1 jmcneill } 175 1.1 jmcneill 176 1.1 jmcneill static struct apple_dart_dma * 177 1.1 jmcneill apple_dart_dma_alloc(bus_dma_tag_t dmat, bus_size_t size, bus_size_t align) 178 1.1 jmcneill { 179 1.1 jmcneill struct apple_dart_dma *dma; 180 1.1 jmcneill int nsegs, error; 181 1.1 jmcneill 182 1.1 jmcneill dma = kmem_zalloc(sizeof(*dma), KM_SLEEP); 183 1.1 jmcneill dma->dma_size = size; 184 1.1 jmcneill 185 1.1 jmcneill error = bus_dmamem_alloc(dmat, size, align, 0, &dma->dma_seg, 1, 186 1.1 jmcneill &nsegs, BUS_DMA_WAITOK); 187 1.1 jmcneill if (error != 0) { 188 1.1 jmcneill goto destroy; 189 1.1 jmcneill } 190 1.1 jmcneill 191 1.1 jmcneill error = bus_dmamem_map(dmat, &dma->dma_seg, nsegs, size, 192 1.1 jmcneill &dma->dma_kva, BUS_DMA_WAITOK | BUS_DMA_NOCACHE); 193 1.1 jmcneill if (error != 0) { 194 1.1 jmcneill goto free; 195 1.1 jmcneill } 196 1.1 jmcneill 197 1.1 jmcneill error = bus_dmamap_create(dmat, size, 1, size, 0, 198 1.1 jmcneill BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &dma->dma_map); 199 1.1 jmcneill if (error != 0) { 200 1.1 jmcneill goto dmafree; 201 1.1 jmcneill } 202 1.1 jmcneill 203 1.1 jmcneill error = bus_dmamap_load(dmat, dma->dma_map, dma->dma_kva, size, 204 1.1 jmcneill NULL, BUS_DMA_WAITOK); 205 1.1 jmcneill if (error != 0) { 206 1.1 jmcneill goto unmap; 207 1.1 jmcneill } 208 1.1 jmcneill 209 1.1 jmcneill memset(dma->dma_kva, 0, size); 210 1.1 jmcneill 211 1.1 jmcneill return dma; 212 1.1 jmcneill 213 1.1 jmcneill destroy: 214 1.1 jmcneill bus_dmamap_destroy(dmat, dma->dma_map); 215 1.1 jmcneill unmap: 216 1.1 jmcneill bus_dmamem_unmap(dmat, dma->dma_kva, size); 217 1.1 jmcneill free: 218 1.1 jmcneill bus_dmamem_free(dmat, &dma->dma_seg, 1); 219 1.1 jmcneill dmafree: 220 1.1 jmcneill kmem_free(dma, sizeof(*dma)); 221 1.1 jmcneill return NULL; 222 1.1 jmcneill } 223 1.1 jmcneill 224 1.1 jmcneill static int 225 1.1 jmcneill apple_dart_intr(void *priv) 226 1.1 jmcneill { 227 1.1 jmcneill struct apple_dart_softc * const sc = priv; 228 1.1 jmcneill char fdt_path[128]; 229 1.1 jmcneill uint64_t addr; 230 1.1 jmcneill uint32_t status; 231 1.1 jmcneill 232 1.1 jmcneill status = DART_READ(sc, DART_ERR_STATUS); 233 1.4 skrll addr = __SHIFTIN(DART_READ(sc, DART_ERR_ADDRL), __BITS(31, 0)); 234 1.4 skrll addr |= __SHIFTIN(DART_READ(sc, DART_ERR_ADDRH), __BITS(63, 32)); 235 1.1 jmcneill DART_WRITE(sc, DART_ERR_STATUS, status); 236 1.1 jmcneill 237 1.4 skrll if ((status & DART_ERR_FLAG) == 0) 238 1.4 skrll return 1; 239 1.4 skrll 240 1.4 skrll #ifdef APPLE_DART_DEBUG 241 1.4 skrll printf("%s: status %#"PRIx32"\n", __func__, status); 242 1.4 skrll printf("%s: addrl %#"PRIx32"\n", __func__, DART_READ(sc, DART_ERR_ADDRL)); 243 1.4 skrll printf("%s: addrh %#"PRIx32"\n", __func__, DART_READ(sc, DART_ERR_ADDRH)); 244 1.4 skrll #endif 245 1.4 skrll 246 1.4 skrll const char *reason = NULL; 247 1.4 skrll int32_t code = __SHIFTOUT(status, DART_ERR_CODE_MASK); 248 1.4 skrll switch (code) { 249 1.4 skrll case DART_ERR_NOTTBR: 250 1.4 skrll reason = "no ttbr for address"; 251 1.4 skrll break; 252 1.4 skrll case DART_ERR_NOPMD: 253 1.4 skrll reason = "no pmd for address"; 254 1.4 skrll break; 255 1.4 skrll case DART_ERR_NOPTE: 256 1.4 skrll reason = "no pte for address"; 257 1.4 skrll break; 258 1.4 skrll case DART_ERR_WRITE_FAULT: 259 1.4 skrll reason = "write fault"; 260 1.4 skrll break; 261 1.4 skrll case DART_ERR_READ_FAULT: 262 1.4 skrll reason = "read fault"; 263 1.4 skrll break; 264 1.4 skrll } 265 1.1 jmcneill fdtbus_get_path(sc->sc_phandle, fdt_path, sizeof(fdt_path)); 266 1.1 jmcneill 267 1.4 skrll printf("%s (%s): error addr 0x%016lx status 0x%08x: %s\n", 268 1.4 skrll device_xname(sc->sc_dev), fdt_path, addr, status, reason); 269 1.1 jmcneill 270 1.1 jmcneill return 1; 271 1.1 jmcneill } 272 1.1 jmcneill 273 1.1 jmcneill static volatile uint64_t * 274 1.1 jmcneill apple_dart_lookup_tte(struct apple_dart_softc *sc, bus_addr_t dva) 275 1.1 jmcneill { 276 1.1 jmcneill int idx = dva / DART_PAGE_SIZE; 277 1.1 jmcneill int l2_idx = idx / (DART_PAGE_SIZE / sizeof(uint64_t)); 278 1.1 jmcneill int tte_idx = idx % (DART_PAGE_SIZE / sizeof(uint64_t)); 279 1.4 skrll volatile uint64_t *l2 = DART_DMA_KVA(sc->sc_l2[l2_idx]); 280 1.1 jmcneill 281 1.1 jmcneill return &l2[tte_idx]; 282 1.1 jmcneill } 283 1.1 jmcneill 284 1.1 jmcneill static void 285 1.1 jmcneill apple_dart_unload_map(struct apple_dart_softc *sc, bus_dmamap_t map) 286 1.1 jmcneill { 287 1.1 jmcneill struct apple_dart_map_state *ams = map->_dm_iommu; 288 1.1 jmcneill volatile uint64_t *tte; 289 1.1 jmcneill int seg; 290 1.1 jmcneill 291 1.1 jmcneill /* For each segment */ 292 1.1 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) { 293 1.1 jmcneill u_long len, dva; 294 1.1 jmcneill 295 1.1 jmcneill if (ams[seg].ams_len == 0) { 296 1.1 jmcneill continue; 297 1.1 jmcneill } 298 1.1 jmcneill 299 1.1 jmcneill dva = ams[seg].ams_dva; 300 1.1 jmcneill len = ams[seg].ams_len; 301 1.1 jmcneill 302 1.1 jmcneill while (len > 0) { 303 1.1 jmcneill tte = apple_dart_lookup_tte(sc, dva); 304 1.1 jmcneill *tte = DART_L2_INVAL; 305 1.1 jmcneill 306 1.1 jmcneill dva += DART_PAGE_SIZE; 307 1.1 jmcneill len -= DART_PAGE_SIZE; 308 1.1 jmcneill } 309 1.1 jmcneill 310 1.1 jmcneill vmem_xfree(sc->sc_dvamap, ams[seg].ams_dva, ams[seg].ams_len); 311 1.1 jmcneill 312 1.1 jmcneill ams[seg].ams_dva = 0; 313 1.1 jmcneill ams[seg].ams_len = 0; 314 1.1 jmcneill } 315 1.1 jmcneill 316 1.1 jmcneill apple_dart_flush_tlb(sc); 317 1.1 jmcneill } 318 1.1 jmcneill 319 1.1 jmcneill static int 320 1.1 jmcneill apple_dart_load_map(struct apple_dart_softc *sc, bus_dmamap_t map) 321 1.1 jmcneill { 322 1.1 jmcneill struct apple_dart_map_state *ams = map->_dm_iommu; 323 1.1 jmcneill volatile uint64_t *tte; 324 1.1 jmcneill int seg, error; 325 1.1 jmcneill 326 1.1 jmcneill /* For each segment */ 327 1.1 jmcneill for (seg = 0; seg < map->dm_nsegs; seg++) { 328 1.1 jmcneill paddr_t pa = map->dm_segs[seg]._ds_paddr; 329 1.1 jmcneill psize_t off = pa - DART_TRUNC_PAGE(pa); 330 1.1 jmcneill u_long len, dva; 331 1.1 jmcneill 332 1.1 jmcneill len = DART_ROUND_PAGE(map->dm_segs[seg].ds_len + off); 333 1.1 jmcneill 334 1.1 jmcneill #ifdef APPLE_DART_DEBUG 335 1.1 jmcneill device_printf(sc->sc_dev, "load pa=%#lx off=%lu len=%lu ", 336 1.1 jmcneill pa, off, len); 337 1.1 jmcneill #endif 338 1.1 jmcneill 339 1.1 jmcneill error = vmem_xalloc(sc->sc_dvamap, len, DART_PAGE_SIZE, 0, 340 1.1 jmcneill 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_BESTFIT|VM_NOSLEEP, 341 1.1 jmcneill &dva); 342 1.1 jmcneill if (error != 0) { 343 1.1 jmcneill apple_dart_unload_map(sc, map); 344 1.1 jmcneill #ifdef APPLE_DART_DEBUG 345 1.1 jmcneill printf("error=%d\n", error); 346 1.1 jmcneill #endif 347 1.1 jmcneill return error; 348 1.1 jmcneill } 349 1.1 jmcneill 350 1.1 jmcneill #ifdef APPLE_DART_DEBUG 351 1.1 jmcneill printf("dva=%#lx\n", dva); 352 1.1 jmcneill #endif 353 1.1 jmcneill 354 1.1 jmcneill ams[seg].ams_dva = dva; 355 1.1 jmcneill ams[seg].ams_len = len; 356 1.1 jmcneill 357 1.1 jmcneill map->dm_segs[seg].ds_addr = dva + off; 358 1.1 jmcneill 359 1.1 jmcneill pa = DART_TRUNC_PAGE(pa); 360 1.4 skrll paddr_t start = DART_TRUNC_OFFSET(off); 361 1.4 skrll paddr_t end = DART_PAGE_MASK; 362 1.1 jmcneill while (len > 0) { 363 1.1 jmcneill tte = apple_dart_lookup_tte(sc, dva); 364 1.4 skrll if (len < DART_PAGE_SIZE) 365 1.4 skrll end = DART_ROUND_OFFSET(len) - 1; 366 1.1 jmcneill 367 1.4 skrll *tte = pa | DART_L2_VALID | 368 1.4 skrll DART_L2_START(start) | DART_L2_END(end); 369 1.4 skrll #ifdef APPLE_DART_DEBUG 370 1.4 skrll printf("tte %p = %"PRIx64"\n", tte, *tte); 371 1.4 skrll #endif 372 1.1 jmcneill pa += DART_PAGE_SIZE; 373 1.1 jmcneill dva += DART_PAGE_SIZE; 374 1.1 jmcneill len -= DART_PAGE_SIZE; 375 1.4 skrll start = 0; 376 1.1 jmcneill } 377 1.1 jmcneill } 378 1.1 jmcneill 379 1.1 jmcneill apple_dart_flush_tlb(sc); 380 1.1 jmcneill 381 1.1 jmcneill return 0; 382 1.1 jmcneill } 383 1.1 jmcneill 384 1.1 jmcneill static int 385 1.1 jmcneill apple_dart_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 386 1.1 jmcneill bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamap) 387 1.1 jmcneill { 388 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 389 1.1 jmcneill struct apple_dart_map_state *ams; 390 1.1 jmcneill bus_dmamap_t map; 391 1.1 jmcneill int error; 392 1.1 jmcneill 393 1.1 jmcneill error = sc->sc_dmat->_dmamap_create(sc->sc_dmat, size, nsegments, 394 1.1 jmcneill maxsegsz, boundary, flags, &map); 395 1.1 jmcneill if (error != 0) { 396 1.1 jmcneill return error; 397 1.1 jmcneill } 398 1.1 jmcneill 399 1.1 jmcneill ams = kmem_zalloc(map->_dm_segcnt * sizeof(*ams), 400 1.1 jmcneill (flags & BUS_DMA_NOWAIT) != 0 ? KM_NOSLEEP : KM_SLEEP); 401 1.1 jmcneill if (ams == NULL) { 402 1.1 jmcneill sc->sc_dmat->_dmamap_destroy(sc->sc_dmat, map); 403 1.1 jmcneill return ENOMEM; 404 1.1 jmcneill } 405 1.1 jmcneill 406 1.1 jmcneill map->_dm_iommu = ams; 407 1.1 jmcneill *dmamap = map; 408 1.1 jmcneill return 0; 409 1.1 jmcneill } 410 1.1 jmcneill 411 1.1 jmcneill static void 412 1.1 jmcneill apple_dart_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 413 1.1 jmcneill { 414 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 415 1.1 jmcneill struct apple_dart_map_state *ams = map->_dm_iommu; 416 1.1 jmcneill 417 1.1 jmcneill kmem_free(ams, map->_dm_segcnt * sizeof(*ams)); 418 1.1 jmcneill sc->sc_dmat->_dmamap_destroy(sc->sc_dmat, map); 419 1.1 jmcneill } 420 1.1 jmcneill 421 1.1 jmcneill static int 422 1.1 jmcneill apple_dart_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 423 1.1 jmcneill size_t buflen, struct proc *p, int flags) 424 1.1 jmcneill { 425 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 426 1.1 jmcneill int error; 427 1.1 jmcneill 428 1.1 jmcneill error = sc->sc_dmat->_dmamap_load(sc->sc_dmat, map, 429 1.1 jmcneill buf, buflen, p, flags); 430 1.1 jmcneill if (error != 0) { 431 1.1 jmcneill return error; 432 1.1 jmcneill } 433 1.1 jmcneill 434 1.1 jmcneill error = apple_dart_load_map(sc, map); 435 1.1 jmcneill if (error != 0) { 436 1.1 jmcneill sc->sc_dmat->_dmamap_unload(sc->sc_dmat, map); 437 1.1 jmcneill } 438 1.1 jmcneill 439 1.1 jmcneill return error; 440 1.1 jmcneill } 441 1.1 jmcneill 442 1.1 jmcneill static int 443 1.1 jmcneill apple_dart_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, 444 1.1 jmcneill struct mbuf *m, int flags) 445 1.1 jmcneill { 446 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 447 1.1 jmcneill int error; 448 1.1 jmcneill 449 1.1 jmcneill error = sc->sc_dmat->_dmamap_load_mbuf(sc->sc_dmat, map, 450 1.1 jmcneill m, flags); 451 1.1 jmcneill if (error != 0) { 452 1.1 jmcneill return error; 453 1.1 jmcneill } 454 1.1 jmcneill 455 1.1 jmcneill error = apple_dart_load_map(sc, map); 456 1.1 jmcneill if (error != 0) { 457 1.1 jmcneill sc->sc_dmat->_dmamap_unload(sc->sc_dmat, map); 458 1.1 jmcneill } 459 1.1 jmcneill 460 1.1 jmcneill return error; 461 1.1 jmcneill } 462 1.1 jmcneill 463 1.1 jmcneill static int 464 1.1 jmcneill apple_dart_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, 465 1.1 jmcneill struct uio *uio, int flags) 466 1.1 jmcneill { 467 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 468 1.1 jmcneill int error; 469 1.1 jmcneill 470 1.1 jmcneill error = sc->sc_dmat->_dmamap_load_uio(sc->sc_dmat, map, 471 1.1 jmcneill uio, flags); 472 1.1 jmcneill if (error != 0) { 473 1.1 jmcneill return error; 474 1.1 jmcneill } 475 1.1 jmcneill 476 1.1 jmcneill error = apple_dart_load_map(sc, map); 477 1.1 jmcneill if (error != 0) { 478 1.1 jmcneill sc->sc_dmat->_dmamap_unload(sc->sc_dmat, map); 479 1.1 jmcneill } 480 1.1 jmcneill 481 1.1 jmcneill return error; 482 1.1 jmcneill } 483 1.1 jmcneill 484 1.1 jmcneill static int 485 1.1 jmcneill apple_dart_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, 486 1.1 jmcneill bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 487 1.1 jmcneill { 488 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 489 1.1 jmcneill int error; 490 1.1 jmcneill 491 1.1 jmcneill error = sc->sc_dmat->_dmamap_load_raw(sc->sc_dmat, map, 492 1.1 jmcneill segs, nsegs, size, flags); 493 1.1 jmcneill if (error != 0) { 494 1.1 jmcneill return error; 495 1.1 jmcneill } 496 1.1 jmcneill 497 1.1 jmcneill error = apple_dart_load_map(sc, map); 498 1.1 jmcneill if (error != 0) { 499 1.1 jmcneill sc->sc_dmat->_dmamap_unload(sc->sc_dmat, map); 500 1.1 jmcneill } 501 1.1 jmcneill 502 1.1 jmcneill return error; 503 1.1 jmcneill } 504 1.1 jmcneill 505 1.1 jmcneill static void 506 1.1 jmcneill apple_dart_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map) 507 1.1 jmcneill { 508 1.1 jmcneill struct apple_dart_softc *sc = t->_cookie; 509 1.1 jmcneill 510 1.1 jmcneill apple_dart_unload_map(sc, map); 511 1.1 jmcneill sc->sc_dmat->_dmamap_unload(sc->sc_dmat, map); 512 1.1 jmcneill } 513 1.1 jmcneill 514 1.2 jmcneill static bus_dma_tag_t 515 1.2 jmcneill apple_dart_iommu_map(device_t dev, const u_int *data, bus_dma_tag_t dmat) 516 1.2 jmcneill { 517 1.2 jmcneill struct apple_dart_softc * const sc = device_private(dev); 518 1.2 jmcneill 519 1.2 jmcneill return &sc->sc_bus_dmat; 520 1.2 jmcneill } 521 1.2 jmcneill 522 1.2 jmcneill const struct fdtbus_iommu_func apple_dart_iommu_funcs = { 523 1.2 jmcneill .map = apple_dart_iommu_map, 524 1.2 jmcneill }; 525 1.2 jmcneill 526 1.1 jmcneill static int 527 1.1 jmcneill apple_dart_match(device_t parent, cfdata_t cf, void *aux) 528 1.1 jmcneill { 529 1.1 jmcneill struct fdt_attach_args * const faa = aux; 530 1.1 jmcneill 531 1.1 jmcneill return of_compatible_match(faa->faa_phandle, compat_data); 532 1.1 jmcneill } 533 1.1 jmcneill 534 1.1 jmcneill static void 535 1.1 jmcneill apple_dart_attach(device_t parent, device_t self, void *aux) 536 1.1 jmcneill { 537 1.1 jmcneill struct apple_dart_softc * const sc = device_private(self); 538 1.1 jmcneill struct fdt_attach_args * const faa = aux; 539 1.1 jmcneill const int phandle = faa->faa_phandle; 540 1.1 jmcneill char intrstr[128]; 541 1.1 jmcneill volatile uint64_t *l1; 542 1.1 jmcneill bus_addr_t addr; 543 1.1 jmcneill bus_size_t size; 544 1.1 jmcneill u_int sid, idx; 545 1.1 jmcneill paddr_t pa; 546 1.1 jmcneill void *ih; 547 1.1 jmcneill 548 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 549 1.1 jmcneill aprint_error(": couldn't get registers\n"); 550 1.1 jmcneill return; 551 1.1 jmcneill } 552 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 553 1.1 jmcneill aprint_error(": couldn't decode interrupt\n"); 554 1.1 jmcneill return; 555 1.1 jmcneill } 556 1.1 jmcneill 557 1.1 jmcneill sc->sc_dev = self; 558 1.1 jmcneill sc->sc_phandle = phandle; 559 1.1 jmcneill sc->sc_dmat = faa->faa_dmat; 560 1.1 jmcneill sc->sc_bst = faa->faa_bst; 561 1.3 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 562 1.1 jmcneill aprint_error(": couldn't map registers\n"); 563 1.1 jmcneill return; 564 1.1 jmcneill } 565 1.1 jmcneill 566 1.4 skrll /* Skip locked DARTs for now. */ 567 1.4 skrll uint32_t config = DART_READ(sc, DART_CONFIG); 568 1.4 skrll if (config & DART_CONFIG_LOCK) { 569 1.4 skrll aprint_naive("\n"); 570 1.4 skrll aprint_normal(": locked\n"); 571 1.4 skrll return; 572 1.4 skrll } 573 1.4 skrll 574 1.4 skrll /* 575 1.4 skrll * Use bypass mode if supported. This avoids an issue with 576 1.4 skrll * the USB3 controllers which need mappings entered into two 577 1.4 skrll * IOMMUs, which is somewhat difficult to implement with our 578 1.4 skrll * current kernel interfaces. 579 1.4 skrll */ 580 1.4 skrll uint32_t params2 = DART_READ(sc, DART_PARAMS2); 581 1.4 skrll if (params2 & DART_PARAMS2_BYPASS_SUPPORT) { 582 1.4 skrll for (sid = 0; sid < DART_NUM_STREAMS; sid++) { 583 1.4 skrll DART_WRITE(sc, DART_TCR(sid), 584 1.4 skrll DART_TCR_BYPASS_DART | DART_TCR_BYPASS_DAPF); 585 1.4 skrll } 586 1.4 skrll aprint_naive("\n"); 587 1.4 skrll aprint_normal(": bypass\n"); 588 1.4 skrll return; 589 1.1 jmcneill } 590 1.1 jmcneill 591 1.4 skrll sc->sc_nsid = of_compatible_lookup(phandle, compat_data)->value; 592 1.4 skrll sc->sc_sid_mask = __MASK(sc->sc_nsid); 593 1.4 skrll 594 1.1 jmcneill aprint_naive("\n"); 595 1.1 jmcneill aprint_normal(": Apple DART @ %#lx/%#lx, %u SIDs (mask 0x%lx)\n", 596 1.1 jmcneill addr, size, sc->sc_nsid, sc->sc_sid_mask); 597 1.1 jmcneill 598 1.1 jmcneill KASSERT(sc->sc_nsid == 16); 599 1.1 jmcneill KASSERT(sc->sc_sid_mask == 0xffff); 600 1.1 jmcneill 601 1.1 jmcneill sc->sc_dvamap = vmem_create(device_xname(self), 602 1.1 jmcneill DART_APERTURE_START, DART_APERTURE_SIZE, DART_PAGE_SIZE, 603 1.1 jmcneill NULL, NULL, NULL, 0, VM_SLEEP, IPL_HIGH); 604 1.1 jmcneill if (sc->sc_dvamap == NULL) { 605 1.1 jmcneill aprint_error_dev(self, "couldn't allocate DVA map\n"); 606 1.1 jmcneill return; 607 1.1 jmcneill } 608 1.1 jmcneill 609 1.1 jmcneill /* Disable translations */ 610 1.1 jmcneill for (sid = 0; sid < sc->sc_nsid; sid++) { 611 1.4 skrll DART_WRITE(sc, DART_TCR(sid), 0); 612 1.1 jmcneill } 613 1.1 jmcneill 614 1.1 jmcneill /* Remove page tables */ 615 1.1 jmcneill for (sid = 0; sid < sc->sc_nsid; sid++) { 616 1.1 jmcneill for (idx = 0; idx < 4; idx++) { 617 1.1 jmcneill DART_WRITE(sc, DART_TTBR(sid, idx), 0); 618 1.1 jmcneill } 619 1.1 jmcneill } 620 1.1 jmcneill apple_dart_flush_tlb(sc); 621 1.1 jmcneill 622 1.1 jmcneill /* 623 1.1 jmcneill * Build translation tables. We pre-allocate the translation 624 1.1 jmcneill * tables for the entire aperture such that we don't have to worry 625 1.1 jmcneill * about growing them in an mpsafe manner later. 626 1.4 skrll * 627 1.4 skrll * Cover the entire address space [0, ..._START + ..._SIZE) even if vmem 628 1.4 skrll * only allocates from [..._START, ..._START + ...+SIZE) 629 1.1 jmcneill */ 630 1.1 jmcneill 631 1.1 jmcneill const u_int ntte = howmany(DART_APERTURE_START + DART_APERTURE_SIZE - 1, 632 1.1 jmcneill DART_PAGE_SIZE); 633 1.1 jmcneill const u_int nl2 = howmany(ntte, DART_PAGE_SIZE / sizeof(uint64_t)); 634 1.1 jmcneill const u_int nl1 = howmany(nl2, DART_PAGE_SIZE / sizeof(uint64_t)); 635 1.1 jmcneill 636 1.1 jmcneill sc->sc_l1 = apple_dart_dma_alloc(sc->sc_dmat, 637 1.1 jmcneill nl1 * DART_PAGE_SIZE, DART_PAGE_SIZE); 638 1.1 jmcneill if (sc->sc_l1 == NULL) { 639 1.1 jmcneill aprint_error_dev(self, "couldn't allocate L1 tables\n"); 640 1.1 jmcneill return; 641 1.1 jmcneill } 642 1.1 jmcneill sc->sc_l2 = kmem_zalloc(nl2 * sizeof(*sc->sc_l2), KM_SLEEP); 643 1.1 jmcneill sc->sc_nl2 = nl2; 644 1.1 jmcneill 645 1.1 jmcneill l1 = DART_DMA_KVA(sc->sc_l1); 646 1.1 jmcneill for (idx = 0; idx < nl2; idx++) { 647 1.1 jmcneill sc->sc_l2[idx] = apple_dart_dma_alloc(sc->sc_dmat, 648 1.1 jmcneill DART_PAGE_SIZE, DART_PAGE_SIZE); 649 1.1 jmcneill if (sc->sc_l2[idx] == NULL) { 650 1.1 jmcneill aprint_error_dev(self, 651 1.1 jmcneill "couldn't allocate L2 tables\n"); 652 1.1 jmcneill return; 653 1.1 jmcneill } 654 1.4 skrll 655 1.1 jmcneill l1[idx] = DART_DMA_DVA(sc->sc_l2[idx]) | DART_L1_TABLE; 656 1.4 skrll #ifdef APPLE_DART_DEBUG 657 1.4 skrll printf("l1[%d] (%p) = %"PRIx64"\n", idx, &l1[idx], l1[idx]); 658 1.4 skrll #endif 659 1.1 jmcneill } 660 1.1 jmcneill 661 1.1 jmcneill /* Install page tables */ 662 1.1 jmcneill for (sid = 0; sid < sc->sc_nsid; sid++) { 663 1.1 jmcneill pa = DART_DMA_DVA(sc->sc_l1); 664 1.1 jmcneill for (idx = 0; idx < nl1; idx++) { 665 1.4 skrll KASSERTMSG(__SHIFTOUT(pa, __BITS(DART_TTBR_SHIFT - 1, 0)) == 0, 666 1.4 skrll "TTBR pa is not correctly aligned %" PRIxPADDR, pa); 667 1.4 skrll 668 1.1 jmcneill DART_WRITE(sc, DART_TTBR(sid, idx), 669 1.1 jmcneill (pa >> DART_TTBR_SHIFT) | DART_TTBR_VALID); 670 1.1 jmcneill pa += DART_PAGE_SIZE; 671 1.4 skrll #ifdef APPLE_DART_DEBUG 672 1.4 skrll printf("writing %"PRIx64" to %"PRIx32"\n", 673 1.4 skrll (pa >> DART_TTBR_SHIFT) | DART_TTBR_VALID, 674 1.4 skrll DART_TTBR(sid, idx)); 675 1.4 skrll #endif 676 1.1 jmcneill } 677 1.1 jmcneill } 678 1.1 jmcneill apple_dart_flush_tlb(sc); 679 1.1 jmcneill 680 1.1 jmcneill /* Enable translations */ 681 1.1 jmcneill for (sid = 0; sid < sc->sc_nsid; sid++) { 682 1.4 skrll DART_WRITE(sc, DART_TCR(sid), DART_TCR_TRANSLATE_ENABLE); 683 1.1 jmcneill } 684 1.1 jmcneill 685 1.1 jmcneill ih = fdtbus_intr_establish_xname(phandle, 0, IPL_HIGH, FDT_INTR_MPSAFE, 686 1.1 jmcneill apple_dart_intr, sc, device_xname(self)); 687 1.1 jmcneill if (ih == NULL) { 688 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n", 689 1.1 jmcneill intrstr); 690 1.1 jmcneill return; 691 1.1 jmcneill } 692 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr); 693 1.1 jmcneill 694 1.1 jmcneill /* Setup bus DMA tag */ 695 1.1 jmcneill sc->sc_bus_dmat = *sc->sc_dmat; 696 1.1 jmcneill sc->sc_bus_dmat._ranges = apple_dart_dma_ranges; 697 1.1 jmcneill sc->sc_bus_dmat._nranges = 1; 698 1.1 jmcneill sc->sc_bus_dmat._cookie = sc; 699 1.1 jmcneill sc->sc_bus_dmat._dmamap_create = apple_dart_dmamap_create; 700 1.1 jmcneill sc->sc_bus_dmat._dmamap_destroy = apple_dart_dmamap_destroy; 701 1.1 jmcneill sc->sc_bus_dmat._dmamap_load = apple_dart_dmamap_load; 702 1.1 jmcneill sc->sc_bus_dmat._dmamap_load_mbuf = apple_dart_dmamap_load_mbuf; 703 1.1 jmcneill sc->sc_bus_dmat._dmamap_load_uio = apple_dart_dmamap_load_uio; 704 1.1 jmcneill sc->sc_bus_dmat._dmamap_load_raw = apple_dart_dmamap_load_raw; 705 1.1 jmcneill sc->sc_bus_dmat._dmamap_unload = apple_dart_dmamap_unload; 706 1.1 jmcneill 707 1.2 jmcneill fdtbus_register_iommu(self, phandle, &apple_dart_iommu_funcs); 708 1.1 jmcneill } 709 1.1 jmcneill 710 1.1 jmcneill CFATTACH_DECL_NEW(apple_dart, sizeof(struct apple_dart_softc), 711 1.1 jmcneill apple_dart_match, apple_dart_attach, NULL, NULL); 712