Home | History | Annotate | Line # | Download | only in pci
tsp_dma.c revision 1.1.6.1
      1 /* $NetBSD: tsp_dma.c,v 1.1.6.1 2000/11/20 19:57:19 bouyer 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_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     96 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     97 
     98 void	tsp_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     99 
    100 int	tsp_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
    101 	    bus_size_t, struct proc *, int));
    102 
    103 int	tsp_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
    104 	    struct mbuf *, int));
    105 
    106 int	tsp_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
    107 	    struct uio *, int));
    108 
    109 int	tsp_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
    110 	    bus_dma_segment_t *, int, bus_size_t, int));
    111 
    112 void	tsp_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
    113 
    114 void	tsp_tlb_invalidate __P((struct tsp_config *));
    115 
    116 void
    117 tsp_dma_init(pcp)
    118 	struct tsp_config *pcp;
    119 {
    120 	int i;
    121 	bus_dma_tag_t t;
    122 	struct ts_pchip *pccsr = pcp->pc_csr;
    123 	bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
    124 	static struct map_expected {
    125 		u_int32_t base, mask, enables;
    126 	} premap[4] = {
    127 		{ 0x800000, 		   0x700000, WSBA_ENA | WSBA_SG },
    128 		{ 0x80000000 | WSBA_ENA, 0x3ff00000, WSBA_ENA           },
    129 		{ 0, 0 },
    130 		{ 0, 0 }
    131 	};
    132 
    133 	alpha_mb();
    134 	for(i = 0; i < 4; ++i) {
    135 		if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
    136 		    EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask))
    137 			printf("tsp%d: window %d: %lx/base %lx/mask %lx"
    138 			    " reinitialized\n",
    139 			    pcp->pc_pslot, i,
    140 			    pccsr->tsp_wsba[i].tsg_r,
    141 			    pccsr->tsp_wsm[i].tsg_r,
    142 			    pccsr->tsp_tba[i].tsg_r);
    143 		pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
    144 		pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
    145 	}
    146 	alpha_mb();
    147 
    148 	/*
    149 	 * Initialize the DMA tag used for direct-mapped DMA.
    150 	 */
    151 	t = &pcp->pc_dmat_direct;
    152 	t->_cookie = pcp;
    153 	t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
    154 	t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
    155 	t->_next_window = &pcp->pc_dmat_sgmap;
    156 	t->_boundary = 0;
    157 	t->_sgmap = NULL;
    158 	t->_get_tag = tsp_dma_get_tag;
    159 	t->_dmamap_create = _bus_dmamap_create;
    160 	t->_dmamap_destroy = _bus_dmamap_destroy;
    161 	t->_dmamap_load = _bus_dmamap_load_direct;
    162 	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
    163 	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
    164 	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
    165 	t->_dmamap_unload = _bus_dmamap_unload;
    166 	t->_dmamap_sync = _bus_dmamap_sync;
    167 
    168 	t->_dmamem_alloc = _bus_dmamem_alloc;
    169 	t->_dmamem_free = _bus_dmamem_free;
    170 	t->_dmamem_map = _bus_dmamem_map;
    171 	t->_dmamem_unmap = _bus_dmamem_unmap;
    172 	t->_dmamem_mmap = _bus_dmamem_mmap;
    173 
    174 	/*
    175 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    176 	 */
    177 	t = &pcp->pc_dmat_sgmap;
    178 	t->_cookie = pcp;
    179 	t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
    180 	t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
    181 	t->_next_window = NULL;
    182 	t->_boundary = 0;
    183 	t->_sgmap = &pcp->pc_sgmap;
    184 	t->_get_tag = tsp_dma_get_tag;
    185 	t->_dmamap_create = tsp_bus_dmamap_create_sgmap;
    186 	t->_dmamap_destroy = tsp_bus_dmamap_destroy_sgmap;
    187 	t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
    188 	t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
    189 	t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
    190 	t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
    191 	t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
    192 	t->_dmamap_sync = _bus_dmamap_sync;
    193 
    194 	t->_dmamem_alloc = _bus_dmamem_alloc;
    195 	t->_dmamem_free = _bus_dmamem_free;
    196 	t->_dmamem_map = _bus_dmamem_map;
    197 	t->_dmamem_unmap = _bus_dmamem_unmap;
    198 	t->_dmamem_mmap = _bus_dmamem_mmap;
    199 
    200 	/*
    201 	 * Initialize the SGMAP.  Align page table to 32k in case
    202 	 * window is somewhat larger than expected.
    203 	 */
    204 	alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
    205 	    sgwbase, 0, sgwlen, sizeof(u_int64_t), NULL, (32*1024));
    206 
    207 	/*
    208 	 * Enable window 0 and enable SG PTE mapping.
    209 	 */
    210 	alpha_mb();
    211 	pccsr->tsp_wsba[0].tsg_r |= WSBA_SG | WSBA_ENA;
    212 	alpha_mb();
    213 
    214 	/*
    215 	 * Check windows for sanity, especially if we later decide to
    216 	 * use the firmware's initialization in some cases.
    217 	 */
    218 	if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
    219 	    (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
    220 		panic("tsp_dma_init: overlap");
    221 
    222 	tbase = pcp->pc_sgmap.aps_ptpa;
    223 	if (tbase & ~0x7fffffc00UL)
    224 		panic("tsp_dma_init: bad page table address");
    225 	alpha_mb();
    226 	pccsr->tsp_tba[0].tsg_r = tbase;
    227 	alpha_mb();
    228 
    229 	tsp_tlb_invalidate(pcp);
    230 	alpha_mb();
    231 
    232 	/* XXX XXX BEGIN XXX XXX */
    233 	{							/* XXX */
    234 		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
    235 		alpha_XXX_dmamap_or = dwbase;			/* XXX */
    236 	}							/* XXX */
    237 	/* XXX XXX END XXX XXX */
    238 }
    239 
    240 /*
    241  * Return the bus dma tag to be used for the specified bus type.
    242  * INTERNAL USE ONLY!
    243  */
    244 bus_dma_tag_t
    245 tsp_dma_get_tag(t, bustype)
    246 	bus_dma_tag_t t;
    247 	alpha_bus_t bustype;
    248 {
    249 	struct tsp_config *pcp = t->_cookie;
    250 
    251 	switch (bustype) {
    252 	case ALPHA_BUS_PCI:
    253 	case ALPHA_BUS_EISA:
    254 		/*
    255 		 * The direct mapped window will work for most systems,
    256 		 * most of the time. When it doesn't, we chain to the sgmap
    257 		 * window automatically.
    258 		 */
    259 		return (&pcp->pc_dmat_direct);
    260 
    261 	case ALPHA_BUS_ISA:
    262 		/*
    263 		 * ISA doesn't have enough address bits to use
    264 		 * the direct-mapped DMA window, so we must use
    265 		 * SGMAPs.
    266 		 */
    267 		return (&pcp->pc_dmat_sgmap);
    268 
    269 	default:
    270 		panic("tsp_dma_get_tag: shouldn't be here, really...");
    271 	}
    272 }
    273 
    274 /*
    275  * Create a TSP SGMAP-mapped DMA map.
    276  */
    277 int
    278 tsp_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    279     flags, dmamp)
    280 	bus_dma_tag_t t;
    281 	bus_size_t size;
    282 	int nsegments;
    283 	bus_size_t maxsegsz;
    284 	bus_size_t boundary;
    285 	int flags;
    286 	bus_dmamap_t *dmamp;
    287 {
    288 	bus_dmamap_t map;
    289 	int error;
    290 
    291 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    292 	    boundary, flags, dmamp);
    293 	if (error)
    294 		return (error);
    295 
    296 	map = *dmamp;
    297 
    298 	if (flags & BUS_DMA_ALLOCNOW) {
    299 		error = alpha_sgmap_alloc(map, round_page(size),
    300 		    t->_sgmap, flags);
    301 		if (error)
    302 			tsp_bus_dmamap_destroy_sgmap(t, map);
    303 	}
    304 
    305 	return (error);
    306 }
    307 
    308 /*
    309  * Destroy a TSP SGMAP-mapped DMA map.
    310  */
    311 void
    312 tsp_bus_dmamap_destroy_sgmap(t, map)
    313 	bus_dma_tag_t t;
    314 	bus_dmamap_t map;
    315 {
    316 
    317 	if (map->_dm_flags & DMAMAP_HAS_SGMAP)
    318 		alpha_sgmap_free(map, t->_sgmap);
    319 
    320 	_bus_dmamap_destroy(t, map);
    321 }
    322 
    323 /*
    324  * Load a TSP SGMAP-mapped DMA map with a linear buffer.
    325  */
    326 int
    327 tsp_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    328 	bus_dma_tag_t t;
    329 	bus_dmamap_t map;
    330 	void *buf;
    331 	bus_size_t buflen;
    332 	struct proc *p;
    333 	int flags;
    334 {
    335 	int error;
    336 
    337 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    338 	    t->_sgmap);
    339 	if (error == 0)
    340 		tsp_tlb_invalidate(t->_cookie);
    341 
    342 	return (error);
    343 }
    344 
    345 /*
    346  * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
    347  */
    348 int
    349 tsp_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    350 	bus_dma_tag_t t;
    351 	bus_dmamap_t map;
    352 	struct mbuf *m;
    353 	int flags;
    354 {
    355 	int error;
    356 
    357 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
    358 	if (error == 0)
    359 		tsp_tlb_invalidate(t->_cookie);
    360 
    361 	return (error);
    362 }
    363 
    364 /*
    365  * Load a TSP SGMAP-mapped DMA map with a uio.
    366  */
    367 int
    368 tsp_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    369 	bus_dma_tag_t t;
    370 	bus_dmamap_t map;
    371 	struct uio *uio;
    372 	int flags;
    373 {
    374 	int error;
    375 
    376 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
    377 	if (error == 0)
    378 		tsp_tlb_invalidate(t->_cookie);
    379 
    380 	return (error);
    381 }
    382 
    383 /*
    384  * Load a TSP SGMAP-mapped DMA map with raw memory.
    385  */
    386 int
    387 tsp_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    388 	bus_dma_tag_t t;
    389 	bus_dmamap_t map;
    390 	bus_dma_segment_t *segs;
    391 	int nsegs;
    392 	bus_size_t size;
    393 	int flags;
    394 {
    395 	int error;
    396 
    397 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    398 	    t->_sgmap);
    399 	if (error == 0)
    400 		tsp_tlb_invalidate(t->_cookie);
    401 
    402 	return (error);
    403 }
    404 
    405 /*
    406  * Unload a TSP DMA map.
    407  */
    408 void
    409 tsp_bus_dmamap_unload_sgmap(t, map)
    410 	bus_dma_tag_t t;
    411 	bus_dmamap_t map;
    412 {
    413 
    414 	/*
    415 	 * Invalidate any SGMAP page table entries used by this
    416 	 * mapping.
    417 	 */
    418 	pci_sgmap_pte64_unload(t, map, t->_sgmap);
    419 	tsp_tlb_invalidate(t->_cookie);
    420 
    421 	/*
    422 	 * Do the generic bits of the unload.
    423 	 */
    424 	_bus_dmamap_unload(t, map);
    425 }
    426 
    427 /*
    428  * Flush the TSP scatter/gather TLB.
    429  */
    430 void
    431 tsp_tlb_invalidate(pcp)
    432 	struct tsp_config *pcp;
    433 {
    434 
    435 	alpha_mb();
    436 	pcp->pc_csr->tsp_tlbia.tsg_r = 0;
    437 	alpha_mb();
    438 }
    439