Home | History | Annotate | Line # | Download | only in common
sgmap_common.c revision 1.3
      1 /* $NetBSD: sgmap_common.c,v 1.3 1997/09/02 13:18:46 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: sgmap_common.c,v 1.3 1997/09/02 13:18:46 thorpej Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 #include <sys/proc.h>
     49 
     50 #include <vm/vm.h>
     51 
     52 #include <machine/bus.h>
     53 
     54 #include <alpha/common/sgmapvar.h>
     55 
     56 void
     57 alpha_sgmap_init(t, sgmap, name, wbase, sgvabase, sgvasize, ptesize, ptva)
     58 	bus_dma_tag_t t;
     59 	struct alpha_sgmap *sgmap;
     60 	const char *name;
     61 	bus_addr_t wbase;
     62 	bus_addr_t sgvabase;
     63 	bus_size_t sgvasize;
     64 	size_t ptesize;
     65 	void *ptva;
     66 {
     67 	bus_dma_segment_t seg;
     68 	size_t ptsize;
     69 	int rseg;
     70 
     71 	if (sgvasize & PGOFSET)
     72 		panic("alpha_sgmap_init: size botch");
     73 
     74 	sgmap->aps_wbase = wbase;
     75 	sgmap->aps_sgvabase = sgvabase;
     76 	sgmap->aps_sgvasize = sgvasize;
     77 
     78 	if (ptva != NULL) {
     79 		/*
     80 		 * We already have a page table; this may be a system
     81 		 * where the page table resides in bridge-resident SRAM.
     82 		 */
     83 		sgmap->aps_pt = ptva;
     84 		sgmap->aps_ptpa = 0;
     85 	} else {
     86 		/*
     87 		 * Compute the page table size and allocate it.  It must
     88 		 * be aligned to its size.
     89 		 */
     90 		ptsize = (sgvasize / NBPG) * ptesize;
     91 		if (bus_dmamem_alloc(t, ptsize, ptsize, 0, &seg, 1, &rseg,
     92 		    BUS_DMA_NOWAIT))
     93 			panic("alpha_sgmap_init: can't allocate page table");
     94 		if (bus_dmamem_map(t, &seg, rseg, ptsize,
     95 		    (caddr_t *)&sgmap->aps_pt, BUS_DMA_NOWAIT))
     96 			panic("alpha_sgmap_init: can't map page table");
     97 		sgmap->aps_ptpa = seg.ds_addr;
     98 	}
     99 
    100 	/*
    101 	 * Create the extent map used to manage the virtual address
    102 	 * space.
    103 	 */
    104 	sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
    105 	    M_DEVBUF, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
    106 	if (sgmap->aps_ex == NULL)
    107 		panic("alpha_sgmap_init: can't create extent map");
    108 }
    109 
    110 int
    111 alpha_sgmap_alloc(map, len, sgmap, flags)
    112 	bus_dmamap_t map;
    113 	bus_size_t len;
    114 	struct alpha_sgmap *sgmap;
    115 	int flags;
    116 {
    117 	struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
    118 	int error;
    119 
    120 #ifdef DIAGNOSTIC
    121 	if (a->apdc_flags & APDC_HAS_SGMAP)
    122 		panic("alpha_sgmap_alloc: already have sgva space");
    123 #endif
    124 
    125 	a->apdc_len = round_page(len);
    126 	error = extent_alloc(sgmap->aps_ex, a->apdc_len, NBPG,
    127 	    map->_dm_boundary, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT :
    128 	    EX_WAITOK, &a->apdc_sgva);
    129 
    130 	if (error == 0)
    131 		a->apdc_flags |= APDC_HAS_SGMAP;
    132 	else
    133 		a->apdc_flags &= ~APDC_HAS_SGMAP;
    134 
    135 	return (error);
    136 }
    137 
    138 void
    139 alpha_sgmap_free(sgmap, a)
    140 	struct alpha_sgmap *sgmap;
    141 	struct alpha_sgmap_cookie *a;
    142 {
    143 
    144 #ifdef DIAGNOSTIC
    145 	if ((a->apdc_flags & APDC_HAS_SGMAP) == 0)
    146 		panic("alpha_sgmap_free: no sgva space to free");
    147 #endif
    148 
    149 	if (extent_free(sgmap->aps_ex, a->apdc_sgva,
    150 	    a->apdc_len, EX_NOWAIT))
    151 		panic("alpha_sgmap_free");
    152 
    153 	a->apdc_flags &= ~APDC_HAS_SGMAP;
    154 }
    155