iommu.c revision 1.5 1 1.5 mrg /* $NetBSD: iommu.c,v 1.5 2000/04/05 14:26:51 mrg Exp $ */
2 1.1 mrg
3 1.1 mrg /*-
4 1.1 mrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * This code is derived from software contributed to The NetBSD Foundation
8 1.1 mrg * by Paul Kranenburg.
9 1.1 mrg *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg * 3. All advertising materials mentioning features or use of this software
19 1.1 mrg * must display the following acknowledgement:
20 1.1 mrg * This product includes software developed by the NetBSD
21 1.1 mrg * Foundation, Inc. and its contributors.
22 1.1 mrg * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 mrg * contributors may be used to endorse or promote products derived
24 1.1 mrg * from this software without specific prior written permission.
25 1.1 mrg *
26 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
37 1.1 mrg */
38 1.1 mrg
39 1.1 mrg /*
40 1.1 mrg * Copyright (c) 1992, 1993
41 1.1 mrg * The Regents of the University of California. All rights reserved.
42 1.1 mrg *
43 1.1 mrg * This software was developed by the Computer Systems Engineering group
44 1.1 mrg * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 1.1 mrg * contributed to Berkeley.
46 1.1 mrg *
47 1.1 mrg * All advertising materials mentioning features or use of this software
48 1.1 mrg * must display the following acknowledgement:
49 1.1 mrg * This product includes software developed by the University of
50 1.1 mrg * California, Lawrence Berkeley Laboratory.
51 1.1 mrg *
52 1.1 mrg * Redistribution and use in source and binary forms, with or without
53 1.1 mrg * modification, are permitted provided that the following conditions
54 1.1 mrg * are met:
55 1.1 mrg * 1. Redistributions of source code must retain the above copyright
56 1.1 mrg * notice, this list of conditions and the following disclaimer.
57 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
58 1.1 mrg * notice, this list of conditions and the following disclaimer in the
59 1.1 mrg * documentation and/or other materials provided with the distribution.
60 1.1 mrg * 3. All advertising materials mentioning features or use of this software
61 1.1 mrg * must display the following acknowledgement:
62 1.1 mrg * This product includes software developed by the University of
63 1.1 mrg * California, Berkeley and its contributors.
64 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
65 1.1 mrg * may be used to endorse or promote products derived from this software
66 1.1 mrg * without specific prior written permission.
67 1.1 mrg *
68 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 1.1 mrg * SUCH DAMAGE.
79 1.1 mrg *
80 1.1 mrg * from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
81 1.1 mrg * from: @(#)sbus.c 8.1 (Berkeley) 6/11/93
82 1.1 mrg */
83 1.1 mrg
84 1.4 mrg #include "opt_ddb.h"
85 1.4 mrg
86 1.1 mrg #include <sys/param.h>
87 1.1 mrg #include <sys/extent.h>
88 1.1 mrg #include <sys/malloc.h>
89 1.1 mrg #include <sys/systm.h>
90 1.1 mrg #include <sys/device.h>
91 1.1 mrg #include <vm/vm.h>
92 1.1 mrg
93 1.1 mrg #include <machine/bus.h>
94 1.1 mrg #include <sparc64/sparc64/vaddrs.h>
95 1.1 mrg #include <sparc64/dev/iommureg.h>
96 1.1 mrg #include <sparc64/dev/iommuvar.h>
97 1.1 mrg
98 1.1 mrg #include <machine/autoconf.h>
99 1.1 mrg #include <machine/ctlreg.h>
100 1.1 mrg #include <machine/cpu.h>
101 1.1 mrg
102 1.1 mrg #ifdef DEBUG
103 1.1 mrg #define IDB_DVMA 0x1
104 1.1 mrg #define IDB_INTR 0x2
105 1.1 mrg int iommudebug = 0;
106 1.4 mrg #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
107 1.4 mrg #else
108 1.4 mrg #define DPRINTF(l, s)
109 1.1 mrg #endif
110 1.1 mrg
111 1.1 mrg /*
112 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
113 1.1 mrg * - allocate and setup the iotsb.
114 1.1 mrg * - enable the IOMMU
115 1.1 mrg * - initialise the streaming buffers
116 1.1 mrg * - create a private DVMA map.
117 1.1 mrg *
118 1.1 mrg */
119 1.1 mrg void
120 1.1 mrg iommu_init(name, is, tsbsize)
121 1.1 mrg char *name;
122 1.1 mrg struct iommu_state *is;
123 1.1 mrg int tsbsize;
124 1.1 mrg {
125 1.1 mrg
126 1.1 mrg /*
127 1.1 mrg * Setup the iommu.
128 1.1 mrg *
129 1.1 mrg * The sun4u iommu is part of the SBUS controller so we will
130 1.1 mrg * deal with it here. We could try to fake a device node so
131 1.1 mrg * we can eventually share it with the PCI bus run by psycho,
132 1.1 mrg * but I don't want to get into that sort of cruft.
133 1.1 mrg *
134 1.1 mrg * First we need to allocate a IOTSB. Problem is that the IOMMU
135 1.1 mrg * can only access the IOTSB by physical address, so all the
136 1.1 mrg * pages must be contiguous. Luckily, the smallest IOTSB size
137 1.1 mrg * is one 8K page.
138 1.1 mrg */
139 1.1 mrg if (tsbsize != 0)
140 1.1 mrg panic("tsbsize != 0; FIX ME"); /* XXX */
141 1.1 mrg
142 1.1 mrg /* we want 8K pages */
143 1.1 mrg is->is_cr = IOMMUCR_8KPG | IOMMUCR_EN;
144 1.2 eeh /*
145 1.2 eeh *
146 1.2 eeh * The IOMMU address space always ends at 0xffffe000, but the starting
147 1.2 eeh * address depends on the size of the map. The map size is 1024 * 2 ^
148 1.2 eeh * is->is_tsbsize entries, where each entry is 8 bytes. The start of
149 1.2 eeh * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
150 1.2 eeh *
151 1.2 eeh * Note: the stupid IOMMU ignores the high bits of an address, so a
152 1.2 eeh * NULL DMA pointer will be translated by the first page of the IOTSB.
153 1.2 eeh * To trap bugs we'll skip the first entry in the IOTSB.
154 1.2 eeh */
155 1.2 eeh is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
156 1.1 mrg is->is_tsbsize = tsbsize;
157 1.1 mrg is->is_tsb = malloc(NBPG, M_DMAMAP, M_WAITOK); /* XXX */
158 1.3 thorpej (void) pmap_extract(pmap_kernel(), (vaddr_t)is->is_tsb,
159 1.3 thorpej (paddr_t *)&is->is_ptsb);
160 1.1 mrg
161 1.1 mrg #ifdef DEBUG
162 1.1 mrg if (iommudebug & IDB_DVMA)
163 1.1 mrg {
164 1.1 mrg /* Probe the iommu */
165 1.1 mrg struct iommureg *regs = is->is_iommu;
166 1.1 mrg int64_t cr, tsb;
167 1.1 mrg
168 1.1 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", ®s->iommu_cr,
169 1.1 mrg ®s->iommu_tsb, ®s->iommu_flush);
170 1.1 mrg cr = regs->iommu_cr;
171 1.1 mrg tsb = regs->iommu_tsb;
172 1.1 mrg printf("iommu cr=%lx tsb=%lx\n", (long)cr, (long)tsb);
173 1.1 mrg printf("TSB base %p phys %p\n", (long)is->is_tsb, (long)is->is_ptsb);
174 1.1 mrg delay(1000000); /* 1 s */
175 1.1 mrg }
176 1.1 mrg #endif
177 1.1 mrg
178 1.1 mrg /*
179 1.1 mrg * Initialize streaming buffer.
180 1.1 mrg */
181 1.3 thorpej (void) pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
182 1.3 thorpej (paddr_t *)&is->is_flushpa);
183 1.1 mrg
184 1.1 mrg /*
185 1.1 mrg * now actually start up the IOMMU
186 1.1 mrg */
187 1.1 mrg iommu_reset(is);
188 1.1 mrg
189 1.1 mrg /*
190 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
191 1.1 mrg */
192 1.1 mrg is->is_dvmamap = extent_create(name,
193 1.2 eeh is->is_dvmabase, IOTSB_VEND,
194 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
195 1.1 mrg }
196 1.1 mrg
197 1.1 mrg void
198 1.1 mrg iommu_reset(is)
199 1.1 mrg struct iommu_state *is;
200 1.1 mrg {
201 1.1 mrg
202 1.1 mrg /* Need to do 64-bit stores */
203 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_cr, 0, is->is_cr);
204 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_tsb, 0, is->is_ptsb);
205 1.5 mrg
206 1.1 mrg /* Enable diagnostics mode? */
207 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0, STRBUF_EN);
208 1.5 mrg
209 1.5 mrg /* No streaming buffers? Disable them */
210 1.5 mrg if (bus_space_read_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0) == 0)
211 1.5 mrg is->is_sb = 0;
212 1.2 eeh }
213 1.2 eeh
214 1.2 eeh /*
215 1.2 eeh * Here are the iommu control routines.
216 1.2 eeh */
217 1.2 eeh void
218 1.2 eeh iommu_enter(is, va, pa, flags)
219 1.2 eeh struct iommu_state *is;
220 1.2 eeh vaddr_t va;
221 1.2 eeh int64_t pa;
222 1.2 eeh int flags;
223 1.2 eeh {
224 1.2 eeh int64_t tte;
225 1.2 eeh
226 1.2 eeh #ifdef DIAGNOSTIC
227 1.2 eeh if (va < is->is_dvmabase)
228 1.4 mrg panic("iommu_enter: va 0x%lx not in DVMA space",va);
229 1.2 eeh #endif
230 1.2 eeh
231 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
232 1.2 eeh !(flags&BUS_DMA_COHERENT));
233 1.2 eeh
234 1.2 eeh /* Is the streamcache flush really needed? */
235 1.5 mrg if (is->is_sb) {
236 1.5 mrg bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush, 0,
237 1.5 mrg va);
238 1.5 mrg iommu_flush(is);
239 1.5 mrg }
240 1.4 mrg DPRINTF(IDB_DVMA, ("Clearing TSB slot %d for va %p\n",
241 1.4 mrg (int)IOTSBSLOT(va,is->is_tsbsize), va));
242 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
243 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush,
244 1.2 eeh 0, va);
245 1.4 mrg DPRINTF(IDB_DVMA, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
246 1.2 eeh va, (long)pa, IOTSBSLOT(va,is->is_tsbsize),
247 1.2 eeh &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
248 1.4 mrg (long)tte));
249 1.2 eeh }
250 1.2 eeh
251 1.2 eeh /*
252 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
253 1.2 eeh *
254 1.2 eeh * Only demap from IOMMU if flag is set.
255 1.2 eeh */
256 1.2 eeh void
257 1.2 eeh iommu_remove(is, va, len)
258 1.2 eeh struct iommu_state *is;
259 1.2 eeh vaddr_t va;
260 1.2 eeh size_t len;
261 1.2 eeh {
262 1.2 eeh
263 1.2 eeh #ifdef DIAGNOSTIC
264 1.2 eeh if (va < is->is_dvmabase)
265 1.4 mrg panic("iommu_remove: va 0x%lx not in DVMA space", (long)va);
266 1.2 eeh if ((long)(va + len) < (long)va)
267 1.4 mrg panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
268 1.2 eeh (long) va, (long) len);
269 1.2 eeh if (len & ~0xfffffff)
270 1.4 mrg panic("iommu_remove: rediculous len 0x%lx", (long)len);
271 1.2 eeh #endif
272 1.2 eeh
273 1.2 eeh va = trunc_page(va);
274 1.2 eeh while (len > 0) {
275 1.2 eeh
276 1.2 eeh /*
277 1.2 eeh * Streaming buffer flushes:
278 1.2 eeh *
279 1.2 eeh * 1 Tell strbuf to flush by storing va to strbuf_pgflush
280 1.2 eeh * If we're not on a cache line boundary (64-bits):
281 1.2 eeh * 2 Store 0 in flag
282 1.2 eeh * 3 Store pointer to flag in flushsync
283 1.2 eeh * 4 wait till flushsync becomes 0x1
284 1.2 eeh *
285 1.2 eeh * If it takes more than .5 sec, something went wrong.
286 1.2 eeh */
287 1.5 mrg if (is->is_sb) {
288 1.5 mrg DPRINTF(IDB_DVMA, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
289 1.2 eeh (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
290 1.2 eeh (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
291 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
292 1.4 mrg (u_long)len));
293 1.5 mrg bus_space_write_8(is->is_bustag,
294 1.5 mrg &is->is_sb->strbuf_pgflush, 0, va);
295 1.5 mrg if (len <= NBPG) {
296 1.5 mrg iommu_flush(is);
297 1.5 mrg len = 0;
298 1.5 mrg } else len -= NBPG;
299 1.5 mrg DPRINTF(IDB_DVMA, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
300 1.2 eeh (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
301 1.2 eeh (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
302 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
303 1.4 mrg (u_long)len));
304 1.5 mrg }
305 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
306 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush, 0, va);
307 1.2 eeh va += NBPG;
308 1.2 eeh }
309 1.2 eeh }
310 1.2 eeh
311 1.2 eeh int
312 1.2 eeh iommu_flush(is)
313 1.2 eeh struct iommu_state *is;
314 1.2 eeh {
315 1.2 eeh struct timeval cur, flushtimeout;
316 1.2 eeh
317 1.2 eeh #define BUMPTIME(t, usec) { \
318 1.2 eeh register volatile struct timeval *tp = (t); \
319 1.2 eeh register long us; \
320 1.2 eeh \
321 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
322 1.2 eeh if (us >= 1000000) { \
323 1.2 eeh tp->tv_usec = us - 1000000; \
324 1.2 eeh tp->tv_sec++; \
325 1.2 eeh } \
326 1.2 eeh }
327 1.5 mrg
328 1.5 mrg if (!is->is_sb)
329 1.5 mrg return (0);
330 1.2 eeh
331 1.2 eeh is->is_flush = 0;
332 1.2 eeh membar_sync();
333 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
334 1.2 eeh membar_sync();
335 1.2 eeh
336 1.2 eeh microtime(&flushtimeout);
337 1.2 eeh cur = flushtimeout;
338 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
339 1.2 eeh
340 1.4 mrg DPRINTF(IDB_DVMA, ("iommu_flush: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
341 1.2 eeh (long)is->is_flush, (long)&is->is_flush,
342 1.2 eeh (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
343 1.4 mrg flushtimeout.tv_sec, flushtimeout.tv_usec));
344 1.2 eeh /* Bypass non-coherent D$ */
345 1.2 eeh while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
346 1.2 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
347 1.2 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
348 1.2 eeh microtime(&cur);
349 1.2 eeh
350 1.2 eeh #ifdef DIAGNOSTIC
351 1.2 eeh if (!is->is_flush) {
352 1.4 mrg printf("iommu_flush: flush timeout %p at %p\n", (long)is->is_flush,
353 1.2 eeh (long)is->is_flushpa); /* panic? */
354 1.2 eeh #ifdef DDB
355 1.2 eeh Debugger();
356 1.2 eeh #endif
357 1.2 eeh }
358 1.2 eeh #endif
359 1.4 mrg DPRINTF(IDB_DVMA, ("iommu_flush: flushed\n"));
360 1.2 eeh return (is->is_flush);
361 1.1 mrg }
362