iommu.c revision 1.51.4.4 1 1.51.4.4 lukem /* $NetBSD: iommu.c,v 1.51.4.4 2002/06/24 22:59:48 lukem Exp $ */
2 1.7 mrg
3 1.7 mrg /*
4 1.48 eeh * Copyright (c) 2001, 2002 Eduardo Horvath
5 1.7 mrg * Copyright (c) 1999, 2000 Matthew R. Green
6 1.7 mrg * All rights reserved.
7 1.7 mrg *
8 1.7 mrg * Redistribution and use in source and binary forms, with or without
9 1.7 mrg * modification, are permitted provided that the following conditions
10 1.7 mrg * are met:
11 1.7 mrg * 1. Redistributions of source code must retain the above copyright
12 1.7 mrg * notice, this list of conditions and the following disclaimer.
13 1.7 mrg * 2. Redistributions in binary form must reproduce the above copyright
14 1.7 mrg * notice, this list of conditions and the following disclaimer in the
15 1.7 mrg * documentation and/or other materials provided with the distribution.
16 1.7 mrg * 3. The name of the author may not be used to endorse or promote products
17 1.7 mrg * derived from this software without specific prior written permission.
18 1.7 mrg *
19 1.7 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.7 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.7 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.7 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.7 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.7 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.7 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.7 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.7 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.7 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.7 mrg * SUCH DAMAGE.
30 1.7 mrg */
31 1.1 mrg
32 1.7 mrg /*
33 1.7 mrg * UltraSPARC IOMMU support; used by both the sbus and pci code.
34 1.7 mrg */
35 1.4 mrg #include "opt_ddb.h"
36 1.4 mrg
37 1.1 mrg #include <sys/param.h>
38 1.1 mrg #include <sys/extent.h>
39 1.1 mrg #include <sys/malloc.h>
40 1.1 mrg #include <sys/systm.h>
41 1.1 mrg #include <sys/device.h>
42 1.41 chs #include <sys/proc.h>
43 1.18 mrg
44 1.18 mrg #include <uvm/uvm_extern.h>
45 1.1 mrg
46 1.1 mrg #include <machine/bus.h>
47 1.7 mrg #include <sparc64/sparc64/cache.h>
48 1.1 mrg #include <sparc64/dev/iommureg.h>
49 1.1 mrg #include <sparc64/dev/iommuvar.h>
50 1.1 mrg
51 1.1 mrg #include <machine/autoconf.h>
52 1.1 mrg #include <machine/cpu.h>
53 1.1 mrg
54 1.1 mrg #ifdef DEBUG
55 1.22 mrg #define IDB_BUSDMA 0x1
56 1.22 mrg #define IDB_IOMMU 0x2
57 1.22 mrg #define IDB_INFO 0x4
58 1.36 eeh #define IDB_SYNC 0x8
59 1.10 mrg int iommudebug = 0x0;
60 1.4 mrg #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
61 1.4 mrg #else
62 1.4 mrg #define DPRINTF(l, s)
63 1.1 mrg #endif
64 1.1 mrg
65 1.51.4.3 lukem #define iommu_strbuf_flush(i, v) do { \
66 1.51.4.3 lukem if ((i)->sb_flush) \
67 1.51.4.3 lukem bus_space_write_8((i)->sb_is->is_bustag, (i)->sb_sb, \
68 1.50 eeh STRBUFREG(strbuf_pgflush), (v)); \
69 1.42 eeh } while (0)
70 1.42 eeh
71 1.51.4.3 lukem static int iommu_strbuf_flush_done __P((struct strbuf_ctl *));
72 1.11 eeh
73 1.1 mrg /*
74 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
75 1.1 mrg * - allocate and setup the iotsb.
76 1.1 mrg * - enable the IOMMU
77 1.7 mrg * - initialise the streaming buffers (if they exist)
78 1.1 mrg * - create a private DVMA map.
79 1.1 mrg */
80 1.1 mrg void
81 1.36 eeh iommu_init(name, is, tsbsize, iovabase)
82 1.1 mrg char *name;
83 1.1 mrg struct iommu_state *is;
84 1.1 mrg int tsbsize;
85 1.36 eeh u_int32_t iovabase;
86 1.1 mrg {
87 1.11 eeh psize_t size;
88 1.11 eeh vaddr_t va;
89 1.11 eeh paddr_t pa;
90 1.35 chs struct vm_page *m;
91 1.11 eeh struct pglist mlist;
92 1.1 mrg
93 1.1 mrg /*
94 1.1 mrg * Setup the iommu.
95 1.1 mrg *
96 1.45 eeh * The sun4u iommu is part of the SBUS or PCI controller so we will
97 1.45 eeh * deal with it here..
98 1.1 mrg *
99 1.45 eeh * For sysio and psycho/psycho+ the IOMMU address space always ends at
100 1.45 eeh * 0xffffe000, but the starting address depends on the size of the
101 1.45 eeh * map. The map size is 1024 * 2 ^ is->is_tsbsize entries, where each
102 1.45 eeh * entry is 8 bytes. The start of the map can be calculated by
103 1.45 eeh * (0xffffe000 << (8 + is->is_tsbsize)).
104 1.45 eeh *
105 1.45 eeh * But sabre and hummingbird use a different scheme that seems to
106 1.45 eeh * be hard-wired, so we read the start and size from the PROM and
107 1.45 eeh * just use those values.
108 1.2 eeh */
109 1.11 eeh is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
110 1.11 eeh is->is_tsbsize = tsbsize;
111 1.45 eeh if (iovabase == -1) {
112 1.45 eeh is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
113 1.45 eeh is->is_dvmaend = IOTSB_VEND;
114 1.45 eeh } else {
115 1.45 eeh is->is_dvmabase = iovabase;
116 1.45 eeh is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize);
117 1.45 eeh }
118 1.11 eeh
119 1.11 eeh /*
120 1.15 eeh * Allocate memory for I/O pagetables. They need to be physically
121 1.15 eeh * contiguous.
122 1.11 eeh */
123 1.11 eeh
124 1.11 eeh size = NBPG<<(is->is_tsbsize);
125 1.11 eeh TAILQ_INIT(&mlist);
126 1.11 eeh if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
127 1.11 eeh (paddr_t)NBPG, (paddr_t)0, &mlist, 1, 0) != 0)
128 1.11 eeh panic("iommu_init: no memory");
129 1.11 eeh
130 1.11 eeh va = uvm_km_valloc(kernel_map, size);
131 1.11 eeh if (va == 0)
132 1.11 eeh panic("iommu_init: no memory");
133 1.11 eeh is->is_tsb = (int64_t *)va;
134 1.11 eeh
135 1.11 eeh m = TAILQ_FIRST(&mlist);
136 1.11 eeh is->is_ptsb = VM_PAGE_TO_PHYS(m);
137 1.11 eeh
138 1.11 eeh /* Map the pages */
139 1.11 eeh for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
140 1.11 eeh pa = VM_PAGE_TO_PHYS(m);
141 1.11 eeh pmap_enter(pmap_kernel(), va, pa | PMAP_NVC,
142 1.11 eeh VM_PROT_READ|VM_PROT_WRITE,
143 1.11 eeh VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
144 1.11 eeh va += NBPG;
145 1.11 eeh }
146 1.38 chris pmap_update(pmap_kernel());
147 1.11 eeh bzero(is->is_tsb, size);
148 1.1 mrg
149 1.1 mrg #ifdef DEBUG
150 1.22 mrg if (iommudebug & IDB_INFO)
151 1.1 mrg {
152 1.1 mrg /* Probe the iommu */
153 1.1 mrg
154 1.25 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
155 1.50 eeh (u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
156 1.50 eeh offsetof (struct iommureg, iommu_cr)),
157 1.50 eeh (u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
158 1.50 eeh offsetof (struct iommureg, iommu_tsb)),
159 1.50 eeh (u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
160 1.50 eeh offsetof (struct iommureg, iommu_flush)));
161 1.50 eeh printf("iommu cr=%llx tsb=%llx\n",
162 1.50 eeh (unsigned long long)bus_space_read_8(is->is_bustag,
163 1.50 eeh is->is_iommu,
164 1.50 eeh offsetof (struct iommureg, iommu_cr)),
165 1.50 eeh (unsigned long long)bus_space_read_8(is->is_bustag,
166 1.50 eeh is->is_iommu,
167 1.50 eeh offsetof (struct iommureg, iommu_tsb)));
168 1.50 eeh printf("TSB base %p phys %llx\n", (void *)is->is_tsb,
169 1.50 eeh (unsigned long long)is->is_ptsb);
170 1.1 mrg delay(1000000); /* 1 s */
171 1.1 mrg }
172 1.1 mrg #endif
173 1.1 mrg
174 1.1 mrg /*
175 1.1 mrg * now actually start up the IOMMU
176 1.1 mrg */
177 1.1 mrg iommu_reset(is);
178 1.1 mrg
179 1.1 mrg /*
180 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
181 1.1 mrg */
182 1.11 eeh printf("DVMA map: %x to %x\n",
183 1.11 eeh (unsigned int)is->is_dvmabase,
184 1.45 eeh (unsigned int)is->is_dvmaend);
185 1.47 eeh printf("IOTSB: %llx to %llx\n",
186 1.47 eeh (unsigned long long)is->is_ptsb,
187 1.47 eeh (unsigned long long)(is->is_ptsb + size));
188 1.1 mrg is->is_dvmamap = extent_create(name,
189 1.45 eeh is->is_dvmabase, is->is_dvmaend - NBPG,
190 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
191 1.1 mrg }
192 1.1 mrg
193 1.8 mrg /*
194 1.8 mrg * Streaming buffers don't exist on the UltraSPARC IIi; we should have
195 1.8 mrg * detected that already and disabled them. If not, we will notice that
196 1.8 mrg * they aren't there when the STRBUF_EN bit does not remain.
197 1.8 mrg */
198 1.1 mrg void
199 1.1 mrg iommu_reset(is)
200 1.1 mrg struct iommu_state *is;
201 1.1 mrg {
202 1.45 eeh int i;
203 1.51.4.3 lukem struct strbuf_ctl *sb;
204 1.1 mrg
205 1.1 mrg /* Need to do 64-bit stores */
206 1.50 eeh bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_tsb),
207 1.50 eeh is->is_ptsb);
208 1.50 eeh
209 1.11 eeh /* Enable IOMMU in diagnostic mode */
210 1.50 eeh bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_cr),
211 1.50 eeh is->is_cr|IOMMUCR_DE);
212 1.11 eeh
213 1.45 eeh for (i=0; i<2; i++) {
214 1.51.4.3 lukem if ((sb = is->is_sb[i])) {
215 1.5 mrg
216 1.45 eeh /* Enable diagnostics mode? */
217 1.51.4.3 lukem bus_space_write_8(is->is_bustag, is->is_sb[i]->sb_sb,
218 1.50 eeh STRBUFREG(strbuf_ctl), STRBUF_EN);
219 1.45 eeh
220 1.45 eeh /* No streaming buffers? Disable them */
221 1.51.4.3 lukem if (bus_space_read_8(is->is_bustag,
222 1.51.4.3 lukem is->is_sb[i]->sb_sb,
223 1.51.4.3 lukem STRBUFREG(strbuf_ctl)) == 0) {
224 1.51.4.3 lukem is->is_sb[i]->sb_flush = NULL;
225 1.51.4.3 lukem } else {
226 1.51.4.3 lukem /*
227 1.51.4.3 lukem * locate the pa of the flush buffer.
228 1.51.4.3 lukem */
229 1.51.4.3 lukem (void)pmap_extract(pmap_kernel(),
230 1.51.4.3 lukem (vaddr_t)is->is_sb[i]->sb_flush,
231 1.51.4.3 lukem &is->is_sb[i]->sb_flushpa);
232 1.51.4.3 lukem }
233 1.45 eeh }
234 1.42 eeh }
235 1.2 eeh }
236 1.2 eeh
237 1.2 eeh /*
238 1.2 eeh * Here are the iommu control routines.
239 1.2 eeh */
240 1.2 eeh void
241 1.51.4.3 lukem iommu_enter(sb, va, pa, flags)
242 1.51.4.3 lukem struct strbuf_ctl *sb;
243 1.2 eeh vaddr_t va;
244 1.2 eeh int64_t pa;
245 1.2 eeh int flags;
246 1.2 eeh {
247 1.51.4.3 lukem struct iommu_state *is = sb->sb_is;
248 1.51.4.3 lukem int strbuf = (flags & BUS_DMA_STREAMING);
249 1.2 eeh int64_t tte;
250 1.2 eeh
251 1.2 eeh #ifdef DIAGNOSTIC
252 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
253 1.13 mrg panic("iommu_enter: va %#lx not in DVMA space", va);
254 1.2 eeh #endif
255 1.2 eeh
256 1.51.4.3 lukem /* Is the streamcache flush really needed? */
257 1.51.4.3 lukem if (sb->sb_flush) {
258 1.51.4.3 lukem iommu_strbuf_flush(sb, va);
259 1.51.4.3 lukem iommu_strbuf_flush_done(sb);
260 1.51.4.3 lukem } else
261 1.51.4.3 lukem /* If we can't flush the strbuf don't enable it. */
262 1.51.4.3 lukem strbuf = 0;
263 1.51.4.3 lukem
264 1.51.4.2 lukem tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
265 1.51.4.3 lukem !(flags & BUS_DMA_NOCACHE), (strbuf));
266 1.50 eeh #ifdef DEBUG
267 1.50 eeh tte |= (flags & 0xff000LL)<<(4*8);
268 1.50 eeh #endif
269 1.2 eeh
270 1.22 mrg DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
271 1.25 mrg (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
272 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
273 1.50 eeh bus_space_write_8(is->is_bustag, is->is_iommu,
274 1.50 eeh IOMMUREG(iommu_flush), va);
275 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
276 1.50 eeh va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
277 1.50 eeh (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
278 1.50 eeh (u_long)tte));
279 1.39 eeh }
280 1.39 eeh
281 1.39 eeh
282 1.39 eeh /*
283 1.39 eeh * Find the value of a DVMA address (debug routine).
284 1.39 eeh */
285 1.39 eeh paddr_t
286 1.39 eeh iommu_extract(is, dva)
287 1.39 eeh struct iommu_state *is;
288 1.39 eeh vaddr_t dva;
289 1.39 eeh {
290 1.39 eeh int64_t tte = 0;
291 1.39 eeh
292 1.45 eeh if (dva >= is->is_dvmabase && dva < is->is_dvmaend)
293 1.51.4.3 lukem tte = is->is_tsb[IOTSBSLOT(dva, is->is_tsbsize)];
294 1.39 eeh
295 1.51.4.2 lukem if ((tte & IOTTE_V) == 0)
296 1.39 eeh return ((paddr_t)-1L);
297 1.51.4.2 lukem return (tte & IOTTE_PAMASK);
298 1.2 eeh }
299 1.2 eeh
300 1.2 eeh /*
301 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
302 1.2 eeh *
303 1.2 eeh * Only demap from IOMMU if flag is set.
304 1.8 mrg *
305 1.8 mrg * XXX: this function needs better internal error checking.
306 1.2 eeh */
307 1.2 eeh void
308 1.2 eeh iommu_remove(is, va, len)
309 1.2 eeh struct iommu_state *is;
310 1.2 eeh vaddr_t va;
311 1.2 eeh size_t len;
312 1.2 eeh {
313 1.2 eeh
314 1.2 eeh #ifdef DIAGNOSTIC
315 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
316 1.25 mrg panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
317 1.2 eeh if ((long)(va + len) < (long)va)
318 1.4 mrg panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
319 1.2 eeh (long) va, (long) len);
320 1.2 eeh if (len & ~0xfffffff)
321 1.25 mrg panic("iommu_remove: rediculous len 0x%lx", (u_long)len);
322 1.2 eeh #endif
323 1.2 eeh
324 1.2 eeh va = trunc_page(va);
325 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
326 1.50 eeh va, (u_long)IOTSBSLOT(va, is->is_tsbsize),
327 1.50 eeh &is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)]));
328 1.2 eeh while (len > 0) {
329 1.50 eeh DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d "
330 1.50 eeh "for va %p size %lx\n",
331 1.50 eeh (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va,
332 1.50 eeh (u_long)len));
333 1.10 mrg if (len <= NBPG)
334 1.10 mrg len = 0;
335 1.10 mrg else
336 1.8 mrg len -= NBPG;
337 1.8 mrg
338 1.47 eeh /* XXX Zero-ing the entry would not require RMW */
339 1.47 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
340 1.50 eeh bus_space_write_8(is->is_bustag, is->is_iommu,
341 1.50 eeh IOMMUREG(iommu_flush), va);
342 1.2 eeh va += NBPG;
343 1.2 eeh }
344 1.2 eeh }
345 1.2 eeh
346 1.14 mrg static int
347 1.51.4.3 lukem iommu_strbuf_flush_done(sb)
348 1.51.4.3 lukem struct strbuf_ctl *sb;
349 1.2 eeh {
350 1.51.4.3 lukem struct iommu_state *is = sb->sb_is;
351 1.2 eeh struct timeval cur, flushtimeout;
352 1.2 eeh
353 1.2 eeh #define BUMPTIME(t, usec) { \
354 1.2 eeh register volatile struct timeval *tp = (t); \
355 1.2 eeh register long us; \
356 1.2 eeh \
357 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
358 1.2 eeh if (us >= 1000000) { \
359 1.2 eeh tp->tv_usec = us - 1000000; \
360 1.2 eeh tp->tv_sec++; \
361 1.2 eeh } \
362 1.2 eeh }
363 1.5 mrg
364 1.51.4.3 lukem if (!sb->sb_flush)
365 1.5 mrg return (0);
366 1.7 mrg
367 1.7 mrg /*
368 1.7 mrg * Streaming buffer flushes:
369 1.7 mrg *
370 1.7 mrg * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
371 1.7 mrg * we're not on a cache line boundary (64-bits):
372 1.7 mrg * 2 Store 0 in flag
373 1.7 mrg * 3 Store pointer to flag in flushsync
374 1.7 mrg * 4 wait till flushsync becomes 0x1
375 1.7 mrg *
376 1.7 mrg * If it takes more than .5 sec, something
377 1.7 mrg * went wrong.
378 1.7 mrg */
379 1.2 eeh
380 1.51.4.3 lukem *sb->sb_flush = 0;
381 1.51.4.3 lukem bus_space_write_8(is->is_bustag, sb->sb_sb,
382 1.51.4.3 lukem STRBUFREG(strbuf_flushsync), sb->sb_flushpa);
383 1.2 eeh
384 1.2 eeh microtime(&flushtimeout);
385 1.2 eeh cur = flushtimeout;
386 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
387 1.2 eeh
388 1.51.4.3 lukem DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx "
389 1.42 eeh "at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
390 1.51.4.3 lukem (long)*sb->sb_flush, (long)sb->sb_flush, (long)sb->sb_flushpa,
391 1.42 eeh cur.tv_sec, cur.tv_usec,
392 1.42 eeh flushtimeout.tv_sec, flushtimeout.tv_usec));
393 1.42 eeh
394 1.2 eeh /* Bypass non-coherent D$ */
395 1.51.4.3 lukem while ((!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) &&
396 1.42 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
397 1.42 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
398 1.2 eeh microtime(&cur);
399 1.2 eeh
400 1.2 eeh #ifdef DIAGNOSTIC
401 1.51.4.3 lukem if (!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) {
402 1.51.4.3 lukem printf("iommu_strbuf_flush_done: flush timeout %p, at %p\n",
403 1.51.4.3 lukem (void *)(u_long)*sb->sb_flush,
404 1.51.4.3 lukem (void *)(u_long)sb->sb_flushpa); /* panic? */
405 1.2 eeh #ifdef DDB
406 1.2 eeh Debugger();
407 1.2 eeh #endif
408 1.2 eeh }
409 1.2 eeh #endif
410 1.31 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
411 1.51.4.3 lukem return (*sb->sb_flush);
412 1.7 mrg }
413 1.7 mrg
414 1.7 mrg /*
415 1.7 mrg * IOMMU DVMA operations, common to SBUS and PCI.
416 1.7 mrg */
417 1.7 mrg int
418 1.51.4.3 lukem iommu_dvmamap_load(t, sb, map, buf, buflen, p, flags)
419 1.7 mrg bus_dma_tag_t t;
420 1.51.4.3 lukem struct strbuf_ctl *sb;
421 1.7 mrg bus_dmamap_t map;
422 1.7 mrg void *buf;
423 1.7 mrg bus_size_t buflen;
424 1.7 mrg struct proc *p;
425 1.7 mrg int flags;
426 1.7 mrg {
427 1.51.4.3 lukem struct iommu_state *is = sb->sb_is;
428 1.7 mrg int s;
429 1.7 mrg int err;
430 1.7 mrg bus_size_t sgsize;
431 1.7 mrg paddr_t curaddr;
432 1.40 eeh u_long dvmaddr, sgstart, sgend;
433 1.21 eeh bus_size_t align, boundary;
434 1.7 mrg vaddr_t vaddr = (vaddr_t)buf;
435 1.40 eeh int seg;
436 1.7 mrg pmap_t pmap;
437 1.7 mrg
438 1.7 mrg if (map->dm_nsegs) {
439 1.7 mrg /* Already in use?? */
440 1.7 mrg #ifdef DIAGNOSTIC
441 1.7 mrg printf("iommu_dvmamap_load: map still in use\n");
442 1.7 mrg #endif
443 1.7 mrg bus_dmamap_unload(t, map);
444 1.7 mrg }
445 1.7 mrg /*
446 1.7 mrg * Make sure that on error condition we return "no valid mappings".
447 1.7 mrg */
448 1.7 mrg map->dm_nsegs = 0;
449 1.7 mrg
450 1.7 mrg if (buflen > map->_dm_size) {
451 1.22 mrg DPRINTF(IDB_BUSDMA,
452 1.7 mrg ("iommu_dvmamap_load(): error %d > %d -- "
453 1.25 mrg "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
454 1.7 mrg return (EINVAL);
455 1.7 mrg }
456 1.7 mrg
457 1.7 mrg sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
458 1.20 mrg
459 1.7 mrg /*
460 1.21 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
461 1.21 eeh * over boundary in the map.
462 1.7 mrg */
463 1.21 eeh if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
464 1.21 eeh boundary = map->_dm_boundary;
465 1.21 eeh align = max(map->dm_segs[0]._ds_align, NBPG);
466 1.7 mrg s = splhigh();
467 1.40 eeh /*
468 1.40 eeh * If our segment size is larger than the boundary we need to
469 1.40 eeh * split the transfer up int little pieces ourselves.
470 1.40 eeh */
471 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
472 1.40 eeh (sgsize > boundary) ? 0 : boundary,
473 1.51.4.2 lukem EX_NOWAIT|EX_BOUNDZERO, &dvmaddr);
474 1.7 mrg splx(s);
475 1.7 mrg
476 1.7 mrg #ifdef DEBUG
477 1.11 eeh if (err || (dvmaddr == (bus_addr_t)-1))
478 1.7 mrg {
479 1.7 mrg printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
480 1.25 mrg (int)sgsize, flags);
481 1.40 eeh #ifdef DDB
482 1.7 mrg Debugger();
483 1.40 eeh #endif
484 1.7 mrg }
485 1.7 mrg #endif
486 1.11 eeh if (err != 0)
487 1.11 eeh return (err);
488 1.11 eeh
489 1.7 mrg if (dvmaddr == (bus_addr_t)-1)
490 1.7 mrg return (ENOMEM);
491 1.7 mrg
492 1.40 eeh /* Set the active DVMA map */
493 1.40 eeh map->_dm_dvmastart = dvmaddr;
494 1.40 eeh map->_dm_dvmasize = sgsize;
495 1.40 eeh
496 1.40 eeh /*
497 1.40 eeh * Now split the DVMA range into segments, not crossing
498 1.40 eeh * the boundary.
499 1.40 eeh */
500 1.40 eeh seg = 0;
501 1.40 eeh sgstart = dvmaddr + (vaddr & PGOFSET);
502 1.40 eeh sgend = sgstart + buflen - 1;
503 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
504 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary-1 %lx "
505 1.40 eeh "~(boundary-1) %lx\n", boundary, (boundary-1), ~(boundary-1)));
506 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
507 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
508 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
509 1.40 eeh "seg %d start %lx size %lx\n", seg,
510 1.48 eeh (long)map->dm_segs[seg].ds_addr,
511 1.48 eeh map->dm_segs[seg].ds_len));
512 1.49 tsutsui map->dm_segs[seg].ds_len =
513 1.49 tsutsui boundary - (sgstart & (boundary - 1));
514 1.51.4.1 lukem if (++seg >= map->_dm_segcnt) {
515 1.40 eeh /* Too many segments. Fail the operation. */
516 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
517 1.40 eeh "too many segments %d\n", seg));
518 1.40 eeh s = splhigh();
519 1.40 eeh /* How can this fail? And if it does what can we do? */
520 1.40 eeh err = extent_free(is->is_dvmamap,
521 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
522 1.40 eeh map->_dm_dvmastart = 0;
523 1.40 eeh map->_dm_dvmasize = 0;
524 1.43 eeh splx(s);
525 1.40 eeh return (E2BIG);
526 1.40 eeh }
527 1.40 eeh sgstart = roundup(sgstart, boundary);
528 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
529 1.40 eeh }
530 1.40 eeh map->dm_segs[seg].ds_len = sgend - sgstart + 1;
531 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
532 1.40 eeh "seg %d start %lx size %lx\n", seg,
533 1.48 eeh (long)map->dm_segs[seg].ds_addr, map->dm_segs[seg].ds_len));
534 1.40 eeh map->dm_nsegs = seg+1;
535 1.7 mrg map->dm_mapsize = buflen;
536 1.7 mrg
537 1.7 mrg if (p != NULL)
538 1.7 mrg pmap = p->p_vmspace->vm_map.pmap;
539 1.7 mrg else
540 1.7 mrg pmap = pmap_kernel();
541 1.7 mrg
542 1.7 mrg for (; buflen > 0; ) {
543 1.7 mrg /*
544 1.7 mrg * Get the physical address for this page.
545 1.7 mrg */
546 1.7 mrg if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
547 1.7 mrg bus_dmamap_unload(t, map);
548 1.7 mrg return (-1);
549 1.7 mrg }
550 1.7 mrg
551 1.7 mrg /*
552 1.7 mrg * Compute the segment size, and adjust counts.
553 1.7 mrg */
554 1.7 mrg sgsize = NBPG - ((u_long)vaddr & PGOFSET);
555 1.7 mrg if (buflen < sgsize)
556 1.7 mrg sgsize = buflen;
557 1.7 mrg
558 1.22 mrg DPRINTF(IDB_BUSDMA,
559 1.36 eeh ("iommu_dvmamap_load: map %p loading va %p "
560 1.36 eeh "dva %lx at pa %lx\n",
561 1.36 eeh map, (void *)vaddr, (long)dvmaddr,
562 1.51.4.2 lukem (long)(curaddr & ~(NBPG-1))));
563 1.51.4.3 lukem iommu_enter(sb, trunc_page(dvmaddr), trunc_page(curaddr),
564 1.45 eeh flags|0x4000);
565 1.7 mrg
566 1.7 mrg dvmaddr += PAGE_SIZE;
567 1.7 mrg vaddr += sgsize;
568 1.7 mrg buflen -= sgsize;
569 1.7 mrg }
570 1.45 eeh #ifdef DIAGNOSTIC
571 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
572 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
573 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
574 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
575 1.48 eeh seg, (long)map->dm_segs[seg].ds_addr,
576 1.45 eeh is->is_dvmabase, is->is_dvmaend);
577 1.45 eeh Debugger();
578 1.45 eeh }
579 1.45 eeh }
580 1.45 eeh #endif
581 1.7 mrg return (0);
582 1.7 mrg }
583 1.7 mrg
584 1.7 mrg
585 1.7 mrg void
586 1.51.4.3 lukem iommu_dvmamap_unload(t, sb, map)
587 1.7 mrg bus_dma_tag_t t;
588 1.51.4.3 lukem struct strbuf_ctl *sb;
589 1.7 mrg bus_dmamap_t map;
590 1.7 mrg {
591 1.51.4.3 lukem struct iommu_state *is = sb->sb_is;
592 1.40 eeh int error, s;
593 1.7 mrg bus_size_t sgsize;
594 1.7 mrg
595 1.40 eeh /* Flush the iommu */
596 1.40 eeh #ifdef DEBUG
597 1.40 eeh if (!map->_dm_dvmastart) {
598 1.40 eeh printf("iommu_dvmamap_unload: No dvmastart is zero\n");
599 1.40 eeh #ifdef DDB
600 1.40 eeh Debugger();
601 1.40 eeh #endif
602 1.40 eeh }
603 1.40 eeh #endif
604 1.40 eeh iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
605 1.7 mrg
606 1.23 eeh /* Flush the caches */
607 1.23 eeh bus_dmamap_unload(t->_parent, map);
608 1.23 eeh
609 1.7 mrg /* Mark the mappings as invalid. */
610 1.7 mrg map->dm_mapsize = 0;
611 1.7 mrg map->dm_nsegs = 0;
612 1.7 mrg
613 1.7 mrg s = splhigh();
614 1.40 eeh error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
615 1.40 eeh map->_dm_dvmasize, EX_NOWAIT);
616 1.43 eeh map->_dm_dvmastart = 0;
617 1.43 eeh map->_dm_dvmasize = 0;
618 1.7 mrg splx(s);
619 1.7 mrg if (error != 0)
620 1.7 mrg printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
621 1.40 eeh
622 1.40 eeh /* Clear the map */
623 1.9 eeh }
624 1.9 eeh
625 1.9 eeh
626 1.9 eeh int
627 1.51.4.3 lukem iommu_dvmamap_load_raw(t, sb, map, segs, nsegs, flags, size)
628 1.9 eeh bus_dma_tag_t t;
629 1.51.4.3 lukem struct strbuf_ctl *sb;
630 1.9 eeh bus_dmamap_t map;
631 1.9 eeh bus_dma_segment_t *segs;
632 1.9 eeh int nsegs;
633 1.22 mrg int flags;
634 1.9 eeh bus_size_t size;
635 1.9 eeh {
636 1.51.4.3 lukem struct iommu_state *is = sb->sb_is;
637 1.35 chs struct vm_page *m;
638 1.40 eeh int i, j, s;
639 1.26 martin int left;
640 1.9 eeh int err;
641 1.9 eeh bus_size_t sgsize;
642 1.9 eeh paddr_t pa;
643 1.21 eeh bus_size_t boundary, align;
644 1.40 eeh u_long dvmaddr, sgstart, sgend;
645 1.9 eeh struct pglist *mlist;
646 1.9 eeh int pagesz = PAGE_SIZE;
647 1.45 eeh int npg = 0; /* DEBUG */
648 1.9 eeh
649 1.9 eeh if (map->dm_nsegs) {
650 1.9 eeh /* Already in use?? */
651 1.9 eeh #ifdef DIAGNOSTIC
652 1.9 eeh printf("iommu_dvmamap_load_raw: map still in use\n");
653 1.9 eeh #endif
654 1.9 eeh bus_dmamap_unload(t, map);
655 1.9 eeh }
656 1.40 eeh
657 1.40 eeh /*
658 1.40 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
659 1.40 eeh * over boundary in the map.
660 1.40 eeh */
661 1.40 eeh if ((boundary = segs[0]._ds_boundary) == 0)
662 1.40 eeh boundary = map->_dm_boundary;
663 1.40 eeh
664 1.45 eeh align = max(segs[0]._ds_align, pagesz);
665 1.40 eeh
666 1.9 eeh /*
667 1.9 eeh * Make sure that on error condition we return "no valid mappings".
668 1.9 eeh */
669 1.9 eeh map->dm_nsegs = 0;
670 1.26 martin /* Count up the total number of pages we need */
671 1.26 martin pa = segs[0].ds_addr;
672 1.26 martin sgsize = 0;
673 1.40 eeh left = size;
674 1.40 eeh for (i=0; left && i<nsegs; i++) {
675 1.26 martin if (round_page(pa) != round_page(segs[i].ds_addr))
676 1.26 martin sgsize = round_page(sgsize);
677 1.40 eeh sgsize += min(left, segs[i].ds_len);
678 1.40 eeh left -= segs[i].ds_len;
679 1.26 martin pa = segs[i].ds_addr + segs[i].ds_len;
680 1.26 martin }
681 1.26 martin sgsize = round_page(sgsize);
682 1.9 eeh
683 1.40 eeh s = splhigh();
684 1.40 eeh /*
685 1.40 eeh * If our segment size is larger than the boundary we need to
686 1.45 eeh * split the transfer up into little pieces ourselves.
687 1.9 eeh */
688 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
689 1.40 eeh (sgsize > boundary) ? 0 : boundary,
690 1.40 eeh ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
691 1.51.4.2 lukem EX_BOUNDZERO, &dvmaddr);
692 1.9 eeh splx(s);
693 1.9 eeh
694 1.9 eeh if (err != 0)
695 1.9 eeh return (err);
696 1.9 eeh
697 1.9 eeh #ifdef DEBUG
698 1.9 eeh if (dvmaddr == (bus_addr_t)-1)
699 1.9 eeh {
700 1.9 eeh printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
701 1.25 mrg (int)sgsize, flags);
702 1.9 eeh Debugger();
703 1.9 eeh }
704 1.9 eeh #endif
705 1.9 eeh if (dvmaddr == (bus_addr_t)-1)
706 1.9 eeh return (ENOMEM);
707 1.9 eeh
708 1.40 eeh /* Set the active DVMA map */
709 1.40 eeh map->_dm_dvmastart = dvmaddr;
710 1.40 eeh map->_dm_dvmasize = sgsize;
711 1.40 eeh
712 1.26 martin if ((mlist = segs[0]._ds_mlist) == NULL) {
713 1.26 martin u_long prev_va = NULL;
714 1.45 eeh paddr_t prev_pa = 0;
715 1.45 eeh int end = 0, offset;
716 1.45 eeh
717 1.26 martin /*
718 1.45 eeh * This segs is made up of individual physical
719 1.45 eeh * segments, probably by _bus_dmamap_load_uio() or
720 1.26 martin * _bus_dmamap_load_mbuf(). Ignore the mlist and
721 1.45 eeh * load each one individually.
722 1.26 martin */
723 1.40 eeh map->dm_mapsize = size;
724 1.40 eeh
725 1.45 eeh j = 0;
726 1.45 eeh for (i = 0; i < nsegs ; i++) {
727 1.40 eeh
728 1.45 eeh pa = segs[i].ds_addr;
729 1.45 eeh offset = (pa & PGOFSET);
730 1.45 eeh pa = trunc_page(pa);
731 1.45 eeh dvmaddr = trunc_page(dvmaddr);
732 1.45 eeh left = min(size, segs[i].ds_len);
733 1.45 eeh
734 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
735 1.45 eeh "physseg %d start %lx size %lx\n", i,
736 1.48 eeh (long)segs[i].ds_addr, segs[i].ds_len));
737 1.26 martin
738 1.47 eeh if ((pa == prev_pa) &&
739 1.47 eeh ((offset != 0) || (end != offset))) {
740 1.45 eeh /* We can re-use this mapping */
741 1.45 eeh dvmaddr = prev_va;
742 1.45 eeh }
743 1.29 martin
744 1.45 eeh sgstart = dvmaddr + offset;
745 1.45 eeh sgend = sgstart + left - 1;
746 1.26 martin
747 1.45 eeh /* Are the segments virtually adjacent? */
748 1.48 eeh if ((j > 0) && (end == offset) &&
749 1.45 eeh ((offset == 0) || (pa == prev_pa))) {
750 1.45 eeh /* Just append to the previous segment. */
751 1.45 eeh map->dm_segs[--j].ds_len += left;
752 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
753 1.45 eeh "appending seg %d start %lx size %lx\n", j,
754 1.48 eeh (long)map->dm_segs[j].ds_addr,
755 1.45 eeh map->dm_segs[j].ds_len));
756 1.45 eeh } else {
757 1.51.4.1 lukem if (j >= map->_dm_segcnt) {
758 1.51.4.3 lukem iommu_dvmamap_unload(t, sb, map);
759 1.51.4.1 lukem return (E2BIG);
760 1.51.4.1 lukem }
761 1.45 eeh map->dm_segs[j].ds_addr = sgstart;
762 1.45 eeh map->dm_segs[j].ds_len = left;
763 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
764 1.45 eeh "seg %d start %lx size %lx\n", j,
765 1.48 eeh (long)map->dm_segs[j].ds_addr,
766 1.45 eeh map->dm_segs[j].ds_len));
767 1.40 eeh }
768 1.45 eeh end = (offset + left) & PGOFSET;
769 1.40 eeh
770 1.40 eeh /* Check for boundary issues */
771 1.40 eeh while ((sgstart & ~(boundary - 1)) !=
772 1.40 eeh (sgend & ~(boundary - 1))) {
773 1.40 eeh /* Need a new segment. */
774 1.40 eeh map->dm_segs[j].ds_len =
775 1.51.4.1 lukem boundary - (sgstart & (boundary - 1));
776 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
777 1.40 eeh "seg %d start %lx size %lx\n", j,
778 1.48 eeh (long)map->dm_segs[j].ds_addr,
779 1.40 eeh map->dm_segs[j].ds_len));
780 1.51.4.1 lukem if (++j >= map->_dm_segcnt) {
781 1.51.4.3 lukem iommu_dvmamap_unload(t, sb, map);
782 1.40 eeh return (E2BIG);
783 1.40 eeh }
784 1.40 eeh sgstart = roundup(sgstart, boundary);
785 1.40 eeh map->dm_segs[j].ds_addr = sgstart;
786 1.40 eeh map->dm_segs[j].ds_len = sgend - sgstart + 1;
787 1.40 eeh }
788 1.40 eeh
789 1.26 martin if (sgsize == 0)
790 1.26 martin panic("iommu_dmamap_load_raw: size botch");
791 1.40 eeh
792 1.45 eeh /* Now map a series of pages. */
793 1.51 eeh while (dvmaddr <= sgend) {
794 1.45 eeh DPRINTF(IDB_BUSDMA,
795 1.45 eeh ("iommu_dvmamap_load_raw: map %p "
796 1.45 eeh "loading va %lx at pa %lx\n",
797 1.45 eeh map, (long)dvmaddr,
798 1.45 eeh (long)(pa)));
799 1.45 eeh /* Enter it if we haven't before. */
800 1.46 eeh if (prev_va != dvmaddr)
801 1.51.4.3 lukem iommu_enter(sb, prev_va = dvmaddr,
802 1.45 eeh prev_pa = pa,
803 1.45 eeh flags|(++npg<<12));
804 1.45 eeh dvmaddr += pagesz;
805 1.45 eeh pa += pagesz;
806 1.45 eeh }
807 1.45 eeh
808 1.45 eeh size -= left;
809 1.45 eeh ++j;
810 1.26 martin }
811 1.45 eeh
812 1.45 eeh map->dm_nsegs = j;
813 1.45 eeh #ifdef DIAGNOSTIC
814 1.45 eeh { int seg;
815 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
816 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
817 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
818 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
819 1.48 eeh seg, (long)map->dm_segs[seg].ds_addr,
820 1.45 eeh is->is_dvmabase, is->is_dvmaend);
821 1.45 eeh Debugger();
822 1.45 eeh }
823 1.45 eeh }
824 1.45 eeh }
825 1.45 eeh #endif
826 1.26 martin return (0);
827 1.26 martin }
828 1.9 eeh /*
829 1.40 eeh * This was allocated with bus_dmamem_alloc.
830 1.40 eeh * The pages are on an `mlist'.
831 1.9 eeh */
832 1.9 eeh map->dm_mapsize = size;
833 1.26 martin i = 0;
834 1.40 eeh sgstart = dvmaddr;
835 1.40 eeh sgend = sgstart + size - 1;
836 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
837 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
838 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
839 1.51.4.1 lukem map->dm_segs[i].ds_len = boundary - (sgstart & (boundary - 1));
840 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
841 1.40 eeh "seg %d start %lx size %lx\n", i,
842 1.48 eeh (long)map->dm_segs[i].ds_addr,
843 1.40 eeh map->dm_segs[i].ds_len));
844 1.51.4.1 lukem if (++i >= map->_dm_segcnt) {
845 1.40 eeh /* Too many segments. Fail the operation. */
846 1.40 eeh s = splhigh();
847 1.40 eeh /* How can this fail? And if it does what can we do? */
848 1.40 eeh err = extent_free(is->is_dvmamap,
849 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
850 1.40 eeh map->_dm_dvmastart = 0;
851 1.40 eeh map->_dm_dvmasize = 0;
852 1.43 eeh splx(s);
853 1.40 eeh return (E2BIG);
854 1.40 eeh }
855 1.40 eeh sgstart = roundup(sgstart, boundary);
856 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
857 1.40 eeh }
858 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
859 1.40 eeh "seg %d start %lx size %lx\n", i,
860 1.48 eeh (long)map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len));
861 1.40 eeh map->dm_segs[i].ds_len = sgend - sgstart + 1;
862 1.9 eeh
863 1.9 eeh for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
864 1.9 eeh if (sgsize == 0)
865 1.9 eeh panic("iommu_dmamap_load_raw: size botch");
866 1.9 eeh pa = VM_PAGE_TO_PHYS(m);
867 1.9 eeh
868 1.22 mrg DPRINTF(IDB_BUSDMA,
869 1.9 eeh ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
870 1.9 eeh map, (long)dvmaddr, (long)(pa)));
871 1.51.4.3 lukem iommu_enter(sb, dvmaddr, pa, flags|0x8000);
872 1.9 eeh
873 1.9 eeh dvmaddr += pagesz;
874 1.9 eeh sgsize -= pagesz;
875 1.9 eeh }
876 1.40 eeh map->dm_mapsize = size;
877 1.40 eeh map->dm_nsegs = i+1;
878 1.45 eeh #ifdef DIAGNOSTIC
879 1.45 eeh { int seg;
880 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
881 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
882 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
883 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
884 1.48 eeh seg, (long)map->dm_segs[seg].ds_addr,
885 1.45 eeh is->is_dvmabase, is->is_dvmaend);
886 1.45 eeh Debugger();
887 1.45 eeh }
888 1.45 eeh }
889 1.45 eeh }
890 1.45 eeh #endif
891 1.9 eeh return (0);
892 1.7 mrg }
893 1.7 mrg
894 1.7 mrg void
895 1.51.4.3 lukem iommu_dvmamap_sync(t, sb, map, offset, len, ops)
896 1.7 mrg bus_dma_tag_t t;
897 1.51.4.3 lukem struct strbuf_ctl *sb;
898 1.7 mrg bus_dmamap_t map;
899 1.7 mrg bus_addr_t offset;
900 1.7 mrg bus_size_t len;
901 1.7 mrg int ops;
902 1.7 mrg {
903 1.51.4.3 lukem struct iommu_state *is = sb->sb_is;
904 1.7 mrg vaddr_t va = map->dm_segs[0].ds_addr + offset;
905 1.51.4.3 lukem int64_t tte;
906 1.7 mrg
907 1.7 mrg /*
908 1.7 mrg * We only support one DMA segment; supporting more makes this code
909 1.7 mrg * too unweildy.
910 1.7 mrg */
911 1.7 mrg
912 1.7 mrg if (ops & BUS_DMASYNC_PREREAD) {
913 1.36 eeh DPRINTF(IDB_SYNC,
914 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
915 1.25 mrg "BUS_DMASYNC_PREREAD\n", (void *)(u_long)va, (u_long)len));
916 1.7 mrg
917 1.7 mrg /* Nothing to do */;
918 1.7 mrg }
919 1.7 mrg if (ops & BUS_DMASYNC_POSTREAD) {
920 1.36 eeh DPRINTF(IDB_SYNC,
921 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
922 1.25 mrg "BUS_DMASYNC_POSTREAD\n", (void *)(u_long)va, (u_long)len));
923 1.51.4.3 lukem #ifdef DIAGNOSTIC
924 1.51.4.3 lukem if (va < is->is_dvmabase || va >= is->is_dvmaend)
925 1.51.4.4 lukem panic("iommu_dvmamap_sync: invalid dva %lx", va);
926 1.51.4.3 lukem #endif
927 1.51.4.3 lukem tte = is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)];
928 1.51.4.3 lukem
929 1.51.4.3 lukem DPRINTF(IDB_SYNC,
930 1.51.4.3 lukem ("iommu_dvmamap_sync: syncing va %p len %lu "
931 1.51.4.3 lukem "BUS_DMASYNC_PREWRITE\n", (void *)(u_long)va, (u_long)len));
932 1.51.4.3 lukem
933 1.7 mrg /* if we have a streaming buffer, flush it here first */
934 1.51.4.3 lukem if ((tte & IOTTE_STREAM) && sb->sb_flush)
935 1.7 mrg while (len > 0) {
936 1.22 mrg DPRINTF(IDB_BUSDMA,
937 1.51.4.3 lukem ("iommu_dvmamap_sync: flushing va %p, %lu "
938 1.51.4.3 lukem "bytes left\n", (void *)(u_long)va,
939 1.51.4.3 lukem (u_long)len));
940 1.51.4.3 lukem iommu_strbuf_flush(sb, va);
941 1.7 mrg if (len <= NBPG) {
942 1.51.4.3 lukem iommu_strbuf_flush_done(sb);
943 1.7 mrg len = 0;
944 1.7 mrg } else
945 1.7 mrg len -= NBPG;
946 1.7 mrg va += NBPG;
947 1.7 mrg }
948 1.7 mrg }
949 1.7 mrg if (ops & BUS_DMASYNC_PREWRITE) {
950 1.51.4.3 lukem #ifdef DIAGNOSTIC
951 1.51.4.3 lukem if (va < is->is_dvmabase || va >= is->is_dvmaend)
952 1.51.4.4 lukem panic("iommu_dvmamap_sync: invalid dva %lx", va);
953 1.51.4.3 lukem #endif
954 1.51.4.3 lukem tte = is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)];
955 1.51.4.3 lukem
956 1.36 eeh DPRINTF(IDB_SYNC,
957 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
958 1.25 mrg "BUS_DMASYNC_PREWRITE\n", (void *)(u_long)va, (u_long)len));
959 1.51.4.3 lukem
960 1.31 eeh /* if we have a streaming buffer, flush it here first */
961 1.51.4.3 lukem if ((tte & IOTTE_STREAM) && sb->sb_flush)
962 1.31 eeh while (len > 0) {
963 1.31 eeh DPRINTF(IDB_BUSDMA,
964 1.31 eeh ("iommu_dvmamap_sync: flushing va %p, %lu "
965 1.50 eeh "bytes left\n", (void *)(u_long)va,
966 1.50 eeh (u_long)len));
967 1.51.4.3 lukem iommu_strbuf_flush(sb, va);
968 1.31 eeh if (len <= NBPG) {
969 1.51.4.3 lukem iommu_strbuf_flush_done(sb);
970 1.31 eeh len = 0;
971 1.31 eeh } else
972 1.31 eeh len -= NBPG;
973 1.31 eeh va += NBPG;
974 1.31 eeh }
975 1.7 mrg }
976 1.7 mrg if (ops & BUS_DMASYNC_POSTWRITE) {
977 1.36 eeh DPRINTF(IDB_SYNC,
978 1.7 mrg ("iommu_dvmamap_sync: syncing va %p len %lu "
979 1.25 mrg "BUS_DMASYNC_POSTWRITE\n", (void *)(u_long)va, (u_long)len));
980 1.7 mrg /* Nothing to do */;
981 1.7 mrg }
982 1.7 mrg }
983 1.7 mrg
984 1.7 mrg int
985 1.51.4.3 lukem iommu_dvmamem_alloc(t, sb, size, alignment, boundary, segs, nsegs, rsegs, flags)
986 1.7 mrg bus_dma_tag_t t;
987 1.51.4.3 lukem struct strbuf_ctl *sb;
988 1.7 mrg bus_size_t size, alignment, boundary;
989 1.7 mrg bus_dma_segment_t *segs;
990 1.7 mrg int nsegs;
991 1.7 mrg int *rsegs;
992 1.7 mrg int flags;
993 1.7 mrg {
994 1.7 mrg
995 1.25 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
996 1.25 mrg "segp %p flags %d\n", (unsigned long long)size,
997 1.25 mrg (unsigned long long)alignment, (unsigned long long)boundary,
998 1.25 mrg segs, flags));
999 1.7 mrg return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
1000 1.21 eeh segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
1001 1.7 mrg }
1002 1.7 mrg
1003 1.7 mrg void
1004 1.51.4.3 lukem iommu_dvmamem_free(t, sb, segs, nsegs)
1005 1.7 mrg bus_dma_tag_t t;
1006 1.51.4.3 lukem struct strbuf_ctl *sb;
1007 1.7 mrg bus_dma_segment_t *segs;
1008 1.7 mrg int nsegs;
1009 1.7 mrg {
1010 1.7 mrg
1011 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
1012 1.7 mrg segs, nsegs));
1013 1.7 mrg bus_dmamem_free(t->_parent, segs, nsegs);
1014 1.7 mrg }
1015 1.7 mrg
1016 1.7 mrg /*
1017 1.7 mrg * Map the DVMA mappings into the kernel pmap.
1018 1.7 mrg * Check the flags to see whether we're streaming or coherent.
1019 1.7 mrg */
1020 1.7 mrg int
1021 1.51.4.3 lukem iommu_dvmamem_map(t, sb, segs, nsegs, size, kvap, flags)
1022 1.7 mrg bus_dma_tag_t t;
1023 1.51.4.3 lukem struct strbuf_ctl *sb;
1024 1.7 mrg bus_dma_segment_t *segs;
1025 1.7 mrg int nsegs;
1026 1.7 mrg size_t size;
1027 1.7 mrg caddr_t *kvap;
1028 1.7 mrg int flags;
1029 1.7 mrg {
1030 1.35 chs struct vm_page *m;
1031 1.7 mrg vaddr_t va;
1032 1.7 mrg bus_addr_t addr;
1033 1.7 mrg struct pglist *mlist;
1034 1.8 mrg int cbit;
1035 1.7 mrg
1036 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
1037 1.7 mrg segs, nsegs, size));
1038 1.7 mrg
1039 1.7 mrg /*
1040 1.8 mrg * Allocate some space in the kernel map, and then map these pages
1041 1.8 mrg * into this space.
1042 1.7 mrg */
1043 1.8 mrg size = round_page(size);
1044 1.8 mrg va = uvm_km_valloc(kernel_map, size);
1045 1.8 mrg if (va == 0)
1046 1.8 mrg return (ENOMEM);
1047 1.7 mrg
1048 1.8 mrg *kvap = (caddr_t)va;
1049 1.7 mrg
1050 1.7 mrg /*
1051 1.7 mrg * digest flags:
1052 1.7 mrg */
1053 1.7 mrg cbit = 0;
1054 1.7 mrg if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1055 1.7 mrg cbit |= PMAP_NVC;
1056 1.7 mrg if (flags & BUS_DMA_NOCACHE) /* sideffects */
1057 1.7 mrg cbit |= PMAP_NC;
1058 1.7 mrg
1059 1.7 mrg /*
1060 1.8 mrg * Now take this and map it into the CPU.
1061 1.7 mrg */
1062 1.7 mrg mlist = segs[0]._ds_mlist;
1063 1.7 mrg for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
1064 1.8 mrg #ifdef DIAGNOSTIC
1065 1.7 mrg if (size == 0)
1066 1.7 mrg panic("iommu_dvmamem_map: size botch");
1067 1.8 mrg #endif
1068 1.7 mrg addr = VM_PAGE_TO_PHYS(m);
1069 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
1070 1.25 mrg "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
1071 1.7 mrg pmap_enter(pmap_kernel(), va, addr | cbit,
1072 1.24 eeh VM_PROT_READ | VM_PROT_WRITE,
1073 1.24 eeh VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
1074 1.7 mrg va += PAGE_SIZE;
1075 1.7 mrg size -= PAGE_SIZE;
1076 1.7 mrg }
1077 1.38 chris pmap_update(pmap_kernel());
1078 1.7 mrg
1079 1.7 mrg return (0);
1080 1.7 mrg }
1081 1.7 mrg
1082 1.7 mrg /*
1083 1.7 mrg * Unmap DVMA mappings from kernel
1084 1.7 mrg */
1085 1.7 mrg void
1086 1.51.4.3 lukem iommu_dvmamem_unmap(t, sb, kva, size)
1087 1.7 mrg bus_dma_tag_t t;
1088 1.51.4.3 lukem struct strbuf_ctl *sb;
1089 1.7 mrg caddr_t kva;
1090 1.7 mrg size_t size;
1091 1.7 mrg {
1092 1.7 mrg
1093 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
1094 1.7 mrg kva, size));
1095 1.7 mrg
1096 1.7 mrg #ifdef DIAGNOSTIC
1097 1.7 mrg if ((u_long)kva & PGOFSET)
1098 1.7 mrg panic("iommu_dvmamem_unmap");
1099 1.7 mrg #endif
1100 1.7 mrg
1101 1.7 mrg size = round_page(size);
1102 1.7 mrg pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
1103 1.38 chris pmap_update(pmap_kernel());
1104 1.8 mrg #if 0
1105 1.8 mrg /*
1106 1.8 mrg * XXX ? is this necessary? i think so and i think other
1107 1.8 mrg * implementations are missing it.
1108 1.8 mrg */
1109 1.8 mrg uvm_km_free(kernel_map, (vaddr_t)kva, size);
1110 1.8 mrg #endif
1111 1.1 mrg }
1112