2001-04-24 08:30:50 +04:00
|
|
|
/* $NetBSD: dvma.c,v 1.19 2001/04/24 04:31:14 thorpej Exp $ */
|
1997-01-14 23:57:06 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1996 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Gordon W. Ross and Jeremy Cooper.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DVMA (Direct Virtual Memory Access - like DMA)
|
|
|
|
*
|
|
|
|
* In the Sun3 architecture, memory cycles initiated by secondary bus
|
|
|
|
* masters (DVMA devices) passed through the same MMU that governed CPU
|
|
|
|
* accesses. All DVMA devices were wired in such a way so that an offset
|
|
|
|
* was added to the addresses they issued, causing them to access virtual
|
|
|
|
* memory starting at address 0x0FF00000 - the offset. The task of
|
|
|
|
* enabling a DVMA device to access main memory only involved creating
|
|
|
|
* valid mapping in the MMU that translated these high addresses into the
|
|
|
|
* appropriate physical addresses.
|
|
|
|
*
|
|
|
|
* The Sun3x presents a challenge to programming DVMA because the MMU is no
|
|
|
|
* longer shared by both secondary bus masters and the CPU. The MC68030's
|
|
|
|
* built-in MMU serves only to manage virtual memory accesses initiated by
|
|
|
|
* the CPU. Secondary bus master bus accesses pass through a different MMU,
|
|
|
|
* aptly named the 'I/O Mapper'. To enable every device driver that uses
|
|
|
|
* DVMA to understand that these two address spaces are disconnected would
|
|
|
|
* require a tremendous amount of code re-writing. To avoid this, we will
|
|
|
|
* ensure that the I/O Mapper and the MC68030 MMU are programmed together,
|
|
|
|
* so that DVMA mappings are consistent in both the CPU virtual address
|
|
|
|
* space and secondary bus master address space - creating an environment
|
|
|
|
* just like the Sun3 system.
|
|
|
|
*
|
|
|
|
* The maximum address space that any DVMA device in the Sun3x architecture
|
|
|
|
* is capable of addressing is 24 bits wide (16 Megabytes.) We can alias
|
|
|
|
* all of the mappings that exist in the I/O mapper by duplicating them in
|
|
|
|
* a specially reserved section of the CPU's virtual address space, 16
|
|
|
|
* Megabytes in size. Whenever a DVMA buffer is allocated, the allocation
|
|
|
|
* code will enter in a mapping both in the MC68030 MMU page tables and the
|
|
|
|
* I/O mapper.
|
|
|
|
*
|
|
|
|
* The address returned by the allocation routine is a virtual address that
|
|
|
|
* the requesting driver must use to access the buffer. It is up to the
|
|
|
|
* device driver to convert this virtual address into the appropriate slave
|
1997-10-10 05:36:32 +04:00
|
|
|
* address that its device should issue to access the buffer. (There will be
|
|
|
|
* routines that assist the driver in doing so.)
|
1997-01-14 23:57:06 +03:00
|
|
|
*/
|
1998-06-10 00:47:17 +04:00
|
|
|
|
1997-01-14 23:57:06 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/map.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/core.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
|
1998-06-10 00:47:17 +04:00
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
1997-01-14 23:57:06 +03:00
|
|
|
#include <machine/autoconf.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/dvma.h>
|
1998-02-05 07:56:24 +03:00
|
|
|
#include <machine/pmap.h>
|
|
|
|
|
|
|
|
#include <sun3/sun3/machdep.h>
|
1997-01-14 23:57:06 +03:00
|
|
|
|
1998-02-05 07:56:24 +03:00
|
|
|
#include <sun3/sun3x/enable.h>
|
|
|
|
#include <sun3/sun3x/iommu.h>
|
1997-01-14 23:57:06 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use a resource map to manage DVMA scratch-memory pages.
|
1998-02-05 07:56:24 +03:00
|
|
|
* Note: SunOS says last three pages are reserved (PROM?)
|
|
|
|
* Note: need a separate map (sub-map?) for last 1MB for
|
|
|
|
* use by VME slave interface.
|
1997-01-14 23:57:06 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Number of slots in dvmamap. */
|
1998-01-23 01:20:35 +03:00
|
|
|
int dvma_max_segs = btoc(DVMA_MAP_SIZE);
|
1997-01-14 23:57:06 +03:00
|
|
|
struct map *dvmamap;
|
|
|
|
|
|
|
|
void
|
|
|
|
dvma_init()
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the resource map for DVMA pages.
|
|
|
|
*/
|
|
|
|
dvmamap = malloc((sizeof(struct map) * dvma_max_segs),
|
|
|
|
M_DEVBUF, M_WAITOK);
|
|
|
|
|
1998-01-23 01:20:35 +03:00
|
|
|
rminit(dvmamap, btoc(DVMA_MAP_AVAIL), btoc(DVMA_MAP_BASE),
|
1997-01-14 23:57:06 +03:00
|
|
|
"dvmamap", dvma_max_segs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable DVMA in the System Enable register.
|
|
|
|
* Note: This is only necessary for VME slave accesses.
|
|
|
|
* On-board devices are always capable of DVMA.
|
|
|
|
*/
|
1998-01-23 01:20:35 +03:00
|
|
|
*enable_reg |= ENA_SDVMA;
|
1997-01-14 23:57:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a DVMA address, return the physical address that
|
|
|
|
* would be used by some OTHER bus-master besides the CPU.
|
|
|
|
* (Examples: on-board ie/le, VME xy board).
|
|
|
|
*/
|
|
|
|
u_long
|
|
|
|
dvma_kvtopa(kva, bustype)
|
|
|
|
void * kva;
|
|
|
|
int bustype;
|
|
|
|
{
|
|
|
|
u_long addr, mask;
|
|
|
|
|
|
|
|
addr = (u_long)kva;
|
1998-01-23 01:20:35 +03:00
|
|
|
if ((addr & DVMA_MAP_BASE) != DVMA_MAP_BASE)
|
2000-11-03 07:52:27 +03:00
|
|
|
panic("dvma_kvtopa: bad dmva addr=0x%lx\n", addr);
|
1997-01-14 23:57:06 +03:00
|
|
|
|
1997-10-16 20:11:17 +04:00
|
|
|
switch (bustype) {
|
|
|
|
case BUS_OBIO:
|
|
|
|
case BUS_OBMEM:
|
1998-01-23 01:20:35 +03:00
|
|
|
mask = DVMA_OBIO_SLAVE_MASK;
|
|
|
|
break;
|
|
|
|
default: /* VME bus device. */
|
|
|
|
mask = DVMA_VME_SLAVE_MASK;
|
1997-10-16 20:11:17 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1997-01-14 23:57:06 +03:00
|
|
|
return(addr & mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map a range [va, va+len] of wired virtual addresses in the given map
|
|
|
|
* to a kernel address in DVMA space.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
dvma_mapin(kmem_va, len, canwait)
|
1997-02-24 04:37:52 +03:00
|
|
|
void * kmem_va;
|
|
|
|
int len, canwait;
|
1997-01-14 23:57:06 +03:00
|
|
|
{
|
|
|
|
void * dvma_addr;
|
|
|
|
vm_offset_t kva, tva;
|
|
|
|
register int npf, s;
|
1999-07-08 22:05:21 +04:00
|
|
|
paddr_t pa;
|
1997-01-14 23:57:06 +03:00
|
|
|
long off, pn;
|
1999-07-08 22:05:21 +04:00
|
|
|
boolean_t rv;
|
1997-01-14 23:57:06 +03:00
|
|
|
|
|
|
|
kva = (u_long)kmem_va;
|
1997-02-24 04:37:52 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
/*
|
|
|
|
* Addresses below VM_MIN_KERNEL_ADDRESS are not part of the kernel
|
|
|
|
* map and should not participate in DVMA.
|
|
|
|
*/
|
1997-01-14 23:57:06 +03:00
|
|
|
if (kva < VM_MIN_KERNEL_ADDRESS)
|
|
|
|
panic("dvma_mapin: bad kva");
|
1997-02-24 04:37:52 +03:00
|
|
|
#endif
|
1997-01-14 23:57:06 +03:00
|
|
|
|
1997-02-24 04:37:52 +03:00
|
|
|
/*
|
|
|
|
* Calculate the offset of the data buffer from a page boundary.
|
|
|
|
*/
|
1997-01-14 23:57:06 +03:00
|
|
|
off = (int)kva & PGOFSET;
|
1997-02-24 04:37:52 +03:00
|
|
|
kva -= off; /* Truncate starting address to nearest page. */
|
|
|
|
len = round_page(len + off); /* Round the buffer length to pages. */
|
|
|
|
npf = btoc(len); /* Determine the number of pages to be mapped. */
|
1997-01-14 23:57:06 +03:00
|
|
|
|
2001-01-14 06:22:04 +03:00
|
|
|
s = splvm();
|
1997-01-14 23:57:06 +03:00
|
|
|
for (;;) {
|
1997-02-24 04:37:52 +03:00
|
|
|
/*
|
|
|
|
* Try to allocate DVMA space of the appropriate size
|
|
|
|
* in which to do a transfer.
|
|
|
|
*/
|
1997-01-14 23:57:06 +03:00
|
|
|
pn = rmalloc(dvmamap, npf);
|
|
|
|
|
|
|
|
if (pn != 0)
|
|
|
|
break;
|
|
|
|
if (canwait) {
|
|
|
|
(void)tsleep(dvmamap, PRIBIO+1, "physio", 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
1997-02-24 04:37:52 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tva is the starting page to which the data buffer will be double
|
|
|
|
* mapped. Dvma_addr is the starting address of the buffer within
|
|
|
|
* that page and is the return value of the function.
|
|
|
|
*/
|
1997-01-14 23:57:06 +03:00
|
|
|
tva = ctob(pn);
|
|
|
|
dvma_addr = (void *) (tva + off);
|
|
|
|
|
1997-02-24 04:37:52 +03:00
|
|
|
for (;npf--; kva += NBPG, tva += NBPG) {
|
|
|
|
/*
|
|
|
|
* Retrieve the physical address of each page in the buffer
|
|
|
|
* and enter mappings into the I/O MMU so they may be seen
|
|
|
|
* by external bus masters and into the special DVMA space
|
|
|
|
* in the MC68030 MMU so they may be seen by the CPU.
|
|
|
|
*/
|
1999-07-08 22:05:21 +04:00
|
|
|
rv = pmap_extract(pmap_kernel(), kva, &pa);
|
1997-02-24 04:37:52 +03:00
|
|
|
#ifdef DEBUG
|
1999-07-08 22:05:21 +04:00
|
|
|
if (rv == FALSE)
|
1997-01-14 23:57:06 +03:00
|
|
|
panic("dvma_mapin: null page frame");
|
1997-02-24 04:37:52 +03:00
|
|
|
#endif DEBUG
|
1997-01-14 23:57:06 +03:00
|
|
|
|
1998-01-23 01:12:36 +03:00
|
|
|
iommu_enter((tva & IOMMU_VA_MASK), pa);
|
1997-01-14 23:57:06 +03:00
|
|
|
pmap_enter(pmap_kernel(), tva, pa | PMAP_NC,
|
1999-11-13 03:30:26 +03:00
|
|
|
VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
|
1997-01-14 23:57:06 +03:00
|
|
|
}
|
2001-04-24 08:30:50 +04:00
|
|
|
pmap_update();
|
1997-01-14 23:57:06 +03:00
|
|
|
|
|
|
|
return (dvma_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove double map of `va' in DVMA space at `kva'.
|
1997-02-24 04:37:52 +03:00
|
|
|
*
|
|
|
|
* TODO - This function might be the perfect place to handle the
|
|
|
|
* synchronization between the DVMA cache and central RAM
|
|
|
|
* on the 3/470.
|
1997-01-14 23:57:06 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
dvma_mapout(dvma_addr, len)
|
|
|
|
void * dvma_addr;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
u_long kva;
|
|
|
|
int s, off;
|
|
|
|
|
|
|
|
kva = (u_long)dvma_addr;
|
|
|
|
off = (int)kva & PGOFSET;
|
|
|
|
kva -= off;
|
|
|
|
len = round_page(len + off);
|
|
|
|
|
1998-01-23 01:12:36 +03:00
|
|
|
iommu_remove((kva & IOMMU_VA_MASK), len);
|
1997-01-14 23:57:06 +03:00
|
|
|
|
1997-02-24 04:37:52 +03:00
|
|
|
/*
|
|
|
|
* XXX - don't call pmap_remove() with DVMA space yet.
|
|
|
|
* XXX It cannot (currently) handle the removal
|
|
|
|
* XXX of address ranges which do not participate in the
|
|
|
|
* XXX PV system by virtue of their _virtual_ addresses.
|
|
|
|
* XXX DVMA is one of these special address spaces.
|
|
|
|
*/
|
|
|
|
#ifdef DVMA_ON_PVLIST
|
1997-01-14 23:57:06 +03:00
|
|
|
pmap_remove(pmap_kernel(), kva, kva + len);
|
2001-04-24 08:30:50 +04:00
|
|
|
pmap_update();
|
1997-02-24 04:37:52 +03:00
|
|
|
#endif /* DVMA_ON_PVLIST */
|
1997-01-14 23:57:06 +03:00
|
|
|
|
2001-01-14 06:22:04 +03:00
|
|
|
s = splvm();
|
1997-01-14 23:57:06 +03:00
|
|
|
rmfree(dvmamap, btoc(len), btoc(kva));
|
|
|
|
wakeup(dvmamap);
|
|
|
|
splx(s);
|
|
|
|
}
|
1997-10-07 20:51:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate actual memory pages in DVMA space.
|
|
|
|
* (For sun3 compatibility - the ie driver.)
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
dvma_malloc(bytes)
|
|
|
|
size_t bytes;
|
|
|
|
{
|
|
|
|
void *new_mem, *dvma_mem;
|
|
|
|
vm_size_t new_size;
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
return NULL;
|
|
|
|
new_size = m68k_round_page(bytes);
|
1999-03-24 08:50:49 +03:00
|
|
|
new_mem = (void*)uvm_km_alloc(kernel_map, new_size);
|
|
|
|
if (!new_mem)
|
1997-10-07 20:51:47 +04:00
|
|
|
return NULL;
|
|
|
|
dvma_mem = dvma_mapin(new_mem, new_size, 1);
|
|
|
|
return (dvma_mem);
|
|
|
|
}
|
1998-02-05 07:56:24 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free pages from dvma_malloc()
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dvma_free(addr, size)
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
{
|
|
|
|
vm_size_t sz = m68k_round_page(size);
|
|
|
|
|
|
|
|
dvma_mapout(addr, sz);
|
|
|
|
/* XXX: need kmem address to free it...
|
|
|
|
Oh well, we never call this anyway. */
|
|
|
|
}
|