Home | History | Annotate | Line # | Download | only in pci
apecs_dma.c revision 1.4
      1 /* $NetBSD: apecs_dma.c,v 1.4 1998/01/17 03:40:32 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997 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 of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     41 
     42 __KERNEL_RCSID(0, "$NetBSD: apecs_dma.c,v 1.4 1998/01/17 03:40:32 thorpej Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/malloc.h>
     49 #include <vm/vm.h>
     50 
     51 #define _ALPHA_BUS_DMA_PRIVATE
     52 #include <machine/bus.h>
     53 
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcivar.h>
     56 #include <alpha/pci/apecsreg.h>
     57 #include <alpha/pci/apecsvar.h>
     58 
     59 bus_dma_tag_t apecs_dma_get_tag __P((bus_dma_tag_t, alpha_bus_t));
     60 
     61 int	apecs_bus_dmamap_create_sgmap __P((bus_dma_tag_t, bus_size_t, int,
     62 	    bus_size_t, bus_size_t, int, bus_dmamap_t *));
     63 
     64 void	apecs_bus_dmamap_destroy_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     65 
     66 int	apecs_bus_dmamap_load_direct __P((bus_dma_tag_t, bus_dmamap_t, void *,
     67 	    bus_size_t, struct proc *, int));
     68 int	apecs_bus_dmamap_load_sgmap __P((bus_dma_tag_t, bus_dmamap_t, void *,
     69 	    bus_size_t, struct proc *, int));
     70 
     71 int	apecs_bus_dmamap_load_mbuf_direct __P((bus_dma_tag_t, bus_dmamap_t,
     72 	    struct mbuf *, int));
     73 int	apecs_bus_dmamap_load_mbuf_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     74 	    struct mbuf *, int));
     75 
     76 int	apecs_bus_dmamap_load_uio_direct __P((bus_dma_tag_t, bus_dmamap_t,
     77 	    struct uio *, int));
     78 int	apecs_bus_dmamap_load_uio_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     79 	    struct uio *, int));
     80 
     81 int	apecs_bus_dmamap_load_raw_direct __P((bus_dma_tag_t, bus_dmamap_t,
     82 	    bus_dma_segment_t *, int, bus_size_t, int));
     83 int	apecs_bus_dmamap_load_raw_sgmap __P((bus_dma_tag_t, bus_dmamap_t,
     84 	    bus_dma_segment_t *, int, bus_size_t, int));
     85 
     86 void	apecs_bus_dmamap_unload_sgmap __P((bus_dma_tag_t, bus_dmamap_t));
     87 
     88 /*
     89  * The 1G direct-mapped DMA window begins at this PCI address.
     90  */
     91 #define	APECS_DIRECT_MAPPED_BASE 0x40000000
     92 
     93 /*
     94  * The 8M SGMAP-mapped DMA window begins at this PCI address.
     95  */
     96 #define	APECS_SGMAP_MAPPED_BASE	(8*1024*1024)
     97 
     98 /*
     99  * Macro to flush APECS scatter/gather TLB.
    100  */
    101 #define	APECS_TLB_INVALIDATE() \
    102 do { \
    103 	alpha_mb(); \
    104 	REGVAL(EPIC_TBIA) = 0; \
    105 	alpha_mb(); \
    106 } while (0)
    107 
    108 void
    109 apecs_dma_init(acp)
    110 	struct apecs_config *acp;
    111 {
    112 	bus_addr_t tbase;
    113 	bus_dma_tag_t t;
    114 
    115 	/*
    116 	 * Initialize the DMA tag used for direct-mapped DMA.
    117 	 */
    118 	t = &acp->ac_dmat_direct;
    119 	t->_cookie = acp;
    120 	t->_get_tag = apecs_dma_get_tag;
    121 	t->_dmamap_create = _bus_dmamap_create;
    122 	t->_dmamap_destroy = _bus_dmamap_destroy;
    123 	t->_dmamap_load = apecs_bus_dmamap_load_direct;
    124 	t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_direct;
    125 	t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_direct;
    126 	t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_direct;
    127 	t->_dmamap_unload = _bus_dmamap_unload;
    128 	t->_dmamap_sync = NULL;		/* Nothing to do. */
    129 
    130 	t->_dmamem_alloc = _bus_dmamem_alloc;
    131 	t->_dmamem_free = _bus_dmamem_free;
    132 	t->_dmamem_map = _bus_dmamem_map;
    133 	t->_dmamem_unmap = _bus_dmamem_unmap;
    134 	t->_dmamem_mmap = _bus_dmamem_mmap;
    135 
    136 	/*
    137 	 * Initialize the DMA tag used for sgmap-mapped DMA.
    138 	 */
    139 	t = &acp->ac_dmat_sgmap;
    140 	t->_cookie = acp;
    141 	t->_get_tag = apecs_dma_get_tag;
    142 	t->_dmamap_create = apecs_bus_dmamap_create_sgmap;
    143 	t->_dmamap_destroy = apecs_bus_dmamap_destroy_sgmap;
    144 	t->_dmamap_load = apecs_bus_dmamap_load_sgmap;
    145 	t->_dmamap_load_mbuf = apecs_bus_dmamap_load_mbuf_sgmap;
    146 	t->_dmamap_load_uio = apecs_bus_dmamap_load_uio_sgmap;
    147 	t->_dmamap_load_raw = apecs_bus_dmamap_load_raw_sgmap;
    148 	t->_dmamap_unload = apecs_bus_dmamap_unload_sgmap;
    149 	t->_dmamap_sync = NULL;		/* Nothing to do. */
    150 
    151 	t->_dmamem_alloc = _bus_dmamem_alloc;
    152 	t->_dmamem_free = _bus_dmamem_free;
    153 	t->_dmamem_map = _bus_dmamem_map;
    154 	t->_dmamem_unmap = _bus_dmamem_unmap;
    155 	t->_dmamem_mmap = _bus_dmamem_mmap;
    156 
    157 	/*
    158 	 * The firmware has set up window 2 as a 1G direct-mapped DMA
    159 	 * window beginning at 1G.  We leave it alone.  Disable
    160 	 * window 1.
    161 	 */
    162 	REGVAL(EPIC_PCI_BASE_1) = 0;
    163 	alpha_mb();
    164 
    165 	/*
    166 	 * Initialize the SGMAP if safe to do so.
    167 	 */
    168 	if (acp->ac_mallocsafe) {
    169 		alpha_sgmap_init(t, &acp->ac_sgmap, "apecs_sgmap",
    170 		    APECS_SGMAP_MAPPED_BASE, 0, (8*1024*1024),
    171 		    sizeof(u_int64_t), NULL, 0);
    172 
    173 		/*
    174 		 * Set up window 1 as an 8MB SGMAP-mapped window
    175 		 * starting at 8MB.
    176 		 */
    177 		tbase = acp->ac_sgmap.aps_ptpa >> EPIC_TBASE_SHIFT;
    178 		if ((tbase & EPIC_TBASE_T_BASE) != tbase)
    179 			panic("apecs_dma_init: bad page table address");
    180 		REGVAL(EPIC_TBASE_1) = tbase;
    181 		REGVAL(EPIC_PCI_MASK_1) = EPIC_PCI_MASK_8M;
    182 		alpha_mb();
    183 
    184 		REGVAL(EPIC_PCI_BASE_1) = APECS_SGMAP_MAPPED_BASE |
    185 		    EPIC_PCI_BASE_SGEN | EPIC_PCI_BASE_WENB;
    186 		alpha_mb();
    187 
    188 		APECS_TLB_INVALIDATE();
    189 	}
    190 
    191 	/* XXX XXX BEGIN XXX XXX */
    192 	{							/* XXX */
    193 		extern vm_offset_t alpha_XXX_dmamap_or;		/* XXX */
    194 		alpha_XXX_dmamap_or = APECS_DIRECT_MAPPED_BASE;	/* XXX */
    195 	}							/* XXX */
    196 	/* XXX XXX END XXX XXX */
    197 }
    198 
    199 /*
    200  * Return the bus dma tag to be used for the specified bus type.
    201  * INTERNAL USE ONLY!
    202  */
    203 bus_dma_tag_t
    204 apecs_dma_get_tag(t, bustype)
    205 	bus_dma_tag_t t;
    206 	alpha_bus_t bustype;
    207 {
    208 	struct apecs_config *acp = t->_cookie;
    209 
    210 	switch (bustype) {
    211 	case ALPHA_BUS_PCI:
    212 	case ALPHA_BUS_EISA:
    213 		/*
    214 		 * Systems with an APECS can only support 1G
    215 		 * of memory, so we use the direct-mapped window
    216 		 * on busses that have 32-bit DMA.
    217 		 */
    218 		return (&acp->ac_dmat_direct);
    219 
    220 	case ALPHA_BUS_ISA:
    221 		/*
    222 		 * ISA doesn't have enough address bits to use
    223 		 * the direct-mapped DMA window, so we must use
    224 		 * SGMAPs.
    225 		 */
    226 		return (&acp->ac_dmat_sgmap);
    227 
    228 	default:
    229 		panic("apecs_dma_get_tag: shouldn't be here, really...");
    230 	}
    231 }
    232 
    233 /*
    234  * Create an APECS SGMAP-mapped DMA map.
    235  */
    236 int
    237 apecs_bus_dmamap_create_sgmap(t, size, nsegments, maxsegsz, boundary,
    238     flags, dmamp)
    239 	bus_dma_tag_t t;
    240 	bus_size_t size;
    241 	int nsegments;
    242 	bus_size_t maxsegsz;
    243 	bus_size_t boundary;
    244 	int flags;
    245 	bus_dmamap_t *dmamp;
    246 {
    247 	struct apecs_config *acp = t->_cookie;
    248 	struct alpha_sgmap_cookie *a;
    249 	bus_dmamap_t map;
    250 	int error;
    251 
    252 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    253 	    boundary, flags, dmamp);
    254 	if (error)
    255 		return (error);
    256 
    257 	map = *dmamp;
    258 
    259 	a = malloc(sizeof(struct alpha_sgmap_cookie), M_DEVBUF,
    260 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
    261 	if (a == NULL) {
    262 		_bus_dmamap_destroy(t, map);
    263 		return (ENOMEM);
    264 	}
    265 	bzero(a, sizeof(struct alpha_sgmap_cookie));
    266 	map->_dm_sgcookie = a;
    267 
    268 	if (flags & BUS_DMA_ALLOCNOW) {
    269 		error = alpha_sgmap_alloc(map, round_page(size),
    270 		    &acp->ac_sgmap, flags);
    271 		if (error)
    272 			apecs_bus_dmamap_destroy_sgmap(t, map);
    273 	}
    274 
    275 	return (error);
    276 }
    277 
    278 /*
    279  * Destroy an APECS SGMAP-mapped DMA map.
    280  */
    281 void
    282 apecs_bus_dmamap_destroy_sgmap(t, map)
    283 	bus_dma_tag_t t;
    284 	bus_dmamap_t map;
    285 {
    286 	struct apecs_config *acp = t->_cookie;
    287 	struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
    288 
    289 	if (a->apdc_flags & APDC_HAS_SGMAP)
    290 		alpha_sgmap_free(&acp->ac_sgmap, a);
    291 
    292 	free(a, M_DEVBUF);
    293 	_bus_dmamap_destroy(t, map);
    294 }
    295 
    296 /*
    297  * Load an APECS direct-mapped DMA map with a linear buffer.
    298  */
    299 int
    300 apecs_bus_dmamap_load_direct(t, map, buf, buflen, p, flags)
    301 	bus_dma_tag_t t;
    302 	bus_dmamap_t map;
    303 	void *buf;
    304 	bus_size_t buflen;
    305 	struct proc *p;
    306 	int flags;
    307 {
    308 
    309 	return (_bus_dmamap_load_direct_common(t, map, buf, buflen, p,
    310 	    flags, APECS_DIRECT_MAPPED_BASE));
    311 }
    312 
    313 /*
    314  * Load an APECS SGMAP-mapped DMA map with a linear buffer.
    315  */
    316 int
    317 apecs_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
    318 	bus_dma_tag_t t;
    319 	bus_dmamap_t map;
    320 	void *buf;
    321 	bus_size_t buflen;
    322 	struct proc *p;
    323 	int flags;
    324 {
    325 	struct apecs_config *acp = t->_cookie;
    326 	int error;
    327 
    328 	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
    329 	    &acp->ac_sgmap);
    330 	if (error == 0)
    331 		APECS_TLB_INVALIDATE();
    332 
    333 	return (error);
    334 }
    335 
    336 /*
    337  * Load an APECS direct-mapped DMA map with an mbuf chain.
    338  */
    339 int
    340 apecs_bus_dmamap_load_mbuf_direct(t, map, m, flags)
    341 	bus_dma_tag_t t;
    342 	bus_dmamap_t map;
    343 	struct mbuf *m;
    344 	int flags;
    345 {
    346 
    347 	return (_bus_dmamap_load_mbuf_direct_common(t, map, m,
    348 	    flags, APECS_DIRECT_MAPPED_BASE));
    349 }
    350 
    351 /*
    352  * Load an APECS SGMAP-mapped DMA map with an mbuf chain.
    353  */
    354 int
    355 apecs_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
    356 	bus_dma_tag_t t;
    357 	bus_dmamap_t map;
    358 	struct mbuf *m;
    359 	int flags;
    360 {
    361 	struct apecs_config *acp = t->_cookie;
    362 	int error;
    363 
    364 	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, &acp->ac_sgmap);
    365 	if (error == 0)
    366 		APECS_TLB_INVALIDATE();
    367 
    368 	return (error);
    369 }
    370 
    371 /*
    372  * Load an APECS direct-mapped DMA map with a uio.
    373  */
    374 int
    375 apecs_bus_dmamap_load_uio_direct(t, map, uio, flags)
    376 	bus_dma_tag_t t;
    377 	bus_dmamap_t map;
    378 	struct uio *uio;
    379 	int flags;
    380 {
    381 
    382 	return (_bus_dmamap_load_uio_direct_common(t, map, uio,
    383 	    flags, APECS_DIRECT_MAPPED_BASE));
    384 }
    385 
    386 /*
    387  * Load an APECS SGMAP-mapped DMA map with a uio.
    388  */
    389 int
    390 apecs_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
    391 	bus_dma_tag_t t;
    392 	bus_dmamap_t map;
    393 	struct uio *uio;
    394 	int flags;
    395 {
    396 	struct apecs_config *acp = t->_cookie;
    397 	int error;
    398 
    399 	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, &acp->ac_sgmap);
    400 	if (error == 0)
    401 		APECS_TLB_INVALIDATE();
    402 
    403 	return (error);
    404 }
    405 
    406 /*
    407  * Load an APECS direct-mapped DMA map with raw memory.
    408  */
    409 int
    410 apecs_bus_dmamap_load_raw_direct(t, map, segs, nsegs, size, flags)
    411 	bus_dma_tag_t t;
    412 	bus_dmamap_t map;
    413 	bus_dma_segment_t *segs;
    414 	int nsegs;
    415 	bus_size_t size;
    416 	int flags;
    417 {
    418 
    419 	return (_bus_dmamap_load_raw_direct_common(t, map, segs, nsegs,
    420 	    size, flags, APECS_DIRECT_MAPPED_BASE));
    421 }
    422 
    423 /*
    424  * Load an APECS SGMAP-mapped DMA map with raw memory.
    425  */
    426 int
    427 apecs_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
    428 	bus_dma_tag_t t;
    429 	bus_dmamap_t map;
    430 	bus_dma_segment_t *segs;
    431 	int nsegs;
    432 	bus_size_t size;
    433 	int flags;
    434 {
    435 	struct apecs_config *acp = t->_cookie;
    436 	int error;
    437 
    438 	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
    439 	    &acp->ac_sgmap);
    440 	if (error == 0)
    441 		APECS_TLB_INVALIDATE();
    442 
    443 	return (error);
    444 }
    445 
    446 /*
    447  * Unload an APECS DMA map.
    448  */
    449 void
    450 apecs_bus_dmamap_unload_sgmap(t, map)
    451 	bus_dma_tag_t t;
    452 	bus_dmamap_t map;
    453 {
    454 	struct apecs_config *acp = t->_cookie;
    455 
    456 	/*
    457 	 * Invalidate any SGMAP page table entries used by this
    458 	 * mapping.
    459 	 */
    460 	pci_sgmap_pte64_unload(t, map, &acp->ac_sgmap);
    461 	APECS_TLB_INVALIDATE();
    462 
    463 	/*
    464 	 * Do the generic bits of the unload.
    465 	 */
    466 	_bus_dmamap_unload(t, map);
    467 }
    468