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