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