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