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