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