Home | History | Annotate | Line # | Download | only in sun3x
dvma.c revision 1.15
      1 /*	$NetBSD: dvma.c,v 1.15 2000/06/26 14:21:03 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross and Jeremy Cooper.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * DVMA (Direct Virtual Memory Access - like DMA)
     41  *
     42  * In the Sun3 architecture, memory cycles initiated by secondary bus
     43  * masters (DVMA devices) passed through the same MMU that governed CPU
     44  * accesses.  All DVMA devices were wired in such a way so that an offset
     45  * was added to the addresses they issued, causing them to access virtual
     46  * memory starting at address 0x0FF00000 - the offset.  The task of
     47  * enabling a DVMA device to access main memory only involved creating
     48  * valid mapping in the MMU that translated these high addresses into the
     49  * appropriate physical addresses.
     50  *
     51  * The Sun3x presents a challenge to programming DVMA because the MMU is no
     52  * longer shared by both secondary bus masters and the CPU.  The MC68030's
     53  * built-in MMU serves only to manage virtual memory accesses initiated by
     54  * the CPU.  Secondary bus master bus accesses pass through a different MMU,
     55  * aptly named the 'I/O Mapper'.  To enable every device driver that uses
     56  * DVMA to understand that these two address spaces are disconnected would
     57  * require a tremendous amount of code re-writing. To avoid this, we will
     58  * ensure that the I/O Mapper and the MC68030 MMU are programmed together,
     59  * so that DVMA mappings are consistent in both the CPU virtual address
     60  * space and secondary bus master address space - creating an environment
     61  * just like the Sun3 system.
     62  *
     63  * The maximum address space that any DVMA device in the Sun3x architecture
     64  * is capable of addressing is 24 bits wide (16 Megabytes.)  We can alias
     65  * all of the mappings that exist in the I/O mapper by duplicating them in
     66  * a specially reserved section of the CPU's virtual address space, 16
     67  * Megabytes in size.  Whenever a DVMA buffer is allocated, the allocation
     68  * code will enter in a mapping both in the MC68030 MMU page tables and the
     69  * I/O mapper.
     70  *
     71  * The address returned by the allocation routine is a virtual address that
     72  * the requesting driver must use to access the buffer.  It is up to the
     73  * device driver to convert this virtual address into the appropriate slave
     74  * address that its device should issue to access the buffer.  (There will be
     75  * routines that assist the driver in doing so.)
     76  */
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/device.h>
     81 #include <sys/proc.h>
     82 #include <sys/malloc.h>
     83 #include <sys/map.h>
     84 #include <sys/buf.h>
     85 #include <sys/vnode.h>
     86 #include <sys/user.h>
     87 #include <sys/core.h>
     88 #include <sys/exec.h>
     89 
     90 #include <vm/vm.h>
     91 
     92 #include <uvm/uvm_extern.h>
     93 
     94 #include <machine/autoconf.h>
     95 #include <machine/cpu.h>
     96 #include <machine/dvma.h>
     97 #include <machine/pmap.h>
     98 
     99 #include <sun3/sun3/machdep.h>
    100 
    101 #include <sun3/sun3x/enable.h>
    102 #include <sun3/sun3x/iommu.h>
    103 
    104 /*
    105  * Use a resource map to manage DVMA scratch-memory pages.
    106  * Note: SunOS says last three pages are reserved (PROM?)
    107  * Note: need a separate map (sub-map?) for last 1MB for
    108  *       use by VME slave interface.
    109  */
    110 
    111 /* Number of slots in dvmamap. */
    112 int dvma_max_segs = btoc(DVMA_MAP_SIZE);
    113 struct map *dvmamap;
    114 
    115 void
    116 dvma_init()
    117 {
    118 
    119 	/*
    120 	 * Create the resource map for DVMA pages.
    121 	 */
    122 	dvmamap = malloc((sizeof(struct map) * dvma_max_segs),
    123 					 M_DEVBUF, M_WAITOK);
    124 
    125 	rminit(dvmamap, btoc(DVMA_MAP_AVAIL), btoc(DVMA_MAP_BASE),
    126 		   "dvmamap", dvma_max_segs);
    127 
    128 	/*
    129 	 * Enable DVMA in the System Enable register.
    130 	 * Note:  This is only necessary for VME slave accesses.
    131 	 *        On-board devices are always capable of DVMA.
    132 	 */
    133 	*enable_reg |= ENA_SDVMA;
    134 }
    135 
    136 
    137 /*
    138  * Given a DVMA address, return the physical address that
    139  * would be used by some OTHER bus-master besides the CPU.
    140  * (Examples: on-board ie/le, VME xy board).
    141  */
    142 u_long
    143 dvma_kvtopa(kva, bustype)
    144 	void * kva;
    145 	int bustype;
    146 {
    147 	u_long addr, mask;
    148 
    149 	addr = (u_long)kva;
    150 	if ((addr & DVMA_MAP_BASE) != DVMA_MAP_BASE)
    151 		panic("dvma_kvtopa: bad dmva addr=0x%x\n", addr);
    152 
    153 	switch (bustype) {
    154 	case BUS_OBIO:
    155 	case BUS_OBMEM:
    156 		mask = DVMA_OBIO_SLAVE_MASK;
    157 		break;
    158 	default:	/* VME bus device. */
    159 		mask = DVMA_VME_SLAVE_MASK;
    160 		break;
    161 	}
    162 
    163 	return(addr & mask);
    164 }
    165 
    166 
    167 /*
    168  * Map a range [va, va+len] of wired virtual addresses in the given map
    169  * to a kernel address in DVMA space.
    170  */
    171 void *
    172 dvma_mapin(kmem_va, len, canwait)
    173 	void *  kmem_va;
    174 	int     len, canwait;
    175 {
    176 	void * dvma_addr;
    177 	vm_offset_t kva, tva;
    178 	register int npf, s;
    179 	paddr_t pa;
    180 	long off, pn;
    181 	boolean_t rv;
    182 
    183 	kva = (u_long)kmem_va;
    184 #ifdef	DIAGNOSTIC
    185 	/*
    186 	 * Addresses below VM_MIN_KERNEL_ADDRESS are not part of the kernel
    187 	 * map and should not participate in DVMA.
    188 	 */
    189 	if (kva < VM_MIN_KERNEL_ADDRESS)
    190 		panic("dvma_mapin: bad kva");
    191 #endif
    192 
    193 	/*
    194 	 * Calculate the offset of the data buffer from a page boundary.
    195 	 */
    196 	off = (int)kva & PGOFSET;
    197 	kva -= off;	/* Truncate starting address to nearest page. */
    198 	len = round_page(len + off); /* Round the buffer length to pages. */
    199 	npf = btoc(len); /* Determine the number of pages to be mapped. */
    200 
    201 	s = splimp();
    202 	for (;;) {
    203 		/*
    204 		 * Try to allocate DVMA space of the appropriate size
    205 		 * in which to do a transfer.
    206 		 */
    207 		pn = rmalloc(dvmamap, npf);
    208 
    209 		if (pn != 0)
    210 			break;
    211 		if (canwait) {
    212 			(void)tsleep(dvmamap, PRIBIO+1, "physio", 0);
    213 			continue;
    214 		}
    215 		splx(s);
    216 		return NULL;
    217 	}
    218 	splx(s);
    219 
    220 
    221 	/*
    222 	 * Tva is the starting page to which the data buffer will be double
    223 	 * mapped.  Dvma_addr is the starting address of the buffer within
    224 	 * that page and is the return value of the function.
    225 	 */
    226 	tva = ctob(pn);
    227 	dvma_addr = (void *) (tva + off);
    228 
    229 	for (;npf--; kva += NBPG, tva += NBPG) {
    230 		/*
    231 		 * Retrieve the physical address of each page in the buffer
    232 		 * and enter mappings into the I/O MMU so they may be seen
    233 		 * by external bus masters and into the special DVMA space
    234 		 * in the MC68030 MMU so they may be seen by the CPU.
    235 		 */
    236 		rv = pmap_extract(pmap_kernel(), kva, &pa);
    237 #ifdef	DEBUG
    238 		if (rv == FALSE)
    239 			panic("dvma_mapin: null page frame");
    240 #endif	DEBUG
    241 
    242 		iommu_enter((tva & IOMMU_VA_MASK), pa);
    243 		pmap_enter(pmap_kernel(), tva, pa | PMAP_NC,
    244 			VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
    245 	}
    246 
    247 	return (dvma_addr);
    248 }
    249 
    250 /*
    251  * Remove double map of `va' in DVMA space at `kva'.
    252  *
    253  * TODO - This function might be the perfect place to handle the
    254  *       synchronization between the DVMA cache and central RAM
    255  *       on the 3/470.
    256  */
    257 void
    258 dvma_mapout(dvma_addr, len)
    259 	void *	dvma_addr;
    260 	int		len;
    261 {
    262 	u_long kva;
    263 	int s, off;
    264 
    265 	kva = (u_long)dvma_addr;
    266 	off = (int)kva & PGOFSET;
    267 	kva -= off;
    268 	len = round_page(len + off);
    269 
    270 	iommu_remove((kva & IOMMU_VA_MASK), len);
    271 
    272 	/*
    273 	 * XXX - don't call pmap_remove() with DVMA space yet.
    274 	 * XXX   It cannot (currently) handle the removal
    275 	 * XXX   of address ranges which do not participate in the
    276 	 * XXX   PV system by virtue of their _virtual_ addresses.
    277 	 * XXX   DVMA is one of these special address spaces.
    278 	 */
    279 #ifdef	DVMA_ON_PVLIST
    280 	pmap_remove(pmap_kernel(), kva, kva + len);
    281 #endif	/* DVMA_ON_PVLIST */
    282 
    283 	s = splimp();
    284 	rmfree(dvmamap, btoc(len), btoc(kva));
    285 	wakeup(dvmamap);
    286 	splx(s);
    287 }
    288 
    289 /*
    290  * Allocate actual memory pages in DVMA space.
    291  * (For sun3 compatibility - the ie driver.)
    292  */
    293 void *
    294 dvma_malloc(bytes)
    295 	size_t bytes;
    296 {
    297 	void *new_mem, *dvma_mem;
    298 	vm_size_t new_size;
    299 
    300 	if (!bytes)
    301 		return NULL;
    302 	new_size = m68k_round_page(bytes);
    303 	new_mem = (void*)uvm_km_alloc(kernel_map, new_size);
    304 	if (!new_mem)
    305 		return NULL;
    306 	dvma_mem = dvma_mapin(new_mem, new_size, 1);
    307 	return (dvma_mem);
    308 }
    309 
    310 /*
    311  * Free pages from dvma_malloc()
    312  */
    313 void
    314 dvma_free(addr, size)
    315 	void *addr;
    316 	size_t size;
    317 {
    318 	vm_size_t sz = m68k_round_page(size);
    319 
    320 	dvma_mapout(addr, sz);
    321 	/* XXX: need kmem address to free it...
    322 	   Oh well, we never call this anyway. */
    323 }
    324