xref: /titanic_52/usr/src/uts/sun4/os/startup.c (revision 1e45ea5a7676dbeffaf69f37a1280c94eb19a422)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fea9cb91Slq150181  * Common Development and Distribution License (the "License").
6100b72f4Sandrei  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fea9cb91Slq150181 
227c478bd9Sstevel@tonic-gate /*
23ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
307c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
317c478bd9Sstevel@tonic-gate #include <sys/vm.h>
327c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
337c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
347c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
357c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
367c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/memlist_plat.h>
387c478bd9Sstevel@tonic-gate #include <sys/memlist_impl.h>
397c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
407c478bd9Sstevel@tonic-gate #include <sys/prom_isa.h>
417c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
427c478bd9Sstevel@tonic-gate #include <sys/intreg.h>
437c478bd9Sstevel@tonic-gate #include <sys/ivintr.h>
447c478bd9Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/iommutsb.h>
467c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
477c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
487c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
497c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
507c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
517c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
537c478bd9Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
547c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
557c478bd9Sstevel@tonic-gate #include <sys/sun4asi.h>
567c478bd9Sstevel@tonic-gate #include <sys/clconf.h>
577c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
587c478bd9Sstevel@tonic-gate #include <sys/panic.h>
597c478bd9Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
607c478bd9Sstevel@tonic-gate #include <sys/clock.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/promif.h>
637c478bd9Sstevel@tonic-gate #include <sys/prom_debug.h>
647c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
657c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
667c478bd9Sstevel@tonic-gate #include <sys/mem_cage.h>
671e2e7a75Shuah #include <sys/mmu.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate extern void setup_trap_table(void);
70982b9107Sjb145095 extern int cpu_intrq_setup(struct cpu *);
717c478bd9Sstevel@tonic-gate extern void cpu_intrq_register(struct cpu *);
727c478bd9Sstevel@tonic-gate extern void contig_mem_init(void);
737c478bd9Sstevel@tonic-gate extern void mach_dump_buffer_init(void);
747c478bd9Sstevel@tonic-gate extern void mach_descrip_init(void);
751ae08745Sheppo extern void mach_descrip_startup_fini(void);
767c478bd9Sstevel@tonic-gate extern void mach_memscrub(void);
777c478bd9Sstevel@tonic-gate extern void mach_fpras(void);
787c478bd9Sstevel@tonic-gate extern void mach_cpu_halt_idle(void);
797c478bd9Sstevel@tonic-gate extern void mach_hw_copy_limit(void);
801ae08745Sheppo extern void load_mach_drivers(void);
817c478bd9Sstevel@tonic-gate extern void load_tod_module(void);
827c478bd9Sstevel@tonic-gate #pragma weak load_tod_module
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate extern int ndata_alloc_mmfsa(struct memlist *ndata);
857c478bd9Sstevel@tonic-gate #pragma weak ndata_alloc_mmfsa
867c478bd9Sstevel@tonic-gate 
871ae08745Sheppo extern void cif_init(void);
881ae08745Sheppo #pragma weak cif_init
891ae08745Sheppo 
907c478bd9Sstevel@tonic-gate extern void parse_idprom(void);
917c478bd9Sstevel@tonic-gate extern void add_vx_handler(char *, int, void (*)(cell_t *));
927c478bd9Sstevel@tonic-gate extern void mem_config_init(void);
937c478bd9Sstevel@tonic-gate extern void memseg_remap_init(void);
947c478bd9Sstevel@tonic-gate 
95fedab560Sae112802 extern void mach_kpm_init(void);
96fedab560Sae112802 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * External Data:
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate extern int vac_size;	/* cache size in bytes */
1017c478bd9Sstevel@tonic-gate extern uint_t vac_mask;	/* VAC alignment consistency mask */
1027c478bd9Sstevel@tonic-gate extern uint_t vac_colors;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Global Data Definitions:
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * XXX - Don't port this to new architectures
1107c478bd9Sstevel@tonic-gate  * A 3rd party volume manager driver (vxdm) depends on the symbol romp.
1117c478bd9Sstevel@tonic-gate  * 'romp' has no use with a prom with an IEEE 1275 client interface.
1127c478bd9Sstevel@tonic-gate  * The driver doesn't use the value, but it depends on the symbol.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate void *romp;		/* veritas driver won't load without romp 4154976 */
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1197c478bd9Sstevel@tonic-gate pgcnt_t segkpsize =
1207c478bd9Sstevel@tonic-gate     btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
1217c478bd9Sstevel@tonic-gate uint_t segmap_percent = 12; /* Size of segmap segment */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int use_cache = 1;		/* cache not reliable (605 bugs) with MP */
1247c478bd9Sstevel@tonic-gate int vac_copyback = 1;
1257c478bd9Sstevel@tonic-gate char *cache_mode = NULL;
1267c478bd9Sstevel@tonic-gate int use_mix = 1;
1277c478bd9Sstevel@tonic-gate int prom_debug = 0;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate struct bootops *bootops = 0;	/* passed in from boot in %o2 */
1307c478bd9Sstevel@tonic-gate caddr_t boot_tba;		/* %tba at boot - used by kmdb */
1317c478bd9Sstevel@tonic-gate uint_t	tba_taken_over = 0;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate caddr_t s_text;			/* start of kernel text segment */
1347c478bd9Sstevel@tonic-gate caddr_t e_text;			/* end of kernel text segment */
1357c478bd9Sstevel@tonic-gate caddr_t s_data;			/* start of kernel data segment */
1367c478bd9Sstevel@tonic-gate caddr_t e_data;			/* end of kernel data segment */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate caddr_t modtext;		/* beginning of module text */
1397c478bd9Sstevel@tonic-gate size_t	modtext_sz;		/* size of module text */
1407c478bd9Sstevel@tonic-gate caddr_t moddata;		/* beginning of module data reserve */
1417c478bd9Sstevel@tonic-gate caddr_t e_moddata;		/* end of module data reserve */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate  * End of first block of contiguous kernel in 32-bit virtual address space
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate caddr_t		econtig32;	/* end of first blk of contiguous kernel */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate caddr_t		ncbase;		/* beginning of non-cached segment */
1497c478bd9Sstevel@tonic-gate caddr_t		ncend;		/* end of non-cached segment */
1507c478bd9Sstevel@tonic-gate caddr_t		sdata;		/* beginning of data segment */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate caddr_t		extra_etva;	/* beginning of unused nucleus text */
1537c478bd9Sstevel@tonic-gate pgcnt_t		extra_etpg;	/* number of pages of unused nucleus text */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate size_t	ndata_remain_sz;	/* bytes from end of data to 4MB boundary */
1567c478bd9Sstevel@tonic-gate caddr_t	nalloc_base;		/* beginning of nucleus allocation */
1577c478bd9Sstevel@tonic-gate caddr_t nalloc_end;		/* end of nucleus allocatable memory */
1587c478bd9Sstevel@tonic-gate caddr_t valloc_base;		/* beginning of kvalloc segment	*/
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate caddr_t kmem64_base;		/* base of kernel mem segment in 64-bit space */
1617c478bd9Sstevel@tonic-gate caddr_t kmem64_end;		/* end of kernel mem segment in 64-bit space */
162bb121940Sdp78419 caddr_t kmem64_aligned_end;	/* end of large page, overmaps 64-bit space */
163bb121940Sdp78419 int	kmem64_alignsize;	/* page size for mem segment in 64-bit space */
164bb121940Sdp78419 int	kmem64_szc;		/* page size code */
165bb121940Sdp78419 uint64_t kmem64_pabase = (uint64_t)-1;	/* physical address of kmem64_base */
1667c478bd9Sstevel@tonic-gate 
167fedab560Sae112802 uintptr_t shm_alignment;	/* VAC address consistency modulus */
1687c478bd9Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
1697c478bd9Sstevel@tonic-gate struct memlist *phys_avail;	/* Available (unreserved) physical memory */
1707c478bd9Sstevel@tonic-gate struct memlist *virt_avail;	/* Available (unmapped?) virtual memory */
1717c478bd9Sstevel@tonic-gate struct memlist ndata;		/* memlist of nucleus allocatable memory */
1727c478bd9Sstevel@tonic-gate int memexp_flag;		/* memory expansion card flag */
1737c478bd9Sstevel@tonic-gate uint64_t ecache_flushaddr;	/* physical address used for flushing E$ */
1747c478bd9Sstevel@tonic-gate pgcnt_t obp_pages;		/* Physical pages used by OBP */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * VM data structures
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
1807c478bd9Sstevel@tonic-gate struct page *pp_base;		/* Base of system page struct array */
1817c478bd9Sstevel@tonic-gate size_t pp_sz;			/* Size in bytes of page struct array */
1827c478bd9Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
1837c478bd9Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
1847c478bd9Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
1857c478bd9Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
1867c478bd9Sstevel@tonic-gate struct seg ktexthole;		/* Segment used for nucleus text hole */
1877c478bd9Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
1887c478bd9Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
1897c478bd9Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate uintptr_t kpm_pp_base;		/* Base of system kpm_page array */
1927c478bd9Sstevel@tonic-gate size_t	kpm_pp_sz;		/* Size of system kpm_page array */
1937c478bd9Sstevel@tonic-gate pgcnt_t	kpm_npages;		/* How many kpm pages are managed */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
1967c478bd9Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
1977c478bd9Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
1987c478bd9Sstevel@tonic-gate 
199ad23a2dbSjohansen int segzio_fromheap = 0;	/* zio allocations occur from heap */
200ad23a2dbSjohansen caddr_t segzio_base;		/* Base address of segzio */
201ad23a2dbSjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
202ad23a2dbSjohansen 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * debugger pages (if allocated)
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate struct vnode kdebugvp;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
209ae115bc7Smrj  * VA range available to the debugger
210ae115bc7Smrj  */
211ae115bc7Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
212ae115bc7Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
213ae115bc7Smrj 
214ae115bc7Smrj /*
2157c478bd9Sstevel@tonic-gate  * Segment for relocated kernel structures in 64-bit large RAM kernels
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate struct seg kmem64;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate struct memseg *memseg_base;
2207c478bd9Sstevel@tonic-gate size_t memseg_sz;		/* Used to translate a va to page */
2217c478bd9Sstevel@tonic-gate struct vnode unused_pages_vp;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * VM data structures allocated early during boot.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate size_t pagehash_sz;
2277c478bd9Sstevel@tonic-gate uint64_t memlist_sz;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate char tbr_wr_addr_inited = 0;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * Static Routines:
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static void memlist_add(uint64_t, uint64_t, struct memlist **,
2367c478bd9Sstevel@tonic-gate 	struct memlist **);
2377c478bd9Sstevel@tonic-gate static void kphysm_init(page_t *, struct memseg *, pgcnt_t, uintptr_t,
2387c478bd9Sstevel@tonic-gate 	pgcnt_t);
2397c478bd9Sstevel@tonic-gate static void kvm_init(void);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static void startup_init(void);
2427c478bd9Sstevel@tonic-gate static void startup_memlist(void);
2437c478bd9Sstevel@tonic-gate static void startup_modules(void);
2447c478bd9Sstevel@tonic-gate static void startup_bop_gone(void);
2457c478bd9Sstevel@tonic-gate static void startup_vm(void);
2467c478bd9Sstevel@tonic-gate static void startup_end(void);
2477c478bd9Sstevel@tonic-gate static void setup_cage_params(void);
248fea9cb91Slq150181 static void startup_create_io_node(void);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static pgcnt_t npages;
2517c478bd9Sstevel@tonic-gate static struct memlist *memlist;
2527c478bd9Sstevel@tonic-gate void *memlist_end;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static pgcnt_t bop_alloc_pages;
2557c478bd9Sstevel@tonic-gate static caddr_t hblk_base;
2567c478bd9Sstevel@tonic-gate uint_t hblk_alloc_dynamic = 0;
2577c478bd9Sstevel@tonic-gate uint_t hblk1_min = H1MIN;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Hooks for unsupported platforms and down-rev firmware
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate int iam_positron(void);
2647c478bd9Sstevel@tonic-gate #pragma weak iam_positron
2657c478bd9Sstevel@tonic-gate static void do_prom_version_check(void);
2667c478bd9Sstevel@tonic-gate static void kpm_init(void);
2677c478bd9Sstevel@tonic-gate static void kpm_npages_setup(int);
2687c478bd9Sstevel@tonic-gate static void kpm_memseg_init(void);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * After receiving a thermal interrupt, this is the number of seconds
2727c478bd9Sstevel@tonic-gate  * to delay before shutting off the system, assuming
2737c478bd9Sstevel@tonic-gate  * shutdown fails.  Use /etc/system to change the delay if this isn't
2747c478bd9Sstevel@tonic-gate  * large enough.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate int thermal_powerdown_delay = 1200;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Used to hold off page relocations into the cage until OBP has completed
2807c478bd9Sstevel@tonic-gate  * its boot-time handoff of its resources to the kernel.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate int page_relocate_ready = 0;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate #ifdef  DEBUGGING_MEM
2887c478bd9Sstevel@tonic-gate static int debugging_mem;
2897c478bd9Sstevel@tonic-gate static void
2907c478bd9Sstevel@tonic-gate printmemlist(char *title, struct memlist *list)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	if (!debugging_mem)
2937c478bd9Sstevel@tonic-gate 		return;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	printf("%s\n", title);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	while (list) {
2987c478bd9Sstevel@tonic-gate 		prom_printf("\taddr = 0x%x %8x, size = 0x%x %8x\n",
2997c478bd9Sstevel@tonic-gate 		    (uint32_t)(list->address >> 32), (uint32_t)list->address,
3007c478bd9Sstevel@tonic-gate 		    (uint32_t)(list->size >> 32), (uint32_t)(list->size));
3017c478bd9Sstevel@tonic-gate 		list = list->next;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate void
3067c478bd9Sstevel@tonic-gate printmemseg(struct memseg *memseg)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	if (!debugging_mem)
3097c478bd9Sstevel@tonic-gate 		return;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	printf("memseg\n");
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	while (memseg) {
3147c478bd9Sstevel@tonic-gate 		prom_printf("\tpage = 0x%p, epage = 0x%p, "
3157c478bd9Sstevel@tonic-gate 		    "pfn = 0x%x, epfn = 0x%x\n",
3167c478bd9Sstevel@tonic-gate 		    memseg->pages, memseg->epages,
3177c478bd9Sstevel@tonic-gate 		    memseg->pages_base, memseg->pages_end);
3187c478bd9Sstevel@tonic-gate 		memseg = memseg->next;
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #define	debug_pause(str)	halt((str))
3237c478bd9Sstevel@tonic-gate #define	MPRINTF(str)		if (debugging_mem) prom_printf((str))
3247c478bd9Sstevel@tonic-gate #define	MPRINTF1(str, a)	if (debugging_mem) prom_printf((str), (a))
3257c478bd9Sstevel@tonic-gate #define	MPRINTF2(str, a, b)	if (debugging_mem) prom_printf((str), (a), (b))
3267c478bd9Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c) \
3277c478bd9Sstevel@tonic-gate 	if (debugging_mem) prom_printf((str), (a), (b), (c))
3287c478bd9Sstevel@tonic-gate #else	/* DEBUGGING_MEM */
3297c478bd9Sstevel@tonic-gate #define	MPRINTF(str)
3307c478bd9Sstevel@tonic-gate #define	MPRINTF1(str, a)
3317c478bd9Sstevel@tonic-gate #define	MPRINTF2(str, a, b)
3327c478bd9Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c)
3337c478bd9Sstevel@tonic-gate #endif	/* DEBUGGING_MEM */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /* Simple message to indicate that the bootops pointer has been zeroed */
3367c478bd9Sstevel@tonic-gate #ifdef DEBUG
3377c478bd9Sstevel@tonic-gate static int bootops_gone_on = 0;
3387c478bd9Sstevel@tonic-gate #define	BOOTOPS_GONE() \
3397c478bd9Sstevel@tonic-gate 	if (bootops_gone_on) \
3407c478bd9Sstevel@tonic-gate 		prom_printf("The bootops vec is zeroed now!\n");
3417c478bd9Sstevel@tonic-gate #else
3427c478bd9Sstevel@tonic-gate #define	BOOTOPS_GONE()
3437c478bd9Sstevel@tonic-gate #endif /* DEBUG */
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * Monitor pages may not be where this says they are.
3477c478bd9Sstevel@tonic-gate  * and the debugger may not be there either.
3487c478bd9Sstevel@tonic-gate  *
3497c478bd9Sstevel@tonic-gate  * Note that 'pages' here are *physical* pages, which are 8k on sun4u.
3507c478bd9Sstevel@tonic-gate  *
3517c478bd9Sstevel@tonic-gate  *                        Physical memory layout
3527c478bd9Sstevel@tonic-gate  *                     (not necessarily contiguous)
3537c478bd9Sstevel@tonic-gate  *                       (THIS IS SOMEWHAT WRONG)
3547c478bd9Sstevel@tonic-gate  *                       /-----------------------\
3557c478bd9Sstevel@tonic-gate  *                       |       monitor pages   |
3567c478bd9Sstevel@tonic-gate  *             availmem -|-----------------------|
3577c478bd9Sstevel@tonic-gate  *                       |                       |
3587c478bd9Sstevel@tonic-gate  *                       |       page pool       |
3597c478bd9Sstevel@tonic-gate  *                       |                       |
3607c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3617c478bd9Sstevel@tonic-gate  *                       |   configured tables   |
3627c478bd9Sstevel@tonic-gate  *                       |       buffers         |
3637c478bd9Sstevel@tonic-gate  *            firstaddr -|-----------------------|
3647c478bd9Sstevel@tonic-gate  *                       |   hat data structures |
3657c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3667c478bd9Sstevel@tonic-gate  *                       |    kernel data, bss   |
3677c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3687c478bd9Sstevel@tonic-gate  *                       |    interrupt stack    |
3697c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3707c478bd9Sstevel@tonic-gate  *                       |    kernel text (RO)   |
3717c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3727c478bd9Sstevel@tonic-gate  *                       |    trap table (4k)    |
3737c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3747c478bd9Sstevel@tonic-gate  *               page 1  |      panicbuf         |
3757c478bd9Sstevel@tonic-gate  *                       |-----------------------|
3767c478bd9Sstevel@tonic-gate  *               page 0  |       reclaimed       |
3777c478bd9Sstevel@tonic-gate  *                       |_______________________|
3787c478bd9Sstevel@tonic-gate  *
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  *
3817c478bd9Sstevel@tonic-gate  *                    Kernel's Virtual Memory Layout.
3827c478bd9Sstevel@tonic-gate  *                       /-----------------------\
3837c478bd9Sstevel@tonic-gate  * 0xFFFFFFFF.FFFFFFFF  -|                       |-
3847c478bd9Sstevel@tonic-gate  *                       |   OBP's virtual page  |
3857c478bd9Sstevel@tonic-gate  *                       |        tables         |
3867c478bd9Sstevel@tonic-gate  * 0xFFFFFFFC.00000000  -|-----------------------|-
3877c478bd9Sstevel@tonic-gate  *                       :                       :
3887c478bd9Sstevel@tonic-gate  *                       :                       :
389ad23a2dbSjohansen  *                      -|-----------------------|-
390ad23a2dbSjohansen  *                       |       segzio          | (base and size vary)
3917c478bd9Sstevel@tonic-gate  * 0xFFFFFE00.00000000  -|-----------------------|-
3927c478bd9Sstevel@tonic-gate  *                       |                       |  Ultrasparc I/II support
3937c478bd9Sstevel@tonic-gate  *                       |    segkpm segment     |  up to 2TB of physical
3947c478bd9Sstevel@tonic-gate  *                       | (64-bit kernel ONLY)  |  memory, VAC has 2 colors
3957c478bd9Sstevel@tonic-gate  *                       |                       |
3967c478bd9Sstevel@tonic-gate  * 0xFFFFFA00.00000000  -|-----------------------|- 2TB segkpm alignment
3977c478bd9Sstevel@tonic-gate  *                       :                       :
3987c478bd9Sstevel@tonic-gate  *                       :                       :
3997c478bd9Sstevel@tonic-gate  * 0xFFFFF810.00000000  -|-----------------------|- hole_end
4007c478bd9Sstevel@tonic-gate  *                       |                       |      ^
4017c478bd9Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
4027c478bd9Sstevel@tonic-gate  *                       | bug requires an extra |      |
4037c478bd9Sstevel@tonic-gate  *                       | 4 GB of space between |      |
4047c478bd9Sstevel@tonic-gate  *                       |   hole and used RAM   |	|
4057c478bd9Sstevel@tonic-gate  *                       |                       |      |
4067c478bd9Sstevel@tonic-gate  * 0xFFFFF800.00000000  -|-----------------------|-     |
4077c478bd9Sstevel@tonic-gate  *                       |                       |      |
4087c478bd9Sstevel@tonic-gate  *                       | Virtual Address Hole  |   UltraSPARC
4097c478bd9Sstevel@tonic-gate  *                       |  on UltraSPARC I/II   |  I/II * ONLY *
4107c478bd9Sstevel@tonic-gate  *                       |                       |      |
4117c478bd9Sstevel@tonic-gate  * 0x00000800.00000000  -|-----------------------|-     |
4127c478bd9Sstevel@tonic-gate  *                       |                       |      |
4137c478bd9Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
4147c478bd9Sstevel@tonic-gate  *                       | bug requires an extra |      |
4157c478bd9Sstevel@tonic-gate  *                       | 4 GB of space between |      |
4167c478bd9Sstevel@tonic-gate  *                       |   hole and used RAM   |      |
4177c478bd9Sstevel@tonic-gate  *                       |                       |      v
4187c478bd9Sstevel@tonic-gate  * 0x000007FF.00000000  -|-----------------------|- hole_start -----
4197c478bd9Sstevel@tonic-gate  *                       :                       :		   ^
4207c478bd9Sstevel@tonic-gate  *                       :                       :		   |
421bb121940Sdp78419  * 0x00000XXX.XXX00000  -|-----------------------|- kmem64_	   |
422bb121940Sdp78419  *                       | overmapped area       |   alignend_end  |
423bb121940Sdp78419  *                       | (kmem64_alignsize     |		   |
424bb121940Sdp78419  *                       |  boundary)            |		   |
4257c478bd9Sstevel@tonic-gate  * 0x00000XXX.XXXXXXXX  -|-----------------------|- kmem64_end	   |
4267c478bd9Sstevel@tonic-gate  *                       |                       |		   |
4277c478bd9Sstevel@tonic-gate  *                       |   64-bit kernel ONLY  |		   |
4287c478bd9Sstevel@tonic-gate  *                       |                       |		   |
4297c478bd9Sstevel@tonic-gate  *                       |    kmem64 segment     |		   |
4307c478bd9Sstevel@tonic-gate  *                       |                       |		   |
4317c478bd9Sstevel@tonic-gate  *                       | (Relocated extra HME  |	     Approximately
4327c478bd9Sstevel@tonic-gate  *                       |   block allocations,  |	    1 TB of virtual
4337c478bd9Sstevel@tonic-gate  *                       |   memnode freelists,  |	     address space
4347c478bd9Sstevel@tonic-gate  *                       |    HME hash buckets,  |		   |
4357c478bd9Sstevel@tonic-gate  *                       | mml_table, kpmp_table,|		   |
4367c478bd9Sstevel@tonic-gate  *                       |  page_t array and     |		   |
4377c478bd9Sstevel@tonic-gate  *                       |  hashblock pool to    |		   |
4387c478bd9Sstevel@tonic-gate  *                       |   avoid hard-coded    |		   |
4397c478bd9Sstevel@tonic-gate  *                       |     32-bit vaddr      |		   |
4407c478bd9Sstevel@tonic-gate  *                       |     limitations)      |		   |
4417c478bd9Sstevel@tonic-gate  *                       |                       |		   v
4427c478bd9Sstevel@tonic-gate  * 0x00000700.00000000  -|-----------------------|- SYSLIMIT (kmem64_base)
4437c478bd9Sstevel@tonic-gate  *                       |                       |
4447c478bd9Sstevel@tonic-gate  *                       |  segkmem segment      | (SYSLIMIT - SYSBASE = 4TB)
4457c478bd9Sstevel@tonic-gate  *                       |                       |
4467c478bd9Sstevel@tonic-gate  * 0x00000300.00000000  -|-----------------------|- SYSBASE
4477c478bd9Sstevel@tonic-gate  *                       :                       :
4487c478bd9Sstevel@tonic-gate  *                       :                       :
4497c478bd9Sstevel@tonic-gate  *                      -|-----------------------|-
4507c478bd9Sstevel@tonic-gate  *                       |                       |
4517c478bd9Sstevel@tonic-gate  *                       |  segmap segment       |   SEGMAPSIZE (1/8th physmem,
4527c478bd9Sstevel@tonic-gate  *                       |                       |               256G MAX)
4537c478bd9Sstevel@tonic-gate  * 0x000002a7.50000000  -|-----------------------|- SEGMAPBASE
4547c478bd9Sstevel@tonic-gate  *                       :                       :
4557c478bd9Sstevel@tonic-gate  *                       :                       :
4567c478bd9Sstevel@tonic-gate  *                      -|-----------------------|-
4577c478bd9Sstevel@tonic-gate  *                       |                       |
4587c478bd9Sstevel@tonic-gate  *                       |       segkp           |    SEGKPSIZE (2GB)
4597c478bd9Sstevel@tonic-gate  *                       |                       |
4607c478bd9Sstevel@tonic-gate  *                       |                       |
4617c478bd9Sstevel@tonic-gate  * 0x000002a1.00000000  -|-----------------------|- SEGKPBASE
4627c478bd9Sstevel@tonic-gate  *                       |                       |
4637c478bd9Sstevel@tonic-gate  * 0x000002a0.00000000  -|-----------------------|- MEMSCRUBBASE
4647c478bd9Sstevel@tonic-gate  *                       |                       |       (SEGKPBASE - 0x400000)
4657c478bd9Sstevel@tonic-gate  * 0x0000029F.FFE00000  -|-----------------------|- ARGSBASE
4667c478bd9Sstevel@tonic-gate  *                       |                       |       (MEMSCRUBBASE - NCARGS)
4677c478bd9Sstevel@tonic-gate  * 0x0000029F.FFD80000  -|-----------------------|- PPMAPBASE
4687c478bd9Sstevel@tonic-gate  *                       |                       |       (ARGSBASE - PPMAPSIZE)
4697c478bd9Sstevel@tonic-gate  * 0x0000029F.FFD00000  -|-----------------------|- PPMAP_FAST_BASE
4707c478bd9Sstevel@tonic-gate  *                       |                       |
4717c478bd9Sstevel@tonic-gate  * 0x0000029F.FF980000  -|-----------------------|- PIOMAPBASE
4727c478bd9Sstevel@tonic-gate  *                       |                       |
4737c478bd9Sstevel@tonic-gate  * 0x0000029F.FF580000  -|-----------------------|- NARG_BASE
4747c478bd9Sstevel@tonic-gate  *                       :                       :
4757c478bd9Sstevel@tonic-gate  *                       :                       :
4767c478bd9Sstevel@tonic-gate  * 0x00000000.FFFFFFFF  -|-----------------------|- OFW_END_ADDR
4777c478bd9Sstevel@tonic-gate  *                       |                       |
4787c478bd9Sstevel@tonic-gate  *                       |         OBP           |
4797c478bd9Sstevel@tonic-gate  *                       |                       |
4807c478bd9Sstevel@tonic-gate  * 0x00000000.F0000000  -|-----------------------|- OFW_START_ADDR
4817c478bd9Sstevel@tonic-gate  *                       |         kmdb          |
4827c478bd9Sstevel@tonic-gate  * 0x00000000.EDD00000  -|-----------------------|- SEGDEBUGBASE
4837c478bd9Sstevel@tonic-gate  *                       :                       :
4847c478bd9Sstevel@tonic-gate  *                       :                       :
4857c478bd9Sstevel@tonic-gate  * 0x00000000.7c000000  -|-----------------------|- SYSLIMIT32
4867c478bd9Sstevel@tonic-gate  *                       |                       |
4877c478bd9Sstevel@tonic-gate  *                       |  segkmem32 segment    | (SYSLIMIT32 - SYSBASE32 =
4887c478bd9Sstevel@tonic-gate  *                       |                       |    ~64MB)
4897c478bd9Sstevel@tonic-gate  * 0x00000000.78002000  -|-----------------------|
4907c478bd9Sstevel@tonic-gate  *                       |     panicbuf          |
4917c478bd9Sstevel@tonic-gate  * 0x00000000.78000000  -|-----------------------|- SYSBASE32
4927c478bd9Sstevel@tonic-gate  *                       :                       :
4937c478bd9Sstevel@tonic-gate  *                       :                       :
4947c478bd9Sstevel@tonic-gate  *                       |                       |
4957c478bd9Sstevel@tonic-gate  *                       |-----------------------|- econtig32
4967c478bd9Sstevel@tonic-gate  *                       |    vm structures      |
4977c478bd9Sstevel@tonic-gate  * 0x00000000.01C00000   |-----------------------|- nalloc_end
4987c478bd9Sstevel@tonic-gate  *                       |         TSBs          |
4997c478bd9Sstevel@tonic-gate  *                       |-----------------------|- end/nalloc_base
5007c478bd9Sstevel@tonic-gate  *                       |   kernel data & bss   |
5017c478bd9Sstevel@tonic-gate  * 0x00000000.01800000  -|-----------------------|
5027c478bd9Sstevel@tonic-gate  *                       :   nucleus text hole   :
5037c478bd9Sstevel@tonic-gate  * 0x00000000.01400000  -|-----------------------|
5047c478bd9Sstevel@tonic-gate  *                       :                       :
5057c478bd9Sstevel@tonic-gate  *                       |-----------------------|
5067c478bd9Sstevel@tonic-gate  *                       |      module text      |
5077c478bd9Sstevel@tonic-gate  *                       |-----------------------|- e_text/modtext
5087c478bd9Sstevel@tonic-gate  *                       |      kernel text      |
5097c478bd9Sstevel@tonic-gate  *                       |-----------------------|
5107c478bd9Sstevel@tonic-gate  *                       |    trap table (48k)   |
5117c478bd9Sstevel@tonic-gate  * 0x00000000.01000000  -|-----------------------|- KERNELBASE
5127c478bd9Sstevel@tonic-gate  *                       | reserved for trapstat |} TSTAT_TOTAL_SIZE
5137c478bd9Sstevel@tonic-gate  *                       |-----------------------|
5147c478bd9Sstevel@tonic-gate  *                       |                       |
5157c478bd9Sstevel@tonic-gate  *                       |        invalid        |
5167c478bd9Sstevel@tonic-gate  *                       |                       |
5177c478bd9Sstevel@tonic-gate  * 0x00000000.00000000  _|_______________________|
5187c478bd9Sstevel@tonic-gate  *
5197c478bd9Sstevel@tonic-gate  *
5207c478bd9Sstevel@tonic-gate  *
5217c478bd9Sstevel@tonic-gate  *                   32-bit User Virtual Memory Layout.
5227c478bd9Sstevel@tonic-gate  *                       /-----------------------\
5237c478bd9Sstevel@tonic-gate  *                       |                       |
5247c478bd9Sstevel@tonic-gate  *                       |        invalid        |
5257c478bd9Sstevel@tonic-gate  *                       |                       |
5267c478bd9Sstevel@tonic-gate  *          0xFFC00000  -|-----------------------|- USERLIMIT
5277c478bd9Sstevel@tonic-gate  *                       |       user stack      |
5287c478bd9Sstevel@tonic-gate  *                       :                       :
5297c478bd9Sstevel@tonic-gate  *                       :                       :
5307c478bd9Sstevel@tonic-gate  *                       :                       :
5317c478bd9Sstevel@tonic-gate  *                       |       user data       |
5327c478bd9Sstevel@tonic-gate  *                      -|-----------------------|-
5337c478bd9Sstevel@tonic-gate  *                       |       user text       |
5347c478bd9Sstevel@tonic-gate  *          0x00002000  -|-----------------------|-
5357c478bd9Sstevel@tonic-gate  *                       |       invalid         |
5367c478bd9Sstevel@tonic-gate  *          0x00000000  _|_______________________|
5377c478bd9Sstevel@tonic-gate  *
5387c478bd9Sstevel@tonic-gate  *
5397c478bd9Sstevel@tonic-gate  *
5407c478bd9Sstevel@tonic-gate  *                   64-bit User Virtual Memory Layout.
5417c478bd9Sstevel@tonic-gate  *                       /-----------------------\
5427c478bd9Sstevel@tonic-gate  *                       |                       |
5437c478bd9Sstevel@tonic-gate  *                       |        invalid        |
5447c478bd9Sstevel@tonic-gate  *                       |                       |
5457c478bd9Sstevel@tonic-gate  *  0xFFFFFFFF.80000000 -|-----------------------|- USERLIMIT
5467c478bd9Sstevel@tonic-gate  *                       |       user stack      |
5477c478bd9Sstevel@tonic-gate  *                       :                       :
5487c478bd9Sstevel@tonic-gate  *                       :                       :
5497c478bd9Sstevel@tonic-gate  *                       :                       :
5507c478bd9Sstevel@tonic-gate  *                       |       user data       |
5517c478bd9Sstevel@tonic-gate  *                      -|-----------------------|-
5527c478bd9Sstevel@tonic-gate  *                       |       user text       |
5537c478bd9Sstevel@tonic-gate  *  0x00000000.00100000 -|-----------------------|-
5547c478bd9Sstevel@tonic-gate  *                       |       invalid         |
5557c478bd9Sstevel@tonic-gate  *  0x00000000.00000000 _|_______________________|
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate extern caddr_t ecache_init_scrub_flush_area(caddr_t alloc_base);
5597c478bd9Sstevel@tonic-gate extern uint64_t ecache_flush_address(void);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate #pragma weak load_platform_modules
56225cf1a30Sjl139090 #pragma weak plat_startup_memlist
5637c478bd9Sstevel@tonic-gate #pragma weak ecache_init_scrub_flush_area
5647c478bd9Sstevel@tonic-gate #pragma weak ecache_flush_address
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * By default the DR Cage is enabled for maximum OS
5697c478bd9Sstevel@tonic-gate  * MPSS performance.  Users needing to disable the cage mechanism
5707c478bd9Sstevel@tonic-gate  * can set this variable to zero via /etc/system.
5717c478bd9Sstevel@tonic-gate  * Disabling the cage on systems supporting Dynamic Reconfiguration (DR)
5727c478bd9Sstevel@tonic-gate  * will result in loss of DR functionality.
5737c478bd9Sstevel@tonic-gate  * Platforms wishing to disable kernel Cage by default
5747c478bd9Sstevel@tonic-gate  * should do so in their set_platform_defaults() routine.
5757c478bd9Sstevel@tonic-gate  */
5767c478bd9Sstevel@tonic-gate int	kernel_cage_enable = 1;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate static void
5797c478bd9Sstevel@tonic-gate setup_cage_params(void)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	void (*func)(void);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	func = (void (*)(void))kobj_getsymvalue("set_platform_cage_params", 0);
5847c478bd9Sstevel@tonic-gate 	if (func != NULL) {
5857c478bd9Sstevel@tonic-gate 		(*func)();
5867c478bd9Sstevel@tonic-gate 		return;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (kernel_cage_enable == 0) {
5907c478bd9Sstevel@tonic-gate 		return;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	kcage_range_lock();
5937c478bd9Sstevel@tonic-gate 	if (kcage_range_init(phys_avail, 1) == 0) {
5947c478bd9Sstevel@tonic-gate 		kcage_init(total_pages / 256);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	kcage_range_unlock();
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (kcage_on) {
5997c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is ENABLED");
6007c478bd9Sstevel@tonic-gate 	} else {
6017c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is DISABLED");
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate  * Machine-dependent startup code
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate void
6107c478bd9Sstevel@tonic-gate startup(void)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	startup_init();
6137c478bd9Sstevel@tonic-gate 	if (&startup_platform)
6147c478bd9Sstevel@tonic-gate 		startup_platform();
6157c478bd9Sstevel@tonic-gate 	startup_memlist();
6167c478bd9Sstevel@tonic-gate 	startup_modules();
6177c478bd9Sstevel@tonic-gate 	setup_cage_params();
6187c478bd9Sstevel@tonic-gate 	startup_bop_gone();
6197c478bd9Sstevel@tonic-gate 	startup_vm();
6207c478bd9Sstevel@tonic-gate 	startup_end();
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate struct regs sync_reg_buf;
6247c478bd9Sstevel@tonic-gate uint64_t sync_tt;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate void
6277c478bd9Sstevel@tonic-gate sync_handler(void)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	struct  trap_info 	ti;
6307c478bd9Sstevel@tonic-gate 	int i;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * Prevent trying to talk to the other CPUs since they are
6347c478bd9Sstevel@tonic-gate 	 * sitting in the prom and won't reply.
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
6377c478bd9Sstevel@tonic-gate 		if ((i != CPU->cpu_id) && CPU_XCALL_READY(i)) {
6387c478bd9Sstevel@tonic-gate 			cpu[i]->cpu_flags &= ~CPU_READY;
6397c478bd9Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
6407c478bd9Sstevel@tonic-gate 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * We've managed to get here without going through the
6467c478bd9Sstevel@tonic-gate 	 * normal panic code path. Try and save some useful
6477c478bd9Sstevel@tonic-gate 	 * information.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	if (!panicstr && (curthread->t_panic_trap == NULL)) {
6507c478bd9Sstevel@tonic-gate 		ti.trap_type = sync_tt;
6517c478bd9Sstevel@tonic-gate 		ti.trap_regs = &sync_reg_buf;
6527c478bd9Sstevel@tonic-gate 		ti.trap_addr = NULL;
6537c478bd9Sstevel@tonic-gate 		ti.trap_mmu_fsr = 0x0;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		curthread->t_panic_trap = &ti;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	/*
6597c478bd9Sstevel@tonic-gate 	 * If we're re-entering the panic path, update the signature
6607c478bd9Sstevel@tonic-gate 	 * block so that the SC knows we're in the second part of panic.
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	if (panicstr)
6637c478bd9Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	nopanicdebug = 1; /* do not perform debug_enter() prior to dump */
6667c478bd9Sstevel@tonic-gate 	panic("sync initiated");
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate static void
6717c478bd9Sstevel@tonic-gate startup_init(void)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * We want to save the registers while we're still in OBP
6757c478bd9Sstevel@tonic-gate 	 * so that we know they haven't been fiddled with since.
6767c478bd9Sstevel@tonic-gate 	 * (In principle, OBP can't change them just because it
6777c478bd9Sstevel@tonic-gate 	 * makes a callback, but we'd rather not depend on that
6787c478bd9Sstevel@tonic-gate 	 * behavior.)
6797c478bd9Sstevel@tonic-gate 	 */
6807c478bd9Sstevel@tonic-gate 	char		sync_str[] =
6817c478bd9Sstevel@tonic-gate 		"warning @ warning off : sync "
6827c478bd9Sstevel@tonic-gate 		"%%tl-c %%tstate h# %p x! "
6837c478bd9Sstevel@tonic-gate 		"%%g1 h# %p x! %%g2 h# %p x! %%g3 h# %p x! "
6847c478bd9Sstevel@tonic-gate 		"%%g4 h# %p x! %%g5 h# %p x! %%g6 h# %p x! "
6857c478bd9Sstevel@tonic-gate 		"%%g7 h# %p x! %%o0 h# %p x! %%o1 h# %p x! "
6867c478bd9Sstevel@tonic-gate 		"%%o2 h# %p x! %%o3 h# %p x! %%o4 h# %p x! "
6877c478bd9Sstevel@tonic-gate 		"%%o5 h# %p x! %%o6 h# %p x! %%o7 h# %p x! "
6887c478bd9Sstevel@tonic-gate 		"%%tl-c %%tpc h# %p x! %%tl-c %%tnpc h# %p x! "
6897c478bd9Sstevel@tonic-gate 		"%%y h# %p l! %%tl-c %%tt h# %p x! "
6907c478bd9Sstevel@tonic-gate 		"sync ; warning !";
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	/*
6937c478bd9Sstevel@tonic-gate 	 * 20 == num of %p substrings
6947c478bd9Sstevel@tonic-gate 	 * 16 == max num of chars %p will expand to.
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	char 		bp[sizeof (sync_str) + 16 * 20];
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	(void) check_boot_version(BOP_GETVERSION(bootops));
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Initialize ptl1 stack for the 1st CPU.
7027c478bd9Sstevel@tonic-gate 	 */
7037c478bd9Sstevel@tonic-gate 	ptl1_init_cpu(&cpu0);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * Initialize the address map for cache consistent mappings
7077c478bd9Sstevel@tonic-gate 	 * to random pages; must be done after vac_size is set.
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	ppmapinit();
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	/*
7127c478bd9Sstevel@tonic-gate 	 * Initialize the PROM callback handler.
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	init_vx_handler();
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 * have prom call sync_callback() to handle the sync and
7187c478bd9Sstevel@tonic-gate 	 * save some useful information which will be stored in the
7197c478bd9Sstevel@tonic-gate 	 * core file later.
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	(void) sprintf((char *)bp, sync_str,
7227c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_tstate, (void *)&sync_reg_buf.r_g1,
7237c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g2, (void *)&sync_reg_buf.r_g3,
7247c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g4, (void *)&sync_reg_buf.r_g5,
7257c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_g6, (void *)&sync_reg_buf.r_g7,
7267c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o0, (void *)&sync_reg_buf.r_o1,
7277c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o2, (void *)&sync_reg_buf.r_o3,
7287c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o4, (void *)&sync_reg_buf.r_o5,
7297c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_o6, (void *)&sync_reg_buf.r_o7,
7307c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_pc, (void *)&sync_reg_buf.r_npc,
7317c478bd9Sstevel@tonic-gate 		(void *)&sync_reg_buf.r_y, (void *)&sync_tt);
7327c478bd9Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
7337c478bd9Sstevel@tonic-gate 	add_vx_handler("sync", 1, (void (*)(cell_t *))sync_handler);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static u_longlong_t *boot_physinstalled, *boot_physavail, *boot_virtavail;
7377c478bd9Sstevel@tonic-gate static size_t boot_physinstalled_len, boot_physavail_len, boot_virtavail_len;
7387c478bd9Sstevel@tonic-gate 
739b0fc0e77Sgovinda #define	IVSIZE	((MAXIVNUM * sizeof (intr_vec_t *)) + \
740b0fc0e77Sgovinda 		(MAX_RSVD_IV * sizeof (intr_vec_t)) + \
741b0fc0e77Sgovinda 		(MAX_RSVD_IVX * sizeof (intr_vecx_t)))
7427c478bd9Sstevel@tonic-gate 
743bb121940Sdp78419 #if !defined(C_OBP)
744bb121940Sdp78419 /*
745bb121940Sdp78419  * Install a temporary tte handler in OBP for kmem64 area.
746bb121940Sdp78419  *
747bb121940Sdp78419  * We map kmem64 area with large pages before the trap table is taken
748bb121940Sdp78419  * over. Since OBP makes 8K mappings, it can create 8K tlb entries in
749bb121940Sdp78419  * the same area. Duplicate tlb entries with different page sizes
750bb121940Sdp78419  * cause unpredicatble behavior.  To avoid this, we don't create
751bb121940Sdp78419  * kmem64 mappings via BOP_ALLOC (ends up as prom_alloc() call to
752bb121940Sdp78419  * OBP).  Instead, we manage translations with a temporary va>tte-data
753bb121940Sdp78419  * handler (kmem64-tte).  This handler is replaced by unix-tte when
754bb121940Sdp78419  * the trap table is taken over.
755bb121940Sdp78419  *
756bb121940Sdp78419  * The temporary handler knows the physical address of the kmem64
757bb121940Sdp78419  * area. It uses the prom's pgmap@ Forth word for other addresses.
758bb121940Sdp78419  *
759bb121940Sdp78419  * We have to use BOP_ALLOC() method for C-OBP platforms because
760bb121940Sdp78419  * pgmap@ is not defined in C-OBP. C-OBP is only used on serengeti
761bb121940Sdp78419  * sun4u platforms. On sun4u we flush tlb after trap table is taken
762bb121940Sdp78419  * over if we use large pages for kernel heap and kmem64. Since sun4u
763bb121940Sdp78419  * prom (unlike sun4v) calls va>tte-data first for client address
764bb121940Sdp78419  * translation prom's ttes for kmem64 can't get into TLB even if we
765bb121940Sdp78419  * later switch to prom's trap table again. C-OBP uses 4M pages for
766bb121940Sdp78419  * client mappings when possible so on all platforms we get the
767bb121940Sdp78419  * benefit from large mappings for kmem64 area immediately during
768bb121940Sdp78419  * boot.
769bb121940Sdp78419  *
770bb121940Sdp78419  * pseudo code:
771bb121940Sdp78419  * if (context != 0) {
772bb121940Sdp78419  * 	return false
773bb121940Sdp78419  * } else if (miss_va in range[kmem64_base, kmem64_end)) {
774bb121940Sdp78419  *	tte = tte_template +
775bb121940Sdp78419  *		(((miss_va & pagemask) - kmem64_base));
776bb121940Sdp78419  *	return tte, true
777bb121940Sdp78419  * } else {
778bb121940Sdp78419  *	return pgmap@ result
779bb121940Sdp78419  * }
780bb121940Sdp78419  */
781bb121940Sdp78419 char kmem64_obp_str[] =
782bb121940Sdp78419 	"h# %lx constant kmem64_base "
783bb121940Sdp78419 	"h# %lx constant kmem64_end "
784bb121940Sdp78419 	"h# %lx constant kmem64_pagemask "
785bb121940Sdp78419 	"h# %lx constant kmem64_template "
786bb121940Sdp78419 
787bb121940Sdp78419 	": kmem64-tte ( addr cnum -- false | tte-data true ) "
788bb121940Sdp78419 	"    if                                       ( addr ) "
789bb121940Sdp78419 	"       drop false exit then                  ( false ) "
790bb121940Sdp78419 	"    dup  kmem64_base kmem64_end  within  if  ( addr ) "
791bb121940Sdp78419 	"	kmem64_pagemask and                   ( addr' ) "
792bb121940Sdp78419 	"	kmem64_base -                         ( addr' ) "
793bb121940Sdp78419 	"	kmem64_template +                     ( tte ) "
794bb121940Sdp78419 	"	true                                  ( tte true ) "
795bb121940Sdp78419 	"    else                                     ( addr ) "
796bb121940Sdp78419 	"	pgmap@                                ( tte ) "
797bb121940Sdp78419 	"       dup 0< if true else drop false then   ( tte true  |  false ) "
798bb121940Sdp78419 	"    then                                     ( tte true  |  false ) "
799bb121940Sdp78419 	"; "
800bb121940Sdp78419 
801bb121940Sdp78419 	"' kmem64-tte is va>tte-data "
802bb121940Sdp78419 ;
803bb121940Sdp78419 
804bb121940Sdp78419 void
805bb121940Sdp78419 install_kmem64_tte()
806bb121940Sdp78419 {
807bb121940Sdp78419 	char b[sizeof (kmem64_obp_str) + (4 * 16)];
808bb121940Sdp78419 	tte_t tte;
809bb121940Sdp78419 
810bb121940Sdp78419 	PRM_DEBUG(kmem64_pabase);
811bb121940Sdp78419 	PRM_DEBUG(kmem64_szc);
812bb121940Sdp78419 	sfmmu_memtte(&tte, kmem64_pabase >> MMU_PAGESHIFT,
813bb121940Sdp78419 	    PROC_DATA | HAT_NOSYNC, kmem64_szc);
814bb121940Sdp78419 	PRM_DEBUG(tte.ll);
815bb121940Sdp78419 	(void) sprintf(b, kmem64_obp_str,
816bb121940Sdp78419 	    kmem64_base, kmem64_end, TTE_PAGEMASK(kmem64_szc), tte.ll);
817bb121940Sdp78419 	ASSERT(strlen(b) < sizeof (b));
818bb121940Sdp78419 	prom_interpret(b, 0, 0, 0, 0, 0);
819bb121940Sdp78419 }
820bb121940Sdp78419 #endif	/* !C_OBP */
821bb121940Sdp78419 
8227c478bd9Sstevel@tonic-gate /*
8237c478bd9Sstevel@tonic-gate  * As OBP takes up some RAM when the system boots, pages will already be "lost"
8247c478bd9Sstevel@tonic-gate  * to the system and reflected in npages by the time we see it.
8257c478bd9Sstevel@tonic-gate  *
8267c478bd9Sstevel@tonic-gate  * We only want to allocate kernel structures in the 64-bit virtual address
8277c478bd9Sstevel@tonic-gate  * space on systems with enough RAM to make the overhead of keeping track of
8287c478bd9Sstevel@tonic-gate  * an extra kernel memory segment worthwhile.
8297c478bd9Sstevel@tonic-gate  *
8307c478bd9Sstevel@tonic-gate  * Since OBP has already performed its memory allocations by this point, if we
8317c478bd9Sstevel@tonic-gate  * have more than MINMOVE_RAM_MB MB of RAM left free, go ahead and map
8327c478bd9Sstevel@tonic-gate  * memory in the 64-bit virtual address space; otherwise keep allocations
8337c478bd9Sstevel@tonic-gate  * contiguous with we've mapped so far in the 32-bit virtual address space.
8347c478bd9Sstevel@tonic-gate  */
8357c478bd9Sstevel@tonic-gate #define	MINMOVE_RAM_MB	((size_t)1900)
8367c478bd9Sstevel@tonic-gate #define	MB_TO_BYTES(mb)	((mb) * 1048576ul)
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate pgcnt_t	tune_npages = (pgcnt_t)
8397c478bd9Sstevel@tonic-gate 	(MB_TO_BYTES(MINMOVE_RAM_MB)/ (size_t)MMU_PAGESIZE);
8407c478bd9Sstevel@tonic-gate 
841fe70c9cfSdp78419 #pragma weak page_set_colorequiv_arr_cpu
842fe70c9cfSdp78419 extern void page_set_colorequiv_arr_cpu(void);
843fe70c9cfSdp78419 
8447c478bd9Sstevel@tonic-gate static void
8457c478bd9Sstevel@tonic-gate startup_memlist(void)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	size_t alloc_sz;
8487c478bd9Sstevel@tonic-gate 	size_t ctrs_sz;
8497c478bd9Sstevel@tonic-gate 	caddr_t alloc_base;
8507c478bd9Sstevel@tonic-gate 	caddr_t ctrs_base, ctrs_end;
8517c478bd9Sstevel@tonic-gate 	caddr_t memspace;
8527c478bd9Sstevel@tonic-gate 	caddr_t va;
8537c478bd9Sstevel@tonic-gate 	int memblocks = 0;
8547c478bd9Sstevel@tonic-gate 	struct memlist *cur;
8557c478bd9Sstevel@tonic-gate 	size_t syslimit = (size_t)SYSLIMIT;
8567c478bd9Sstevel@tonic-gate 	size_t sysbase = (size_t)SYSBASE;
857bb121940Sdp78419 	int alloc_alignsize = ecache_alignsize;
858bb121940Sdp78419 	int i;
8597c478bd9Sstevel@tonic-gate 	extern void page_coloring_init(void);
860932dc8e5Sdp78419 	extern void page_set_colorequiv_arr(void);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/*
8637c478bd9Sstevel@tonic-gate 	 * Initialize enough of the system to allow kmem_alloc to work by
8647c478bd9Sstevel@tonic-gate 	 * calling boot to allocate its memory until the time that
8657c478bd9Sstevel@tonic-gate 	 * kvm_init is completed.  The page structs are allocated after
8667c478bd9Sstevel@tonic-gate 	 * rounding up end to the nearest page boundary; the memsegs are
8677c478bd9Sstevel@tonic-gate 	 * initialized and the space they use comes from the kernel heap.
8687c478bd9Sstevel@tonic-gate 	 * With appropriate initialization, they can be reallocated later
8697c478bd9Sstevel@tonic-gate 	 * to a size appropriate for the machine's configuration.
8707c478bd9Sstevel@tonic-gate 	 *
8717c478bd9Sstevel@tonic-gate 	 * At this point, memory is allocated for things that will never
8727c478bd9Sstevel@tonic-gate 	 * need to be freed, this used to be "valloced".  This allows a
8737c478bd9Sstevel@tonic-gate 	 * savings as the pages don't need page structures to describe
8747c478bd9Sstevel@tonic-gate 	 * them because them will not be managed by the vm system.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	/*
8787c478bd9Sstevel@tonic-gate 	 * We're loaded by boot with the following configuration (as
8797c478bd9Sstevel@tonic-gate 	 * specified in the sun4u/conf/Mapfile):
8807c478bd9Sstevel@tonic-gate 	 *
8817c478bd9Sstevel@tonic-gate 	 * 	text:		4 MB chunk aligned on a 4MB boundary
8827c478bd9Sstevel@tonic-gate 	 * 	data & bss:	4 MB chunk aligned on a 4MB boundary
8837c478bd9Sstevel@tonic-gate 	 *
8847c478bd9Sstevel@tonic-gate 	 * These two chunks will eventually be mapped by 2 locked 4MB
8857c478bd9Sstevel@tonic-gate 	 * ttes and will represent the nucleus of the kernel.  This gives
8867c478bd9Sstevel@tonic-gate 	 * us some free space that is already allocated, some or all of
8877c478bd9Sstevel@tonic-gate 	 * which is made available to kernel module text.
8887c478bd9Sstevel@tonic-gate 	 *
8897c478bd9Sstevel@tonic-gate 	 * The free space in the data-bss chunk is used for nucleus
8907c478bd9Sstevel@tonic-gate 	 * allocatable data structures and we reserve it using the
8917c478bd9Sstevel@tonic-gate 	 * nalloc_base and nalloc_end variables.  This space is currently
8927c478bd9Sstevel@tonic-gate 	 * being used for hat data structures required for tlb miss
8937c478bd9Sstevel@tonic-gate 	 * handling operations.  We align nalloc_base to a l2 cache
8947c478bd9Sstevel@tonic-gate 	 * linesize because this is the line size the hardware uses to
8957c478bd9Sstevel@tonic-gate 	 * maintain cache coherency.
8967c478bd9Sstevel@tonic-gate 	 * 256K is carved out for module data.
8977c478bd9Sstevel@tonic-gate 	 */
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	nalloc_base = (caddr_t)roundup((uintptr_t)e_data, MMU_PAGESIZE);
9007c478bd9Sstevel@tonic-gate 	moddata = nalloc_base;
9017c478bd9Sstevel@tonic-gate 	e_moddata = nalloc_base + MODDATA;
9027c478bd9Sstevel@tonic-gate 	nalloc_base = e_moddata;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	nalloc_end = (caddr_t)roundup((uintptr_t)nalloc_base, MMU_PAGESIZE4M);
9057c478bd9Sstevel@tonic-gate 	valloc_base = nalloc_base;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/*
9087c478bd9Sstevel@tonic-gate 	 * Calculate the start of the data segment.
9097c478bd9Sstevel@tonic-gate 	 */
9107c478bd9Sstevel@tonic-gate 	sdata = (caddr_t)((uintptr_t)e_data & MMU_PAGEMASK4M);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9137c478bd9Sstevel@tonic-gate 	PRM_DEBUG(nalloc_base);
9147c478bd9Sstevel@tonic-gate 	PRM_DEBUG(nalloc_end);
9157c478bd9Sstevel@tonic-gate 	PRM_DEBUG(sdata);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/*
9187c478bd9Sstevel@tonic-gate 	 * Remember any slop after e_text so we can give it to the modules.
9197c478bd9Sstevel@tonic-gate 	 */
9207c478bd9Sstevel@tonic-gate 	PRM_DEBUG(e_text);
9217c478bd9Sstevel@tonic-gate 	modtext = (caddr_t)roundup((uintptr_t)e_text, MMU_PAGESIZE);
92268d3ac02Skchow 	if (((uintptr_t)e_text & MMU_PAGEMASK4M) != (uintptr_t)s_text)
923bb121940Sdp78419 		prom_panic("nucleus text overflow");
9247c478bd9Sstevel@tonic-gate 	modtext_sz = (caddr_t)roundup((uintptr_t)modtext, MMU_PAGESIZE4M) -
9257c478bd9Sstevel@tonic-gate 	    modtext;
9267c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext);
9277c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
9307c478bd9Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
9317c478bd9Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * Remember what the physically available highest page is
9347c478bd9Sstevel@tonic-gate 	 * so that dumpsys works properly, and find out how much
9357c478bd9Sstevel@tonic-gate 	 * memory is installed.
9367c478bd9Sstevel@tonic-gate 	 */
9377c478bd9Sstevel@tonic-gate 	installed_top_size_memlist_array(boot_physinstalled,
9387c478bd9Sstevel@tonic-gate 	    boot_physinstalled_len, &physmax, &physinstalled);
9397c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
9407c478bd9Sstevel@tonic-gate 	PRM_DEBUG(physmax);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/* Fill out memory nodes config structure */
9437c478bd9Sstevel@tonic-gate 	startup_build_mem_nodes(boot_physinstalled, boot_physinstalled_len);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/*
9467c478bd9Sstevel@tonic-gate 	 * Get the list of physically available memory to size
9477c478bd9Sstevel@tonic-gate 	 * the number of page structures needed.
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
9507c478bd9Sstevel@tonic-gate 	/*
9517c478bd9Sstevel@tonic-gate 	 * This first snap shot of npages can represent the pages used
9527c478bd9Sstevel@tonic-gate 	 * by OBP's text and data approximately. This is used in the
9537c478bd9Sstevel@tonic-gate 	 * the calculation of the kernel size
9547c478bd9Sstevel@tonic-gate 	 */
9557c478bd9Sstevel@tonic-gate 	obp_pages = physinstalled - npages;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	/*
9597c478bd9Sstevel@tonic-gate 	 * On small-memory systems (<MODTEXT_SM_SIZE MB, currently 256MB), the
9607c478bd9Sstevel@tonic-gate 	 * in-nucleus module text is capped to MODTEXT_SM_CAP bytes (currently
9617c478bd9Sstevel@tonic-gate 	 * 2MB) and any excess pages are put on physavail.  The assumption is
9627c478bd9Sstevel@tonic-gate 	 * that small-memory systems will need more pages more than they'll
9637c478bd9Sstevel@tonic-gate 	 * need efficiently-mapped module texts.
9647c478bd9Sstevel@tonic-gate 	 */
9657c478bd9Sstevel@tonic-gate 	if ((physinstalled < mmu_btop(MODTEXT_SM_SIZE << 20)) &&
9667c478bd9Sstevel@tonic-gate 	    modtext_sz > MODTEXT_SM_CAP) {
9677c478bd9Sstevel@tonic-gate 		extra_etpg = mmu_btop(modtext_sz - MODTEXT_SM_CAP);
9687c478bd9Sstevel@tonic-gate 		modtext_sz = MODTEXT_SM_CAP;
96968d3ac02Skchow 		extra_etva = modtext + modtext_sz;
97068d3ac02Skchow 	}
97168d3ac02Skchow 
9727c478bd9Sstevel@tonic-gate 	PRM_DEBUG(extra_etpg);
9737c478bd9Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
9747c478bd9Sstevel@tonic-gate 	PRM_DEBUG(extra_etva);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	/*
9777c478bd9Sstevel@tonic-gate 	 * Account for any pages after e_text and e_data.
9787c478bd9Sstevel@tonic-gate 	 */
9797c478bd9Sstevel@tonic-gate 	npages += extra_etpg;
9807c478bd9Sstevel@tonic-gate 	npages += mmu_btopr(nalloc_end - nalloc_base);
9817c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/*
9847c478bd9Sstevel@tonic-gate 	 * npages is the maximum of available physical memory possible.
9857c478bd9Sstevel@tonic-gate 	 * (ie. it will never be more than this)
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	/*
9897c478bd9Sstevel@tonic-gate 	 * initialize the nucleus memory allocator.
9907c478bd9Sstevel@tonic-gate 	 */
9917c478bd9Sstevel@tonic-gate 	ndata_alloc_init(&ndata, (uintptr_t)nalloc_base, (uintptr_t)nalloc_end);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * Allocate mmu fault status area from the nucleus data area.
9957c478bd9Sstevel@tonic-gate 	 */
9967c478bd9Sstevel@tonic-gate 	if ((&ndata_alloc_mmfsa != NULL) && (ndata_alloc_mmfsa(&ndata) != 0))
9977c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after mfsa alloc");
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
10007c478bd9Sstevel@tonic-gate 	 * Allocate kernel TSBs from the nucleus data area.
10017c478bd9Sstevel@tonic-gate 	 */
10027c478bd9Sstevel@tonic-gate 	if (ndata_alloc_tsbs(&ndata, npages) != 0)
10037c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after tsbs alloc");
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Allocate dmv dispatch table from the nucleus data area.
10077c478bd9Sstevel@tonic-gate 	 */
10087c478bd9Sstevel@tonic-gate 	if (ndata_alloc_dmv(&ndata) != 0)
10097c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after dmv alloc");
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	page_coloring_init();
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/*
10157c478bd9Sstevel@tonic-gate 	 * Allocate page_freelists bin headers for memnode 0 from the
10167c478bd9Sstevel@tonic-gate 	 * nucleus data area.
10177c478bd9Sstevel@tonic-gate 	 */
10187c478bd9Sstevel@tonic-gate 	if (ndata_alloc_page_freelists(&ndata, 0) != 0)
10197c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC,
10207c478bd9Sstevel@tonic-gate 		    "no more nucleus memory after page free lists alloc");
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
10237c478bd9Sstevel@tonic-gate 		kpm_init();
10247c478bd9Sstevel@tonic-gate 		/*
10257c478bd9Sstevel@tonic-gate 		 * kpm page space -- Update kpm_npages and make the
10267c478bd9Sstevel@tonic-gate 		 * same assumption about fragmenting as it is done
10277c478bd9Sstevel@tonic-gate 		 * for memseg_sz.
10287c478bd9Sstevel@tonic-gate 		 */
10297c478bd9Sstevel@tonic-gate 		kpm_npages_setup(memblocks + 4);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	/*
10337c478bd9Sstevel@tonic-gate 	 * Allocate hat related structs from the nucleus data area.
10347c478bd9Sstevel@tonic-gate 	 */
10357c478bd9Sstevel@tonic-gate 	if (ndata_alloc_hat(&ndata, npages, kpm_npages) != 0)
10367c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after hat alloc");
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * We want to do the BOP_ALLOCs before the real allocation of page
10407c478bd9Sstevel@tonic-gate 	 * structs in order to not have to allocate page structs for this
10417c478bd9Sstevel@tonic-gate 	 * memory.  We need to calculate a virtual address because we want
10427c478bd9Sstevel@tonic-gate 	 * the page structs to come before other allocations in virtual address
10437c478bd9Sstevel@tonic-gate 	 * space.  This is so some (if not all) of page structs can actually
10447c478bd9Sstevel@tonic-gate 	 * live in the nucleus.
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/*
10487c478bd9Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10497c478bd9Sstevel@tonic-gate 	 *
10507c478bd9Sstevel@tonic-gate 	 * There are comments all over the SFMMU code warning of dire
10517c478bd9Sstevel@tonic-gate 	 * consequences if the TSBs are moved out of 32-bit space.  This
10527c478bd9Sstevel@tonic-gate 	 * is largely because the asm code uses "sethi %hi(addr)"-type
10537c478bd9Sstevel@tonic-gate 	 * instructions which will not provide the expected result if the
10547c478bd9Sstevel@tonic-gate 	 * address is a 64-bit one.
10557c478bd9Sstevel@tonic-gate 	 *
10567c478bd9Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10577c478bd9Sstevel@tonic-gate 	 */
10587c478bd9Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)nalloc_end, MMU_PAGESIZE);
10597c478bd9Sstevel@tonic-gate 	alloc_base = sfmmu_ktsb_alloc(alloc_base);
10607c478bd9Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
10617c478bd9Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/*
10647c478bd9Sstevel@tonic-gate 	 * Allocate IOMMU TSB array.  We do this here so that the physical
10657c478bd9Sstevel@tonic-gate 	 * memory gets deducted from the PROM's physical memory list.
10667c478bd9Sstevel@tonic-gate 	 */
10677c478bd9Sstevel@tonic-gate 	alloc_base = iommu_tsb_init(alloc_base);
10687c478bd9Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10697c478bd9Sstevel@tonic-gate 	    ecache_alignsize);
10707c478bd9Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	/*
107325cf1a30Sjl139090 	 * Platforms like Starcat and OPL need special structures assigned in
107425cf1a30Sjl139090 	 * 32-bit virtual address space because their probing routines execute
107525cf1a30Sjl139090 	 * FCode, and FCode can't handle 64-bit virtual addresses...
10767c478bd9Sstevel@tonic-gate 	 */
107725cf1a30Sjl139090 	if (&plat_startup_memlist) {
107825cf1a30Sjl139090 		alloc_base = plat_startup_memlist(alloc_base);
10797c478bd9Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
10807c478bd9Sstevel@tonic-gate 		    ecache_alignsize);
10817c478bd9Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * Save off where the contiguous allocations to date have ended
10867c478bd9Sstevel@tonic-gate 	 * in econtig32.
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	econtig32 = alloc_base;
10897c478bd9Sstevel@tonic-gate 	PRM_DEBUG(econtig32);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	if (econtig32 > (caddr_t)KERNEL_LIMIT32)
10927c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "econtig32 too big");
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/*
10957c478bd9Sstevel@tonic-gate 	 * To avoid memory allocation collisions in the 32-bit virtual address
10967c478bd9Sstevel@tonic-gate 	 * space, make allocations from this point forward in 64-bit virtual
1097bb121940Sdp78419 	 * address space starting at syslimit and working up.
10987c478bd9Sstevel@tonic-gate 	 *
10997c478bd9Sstevel@tonic-gate 	 * All this is needed because on large memory systems, the default
11007c478bd9Sstevel@tonic-gate 	 * Solaris allocations will collide with SYSBASE32, which is hard
11017c478bd9Sstevel@tonic-gate 	 * coded to be at the virtual address 0x78000000.  Therefore, on 64-bit
11027c478bd9Sstevel@tonic-gate 	 * kernels, move the allocations to a location in the 64-bit virtual
11037c478bd9Sstevel@tonic-gate 	 * address space space, allowing those structures to grow without
11047c478bd9Sstevel@tonic-gate 	 * worry.
11057c478bd9Sstevel@tonic-gate 	 *
11067c478bd9Sstevel@tonic-gate 	 * On current CPUs we'll run out of physical memory address bits before
11077c478bd9Sstevel@tonic-gate 	 * we need to worry about the allocations running into anything else in
11087c478bd9Sstevel@tonic-gate 	 * VM or the virtual address holes on US-I and II, as there's currently
11097c478bd9Sstevel@tonic-gate 	 * about 1 TB of addressable space before the US-I/II VA hole.
11107c478bd9Sstevel@tonic-gate 	 */
11117c478bd9Sstevel@tonic-gate 	kmem64_base = (caddr_t)syslimit;
11127c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kmem64_base);
11137c478bd9Sstevel@tonic-gate 
1114bb121940Sdp78419 	/*
1115bb121940Sdp78419 	 * Allocate addresses, but not physical memory. None of these locations
1116bb121940Sdp78419 	 * can be touched until physical memory is allocated below.
1117bb121940Sdp78419 	 */
1118bb121940Sdp78419 	alloc_base = kmem64_base;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/*
11217c478bd9Sstevel@tonic-gate 	 * If KHME and/or UHME hash buckets won't fit in the nucleus, allocate
11227c478bd9Sstevel@tonic-gate 	 * them here.
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	if (khme_hash == NULL || uhme_hash == NULL) {
11257c478bd9Sstevel@tonic-gate 		/*
11267c478bd9Sstevel@tonic-gate 		 * alloc_hme_buckets() will align alloc_base properly before
11277c478bd9Sstevel@tonic-gate 		 * assigning the hash buckets, so we don't need to do it
11287c478bd9Sstevel@tonic-gate 		 * before the call...
11297c478bd9Sstevel@tonic-gate 		 */
11307c478bd9Sstevel@tonic-gate 		alloc_base = alloc_hme_buckets(alloc_base, alloc_alignsize);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11337c478bd9Sstevel@tonic-gate 		PRM_DEBUG(khme_hash);
11347c478bd9Sstevel@tonic-gate 		PRM_DEBUG(uhme_hash);
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	/*
11387c478bd9Sstevel@tonic-gate 	 * Allocate the remaining page freelists.  NUMA systems can
11397c478bd9Sstevel@tonic-gate 	 * have lots of page freelists, one per node, which quickly
11407c478bd9Sstevel@tonic-gate 	 * outgrow the amount of nucleus memory available.
11417c478bd9Sstevel@tonic-gate 	 */
11427c478bd9Sstevel@tonic-gate 	if (max_mem_nodes > 1) {
11437c478bd9Sstevel@tonic-gate 		int mnode;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		for (mnode = 1; mnode < max_mem_nodes; mnode++) {
11467c478bd9Sstevel@tonic-gate 			alloc_base = alloc_page_freelists(mnode, alloc_base,
11477c478bd9Sstevel@tonic-gate 				ecache_alignsize);
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	if (!mml_table) {
11537c478bd9Sstevel@tonic-gate 		size_t mmltable_sz;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		/*
11567c478bd9Sstevel@tonic-gate 		 * We need to allocate the mml_table here because there
11577c478bd9Sstevel@tonic-gate 		 * was not enough space within the nucleus.
11587c478bd9Sstevel@tonic-gate 		 */
11597c478bd9Sstevel@tonic-gate 		mmltable_sz = sizeof (kmutex_t) * mml_table_sz;
11607c478bd9Sstevel@tonic-gate 		alloc_sz = roundup(mmltable_sz, alloc_alignsize);
11617c478bd9Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
11627c478bd9Sstevel@tonic-gate 		    alloc_alignsize);
1163bb121940Sdp78419 		mml_table = (kmutex_t *)alloc_base;
11647c478bd9Sstevel@tonic-gate 		alloc_base += alloc_sz;
11657c478bd9Sstevel@tonic-gate 		PRM_DEBUG(mml_table);
11667c478bd9Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	if (kpm_enable && !(kpmp_table || kpmp_stable)) {
11707c478bd9Sstevel@tonic-gate 		size_t kpmptable_sz;
11717c478bd9Sstevel@tonic-gate 		caddr_t table;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		/*
11747c478bd9Sstevel@tonic-gate 		 * We need to allocate either kpmp_table or kpmp_stable here
11757c478bd9Sstevel@tonic-gate 		 * because there was not enough space within the nucleus.
11767c478bd9Sstevel@tonic-gate 		 */
11777c478bd9Sstevel@tonic-gate 		kpmptable_sz = (kpm_smallpages == 0) ?
11787c478bd9Sstevel@tonic-gate 				sizeof (kpm_hlk_t) * kpmp_table_sz :
11797c478bd9Sstevel@tonic-gate 				sizeof (kpm_shlk_t) * kpmp_stable_sz;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		alloc_sz = roundup(kpmptable_sz, alloc_alignsize);
11827c478bd9Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
11837c478bd9Sstevel@tonic-gate 		    alloc_alignsize);
11847c478bd9Sstevel@tonic-gate 
1185bb121940Sdp78419 		table = alloc_base;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 		if (kpm_smallpages == 0) {
11887c478bd9Sstevel@tonic-gate 			kpmp_table = (kpm_hlk_t *)table;
11897c478bd9Sstevel@tonic-gate 			PRM_DEBUG(kpmp_table);
11907c478bd9Sstevel@tonic-gate 		} else {
11917c478bd9Sstevel@tonic-gate 			kpmp_stable = (kpm_shlk_t *)table;
11927c478bd9Sstevel@tonic-gate 			PRM_DEBUG(kpmp_stable);
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		alloc_base += alloc_sz;
11967c478bd9Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	if (&ecache_init_scrub_flush_area) {
12007c478bd9Sstevel@tonic-gate 		/*
12017c478bd9Sstevel@tonic-gate 		 * Pass alloc_base directly, as the routine itself is
12027c478bd9Sstevel@tonic-gate 		 * responsible for any special alignment requirements...
12037c478bd9Sstevel@tonic-gate 		 */
12047c478bd9Sstevel@tonic-gate 		alloc_base = ecache_init_scrub_flush_area(alloc_base);
12057c478bd9Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/*
12097c478bd9Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
12127c478bd9Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
12137c478bd9Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/*
12167c478bd9Sstevel@tonic-gate 	 * Reset npages and memblocks based on boot_physavail list.
12177c478bd9Sstevel@tonic-gate 	 */
12187c478bd9Sstevel@tonic-gate 	size_physavail(boot_physavail, boot_physavail_len, &npages, &memblocks);
12197c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	/*
12227c478bd9Sstevel@tonic-gate 	 * Account for extra memory after e_text.
12237c478bd9Sstevel@tonic-gate 	 */
12247c478bd9Sstevel@tonic-gate 	npages += extra_etpg;
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	/*
12277c478bd9Sstevel@tonic-gate 	 * Calculate the largest free memory chunk in the nucleus data area.
12287c478bd9Sstevel@tonic-gate 	 * We need to figure out if page structs can fit in there or not.
12297c478bd9Sstevel@tonic-gate 	 * We also make sure enough page structs get created for any physical
12307c478bd9Sstevel@tonic-gate 	 * memory we might be returning to the system.
12317c478bd9Sstevel@tonic-gate 	 */
12327c478bd9Sstevel@tonic-gate 	ndata_remain_sz = ndata_maxsize(&ndata);
12337c478bd9Sstevel@tonic-gate 	PRM_DEBUG(ndata_remain_sz);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	pp_sz = sizeof (struct page) * npages;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	/*
12387c478bd9Sstevel@tonic-gate 	 * Here's a nice bit of code based on somewhat recursive logic:
12397c478bd9Sstevel@tonic-gate 	 *
12407c478bd9Sstevel@tonic-gate 	 * If the page array would fit within the nucleus, we want to
12417c478bd9Sstevel@tonic-gate 	 * add npages to cover any extra memory we may be returning back
12427c478bd9Sstevel@tonic-gate 	 * to the system.
12437c478bd9Sstevel@tonic-gate 	 *
12447c478bd9Sstevel@tonic-gate 	 * HOWEVER, the page array is sized by calculating the size of
12457c478bd9Sstevel@tonic-gate 	 * (struct page * npages), as are the pagehash table, ctrs and
12467c478bd9Sstevel@tonic-gate 	 * memseg_list, so the very act of performing the calculation below may
12477c478bd9Sstevel@tonic-gate 	 * in fact make the array large enough that it no longer fits in the
12487c478bd9Sstevel@tonic-gate 	 * nucleus, meaning there would now be a much larger area of the
12497c478bd9Sstevel@tonic-gate 	 * nucleus free that should really be added to npages, which would
12507c478bd9Sstevel@tonic-gate 	 * make the page array that much larger, and so on.
12517c478bd9Sstevel@tonic-gate 	 *
12527c478bd9Sstevel@tonic-gate 	 * This also ignores the memory possibly used in the nucleus for the
12537c478bd9Sstevel@tonic-gate 	 * the page hash, ctrs and memseg list and the fact that whether they
12547c478bd9Sstevel@tonic-gate 	 * fit there or not varies with the npages calculation below, but we
12557c478bd9Sstevel@tonic-gate 	 * don't even factor them into the equation at this point; perhaps we
12567c478bd9Sstevel@tonic-gate 	 * should or perhaps we should just take the approach that the few
12577c478bd9Sstevel@tonic-gate 	 * extra pages we could add via this calculation REALLY aren't worth
12587c478bd9Sstevel@tonic-gate 	 * the hassle...
12597c478bd9Sstevel@tonic-gate 	 */
12607c478bd9Sstevel@tonic-gate 	if (ndata_remain_sz > pp_sz) {
12617c478bd9Sstevel@tonic-gate 		size_t spare = ndata_spare(&ndata, pp_sz, ecache_alignsize);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		npages += mmu_btop(spare);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 		pp_sz = npages * sizeof (struct page);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		pp_base = ndata_alloc(&ndata, pp_sz, ecache_alignsize);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	/*
12717c478bd9Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
12727c478bd9Sstevel@tonic-gate 	 * the monitor value unless physmem is larger than the total
12737c478bd9Sstevel@tonic-gate 	 * amount of memory on hand.
12747c478bd9Sstevel@tonic-gate 	 */
12757c478bd9Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
12767c478bd9Sstevel@tonic-gate 		physmem = npages;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	/*
12797c478bd9Sstevel@tonic-gate 	 * If pp_base is NULL that means the routines above have determined
12807c478bd9Sstevel@tonic-gate 	 * the page array will not fit in the nucleus; we'll have to
12817c478bd9Sstevel@tonic-gate 	 * BOP_ALLOC() ourselves some space for them.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	if (pp_base == NULL) {
12847c478bd9Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
12857c478bd9Sstevel@tonic-gate 		    alloc_alignsize);
12867c478bd9Sstevel@tonic-gate 		alloc_sz = roundup(pp_sz, alloc_alignsize);
12877c478bd9Sstevel@tonic-gate 
1288bb121940Sdp78419 		pp_base = (struct page *)alloc_base;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		alloc_base += alloc_sz;
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	/*
12947c478bd9Sstevel@tonic-gate 	 * The page structure hash table size is a power of 2
12957c478bd9Sstevel@tonic-gate 	 * such that the average hash chain length is PAGE_HASHAVELEN.
12967c478bd9Sstevel@tonic-gate 	 */
12977c478bd9Sstevel@tonic-gate 	page_hashsz = npages / PAGE_HASHAVELEN;
12987c478bd9Sstevel@tonic-gate 	page_hashsz = 1 << highbit((ulong_t)page_hashsz);
12997c478bd9Sstevel@tonic-gate 	pagehash_sz = sizeof (struct page *) * page_hashsz;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/*
13027c478bd9Sstevel@tonic-gate 	 * We want to TRY to fit the page structure hash table,
13037c478bd9Sstevel@tonic-gate 	 * the page size free list counters, the memseg list and
13047c478bd9Sstevel@tonic-gate 	 * and the kpm page space in the nucleus if possible.
13057c478bd9Sstevel@tonic-gate 	 *
13067c478bd9Sstevel@tonic-gate 	 * alloc_sz counts how much memory needs to be allocated by
13077c478bd9Sstevel@tonic-gate 	 * BOP_ALLOC().
13087c478bd9Sstevel@tonic-gate 	 */
13097c478bd9Sstevel@tonic-gate 	page_hash = ndata_alloc(&ndata, pagehash_sz, ecache_alignsize);
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	alloc_sz = (page_hash == NULL ? pagehash_sz : 0);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * Size up per page size free list counters.
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	ctrs_sz = page_ctrs_sz();
13177c478bd9Sstevel@tonic-gate 	ctrs_base = ndata_alloc(&ndata, ctrs_sz, ecache_alignsize);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	if (ctrs_base == NULL)
13207c478bd9Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + ctrs_sz;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * The memseg list is for the chunks of physical memory that
13247c478bd9Sstevel@tonic-gate 	 * will be managed by the vm system.  The number calculated is
13257c478bd9Sstevel@tonic-gate 	 * a guess as boot may fragment it more when memory allocations
13267c478bd9Sstevel@tonic-gate 	 * are made before kphysm_init().  Currently, there are two
13277c478bd9Sstevel@tonic-gate 	 * allocations before then, so we assume each causes fragmen-
13287c478bd9Sstevel@tonic-gate 	 * tation, and add a couple more for good measure.
13297c478bd9Sstevel@tonic-gate 	 */
13307c478bd9Sstevel@tonic-gate 	memseg_sz = sizeof (struct memseg) * (memblocks + 4);
13317c478bd9Sstevel@tonic-gate 	memseg_base = ndata_alloc(&ndata, memseg_sz, ecache_alignsize);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (memseg_base == NULL)
13347c478bd9Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, ecache_alignsize) + memseg_sz;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
13387c478bd9Sstevel@tonic-gate 		/*
13397c478bd9Sstevel@tonic-gate 		 * kpm page space -- Update kpm_npages and make the
13407c478bd9Sstevel@tonic-gate 		 * same assumption about fragmenting as it is done
13417c478bd9Sstevel@tonic-gate 		 * for memseg_sz above.
13427c478bd9Sstevel@tonic-gate 		 */
13437c478bd9Sstevel@tonic-gate 		kpm_npages_setup(memblocks + 4);
13447c478bd9Sstevel@tonic-gate 		kpm_pp_sz = (kpm_smallpages == 0) ?
13457c478bd9Sstevel@tonic-gate 		    kpm_npages * sizeof (kpm_page_t):
13467c478bd9Sstevel@tonic-gate 		    kpm_npages * sizeof (kpm_spage_t);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 		kpm_pp_base = (uintptr_t)ndata_alloc(&ndata, kpm_pp_sz,
13497c478bd9Sstevel@tonic-gate 		    ecache_alignsize);
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		if (kpm_pp_base == NULL)
13527c478bd9Sstevel@tonic-gate 			alloc_sz = roundup(alloc_sz, ecache_alignsize) +
13537c478bd9Sstevel@tonic-gate 			    kpm_pp_sz;
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (alloc_sz > 0) {
13577c478bd9Sstevel@tonic-gate 		uintptr_t bop_base;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 		/*
13607c478bd9Sstevel@tonic-gate 		 * We need extra memory allocated through BOP_ALLOC.
13617c478bd9Sstevel@tonic-gate 		 */
13627c478bd9Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
13637c478bd9Sstevel@tonic-gate 		    alloc_alignsize);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 		alloc_sz = roundup(alloc_sz, alloc_alignsize);
13667c478bd9Sstevel@tonic-gate 
1367bb121940Sdp78419 		bop_base = (uintptr_t)alloc_base;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		alloc_base += alloc_sz;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 		if (page_hash == NULL) {
13727c478bd9Sstevel@tonic-gate 			page_hash = (struct page **)bop_base;
13737c478bd9Sstevel@tonic-gate 			bop_base = roundup(bop_base + pagehash_sz,
13747c478bd9Sstevel@tonic-gate 			    ecache_alignsize);
13757c478bd9Sstevel@tonic-gate 		}
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 		if (ctrs_base == NULL) {
13787c478bd9Sstevel@tonic-gate 			ctrs_base = (caddr_t)bop_base;
13797c478bd9Sstevel@tonic-gate 			bop_base = roundup(bop_base + ctrs_sz,
13807c478bd9Sstevel@tonic-gate 			    ecache_alignsize);
13817c478bd9Sstevel@tonic-gate 		}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		if (memseg_base == NULL) {
13847c478bd9Sstevel@tonic-gate 			memseg_base = (struct memseg *)bop_base;
13857c478bd9Sstevel@tonic-gate 			bop_base = roundup(bop_base + memseg_sz,
13867c478bd9Sstevel@tonic-gate 			    ecache_alignsize);
13877c478bd9Sstevel@tonic-gate 		}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 		if (kpm_enable && kpm_pp_base == NULL) {
13907c478bd9Sstevel@tonic-gate 			kpm_pp_base = (uintptr_t)bop_base;
13917c478bd9Sstevel@tonic-gate 			bop_base = roundup(bop_base + kpm_pp_sz,
13927c478bd9Sstevel@tonic-gate 			    ecache_alignsize);
13937c478bd9Sstevel@tonic-gate 		}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		ASSERT(bop_base <= (uintptr_t)alloc_base);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	PRM_DEBUG(page_hash);
13997c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_base);
14007c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kpm_pp_base);
14017c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kpm_pp_sz);
14027c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_base);
14037c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_sz);
14047c478bd9Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate #ifdef	TRAPTRACE
14077c478bd9Sstevel@tonic-gate 	alloc_base = trap_trace_alloc(alloc_base);
14087c478bd9Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
14097c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	/*
1412bb121940Sdp78419 	 * In theory it's possible that kmem64 chunk is 0 sized
1413bb121940Sdp78419 	 * (on very small machines). Check for that.
1414bb121940Sdp78419 	 */
1415bb121940Sdp78419 	if (alloc_base == kmem64_base) {
1416bb121940Sdp78419 		kmem64_base = NULL;
1417bb121940Sdp78419 		kmem64_end = NULL;
1418bb121940Sdp78419 		kmem64_aligned_end = NULL;
1419bb121940Sdp78419 		goto kmem64_alloced;
1420bb121940Sdp78419 	}
1421bb121940Sdp78419 
1422bb121940Sdp78419 	/*
1423bb121940Sdp78419 	 * Allocate kmem64 memory.
1424bb121940Sdp78419 	 * Round up to end of large page and overmap.
1425bb121940Sdp78419 	 * kmem64_end..kmem64_aligned_end is added to memory list for reuse
14267c478bd9Sstevel@tonic-gate 	 */
14277c478bd9Sstevel@tonic-gate 	kmem64_end = (caddr_t)roundup((uintptr_t)alloc_base,
1428bb121940Sdp78419 	    MMU_PAGESIZE);
1429bb121940Sdp78419 
1430bb121940Sdp78419 	/*
1431bb121940Sdp78419 	 * Make one large memory alloc after figuring out the 64-bit size. This
1432bb121940Sdp78419 	 * will enable use of the largest page size appropriate for the system
1433bb121940Sdp78419 	 * architecture.
1434bb121940Sdp78419 	 */
1435bb121940Sdp78419 	ASSERT(mmu_exported_pagesize_mask & (1 << TTE8K));
1436bb121940Sdp78419 	ASSERT(IS_P2ALIGNED(kmem64_base, TTEBYTES(max_bootlp_tteszc)));
1437bb121940Sdp78419 	for (i = max_bootlp_tteszc; i >= TTE8K; i--) {
1438bb121940Sdp78419 		size_t asize;
1439bb121940Sdp78419 #if !defined(C_OBP)
1440bb121940Sdp78419 		unsigned long long pa;
1441bb121940Sdp78419 #endif	/* !C_OBP */
1442bb121940Sdp78419 
1443bb121940Sdp78419 		if ((mmu_exported_pagesize_mask & (1 << i)) == 0)
1444bb121940Sdp78419 			continue;
1445bb121940Sdp78419 		kmem64_alignsize = TTEBYTES(i);
1446bb121940Sdp78419 		kmem64_szc = i;
1447bb121940Sdp78419 
1448bb121940Sdp78419 		/* limit page size for small memory */
1449bb121940Sdp78419 		if (mmu_btop(kmem64_alignsize) > (npages >> 2))
1450bb121940Sdp78419 			continue;
1451bb121940Sdp78419 
1452bb121940Sdp78419 		kmem64_aligned_end = (caddr_t)roundup((uintptr_t)kmem64_end,
1453bb121940Sdp78419 		    kmem64_alignsize);
1454bb121940Sdp78419 		asize = kmem64_aligned_end - kmem64_base;
1455bb121940Sdp78419 #if !defined(C_OBP)
1456bb121940Sdp78419 		if (prom_allocate_phys(asize, kmem64_alignsize, &pa) == 0) {
1457bb121940Sdp78419 			if (prom_claim_virt(asize, kmem64_base) !=
1458bb121940Sdp78419 			    (caddr_t)-1) {
1459bb121940Sdp78419 				kmem64_pabase = pa;
1460bb121940Sdp78419 				install_kmem64_tte();
1461bb121940Sdp78419 				break;
1462bb121940Sdp78419 			} else {
1463bb121940Sdp78419 				prom_free_phys(asize, pa);
1464bb121940Sdp78419 			}
1465bb121940Sdp78419 		}
1466bb121940Sdp78419 #else	/* !C_OBP */
1467bb121940Sdp78419 		if ((caddr_t)BOP_ALLOC(bootops, kmem64_base, asize,
1468bb121940Sdp78419 		    kmem64_alignsize) == kmem64_base) {
1469bb121940Sdp78419 			kmem64_pabase = va_to_pa(kmem64_base);
1470bb121940Sdp78419 			break;
1471bb121940Sdp78419 		}
1472bb121940Sdp78419 #endif	/* !C_OBP */
1473bb121940Sdp78419 		if (i == TTE8K) {
1474bb121940Sdp78419 			prom_panic("kmem64 allocation failure");
1475bb121940Sdp78419 		}
1476bb121940Sdp78419 	}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kmem64_base);
14797c478bd9Sstevel@tonic-gate 	PRM_DEBUG(kmem64_end);
1480bb121940Sdp78419 	PRM_DEBUG(kmem64_aligned_end);
1481bb121940Sdp78419 	PRM_DEBUG(kmem64_alignsize);
1482bb121940Sdp78419 
1483bb121940Sdp78419 	/*
1484bb121940Sdp78419 	 * Now set pa using saved va from above.
1485bb121940Sdp78419 	 */
1486bb121940Sdp78419 	if (&ecache_init_scrub_flush_area) {
1487bb121940Sdp78419 		(void) ecache_init_scrub_flush_area(NULL);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 
1490bb121940Sdp78419 kmem64_alloced:
1491bb121940Sdp78419 
1492bb121940Sdp78419 	/*
1493bb121940Sdp78419 	 * Initialize per page size free list counters.
1494bb121940Sdp78419 	 */
1495bb121940Sdp78419 	ctrs_end = page_ctrs_alloc(ctrs_base);
1496bb121940Sdp78419 	ASSERT(ctrs_base + ctrs_sz >= ctrs_end);
1497bb121940Sdp78419 
14987c478bd9Sstevel@tonic-gate 	/*
1499b0fc0e77Sgovinda 	 * Allocate space for the interrupt vector table and also for the
1500b0fc0e77Sgovinda 	 * reserved interrupt vector data structures.
15017c478bd9Sstevel@tonic-gate 	 */
1502b0fc0e77Sgovinda 	memspace = (caddr_t)BOP_ALLOC(bootops, (caddr_t)intr_vec_table,
15037c478bd9Sstevel@tonic-gate 	    IVSIZE, MMU_PAGESIZE);
1504b0fc0e77Sgovinda 	if (memspace != (caddr_t)intr_vec_table)
1505bb121940Sdp78419 		prom_panic("interrupt vector table allocation failure");
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	/*
15087c478bd9Sstevel@tonic-gate 	 * The memory lists from boot are allocated from the heap arena
15097c478bd9Sstevel@tonic-gate 	 * so that later they can be freed and/or reallocated.
15107c478bd9Sstevel@tonic-gate 	 */
15117c478bd9Sstevel@tonic-gate 	if (BOP_GETPROP(bootops, "extent", &memlist_sz) == -1)
1512bb121940Sdp78419 		prom_panic("could not retrieve property \"extent\"");
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	/*
15157c478bd9Sstevel@tonic-gate 	 * Between now and when we finish copying in the memory lists,
15167c478bd9Sstevel@tonic-gate 	 * allocations happen so the space gets fragmented and the
15177c478bd9Sstevel@tonic-gate 	 * lists longer.  Leave enough space for lists twice as long
15187c478bd9Sstevel@tonic-gate 	 * as what boot says it has now; roundup to a pagesize.
15197c478bd9Sstevel@tonic-gate 	 * Also add space for the final phys-avail copy in the fixup
15207c478bd9Sstevel@tonic-gate 	 * routine.
15217c478bd9Sstevel@tonic-gate 	 */
15227c478bd9Sstevel@tonic-gate 	va = (caddr_t)(sysbase + PAGESIZE + PANICBUFSIZE +
15237c478bd9Sstevel@tonic-gate 	    roundup(IVSIZE, MMU_PAGESIZE));
15247c478bd9Sstevel@tonic-gate 	memlist_sz *= 4;
15257c478bd9Sstevel@tonic-gate 	memlist_sz = roundup(memlist_sz, MMU_PAGESIZE);
15267c478bd9Sstevel@tonic-gate 	memspace = (caddr_t)BOP_ALLOC(bootops, va, memlist_sz, BO_NO_ALIGN);
15277c478bd9Sstevel@tonic-gate 	if (memspace == NULL)
15287c478bd9Sstevel@tonic-gate 		halt("Boot allocation failed.");
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	memlist = (struct memlist *)memspace;
15317c478bd9Sstevel@tonic-gate 	memlist_end = (char *)memspace + memlist_sz;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist);
15347c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_end);
15357c478bd9Sstevel@tonic-gate 	PRM_DEBUG(sysbase);
15367c478bd9Sstevel@tonic-gate 	PRM_DEBUG(syslimit);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	kernelheap_init((void *)sysbase, (void *)syslimit,
15397c478bd9Sstevel@tonic-gate 	    (caddr_t)sysbase + PAGESIZE, NULL, NULL);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/*
15427c478bd9Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
15437c478bd9Sstevel@tonic-gate 	 */
15447c478bd9Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
15457c478bd9Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
15467c478bd9Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * Remove the space used by BOP_ALLOC from the kernel heap
15507c478bd9Sstevel@tonic-gate 	 * plus the area actually used by the OBP (if any)
15517c478bd9Sstevel@tonic-gate 	 * ignoring virtual addresses in virt_avail, above syslimit.
15527c478bd9Sstevel@tonic-gate 	 */
15537c478bd9Sstevel@tonic-gate 	virt_avail = memlist;
15547c478bd9Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	for (cur = virt_avail; cur->next; cur = cur->next) {
15577c478bd9Sstevel@tonic-gate 		uint64_t range_base, range_size;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 		if ((range_base = cur->address + cur->size) < (uint64_t)sysbase)
15607c478bd9Sstevel@tonic-gate 			continue;
15617c478bd9Sstevel@tonic-gate 		if (range_base >= (uint64_t)syslimit)
15627c478bd9Sstevel@tonic-gate 			break;
15637c478bd9Sstevel@tonic-gate 		/*
15647c478bd9Sstevel@tonic-gate 		 * Limit the range to end at syslimit.
15657c478bd9Sstevel@tonic-gate 		 */
15667c478bd9Sstevel@tonic-gate 		range_size = MIN(cur->next->address,
15677c478bd9Sstevel@tonic-gate 		    (uint64_t)syslimit) - range_base;
15687c478bd9Sstevel@tonic-gate 		(void) vmem_xalloc(heap_arena, (size_t)range_size, PAGESIZE,
15697c478bd9Sstevel@tonic-gate 		    0, 0, (void *)range_base, (void *)(range_base + range_size),
15707c478bd9Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	phys_avail = memlist;
15747c478bd9Sstevel@tonic-gate 	(void) copy_physavail(boot_physavail, boot_physavail_len,
15757c478bd9Sstevel@tonic-gate 	    &memlist, 0, 0);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	/*
1578bb121940Sdp78419 	 * Add any unused kmem64 memory from overmapped page
1579bb121940Sdp78419 	 * (Note: va_to_pa does not work for kmem64_end)
1580bb121940Sdp78419 	 */
1581bb121940Sdp78419 	if (kmem64_end < kmem64_aligned_end) {
1582bb121940Sdp78419 		uint64_t overlap_size = kmem64_aligned_end - kmem64_end;
1583bb121940Sdp78419 		uint64_t overlap_pa = kmem64_pabase +
1584bb121940Sdp78419 		    (kmem64_end - kmem64_base);
1585bb121940Sdp78419 
1586bb121940Sdp78419 		PRM_DEBUG(overlap_pa);
1587bb121940Sdp78419 		PRM_DEBUG(overlap_size);
1588bb121940Sdp78419 		memlist_add(overlap_pa, overlap_size, &memlist, &phys_avail);
1589bb121940Sdp78419 	}
1590bb121940Sdp78419 
1591bb121940Sdp78419 	/*
15927c478bd9Sstevel@tonic-gate 	 * Add any extra memory after e_text to the phys_avail list, as long
15937c478bd9Sstevel@tonic-gate 	 * as there's at least a page to add.
15947c478bd9Sstevel@tonic-gate 	 */
15957c478bd9Sstevel@tonic-gate 	if (extra_etpg)
15967c478bd9Sstevel@tonic-gate 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
15977c478bd9Sstevel@tonic-gate 		    &memlist, &phys_avail);
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	/*
1600*1e45ea5aSblakej 	 * Add any extra memory at the end of the ndata region if there's at
1601*1e45ea5aSblakej 	 * least a page to add.  There might be a few more pages available in
1602*1e45ea5aSblakej 	 * the middle of the ndata region, but for now they are ignored.
16037c478bd9Sstevel@tonic-gate 	 */
1604*1e45ea5aSblakej 	nalloc_base = ndata_extra_base(&ndata, MMU_PAGESIZE, nalloc_end);
1605*1e45ea5aSblakej 	if (nalloc_base == NULL)
16067c478bd9Sstevel@tonic-gate 		nalloc_base = nalloc_end;
16077c478bd9Sstevel@tonic-gate 	ndata_remain_sz = nalloc_end - nalloc_base;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
16107c478bd9Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
16117c478bd9Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &phys_avail);
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist);
16147c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memlist_sz);
16157c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memspace);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	if ((caddr_t)memlist > (memspace + memlist_sz))
1618bb121940Sdp78419 		prom_panic("memlist overflow");
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	PRM_DEBUG(pp_base);
16217c478bd9Sstevel@tonic-gate 	PRM_DEBUG(memseg_base);
16227c478bd9Sstevel@tonic-gate 	PRM_DEBUG(npages);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
16267c478bd9Sstevel@tonic-gate 	 */
16277c478bd9Sstevel@tonic-gate 	kphysm_init(pp_base, memseg_base, npages, kpm_pp_base, kpm_npages);
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem;
16307c478bd9Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	/*
16337c478bd9Sstevel@tonic-gate 	 * Some of the locks depend on page_hashsz being set!
16347c478bd9Sstevel@tonic-gate 	 * kmem_init() depends on this; so, keep it here.
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 	page_lock_init();
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	/*
16397c478bd9Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
16407c478bd9Sstevel@tonic-gate 	 */
16417c478bd9Sstevel@tonic-gate 	kmem_init();
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/*
1644932dc8e5Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
1645932dc8e5Sdp78419 	 */
1646fe70c9cfSdp78419 	if (&page_set_colorequiv_arr_cpu != NULL)
1647fe70c9cfSdp78419 		page_set_colorequiv_arr_cpu();
1648fe70c9cfSdp78419 	else
1649932dc8e5Sdp78419 		page_set_colorequiv_arr();
1650932dc8e5Sdp78419 
1651932dc8e5Sdp78419 	/*
16527c478bd9Sstevel@tonic-gate 	 * Initialize bp_mapin().
16537c478bd9Sstevel@tonic-gate 	 */
16547c478bd9Sstevel@tonic-gate 	bp_init(shm_alignment, HAT_STRICTORDER);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	/*
1657b0fc0e77Sgovinda 	 * Reserve space for panicbuf, intr_vec_table and reserved interrupt
1658b0fc0e77Sgovinda 	 * vector data structures from the 32-bit heap.
16597c478bd9Sstevel@tonic-gate 	 */
16607c478bd9Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, PANICBUFSIZE, PAGESIZE, 0, 0,
16617c478bd9Sstevel@tonic-gate 	    panicbuf, panicbuf + PANICBUFSIZE,
16627c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, IVSIZE, PAGESIZE, 0, 0,
1665b0fc0e77Sgovinda 	    intr_vec_table, (caddr_t)intr_vec_table + IVSIZE,
16667c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	mem_config_init();
16697c478bd9Sstevel@tonic-gate }
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate static void
16727c478bd9Sstevel@tonic-gate startup_modules(void)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	int proplen, nhblk1, nhblk8;
16757c478bd9Sstevel@tonic-gate 	size_t  nhblksz;
1676bb121940Sdp78419 	pgcnt_t pages_per_hblk;
16777c478bd9Sstevel@tonic-gate 	size_t hme8blk_sz, hme1blk_sz;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	/*
16807c478bd9Sstevel@tonic-gate 	 * Log any optional messages from the boot program
16817c478bd9Sstevel@tonic-gate 	 */
16827c478bd9Sstevel@tonic-gate 	proplen = (size_t)BOP_GETPROPLEN(bootops, "boot-message");
16837c478bd9Sstevel@tonic-gate 	if (proplen > 0) {
16847c478bd9Sstevel@tonic-gate 		char *msg;
16857c478bd9Sstevel@tonic-gate 		size_t len = (size_t)proplen;
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 		msg = kmem_zalloc(len, KM_SLEEP);
16887c478bd9Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "boot-message", msg);
16897c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s\n", msg);
16907c478bd9Sstevel@tonic-gate 		kmem_free(msg, len);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	/*
16947c478bd9Sstevel@tonic-gate 	 * Let the platforms have a chance to change default
16957c478bd9Sstevel@tonic-gate 	 * values before reading system file.
16967c478bd9Sstevel@tonic-gate 	 */
16977c478bd9Sstevel@tonic-gate 	if (&set_platform_defaults)
16987c478bd9Sstevel@tonic-gate 		set_platform_defaults();
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	/*
17017c478bd9Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
17027c478bd9Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
17037c478bd9Sstevel@tonic-gate 	 */
17047c478bd9Sstevel@tonic-gate 	param_calc(0);
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	mod_setup();
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/*
17097c478bd9Sstevel@tonic-gate 	 * If this is a positron, complain and halt.
17107c478bd9Sstevel@tonic-gate 	 */
17117c478bd9Sstevel@tonic-gate 	if (&iam_positron && iam_positron()) {
17127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "This hardware platform is not supported"
17137c478bd9Sstevel@tonic-gate 		    " by this release of Solaris.\n");
17147c478bd9Sstevel@tonic-gate #ifdef DEBUG
17157c478bd9Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to resume */
17167c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting an unsupported platform.\n");
17177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware.\n");
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate #else /* DEBUG */
17207c478bd9Sstevel@tonic-gate 		halt(0);
17217c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	/*
17257c478bd9Sstevel@tonic-gate 	 * If we are running firmware that isn't 64-bit ready
17267c478bd9Sstevel@tonic-gate 	 * then complain and halt.
17277c478bd9Sstevel@tonic-gate 	 */
17287c478bd9Sstevel@tonic-gate 	do_prom_version_check();
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * Initialize system parameters
17327c478bd9Sstevel@tonic-gate 	 */
17337c478bd9Sstevel@tonic-gate 	param_init();
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	/*
17367c478bd9Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
17377c478bd9Sstevel@tonic-gate 	 */
17387c478bd9Sstevel@tonic-gate 	maxmem = physmem;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/* Set segkp limits. */
1741ae115bc7Smrj 	ncbase = kdi_segdebugbase;
1742ae115bc7Smrj 	ncend = kdi_segdebugbase;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/*
17457c478bd9Sstevel@tonic-gate 	 * Initialize the hat layer.
17467c478bd9Sstevel@tonic-gate 	 */
17477c478bd9Sstevel@tonic-gate 	hat_init();
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	/*
17507c478bd9Sstevel@tonic-gate 	 * Initialize segment management stuff.
17517c478bd9Sstevel@tonic-gate 	 */
17527c478bd9Sstevel@tonic-gate 	seg_init();
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	/*
17557c478bd9Sstevel@tonic-gate 	 * Create the va>tte handler, so the prom can understand
17567c478bd9Sstevel@tonic-gate 	 * kernel translations.  The handler is installed later, just
17577c478bd9Sstevel@tonic-gate 	 * as we are about to take over the trap table from the prom.
17587c478bd9Sstevel@tonic-gate 	 */
17597c478bd9Sstevel@tonic-gate 	create_va_to_tte();
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	/*
17627c478bd9Sstevel@tonic-gate 	 * Load the forthdebugger (optional)
17637c478bd9Sstevel@tonic-gate 	 */
17647c478bd9Sstevel@tonic-gate 	forthdebug_init();
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/*
17677c478bd9Sstevel@tonic-gate 	 * Create OBP node for console input callbacks
17687c478bd9Sstevel@tonic-gate 	 * if it is needed.
17697c478bd9Sstevel@tonic-gate 	 */
1770fea9cb91Slq150181 	startup_create_io_node();
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	if (modloadonly("fs", "specfs") == -1)
17737c478bd9Sstevel@tonic-gate 		halt("Can't load specfs");
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	if (modloadonly("fs", "devfs") == -1)
17767c478bd9Sstevel@tonic-gate 		halt("Can't load devfs");
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	if (modloadonly("misc", "swapgeneric") == -1)
17797c478bd9Sstevel@tonic-gate 		halt("Can't load swapgeneric");
17807c478bd9Sstevel@tonic-gate 
178145916cd2Sjpk 	(void) modloadonly("sys", "lbl_edition");
178245916cd2Sjpk 
17837c478bd9Sstevel@tonic-gate 	dispinit();
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	/*
17867c478bd9Sstevel@tonic-gate 	 * Infer meanings to the members of the idprom buffer.
17877c478bd9Sstevel@tonic-gate 	 */
17887c478bd9Sstevel@tonic-gate 	parse_idprom();
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	/* Read cluster configuration data. */
17917c478bd9Sstevel@tonic-gate 	clconf_init();
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	setup_ddi();
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * Lets take this opportunity to load the root device.
17977c478bd9Sstevel@tonic-gate 	 */
17987c478bd9Sstevel@tonic-gate 	if (loadrootmodules() != 0)
17997c478bd9Sstevel@tonic-gate 		debug_enter("Can't load the root filesystem");
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	/*
18027c478bd9Sstevel@tonic-gate 	 * Load tod driver module for the tod part found on this system.
18037c478bd9Sstevel@tonic-gate 	 * Recompute the cpu frequency/delays based on tod as tod part
18047c478bd9Sstevel@tonic-gate 	 * tends to keep time more accurately.
18057c478bd9Sstevel@tonic-gate 	 */
18067c478bd9Sstevel@tonic-gate 	if (&load_tod_module)
18077c478bd9Sstevel@tonic-gate 		load_tod_module();
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/*
18107c478bd9Sstevel@tonic-gate 	 * Allow platforms to load modules which might
18117c478bd9Sstevel@tonic-gate 	 * be needed after bootops are gone.
18127c478bd9Sstevel@tonic-gate 	 */
18137c478bd9Sstevel@tonic-gate 	if (&load_platform_modules)
18147c478bd9Sstevel@tonic-gate 		load_platform_modules();
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	setcpudelay();
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
18197c478bd9Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
18207c478bd9Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * Calculation and allocation of hmeblks needed to remap
1824bb121940Sdp78419 	 * the memory allocated by PROM till now.
1825bb121940Sdp78419 	 * Overestimate the number of hblk1 elements by assuming
1826bb121940Sdp78419 	 * worst case of TTE64K mappings.
1827bb121940Sdp78419 	 * sfmmu_hblk_alloc will panic if this calculation is wrong.
18287c478bd9Sstevel@tonic-gate 	 */
1829bb121940Sdp78419 	bop_alloc_pages = btopr(kmem64_end - kmem64_base);
1830bb121940Sdp78419 	pages_per_hblk = btop(HMEBLK_SPAN(TTE64K));
1831bb121940Sdp78419 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
1832bb121940Sdp78419 	nhblk1 = bop_alloc_pages / pages_per_hblk + hblk1_min;
18337c478bd9Sstevel@tonic-gate 
1834bb121940Sdp78419 	bop_alloc_pages = size_virtalloc(boot_virtavail, boot_virtavail_len);
1835bb121940Sdp78419 
1836bb121940Sdp78419 	/* sfmmu_init_nucleus_hblks expects properly aligned data structures */
18377c478bd9Sstevel@tonic-gate 	hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
18387c478bd9Sstevel@tonic-gate 	hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
18397c478bd9Sstevel@tonic-gate 
1840bb121940Sdp78419 	bop_alloc_pages += btopr(nhblk1 * hme1blk_sz);
1841bb121940Sdp78419 
18427c478bd9Sstevel@tonic-gate 	pages_per_hblk = btop(HMEBLK_SPAN(TTE8K));
1843bb121940Sdp78419 	nhblk8 = 0;
1844bb121940Sdp78419 	while (bop_alloc_pages > 1) {
18457c478bd9Sstevel@tonic-gate 		bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
1846bb121940Sdp78419 		nhblk8 += bop_alloc_pages /= pages_per_hblk;
1847bb121940Sdp78419 		bop_alloc_pages *= hme8blk_sz;
1848bb121940Sdp78419 		bop_alloc_pages = btopr(bop_alloc_pages);
1849bb121940Sdp78419 	}
1850bb121940Sdp78419 	nhblk8 += 2;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	/*
18537c478bd9Sstevel@tonic-gate 	 * Since hblk8's can hold up to 64k of mappings aligned on a 64k
18547c478bd9Sstevel@tonic-gate 	 * boundary, the number of hblk8's needed to map the entries in the
18557c478bd9Sstevel@tonic-gate 	 * boot_virtavail list needs to be adjusted to take this into
18567c478bd9Sstevel@tonic-gate 	 * consideration.  Thus, we need to add additional hblk8's since it
18577c478bd9Sstevel@tonic-gate 	 * is possible that an hblk8 will not have all 8 slots used due to
18587c478bd9Sstevel@tonic-gate 	 * alignment constraints.  Since there were boot_virtavail_len entries
18597c478bd9Sstevel@tonic-gate 	 * in that list, we need to add that many hblk8's to the number
18607c478bd9Sstevel@tonic-gate 	 * already calculated to make sure we don't underestimate.
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	nhblk8 += boot_virtavail_len;
18637c478bd9Sstevel@tonic-gate 	nhblksz = nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/* Allocate in pagesize chunks */
18667c478bd9Sstevel@tonic-gate 	nhblksz = roundup(nhblksz, MMU_PAGESIZE);
18677c478bd9Sstevel@tonic-gate 	hblk_base = kmem_zalloc(nhblksz, KM_SLEEP);
18687c478bd9Sstevel@tonic-gate 	sfmmu_init_nucleus_hblks(hblk_base, nhblksz, nhblk8, nhblk1);
18697c478bd9Sstevel@tonic-gate }
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate static void
18727c478bd9Sstevel@tonic-gate startup_bop_gone(void)
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	extern int bop_io_quiesced;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	/*
18771ae08745Sheppo 	 * Destroy the MD initialized at startup
18781ae08745Sheppo 	 * The startup initializes the MD framework
18791ae08745Sheppo 	 * using prom and BOP alloc free it now.
18801ae08745Sheppo 	 */
18811ae08745Sheppo 	mach_descrip_startup_fini();
18821ae08745Sheppo 
18831ae08745Sheppo 	/*
18847c478bd9Sstevel@tonic-gate 	 * Call back into boot and release boots resources.
18857c478bd9Sstevel@tonic-gate 	 */
18867c478bd9Sstevel@tonic-gate 	BOP_QUIESCE_IO(bootops);
18877c478bd9Sstevel@tonic-gate 	bop_io_quiesced = 1;
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
18907c478bd9Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
18917c478bd9Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
18927c478bd9Sstevel@tonic-gate 	/*
18937c478bd9Sstevel@tonic-gate 	 * Copy physinstalled list into kernel space.
18947c478bd9Sstevel@tonic-gate 	 */
18957c478bd9Sstevel@tonic-gate 	phys_install = memlist;
18967c478bd9Sstevel@tonic-gate 	copy_memlist(boot_physinstalled, boot_physinstalled_len, &memlist);
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	/*
18997c478bd9Sstevel@tonic-gate 	 * setup physically contiguous area twice as large as the ecache.
19007c478bd9Sstevel@tonic-gate 	 * this is used while doing displacement flush of ecaches
19017c478bd9Sstevel@tonic-gate 	 */
19027c478bd9Sstevel@tonic-gate 	if (&ecache_flush_address) {
19037c478bd9Sstevel@tonic-gate 		ecache_flushaddr = ecache_flush_address();
19047c478bd9Sstevel@tonic-gate 		if (ecache_flushaddr == (uint64_t)-1) {
19057c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC,
19067c478bd9Sstevel@tonic-gate 			    "startup: no memory to set ecache_flushaddr");
19077c478bd9Sstevel@tonic-gate 		}
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	/*
19117c478bd9Sstevel@tonic-gate 	 * Virtual available next.
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	ASSERT(virt_avail != NULL);
19147c478bd9Sstevel@tonic-gate 	memlist_free_list(virt_avail);
19157c478bd9Sstevel@tonic-gate 	virt_avail = memlist;
19167c478bd9Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	/*
19197c478bd9Sstevel@tonic-gate 	 * Last chance to ask our booter questions ..
19207c478bd9Sstevel@tonic-gate 	 */
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate /*
19257c478bd9Sstevel@tonic-gate  * startup_fixup_physavail - called from mach_sfmmu.c after the final
19267c478bd9Sstevel@tonic-gate  * allocations have been performed.  We can't call it in startup_bop_gone
19277c478bd9Sstevel@tonic-gate  * since later operations can cause obp to allocate more memory.
19287c478bd9Sstevel@tonic-gate  */
19297c478bd9Sstevel@tonic-gate void
19307c478bd9Sstevel@tonic-gate startup_fixup_physavail(void)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate 	struct memlist *cur;
1933bb121940Sdp78419 	size_t kmem64_overmap_size = kmem64_aligned_end - kmem64_end;
1934bb121940Sdp78419 
1935bb121940Sdp78419 	PRM_DEBUG(kmem64_overmap_size);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
19387c478bd9Sstevel@tonic-gate 	 * take the most current snapshot we can by calling mem-update
19397c478bd9Sstevel@tonic-gate 	 */
19407c478bd9Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
19417c478bd9Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
19427c478bd9Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	/*
19457c478bd9Sstevel@tonic-gate 	 * Copy phys_avail list, again.
19467c478bd9Sstevel@tonic-gate 	 * Both the kernel/boot and the prom have been allocating
19477c478bd9Sstevel@tonic-gate 	 * from the original list we copied earlier.
19487c478bd9Sstevel@tonic-gate 	 */
19497c478bd9Sstevel@tonic-gate 	cur = memlist;
19507c478bd9Sstevel@tonic-gate 	(void) copy_physavail(boot_physavail, boot_physavail_len,
19517c478bd9Sstevel@tonic-gate 	    &memlist, 0, 0);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
1954bb121940Sdp78419 	 * Add any unused kmem64 memory from overmapped page
1955bb121940Sdp78419 	 * (Note: va_to_pa does not work for kmem64_end)
1956bb121940Sdp78419 	 */
1957bb121940Sdp78419 	if (kmem64_overmap_size) {
1958bb121940Sdp78419 		memlist_add(kmem64_pabase + (kmem64_end - kmem64_base),
1959bb121940Sdp78419 		    kmem64_overmap_size,
1960bb121940Sdp78419 		    &memlist, &cur);
1961bb121940Sdp78419 	}
1962bb121940Sdp78419 
1963bb121940Sdp78419 	/*
19647c478bd9Sstevel@tonic-gate 	 * Add any extra memory after e_text we added to the phys_avail list
19657c478bd9Sstevel@tonic-gate 	 * back to the old list.
19667c478bd9Sstevel@tonic-gate 	 */
19677c478bd9Sstevel@tonic-gate 	if (extra_etpg)
19687c478bd9Sstevel@tonic-gate 		memlist_add(va_to_pa(extra_etva), mmu_ptob(extra_etpg),
19697c478bd9Sstevel@tonic-gate 		    &memlist, &cur);
19707c478bd9Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
19717c478bd9Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
19727c478bd9Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &cur);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	/*
19757c478bd9Sstevel@tonic-gate 	 * There isn't any bounds checking on the memlist area
19767c478bd9Sstevel@tonic-gate 	 * so ensure it hasn't overgrown.
19777c478bd9Sstevel@tonic-gate 	 */
19787c478bd9Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
19797c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: memlist size exceeded");
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	/*
19827c478bd9Sstevel@tonic-gate 	 * The kernel removes the pages that were allocated for it from
19837c478bd9Sstevel@tonic-gate 	 * the freelist, but we now have to find any -extra- pages that
19847c478bd9Sstevel@tonic-gate 	 * the prom has allocated for it's own book-keeping, and remove
19857c478bd9Sstevel@tonic-gate 	 * them from the freelist too. sigh.
19867c478bd9Sstevel@tonic-gate 	 */
19877c478bd9Sstevel@tonic-gate 	fix_prom_pages(phys_avail, cur);
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	ASSERT(phys_avail != NULL);
19907c478bd9Sstevel@tonic-gate 	memlist_free_list(phys_avail);
19917c478bd9Sstevel@tonic-gate 	phys_avail = cur;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	/*
19947c478bd9Sstevel@tonic-gate 	 * We're done with boot.  Just after this point in time, boot
19957c478bd9Sstevel@tonic-gate 	 * gets unmapped, so we can no longer rely on its services.
19967c478bd9Sstevel@tonic-gate 	 * Zero the bootops to indicate this fact.
19977c478bd9Sstevel@tonic-gate 	 */
19987c478bd9Sstevel@tonic-gate 	bootops = (struct bootops *)NULL;
19997c478bd9Sstevel@tonic-gate 	BOOTOPS_GONE();
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate static void
20037c478bd9Sstevel@tonic-gate startup_vm(void)
20047c478bd9Sstevel@tonic-gate {
20057c478bd9Sstevel@tonic-gate 	size_t	i;
20067c478bd9Sstevel@tonic-gate 	struct segmap_crargs a;
20077c478bd9Sstevel@tonic-gate 	struct segkpm_crargs b;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	uint64_t avmem;
20107c478bd9Sstevel@tonic-gate 	caddr_t va;
20117c478bd9Sstevel@tonic-gate 	pgcnt_t	max_phys_segkp;
20127c478bd9Sstevel@tonic-gate 	int	mnode;
20137c478bd9Sstevel@tonic-gate 
2014ec25b48fSsusans 	extern int use_brk_lpg, use_stk_lpg;
2015beb1bda0Sdavemq 
20167c478bd9Sstevel@tonic-gate 	/*
20177c478bd9Sstevel@tonic-gate 	 * get prom's mappings, create hments for them and switch
20187c478bd9Sstevel@tonic-gate 	 * to the kernel context.
20197c478bd9Sstevel@tonic-gate 	 */
20207c478bd9Sstevel@tonic-gate 	hat_kern_setup();
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	/*
20237c478bd9Sstevel@tonic-gate 	 * Take over trap table
20247c478bd9Sstevel@tonic-gate 	 */
20257c478bd9Sstevel@tonic-gate 	setup_trap_table();
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	/*
20287c478bd9Sstevel@tonic-gate 	 * Install the va>tte handler, so that the prom can handle
20297c478bd9Sstevel@tonic-gate 	 * misses and understand the kernel table layout in case
20307c478bd9Sstevel@tonic-gate 	 * we need call into the prom.
20317c478bd9Sstevel@tonic-gate 	 */
20327c478bd9Sstevel@tonic-gate 	install_va_to_tte();
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	/*
20357c478bd9Sstevel@tonic-gate 	 * Set a flag to indicate that the tba has been taken over.
20367c478bd9Sstevel@tonic-gate 	 */
20377c478bd9Sstevel@tonic-gate 	tba_taken_over = 1;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	/* initialize MMU primary context register */
20407c478bd9Sstevel@tonic-gate 	mmu_init_kcontext();
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	/*
20437c478bd9Sstevel@tonic-gate 	 * The boot cpu can now take interrupts, x-calls, x-traps
20447c478bd9Sstevel@tonic-gate 	 */
20457c478bd9Sstevel@tonic-gate 	CPUSET_ADD(cpu_ready_set, CPU->cpu_id);
20467c478bd9Sstevel@tonic-gate 	CPU->cpu_flags |= (CPU_READY | CPU_ENABLE | CPU_EXISTS);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	/*
20497c478bd9Sstevel@tonic-gate 	 * Set a flag to tell write_scb_int() that it can access V_TBR_WR_ADDR.
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	tbr_wr_addr_inited = 1;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	/*
20547c478bd9Sstevel@tonic-gate 	 * Initialize VM system, and map kernel address space.
20557c478bd9Sstevel@tonic-gate 	 */
20567c478bd9Sstevel@tonic-gate 	kvm_init();
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	/*
20597c478bd9Sstevel@tonic-gate 	 * XXX4U: previously, we initialized and turned on
20607c478bd9Sstevel@tonic-gate 	 * the caches at this point. But of course we have
20617c478bd9Sstevel@tonic-gate 	 * nothing to do, as the prom has already done this
20627c478bd9Sstevel@tonic-gate 	 * for us -- main memory must be E$able at all times.
20637c478bd9Sstevel@tonic-gate 	 */
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * If the following is true, someone has patched
20677c478bd9Sstevel@tonic-gate 	 * phsymem to be less than the number of pages that
20687c478bd9Sstevel@tonic-gate 	 * the system actually has.  Remove pages until system
20697c478bd9Sstevel@tonic-gate 	 * memory is limited to the requested amount.  Since we
20707c478bd9Sstevel@tonic-gate 	 * have allocated page structures for all pages, we
20717c478bd9Sstevel@tonic-gate 	 * correct the amount of memory we want to remove
20727c478bd9Sstevel@tonic-gate 	 * by the size of the memory used to hold page structures
20737c478bd9Sstevel@tonic-gate 	 * for the non-used pages.
20747c478bd9Sstevel@tonic-gate 	 */
20757c478bd9Sstevel@tonic-gate 	if (physmem < npages) {
20767c478bd9Sstevel@tonic-gate 		pgcnt_t diff, off;
20777c478bd9Sstevel@tonic-gate 		struct page *pp;
20787c478bd9Sstevel@tonic-gate 		struct seg kseg;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %ld pages", physmem);
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 		off = 0;
20837c478bd9Sstevel@tonic-gate 		diff = npages - physmem;
20847c478bd9Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
20857c478bd9Sstevel@tonic-gate 		kseg.s_as = &kas;
20867c478bd9Sstevel@tonic-gate 		while (diff--) {
20877c478bd9Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, (offset_t)off,
20887c478bd9Sstevel@tonic-gate 			    MMU_PAGESIZE, PG_WAIT | PG_EXCL,
20897c478bd9Sstevel@tonic-gate 			    &kseg, (caddr_t)off);
20907c478bd9Sstevel@tonic-gate 			if (pp == NULL)
20917c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, "limited physmem too much!");
20927c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
20937c478bd9Sstevel@tonic-gate 			page_downgrade(pp);
20947c478bd9Sstevel@tonic-gate 			availrmem--;
20957c478bd9Sstevel@tonic-gate 			off += MMU_PAGESIZE;
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	/*
21007c478bd9Sstevel@tonic-gate 	 * When printing memory, show the total as physmem less
21017c478bd9Sstevel@tonic-gate 	 * that stolen by a debugger.
21027c478bd9Sstevel@tonic-gate 	 */
21037c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %ldK (0x%lx000)\n",
21047c478bd9Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 10),
21057c478bd9Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 12));
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
21087c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "?avail mem = %lld\n", (unsigned long long)avmem);
21097c478bd9Sstevel@tonic-gate 
2110ec25b48fSsusans 	/*
2111ec25b48fSsusans 	 * For small memory systems disable automatic large pages.
2112ec25b48fSsusans 	 */
2113ec25b48fSsusans 	if (physmem < privm_lpg_min_physmem) {
2114beb1bda0Sdavemq 		use_brk_lpg = 0;
2115beb1bda0Sdavemq 		use_stk_lpg = 0;
2116beb1bda0Sdavemq 	}
2117beb1bda0Sdavemq 
21187c478bd9Sstevel@tonic-gate 	/*
21197c478bd9Sstevel@tonic-gate 	 * Perform platform specific freelist processing
21207c478bd9Sstevel@tonic-gate 	 */
21217c478bd9Sstevel@tonic-gate 	if (&plat_freelist_process) {
21227c478bd9Sstevel@tonic-gate 		for (mnode = 0; mnode < max_mem_nodes; mnode++)
21237c478bd9Sstevel@tonic-gate 			if (mem_node_config[mnode].exists)
21247c478bd9Sstevel@tonic-gate 				plat_freelist_process(mnode);
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	/*
21287c478bd9Sstevel@tonic-gate 	 * Initialize the segkp segment type.  We position it
21297c478bd9Sstevel@tonic-gate 	 * after the configured tables and buffers (whose end
21307c478bd9Sstevel@tonic-gate 	 * is given by econtig) and before V_WKBASE_ADDR.
21317c478bd9Sstevel@tonic-gate 	 * Also in this area is segkmap (size SEGMAPSIZE).
21327c478bd9Sstevel@tonic-gate 	 */
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	/* XXX - cache alignment? */
21357c478bd9Sstevel@tonic-gate 	va = (caddr_t)SEGKPBASE;
21367c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & PAGEOFFSET) == 0);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	max_phys_segkp = (physmem * 2);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	if (segkpsize < btop(SEGKPMINSIZE) || segkpsize > btop(SEGKPMAXSIZE)) {
21417c478bd9Sstevel@tonic-gate 		segkpsize = btop(SEGKPDEFSIZE);
21427c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal value for segkpsize. "
21437c478bd9Sstevel@tonic-gate 		    "segkpsize has been reset to %ld pages", segkpsize);
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	i = ptob(MIN(segkpsize, max_phys_segkp));
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
21497c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkp) < 0)
21507c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: cannot attach segkp");
21517c478bd9Sstevel@tonic-gate 	if (segkp_create(segkp) != 0)
21527c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: segkp_create failed");
21537c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/*
21567c478bd9Sstevel@tonic-gate 	 * kpm segment
21577c478bd9Sstevel@tonic-gate 	 */
21587c478bd9Sstevel@tonic-gate 	segmap_kpm = kpm_enable &&
21597c478bd9Sstevel@tonic-gate 		segmap_kpm && PAGESIZE == MAXBSIZE;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
21627c478bd9Sstevel@tonic-gate 		rw_enter(&kas.a_lock, RW_WRITER);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 		/*
21657c478bd9Sstevel@tonic-gate 		 * The segkpm virtual range range is larger than the
21667c478bd9Sstevel@tonic-gate 		 * actual physical memory size and also covers gaps in
21677c478bd9Sstevel@tonic-gate 		 * the physical address range for the following reasons:
21687c478bd9Sstevel@tonic-gate 		 * . keep conversion between segkpm and physical addresses
21697c478bd9Sstevel@tonic-gate 		 *   simple, cheap and unambiguous.
21707c478bd9Sstevel@tonic-gate 		 * . avoid extension/shrink of the the segkpm in case of DR.
21717c478bd9Sstevel@tonic-gate 		 * . avoid complexity for handling of virtual addressed
21727c478bd9Sstevel@tonic-gate 		 *   caches, segkpm and the regular mapping scheme must be
21737c478bd9Sstevel@tonic-gate 		 *   kept in sync wrt. the virtual color of mapped pages.
21747c478bd9Sstevel@tonic-gate 		 * Any accesses to virtual segkpm ranges not backed by
21757c478bd9Sstevel@tonic-gate 		 * physical memory will fall through the memseg pfn hash
21767c478bd9Sstevel@tonic-gate 		 * and will be handled in segkpm_fault.
21777c478bd9Sstevel@tonic-gate 		 * Additional kpm_size spaces needed for vac alias prevention.
21787c478bd9Sstevel@tonic-gate 		 */
21797c478bd9Sstevel@tonic-gate 		if (seg_attach(&kas, kpm_vbase, kpm_size * vac_colors,
21807c478bd9Sstevel@tonic-gate 		    segkpm) < 0)
21817c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "cannot attach segkpm");
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 		b.prot = PROT_READ | PROT_WRITE;
21847c478bd9Sstevel@tonic-gate 		b.nvcolors = shm_alignment >> MMU_PAGESHIFT;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 		if (segkpm_create(segkpm, (caddr_t)&b) != 0)
21877c478bd9Sstevel@tonic-gate 			panic("segkpm_create segkpm");
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 		rw_exit(&kas.a_lock);
2190fedab560Sae112802 
2191fedab560Sae112802 		mach_kpm_init();
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 
2194ad23a2dbSjohansen 	if (!segzio_fromheap) {
2195ad23a2dbSjohansen 		size_t size;
2196a778305aSjohansen 		size_t physmem_b = mmu_ptob(physmem);
2197ad23a2dbSjohansen 
2198ad23a2dbSjohansen 		/* size is in bytes, segziosize is in pages */
2199ad23a2dbSjohansen 		if (segziosize == 0) {
2200a778305aSjohansen 			size = physmem_b;
2201ad23a2dbSjohansen 		} else {
2202ad23a2dbSjohansen 			size = mmu_ptob(segziosize);
2203ad23a2dbSjohansen 		}
2204ad23a2dbSjohansen 
2205ad23a2dbSjohansen 		if (size < SEGZIOMINSIZE) {
2206ad23a2dbSjohansen 			size = SEGZIOMINSIZE;
2207a778305aSjohansen 		} else if (size > SEGZIOMAXSIZE) {
2208a778305aSjohansen 			size = SEGZIOMAXSIZE;
2209a778305aSjohansen 			/*
2210a778305aSjohansen 			 * On 64-bit x86, we only have 2TB of KVA.  This exists
2211a778305aSjohansen 			 * for parity with x86.
2212a778305aSjohansen 			 *
2213a778305aSjohansen 			 * SEGZIOMAXSIZE is capped at 512gb so that segzio
2214a778305aSjohansen 			 * doesn't consume all of KVA.  However, if we have a
2215a778305aSjohansen 			 * system that has more thant 512gb of physical memory,
2216a778305aSjohansen 			 * we can actually consume about half of the difference
2217a778305aSjohansen 			 * between 512gb and the rest of the available physical
2218a778305aSjohansen 			 * memory.
2219a778305aSjohansen 			 */
2220a778305aSjohansen 			if (physmem_b > SEGZIOMAXSIZE) {
2221a778305aSjohansen 				size += (physmem_b - SEGZIOMAXSIZE) / 2;
2222a778305aSjohansen 		}
2223ad23a2dbSjohansen 		}
2224ad23a2dbSjohansen 		segziosize = mmu_btop(roundup(size, MMU_PAGESIZE));
2225ad23a2dbSjohansen 		/* put the base of the ZIO segment after the kpm segment */
2226ad23a2dbSjohansen 		segzio_base = kpm_vbase + (kpm_size * vac_colors);
2227ad23a2dbSjohansen 		PRM_DEBUG(segziosize);
2228ad23a2dbSjohansen 		PRM_DEBUG(segzio_base);
2229ad23a2dbSjohansen 
2230ad23a2dbSjohansen 		/*
2231ad23a2dbSjohansen 		 * On some platforms, kvm_init is called after the kpm
2232ad23a2dbSjohansen 		 * sizes have been determined.  On SPARC, kvm_init is called
2233ad23a2dbSjohansen 		 * before, so we have to attach the kzioseg after kvm is
2234ad23a2dbSjohansen 		 * initialized, otherwise we'll try to allocate from the boot
2235ad23a2dbSjohansen 		 * area since the kernel heap hasn't yet been configured.
2236ad23a2dbSjohansen 		 */
2237ad23a2dbSjohansen 		rw_enter(&kas.a_lock, RW_WRITER);
2238ad23a2dbSjohansen 
2239ad23a2dbSjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
2240ad23a2dbSjohansen 		    &kzioseg);
2241ad23a2dbSjohansen 		(void) segkmem_zio_create(&kzioseg);
2242ad23a2dbSjohansen 
2243ad23a2dbSjohansen 		/* create zio area covering new segment */
2244ad23a2dbSjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
2245ad23a2dbSjohansen 
2246ad23a2dbSjohansen 		rw_exit(&kas.a_lock);
2247ad23a2dbSjohansen 	}
2248ad23a2dbSjohansen 
2249ad23a2dbSjohansen 
22507c478bd9Sstevel@tonic-gate 	/*
22517c478bd9Sstevel@tonic-gate 	 * Now create generic mapping segment.  This mapping
22527c478bd9Sstevel@tonic-gate 	 * goes SEGMAPSIZE beyond SEGMAPBASE.  But if the total
22537c478bd9Sstevel@tonic-gate 	 * virtual address is greater than the amount of free
22547c478bd9Sstevel@tonic-gate 	 * memory that is available, then we trim back the
22557c478bd9Sstevel@tonic-gate 	 * segment size to that amount
22567c478bd9Sstevel@tonic-gate 	 */
22577c478bd9Sstevel@tonic-gate 	va = (caddr_t)SEGMAPBASE;
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	/*
22607c478bd9Sstevel@tonic-gate 	 * 1201049: segkmap base address must be MAXBSIZE aligned
22617c478bd9Sstevel@tonic-gate 	 */
22627c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & MAXBOFFSET) == 0);
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	/*
22657c478bd9Sstevel@tonic-gate 	 * Set size of segmap to percentage of freemem at boot,
22667c478bd9Sstevel@tonic-gate 	 * but stay within the allowable range
22677c478bd9Sstevel@tonic-gate 	 * Note we take percentage  before converting from pages
22687c478bd9Sstevel@tonic-gate 	 * to bytes to avoid an overflow on 32-bit kernels.
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	i = mmu_ptob((freemem * segmap_percent) / 100);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	if (i < MINMAPSIZE)
22737c478bd9Sstevel@tonic-gate 		i = MINMAPSIZE;
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	if (i > MIN(SEGMAPSIZE, mmu_ptob(freemem)))
22767c478bd9Sstevel@tonic-gate 		i = MIN(SEGMAPSIZE, mmu_ptob(freemem));
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	i &= MAXBMASK;	/* 1201049: segkmap size must be MAXBSIZE aligned */
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
22817c478bd9Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkmap) < 0)
22827c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cannot attach segkmap");
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
22857c478bd9Sstevel@tonic-gate 	a.shmsize = shm_alignment;
22867c478bd9Sstevel@tonic-gate 	a.nfreelist = 0;	/* use segmap driver defaults */
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
22897c478bd9Sstevel@tonic-gate 		panic("segmap_create segkmap");
22907c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	segdev_init();
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate static void
22967c478bd9Sstevel@tonic-gate startup_end(void)
22977c478bd9Sstevel@tonic-gate {
22987c478bd9Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
22997c478bd9Sstevel@tonic-gate 		panic("memlist overflow 2");
23007c478bd9Sstevel@tonic-gate 	memlist_free_block((caddr_t)memlist,
23017c478bd9Sstevel@tonic-gate 	    ((caddr_t)memlist_end - (caddr_t)memlist));
23027c478bd9Sstevel@tonic-gate 	memlist = NULL;
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	/* enable page_relocation since OBP is now done */
23057c478bd9Sstevel@tonic-gate 	page_relocate_ready = 1;
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	/*
23087c478bd9Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
23097c478bd9Sstevel@tonic-gate 	 * initialization has been done but before the clock
23107c478bd9Sstevel@tonic-gate 	 * and other devices get started.
23117c478bd9Sstevel@tonic-gate 	 */
23127c478bd9Sstevel@tonic-gate 	kern_setup1();
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	/*
23157c478bd9Sstevel@tonic-gate 	 * Intialize the VM arenas for allocating physically
23167c478bd9Sstevel@tonic-gate 	 * contiguus memory chunk for interrupt queues snd
23177c478bd9Sstevel@tonic-gate 	 * allocate/register boot cpu's queues, if any and
23187c478bd9Sstevel@tonic-gate 	 * allocate dump buffer for sun4v systems to store
23197c478bd9Sstevel@tonic-gate 	 * extra crash information during crash dump
23207c478bd9Sstevel@tonic-gate 	 */
23217c478bd9Sstevel@tonic-gate 	contig_mem_init();
23227c478bd9Sstevel@tonic-gate 	mach_descrip_init();
2323982b9107Sjb145095 
2324982b9107Sjb145095 	if (cpu_intrq_setup(CPU)) {
2325982b9107Sjb145095 		cmn_err(CE_PANIC, "cpu%d: setup failed", CPU->cpu_id);
2326982b9107Sjb145095 	}
23277c478bd9Sstevel@tonic-gate 	cpu_intrq_register(CPU);
2328db6d2ee3Ssvemuri 	mach_htraptrace_setup(CPU->cpu_id);
2329db6d2ee3Ssvemuri 	mach_htraptrace_configure(CPU->cpu_id);
23307c478bd9Sstevel@tonic-gate 	mach_dump_buffer_init();
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	/*
23337c478bd9Sstevel@tonic-gate 	 * Initialize interrupt related stuff
23347c478bd9Sstevel@tonic-gate 	 */
2335100b72f4Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	(void) splzs();			/* allow hi clock ints but not zs */
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	/*
23407c478bd9Sstevel@tonic-gate 	 * Initialize errors.
23417c478bd9Sstevel@tonic-gate 	 */
23427c478bd9Sstevel@tonic-gate 	error_init();
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	/*
23457c478bd9Sstevel@tonic-gate 	 * Note that we may have already used kernel bcopy before this
23467c478bd9Sstevel@tonic-gate 	 * point - but if you really care about this, adb the use_hw_*
23477c478bd9Sstevel@tonic-gate 	 * variables to 0 before rebooting.
23487c478bd9Sstevel@tonic-gate 	 */
23497c478bd9Sstevel@tonic-gate 	mach_hw_copy_limit();
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	/*
23527c478bd9Sstevel@tonic-gate 	 * Install the "real" preemption guards before DDI services
23537c478bd9Sstevel@tonic-gate 	 * are available.
23547c478bd9Sstevel@tonic-gate 	 */
23557c478bd9Sstevel@tonic-gate 	(void) prom_set_preprom(kern_preprom);
23567c478bd9Sstevel@tonic-gate 	(void) prom_set_postprom(kern_postprom);
23577c478bd9Sstevel@tonic-gate 	CPU->cpu_m.mutex_ready = 1;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	/*
23607c478bd9Sstevel@tonic-gate 	 * Initialize segnf (kernel support for non-faulting loads).
23617c478bd9Sstevel@tonic-gate 	 */
23627c478bd9Sstevel@tonic-gate 	segnf_init();
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	/*
23657c478bd9Sstevel@tonic-gate 	 * Configure the root devinfo node.
23667c478bd9Sstevel@tonic-gate 	 */
23677c478bd9Sstevel@tonic-gate 	configure();		/* set up devices */
23687c478bd9Sstevel@tonic-gate 	mach_cpu_halt_idle();
23697c478bd9Sstevel@tonic-gate }
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate void
23737c478bd9Sstevel@tonic-gate post_startup(void)
23747c478bd9Sstevel@tonic-gate {
23757c478bd9Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
23767c478bd9Sstevel@tonic-gate 	extern void init_ptl1_thread(void);
23777c478bd9Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
23787c478bd9Sstevel@tonic-gate 	extern void abort_sequence_init(void);
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	/*
23817c478bd9Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
23827c478bd9Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
23837c478bd9Sstevel@tonic-gate 	 * instruction set extensions.
23847c478bd9Sstevel@tonic-gate 	 */
23857c478bd9Sstevel@tonic-gate 	bind_hwcap();
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	/*
23887c478bd9Sstevel@tonic-gate 	 * Startup memory scrubber (if any)
23897c478bd9Sstevel@tonic-gate 	 */
23907c478bd9Sstevel@tonic-gate 	mach_memscrub();
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	/*
23937c478bd9Sstevel@tonic-gate 	 * Allocate soft interrupt to handle abort sequence.
23947c478bd9Sstevel@tonic-gate 	 */
23957c478bd9Sstevel@tonic-gate 	abort_sequence_init();
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 	/*
23987c478bd9Sstevel@tonic-gate 	 * Configure the rest of the system.
23997c478bd9Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
24007c478bd9Sstevel@tonic-gate 	 */
24017c478bd9Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
24027c478bd9Sstevel@tonic-gate 	/*
24037c478bd9Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
24047c478bd9Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
24057c478bd9Sstevel@tonic-gate 	 */
24067c478bd9Sstevel@tonic-gate 	(void) modload("fs", "procfs");
24077c478bd9Sstevel@tonic-gate 
24081ae08745Sheppo 	/* load machine class specific drivers */
24091ae08745Sheppo 	load_mach_drivers();
24101ae08745Sheppo 
24111ae08745Sheppo 	/* load platform specific drivers */
24127c478bd9Sstevel@tonic-gate 	if (&load_platform_drivers)
24137c478bd9Sstevel@tonic-gate 		load_platform_drivers();
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	/* load vis simulation module, if we are running w/fpu off */
24167c478bd9Sstevel@tonic-gate 	if (!fpu_exists) {
24177c478bd9Sstevel@tonic-gate 		if (modload("misc", "vis") == -1)
24187c478bd9Sstevel@tonic-gate 			halt("Can't load vis");
24197c478bd9Sstevel@tonic-gate 	}
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	mach_fpras();
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	maxmem = freemem;
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
24267c478bd9Sstevel@tonic-gate 	init_ptl1_thread();
24277c478bd9Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
24281ae08745Sheppo 
24291ae08745Sheppo 	if (&cif_init)
24301ae08745Sheppo 		cif_init();
24317c478bd9Sstevel@tonic-gate }
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
24347c478bd9Sstevel@tonic-gate int		ptl1_panic_test = 0;
24357c478bd9Sstevel@tonic-gate int		ptl1_panic_xc_one_test = 0;
24367c478bd9Sstevel@tonic-gate int		ptl1_panic_xc_all_test = 0;
24377c478bd9Sstevel@tonic-gate int		ptl1_panic_xt_one_test = 0;
24387c478bd9Sstevel@tonic-gate int		ptl1_panic_xt_all_test = 0;
24397c478bd9Sstevel@tonic-gate kthread_id_t	ptl1_thread_p = NULL;
24407c478bd9Sstevel@tonic-gate kcondvar_t	ptl1_cv;
24417c478bd9Sstevel@tonic-gate kmutex_t	ptl1_mutex;
24427c478bd9Sstevel@tonic-gate int		ptl1_recurse_count_threshold = 0x40;
24437c478bd9Sstevel@tonic-gate int		ptl1_recurse_trap_threshold = 0x3d;
24447c478bd9Sstevel@tonic-gate extern void	ptl1_recurse(int, int);
24457c478bd9Sstevel@tonic-gate extern void	ptl1_panic_xt(int, int);
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate /*
24487c478bd9Sstevel@tonic-gate  * Called once per second by timeout() to wake up
24497c478bd9Sstevel@tonic-gate  * the ptl1_panic thread to see if it should cause
24507c478bd9Sstevel@tonic-gate  * a trap to the ptl1_panic() code.
24517c478bd9Sstevel@tonic-gate  */
24527c478bd9Sstevel@tonic-gate /* ARGSUSED */
24537c478bd9Sstevel@tonic-gate static void
24547c478bd9Sstevel@tonic-gate ptl1_wakeup(void *arg)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
24577c478bd9Sstevel@tonic-gate 	cv_signal(&ptl1_cv);
24587c478bd9Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
24597c478bd9Sstevel@tonic-gate }
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate /*
24627c478bd9Sstevel@tonic-gate  * ptl1_panic cross call function:
24637c478bd9Sstevel@tonic-gate  *     Needed because xc_one() and xc_some() can pass
24647c478bd9Sstevel@tonic-gate  *	64 bit args but ptl1_recurse() expects ints.
24657c478bd9Sstevel@tonic-gate  */
24667c478bd9Sstevel@tonic-gate static void
24677c478bd9Sstevel@tonic-gate ptl1_panic_xc(void)
24687c478bd9Sstevel@tonic-gate {
24697c478bd9Sstevel@tonic-gate 	ptl1_recurse(ptl1_recurse_count_threshold,
24707c478bd9Sstevel@tonic-gate 	    ptl1_recurse_trap_threshold);
24717c478bd9Sstevel@tonic-gate }
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate /*
24747c478bd9Sstevel@tonic-gate  * The ptl1 thread waits for a global flag to be set
24757c478bd9Sstevel@tonic-gate  * and uses the recurse thresholds to set the stack depth
24767c478bd9Sstevel@tonic-gate  * to cause a ptl1_panic() directly via a call to ptl1_recurse
24777c478bd9Sstevel@tonic-gate  * or indirectly via the cross call and cross trap functions.
24787c478bd9Sstevel@tonic-gate  *
24797c478bd9Sstevel@tonic-gate  * This is useful testing stack overflows and normal
24807c478bd9Sstevel@tonic-gate  * ptl1_panic() states with a know stack frame.
24817c478bd9Sstevel@tonic-gate  *
24827c478bd9Sstevel@tonic-gate  * ptl1_recurse() is an asm function in ptl1_panic.s that
24837c478bd9Sstevel@tonic-gate  * sets the {In, Local, Out, and Global} registers to a
24847c478bd9Sstevel@tonic-gate  * know state on the stack and just prior to causing a
24857c478bd9Sstevel@tonic-gate  * test ptl1_panic trap.
24867c478bd9Sstevel@tonic-gate  */
24877c478bd9Sstevel@tonic-gate static void
24887c478bd9Sstevel@tonic-gate ptl1_thread(void)
24897c478bd9Sstevel@tonic-gate {
24907c478bd9Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
24917c478bd9Sstevel@tonic-gate 	while (ptl1_thread_p) {
24927c478bd9Sstevel@tonic-gate 		cpuset_t	other_cpus;
24937c478bd9Sstevel@tonic-gate 		int		cpu_id;
24947c478bd9Sstevel@tonic-gate 		int		my_cpu_id;
24957c478bd9Sstevel@tonic-gate 		int		target_cpu_id;
24967c478bd9Sstevel@tonic-gate 		int		target_found;
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 		if (ptl1_panic_test) {
24997c478bd9Sstevel@tonic-gate 			ptl1_recurse(ptl1_recurse_count_threshold,
25007c478bd9Sstevel@tonic-gate 			    ptl1_recurse_trap_threshold);
25017c478bd9Sstevel@tonic-gate 		}
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 		/*
25047c478bd9Sstevel@tonic-gate 		 * Find potential targets for x-call and x-trap,
25057c478bd9Sstevel@tonic-gate 		 * if any exist while preempt is disabled we
25067c478bd9Sstevel@tonic-gate 		 * start a ptl1_panic if requested via a
25077c478bd9Sstevel@tonic-gate 		 * globals.
25087c478bd9Sstevel@tonic-gate 		 */
25097c478bd9Sstevel@tonic-gate 		kpreempt_disable();
25107c478bd9Sstevel@tonic-gate 		my_cpu_id = CPU->cpu_id;
25117c478bd9Sstevel@tonic-gate 		other_cpus = cpu_ready_set;
25127c478bd9Sstevel@tonic-gate 		CPUSET_DEL(other_cpus, CPU->cpu_id);
25137c478bd9Sstevel@tonic-gate 		target_found = 0;
25147c478bd9Sstevel@tonic-gate 		if (!CPUSET_ISNULL(other_cpus)) {
25157c478bd9Sstevel@tonic-gate 			/*
25167c478bd9Sstevel@tonic-gate 			 * Pick the first one
25177c478bd9Sstevel@tonic-gate 			 */
25187c478bd9Sstevel@tonic-gate 			for (cpu_id = 0; cpu_id < NCPU; cpu_id++) {
25197c478bd9Sstevel@tonic-gate 				if (cpu_id == my_cpu_id)
25207c478bd9Sstevel@tonic-gate 					continue;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 				if (CPU_XCALL_READY(cpu_id)) {
25237c478bd9Sstevel@tonic-gate 					target_cpu_id = cpu_id;
25247c478bd9Sstevel@tonic-gate 					target_found = 1;
25257c478bd9Sstevel@tonic-gate 					break;
25267c478bd9Sstevel@tonic-gate 				}
25277c478bd9Sstevel@tonic-gate 			}
25287c478bd9Sstevel@tonic-gate 			ASSERT(target_found);
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 			if (ptl1_panic_xc_one_test) {
25317c478bd9Sstevel@tonic-gate 				xc_one(target_cpu_id,
25327c478bd9Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
25337c478bd9Sstevel@tonic-gate 			}
25347c478bd9Sstevel@tonic-gate 			if (ptl1_panic_xc_all_test) {
25357c478bd9Sstevel@tonic-gate 				xc_some(other_cpus,
25367c478bd9Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
25377c478bd9Sstevel@tonic-gate 			}
25387c478bd9Sstevel@tonic-gate 			if (ptl1_panic_xt_one_test) {
25397c478bd9Sstevel@tonic-gate 				xt_one(target_cpu_id,
25407c478bd9Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
25417c478bd9Sstevel@tonic-gate 			}
25427c478bd9Sstevel@tonic-gate 			if (ptl1_panic_xt_all_test) {
25437c478bd9Sstevel@tonic-gate 				xt_some(other_cpus,
25447c478bd9Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
25457c478bd9Sstevel@tonic-gate 			}
25467c478bd9Sstevel@tonic-gate 		}
25477c478bd9Sstevel@tonic-gate 		kpreempt_enable();
25487c478bd9Sstevel@tonic-gate 		(void) timeout(ptl1_wakeup, NULL, hz);
25497c478bd9Sstevel@tonic-gate 		(void) cv_wait(&ptl1_cv, &ptl1_mutex);
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate /*
25557c478bd9Sstevel@tonic-gate  * Called during early startup to create the ptl1_thread
25567c478bd9Sstevel@tonic-gate  */
25577c478bd9Sstevel@tonic-gate void
25587c478bd9Sstevel@tonic-gate init_ptl1_thread(void)
25597c478bd9Sstevel@tonic-gate {
25607c478bd9Sstevel@tonic-gate 	ptl1_thread_p = thread_create(NULL, 0, ptl1_thread, NULL, 0,
25617c478bd9Sstevel@tonic-gate 	    &p0, TS_RUN, 0);
25627c478bd9Sstevel@tonic-gate }
25637c478bd9Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate /*
25677c478bd9Sstevel@tonic-gate  * Add to a memory list.
25687c478bd9Sstevel@tonic-gate  * start = start of new memory segment
25697c478bd9Sstevel@tonic-gate  * len = length of new memory segment in bytes
25707c478bd9Sstevel@tonic-gate  * memlistp = pointer to array of available memory segment structures
25717c478bd9Sstevel@tonic-gate  * curmemlistp = memory list to which to add segment.
25727c478bd9Sstevel@tonic-gate  */
25737c478bd9Sstevel@tonic-gate static void
25747c478bd9Sstevel@tonic-gate memlist_add(uint64_t start, uint64_t len, struct memlist **memlistp,
25757c478bd9Sstevel@tonic-gate 	struct memlist **curmemlistp)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	struct memlist *new;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	new = *memlistp;
25807c478bd9Sstevel@tonic-gate 	new->address = start;
25817c478bd9Sstevel@tonic-gate 	new->size = len;
25827c478bd9Sstevel@tonic-gate 	*memlistp = new + 1;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	memlist_insert(new, curmemlistp);
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate /*
25887c478bd9Sstevel@tonic-gate  * In the case of architectures that support dynamic addition of
25897c478bd9Sstevel@tonic-gate  * memory at run-time there are two cases where memsegs need to
25907c478bd9Sstevel@tonic-gate  * be initialized and added to the memseg list.
25917c478bd9Sstevel@tonic-gate  * 1) memsegs that are constructed at startup.
25927c478bd9Sstevel@tonic-gate  * 2) memsegs that are constructed at run-time on
25937c478bd9Sstevel@tonic-gate  *    hot-plug capable architectures.
25947c478bd9Sstevel@tonic-gate  * This code was originally part of the function kphysm_init().
25957c478bd9Sstevel@tonic-gate  */
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate static void
25987c478bd9Sstevel@tonic-gate memseg_list_add(struct memseg *memsegp)
25997c478bd9Sstevel@tonic-gate {
26007c478bd9Sstevel@tonic-gate 	struct memseg **prev_memsegp;
26017c478bd9Sstevel@tonic-gate 	pgcnt_t num;
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	/* insert in memseg list, decreasing number of pages order */
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	num = MSEG_NPAGES(memsegp);
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	for (prev_memsegp = &memsegs; *prev_memsegp;
26087c478bd9Sstevel@tonic-gate 	    prev_memsegp = &((*prev_memsegp)->next)) {
26097c478bd9Sstevel@tonic-gate 		if (num > MSEG_NPAGES(*prev_memsegp))
26107c478bd9Sstevel@tonic-gate 			break;
26117c478bd9Sstevel@tonic-gate 	}
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	memsegp->next = *prev_memsegp;
26147c478bd9Sstevel@tonic-gate 	*prev_memsegp = memsegp;
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	if (kpm_enable) {
26177c478bd9Sstevel@tonic-gate 		memsegp->nextpa = (memsegp->next) ?
26187c478bd9Sstevel@tonic-gate 			va_to_pa(memsegp->next) : MSEG_NULLPTR_PA;
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 		if (prev_memsegp != &memsegs) {
26217c478bd9Sstevel@tonic-gate 			struct memseg *msp;
26227c478bd9Sstevel@tonic-gate 			msp = (struct memseg *)((caddr_t)prev_memsegp -
26237c478bd9Sstevel@tonic-gate 				offsetof(struct memseg, next));
26247c478bd9Sstevel@tonic-gate 			msp->nextpa = va_to_pa(memsegp);
26257c478bd9Sstevel@tonic-gate 		} else {
26267c478bd9Sstevel@tonic-gate 			memsegspa = va_to_pa(memsegs);
26277c478bd9Sstevel@tonic-gate 		}
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate }
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate /*
26327c478bd9Sstevel@tonic-gate  * PSM add_physmem_cb(). US-II and newer processors have some
26337c478bd9Sstevel@tonic-gate  * flavor of the prefetch capability implemented. We exploit
26347c478bd9Sstevel@tonic-gate  * this capability for optimum performance.
26357c478bd9Sstevel@tonic-gate  */
26367c478bd9Sstevel@tonic-gate #define	PREFETCH_BYTES	64
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate void
26397c478bd9Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
26407c478bd9Sstevel@tonic-gate {
26417c478bd9Sstevel@tonic-gate 	extern void	 prefetch_page_w(void *);
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	pp->p_pagenum = pnum;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	/*
26467c478bd9Sstevel@tonic-gate 	 * Prefetch one more page_t into E$. To prevent future
26477c478bd9Sstevel@tonic-gate 	 * mishaps with the sizeof(page_t) changing on us, we
26487c478bd9Sstevel@tonic-gate 	 * catch this on debug kernels if we can't bring in the
26497c478bd9Sstevel@tonic-gate 	 * entire hpage with 2 PREFETCH_BYTES reads. See
26507c478bd9Sstevel@tonic-gate 	 * also, sun4u/cpu/cpu_module.c
26517c478bd9Sstevel@tonic-gate 	 */
26527c478bd9Sstevel@tonic-gate 	/*LINTED*/
26537c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (page_t) <= 2*PREFETCH_BYTES);
26547c478bd9Sstevel@tonic-gate 	prefetch_page_w((char *)pp);
26557c478bd9Sstevel@tonic-gate }
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate /*
26587c478bd9Sstevel@tonic-gate  * kphysm_init() tackles the problem of initializing physical memory.
26597c478bd9Sstevel@tonic-gate  * The old startup made some assumptions about the kernel living in
26607c478bd9Sstevel@tonic-gate  * physically contiguous space which is no longer valid.
26617c478bd9Sstevel@tonic-gate  */
26627c478bd9Sstevel@tonic-gate static void
26637c478bd9Sstevel@tonic-gate kphysm_init(page_t *pp, struct memseg *memsegp, pgcnt_t npages,
26647c478bd9Sstevel@tonic-gate 	uintptr_t kpm_pp, pgcnt_t kpm_npages)
26657c478bd9Sstevel@tonic-gate {
26667c478bd9Sstevel@tonic-gate 	struct memlist	*pmem;
26677c478bd9Sstevel@tonic-gate 	struct memseg	*msp;
26687c478bd9Sstevel@tonic-gate 	pfn_t		 base;
26697c478bd9Sstevel@tonic-gate 	pgcnt_t		 num;
26707c478bd9Sstevel@tonic-gate 	pfn_t		 lastseg_pages_end = 0;
26717c478bd9Sstevel@tonic-gate 	pgcnt_t		 nelem_used = 0;
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	ASSERT(page_hash != NULL && page_hashsz != 0);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	msp = memsegp;
26767c478bd9Sstevel@tonic-gate 	for (pmem = phys_avail; pmem && npages; pmem = pmem->next) {
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 		/*
26797c478bd9Sstevel@tonic-gate 		 * Build the memsegs entry
26807c478bd9Sstevel@tonic-gate 		 */
26817c478bd9Sstevel@tonic-gate 		num = btop(pmem->size);
26827c478bd9Sstevel@tonic-gate 		if (num > npages)
26837c478bd9Sstevel@tonic-gate 			num = npages;
26847c478bd9Sstevel@tonic-gate 		npages -= num;
26857c478bd9Sstevel@tonic-gate 		base = btop(pmem->address);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 		msp->pages = pp;
26887c478bd9Sstevel@tonic-gate 		msp->epages = pp + num;
26897c478bd9Sstevel@tonic-gate 		msp->pages_base = base;
26907c478bd9Sstevel@tonic-gate 		msp->pages_end = base + num;
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 		if (kpm_enable) {
26937c478bd9Sstevel@tonic-gate 			pfn_t pbase_a;
26947c478bd9Sstevel@tonic-gate 			pfn_t pend_a;
26957c478bd9Sstevel@tonic-gate 			pfn_t prev_pend_a;
26967c478bd9Sstevel@tonic-gate 			pgcnt_t	nelem;
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 			msp->pagespa = va_to_pa(pp);
26997c478bd9Sstevel@tonic-gate 			msp->epagespa = va_to_pa(pp + num);
27007c478bd9Sstevel@tonic-gate 			pbase_a = kpmptop(ptokpmp(base));
27017c478bd9Sstevel@tonic-gate 			pend_a = kpmptop(ptokpmp(base + num - 1)) + kpmpnpgs;
27027c478bd9Sstevel@tonic-gate 			nelem = ptokpmp(pend_a - pbase_a);
27037c478bd9Sstevel@tonic-gate 			msp->kpm_nkpmpgs = nelem;
27047c478bd9Sstevel@tonic-gate 			msp->kpm_pbase = pbase_a;
27057c478bd9Sstevel@tonic-gate 			if (lastseg_pages_end) {
27067c478bd9Sstevel@tonic-gate 				/*
27077c478bd9Sstevel@tonic-gate 				 * Assume phys_avail is in ascending order
27087c478bd9Sstevel@tonic-gate 				 * of physical addresses.
27097c478bd9Sstevel@tonic-gate 				 */
27107c478bd9Sstevel@tonic-gate 				ASSERT(base + num > lastseg_pages_end);
27117c478bd9Sstevel@tonic-gate 				prev_pend_a = kpmptop(
27127c478bd9Sstevel@tonic-gate 				    ptokpmp(lastseg_pages_end - 1)) + kpmpnpgs;
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 				if (prev_pend_a > pbase_a) {
27157c478bd9Sstevel@tonic-gate 					/*
27167c478bd9Sstevel@tonic-gate 					 * Overlap, more than one memseg may
27177c478bd9Sstevel@tonic-gate 					 * point to the same kpm_page range.
27187c478bd9Sstevel@tonic-gate 					 */
27197c478bd9Sstevel@tonic-gate 					if (kpm_smallpages == 0) {
27207c478bd9Sstevel@tonic-gate 						msp->kpm_pages =
27217c478bd9Sstevel@tonic-gate 						    (kpm_page_t *)kpm_pp - 1;
27227c478bd9Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27237c478bd9Sstevel@tonic-gate 							((kpm_page_t *)kpm_pp
27247c478bd9Sstevel@tonic-gate 							+ nelem - 1);
27257c478bd9Sstevel@tonic-gate 					} else {
27267c478bd9Sstevel@tonic-gate 						msp->kpm_spages =
27277c478bd9Sstevel@tonic-gate 						    (kpm_spage_t *)kpm_pp - 1;
27287c478bd9Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27297c478bd9Sstevel@tonic-gate 							((kpm_spage_t *)kpm_pp
27307c478bd9Sstevel@tonic-gate 							+ nelem - 1);
27317c478bd9Sstevel@tonic-gate 					}
27327c478bd9Sstevel@tonic-gate 					nelem_used += nelem - 1;
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 				} else {
27357c478bd9Sstevel@tonic-gate 					if (kpm_smallpages == 0) {
27367c478bd9Sstevel@tonic-gate 						msp->kpm_pages =
27377c478bd9Sstevel@tonic-gate 						    (kpm_page_t *)kpm_pp;
27387c478bd9Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27397c478bd9Sstevel@tonic-gate 							((kpm_page_t *)kpm_pp
27407c478bd9Sstevel@tonic-gate 							+ nelem);
27417c478bd9Sstevel@tonic-gate 					} else {
27427c478bd9Sstevel@tonic-gate 						msp->kpm_spages =
27437c478bd9Sstevel@tonic-gate 						    (kpm_spage_t *)kpm_pp;
27447c478bd9Sstevel@tonic-gate 						kpm_pp = (uintptr_t)
27457c478bd9Sstevel@tonic-gate 							((kpm_spage_t *)
27467c478bd9Sstevel@tonic-gate 							kpm_pp + nelem);
27477c478bd9Sstevel@tonic-gate 					}
27487c478bd9Sstevel@tonic-gate 					nelem_used += nelem;
27497c478bd9Sstevel@tonic-gate 				}
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 			} else {
27527c478bd9Sstevel@tonic-gate 				if (kpm_smallpages == 0) {
27537c478bd9Sstevel@tonic-gate 					msp->kpm_pages = (kpm_page_t *)kpm_pp;
27547c478bd9Sstevel@tonic-gate 					kpm_pp = (uintptr_t)
27557c478bd9Sstevel@tonic-gate 						((kpm_page_t *)kpm_pp + nelem);
27567c478bd9Sstevel@tonic-gate 				} else {
27577c478bd9Sstevel@tonic-gate 					msp->kpm_spages = (kpm_spage_t *)kpm_pp;
27587c478bd9Sstevel@tonic-gate 					kpm_pp = (uintptr_t)
27597c478bd9Sstevel@tonic-gate 						((kpm_spage_t *)kpm_pp + nelem);
27607c478bd9Sstevel@tonic-gate 				}
27617c478bd9Sstevel@tonic-gate 				nelem_used = nelem;
27627c478bd9Sstevel@tonic-gate 			}
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 			if (nelem_used > kpm_npages)
27657c478bd9Sstevel@tonic-gate 				panic("kphysm_init: kpm_pp overflow\n");
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 			msp->kpm_pagespa = va_to_pa(msp->kpm_pages);
27687c478bd9Sstevel@tonic-gate 			lastseg_pages_end = msp->pages_end;
27697c478bd9Sstevel@tonic-gate 		}
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 		memseg_list_add(msp);
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 		/*
27747c478bd9Sstevel@tonic-gate 		 * add_physmem() initializes the PSM part of the page
27757c478bd9Sstevel@tonic-gate 		 * struct by calling the PSM back with add_physmem_cb().
27767c478bd9Sstevel@tonic-gate 		 * In addition it coalesces pages into larger pages as
27777c478bd9Sstevel@tonic-gate 		 * it initializes them.
27787c478bd9Sstevel@tonic-gate 		 */
27797c478bd9Sstevel@tonic-gate 		add_physmem(pp, num, base);
27807c478bd9Sstevel@tonic-gate 		pp += num;
27817c478bd9Sstevel@tonic-gate 		msp++;
27827c478bd9Sstevel@tonic-gate 	}
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	build_pfn_hash();
27857c478bd9Sstevel@tonic-gate }
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate /*
27887c478bd9Sstevel@tonic-gate  * Kernel VM initialization.
27897c478bd9Sstevel@tonic-gate  * Assumptions about kernel address space ordering:
27907c478bd9Sstevel@tonic-gate  *	(1) gap (user space)
27917c478bd9Sstevel@tonic-gate  *	(2) kernel text
27927c478bd9Sstevel@tonic-gate  *	(3) kernel data/bss
27937c478bd9Sstevel@tonic-gate  *	(4) gap
27947c478bd9Sstevel@tonic-gate  *	(5) kernel data structures
27957c478bd9Sstevel@tonic-gate  *	(6) gap
27967c478bd9Sstevel@tonic-gate  *	(7) debugger (optional)
27977c478bd9Sstevel@tonic-gate  *	(8) monitor
27987c478bd9Sstevel@tonic-gate  *	(9) gap (possibly null)
27997c478bd9Sstevel@tonic-gate  *	(10) dvma
28007c478bd9Sstevel@tonic-gate  *	(11) devices
28017c478bd9Sstevel@tonic-gate  */
28027c478bd9Sstevel@tonic-gate static void
28037c478bd9Sstevel@tonic-gate kvm_init(void)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate 	/*
28067c478bd9Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
28077c478bd9Sstevel@tonic-gate 	 */
28087c478bd9Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
28097c478bd9Sstevel@tonic-gate 	as_avlinit(&kas);
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)KERNELBASE,
28127c478bd9Sstevel@tonic-gate 	    (size_t)(e_moddata - KERNELBASE), &ktextseg);
28137c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)(KERNELBASE + MMU_PAGESIZE4M),
28167c478bd9Sstevel@tonic-gate 	    (size_t)(MMU_PAGESIZE4M), &ktexthole);
28177c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&ktexthole);
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base,
28207c478bd9Sstevel@tonic-gate 	    (size_t)(econtig32 - valloc_base), &kvalloc);
28217c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	if (kmem64_base) {
28247c478bd9Sstevel@tonic-gate 	    (void) seg_attach(&kas, (caddr_t)kmem64_base,
28257c478bd9Sstevel@tonic-gate 		(size_t)(kmem64_end - kmem64_base), &kmem64);
28267c478bd9Sstevel@tonic-gate 	    (void) segkmem_create(&kmem64);
28277c478bd9Sstevel@tonic-gate 	}
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	/*
28307c478bd9Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
28317c478bd9Sstevel@tonic-gate 	 * system resource management transition. We can no longer
28327c478bd9Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
28337c478bd9Sstevel@tonic-gate 	 */
28347c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, kernelheap, ekernelheap - kernelheap, &kvseg);
28357c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
28367c478bd9Sstevel@tonic-gate 	hblk_alloc_dynamic = 1;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	/*
28397c478bd9Sstevel@tonic-gate 	 * we need to preallocate pages for DR operations before enabling large
28407c478bd9Sstevel@tonic-gate 	 * page kernel heap because of memseg_remap_init() hat_unload() hack.
28417c478bd9Sstevel@tonic-gate 	 */
28427c478bd9Sstevel@tonic-gate 	memseg_remap_init();
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	/* at this point we are ready to use large page heap */
28457c478bd9Sstevel@tonic-gate 	segkmem_heap_lp_init();
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SYSBASE32, SYSLIMIT32 - SYSBASE32,
28487c478bd9Sstevel@tonic-gate 	    &kvseg32);
28497c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kvseg32);
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	/*
28527c478bd9Sstevel@tonic-gate 	 * Create a segment for the debugger.
28537c478bd9Sstevel@tonic-gate 	 */
2854ae115bc7Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
28557c478bd9Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
28587c478bd9Sstevel@tonic-gate }
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate char obp_tte_str[] =
28617c478bd9Sstevel@tonic-gate 	"h# %x constant MMU_PAGESHIFT "
28627c478bd9Sstevel@tonic-gate 	"h# %x constant TTE8K "
28637c478bd9Sstevel@tonic-gate 	"h# %x constant SFHME_SIZE "
28647c478bd9Sstevel@tonic-gate 	"h# %x constant SFHME_TTE "
28657c478bd9Sstevel@tonic-gate 	"h# %x constant HMEBLK_TAG "
28667c478bd9Sstevel@tonic-gate 	"h# %x constant HMEBLK_NEXT "
28677c478bd9Sstevel@tonic-gate 	"h# %x constant HMEBLK_MISC "
28687c478bd9Sstevel@tonic-gate 	"h# %x constant HMEBLK_HME1 "
28697c478bd9Sstevel@tonic-gate 	"h# %x constant NHMENTS "
28707c478bd9Sstevel@tonic-gate 	"h# %x constant HBLK_SZMASK "
28717c478bd9Sstevel@tonic-gate 	"h# %x constant HBLK_RANGE_SHIFT "
28727c478bd9Sstevel@tonic-gate 	"h# %x constant HMEBP_HBLK "
28737c478bd9Sstevel@tonic-gate 	"h# %x constant HMEBUCKET_SIZE "
28747c478bd9Sstevel@tonic-gate 	"h# %x constant HTAG_SFMMUPSZ "
28757c478bd9Sstevel@tonic-gate 	"h# %x constant HTAG_REHASHSZ "
28767c478bd9Sstevel@tonic-gate 	"h# %x constant mmu_hashcnt "
28777c478bd9Sstevel@tonic-gate 	"h# %p constant uhme_hash "
28787c478bd9Sstevel@tonic-gate 	"h# %p constant khme_hash "
28797c478bd9Sstevel@tonic-gate 	"h# %x constant UHMEHASH_SZ "
28807c478bd9Sstevel@tonic-gate 	"h# %x constant KHMEHASH_SZ "
28811e2e7a75Shuah 	"h# %p constant KCONTEXT "
28827c478bd9Sstevel@tonic-gate 	"h# %p constant KHATID "
28837c478bd9Sstevel@tonic-gate 	"h# %x constant ASI_MEM "
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	": PHYS-X@ ( phys -- data ) "
28867c478bd9Sstevel@tonic-gate 	"   ASI_MEM spacex@ "
28877c478bd9Sstevel@tonic-gate 	"; "
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	": PHYS-W@ ( phys -- data ) "
28907c478bd9Sstevel@tonic-gate 	"   ASI_MEM spacew@ "
28917c478bd9Sstevel@tonic-gate 	"; "
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	": PHYS-L@ ( phys -- data ) "
28947c478bd9Sstevel@tonic-gate 	"   ASI_MEM spaceL@ "
28957c478bd9Sstevel@tonic-gate 	"; "
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	": TTE_PAGE_SHIFT ( ttesz -- hmeshift ) "
28987c478bd9Sstevel@tonic-gate 	"   3 * MMU_PAGESHIFT + "
28997c478bd9Sstevel@tonic-gate 	"; "
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	": TTE_IS_VALID ( ttep -- flag ) "
29027c478bd9Sstevel@tonic-gate 	"   PHYS-X@ 0< "
29037c478bd9Sstevel@tonic-gate 	"; "
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	": HME_HASH_SHIFT ( ttesz -- hmeshift ) "
29067c478bd9Sstevel@tonic-gate 	"   dup TTE8K =  if "
29077c478bd9Sstevel@tonic-gate 	"      drop HBLK_RANGE_SHIFT "
29087c478bd9Sstevel@tonic-gate 	"   else "
29097c478bd9Sstevel@tonic-gate 	"      TTE_PAGE_SHIFT "
29107c478bd9Sstevel@tonic-gate 	"   then "
29117c478bd9Sstevel@tonic-gate 	"; "
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	": HME_HASH_BSPAGE ( addr hmeshift -- bspage ) "
29147c478bd9Sstevel@tonic-gate 	"   tuck >> swap MMU_PAGESHIFT - << "
29157c478bd9Sstevel@tonic-gate 	"; "
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate 	": HME_HASH_FUNCTION ( sfmmup addr hmeshift -- hmebp ) "
29187c478bd9Sstevel@tonic-gate 	"   >> over xor swap                    ( hash sfmmup ) "
29197c478bd9Sstevel@tonic-gate 	"   KHATID <>  if                       ( hash ) "
29207c478bd9Sstevel@tonic-gate 	"      UHMEHASH_SZ and                  ( bucket ) "
29217c478bd9Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * uhme_hash +     ( hmebp ) "
29227c478bd9Sstevel@tonic-gate 	"   else                                ( hash ) "
29237c478bd9Sstevel@tonic-gate 	"      KHMEHASH_SZ and                  ( bucket ) "
29247c478bd9Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * khme_hash +     ( hmebp ) "
29257c478bd9Sstevel@tonic-gate 	"   then                                ( hmebp ) "
29267c478bd9Sstevel@tonic-gate 	"; "
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	": HME_HASH_TABLE_SEARCH "
29297c478bd9Sstevel@tonic-gate 	"       ( sfmmup hmebp hblktag --  sfmmup null | sfmmup hmeblkp ) "
29307c478bd9Sstevel@tonic-gate 	"   >r hmebp_hblk + phys-x@ begin ( sfmmup hmeblkp ) ( r: hblktag ) "
29317c478bd9Sstevel@tonic-gate 	"      dup if   		( sfmmup hmeblkp ) ( r: hblktag ) "
29327c478bd9Sstevel@tonic-gate 	"         dup hmeblk_tag + phys-x@ r@ = if ( sfmmup hmeblkp )	  "
29337c478bd9Sstevel@tonic-gate 	"	     dup hmeblk_tag + 8 + phys-x@ 2 pick = if		  "
29347c478bd9Sstevel@tonic-gate 	"		  true 	( sfmmup hmeblkp true ) ( r: hblktag )	  "
29357c478bd9Sstevel@tonic-gate 	"	     else						  "
29367c478bd9Sstevel@tonic-gate 	"	     	  hmeblk_next + phys-x@ false 			  "
29377c478bd9Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
29387c478bd9Sstevel@tonic-gate 	"	     then  						  "
29397c478bd9Sstevel@tonic-gate 	"	  else							  "
29407c478bd9Sstevel@tonic-gate 	"	     hmeblk_next + phys-x@ false 			  "
29417c478bd9Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
29427c478bd9Sstevel@tonic-gate 	"	  then 							  "
29437c478bd9Sstevel@tonic-gate 	"      else							  "
29447c478bd9Sstevel@tonic-gate 	"         true 							  "
29457c478bd9Sstevel@tonic-gate 	"      then  							  "
29467c478bd9Sstevel@tonic-gate 	"   until r> drop 						  "
29477c478bd9Sstevel@tonic-gate 	"; "
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	": HME_HASH_TAG ( sfmmup rehash addr -- hblktag ) "
29507c478bd9Sstevel@tonic-gate 	"   over HME_HASH_SHIFT HME_HASH_BSPAGE      ( sfmmup rehash bspage ) "
29517c478bd9Sstevel@tonic-gate 	"   HTAG_REHASHSZ << or nip		     ( hblktag ) "
29527c478bd9Sstevel@tonic-gate 	"; "
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	": HBLK_TO_TTEP ( hmeblkp addr -- ttep ) "
29557c478bd9Sstevel@tonic-gate 	"   over HMEBLK_MISC + PHYS-L@ HBLK_SZMASK and  ( hmeblkp addr ttesz ) "
29567c478bd9Sstevel@tonic-gate 	"   TTE8K =  if                            ( hmeblkp addr ) "
29577c478bd9Sstevel@tonic-gate 	"      MMU_PAGESHIFT >> NHMENTS 1- and     ( hmeblkp hme-index ) "
29587c478bd9Sstevel@tonic-gate 	"   else                                   ( hmeblkp addr ) "
29597c478bd9Sstevel@tonic-gate 	"      drop 0                              ( hmeblkp 0 ) "
29607c478bd9Sstevel@tonic-gate 	"   then                                   ( hmeblkp hme-index ) "
29617c478bd9Sstevel@tonic-gate 	"   SFHME_SIZE * + HMEBLK_HME1 +           ( hmep ) "
29627c478bd9Sstevel@tonic-gate 	"   SFHME_TTE +                            ( ttep ) "
29637c478bd9Sstevel@tonic-gate 	"; "
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	": unix-tte ( addr cnum -- false | tte-data true ) "
29661e2e7a75Shuah 	"    KCONTEXT = if                   ( addr ) "
29671e2e7a75Shuah 	"	KHATID                       ( addr khatid ) "
29681e2e7a75Shuah 	"    else                            ( addr ) "
29691e2e7a75Shuah 	"       drop false exit              ( false ) "
29701e2e7a75Shuah 	"    then "
29711e2e7a75Shuah 	"      ( addr khatid ) "
29727c478bd9Sstevel@tonic-gate 	"      mmu_hashcnt 1+ 1  do           ( addr sfmmup ) "
29737c478bd9Sstevel@tonic-gate 	"         2dup swap i HME_HASH_SHIFT  "
29747c478bd9Sstevel@tonic-gate 					"( addr sfmmup sfmmup addr hmeshift ) "
29757c478bd9Sstevel@tonic-gate 	"         HME_HASH_FUNCTION           ( addr sfmmup hmebp ) "
29767c478bd9Sstevel@tonic-gate 	"         over i 4 pick               "
29777c478bd9Sstevel@tonic-gate 				"( addr sfmmup hmebp sfmmup rehash addr ) "
29787c478bd9Sstevel@tonic-gate 	"         HME_HASH_TAG                ( addr sfmmup hmebp hblktag ) "
29797c478bd9Sstevel@tonic-gate 	"         HME_HASH_TABLE_SEARCH       "
29807c478bd9Sstevel@tonic-gate 					"( addr sfmmup { null | hmeblkp } ) "
29817c478bd9Sstevel@tonic-gate 	"         ?dup  if                    ( addr sfmmup hmeblkp ) "
29827c478bd9Sstevel@tonic-gate 	"            nip swap HBLK_TO_TTEP    ( ttep ) "
29837c478bd9Sstevel@tonic-gate 	"            dup TTE_IS_VALID  if     ( valid-ttep ) "
29847c478bd9Sstevel@tonic-gate 	"               PHYS-X@ true          ( tte-data true ) "
29857c478bd9Sstevel@tonic-gate 	"            else                     ( invalid-tte ) "
29867c478bd9Sstevel@tonic-gate 	"               drop false            ( false ) "
29877c478bd9Sstevel@tonic-gate 	"            then                     ( false | tte-data true ) "
29887c478bd9Sstevel@tonic-gate 	"            unloop exit              ( false | tte-data true ) "
29897c478bd9Sstevel@tonic-gate 	"         then                        ( addr sfmmup ) "
29907c478bd9Sstevel@tonic-gate 	"      loop                           ( addr sfmmup ) "
29917c478bd9Sstevel@tonic-gate 	"      2drop false                    ( false ) "
29927c478bd9Sstevel@tonic-gate 	"; "
29937c478bd9Sstevel@tonic-gate ;
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate void
29967c478bd9Sstevel@tonic-gate create_va_to_tte(void)
29977c478bd9Sstevel@tonic-gate {
29987c478bd9Sstevel@tonic-gate 	char *bp;
29997c478bd9Sstevel@tonic-gate 	extern int khmehash_num, uhmehash_num;
30007c478bd9Sstevel@tonic-gate 	extern struct hmehash_bucket *khme_hash, *uhme_hash;
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate #define	OFFSET(type, field)	((uintptr_t)(&((type *)0)->field))
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 	bp = (char *)kobj_zalloc(MMU_PAGESIZE, KM_SLEEP);
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	/*
30077c478bd9Sstevel@tonic-gate 	 * Teach obp how to parse our sw ttes.
30087c478bd9Sstevel@tonic-gate 	 */
30097c478bd9Sstevel@tonic-gate 	(void) sprintf(bp, obp_tte_str,
30107c478bd9Sstevel@tonic-gate 	    MMU_PAGESHIFT,
30117c478bd9Sstevel@tonic-gate 	    TTE8K,
30127c478bd9Sstevel@tonic-gate 	    sizeof (struct sf_hment),
30137c478bd9Sstevel@tonic-gate 	    OFFSET(struct sf_hment, hme_tte),
30147c478bd9Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_tag),
30157c478bd9Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_nextpa),
30167c478bd9Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_misc),
30177c478bd9Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_hme),
30187c478bd9Sstevel@tonic-gate 	    NHMENTS,
30197c478bd9Sstevel@tonic-gate 	    HBLK_SZMASK,
30207c478bd9Sstevel@tonic-gate 	    HBLK_RANGE_SHIFT,
30217c478bd9Sstevel@tonic-gate 	    OFFSET(struct hmehash_bucket, hmeh_nextpa),
30227c478bd9Sstevel@tonic-gate 	    sizeof (struct hmehash_bucket),
30237c478bd9Sstevel@tonic-gate 	    HTAG_SFMMUPSZ,
30247c478bd9Sstevel@tonic-gate 	    HTAG_REHASHSZ,
30257c478bd9Sstevel@tonic-gate 	    mmu_hashcnt,
30267c478bd9Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)uhme_hash),
30277c478bd9Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)khme_hash),
30287c478bd9Sstevel@tonic-gate 	    UHMEHASH_SZ,
30297c478bd9Sstevel@tonic-gate 	    KHMEHASH_SZ,
30301e2e7a75Shuah 	    KCONTEXT,
30317c478bd9Sstevel@tonic-gate 	    KHATID,
30327c478bd9Sstevel@tonic-gate 	    ASI_MEM);
30337c478bd9Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	kobj_free(bp, MMU_PAGESIZE);
30367c478bd9Sstevel@tonic-gate }
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate void
30397c478bd9Sstevel@tonic-gate install_va_to_tte(void)
30407c478bd9Sstevel@tonic-gate {
30417c478bd9Sstevel@tonic-gate 	/*
30427c478bd9Sstevel@tonic-gate 	 * advise prom that he can use unix-tte
30437c478bd9Sstevel@tonic-gate 	 */
30447c478bd9Sstevel@tonic-gate 	prom_interpret("' unix-tte is va>tte-data", 0, 0, 0, 0, 0);
30457c478bd9Sstevel@tonic-gate }
30467c478bd9Sstevel@tonic-gate 
3047fea9cb91Slq150181 /*
30482d7b546fSlq150181  * Here we add "device-type=console" for /os-io node, for currently
30492d7b546fSlq150181  * our kernel console output only supports displaying text and
30502d7b546fSlq150181  * performing cursor-positioning operations (through kernel framebuffer
30512d7b546fSlq150181  * driver) and it doesn't support other functionalities required for a
30522d7b546fSlq150181  * standard "display" device as specified in 1275 spec. The main missing
30532d7b546fSlq150181  * interface defined by the 1275 spec is "draw-logo".
30542d7b546fSlq150181  * also see the comments above prom_stdout_is_framebuffer().
3055fea9cb91Slq150181  */
30567c478bd9Sstevel@tonic-gate static char *create_node =
305712ef07e9Slq150181 	"\" /\" find-device "
30587c478bd9Sstevel@tonic-gate 	"new-device "
30597c478bd9Sstevel@tonic-gate 	"\" os-io\" device-name "
30602d7b546fSlq150181 	"\" "OBP_DISPLAY_CONSOLE"\" device-type "
30617c478bd9Sstevel@tonic-gate 	": cb-r/w  ( adr,len method$ -- #read/#written ) "
30627c478bd9Sstevel@tonic-gate 	"   2>r swap 2 2r> ['] $callback  catch  if "
30637c478bd9Sstevel@tonic-gate 	"      2drop 3drop 0 "
30647c478bd9Sstevel@tonic-gate 	"   then "
30657c478bd9Sstevel@tonic-gate 	"; "
30667c478bd9Sstevel@tonic-gate 	": read ( adr,len -- #read ) "
30677c478bd9Sstevel@tonic-gate 	"       \" read\" ['] cb-r/w catch  if  2drop 2drop -2 exit then "
30687c478bd9Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
30697c478bd9Sstevel@tonic-gate 	"       ?dup  if "
30707c478bd9Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
30717c478bd9Sstevel@tonic-gate 	"       else "
30727c478bd9Sstevel@tonic-gate 	"               -2 "
307312ef07e9Slq150181 	"       then "
30747c478bd9Sstevel@tonic-gate 	";    "
30757c478bd9Sstevel@tonic-gate 	": write ( adr,len -- #written ) "
30767c478bd9Sstevel@tonic-gate 	"       \" write\" ['] cb-r/w catch  if  2drop 2drop 0 exit  then "
30777c478bd9Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
30787c478bd9Sstevel@tonic-gate 	"       ?dup  if "
30797c478bd9Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
30807c478bd9Sstevel@tonic-gate 	"        else "
30817c478bd9Sstevel@tonic-gate 	"               0 "
30827c478bd9Sstevel@tonic-gate 	"       then "
30837c478bd9Sstevel@tonic-gate 	"; "
30847c478bd9Sstevel@tonic-gate 	": poll-tty ( -- ) ; "
30857c478bd9Sstevel@tonic-gate 	": install-abort  ( -- )  ['] poll-tty d# 10 alarm ; "
30867c478bd9Sstevel@tonic-gate 	": remove-abort ( -- )  ['] poll-tty 0 alarm ; "
30877c478bd9Sstevel@tonic-gate 	": cb-give/take ( $method -- ) "
30887c478bd9Sstevel@tonic-gate 	"       0 -rot ['] $callback catch  ?dup  if "
30897c478bd9Sstevel@tonic-gate 	"               >r 2drop 2drop r> throw "
30907c478bd9Sstevel@tonic-gate 	"       else "
30917c478bd9Sstevel@tonic-gate 	"               0  ?do  drop  loop "
30927c478bd9Sstevel@tonic-gate 	"       then "
30937c478bd9Sstevel@tonic-gate 	"; "
30947c478bd9Sstevel@tonic-gate 	": give ( -- )  \" exit-input\" cb-give/take ; "
30957c478bd9Sstevel@tonic-gate 	": take ( -- )  \" enter-input\" cb-give/take ; "
30967c478bd9Sstevel@tonic-gate 	": open ( -- ok? )  true ; "
30977c478bd9Sstevel@tonic-gate 	": close ( -- ) ; "
30987c478bd9Sstevel@tonic-gate 	"finish-device "
30997c478bd9Sstevel@tonic-gate 	"device-end ";
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate /*
3102fea9cb91Slq150181  * Create the OBP input/output node (FCode serial driver).
3103fea9cb91Slq150181  * It is needed for both USB console keyboard and for
3104fea9cb91Slq150181  * the kernel terminal emulator.  It is too early to check for a
3105fea9cb91Slq150181  * kernel console compatible framebuffer now, so we create this
3106fea9cb91Slq150181  * so that we're ready if we need to enable kernel terminal emulation.
31077c478bd9Sstevel@tonic-gate  *
3108fea9cb91Slq150181  * When the USB software takes over the input device at the time
3109fea9cb91Slq150181  * consconfig runs, OBP's stdin is redirected to this node.
3110fea9cb91Slq150181  * Whenever the FORTH user interface is used after this switch,
3111fea9cb91Slq150181  * the node will call back into the kernel for console input.
3112fea9cb91Slq150181  * If a serial device such as ttya or a UART with a Type 5 keyboard
3113fea9cb91Slq150181  * attached is used, OBP takes over the serial device when the system
3114fea9cb91Slq150181  * goes to the debugger after the system is booted.  This sharing
3115fea9cb91Slq150181  * of the relatively simple serial device is difficult but possible.
3116fea9cb91Slq150181  * Sharing the USB host controller is impossible due its complexity.
3117fea9cb91Slq150181  *
3118fea9cb91Slq150181  * Similarly to USB keyboard input redirection, after consconfig_dacf
3119fea9cb91Slq150181  * configures a kernel console framebuffer as the standard output
3120fea9cb91Slq150181  * device, OBP's stdout is switched to to vector through the
3121fea9cb91Slq150181  * /os-io node into the kernel terminal emulator.
31227c478bd9Sstevel@tonic-gate  */
31237c478bd9Sstevel@tonic-gate static void
3124fea9cb91Slq150181 startup_create_io_node(void)
31257c478bd9Sstevel@tonic-gate {
31267c478bd9Sstevel@tonic-gate 	prom_interpret(create_node, 0, 0, 0, 0, 0);
31277c478bd9Sstevel@tonic-gate }
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate static void
31317c478bd9Sstevel@tonic-gate do_prom_version_check(void)
31327c478bd9Sstevel@tonic-gate {
31337c478bd9Sstevel@tonic-gate 	int i;
3134fa9e4066Sahrens 	pnode_t node;
31357c478bd9Sstevel@tonic-gate 	char buf[64];
31367c478bd9Sstevel@tonic-gate 	static char drev[] = "Down-rev firmware detected%s\n"
31377c478bd9Sstevel@tonic-gate 		"\tPlease upgrade to the following minimum version:\n"
31387c478bd9Sstevel@tonic-gate 		"\t\t%s\n";
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	i = prom_version_check(buf, sizeof (buf), &node);
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 	if (i == PROM_VER64_OK)
31437c478bd9Sstevel@tonic-gate 		return;
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	if (i == PROM_VER64_UPGRADE) {
31467c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, drev, "", buf);
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate #ifdef	DEBUG
31497c478bd9Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to continue */
31507c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware\n");
31517c478bd9Sstevel@tonic-gate 		return;
31527c478bd9Sstevel@tonic-gate #else
31537c478bd9Sstevel@tonic-gate 		halt(0);
31547c478bd9Sstevel@tonic-gate #endif
31557c478bd9Sstevel@tonic-gate 	}
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	/*
31587c478bd9Sstevel@tonic-gate 	 * The other possibility is that this is a server running
31597c478bd9Sstevel@tonic-gate 	 * good firmware, but down-rev firmware was detected on at
31607c478bd9Sstevel@tonic-gate 	 * least one other cpu board. We just complain if we see
31617c478bd9Sstevel@tonic-gate 	 * that.
31627c478bd9Sstevel@tonic-gate 	 */
31637c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, drev, " on one or more CPU boards", buf);
31647c478bd9Sstevel@tonic-gate }
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate static void
31677c478bd9Sstevel@tonic-gate kpm_init()
31687c478bd9Sstevel@tonic-gate {
31697c478bd9Sstevel@tonic-gate 	kpm_pgshft = (kpm_smallpages == 0) ? MMU_PAGESHIFT4M : MMU_PAGESHIFT;
31707c478bd9Sstevel@tonic-gate 	kpm_pgsz = 1ull << kpm_pgshft;
31717c478bd9Sstevel@tonic-gate 	kpm_pgoff = kpm_pgsz - 1;
31727c478bd9Sstevel@tonic-gate 	kpmp2pshft = kpm_pgshft - PAGESHIFT;
31737c478bd9Sstevel@tonic-gate 	kpmpnpgs = 1 << kpmp2pshft;
31747c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)kpm_vbase & (kpm_pgsz - 1)) == 0);
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate void
31787c478bd9Sstevel@tonic-gate kpm_npages_setup(int memblocks)
31797c478bd9Sstevel@tonic-gate {
31807c478bd9Sstevel@tonic-gate 	/*
31817c478bd9Sstevel@tonic-gate 	 * npages can be scattered in a maximum of 'memblocks'
31827c478bd9Sstevel@tonic-gate 	 */
31837c478bd9Sstevel@tonic-gate 	kpm_npages = ptokpmpr(npages) + memblocks;
31847c478bd9Sstevel@tonic-gate }
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate /*
31877c478bd9Sstevel@tonic-gate  * Must be defined in platform dependent code.
31887c478bd9Sstevel@tonic-gate  */
31897c478bd9Sstevel@tonic-gate extern caddr_t modtext;
31907c478bd9Sstevel@tonic-gate extern size_t modtext_sz;
31917c478bd9Sstevel@tonic-gate extern caddr_t moddata;
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate #define	HEAPTEXT_ARENA(addr)	\
31947c478bd9Sstevel@tonic-gate 	((uintptr_t)(addr) < KERNELBASE + 2 * MMU_PAGESIZE4M ? 0 : \
31957c478bd9Sstevel@tonic-gate 	(((uintptr_t)(addr) - HEAPTEXT_BASE) / \
31967c478bd9Sstevel@tonic-gate 	(HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) + 1))
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate #define	HEAPTEXT_OVERSIZED(addr)	\
31997c478bd9Sstevel@tonic-gate 	((uintptr_t)(addr) >= HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE)
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate vmem_t *texthole_source[HEAPTEXT_NARENAS];
32027c478bd9Sstevel@tonic-gate vmem_t *texthole_arena[HEAPTEXT_NARENAS];
32037c478bd9Sstevel@tonic-gate kmutex_t texthole_lock;
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate void
32087c478bd9Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
32097c478bd9Sstevel@tonic-gate {
32107c478bd9Sstevel@tonic-gate 	uintptr_t addr, limit;
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	addr = HEAPTEXT_BASE;
32137c478bd9Sstevel@tonic-gate 	limit = addr + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE;
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 	/*
32167c478bd9Sstevel@tonic-gate 	 * Before we initialize the text_arena, we want to punch holes in the
32177c478bd9Sstevel@tonic-gate 	 * underlying heaptext_arena.  This guarantees that for any text
32187c478bd9Sstevel@tonic-gate 	 * address we can find a text hole less than HEAPTEXT_MAPPED away.
32197c478bd9Sstevel@tonic-gate 	 */
32207c478bd9Sstevel@tonic-gate 	for (; addr + HEAPTEXT_UNMAPPED <= limit;
32217c478bd9Sstevel@tonic-gate 	    addr += HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) {
32227c478bd9Sstevel@tonic-gate 		(void) vmem_xalloc(heaptext_arena, HEAPTEXT_UNMAPPED, PAGESIZE,
32237c478bd9Sstevel@tonic-gate 		    0, 0, (void *)addr, (void *)(addr + HEAPTEXT_UNMAPPED),
32247c478bd9Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
32257c478bd9Sstevel@tonic-gate 	}
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	/*
32287c478bd9Sstevel@tonic-gate 	 * Allocate one page at the oversize to break up the text region
32297c478bd9Sstevel@tonic-gate 	 * from the oversized region.
32307c478bd9Sstevel@tonic-gate 	 */
32317c478bd9Sstevel@tonic-gate 	(void) vmem_xalloc(heaptext_arena, PAGESIZE, PAGESIZE, 0, 0,
32327c478bd9Sstevel@tonic-gate 	    (void *)limit, (void *)(limit + PAGESIZE),
32337c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
32347c478bd9Sstevel@tonic-gate 
323568d3ac02Skchow 	*text_arena = vmem_create("module_text", modtext_sz ? modtext : NULL,
323668d3ac02Skchow 	    modtext_sz, sizeof (uintptr_t), segkmem_alloc, segkmem_free,
32377c478bd9Sstevel@tonic-gate 	    heaptext_arena, 0, VM_SLEEP);
32387c478bd9Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", moddata, MODDATA, 1,
32397c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
32407c478bd9Sstevel@tonic-gate }
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate caddr_t
32437c478bd9Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
32447c478bd9Sstevel@tonic-gate {
32457c478bd9Sstevel@tonic-gate 	caddr_t rval, better;
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate 	/*
32487c478bd9Sstevel@tonic-gate 	 * First, try a sleeping allocation.
32497c478bd9Sstevel@tonic-gate 	 */
32507c478bd9Sstevel@tonic-gate 	rval = vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT);
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	if (size >= HEAPTEXT_MAPPED || !HEAPTEXT_OVERSIZED(rval))
32537c478bd9Sstevel@tonic-gate 		return (rval);
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	/*
32567c478bd9Sstevel@tonic-gate 	 * We didn't get the area that we wanted.  We're going to try to do an
32577c478bd9Sstevel@tonic-gate 	 * allocation with explicit constraints.
32587c478bd9Sstevel@tonic-gate 	 */
32597c478bd9Sstevel@tonic-gate 	better = vmem_xalloc(arena, size, sizeof (uintptr_t), 0, 0, NULL,
32607c478bd9Sstevel@tonic-gate 	    (void *)(HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE),
32617c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT);
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	if (better != NULL) {
32647c478bd9Sstevel@tonic-gate 		/*
32657c478bd9Sstevel@tonic-gate 		 * That worked.  Free our first attempt and return.
32667c478bd9Sstevel@tonic-gate 		 */
32677c478bd9Sstevel@tonic-gate 		vmem_free(arena, rval, size);
32687c478bd9Sstevel@tonic-gate 		return (better);
32697c478bd9Sstevel@tonic-gate 	}
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 	/*
32727c478bd9Sstevel@tonic-gate 	 * That didn't work; we'll have to return our first attempt.
32737c478bd9Sstevel@tonic-gate 	 */
32747c478bd9Sstevel@tonic-gate 	return (rval);
32757c478bd9Sstevel@tonic-gate }
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate caddr_t
32787c478bd9Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
32797c478bd9Sstevel@tonic-gate {
32807c478bd9Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
32817c478bd9Sstevel@tonic-gate 	char c[30];
32827c478bd9Sstevel@tonic-gate 	uintptr_t base;
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	if (HEAPTEXT_OVERSIZED(addr)) {
32857c478bd9Sstevel@tonic-gate 		/*
32867c478bd9Sstevel@tonic-gate 		 * If this is an oversized allocation, there is no text hole
32877c478bd9Sstevel@tonic-gate 		 * available for it; return NULL.
32887c478bd9Sstevel@tonic-gate 		 */
32897c478bd9Sstevel@tonic-gate 		return (NULL);
32907c478bd9Sstevel@tonic-gate 	}
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	mutex_enter(&texthole_lock);
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	if (texthole_arena[arena] == NULL) {
32957c478bd9Sstevel@tonic-gate 		ASSERT(texthole_source[arena] == NULL);
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 		if (arena == 0) {
32987c478bd9Sstevel@tonic-gate 			texthole_source[0] = vmem_create("module_text_holesrc",
32997c478bd9Sstevel@tonic-gate 			    (void *)(KERNELBASE + MMU_PAGESIZE4M),
33007c478bd9Sstevel@tonic-gate 			    MMU_PAGESIZE4M, PAGESIZE, NULL, NULL, NULL,
33017c478bd9Sstevel@tonic-gate 			    0, VM_SLEEP);
33027c478bd9Sstevel@tonic-gate 		} else {
33037c478bd9Sstevel@tonic-gate 			base = HEAPTEXT_BASE +
33047c478bd9Sstevel@tonic-gate 			    (arena - 1) * (HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED);
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c),
33077c478bd9Sstevel@tonic-gate 			    "heaptext_holesrc_%d", arena);
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 			texthole_source[arena] = vmem_create(c, (void *)base,
33107c478bd9Sstevel@tonic-gate 			    HEAPTEXT_UNMAPPED, PAGESIZE, NULL, NULL, NULL,
33117c478bd9Sstevel@tonic-gate 			    0, VM_SLEEP);
33127c478bd9Sstevel@tonic-gate 		}
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 		(void) snprintf(c, sizeof (c), "heaptext_hole_%d", arena);
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 		texthole_arena[arena] = vmem_create(c, NULL, 0,
33177c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t), segkmem_alloc_permanent, segkmem_free,
33187c478bd9Sstevel@tonic-gate 		    texthole_source[arena], 0, VM_SLEEP);
33197c478bd9Sstevel@tonic-gate 	}
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	mutex_exit(&texthole_lock);
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
33247c478bd9Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
33257c478bd9Sstevel@tonic-gate 	return (vmem_alloc(texthole_arena[arena], size,
33267c478bd9Sstevel@tonic-gate 	    VM_BESTFIT | VM_NOSLEEP));
33277c478bd9Sstevel@tonic-gate }
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate void
33307c478bd9Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
33317c478bd9Sstevel@tonic-gate {
33327c478bd9Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
33357c478bd9Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
33367c478bd9Sstevel@tonic-gate 	vmem_free(texthole_arena[arena], addr, size);
33377c478bd9Sstevel@tonic-gate }
3338