1 /* $NetBSD: apecs_dma.c,v 1.17 2009/03/14 14:45:53 dsl Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
35 __KERNEL_RCSID(0, "$NetBSD: apecs_dma.c,v 1.17 2009/03/14 14:45:53 dsl Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
43 #include <uvm/uvm_extern.h>
45 #define _ALPHA_BUS_DMA_PRIVATE
46 #include <machine/bus.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <alpha/pci/apecsreg.h>
51 #include <alpha/pci/apecsvar.h>
53 bus_dma_tag_t
apecs_dma_get_tag(bus_dma_tag_t
, alpha_bus_t
);
55 int apecs_bus_dmamap_load_sgmap(bus_dma_tag_t
, bus_dmamap_t
, void *,
56 bus_size_t
, struct proc
*, int);
58 int apecs_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t
, bus_dmamap_t
,
61 int apecs_bus_dmamap_load_uio_sgmap(bus_dma_tag_t
, bus_dmamap_t
,
64 int apecs_bus_dmamap_load_raw_sgmap(bus_dma_tag_t
, bus_dmamap_t
,
65 bus_dma_segment_t
*, int, bus_size_t
, int);
67 void apecs_bus_dmamap_unload_sgmap(bus_dma_tag_t
, bus_dmamap_t
);
70 * Direct-mapped window: 1G at 1G
72 #define APECS_DIRECT_MAPPED_BASE (1*1024*1024*1024)
73 #define APECS_DIRECT_MAPPED_SIZE (1*1024*1024*1024)
76 * SGMAP window: 8M at 8M
78 #define APECS_SGMAP_MAPPED_BASE (8*1024*1024)
79 #define APECS_SGMAP_MAPPED_SIZE (8*1024*1024)
81 /* APECS has a 256-byte out-bound DMA prefetch threshold. */
82 #define APECS_SGMAP_PFTHRESH 256
85 * Macro to flush APECS scatter/gather TLB.
87 #define APECS_TLB_INVALIDATE() \
90 REGVAL(EPIC_TBIA) = 0; \
95 apecs_dma_init(struct apecs_config
*acp
)
101 * Initialize the DMA tag used for direct-mapped DMA.
103 t
= &acp
->ac_dmat_direct
;
105 t
->_wbase
= APECS_DIRECT_MAPPED_BASE
;
106 t
->_wsize
= APECS_DIRECT_MAPPED_SIZE
;
107 t
->_next_window
= NULL
;
110 t
->_get_tag
= apecs_dma_get_tag
;
111 t
->_dmamap_create
= _bus_dmamap_create
;
112 t
->_dmamap_destroy
= _bus_dmamap_destroy
;
113 t
->_dmamap_load
= _bus_dmamap_load_direct
;
114 t
->_dmamap_load_mbuf
= _bus_dmamap_load_mbuf_direct
;
115 t
->_dmamap_load_uio
= _bus_dmamap_load_uio_direct
;
116 t
->_dmamap_load_raw
= _bus_dmamap_load_raw_direct
;
117 t
->_dmamap_unload
= _bus_dmamap_unload
;
118 t
->_dmamap_sync
= _bus_dmamap_sync
;
120 t
->_dmamem_alloc
= _bus_dmamem_alloc
;
121 t
->_dmamem_free
= _bus_dmamem_free
;
122 t
->_dmamem_map
= _bus_dmamem_map
;
123 t
->_dmamem_unmap
= _bus_dmamem_unmap
;
124 t
->_dmamem_mmap
= _bus_dmamem_mmap
;
127 * Initialize the DMA tag used for sgmap-mapped DMA.
129 t
= &acp
->ac_dmat_sgmap
;
131 t
->_wbase
= APECS_SGMAP_MAPPED_BASE
;
132 t
->_wsize
= APECS_SGMAP_MAPPED_SIZE
;
133 t
->_next_window
= NULL
;
135 t
->_sgmap
= &acp
->ac_sgmap
;
136 t
->_pfthresh
= APECS_SGMAP_PFTHRESH
;
137 t
->_get_tag
= apecs_dma_get_tag
;
138 t
->_dmamap_create
= alpha_sgmap_dmamap_create
;
139 t
->_dmamap_destroy
= alpha_sgmap_dmamap_destroy
;
140 t
->_dmamap_load
= apecs_bus_dmamap_load_sgmap
;
141 t
->_dmamap_load_mbuf
= apecs_bus_dmamap_load_mbuf_sgmap
;
142 t
->_dmamap_load_uio
= apecs_bus_dmamap_load_uio_sgmap
;
143 t
->_dmamap_load_raw
= apecs_bus_dmamap_load_raw_sgmap
;
144 t
->_dmamap_unload
= apecs_bus_dmamap_unload_sgmap
;
145 t
->_dmamap_sync
= _bus_dmamap_sync
;
147 t
->_dmamem_alloc
= _bus_dmamem_alloc
;
148 t
->_dmamem_free
= _bus_dmamem_free
;
149 t
->_dmamem_map
= _bus_dmamem_map
;
150 t
->_dmamem_unmap
= _bus_dmamem_unmap
;
151 t
->_dmamem_mmap
= _bus_dmamem_mmap
;
154 * The firmware has set up window 2 as a 1G direct-mapped DMA
155 * window beginning at 1G. We leave it alone. Disable
158 REGVAL(EPIC_PCI_BASE_1
) = 0;
162 * Initialize the SGMAP.
164 alpha_sgmap_init(t
, &acp
->ac_sgmap
, "apecs_sgmap",
165 APECS_SGMAP_MAPPED_BASE
, 0, APECS_SGMAP_MAPPED_SIZE
,
166 sizeof(u_int64_t
), NULL
, 0);
169 * Set up window 1 as an 8MB SGMAP-mapped window
172 REGVAL(EPIC_PCI_BASE_1
) = APECS_SGMAP_MAPPED_BASE
|
173 EPIC_PCI_BASE_SGEN
| EPIC_PCI_BASE_WENB
;
176 REGVAL(EPIC_PCI_MASK_1
) = EPIC_PCI_MASK_8M
;
179 tbase
= acp
->ac_sgmap
.aps_ptpa
>> EPIC_TBASE_SHIFT
;
180 if ((tbase
& EPIC_TBASE_T_BASE
) != tbase
)
181 panic("apecs_dma_init: bad page table address");
182 REGVAL(EPIC_TBASE_1
) = tbase
;
185 APECS_TLB_INVALIDATE();
187 /* XXX XXX BEGIN XXX XXX */
189 extern paddr_t alpha_XXX_dmamap_or
; /* XXX */
190 alpha_XXX_dmamap_or
= APECS_DIRECT_MAPPED_BASE
; /* XXX */
192 /* XXX XXX END XXX XXX */
196 * Return the bus dma tag to be used for the specified bus type.
200 apecs_dma_get_tag(bus_dma_tag_t t
, alpha_bus_t bustype
)
202 struct apecs_config
*acp
= t
->_cookie
;
208 * Systems with an APECS can only support 1G
209 * of memory, so we use the direct-mapped window
210 * on busses that have 32-bit DMA.
212 return (&acp
->ac_dmat_direct
);
216 * ISA doesn't have enough address bits to use
217 * the direct-mapped DMA window, so we must use
220 return (&acp
->ac_dmat_sgmap
);
223 panic("apecs_dma_get_tag: shouldn't be here, really...");
228 * Load an APECS SGMAP-mapped DMA map with a linear buffer.
231 apecs_bus_dmamap_load_sgmap(bus_dma_tag_t t
, bus_dmamap_t map
, void *buf
, bus_size_t buflen
, struct proc
*p
, int flags
)
235 error
= pci_sgmap_pte64_load(t
, map
, buf
, buflen
, p
, flags
,
238 APECS_TLB_INVALIDATE();
244 * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
247 apecs_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t
, bus_dmamap_t map
, struct mbuf
*m
, int flags
)
251 error
= pci_sgmap_pte64_load_mbuf(t
, map
, m
, flags
, t
->_sgmap
);
253 APECS_TLB_INVALIDATE();
259 * Load an APECS SGMAP-mapped DMA map with a uio.
262 apecs_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t
, bus_dmamap_t map
, struct uio
*uio
, int flags
)
266 error
= pci_sgmap_pte64_load_uio(t
, map
, uio
, flags
, t
->_sgmap
);
268 APECS_TLB_INVALIDATE();
274 * Load an APECS SGMAP-mapped DMA map with raw memory.
277 apecs_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t
, bus_dmamap_t map
, bus_dma_segment_t
*segs
, int nsegs
, bus_size_t size
, int flags
)
281 error
= pci_sgmap_pte64_load_raw(t
, map
, segs
, nsegs
, size
, flags
,
284 APECS_TLB_INVALIDATE();
290 * Unload an APECS DMA map.
293 apecs_bus_dmamap_unload_sgmap(bus_dma_tag_t t
, bus_dmamap_t map
)
297 * Invalidate any SGMAP page table entries used by this
300 pci_sgmap_pte64_unload(t
, map
, t
->_sgmap
);
301 APECS_TLB_INVALIDATE();
304 * Do the generic bits of the unload.
306 _bus_dmamap_unload(t
, map
);