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