xref: /onnv-gate/usr/src/uts/sun4/os/startup.c (revision 5872)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51253Slq150181  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211253Slq150181 
220Sstevel@tonic-gate /*
23*5872Ssetje  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/machsystm.h>
300Sstevel@tonic-gate #include <sys/archsystm.h>
310Sstevel@tonic-gate #include <sys/vm.h>
320Sstevel@tonic-gate #include <sys/cpu.h>
330Sstevel@tonic-gate #include <sys/atomic.h>
340Sstevel@tonic-gate #include <sys/reboot.h>
350Sstevel@tonic-gate #include <sys/kdi.h>
360Sstevel@tonic-gate #include <sys/bootconf.h>
370Sstevel@tonic-gate #include <sys/memlist_plat.h>
380Sstevel@tonic-gate #include <sys/memlist_impl.h>
390Sstevel@tonic-gate #include <sys/prom_plat.h>
400Sstevel@tonic-gate #include <sys/prom_isa.h>
410Sstevel@tonic-gate #include <sys/autoconf.h>
420Sstevel@tonic-gate #include <sys/intreg.h>
430Sstevel@tonic-gate #include <sys/ivintr.h>
440Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
450Sstevel@tonic-gate #include <sys/iommutsb.h>
460Sstevel@tonic-gate #include <vm/vm_dep.h>
470Sstevel@tonic-gate #include <vm/seg_dev.h>
480Sstevel@tonic-gate #include <vm/seg_kmem.h>
490Sstevel@tonic-gate #include <vm/seg_kpm.h>
500Sstevel@tonic-gate #include <vm/seg_map.h>
510Sstevel@tonic-gate #include <vm/seg_kp.h>
520Sstevel@tonic-gate #include <sys/sysconf.h>
530Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
540Sstevel@tonic-gate #include <sys/kobj.h>
550Sstevel@tonic-gate #include <sys/sun4asi.h>
560Sstevel@tonic-gate #include <sys/clconf.h>
570Sstevel@tonic-gate #include <sys/platform_module.h>
580Sstevel@tonic-gate #include <sys/panic.h>
590Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
600Sstevel@tonic-gate #include <sys/clock.h>
610Sstevel@tonic-gate #include <sys/cmn_err.h>
620Sstevel@tonic-gate #include <sys/promif.h>
630Sstevel@tonic-gate #include <sys/prom_debug.h>
640Sstevel@tonic-gate #include <sys/traptrace.h>
650Sstevel@tonic-gate #include <sys/memnode.h>
660Sstevel@tonic-gate #include <sys/mem_cage.h>
672241Shuah #include <sys/mmu.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate extern void setup_trap_table(void);
704050Sjb145095 extern int cpu_intrq_setup(struct cpu *);
710Sstevel@tonic-gate extern void cpu_intrq_register(struct cpu *);
720Sstevel@tonic-gate extern void contig_mem_init(void);
734204Sha137994 extern caddr_t contig_mem_prealloc(caddr_t, pgcnt_t);
740Sstevel@tonic-gate extern void mach_dump_buffer_init(void);
750Sstevel@tonic-gate extern void mach_descrip_init(void);
761991Sheppo extern void mach_descrip_startup_fini(void);
770Sstevel@tonic-gate extern void mach_memscrub(void);
780Sstevel@tonic-gate extern void mach_fpras(void);
790Sstevel@tonic-gate extern void mach_cpu_halt_idle(void);
800Sstevel@tonic-gate extern void mach_hw_copy_limit(void);
811991Sheppo extern void load_mach_drivers(void);
820Sstevel@tonic-gate extern void load_tod_module(void);
830Sstevel@tonic-gate #pragma weak load_tod_module
840Sstevel@tonic-gate 
850Sstevel@tonic-gate extern int ndata_alloc_mmfsa(struct memlist *ndata);
860Sstevel@tonic-gate #pragma weak ndata_alloc_mmfsa
870Sstevel@tonic-gate 
881991Sheppo extern void cif_init(void);
891991Sheppo #pragma weak cif_init
901991Sheppo 
910Sstevel@tonic-gate extern void parse_idprom(void);
920Sstevel@tonic-gate extern void add_vx_handler(char *, int, void (*)(cell_t *));
930Sstevel@tonic-gate extern void mem_config_init(void);
940Sstevel@tonic-gate extern void memseg_remap_init(void);
950Sstevel@tonic-gate 
962296Sae112802 extern void mach_kpm_init(void);
974878Sblakej extern int size_pse_array(pgcnt_t, int);
982296Sae112802 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * External Data:
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate extern int vac_size;	/* cache size in bytes */
1030Sstevel@tonic-gate extern uint_t vac_mask;	/* VAC alignment consistency mask */
1040Sstevel@tonic-gate extern uint_t vac_colors;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Global Data Definitions:
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * XXX - Don't port this to new architectures
1120Sstevel@tonic-gate  * A 3rd party volume manager driver (vxdm) depends on the symbol romp.
1130Sstevel@tonic-gate  * 'romp' has no use with a prom with an IEEE 1275 client interface.
1140Sstevel@tonic-gate  * The driver doesn't use the value, but it depends on the symbol.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate void *romp;		/* veritas driver won't load without romp 4154976 */
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1210Sstevel@tonic-gate pgcnt_t segkpsize =
1220Sstevel@tonic-gate     btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
1230Sstevel@tonic-gate uint_t segmap_percent = 12; /* Size of segmap segment */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int use_cache = 1;		/* cache not reliable (605 bugs) with MP */
1260Sstevel@tonic-gate int vac_copyback = 1;
1270Sstevel@tonic-gate char *cache_mode = NULL;
1280Sstevel@tonic-gate int use_mix = 1;
1290Sstevel@tonic-gate int prom_debug = 0;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate caddr_t boot_tba;		/* %tba at boot - used by kmdb */
1320Sstevel@tonic-gate uint_t	tba_taken_over = 0;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate caddr_t s_text;			/* start of kernel text segment */
1350Sstevel@tonic-gate caddr_t e_text;			/* end of kernel text segment */
1360Sstevel@tonic-gate caddr_t s_data;			/* start of kernel data segment */
1370Sstevel@tonic-gate caddr_t e_data;			/* end of kernel data segment */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate caddr_t modtext;		/* beginning of module text */
1400Sstevel@tonic-gate size_t	modtext_sz;		/* size of module text */
1410Sstevel@tonic-gate caddr_t moddata;		/* beginning of module data reserve */
1420Sstevel@tonic-gate caddr_t e_moddata;		/* end of module data reserve */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * End of first block of contiguous kernel in 32-bit virtual address space
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate caddr_t		econtig32;	/* end of first blk of contiguous kernel */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate caddr_t		ncbase;		/* beginning of non-cached segment */
1500Sstevel@tonic-gate caddr_t		ncend;		/* end of non-cached segment */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate size_t	ndata_remain_sz;	/* bytes from end of data to 4MB boundary */
1530Sstevel@tonic-gate caddr_t	nalloc_base;		/* beginning of nucleus allocation */
1540Sstevel@tonic-gate caddr_t nalloc_end;		/* end of nucleus allocatable memory */
1550Sstevel@tonic-gate caddr_t valloc_base;		/* beginning of kvalloc segment	*/
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate caddr_t kmem64_base;		/* base of kernel mem segment in 64-bit space */
1580Sstevel@tonic-gate caddr_t kmem64_end;		/* end of kernel mem segment in 64-bit space */
1595648Ssetje size_t	kmem64_sz;		/* bytes in kernel mem segment, 64-bit space */
1603764Sdp78419 caddr_t kmem64_aligned_end;	/* end of large page, overmaps 64-bit space */
1613764Sdp78419 int	kmem64_szc;		/* page size code */
1623764Sdp78419 uint64_t kmem64_pabase = (uint64_t)-1;	/* physical address of kmem64_base */
1630Sstevel@tonic-gate 
1642296Sae112802 uintptr_t shm_alignment;	/* VAC address consistency modulus */
1650Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
1660Sstevel@tonic-gate struct memlist *phys_avail;	/* Available (unreserved) physical memory */
1670Sstevel@tonic-gate struct memlist *virt_avail;	/* Available (unmapped?) virtual memory */
1685648Ssetje struct memlist *nopp_list;	/* pages with no backing page structs */
1690Sstevel@tonic-gate struct memlist ndata;		/* memlist of nucleus allocatable memory */
1700Sstevel@tonic-gate int memexp_flag;		/* memory expansion card flag */
1710Sstevel@tonic-gate uint64_t ecache_flushaddr;	/* physical address used for flushing E$ */
1720Sstevel@tonic-gate pgcnt_t obp_pages;		/* Physical pages used by OBP */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * VM data structures
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
1780Sstevel@tonic-gate struct page *pp_base;		/* Base of system page struct array */
1790Sstevel@tonic-gate size_t pp_sz;			/* Size in bytes of page struct array */
1800Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
1814878Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
1824878Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
1834878Sblakej int pse_shift;			/* log2(pse_table_size) */
1840Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
1850Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
1860Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
1870Sstevel@tonic-gate struct seg ktexthole;		/* Segment used for nucleus text hole */
1880Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
1890Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
1900Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
1910Sstevel@tonic-gate 
1925648Ssetje void *kpm_pp_base;		/* Base of system kpm_page array */
1930Sstevel@tonic-gate size_t	kpm_pp_sz;		/* Size of system kpm_page array */
1940Sstevel@tonic-gate pgcnt_t	kpm_npages;		/* How many kpm pages are managed */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
1970Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
1980Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
1990Sstevel@tonic-gate 
2003290Sjohansen int segzio_fromheap = 0;	/* zio allocations occur from heap */
2013290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
2023290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
2033290Sjohansen 
2040Sstevel@tonic-gate /*
2050Sstevel@tonic-gate  * debugger pages (if allocated)
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate struct vnode kdebugvp;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2103446Smrj  * VA range available to the debugger
2113446Smrj  */
2123446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
2133446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
2143446Smrj 
2153446Smrj /*
2160Sstevel@tonic-gate  * Segment for relocated kernel structures in 64-bit large RAM kernels
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate struct seg kmem64;
2190Sstevel@tonic-gate 
2205648Ssetje struct memseg *memseg_free;
2215648Ssetje 
2220Sstevel@tonic-gate struct vnode unused_pages_vp;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * VM data structures allocated early during boot.
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate size_t pagehash_sz;
2280Sstevel@tonic-gate uint64_t memlist_sz;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate char tbr_wr_addr_inited = 0;
2310Sstevel@tonic-gate 
2325358Sjc25722 caddr_t	mpo_heap32_buf = NULL;
2335358Sjc25722 size_t	mpo_heap32_bufsz = 0;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /*
2360Sstevel@tonic-gate  * Static Routines:
2370Sstevel@tonic-gate  */
2385648Ssetje static int ndata_alloc_memseg(struct memlist *, size_t);
2395648Ssetje static void memlist_new(uint64_t, uint64_t, struct memlist **);
2405648Ssetje static void memlist_add(uint64_t, uint64_t,
2415648Ssetje 	struct memlist **, struct memlist **);
2425648Ssetje static void kphysm_init(void);
2430Sstevel@tonic-gate static void kvm_init(void);
2445648Ssetje static void install_kmem64_tte(void);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate static void startup_init(void);
2470Sstevel@tonic-gate static void startup_memlist(void);
2480Sstevel@tonic-gate static void startup_modules(void);
2490Sstevel@tonic-gate static void startup_bop_gone(void);
2500Sstevel@tonic-gate static void startup_vm(void);
2510Sstevel@tonic-gate static void startup_end(void);
2520Sstevel@tonic-gate static void setup_cage_params(void);
2531253Slq150181 static void startup_create_io_node(void);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static pgcnt_t npages;
2560Sstevel@tonic-gate static struct memlist *memlist;
2570Sstevel@tonic-gate void *memlist_end;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate static pgcnt_t bop_alloc_pages;
2600Sstevel@tonic-gate static caddr_t hblk_base;
2610Sstevel@tonic-gate uint_t hblk_alloc_dynamic = 0;
2620Sstevel@tonic-gate uint_t hblk1_min = H1MIN;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * Hooks for unsupported platforms and down-rev firmware
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate int iam_positron(void);
2690Sstevel@tonic-gate #pragma weak iam_positron
2700Sstevel@tonic-gate static void do_prom_version_check(void);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * After receiving a thermal interrupt, this is the number of seconds
2740Sstevel@tonic-gate  * to delay before shutting off the system, assuming
2750Sstevel@tonic-gate  * shutdown fails.  Use /etc/system to change the delay if this isn't
2760Sstevel@tonic-gate  * large enough.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate int thermal_powerdown_delay = 1200;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Used to hold off page relocations into the cage until OBP has completed
2820Sstevel@tonic-gate  * its boot-time handoff of its resources to the kernel.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate int page_relocate_ready = 0;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate #ifdef  DEBUGGING_MEM
2900Sstevel@tonic-gate static int debugging_mem;
2910Sstevel@tonic-gate static void
2920Sstevel@tonic-gate printmemlist(char *title, struct memlist *list)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	if (!debugging_mem)
2950Sstevel@tonic-gate 		return;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	printf("%s\n", title);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	while (list) {
3000Sstevel@tonic-gate 		prom_printf("\taddr = 0x%x %8x, size = 0x%x %8x\n",
3010Sstevel@tonic-gate 		    (uint32_t)(list->address >> 32), (uint32_t)list->address,
3020Sstevel@tonic-gate 		    (uint32_t)(list->size >> 32), (uint32_t)(list->size));
3030Sstevel@tonic-gate 		list = list->next;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate void
3080Sstevel@tonic-gate printmemseg(struct memseg *memseg)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	if (!debugging_mem)
3110Sstevel@tonic-gate 		return;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	printf("memseg\n");
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	while (memseg) {
3160Sstevel@tonic-gate 		prom_printf("\tpage = 0x%p, epage = 0x%p, "
3170Sstevel@tonic-gate 		    "pfn = 0x%x, epfn = 0x%x\n",
3180Sstevel@tonic-gate 		    memseg->pages, memseg->epages,
3190Sstevel@tonic-gate 		    memseg->pages_base, memseg->pages_end);
3200Sstevel@tonic-gate 		memseg = memseg->next;
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate #define	debug_pause(str)	halt((str))
3250Sstevel@tonic-gate #define	MPRINTF(str)		if (debugging_mem) prom_printf((str))
3260Sstevel@tonic-gate #define	MPRINTF1(str, a)	if (debugging_mem) prom_printf((str), (a))
3270Sstevel@tonic-gate #define	MPRINTF2(str, a, b)	if (debugging_mem) prom_printf((str), (a), (b))
3280Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c) \
3290Sstevel@tonic-gate 	if (debugging_mem) prom_printf((str), (a), (b), (c))
3300Sstevel@tonic-gate #else	/* DEBUGGING_MEM */
3310Sstevel@tonic-gate #define	MPRINTF(str)
3320Sstevel@tonic-gate #define	MPRINTF1(str, a)
3330Sstevel@tonic-gate #define	MPRINTF2(str, a, b)
3340Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c)
3350Sstevel@tonic-gate #endif	/* DEBUGGING_MEM */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  *
3400Sstevel@tonic-gate  *                    Kernel's Virtual Memory Layout.
3410Sstevel@tonic-gate  *                       /-----------------------\
3420Sstevel@tonic-gate  * 0xFFFFFFFF.FFFFFFFF  -|                       |-
3430Sstevel@tonic-gate  *                       |   OBP's virtual page  |
3440Sstevel@tonic-gate  *                       |        tables         |
3450Sstevel@tonic-gate  * 0xFFFFFFFC.00000000  -|-----------------------|-
3460Sstevel@tonic-gate  *                       :                       :
3470Sstevel@tonic-gate  *                       :                       :
3483290Sjohansen  *                      -|-----------------------|-
3493290Sjohansen  *                       |       segzio          | (base and size vary)
3500Sstevel@tonic-gate  * 0xFFFFFE00.00000000  -|-----------------------|-
3510Sstevel@tonic-gate  *                       |                       |  Ultrasparc I/II support
3520Sstevel@tonic-gate  *                       |    segkpm segment     |  up to 2TB of physical
3530Sstevel@tonic-gate  *                       | (64-bit kernel ONLY)  |  memory, VAC has 2 colors
3540Sstevel@tonic-gate  *                       |                       |
3550Sstevel@tonic-gate  * 0xFFFFFA00.00000000  -|-----------------------|- 2TB segkpm alignment
3560Sstevel@tonic-gate  *                       :                       :
3570Sstevel@tonic-gate  *                       :                       :
3580Sstevel@tonic-gate  * 0xFFFFF810.00000000  -|-----------------------|- hole_end
3590Sstevel@tonic-gate  *                       |                       |      ^
3600Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
3610Sstevel@tonic-gate  *                       | bug requires an extra |      |
3620Sstevel@tonic-gate  *                       | 4 GB of space between |      |
3630Sstevel@tonic-gate  *                       |   hole and used RAM   |	|
3640Sstevel@tonic-gate  *                       |                       |      |
3650Sstevel@tonic-gate  * 0xFFFFF800.00000000  -|-----------------------|-     |
3660Sstevel@tonic-gate  *                       |                       |      |
3670Sstevel@tonic-gate  *                       | Virtual Address Hole  |   UltraSPARC
3680Sstevel@tonic-gate  *                       |  on UltraSPARC I/II   |  I/II * ONLY *
3690Sstevel@tonic-gate  *                       |                       |      |
3700Sstevel@tonic-gate  * 0x00000800.00000000  -|-----------------------|-     |
3710Sstevel@tonic-gate  *                       |                       |      |
3720Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
3730Sstevel@tonic-gate  *                       | bug requires an extra |      |
3740Sstevel@tonic-gate  *                       | 4 GB of space between |      |
3750Sstevel@tonic-gate  *                       |   hole and used RAM   |      |
3760Sstevel@tonic-gate  *                       |                       |      v
3770Sstevel@tonic-gate  * 0x000007FF.00000000  -|-----------------------|- hole_start -----
3780Sstevel@tonic-gate  *                       :                       :		   ^
3790Sstevel@tonic-gate  *                       :                       :		   |
3805648Ssetje  *                       |-----------------------|                 |
3815648Ssetje  *                       |                       |                 |
3825648Ssetje  *                       |  ecache flush area    |                 |
3835648Ssetje  *                       |  (twice largest e$)   |                 |
3845648Ssetje  *                       |                       |                 |
3853764Sdp78419  * 0x00000XXX.XXX00000  -|-----------------------|- kmem64_	   |
3863764Sdp78419  *                       | overmapped area       |   alignend_end  |
3873764Sdp78419  *                       | (kmem64_alignsize     |		   |
3883764Sdp78419  *                       |  boundary)            |		   |
3890Sstevel@tonic-gate  * 0x00000XXX.XXXXXXXX  -|-----------------------|- kmem64_end	   |
3900Sstevel@tonic-gate  *                       |                       |		   |
3910Sstevel@tonic-gate  *                       |   64-bit kernel ONLY  |		   |
3920Sstevel@tonic-gate  *                       |                       |		   |
3930Sstevel@tonic-gate  *                       |    kmem64 segment     |		   |
3940Sstevel@tonic-gate  *                       |                       |		   |
3950Sstevel@tonic-gate  *                       | (Relocated extra HME  |	     Approximately
3960Sstevel@tonic-gate  *                       |   block allocations,  |	    1 TB of virtual
3970Sstevel@tonic-gate  *                       |   memnode freelists,  |	     address space
3980Sstevel@tonic-gate  *                       |    HME hash buckets,  |		   |
3990Sstevel@tonic-gate  *                       | mml_table, kpmp_table,|		   |
4000Sstevel@tonic-gate  *                       |  page_t array and     |		   |
4010Sstevel@tonic-gate  *                       |  hashblock pool to    |		   |
4020Sstevel@tonic-gate  *                       |   avoid hard-coded    |		   |
4030Sstevel@tonic-gate  *                       |     32-bit vaddr      |		   |
4040Sstevel@tonic-gate  *                       |     limitations)      |		   |
4050Sstevel@tonic-gate  *                       |                       |		   v
4060Sstevel@tonic-gate  * 0x00000700.00000000  -|-----------------------|- SYSLIMIT (kmem64_base)
4070Sstevel@tonic-gate  *                       |                       |
4080Sstevel@tonic-gate  *                       |  segkmem segment      | (SYSLIMIT - SYSBASE = 4TB)
4090Sstevel@tonic-gate  *                       |                       |
4100Sstevel@tonic-gate  * 0x00000300.00000000  -|-----------------------|- SYSBASE
4110Sstevel@tonic-gate  *                       :                       :
4120Sstevel@tonic-gate  *                       :                       :
4130Sstevel@tonic-gate  *                      -|-----------------------|-
4140Sstevel@tonic-gate  *                       |                       |
4150Sstevel@tonic-gate  *                       |  segmap segment       |   SEGMAPSIZE (1/8th physmem,
4160Sstevel@tonic-gate  *                       |                       |               256G MAX)
4170Sstevel@tonic-gate  * 0x000002a7.50000000  -|-----------------------|- SEGMAPBASE
4180Sstevel@tonic-gate  *                       :                       :
4190Sstevel@tonic-gate  *                       :                       :
4200Sstevel@tonic-gate  *                      -|-----------------------|-
4210Sstevel@tonic-gate  *                       |                       |
4220Sstevel@tonic-gate  *                       |       segkp           |    SEGKPSIZE (2GB)
4230Sstevel@tonic-gate  *                       |                       |
4240Sstevel@tonic-gate  *                       |                       |
4250Sstevel@tonic-gate  * 0x000002a1.00000000  -|-----------------------|- SEGKPBASE
4260Sstevel@tonic-gate  *                       |                       |
4270Sstevel@tonic-gate  * 0x000002a0.00000000  -|-----------------------|- MEMSCRUBBASE
4280Sstevel@tonic-gate  *                       |                       |       (SEGKPBASE - 0x400000)
4290Sstevel@tonic-gate  * 0x0000029F.FFE00000  -|-----------------------|- ARGSBASE
4300Sstevel@tonic-gate  *                       |                       |       (MEMSCRUBBASE - NCARGS)
4310Sstevel@tonic-gate  * 0x0000029F.FFD80000  -|-----------------------|- PPMAPBASE
4320Sstevel@tonic-gate  *                       |                       |       (ARGSBASE - PPMAPSIZE)
4330Sstevel@tonic-gate  * 0x0000029F.FFD00000  -|-----------------------|- PPMAP_FAST_BASE
4340Sstevel@tonic-gate  *                       |                       |
4350Sstevel@tonic-gate  * 0x0000029F.FF980000  -|-----------------------|- PIOMAPBASE
4360Sstevel@tonic-gate  *                       |                       |
4370Sstevel@tonic-gate  * 0x0000029F.FF580000  -|-----------------------|- NARG_BASE
4380Sstevel@tonic-gate  *                       :                       :
4390Sstevel@tonic-gate  *                       :                       :
4400Sstevel@tonic-gate  * 0x00000000.FFFFFFFF  -|-----------------------|- OFW_END_ADDR
4410Sstevel@tonic-gate  *                       |                       |
4420Sstevel@tonic-gate  *                       |         OBP           |
4430Sstevel@tonic-gate  *                       |                       |
4440Sstevel@tonic-gate  * 0x00000000.F0000000  -|-----------------------|- OFW_START_ADDR
4450Sstevel@tonic-gate  *                       |         kmdb          |
4460Sstevel@tonic-gate  * 0x00000000.EDD00000  -|-----------------------|- SEGDEBUGBASE
4470Sstevel@tonic-gate  *                       :                       :
4480Sstevel@tonic-gate  *                       :                       :
4490Sstevel@tonic-gate  * 0x00000000.7c000000  -|-----------------------|- SYSLIMIT32
4500Sstevel@tonic-gate  *                       |                       |
4510Sstevel@tonic-gate  *                       |  segkmem32 segment    | (SYSLIMIT32 - SYSBASE32 =
4520Sstevel@tonic-gate  *                       |                       |    ~64MB)
4535648Ssetje  * 0x00000000.70002000  -|-----------------------|
4540Sstevel@tonic-gate  *                       |     panicbuf          |
4555648Ssetje  * 0x00000000.70000000  -|-----------------------|- SYSBASE32
4565648Ssetje  *                       |       boot-time       |
4575648Ssetje  *                       |    temporary space    |
4585648Ssetje  * 0x00000000.4C000000  -|-----------------------|- BOOTTMPBASE
4590Sstevel@tonic-gate  *                       :                       :
4600Sstevel@tonic-gate  *                       :                       :
4610Sstevel@tonic-gate  *                       |                       |
4620Sstevel@tonic-gate  *                       |-----------------------|- econtig32
4630Sstevel@tonic-gate  *                       |    vm structures      |
4640Sstevel@tonic-gate  * 0x00000000.01C00000   |-----------------------|- nalloc_end
4650Sstevel@tonic-gate  *                       |         TSBs          |
4660Sstevel@tonic-gate  *                       |-----------------------|- end/nalloc_base
4670Sstevel@tonic-gate  *                       |   kernel data & bss   |
4680Sstevel@tonic-gate  * 0x00000000.01800000  -|-----------------------|
4690Sstevel@tonic-gate  *                       :   nucleus text hole   :
4700Sstevel@tonic-gate  * 0x00000000.01400000  -|-----------------------|
4710Sstevel@tonic-gate  *                       :                       :
4720Sstevel@tonic-gate  *                       |-----------------------|
4730Sstevel@tonic-gate  *                       |      module text      |
4740Sstevel@tonic-gate  *                       |-----------------------|- e_text/modtext
4750Sstevel@tonic-gate  *                       |      kernel text      |
4760Sstevel@tonic-gate  *                       |-----------------------|
4770Sstevel@tonic-gate  *                       |    trap table (48k)   |
4780Sstevel@tonic-gate  * 0x00000000.01000000  -|-----------------------|- KERNELBASE
4790Sstevel@tonic-gate  *                       | reserved for trapstat |} TSTAT_TOTAL_SIZE
4800Sstevel@tonic-gate  *                       |-----------------------|
4810Sstevel@tonic-gate  *                       |                       |
4820Sstevel@tonic-gate  *                       |        invalid        |
4830Sstevel@tonic-gate  *                       |                       |
4840Sstevel@tonic-gate  * 0x00000000.00000000  _|_______________________|
4850Sstevel@tonic-gate  *
4860Sstevel@tonic-gate  *
4870Sstevel@tonic-gate  *
4880Sstevel@tonic-gate  *                   32-bit User Virtual Memory Layout.
4890Sstevel@tonic-gate  *                       /-----------------------\
4900Sstevel@tonic-gate  *                       |                       |
4910Sstevel@tonic-gate  *                       |        invalid        |
4920Sstevel@tonic-gate  *                       |                       |
4930Sstevel@tonic-gate  *          0xFFC00000  -|-----------------------|- USERLIMIT
4940Sstevel@tonic-gate  *                       |       user stack      |
4950Sstevel@tonic-gate  *                       :                       :
4960Sstevel@tonic-gate  *                       :                       :
4970Sstevel@tonic-gate  *                       :                       :
4980Sstevel@tonic-gate  *                       |       user data       |
4990Sstevel@tonic-gate  *                      -|-----------------------|-
5000Sstevel@tonic-gate  *                       |       user text       |
5010Sstevel@tonic-gate  *          0x00002000  -|-----------------------|-
5020Sstevel@tonic-gate  *                       |       invalid         |
5030Sstevel@tonic-gate  *          0x00000000  _|_______________________|
5040Sstevel@tonic-gate  *
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  *
5070Sstevel@tonic-gate  *                   64-bit User Virtual Memory Layout.
5080Sstevel@tonic-gate  *                       /-----------------------\
5090Sstevel@tonic-gate  *                       |                       |
5100Sstevel@tonic-gate  *                       |        invalid        |
5110Sstevel@tonic-gate  *                       |                       |
5120Sstevel@tonic-gate  *  0xFFFFFFFF.80000000 -|-----------------------|- USERLIMIT
5130Sstevel@tonic-gate  *                       |       user stack      |
5140Sstevel@tonic-gate  *                       :                       :
5150Sstevel@tonic-gate  *                       :                       :
5160Sstevel@tonic-gate  *                       :                       :
5170Sstevel@tonic-gate  *                       |       user data       |
5180Sstevel@tonic-gate  *                      -|-----------------------|-
5190Sstevel@tonic-gate  *                       |       user text       |
5205648Ssetje  *  0x00000000.01000000 -|-----------------------|-
5210Sstevel@tonic-gate  *                       |       invalid         |
5220Sstevel@tonic-gate  *  0x00000000.00000000 _|_______________________|
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate extern caddr_t ecache_init_scrub_flush_area(caddr_t alloc_base);
5260Sstevel@tonic-gate extern uint64_t ecache_flush_address(void);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate #pragma weak load_platform_modules
5291772Sjl139090 #pragma weak plat_startup_memlist
5300Sstevel@tonic-gate #pragma weak ecache_init_scrub_flush_area
5310Sstevel@tonic-gate #pragma weak ecache_flush_address
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*
5350Sstevel@tonic-gate  * By default the DR Cage is enabled for maximum OS
5360Sstevel@tonic-gate  * MPSS performance.  Users needing to disable the cage mechanism
5370Sstevel@tonic-gate  * can set this variable to zero via /etc/system.
5380Sstevel@tonic-gate  * Disabling the cage on systems supporting Dynamic Reconfiguration (DR)
5390Sstevel@tonic-gate  * will result in loss of DR functionality.
5400Sstevel@tonic-gate  * Platforms wishing to disable kernel Cage by default
5410Sstevel@tonic-gate  * should do so in their set_platform_defaults() routine.
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate int	kernel_cage_enable = 1;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate static void
5460Sstevel@tonic-gate setup_cage_params(void)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	void (*func)(void);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	func = (void (*)(void))kobj_getsymvalue("set_platform_cage_params", 0);
5510Sstevel@tonic-gate 	if (func != NULL) {
5520Sstevel@tonic-gate 		(*func)();
5530Sstevel@tonic-gate 		return;
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if (kernel_cage_enable == 0) {
5570Sstevel@tonic-gate 		return;
5580Sstevel@tonic-gate 	}
5594266Sdp78419 	kcage_range_init(phys_avail, KCAGE_DOWN, total_pages / 256);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (kcage_on) {
5620Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is ENABLED");
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is DISABLED");
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate /*
5700Sstevel@tonic-gate  * Machine-dependent startup code
5710Sstevel@tonic-gate  */
5720Sstevel@tonic-gate void
5730Sstevel@tonic-gate startup(void)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	startup_init();
5760Sstevel@tonic-gate 	if (&startup_platform)
5770Sstevel@tonic-gate 		startup_platform();
5780Sstevel@tonic-gate 	startup_memlist();
5790Sstevel@tonic-gate 	startup_modules();
5800Sstevel@tonic-gate 	setup_cage_params();
5810Sstevel@tonic-gate 	startup_bop_gone();
5820Sstevel@tonic-gate 	startup_vm();
5830Sstevel@tonic-gate 	startup_end();
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate struct regs sync_reg_buf;
5870Sstevel@tonic-gate uint64_t sync_tt;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate void
5900Sstevel@tonic-gate sync_handler(void)
5910Sstevel@tonic-gate {
5925084Sjohnlev 	struct  panic_trap_info 	ti;
5930Sstevel@tonic-gate 	int i;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/*
5960Sstevel@tonic-gate 	 * Prevent trying to talk to the other CPUs since they are
5970Sstevel@tonic-gate 	 * sitting in the prom and won't reply.
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
6000Sstevel@tonic-gate 		if ((i != CPU->cpu_id) && CPU_XCALL_READY(i)) {
6010Sstevel@tonic-gate 			cpu[i]->cpu_flags &= ~CPU_READY;
6020Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
6030Sstevel@tonic-gate 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * We've managed to get here without going through the
6090Sstevel@tonic-gate 	 * normal panic code path. Try and save some useful
6100Sstevel@tonic-gate 	 * information.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if (!panicstr && (curthread->t_panic_trap == NULL)) {
6130Sstevel@tonic-gate 		ti.trap_type = sync_tt;
6140Sstevel@tonic-gate 		ti.trap_regs = &sync_reg_buf;
6150Sstevel@tonic-gate 		ti.trap_addr = NULL;
6160Sstevel@tonic-gate 		ti.trap_mmu_fsr = 0x0;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		curthread->t_panic_trap = &ti;
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/*
6220Sstevel@tonic-gate 	 * If we're re-entering the panic path, update the signature
6230Sstevel@tonic-gate 	 * block so that the SC knows we're in the second part of panic.
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 	if (panicstr)
6260Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	nopanicdebug = 1; /* do not perform debug_enter() prior to dump */
6290Sstevel@tonic-gate 	panic("sync initiated");
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate static void
6340Sstevel@tonic-gate startup_init(void)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * We want to save the registers while we're still in OBP
6380Sstevel@tonic-gate 	 * so that we know they haven't been fiddled with since.
6390Sstevel@tonic-gate 	 * (In principle, OBP can't change them just because it
6400Sstevel@tonic-gate 	 * makes a callback, but we'd rather not depend on that
6410Sstevel@tonic-gate 	 * behavior.)
6420Sstevel@tonic-gate 	 */
6430Sstevel@tonic-gate 	char		sync_str[] =
6444528Spaulsan 	    "warning @ warning off : sync "
6454528Spaulsan 	    "%%tl-c %%tstate h# %p x! "
6464528Spaulsan 	    "%%g1 h# %p x! %%g2 h# %p x! %%g3 h# %p x! "
6474528Spaulsan 	    "%%g4 h# %p x! %%g5 h# %p x! %%g6 h# %p x! "
6484528Spaulsan 	    "%%g7 h# %p x! %%o0 h# %p x! %%o1 h# %p x! "
6494528Spaulsan 	    "%%o2 h# %p x! %%o3 h# %p x! %%o4 h# %p x! "
6504528Spaulsan 	    "%%o5 h# %p x! %%o6 h# %p x! %%o7 h# %p x! "
6514528Spaulsan 	    "%%tl-c %%tpc h# %p x! %%tl-c %%tnpc h# %p x! "
6524528Spaulsan 	    "%%y h# %p l! %%tl-c %%tt h# %p x! "
6534528Spaulsan 	    "sync ; warning !";
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * 20 == num of %p substrings
6570Sstevel@tonic-gate 	 * 16 == max num of chars %p will expand to.
6580Sstevel@tonic-gate 	 */
6590Sstevel@tonic-gate 	char 		bp[sizeof (sync_str) + 16 * 20];
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/*
6620Sstevel@tonic-gate 	 * Initialize ptl1 stack for the 1st CPU.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	ptl1_init_cpu(&cpu0);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/*
6670Sstevel@tonic-gate 	 * Initialize the address map for cache consistent mappings
6680Sstevel@tonic-gate 	 * to random pages; must be done after vac_size is set.
6690Sstevel@tonic-gate 	 */
6700Sstevel@tonic-gate 	ppmapinit();
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	/*
6730Sstevel@tonic-gate 	 * Initialize the PROM callback handler.
6740Sstevel@tonic-gate 	 */
6750Sstevel@tonic-gate 	init_vx_handler();
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * have prom call sync_callback() to handle the sync and
6790Sstevel@tonic-gate 	 * save some useful information which will be stored in the
6800Sstevel@tonic-gate 	 * core file later.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	(void) sprintf((char *)bp, sync_str,
6834528Spaulsan 	    (void *)&sync_reg_buf.r_tstate, (void *)&sync_reg_buf.r_g1,
6844528Spaulsan 	    (void *)&sync_reg_buf.r_g2, (void *)&sync_reg_buf.r_g3,
6854528Spaulsan 	    (void *)&sync_reg_buf.r_g4, (void *)&sync_reg_buf.r_g5,
6864528Spaulsan 	    (void *)&sync_reg_buf.r_g6, (void *)&sync_reg_buf.r_g7,
6874528Spaulsan 	    (void *)&sync_reg_buf.r_o0, (void *)&sync_reg_buf.r_o1,
6884528Spaulsan 	    (void *)&sync_reg_buf.r_o2, (void *)&sync_reg_buf.r_o3,
6894528Spaulsan 	    (void *)&sync_reg_buf.r_o4, (void *)&sync_reg_buf.r_o5,
6904528Spaulsan 	    (void *)&sync_reg_buf.r_o6, (void *)&sync_reg_buf.r_o7,
6914528Spaulsan 	    (void *)&sync_reg_buf.r_pc, (void *)&sync_reg_buf.r_npc,
6924528Spaulsan 	    (void *)&sync_reg_buf.r_y, (void *)&sync_tt);
6930Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
6940Sstevel@tonic-gate 	add_vx_handler("sync", 1, (void (*)(cell_t *))sync_handler);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6975648Ssetje 
6985648Ssetje size_t
6995648Ssetje calc_pp_sz(pgcnt_t npages)
7005648Ssetje {
7015648Ssetje 
7025648Ssetje 	return (npages * sizeof (struct page));
7035648Ssetje }
7045648Ssetje 
7055648Ssetje size_t
7065648Ssetje calc_kpmpp_sz(pgcnt_t npages)
7075648Ssetje {
7085648Ssetje 
7095648Ssetje 	kpm_pgshft = (kpm_smallpages == 0) ? MMU_PAGESHIFT4M : MMU_PAGESHIFT;
7105648Ssetje 	kpm_pgsz = 1ull << kpm_pgshft;
7115648Ssetje 	kpm_pgoff = kpm_pgsz - 1;
7125648Ssetje 	kpmp2pshft = kpm_pgshft - PAGESHIFT;
7135648Ssetje 	kpmpnpgs = 1 << kpmp2pshft;
7145648Ssetje 
7155648Ssetje 	if (kpm_smallpages == 0) {
7165648Ssetje 		/*
7175648Ssetje 		 * Avoid fragmentation problems in kphysm_init()
7185648Ssetje 		 * by allocating for all of physical memory
7195648Ssetje 		 */
7205648Ssetje 		kpm_npages = ptokpmpr(physinstalled);
7215648Ssetje 		return (kpm_npages * sizeof (kpm_page_t));
7225648Ssetje 	} else {
7235648Ssetje 		kpm_npages = npages;
7245648Ssetje 		return (kpm_npages * sizeof (kpm_spage_t));
7255648Ssetje 	}
7265648Ssetje }
7275648Ssetje 
7285648Ssetje size_t
7295648Ssetje calc_pagehash_sz(pgcnt_t npages)
7305648Ssetje {
7315648Ssetje 
7325648Ssetje 	/*
7335648Ssetje 	 * The page structure hash table size is a power of 2
7345648Ssetje 	 * such that the average hash chain length is PAGE_HASHAVELEN.
7355648Ssetje 	 */
7365648Ssetje 	page_hashsz = npages / PAGE_HASHAVELEN;
7375648Ssetje 	page_hashsz = 1 << highbit(page_hashsz);
7385648Ssetje 	return (page_hashsz * sizeof (struct page *));
7395648Ssetje }
7405648Ssetje 
7415648Ssetje void
7425648Ssetje alloc_kmem64(caddr_t base, caddr_t end)
7435648Ssetje {
7445648Ssetje 	int i;
7455648Ssetje 	caddr_t aligned_end = NULL;
7465648Ssetje 
7475648Ssetje 	/*
7485648Ssetje 	 * Make one large memory alloc after figuring out the 64-bit size. This
7495648Ssetje 	 * will enable use of the largest page size appropriate for the system
7505648Ssetje 	 * architecture.
7515648Ssetje 	 */
7525648Ssetje 	ASSERT(mmu_exported_pagesize_mask & (1 << TTE8K));
7535648Ssetje 	ASSERT(IS_P2ALIGNED(base, TTEBYTES(max_bootlp_tteszc)));
7545648Ssetje 	for (i = max_bootlp_tteszc; i >= TTE8K; i--) {
7555648Ssetje 		size_t alloc_size, alignsize;
7565648Ssetje #if !defined(C_OBP)
7575648Ssetje 		unsigned long long pa;
7585648Ssetje #endif	/* !C_OBP */
7595648Ssetje 
7605648Ssetje 		if ((mmu_exported_pagesize_mask & (1 << i)) == 0)
7615648Ssetje 			continue;
7625648Ssetje 		alignsize = TTEBYTES(i);
7635648Ssetje 		kmem64_szc = i;
7645648Ssetje 
7655648Ssetje 		/* limit page size for small memory */
7665648Ssetje 		if (mmu_btop(alignsize) > (npages >> 2))
7675648Ssetje 			continue;
7685648Ssetje 
7695648Ssetje 		aligned_end = (caddr_t)roundup((uintptr_t)end, alignsize);
7705648Ssetje 		alloc_size = aligned_end - base;
7715648Ssetje #if !defined(C_OBP)
7725648Ssetje 		if (prom_allocate_phys(alloc_size, alignsize, &pa) == 0) {
7735648Ssetje 			if (prom_claim_virt(alloc_size, base) != (caddr_t)-1) {
7745648Ssetje 				kmem64_pabase = pa;
7755648Ssetje 				kmem64_aligned_end = aligned_end;
7765648Ssetje 				install_kmem64_tte();
7775648Ssetje 				break;
7785648Ssetje 			} else {
7795648Ssetje 				prom_free_phys(alloc_size, pa);
7805648Ssetje 			}
7815648Ssetje 		}
7825648Ssetje #else	/* !C_OBP */
7835648Ssetje 		if (prom_alloc(base, alloc_size, alignsize) == base) {
7845648Ssetje 			kmem64_pabase = va_to_pa(kmem64_base);
7855648Ssetje 			kmem64_aligned_end = aligned_end;
7865648Ssetje 			break;
7875648Ssetje 		}
7885648Ssetje #endif	/* !C_OBP */
7895648Ssetje 		if (i == TTE8K) {
7905648Ssetje 			prom_panic("kmem64 allocation failure");
7915648Ssetje 		}
7925648Ssetje 	}
7935648Ssetje 	ASSERT(aligned_end != NULL);
7945648Ssetje }
7955648Ssetje 
7965648Ssetje static prom_memlist_t *boot_physinstalled, *boot_physavail, *boot_virtavail;
7970Sstevel@tonic-gate static size_t boot_physinstalled_len, boot_physavail_len, boot_virtavail_len;
7980Sstevel@tonic-gate 
7995648Ssetje #define	IVSIZE	roundup(((MAXIVNUM * sizeof (intr_vec_t *)) + \
8005648Ssetje 			(MAX_RSVD_IV * sizeof (intr_vec_t)) + \
8015648Ssetje 			(MAX_RSVD_IVX * sizeof (intr_vecx_t))), PAGESIZE)
8020Sstevel@tonic-gate 
8033764Sdp78419 #if !defined(C_OBP)
8043764Sdp78419 /*
8053764Sdp78419  * Install a temporary tte handler in OBP for kmem64 area.
8063764Sdp78419  *
8073764Sdp78419  * We map kmem64 area with large pages before the trap table is taken
8083764Sdp78419  * over. Since OBP makes 8K mappings, it can create 8K tlb entries in
8093764Sdp78419  * the same area. Duplicate tlb entries with different page sizes
8103764Sdp78419  * cause unpredicatble behavior.  To avoid this, we don't create
8113764Sdp78419  * kmem64 mappings via BOP_ALLOC (ends up as prom_alloc() call to
8123764Sdp78419  * OBP).  Instead, we manage translations with a temporary va>tte-data
8133764Sdp78419  * handler (kmem64-tte).  This handler is replaced by unix-tte when
8143764Sdp78419  * the trap table is taken over.
8153764Sdp78419  *
8163764Sdp78419  * The temporary handler knows the physical address of the kmem64
8173764Sdp78419  * area. It uses the prom's pgmap@ Forth word for other addresses.
8183764Sdp78419  *
8193764Sdp78419  * We have to use BOP_ALLOC() method for C-OBP platforms because
8203764Sdp78419  * pgmap@ is not defined in C-OBP. C-OBP is only used on serengeti
8213764Sdp78419  * sun4u platforms. On sun4u we flush tlb after trap table is taken
8223764Sdp78419  * over if we use large pages for kernel heap and kmem64. Since sun4u
8233764Sdp78419  * prom (unlike sun4v) calls va>tte-data first for client address
8243764Sdp78419  * translation prom's ttes for kmem64 can't get into TLB even if we
8253764Sdp78419  * later switch to prom's trap table again. C-OBP uses 4M pages for
8263764Sdp78419  * client mappings when possible so on all platforms we get the
8273764Sdp78419  * benefit from large mappings for kmem64 area immediately during
8283764Sdp78419  * boot.
8293764Sdp78419  *
8303764Sdp78419  * pseudo code:
8313764Sdp78419  * if (context != 0) {
8323764Sdp78419  * 	return false
8333764Sdp78419  * } else if (miss_va in range[kmem64_base, kmem64_end)) {
8343764Sdp78419  *	tte = tte_template +
8353764Sdp78419  *		(((miss_va & pagemask) - kmem64_base));
8363764Sdp78419  *	return tte, true
8373764Sdp78419  * } else {
8383764Sdp78419  *	return pgmap@ result
8393764Sdp78419  * }
8403764Sdp78419  */
8413764Sdp78419 char kmem64_obp_str[] =
8425648Ssetje 	"h# %lx constant kmem64-base "
8435648Ssetje 	"h# %lx constant kmem64-end "
8445648Ssetje 	"h# %lx constant kmem64-pagemask "
8455648Ssetje 	"h# %lx constant kmem64-template "
8463764Sdp78419 
8473764Sdp78419 	": kmem64-tte ( addr cnum -- false | tte-data true ) "
8483764Sdp78419 	"    if                                       ( addr ) "
8493764Sdp78419 	"       drop false exit then                  ( false ) "
8505648Ssetje 	"    dup  kmem64-base kmem64-end  within  if  ( addr ) "
8515648Ssetje 	"	kmem64-pagemask and                   ( addr' ) "
8525648Ssetje 	"	kmem64-base -                         ( addr' ) "
8535648Ssetje 	"	kmem64-template +                     ( tte ) "
8543764Sdp78419 	"	true                                  ( tte true ) "
8553764Sdp78419 	"    else                                     ( addr ) "
8563764Sdp78419 	"	pgmap@                                ( tte ) "
8573764Sdp78419 	"       dup 0< if true else drop false then   ( tte true  |  false ) "
8583764Sdp78419 	"    then                                     ( tte true  |  false ) "
8593764Sdp78419 	"; "
8603764Sdp78419 
8613764Sdp78419 	"' kmem64-tte is va>tte-data "
8623764Sdp78419 ;
8633764Sdp78419 
8645648Ssetje static void
8653764Sdp78419 install_kmem64_tte()
8663764Sdp78419 {
8673764Sdp78419 	char b[sizeof (kmem64_obp_str) + (4 * 16)];
8683764Sdp78419 	tte_t tte;
8693764Sdp78419 
8703764Sdp78419 	PRM_DEBUG(kmem64_pabase);
8713764Sdp78419 	PRM_DEBUG(kmem64_szc);
8723764Sdp78419 	sfmmu_memtte(&tte, kmem64_pabase >> MMU_PAGESHIFT,
8733764Sdp78419 	    PROC_DATA | HAT_NOSYNC, kmem64_szc);
8743764Sdp78419 	PRM_DEBUG(tte.ll);
8753764Sdp78419 	(void) sprintf(b, kmem64_obp_str,
8763764Sdp78419 	    kmem64_base, kmem64_end, TTE_PAGEMASK(kmem64_szc), tte.ll);
8773764Sdp78419 	ASSERT(strlen(b) < sizeof (b));
8783764Sdp78419 	prom_interpret(b, 0, 0, 0, 0, 0);
8793764Sdp78419 }
8803764Sdp78419 #endif	/* !C_OBP */
8813764Sdp78419 
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate  * As OBP takes up some RAM when the system boots, pages will already be "lost"
8840Sstevel@tonic-gate  * to the system and reflected in npages by the time we see it.
8850Sstevel@tonic-gate  *
8860Sstevel@tonic-gate  * We only want to allocate kernel structures in the 64-bit virtual address
8870Sstevel@tonic-gate  * space on systems with enough RAM to make the overhead of keeping track of
8880Sstevel@tonic-gate  * an extra kernel memory segment worthwhile.
8890Sstevel@tonic-gate  *
8900Sstevel@tonic-gate  * Since OBP has already performed its memory allocations by this point, if we
8910Sstevel@tonic-gate  * have more than MINMOVE_RAM_MB MB of RAM left free, go ahead and map
8920Sstevel@tonic-gate  * memory in the 64-bit virtual address space; otherwise keep allocations
8930Sstevel@tonic-gate  * contiguous with we've mapped so far in the 32-bit virtual address space.
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate #define	MINMOVE_RAM_MB	((size_t)1900)
8960Sstevel@tonic-gate #define	MB_TO_BYTES(mb)	((mb) * 1048576ul)
897*5872Ssetje #define	BYTES_TO_MB(b) ((b) / 1048576ul)
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate pgcnt_t	tune_npages = (pgcnt_t)
9000Sstevel@tonic-gate 	(MB_TO_BYTES(MINMOVE_RAM_MB)/ (size_t)MMU_PAGESIZE);
9010Sstevel@tonic-gate 
9023733Sdp78419 #pragma weak page_set_colorequiv_arr_cpu
9033733Sdp78419 extern void page_set_colorequiv_arr_cpu(void);
9045648Ssetje extern void page_set_colorequiv_arr(void);
9055648Ssetje 
906*5872Ssetje static pgcnt_t ramdisk_npages;
907*5872Ssetje static struct memlist *old_phys_avail;
908*5872Ssetje 
909*5872Ssetje kcage_dir_t kcage_startup_dir = KCAGE_DOWN;
9103733Sdp78419 
9110Sstevel@tonic-gate static void
9120Sstevel@tonic-gate startup_memlist(void)
9130Sstevel@tonic-gate {
9145648Ssetje 	size_t hmehash_sz, pagelist_sz, tt_sz;
9155648Ssetje 	size_t psetable_sz;
9160Sstevel@tonic-gate 	caddr_t alloc_base;
9170Sstevel@tonic-gate 	caddr_t memspace;
9180Sstevel@tonic-gate 	struct memlist *cur;
9190Sstevel@tonic-gate 	size_t syslimit = (size_t)SYSLIMIT;
9200Sstevel@tonic-gate 	size_t sysbase = (size_t)SYSBASE;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/*
9230Sstevel@tonic-gate 	 * Initialize enough of the system to allow kmem_alloc to work by
9240Sstevel@tonic-gate 	 * calling boot to allocate its memory until the time that
9250Sstevel@tonic-gate 	 * kvm_init is completed.  The page structs are allocated after
9260Sstevel@tonic-gate 	 * rounding up end to the nearest page boundary; the memsegs are
9270Sstevel@tonic-gate 	 * initialized and the space they use comes from the kernel heap.
9280Sstevel@tonic-gate 	 * With appropriate initialization, they can be reallocated later
9290Sstevel@tonic-gate 	 * to a size appropriate for the machine's configuration.
9300Sstevel@tonic-gate 	 *
9310Sstevel@tonic-gate 	 * At this point, memory is allocated for things that will never
9320Sstevel@tonic-gate 	 * need to be freed, this used to be "valloced".  This allows a
9330Sstevel@tonic-gate 	 * savings as the pages don't need page structures to describe
9340Sstevel@tonic-gate 	 * them because them will not be managed by the vm system.
9350Sstevel@tonic-gate 	 */
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/*
9380Sstevel@tonic-gate 	 * We're loaded by boot with the following configuration (as
9390Sstevel@tonic-gate 	 * specified in the sun4u/conf/Mapfile):
9400Sstevel@tonic-gate 	 *
9410Sstevel@tonic-gate 	 * 	text:		4 MB chunk aligned on a 4MB boundary
9420Sstevel@tonic-gate 	 * 	data & bss:	4 MB chunk aligned on a 4MB boundary
9430Sstevel@tonic-gate 	 *
9440Sstevel@tonic-gate 	 * These two chunks will eventually be mapped by 2 locked 4MB
9450Sstevel@tonic-gate 	 * ttes and will represent the nucleus of the kernel.  This gives
9460Sstevel@tonic-gate 	 * us some free space that is already allocated, some or all of
9470Sstevel@tonic-gate 	 * which is made available to kernel module text.
9480Sstevel@tonic-gate 	 *
9490Sstevel@tonic-gate 	 * The free space in the data-bss chunk is used for nucleus
9500Sstevel@tonic-gate 	 * allocatable data structures and we reserve it using the
9510Sstevel@tonic-gate 	 * nalloc_base and nalloc_end variables.  This space is currently
9520Sstevel@tonic-gate 	 * being used for hat data structures required for tlb miss
9530Sstevel@tonic-gate 	 * handling operations.  We align nalloc_base to a l2 cache
9540Sstevel@tonic-gate 	 * linesize because this is the line size the hardware uses to
9550Sstevel@tonic-gate 	 * maintain cache coherency.
9565648Ssetje 	 * 512K is carved out for module data.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 
9595648Ssetje 	moddata = (caddr_t)roundup((uintptr_t)e_data, MMU_PAGESIZE);
9605648Ssetje 	e_moddata = moddata + MODDATA;
9610Sstevel@tonic-gate 	nalloc_base = e_moddata;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	nalloc_end = (caddr_t)roundup((uintptr_t)nalloc_base, MMU_PAGESIZE4M);
9640Sstevel@tonic-gate 	valloc_base = nalloc_base;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/*
9670Sstevel@tonic-gate 	 * Calculate the start of the data segment.
9680Sstevel@tonic-gate 	 */
9695648Ssetje 	if (((uintptr_t)e_moddata & MMU_PAGEMASK4M) != (uintptr_t)s_data)
9705648Ssetje 		prom_panic("nucleus data overflow");
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9730Sstevel@tonic-gate 	PRM_DEBUG(nalloc_base);
9740Sstevel@tonic-gate 	PRM_DEBUG(nalloc_end);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/*
9770Sstevel@tonic-gate 	 * Remember any slop after e_text so we can give it to the modules.
9780Sstevel@tonic-gate 	 */
9790Sstevel@tonic-gate 	PRM_DEBUG(e_text);
9800Sstevel@tonic-gate 	modtext = (caddr_t)roundup((uintptr_t)e_text, MMU_PAGESIZE);
9813791Skchow 	if (((uintptr_t)e_text & MMU_PAGEMASK4M) != (uintptr_t)s_text)
9823764Sdp78419 		prom_panic("nucleus text overflow");
9830Sstevel@tonic-gate 	modtext_sz = (caddr_t)roundup((uintptr_t)modtext, MMU_PAGESIZE4M) -
9840Sstevel@tonic-gate 	    modtext;
9850Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9860Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
9870Sstevel@tonic-gate 
9885648Ssetje 	init_boot_memlists();
9890Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
9900Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
9910Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
9925648Ssetje 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Remember what the physically available highest page is
9950Sstevel@tonic-gate 	 * so that dumpsys works properly, and find out how much
9960Sstevel@tonic-gate 	 * memory is installed.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	installed_top_size_memlist_array(boot_physinstalled,
9990Sstevel@tonic-gate 	    boot_physinstalled_len, &physmax, &physinstalled);
10000Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
10010Sstevel@tonic-gate 	PRM_DEBUG(physmax);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	/* Fill out memory nodes config structure */
10040Sstevel@tonic-gate 	startup_build_mem_nodes(boot_physinstalled, boot_physinstalled_len);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
10070Sstevel@tonic-gate 	 * npages is the maximum of available physical memory possible.
10080Sstevel@tonic-gate 	 * (ie. it will never be more than this)
10095648Ssetje 	 *
10105648Ssetje 	 * When we boot from a ramdisk, the ramdisk memory isn't free, so
10115648Ssetje 	 * using phys_avail will underestimate what will end up being freed.
10125648Ssetje 	 * A better initial guess is just total memory minus the kernel text
10130Sstevel@tonic-gate 	 */
10145648Ssetje 	npages = physinstalled - btop(MMU_PAGESIZE4M);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	/*
10175648Ssetje 	 * First allocate things that can go in the nucleus data page
10185648Ssetje 	 * (fault status, TSBs, dmv, CPUs)
10190Sstevel@tonic-gate 	 */
10205648Ssetje 	ndata_alloc_init(&ndata, (uintptr_t)nalloc_base, (uintptr_t)nalloc_end);
10215648Ssetje 
10220Sstevel@tonic-gate 	if ((&ndata_alloc_mmfsa != NULL) && (ndata_alloc_mmfsa(&ndata) != 0))
10230Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after mfsa alloc");
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (ndata_alloc_tsbs(&ndata, npages) != 0)
10260Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after tsbs alloc");
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (ndata_alloc_dmv(&ndata) != 0)
10290Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after dmv alloc");
10300Sstevel@tonic-gate 
10315648Ssetje 	if (ndata_alloc_page_mutexs(&ndata) != 0)
10320Sstevel@tonic-gate 		cmn_err(CE_PANIC,
10330Sstevel@tonic-gate 		    "no more nucleus memory after page free lists alloc");
10340Sstevel@tonic-gate 
10355648Ssetje 	if (ndata_alloc_hat(&ndata, npages) != 0)
10360Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after hat alloc");
10370Sstevel@tonic-gate 
10385648Ssetje 	if (ndata_alloc_memseg(&ndata, boot_physavail_len) != 0)
10395648Ssetje 		cmn_err(CE_PANIC, "no more nucleus memory after memseg alloc");
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/*
10420Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10430Sstevel@tonic-gate 	 *
10440Sstevel@tonic-gate 	 * There are comments all over the SFMMU code warning of dire
10450Sstevel@tonic-gate 	 * consequences if the TSBs are moved out of 32-bit space.  This
10460Sstevel@tonic-gate 	 * is largely because the asm code uses "sethi %hi(addr)"-type
10470Sstevel@tonic-gate 	 * instructions which will not provide the expected result if the
10480Sstevel@tonic-gate 	 * address is a 64-bit one.
10490Sstevel@tonic-gate 	 *
10500Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10510Sstevel@tonic-gate 	 */
10520Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)nalloc_end, MMU_PAGESIZE);
10535648Ssetje 	PRM_DEBUG(alloc_base);
10545648Ssetje 
10550Sstevel@tonic-gate 	alloc_base = sfmmu_ktsb_alloc(alloc_base);
10560Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
10570Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * Allocate IOMMU TSB array.  We do this here so that the physical
10610Sstevel@tonic-gate 	 * memory gets deducted from the PROM's physical memory list.
10620Sstevel@tonic-gate 	 */
10630Sstevel@tonic-gate 	alloc_base = iommu_tsb_init(alloc_base);
10645648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
10650Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	/*
10685648Ssetje 	 * Allow for an early allocation of physically contiguous memory.
10695648Ssetje 	 */
10705648Ssetje 	alloc_base = contig_mem_prealloc(alloc_base, npages);
10715648Ssetje 
10725648Ssetje 	/*
10731772Sjl139090 	 * Platforms like Starcat and OPL need special structures assigned in
10741772Sjl139090 	 * 32-bit virtual address space because their probing routines execute
10751772Sjl139090 	 * FCode, and FCode can't handle 64-bit virtual addresses...
10760Sstevel@tonic-gate 	 */
10771772Sjl139090 	if (&plat_startup_memlist) {
10781772Sjl139090 		alloc_base = plat_startup_memlist(alloc_base);
10790Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10800Sstevel@tonic-gate 		    ecache_alignsize);
10810Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/*
10850Sstevel@tonic-gate 	 * Save off where the contiguous allocations to date have ended
10860Sstevel@tonic-gate 	 * in econtig32.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	econtig32 = alloc_base;
10890Sstevel@tonic-gate 	PRM_DEBUG(econtig32);
10900Sstevel@tonic-gate 	if (econtig32 > (caddr_t)KERNEL_LIMIT32)
10910Sstevel@tonic-gate 		cmn_err(CE_PANIC, "econtig32 too big");
10920Sstevel@tonic-gate 
10935648Ssetje 	pp_sz = calc_pp_sz(npages);
10945648Ssetje 	PRM_DEBUG(pp_sz);
10955648Ssetje 	if (kpm_enable) {
10965648Ssetje 		kpm_pp_sz = calc_kpmpp_sz(npages);
10975648Ssetje 		PRM_DEBUG(kpm_pp_sz);
10985648Ssetje 	}
10995648Ssetje 
11005648Ssetje 	hmehash_sz = calc_hmehash_sz(npages);
11015648Ssetje 	PRM_DEBUG(hmehash_sz);
11025648Ssetje 
11035648Ssetje 	pagehash_sz = calc_pagehash_sz(npages);
11045648Ssetje 	PRM_DEBUG(pagehash_sz);
11055648Ssetje 
11065648Ssetje 	pagelist_sz = calc_free_pagelist_sz();
11075648Ssetje 	PRM_DEBUG(pagelist_sz);
11085648Ssetje 
11095648Ssetje #ifdef	TRAPTRACE
11105648Ssetje 	tt_sz = calc_traptrace_sz();
11115648Ssetje 	PRM_DEBUG(tt_sz);
11125648Ssetje #else
11135648Ssetje 	tt_sz = 0;
11145648Ssetje #endif	/* TRAPTRACE */
11150Sstevel@tonic-gate 
11163764Sdp78419 	/*
11175648Ssetje 	 * Place the array that protects pp->p_selock in the kmem64 wad.
11183764Sdp78419 	 */
11195648Ssetje 	pse_shift = size_pse_array(physmem, max_ncpus);
11205648Ssetje 	PRM_DEBUG(pse_shift);
11215648Ssetje 	pse_table_size = 1 << pse_shift;
11225648Ssetje 	PRM_DEBUG(pse_table_size);
11235648Ssetje 	psetable_sz = roundup(
11245648Ssetje 	    pse_table_size * sizeof (pad_mutex_t), ecache_alignsize);
11255648Ssetje 	PRM_DEBUG(psetable_sz);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/*
11285648Ssetje 	 * Now allocate the whole wad
11290Sstevel@tonic-gate 	 */
11305648Ssetje 	kmem64_sz = pp_sz + kpm_pp_sz + hmehash_sz + pagehash_sz +
11315648Ssetje 	    pagelist_sz + tt_sz + psetable_sz;
11325648Ssetje 	kmem64_sz = roundup(kmem64_sz, PAGESIZE);
11335648Ssetje 	kmem64_base = (caddr_t)syslimit;
11345648Ssetje 	kmem64_end = kmem64_base + kmem64_sz;
11355648Ssetje 	alloc_kmem64(kmem64_base, kmem64_end);
11365648Ssetje 	if (kmem64_aligned_end > (hole_start ? hole_start : kpm_vbase))
11375648Ssetje 		cmn_err(CE_PANIC, "not enough kmem64 space");
11385648Ssetje 	PRM_DEBUG(kmem64_base);
11395648Ssetje 	PRM_DEBUG(kmem64_end);
11405648Ssetje 	PRM_DEBUG(kmem64_aligned_end);
11414204Sha137994 
11424204Sha137994 	/*
11435648Ssetje 	 * ... and divy it up
11440Sstevel@tonic-gate 	 */
11455648Ssetje 	alloc_base = kmem64_base;
1146*5872Ssetje 
1147*5872Ssetje 	if (kpm_smallpages == 0) {
1148*5872Ssetje 		npages -= kmem64_sz / (PAGESIZE + sizeof (struct page));
1149*5872Ssetje 	} else {
1150*5872Ssetje 		npages -= kmem64_sz / (PAGESIZE + sizeof (struct page) +
1151*5872Ssetje 		    sizeof (kpm_spage_t));
1152*5872Ssetje 	}
1153*5872Ssetje 
11545648Ssetje 	pp_base = (page_t *)alloc_base;
11555648Ssetje 	pp_sz = npages * sizeof (struct page);
11565648Ssetje 	alloc_base += pp_sz;
11575648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11585648Ssetje 	PRM_DEBUG(pp_base);
11595648Ssetje 	PRM_DEBUG(npages);
11605648Ssetje 
11615648Ssetje 	if (kpm_enable) {
11625648Ssetje 		kpm_pp_base = alloc_base;
11635648Ssetje 		if (kpm_smallpages == 0) {
11645648Ssetje 			/* kpm_npages based on physinstalled, don't reset */
11655648Ssetje 			kpm_pp_sz = kpm_npages * sizeof (kpm_page_t);
11665648Ssetje 		} else {
11675648Ssetje 			kpm_npages = ptokpmpr(npages);
11685648Ssetje 			kpm_pp_sz = kpm_npages * sizeof (kpm_spage_t);
11690Sstevel@tonic-gate 		}
11705648Ssetje 		alloc_base += kpm_pp_sz;
11715648Ssetje 		alloc_base =
11725648Ssetje 		    (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11735648Ssetje 		PRM_DEBUG(kpm_pp_base);
11740Sstevel@tonic-gate 	}
11750Sstevel@tonic-gate 
11765648Ssetje 	alloc_base = alloc_hmehash(alloc_base);
11775648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11785648Ssetje 	PRM_DEBUG(alloc_base);
11795648Ssetje 
11805648Ssetje 	page_hash = (page_t **)alloc_base;
11815648Ssetje 	alloc_base += pagehash_sz;
11825648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11835648Ssetje 	PRM_DEBUG(page_hash);
11845648Ssetje 
11855648Ssetje 	alloc_base = alloc_page_freelists(alloc_base);
11865648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11875648Ssetje 	PRM_DEBUG(alloc_base);
11885648Ssetje 
11895648Ssetje #ifdef	TRAPTRACE
11905648Ssetje 	ttrace_buf = alloc_base;
11915648Ssetje 	alloc_base += tt_sz;
11925648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11935648Ssetje 	PRM_DEBUG(alloc_base);
11945648Ssetje #endif	/* TRAPTRACE */
11955648Ssetje 
11965648Ssetje 	pse_mutex = (pad_mutex_t *)alloc_base;
11975648Ssetje 	alloc_base += psetable_sz;
11985648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
11995648Ssetje 	PRM_DEBUG(alloc_base);
12005648Ssetje 
12015648Ssetje 	/* adjust kmem64_end to what we really allocated */
12025648Ssetje 	kmem64_end = (caddr_t)roundup((uintptr_t)alloc_base, PAGESIZE);
12035648Ssetje 	kmem64_sz = kmem64_end - kmem64_base;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	if (&ecache_init_scrub_flush_area) {
12065648Ssetje 		alloc_base = ecache_init_scrub_flush_area(kmem64_aligned_end);
12075648Ssetje 		ASSERT(alloc_base <= (hole_start ? hole_start : kpm_vbase));
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
12120Sstevel@tonic-gate 	 * the monitor value unless physmem is larger than the total
12130Sstevel@tonic-gate 	 * amount of memory on hand.
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
12160Sstevel@tonic-gate 		physmem = npages;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	/*
12195648Ssetje 	 * root_is_ramdisk is set via /etc/system when the ramdisk miniroot
12205648Ssetje 	 * is mounted as root. This memory is held down by OBP and unlike
12215648Ssetje 	 * the stub boot_archive is never released.
12220Sstevel@tonic-gate 	 *
12235648Ssetje 	 * In order to get things sized correctly on lower memory
12245648Ssetje 	 * machines (where the memory used by the ramdisk represents
12255648Ssetje 	 * a significant portion of memory), physmem is adjusted.
12265648Ssetje 	 *
12275648Ssetje 	 * This is done by subtracting the ramdisk_size which is set
12285648Ssetje 	 * to the size of the ramdisk (in Kb) in /etc/system at the
12295648Ssetje 	 * time the miniroot archive is constructed.
12300Sstevel@tonic-gate 	 */
1231*5872Ssetje 	if (root_is_ramdisk == B_TRUE) {
1232*5872Ssetje 		ramdisk_npages = (ramdisk_size * 1024) / PAGESIZE;
1233*5872Ssetje 		physmem -= ramdisk_npages;
1234*5872Ssetje 	}
12355648Ssetje 
12365648Ssetje 	if (kpm_enable && (ndata_alloc_kpm(&ndata, kpm_npages) != 0))
12375648Ssetje 		cmn_err(CE_PANIC, "no more nucleus memory after kpm alloc");
12380Sstevel@tonic-gate 
12394878Sblakej 	/*
12405648Ssetje 	 * Allocate space for the interrupt vector table.
12413764Sdp78419 	 */
12425648Ssetje 	memspace = prom_alloc((caddr_t)intr_vec_table, IVSIZE, MMU_PAGESIZE);
12432973Sgovinda 	if (memspace != (caddr_t)intr_vec_table)
12443764Sdp78419 		prom_panic("interrupt vector table allocation failure");
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	/*
12470Sstevel@tonic-gate 	 * Between now and when we finish copying in the memory lists,
12480Sstevel@tonic-gate 	 * allocations happen so the space gets fragmented and the
12495648Ssetje 	 * lists longer.  Leave enough space for lists twice as
12505648Ssetje 	 * long as we have now; then roundup to a pagesize.
12510Sstevel@tonic-gate 	 */
12525648Ssetje 	memlist_sz = sizeof (struct memlist) * (prom_phys_installed_len() +
12535648Ssetje 	    prom_phys_avail_len() + prom_virt_avail_len());
12545648Ssetje 	memlist_sz *= 2;
12555648Ssetje 	memlist_sz = roundup(memlist_sz, PAGESIZE);
12565648Ssetje 	memspace = ndata_alloc(&ndata, memlist_sz, ecache_alignsize);
12570Sstevel@tonic-gate 	if (memspace == NULL)
12585648Ssetje 		cmn_err(CE_PANIC, "no more nucleus memory after memlist alloc");
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	memlist = (struct memlist *)memspace;
12610Sstevel@tonic-gate 	memlist_end = (char *)memspace + memlist_sz;
12620Sstevel@tonic-gate 	PRM_DEBUG(memlist);
12630Sstevel@tonic-gate 	PRM_DEBUG(memlist_end);
12645648Ssetje 
12650Sstevel@tonic-gate 	PRM_DEBUG(sysbase);
12660Sstevel@tonic-gate 	PRM_DEBUG(syslimit);
12670Sstevel@tonic-gate 	kernelheap_init((void *)sysbase, (void *)syslimit,
12680Sstevel@tonic-gate 	    (caddr_t)sysbase + PAGESIZE, NULL, NULL);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12710Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
12720Sstevel@tonic-gate 	 */
12730Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
12740Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
12750Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/*
12785648Ssetje 	 * Remove the space used by prom_alloc from the kernel heap
12790Sstevel@tonic-gate 	 * plus the area actually used by the OBP (if any)
12800Sstevel@tonic-gate 	 * ignoring virtual addresses in virt_avail, above syslimit.
12810Sstevel@tonic-gate 	 */
12820Sstevel@tonic-gate 	virt_avail = memlist;
12830Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	for (cur = virt_avail; cur->next; cur = cur->next) {
12860Sstevel@tonic-gate 		uint64_t range_base, range_size;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		if ((range_base = cur->address + cur->size) < (uint64_t)sysbase)
12890Sstevel@tonic-gate 			continue;
12900Sstevel@tonic-gate 		if (range_base >= (uint64_t)syslimit)
12910Sstevel@tonic-gate 			break;
12920Sstevel@tonic-gate 		/*
12930Sstevel@tonic-gate 		 * Limit the range to end at syslimit.
12940Sstevel@tonic-gate 		 */
12950Sstevel@tonic-gate 		range_size = MIN(cur->next->address,
12960Sstevel@tonic-gate 		    (uint64_t)syslimit) - range_base;
12970Sstevel@tonic-gate 		(void) vmem_xalloc(heap_arena, (size_t)range_size, PAGESIZE,
12980Sstevel@tonic-gate 		    0, 0, (void *)range_base, (void *)(range_base + range_size),
12990Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	phys_avail = memlist;
13035648Ssetje 	copy_memlist(boot_physavail, boot_physavail_len, &memlist);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13064104Sblakej 	 * Add any extra memory at the end of the ndata region if there's at
13074104Sblakej 	 * least a page to add.  There might be a few more pages available in
13084104Sblakej 	 * the middle of the ndata region, but for now they are ignored.
13090Sstevel@tonic-gate 	 */
13104104Sblakej 	nalloc_base = ndata_extra_base(&ndata, MMU_PAGESIZE, nalloc_end);
13114104Sblakej 	if (nalloc_base == NULL)
13120Sstevel@tonic-gate 		nalloc_base = nalloc_end;
13130Sstevel@tonic-gate 	ndata_remain_sz = nalloc_end - nalloc_base;
13140Sstevel@tonic-gate 
13155648Ssetje 	/*
13165648Ssetje 	 * Copy physinstalled list into kernel space.
13175648Ssetje 	 */
13185648Ssetje 	phys_install = memlist;
13195648Ssetje 	copy_memlist(boot_physinstalled, boot_physinstalled_len, &memlist);
13205648Ssetje 
13215648Ssetje 	/*
13225648Ssetje 	 * Create list of physical addrs we don't need pp's for:
13235648Ssetje 	 * kernel text 4M page
13245648Ssetje 	 * kernel data 4M page - ndata_remain_sz
13255648Ssetje 	 * kmem64 pages
13265648Ssetje 	 *
13275648Ssetje 	 * NB if adding any pages here, make sure no kpm page
13285648Ssetje 	 * overlaps can occur (see ASSERTs in kphysm_memsegs)
13295648Ssetje 	 */
13305648Ssetje 	nopp_list = memlist;
13315648Ssetje 	memlist_new(va_to_pa(s_text), MMU_PAGESIZE4M, &memlist);
13325648Ssetje 	memlist_add(va_to_pa(s_data), MMU_PAGESIZE4M - ndata_remain_sz,
13335648Ssetje 	    &memlist, &nopp_list);
13345648Ssetje 	memlist_add(kmem64_pabase, kmem64_sz, &memlist, &nopp_list);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if ((caddr_t)memlist > (memspace + memlist_sz))
13373764Sdp78419 		prom_panic("memlist overflow");
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/*
13400Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
13410Sstevel@tonic-gate 	 */
13425648Ssetje 	kphysm_init();
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem;
13450Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Some of the locks depend on page_hashsz being set!
13490Sstevel@tonic-gate 	 * kmem_init() depends on this; so, keep it here.
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	page_lock_init();
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
13550Sstevel@tonic-gate 	 */
13560Sstevel@tonic-gate 	kmem_init();
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	/*
13593717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
13603717Sdp78419 	 */
13613733Sdp78419 	if (&page_set_colorequiv_arr_cpu != NULL)
13623733Sdp78419 		page_set_colorequiv_arr_cpu();
13633733Sdp78419 	else
13643733Sdp78419 		page_set_colorequiv_arr();
13653717Sdp78419 
13663717Sdp78419 	/*
13670Sstevel@tonic-gate 	 * Initialize bp_mapin().
13680Sstevel@tonic-gate 	 */
13690Sstevel@tonic-gate 	bp_init(shm_alignment, HAT_STRICTORDER);
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/*
13725358Sjc25722 	 * Reserve space for panicbuf, intr_vec_table, reserved interrupt
13735358Sjc25722 	 * vector data structures and MPO mblock structs from the 32-bit heap.
13740Sstevel@tonic-gate 	 */
13750Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, PANICBUFSIZE, PAGESIZE, 0, 0,
13760Sstevel@tonic-gate 	    panicbuf, panicbuf + PANICBUFSIZE,
13770Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, IVSIZE, PAGESIZE, 0, 0,
13802973Sgovinda 	    intr_vec_table, (caddr_t)intr_vec_table + IVSIZE,
13810Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
13820Sstevel@tonic-gate 
13835358Sjc25722 	if (mpo_heap32_bufsz > (size_t)0) {
13845358Sjc25722 		(void) vmem_xalloc(heap32_arena, mpo_heap32_bufsz,
13855358Sjc25722 		    PAGESIZE, 0, 0, mpo_heap32_buf,
13865358Sjc25722 		    mpo_heap32_buf + mpo_heap32_bufsz,
13875358Sjc25722 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
13885358Sjc25722 	}
13890Sstevel@tonic-gate 	mem_config_init();
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate static void
13930Sstevel@tonic-gate startup_modules(void)
13940Sstevel@tonic-gate {
13955648Ssetje 	int nhblk1, nhblk8;
13960Sstevel@tonic-gate 	size_t  nhblksz;
13973764Sdp78419 	pgcnt_t pages_per_hblk;
13980Sstevel@tonic-gate 	size_t hme8blk_sz, hme1blk_sz;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	/*
14010Sstevel@tonic-gate 	 * Let the platforms have a chance to change default
14020Sstevel@tonic-gate 	 * values before reading system file.
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	if (&set_platform_defaults)
14050Sstevel@tonic-gate 		set_platform_defaults();
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	/*
14080Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
14090Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
14100Sstevel@tonic-gate 	 */
14110Sstevel@tonic-gate 	param_calc(0);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	mod_setup();
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	/*
14160Sstevel@tonic-gate 	 * If this is a positron, complain and halt.
14170Sstevel@tonic-gate 	 */
14180Sstevel@tonic-gate 	if (&iam_positron && iam_positron()) {
14190Sstevel@tonic-gate 		cmn_err(CE_WARN, "This hardware platform is not supported"
14200Sstevel@tonic-gate 		    " by this release of Solaris.\n");
14210Sstevel@tonic-gate #ifdef DEBUG
14220Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to resume */
14230Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting an unsupported platform.\n");
14240Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware.\n");
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate #else /* DEBUG */
14270Sstevel@tonic-gate 		halt(0);
14280Sstevel@tonic-gate #endif /* DEBUG */
14290Sstevel@tonic-gate 	}
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	/*
14320Sstevel@tonic-gate 	 * If we are running firmware that isn't 64-bit ready
14330Sstevel@tonic-gate 	 * then complain and halt.
14340Sstevel@tonic-gate 	 */
14350Sstevel@tonic-gate 	do_prom_version_check();
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	/*
14380Sstevel@tonic-gate 	 * Initialize system parameters
14390Sstevel@tonic-gate 	 */
14400Sstevel@tonic-gate 	param_init();
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	/*
14430Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
14440Sstevel@tonic-gate 	 */
14450Sstevel@tonic-gate 	maxmem = physmem;
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	/* Set segkp limits. */
14483446Smrj 	ncbase = kdi_segdebugbase;
14493446Smrj 	ncend = kdi_segdebugbase;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/*
14520Sstevel@tonic-gate 	 * Initialize the hat layer.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	hat_init();
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	/*
14570Sstevel@tonic-gate 	 * Initialize segment management stuff.
14580Sstevel@tonic-gate 	 */
14590Sstevel@tonic-gate 	seg_init();
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	/*
14620Sstevel@tonic-gate 	 * Create the va>tte handler, so the prom can understand
14630Sstevel@tonic-gate 	 * kernel translations.  The handler is installed later, just
14640Sstevel@tonic-gate 	 * as we are about to take over the trap table from the prom.
14650Sstevel@tonic-gate 	 */
14660Sstevel@tonic-gate 	create_va_to_tte();
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/*
14690Sstevel@tonic-gate 	 * Load the forthdebugger (optional)
14700Sstevel@tonic-gate 	 */
14710Sstevel@tonic-gate 	forthdebug_init();
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	/*
14740Sstevel@tonic-gate 	 * Create OBP node for console input callbacks
14750Sstevel@tonic-gate 	 * if it is needed.
14760Sstevel@tonic-gate 	 */
14771253Slq150181 	startup_create_io_node();
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	if (modloadonly("fs", "specfs") == -1)
14800Sstevel@tonic-gate 		halt("Can't load specfs");
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	if (modloadonly("fs", "devfs") == -1)
14830Sstevel@tonic-gate 		halt("Can't load devfs");
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	if (modloadonly("misc", "swapgeneric") == -1)
14860Sstevel@tonic-gate 		halt("Can't load swapgeneric");
14870Sstevel@tonic-gate 
14881676Sjpk 	(void) modloadonly("sys", "lbl_edition");
14891676Sjpk 
14900Sstevel@tonic-gate 	dispinit();
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	/*
14930Sstevel@tonic-gate 	 * Infer meanings to the members of the idprom buffer.
14940Sstevel@tonic-gate 	 */
14950Sstevel@tonic-gate 	parse_idprom();
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/* Read cluster configuration data. */
14980Sstevel@tonic-gate 	clconf_init();
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	setup_ddi();
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 * Lets take this opportunity to load the root device.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	if (loadrootmodules() != 0)
15060Sstevel@tonic-gate 		debug_enter("Can't load the root filesystem");
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/*
15090Sstevel@tonic-gate 	 * Load tod driver module for the tod part found on this system.
15100Sstevel@tonic-gate 	 * Recompute the cpu frequency/delays based on tod as tod part
15110Sstevel@tonic-gate 	 * tends to keep time more accurately.
15120Sstevel@tonic-gate 	 */
15130Sstevel@tonic-gate 	if (&load_tod_module)
15140Sstevel@tonic-gate 		load_tod_module();
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/*
15170Sstevel@tonic-gate 	 * Allow platforms to load modules which might
15180Sstevel@tonic-gate 	 * be needed after bootops are gone.
15190Sstevel@tonic-gate 	 */
15200Sstevel@tonic-gate 	if (&load_platform_modules)
15210Sstevel@tonic-gate 		load_platform_modules();
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	setcpudelay();
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
15260Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
15270Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/*
15300Sstevel@tonic-gate 	 * Calculation and allocation of hmeblks needed to remap
15313764Sdp78419 	 * the memory allocated by PROM till now.
15323764Sdp78419 	 * Overestimate the number of hblk1 elements by assuming
15333764Sdp78419 	 * worst case of TTE64K mappings.
15343764Sdp78419 	 * sfmmu_hblk_alloc will panic if this calculation is wrong.
15350Sstevel@tonic-gate 	 */
15363764Sdp78419 	bop_alloc_pages = btopr(kmem64_end - kmem64_base);
15373764Sdp78419 	pages_per_hblk = btop(HMEBLK_SPAN(TTE64K));
15383764Sdp78419 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
15393764Sdp78419 	nhblk1 = bop_alloc_pages / pages_per_hblk + hblk1_min;
15403764Sdp78419 
15413764Sdp78419 	bop_alloc_pages = size_virtalloc(boot_virtavail, boot_virtavail_len);
15423764Sdp78419 
15433764Sdp78419 	/* sfmmu_init_nucleus_hblks expects properly aligned data structures */
15440Sstevel@tonic-gate 	hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
15450Sstevel@tonic-gate 	hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
15460Sstevel@tonic-gate 
15473764Sdp78419 	bop_alloc_pages += btopr(nhblk1 * hme1blk_sz);
15483764Sdp78419 
15490Sstevel@tonic-gate 	pages_per_hblk = btop(HMEBLK_SPAN(TTE8K));
15503764Sdp78419 	nhblk8 = 0;
15513764Sdp78419 	while (bop_alloc_pages > 1) {
15523764Sdp78419 		bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
15533764Sdp78419 		nhblk8 += bop_alloc_pages /= pages_per_hblk;
15543764Sdp78419 		bop_alloc_pages *= hme8blk_sz;
15553764Sdp78419 		bop_alloc_pages = btopr(bop_alloc_pages);
15563764Sdp78419 	}
15573764Sdp78419 	nhblk8 += 2;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15600Sstevel@tonic-gate 	 * Since hblk8's can hold up to 64k of mappings aligned on a 64k
15610Sstevel@tonic-gate 	 * boundary, the number of hblk8's needed to map the entries in the
15620Sstevel@tonic-gate 	 * boot_virtavail list needs to be adjusted to take this into
15630Sstevel@tonic-gate 	 * consideration.  Thus, we need to add additional hblk8's since it
15640Sstevel@tonic-gate 	 * is possible that an hblk8 will not have all 8 slots used due to
15650Sstevel@tonic-gate 	 * alignment constraints.  Since there were boot_virtavail_len entries
15660Sstevel@tonic-gate 	 * in that list, we need to add that many hblk8's to the number
15670Sstevel@tonic-gate 	 * already calculated to make sure we don't underestimate.
15680Sstevel@tonic-gate 	 */
15690Sstevel@tonic-gate 	nhblk8 += boot_virtavail_len;
15700Sstevel@tonic-gate 	nhblksz = nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/* Allocate in pagesize chunks */
15730Sstevel@tonic-gate 	nhblksz = roundup(nhblksz, MMU_PAGESIZE);
15740Sstevel@tonic-gate 	hblk_base = kmem_zalloc(nhblksz, KM_SLEEP);
15750Sstevel@tonic-gate 	sfmmu_init_nucleus_hblks(hblk_base, nhblksz, nhblk8, nhblk1);
15760Sstevel@tonic-gate }
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate static void
15790Sstevel@tonic-gate startup_bop_gone(void)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	/*
15831991Sheppo 	 * Destroy the MD initialized at startup
15841991Sheppo 	 * The startup initializes the MD framework
15851991Sheppo 	 * using prom and BOP alloc free it now.
15861991Sheppo 	 */
15871991Sheppo 	mach_descrip_startup_fini();
15881991Sheppo 
15891991Sheppo 	/*
15905648Ssetje 	 * We're done with prom allocations.
15910Sstevel@tonic-gate 	 */
15925648Ssetje 	bop_fini();
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
15950Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
15960Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	/*
15990Sstevel@tonic-gate 	 * setup physically contiguous area twice as large as the ecache.
16000Sstevel@tonic-gate 	 * this is used while doing displacement flush of ecaches
16010Sstevel@tonic-gate 	 */
16020Sstevel@tonic-gate 	if (&ecache_flush_address) {
16030Sstevel@tonic-gate 		ecache_flushaddr = ecache_flush_address();
16040Sstevel@tonic-gate 		if (ecache_flushaddr == (uint64_t)-1) {
16050Sstevel@tonic-gate 			cmn_err(CE_PANIC,
16060Sstevel@tonic-gate 			    "startup: no memory to set ecache_flushaddr");
16070Sstevel@tonic-gate 		}
16080Sstevel@tonic-gate 	}
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	/*
16110Sstevel@tonic-gate 	 * Virtual available next.
16120Sstevel@tonic-gate 	 */
16130Sstevel@tonic-gate 	ASSERT(virt_avail != NULL);
16140Sstevel@tonic-gate 	memlist_free_list(virt_avail);
16150Sstevel@tonic-gate 	virt_avail = memlist;
16160Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate  * startup_fixup_physavail - called from mach_sfmmu.c after the final
16230Sstevel@tonic-gate  * allocations have been performed.  We can't call it in startup_bop_gone
16240Sstevel@tonic-gate  * since later operations can cause obp to allocate more memory.
16250Sstevel@tonic-gate  */
16260Sstevel@tonic-gate void
16270Sstevel@tonic-gate startup_fixup_physavail(void)
16280Sstevel@tonic-gate {
16290Sstevel@tonic-gate 	struct memlist *cur;
16303764Sdp78419 	size_t kmem64_overmap_size = kmem64_aligned_end - kmem64_end;
16313764Sdp78419 
16323764Sdp78419 	PRM_DEBUG(kmem64_overmap_size);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	/*
16350Sstevel@tonic-gate 	 * take the most current snapshot we can by calling mem-update
16360Sstevel@tonic-gate 	 */
16370Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
16380Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
16390Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	/*
16420Sstevel@tonic-gate 	 * Copy phys_avail list, again.
16430Sstevel@tonic-gate 	 * Both the kernel/boot and the prom have been allocating
16440Sstevel@tonic-gate 	 * from the original list we copied earlier.
16450Sstevel@tonic-gate 	 */
16460Sstevel@tonic-gate 	cur = memlist;
16475648Ssetje 	copy_memlist(boot_physavail, boot_physavail_len, &memlist);
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	/*
16503764Sdp78419 	 * Add any unused kmem64 memory from overmapped page
16513764Sdp78419 	 * (Note: va_to_pa does not work for kmem64_end)
16523764Sdp78419 	 */
16533764Sdp78419 	if (kmem64_overmap_size) {
16543764Sdp78419 		memlist_add(kmem64_pabase + (kmem64_end - kmem64_base),
16555648Ssetje 		    kmem64_overmap_size, &memlist, &cur);
16563764Sdp78419 	}
16573764Sdp78419 
16583764Sdp78419 	/*
16595648Ssetje 	 * Add any extra memory after e_data we added to the phys_avail list
16600Sstevel@tonic-gate 	 * back to the old list.
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
16630Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
16640Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &cur);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/*
16670Sstevel@tonic-gate 	 * There isn't any bounds checking on the memlist area
16680Sstevel@tonic-gate 	 * so ensure it hasn't overgrown.
16690Sstevel@tonic-gate 	 */
16700Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
16710Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: memlist size exceeded");
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/*
16740Sstevel@tonic-gate 	 * The kernel removes the pages that were allocated for it from
16750Sstevel@tonic-gate 	 * the freelist, but we now have to find any -extra- pages that
16760Sstevel@tonic-gate 	 * the prom has allocated for it's own book-keeping, and remove
16770Sstevel@tonic-gate 	 * them from the freelist too. sigh.
16780Sstevel@tonic-gate 	 */
16795648Ssetje 	sync_memlists(phys_avail, cur);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	ASSERT(phys_avail != NULL);
1682*5872Ssetje 
1683*5872Ssetje 	old_phys_avail = phys_avail;
16840Sstevel@tonic-gate 	phys_avail = cur;
1685*5872Ssetje }
1686*5872Ssetje 
1687*5872Ssetje void
1688*5872Ssetje update_kcage_ranges(uint64_t addr, uint64_t len)
1689*5872Ssetje {
1690*5872Ssetje 	pfn_t base = btop(addr);
1691*5872Ssetje 	pgcnt_t num = btop(len);
1692*5872Ssetje 	int rv;
1693*5872Ssetje 
1694*5872Ssetje 	rv = kcage_range_add(base, num, kcage_startup_dir);
1695*5872Ssetje 
1696*5872Ssetje 	if (rv == ENOMEM) {
1697*5872Ssetje 		cmn_err(CE_WARN, "%ld megabytes not available to kernel cage",
1698*5872Ssetje 		    (len == 0 ? 0 : BYTES_TO_MB(len)));
1699*5872Ssetje 	} else if (rv != 0) {
1700*5872Ssetje 		/* catch this in debug kernels */
1701*5872Ssetje 		ASSERT(0);
1702*5872Ssetje 
1703*5872Ssetje 		cmn_err(CE_WARN, "unexpected kcage_range_add"
1704*5872Ssetje 		    " return value %d", rv);
1705*5872Ssetje 	}
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate static void
17090Sstevel@tonic-gate startup_vm(void)
17100Sstevel@tonic-gate {
17110Sstevel@tonic-gate 	size_t	i;
17120Sstevel@tonic-gate 	struct segmap_crargs a;
17130Sstevel@tonic-gate 	struct segkpm_crargs b;
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	uint64_t avmem;
17160Sstevel@tonic-gate 	caddr_t va;
17170Sstevel@tonic-gate 	pgcnt_t	max_phys_segkp;
17180Sstevel@tonic-gate 	int	mnode;
17190Sstevel@tonic-gate 
17202991Ssusans 	extern int use_brk_lpg, use_stk_lpg;
1721423Sdavemq 
17220Sstevel@tonic-gate 	/*
17230Sstevel@tonic-gate 	 * get prom's mappings, create hments for them and switch
17240Sstevel@tonic-gate 	 * to the kernel context.
17250Sstevel@tonic-gate 	 */
17260Sstevel@tonic-gate 	hat_kern_setup();
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	/*
17290Sstevel@tonic-gate 	 * Take over trap table
17300Sstevel@tonic-gate 	 */
17310Sstevel@tonic-gate 	setup_trap_table();
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	/*
17340Sstevel@tonic-gate 	 * Install the va>tte handler, so that the prom can handle
17350Sstevel@tonic-gate 	 * misses and understand the kernel table layout in case
17360Sstevel@tonic-gate 	 * we need call into the prom.
17370Sstevel@tonic-gate 	 */
17380Sstevel@tonic-gate 	install_va_to_tte();
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	/*
17410Sstevel@tonic-gate 	 * Set a flag to indicate that the tba has been taken over.
17420Sstevel@tonic-gate 	 */
17430Sstevel@tonic-gate 	tba_taken_over = 1;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	/* initialize MMU primary context register */
17460Sstevel@tonic-gate 	mmu_init_kcontext();
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	/*
17490Sstevel@tonic-gate 	 * The boot cpu can now take interrupts, x-calls, x-traps
17500Sstevel@tonic-gate 	 */
17510Sstevel@tonic-gate 	CPUSET_ADD(cpu_ready_set, CPU->cpu_id);
17520Sstevel@tonic-gate 	CPU->cpu_flags |= (CPU_READY | CPU_ENABLE | CPU_EXISTS);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/*
17550Sstevel@tonic-gate 	 * Set a flag to tell write_scb_int() that it can access V_TBR_WR_ADDR.
17560Sstevel@tonic-gate 	 */
17570Sstevel@tonic-gate 	tbr_wr_addr_inited = 1;
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	/*
17600Sstevel@tonic-gate 	 * Initialize VM system, and map kernel address space.
17610Sstevel@tonic-gate 	 */
17620Sstevel@tonic-gate 	kvm_init();
17630Sstevel@tonic-gate 
1764*5872Ssetje 	ASSERT(old_phys_avail != NULL && phys_avail != NULL);
1765*5872Ssetje 	if (kernel_cage_enable) {
1766*5872Ssetje 		diff_memlists(phys_avail, old_phys_avail, update_kcage_ranges);
1767*5872Ssetje 	}
1768*5872Ssetje 	memlist_free_list(old_phys_avail);
1769*5872Ssetje 
17700Sstevel@tonic-gate 	/*
17710Sstevel@tonic-gate 	 * If the following is true, someone has patched
17720Sstevel@tonic-gate 	 * phsymem to be less than the number of pages that
17730Sstevel@tonic-gate 	 * the system actually has.  Remove pages until system
17740Sstevel@tonic-gate 	 * memory is limited to the requested amount.  Since we
17750Sstevel@tonic-gate 	 * have allocated page structures for all pages, we
17760Sstevel@tonic-gate 	 * correct the amount of memory we want to remove
17770Sstevel@tonic-gate 	 * by the size of the memory used to hold page structures
17780Sstevel@tonic-gate 	 * for the non-used pages.
17790Sstevel@tonic-gate 	 */
1780*5872Ssetje 	if (physmem + ramdisk_npages < npages) {
17810Sstevel@tonic-gate 		pgcnt_t diff, off;
17820Sstevel@tonic-gate 		struct page *pp;
17830Sstevel@tonic-gate 		struct seg kseg;
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %ld pages", physmem);
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 		off = 0;
1788*5872Ssetje 		diff = npages - (physmem + ramdisk_npages);
17890Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
17900Sstevel@tonic-gate 		kseg.s_as = &kas;
17910Sstevel@tonic-gate 		while (diff--) {
17920Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, (offset_t)off,
17930Sstevel@tonic-gate 			    MMU_PAGESIZE, PG_WAIT | PG_EXCL,
17940Sstevel@tonic-gate 			    &kseg, (caddr_t)off);
17950Sstevel@tonic-gate 			if (pp == NULL)
17960Sstevel@tonic-gate 				cmn_err(CE_PANIC, "limited physmem too much!");
17970Sstevel@tonic-gate 			page_io_unlock(pp);
17980Sstevel@tonic-gate 			page_downgrade(pp);
17990Sstevel@tonic-gate 			availrmem--;
18000Sstevel@tonic-gate 			off += MMU_PAGESIZE;
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	/*
18050Sstevel@tonic-gate 	 * When printing memory, show the total as physmem less
18060Sstevel@tonic-gate 	 * that stolen by a debugger.
18070Sstevel@tonic-gate 	 */
18080Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %ldK (0x%lx000)\n",
18090Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 10),
18100Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 12));
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
18130Sstevel@tonic-gate 	cmn_err(CE_CONT, "?avail mem = %lld\n", (unsigned long long)avmem);
18140Sstevel@tonic-gate 
18152991Ssusans 	/*
18162991Ssusans 	 * For small memory systems disable automatic large pages.
18172991Ssusans 	 */
18182991Ssusans 	if (physmem < privm_lpg_min_physmem) {
1819423Sdavemq 		use_brk_lpg = 0;
1820423Sdavemq 		use_stk_lpg = 0;
1821423Sdavemq 	}
1822423Sdavemq 
18230Sstevel@tonic-gate 	/*
18240Sstevel@tonic-gate 	 * Perform platform specific freelist processing
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	if (&plat_freelist_process) {
18270Sstevel@tonic-gate 		for (mnode = 0; mnode < max_mem_nodes; mnode++)
18280Sstevel@tonic-gate 			if (mem_node_config[mnode].exists)
18290Sstevel@tonic-gate 				plat_freelist_process(mnode);
18300Sstevel@tonic-gate 	}
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 	/*
18330Sstevel@tonic-gate 	 * Initialize the segkp segment type.  We position it
18340Sstevel@tonic-gate 	 * after the configured tables and buffers (whose end
18350Sstevel@tonic-gate 	 * is given by econtig) and before V_WKBASE_ADDR.
18360Sstevel@tonic-gate 	 * Also in this area is segkmap (size SEGMAPSIZE).
18370Sstevel@tonic-gate 	 */
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	/* XXX - cache alignment? */
18400Sstevel@tonic-gate 	va = (caddr_t)SEGKPBASE;
18410Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & PAGEOFFSET) == 0);
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	max_phys_segkp = (physmem * 2);
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	if (segkpsize < btop(SEGKPMINSIZE) || segkpsize > btop(SEGKPMAXSIZE)) {
18460Sstevel@tonic-gate 		segkpsize = btop(SEGKPDEFSIZE);
18470Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal value for segkpsize. "
18480Sstevel@tonic-gate 		    "segkpsize has been reset to %ld pages", segkpsize);
18490Sstevel@tonic-gate 	}
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	i = ptob(MIN(segkpsize, max_phys_segkp));
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
18540Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkp) < 0)
18550Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: cannot attach segkp");
18560Sstevel@tonic-gate 	if (segkp_create(segkp) != 0)
18570Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: segkp_create failed");
18580Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	/*
18610Sstevel@tonic-gate 	 * kpm segment
18620Sstevel@tonic-gate 	 */
18630Sstevel@tonic-gate 	segmap_kpm = kpm_enable &&
18644528Spaulsan 	    segmap_kpm && PAGESIZE == MAXBSIZE;
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	if (kpm_enable) {
18670Sstevel@tonic-gate 		rw_enter(&kas.a_lock, RW_WRITER);
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 		/*
18700Sstevel@tonic-gate 		 * The segkpm virtual range range is larger than the
18710Sstevel@tonic-gate 		 * actual physical memory size and also covers gaps in
18720Sstevel@tonic-gate 		 * the physical address range for the following reasons:
18730Sstevel@tonic-gate 		 * . keep conversion between segkpm and physical addresses
18740Sstevel@tonic-gate 		 *   simple, cheap and unambiguous.
18750Sstevel@tonic-gate 		 * . avoid extension/shrink of the the segkpm in case of DR.
18760Sstevel@tonic-gate 		 * . avoid complexity for handling of virtual addressed
18770Sstevel@tonic-gate 		 *   caches, segkpm and the regular mapping scheme must be
18780Sstevel@tonic-gate 		 *   kept in sync wrt. the virtual color of mapped pages.
18790Sstevel@tonic-gate 		 * Any accesses to virtual segkpm ranges not backed by
18800Sstevel@tonic-gate 		 * physical memory will fall through the memseg pfn hash
18810Sstevel@tonic-gate 		 * and will be handled in segkpm_fault.
18820Sstevel@tonic-gate 		 * Additional kpm_size spaces needed for vac alias prevention.
18830Sstevel@tonic-gate 		 */
18840Sstevel@tonic-gate 		if (seg_attach(&kas, kpm_vbase, kpm_size * vac_colors,
18850Sstevel@tonic-gate 		    segkpm) < 0)
18860Sstevel@tonic-gate 			cmn_err(CE_PANIC, "cannot attach segkpm");
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 		b.prot = PROT_READ | PROT_WRITE;
18890Sstevel@tonic-gate 		b.nvcolors = shm_alignment >> MMU_PAGESHIFT;
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 		if (segkpm_create(segkpm, (caddr_t)&b) != 0)
18920Sstevel@tonic-gate 			panic("segkpm_create segkpm");
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 		rw_exit(&kas.a_lock);
18952296Sae112802 
18962296Sae112802 		mach_kpm_init();
18970Sstevel@tonic-gate 	}
18980Sstevel@tonic-gate 
18993290Sjohansen 	if (!segzio_fromheap) {
19003290Sjohansen 		size_t size;
19013552Sjohansen 		size_t physmem_b = mmu_ptob(physmem);
19023290Sjohansen 
19033290Sjohansen 		/* size is in bytes, segziosize is in pages */
19043290Sjohansen 		if (segziosize == 0) {
19053552Sjohansen 			size = physmem_b;
19063290Sjohansen 		} else {
19073290Sjohansen 			size = mmu_ptob(segziosize);
19083290Sjohansen 		}
19093290Sjohansen 
19103290Sjohansen 		if (size < SEGZIOMINSIZE) {
19113290Sjohansen 			size = SEGZIOMINSIZE;
19123552Sjohansen 		} else if (size > SEGZIOMAXSIZE) {
19133552Sjohansen 			size = SEGZIOMAXSIZE;
19143552Sjohansen 			/*
19153552Sjohansen 			 * On 64-bit x86, we only have 2TB of KVA.  This exists
19163552Sjohansen 			 * for parity with x86.
19173552Sjohansen 			 *
19183552Sjohansen 			 * SEGZIOMAXSIZE is capped at 512gb so that segzio
19193552Sjohansen 			 * doesn't consume all of KVA.  However, if we have a
19203552Sjohansen 			 * system that has more thant 512gb of physical memory,
19213552Sjohansen 			 * we can actually consume about half of the difference
19223552Sjohansen 			 * between 512gb and the rest of the available physical
19233552Sjohansen 			 * memory.
19243552Sjohansen 			 */
19253552Sjohansen 			if (physmem_b > SEGZIOMAXSIZE) {
19263552Sjohansen 				size += (physmem_b - SEGZIOMAXSIZE) / 2;
19273552Sjohansen 		}
19283290Sjohansen 		}
19293290Sjohansen 		segziosize = mmu_btop(roundup(size, MMU_PAGESIZE));
19303290Sjohansen 		/* put the base of the ZIO segment after the kpm segment */
19313290Sjohansen 		segzio_base = kpm_vbase + (kpm_size * vac_colors);
19323290Sjohansen 		PRM_DEBUG(segziosize);
19333290Sjohansen 		PRM_DEBUG(segzio_base);
19343290Sjohansen 
19353290Sjohansen 		/*
19363290Sjohansen 		 * On some platforms, kvm_init is called after the kpm
19373290Sjohansen 		 * sizes have been determined.  On SPARC, kvm_init is called
19383290Sjohansen 		 * before, so we have to attach the kzioseg after kvm is
19393290Sjohansen 		 * initialized, otherwise we'll try to allocate from the boot
19403290Sjohansen 		 * area since the kernel heap hasn't yet been configured.
19413290Sjohansen 		 */
19423290Sjohansen 		rw_enter(&kas.a_lock, RW_WRITER);
19433290Sjohansen 
19443290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
19453290Sjohansen 		    &kzioseg);
19463290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
19473290Sjohansen 
19483290Sjohansen 		/* create zio area covering new segment */
19493290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
19503290Sjohansen 
19513290Sjohansen 		rw_exit(&kas.a_lock);
19523290Sjohansen 	}
19533290Sjohansen 
19543290Sjohansen 
19550Sstevel@tonic-gate 	/*
19560Sstevel@tonic-gate 	 * Now create generic mapping segment.  This mapping
19570Sstevel@tonic-gate 	 * goes SEGMAPSIZE beyond SEGMAPBASE.  But if the total
19580Sstevel@tonic-gate 	 * virtual address is greater than the amount of free
19590Sstevel@tonic-gate 	 * memory that is available, then we trim back the
19600Sstevel@tonic-gate 	 * segment size to that amount
19610Sstevel@tonic-gate 	 */
19620Sstevel@tonic-gate 	va = (caddr_t)SEGMAPBASE;
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	/*
19650Sstevel@tonic-gate 	 * 1201049: segkmap base address must be MAXBSIZE aligned
19660Sstevel@tonic-gate 	 */
19670Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & MAXBOFFSET) == 0);
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * Set size of segmap to percentage of freemem at boot,
19710Sstevel@tonic-gate 	 * but stay within the allowable range
19720Sstevel@tonic-gate 	 * Note we take percentage  before converting from pages
19730Sstevel@tonic-gate 	 * to bytes to avoid an overflow on 32-bit kernels.
19740Sstevel@tonic-gate 	 */
19750Sstevel@tonic-gate 	i = mmu_ptob((freemem * segmap_percent) / 100);
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	if (i < MINMAPSIZE)
19780Sstevel@tonic-gate 		i = MINMAPSIZE;
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	if (i > MIN(SEGMAPSIZE, mmu_ptob(freemem)))
19810Sstevel@tonic-gate 		i = MIN(SEGMAPSIZE, mmu_ptob(freemem));
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	i &= MAXBMASK;	/* 1201049: segkmap size must be MAXBSIZE aligned */
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
19860Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkmap) < 0)
19870Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cannot attach segkmap");
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
19900Sstevel@tonic-gate 	a.shmsize = shm_alignment;
19910Sstevel@tonic-gate 	a.nfreelist = 0;	/* use segmap driver defaults */
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
19940Sstevel@tonic-gate 		panic("segmap_create segkmap");
19950Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	segdev_init();
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate static void
20010Sstevel@tonic-gate startup_end(void)
20020Sstevel@tonic-gate {
20030Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
20040Sstevel@tonic-gate 		panic("memlist overflow 2");
20050Sstevel@tonic-gate 	memlist_free_block((caddr_t)memlist,
20060Sstevel@tonic-gate 	    ((caddr_t)memlist_end - (caddr_t)memlist));
20070Sstevel@tonic-gate 	memlist = NULL;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	/* enable page_relocation since OBP is now done */
20100Sstevel@tonic-gate 	page_relocate_ready = 1;
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	/*
20130Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
20140Sstevel@tonic-gate 	 * initialization has been done but before the clock
20150Sstevel@tonic-gate 	 * and other devices get started.
20160Sstevel@tonic-gate 	 */
20170Sstevel@tonic-gate 	kern_setup1();
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	/*
20200Sstevel@tonic-gate 	 * Intialize the VM arenas for allocating physically
20210Sstevel@tonic-gate 	 * contiguus memory chunk for interrupt queues snd
20220Sstevel@tonic-gate 	 * allocate/register boot cpu's queues, if any and
20230Sstevel@tonic-gate 	 * allocate dump buffer for sun4v systems to store
20240Sstevel@tonic-gate 	 * extra crash information during crash dump
20250Sstevel@tonic-gate 	 */
20260Sstevel@tonic-gate 	contig_mem_init();
20270Sstevel@tonic-gate 	mach_descrip_init();
20284050Sjb145095 
20294050Sjb145095 	if (cpu_intrq_setup(CPU)) {
20304050Sjb145095 		cmn_err(CE_PANIC, "cpu%d: setup failed", CPU->cpu_id);
20314050Sjb145095 	}
20320Sstevel@tonic-gate 	cpu_intrq_register(CPU);
20331077Ssvemuri 	mach_htraptrace_setup(CPU->cpu_id);
20341077Ssvemuri 	mach_htraptrace_configure(CPU->cpu_id);
20350Sstevel@tonic-gate 	mach_dump_buffer_init();
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	/*
20380Sstevel@tonic-gate 	 * Initialize interrupt related stuff
20390Sstevel@tonic-gate 	 */
20401455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	(void) splzs();			/* allow hi clock ints but not zs */
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	/*
20450Sstevel@tonic-gate 	 * Initialize errors.
20460Sstevel@tonic-gate 	 */
20470Sstevel@tonic-gate 	error_init();
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	/*
20500Sstevel@tonic-gate 	 * Note that we may have already used kernel bcopy before this
20510Sstevel@tonic-gate 	 * point - but if you really care about this, adb the use_hw_*
20520Sstevel@tonic-gate 	 * variables to 0 before rebooting.
20530Sstevel@tonic-gate 	 */
20540Sstevel@tonic-gate 	mach_hw_copy_limit();
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	/*
20570Sstevel@tonic-gate 	 * Install the "real" preemption guards before DDI services
20580Sstevel@tonic-gate 	 * are available.
20590Sstevel@tonic-gate 	 */
20600Sstevel@tonic-gate 	(void) prom_set_preprom(kern_preprom);
20610Sstevel@tonic-gate 	(void) prom_set_postprom(kern_postprom);
20620Sstevel@tonic-gate 	CPU->cpu_m.mutex_ready = 1;
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	/*
20650Sstevel@tonic-gate 	 * Initialize segnf (kernel support for non-faulting loads).
20660Sstevel@tonic-gate 	 */
20670Sstevel@tonic-gate 	segnf_init();
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	/*
20700Sstevel@tonic-gate 	 * Configure the root devinfo node.
20710Sstevel@tonic-gate 	 */
20720Sstevel@tonic-gate 	configure();		/* set up devices */
20730Sstevel@tonic-gate 	mach_cpu_halt_idle();
20740Sstevel@tonic-gate }
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate void
20780Sstevel@tonic-gate post_startup(void)
20790Sstevel@tonic-gate {
20800Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
20810Sstevel@tonic-gate 	extern void init_ptl1_thread(void);
20820Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
20830Sstevel@tonic-gate 	extern void abort_sequence_init(void);
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	/*
20860Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
20870Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
20880Sstevel@tonic-gate 	 * instruction set extensions.
20890Sstevel@tonic-gate 	 */
20900Sstevel@tonic-gate 	bind_hwcap();
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	/*
20930Sstevel@tonic-gate 	 * Startup memory scrubber (if any)
20940Sstevel@tonic-gate 	 */
20950Sstevel@tonic-gate 	mach_memscrub();
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	/*
20980Sstevel@tonic-gate 	 * Allocate soft interrupt to handle abort sequence.
20990Sstevel@tonic-gate 	 */
21000Sstevel@tonic-gate 	abort_sequence_init();
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	/*
21030Sstevel@tonic-gate 	 * Configure the rest of the system.
21040Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
21050Sstevel@tonic-gate 	 */
21060Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
21070Sstevel@tonic-gate 	/*
21080Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
21090Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
21100Sstevel@tonic-gate 	 */
21110Sstevel@tonic-gate 	(void) modload("fs", "procfs");
21120Sstevel@tonic-gate 
21131991Sheppo 	/* load machine class specific drivers */
21141991Sheppo 	load_mach_drivers();
21151991Sheppo 
21161991Sheppo 	/* load platform specific drivers */
21170Sstevel@tonic-gate 	if (&load_platform_drivers)
21180Sstevel@tonic-gate 		load_platform_drivers();
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	/* load vis simulation module, if we are running w/fpu off */
21210Sstevel@tonic-gate 	if (!fpu_exists) {
21220Sstevel@tonic-gate 		if (modload("misc", "vis") == -1)
21230Sstevel@tonic-gate 			halt("Can't load vis");
21240Sstevel@tonic-gate 	}
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	mach_fpras();
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	maxmem = freemem;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
21310Sstevel@tonic-gate 	init_ptl1_thread();
21320Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
21331991Sheppo 
21341991Sheppo 	if (&cif_init)
21351991Sheppo 		cif_init();
21360Sstevel@tonic-gate }
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
21390Sstevel@tonic-gate int		ptl1_panic_test = 0;
21400Sstevel@tonic-gate int		ptl1_panic_xc_one_test = 0;
21410Sstevel@tonic-gate int		ptl1_panic_xc_all_test = 0;
21420Sstevel@tonic-gate int		ptl1_panic_xt_one_test = 0;
21430Sstevel@tonic-gate int		ptl1_panic_xt_all_test = 0;
21440Sstevel@tonic-gate kthread_id_t	ptl1_thread_p = NULL;
21450Sstevel@tonic-gate kcondvar_t	ptl1_cv;
21460Sstevel@tonic-gate kmutex_t	ptl1_mutex;
21470Sstevel@tonic-gate int		ptl1_recurse_count_threshold = 0x40;
21480Sstevel@tonic-gate int		ptl1_recurse_trap_threshold = 0x3d;
21490Sstevel@tonic-gate extern void	ptl1_recurse(int, int);
21500Sstevel@tonic-gate extern void	ptl1_panic_xt(int, int);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate /*
21530Sstevel@tonic-gate  * Called once per second by timeout() to wake up
21540Sstevel@tonic-gate  * the ptl1_panic thread to see if it should cause
21550Sstevel@tonic-gate  * a trap to the ptl1_panic() code.
21560Sstevel@tonic-gate  */
21570Sstevel@tonic-gate /* ARGSUSED */
21580Sstevel@tonic-gate static void
21590Sstevel@tonic-gate ptl1_wakeup(void *arg)
21600Sstevel@tonic-gate {
21610Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
21620Sstevel@tonic-gate 	cv_signal(&ptl1_cv);
21630Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
21640Sstevel@tonic-gate }
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate /*
21670Sstevel@tonic-gate  * ptl1_panic cross call function:
21680Sstevel@tonic-gate  *     Needed because xc_one() and xc_some() can pass
21690Sstevel@tonic-gate  *	64 bit args but ptl1_recurse() expects ints.
21700Sstevel@tonic-gate  */
21710Sstevel@tonic-gate static void
21720Sstevel@tonic-gate ptl1_panic_xc(void)
21730Sstevel@tonic-gate {
21740Sstevel@tonic-gate 	ptl1_recurse(ptl1_recurse_count_threshold,
21750Sstevel@tonic-gate 	    ptl1_recurse_trap_threshold);
21760Sstevel@tonic-gate }
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate /*
21790Sstevel@tonic-gate  * The ptl1 thread waits for a global flag to be set
21800Sstevel@tonic-gate  * and uses the recurse thresholds to set the stack depth
21810Sstevel@tonic-gate  * to cause a ptl1_panic() directly via a call to ptl1_recurse
21820Sstevel@tonic-gate  * or indirectly via the cross call and cross trap functions.
21830Sstevel@tonic-gate  *
21840Sstevel@tonic-gate  * This is useful testing stack overflows and normal
21850Sstevel@tonic-gate  * ptl1_panic() states with a know stack frame.
21860Sstevel@tonic-gate  *
21870Sstevel@tonic-gate  * ptl1_recurse() is an asm function in ptl1_panic.s that
21880Sstevel@tonic-gate  * sets the {In, Local, Out, and Global} registers to a
21890Sstevel@tonic-gate  * know state on the stack and just prior to causing a
21900Sstevel@tonic-gate  * test ptl1_panic trap.
21910Sstevel@tonic-gate  */
21920Sstevel@tonic-gate static void
21930Sstevel@tonic-gate ptl1_thread(void)
21940Sstevel@tonic-gate {
21950Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
21960Sstevel@tonic-gate 	while (ptl1_thread_p) {
21970Sstevel@tonic-gate 		cpuset_t	other_cpus;
21980Sstevel@tonic-gate 		int		cpu_id;
21990Sstevel@tonic-gate 		int		my_cpu_id;
22000Sstevel@tonic-gate 		int		target_cpu_id;
22010Sstevel@tonic-gate 		int		target_found;
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 		if (ptl1_panic_test) {
22040Sstevel@tonic-gate 			ptl1_recurse(ptl1_recurse_count_threshold,
22050Sstevel@tonic-gate 			    ptl1_recurse_trap_threshold);
22060Sstevel@tonic-gate 		}
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 		/*
22090Sstevel@tonic-gate 		 * Find potential targets for x-call and x-trap,
22100Sstevel@tonic-gate 		 * if any exist while preempt is disabled we
22110Sstevel@tonic-gate 		 * start a ptl1_panic if requested via a
22120Sstevel@tonic-gate 		 * globals.
22130Sstevel@tonic-gate 		 */
22140Sstevel@tonic-gate 		kpreempt_disable();
22150Sstevel@tonic-gate 		my_cpu_id = CPU->cpu_id;
22160Sstevel@tonic-gate 		other_cpus = cpu_ready_set;
22170Sstevel@tonic-gate 		CPUSET_DEL(other_cpus, CPU->cpu_id);
22180Sstevel@tonic-gate 		target_found = 0;
22190Sstevel@tonic-gate 		if (!CPUSET_ISNULL(other_cpus)) {
22200Sstevel@tonic-gate 			/*
22210Sstevel@tonic-gate 			 * Pick the first one
22220Sstevel@tonic-gate 			 */
22230Sstevel@tonic-gate 			for (cpu_id = 0; cpu_id < NCPU; cpu_id++) {
22240Sstevel@tonic-gate 				if (cpu_id == my_cpu_id)
22250Sstevel@tonic-gate 					continue;
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 				if (CPU_XCALL_READY(cpu_id)) {
22280Sstevel@tonic-gate 					target_cpu_id = cpu_id;
22290Sstevel@tonic-gate 					target_found = 1;
22300Sstevel@tonic-gate 					break;
22310Sstevel@tonic-gate 				}
22320Sstevel@tonic-gate 			}
22330Sstevel@tonic-gate 			ASSERT(target_found);
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 			if (ptl1_panic_xc_one_test) {
22360Sstevel@tonic-gate 				xc_one(target_cpu_id,
22370Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
22380Sstevel@tonic-gate 			}
22390Sstevel@tonic-gate 			if (ptl1_panic_xc_all_test) {
22400Sstevel@tonic-gate 				xc_some(other_cpus,
22410Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
22420Sstevel@tonic-gate 			}
22430Sstevel@tonic-gate 			if (ptl1_panic_xt_one_test) {
22440Sstevel@tonic-gate 				xt_one(target_cpu_id,
22450Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
22460Sstevel@tonic-gate 			}
22470Sstevel@tonic-gate 			if (ptl1_panic_xt_all_test) {
22480Sstevel@tonic-gate 				xt_some(other_cpus,
22490Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
22500Sstevel@tonic-gate 			}
22510Sstevel@tonic-gate 		}
22520Sstevel@tonic-gate 		kpreempt_enable();
22530Sstevel@tonic-gate 		(void) timeout(ptl1_wakeup, NULL, hz);
22540Sstevel@tonic-gate 		(void) cv_wait(&ptl1_cv, &ptl1_mutex);
22550Sstevel@tonic-gate 	}
22560Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
22570Sstevel@tonic-gate }
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate /*
22600Sstevel@tonic-gate  * Called during early startup to create the ptl1_thread
22610Sstevel@tonic-gate  */
22620Sstevel@tonic-gate void
22630Sstevel@tonic-gate init_ptl1_thread(void)
22640Sstevel@tonic-gate {
22650Sstevel@tonic-gate 	ptl1_thread_p = thread_create(NULL, 0, ptl1_thread, NULL, 0,
22660Sstevel@tonic-gate 	    &p0, TS_RUN, 0);
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 
22715648Ssetje static void
22725648Ssetje memlist_new(uint64_t start, uint64_t len, struct memlist **memlistp)
22735648Ssetje {
22745648Ssetje 	struct memlist *new;
22755648Ssetje 
22765648Ssetje 	new = *memlistp;
22775648Ssetje 	new->address = start;
22785648Ssetje 	new->size = len;
22795648Ssetje 	*memlistp = new + 1;
22805648Ssetje }
22815648Ssetje 
22820Sstevel@tonic-gate /*
22830Sstevel@tonic-gate  * Add to a memory list.
22840Sstevel@tonic-gate  * start = start of new memory segment
22850Sstevel@tonic-gate  * len = length of new memory segment in bytes
22860Sstevel@tonic-gate  * memlistp = pointer to array of available memory segment structures
22870Sstevel@tonic-gate  * curmemlistp = memory list to which to add segment.
22880Sstevel@tonic-gate  */
22890Sstevel@tonic-gate static void
22900Sstevel@tonic-gate memlist_add(uint64_t start, uint64_t len, struct memlist **memlistp,
22910Sstevel@tonic-gate 	struct memlist **curmemlistp)
22920Sstevel@tonic-gate {
22935648Ssetje 	struct memlist *new = *memlistp;
22945648Ssetje 
22955648Ssetje 	memlist_new(start, len, memlistp);
22960Sstevel@tonic-gate 	memlist_insert(new, curmemlistp);
22970Sstevel@tonic-gate }
22980Sstevel@tonic-gate 
22995648Ssetje static int
23005648Ssetje ndata_alloc_memseg(struct memlist *ndata, size_t avail)
23015648Ssetje {
23025648Ssetje 	int nseg;
23035648Ssetje 	size_t memseg_sz;
23045648Ssetje 	struct memseg *msp;
23055648Ssetje 
23065648Ssetje 	/*
23075648Ssetje 	 * The memseg list is for the chunks of physical memory that
23085648Ssetje 	 * will be managed by the vm system.  The number calculated is
23095648Ssetje 	 * a guess as boot may fragment it more when memory allocations
23105648Ssetje 	 * are made before kphysm_init().
23115648Ssetje 	 */
23125648Ssetje 	memseg_sz = (avail + 10) * sizeof (struct memseg);
23135648Ssetje 	memseg_sz = roundup(memseg_sz, PAGESIZE);
23145648Ssetje 	nseg = memseg_sz / sizeof (struct memseg);
23155648Ssetje 	msp = ndata_alloc(ndata, memseg_sz, ecache_alignsize);
23165648Ssetje 	if (msp == NULL)
23175648Ssetje 		return (1);
23185648Ssetje 	PRM_DEBUG(memseg_free);
23195648Ssetje 
23205648Ssetje 	while (nseg--) {
23215648Ssetje 		msp->next = memseg_free;
23225648Ssetje 		memseg_free = msp;
23235648Ssetje 		msp++;
23245648Ssetje 	}
23255648Ssetje 	return (0);
23265648Ssetje }
23275648Ssetje 
23280Sstevel@tonic-gate /*
23290Sstevel@tonic-gate  * In the case of architectures that support dynamic addition of
23300Sstevel@tonic-gate  * memory at run-time there are two cases where memsegs need to
23310Sstevel@tonic-gate  * be initialized and added to the memseg list.
23320Sstevel@tonic-gate  * 1) memsegs that are constructed at startup.
23330Sstevel@tonic-gate  * 2) memsegs that are constructed at run-time on
23340Sstevel@tonic-gate  *    hot-plug capable architectures.
23350Sstevel@tonic-gate  * This code was originally part of the function kphysm_init().
23360Sstevel@tonic-gate  */
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate static void
23390Sstevel@tonic-gate memseg_list_add(struct memseg *memsegp)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	struct memseg **prev_memsegp;
23420Sstevel@tonic-gate 	pgcnt_t num;
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	/* insert in memseg list, decreasing number of pages order */
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	num = MSEG_NPAGES(memsegp);
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	for (prev_memsegp = &memsegs; *prev_memsegp;
23490Sstevel@tonic-gate 	    prev_memsegp = &((*prev_memsegp)->next)) {
23500Sstevel@tonic-gate 		if (num > MSEG_NPAGES(*prev_memsegp))
23510Sstevel@tonic-gate 			break;
23520Sstevel@tonic-gate 	}
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	memsegp->next = *prev_memsegp;
23550Sstevel@tonic-gate 	*prev_memsegp = memsegp;
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	if (kpm_enable) {
23580Sstevel@tonic-gate 		memsegp->nextpa = (memsegp->next) ?
23594528Spaulsan 		    va_to_pa(memsegp->next) : MSEG_NULLPTR_PA;
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 		if (prev_memsegp != &memsegs) {
23620Sstevel@tonic-gate 			struct memseg *msp;
23630Sstevel@tonic-gate 			msp = (struct memseg *)((caddr_t)prev_memsegp -
23644528Spaulsan 			    offsetof(struct memseg, next));
23650Sstevel@tonic-gate 			msp->nextpa = va_to_pa(memsegp);
23660Sstevel@tonic-gate 		} else {
23670Sstevel@tonic-gate 			memsegspa = va_to_pa(memsegs);
23680Sstevel@tonic-gate 		}
23690Sstevel@tonic-gate 	}
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate /*
23730Sstevel@tonic-gate  * PSM add_physmem_cb(). US-II and newer processors have some
23740Sstevel@tonic-gate  * flavor of the prefetch capability implemented. We exploit
23750Sstevel@tonic-gate  * this capability for optimum performance.
23760Sstevel@tonic-gate  */
23770Sstevel@tonic-gate #define	PREFETCH_BYTES	64
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate void
23800Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate 	extern void	 prefetch_page_w(void *);
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	pp->p_pagenum = pnum;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	/*
23870Sstevel@tonic-gate 	 * Prefetch one more page_t into E$. To prevent future
23880Sstevel@tonic-gate 	 * mishaps with the sizeof(page_t) changing on us, we
23890Sstevel@tonic-gate 	 * catch this on debug kernels if we can't bring in the
23900Sstevel@tonic-gate 	 * entire hpage with 2 PREFETCH_BYTES reads. See
23910Sstevel@tonic-gate 	 * also, sun4u/cpu/cpu_module.c
23920Sstevel@tonic-gate 	 */
23930Sstevel@tonic-gate 	/*LINTED*/
23940Sstevel@tonic-gate 	ASSERT(sizeof (page_t) <= 2*PREFETCH_BYTES);
23950Sstevel@tonic-gate 	prefetch_page_w((char *)pp);
23960Sstevel@tonic-gate }
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate /*
23995648Ssetje  * Find memseg with given pfn
24005648Ssetje  */
24015648Ssetje static struct memseg *
24025648Ssetje memseg_find(pfn_t base, pfn_t *next)
24035648Ssetje {
24045648Ssetje 	struct memseg *seg;
24055648Ssetje 
24065648Ssetje 	if (next != NULL)
24075648Ssetje 		*next = LONG_MAX;
24085648Ssetje 	for (seg = memsegs; seg != NULL; seg = seg->next) {
24095648Ssetje 		if (base >= seg->pages_base && base < seg->pages_end)
24105648Ssetje 			return (seg);
24115648Ssetje 		if (next != NULL && seg->pages_base > base &&
24125648Ssetje 		    seg->pages_base < *next)
24135648Ssetje 			*next = seg->pages_base;
24145648Ssetje 	}
24155648Ssetje 	return (NULL);
24165648Ssetje }
24175648Ssetje 
24185648Ssetje extern struct vnode prom_ppages;
24195648Ssetje 
24205648Ssetje /*
24215648Ssetje  * Put page allocated by OBP on prom_ppages
24220Sstevel@tonic-gate  */
24230Sstevel@tonic-gate static void
24245648Ssetje kphysm_erase(uint64_t addr, uint64_t len)
24255648Ssetje {
24265648Ssetje 	struct page *pp;
24275648Ssetje 	struct memseg *seg;
24285648Ssetje 	pfn_t base = btop(addr), next;
24295648Ssetje 	pgcnt_t num = btop(len);
24305648Ssetje 
24315648Ssetje 	while (num != 0) {
24325648Ssetje 		pgcnt_t off, left;
24335648Ssetje 
24345648Ssetje 		seg = memseg_find(base, &next);
24355648Ssetje 		if (seg == NULL) {
24365648Ssetje 			if (next == LONG_MAX)
24375648Ssetje 				break;
24385648Ssetje 			left = MIN(next - base, num);
24395648Ssetje 			base += left, num -= left;
24405648Ssetje 			continue;
24415648Ssetje 		}
24425648Ssetje 		off = base - seg->pages_base;
24435648Ssetje 		pp = seg->pages + off;
24445648Ssetje 		left = num - MIN(num, (seg->pages_end - seg->pages_base) - off);
24455648Ssetje 		while (num != left) {
24465648Ssetje 			/*
24475648Ssetje 			 * init it, lock it, and hashin on prom_pages vp.
24485648Ssetje 			 *
24495648Ssetje 			 * XXX	vnode offsets on the prom_ppages vnode
24505648Ssetje 			 *	are page numbers (gack) for >32 bit
24515648Ssetje 			 *	physical memory machines.
24525648Ssetje 			 */
24535648Ssetje 			add_physmem_cb(pp, base);
24545648Ssetje 			if (page_trylock(pp, SE_EXCL) == 0)
24555648Ssetje 				cmn_err(CE_PANIC, "prom page locked");
24565648Ssetje 			(void) page_hashin(pp, &prom_ppages,
24575648Ssetje 			    (offset_t)base, NULL);
24585648Ssetje 			(void) page_pp_lock(pp, 0, 1);
24595648Ssetje 			pp++, base++, num--;
24605648Ssetje 		}
24615648Ssetje 	}
24625648Ssetje }
24635648Ssetje 
24645648Ssetje static page_t *ppnext;
24655648Ssetje static pgcnt_t ppleft;
24665648Ssetje 
24675648Ssetje static void *kpm_ppnext;
24685648Ssetje static pgcnt_t kpm_ppleft;
24695648Ssetje 
24705648Ssetje /*
24715648Ssetje  * Create a memseg
24725648Ssetje  */
24735648Ssetje static void
24745648Ssetje kphysm_memseg(uint64_t addr, uint64_t len)
24750Sstevel@tonic-gate {
24765648Ssetje 	pfn_t base = btop(addr);
24775648Ssetje 	pgcnt_t num = btop(len);
24785648Ssetje 	struct memseg *seg;
24795648Ssetje 
24805648Ssetje 	seg = memseg_free;
24815648Ssetje 	memseg_free = seg->next;
24825648Ssetje 	ASSERT(seg != NULL);
24835648Ssetje 
24845648Ssetje 	seg->pages = ppnext;
24855648Ssetje 	seg->epages = ppnext + num;
24865648Ssetje 	seg->pages_base = base;
24875648Ssetje 	seg->pages_end = base + num;
24885648Ssetje 	ppnext += num;
24895648Ssetje 	ppleft -= num;
24905648Ssetje 
24915648Ssetje 	if (kpm_enable) {
24925648Ssetje 		pgcnt_t kpnum = ptokpmpr(num);
24935648Ssetje 
24945648Ssetje 		if (kpnum > kpm_ppleft)
24955648Ssetje 			panic("kphysm_memseg: kpm_pp overflow");
24965648Ssetje 		seg->pagespa = va_to_pa(seg->pages);
24975648Ssetje 		seg->epagespa = va_to_pa(seg->epages);
24985648Ssetje 		seg->kpm_pbase = kpmptop(ptokpmp(base));
24995648Ssetje 		seg->kpm_nkpmpgs = kpnum;
25005648Ssetje 		/*
25015648Ssetje 		 * In the kpm_smallpage case, the kpm array
25025648Ssetje 		 * is 1-1 wrt the page array
25035648Ssetje 		 */
25045648Ssetje 		if (kpm_smallpages) {
25055648Ssetje 			kpm_spage_t *kpm_pp = kpm_ppnext;
25065648Ssetje 
25075648Ssetje 			kpm_ppnext = kpm_pp + kpnum;
25085648Ssetje 			seg->kpm_spages = kpm_pp;
25095648Ssetje 			seg->kpm_pagespa = va_to_pa(seg->kpm_spages);
25105648Ssetje 		} else {
25115648Ssetje 			kpm_page_t *kpm_pp = kpm_ppnext;
25125648Ssetje 
25135648Ssetje 			kpm_ppnext = kpm_pp + kpnum;
25145648Ssetje 			seg->kpm_pages = kpm_pp;
25155648Ssetje 			seg->kpm_pagespa = va_to_pa(seg->kpm_pages);
25165648Ssetje 			/* ASSERT no kpm overlaps */
25175648Ssetje 			ASSERT(
25185648Ssetje 			    memseg_find(base - pmodkpmp(base), NULL) == NULL);
25195648Ssetje 			ASSERT(memseg_find(
25205648Ssetje 			    roundup(base + num, kpmpnpgs) - 1, NULL) == NULL);
25215648Ssetje 		}
25225648Ssetje 		kpm_ppleft -= num;
25235648Ssetje 	}
25245648Ssetje 
25255648Ssetje 	memseg_list_add(seg);
25265648Ssetje }
25275648Ssetje 
25285648Ssetje /*
25295648Ssetje  * Add range to free list
25305648Ssetje  */
25315648Ssetje void
25325648Ssetje kphysm_add(uint64_t addr, uint64_t len, int reclaim)
25335648Ssetje {
25345648Ssetje 	struct page *pp;
25355648Ssetje 	struct memseg *seg;
25365648Ssetje 	pfn_t base = btop(addr);
25375648Ssetje 	pgcnt_t num = btop(len);
25385648Ssetje 
25395648Ssetje 	seg = memseg_find(base, NULL);
25405648Ssetje 	ASSERT(seg != NULL);
25415648Ssetje 	pp = seg->pages + (base - seg->pages_base);
25425648Ssetje 
25435648Ssetje 	if (reclaim) {
25445648Ssetje 		struct page *rpp = pp;
25455648Ssetje 		struct page *lpp = pp + num;
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 		/*
25485648Ssetje 		 * page should be locked on prom_ppages
25495648Ssetje 		 * unhash and unlock it
25500Sstevel@tonic-gate 		 */
25515648Ssetje 		while (rpp < lpp) {
25525648Ssetje 			ASSERT(PAGE_EXCL(rpp) && rpp->p_vnode == &prom_ppages);
25535648Ssetje 			page_pp_unlock(rpp, 0, 1);
25545648Ssetje 			page_hashout(rpp, NULL);
25555648Ssetje 			page_unlock(rpp);
25565648Ssetje 			rpp++;
25570Sstevel@tonic-gate 		}
25580Sstevel@tonic-gate 	}
25590Sstevel@tonic-gate 
25605648Ssetje 	/*
25615648Ssetje 	 * add_physmem() initializes the PSM part of the page
25625648Ssetje 	 * struct by calling the PSM back with add_physmem_cb().
25635648Ssetje 	 * In addition it coalesces pages into larger pages as
25645648Ssetje 	 * it initializes them.
25655648Ssetje 	 */
25665648Ssetje 	add_physmem(pp, num, base);
25675648Ssetje }
25685648Ssetje 
25695648Ssetje /*
25705648Ssetje  * kphysm_init() tackles the problem of initializing physical memory.
25715648Ssetje  */
25725648Ssetje static void
25735648Ssetje kphysm_init(void)
25745648Ssetje {
25755648Ssetje 	struct memlist *pmem;
25765648Ssetje 
25775648Ssetje 	ASSERT(page_hash != NULL && page_hashsz != 0);
25785648Ssetje 
25795648Ssetje 	ppnext = pp_base;
25805648Ssetje 	ppleft = npages;
25815648Ssetje 	kpm_ppnext = kpm_pp_base;
25825648Ssetje 	kpm_ppleft = kpm_npages;
25835648Ssetje 
25845648Ssetje 	/*
25855648Ssetje 	 * installed pages not on nopp_memlist go in memseg list
25865648Ssetje 	 */
25875648Ssetje 	diff_memlists(phys_install, nopp_list, kphysm_memseg);
25885648Ssetje 
25895648Ssetje 	/*
25905648Ssetje 	 * Free the avail list
25915648Ssetje 	 */
25925648Ssetje 	for (pmem = phys_avail; pmem != NULL; pmem = pmem->next)
25935648Ssetje 		kphysm_add(pmem->address, pmem->size, 0);
25945648Ssetje 
25955648Ssetje 	/*
25965648Ssetje 	 * Erase pages that aren't available
25975648Ssetje 	 */
25985648Ssetje 	diff_memlists(phys_install, phys_avail, kphysm_erase);
25995648Ssetje 
26000Sstevel@tonic-gate 	build_pfn_hash();
26010Sstevel@tonic-gate }
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate /*
26040Sstevel@tonic-gate  * Kernel VM initialization.
26050Sstevel@tonic-gate  * Assumptions about kernel address space ordering:
26060Sstevel@tonic-gate  *	(1) gap (user space)
26070Sstevel@tonic-gate  *	(2) kernel text
26080Sstevel@tonic-gate  *	(3) kernel data/bss
26090Sstevel@tonic-gate  *	(4) gap
26100Sstevel@tonic-gate  *	(5) kernel data structures
26110Sstevel@tonic-gate  *	(6) gap
26120Sstevel@tonic-gate  *	(7) debugger (optional)
26130Sstevel@tonic-gate  *	(8) monitor
26140Sstevel@tonic-gate  *	(9) gap (possibly null)
26150Sstevel@tonic-gate  *	(10) dvma
26160Sstevel@tonic-gate  *	(11) devices
26170Sstevel@tonic-gate  */
26180Sstevel@tonic-gate static void
26190Sstevel@tonic-gate kvm_init(void)
26200Sstevel@tonic-gate {
26210Sstevel@tonic-gate 	/*
26220Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
26230Sstevel@tonic-gate 	 */
26240Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
26250Sstevel@tonic-gate 	as_avlinit(&kas);
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)KERNELBASE,
26280Sstevel@tonic-gate 	    (size_t)(e_moddata - KERNELBASE), &ktextseg);
26290Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)(KERNELBASE + MMU_PAGESIZE4M),
26320Sstevel@tonic-gate 	    (size_t)(MMU_PAGESIZE4M), &ktexthole);
26330Sstevel@tonic-gate 	(void) segkmem_create(&ktexthole);
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base,
26360Sstevel@tonic-gate 	    (size_t)(econtig32 - valloc_base), &kvalloc);
26370Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	if (kmem64_base) {
26404528Spaulsan 		(void) seg_attach(&kas, (caddr_t)kmem64_base,
26414528Spaulsan 		    (size_t)(kmem64_end - kmem64_base), &kmem64);
26424528Spaulsan 		(void) segkmem_create(&kmem64);
26430Sstevel@tonic-gate 	}
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	/*
26460Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
26470Sstevel@tonic-gate 	 * system resource management transition. We can no longer
26480Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
26490Sstevel@tonic-gate 	 */
26500Sstevel@tonic-gate 	(void) seg_attach(&kas, kernelheap, ekernelheap - kernelheap, &kvseg);
26510Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
26520Sstevel@tonic-gate 	hblk_alloc_dynamic = 1;
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	/*
26550Sstevel@tonic-gate 	 * we need to preallocate pages for DR operations before enabling large
26560Sstevel@tonic-gate 	 * page kernel heap because of memseg_remap_init() hat_unload() hack.
26570Sstevel@tonic-gate 	 */
26580Sstevel@tonic-gate 	memseg_remap_init();
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	/* at this point we are ready to use large page heap */
26610Sstevel@tonic-gate 	segkmem_heap_lp_init();
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SYSBASE32, SYSLIMIT32 - SYSBASE32,
26640Sstevel@tonic-gate 	    &kvseg32);
26650Sstevel@tonic-gate 	(void) segkmem_create(&kvseg32);
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	/*
26680Sstevel@tonic-gate 	 * Create a segment for the debugger.
26690Sstevel@tonic-gate 	 */
26703446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
26710Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
26740Sstevel@tonic-gate }
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate char obp_tte_str[] =
26770Sstevel@tonic-gate 	"h# %x constant MMU_PAGESHIFT "
26780Sstevel@tonic-gate 	"h# %x constant TTE8K "
26790Sstevel@tonic-gate 	"h# %x constant SFHME_SIZE "
26800Sstevel@tonic-gate 	"h# %x constant SFHME_TTE "
26810Sstevel@tonic-gate 	"h# %x constant HMEBLK_TAG "
26820Sstevel@tonic-gate 	"h# %x constant HMEBLK_NEXT "
26830Sstevel@tonic-gate 	"h# %x constant HMEBLK_MISC "
26840Sstevel@tonic-gate 	"h# %x constant HMEBLK_HME1 "
26850Sstevel@tonic-gate 	"h# %x constant NHMENTS "
26860Sstevel@tonic-gate 	"h# %x constant HBLK_SZMASK "
26870Sstevel@tonic-gate 	"h# %x constant HBLK_RANGE_SHIFT "
26880Sstevel@tonic-gate 	"h# %x constant HMEBP_HBLK "
26890Sstevel@tonic-gate 	"h# %x constant HMEBUCKET_SIZE "
26900Sstevel@tonic-gate 	"h# %x constant HTAG_SFMMUPSZ "
26914528Spaulsan 	"h# %x constant HTAG_BSPAGE_SHIFT "
26924528Spaulsan 	"h# %x constant HTAG_REHASH_SHIFT "
26934528Spaulsan 	"h# %x constant SFMMU_INVALID_SHMERID "
26940Sstevel@tonic-gate 	"h# %x constant mmu_hashcnt "
26950Sstevel@tonic-gate 	"h# %p constant uhme_hash "
26960Sstevel@tonic-gate 	"h# %p constant khme_hash "
26970Sstevel@tonic-gate 	"h# %x constant UHMEHASH_SZ "
26980Sstevel@tonic-gate 	"h# %x constant KHMEHASH_SZ "
26992241Shuah 	"h# %p constant KCONTEXT "
27000Sstevel@tonic-gate 	"h# %p constant KHATID "
27010Sstevel@tonic-gate 	"h# %x constant ASI_MEM "
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 	": PHYS-X@ ( phys -- data ) "
27040Sstevel@tonic-gate 	"   ASI_MEM spacex@ "
27050Sstevel@tonic-gate 	"; "
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	": PHYS-W@ ( phys -- data ) "
27080Sstevel@tonic-gate 	"   ASI_MEM spacew@ "
27090Sstevel@tonic-gate 	"; "
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	": PHYS-L@ ( phys -- data ) "
27120Sstevel@tonic-gate 	"   ASI_MEM spaceL@ "
27130Sstevel@tonic-gate 	"; "
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 	": TTE_PAGE_SHIFT ( ttesz -- hmeshift ) "
27160Sstevel@tonic-gate 	"   3 * MMU_PAGESHIFT + "
27170Sstevel@tonic-gate 	"; "
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	": TTE_IS_VALID ( ttep -- flag ) "
27200Sstevel@tonic-gate 	"   PHYS-X@ 0< "
27210Sstevel@tonic-gate 	"; "
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	": HME_HASH_SHIFT ( ttesz -- hmeshift ) "
27240Sstevel@tonic-gate 	"   dup TTE8K =  if "
27250Sstevel@tonic-gate 	"      drop HBLK_RANGE_SHIFT "
27260Sstevel@tonic-gate 	"   else "
27270Sstevel@tonic-gate 	"      TTE_PAGE_SHIFT "
27280Sstevel@tonic-gate 	"   then "
27290Sstevel@tonic-gate 	"; "
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 	": HME_HASH_BSPAGE ( addr hmeshift -- bspage ) "
27320Sstevel@tonic-gate 	"   tuck >> swap MMU_PAGESHIFT - << "
27330Sstevel@tonic-gate 	"; "
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 	": HME_HASH_FUNCTION ( sfmmup addr hmeshift -- hmebp ) "
27360Sstevel@tonic-gate 	"   >> over xor swap                    ( hash sfmmup ) "
27370Sstevel@tonic-gate 	"   KHATID <>  if                       ( hash ) "
27380Sstevel@tonic-gate 	"      UHMEHASH_SZ and                  ( bucket ) "
27390Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * uhme_hash +     ( hmebp ) "
27400Sstevel@tonic-gate 	"   else                                ( hash ) "
27410Sstevel@tonic-gate 	"      KHMEHASH_SZ and                  ( bucket ) "
27420Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * khme_hash +     ( hmebp ) "
27430Sstevel@tonic-gate 	"   then                                ( hmebp ) "
27440Sstevel@tonic-gate 	"; "
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	": HME_HASH_TABLE_SEARCH "
27470Sstevel@tonic-gate 	"       ( sfmmup hmebp hblktag --  sfmmup null | sfmmup hmeblkp ) "
27480Sstevel@tonic-gate 	"   >r hmebp_hblk + phys-x@ begin ( sfmmup hmeblkp ) ( r: hblktag ) "
27490Sstevel@tonic-gate 	"      dup if   		( sfmmup hmeblkp ) ( r: hblktag ) "
27500Sstevel@tonic-gate 	"         dup hmeblk_tag + phys-x@ r@ = if ( sfmmup hmeblkp )	  "
27510Sstevel@tonic-gate 	"	     dup hmeblk_tag + 8 + phys-x@ 2 pick = if		  "
27520Sstevel@tonic-gate 	"		  true 	( sfmmup hmeblkp true ) ( r: hblktag )	  "
27530Sstevel@tonic-gate 	"	     else						  "
27540Sstevel@tonic-gate 	"	     	  hmeblk_next + phys-x@ false 			  "
27550Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
27560Sstevel@tonic-gate 	"	     then  						  "
27570Sstevel@tonic-gate 	"	  else							  "
27580Sstevel@tonic-gate 	"	     hmeblk_next + phys-x@ false 			  "
27590Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
27600Sstevel@tonic-gate 	"	  then 							  "
27610Sstevel@tonic-gate 	"      else							  "
27620Sstevel@tonic-gate 	"         true 							  "
27630Sstevel@tonic-gate 	"      then  							  "
27640Sstevel@tonic-gate 	"   until r> drop 						  "
27650Sstevel@tonic-gate 	"; "
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	": HME_HASH_TAG ( sfmmup rehash addr -- hblktag ) "
27684528Spaulsan 	"   over HME_HASH_SHIFT HME_HASH_BSPAGE  ( sfmmup rehash bspage ) "
27694528Spaulsan 	"   HTAG_BSPAGE_SHIFT <<		 ( sfmmup rehash htag-bspage )"
27704528Spaulsan 	"   swap HTAG_REHASH_SHIFT << or	 ( sfmmup htag-bspage-rehash )"
27714528Spaulsan 	"   SFMMU_INVALID_SHMERID or nip	 ( hblktag ) "
27720Sstevel@tonic-gate 	"; "
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	": HBLK_TO_TTEP ( hmeblkp addr -- ttep ) "
27750Sstevel@tonic-gate 	"   over HMEBLK_MISC + PHYS-L@ HBLK_SZMASK and  ( hmeblkp addr ttesz ) "
27760Sstevel@tonic-gate 	"   TTE8K =  if                            ( hmeblkp addr ) "
27770Sstevel@tonic-gate 	"      MMU_PAGESHIFT >> NHMENTS 1- and     ( hmeblkp hme-index ) "
27780Sstevel@tonic-gate 	"   else                                   ( hmeblkp addr ) "
27790Sstevel@tonic-gate 	"      drop 0                              ( hmeblkp 0 ) "
27800Sstevel@tonic-gate 	"   then                                   ( hmeblkp hme-index ) "
27810Sstevel@tonic-gate 	"   SFHME_SIZE * + HMEBLK_HME1 +           ( hmep ) "
27820Sstevel@tonic-gate 	"   SFHME_TTE +                            ( ttep ) "
27830Sstevel@tonic-gate 	"; "
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	": unix-tte ( addr cnum -- false | tte-data true ) "
27862241Shuah 	"    KCONTEXT = if                   ( addr ) "
27872241Shuah 	"	KHATID                       ( addr khatid ) "
27882241Shuah 	"    else                            ( addr ) "
27892241Shuah 	"       drop false exit              ( false ) "
27902241Shuah 	"    then "
27912241Shuah 	"      ( addr khatid ) "
27920Sstevel@tonic-gate 	"      mmu_hashcnt 1+ 1  do           ( addr sfmmup ) "
27930Sstevel@tonic-gate 	"         2dup swap i HME_HASH_SHIFT  "
27940Sstevel@tonic-gate 					"( addr sfmmup sfmmup addr hmeshift ) "
27950Sstevel@tonic-gate 	"         HME_HASH_FUNCTION           ( addr sfmmup hmebp ) "
27960Sstevel@tonic-gate 	"         over i 4 pick               "
27970Sstevel@tonic-gate 				"( addr sfmmup hmebp sfmmup rehash addr ) "
27980Sstevel@tonic-gate 	"         HME_HASH_TAG                ( addr sfmmup hmebp hblktag ) "
27990Sstevel@tonic-gate 	"         HME_HASH_TABLE_SEARCH       "
28000Sstevel@tonic-gate 					"( addr sfmmup { null | hmeblkp } ) "
28010Sstevel@tonic-gate 	"         ?dup  if                    ( addr sfmmup hmeblkp ) "
28020Sstevel@tonic-gate 	"            nip swap HBLK_TO_TTEP    ( ttep ) "
28030Sstevel@tonic-gate 	"            dup TTE_IS_VALID  if     ( valid-ttep ) "
28040Sstevel@tonic-gate 	"               PHYS-X@ true          ( tte-data true ) "
28050Sstevel@tonic-gate 	"            else                     ( invalid-tte ) "
28060Sstevel@tonic-gate 	"               drop false            ( false ) "
28070Sstevel@tonic-gate 	"            then                     ( false | tte-data true ) "
28080Sstevel@tonic-gate 	"            unloop exit              ( false | tte-data true ) "
28090Sstevel@tonic-gate 	"         then                        ( addr sfmmup ) "
28100Sstevel@tonic-gate 	"      loop                           ( addr sfmmup ) "
28110Sstevel@tonic-gate 	"      2drop false                    ( false ) "
28120Sstevel@tonic-gate 	"; "
28130Sstevel@tonic-gate ;
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate void
28160Sstevel@tonic-gate create_va_to_tte(void)
28170Sstevel@tonic-gate {
28180Sstevel@tonic-gate 	char *bp;
28190Sstevel@tonic-gate 	extern int khmehash_num, uhmehash_num;
28200Sstevel@tonic-gate 	extern struct hmehash_bucket *khme_hash, *uhme_hash;
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate #define	OFFSET(type, field)	((uintptr_t)(&((type *)0)->field))
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 	bp = (char *)kobj_zalloc(MMU_PAGESIZE, KM_SLEEP);
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	/*
28270Sstevel@tonic-gate 	 * Teach obp how to parse our sw ttes.
28280Sstevel@tonic-gate 	 */
28290Sstevel@tonic-gate 	(void) sprintf(bp, obp_tte_str,
28300Sstevel@tonic-gate 	    MMU_PAGESHIFT,
28310Sstevel@tonic-gate 	    TTE8K,
28320Sstevel@tonic-gate 	    sizeof (struct sf_hment),
28330Sstevel@tonic-gate 	    OFFSET(struct sf_hment, hme_tte),
28340Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_tag),
28350Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_nextpa),
28360Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_misc),
28370Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_hme),
28380Sstevel@tonic-gate 	    NHMENTS,
28390Sstevel@tonic-gate 	    HBLK_SZMASK,
28400Sstevel@tonic-gate 	    HBLK_RANGE_SHIFT,
28410Sstevel@tonic-gate 	    OFFSET(struct hmehash_bucket, hmeh_nextpa),
28420Sstevel@tonic-gate 	    sizeof (struct hmehash_bucket),
28430Sstevel@tonic-gate 	    HTAG_SFMMUPSZ,
28444528Spaulsan 	    HTAG_BSPAGE_SHIFT,
28454528Spaulsan 	    HTAG_REHASH_SHIFT,
28464528Spaulsan 	    SFMMU_INVALID_SHMERID,
28470Sstevel@tonic-gate 	    mmu_hashcnt,
28480Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)uhme_hash),
28490Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)khme_hash),
28500Sstevel@tonic-gate 	    UHMEHASH_SZ,
28510Sstevel@tonic-gate 	    KHMEHASH_SZ,
28522241Shuah 	    KCONTEXT,
28530Sstevel@tonic-gate 	    KHATID,
28540Sstevel@tonic-gate 	    ASI_MEM);
28550Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	kobj_free(bp, MMU_PAGESIZE);
28580Sstevel@tonic-gate }
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate void
28610Sstevel@tonic-gate install_va_to_tte(void)
28620Sstevel@tonic-gate {
28630Sstevel@tonic-gate 	/*
28640Sstevel@tonic-gate 	 * advise prom that he can use unix-tte
28650Sstevel@tonic-gate 	 */
28660Sstevel@tonic-gate 	prom_interpret("' unix-tte is va>tte-data", 0, 0, 0, 0, 0);
28670Sstevel@tonic-gate }
28680Sstevel@tonic-gate 
28691253Slq150181 /*
28703928Slq150181  * Here we add "device-type=console" for /os-io node, for currently
28713928Slq150181  * our kernel console output only supports displaying text and
28723928Slq150181  * performing cursor-positioning operations (through kernel framebuffer
28733928Slq150181  * driver) and it doesn't support other functionalities required for a
28743928Slq150181  * standard "display" device as specified in 1275 spec. The main missing
28753928Slq150181  * interface defined by the 1275 spec is "draw-logo".
28763928Slq150181  * also see the comments above prom_stdout_is_framebuffer().
28771253Slq150181  */
28780Sstevel@tonic-gate static char *create_node =
28791492Slq150181 	"\" /\" find-device "
28800Sstevel@tonic-gate 	"new-device "
28810Sstevel@tonic-gate 	"\" os-io\" device-name "
28823928Slq150181 	"\" "OBP_DISPLAY_CONSOLE"\" device-type "
28830Sstevel@tonic-gate 	": cb-r/w  ( adr,len method$ -- #read/#written ) "
28840Sstevel@tonic-gate 	"   2>r swap 2 2r> ['] $callback  catch  if "
28850Sstevel@tonic-gate 	"      2drop 3drop 0 "
28860Sstevel@tonic-gate 	"   then "
28870Sstevel@tonic-gate 	"; "
28880Sstevel@tonic-gate 	": read ( adr,len -- #read ) "
28890Sstevel@tonic-gate 	"       \" read\" ['] cb-r/w catch  if  2drop 2drop -2 exit then "
28900Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
28910Sstevel@tonic-gate 	"       ?dup  if "
28920Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
28930Sstevel@tonic-gate 	"       else "
28940Sstevel@tonic-gate 	"               -2 "
28951492Slq150181 	"       then "
28960Sstevel@tonic-gate 	";    "
28970Sstevel@tonic-gate 	": write ( adr,len -- #written ) "
28980Sstevel@tonic-gate 	"       \" write\" ['] cb-r/w catch  if  2drop 2drop 0 exit  then "
28990Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
29000Sstevel@tonic-gate 	"       ?dup  if "
29010Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
29020Sstevel@tonic-gate 	"        else "
29030Sstevel@tonic-gate 	"               0 "
29040Sstevel@tonic-gate 	"       then "
29050Sstevel@tonic-gate 	"; "
29060Sstevel@tonic-gate 	": poll-tty ( -- ) ; "
29070Sstevel@tonic-gate 	": install-abort  ( -- )  ['] poll-tty d# 10 alarm ; "
29080Sstevel@tonic-gate 	": remove-abort ( -- )  ['] poll-tty 0 alarm ; "
29090Sstevel@tonic-gate 	": cb-give/take ( $method -- ) "
29100Sstevel@tonic-gate 	"       0 -rot ['] $callback catch  ?dup  if "
29110Sstevel@tonic-gate 	"               >r 2drop 2drop r> throw "
29120Sstevel@tonic-gate 	"       else "
29130Sstevel@tonic-gate 	"               0  ?do  drop  loop "
29140Sstevel@tonic-gate 	"       then "
29150Sstevel@tonic-gate 	"; "
29160Sstevel@tonic-gate 	": give ( -- )  \" exit-input\" cb-give/take ; "
29170Sstevel@tonic-gate 	": take ( -- )  \" enter-input\" cb-give/take ; "
29180Sstevel@tonic-gate 	": open ( -- ok? )  true ; "
29190Sstevel@tonic-gate 	": close ( -- ) ; "
29200Sstevel@tonic-gate 	"finish-device "
29210Sstevel@tonic-gate 	"device-end ";
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate /*
29241253Slq150181  * Create the OBP input/output node (FCode serial driver).
29251253Slq150181  * It is needed for both USB console keyboard and for
29261253Slq150181  * the kernel terminal emulator.  It is too early to check for a
29271253Slq150181  * kernel console compatible framebuffer now, so we create this
29281253Slq150181  * so that we're ready if we need to enable kernel terminal emulation.
29290Sstevel@tonic-gate  *
29301253Slq150181  * When the USB software takes over the input device at the time
29311253Slq150181  * consconfig runs, OBP's stdin is redirected to this node.
29321253Slq150181  * Whenever the FORTH user interface is used after this switch,
29331253Slq150181  * the node will call back into the kernel for console input.
29341253Slq150181  * If a serial device such as ttya or a UART with a Type 5 keyboard
29351253Slq150181  * attached is used, OBP takes over the serial device when the system
29361253Slq150181  * goes to the debugger after the system is booted.  This sharing
29371253Slq150181  * of the relatively simple serial device is difficult but possible.
29381253Slq150181  * Sharing the USB host controller is impossible due its complexity.
29391253Slq150181  *
29401253Slq150181  * Similarly to USB keyboard input redirection, after consconfig_dacf
29411253Slq150181  * configures a kernel console framebuffer as the standard output
29421253Slq150181  * device, OBP's stdout is switched to to vector through the
29431253Slq150181  * /os-io node into the kernel terminal emulator.
29440Sstevel@tonic-gate  */
29450Sstevel@tonic-gate static void
29461253Slq150181 startup_create_io_node(void)
29470Sstevel@tonic-gate {
29481253Slq150181 	prom_interpret(create_node, 0, 0, 0, 0, 0);
29490Sstevel@tonic-gate }
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate static void
29530Sstevel@tonic-gate do_prom_version_check(void)
29540Sstevel@tonic-gate {
29550Sstevel@tonic-gate 	int i;
2956789Sahrens 	pnode_t node;
29570Sstevel@tonic-gate 	char buf[64];
29580Sstevel@tonic-gate 	static char drev[] = "Down-rev firmware detected%s\n"
29594528Spaulsan 	    "\tPlease upgrade to the following minimum version:\n"
29604528Spaulsan 	    "\t\t%s\n";
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 	i = prom_version_check(buf, sizeof (buf), &node);
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 	if (i == PROM_VER64_OK)
29650Sstevel@tonic-gate 		return;
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	if (i == PROM_VER64_UPGRADE) {
29680Sstevel@tonic-gate 		cmn_err(CE_WARN, drev, "", buf);
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate #ifdef	DEBUG
29710Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to continue */
29720Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware\n");
29730Sstevel@tonic-gate 		return;
29740Sstevel@tonic-gate #else
29750Sstevel@tonic-gate 		halt(0);
29760Sstevel@tonic-gate #endif
29770Sstevel@tonic-gate 	}
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 	/*
29800Sstevel@tonic-gate 	 * The other possibility is that this is a server running
29810Sstevel@tonic-gate 	 * good firmware, but down-rev firmware was detected on at
29820Sstevel@tonic-gate 	 * least one other cpu board. We just complain if we see
29830Sstevel@tonic-gate 	 * that.
29840Sstevel@tonic-gate 	 */
29850Sstevel@tonic-gate 	cmn_err(CE_WARN, drev, " on one or more CPU boards", buf);
29860Sstevel@tonic-gate }
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate /*
29900Sstevel@tonic-gate  * Must be defined in platform dependent code.
29910Sstevel@tonic-gate  */
29920Sstevel@tonic-gate extern caddr_t modtext;
29930Sstevel@tonic-gate extern size_t modtext_sz;
29940Sstevel@tonic-gate extern caddr_t moddata;
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate #define	HEAPTEXT_ARENA(addr)	\
29970Sstevel@tonic-gate 	((uintptr_t)(addr) < KERNELBASE + 2 * MMU_PAGESIZE4M ? 0 : \
29980Sstevel@tonic-gate 	(((uintptr_t)(addr) - HEAPTEXT_BASE) / \
29990Sstevel@tonic-gate 	(HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) + 1))
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate #define	HEAPTEXT_OVERSIZED(addr)	\
30020Sstevel@tonic-gate 	((uintptr_t)(addr) >= HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE)
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate vmem_t *texthole_source[HEAPTEXT_NARENAS];
30050Sstevel@tonic-gate vmem_t *texthole_arena[HEAPTEXT_NARENAS];
30060Sstevel@tonic-gate kmutex_t texthole_lock;
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate void
30110Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
30120Sstevel@tonic-gate {
30130Sstevel@tonic-gate 	uintptr_t addr, limit;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	addr = HEAPTEXT_BASE;
30160Sstevel@tonic-gate 	limit = addr + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE;
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate 	/*
30190Sstevel@tonic-gate 	 * Before we initialize the text_arena, we want to punch holes in the
30200Sstevel@tonic-gate 	 * underlying heaptext_arena.  This guarantees that for any text
30210Sstevel@tonic-gate 	 * address we can find a text hole less than HEAPTEXT_MAPPED away.
30220Sstevel@tonic-gate 	 */
30230Sstevel@tonic-gate 	for (; addr + HEAPTEXT_UNMAPPED <= limit;
30240Sstevel@tonic-gate 	    addr += HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) {
30250Sstevel@tonic-gate 		(void) vmem_xalloc(heaptext_arena, HEAPTEXT_UNMAPPED, PAGESIZE,
30260Sstevel@tonic-gate 		    0, 0, (void *)addr, (void *)(addr + HEAPTEXT_UNMAPPED),
30270Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	/*
30310Sstevel@tonic-gate 	 * Allocate one page at the oversize to break up the text region
30320Sstevel@tonic-gate 	 * from the oversized region.
30330Sstevel@tonic-gate 	 */
30340Sstevel@tonic-gate 	(void) vmem_xalloc(heaptext_arena, PAGESIZE, PAGESIZE, 0, 0,
30350Sstevel@tonic-gate 	    (void *)limit, (void *)(limit + PAGESIZE),
30360Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
30370Sstevel@tonic-gate 
30383791Skchow 	*text_arena = vmem_create("module_text", modtext_sz ? modtext : NULL,
30393791Skchow 	    modtext_sz, sizeof (uintptr_t), segkmem_alloc, segkmem_free,
30400Sstevel@tonic-gate 	    heaptext_arena, 0, VM_SLEEP);
30410Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", moddata, MODDATA, 1,
30420Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
30430Sstevel@tonic-gate }
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate caddr_t
30460Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
30470Sstevel@tonic-gate {
30480Sstevel@tonic-gate 	caddr_t rval, better;
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	/*
30510Sstevel@tonic-gate 	 * First, try a sleeping allocation.
30520Sstevel@tonic-gate 	 */
30530Sstevel@tonic-gate 	rval = vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT);
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	if (size >= HEAPTEXT_MAPPED || !HEAPTEXT_OVERSIZED(rval))
30560Sstevel@tonic-gate 		return (rval);
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 	/*
30590Sstevel@tonic-gate 	 * We didn't get the area that we wanted.  We're going to try to do an
30600Sstevel@tonic-gate 	 * allocation with explicit constraints.
30610Sstevel@tonic-gate 	 */
30620Sstevel@tonic-gate 	better = vmem_xalloc(arena, size, sizeof (uintptr_t), 0, 0, NULL,
30630Sstevel@tonic-gate 	    (void *)(HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE),
30640Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT);
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	if (better != NULL) {
30670Sstevel@tonic-gate 		/*
30680Sstevel@tonic-gate 		 * That worked.  Free our first attempt and return.
30690Sstevel@tonic-gate 		 */
30700Sstevel@tonic-gate 		vmem_free(arena, rval, size);
30710Sstevel@tonic-gate 		return (better);
30720Sstevel@tonic-gate 	}
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	/*
30750Sstevel@tonic-gate 	 * That didn't work; we'll have to return our first attempt.
30760Sstevel@tonic-gate 	 */
30770Sstevel@tonic-gate 	return (rval);
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate caddr_t
30810Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
30820Sstevel@tonic-gate {
30830Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
30840Sstevel@tonic-gate 	char c[30];
30850Sstevel@tonic-gate 	uintptr_t base;
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 	if (HEAPTEXT_OVERSIZED(addr)) {
30880Sstevel@tonic-gate 		/*
30890Sstevel@tonic-gate 		 * If this is an oversized allocation, there is no text hole
30900Sstevel@tonic-gate 		 * available for it; return NULL.
30910Sstevel@tonic-gate 		 */
30920Sstevel@tonic-gate 		return (NULL);
30930Sstevel@tonic-gate 	}
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 	mutex_enter(&texthole_lock);
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	if (texthole_arena[arena] == NULL) {
30980Sstevel@tonic-gate 		ASSERT(texthole_source[arena] == NULL);
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 		if (arena == 0) {
31010Sstevel@tonic-gate 			texthole_source[0] = vmem_create("module_text_holesrc",
31020Sstevel@tonic-gate 			    (void *)(KERNELBASE + MMU_PAGESIZE4M),
31030Sstevel@tonic-gate 			    MMU_PAGESIZE4M, PAGESIZE, NULL, NULL, NULL,
31040Sstevel@tonic-gate 			    0, VM_SLEEP);
31050Sstevel@tonic-gate 		} else {
31060Sstevel@tonic-gate 			base = HEAPTEXT_BASE +
31070Sstevel@tonic-gate 			    (arena - 1) * (HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED);
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c),
31100Sstevel@tonic-gate 			    "heaptext_holesrc_%d", arena);
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 			texthole_source[arena] = vmem_create(c, (void *)base,
31130Sstevel@tonic-gate 			    HEAPTEXT_UNMAPPED, PAGESIZE, NULL, NULL, NULL,
31140Sstevel@tonic-gate 			    0, VM_SLEEP);
31150Sstevel@tonic-gate 		}
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 		(void) snprintf(c, sizeof (c), "heaptext_hole_%d", arena);
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 		texthole_arena[arena] = vmem_create(c, NULL, 0,
31200Sstevel@tonic-gate 		    sizeof (uint32_t), segkmem_alloc_permanent, segkmem_free,
31210Sstevel@tonic-gate 		    texthole_source[arena], 0, VM_SLEEP);
31220Sstevel@tonic-gate 	}
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 	mutex_exit(&texthole_lock);
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
31270Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
31280Sstevel@tonic-gate 	return (vmem_alloc(texthole_arena[arena], size,
31290Sstevel@tonic-gate 	    VM_BESTFIT | VM_NOSLEEP));
31300Sstevel@tonic-gate }
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate void
31330Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
31340Sstevel@tonic-gate {
31350Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
31380Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
31390Sstevel@tonic-gate 	vmem_free(texthole_arena[arena], addr, size);
31400Sstevel@tonic-gate }
3141