Home | History | Annotate | Line # | Download | only in sun3x
dvma.c revision 1.1
      1 /*	$NetBSD: dvma.c,v 1.1 1997/01/14 20:57:07 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.  (The will be
     75  * routines that will assist the driver in doing so.)
     76  *
     77  * XXX - This needs work.  The address from dvma_malloc() faults!
     78  */
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/device.h>
     82 #include <sys/proc.h>
     83 #include <sys/malloc.h>
     84 #include <sys/map.h>
     85 #include <sys/buf.h>
     86 #include <sys/vnode.h>
     87 #include <sys/user.h>
     88 #include <sys/core.h>
     89 #include <sys/exec.h>
     90 
     91 #include <vm/vm.h>
     92 #include <vm/vm_kern.h>
     93 #include <vm/vm_map.h>
     94 
     95 #include <machine/autoconf.h>
     96 #include <machine/cpu.h>
     97 #include <machine/enable.h>
     98 #include <machine/reg.h>
     99 #include <machine/pmap.h>
    100 #include <machine/dvma.h>
    101 
    102 #include "machdep.h"
    103 #include "iommu.h"
    104 
    105 /*
    106  * Use a resource map to manage DVMA scratch-memory pages.
    107  */
    108 
    109 /* Number of slots in dvmamap. */
    110 int dvma_max_segs = 256;
    111 struct map *dvmamap;
    112 
    113 void
    114 dvma_init()
    115 {
    116 
    117 	/*
    118 	 * Create the resource map for DVMA pages.
    119 	 */
    120 	dvmamap = malloc((sizeof(struct map) * dvma_max_segs),
    121 					 M_DEVBUF, M_WAITOK);
    122 
    123 	rminit(dvmamap, btoc(DVMA_SPACE_LENGTH), btoc(0xFF000000),
    124 		   "dvmamap", dvma_max_segs);
    125 
    126 	/*
    127 	 * Enable DVMA in the System Enable register.
    128 	 * Note:  This is only necessary for VME slave accesses.
    129 	 *        On-board devices are always capable of DVMA.
    130 	 * *enable_reg |= ENA_SDVMA;
    131 	 */
    132 }
    133 
    134 
    135 /*
    136  * Given a DVMA address, return the physical address that
    137  * would be used by some OTHER bus-master besides the CPU.
    138  * (Examples: on-board ie/le, VME xy board).
    139  */
    140 u_long
    141 dvma_kvtopa(kva, bustype)
    142 	void * kva;
    143 	int bustype;
    144 {
    145 	u_long addr, mask;
    146 
    147 	addr = (u_long)kva;
    148 	if ((addr & DVMA_SPACE_START) != DVMA_SPACE_START)
    149 		panic("dvma_kvtopa: bad dmva addr=0x%x\n", addr);
    150 
    151 	/* Everything has just 24 bits. */
    152 	mask = DVMA_SLAVE_MASK;
    153 
    154 	return(addr & mask);
    155 }
    156 
    157 
    158 /*
    159  * Map a range [va, va+len] of wired virtual addresses in the given map
    160  * to a kernel address in DVMA space.
    161  */
    162 void *
    163 dvma_mapin(kmem_va, len, canwait)
    164 	void *	kmem_va;
    165 	int		len, canwait;
    166 {
    167 	void * dvma_addr;
    168 	vm_offset_t kva, tva;
    169 	register int npf, s;
    170 	register vm_offset_t pa;
    171 	long off, pn;
    172 
    173 	kva = (u_long)kmem_va;
    174 	if (kva < VM_MIN_KERNEL_ADDRESS)
    175 		panic("dvma_mapin: bad kva");
    176 
    177 	off = (int)kva & PGOFSET;
    178 	kva -= off;
    179 	len = round_page(len + off);
    180 	npf = btoc(len);
    181 
    182 	s = splimp();
    183 	for (;;) {
    184 
    185 		pn = rmalloc(dvmamap, npf);
    186 
    187 		if (pn != 0)
    188 			break;
    189 		if (canwait) {
    190 			(void)tsleep(dvmamap, PRIBIO+1, "physio", 0);
    191 			continue;
    192 		}
    193 		splx(s);
    194 		return NULL;
    195 	}
    196 	splx(s);
    197 
    198 	tva = ctob(pn);
    199 	dvma_addr = (void *) (tva + off);
    200 
    201 	while (npf--) {
    202 		pa = pmap_extract(pmap_kernel(), kva);
    203 		if (pa == 0)
    204 			panic("dvma_mapin: null page frame");
    205 		pa = trunc_page(pa);
    206 
    207 		iommu_enter((tva & DVMA_SLAVE_MASK), pa);
    208 
    209 		pmap_enter(pmap_kernel(), tva, pa | PMAP_NC,
    210 			VM_PROT_READ|VM_PROT_WRITE, 1);
    211 
    212 		kva += NBPG;
    213 		tva += NBPG;
    214 	}
    215 
    216 	return (dvma_addr);
    217 }
    218 
    219 /*
    220  * Remove double map of `va' in DVMA space at `kva'.
    221  */
    222 void
    223 dvma_mapout(dvma_addr, len)
    224 	void *	dvma_addr;
    225 	int		len;
    226 {
    227 	u_long kva;
    228 	int s, off;
    229 
    230 	kva = (u_long)dvma_addr;
    231 	off = (int)kva & PGOFSET;
    232 	kva -= off;
    233 	len = round_page(len + off);
    234 
    235 	iommu_remove((kva & DVMA_SLAVE_MASK), len);
    236 
    237 	pmap_remove(pmap_kernel(), kva, kva + len);
    238 
    239 	s = splimp();
    240 	rmfree(dvmamap, btoc(len), btoc(kva));
    241 	wakeup(dvmamap);
    242 	splx(s);
    243 }
    244