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