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