Home | History | Annotate | Line # | Download | only in pci
ttwoga_dma.c revision 1.2
      1 /* $NetBSD: ttwoga_dma.c,v 1.2 2001/01/03 19:16:00 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     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.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     40 
     41 __KERNEL_RCSID(0, "$NetBSD: ttwoga_dma.c,v 1.2 2001/01/03 19:16:00 thorpej Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 
     48 #include <uvm/uvm_extern.h>
     49 
     50 #define	_ALPHA_BUS_DMA_PRIVATE
     51 #include <machine/bus.h>
     52 
     53 #include <dev/pci/pcireg.h>
     54 #include <dev/pci/pcivar.h>
     55 
     56 #include <alpha/pci/ttwogareg.h>
     57 #include <alpha/pci/ttwogavar.h>
     58 
     59 bus_dma_tag_t ttwoga_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     60 
     61 int	ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     62 	    bus_size_t, struct proc *, int);
     63 
     64 int	ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     65 	    struct mbuf *, int);
     66 
     67 int	ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     68 	    struct uio *, int);
     69 
     70 int	ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
     71 	    bus_dma_segment_t *, int, bus_size_t, int);
     72 
     73 void	ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
     74 
     75 /*
     76  * Direct-mapped window: 1G at 1G
     77  */
     78 #define	TTWOGA_DIRECT_MAPPED_BASE	(1UL*1024UL*1024UL*1024UL)
     79 #define	TTWOGA_DIRECT_MAPPED_SIZE	(1UL*1024UL*1024UL*1024UL)
     80 
     81 /*
     82  * SGMAP window: 8M at 8M
     83  */
     84 #define	TTWOGA_SGMAP_MAPPED_BASE	(8UL*1024UL*1024UL)
     85 #define	TTWOGA_SGMAP_MAPPED_SIZE	(8UL*1024UL*1024UL)
     86 
     87 /*
     88  * Macro to flush the T2 Gate Array scatter/gather TLB.
     89  */
     90 #define	TTWOGA_TLB_INVALIDATE(tcp)					\
     91 do {									\
     92 	u_int64_t temp;							\
     93 									\
     94 	alpha_mb();							\
     95 	temp = T2GA((tcp), T2_IOCSR);					\
     96 	T2GA((tcp), T2_IOCSR) = temp | IOCSR_FTLB;			\
     97 	alpha_mb();							\
     98 	alpha_mb();	/* MAGIC */					\
     99 	T2GA((tcp), T2_IOCSR) = temp;					\
    100 	alpha_mb();							\
    101 	alpha_mb();	/* MAGIC */					\
    102 } while (0)
    103 
    104 void
    105 ttwoga_dma_init(struct ttwoga_config *tcp)
    106 {
    107 	bus_dma_tag_t t;
    108 
    109 	/*
    110 	 * Initialize the DMA tag used for direct-mapped DMA.
    111 	 */
    112 	t = &tcp->tc_dmat_direct;
    113 	t->_cookie = tcp;
    114 	t->_wbase = TTWOGA_DIRECT_MAPPED_BASE;
    115 	t->_wsize = TTWOGA_DIRECT_MAPPED_SIZE;
    116 	t->_next_window = NULL;
    117 	t->_boundary = 0;
    118 	t->_sgmap = NULL;
    119 	t->_get_tag = ttwoga_dma_get_tag;
    120 	t->_dmamap_create = _bus_dmamap_create;
    121 	t->_dmamap_destroy = _bus_dmamap_destroy;
    122 	t->_dmamap_load = _bus_dmamap_load_direct;
    123 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    124 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    125 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    126 	t->_dmamap_unload = _bus_dmamap_unload;
    127 	t->_dmamap_sync = _bus_dmamap_sync;
    128 
    129 	t->_dmamem_alloc = _bus_dmamem_alloc;
    130 	t->_dmamem_free = _bus_dmamem_free;
    131 	t->_dmamem_map = _bus_dmamem_map;
    132 	t->_dmamem_unmap = _bus_dmamem_unmap;
    133 	t->_dmamem_mmap = _bus_dmamem_mmap;
    134 
    135 	/*
    136 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    137 	 */
    138 	t = &tcp->tc_dmat_sgmap;
    139 	t->_cookie = tcp;
    140 	t->_wbase = TTWOGA_SGMAP_MAPPED_BASE;
    141 	t->_wsize = TTWOGA_SGMAP_MAPPED_SIZE;
    142 	t->_next_window = NULL;
    143 	t->_boundary = 0;
    144 	t->_sgmap = &tcp->tc_sgmap;
    145 	t->_get_tag = ttwoga_dma_get_tag;
    146 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    147 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    148 	t->_dmamap_load = ttwoga_bus_dmamap_load_sgmap;
    149 	t->_dmamap_load_mbuf = ttwoga_bus_dmamap_load_mbuf_sgmap;
    150 	t->_dmamap_load_uio = ttwoga_bus_dmamap_load_uio_sgmap;
    151 	t->_dmamap_load_raw = ttwoga_bus_dmamap_load_raw_sgmap;
    152 	t->_dmamap_unload = ttwoga_bus_dmamap_unload_sgmap;
    153 	t->_dmamap_sync = _bus_dmamap_sync;
    154 
    155 	t->_dmamem_alloc = _bus_dmamem_alloc;
    156 	t->_dmamem_free = _bus_dmamem_free;
    157 	t->_dmamem_map = _bus_dmamem_map;
    158 	t->_dmamem_unmap = _bus_dmamem_unmap;
    159 	t->_dmamem_mmap = _bus_dmamem_mmap;
    160 
    161 	/*
    162 	 * Disable the SGMAP TLB, and flush it.  We reenable it if
    163 	 * we have a Sable or a Gamma with T3 or T4; Gamma with T2
    164 	 * has a TLB bug apparently severe enough to require disabling
    165 	 * it.
    166 	 */
    167 	alpha_mb();
    168 	T2GA(tcp, T2_IOCSR) &= ~IOCSR_ETLB;
    169 	alpha_mb();
    170 	alpha_mb();	/* MAGIC */
    171 
    172 	TTWOGA_TLB_INVALIDATE(tcp);
    173 
    174 	/*
    175 	 * XXX We might want to make sure our DMA windows don't
    176 	 * XXX overlap with PCI memory space!
    177 	 */
    178 
    179 	/*
    180 	 * Set up window 1 as a 1G direct-mapped window
    181 	 * starting at 1G.
    182 	 */
    183 	T2GA(tcp, T2_WBASE1) = 0;
    184 	alpha_mb();
    185 
    186 	T2GA(tcp, T2_WMASK1) = (TTWOGA_DIRECT_MAPPED_SIZE - 1) & WMASKx_PWM;
    187 	alpha_mb();
    188 
    189 	T2GA(tcp, T2_TBASE1) = 0;
    190 	alpha_mb();
    191 
    192 	T2GA(tcp, T2_WBASE1) = TTWOGA_DIRECT_MAPPED_BASE |
    193 	    ((TTWOGA_DIRECT_MAPPED_BASE + (TTWOGA_DIRECT_MAPPED_SIZE - 1)) >>
    194 	     WBASEx_PWxA_SHIFT) | WBASEx_PWE;
    195 	alpha_mb();
    196 
    197 	/*
    198 	 * Initialize the SGMAP.
    199 	 */
    200 	alpha_sgmap_init(t, &tcp->tc_sgmap, "ttwoga_sgmap",
    201 	    TTWOGA_SGMAP_MAPPED_BASE, 0, TTWOGA_SGMAP_MAPPED_SIZE,
    202 	    sizeof(u_int64_t), NULL, 0);
    203 
    204 	/*
    205 	 * Set up window 2 as an 8MB SGMAP-mapped window
    206 	 * starting at 8MB.
    207 	 */
    208 #ifdef DIAGNOSTIC
    209 	if ((TTWOGA_SGMAP_MAPPED_BASE & WBASEx_PWSA) !=
    210 	    TTWOGA_SGMAP_MAPPED_BASE)
    211 		panic("ttwoga_dma_init: SGMAP base inconsistency");
    212 #endif
    213 	T2GA(tcp, T2_WBASE2) = 0;
    214 	alpha_mb();
    215 
    216 	T2GA(tcp, T2_WMASK2) = (TTWOGA_SGMAP_MAPPED_SIZE - 1) & WMASKx_PWM;
    217 	alpha_mb();
    218 
    219 	T2GA(tcp, T2_TBASE2) = tcp->tc_sgmap.aps_ptpa >> 1;
    220 	alpha_mb();
    221 
    222 	T2GA(tcp, T2_WBASE2) = TTWOGA_SGMAP_MAPPED_BASE |
    223 	    ((TTWOGA_SGMAP_MAPPED_BASE + (TTWOGA_SGMAP_MAPPED_SIZE - 1)) >>
    224 	     WBASEx_PWxA_SHIFT) | WBASEx_SGE | WBASEx_PWE;
    225 	alpha_mb();
    226 
    227 	/*
    228 	 * Enable SGMAP TLB on Sable or Gamma with T3 or T4; see above.
    229 	 */
    230 	if (alpha_implver() == ALPHA_IMPLVER_EV4 ||
    231 	    tcp->tc_rev >= TRN_T3) {
    232 		alpha_mb();
    233 		T2GA(tcp, T2_IOCSR) |= IOCSR_ETLB;
    234 		alpha_mb();
    235 		alpha_mb();	/* MAGIC */
    236 		tcp->tc_use_tlb = 1;
    237 	}
    238 
    239 	/* XXX XXX BEGIN XXX XXX */
    240 	{							/* XXX */
    241 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    242 		alpha_XXX_dmamap_or = TTWOGA_DIRECT_MAPPED_BASE;/* XXX */
    243 	}							/* XXX */
    244 	/* XXX XXX END XXX XXX */
    245 }
    246 
    247 /*
    248  * Return the bus dma tag to be used for the specified bus type.
    249  * INTERNAL USE ONLY!
    250  */
    251 bus_dma_tag_t
    252 ttwoga_dma_get_tag(bus_dma_tag_t t, alpha_bus_t bustype)
    253 {
    254 	struct ttwoga_config *tcp = t->_cookie;
    255 
    256 	switch (bustype) {
    257 	case ALPHA_BUS_PCI:
    258 	case ALPHA_BUS_EISA:
    259 		/*
    260 		 * Systems with a T2 Gate Array can have 2G
    261 		 * of memory, but we only get a direct-mapped
    262 		 * window of 1G!
    263 		 *
    264 		 * XXX FIX THIS SOMEDAY!
    265 		 */
    266 		return (&tcp->tc_dmat_direct);
    267 
    268 	case ALPHA_BUS_ISA:
    269 		/*
    270 		 * ISA doesn't have enough address bits to use
    271 		 * the direct-mapped DMA window, so we must use
    272 		 * SGMAPs.
    273 		 */
    274 		return (&tcp->tc_dmat_sgmap);
    275 
    276 	default:
    277 		panic("ttwoga_dma_get_tag: shouldn't be here, really...");
    278 	}
    279 }
    280 
    281 /*
    282  * Load a T2 SGMAP-mapped DMA map with a liner buffer.
    283  */
    284 int
    285 ttwoga_bus_dmamap_load_sgmap(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    286     bus_size_t buflen, struct proc *p, int flags)
    287 {
    288 	struct ttwoga_config *tcp = t->_cookie;
    289 	int error;
    290 
    291 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    292 	    t->_sgmap);
    293 	if (error == 0 && tcp->tc_use_tlb)
    294 		TTWOGA_TLB_INVALIDATE(tcp);
    295 
    296 	return (error);
    297 }
    298 
    299 /*
    300  * Load a T2 SGMAP-mapped DMA map with an mbuf chain.
    301  */
    302 int
    303 ttwoga_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    304     struct mbuf *m, int flags)
    305 {
    306 	struct ttwoga_config *tcp = t->_cookie;
    307 	int error;
    308 
    309 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    310 	if (error == 0 && tcp->tc_use_tlb)
    311 		TTWOGA_TLB_INVALIDATE(tcp);
    312 
    313 	return (error);
    314 }
    315 
    316 /*
    317  * Load a T2 SGMAP-mapped DMA map with a uio.
    318  */
    319 int
    320 ttwoga_bus_dmamap_load_uio_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    321     struct uio *uio, int flags)
    322 {
    323 	struct ttwoga_config *tcp = t->_cookie;
    324 	int error;
    325 
    326 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    327 	if (error == 0 && tcp->tc_use_tlb)
    328 		TTWOGA_TLB_INVALIDATE(tcp);
    329 
    330 	return (error);
    331 }
    332 
    333 /*
    334  * Load a T2 SGMAP-mapped DMA map with raw memory.
    335  */
    336 int
    337 ttwoga_bus_dmamap_load_raw_sgmap(bus_dma_tag_t t, bus_dmamap_t map,
    338     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    339 {
    340 	struct ttwoga_config *tcp = t->_cookie;
    341 	int error;
    342 
    343 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    344 	    t->_sgmap);
    345 	if (error == 0 && tcp->tc_use_tlb)
    346 		TTWOGA_TLB_INVALIDATE(tcp);
    347 
    348 	return (error);
    349 }
    350 
    351 /*
    352  * Unload an T2 DMA map.
    353  */
    354 void
    355 ttwoga_bus_dmamap_unload_sgmap(bus_dma_tag_t t, bus_dmamap_t map)
    356 {
    357 	struct ttwoga_config *tcp = t->_cookie;
    358 
    359 	/*
    360 	 * Invalidate any SGMAP page table entries used by this
    361 	 * mapping.
    362 	 */
    363 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    364 	if (tcp->tc_use_tlb)
    365 		TTWOGA_TLB_INVALIDATE(tcp);
    366 
    367 	/*
    368 	 * Do the generic bits of the unload.
    369 	 */
    370 	_bus_dmamap_unload(t, map);
    371 }
    372