Home | History | Annotate | Line # | Download | only in sun3x
dvma.c revision 1.7
      1 /*	$NetBSD: dvma.c,v 1.7 1998/01/22 22:12:36 gwr 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 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/device.h>
     80 #include <sys/proc.h>
     81 #include <sys/malloc.h>
     82 #include <sys/map.h>
     83 #include <sys/buf.h>
     84 #include <sys/vnode.h>
     85 #include <sys/user.h>
     86 #include <sys/core.h>
     87 #include <sys/exec.h>
     88 
     89 #include <vm/vm.h>
     90 #include <vm/vm_kern.h>
     91 #include <vm/vm_map.h>
     92 
     93 #include <machine/autoconf.h>
     94 #include <machine/cpu.h>
     95 #include <machine/enable.h>
     96 #include <machine/reg.h>
     97 #include <machine/pmap.h>
     98 #include <machine/dvma.h>
     99 #include <machine/machdep.h>
    100 
    101 #include "iommu.h"
    102 
    103 /*
    104  * Use a resource map to manage DVMA scratch-memory pages.
    105  */
    106 
    107 /* Number of slots in dvmamap. */
    108 int dvma_max_segs = 256;
    109 struct map *dvmamap;
    110 
    111 void
    112 dvma_init()
    113 {
    114 
    115 	/*
    116 	 * Create the resource map for DVMA pages.
    117 	 */
    118 	dvmamap = malloc((sizeof(struct map) * dvma_max_segs),
    119 					 M_DEVBUF, M_WAITOK);
    120 
    121 	rminit(dvmamap, btoc(DVMA_SPACE_LENGTH), btoc(0xFF000000),
    122 		   "dvmamap", dvma_max_segs);
    123 
    124 	/*
    125 	 * Enable DVMA in the System Enable register.
    126 	 * Note:  This is only necessary for VME slave accesses.
    127 	 *        On-board devices are always capable of DVMA.
    128 	 * *enable_reg |= ENA_SDVMA;
    129 	 */
    130 }
    131 
    132 
    133 /*
    134  * Given a DVMA address, return the physical address that
    135  * would be used by some OTHER bus-master besides the CPU.
    136  * (Examples: on-board ie/le, VME xy board).
    137  */
    138 u_long
    139 dvma_kvtopa(kva, bustype)
    140 	void * kva;
    141 	int bustype;
    142 {
    143 	u_long addr, mask;
    144 
    145 	addr = (u_long)kva;
    146 	if ((addr & DVMA_SPACE_START) != DVMA_SPACE_START)
    147 		panic("dvma_kvtopa: bad dmva addr=0x%x\n", addr);
    148 	mask = DVMA_SLAVE_MASK;
    149 
    150 	/* XXX: Only support a 24 bit mask for now. */
    151 	switch (bustype) {
    152 	case BUS_OBIO:
    153 	case BUS_OBMEM:
    154 	case BUS_VME32D32:
    155 	case BUS_VME24D32:
    156 	case BUS_VME24D16:
    157 		break;
    158 
    159 	case BUS_VME16D32:
    160 	case BUS_VME16D16:
    161 	default:
    162 		panic("dvma_kvtopa: bustype=%d\n", bustype);
    163 	}
    164 
    165 	return(addr & mask);
    166 }
    167 
    168 
    169 /*
    170  * Map a range [va, va+len] of wired virtual addresses in the given map
    171  * to a kernel address in DVMA space.
    172  */
    173 void *
    174 dvma_mapin(kmem_va, len, canwait)
    175 	void *  kmem_va;
    176 	int     len, canwait;
    177 {
    178 	void * dvma_addr;
    179 	vm_offset_t kva, tva;
    180 	register int npf, s;
    181 	register vm_offset_t pa;
    182 	long off, pn;
    183 
    184 	kva = (u_long)kmem_va;
    185 #ifdef	DIAGNOSTIC
    186 	/*
    187 	 * Addresses below VM_MIN_KERNEL_ADDRESS are not part of the kernel
    188 	 * map and should not participate in DVMA.
    189 	 */
    190 	if (kva < VM_MIN_KERNEL_ADDRESS)
    191 		panic("dvma_mapin: bad kva");
    192 #endif
    193 
    194 	/*
    195 	 * Calculate the offset of the data buffer from a page boundary.
    196 	 */
    197 	off = (int)kva & PGOFSET;
    198 	kva -= off;	/* Truncate starting address to nearest page. */
    199 	len = round_page(len + off); /* Round the buffer length to pages. */
    200 	npf = btoc(len); /* Determine the number of pages to be mapped. */
    201 
    202 	s = splimp();
    203 	for (;;) {
    204 		/*
    205 		 * Try to allocate DVMA space of the appropriate size
    206 		 * in which to do a transfer.
    207 		 */
    208 		pn = rmalloc(dvmamap, npf);
    209 
    210 		if (pn != 0)
    211 			break;
    212 		if (canwait) {
    213 			(void)tsleep(dvmamap, PRIBIO+1, "physio", 0);
    214 			continue;
    215 		}
    216 		splx(s);
    217 		return NULL;
    218 	}
    219 	splx(s);
    220 
    221 
    222 	/*
    223 	 * Tva is the starting page to which the data buffer will be double
    224 	 * mapped.  Dvma_addr is the starting address of the buffer within
    225 	 * that page and is the return value of the function.
    226 	 */
    227 	tva = ctob(pn);
    228 	dvma_addr = (void *) (tva + off);
    229 
    230 	for (;npf--; kva += NBPG, tva += NBPG) {
    231 		/*
    232 		 * Retrieve the physical address of each page in the buffer
    233 		 * and enter mappings into the I/O MMU so they may be seen
    234 		 * by external bus masters and into the special DVMA space
    235 		 * in the MC68030 MMU so they may be seen by the CPU.
    236 		 */
    237 		pa = pmap_extract(pmap_kernel(), kva);
    238 #ifdef	DEBUG
    239 		if (pa == 0)
    240 			panic("dvma_mapin: null page frame");
    241 #endif	DEBUG
    242 
    243 		iommu_enter((tva & IOMMU_VA_MASK), pa);
    244 		pmap_enter(pmap_kernel(), tva, pa | PMAP_NC,
    245 			VM_PROT_READ|VM_PROT_WRITE, 1);
    246 	}
    247 
    248 	return (dvma_addr);
    249 }
    250 
    251 /*
    252  * Remove double map of `va' in DVMA space at `kva'.
    253  *
    254  * TODO - This function might be the perfect place to handle the
    255  *       synchronization between the DVMA cache and central RAM
    256  *       on the 3/470.
    257  */
    258 void
    259 dvma_mapout(dvma_addr, len)
    260 	void *	dvma_addr;
    261 	int		len;
    262 {
    263 	u_long kva;
    264 	int s, off;
    265 
    266 	kva = (u_long)dvma_addr;
    267 	off = (int)kva & PGOFSET;
    268 	kva -= off;
    269 	len = round_page(len + off);
    270 
    271 	iommu_remove((kva & IOMMU_VA_MASK), len);
    272 
    273 	/*
    274 	 * XXX - don't call pmap_remove() with DVMA space yet.
    275 	 * XXX   It cannot (currently) handle the removal
    276 	 * XXX   of address ranges which do not participate in the
    277 	 * XXX   PV system by virtue of their _virtual_ addresses.
    278 	 * XXX   DVMA is one of these special address spaces.
    279 	 */
    280 #ifdef	DVMA_ON_PVLIST
    281 	pmap_remove(pmap_kernel(), kva, kva + len);
    282 #endif	/* DVMA_ON_PVLIST */
    283 
    284 	s = splimp();
    285 	rmfree(dvmamap, btoc(len), btoc(kva));
    286 	wakeup(dvmamap);
    287 	splx(s);
    288 }
    289 
    290 /*
    291  * Allocate actual memory pages in DVMA space.
    292  * (For sun3 compatibility - the ie driver.)
    293  */
    294 void *
    295 dvma_malloc(bytes)
    296 	size_t bytes;
    297 {
    298 	void *new_mem, *dvma_mem;
    299 	vm_size_t new_size;
    300 
    301 	if (!bytes)
    302 		return NULL;
    303 	new_size = m68k_round_page(bytes);
    304 	new_mem = (void*)kmem_alloc(kernel_map, new_size);
    305     if (!new_mem)
    306 		return NULL;
    307 	dvma_mem = dvma_mapin(new_mem, new_size, 1);
    308 	return (dvma_mem);
    309 }
    310