1 /* $NetBSD: ttwoga_dma.c,v 1.3 2001/07/19 18:50:25 thorpej Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
,
60 int ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t
, bus_dmamap_t
,
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) \
91 temp = T2GA((tcp), T2_IOCSR); \
92 T2GA((tcp), T2_IOCSR) = temp | IOCSR_FTLB; \
94 alpha_mb(); /* MAGIC */ \
95 T2GA((tcp), T2_IOCSR) = temp; \
97 alpha_mb(); /* MAGIC */ \
101 ttwoga_dma_init(struct ttwoga_config
*tcp
)
106 * Initialize the DMA tag used for direct-mapped DMA.
108 t
= &tcp
->tc_dmat_direct
;
110 t
->_wbase
= TTWOGA_DIRECT_MAPPED_BASE
;
111 t
->_wsize
= TTWOGA_DIRECT_MAPPED_SIZE
;
112 t
->_next_window
= 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
;
136 t
->_wbase
= TTWOGA_SGMAP_MAPPED_BASE
;
137 t
->_wsize
= TTWOGA_SGMAP_MAPPED_SIZE
;
138 t
->_next_window
= NULL
;
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
165 T2GA(tcp
, T2_IOCSR
) &= ~IOCSR_ETLB
;
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
180 T2GA(tcp
, T2_WBASE1
) = 0;
183 T2GA(tcp
, T2_WMASK1
) = (TTWOGA_DIRECT_MAPPED_SIZE
- 1) & WMASKx_PWM
;
186 T2GA(tcp
, T2_TBASE1
) = 0;
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
;
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
206 if ((TTWOGA_SGMAP_MAPPED_BASE
& WBASEx_PWSA
) !=
207 TTWOGA_SGMAP_MAPPED_BASE
)
208 panic("ttwoga_dma_init: SGMAP base inconsistency");
210 T2GA(tcp
, T2_WBASE2
) = 0;
213 T2GA(tcp
, T2_WMASK2
) = (TTWOGA_SGMAP_MAPPED_SIZE
- 1) & WMASKx_PWM
;
216 T2GA(tcp
, T2_TBASE2
) = tcp
->tc_sgmap
.aps_ptpa
>> 1;
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
;
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
) {
230 T2GA(tcp
, T2_IOCSR
) |= IOCSR_ETLB
;
232 alpha_mb(); /* MAGIC */
236 /* XXX XXX BEGIN XXX XXX */
238 extern paddr_t alpha_XXX_dmamap_or
; /* XXX */
239 alpha_XXX_dmamap_or
= TTWOGA_DIRECT_MAPPED_BASE
;/* XXX */
241 /* XXX XXX END XXX XXX */
245 * Return the bus dma tag to be used for the specified bus type.
249 ttwoga_dma_get_tag(bus_dma_tag_t t
, alpha_bus_t bustype
)
251 struct ttwoga_config
*tcp
= t
->_cookie
;
257 * Systems with a T2 Gate Array can have 2G
258 * of memory, but we only get a direct-mapped
261 * XXX FIX THIS SOMEDAY!
263 return (&tcp
->tc_dmat_direct
);
267 * ISA doesn't have enough address bits to use
268 * the direct-mapped DMA window, so we must use
271 return (&tcp
->tc_dmat_sgmap
);
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
;
288 error
= pci_sgmap_pte64_load(t
, map
, buf
, buflen
, p
, flags
,
290 if (error
== 0 && tcp
->tc_use_tlb
)
291 TTWOGA_TLB_INVALIDATE(tcp
);
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
;
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
);
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
;
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
);
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
;
340 error
= pci_sgmap_pte64_load_raw(t
, map
, segs
, nsegs
, size
, flags
,
342 if (error
== 0 && tcp
->tc_use_tlb
)
343 TTWOGA_TLB_INVALIDATE(tcp
);
349 * Unload an T2 DMA map.
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
360 pci_sgmap_pte64_unload(t
, map
, t
->_sgmap
);
362 TTWOGA_TLB_INVALIDATE(tcp
);
365 * Do the generic bits of the unload.
367 _bus_dmamap_unload(t
, map
);