Home | History | Annotate | Line # | Download | only in libsa
sun2.c revision 1.3
      1 /*	$NetBSD: sun2.c,v 1.3 2001/12/15 23:02:34 fredette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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 Matthew Fredette.
      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  * Standalone functions specific to the Sun2.
     41  */
     42 
     43 /* Need to avoid conflicts on these: */
     44 #define get_pte sun2_get_pte
     45 #define set_pte sun2_set_pte
     46 #define get_segmap sun2_get_segmap
     47 #define set_segmap sun2_set_segmap
     48 
     49 #include <sys/param.h>
     50 #include <machine/idprom.h>
     51 #include <machine/mon.h>
     52 
     53 #include <arch/sun2/include/pte.h>
     54 #include <arch/sun2/sun2/control.h>
     55 #ifdef notyet
     56 #include <arch/sun3/sun3/vme.h>
     57 #else
     58 #define VME16_BASE MBIO_BASE
     59 #define VME16_MASK MBIO_MASK
     60 #endif
     61 #include <arch/sun2/sun2/mbmem.h>
     62 #include <arch/sun2/sun2/mbio.h>
     63 
     64 #include <stand.h>
     65 
     66 #include "libsa.h"
     67 #include "dvma.h"
     68 #include "saio.h"	/* enum MAPTYPES */
     69 
     70 #define OBIO_MASK 0xFFFFFF
     71 
     72 u_int	get_pte __P((vaddr_t va));
     73 void	set_pte __P((vaddr_t va, u_int pte));
     74 char *	dvma2_alloc  __P((int len));
     75 void	dvma2_free  __P((char *dvma, int len));
     76 char *	dvma2_mapin  __P((char *pkt, int len));
     77 void	dvma2_mapout  __P((char *dmabuf, int len));
     78 char *	dev2_mapin  __P((int type, u_long addr, int len));
     79 
     80 struct mapinfo {
     81 	int maptype;
     82 	int pgtype;
     83 	u_int base;
     84 	u_int mask;
     85 };
     86 
     87 #ifdef	notyet
     88 struct mapinfo
     89 sun2_mapinfo[MAP__NTYPES] = {
     90 	/* On-board memory, I/O */
     91 	{ MAP_MAINMEM,   PGT_OBMEM,   0,          ~0 },
     92 	{ MAP_OBIO,      PGT_OBIO,    0,          OBIO_MASK },
     93 	/* Multibus memory, I/O */
     94 	{ MAP_MBMEM,     PGT_MBMEM, MBMEM_BASE, MBMEM_MASK },
     95 	{ MAP_MBIO,      PGT_MBIO,  MBIO_BASE, MBIO_MASK },
     96 	/* VME A16 */
     97 	{ MAP_VME16A16D, PGT_VME_D16, VME16_BASE, VME16_MASK },
     98 	{ MAP_VME16A32D, 0, 0, 0 },
     99 	/* VME A24 */
    100 	{ MAP_VME24A16D, 0, 0, 0 },
    101 	{ MAP_VME24A32D, 0, 0, 0 },
    102 	/* VME A32 */
    103 	{ MAP_VME32A16D, 0, 0, 0 },
    104 	{ MAP_VME32A32D, 0, 0, 0 },
    105 };
    106 #endif
    107 
    108 /* The virtual address we will use for PROM device mappings. */
    109 int sun2_devmap = SUN3_MONSHORTSEG;
    110 
    111 char *
    112 dev2_mapin(maptype, physaddr, length)
    113 	int maptype;
    114 	u_long physaddr;
    115 	int length;
    116 {
    117 #ifdef	notyet
    118 	u_int i, pa, pte, pgva, va;
    119 
    120 	if ((sun2_devmap + length) > SUN3_MONSHORTPAGE)
    121 		panic("dev2_mapin: length=%d\n", length);
    122 
    123 	for (i = 0; i < MAP__NTYPES; i++)
    124 		if (sun2_mapinfo[i].maptype == maptype)
    125 			goto found;
    126 	panic("dev2_mapin: bad maptype");
    127 found:
    128 
    129 	if (physaddr & ~(sun2_mapinfo[i].mask))
    130 		panic("dev2_mapin: bad address");
    131 	pa = sun2_mapinfo[i].base += physaddr;
    132 
    133 	pte = PA_PGNUM(pa) | PG_PERM |
    134 		sun2_mapinfo[i].pgtype;
    135 
    136 	va = pgva = sun2_devmap;
    137 	do {
    138 		set_pte(pgva, pte);
    139 		pgva += NBPG;
    140 		pte += 1;
    141 		length -= NBPG;
    142 	} while (length > 0);
    143 	sun2_devmap = pgva;
    144 	va += (physaddr & PGOFSET);
    145 
    146 #ifdef	DEBUG_PROM
    147 	if (debug)
    148 		printf("dev2_mapin: va=0x%x pte=0x%x\n",
    149 			   va, get_pte(va));
    150 #endif
    151 	return ((char*)va);
    152 #else
    153 	panic("dev2_mapin");
    154 	return(NULL);
    155 #endif
    156 }
    157 
    158 /*****************************************************************
    159  * DVMA support
    160  */
    161 
    162 /*
    163  * The easiest way to deal with the need for DVMA mappings is to
    164  * create a DVMA alias mapping of the entire address range used by
    165  * the boot program.  That way, dvma_mapin can just compute the
    166  * DVMA alias address, and dvma_mapout does nothing.
    167  *
    168  * Note that this assumes that standalone programs will do I/O
    169  * operations only within range (SA_MIN_VA .. SA_MAX_VA) checked.
    170  */
    171 
    172 #define DVMA_BASE 0x00f00000
    173 #define DVMA_MAPLEN  0x38000	/* 256K - 32K (save MONSHORTSEG) */
    174 
    175 #define SA_MIN_VA	0x220000
    176 #define SA_MAX_VA	(SA_MIN_VA + DVMA_MAPLEN)
    177 
    178 /* This points to the end of the free DVMA space. */
    179 u_int dvma2_end = DVMA_BASE + DVMA_MAPLEN;
    180 
    181 void
    182 dvma2_init()
    183 {
    184 	int segva, dmava, sme;
    185 
    186 	segva = SA_MIN_VA;
    187 	dmava = DVMA_BASE;
    188 
    189 	while (segva < SA_MAX_VA) {
    190 		sme = get_segmap(segva);
    191 		set_segmap(dmava, sme);
    192 		segva += NBSG;
    193 		dmava += NBSG;
    194 	}
    195 }
    196 
    197 /* Convert a local address to a DVMA address. */
    198 char *
    199 dvma2_mapin(char *addr, int len)
    200 {
    201 	int va = (int)addr;
    202 
    203 	/* Make sure the address is in the DVMA map. */
    204 	if ((va < SA_MIN_VA) || (va >= SA_MAX_VA))
    205 		panic("dvma2_mapin: 0x%x outside 0x%x..0x%x\n",
    206 		    va, SA_MIN_VA, SA_MAX_VA);
    207 
    208 	va -= SA_MIN_VA;
    209 	va += DVMA_BASE;
    210 
    211 	return ((char *) va);
    212 }
    213 
    214 /* Destroy a DVMA address alias. */
    215 void
    216 dvma2_mapout(char *addr, int len)
    217 {
    218 	int va = (int)addr;
    219 
    220 	/* Make sure the address is in the DVMA map. */
    221 	if ((va < DVMA_BASE) || (va >= (DVMA_BASE + DVMA_MAPLEN)))
    222 		panic("dvma2_mapout");
    223 }
    224 
    225 char *
    226 dvma2_alloc(int len)
    227 {
    228 	len = m68k_round_page(len);
    229 	dvma2_end -= len;
    230 	return((char*)dvma2_end);
    231 }
    232 
    233 void
    234 dvma2_free(char *dvma, int len)
    235 {
    236 	/* not worth the trouble */
    237 }
    238 
    239 /*****************************************************************
    240  * Control space stuff...
    241  */
    242 
    243 u_int
    244 get_pte(va)
    245 	vaddr_t va;
    246 {
    247 	u_int pte;
    248 
    249 	pte = get_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va));
    250 	if (pte & PG_VALID) {
    251 		/*
    252 		 * This clears bit 30 (the kernel readable bit, which
    253 		 * should always be set), bit 28 (which should always
    254 		 * be set) and bit 26 (the user writable bit, which we
    255 		 * always have tracking the kernel writable bit).  In
    256 		 * the protection, this leaves bit 29 (the kernel
    257 		 * writable bit) and bit 27 (the user readable bit).
    258 		 * See pte2.h for more about this hack.
    259 		 */
    260 		pte &= ~(0x54000000);
    261 		/*
    262 		 * Flip bit 27 (the user readable bit) to become bit
    263 		 * 27 (the PG_SYSTEM bit).
    264 		 */
    265 		pte ^= (PG_SYSTEM);
    266 	}
    267 	return (pte);
    268 }
    269 
    270 void
    271 set_pte(va, pte)
    272 	vaddr_t va;
    273 	u_int pte;
    274 {
    275 	if (pte & PG_VALID) {
    276 		/* Clear bit 26 (the user writable bit).  */
    277 		pte &= (~0x04000000);
    278 		/*
    279 		 * Flip bit 27 (the PG_SYSTEM bit) to become bit 27
    280 		 * (the user readable bit).
    281 		 */
    282 		pte ^= (PG_SYSTEM);
    283 		/*
    284 		 * Always set bits 30 (the kernel readable bit) and
    285 		 * bit 28, and set bit 26 (the user writable bit) iff
    286 		 * bit 29 (the kernel writable bit) is set *and* bit
    287 		 * 27 (the user readable bit) is set.  This latter bit
    288 		 * of logic is expressed in the bizarre second term
    289 		 * below, chosen because it needs no branches.
    290 		 */
    291 #if (PG_WRITE >> 2) != PG_SYSTEM
    292 #error	"PG_WRITE and PG_SYSTEM definitions don't match!"
    293 #endif
    294 		pte |= 0x50000000
    295 		    | ((((pte & PG_WRITE) >> 2) & pte) >> 1);
    296 	}
    297 	set_control_word(CONTROL_ADDR_BUILD(PGMAP_BASE, va), pte);
    298 }
    299 
    300 int
    301 get_segmap(va)
    302 	vaddr_t va;
    303 {
    304 	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
    305 	return (get_control_byte(va));
    306 }
    307 
    308 void
    309 set_segmap(va, sme)
    310 	vaddr_t va;
    311 	int sme;
    312 {
    313 	va = CONTROL_ADDR_BUILD(SEGMAP_BASE, va);
    314 	set_control_byte(va, sme);
    315 }
    316 
    317 /*
    318  * Copy the IDPROM contents into the passed buffer.
    319  * The caller (idprom.c) will do the checksum.
    320  */
    321 void
    322 sun2_getidprom(u_char *dst)
    323 {
    324 	vaddr_t src;	/* control space address */
    325 	int len, x;
    326 
    327 	src = IDPROM_BASE;
    328 	len = sizeof(struct idprom);
    329 	do {
    330 		x = get_control_byte(src);
    331 		src += NBPG;
    332 		*dst++ = x;
    333 	} while (--len > 0);
    334 }
    335 
    336 /*****************************************************************
    337  * Init our function pointers, etc.
    338  */
    339 
    340 /*
    341  * For booting, the PROM in fredette's Sun 2/120 doesn't map
    342  * much main memory, and what is mapped is mapped strangely.
    343  * Low virtual memory is mapped like:
    344  *
    345  * 0x000000 - 0x0bffff virtual -> 0x000000 - 0x0bffff physical
    346  * 0x0c0000 - 0x0fffff virtual -> invalid
    347  * 0x100000 - 0x13ffff virtual -> 0x0c0000 - 0x0fffff physical
    348  * 0x200800 - 0x3fffff virtual -> 0x200800 - 0x3fffff physical
    349  *
    350  * I think the SunOS authors wanted to load kernels starting at
    351  * physical zero, and assumed that kernels would be less
    352  * than 768K (0x0c0000) long.  Also, the PROM maps physical
    353  * 0x0c0000 - 0x0fffff into DVMA space, so we can't take the
    354  * easy road and just add more mappings to use that physical
    355  * memory while loading (the PROM might do DMA there).
    356  *
    357  * What we do, then, is assume a 4MB machine (you'll really
    358  * need that to run NetBSD at all anyways), and we map two
    359  * chunks of physical and virtual space:
    360  *
    361  * 0x400000 - 0x4bffff virtual -> 0x000000 - 0x0bffff physical
    362  * 0x4c0000 - 0x600000 virtual -> 0x2c0000 - 0x3fffff physical
    363  *
    364  * And then we load starting at virtual 0x400000.  We will do
    365  * all of this mapping just by copying PMEGs.
    366  *
    367  * After the load is done, but before we enter the kernel, we're
    368  * done with the PROM, so we copy the part of the kernel that
    369  * got loaded at physical 0x2c0000 down to physical 0x0c0000.
    370  * This can't just be a PMEG copy; we've actually got to move
    371  * bytes in physical memory.
    372  *
    373  * These two chunks of physical and virtual space are defined
    374  * in macros below.  Some of the macros are only for completeness:
    375  */
    376 #define MEM_CHUNK0_SIZE			(0x0c0000)
    377 #define MEM_CHUNK0_LOAD_PHYS		(0x000000)
    378 #define MEM_CHUNK0_LOAD_VIRT		(0x400000)
    379 #define MEM_CHUNK0_LOAD_VIRT_PROM	MEM_CHUNK0_LOAD_PHYS
    380 #define MEM_CHUNK0_COPY_PHYS		MEM_CHUNK0_LOAD_PHYS
    381 #define MEM_CHUNK0_COPY_VIRT		MEM_CHUNK0_COPY_PHYS
    382 
    383 #define MEM_CHUNK1_SIZE			(0x140000)
    384 #define MEM_CHUNK1_LOAD_PHYS		(0x2c0000)
    385 #define MEM_CHUNK1_LOAD_VIRT		(MEM_CHUNK0_LOAD_VIRT + MEM_CHUNK0_SIZE)
    386 #define MEM_CHUNK1_LOAD_VIRT_PROM	MEM_CHUNK1_LOAD_PHYS
    387 #define MEM_CHUNK1_COPY_PHYS		(MEM_CHUNK0_LOAD_PHYS + MEM_CHUNK0_SIZE)
    388 #define MEM_CHUNK1_COPY_VIRT		MEM_CHUNK1_COPY_PHYS
    389 
    390 /* Maps memory for loading. */
    391 u_long
    392 sun2_map_mem_load()
    393 {
    394 	vaddr_t off;
    395 
    396 	/* Map chunk zero for loading. */
    397 	for(off = 0; off < MEM_CHUNK0_SIZE; off += NBSG)
    398 		set_segmap(MEM_CHUNK0_LOAD_VIRT + off,
    399 			   get_segmap(MEM_CHUNK0_LOAD_VIRT_PROM + off));
    400 
    401 	/* Map chunk one for loading. */
    402 	for(off = 0; off < MEM_CHUNK1_SIZE; off += NBSG)
    403 		set_segmap(MEM_CHUNK1_LOAD_VIRT + off,
    404 			   get_segmap(MEM_CHUNK1_LOAD_VIRT_PROM + off));
    405 
    406 	/* Tell our caller where in virtual space to load. */
    407 	return MEM_CHUNK0_LOAD_VIRT;
    408 }
    409 
    410 /* Remaps memory for running. */
    411 void *
    412 sun2_map_mem_run(entry)
    413 	void *entry;
    414 {
    415 	vaddr_t off, off_end;
    416 	int sme;
    417 	u_int pte;
    418 
    419 	/* Chunk zero is already mapped and copied. */
    420 
    421 	/* Chunk one needs to be mapped and copied. */
    422 	pte = (get_pte(0) & ~PG_FRAME);
    423 	for(off = 0; off < MEM_CHUNK1_SIZE; ) {
    424 
    425 		/*
    426 		 * We use the PMEG immediately before the
    427 		 * segment we're copying in the PROM virtual
    428 		 * mapping of the chunk.  If this is the first
    429 		 * segment, this is the PMEG the PROM used to
    430 		 * map 0x2b8000 virtual to 0x2b8000 physical,
    431 		 * which I'll assume is unused.  For the second
    432 		 * and subsequent segments, this will be the
    433 		 * PMEG used to map the previous segment, which
    434 		 * is now (since we already copied it) unused.
    435 		 */
    436 		sme = get_segmap((MEM_CHUNK1_LOAD_VIRT_PROM + off) - NBSG);
    437 		set_segmap(MEM_CHUNK1_COPY_VIRT + off, sme);
    438 
    439 		/* Set the PTEs in this new PMEG. */
    440 		for(off_end = off + NBSG; off < off_end; off += NBPG)
    441 			set_pte(MEM_CHUNK1_COPY_VIRT + off,
    442 				pte | PA_PGNUM(MEM_CHUNK1_COPY_PHYS + off));
    443 
    444 		/* Copy this segment. */
    445 		bcopy((caddr_t)(MEM_CHUNK1_LOAD_VIRT + (off - NBSG)),
    446 		      (caddr_t)(MEM_CHUNK1_COPY_VIRT + (off - NBSG)),
    447 		      NBSG);
    448 	}
    449 
    450 	/* Tell our caller where in virtual space to enter. */
    451 	return ((caddr_t)entry) - MEM_CHUNK0_LOAD_VIRT;
    452 }
    453 
    454 void
    455 sun2_init()
    456 {
    457 	/* Set the function pointers. */
    458 	dev_mapin_p   = dev2_mapin;
    459 	dvma_alloc_p  = dvma2_alloc;
    460 	dvma_free_p   = dvma2_free;
    461 	dvma_mapin_p  = dvma2_mapin;
    462 	dvma_mapout_p = dvma2_mapout;
    463 
    464 	/* Prepare DVMA segment. */
    465 	dvma2_init();
    466 }
    467