Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / alpha / pci / ttwoga_dma.c
blob7f1ba0e32e4d99902c64952186ccdb3d7ac232b3
1 /* $NetBSD: ttwoga_dma.c,v 1.3 2001/07/19 18:50:25 thorpej Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 __KERNEL_RCSID(0, "$NetBSD: ttwoga_dma.c,v 1.3 2001/07/19 18:50:25 thorpej Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
41 #include <uvm/uvm_extern.h>
43 #define _ALPHA_BUS_DMA_PRIVATE
44 #include <machine/bus.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
49 #include <alpha/pci/ttwogareg.h>
50 #include <alpha/pci/ttwogavar.h>
52 bus_dma_tag_t ttwoga_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
54 int ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
55 bus_size_t, struct proc *, int);
57 int ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
58 struct mbuf *, int);
60 int ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
61 struct uio *, int);
63 int ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
64 bus_dma_segment_t *, int, bus_size_t, int);
66 void ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
69 * Direct-mapped window: 1G at 1G
71 #define TTWOGA_DIRECT_MAPPED_BASE (1UL*1024UL*1024UL*1024UL)
72 #define TTWOGA_DIRECT_MAPPED_SIZE (1UL*1024UL*1024UL*1024UL)
75 * SGMAP window: 8M at 8M
77 #define TTWOGA_SGMAP_MAPPED_BASE (8UL*1024UL*1024UL)
78 #define TTWOGA_SGMAP_MAPPED_SIZE (8UL*1024UL*1024UL)
80 /* T2 has a 256-byte out-bound DMA prefetch threshold. */
81 #define TTWOGA_SGMAP_PFTHRESH 256
84 * Macro to flush the T2 Gate Array scatter/gather TLB.
86 #define TTWOGA_TLB_INVALIDATE(tcp) \
87 do { \
88 u_int64_t temp; \
90 alpha_mb(); \
91 temp = T2GA((tcp), T2_IOCSR); \
92 T2GA((tcp), T2_IOCSR) = temp | IOCSR_FTLB; \
93 alpha_mb(); \
94 alpha_mb(); /* MAGIC */ \
95 T2GA((tcp), T2_IOCSR) = temp; \
96 alpha_mb(); \
97 alpha_mb(); /* MAGIC */ \
98 } while (0)
100 void
101 ttwoga_dma_init(struct ttwoga_config *tcp)
103 bus_dma_tag_t t;
106 * Initialize the DMA tag used for direct-mapped DMA.
108 t = &tcp->tc_dmat_direct;
109 t->_cookie = tcp;
110 t->_wbase = TTWOGA_DIRECT_MAPPED_BASE;
111 t->_wsize = TTWOGA_DIRECT_MAPPED_SIZE;
112 t->_next_window = NULL;
113 t->_boundary = 0;
114 t->_sgmap = NULL;
115 t->_get_tag = ttwoga_dma_get_tag;
116 t->_dmamap_create = _bus_dmamap_create;
117 t->_dmamap_destroy = _bus_dmamap_destroy;
118 t->_dmamap_load = _bus_dmamap_load_direct;
119 t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
120 t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
121 t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
122 t->_dmamap_unload = _bus_dmamap_unload;
123 t->_dmamap_sync = _bus_dmamap_sync;
125 t->_dmamem_alloc = _bus_dmamem_alloc;
126 t->_dmamem_free = _bus_dmamem_free;
127 t->_dmamem_map = _bus_dmamem_map;
128 t->_dmamem_unmap = _bus_dmamem_unmap;
129 t->_dmamem_mmap = _bus_dmamem_mmap;
132 * Initialize the DMA tag used for sgmap-mapped DMA.
134 t = &tcp->tc_dmat_sgmap;
135 t->_cookie = tcp;
136 t->_wbase = TTWOGA_SGMAP_MAPPED_BASE;
137 t->_wsize = TTWOGA_SGMAP_MAPPED_SIZE;
138 t->_next_window = NULL;
139 t->_boundary = 0;
140 t->_sgmap = &tcp->tc_sgmap;
141 t->_pfthresh = TTWOGA_SGMAP_PFTHRESH;
142 t->_get_tag = ttwoga_dma_get_tag;
143 t->_dmamap_create = alpha_sgmap_dmamap_create;
144 t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
145 t->_dmamap_load = ttwoga_bus_dmamap_load_sgmap;
146 t->_dmamap_load_mbuf = ttwoga_bus_dmamap_load_mbuf_sgmap;
147 t->_dmamap_load_uio = ttwoga_bus_dmamap_load_uio_sgmap;
148 t->_dmamap_load_raw = ttwoga_bus_dmamap_load_raw_sgmap;
149 t->_dmamap_unload = ttwoga_bus_dmamap_unload_sgmap;
150 t->_dmamap_sync = _bus_dmamap_sync;
152 t->_dmamem_alloc = _bus_dmamem_alloc;
153 t->_dmamem_free = _bus_dmamem_free;
154 t->_dmamem_map = _bus_dmamem_map;
155 t->_dmamem_unmap = _bus_dmamem_unmap;
156 t->_dmamem_mmap = _bus_dmamem_mmap;
159 * Disable the SGMAP TLB, and flush it. We reenable it if
160 * we have a Sable or a Gamma with T3 or T4; Gamma with T2
161 * has a TLB bug apparently severe enough to require disabling
162 * it.
164 alpha_mb();
165 T2GA(tcp, T2_IOCSR) &= ~IOCSR_ETLB;
166 alpha_mb();
167 alpha_mb(); /* MAGIC */
169 TTWOGA_TLB_INVALIDATE(tcp);
172 * XXX We might want to make sure our DMA windows don't
173 * XXX overlap with PCI memory space!
177 * Set up window 1 as a 1G direct-mapped window
178 * starting at 1G.
180 T2GA(tcp, T2_WBASE1) = 0;
181 alpha_mb();
183 T2GA(tcp, T2_WMASK1) = (TTWOGA_DIRECT_MAPPED_SIZE - 1) & WMASKx_PWM;
184 alpha_mb();
186 T2GA(tcp, T2_TBASE1) = 0;
187 alpha_mb();
189 T2GA(tcp, T2_WBASE1) = TTWOGA_DIRECT_MAPPED_BASE |
190 ((TTWOGA_DIRECT_MAPPED_BASE + (TTWOGA_DIRECT_MAPPED_SIZE - 1)) >>
191 WBASEx_PWxA_SHIFT) | WBASEx_PWE;
192 alpha_mb();
195 * Initialize the SGMAP.
197 alpha_sgmap_init(t, &tcp->tc_sgmap, "ttwoga_sgmap",
198 TTWOGA_SGMAP_MAPPED_BASE, 0, TTWOGA_SGMAP_MAPPED_SIZE,
199 sizeof(u_int64_t), NULL, 0);
202 * Set up window 2 as an 8MB SGMAP-mapped window
203 * starting at 8MB.
205 #ifdef DIAGNOSTIC
206 if ((TTWOGA_SGMAP_MAPPED_BASE & WBASEx_PWSA) !=
207 TTWOGA_SGMAP_MAPPED_BASE)
208 panic("ttwoga_dma_init: SGMAP base inconsistency");
209 #endif
210 T2GA(tcp, T2_WBASE2) = 0;
211 alpha_mb();
213 T2GA(tcp, T2_WMASK2) = (TTWOGA_SGMAP_MAPPED_SIZE - 1) & WMASKx_PWM;
214 alpha_mb();
216 T2GA(tcp, T2_TBASE2) = tcp->tc_sgmap.aps_ptpa >> 1;
217 alpha_mb();
219 T2GA(tcp, T2_WBASE2) = TTWOGA_SGMAP_MAPPED_BASE |
220 ((TTWOGA_SGMAP_MAPPED_BASE + (TTWOGA_SGMAP_MAPPED_SIZE - 1)) >>
221 WBASEx_PWxA_SHIFT) | WBASEx_SGE | WBASEx_PWE;
222 alpha_mb();
225 * Enable SGMAP TLB on Sable or Gamma with T3 or T4; see above.
227 if (alpha_implver() == ALPHA_IMPLVER_EV4 ||
228 tcp->tc_rev >= TRN_T3) {
229 alpha_mb();
230 T2GA(tcp, T2_IOCSR) |= IOCSR_ETLB;
231 alpha_mb();
232 alpha_mb(); /* MAGIC */
233 tcp->tc_use_tlb = 1;
236 /* XXX XXX BEGIN XXX XXX */
237 { /* XXX */
238 extern paddr_t alpha_XXX_dmamap_or; /* XXX */
239 alpha_XXX_dmamap_or = TTWOGA_DIRECT_MAPPED_BASE;/* XXX */
240 } /* XXX */
241 /* XXX XXX END XXX XXX */
245 * Return the bus dma tag to be used for the specified bus type.
246 * INTERNAL USE ONLY!
248 bus_dma_tag_t
249 ttwoga_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
251 struct ttwoga_config *tcp = t->_cookie;
253 switch (bustype) {
254 case ALPHA_BUS_PCI:
255 case ALPHA_BUS_EISA:
257 * Systems with a T2 Gate Array can have 2G
258 * of memory, but we only get a direct-mapped
259 * window of 1G!
261 * XXX FIX THIS SOMEDAY!
263 return (&tcp->tc_dmat_direct);
265 case ALPHA_BUS_ISA:
267 * ISA doesn't have enough address bits to use
268 * the direct-mapped DMA window, so we must use
269 * SGMAPs.
271 return (&tcp->tc_dmat_sgmap);
273 default:
274 panic("ttwoga_dma_get_tag: shouldn't be here, really...");
279 * Load a T2 SGMAP-mapped DMA map with a liner buffer.
282 ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
283 bus_size_t buflen, struct proc *p, int flags)
285 struct ttwoga_config *tcp = t->_cookie;
286 int error;
288 error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
289 t->_sgmap);
290 if (error == 0 && tcp->tc_use_tlb)
291 TTWOGA_TLB_INVALIDATE(tcp);
293 return (error);
297 * Load a T2 SGMAP-mapped DMA map with an mbuf chain.
300 ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
301 struct mbuf *m, int flags)
303 struct ttwoga_config *tcp = t->_cookie;
304 int error;
306 error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
307 if (error == 0 && tcp->tc_use_tlb)
308 TTWOGA_TLB_INVALIDATE(tcp);
310 return (error);
314 * Load a T2 SGMAP-mapped DMA map with a uio.
317 ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
318 struct uio *uio, int flags)
320 struct ttwoga_config *tcp = t->_cookie;
321 int error;
323 error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
324 if (error == 0 && tcp->tc_use_tlb)
325 TTWOGA_TLB_INVALIDATE(tcp);
327 return (error);
331 * Load a T2 SGMAP-mapped DMA map with raw memory.
334 ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
335 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
337 struct ttwoga_config *tcp = t->_cookie;
338 int error;
340 error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
341 t->_sgmap);
342 if (error == 0 && tcp->tc_use_tlb)
343 TTWOGA_TLB_INVALIDATE(tcp);
345 return (error);
349 * Unload an T2 DMA map.
351 void
352 ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
354 struct ttwoga_config *tcp = t->_cookie;
357 * Invalidate any SGMAP page table entries used by this
358 * mapping.
360 pci_sgmap_pte64_unload(t, map, t->_sgmap);
361 if (tcp->tc_use_tlb)
362 TTWOGA_TLB_INVALIDATE(tcp);
365 * Do the generic bits of the unload.
367 _bus_dmamap_unload(t, map);