Home | History | Annotate | Line # | Download | only in pci
tsp_dma.c revision 1.8
      1 /* $NetBSD: tsp_dma.c,v 1.8 2009/03/14 14:45:53 dsl Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 by Ross Harvey.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Ross Harvey.
     17  * 4. The name of Ross Harvey may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
     21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY BE LIABLE FOR ANY
     24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  */
     33 
     34 /*-
     35  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     36  * All rights reserved.
     37  *
     38  * This code is derived from software contributed to The NetBSD Foundation
     39  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     40  * NASA Ames Research Center.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     52  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61  * POSSIBILITY OF SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: tsp_dma.c,v 1.8 2009/03/14 14:45:53 dsl Exp $");
     66 
     67 #include <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/kernel.h>
     70 #include <sys/device.h>
     71 #include <sys/malloc.h>
     72 
     73 #include <uvm/uvm_extern.h>
     74 
     75 #include <machine/autoconf.h>
     76 #define _ALPHA_BUS_DMA_PRIVATE
     77 #include <machine/bus.h>
     78 #include <machine/rpb.h>
     79 
     80 #include <dev/pci/pcireg.h>
     81 #include <dev/pci/pcivar.h>
     82 #include <alpha/pci/tsreg.h>
     83 #include <alpha/pci/tsvar.h>
     84 
     85 #define tsp_dma() { Generate ctags(1) key. }
     86 
     87 #define	EDIFF(a, b) (((a) | WSBA_ENA | WSBA_SG)	!= ((b) | WSBA_ENA | WSBA_SG))
     88 
     89 bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
     90 
     91 int	tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
     92 	    bus_size_t, struct proc *, int);
     93 
     94 int	tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
     95 	    struct mbuf *, int);
     96 
     97 int	tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
     98 	    struct uio *, int);
     99 
    100 int	tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
    101 	    bus_dma_segment_t *, int, bus_size_t, int);
    102 
    103 void	tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
    104 
    105 void	tsp_tlb_invalidate(struct tsp_config *);
    106 
    107 /*
    108  * XXX Need to figure out what this is, if any.  Initialize it to
    109  * XXX something that should be safe.
    110  */
    111 #define	TSP_SGMAP_PFTHRESH	256
    112 
    113 void
    114 tsp_dma_init(pcp)
    115 	struct tsp_config *pcp;
    116 {
    117 	int i;
    118 	bus_dma_tag_t t;
    119 	struct ts_pchip *pccsr = pcp->pc_csr;
    120 	bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
    121 	static struct map_expected {
    122 		u_int32_t base, mask, enables;
    123 	} premap[4] = {
    124 		{ 0x800000, 		   0x700000, WSBA_ENA | WSBA_SG },
    125 		{ 0x80000000 | WSBA_ENA, 0x3ff00000, WSBA_ENA           },
    126 		{ 0, 0 },
    127 		{ 0, 0 }
    128 	};
    129 
    130 	alpha_mb();
    131 	for(i = 0; i < 4; ++i) {
    132 		if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
    133 		    EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask))
    134 			printf("tsp%d: window %d: %lx/base %lx/mask %lx"
    135 			    " reinitialized\n",
    136 			    pcp->pc_pslot, i,
    137 			    pccsr->tsp_wsba[i].tsg_r,
    138 			    pccsr->tsp_wsm[i].tsg_r,
    139 			    pccsr->tsp_tba[i].tsg_r);
    140 		pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
    141 		pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
    142 	}
    143 	alpha_mb();
    144 
    145 	/*
    146 	 * Initialize the DMA tag used for direct-mapped DMA.
    147 	 */
    148 	t = &pcp->pc_dmat_direct;
    149 	t->_cookie = pcp;
    150 	t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
    151 	t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
    152 	t->_next_window = &pcp->pc_dmat_sgmap;
    153 	t->_boundary = 0;
    154 	t->_sgmap = NULL;
    155 	t->_get_tag = tsp_dma_get_tag;
    156 	t->_dmamap_create = _bus_dmamap_create;
    157 	t->_dmamap_destroy = _bus_dmamap_destroy;
    158 	t->_dmamap_load = _bus_dmamap_load_direct;
    159 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    160 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    161 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    162 	t->_dmamap_unload = _bus_dmamap_unload;
    163 	t->_dmamap_sync = _bus_dmamap_sync;
    164 
    165 	t->_dmamem_alloc = _bus_dmamem_alloc;
    166 	t->_dmamem_free = _bus_dmamem_free;
    167 	t->_dmamem_map = _bus_dmamem_map;
    168 	t->_dmamem_unmap = _bus_dmamem_unmap;
    169 	t->_dmamem_mmap = _bus_dmamem_mmap;
    170 
    171 	/*
    172 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    173 	 */
    174 	t = &pcp->pc_dmat_sgmap;
    175 	t->_cookie = pcp;
    176 	t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
    177 	t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
    178 	t->_next_window = NULL;
    179 	t->_boundary = 0;
    180 	t->_sgmap = &pcp->pc_sgmap;
    181 	t->_pfthresh = TSP_SGMAP_PFTHRESH;
    182 	t->_get_tag = tsp_dma_get_tag;
    183 	t->_dmamap_create = alpha_sgmap_dmamap_create;
    184 	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
    185 	t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
    186 	t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
    187 	t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
    188 	t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
    189 	t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
    190 	t->_dmamap_sync = _bus_dmamap_sync;
    191 
    192 	t->_dmamem_alloc = _bus_dmamem_alloc;
    193 	t->_dmamem_free = _bus_dmamem_free;
    194 	t->_dmamem_map = _bus_dmamem_map;
    195 	t->_dmamem_unmap = _bus_dmamem_unmap;
    196 	t->_dmamem_mmap = _bus_dmamem_mmap;
    197 
    198 	/*
    199 	 * Initialize the SGMAP.  Align page table to 32k in case
    200 	 * window is somewhat larger than expected.
    201 	 */
    202 	alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
    203 	    sgwbase, 0, sgwlen, sizeof(u_int64_t), NULL, (32*1024));
    204 
    205 	/*
    206 	 * Enable window 0 and enable SG PTE mapping.
    207 	 */
    208 	alpha_mb();
    209 	pccsr->tsp_wsba[0].tsg_r |= WSBA_SG | WSBA_ENA;
    210 	alpha_mb();
    211 
    212 	/*
    213 	 * Check windows for sanity, especially if we later decide to
    214 	 * use the firmware's initialization in some cases.
    215 	 */
    216 	if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
    217 	    (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
    218 		panic("tsp_dma_init: overlap");
    219 
    220 	tbase = pcp->pc_sgmap.aps_ptpa;
    221 	if (tbase & ~0x7fffffc00UL)
    222 		panic("tsp_dma_init: bad page table address");
    223 	alpha_mb();
    224 	pccsr->tsp_tba[0].tsg_r = tbase;
    225 	alpha_mb();
    226 
    227 	tsp_tlb_invalidate(pcp);
    228 	alpha_mb();
    229 
    230 	/* XXX XXX BEGIN XXX XXX */
    231 	{							/* XXX */
    232 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    233 		alpha_XXX_dmamap_or = dwbase;			/* XXX */
    234 	}							/* XXX */
    235 	/* XXX XXX END XXX XXX */
    236 }
    237 
    238 /*
    239  * Return the bus dma tag to be used for the specified bus type.
    240  * INTERNAL USE ONLY!
    241  */
    242 bus_dma_tag_t
    243 tsp_dma_get_tag(t, bustype)
    244 	bus_dma_tag_t t;
    245 	alpha_bus_t bustype;
    246 {
    247 	struct tsp_config *pcp = t->_cookie;
    248 
    249 	switch (bustype) {
    250 	case ALPHA_BUS_PCI:
    251 	case ALPHA_BUS_EISA:
    252 		/*
    253 		 * The direct mapped window will work for most systems,
    254 		 * most of the time. When it doesn't, we chain to the sgmap
    255 		 * window automatically.
    256 		 */
    257 		return (&pcp->pc_dmat_direct);
    258 
    259 	case ALPHA_BUS_ISA:
    260 		/*
    261 		 * ISA doesn't have enough address bits to use
    262 		 * the direct-mapped DMA window, so we must use
    263 		 * SGMAPs.
    264 		 */
    265 		return (&pcp->pc_dmat_sgmap);
    266 
    267 	default:
    268 		panic("tsp_dma_get_tag: shouldn't be here, really...");
    269 	}
    270 }
    271 
    272 /*
    273  * Load a TSP SGMAP-mapped DMA map with a linear buffer.
    274  */
    275 int
    276 tsp_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    277 	bus_dma_tag_t t;
    278 	bus_dmamap_t map;
    279 	void *buf;
    280 	bus_size_t buflen;
    281 	struct proc *p;
    282 	int flags;
    283 {
    284 	int error;
    285 
    286 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    287 	    t->_sgmap);
    288 	if (error == 0)
    289 		tsp_tlb_invalidate(t->_cookie);
    290 
    291 	return (error);
    292 }
    293 
    294 /*
    295  * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
    296  */
    297 int
    298 tsp_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    299 	bus_dma_tag_t t;
    300 	bus_dmamap_t map;
    301 	struct mbuf *m;
    302 	int flags;
    303 {
    304 	int error;
    305 
    306 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    307 	if (error == 0)
    308 		tsp_tlb_invalidate(t->_cookie);
    309 
    310 	return (error);
    311 }
    312 
    313 /*
    314  * Load a TSP SGMAP-mapped DMA map with a uio.
    315  */
    316 int
    317 tsp_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    318 	bus_dma_tag_t t;
    319 	bus_dmamap_t map;
    320 	struct uio *uio;
    321 	int flags;
    322 {
    323 	int error;
    324 
    325 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    326 	if (error == 0)
    327 		tsp_tlb_invalidate(t->_cookie);
    328 
    329 	return (error);
    330 }
    331 
    332 /*
    333  * Load a TSP SGMAP-mapped DMA map with raw memory.
    334  */
    335 int
    336 tsp_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    337 	bus_dma_tag_t t;
    338 	bus_dmamap_t map;
    339 	bus_dma_segment_t *segs;
    340 	int nsegs;
    341 	bus_size_t size;
    342 	int flags;
    343 {
    344 	int error;
    345 
    346 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    347 	    t->_sgmap);
    348 	if (error == 0)
    349 		tsp_tlb_invalidate(t->_cookie);
    350 
    351 	return (error);
    352 }
    353 
    354 /*
    355  * Unload a TSP DMA map.
    356  */
    357 void
    358 tsp_bus_dmamap_unload_sgmap(t, map)
    359 	bus_dma_tag_t t;
    360 	bus_dmamap_t map;
    361 {
    362 
    363 	/*
    364 	 * Invalidate any SGMAP page table entries used by this
    365 	 * mapping.
    366 	 */
    367 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    368 	tsp_tlb_invalidate(t->_cookie);
    369 
    370 	/*
    371 	 * Do the generic bits of the unload.
    372 	 */
    373 	_bus_dmamap_unload(t, map);
    374 }
    375 
    376 /*
    377  * Flush the TSP scatter/gather TLB.
    378  */
    379 void
    380 tsp_tlb_invalidate(pcp)
    381 	struct tsp_config *pcp;
    382 {
    383 
    384 	alpha_mb();
    385 	pcp->pc_csr->tsp_tlbia.tsg_r = 0;
    386 	alpha_mb();
    387 }
    388