Home | History | Annotate | Line # | Download | only in common
sgmap_common.c revision 1.18.2.2
      1  1.18.2.2  nathanw /* $NetBSD: sgmap_common.c,v 1.18.2.2 2002/10/18 02:34:06 nathanw Exp $ */
      2  1.18.2.2  nathanw 
      3  1.18.2.2  nathanw /*-
      4  1.18.2.2  nathanw  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
      5  1.18.2.2  nathanw  * All rights reserved.
      6  1.18.2.2  nathanw  *
      7  1.18.2.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.18.2.2  nathanw  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.18.2.2  nathanw  * NASA Ames Research Center.
     10  1.18.2.2  nathanw  *
     11  1.18.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     12  1.18.2.2  nathanw  * modification, are permitted provided that the following conditions
     13  1.18.2.2  nathanw  * are met:
     14  1.18.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     15  1.18.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     16  1.18.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.18.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     18  1.18.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     19  1.18.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     20  1.18.2.2  nathanw  *    must display the following acknowledgement:
     21  1.18.2.2  nathanw  *	This product includes software developed by the NetBSD
     22  1.18.2.2  nathanw  *	Foundation, Inc. and its contributors.
     23  1.18.2.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.18.2.2  nathanw  *    contributors may be used to endorse or promote products derived
     25  1.18.2.2  nathanw  *    from this software without specific prior written permission.
     26  1.18.2.2  nathanw  *
     27  1.18.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.18.2.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.18.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.18.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.18.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.18.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.18.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.18.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.18.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.18.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.18.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     38  1.18.2.2  nathanw  */
     39  1.18.2.2  nathanw 
     40  1.18.2.2  nathanw #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     41  1.18.2.2  nathanw 
     42  1.18.2.2  nathanw __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.18.2.2 2002/10/18 02:34:06 nathanw Exp $");
     43  1.18.2.2  nathanw 
     44  1.18.2.2  nathanw #include <sys/param.h>
     45  1.18.2.2  nathanw #include <sys/systm.h>
     46  1.18.2.2  nathanw #include <sys/kernel.h>
     47  1.18.2.2  nathanw #include <sys/malloc.h>
     48  1.18.2.2  nathanw #include <sys/proc.h>
     49  1.18.2.2  nathanw 
     50  1.18.2.2  nathanw #include <uvm/uvm_extern.h>
     51  1.18.2.2  nathanw 
     52  1.18.2.2  nathanw #define	_ALPHA_BUS_DMA_PRIVATE
     53  1.18.2.2  nathanw #include <machine/bus.h>
     54  1.18.2.2  nathanw 
     55  1.18.2.2  nathanw #include <alpha/common/sgmapvar.h>
     56  1.18.2.2  nathanw 
     57  1.18.2.2  nathanw /*
     58  1.18.2.2  nathanw  * Some systems will prefetch the next page during a memory -> device DMA.
     59  1.18.2.2  nathanw  * This can cause machine checks if there is not a spill page after the
     60  1.18.2.2  nathanw  * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE).
     61  1.18.2.2  nathanw  */
     62  1.18.2.2  nathanw vaddr_t		alpha_sgmap_prefetch_spill_page_va;
     63  1.18.2.2  nathanw bus_addr_t	alpha_sgmap_prefetch_spill_page_pa;
     64  1.18.2.2  nathanw 
     65  1.18.2.2  nathanw void
     66  1.18.2.2  nathanw alpha_sgmap_init(bus_dma_tag_t t, struct alpha_sgmap *sgmap, const char *name,
     67  1.18.2.2  nathanw     bus_addr_t wbase, bus_addr_t sgvabase, bus_size_t sgvasize, size_t ptesize,
     68  1.18.2.2  nathanw     void *ptva, bus_size_t minptalign)
     69  1.18.2.2  nathanw {
     70  1.18.2.2  nathanw 	bus_dma_segment_t seg;
     71  1.18.2.2  nathanw 	size_t ptsize;
     72  1.18.2.2  nathanw 	int rseg;
     73  1.18.2.2  nathanw 
     74  1.18.2.2  nathanw 	if (sgvasize & PGOFSET) {
     75  1.18.2.2  nathanw 		printf("size botch for sgmap `%s'\n", name);
     76  1.18.2.2  nathanw 		goto die;
     77  1.18.2.2  nathanw 	}
     78  1.18.2.2  nathanw 
     79  1.18.2.2  nathanw 	sgmap->aps_wbase = wbase;
     80  1.18.2.2  nathanw 	sgmap->aps_sgvabase = sgvabase;
     81  1.18.2.2  nathanw 	sgmap->aps_sgvasize = sgvasize;
     82  1.18.2.2  nathanw 
     83  1.18.2.2  nathanw 	if (ptva != NULL) {
     84  1.18.2.2  nathanw 		/*
     85  1.18.2.2  nathanw 		 * We already have a page table; this may be a system
     86  1.18.2.2  nathanw 		 * where the page table resides in bridge-resident SRAM.
     87  1.18.2.2  nathanw 		 */
     88  1.18.2.2  nathanw 		sgmap->aps_pt = ptva;
     89  1.18.2.2  nathanw 		sgmap->aps_ptpa = 0;
     90  1.18.2.2  nathanw 	} else {
     91  1.18.2.2  nathanw 		/*
     92  1.18.2.2  nathanw 		 * Compute the page table size and allocate it.  At minimum,
     93  1.18.2.2  nathanw 		 * this must be aligned to the page table size.  However,
     94  1.18.2.2  nathanw 		 * some platforms have more strict alignment reqirements.
     95  1.18.2.2  nathanw 		 */
     96  1.18.2.2  nathanw 		ptsize = (sgvasize / NBPG) * ptesize;
     97  1.18.2.2  nathanw 		if (minptalign != 0) {
     98  1.18.2.2  nathanw 			if (minptalign < ptsize)
     99  1.18.2.2  nathanw 				minptalign = ptsize;
    100  1.18.2.2  nathanw 		} else
    101  1.18.2.2  nathanw 			minptalign = ptsize;
    102  1.18.2.2  nathanw 		if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
    103  1.18.2.2  nathanw 		    BUS_DMA_NOWAIT)) {
    104  1.18.2.2  nathanw 			panic("unable to allocate page table for sgmap `%s'",
    105  1.18.2.2  nathanw 			    name);
    106  1.18.2.2  nathanw 			goto die;
    107  1.18.2.2  nathanw 		}
    108  1.18.2.2  nathanw 		sgmap->aps_ptpa = seg.ds_addr;
    109  1.18.2.2  nathanw 		sgmap->aps_pt = (caddr_t)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa);
    110  1.18.2.2  nathanw 	}
    111  1.18.2.2  nathanw 
    112  1.18.2.2  nathanw 	/*
    113  1.18.2.2  nathanw 	 * Create the extent map used to manage the virtual address
    114  1.18.2.2  nathanw 	 * space.
    115  1.18.2.2  nathanw 	 */
    116  1.18.2.2  nathanw 	sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
    117  1.18.2.2  nathanw 	    M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
    118  1.18.2.2  nathanw 	if (sgmap->aps_ex == NULL) {
    119  1.18.2.2  nathanw 		printf("unable to create extent map for sgmap `%s'\n",
    120  1.18.2.2  nathanw 		    name);
    121  1.18.2.2  nathanw 		goto die;
    122  1.18.2.2  nathanw 	}
    123  1.18.2.2  nathanw 
    124  1.18.2.2  nathanw 	/*
    125  1.18.2.2  nathanw 	 * Allocate a spill page if that hasn't already been done.
    126  1.18.2.2  nathanw 	 */
    127  1.18.2.2  nathanw 	if (alpha_sgmap_prefetch_spill_page_va == 0) {
    128  1.18.2.2  nathanw 		if (bus_dmamem_alloc(t, NBPG, 0, 0, &seg, 1, &rseg,
    129  1.18.2.2  nathanw 		    BUS_DMA_NOWAIT)) {
    130  1.18.2.2  nathanw 			printf("unable to allocate spill page for sgmap `%s'\n",
    131  1.18.2.2  nathanw 			    name);
    132  1.18.2.2  nathanw 			goto die;
    133  1.18.2.2  nathanw 		}
    134  1.18.2.2  nathanw 		alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr;
    135  1.18.2.2  nathanw 		alpha_sgmap_prefetch_spill_page_va =
    136  1.18.2.2  nathanw 		    ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa);
    137  1.18.2.2  nathanw 		memset((caddr_t)alpha_sgmap_prefetch_spill_page_va, 0, NBPG);
    138  1.18.2.2  nathanw 	}
    139  1.18.2.2  nathanw 
    140  1.18.2.2  nathanw 	return;
    141  1.18.2.2  nathanw  die:
    142  1.18.2.2  nathanw 	panic("alpha_sgmap_init");
    143  1.18.2.2  nathanw }
    144  1.18.2.2  nathanw 
    145  1.18.2.2  nathanw int
    146  1.18.2.2  nathanw alpha_sgmap_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    147  1.18.2.2  nathanw     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
    148  1.18.2.2  nathanw {
    149  1.18.2.2  nathanw 	bus_dmamap_t map;
    150  1.18.2.2  nathanw 	int error;
    151  1.18.2.2  nathanw 
    152  1.18.2.2  nathanw 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
    153  1.18.2.2  nathanw 	    boundary, flags, &map);
    154  1.18.2.2  nathanw 	if (error)
    155  1.18.2.2  nathanw 		return (error);
    156  1.18.2.2  nathanw 
    157  1.18.2.2  nathanw 	/* XXX BUS_DMA_ALLOCNOW */
    158  1.18.2.2  nathanw 
    159  1.18.2.2  nathanw 	if (error == 0)
    160  1.18.2.2  nathanw 		*dmamp = map;
    161  1.18.2.2  nathanw 	else
    162  1.18.2.2  nathanw 		alpha_sgmap_dmamap_destroy(t, map);
    163  1.18.2.2  nathanw 
    164  1.18.2.2  nathanw 	return (error);
    165  1.18.2.2  nathanw }
    166  1.18.2.2  nathanw 
    167  1.18.2.2  nathanw void
    168  1.18.2.2  nathanw alpha_sgmap_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    169  1.18.2.2  nathanw {
    170  1.18.2.2  nathanw 
    171  1.18.2.2  nathanw 	KASSERT(map->dm_mapsize == 0);
    172  1.18.2.2  nathanw 
    173  1.18.2.2  nathanw 	_bus_dmamap_destroy(t, map);
    174  1.18.2.2  nathanw }
    175