2006-05-07 16:40:13 +00:00
|
|
|
/**
|
|
|
|
|
** Proll (PROM replacement)
|
|
|
|
|
** iommu.c: Functions for DVMA management.
|
|
|
|
|
** Copyright 1999 Pete Zaitcev
|
|
|
|
|
** This code is licensed under GNU General Public License.
|
|
|
|
|
**/
|
|
|
|
|
#include "openbios/config.h"
|
|
|
|
|
#include "openbios/bindings.h"
|
|
|
|
|
#include "openbios/kernel.h"
|
|
|
|
|
#include "libc/byteorder.h"
|
|
|
|
|
#include "libc/vsprintf.h"
|
|
|
|
|
#include "libc/string.h"
|
|
|
|
|
|
|
|
|
|
#include "openbios/drivers.h"
|
|
|
|
|
#include "asm/asi.h"
|
|
|
|
|
#include "asm/crs.h"
|
|
|
|
|
#include "asm/io.h"
|
|
|
|
|
#include "pgtsrmmu.h"
|
|
|
|
|
#include "iommu.h"
|
|
|
|
|
|
2006-06-10 01:27:11 +00:00
|
|
|
#define IOMMU_REGS 0x300
|
|
|
|
|
#define NCTX_SWIFT 0x100
|
|
|
|
|
|
2006-05-07 16:40:13 +00:00
|
|
|
#define IOPERM (IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID)
|
|
|
|
|
#define MKIOPTE(phys) (((((phys)>>4) & IOPTE_PAGE) | IOPERM) & ~IOPTE_WAZ)
|
|
|
|
|
#define LOWMEMSZ 32 * 1024 * 1024
|
2006-05-16 17:36:09 +00:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_IOMMU
|
|
|
|
|
#define DPRINTF(fmt, args...) \
|
|
|
|
|
do { printk(fmt , ##args); } while (0)
|
|
|
|
|
#else
|
|
|
|
|
#define DPRINTF(fmt, args...)
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-05-07 16:40:13 +00:00
|
|
|
/*
|
|
|
|
|
* Allocatable memory chunk.
|
|
|
|
|
*/
|
|
|
|
|
struct mem {
|
|
|
|
|
char *start, *uplim;
|
|
|
|
|
char *curp;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct mem cmem; /* Current memory, virtual */
|
2008-11-30 11:54:01 +00:00
|
|
|
static struct mem cio; /* Current I/O space */
|
2006-05-07 16:40:13 +00:00
|
|
|
|
|
|
|
|
unsigned int va_shift;
|
|
|
|
|
|
2006-05-10 23:00:34 +00:00
|
|
|
static unsigned long *context_table;
|
|
|
|
|
static unsigned long *l1;
|
2006-05-07 16:40:13 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* IOMMU parameters
|
|
|
|
|
*/
|
|
|
|
|
struct iommu {
|
|
|
|
|
struct iommu_regs *regs;
|
|
|
|
|
unsigned int *page_table;
|
|
|
|
|
unsigned long plow; /* Base bus address */
|
|
|
|
|
unsigned long vasize; /* Size of VA region that we manage */
|
|
|
|
|
struct mem bmap;
|
|
|
|
|
};
|
|
|
|
|
|
2008-11-30 11:54:01 +00:00
|
|
|
static struct iommu ciommu;
|
2006-06-10 01:27:11 +00:00
|
|
|
static struct iommu_regs *regs;
|
2006-05-07 16:40:13 +00:00
|
|
|
|
2007-05-19 12:55:01 +00:00
|
|
|
static void iommu_init(struct iommu *t, uint64_t base);
|
2006-05-22 22:10:54 +00:00
|
|
|
|
2006-05-07 16:40:13 +00:00
|
|
|
static void
|
2006-06-10 01:27:11 +00:00
|
|
|
iommu_invalidate(struct iommu_regs *iregs)
|
|
|
|
|
{
|
|
|
|
|
iregs->tlbflush = 0;
|
2006-05-07 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Allocate memory. This is reusable.
|
|
|
|
|
*/
|
2006-05-22 10:37:34 +00:00
|
|
|
void
|
2006-05-07 16:40:13 +00:00
|
|
|
mem_init(struct mem *t, char *begin, char *limit)
|
|
|
|
|
{
|
|
|
|
|
t->start = begin;
|
|
|
|
|
t->uplim = limit;
|
|
|
|
|
t->curp = begin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
mem_alloc(struct mem *t, int size, int align)
|
|
|
|
|
{
|
|
|
|
|
char *p;
|
2006-05-22 10:37:34 +00:00
|
|
|
unsigned long pa;
|
2006-05-07 16:40:13 +00:00
|
|
|
|
2006-05-22 10:37:34 +00:00
|
|
|
// The alignment restrictions refer to physical, not virtual
|
|
|
|
|
// addresses
|
|
|
|
|
pa = va2pa((unsigned long)t->curp) + (align - 1);
|
|
|
|
|
pa &= ~(align - 1);
|
|
|
|
|
p = (char *)pa2va(pa);
|
2008-07-07 18:35:51 +00:00
|
|
|
|
2007-06-27 20:07:37 +00:00
|
|
|
if ((unsigned long)p >= (unsigned long)t->uplim ||
|
|
|
|
|
(unsigned long)p + size > (unsigned long)t->uplim)
|
2008-11-30 11:54:01 +00:00
|
|
|
return NULL;
|
2006-05-07 16:40:13 +00:00
|
|
|
t->curp = p + size;
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
mem_zalloc(struct mem *t, int size, int align)
|
|
|
|
|
{
|
|
|
|
|
char *p;
|
|
|
|
|
|
2008-11-30 11:54:01 +00:00
|
|
|
if ((p = mem_alloc(t, size, align)) != NULL)
|
2006-05-07 16:40:13 +00:00
|
|
|
memset(p, 0, size);
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 17:15:45 +00:00
|
|
|
static unsigned long
|
|
|
|
|
find_pte(unsigned long va, int alloc)
|
2006-05-07 16:40:13 +00:00
|
|
|
{
|
|
|
|
|
uint32_t pte;
|
|
|
|
|
void *p;
|
|
|
|
|
unsigned long pa;
|
|
|
|
|
|
2006-05-10 23:00:34 +00:00
|
|
|
pte = l1[(va >> SRMMU_PGDIR_SHIFT) & (SRMMU_PTRS_PER_PGD - 1)];
|
2006-05-07 16:40:13 +00:00
|
|
|
if ((pte & SRMMU_ET_MASK) == SRMMU_ET_INVALID) {
|
2007-04-26 17:15:45 +00:00
|
|
|
if (alloc) {
|
|
|
|
|
p = mem_zalloc(&cmem, SRMMU_PTRS_PER_PMD * sizeof(int),
|
|
|
|
|
SRMMU_PTRS_PER_PMD * sizeof(int));
|
2008-11-30 11:54:01 +00:00
|
|
|
if (p == NULL)
|
2007-05-05 18:34:01 +00:00
|
|
|
return -1;
|
2007-04-26 17:15:45 +00:00
|
|
|
pte = SRMMU_ET_PTD | ((va2pa((unsigned long)p)) >> 4);
|
|
|
|
|
l1[(va >> SRMMU_PGDIR_SHIFT) & (SRMMU_PTRS_PER_PGD - 1)] = pte;
|
|
|
|
|
/* barrier() */
|
|
|
|
|
} else {
|
2007-05-05 18:34:01 +00:00
|
|
|
return -1;
|
2007-04-26 17:15:45 +00:00
|
|
|
}
|
2006-05-07 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa = (pte & 0xFFFFFFF0) << 4;
|
|
|
|
|
pa += ((va >> SRMMU_PMD_SHIFT) & (SRMMU_PTRS_PER_PMD - 1)) << 2;
|
|
|
|
|
pte = *(uint32_t *)pa2va(pa);
|
|
|
|
|
if ((pte & SRMMU_ET_MASK) == SRMMU_ET_INVALID) {
|
2007-04-26 17:15:45 +00:00
|
|
|
if (alloc) {
|
|
|
|
|
p = mem_zalloc(&cmem, SRMMU_PTRS_PER_PTE * sizeof(void *),
|
|
|
|
|
SRMMU_PTRS_PER_PTE * sizeof(void *));
|
2008-11-30 11:54:01 +00:00
|
|
|
if (p == NULL)
|
2007-05-05 18:34:01 +00:00
|
|
|
return -2;
|
2007-04-26 17:15:45 +00:00
|
|
|
pte = SRMMU_ET_PTD | ((va2pa((unsigned int)p)) >> 4);
|
|
|
|
|
*(uint32_t *)pa2va(pa) = pte;
|
|
|
|
|
} else {
|
2007-05-05 18:34:01 +00:00
|
|
|
return -2;
|
2007-04-26 17:15:45 +00:00
|
|
|
}
|
2006-05-07 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa = (pte & 0xFFFFFFF0) << 4;
|
|
|
|
|
pa += ((va >> PAGE_SHIFT) & (SRMMU_PTRS_PER_PTE - 1)) << 2;
|
|
|
|
|
|
2007-04-26 17:15:45 +00:00
|
|
|
return pa2va(pa);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create a memory mapping from va to epa.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2007-05-19 12:55:01 +00:00
|
|
|
map_page(unsigned long va, uint64_t epa, int type)
|
2007-04-26 17:15:45 +00:00
|
|
|
{
|
|
|
|
|
uint32_t pte;
|
|
|
|
|
unsigned long pa;
|
|
|
|
|
|
|
|
|
|
pa = find_pte(va, 1);
|
|
|
|
|
|
2006-05-07 16:40:13 +00:00
|
|
|
pte = SRMMU_ET_PTE | ((epa & PAGE_MASK) >> 4);
|
|
|
|
|
if (type) { /* I/O */
|
|
|
|
|
pte |= SRMMU_REF;
|
|
|
|
|
/* SRMMU cannot make Supervisor-only, but not exectutable */
|
|
|
|
|
pte |= SRMMU_PRIV;
|
|
|
|
|
} else { /* memory */
|
|
|
|
|
pte |= SRMMU_REF | SRMMU_CACHE;
|
|
|
|
|
pte |= SRMMU_PRIV; /* Supervisor only access */
|
|
|
|
|
}
|
2007-04-26 17:15:45 +00:00
|
|
|
*(uint32_t *)pa = pte;
|
2007-05-19 12:55:01 +00:00
|
|
|
DPRINTF("map_page: va 0x%lx pa 0x%llx pte 0x%x\n", va, epa, pte);
|
2006-05-07 16:40:13 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create an I/O mapping to pa[size].
|
|
|
|
|
* Returns va of the mapping or 0 if unsuccessful.
|
|
|
|
|
*/
|
|
|
|
|
void *
|
2007-05-19 12:55:01 +00:00
|
|
|
map_io(uint64_t pa, int size)
|
2006-05-07 16:40:13 +00:00
|
|
|
{
|
|
|
|
|
void *va;
|
|
|
|
|
unsigned int npages;
|
|
|
|
|
unsigned int off;
|
|
|
|
|
unsigned int mva;
|
|
|
|
|
|
|
|
|
|
off = pa & (PAGE_SIZE - 1);
|
2007-07-20 11:23:30 +00:00
|
|
|
npages = (off + size - 1) / PAGE_SIZE + 1;
|
2006-05-07 16:40:13 +00:00
|
|
|
pa &= ~(PAGE_SIZE - 1);
|
|
|
|
|
|
|
|
|
|
va = mem_alloc(&cio, npages * PAGE_SIZE, PAGE_SIZE);
|
2008-11-30 11:54:01 +00:00
|
|
|
if (va == NULL)
|
2006-05-07 16:40:13 +00:00
|
|
|
return va;
|
|
|
|
|
|
|
|
|
|
mva = (unsigned int) va;
|
2007-05-19 12:55:01 +00:00
|
|
|
DPRINTF("map_io: va 0x%p pa 0x%llx off 0x%x npages %d\n", va, pa, off, npages); /* P3 */
|
2006-05-07 16:40:13 +00:00
|
|
|
while (npages-- != 0) {
|
2006-05-10 23:00:34 +00:00
|
|
|
map_page(mva, pa, 1);
|
2006-05-07 16:40:13 +00:00
|
|
|
mva += PAGE_SIZE;
|
|
|
|
|
pa += PAGE_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (void *)((unsigned int)va + off);
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-26 17:15:45 +00:00
|
|
|
/*
|
|
|
|
|
* D5.3 pgmap@ ( va -- pte )
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
pgmap_fetch(void)
|
|
|
|
|
{
|
|
|
|
|
uint32_t pte;
|
|
|
|
|
unsigned long va, pa;
|
|
|
|
|
|
|
|
|
|
va = POP();
|
|
|
|
|
|
|
|
|
|
pa = find_pte(va, 0);
|
|
|
|
|
if (pa == 1 || pa == 2)
|
|
|
|
|
goto error;
|
|
|
|
|
pte = *(uint32_t *)pa;
|
|
|
|
|
DPRINTF("pgmap@: va 0x%lx pa 0x%lx pte 0x%x\n", va, pa, pte);
|
|
|
|
|
|
|
|
|
|
PUSH(pte);
|
|
|
|
|
return;
|
|
|
|
|
error:
|
|
|
|
|
PUSH(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* D5.3 pgmap! ( pte va -- )
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
pgmap_store(void)
|
|
|
|
|
{
|
|
|
|
|
uint32_t pte;
|
|
|
|
|
unsigned long va, pa;
|
|
|
|
|
|
|
|
|
|
va = POP();
|
|
|
|
|
pte = POP();
|
|
|
|
|
|
|
|
|
|
pa = find_pte(va, 1);
|
|
|
|
|
*(uint32_t *)pa = pte;
|
|
|
|
|
DPRINTF("pgmap!: va 0x%lx pa 0x%lx pte 0x%x\n", va, pa, pte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* D5.3 map-pages ( pa space va size -- )
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
map_pages(void)
|
|
|
|
|
{
|
2007-05-19 12:55:01 +00:00
|
|
|
unsigned long va;
|
2007-04-26 17:15:45 +00:00
|
|
|
int size;
|
2007-05-19 12:55:01 +00:00
|
|
|
uint64_t pa;
|
2007-04-26 17:15:45 +00:00
|
|
|
|
|
|
|
|
size = POP();
|
|
|
|
|
va = POP();
|
|
|
|
|
pa = POP();
|
2007-05-19 12:55:01 +00:00
|
|
|
pa <<= 32;
|
|
|
|
|
pa |= POP() & 0xffffffff;
|
2007-04-26 17:15:45 +00:00
|
|
|
|
|
|
|
|
for (; size > 0; size -= PAGE_SIZE, pa += PAGE_SIZE, va += PAGE_SIZE)
|
|
|
|
|
map_page(va, pa, 1);
|
|
|
|
|
DPRINTF("map-page: va 0x%lx pa 0x%lx size 0x%x\n", va, pa, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-05-07 16:40:13 +00:00
|
|
|
void
|
2007-05-19 12:55:01 +00:00
|
|
|
ob_init_mmu(uint64_t base)
|
2006-05-07 16:40:13 +00:00
|
|
|
{
|
2006-05-22 22:10:54 +00:00
|
|
|
extern unsigned int qemu_mem_size;
|
2006-05-07 16:40:13 +00:00
|
|
|
|
2006-05-22 22:10:54 +00:00
|
|
|
push_str("/memory");
|
|
|
|
|
fword("find-device");
|
|
|
|
|
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
PUSH(qemu_mem_size);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
push_str("reg");
|
|
|
|
|
fword("property");
|
|
|
|
|
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
Patch for SunOS compatibility from pjcreath+openbios@gmail.com:
I've been trying to get old versions of SunOS to load under qemu. In
doing so, I've encountered a number of bugs in OBP. I'm not always
certain of the best fix, but I can at least provide a quick hack that
will get people farther along.
1) Error message: "kmem_alloc failed, nbytes 680"
Bug: obp_dumb_memalloc is a bit too dumb. It needs to pick an address
if passed a null address. (According to the comment in the allocator
in OpenSolaris prom_alloc.c (see
<http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/psm/promif/ieee1275/sun4/prom_alloc.c>),
"If virthint is zero, a suitable virt is chosen.")
Quick fix: If passed a null address, start doling out addresses at
10MB and increment by size.
Shortcomings: The quick fix ignores the issue of free() and doesn't
remove memory from the virtual-memory/available node.
After the quick fix, the boot gets farther, leading us to:
2) Error message: "Unhandled Exception 0x00000080"
Bug: Trap 0 (entry 0x80 in the table, i.e. syscall_trap_4x) is
undefined. This is because the SunOS bootloader installs the trap by
writing code in the trap table, but the trap table is in the .text
section of OpenBIOS. Thus the trap 0 handler simply jumps to "bug".
Quick fix: Move the trap table to the .data section. Insert a "b
entry; nop; nop; nop;" before "bug:".
Shortcomings: Requires the extra "b entry" code. Allows the only VM
copy of the trap table to be permanently changed. OpenBIOS should
copy the read-only trap table to read-write memory (and update %tbr)
upon reset/entry.
3) #2 above actually exposes another bug. The write to the read-only
trap table does not cause an access violation -- instead, it silently
fails. The "std" instruction at 0x403e6c in the bootloader has no
effect.
Bug: Uncertain. It could be a systemic bug in qemu, but it appears
that the VM's MMU believes that the page is writable. That means that
the VM's MMU is not having the access protection flags set for pages
mapped to ROM. It thinks everything is rwx.
Fix?: The VM's MMU should have the access protection flags properly
set for each ROM section. This should probably be done within
OpenBIOS. E.g., .text should be r-x, .data should probably be rwx,
etc.
This is the one fix I'm really not sure how to implement. Any
suggestions? This may be a problem that only affects this bootloader,
so fixing #2 above may be all that's strictly necessary. But I'm not
positive that this bug doesn't have other ill effects I haven't found
yet.
At any rate, fixing #2 gets us still further, to:
4) Error messages:
"obp_devopen(sd(0,0,0):d) = 0xffd8e270
obp_inst2pkg(fd 0xffd8e270) = 0xffd57f44
obp_getprop(0xffd57f44, device_type) (not found)"
Bug: The OpenBIOS "interpose" implementation is not transparent to
non-interposition-aware code (in violation of the interposition spec).
The inst2pkg call in this sequence returns the phandle for
/packages/misc-files, instead of the proper phandle.
Quick fix: Comment out the "interpose disk-label" lines in ob_sd_open.
Shortcomings: It disables disk-label. The correct fix is to fix the
underlying problem with interposition, but I'm not sure exactly what
it is. Could someone help?
Fixing #4 gets us quite a bit further, until:
5) Error message:
"Unhandled Exception 0x00000009
PC = 0xf0138b20 NPC = 0xf0138b24
Stopping execution"
Bug: The instruction is trying to read from 0xfd020000+4, which is an
invalid address. This address isn't mapped by OBP by default on Sun
hardware, so the bootloader must be trying to (a) map this address and
failing silently or (b) skipping the mapping for some reason. The
instruction is hard-coded to look at this absolute address.
Fix: Unknown. This may be another instance of writes silently
failing, hence my interest in #3 above. It could also be a
side-effect of the quick fix for #4.
6) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008f0c addr=feff8008 mmu_fsr=3a6 rw=2
MMU sfsr=3a6: Invalid Address on supv data store at level 3
regs at fd008f0c:
psr=4400fc7 pc=f00053f4 npc=f00053f8
..."
Bug: Real sun4m hardware registers 4 CPU-specific interrupts followed
by a system-wide interrupt, regardless of the number of CPUs
installed. The same is true of counters. SunOS looks at the 5th
interrupt for the system-wide interrupt. OBP, since there's only one
CPU, just sets up one CPU-specific interrupt followed by the
system-wide interrupt, so there is no 5th interrupt. See the comment
on "NCPU" at
<http://stuff.mit.edu/afs/athena/astaff/project/opssrc/sys.sunos/sun4m/devaddr.h>.
Fix: in obp_interrupt_init() and obp_counter_init() register 4
CPU-specific interrupts before allocating the system-wide interrupt.
The kernel will then map the 5th interrupt to the system-wide
interrupt.
7) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008d8c addr=7ff000 mmu_fsr=126 rw=1
MMU sfsr=126: Invalid Address on supv data fetch at level 1
regs at fd008d8c:
psr=4000cc4 pc=f01339a4 npc=f01339a8
..."
Bug: The command-line arguments passed to the kernel are fixed at
address 0x7FF000 (CMDLINE_ADDR, passed from qemu via nv_info.cmdline),
which is no longer mapped by the time the kernel looks at the boot
arguments. A regular Sun boot ROM will copy this into mapped memory.
Fix: Copy the string in nv_info.cmdline to a OpenBIOS global (since
OpenBIOS continues to be mapped) in ob_nvram_init().
8) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008dec addr=1019000 mmu_fsr=126 rw=1
MMU sfsr=126: Invalid Address on supv data fetch at level 1
regs at fd008dec:
psr=4400cc5 pc=f0131680 npc=f0131684
..."
Bug: The dumb memory allocator from bug #1 was allocating a range that
the SunOS 4 kernel doesn't like.
Fix: Mimic the Sun boot ROM allocator: the top of the heap should be
a 0xFFEDA000 and allocations should return descending addresses. So,
for example, if asking for 0x1000 bytes, the first returned pointer
should be 0xFFED9000.
9) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008d2c addr=b1b91000 mmu_fsr=126 rw=1
MMU sfsr=126: Invalid Address on supv data fetch at level 1
regs at fd008d2c:
psr=4900cc3 pc=f0142c04 npc=f0142c08
..."
Bug: The precise underlying cause isn't clear. The bug appears due to
a variation between OBP's behavior and stock Sun behavior.
Fix: Add the "cache-physical?" property to the CPU node in
ob_nvram_init() and bump the "mmu-nctx" property up to 4096 (from
256).
git-svn-id: svn://coreboot.org/openbios/openbios-devel@114 f158a5a8-5612-0410-a976-696ce0be7e32
2007-03-09 00:59:05 +00:00
|
|
|
PUSH(va2pa((unsigned long)&_start) - PAGE_SIZE);
|
2006-05-22 22:10:54 +00:00
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
push_str("available");
|
|
|
|
|
fword("property");
|
|
|
|
|
|
|
|
|
|
push_str("/virtual-memory");
|
|
|
|
|
fword("find-device");
|
|
|
|
|
|
2007-05-19 12:55:01 +00:00
|
|
|
PUSH(base >> 32);
|
2006-05-22 22:10:54 +00:00
|
|
|
fword("encode-int");
|
2007-05-19 12:55:01 +00:00
|
|
|
PUSH(base & 0xffffffff);
|
2006-05-22 22:10:54 +00:00
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
2006-06-10 01:27:11 +00:00
|
|
|
PUSH(IOMMU_REGS);
|
2006-05-22 22:10:54 +00:00
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
push_str("reg");
|
|
|
|
|
fword("property");
|
|
|
|
|
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
2006-05-28 18:48:47 +00:00
|
|
|
PUSH((unsigned long)&_start - PAGE_SIZE);
|
2006-05-22 22:10:54 +00:00
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
PUSH(va2pa((unsigned long)&_iomem));
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
PUSH(-va2pa((unsigned long)&_iomem));
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
push_str("available");
|
|
|
|
|
fword("property");
|
2006-06-10 01:27:11 +00:00
|
|
|
|
|
|
|
|
push_str("/iommu");
|
|
|
|
|
fword("find-device");
|
|
|
|
|
PUSH((unsigned long)regs);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
push_str("address");
|
|
|
|
|
fword("property");
|
2007-04-09 12:35:41 +00:00
|
|
|
|
2007-05-19 12:55:01 +00:00
|
|
|
PUSH(base >> 32);
|
2007-04-09 12:35:41 +00:00
|
|
|
fword("encode-int");
|
2007-05-19 12:55:01 +00:00
|
|
|
PUSH(base & 0xffffffff);
|
2007-04-09 12:35:41 +00:00
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
PUSH(IOMMU_REGS);
|
|
|
|
|
fword("encode-int");
|
|
|
|
|
fword("encode+");
|
|
|
|
|
push_str("reg");
|
|
|
|
|
fword("property");
|
2007-04-26 17:15:45 +00:00
|
|
|
|
|
|
|
|
PUSH(0);
|
|
|
|
|
fword("active-package!");
|
|
|
|
|
bind_func("pgmap@", pgmap_fetch);
|
|
|
|
|
bind_func("pgmap!", pgmap_store);
|
|
|
|
|
bind_func("map-pages", map_pages);
|
2006-06-05 12:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-10 01:27:11 +00:00
|
|
|
|
2006-06-05 12:35:11 +00:00
|
|
|
/*
|
|
|
|
|
* Switch page tables.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2007-05-19 12:55:01 +00:00
|
|
|
init_mmu_swift(uint64_t base)
|
2006-06-05 12:35:11 +00:00
|
|
|
{
|
|
|
|
|
unsigned int addr, i;
|
|
|
|
|
unsigned long pa, va;
|
2006-05-22 22:10:54 +00:00
|
|
|
|
2006-05-07 16:40:13 +00:00
|
|
|
mem_init(&cio, (char *)&_end, (char *)&_iomem);
|
|
|
|
|
|
|
|
|
|
context_table = mem_zalloc(&cmem, NCTX_SWIFT * sizeof(int), NCTX_SWIFT * sizeof(int));
|
|
|
|
|
l1 = mem_zalloc(&cmem, 256 * sizeof(int), 256 * sizeof(int));
|
|
|
|
|
|
|
|
|
|
context_table[0] = (((unsigned long)va2pa((unsigned long)l1)) >> 4) | SRMMU_ET_PTD;
|
|
|
|
|
|
|
|
|
|
for (i = 1; i < NCTX_SWIFT; i++) {
|
|
|
|
|
context_table[i] = SRMMU_ET_INVALID;
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < 256; i += 4) {
|
|
|
|
|
l1[i] = SRMMU_ET_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
Patch for SunOS compatibility from pjcreath+openbios@gmail.com:
I've been trying to get old versions of SunOS to load under qemu. In
doing so, I've encountered a number of bugs in OBP. I'm not always
certain of the best fix, but I can at least provide a quick hack that
will get people farther along.
1) Error message: "kmem_alloc failed, nbytes 680"
Bug: obp_dumb_memalloc is a bit too dumb. It needs to pick an address
if passed a null address. (According to the comment in the allocator
in OpenSolaris prom_alloc.c (see
<http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/psm/promif/ieee1275/sun4/prom_alloc.c>),
"If virthint is zero, a suitable virt is chosen.")
Quick fix: If passed a null address, start doling out addresses at
10MB and increment by size.
Shortcomings: The quick fix ignores the issue of free() and doesn't
remove memory from the virtual-memory/available node.
After the quick fix, the boot gets farther, leading us to:
2) Error message: "Unhandled Exception 0x00000080"
Bug: Trap 0 (entry 0x80 in the table, i.e. syscall_trap_4x) is
undefined. This is because the SunOS bootloader installs the trap by
writing code in the trap table, but the trap table is in the .text
section of OpenBIOS. Thus the trap 0 handler simply jumps to "bug".
Quick fix: Move the trap table to the .data section. Insert a "b
entry; nop; nop; nop;" before "bug:".
Shortcomings: Requires the extra "b entry" code. Allows the only VM
copy of the trap table to be permanently changed. OpenBIOS should
copy the read-only trap table to read-write memory (and update %tbr)
upon reset/entry.
3) #2 above actually exposes another bug. The write to the read-only
trap table does not cause an access violation -- instead, it silently
fails. The "std" instruction at 0x403e6c in the bootloader has no
effect.
Bug: Uncertain. It could be a systemic bug in qemu, but it appears
that the VM's MMU believes that the page is writable. That means that
the VM's MMU is not having the access protection flags set for pages
mapped to ROM. It thinks everything is rwx.
Fix?: The VM's MMU should have the access protection flags properly
set for each ROM section. This should probably be done within
OpenBIOS. E.g., .text should be r-x, .data should probably be rwx,
etc.
This is the one fix I'm really not sure how to implement. Any
suggestions? This may be a problem that only affects this bootloader,
so fixing #2 above may be all that's strictly necessary. But I'm not
positive that this bug doesn't have other ill effects I haven't found
yet.
At any rate, fixing #2 gets us still further, to:
4) Error messages:
"obp_devopen(sd(0,0,0):d) = 0xffd8e270
obp_inst2pkg(fd 0xffd8e270) = 0xffd57f44
obp_getprop(0xffd57f44, device_type) (not found)"
Bug: The OpenBIOS "interpose" implementation is not transparent to
non-interposition-aware code (in violation of the interposition spec).
The inst2pkg call in this sequence returns the phandle for
/packages/misc-files, instead of the proper phandle.
Quick fix: Comment out the "interpose disk-label" lines in ob_sd_open.
Shortcomings: It disables disk-label. The correct fix is to fix the
underlying problem with interposition, but I'm not sure exactly what
it is. Could someone help?
Fixing #4 gets us quite a bit further, until:
5) Error message:
"Unhandled Exception 0x00000009
PC = 0xf0138b20 NPC = 0xf0138b24
Stopping execution"
Bug: The instruction is trying to read from 0xfd020000+4, which is an
invalid address. This address isn't mapped by OBP by default on Sun
hardware, so the bootloader must be trying to (a) map this address and
failing silently or (b) skipping the mapping for some reason. The
instruction is hard-coded to look at this absolute address.
Fix: Unknown. This may be another instance of writes silently
failing, hence my interest in #3 above. It could also be a
side-effect of the quick fix for #4.
6) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008f0c addr=feff8008 mmu_fsr=3a6 rw=2
MMU sfsr=3a6: Invalid Address on supv data store at level 3
regs at fd008f0c:
psr=4400fc7 pc=f00053f4 npc=f00053f8
..."
Bug: Real sun4m hardware registers 4 CPU-specific interrupts followed
by a system-wide interrupt, regardless of the number of CPUs
installed. The same is true of counters. SunOS looks at the 5th
interrupt for the system-wide interrupt. OBP, since there's only one
CPU, just sets up one CPU-specific interrupt followed by the
system-wide interrupt, so there is no 5th interrupt. See the comment
on "NCPU" at
<http://stuff.mit.edu/afs/athena/astaff/project/opssrc/sys.sunos/sun4m/devaddr.h>.
Fix: in obp_interrupt_init() and obp_counter_init() register 4
CPU-specific interrupts before allocating the system-wide interrupt.
The kernel will then map the 5th interrupt to the system-wide
interrupt.
7) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008d8c addr=7ff000 mmu_fsr=126 rw=1
MMU sfsr=126: Invalid Address on supv data fetch at level 1
regs at fd008d8c:
psr=4000cc4 pc=f01339a4 npc=f01339a8
..."
Bug: The command-line arguments passed to the kernel are fixed at
address 0x7FF000 (CMDLINE_ADDR, passed from qemu via nv_info.cmdline),
which is no longer mapped by the time the kernel looks at the boot
arguments. A regular Sun boot ROM will copy this into mapped memory.
Fix: Copy the string in nv_info.cmdline to a OpenBIOS global (since
OpenBIOS continues to be mapped) in ob_nvram_init().
8) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008dec addr=1019000 mmu_fsr=126 rw=1
MMU sfsr=126: Invalid Address on supv data fetch at level 1
regs at fd008dec:
psr=4400cc5 pc=f0131680 npc=f0131684
..."
Bug: The dumb memory allocator from bug #1 was allocating a range that
the SunOS 4 kernel doesn't like.
Fix: Mimic the Sun boot ROM allocator: the top of the heap should be
a 0xFFEDA000 and allocations should return descending addresses. So,
for example, if asking for 0x1000 bytes, the first returned pointer
should be 0xFFED9000.
9) Error message:
"BAD TRAP: cpu=0 type=9 rp=fd008d2c addr=b1b91000 mmu_fsr=126 rw=1
MMU sfsr=126: Invalid Address on supv data fetch at level 1
regs at fd008d2c:
psr=4900cc3 pc=f0142c04 npc=f0142c08
..."
Bug: The precise underlying cause isn't clear. The bug appears due to
a variation between OBP's behavior and stock Sun behavior.
Fix: Add the "cache-physical?" property to the CPU node in
ob_nvram_init() and bump the "mmu-nctx" property up to 4096 (from
256).
git-svn-id: svn://coreboot.org/openbios/openbios-devel@114 f158a5a8-5612-0410-a976-696ce0be7e32
2007-03-09 00:59:05 +00:00
|
|
|
// text, rodata, data, and bss mapped to end of RAM
|
|
|
|
|
va = (unsigned long)&_start;
|
2006-05-07 16:40:13 +00:00
|
|
|
for (; va < (unsigned long)&_end; va += PAGE_SIZE) {
|
|
|
|
|
pa = va2pa(va);
|
2006-05-10 23:00:34 +00:00
|
|
|
map_page(va, pa, 0);
|
2006-05-07 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1:1 mapping for RAM
|
|
|
|
|
pa = va = 0;
|
|
|
|
|
for (; va < LOWMEMSZ; va += PAGE_SIZE, pa += PAGE_SIZE) {
|
2006-05-10 23:00:34 +00:00
|
|
|
map_page(va, pa, 0);
|
2006-05-07 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Flush cache
|
|
|
|
|
*/
|
|
|
|
|
for (addr = 0; addr < 0x2000; addr += 0x10) {
|
|
|
|
|
__asm__ __volatile__ ("sta %%g0, [%0] %1\n\t" : :
|
|
|
|
|
"r" (addr), "i" (ASI_M_DATAC_TAG));
|
|
|
|
|
__asm__ __volatile__ ("sta %%g0, [%0] %1\n\t" : :
|
|
|
|
|
"r" (addr<<1), "i" (ASI_M_TXTC_TAG));
|
|
|
|
|
}
|
|
|
|
|
srmmu_set_context(0);
|
|
|
|
|
srmmu_set_ctable_ptr(va2pa((unsigned long)context_table));
|
|
|
|
|
srmmu_flush_whole_tlb();
|
2006-05-22 22:10:54 +00:00
|
|
|
iommu_init(&ciommu, base);
|
2006-05-07 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* XXX This is a problematic interface. We alloc _memory_ which is uncached.
|
|
|
|
|
* So if we ever reuse allocations somebody is going to get uncached pages.
|
|
|
|
|
* Returned address is always aligned by page.
|
|
|
|
|
* BTW, we were not going to give away anonymous storage, were we not?
|
|
|
|
|
*/
|
|
|
|
|
void *
|
|
|
|
|
dvma_alloc(int size, unsigned int *pphys)
|
|
|
|
|
{
|
|
|
|
|
void *va;
|
|
|
|
|
unsigned int pa, ba;
|
|
|
|
|
unsigned int npages;
|
|
|
|
|
unsigned int mva, mpa;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
unsigned int *iopte;
|
|
|
|
|
struct iommu *t = &ciommu;
|
|
|
|
|
|
|
|
|
|
npages = (size + (PAGE_SIZE-1)) / PAGE_SIZE;
|
|
|
|
|
va = mem_alloc(&cmem, npages * PAGE_SIZE, PAGE_SIZE);
|
2008-11-30 11:54:01 +00:00
|
|
|
if (va == NULL)
|
|
|
|
|
return NULL;
|
2006-05-07 16:40:13 +00:00
|
|
|
|
|
|
|
|
ba = (unsigned int)mem_alloc(&t->bmap, npages * PAGE_SIZE, PAGE_SIZE);
|
|
|
|
|
if (ba == 0)
|
2008-11-30 11:54:01 +00:00
|
|
|
return NULL;
|
2006-05-07 16:40:13 +00:00
|
|
|
|
|
|
|
|
pa = (unsigned int)va2pa((unsigned long)va);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Change page attributes in MMU to uncached.
|
|
|
|
|
*/
|
|
|
|
|
mva = (unsigned int) va;
|
|
|
|
|
mpa = (unsigned int) pa;
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
2006-05-10 23:00:34 +00:00
|
|
|
map_page(mva, mpa, 1);
|
2006-05-07 16:40:13 +00:00
|
|
|
mva += PAGE_SIZE;
|
|
|
|
|
mpa += PAGE_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Map into IOMMU page table.
|
|
|
|
|
*/
|
|
|
|
|
mpa = (unsigned int) pa;
|
|
|
|
|
iopte = &t->page_table[(ba - t->plow) / PAGE_SIZE];
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
|
*iopte++ = MKIOPTE(mpa);
|
|
|
|
|
mpa += PAGE_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*pphys = ba;
|
|
|
|
|
|
|
|
|
|
return va;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Initialize IOMMU
|
|
|
|
|
* This looks like initialization of CPU MMU but
|
|
|
|
|
* the routine is higher in food chain.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
2007-05-19 12:55:01 +00:00
|
|
|
iommu_init(struct iommu *t, uint64_t base)
|
2006-05-07 16:40:13 +00:00
|
|
|
{
|
|
|
|
|
unsigned int *ptab;
|
|
|
|
|
int ptsize;
|
|
|
|
|
unsigned int impl, vers;
|
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
2006-06-10 01:27:11 +00:00
|
|
|
regs = map_io(base, IOMMU_REGS);
|
2008-11-30 11:54:01 +00:00
|
|
|
if (regs == NULL) {
|
2006-05-16 17:36:09 +00:00
|
|
|
DPRINTF("Cannot map IOMMU\n");
|
2006-05-07 16:40:13 +00:00
|
|
|
for (;;) { }
|
|
|
|
|
}
|
|
|
|
|
t->regs = regs;
|
|
|
|
|
impl = (regs->control & IOMMU_CTRL_IMPL) >> 28;
|
|
|
|
|
vers = (regs->control & IOMMU_CTRL_VERS) >> 24;
|
|
|
|
|
|
|
|
|
|
tmp = regs->control;
|
|
|
|
|
tmp &= ~(IOMMU_CTRL_RNGE);
|
|
|
|
|
|
|
|
|
|
tmp |= (IOMMU_RNGE_32MB | IOMMU_CTRL_ENAB);
|
|
|
|
|
t->plow = 0xfe000000; /* End - 32 MB */
|
|
|
|
|
t->vasize = 0x2000000; /* 32 MB */
|
|
|
|
|
|
|
|
|
|
regs->control = tmp;
|
|
|
|
|
iommu_invalidate(regs);
|
|
|
|
|
|
|
|
|
|
/* Allocate IOMMU page table */
|
|
|
|
|
/* Thremendous alignment causes great waste... */
|
|
|
|
|
ptsize = (t->vasize/PAGE_SIZE) * sizeof(int);
|
2008-11-30 11:54:01 +00:00
|
|
|
if ((ptab = mem_zalloc(&cmem, ptsize, ptsize)) == NULL) {
|
2006-05-16 17:36:09 +00:00
|
|
|
DPRINTF("Cannot allocate IOMMU table [0x%x]\n", ptsize);
|
2006-05-07 16:40:13 +00:00
|
|
|
for (;;) { }
|
|
|
|
|
}
|
|
|
|
|
t->page_table = ptab;
|
|
|
|
|
|
|
|
|
|
/* flush_cache_all(); */
|
|
|
|
|
/** flush_tlb_all(); **/
|
2006-05-22 10:37:34 +00:00
|
|
|
tmp = (unsigned int)va2pa((unsigned long)ptab);
|
|
|
|
|
regs->base = tmp >> 4;
|
2006-05-07 16:40:13 +00:00
|
|
|
iommu_invalidate(regs);
|
|
|
|
|
|
2006-05-22 10:37:34 +00:00
|
|
|
DPRINTF("IOMMU: impl %d vers %d page table at 0x%p (pa 0x%x) of size %d bytes\n",
|
|
|
|
|
impl, vers, t->page_table, tmp, ptsize);
|
2006-05-07 16:40:13 +00:00
|
|
|
|
|
|
|
|
mem_init(&t->bmap, (char*)t->plow, (char *)0xfffff000);
|
|
|
|
|
}
|