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