xref: /onnv-gate/usr/src/uts/sun4/os/startup.c (revision 10271:7c80b70bb8de)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51253Slq150181  * Common Development and Distribution License (the "License").
61455Sandrei  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211253Slq150181 
220Sstevel@tonic-gate /*
238803SJonathan.Haslam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/machsystm.h>
280Sstevel@tonic-gate #include <sys/archsystm.h>
290Sstevel@tonic-gate #include <sys/vm.h>
300Sstevel@tonic-gate #include <sys/cpu.h>
310Sstevel@tonic-gate #include <sys/atomic.h>
320Sstevel@tonic-gate #include <sys/reboot.h>
330Sstevel@tonic-gate #include <sys/kdi.h>
340Sstevel@tonic-gate #include <sys/bootconf.h>
350Sstevel@tonic-gate #include <sys/memlist_plat.h>
360Sstevel@tonic-gate #include <sys/memlist_impl.h>
370Sstevel@tonic-gate #include <sys/prom_plat.h>
380Sstevel@tonic-gate #include <sys/prom_isa.h>
390Sstevel@tonic-gate #include <sys/autoconf.h>
400Sstevel@tonic-gate #include <sys/intreg.h>
410Sstevel@tonic-gate #include <sys/ivintr.h>
420Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
430Sstevel@tonic-gate #include <sys/iommutsb.h>
440Sstevel@tonic-gate #include <vm/vm_dep.h>
450Sstevel@tonic-gate #include <vm/seg_dev.h>
460Sstevel@tonic-gate #include <vm/seg_kmem.h>
470Sstevel@tonic-gate #include <vm/seg_kpm.h>
480Sstevel@tonic-gate #include <vm/seg_map.h>
490Sstevel@tonic-gate #include <vm/seg_kp.h>
500Sstevel@tonic-gate #include <sys/sysconf.h>
510Sstevel@tonic-gate #include <vm/hat_sfmmu.h>
520Sstevel@tonic-gate #include <sys/kobj.h>
530Sstevel@tonic-gate #include <sys/sun4asi.h>
540Sstevel@tonic-gate #include <sys/clconf.h>
550Sstevel@tonic-gate #include <sys/platform_module.h>
560Sstevel@tonic-gate #include <sys/panic.h>
570Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
580Sstevel@tonic-gate #include <sys/clock.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/promif.h>
610Sstevel@tonic-gate #include <sys/prom_debug.h>
620Sstevel@tonic-gate #include <sys/traptrace.h>
630Sstevel@tonic-gate #include <sys/memnode.h>
640Sstevel@tonic-gate #include <sys/mem_cage.h>
652241Shuah #include <sys/mmu.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate extern void setup_trap_table(void);
684050Sjb145095 extern int cpu_intrq_setup(struct cpu *);
690Sstevel@tonic-gate extern void cpu_intrq_register(struct cpu *);
700Sstevel@tonic-gate extern void contig_mem_init(void);
714204Sha137994 extern caddr_t contig_mem_prealloc(caddr_t, pgcnt_t);
720Sstevel@tonic-gate extern void mach_dump_buffer_init(void);
730Sstevel@tonic-gate extern void mach_descrip_init(void);
741991Sheppo extern void mach_descrip_startup_fini(void);
750Sstevel@tonic-gate extern void mach_memscrub(void);
760Sstevel@tonic-gate extern void mach_fpras(void);
770Sstevel@tonic-gate extern void mach_cpu_halt_idle(void);
780Sstevel@tonic-gate extern void mach_hw_copy_limit(void);
791991Sheppo extern void load_mach_drivers(void);
800Sstevel@tonic-gate extern void load_tod_module(void);
810Sstevel@tonic-gate #pragma weak load_tod_module
820Sstevel@tonic-gate 
830Sstevel@tonic-gate extern int ndata_alloc_mmfsa(struct memlist *ndata);
840Sstevel@tonic-gate #pragma weak ndata_alloc_mmfsa
850Sstevel@tonic-gate 
861991Sheppo extern void cif_init(void);
871991Sheppo #pragma weak cif_init
881991Sheppo 
890Sstevel@tonic-gate extern void parse_idprom(void);
900Sstevel@tonic-gate extern void add_vx_handler(char *, int, void (*)(cell_t *));
910Sstevel@tonic-gate extern void mem_config_init(void);
920Sstevel@tonic-gate extern void memseg_remap_init(void);
930Sstevel@tonic-gate 
942296Sae112802 extern void mach_kpm_init(void);
956880Sdv142724 extern void pcf_init();
964878Sblakej extern int size_pse_array(pgcnt_t, int);
978906SEric.Saxe@Sun.COM extern void pg_init();
982296Sae112802 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * External Data:
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate extern int vac_size;	/* cache size in bytes */
1030Sstevel@tonic-gate extern uint_t vac_mask;	/* VAC alignment consistency mask */
1040Sstevel@tonic-gate extern uint_t vac_colors;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Global Data Definitions:
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * XXX - Don't port this to new architectures
1120Sstevel@tonic-gate  * A 3rd party volume manager driver (vxdm) depends on the symbol romp.
1130Sstevel@tonic-gate  * 'romp' has no use with a prom with an IEEE 1275 client interface.
1140Sstevel@tonic-gate  * The driver doesn't use the value, but it depends on the symbol.
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate void *romp;		/* veritas driver won't load without romp 4154976 */
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Declare these as initialized data so we can patch them.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate pgcnt_t physmem = 0;	/* memory size in pages, patch if you want less */
1210Sstevel@tonic-gate pgcnt_t segkpsize =
1220Sstevel@tonic-gate     btop(SEGKPDEFSIZE);	/* size of segkp segment in pages */
1239281SPrakash.Sangappa@Sun.COM uint_t segmap_percent = 6; /* Size of segmap segment */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int use_cache = 1;		/* cache not reliable (605 bugs) with MP */
1260Sstevel@tonic-gate int vac_copyback = 1;
1270Sstevel@tonic-gate char *cache_mode = NULL;
1280Sstevel@tonic-gate int use_mix = 1;
1290Sstevel@tonic-gate int prom_debug = 0;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate caddr_t boot_tba;		/* %tba at boot - used by kmdb */
1320Sstevel@tonic-gate uint_t	tba_taken_over = 0;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate caddr_t s_text;			/* start of kernel text segment */
1350Sstevel@tonic-gate caddr_t e_text;			/* end of kernel text segment */
1360Sstevel@tonic-gate caddr_t s_data;			/* start of kernel data segment */
1370Sstevel@tonic-gate caddr_t e_data;			/* end of kernel data segment */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate caddr_t modtext;		/* beginning of module text */
1400Sstevel@tonic-gate size_t	modtext_sz;		/* size of module text */
1410Sstevel@tonic-gate caddr_t moddata;		/* beginning of module data reserve */
1420Sstevel@tonic-gate caddr_t e_moddata;		/* end of module data reserve */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * End of first block of contiguous kernel in 32-bit virtual address space
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate caddr_t		econtig32;	/* end of first blk of contiguous kernel */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate caddr_t		ncbase;		/* beginning of non-cached segment */
1500Sstevel@tonic-gate caddr_t		ncend;		/* end of non-cached segment */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate size_t	ndata_remain_sz;	/* bytes from end of data to 4MB boundary */
1530Sstevel@tonic-gate caddr_t	nalloc_base;		/* beginning of nucleus allocation */
1540Sstevel@tonic-gate caddr_t nalloc_end;		/* end of nucleus allocatable memory */
1550Sstevel@tonic-gate caddr_t valloc_base;		/* beginning of kvalloc segment	*/
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate caddr_t kmem64_base;		/* base of kernel mem segment in 64-bit space */
1580Sstevel@tonic-gate caddr_t kmem64_end;		/* end of kernel mem segment in 64-bit space */
1595648Ssetje size_t	kmem64_sz;		/* bytes in kernel mem segment, 64-bit space */
1603764Sdp78419 caddr_t kmem64_aligned_end;	/* end of large page, overmaps 64-bit space */
1613764Sdp78419 int	kmem64_szc;		/* page size code */
1623764Sdp78419 uint64_t kmem64_pabase = (uint64_t)-1;	/* physical address of kmem64_base */
1630Sstevel@tonic-gate 
1642296Sae112802 uintptr_t shm_alignment;	/* VAC address consistency modulus */
1650Sstevel@tonic-gate struct memlist *phys_install;	/* Total installed physical memory */
1660Sstevel@tonic-gate struct memlist *phys_avail;	/* Available (unreserved) physical memory */
1670Sstevel@tonic-gate struct memlist *virt_avail;	/* Available (unmapped?) virtual memory */
1685648Ssetje struct memlist *nopp_list;	/* pages with no backing page structs */
1690Sstevel@tonic-gate struct memlist ndata;		/* memlist of nucleus allocatable memory */
1700Sstevel@tonic-gate int memexp_flag;		/* memory expansion card flag */
1710Sstevel@tonic-gate uint64_t ecache_flushaddr;	/* physical address used for flushing E$ */
1720Sstevel@tonic-gate pgcnt_t obp_pages;		/* Physical pages used by OBP */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * VM data structures
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate long page_hashsz;		/* Size of page hash table (power of two) */
1780Sstevel@tonic-gate struct page *pp_base;		/* Base of system page struct array */
1790Sstevel@tonic-gate size_t pp_sz;			/* Size in bytes of page struct array */
1800Sstevel@tonic-gate struct page **page_hash;	/* Page hash table */
1814878Sblakej pad_mutex_t *pse_mutex;		/* Locks protecting pp->p_selock */
1824878Sblakej size_t pse_table_size;		/* Number of mutexes in pse_mutex[] */
1834878Sblakej int pse_shift;			/* log2(pse_table_size) */
1840Sstevel@tonic-gate struct seg ktextseg;		/* Segment used for kernel executable image */
1850Sstevel@tonic-gate struct seg kvalloc;		/* Segment used for "valloc" mapping */
1860Sstevel@tonic-gate struct seg kpseg;		/* Segment used for pageable kernel virt mem */
1870Sstevel@tonic-gate struct seg ktexthole;		/* Segment used for nucleus text hole */
1880Sstevel@tonic-gate struct seg kmapseg;		/* Segment used for generic kernel mappings */
1890Sstevel@tonic-gate struct seg kpmseg;		/* Segment used for physical mapping */
1900Sstevel@tonic-gate struct seg kdebugseg;		/* Segment used for the kernel debugger */
1910Sstevel@tonic-gate 
1925648Ssetje void *kpm_pp_base;		/* Base of system kpm_page array */
1930Sstevel@tonic-gate size_t	kpm_pp_sz;		/* Size of system kpm_page array */
1940Sstevel@tonic-gate pgcnt_t	kpm_npages;		/* How many kpm pages are managed */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate struct seg *segkp = &kpseg;	/* Pageable kernel virtual memory segment */
1970Sstevel@tonic-gate struct seg *segkmap = &kmapseg;	/* Kernel generic mapping segment */
1980Sstevel@tonic-gate struct seg *segkpm = &kpmseg;	/* 64bit kernel physical mapping segment */
1990Sstevel@tonic-gate 
2003290Sjohansen int segzio_fromheap = 0;	/* zio allocations occur from heap */
2013290Sjohansen caddr_t segzio_base;		/* Base address of segzio */
2023290Sjohansen pgcnt_t segziosize = 0;		/* size of zio segment in pages */
2033290Sjohansen 
2040Sstevel@tonic-gate /*
20510106SJason.Beloro@Sun.COM  * A static DR page_t VA map is reserved that can map the page structures
20610106SJason.Beloro@Sun.COM  * for a domain's entire RA space. The pages that backs this space are
20710106SJason.Beloro@Sun.COM  * dynamically allocated and need not be physically contiguous.  The DR
20810106SJason.Beloro@Sun.COM  * map size is derived from KPM size.
20910106SJason.Beloro@Sun.COM  */
21010106SJason.Beloro@Sun.COM int ppvm_enable = 0;		/* Static virtual map for page structs */
21110106SJason.Beloro@Sun.COM page_t *ppvm_base;		/* Base of page struct map */
21210106SJason.Beloro@Sun.COM pgcnt_t ppvm_size = 0;		/* Size of page struct map */
21310106SJason.Beloro@Sun.COM 
21410106SJason.Beloro@Sun.COM /*
2150Sstevel@tonic-gate  * debugger pages (if allocated)
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate struct vnode kdebugvp;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2203446Smrj  * VA range available to the debugger
2213446Smrj  */
2223446Smrj const caddr_t kdi_segdebugbase = (const caddr_t)SEGDEBUGBASE;
2233446Smrj const size_t kdi_segdebugsize = SEGDEBUGSIZE;
2243446Smrj 
2253446Smrj /*
2260Sstevel@tonic-gate  * Segment for relocated kernel structures in 64-bit large RAM kernels
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate struct seg kmem64;
2290Sstevel@tonic-gate 
2305648Ssetje struct memseg *memseg_free;
2315648Ssetje 
2320Sstevel@tonic-gate struct vnode unused_pages_vp;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * VM data structures allocated early during boot.
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate size_t pagehash_sz;
2380Sstevel@tonic-gate uint64_t memlist_sz;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate char tbr_wr_addr_inited = 0;
2410Sstevel@tonic-gate 
2425358Sjc25722 caddr_t	mpo_heap32_buf = NULL;
2435358Sjc25722 size_t	mpo_heap32_bufsz = 0;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Static Routines:
2470Sstevel@tonic-gate  */
2485648Ssetje static int ndata_alloc_memseg(struct memlist *, size_t);
2495648Ssetje static void memlist_new(uint64_t, uint64_t, struct memlist **);
2505648Ssetje static void memlist_add(uint64_t, uint64_t,
2515648Ssetje 	struct memlist **, struct memlist **);
2525648Ssetje static void kphysm_init(void);
2530Sstevel@tonic-gate static void kvm_init(void);
2545648Ssetje static void install_kmem64_tte(void);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static void startup_init(void);
2570Sstevel@tonic-gate static void startup_memlist(void);
2580Sstevel@tonic-gate static void startup_modules(void);
2590Sstevel@tonic-gate static void startup_bop_gone(void);
2600Sstevel@tonic-gate static void startup_vm(void);
2610Sstevel@tonic-gate static void startup_end(void);
2620Sstevel@tonic-gate static void setup_cage_params(void);
2631253Slq150181 static void startup_create_io_node(void);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static pgcnt_t npages;
2660Sstevel@tonic-gate static struct memlist *memlist;
2670Sstevel@tonic-gate void *memlist_end;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate static pgcnt_t bop_alloc_pages;
2700Sstevel@tonic-gate static caddr_t hblk_base;
2710Sstevel@tonic-gate uint_t hblk_alloc_dynamic = 0;
2720Sstevel@tonic-gate uint_t hblk1_min = H1MIN;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Hooks for unsupported platforms and down-rev firmware
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate int iam_positron(void);
2790Sstevel@tonic-gate #pragma weak iam_positron
2800Sstevel@tonic-gate static void do_prom_version_check(void);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * After receiving a thermal interrupt, this is the number of seconds
2840Sstevel@tonic-gate  * to delay before shutting off the system, assuming
2850Sstevel@tonic-gate  * shutdown fails.  Use /etc/system to change the delay if this isn't
2860Sstevel@tonic-gate  * large enough.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate int thermal_powerdown_delay = 1200;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Used to hold off page relocations into the cage until OBP has completed
2920Sstevel@tonic-gate  * its boot-time handoff of its resources to the kernel.
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate int page_relocate_ready = 0;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2977218Ssvemuri  * Indicate if kmem64 allocation was done in small chunks
2987218Ssvemuri  */
2997218Ssvemuri int kmem64_smchunks = 0;
3007218Ssvemuri 
3017218Ssvemuri /*
3020Sstevel@tonic-gate  * Enable some debugging messages concerning memory usage...
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate #ifdef  DEBUGGING_MEM
3050Sstevel@tonic-gate static int debugging_mem;
3060Sstevel@tonic-gate static void
3070Sstevel@tonic-gate printmemlist(char *title, struct memlist *list)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	if (!debugging_mem)
3100Sstevel@tonic-gate 		return;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	printf("%s\n", title);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	while (list) {
3150Sstevel@tonic-gate 		prom_printf("\taddr = 0x%x %8x, size = 0x%x %8x\n",
3160Sstevel@tonic-gate 		    (uint32_t)(list->address >> 32), (uint32_t)list->address,
3170Sstevel@tonic-gate 		    (uint32_t)(list->size >> 32), (uint32_t)(list->size));
3180Sstevel@tonic-gate 		list = list->next;
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate void
3230Sstevel@tonic-gate printmemseg(struct memseg *memseg)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	if (!debugging_mem)
3260Sstevel@tonic-gate 		return;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	printf("memseg\n");
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	while (memseg) {
3310Sstevel@tonic-gate 		prom_printf("\tpage = 0x%p, epage = 0x%p, "
3320Sstevel@tonic-gate 		    "pfn = 0x%x, epfn = 0x%x\n",
3330Sstevel@tonic-gate 		    memseg->pages, memseg->epages,
3340Sstevel@tonic-gate 		    memseg->pages_base, memseg->pages_end);
3350Sstevel@tonic-gate 		memseg = memseg->next;
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate #define	debug_pause(str)	halt((str))
3400Sstevel@tonic-gate #define	MPRINTF(str)		if (debugging_mem) prom_printf((str))
3410Sstevel@tonic-gate #define	MPRINTF1(str, a)	if (debugging_mem) prom_printf((str), (a))
3420Sstevel@tonic-gate #define	MPRINTF2(str, a, b)	if (debugging_mem) prom_printf((str), (a), (b))
3430Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c) \
3440Sstevel@tonic-gate 	if (debugging_mem) prom_printf((str), (a), (b), (c))
3450Sstevel@tonic-gate #else	/* DEBUGGING_MEM */
3460Sstevel@tonic-gate #define	MPRINTF(str)
3470Sstevel@tonic-gate #define	MPRINTF1(str, a)
3480Sstevel@tonic-gate #define	MPRINTF2(str, a, b)
3490Sstevel@tonic-gate #define	MPRINTF3(str, a, b, c)
3500Sstevel@tonic-gate #endif	/* DEBUGGING_MEM */
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  *
3550Sstevel@tonic-gate  *                    Kernel's Virtual Memory Layout.
3560Sstevel@tonic-gate  *                       /-----------------------\
3570Sstevel@tonic-gate  * 0xFFFFFFFF.FFFFFFFF  -|                       |-
3580Sstevel@tonic-gate  *                       |   OBP's virtual page  |
3590Sstevel@tonic-gate  *                       |        tables         |
3600Sstevel@tonic-gate  * 0xFFFFFFFC.00000000  -|-----------------------|-
3610Sstevel@tonic-gate  *                       :                       :
3620Sstevel@tonic-gate  *                       :                       :
3633290Sjohansen  *                      -|-----------------------|-
3643290Sjohansen  *                       |       segzio          | (base and size vary)
3650Sstevel@tonic-gate  * 0xFFFFFE00.00000000  -|-----------------------|-
3660Sstevel@tonic-gate  *                       |                       |  Ultrasparc I/II support
3670Sstevel@tonic-gate  *                       |    segkpm segment     |  up to 2TB of physical
3680Sstevel@tonic-gate  *                       | (64-bit kernel ONLY)  |  memory, VAC has 2 colors
3690Sstevel@tonic-gate  *                       |                       |
3700Sstevel@tonic-gate  * 0xFFFFFA00.00000000  -|-----------------------|- 2TB segkpm alignment
3710Sstevel@tonic-gate  *                       :                       :
3720Sstevel@tonic-gate  *                       :                       :
3730Sstevel@tonic-gate  * 0xFFFFF810.00000000  -|-----------------------|- hole_end
3740Sstevel@tonic-gate  *                       |                       |      ^
3750Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
3760Sstevel@tonic-gate  *                       | bug requires an extra |      |
3770Sstevel@tonic-gate  *                       | 4 GB of space between |      |
3780Sstevel@tonic-gate  *                       |   hole and used RAM   |	|
3790Sstevel@tonic-gate  *                       |                       |      |
3800Sstevel@tonic-gate  * 0xFFFFF800.00000000  -|-----------------------|-     |
3810Sstevel@tonic-gate  *                       |                       |      |
3820Sstevel@tonic-gate  *                       | Virtual Address Hole  |   UltraSPARC
3830Sstevel@tonic-gate  *                       |  on UltraSPARC I/II   |  I/II * ONLY *
3840Sstevel@tonic-gate  *                       |                       |      |
3850Sstevel@tonic-gate  * 0x00000800.00000000  -|-----------------------|-     |
3860Sstevel@tonic-gate  *                       |                       |      |
3870Sstevel@tonic-gate  *                       |  UltraSPARC I/II call |      |
3880Sstevel@tonic-gate  *                       | bug requires an extra |      |
3890Sstevel@tonic-gate  *                       | 4 GB of space between |      |
3900Sstevel@tonic-gate  *                       |   hole and used RAM   |      |
3910Sstevel@tonic-gate  *                       |                       |      v
3920Sstevel@tonic-gate  * 0x000007FF.00000000  -|-----------------------|- hole_start -----
3930Sstevel@tonic-gate  *                       :                       :		   ^
3940Sstevel@tonic-gate  *                       :                       :		   |
3955648Ssetje  *                       |-----------------------|                 |
3965648Ssetje  *                       |                       |                 |
3975648Ssetje  *                       |  ecache flush area    |                 |
3985648Ssetje  *                       |  (twice largest e$)   |                 |
3995648Ssetje  *                       |                       |                 |
4003764Sdp78419  * 0x00000XXX.XXX00000  -|-----------------------|- kmem64_	   |
4013764Sdp78419  *                       | overmapped area       |   alignend_end  |
4023764Sdp78419  *                       | (kmem64_alignsize     |		   |
4033764Sdp78419  *                       |  boundary)            |		   |
4040Sstevel@tonic-gate  * 0x00000XXX.XXXXXXXX  -|-----------------------|- kmem64_end	   |
4050Sstevel@tonic-gate  *                       |                       |		   |
4060Sstevel@tonic-gate  *                       |   64-bit kernel ONLY  |		   |
4070Sstevel@tonic-gate  *                       |                       |		   |
4080Sstevel@tonic-gate  *                       |    kmem64 segment     |		   |
4090Sstevel@tonic-gate  *                       |                       |		   |
4100Sstevel@tonic-gate  *                       | (Relocated extra HME  |	     Approximately
4110Sstevel@tonic-gate  *                       |   block allocations,  |	    1 TB of virtual
4120Sstevel@tonic-gate  *                       |   memnode freelists,  |	     address space
4130Sstevel@tonic-gate  *                       |    HME hash buckets,  |		   |
4140Sstevel@tonic-gate  *                       | mml_table, kpmp_table,|		   |
4150Sstevel@tonic-gate  *                       |  page_t array and     |		   |
4160Sstevel@tonic-gate  *                       |  hashblock pool to    |		   |
4170Sstevel@tonic-gate  *                       |   avoid hard-coded    |		   |
4180Sstevel@tonic-gate  *                       |     32-bit vaddr      |		   |
4190Sstevel@tonic-gate  *                       |     limitations)      |		   |
4200Sstevel@tonic-gate  *                       |                       |		   v
4210Sstevel@tonic-gate  * 0x00000700.00000000  -|-----------------------|- SYSLIMIT (kmem64_base)
4220Sstevel@tonic-gate  *                       |                       |
4230Sstevel@tonic-gate  *                       |  segkmem segment      | (SYSLIMIT - SYSBASE = 4TB)
4240Sstevel@tonic-gate  *                       |                       |
4250Sstevel@tonic-gate  * 0x00000300.00000000  -|-----------------------|- SYSBASE
4260Sstevel@tonic-gate  *                       :                       :
4270Sstevel@tonic-gate  *                       :                       :
4280Sstevel@tonic-gate  *                      -|-----------------------|-
4290Sstevel@tonic-gate  *                       |                       |
4300Sstevel@tonic-gate  *                       |  segmap segment       |   SEGMAPSIZE (1/8th physmem,
4310Sstevel@tonic-gate  *                       |                       |               256G MAX)
4320Sstevel@tonic-gate  * 0x000002a7.50000000  -|-----------------------|- SEGMAPBASE
4330Sstevel@tonic-gate  *                       :                       :
4340Sstevel@tonic-gate  *                       :                       :
4350Sstevel@tonic-gate  *                      -|-----------------------|-
4360Sstevel@tonic-gate  *                       |                       |
4370Sstevel@tonic-gate  *                       |       segkp           |    SEGKPSIZE (2GB)
4380Sstevel@tonic-gate  *                       |                       |
4390Sstevel@tonic-gate  *                       |                       |
4400Sstevel@tonic-gate  * 0x000002a1.00000000  -|-----------------------|- SEGKPBASE
4410Sstevel@tonic-gate  *                       |                       |
4420Sstevel@tonic-gate  * 0x000002a0.00000000  -|-----------------------|- MEMSCRUBBASE
4430Sstevel@tonic-gate  *                       |                       |       (SEGKPBASE - 0x400000)
4440Sstevel@tonic-gate  * 0x0000029F.FFE00000  -|-----------------------|- ARGSBASE
4450Sstevel@tonic-gate  *                       |                       |       (MEMSCRUBBASE - NCARGS)
4460Sstevel@tonic-gate  * 0x0000029F.FFD80000  -|-----------------------|- PPMAPBASE
4470Sstevel@tonic-gate  *                       |                       |       (ARGSBASE - PPMAPSIZE)
4480Sstevel@tonic-gate  * 0x0000029F.FFD00000  -|-----------------------|- PPMAP_FAST_BASE
4490Sstevel@tonic-gate  *                       |                       |
4500Sstevel@tonic-gate  * 0x0000029F.FF980000  -|-----------------------|- PIOMAPBASE
4510Sstevel@tonic-gate  *                       |                       |
4520Sstevel@tonic-gate  * 0x0000029F.FF580000  -|-----------------------|- NARG_BASE
4530Sstevel@tonic-gate  *                       :                       :
4540Sstevel@tonic-gate  *                       :                       :
4550Sstevel@tonic-gate  * 0x00000000.FFFFFFFF  -|-----------------------|- OFW_END_ADDR
4560Sstevel@tonic-gate  *                       |                       |
4570Sstevel@tonic-gate  *                       |         OBP           |
4580Sstevel@tonic-gate  *                       |                       |
4590Sstevel@tonic-gate  * 0x00000000.F0000000  -|-----------------------|- OFW_START_ADDR
4600Sstevel@tonic-gate  *                       |         kmdb          |
4610Sstevel@tonic-gate  * 0x00000000.EDD00000  -|-----------------------|- SEGDEBUGBASE
4620Sstevel@tonic-gate  *                       :                       :
4630Sstevel@tonic-gate  *                       :                       :
4640Sstevel@tonic-gate  * 0x00000000.7c000000  -|-----------------------|- SYSLIMIT32
4650Sstevel@tonic-gate  *                       |                       |
4660Sstevel@tonic-gate  *                       |  segkmem32 segment    | (SYSLIMIT32 - SYSBASE32 =
4670Sstevel@tonic-gate  *                       |                       |    ~64MB)
4685648Ssetje  * 0x00000000.70002000  -|-----------------------|
4690Sstevel@tonic-gate  *                       |     panicbuf          |
4705648Ssetje  * 0x00000000.70000000  -|-----------------------|- SYSBASE32
4715648Ssetje  *                       |       boot-time       |
4725648Ssetje  *                       |    temporary space    |
4735648Ssetje  * 0x00000000.4C000000  -|-----------------------|- BOOTTMPBASE
4740Sstevel@tonic-gate  *                       :                       :
4750Sstevel@tonic-gate  *                       :                       :
4760Sstevel@tonic-gate  *                       |                       |
4770Sstevel@tonic-gate  *                       |-----------------------|- econtig32
4780Sstevel@tonic-gate  *                       |    vm structures      |
4790Sstevel@tonic-gate  * 0x00000000.01C00000   |-----------------------|- nalloc_end
4800Sstevel@tonic-gate  *                       |         TSBs          |
4810Sstevel@tonic-gate  *                       |-----------------------|- end/nalloc_base
4820Sstevel@tonic-gate  *                       |   kernel data & bss   |
4830Sstevel@tonic-gate  * 0x00000000.01800000  -|-----------------------|
4840Sstevel@tonic-gate  *                       :   nucleus text hole   :
4850Sstevel@tonic-gate  * 0x00000000.01400000  -|-----------------------|
4860Sstevel@tonic-gate  *                       :                       :
4870Sstevel@tonic-gate  *                       |-----------------------|
4880Sstevel@tonic-gate  *                       |      module text      |
4890Sstevel@tonic-gate  *                       |-----------------------|- e_text/modtext
4900Sstevel@tonic-gate  *                       |      kernel text      |
4910Sstevel@tonic-gate  *                       |-----------------------|
4920Sstevel@tonic-gate  *                       |    trap table (48k)   |
4930Sstevel@tonic-gate  * 0x00000000.01000000  -|-----------------------|- KERNELBASE
4940Sstevel@tonic-gate  *                       | reserved for trapstat |} TSTAT_TOTAL_SIZE
4950Sstevel@tonic-gate  *                       |-----------------------|
4960Sstevel@tonic-gate  *                       |                       |
4970Sstevel@tonic-gate  *                       |        invalid        |
4980Sstevel@tonic-gate  *                       |                       |
4990Sstevel@tonic-gate  * 0x00000000.00000000  _|_______________________|
5000Sstevel@tonic-gate  *
5010Sstevel@tonic-gate  *
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  *                   32-bit User Virtual Memory Layout.
5040Sstevel@tonic-gate  *                       /-----------------------\
5050Sstevel@tonic-gate  *                       |                       |
5060Sstevel@tonic-gate  *                       |        invalid        |
5070Sstevel@tonic-gate  *                       |                       |
5080Sstevel@tonic-gate  *          0xFFC00000  -|-----------------------|- USERLIMIT
5090Sstevel@tonic-gate  *                       |       user stack      |
5100Sstevel@tonic-gate  *                       :                       :
5110Sstevel@tonic-gate  *                       :                       :
5120Sstevel@tonic-gate  *                       :                       :
5130Sstevel@tonic-gate  *                       |       user data       |
5140Sstevel@tonic-gate  *                      -|-----------------------|-
5150Sstevel@tonic-gate  *                       |       user text       |
5160Sstevel@tonic-gate  *          0x00002000  -|-----------------------|-
5170Sstevel@tonic-gate  *                       |       invalid         |
5180Sstevel@tonic-gate  *          0x00000000  _|_______________________|
5190Sstevel@tonic-gate  *
5200Sstevel@tonic-gate  *
5210Sstevel@tonic-gate  *
5220Sstevel@tonic-gate  *                   64-bit User Virtual Memory Layout.
5230Sstevel@tonic-gate  *                       /-----------------------\
5240Sstevel@tonic-gate  *                       |                       |
5250Sstevel@tonic-gate  *                       |        invalid        |
5260Sstevel@tonic-gate  *                       |                       |
5270Sstevel@tonic-gate  *  0xFFFFFFFF.80000000 -|-----------------------|- USERLIMIT
5280Sstevel@tonic-gate  *                       |       user stack      |
5290Sstevel@tonic-gate  *                       :                       :
5300Sstevel@tonic-gate  *                       :                       :
5310Sstevel@tonic-gate  *                       :                       :
5320Sstevel@tonic-gate  *                       |       user data       |
5330Sstevel@tonic-gate  *                      -|-----------------------|-
5340Sstevel@tonic-gate  *                       |       user text       |
5355648Ssetje  *  0x00000000.01000000 -|-----------------------|-
5360Sstevel@tonic-gate  *                       |       invalid         |
5370Sstevel@tonic-gate  *  0x00000000.00000000 _|_______________________|
5380Sstevel@tonic-gate  */
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate extern caddr_t ecache_init_scrub_flush_area(caddr_t alloc_base);
5410Sstevel@tonic-gate extern uint64_t ecache_flush_address(void);
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate #pragma weak load_platform_modules
5441772Sjl139090 #pragma weak plat_startup_memlist
5450Sstevel@tonic-gate #pragma weak ecache_init_scrub_flush_area
5460Sstevel@tonic-gate #pragma weak ecache_flush_address
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * By default the DR Cage is enabled for maximum OS
5510Sstevel@tonic-gate  * MPSS performance.  Users needing to disable the cage mechanism
5520Sstevel@tonic-gate  * can set this variable to zero via /etc/system.
5530Sstevel@tonic-gate  * Disabling the cage on systems supporting Dynamic Reconfiguration (DR)
5540Sstevel@tonic-gate  * will result in loss of DR functionality.
5550Sstevel@tonic-gate  * Platforms wishing to disable kernel Cage by default
5560Sstevel@tonic-gate  * should do so in their set_platform_defaults() routine.
5570Sstevel@tonic-gate  */
5580Sstevel@tonic-gate int	kernel_cage_enable = 1;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate static void
5610Sstevel@tonic-gate setup_cage_params(void)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	void (*func)(void);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	func = (void (*)(void))kobj_getsymvalue("set_platform_cage_params", 0);
5660Sstevel@tonic-gate 	if (func != NULL) {
5670Sstevel@tonic-gate 		(*func)();
5680Sstevel@tonic-gate 		return;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (kernel_cage_enable == 0) {
5720Sstevel@tonic-gate 		return;
5730Sstevel@tonic-gate 	}
5744266Sdp78419 	kcage_range_init(phys_avail, KCAGE_DOWN, total_pages / 256);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if (kcage_on) {
5770Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is ENABLED");
5780Sstevel@tonic-gate 	} else {
5790Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!Kernel Cage is DISABLED");
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate  * Machine-dependent startup code
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate void
5880Sstevel@tonic-gate startup(void)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	startup_init();
5910Sstevel@tonic-gate 	if (&startup_platform)
5920Sstevel@tonic-gate 		startup_platform();
5930Sstevel@tonic-gate 	startup_memlist();
5940Sstevel@tonic-gate 	startup_modules();
5950Sstevel@tonic-gate 	setup_cage_params();
5960Sstevel@tonic-gate 	startup_bop_gone();
5970Sstevel@tonic-gate 	startup_vm();
5980Sstevel@tonic-gate 	startup_end();
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate struct regs sync_reg_buf;
6020Sstevel@tonic-gate uint64_t sync_tt;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate void
6050Sstevel@tonic-gate sync_handler(void)
6060Sstevel@tonic-gate {
6075084Sjohnlev 	struct  panic_trap_info 	ti;
6080Sstevel@tonic-gate 	int i;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * Prevent trying to talk to the other CPUs since they are
6120Sstevel@tonic-gate 	 * sitting in the prom and won't reply.
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
6150Sstevel@tonic-gate 		if ((i != CPU->cpu_id) && CPU_XCALL_READY(i)) {
6160Sstevel@tonic-gate 			cpu[i]->cpu_flags &= ~CPU_READY;
6170Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
6180Sstevel@tonic-gate 			CPUSET_DEL(cpu_ready_set, cpu[i]->cpu_id);
6190Sstevel@tonic-gate 		}
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 * We've managed to get here without going through the
6240Sstevel@tonic-gate 	 * normal panic code path. Try and save some useful
6250Sstevel@tonic-gate 	 * information.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	if (!panicstr && (curthread->t_panic_trap == NULL)) {
6280Sstevel@tonic-gate 		ti.trap_type = sync_tt;
6290Sstevel@tonic-gate 		ti.trap_regs = &sync_reg_buf;
6300Sstevel@tonic-gate 		ti.trap_addr = NULL;
6310Sstevel@tonic-gate 		ti.trap_mmu_fsr = 0x0;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		curthread->t_panic_trap = &ti;
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * If we're re-entering the panic path, update the signature
6380Sstevel@tonic-gate 	 * block so that the SC knows we're in the second part of panic.
6390Sstevel@tonic-gate 	 */
6400Sstevel@tonic-gate 	if (panicstr)
6410Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DUMP, -1);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	nopanicdebug = 1; /* do not perform debug_enter() prior to dump */
6440Sstevel@tonic-gate 	panic("sync initiated");
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate static void
6490Sstevel@tonic-gate startup_init(void)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * We want to save the registers while we're still in OBP
6530Sstevel@tonic-gate 	 * so that we know they haven't been fiddled with since.
6540Sstevel@tonic-gate 	 * (In principle, OBP can't change them just because it
6550Sstevel@tonic-gate 	 * makes a callback, but we'd rather not depend on that
6560Sstevel@tonic-gate 	 * behavior.)
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	char		sync_str[] =
6594528Spaulsan 	    "warning @ warning off : sync "
6604528Spaulsan 	    "%%tl-c %%tstate h# %p x! "
6614528Spaulsan 	    "%%g1 h# %p x! %%g2 h# %p x! %%g3 h# %p x! "
6624528Spaulsan 	    "%%g4 h# %p x! %%g5 h# %p x! %%g6 h# %p x! "
6634528Spaulsan 	    "%%g7 h# %p x! %%o0 h# %p x! %%o1 h# %p x! "
6644528Spaulsan 	    "%%o2 h# %p x! %%o3 h# %p x! %%o4 h# %p x! "
6654528Spaulsan 	    "%%o5 h# %p x! %%o6 h# %p x! %%o7 h# %p x! "
6664528Spaulsan 	    "%%tl-c %%tpc h# %p x! %%tl-c %%tnpc h# %p x! "
6674528Spaulsan 	    "%%y h# %p l! %%tl-c %%tt h# %p x! "
6684528Spaulsan 	    "sync ; warning !";
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/*
6710Sstevel@tonic-gate 	 * 20 == num of %p substrings
6720Sstevel@tonic-gate 	 * 16 == max num of chars %p will expand to.
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	char 		bp[sizeof (sync_str) + 16 * 20];
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * Initialize ptl1 stack for the 1st CPU.
6780Sstevel@tonic-gate 	 */
6790Sstevel@tonic-gate 	ptl1_init_cpu(&cpu0);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * Initialize the address map for cache consistent mappings
6830Sstevel@tonic-gate 	 * to random pages; must be done after vac_size is set.
6840Sstevel@tonic-gate 	 */
6850Sstevel@tonic-gate 	ppmapinit();
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * Initialize the PROM callback handler.
6890Sstevel@tonic-gate 	 */
6900Sstevel@tonic-gate 	init_vx_handler();
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * have prom call sync_callback() to handle the sync and
6940Sstevel@tonic-gate 	 * save some useful information which will be stored in the
6950Sstevel@tonic-gate 	 * core file later.
6960Sstevel@tonic-gate 	 */
6970Sstevel@tonic-gate 	(void) sprintf((char *)bp, sync_str,
6984528Spaulsan 	    (void *)&sync_reg_buf.r_tstate, (void *)&sync_reg_buf.r_g1,
6994528Spaulsan 	    (void *)&sync_reg_buf.r_g2, (void *)&sync_reg_buf.r_g3,
7004528Spaulsan 	    (void *)&sync_reg_buf.r_g4, (void *)&sync_reg_buf.r_g5,
7014528Spaulsan 	    (void *)&sync_reg_buf.r_g6, (void *)&sync_reg_buf.r_g7,
7024528Spaulsan 	    (void *)&sync_reg_buf.r_o0, (void *)&sync_reg_buf.r_o1,
7034528Spaulsan 	    (void *)&sync_reg_buf.r_o2, (void *)&sync_reg_buf.r_o3,
7044528Spaulsan 	    (void *)&sync_reg_buf.r_o4, (void *)&sync_reg_buf.r_o5,
7054528Spaulsan 	    (void *)&sync_reg_buf.r_o6, (void *)&sync_reg_buf.r_o7,
7064528Spaulsan 	    (void *)&sync_reg_buf.r_pc, (void *)&sync_reg_buf.r_npc,
7074528Spaulsan 	    (void *)&sync_reg_buf.r_y, (void *)&sync_tt);
7080Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
7090Sstevel@tonic-gate 	add_vx_handler("sync", 1, (void (*)(cell_t *))sync_handler);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7125648Ssetje 
7135648Ssetje size_t
7145648Ssetje calc_pp_sz(pgcnt_t npages)
7155648Ssetje {
7165648Ssetje 
7175648Ssetje 	return (npages * sizeof (struct page));
7185648Ssetje }
7195648Ssetje 
7205648Ssetje size_t
7215648Ssetje calc_kpmpp_sz(pgcnt_t npages)
7225648Ssetje {
7235648Ssetje 
7245648Ssetje 	kpm_pgshft = (kpm_smallpages == 0) ? MMU_PAGESHIFT4M : MMU_PAGESHIFT;
7255648Ssetje 	kpm_pgsz = 1ull << kpm_pgshft;
7265648Ssetje 	kpm_pgoff = kpm_pgsz - 1;
7275648Ssetje 	kpmp2pshft = kpm_pgshft - PAGESHIFT;
7285648Ssetje 	kpmpnpgs = 1 << kpmp2pshft;
7295648Ssetje 
7305648Ssetje 	if (kpm_smallpages == 0) {
7315648Ssetje 		/*
7325648Ssetje 		 * Avoid fragmentation problems in kphysm_init()
7335648Ssetje 		 * by allocating for all of physical memory
7345648Ssetje 		 */
7355648Ssetje 		kpm_npages = ptokpmpr(physinstalled);
7365648Ssetje 		return (kpm_npages * sizeof (kpm_page_t));
7375648Ssetje 	} else {
7385648Ssetje 		kpm_npages = npages;
7395648Ssetje 		return (kpm_npages * sizeof (kpm_spage_t));
7405648Ssetje 	}
7415648Ssetje }
7425648Ssetje 
7435648Ssetje size_t
7445648Ssetje calc_pagehash_sz(pgcnt_t npages)
7455648Ssetje {
7465648Ssetje 
7475648Ssetje 	/*
7485648Ssetje 	 * The page structure hash table size is a power of 2
7495648Ssetje 	 * such that the average hash chain length is PAGE_HASHAVELEN.
7505648Ssetje 	 */
7515648Ssetje 	page_hashsz = npages / PAGE_HASHAVELEN;
7525648Ssetje 	page_hashsz = 1 << highbit(page_hashsz);
7535648Ssetje 	return (page_hashsz * sizeof (struct page *));
7545648Ssetje }
7555648Ssetje 
7567218Ssvemuri int testkmem64_smchunks = 0;
7577218Ssvemuri 
7587218Ssvemuri int
7595648Ssetje alloc_kmem64(caddr_t base, caddr_t end)
7605648Ssetje {
7615648Ssetje 	int i;
7625648Ssetje 	caddr_t aligned_end = NULL;
7635648Ssetje 
7647218Ssvemuri 	if (testkmem64_smchunks)
7657218Ssvemuri 		return (1);
7667218Ssvemuri 
7675648Ssetje 	/*
7685648Ssetje 	 * Make one large memory alloc after figuring out the 64-bit size. This
7695648Ssetje 	 * will enable use of the largest page size appropriate for the system
7705648Ssetje 	 * architecture.
7715648Ssetje 	 */
7725648Ssetje 	ASSERT(mmu_exported_pagesize_mask & (1 << TTE8K));
7735648Ssetje 	ASSERT(IS_P2ALIGNED(base, TTEBYTES(max_bootlp_tteszc)));
7745648Ssetje 	for (i = max_bootlp_tteszc; i >= TTE8K; i--) {
7755648Ssetje 		size_t alloc_size, alignsize;
7765648Ssetje #if !defined(C_OBP)
7775648Ssetje 		unsigned long long pa;
7785648Ssetje #endif	/* !C_OBP */
7795648Ssetje 
7805648Ssetje 		if ((mmu_exported_pagesize_mask & (1 << i)) == 0)
7815648Ssetje 			continue;
7825648Ssetje 		alignsize = TTEBYTES(i);
7835648Ssetje 		kmem64_szc = i;
7845648Ssetje 
7855648Ssetje 		/* limit page size for small memory */
7865648Ssetje 		if (mmu_btop(alignsize) > (npages >> 2))
7875648Ssetje 			continue;
7885648Ssetje 
7895648Ssetje 		aligned_end = (caddr_t)roundup((uintptr_t)end, alignsize);
7905648Ssetje 		alloc_size = aligned_end - base;
7915648Ssetje #if !defined(C_OBP)
7925648Ssetje 		if (prom_allocate_phys(alloc_size, alignsize, &pa) == 0) {
7935648Ssetje 			if (prom_claim_virt(alloc_size, base) != (caddr_t)-1) {
7945648Ssetje 				kmem64_pabase = pa;
7955648Ssetje 				kmem64_aligned_end = aligned_end;
7965648Ssetje 				install_kmem64_tte();
7975648Ssetje 				break;
7985648Ssetje 			} else {
7995648Ssetje 				prom_free_phys(alloc_size, pa);
8005648Ssetje 			}
8015648Ssetje 		}
8025648Ssetje #else	/* !C_OBP */
8035648Ssetje 		if (prom_alloc(base, alloc_size, alignsize) == base) {
8045648Ssetje 			kmem64_pabase = va_to_pa(kmem64_base);
8055648Ssetje 			kmem64_aligned_end = aligned_end;
8065648Ssetje 			break;
8075648Ssetje 		}
8085648Ssetje #endif	/* !C_OBP */
8095648Ssetje 		if (i == TTE8K) {
8107218Ssvemuri #ifdef sun4v
8117218Ssvemuri 			/* return failure to try small allocations */
8127218Ssvemuri 			return (1);
8137218Ssvemuri #else
8145648Ssetje 			prom_panic("kmem64 allocation failure");
8157218Ssvemuri #endif
8165648Ssetje 		}
8175648Ssetje 	}
8185648Ssetje 	ASSERT(aligned_end != NULL);
8197218Ssvemuri 	return (0);
8205648Ssetje }
8215648Ssetje 
8225648Ssetje static prom_memlist_t *boot_physinstalled, *boot_physavail, *boot_virtavail;
8230Sstevel@tonic-gate static size_t boot_physinstalled_len, boot_physavail_len, boot_virtavail_len;
8240Sstevel@tonic-gate 
8255648Ssetje #define	IVSIZE	roundup(((MAXIVNUM * sizeof (intr_vec_t *)) + \
8265648Ssetje 			(MAX_RSVD_IV * sizeof (intr_vec_t)) + \
8275648Ssetje 			(MAX_RSVD_IVX * sizeof (intr_vecx_t))), PAGESIZE)
8280Sstevel@tonic-gate 
8293764Sdp78419 #if !defined(C_OBP)
8303764Sdp78419 /*
8313764Sdp78419  * Install a temporary tte handler in OBP for kmem64 area.
8323764Sdp78419  *
8333764Sdp78419  * We map kmem64 area with large pages before the trap table is taken
8343764Sdp78419  * over. Since OBP makes 8K mappings, it can create 8K tlb entries in
8353764Sdp78419  * the same area. Duplicate tlb entries with different page sizes
8363764Sdp78419  * cause unpredicatble behavior.  To avoid this, we don't create
8373764Sdp78419  * kmem64 mappings via BOP_ALLOC (ends up as prom_alloc() call to
8383764Sdp78419  * OBP).  Instead, we manage translations with a temporary va>tte-data
8393764Sdp78419  * handler (kmem64-tte).  This handler is replaced by unix-tte when
8403764Sdp78419  * the trap table is taken over.
8413764Sdp78419  *
8423764Sdp78419  * The temporary handler knows the physical address of the kmem64
8433764Sdp78419  * area. It uses the prom's pgmap@ Forth word for other addresses.
8443764Sdp78419  *
8453764Sdp78419  * We have to use BOP_ALLOC() method for C-OBP platforms because
8463764Sdp78419  * pgmap@ is not defined in C-OBP. C-OBP is only used on serengeti
8473764Sdp78419  * sun4u platforms. On sun4u we flush tlb after trap table is taken
8483764Sdp78419  * over if we use large pages for kernel heap and kmem64. Since sun4u
8493764Sdp78419  * prom (unlike sun4v) calls va>tte-data first for client address
8503764Sdp78419  * translation prom's ttes for kmem64 can't get into TLB even if we
8513764Sdp78419  * later switch to prom's trap table again. C-OBP uses 4M pages for
8523764Sdp78419  * client mappings when possible so on all platforms we get the
8533764Sdp78419  * benefit from large mappings for kmem64 area immediately during
8543764Sdp78419  * boot.
8553764Sdp78419  *
8563764Sdp78419  * pseudo code:
8573764Sdp78419  * if (context != 0) {
8583764Sdp78419  * 	return false
8593764Sdp78419  * } else if (miss_va in range[kmem64_base, kmem64_end)) {
8603764Sdp78419  *	tte = tte_template +
8613764Sdp78419  *		(((miss_va & pagemask) - kmem64_base));
8623764Sdp78419  *	return tte, true
8633764Sdp78419  * } else {
8643764Sdp78419  *	return pgmap@ result
8653764Sdp78419  * }
8663764Sdp78419  */
8673764Sdp78419 char kmem64_obp_str[] =
8685648Ssetje 	"h# %lx constant kmem64-base "
8695648Ssetje 	"h# %lx constant kmem64-end "
8705648Ssetje 	"h# %lx constant kmem64-pagemask "
8715648Ssetje 	"h# %lx constant kmem64-template "
8723764Sdp78419 
8733764Sdp78419 	": kmem64-tte ( addr cnum -- false | tte-data true ) "
8743764Sdp78419 	"    if                                       ( addr ) "
8753764Sdp78419 	"       drop false exit then                  ( false ) "
8765648Ssetje 	"    dup  kmem64-base kmem64-end  within  if  ( addr ) "
8775648Ssetje 	"	kmem64-pagemask and                   ( addr' ) "
8785648Ssetje 	"	kmem64-base -                         ( addr' ) "
8795648Ssetje 	"	kmem64-template +                     ( tte ) "
8803764Sdp78419 	"	true                                  ( tte true ) "
8813764Sdp78419 	"    else                                     ( addr ) "
8823764Sdp78419 	"	pgmap@                                ( tte ) "
8833764Sdp78419 	"       dup 0< if true else drop false then   ( tte true  |  false ) "
8843764Sdp78419 	"    then                                     ( tte true  |  false ) "
8853764Sdp78419 	"; "
8863764Sdp78419 
8873764Sdp78419 	"' kmem64-tte is va>tte-data "
8883764Sdp78419 ;
8893764Sdp78419 
8905648Ssetje static void
8913764Sdp78419 install_kmem64_tte()
8923764Sdp78419 {
8933764Sdp78419 	char b[sizeof (kmem64_obp_str) + (4 * 16)];
8943764Sdp78419 	tte_t tte;
8953764Sdp78419 
8963764Sdp78419 	PRM_DEBUG(kmem64_pabase);
8973764Sdp78419 	PRM_DEBUG(kmem64_szc);
8983764Sdp78419 	sfmmu_memtte(&tte, kmem64_pabase >> MMU_PAGESHIFT,
899*10271SJason.Beloro@Sun.COM 	    PROC_DATA | HAT_NOSYNC, kmem64_szc);
9003764Sdp78419 	PRM_DEBUG(tte.ll);
9013764Sdp78419 	(void) sprintf(b, kmem64_obp_str,
9023764Sdp78419 	    kmem64_base, kmem64_end, TTE_PAGEMASK(kmem64_szc), tte.ll);
9033764Sdp78419 	ASSERT(strlen(b) < sizeof (b));
9043764Sdp78419 	prom_interpret(b, 0, 0, 0, 0, 0);
9053764Sdp78419 }
9063764Sdp78419 #endif	/* !C_OBP */
9073764Sdp78419 
9080Sstevel@tonic-gate /*
9090Sstevel@tonic-gate  * As OBP takes up some RAM when the system boots, pages will already be "lost"
9100Sstevel@tonic-gate  * to the system and reflected in npages by the time we see it.
9110Sstevel@tonic-gate  *
9120Sstevel@tonic-gate  * We only want to allocate kernel structures in the 64-bit virtual address
9130Sstevel@tonic-gate  * space on systems with enough RAM to make the overhead of keeping track of
9140Sstevel@tonic-gate  * an extra kernel memory segment worthwhile.
9150Sstevel@tonic-gate  *
9160Sstevel@tonic-gate  * Since OBP has already performed its memory allocations by this point, if we
9170Sstevel@tonic-gate  * have more than MINMOVE_RAM_MB MB of RAM left free, go ahead and map
9180Sstevel@tonic-gate  * memory in the 64-bit virtual address space; otherwise keep allocations
9190Sstevel@tonic-gate  * contiguous with we've mapped so far in the 32-bit virtual address space.
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate #define	MINMOVE_RAM_MB	((size_t)1900)
9220Sstevel@tonic-gate #define	MB_TO_BYTES(mb)	((mb) * 1048576ul)
9235872Ssetje #define	BYTES_TO_MB(b) ((b) / 1048576ul)
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate pgcnt_t	tune_npages = (pgcnt_t)
9260Sstevel@tonic-gate 	(MB_TO_BYTES(MINMOVE_RAM_MB)/ (size_t)MMU_PAGESIZE);
9270Sstevel@tonic-gate 
9283733Sdp78419 #pragma weak page_set_colorequiv_arr_cpu
9293733Sdp78419 extern void page_set_colorequiv_arr_cpu(void);
9305648Ssetje extern void page_set_colorequiv_arr(void);
9315648Ssetje 
9325872Ssetje static pgcnt_t ramdisk_npages;
9335872Ssetje static struct memlist *old_phys_avail;
9345872Ssetje 
9355872Ssetje kcage_dir_t kcage_startup_dir = KCAGE_DOWN;
9363733Sdp78419 
9370Sstevel@tonic-gate static void
9380Sstevel@tonic-gate startup_memlist(void)
9390Sstevel@tonic-gate {
9405648Ssetje 	size_t hmehash_sz, pagelist_sz, tt_sz;
9415648Ssetje 	size_t psetable_sz;
9420Sstevel@tonic-gate 	caddr_t alloc_base;
9430Sstevel@tonic-gate 	caddr_t memspace;
9440Sstevel@tonic-gate 	struct memlist *cur;
9450Sstevel@tonic-gate 	size_t syslimit = (size_t)SYSLIMIT;
9460Sstevel@tonic-gate 	size_t sysbase = (size_t)SYSBASE;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	/*
9490Sstevel@tonic-gate 	 * Initialize enough of the system to allow kmem_alloc to work by
9500Sstevel@tonic-gate 	 * calling boot to allocate its memory until the time that
9510Sstevel@tonic-gate 	 * kvm_init is completed.  The page structs are allocated after
9520Sstevel@tonic-gate 	 * rounding up end to the nearest page boundary; the memsegs are
9530Sstevel@tonic-gate 	 * initialized and the space they use comes from the kernel heap.
9540Sstevel@tonic-gate 	 * With appropriate initialization, they can be reallocated later
9550Sstevel@tonic-gate 	 * to a size appropriate for the machine's configuration.
9560Sstevel@tonic-gate 	 *
9570Sstevel@tonic-gate 	 * At this point, memory is allocated for things that will never
9580Sstevel@tonic-gate 	 * need to be freed, this used to be "valloced".  This allows a
9590Sstevel@tonic-gate 	 * savings as the pages don't need page structures to describe
9600Sstevel@tonic-gate 	 * them because them will not be managed by the vm system.
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * We're loaded by boot with the following configuration (as
9650Sstevel@tonic-gate 	 * specified in the sun4u/conf/Mapfile):
9660Sstevel@tonic-gate 	 *
9670Sstevel@tonic-gate 	 * 	text:		4 MB chunk aligned on a 4MB boundary
9680Sstevel@tonic-gate 	 * 	data & bss:	4 MB chunk aligned on a 4MB boundary
9690Sstevel@tonic-gate 	 *
9700Sstevel@tonic-gate 	 * These two chunks will eventually be mapped by 2 locked 4MB
9710Sstevel@tonic-gate 	 * ttes and will represent the nucleus of the kernel.  This gives
9720Sstevel@tonic-gate 	 * us some free space that is already allocated, some or all of
9730Sstevel@tonic-gate 	 * which is made available to kernel module text.
9740Sstevel@tonic-gate 	 *
9750Sstevel@tonic-gate 	 * The free space in the data-bss chunk is used for nucleus
9760Sstevel@tonic-gate 	 * allocatable data structures and we reserve it using the
9770Sstevel@tonic-gate 	 * nalloc_base and nalloc_end variables.  This space is currently
9780Sstevel@tonic-gate 	 * being used for hat data structures required for tlb miss
9790Sstevel@tonic-gate 	 * handling operations.  We align nalloc_base to a l2 cache
9800Sstevel@tonic-gate 	 * linesize because this is the line size the hardware uses to
9810Sstevel@tonic-gate 	 * maintain cache coherency.
9825648Ssetje 	 * 512K is carved out for module data.
9830Sstevel@tonic-gate 	 */
9840Sstevel@tonic-gate 
9855648Ssetje 	moddata = (caddr_t)roundup((uintptr_t)e_data, MMU_PAGESIZE);
9865648Ssetje 	e_moddata = moddata + MODDATA;
9870Sstevel@tonic-gate 	nalloc_base = e_moddata;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	nalloc_end = (caddr_t)roundup((uintptr_t)nalloc_base, MMU_PAGESIZE4M);
9900Sstevel@tonic-gate 	valloc_base = nalloc_base;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	/*
9930Sstevel@tonic-gate 	 * Calculate the start of the data segment.
9940Sstevel@tonic-gate 	 */
9955648Ssetje 	if (((uintptr_t)e_moddata & MMU_PAGEMASK4M) != (uintptr_t)s_data)
9965648Ssetje 		prom_panic("nucleus data overflow");
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	PRM_DEBUG(moddata);
9990Sstevel@tonic-gate 	PRM_DEBUG(nalloc_base);
10000Sstevel@tonic-gate 	PRM_DEBUG(nalloc_end);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	/*
10030Sstevel@tonic-gate 	 * Remember any slop after e_text so we can give it to the modules.
10040Sstevel@tonic-gate 	 */
10050Sstevel@tonic-gate 	PRM_DEBUG(e_text);
10060Sstevel@tonic-gate 	modtext = (caddr_t)roundup((uintptr_t)e_text, MMU_PAGESIZE);
10073791Skchow 	if (((uintptr_t)e_text & MMU_PAGEMASK4M) != (uintptr_t)s_text)
10083764Sdp78419 		prom_panic("nucleus text overflow");
10090Sstevel@tonic-gate 	modtext_sz = (caddr_t)roundup((uintptr_t)modtext, MMU_PAGESIZE4M) -
10100Sstevel@tonic-gate 	    modtext;
10110Sstevel@tonic-gate 	PRM_DEBUG(modtext);
10120Sstevel@tonic-gate 	PRM_DEBUG(modtext_sz);
10130Sstevel@tonic-gate 
10145648Ssetje 	init_boot_memlists();
10150Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
10160Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
10170Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
10185648Ssetje 
10190Sstevel@tonic-gate 	/*
10200Sstevel@tonic-gate 	 * Remember what the physically available highest page is
10210Sstevel@tonic-gate 	 * so that dumpsys works properly, and find out how much
10220Sstevel@tonic-gate 	 * memory is installed.
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	installed_top_size_memlist_array(boot_physinstalled,
10250Sstevel@tonic-gate 	    boot_physinstalled_len, &physmax, &physinstalled);
10260Sstevel@tonic-gate 	PRM_DEBUG(physinstalled);
10270Sstevel@tonic-gate 	PRM_DEBUG(physmax);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/* Fill out memory nodes config structure */
10300Sstevel@tonic-gate 	startup_build_mem_nodes(boot_physinstalled, boot_physinstalled_len);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/*
10330Sstevel@tonic-gate 	 * npages is the maximum of available physical memory possible.
10340Sstevel@tonic-gate 	 * (ie. it will never be more than this)
10355648Ssetje 	 *
10365648Ssetje 	 * When we boot from a ramdisk, the ramdisk memory isn't free, so
10375648Ssetje 	 * using phys_avail will underestimate what will end up being freed.
10385648Ssetje 	 * A better initial guess is just total memory minus the kernel text
10390Sstevel@tonic-gate 	 */
10405648Ssetje 	npages = physinstalled - btop(MMU_PAGESIZE4M);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/*
10435648Ssetje 	 * First allocate things that can go in the nucleus data page
10445648Ssetje 	 * (fault status, TSBs, dmv, CPUs)
10450Sstevel@tonic-gate 	 */
10465648Ssetje 	ndata_alloc_init(&ndata, (uintptr_t)nalloc_base, (uintptr_t)nalloc_end);
10475648Ssetje 
10480Sstevel@tonic-gate 	if ((&ndata_alloc_mmfsa != NULL) && (ndata_alloc_mmfsa(&ndata) != 0))
10490Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after mfsa alloc");
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	if (ndata_alloc_tsbs(&ndata, npages) != 0)
10520Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after tsbs alloc");
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	if (ndata_alloc_dmv(&ndata) != 0)
10550Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after dmv alloc");
10560Sstevel@tonic-gate 
10575648Ssetje 	if (ndata_alloc_page_mutexs(&ndata) != 0)
10580Sstevel@tonic-gate 		cmn_err(CE_PANIC,
10590Sstevel@tonic-gate 		    "no more nucleus memory after page free lists alloc");
10600Sstevel@tonic-gate 
10615648Ssetje 	if (ndata_alloc_hat(&ndata, npages) != 0)
10620Sstevel@tonic-gate 		cmn_err(CE_PANIC, "no more nucleus memory after hat alloc");
10630Sstevel@tonic-gate 
10645648Ssetje 	if (ndata_alloc_memseg(&ndata, boot_physavail_len) != 0)
10655648Ssetje 		cmn_err(CE_PANIC, "no more nucleus memory after memseg alloc");
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	/*
10680Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10690Sstevel@tonic-gate 	 *
10700Sstevel@tonic-gate 	 * There are comments all over the SFMMU code warning of dire
10710Sstevel@tonic-gate 	 * consequences if the TSBs are moved out of 32-bit space.  This
10720Sstevel@tonic-gate 	 * is largely because the asm code uses "sethi %hi(addr)"-type
10730Sstevel@tonic-gate 	 * instructions which will not provide the expected result if the
10740Sstevel@tonic-gate 	 * address is a 64-bit one.
10750Sstevel@tonic-gate 	 *
10760Sstevel@tonic-gate 	 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING
10770Sstevel@tonic-gate 	 */
10780Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)nalloc_end, MMU_PAGESIZE);
10795648Ssetje 	PRM_DEBUG(alloc_base);
10805648Ssetje 
10810Sstevel@tonic-gate 	alloc_base = sfmmu_ktsb_alloc(alloc_base);
10820Sstevel@tonic-gate 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
10830Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 * Allocate IOMMU TSB array.  We do this here so that the physical
10870Sstevel@tonic-gate 	 * memory gets deducted from the PROM's physical memory list.
10880Sstevel@tonic-gate 	 */
10890Sstevel@tonic-gate 	alloc_base = iommu_tsb_init(alloc_base);
10905648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
10910Sstevel@tonic-gate 	PRM_DEBUG(alloc_base);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	/*
10945648Ssetje 	 * Allow for an early allocation of physically contiguous memory.
10955648Ssetje 	 */
10965648Ssetje 	alloc_base = contig_mem_prealloc(alloc_base, npages);
10975648Ssetje 
10985648Ssetje 	/*
10991772Sjl139090 	 * Platforms like Starcat and OPL need special structures assigned in
11001772Sjl139090 	 * 32-bit virtual address space because their probing routines execute
11011772Sjl139090 	 * FCode, and FCode can't handle 64-bit virtual addresses...
11020Sstevel@tonic-gate 	 */
11031772Sjl139090 	if (&plat_startup_memlist) {
11041772Sjl139090 		alloc_base = plat_startup_memlist(alloc_base);
11050Sstevel@tonic-gate 		alloc_base = (caddr_t)roundup((uintptr_t)alloc_base,
11060Sstevel@tonic-gate 		    ecache_alignsize);
11070Sstevel@tonic-gate 		PRM_DEBUG(alloc_base);
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/*
11110Sstevel@tonic-gate 	 * Save off where the contiguous allocations to date have ended
11120Sstevel@tonic-gate 	 * in econtig32.
11130Sstevel@tonic-gate 	 */
11140Sstevel@tonic-gate 	econtig32 = alloc_base;
11150Sstevel@tonic-gate 	PRM_DEBUG(econtig32);
11160Sstevel@tonic-gate 	if (econtig32 > (caddr_t)KERNEL_LIMIT32)
11170Sstevel@tonic-gate 		cmn_err(CE_PANIC, "econtig32 too big");
11180Sstevel@tonic-gate 
11195648Ssetje 	pp_sz = calc_pp_sz(npages);
11205648Ssetje 	PRM_DEBUG(pp_sz);
11215648Ssetje 	if (kpm_enable) {
11225648Ssetje 		kpm_pp_sz = calc_kpmpp_sz(npages);
11235648Ssetje 		PRM_DEBUG(kpm_pp_sz);
11245648Ssetje 	}
11255648Ssetje 
11265648Ssetje 	hmehash_sz = calc_hmehash_sz(npages);
11275648Ssetje 	PRM_DEBUG(hmehash_sz);
11285648Ssetje 
11295648Ssetje 	pagehash_sz = calc_pagehash_sz(npages);
11305648Ssetje 	PRM_DEBUG(pagehash_sz);
11315648Ssetje 
11325648Ssetje 	pagelist_sz = calc_free_pagelist_sz();
11335648Ssetje 	PRM_DEBUG(pagelist_sz);
11345648Ssetje 
11355648Ssetje #ifdef	TRAPTRACE
11365648Ssetje 	tt_sz = calc_traptrace_sz();
11375648Ssetje 	PRM_DEBUG(tt_sz);
11385648Ssetje #else
11395648Ssetje 	tt_sz = 0;
11405648Ssetje #endif	/* TRAPTRACE */
11410Sstevel@tonic-gate 
11423764Sdp78419 	/*
11435648Ssetje 	 * Place the array that protects pp->p_selock in the kmem64 wad.
11443764Sdp78419 	 */
11456582Ssetje 	pse_shift = size_pse_array(npages, max_ncpus);
11465648Ssetje 	PRM_DEBUG(pse_shift);
11475648Ssetje 	pse_table_size = 1 << pse_shift;
11485648Ssetje 	PRM_DEBUG(pse_table_size);
11495648Ssetje 	psetable_sz = roundup(
11505648Ssetje 	    pse_table_size * sizeof (pad_mutex_t), ecache_alignsize);
11515648Ssetje 	PRM_DEBUG(psetable_sz);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	/*
11545648Ssetje 	 * Now allocate the whole wad
11550Sstevel@tonic-gate 	 */
11565648Ssetje 	kmem64_sz = pp_sz + kpm_pp_sz + hmehash_sz + pagehash_sz +
11575648Ssetje 	    pagelist_sz + tt_sz + psetable_sz;
11585648Ssetje 	kmem64_sz = roundup(kmem64_sz, PAGESIZE);
11595648Ssetje 	kmem64_base = (caddr_t)syslimit;
11605648Ssetje 	kmem64_end = kmem64_base + kmem64_sz;
11617218Ssvemuri 	if (alloc_kmem64(kmem64_base, kmem64_end)) {
11627218Ssvemuri 		/*
11637218Ssvemuri 		 * Attempt for kmem64 to allocate one big
11647218Ssvemuri 		 * contiguous chunk of memory failed.
11657218Ssvemuri 		 * We get here because we are sun4v.
11667218Ssvemuri 		 * We will proceed by breaking up
11677218Ssvemuri 		 * the allocation into two attempts.
11687218Ssvemuri 		 * First, we allocate kpm_pp_sz, hmehash_sz,
11697218Ssvemuri 		 * pagehash_sz, pagelist_sz, tt_sz & psetable_sz as
11707218Ssvemuri 		 * one contiguous chunk. This is a much smaller
11717218Ssvemuri 		 * chunk and we should get it, if not we panic.
11727218Ssvemuri 		 * Note that hmehash and tt need to be physically
11737218Ssvemuri 		 * (in the real address sense) contiguous.
11747218Ssvemuri 		 * Next, we use bop_alloc_chunk() to
11757218Ssvemuri 		 * to allocate the page_t structures.
11767218Ssvemuri 		 * This will allow the page_t to be allocated
11777218Ssvemuri 		 * in multiple smaller chunks.
11787218Ssvemuri 		 * In doing so, the assumption that page_t is
11797218Ssvemuri 		 * physically contiguous no longer hold, this is ok
11807218Ssvemuri 		 * for sun4v but not for sun4u.
11817218Ssvemuri 		 */
11827218Ssvemuri 		size_t  tmp_size;
11837218Ssvemuri 		caddr_t tmp_base;
11847218Ssvemuri 
11857218Ssvemuri 		pp_sz  = roundup(pp_sz, PAGESIZE);
11867218Ssvemuri 
11877218Ssvemuri 		/*
11887218Ssvemuri 		 * Allocate kpm_pp_sz, hmehash_sz,
11897218Ssvemuri 		 * pagehash_sz, pagelist_sz, tt_sz & psetable_sz
11907218Ssvemuri 		 */
11917218Ssvemuri 		tmp_base = kmem64_base + pp_sz;
11927218Ssvemuri 		tmp_size = roundup(kpm_pp_sz + hmehash_sz + pagehash_sz +
11937218Ssvemuri 		    pagelist_sz + tt_sz + psetable_sz, PAGESIZE);
11947218Ssvemuri 		if (prom_alloc(tmp_base, tmp_size, PAGESIZE) == 0)
11957218Ssvemuri 			prom_panic("kmem64 prom_alloc contig failed");
11967218Ssvemuri 		PRM_DEBUG(tmp_base);
11977218Ssvemuri 		PRM_DEBUG(tmp_size);
11987218Ssvemuri 
11997218Ssvemuri 		/*
12007218Ssvemuri 		 * Allocate the page_ts
12017218Ssvemuri 		 */
12027218Ssvemuri 		if (bop_alloc_chunk(kmem64_base, pp_sz, PAGESIZE) == 0)
12037218Ssvemuri 			prom_panic("kmem64 bop_alloc_chunk page_t failed");
12047218Ssvemuri 		PRM_DEBUG(kmem64_base);
12057218Ssvemuri 		PRM_DEBUG(pp_sz);
12067218Ssvemuri 
12077218Ssvemuri 		kmem64_aligned_end = kmem64_base + pp_sz + tmp_size;
12087218Ssvemuri 		ASSERT(kmem64_aligned_end >= kmem64_end);
12097218Ssvemuri 
12107218Ssvemuri 		kmem64_smchunks = 1;
12117218Ssvemuri 	} else {
12127218Ssvemuri 
12137218Ssvemuri 		/*
12147218Ssvemuri 		 * We need to adjust pp_sz for the normal
12157218Ssvemuri 		 * case where kmem64 can allocate one large chunk
12167218Ssvemuri 		 */
12177218Ssvemuri 		if (kpm_smallpages == 0) {
12187218Ssvemuri 			npages -= kmem64_sz / (PAGESIZE + sizeof (struct page));
12197218Ssvemuri 		} else {
12207218Ssvemuri 			npages -= kmem64_sz / (PAGESIZE + sizeof (struct page) +
12217218Ssvemuri 			    sizeof (kpm_spage_t));
12227218Ssvemuri 		}
12237218Ssvemuri 		pp_sz = npages * sizeof (struct page);
12247218Ssvemuri 	}
12257218Ssvemuri 
12265648Ssetje 	if (kmem64_aligned_end > (hole_start ? hole_start : kpm_vbase))
12275648Ssetje 		cmn_err(CE_PANIC, "not enough kmem64 space");
12285648Ssetje 	PRM_DEBUG(kmem64_base);
12295648Ssetje 	PRM_DEBUG(kmem64_end);
12305648Ssetje 	PRM_DEBUG(kmem64_aligned_end);
12314204Sha137994 
12324204Sha137994 	/*
12335648Ssetje 	 * ... and divy it up
12340Sstevel@tonic-gate 	 */
12355648Ssetje 	alloc_base = kmem64_base;
12365872Ssetje 
12375648Ssetje 	pp_base = (page_t *)alloc_base;
12385648Ssetje 	alloc_base += pp_sz;
12395648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12405648Ssetje 	PRM_DEBUG(pp_base);
12415648Ssetje 	PRM_DEBUG(npages);
12425648Ssetje 
12435648Ssetje 	if (kpm_enable) {
12445648Ssetje 		kpm_pp_base = alloc_base;
12455648Ssetje 		if (kpm_smallpages == 0) {
12465648Ssetje 			/* kpm_npages based on physinstalled, don't reset */
12475648Ssetje 			kpm_pp_sz = kpm_npages * sizeof (kpm_page_t);
12485648Ssetje 		} else {
12495648Ssetje 			kpm_npages = ptokpmpr(npages);
12505648Ssetje 			kpm_pp_sz = kpm_npages * sizeof (kpm_spage_t);
12510Sstevel@tonic-gate 		}
12525648Ssetje 		alloc_base += kpm_pp_sz;
12535648Ssetje 		alloc_base =
12545648Ssetje 		    (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12555648Ssetje 		PRM_DEBUG(kpm_pp_base);
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate 
12585648Ssetje 	alloc_base = alloc_hmehash(alloc_base);
12595648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12605648Ssetje 	PRM_DEBUG(alloc_base);
12615648Ssetje 
12625648Ssetje 	page_hash = (page_t **)alloc_base;
12635648Ssetje 	alloc_base += pagehash_sz;
12645648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12655648Ssetje 	PRM_DEBUG(page_hash);
12665648Ssetje 
12675648Ssetje 	alloc_base = alloc_page_freelists(alloc_base);
12685648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12695648Ssetje 	PRM_DEBUG(alloc_base);
12705648Ssetje 
12715648Ssetje #ifdef	TRAPTRACE
12725648Ssetje 	ttrace_buf = alloc_base;
12735648Ssetje 	alloc_base += tt_sz;
12745648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12755648Ssetje 	PRM_DEBUG(alloc_base);
12765648Ssetje #endif	/* TRAPTRACE */
12775648Ssetje 
12785648Ssetje 	pse_mutex = (pad_mutex_t *)alloc_base;
12795648Ssetje 	alloc_base += psetable_sz;
12805648Ssetje 	alloc_base = (caddr_t)roundup((uintptr_t)alloc_base, ecache_alignsize);
12815648Ssetje 	PRM_DEBUG(alloc_base);
12825648Ssetje 
12837218Ssvemuri 	/*
12847218Ssvemuri 	 * Note that if we use small chunk allocations for
12857218Ssvemuri 	 * kmem64, we need to ensure kmem64_end is the same as
12867218Ssvemuri 	 * kmem64_aligned_end to prevent subsequent logic from
12877218Ssvemuri 	 * trying to reuse the overmapping.
12887218Ssvemuri 	 * Otherwise we adjust kmem64_end to what we really allocated.
12897218Ssvemuri 	 */
12907218Ssvemuri 	if (kmem64_smchunks) {
12917218Ssvemuri 		kmem64_end = kmem64_aligned_end;
12927218Ssvemuri 	} else {
12937218Ssvemuri 		kmem64_end = (caddr_t)roundup((uintptr_t)alloc_base, PAGESIZE);
12947218Ssvemuri 	}
12955648Ssetje 	kmem64_sz = kmem64_end - kmem64_base;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	if (&ecache_init_scrub_flush_area) {
12985648Ssetje 		alloc_base = ecache_init_scrub_flush_area(kmem64_aligned_end);
12995648Ssetje 		ASSERT(alloc_base <= (hole_start ? hole_start : kpm_vbase));
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/*
13030Sstevel@tonic-gate 	 * If physmem is patched to be non-zero, use it instead of
13040Sstevel@tonic-gate 	 * the monitor value unless physmem is larger than the total
13050Sstevel@tonic-gate 	 * amount of memory on hand.
13060Sstevel@tonic-gate 	 */
13070Sstevel@tonic-gate 	if (physmem == 0 || physmem > npages)
13080Sstevel@tonic-gate 		physmem = npages;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	/*
13115648Ssetje 	 * root_is_ramdisk is set via /etc/system when the ramdisk miniroot
13125648Ssetje 	 * is mounted as root. This memory is held down by OBP and unlike
13135648Ssetje 	 * the stub boot_archive is never released.
13140Sstevel@tonic-gate 	 *
13155648Ssetje 	 * In order to get things sized correctly on lower memory
13165648Ssetje 	 * machines (where the memory used by the ramdisk represents
13175648Ssetje 	 * a significant portion of memory), physmem is adjusted.
13185648Ssetje 	 *
13195648Ssetje 	 * This is done by subtracting the ramdisk_size which is set
13205648Ssetje 	 * to the size of the ramdisk (in Kb) in /etc/system at the
13215648Ssetje 	 * time the miniroot archive is constructed.
13220Sstevel@tonic-gate 	 */
13235872Ssetje 	if (root_is_ramdisk == B_TRUE) {
13245872Ssetje 		ramdisk_npages = (ramdisk_size * 1024) / PAGESIZE;
13255872Ssetje 		physmem -= ramdisk_npages;
13265872Ssetje 	}
13275648Ssetje 
13285648Ssetje 	if (kpm_enable && (ndata_alloc_kpm(&ndata, kpm_npages) != 0))
13295648Ssetje 		cmn_err(CE_PANIC, "no more nucleus memory after kpm alloc");
13300Sstevel@tonic-gate 
13314878Sblakej 	/*
13325648Ssetje 	 * Allocate space for the interrupt vector table.
13333764Sdp78419 	 */
13345648Ssetje 	memspace = prom_alloc((caddr_t)intr_vec_table, IVSIZE, MMU_PAGESIZE);
13352973Sgovinda 	if (memspace != (caddr_t)intr_vec_table)
13363764Sdp78419 		prom_panic("interrupt vector table allocation failure");
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	/*
13390Sstevel@tonic-gate 	 * Between now and when we finish copying in the memory lists,
13400Sstevel@tonic-gate 	 * allocations happen so the space gets fragmented and the
13415648Ssetje 	 * lists longer.  Leave enough space for lists twice as
13425648Ssetje 	 * long as we have now; then roundup to a pagesize.
13430Sstevel@tonic-gate 	 */
13445648Ssetje 	memlist_sz = sizeof (struct memlist) * (prom_phys_installed_len() +
13455648Ssetje 	    prom_phys_avail_len() + prom_virt_avail_len());
13465648Ssetje 	memlist_sz *= 2;
13475648Ssetje 	memlist_sz = roundup(memlist_sz, PAGESIZE);
13485648Ssetje 	memspace = ndata_alloc(&ndata, memlist_sz, ecache_alignsize);
13490Sstevel@tonic-gate 	if (memspace == NULL)
13505648Ssetje 		cmn_err(CE_PANIC, "no more nucleus memory after memlist alloc");
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	memlist = (struct memlist *)memspace;
13530Sstevel@tonic-gate 	memlist_end = (char *)memspace + memlist_sz;
13540Sstevel@tonic-gate 	PRM_DEBUG(memlist);
13550Sstevel@tonic-gate 	PRM_DEBUG(memlist_end);
13565648Ssetje 
13570Sstevel@tonic-gate 	PRM_DEBUG(sysbase);
13580Sstevel@tonic-gate 	PRM_DEBUG(syslimit);
13590Sstevel@tonic-gate 	kernelheap_init((void *)sysbase, (void *)syslimit,
13600Sstevel@tonic-gate 	    (caddr_t)sysbase + PAGESIZE, NULL, NULL);
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	/*
13630Sstevel@tonic-gate 	 * Take the most current snapshot we can by calling mem-update.
13640Sstevel@tonic-gate 	 */
13650Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
13660Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
13670Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	/*
13705648Ssetje 	 * Remove the space used by prom_alloc from the kernel heap
13710Sstevel@tonic-gate 	 * plus the area actually used by the OBP (if any)
13720Sstevel@tonic-gate 	 * ignoring virtual addresses in virt_avail, above syslimit.
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	virt_avail = memlist;
13750Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	for (cur = virt_avail; cur->next; cur = cur->next) {
13780Sstevel@tonic-gate 		uint64_t range_base, range_size;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		if ((range_base = cur->address + cur->size) < (uint64_t)sysbase)
13810Sstevel@tonic-gate 			continue;
13820Sstevel@tonic-gate 		if (range_base >= (uint64_t)syslimit)
13830Sstevel@tonic-gate 			break;
13840Sstevel@tonic-gate 		/*
13850Sstevel@tonic-gate 		 * Limit the range to end at syslimit.
13860Sstevel@tonic-gate 		 */
13870Sstevel@tonic-gate 		range_size = MIN(cur->next->address,
13880Sstevel@tonic-gate 		    (uint64_t)syslimit) - range_base;
13890Sstevel@tonic-gate 		(void) vmem_xalloc(heap_arena, (size_t)range_size, PAGESIZE,
13900Sstevel@tonic-gate 		    0, 0, (void *)range_base, (void *)(range_base + range_size),
13910Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	phys_avail = memlist;
13955648Ssetje 	copy_memlist(boot_physavail, boot_physavail_len, &memlist);
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/*
13984104Sblakej 	 * Add any extra memory at the end of the ndata region if there's at
13994104Sblakej 	 * least a page to add.  There might be a few more pages available in
14004104Sblakej 	 * the middle of the ndata region, but for now they are ignored.
14010Sstevel@tonic-gate 	 */
14024104Sblakej 	nalloc_base = ndata_extra_base(&ndata, MMU_PAGESIZE, nalloc_end);
14034104Sblakej 	if (nalloc_base == NULL)
14040Sstevel@tonic-gate 		nalloc_base = nalloc_end;
14050Sstevel@tonic-gate 	ndata_remain_sz = nalloc_end - nalloc_base;
14060Sstevel@tonic-gate 
14075648Ssetje 	/*
14085648Ssetje 	 * Copy physinstalled list into kernel space.
14095648Ssetje 	 */
14105648Ssetje 	phys_install = memlist;
14115648Ssetje 	copy_memlist(boot_physinstalled, boot_physinstalled_len, &memlist);
14125648Ssetje 
14135648Ssetje 	/*
14145648Ssetje 	 * Create list of physical addrs we don't need pp's for:
14155648Ssetje 	 * kernel text 4M page
14165648Ssetje 	 * kernel data 4M page - ndata_remain_sz
14175648Ssetje 	 * kmem64 pages
14185648Ssetje 	 *
14195648Ssetje 	 * NB if adding any pages here, make sure no kpm page
14205648Ssetje 	 * overlaps can occur (see ASSERTs in kphysm_memsegs)
14215648Ssetje 	 */
14225648Ssetje 	nopp_list = memlist;
14235648Ssetje 	memlist_new(va_to_pa(s_text), MMU_PAGESIZE4M, &memlist);
14245648Ssetje 	memlist_add(va_to_pa(s_data), MMU_PAGESIZE4M - ndata_remain_sz,
14255648Ssetje 	    &memlist, &nopp_list);
14267218Ssvemuri 
14277218Ssvemuri 	/* Don't add to nopp_list if kmem64 was allocated in smchunks */
14287218Ssvemuri 	if (!kmem64_smchunks)
14297218Ssvemuri 		memlist_add(kmem64_pabase, kmem64_sz, &memlist, &nopp_list);
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	if ((caddr_t)memlist > (memspace + memlist_sz))
14323764Sdp78419 		prom_panic("memlist overflow");
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	/*
14356880Sdv142724 	 * Size the pcf array based on the number of cpus in the box at
14366880Sdv142724 	 * boot time.
14376880Sdv142724 	 */
14386880Sdv142724 	pcf_init();
14396880Sdv142724 
14406880Sdv142724 	/*
14410Sstevel@tonic-gate 	 * Initialize the page structures from the memory lists.
14420Sstevel@tonic-gate 	 */
14435648Ssetje 	kphysm_init();
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 	availrmem_initial = availrmem = freemem;
14460Sstevel@tonic-gate 	PRM_DEBUG(availrmem);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * Some of the locks depend on page_hashsz being set!
14500Sstevel@tonic-gate 	 * kmem_init() depends on this; so, keep it here.
14510Sstevel@tonic-gate 	 */
14520Sstevel@tonic-gate 	page_lock_init();
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * Initialize kernel memory allocator.
14560Sstevel@tonic-gate 	 */
14570Sstevel@tonic-gate 	kmem_init();
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	/*
14603717Sdp78419 	 * Factor in colorequiv to check additional 'equivalent' bins
14613717Sdp78419 	 */
14623733Sdp78419 	if (&page_set_colorequiv_arr_cpu != NULL)
14633733Sdp78419 		page_set_colorequiv_arr_cpu();
14643733Sdp78419 	else
14653733Sdp78419 		page_set_colorequiv_arr();
14663717Sdp78419 
14673717Sdp78419 	/*
14680Sstevel@tonic-gate 	 * Initialize bp_mapin().
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	bp_init(shm_alignment, HAT_STRICTORDER);
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	/*
14735358Sjc25722 	 * Reserve space for panicbuf, intr_vec_table, reserved interrupt
14745358Sjc25722 	 * vector data structures and MPO mblock structs from the 32-bit heap.
14750Sstevel@tonic-gate 	 */
14760Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, PANICBUFSIZE, PAGESIZE, 0, 0,
14770Sstevel@tonic-gate 	    panicbuf, panicbuf + PANICBUFSIZE,
14780Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	(void) vmem_xalloc(heap32_arena, IVSIZE, PAGESIZE, 0, 0,
14812973Sgovinda 	    intr_vec_table, (caddr_t)intr_vec_table + IVSIZE,
14820Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
14830Sstevel@tonic-gate 
14845358Sjc25722 	if (mpo_heap32_bufsz > (size_t)0) {
14855358Sjc25722 		(void) vmem_xalloc(heap32_arena, mpo_heap32_bufsz,
14865358Sjc25722 		    PAGESIZE, 0, 0, mpo_heap32_buf,
14875358Sjc25722 		    mpo_heap32_buf + mpo_heap32_bufsz,
14885358Sjc25722 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
14895358Sjc25722 	}
14900Sstevel@tonic-gate 	mem_config_init();
14910Sstevel@tonic-gate }
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate static void
14940Sstevel@tonic-gate startup_modules(void)
14950Sstevel@tonic-gate {
14965648Ssetje 	int nhblk1, nhblk8;
14970Sstevel@tonic-gate 	size_t  nhblksz;
14983764Sdp78419 	pgcnt_t pages_per_hblk;
14990Sstevel@tonic-gate 	size_t hme8blk_sz, hme1blk_sz;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * Let the platforms have a chance to change default
15030Sstevel@tonic-gate 	 * values before reading system file.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	if (&set_platform_defaults)
15060Sstevel@tonic-gate 		set_platform_defaults();
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/*
15090Sstevel@tonic-gate 	 * Calculate default settings of system parameters based upon
15100Sstevel@tonic-gate 	 * maxusers, yet allow to be overridden via the /etc/system file.
15110Sstevel@tonic-gate 	 */
15120Sstevel@tonic-gate 	param_calc(0);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	mod_setup();
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/*
15170Sstevel@tonic-gate 	 * If this is a positron, complain and halt.
15180Sstevel@tonic-gate 	 */
15190Sstevel@tonic-gate 	if (&iam_positron && iam_positron()) {
15200Sstevel@tonic-gate 		cmn_err(CE_WARN, "This hardware platform is not supported"
15210Sstevel@tonic-gate 		    " by this release of Solaris.\n");
15220Sstevel@tonic-gate #ifdef DEBUG
15230Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to resume */
15240Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting an unsupported platform.\n");
15250Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware.\n");
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate #else /* DEBUG */
15280Sstevel@tonic-gate 		halt(0);
15290Sstevel@tonic-gate #endif /* DEBUG */
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * If we are running firmware that isn't 64-bit ready
15340Sstevel@tonic-gate 	 * then complain and halt.
15350Sstevel@tonic-gate 	 */
15360Sstevel@tonic-gate 	do_prom_version_check();
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/*
15390Sstevel@tonic-gate 	 * Initialize system parameters
15400Sstevel@tonic-gate 	 */
15410Sstevel@tonic-gate 	param_init();
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	/*
15440Sstevel@tonic-gate 	 * maxmem is the amount of physical memory we're playing with.
15450Sstevel@tonic-gate 	 */
15460Sstevel@tonic-gate 	maxmem = physmem;
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	/* Set segkp limits. */
15493446Smrj 	ncbase = kdi_segdebugbase;
15503446Smrj 	ncend = kdi_segdebugbase;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	/*
15530Sstevel@tonic-gate 	 * Initialize the hat layer.
15540Sstevel@tonic-gate 	 */
15550Sstevel@tonic-gate 	hat_init();
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	/*
15580Sstevel@tonic-gate 	 * Initialize segment management stuff.
15590Sstevel@tonic-gate 	 */
15600Sstevel@tonic-gate 	seg_init();
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	/*
15630Sstevel@tonic-gate 	 * Create the va>tte handler, so the prom can understand
15640Sstevel@tonic-gate 	 * kernel translations.  The handler is installed later, just
15650Sstevel@tonic-gate 	 * as we are about to take over the trap table from the prom.
15660Sstevel@tonic-gate 	 */
15670Sstevel@tonic-gate 	create_va_to_tte();
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	/*
15700Sstevel@tonic-gate 	 * Load the forthdebugger (optional)
15710Sstevel@tonic-gate 	 */
15720Sstevel@tonic-gate 	forthdebug_init();
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/*
15750Sstevel@tonic-gate 	 * Create OBP node for console input callbacks
15760Sstevel@tonic-gate 	 * if it is needed.
15770Sstevel@tonic-gate 	 */
15781253Slq150181 	startup_create_io_node();
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	if (modloadonly("fs", "specfs") == -1)
15810Sstevel@tonic-gate 		halt("Can't load specfs");
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	if (modloadonly("fs", "devfs") == -1)
15840Sstevel@tonic-gate 		halt("Can't load devfs");
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	if (modloadonly("misc", "swapgeneric") == -1)
15870Sstevel@tonic-gate 		halt("Can't load swapgeneric");
15880Sstevel@tonic-gate 
15891676Sjpk 	(void) modloadonly("sys", "lbl_edition");
15901676Sjpk 
15910Sstevel@tonic-gate 	dispinit();
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	/*
15940Sstevel@tonic-gate 	 * Infer meanings to the members of the idprom buffer.
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	parse_idprom();
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	/* Read cluster configuration data. */
15990Sstevel@tonic-gate 	clconf_init();
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	setup_ddi();
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	/*
16040Sstevel@tonic-gate 	 * Lets take this opportunity to load the root device.
16050Sstevel@tonic-gate 	 */
16060Sstevel@tonic-gate 	if (loadrootmodules() != 0)
16070Sstevel@tonic-gate 		debug_enter("Can't load the root filesystem");
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	/*
16100Sstevel@tonic-gate 	 * Load tod driver module for the tod part found on this system.
16110Sstevel@tonic-gate 	 * Recompute the cpu frequency/delays based on tod as tod part
16120Sstevel@tonic-gate 	 * tends to keep time more accurately.
16130Sstevel@tonic-gate 	 */
16140Sstevel@tonic-gate 	if (&load_tod_module)
16150Sstevel@tonic-gate 		load_tod_module();
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	/*
16180Sstevel@tonic-gate 	 * Allow platforms to load modules which might
16190Sstevel@tonic-gate 	 * be needed after bootops are gone.
16200Sstevel@tonic-gate 	 */
16210Sstevel@tonic-gate 	if (&load_platform_modules)
16220Sstevel@tonic-gate 		load_platform_modules();
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	setcpudelay();
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
16270Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
16280Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	/*
16310Sstevel@tonic-gate 	 * Calculation and allocation of hmeblks needed to remap
16323764Sdp78419 	 * the memory allocated by PROM till now.
16333764Sdp78419 	 * Overestimate the number of hblk1 elements by assuming
16343764Sdp78419 	 * worst case of TTE64K mappings.
16353764Sdp78419 	 * sfmmu_hblk_alloc will panic if this calculation is wrong.
16360Sstevel@tonic-gate 	 */
16373764Sdp78419 	bop_alloc_pages = btopr(kmem64_end - kmem64_base);
16383764Sdp78419 	pages_per_hblk = btop(HMEBLK_SPAN(TTE64K));
16393764Sdp78419 	bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
16403764Sdp78419 	nhblk1 = bop_alloc_pages / pages_per_hblk + hblk1_min;
16413764Sdp78419 
16423764Sdp78419 	bop_alloc_pages = size_virtalloc(boot_virtavail, boot_virtavail_len);
16433764Sdp78419 
16443764Sdp78419 	/* sfmmu_init_nucleus_hblks expects properly aligned data structures */
16450Sstevel@tonic-gate 	hme8blk_sz = roundup(HME8BLK_SZ, sizeof (int64_t));
16460Sstevel@tonic-gate 	hme1blk_sz = roundup(HME1BLK_SZ, sizeof (int64_t));
16470Sstevel@tonic-gate 
16483764Sdp78419 	bop_alloc_pages += btopr(nhblk1 * hme1blk_sz);
16493764Sdp78419 
16500Sstevel@tonic-gate 	pages_per_hblk = btop(HMEBLK_SPAN(TTE8K));
16513764Sdp78419 	nhblk8 = 0;
16523764Sdp78419 	while (bop_alloc_pages > 1) {
16533764Sdp78419 		bop_alloc_pages = roundup(bop_alloc_pages, pages_per_hblk);
16543764Sdp78419 		nhblk8 += bop_alloc_pages /= pages_per_hblk;
16553764Sdp78419 		bop_alloc_pages *= hme8blk_sz;
16563764Sdp78419 		bop_alloc_pages = btopr(bop_alloc_pages);
16573764Sdp78419 	}
16583764Sdp78419 	nhblk8 += 2;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	/*
16610Sstevel@tonic-gate 	 * Since hblk8's can hold up to 64k of mappings aligned on a 64k
16620Sstevel@tonic-gate 	 * boundary, the number of hblk8's needed to map the entries in the
16630Sstevel@tonic-gate 	 * boot_virtavail list needs to be adjusted to take this into
16640Sstevel@tonic-gate 	 * consideration.  Thus, we need to add additional hblk8's since it
16650Sstevel@tonic-gate 	 * is possible that an hblk8 will not have all 8 slots used due to
16660Sstevel@tonic-gate 	 * alignment constraints.  Since there were boot_virtavail_len entries
16670Sstevel@tonic-gate 	 * in that list, we need to add that many hblk8's to the number
16680Sstevel@tonic-gate 	 * already calculated to make sure we don't underestimate.
16690Sstevel@tonic-gate 	 */
16700Sstevel@tonic-gate 	nhblk8 += boot_virtavail_len;
16710Sstevel@tonic-gate 	nhblksz = nhblk8 * hme8blk_sz + nhblk1 * hme1blk_sz;
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/* Allocate in pagesize chunks */
16740Sstevel@tonic-gate 	nhblksz = roundup(nhblksz, MMU_PAGESIZE);
16750Sstevel@tonic-gate 	hblk_base = kmem_zalloc(nhblksz, KM_SLEEP);
16760Sstevel@tonic-gate 	sfmmu_init_nucleus_hblks(hblk_base, nhblksz, nhblk8, nhblk1);
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate static void
16800Sstevel@tonic-gate startup_bop_gone(void)
16810Sstevel@tonic-gate {
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	/*
16841991Sheppo 	 * Destroy the MD initialized at startup
16851991Sheppo 	 * The startup initializes the MD framework
16861991Sheppo 	 * using prom and BOP alloc free it now.
16871991Sheppo 	 */
16881991Sheppo 	mach_descrip_startup_fini();
16891991Sheppo 
16901991Sheppo 	/*
16915648Ssetje 	 * We're done with prom allocations.
16920Sstevel@tonic-gate 	 */
16935648Ssetje 	bop_fini();
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
16960Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
16970Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	/*
17000Sstevel@tonic-gate 	 * setup physically contiguous area twice as large as the ecache.
17010Sstevel@tonic-gate 	 * this is used while doing displacement flush of ecaches
17020Sstevel@tonic-gate 	 */
17030Sstevel@tonic-gate 	if (&ecache_flush_address) {
17040Sstevel@tonic-gate 		ecache_flushaddr = ecache_flush_address();
17050Sstevel@tonic-gate 		if (ecache_flushaddr == (uint64_t)-1) {
17060Sstevel@tonic-gate 			cmn_err(CE_PANIC,
17070Sstevel@tonic-gate 			    "startup: no memory to set ecache_flushaddr");
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 	}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	/*
17120Sstevel@tonic-gate 	 * Virtual available next.
17130Sstevel@tonic-gate 	 */
17140Sstevel@tonic-gate 	ASSERT(virt_avail != NULL);
17150Sstevel@tonic-gate 	memlist_free_list(virt_avail);
17160Sstevel@tonic-gate 	virt_avail = memlist;
17170Sstevel@tonic-gate 	copy_memlist(boot_virtavail, boot_virtavail_len, &memlist);
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate /*
17230Sstevel@tonic-gate  * startup_fixup_physavail - called from mach_sfmmu.c after the final
17240Sstevel@tonic-gate  * allocations have been performed.  We can't call it in startup_bop_gone
17250Sstevel@tonic-gate  * since later operations can cause obp to allocate more memory.
17260Sstevel@tonic-gate  */
17270Sstevel@tonic-gate void
17280Sstevel@tonic-gate startup_fixup_physavail(void)
17290Sstevel@tonic-gate {
17300Sstevel@tonic-gate 	struct memlist *cur;
17313764Sdp78419 	size_t kmem64_overmap_size = kmem64_aligned_end - kmem64_end;
17323764Sdp78419 
17333764Sdp78419 	PRM_DEBUG(kmem64_overmap_size);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	/*
17360Sstevel@tonic-gate 	 * take the most current snapshot we can by calling mem-update
17370Sstevel@tonic-gate 	 */
17380Sstevel@tonic-gate 	copy_boot_memlists(&boot_physinstalled, &boot_physinstalled_len,
17390Sstevel@tonic-gate 	    &boot_physavail, &boot_physavail_len,
17400Sstevel@tonic-gate 	    &boot_virtavail, &boot_virtavail_len);
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	/*
17430Sstevel@tonic-gate 	 * Copy phys_avail list, again.
17440Sstevel@tonic-gate 	 * Both the kernel/boot and the prom have been allocating
17450Sstevel@tonic-gate 	 * from the original list we copied earlier.
17460Sstevel@tonic-gate 	 */
17470Sstevel@tonic-gate 	cur = memlist;
17485648Ssetje 	copy_memlist(boot_physavail, boot_physavail_len, &memlist);
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	/*
17513764Sdp78419 	 * Add any unused kmem64 memory from overmapped page
17523764Sdp78419 	 * (Note: va_to_pa does not work for kmem64_end)
17533764Sdp78419 	 */
17543764Sdp78419 	if (kmem64_overmap_size) {
17553764Sdp78419 		memlist_add(kmem64_pabase + (kmem64_end - kmem64_base),
17565648Ssetje 		    kmem64_overmap_size, &memlist, &cur);
17573764Sdp78419 	}
17583764Sdp78419 
17593764Sdp78419 	/*
17605648Ssetje 	 * Add any extra memory after e_data we added to the phys_avail list
17610Sstevel@tonic-gate 	 * back to the old list.
17620Sstevel@tonic-gate 	 */
17630Sstevel@tonic-gate 	if (ndata_remain_sz >= MMU_PAGESIZE)
17640Sstevel@tonic-gate 		memlist_add(va_to_pa(nalloc_base),
17650Sstevel@tonic-gate 		    (uint64_t)ndata_remain_sz, &memlist, &cur);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	/*
17680Sstevel@tonic-gate 	 * There isn't any bounds checking on the memlist area
17690Sstevel@tonic-gate 	 * so ensure it hasn't overgrown.
17700Sstevel@tonic-gate 	 */
17710Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
17720Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: memlist size exceeded");
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	/*
17750Sstevel@tonic-gate 	 * The kernel removes the pages that were allocated for it from
17760Sstevel@tonic-gate 	 * the freelist, but we now have to find any -extra- pages that
17770Sstevel@tonic-gate 	 * the prom has allocated for it's own book-keeping, and remove
17780Sstevel@tonic-gate 	 * them from the freelist too. sigh.
17790Sstevel@tonic-gate 	 */
17805648Ssetje 	sync_memlists(phys_avail, cur);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	ASSERT(phys_avail != NULL);
17835872Ssetje 
17845872Ssetje 	old_phys_avail = phys_avail;
17850Sstevel@tonic-gate 	phys_avail = cur;
17865872Ssetje }
17875872Ssetje 
17885872Ssetje void
17895872Ssetje update_kcage_ranges(uint64_t addr, uint64_t len)
17905872Ssetje {
17915872Ssetje 	pfn_t base = btop(addr);
17925872Ssetje 	pgcnt_t num = btop(len);
17935872Ssetje 	int rv;
17945872Ssetje 
17955872Ssetje 	rv = kcage_range_add(base, num, kcage_startup_dir);
17965872Ssetje 
17975872Ssetje 	if (rv == ENOMEM) {
17985872Ssetje 		cmn_err(CE_WARN, "%ld megabytes not available to kernel cage",
17995872Ssetje 		    (len == 0 ? 0 : BYTES_TO_MB(len)));
18005872Ssetje 	} else if (rv != 0) {
18015872Ssetje 		/* catch this in debug kernels */
18025872Ssetje 		ASSERT(0);
18035872Ssetje 
18045872Ssetje 		cmn_err(CE_WARN, "unexpected kcage_range_add"
18055872Ssetje 		    " return value %d", rv);
18065872Ssetje 	}
18070Sstevel@tonic-gate }
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate static void
18100Sstevel@tonic-gate startup_vm(void)
18110Sstevel@tonic-gate {
18120Sstevel@tonic-gate 	size_t	i;
18130Sstevel@tonic-gate 	struct segmap_crargs a;
18140Sstevel@tonic-gate 	struct segkpm_crargs b;
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	uint64_t avmem;
18170Sstevel@tonic-gate 	caddr_t va;
18180Sstevel@tonic-gate 	pgcnt_t	max_phys_segkp;
18190Sstevel@tonic-gate 	int	mnode;
18200Sstevel@tonic-gate 
18212991Ssusans 	extern int use_brk_lpg, use_stk_lpg;
1822423Sdavemq 
18230Sstevel@tonic-gate 	/*
18240Sstevel@tonic-gate 	 * get prom's mappings, create hments for them and switch
18250Sstevel@tonic-gate 	 * to the kernel context.
18260Sstevel@tonic-gate 	 */
18270Sstevel@tonic-gate 	hat_kern_setup();
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	/*
18300Sstevel@tonic-gate 	 * Take over trap table
18310Sstevel@tonic-gate 	 */
18320Sstevel@tonic-gate 	setup_trap_table();
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 	/*
18350Sstevel@tonic-gate 	 * Install the va>tte handler, so that the prom can handle
18360Sstevel@tonic-gate 	 * misses and understand the kernel table layout in case
18370Sstevel@tonic-gate 	 * we need call into the prom.
18380Sstevel@tonic-gate 	 */
18390Sstevel@tonic-gate 	install_va_to_tte();
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	/*
18420Sstevel@tonic-gate 	 * Set a flag to indicate that the tba has been taken over.
18430Sstevel@tonic-gate 	 */
18440Sstevel@tonic-gate 	tba_taken_over = 1;
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	/* initialize MMU primary context register */
18470Sstevel@tonic-gate 	mmu_init_kcontext();
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	/*
18500Sstevel@tonic-gate 	 * The boot cpu can now take interrupts, x-calls, x-traps
18510Sstevel@tonic-gate 	 */
18520Sstevel@tonic-gate 	CPUSET_ADD(cpu_ready_set, CPU->cpu_id);
18530Sstevel@tonic-gate 	CPU->cpu_flags |= (CPU_READY | CPU_ENABLE | CPU_EXISTS);
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * Set a flag to tell write_scb_int() that it can access V_TBR_WR_ADDR.
18570Sstevel@tonic-gate 	 */
18580Sstevel@tonic-gate 	tbr_wr_addr_inited = 1;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	/*
18610Sstevel@tonic-gate 	 * Initialize VM system, and map kernel address space.
18620Sstevel@tonic-gate 	 */
18630Sstevel@tonic-gate 	kvm_init();
18640Sstevel@tonic-gate 
18655872Ssetje 	ASSERT(old_phys_avail != NULL && phys_avail != NULL);
18665872Ssetje 	if (kernel_cage_enable) {
18675872Ssetje 		diff_memlists(phys_avail, old_phys_avail, update_kcage_ranges);
18685872Ssetje 	}
18695872Ssetje 	memlist_free_list(old_phys_avail);
18705872Ssetje 
18710Sstevel@tonic-gate 	/*
18720Sstevel@tonic-gate 	 * If the following is true, someone has patched
18730Sstevel@tonic-gate 	 * phsymem to be less than the number of pages that
18740Sstevel@tonic-gate 	 * the system actually has.  Remove pages until system
18750Sstevel@tonic-gate 	 * memory is limited to the requested amount.  Since we
18760Sstevel@tonic-gate 	 * have allocated page structures for all pages, we
18770Sstevel@tonic-gate 	 * correct the amount of memory we want to remove
18780Sstevel@tonic-gate 	 * by the size of the memory used to hold page structures
18790Sstevel@tonic-gate 	 * for the non-used pages.
18800Sstevel@tonic-gate 	 */
18815872Ssetje 	if (physmem + ramdisk_npages < npages) {
18820Sstevel@tonic-gate 		pgcnt_t diff, off;
18830Sstevel@tonic-gate 		struct page *pp;
18840Sstevel@tonic-gate 		struct seg kseg;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		cmn_err(CE_WARN, "limiting physmem to %ld pages", physmem);
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 		off = 0;
18895872Ssetje 		diff = npages - (physmem + ramdisk_npages);
18900Sstevel@tonic-gate 		diff -= mmu_btopr(diff * sizeof (struct page));
18910Sstevel@tonic-gate 		kseg.s_as = &kas;
18920Sstevel@tonic-gate 		while (diff--) {
18930Sstevel@tonic-gate 			pp = page_create_va(&unused_pages_vp, (offset_t)off,
18940Sstevel@tonic-gate 			    MMU_PAGESIZE, PG_WAIT | PG_EXCL,
18950Sstevel@tonic-gate 			    &kseg, (caddr_t)off);
18960Sstevel@tonic-gate 			if (pp == NULL)
18970Sstevel@tonic-gate 				cmn_err(CE_PANIC, "limited physmem too much!");
18980Sstevel@tonic-gate 			page_io_unlock(pp);
18990Sstevel@tonic-gate 			page_downgrade(pp);
19000Sstevel@tonic-gate 			availrmem--;
19010Sstevel@tonic-gate 			off += MMU_PAGESIZE;
19020Sstevel@tonic-gate 		}
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	/*
19060Sstevel@tonic-gate 	 * When printing memory, show the total as physmem less
19070Sstevel@tonic-gate 	 * that stolen by a debugger.
19080Sstevel@tonic-gate 	 */
19090Sstevel@tonic-gate 	cmn_err(CE_CONT, "?mem = %ldK (0x%lx000)\n",
19100Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 10),
19110Sstevel@tonic-gate 	    (ulong_t)(physinstalled) << (PAGESHIFT - 12));
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	avmem = (uint64_t)freemem << PAGESHIFT;
19140Sstevel@tonic-gate 	cmn_err(CE_CONT, "?avail mem = %lld\n", (unsigned long long)avmem);
19150Sstevel@tonic-gate 
19162991Ssusans 	/*
19172991Ssusans 	 * For small memory systems disable automatic large pages.
19182991Ssusans 	 */
19192991Ssusans 	if (physmem < privm_lpg_min_physmem) {
1920423Sdavemq 		use_brk_lpg = 0;
1921423Sdavemq 		use_stk_lpg = 0;
1922423Sdavemq 	}
1923423Sdavemq 
19240Sstevel@tonic-gate 	/*
19250Sstevel@tonic-gate 	 * Perform platform specific freelist processing
19260Sstevel@tonic-gate 	 */
19270Sstevel@tonic-gate 	if (&plat_freelist_process) {
19280Sstevel@tonic-gate 		for (mnode = 0; mnode < max_mem_nodes; mnode++)
19290Sstevel@tonic-gate 			if (mem_node_config[mnode].exists)
19300Sstevel@tonic-gate 				plat_freelist_process(mnode);
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	/*
19340Sstevel@tonic-gate 	 * Initialize the segkp segment type.  We position it
19350Sstevel@tonic-gate 	 * after the configured tables and buffers (whose end
19360Sstevel@tonic-gate 	 * is given by econtig) and before V_WKBASE_ADDR.
19370Sstevel@tonic-gate 	 * Also in this area is segkmap (size SEGMAPSIZE).
19380Sstevel@tonic-gate 	 */
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	/* XXX - cache alignment? */
19410Sstevel@tonic-gate 	va = (caddr_t)SEGKPBASE;
19420Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & PAGEOFFSET) == 0);
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	max_phys_segkp = (physmem * 2);
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	if (segkpsize < btop(SEGKPMINSIZE) || segkpsize > btop(SEGKPMAXSIZE)) {
19470Sstevel@tonic-gate 		segkpsize = btop(SEGKPDEFSIZE);
19480Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal value for segkpsize. "
19490Sstevel@tonic-gate 		    "segkpsize has been reset to %ld pages", segkpsize);
19500Sstevel@tonic-gate 	}
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	i = ptob(MIN(segkpsize, max_phys_segkp));
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
19550Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkp) < 0)
19560Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: cannot attach segkp");
19570Sstevel@tonic-gate 	if (segkp_create(segkp) != 0)
19580Sstevel@tonic-gate 		cmn_err(CE_PANIC, "startup: segkp_create failed");
19590Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	/*
19620Sstevel@tonic-gate 	 * kpm segment
19630Sstevel@tonic-gate 	 */
19640Sstevel@tonic-gate 	segmap_kpm = kpm_enable &&
19654528Spaulsan 	    segmap_kpm && PAGESIZE == MAXBSIZE;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	if (kpm_enable) {
19680Sstevel@tonic-gate 		rw_enter(&kas.a_lock, RW_WRITER);
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 		/*
19710Sstevel@tonic-gate 		 * The segkpm virtual range range is larger than the
19720Sstevel@tonic-gate 		 * actual physical memory size and also covers gaps in
19730Sstevel@tonic-gate 		 * the physical address range for the following reasons:
19740Sstevel@tonic-gate 		 * . keep conversion between segkpm and physical addresses
19750Sstevel@tonic-gate 		 *   simple, cheap and unambiguous.
19760Sstevel@tonic-gate 		 * . avoid extension/shrink of the the segkpm in case of DR.
19770Sstevel@tonic-gate 		 * . avoid complexity for handling of virtual addressed
19780Sstevel@tonic-gate 		 *   caches, segkpm and the regular mapping scheme must be
19790Sstevel@tonic-gate 		 *   kept in sync wrt. the virtual color of mapped pages.
19800Sstevel@tonic-gate 		 * Any accesses to virtual segkpm ranges not backed by
19810Sstevel@tonic-gate 		 * physical memory will fall through the memseg pfn hash
19820Sstevel@tonic-gate 		 * and will be handled in segkpm_fault.
19830Sstevel@tonic-gate 		 * Additional kpm_size spaces needed for vac alias prevention.
19840Sstevel@tonic-gate 		 */
19850Sstevel@tonic-gate 		if (seg_attach(&kas, kpm_vbase, kpm_size * vac_colors,
19860Sstevel@tonic-gate 		    segkpm) < 0)
19870Sstevel@tonic-gate 			cmn_err(CE_PANIC, "cannot attach segkpm");
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 		b.prot = PROT_READ | PROT_WRITE;
19900Sstevel@tonic-gate 		b.nvcolors = shm_alignment >> MMU_PAGESHIFT;
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 		if (segkpm_create(segkpm, (caddr_t)&b) != 0)
19930Sstevel@tonic-gate 			panic("segkpm_create segkpm");
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 		rw_exit(&kas.a_lock);
19962296Sae112802 
19972296Sae112802 		mach_kpm_init();
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate 
200010106SJason.Beloro@Sun.COM 	va = kpm_vbase + (kpm_size * vac_colors);
200110106SJason.Beloro@Sun.COM 
20023290Sjohansen 	if (!segzio_fromheap) {
20033290Sjohansen 		size_t size;
20043552Sjohansen 		size_t physmem_b = mmu_ptob(physmem);
20053290Sjohansen 
20063290Sjohansen 		/* size is in bytes, segziosize is in pages */
20073290Sjohansen 		if (segziosize == 0) {
20083552Sjohansen 			size = physmem_b;
20093290Sjohansen 		} else {
20103290Sjohansen 			size = mmu_ptob(segziosize);
20113290Sjohansen 		}
20123290Sjohansen 
20133290Sjohansen 		if (size < SEGZIOMINSIZE) {
20143290Sjohansen 			size = SEGZIOMINSIZE;
20153552Sjohansen 		} else if (size > SEGZIOMAXSIZE) {
20163552Sjohansen 			size = SEGZIOMAXSIZE;
20173552Sjohansen 			/*
20183552Sjohansen 			 * On 64-bit x86, we only have 2TB of KVA.  This exists
20193552Sjohansen 			 * for parity with x86.
20203552Sjohansen 			 *
20213552Sjohansen 			 * SEGZIOMAXSIZE is capped at 512gb so that segzio
20223552Sjohansen 			 * doesn't consume all of KVA.  However, if we have a
20233552Sjohansen 			 * system that has more thant 512gb of physical memory,
20243552Sjohansen 			 * we can actually consume about half of the difference
20253552Sjohansen 			 * between 512gb and the rest of the available physical
20263552Sjohansen 			 * memory.
20273552Sjohansen 			 */
20283552Sjohansen 			if (physmem_b > SEGZIOMAXSIZE) {
20293552Sjohansen 				size += (physmem_b - SEGZIOMAXSIZE) / 2;
20303552Sjohansen 		}
20313290Sjohansen 		}
20323290Sjohansen 		segziosize = mmu_btop(roundup(size, MMU_PAGESIZE));
20333290Sjohansen 		/* put the base of the ZIO segment after the kpm segment */
203410106SJason.Beloro@Sun.COM 		segzio_base = va;
203510106SJason.Beloro@Sun.COM 		va += mmu_ptob(segziosize);
20363290Sjohansen 		PRM_DEBUG(segziosize);
20373290Sjohansen 		PRM_DEBUG(segzio_base);
20383290Sjohansen 
20393290Sjohansen 		/*
20403290Sjohansen 		 * On some platforms, kvm_init is called after the kpm
20413290Sjohansen 		 * sizes have been determined.  On SPARC, kvm_init is called
20423290Sjohansen 		 * before, so we have to attach the kzioseg after kvm is
20433290Sjohansen 		 * initialized, otherwise we'll try to allocate from the boot
20443290Sjohansen 		 * area since the kernel heap hasn't yet been configured.
20453290Sjohansen 		 */
20463290Sjohansen 		rw_enter(&kas.a_lock, RW_WRITER);
20473290Sjohansen 
20483290Sjohansen 		(void) seg_attach(&kas, segzio_base, mmu_ptob(segziosize),
20493290Sjohansen 		    &kzioseg);
20503290Sjohansen 		(void) segkmem_zio_create(&kzioseg);
20513290Sjohansen 
20523290Sjohansen 		/* create zio area covering new segment */
20533290Sjohansen 		segkmem_zio_init(segzio_base, mmu_ptob(segziosize));
20543290Sjohansen 
20553290Sjohansen 		rw_exit(&kas.a_lock);
20563290Sjohansen 	}
20573290Sjohansen 
205810106SJason.Beloro@Sun.COM 	if (ppvm_enable) {
205910106SJason.Beloro@Sun.COM 		caddr_t ppvm_max;
206010106SJason.Beloro@Sun.COM 
206110106SJason.Beloro@Sun.COM 		/*
206210106SJason.Beloro@Sun.COM 		 * ppvm refers to the static VA space used to map
206310106SJason.Beloro@Sun.COM 		 * the page_t's for dynamically added memory.
206410106SJason.Beloro@Sun.COM 		 *
206510106SJason.Beloro@Sun.COM 		 * ppvm_base should not cross a potential VA hole.
206610106SJason.Beloro@Sun.COM 		 *
206710106SJason.Beloro@Sun.COM 		 * ppvm_size should be large enough to map the
206810106SJason.Beloro@Sun.COM 		 * page_t's needed to manage all of KPM range.
206910106SJason.Beloro@Sun.COM 		 */
207010106SJason.Beloro@Sun.COM 		ppvm_size =
207110106SJason.Beloro@Sun.COM 		    roundup(mmu_btop(kpm_size * vac_colors) * sizeof (page_t),
207210106SJason.Beloro@Sun.COM 		    MMU_PAGESIZE);
207310106SJason.Beloro@Sun.COM 		ppvm_max = (caddr_t)(0ull - ppvm_size);
207410106SJason.Beloro@Sun.COM 		ppvm_base = (page_t *)va;
207510106SJason.Beloro@Sun.COM 
207610106SJason.Beloro@Sun.COM 		if ((caddr_t)ppvm_base <= hole_end) {
207710106SJason.Beloro@Sun.COM 			cmn_err(CE_WARN,
207810106SJason.Beloro@Sun.COM 			    "Memory DR disabled: invalid DR map base: 0x%p\n",
207910106SJason.Beloro@Sun.COM 			    (void *)ppvm_base);
208010106SJason.Beloro@Sun.COM 			ppvm_enable = 0;
208110106SJason.Beloro@Sun.COM 		} else if ((caddr_t)ppvm_base > ppvm_max) {
208210106SJason.Beloro@Sun.COM 			uint64_t diff = (caddr_t)ppvm_base - ppvm_max;
208310106SJason.Beloro@Sun.COM 
208410106SJason.Beloro@Sun.COM 			cmn_err(CE_WARN,
208510106SJason.Beloro@Sun.COM 			    "Memory DR disabled: insufficient DR map size:"
208610106SJason.Beloro@Sun.COM 			    " 0x%lx (needed 0x%lx)\n",
208710106SJason.Beloro@Sun.COM 			    ppvm_size - diff, ppvm_size);
208810106SJason.Beloro@Sun.COM 			ppvm_enable = 0;
208910106SJason.Beloro@Sun.COM 		}
209010106SJason.Beloro@Sun.COM 		PRM_DEBUG(ppvm_size);
209110106SJason.Beloro@Sun.COM 		PRM_DEBUG(ppvm_base);
209210106SJason.Beloro@Sun.COM 	}
20933290Sjohansen 
20940Sstevel@tonic-gate 	/*
20950Sstevel@tonic-gate 	 * Now create generic mapping segment.  This mapping
20960Sstevel@tonic-gate 	 * goes SEGMAPSIZE beyond SEGMAPBASE.  But if the total
20970Sstevel@tonic-gate 	 * virtual address is greater than the amount of free
20980Sstevel@tonic-gate 	 * memory that is available, then we trim back the
20990Sstevel@tonic-gate 	 * segment size to that amount
21000Sstevel@tonic-gate 	 */
21010Sstevel@tonic-gate 	va = (caddr_t)SEGMAPBASE;
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * 1201049: segkmap base address must be MAXBSIZE aligned
21050Sstevel@tonic-gate 	 */
21060Sstevel@tonic-gate 	ASSERT(((uintptr_t)va & MAXBOFFSET) == 0);
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * Set size of segmap to percentage of freemem at boot,
21100Sstevel@tonic-gate 	 * but stay within the allowable range
21110Sstevel@tonic-gate 	 * Note we take percentage  before converting from pages
21120Sstevel@tonic-gate 	 * to bytes to avoid an overflow on 32-bit kernels.
21130Sstevel@tonic-gate 	 */
21140Sstevel@tonic-gate 	i = mmu_ptob((freemem * segmap_percent) / 100);
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	if (i < MINMAPSIZE)
21170Sstevel@tonic-gate 		i = MINMAPSIZE;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	if (i > MIN(SEGMAPSIZE, mmu_ptob(freemem)))
21200Sstevel@tonic-gate 		i = MIN(SEGMAPSIZE, mmu_ptob(freemem));
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	i &= MAXBMASK;	/* 1201049: segkmap size must be MAXBSIZE aligned */
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
21250Sstevel@tonic-gate 	if (seg_attach(&kas, va, i, segkmap) < 0)
21260Sstevel@tonic-gate 		cmn_err(CE_PANIC, "cannot attach segkmap");
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	a.prot = PROT_READ | PROT_WRITE;
21290Sstevel@tonic-gate 	a.shmsize = shm_alignment;
21300Sstevel@tonic-gate 	a.nfreelist = 0;	/* use segmap driver defaults */
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	if (segmap_create(segkmap, (caddr_t)&a) != 0)
21330Sstevel@tonic-gate 		panic("segmap_create segkmap");
21340Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	segdev_init();
21370Sstevel@tonic-gate }
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate static void
21400Sstevel@tonic-gate startup_end(void)
21410Sstevel@tonic-gate {
21420Sstevel@tonic-gate 	if ((caddr_t)memlist > (caddr_t)memlist_end)
21430Sstevel@tonic-gate 		panic("memlist overflow 2");
21440Sstevel@tonic-gate 	memlist_free_block((caddr_t)memlist,
21450Sstevel@tonic-gate 	    ((caddr_t)memlist_end - (caddr_t)memlist));
21460Sstevel@tonic-gate 	memlist = NULL;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	/* enable page_relocation since OBP is now done */
21490Sstevel@tonic-gate 	page_relocate_ready = 1;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	/*
21520Sstevel@tonic-gate 	 * Perform tasks that get done after most of the VM
21530Sstevel@tonic-gate 	 * initialization has been done but before the clock
21540Sstevel@tonic-gate 	 * and other devices get started.
21550Sstevel@tonic-gate 	 */
21560Sstevel@tonic-gate 	kern_setup1();
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	/*
21598803SJonathan.Haslam@Sun.COM 	 * Perform CPC initialization for this CPU.
21608803SJonathan.Haslam@Sun.COM 	 */
21618803SJonathan.Haslam@Sun.COM 	kcpc_hw_init();
21628803SJonathan.Haslam@Sun.COM 
21638803SJonathan.Haslam@Sun.COM 	/*
21640Sstevel@tonic-gate 	 * Intialize the VM arenas for allocating physically
21650Sstevel@tonic-gate 	 * contiguus memory chunk for interrupt queues snd
21660Sstevel@tonic-gate 	 * allocate/register boot cpu's queues, if any and
21670Sstevel@tonic-gate 	 * allocate dump buffer for sun4v systems to store
21680Sstevel@tonic-gate 	 * extra crash information during crash dump
21690Sstevel@tonic-gate 	 */
21700Sstevel@tonic-gate 	contig_mem_init();
21710Sstevel@tonic-gate 	mach_descrip_init();
21724050Sjb145095 
21734050Sjb145095 	if (cpu_intrq_setup(CPU)) {
21744050Sjb145095 		cmn_err(CE_PANIC, "cpu%d: setup failed", CPU->cpu_id);
21754050Sjb145095 	}
21760Sstevel@tonic-gate 	cpu_intrq_register(CPU);
21771077Ssvemuri 	mach_htraptrace_setup(CPU->cpu_id);
21781077Ssvemuri 	mach_htraptrace_configure(CPU->cpu_id);
21790Sstevel@tonic-gate 	mach_dump_buffer_init();
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	/*
21820Sstevel@tonic-gate 	 * Initialize interrupt related stuff
21830Sstevel@tonic-gate 	 */
21841455Sandrei 	cpu_intr_alloc(CPU, NINTR_THREADS);
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	(void) splzs();			/* allow hi clock ints but not zs */
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	/*
21890Sstevel@tonic-gate 	 * Initialize errors.
21900Sstevel@tonic-gate 	 */
21910Sstevel@tonic-gate 	error_init();
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 	/*
21940Sstevel@tonic-gate 	 * Note that we may have already used kernel bcopy before this
21950Sstevel@tonic-gate 	 * point - but if you really care about this, adb the use_hw_*
21960Sstevel@tonic-gate 	 * variables to 0 before rebooting.
21970Sstevel@tonic-gate 	 */
21980Sstevel@tonic-gate 	mach_hw_copy_limit();
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 	/*
22010Sstevel@tonic-gate 	 * Install the "real" preemption guards before DDI services
22020Sstevel@tonic-gate 	 * are available.
22030Sstevel@tonic-gate 	 */
22040Sstevel@tonic-gate 	(void) prom_set_preprom(kern_preprom);
22050Sstevel@tonic-gate 	(void) prom_set_postprom(kern_postprom);
22060Sstevel@tonic-gate 	CPU->cpu_m.mutex_ready = 1;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	/*
22090Sstevel@tonic-gate 	 * Initialize segnf (kernel support for non-faulting loads).
22100Sstevel@tonic-gate 	 */
22110Sstevel@tonic-gate 	segnf_init();
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	/*
22140Sstevel@tonic-gate 	 * Configure the root devinfo node.
22150Sstevel@tonic-gate 	 */
22160Sstevel@tonic-gate 	configure();		/* set up devices */
22170Sstevel@tonic-gate 	mach_cpu_halt_idle();
22180Sstevel@tonic-gate }
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate void
22220Sstevel@tonic-gate post_startup(void)
22230Sstevel@tonic-gate {
22240Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
22250Sstevel@tonic-gate 	extern void init_ptl1_thread(void);
22260Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
22270Sstevel@tonic-gate 	extern void abort_sequence_init(void);
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	/*
22300Sstevel@tonic-gate 	 * Set the system wide, processor-specific flags to be passed
22310Sstevel@tonic-gate 	 * to userland via the aux vector for performance hints and
22320Sstevel@tonic-gate 	 * instruction set extensions.
22330Sstevel@tonic-gate 	 */
22340Sstevel@tonic-gate 	bind_hwcap();
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	/*
22370Sstevel@tonic-gate 	 * Startup memory scrubber (if any)
22380Sstevel@tonic-gate 	 */
22390Sstevel@tonic-gate 	mach_memscrub();
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 	/*
22420Sstevel@tonic-gate 	 * Allocate soft interrupt to handle abort sequence.
22430Sstevel@tonic-gate 	 */
22440Sstevel@tonic-gate 	abort_sequence_init();
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 	/*
22470Sstevel@tonic-gate 	 * Configure the rest of the system.
22480Sstevel@tonic-gate 	 * Perform forceloading tasks for /etc/system.
22490Sstevel@tonic-gate 	 */
22500Sstevel@tonic-gate 	(void) mod_sysctl(SYS_FORCELOAD, NULL);
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * ON4.0: Force /proc module in until clock interrupt handle fixed
22530Sstevel@tonic-gate 	 * ON4.0: This must be fixed or restated in /etc/systems.
22540Sstevel@tonic-gate 	 */
22550Sstevel@tonic-gate 	(void) modload("fs", "procfs");
22560Sstevel@tonic-gate 
22571991Sheppo 	/* load machine class specific drivers */
22581991Sheppo 	load_mach_drivers();
22591991Sheppo 
22601991Sheppo 	/* load platform specific drivers */
22610Sstevel@tonic-gate 	if (&load_platform_drivers)
22620Sstevel@tonic-gate 		load_platform_drivers();
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	/* load vis simulation module, if we are running w/fpu off */
22650Sstevel@tonic-gate 	if (!fpu_exists) {
22660Sstevel@tonic-gate 		if (modload("misc", "vis") == -1)
22670Sstevel@tonic-gate 			halt("Can't load vis");
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	mach_fpras();
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	maxmem = freemem;
22730Sstevel@tonic-gate 
22748906SEric.Saxe@Sun.COM 	pg_init();
22758906SEric.Saxe@Sun.COM 
22760Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
22770Sstevel@tonic-gate 	init_ptl1_thread();
22780Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
22790Sstevel@tonic-gate }
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate #ifdef	PTL1_PANIC_DEBUG
22820Sstevel@tonic-gate int		ptl1_panic_test = 0;
22830Sstevel@tonic-gate int		ptl1_panic_xc_one_test = 0;
22840Sstevel@tonic-gate int		ptl1_panic_xc_all_test = 0;
22850Sstevel@tonic-gate int		ptl1_panic_xt_one_test = 0;
22860Sstevel@tonic-gate int		ptl1_panic_xt_all_test = 0;
22870Sstevel@tonic-gate kthread_id_t	ptl1_thread_p = NULL;
22880Sstevel@tonic-gate kcondvar_t	ptl1_cv;
22890Sstevel@tonic-gate kmutex_t	ptl1_mutex;
22900Sstevel@tonic-gate int		ptl1_recurse_count_threshold = 0x40;
22910Sstevel@tonic-gate int		ptl1_recurse_trap_threshold = 0x3d;
22920Sstevel@tonic-gate extern void	ptl1_recurse(int, int);
22930Sstevel@tonic-gate extern void	ptl1_panic_xt(int, int);
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate /*
22960Sstevel@tonic-gate  * Called once per second by timeout() to wake up
22970Sstevel@tonic-gate  * the ptl1_panic thread to see if it should cause
22980Sstevel@tonic-gate  * a trap to the ptl1_panic() code.
22990Sstevel@tonic-gate  */
23000Sstevel@tonic-gate /* ARGSUSED */
23010Sstevel@tonic-gate static void
23020Sstevel@tonic-gate ptl1_wakeup(void *arg)
23030Sstevel@tonic-gate {
23040Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
23050Sstevel@tonic-gate 	cv_signal(&ptl1_cv);
23060Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
23070Sstevel@tonic-gate }
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate /*
23100Sstevel@tonic-gate  * ptl1_panic cross call function:
23110Sstevel@tonic-gate  *     Needed because xc_one() and xc_some() can pass
23120Sstevel@tonic-gate  *	64 bit args but ptl1_recurse() expects ints.
23130Sstevel@tonic-gate  */
23140Sstevel@tonic-gate static void
23150Sstevel@tonic-gate ptl1_panic_xc(void)
23160Sstevel@tonic-gate {
23170Sstevel@tonic-gate 	ptl1_recurse(ptl1_recurse_count_threshold,
23180Sstevel@tonic-gate 	    ptl1_recurse_trap_threshold);
23190Sstevel@tonic-gate }
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate /*
23220Sstevel@tonic-gate  * The ptl1 thread waits for a global flag to be set
23230Sstevel@tonic-gate  * and uses the recurse thresholds to set the stack depth
23240Sstevel@tonic-gate  * to cause a ptl1_panic() directly via a call to ptl1_recurse
23250Sstevel@tonic-gate  * or indirectly via the cross call and cross trap functions.
23260Sstevel@tonic-gate  *
23270Sstevel@tonic-gate  * This is useful testing stack overflows and normal
23280Sstevel@tonic-gate  * ptl1_panic() states with a know stack frame.
23290Sstevel@tonic-gate  *
23300Sstevel@tonic-gate  * ptl1_recurse() is an asm function in ptl1_panic.s that
23310Sstevel@tonic-gate  * sets the {In, Local, Out, and Global} registers to a
23320Sstevel@tonic-gate  * know state on the stack and just prior to causing a
23330Sstevel@tonic-gate  * test ptl1_panic trap.
23340Sstevel@tonic-gate  */
23350Sstevel@tonic-gate static void
23360Sstevel@tonic-gate ptl1_thread(void)
23370Sstevel@tonic-gate {
23380Sstevel@tonic-gate 	mutex_enter(&ptl1_mutex);
23390Sstevel@tonic-gate 	while (ptl1_thread_p) {
23400Sstevel@tonic-gate 		cpuset_t	other_cpus;
23410Sstevel@tonic-gate 		int		cpu_id;
23420Sstevel@tonic-gate 		int		my_cpu_id;
23430Sstevel@tonic-gate 		int		target_cpu_id;
23440Sstevel@tonic-gate 		int		target_found;
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 		if (ptl1_panic_test) {
23470Sstevel@tonic-gate 			ptl1_recurse(ptl1_recurse_count_threshold,
23480Sstevel@tonic-gate 			    ptl1_recurse_trap_threshold);
23490Sstevel@tonic-gate 		}
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 		/*
23520Sstevel@tonic-gate 		 * Find potential targets for x-call and x-trap,
23530Sstevel@tonic-gate 		 * if any exist while preempt is disabled we
23540Sstevel@tonic-gate 		 * start a ptl1_panic if requested via a
23550Sstevel@tonic-gate 		 * globals.
23560Sstevel@tonic-gate 		 */
23570Sstevel@tonic-gate 		kpreempt_disable();
23580Sstevel@tonic-gate 		my_cpu_id = CPU->cpu_id;
23590Sstevel@tonic-gate 		other_cpus = cpu_ready_set;
23600Sstevel@tonic-gate 		CPUSET_DEL(other_cpus, CPU->cpu_id);
23610Sstevel@tonic-gate 		target_found = 0;
23620Sstevel@tonic-gate 		if (!CPUSET_ISNULL(other_cpus)) {
23630Sstevel@tonic-gate 			/*
23640Sstevel@tonic-gate 			 * Pick the first one
23650Sstevel@tonic-gate 			 */
23660Sstevel@tonic-gate 			for (cpu_id = 0; cpu_id < NCPU; cpu_id++) {
23670Sstevel@tonic-gate 				if (cpu_id == my_cpu_id)
23680Sstevel@tonic-gate 					continue;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 				if (CPU_XCALL_READY(cpu_id)) {
23710Sstevel@tonic-gate 					target_cpu_id = cpu_id;
23720Sstevel@tonic-gate 					target_found = 1;
23730Sstevel@tonic-gate 					break;
23740Sstevel@tonic-gate 				}
23750Sstevel@tonic-gate 			}
23760Sstevel@tonic-gate 			ASSERT(target_found);
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 			if (ptl1_panic_xc_one_test) {
23790Sstevel@tonic-gate 				xc_one(target_cpu_id,
23800Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
23810Sstevel@tonic-gate 			}
23820Sstevel@tonic-gate 			if (ptl1_panic_xc_all_test) {
23830Sstevel@tonic-gate 				xc_some(other_cpus,
23840Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xc, 0, 0);
23850Sstevel@tonic-gate 			}
23860Sstevel@tonic-gate 			if (ptl1_panic_xt_one_test) {
23870Sstevel@tonic-gate 				xt_one(target_cpu_id,
23880Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
23890Sstevel@tonic-gate 			}
23900Sstevel@tonic-gate 			if (ptl1_panic_xt_all_test) {
23910Sstevel@tonic-gate 				xt_some(other_cpus,
23920Sstevel@tonic-gate 				    (xcfunc_t *)ptl1_panic_xt, 0, 0);
23930Sstevel@tonic-gate 			}
23940Sstevel@tonic-gate 		}
23950Sstevel@tonic-gate 		kpreempt_enable();
23960Sstevel@tonic-gate 		(void) timeout(ptl1_wakeup, NULL, hz);
23970Sstevel@tonic-gate 		(void) cv_wait(&ptl1_cv, &ptl1_mutex);
23980Sstevel@tonic-gate 	}
23990Sstevel@tonic-gate 	mutex_exit(&ptl1_mutex);
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate /*
24030Sstevel@tonic-gate  * Called during early startup to create the ptl1_thread
24040Sstevel@tonic-gate  */
24050Sstevel@tonic-gate void
24060Sstevel@tonic-gate init_ptl1_thread(void)
24070Sstevel@tonic-gate {
24080Sstevel@tonic-gate 	ptl1_thread_p = thread_create(NULL, 0, ptl1_thread, NULL, 0,
24090Sstevel@tonic-gate 	    &p0, TS_RUN, 0);
24100Sstevel@tonic-gate }
24110Sstevel@tonic-gate #endif	/* PTL1_PANIC_DEBUG */
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 
24145648Ssetje static void
24155648Ssetje memlist_new(uint64_t start, uint64_t len, struct memlist **memlistp)
24165648Ssetje {
24175648Ssetje 	struct memlist *new;
24185648Ssetje 
24195648Ssetje 	new = *memlistp;
24205648Ssetje 	new->address = start;
24215648Ssetje 	new->size = len;
24225648Ssetje 	*memlistp = new + 1;
24235648Ssetje }
24245648Ssetje 
24250Sstevel@tonic-gate /*
24260Sstevel@tonic-gate  * Add to a memory list.
24270Sstevel@tonic-gate  * start = start of new memory segment
24280Sstevel@tonic-gate  * len = length of new memory segment in bytes
24290Sstevel@tonic-gate  * memlistp = pointer to array of available memory segment structures
24300Sstevel@tonic-gate  * curmemlistp = memory list to which to add segment.
24310Sstevel@tonic-gate  */
24320Sstevel@tonic-gate static void
24330Sstevel@tonic-gate memlist_add(uint64_t start, uint64_t len, struct memlist **memlistp,
24340Sstevel@tonic-gate 	struct memlist **curmemlistp)
24350Sstevel@tonic-gate {
24365648Ssetje 	struct memlist *new = *memlistp;
24375648Ssetje 
24385648Ssetje 	memlist_new(start, len, memlistp);
24390Sstevel@tonic-gate 	memlist_insert(new, curmemlistp);
24400Sstevel@tonic-gate }
24410Sstevel@tonic-gate 
24425648Ssetje static int
24435648Ssetje ndata_alloc_memseg(struct memlist *ndata, size_t avail)
24445648Ssetje {
24455648Ssetje 	int nseg;
24465648Ssetje 	size_t memseg_sz;
24475648Ssetje 	struct memseg *msp;
24485648Ssetje 
24495648Ssetje 	/*
24505648Ssetje 	 * The memseg list is for the chunks of physical memory that
24515648Ssetje 	 * will be managed by the vm system.  The number calculated is
24525648Ssetje 	 * a guess as boot may fragment it more when memory allocations
24535648Ssetje 	 * are made before kphysm_init().
24545648Ssetje 	 */
24555648Ssetje 	memseg_sz = (avail + 10) * sizeof (struct memseg);
24565648Ssetje 	memseg_sz = roundup(memseg_sz, PAGESIZE);
24575648Ssetje 	nseg = memseg_sz / sizeof (struct memseg);
24585648Ssetje 	msp = ndata_alloc(ndata, memseg_sz, ecache_alignsize);
24595648Ssetje 	if (msp == NULL)
24605648Ssetje 		return (1);
24615648Ssetje 	PRM_DEBUG(memseg_free);
24625648Ssetje 
24635648Ssetje 	while (nseg--) {
24645648Ssetje 		msp->next = memseg_free;
24655648Ssetje 		memseg_free = msp;
24665648Ssetje 		msp++;
24675648Ssetje 	}
24685648Ssetje 	return (0);
24695648Ssetje }
24705648Ssetje 
24710Sstevel@tonic-gate /*
24720Sstevel@tonic-gate  * In the case of architectures that support dynamic addition of
24730Sstevel@tonic-gate  * memory at run-time there are two cases where memsegs need to
24740Sstevel@tonic-gate  * be initialized and added to the memseg list.
24750Sstevel@tonic-gate  * 1) memsegs that are constructed at startup.
24760Sstevel@tonic-gate  * 2) memsegs that are constructed at run-time on
24770Sstevel@tonic-gate  *    hot-plug capable architectures.
24780Sstevel@tonic-gate  * This code was originally part of the function kphysm_init().
24790Sstevel@tonic-gate  */
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate static void
24820Sstevel@tonic-gate memseg_list_add(struct memseg *memsegp)
24830Sstevel@tonic-gate {
24840Sstevel@tonic-gate 	struct memseg **prev_memsegp;
24850Sstevel@tonic-gate 	pgcnt_t num;
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	/* insert in memseg list, decreasing number of pages order */
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	num = MSEG_NPAGES(memsegp);
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 	for (prev_memsegp = &memsegs; *prev_memsegp;
24920Sstevel@tonic-gate 	    prev_memsegp = &((*prev_memsegp)->next)) {
24930Sstevel@tonic-gate 		if (num > MSEG_NPAGES(*prev_memsegp))
24940Sstevel@tonic-gate 			break;
24950Sstevel@tonic-gate 	}
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	memsegp->next = *prev_memsegp;
24980Sstevel@tonic-gate 	*prev_memsegp = memsegp;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	if (kpm_enable) {
25010Sstevel@tonic-gate 		memsegp->nextpa = (memsegp->next) ?
25024528Spaulsan 		    va_to_pa(memsegp->next) : MSEG_NULLPTR_PA;
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 		if (prev_memsegp != &memsegs) {
25050Sstevel@tonic-gate 			struct memseg *msp;
25060Sstevel@tonic-gate 			msp = (struct memseg *)((caddr_t)prev_memsegp -
25074528Spaulsan 			    offsetof(struct memseg, next));
25080Sstevel@tonic-gate 			msp->nextpa = va_to_pa(memsegp);
25090Sstevel@tonic-gate 		} else {
25100Sstevel@tonic-gate 			memsegspa = va_to_pa(memsegs);
25110Sstevel@tonic-gate 		}
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate }
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate /*
25160Sstevel@tonic-gate  * PSM add_physmem_cb(). US-II and newer processors have some
25170Sstevel@tonic-gate  * flavor of the prefetch capability implemented. We exploit
25180Sstevel@tonic-gate  * this capability for optimum performance.
25190Sstevel@tonic-gate  */
25200Sstevel@tonic-gate #define	PREFETCH_BYTES	64
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate void
25230Sstevel@tonic-gate add_physmem_cb(page_t *pp, pfn_t pnum)
25240Sstevel@tonic-gate {
25250Sstevel@tonic-gate 	extern void	 prefetch_page_w(void *);
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	pp->p_pagenum = pnum;
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	/*
25300Sstevel@tonic-gate 	 * Prefetch one more page_t into E$. To prevent future
25310Sstevel@tonic-gate 	 * mishaps with the sizeof(page_t) changing on us, we
25320Sstevel@tonic-gate 	 * catch this on debug kernels if we can't bring in the
25330Sstevel@tonic-gate 	 * entire hpage with 2 PREFETCH_BYTES reads. See
25340Sstevel@tonic-gate 	 * also, sun4u/cpu/cpu_module.c
25350Sstevel@tonic-gate 	 */
25360Sstevel@tonic-gate 	/*LINTED*/
25370Sstevel@tonic-gate 	ASSERT(sizeof (page_t) <= 2*PREFETCH_BYTES);
25380Sstevel@tonic-gate 	prefetch_page_w((char *)pp);
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate /*
25425648Ssetje  * Find memseg with given pfn
25435648Ssetje  */
25445648Ssetje static struct memseg *
25455648Ssetje memseg_find(pfn_t base, pfn_t *next)
25465648Ssetje {
25475648Ssetje 	struct memseg *seg;
25485648Ssetje 
25495648Ssetje 	if (next != NULL)
25505648Ssetje 		*next = LONG_MAX;
25515648Ssetje 	for (seg = memsegs; seg != NULL; seg = seg->next) {
25525648Ssetje 		if (base >= seg->pages_base && base < seg->pages_end)
25535648Ssetje 			return (seg);
25545648Ssetje 		if (next != NULL && seg->pages_base > base &&
25555648Ssetje 		    seg->pages_base < *next)
25565648Ssetje 			*next = seg->pages_base;
25575648Ssetje 	}
25585648Ssetje 	return (NULL);
25595648Ssetje }
25605648Ssetje 
25615648Ssetje extern struct vnode prom_ppages;
25625648Ssetje 
25635648Ssetje /*
25645648Ssetje  * Put page allocated by OBP on prom_ppages
25650Sstevel@tonic-gate  */
25660Sstevel@tonic-gate static void
25675648Ssetje kphysm_erase(uint64_t addr, uint64_t len)
25685648Ssetje {
25695648Ssetje 	struct page *pp;
25705648Ssetje 	struct memseg *seg;
25715648Ssetje 	pfn_t base = btop(addr), next;
25725648Ssetje 	pgcnt_t num = btop(len);
25735648Ssetje 
25745648Ssetje 	while (num != 0) {
25755648Ssetje 		pgcnt_t off, left;
25765648Ssetje 
25775648Ssetje 		seg = memseg_find(base, &next);
25785648Ssetje 		if (seg == NULL) {
25795648Ssetje 			if (next == LONG_MAX)
25805648Ssetje 				break;
25815648Ssetje 			left = MIN(next - base, num);
25825648Ssetje 			base += left, num -= left;
25835648Ssetje 			continue;
25845648Ssetje 		}
25855648Ssetje 		off = base - seg->pages_base;
25865648Ssetje 		pp = seg->pages + off;
25875648Ssetje 		left = num - MIN(num, (seg->pages_end - seg->pages_base) - off);
25885648Ssetje 		while (num != left) {
25895648Ssetje 			/*
25905648Ssetje 			 * init it, lock it, and hashin on prom_pages vp.
25915648Ssetje 			 *
259210106SJason.Beloro@Sun.COM 			 * Mark it as NONRELOC to let DR know the page
259310106SJason.Beloro@Sun.COM 			 * is locked long term, otherwise DR hangs when
259410106SJason.Beloro@Sun.COM 			 * trying to remove those pages.
259510106SJason.Beloro@Sun.COM 			 *
25965648Ssetje 			 * XXX	vnode offsets on the prom_ppages vnode
25975648Ssetje 			 *	are page numbers (gack) for >32 bit
25985648Ssetje 			 *	physical memory machines.
25995648Ssetje 			 */
260010106SJason.Beloro@Sun.COM 			PP_SETNORELOC(pp);
26015648Ssetje 			add_physmem_cb(pp, base);
26025648Ssetje 			if (page_trylock(pp, SE_EXCL) == 0)
26035648Ssetje 				cmn_err(CE_PANIC, "prom page locked");
26045648Ssetje 			(void) page_hashin(pp, &prom_ppages,
26055648Ssetje 			    (offset_t)base, NULL);
26065648Ssetje 			(void) page_pp_lock(pp, 0, 1);
26075648Ssetje 			pp++, base++, num--;
26085648Ssetje 		}
26095648Ssetje 	}
26105648Ssetje }
26115648Ssetje 
26125648Ssetje static page_t *ppnext;
26135648Ssetje static pgcnt_t ppleft;
26145648Ssetje 
26155648Ssetje static void *kpm_ppnext;
26165648Ssetje static pgcnt_t kpm_ppleft;
26175648Ssetje 
26185648Ssetje /*
26195648Ssetje  * Create a memseg
26205648Ssetje  */
26215648Ssetje static void
26225648Ssetje kphysm_memseg(uint64_t addr, uint64_t len)
26230Sstevel@tonic-gate {
26245648Ssetje 	pfn_t base = btop(addr);
26255648Ssetje 	pgcnt_t num = btop(len);
26265648Ssetje 	struct memseg *seg;
26275648Ssetje 
26285648Ssetje 	seg = memseg_free;
26295648Ssetje 	memseg_free = seg->next;
26305648Ssetje 	ASSERT(seg != NULL);
26315648Ssetje 
26325648Ssetje 	seg->pages = ppnext;
26335648Ssetje 	seg->epages = ppnext + num;
26345648Ssetje 	seg->pages_base = base;
26355648Ssetje 	seg->pages_end = base + num;
26365648Ssetje 	ppnext += num;
26375648Ssetje 	ppleft -= num;
26385648Ssetje 
26395648Ssetje 	if (kpm_enable) {
26405648Ssetje 		pgcnt_t kpnum = ptokpmpr(num);
26415648Ssetje 
26425648Ssetje 		if (kpnum > kpm_ppleft)
26435648Ssetje 			panic("kphysm_memseg: kpm_pp overflow");
26445648Ssetje 		seg->pagespa = va_to_pa(seg->pages);
26455648Ssetje 		seg->epagespa = va_to_pa(seg->epages);
26465648Ssetje 		seg->kpm_pbase = kpmptop(ptokpmp(base));
26475648Ssetje 		seg->kpm_nkpmpgs = kpnum;
26485648Ssetje 		/*
26495648Ssetje 		 * In the kpm_smallpage case, the kpm array
26505648Ssetje 		 * is 1-1 wrt the page array
26515648Ssetje 		 */
26525648Ssetje 		if (kpm_smallpages) {
26535648Ssetje 			kpm_spage_t *kpm_pp = kpm_ppnext;
26545648Ssetje 
26555648Ssetje 			kpm_ppnext = kpm_pp + kpnum;
26565648Ssetje 			seg->kpm_spages = kpm_pp;
26575648Ssetje 			seg->kpm_pagespa = va_to_pa(seg->kpm_spages);
26585648Ssetje 		} else {
26595648Ssetje 			kpm_page_t *kpm_pp = kpm_ppnext;
26605648Ssetje 
26615648Ssetje 			kpm_ppnext = kpm_pp + kpnum;
26625648Ssetje 			seg->kpm_pages = kpm_pp;
26635648Ssetje 			seg->kpm_pagespa = va_to_pa(seg->kpm_pages);
26645648Ssetje 			/* ASSERT no kpm overlaps */
26655648Ssetje 			ASSERT(
26665648Ssetje 			    memseg_find(base - pmodkpmp(base), NULL) == NULL);
26675648Ssetje 			ASSERT(memseg_find(
26685648Ssetje 			    roundup(base + num, kpmpnpgs) - 1, NULL) == NULL);
26695648Ssetje 		}
267010106SJason.Beloro@Sun.COM 		kpm_ppleft -= kpnum;
26715648Ssetje 	}
26725648Ssetje 
26735648Ssetje 	memseg_list_add(seg);
26745648Ssetje }
26755648Ssetje 
26765648Ssetje /*
26775648Ssetje  * Add range to free list
26785648Ssetje  */
26795648Ssetje void
26805648Ssetje kphysm_add(uint64_t addr, uint64_t len, int reclaim)
26815648Ssetje {
26825648Ssetje 	struct page *pp;
26835648Ssetje 	struct memseg *seg;
26845648Ssetje 	pfn_t base = btop(addr);
26855648Ssetje 	pgcnt_t num = btop(len);
26865648Ssetje 
26875648Ssetje 	seg = memseg_find(base, NULL);
26885648Ssetje 	ASSERT(seg != NULL);
26895648Ssetje 	pp = seg->pages + (base - seg->pages_base);
26905648Ssetje 
26915648Ssetje 	if (reclaim) {
26925648Ssetje 		struct page *rpp = pp;
26935648Ssetje 		struct page *lpp = pp + num;
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 		/*
26965648Ssetje 		 * page should be locked on prom_ppages
26975648Ssetje 		 * unhash and unlock it
26980Sstevel@tonic-gate 		 */
26995648Ssetje 		while (rpp < lpp) {
27005648Ssetje 			ASSERT(PAGE_EXCL(rpp) && rpp->p_vnode == &prom_ppages);
270110106SJason.Beloro@Sun.COM 			ASSERT(PP_ISNORELOC(rpp));
270210106SJason.Beloro@Sun.COM 			PP_CLRNORELOC(rpp);
27035648Ssetje 			page_pp_unlock(rpp, 0, 1);
27045648Ssetje 			page_hashout(rpp, NULL);
27055648Ssetje 			page_unlock(rpp);
27065648Ssetje 			rpp++;
27070Sstevel@tonic-gate 		}
27080Sstevel@tonic-gate 	}
27090Sstevel@tonic-gate 
27105648Ssetje 	/*
27115648Ssetje 	 * add_physmem() initializes the PSM part of the page
27125648Ssetje 	 * struct by calling the PSM back with add_physmem_cb().
27135648Ssetje 	 * In addition it coalesces pages into larger pages as
27145648Ssetje 	 * it initializes them.
27155648Ssetje 	 */
27165648Ssetje 	add_physmem(pp, num, base);
27175648Ssetje }
27185648Ssetje 
27195648Ssetje /*
27205648Ssetje  * kphysm_init() tackles the problem of initializing physical memory.
27215648Ssetje  */
27225648Ssetje static void
27235648Ssetje kphysm_init(void)
27245648Ssetje {
27255648Ssetje 	struct memlist *pmem;
27265648Ssetje 
27275648Ssetje 	ASSERT(page_hash != NULL && page_hashsz != 0);
27285648Ssetje 
27295648Ssetje 	ppnext = pp_base;
27305648Ssetje 	ppleft = npages;
27315648Ssetje 	kpm_ppnext = kpm_pp_base;
27325648Ssetje 	kpm_ppleft = kpm_npages;
27335648Ssetje 
27345648Ssetje 	/*
27355648Ssetje 	 * installed pages not on nopp_memlist go in memseg list
27365648Ssetje 	 */
27375648Ssetje 	diff_memlists(phys_install, nopp_list, kphysm_memseg);
27385648Ssetje 
27395648Ssetje 	/*
27405648Ssetje 	 * Free the avail list
27415648Ssetje 	 */
27425648Ssetje 	for (pmem = phys_avail; pmem != NULL; pmem = pmem->next)
27435648Ssetje 		kphysm_add(pmem->address, pmem->size, 0);
27445648Ssetje 
27455648Ssetje 	/*
27465648Ssetje 	 * Erase pages that aren't available
27475648Ssetje 	 */
27485648Ssetje 	diff_memlists(phys_install, phys_avail, kphysm_erase);
27495648Ssetje 
27500Sstevel@tonic-gate 	build_pfn_hash();
27510Sstevel@tonic-gate }
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate /*
27540Sstevel@tonic-gate  * Kernel VM initialization.
27550Sstevel@tonic-gate  * Assumptions about kernel address space ordering:
27560Sstevel@tonic-gate  *	(1) gap (user space)
27570Sstevel@tonic-gate  *	(2) kernel text
27580Sstevel@tonic-gate  *	(3) kernel data/bss
27590Sstevel@tonic-gate  *	(4) gap
27600Sstevel@tonic-gate  *	(5) kernel data structures
27610Sstevel@tonic-gate  *	(6) gap
27620Sstevel@tonic-gate  *	(7) debugger (optional)
27630Sstevel@tonic-gate  *	(8) monitor
27640Sstevel@tonic-gate  *	(9) gap (possibly null)
27650Sstevel@tonic-gate  *	(10) dvma
27660Sstevel@tonic-gate  *	(11) devices
27670Sstevel@tonic-gate  */
27680Sstevel@tonic-gate static void
27690Sstevel@tonic-gate kvm_init(void)
27700Sstevel@tonic-gate {
27710Sstevel@tonic-gate 	/*
27720Sstevel@tonic-gate 	 * Put the kernel segments in kernel address space.
27730Sstevel@tonic-gate 	 */
27740Sstevel@tonic-gate 	rw_enter(&kas.a_lock, RW_WRITER);
27750Sstevel@tonic-gate 	as_avlinit(&kas);
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)KERNELBASE,
27780Sstevel@tonic-gate 	    (size_t)(e_moddata - KERNELBASE), &ktextseg);
27790Sstevel@tonic-gate 	(void) segkmem_create(&ktextseg);
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)(KERNELBASE + MMU_PAGESIZE4M),
27820Sstevel@tonic-gate 	    (size_t)(MMU_PAGESIZE4M), &ktexthole);
27830Sstevel@tonic-gate 	(void) segkmem_create(&ktexthole);
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)valloc_base,
27860Sstevel@tonic-gate 	    (size_t)(econtig32 - valloc_base), &kvalloc);
27870Sstevel@tonic-gate 	(void) segkmem_create(&kvalloc);
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	if (kmem64_base) {
27904528Spaulsan 		(void) seg_attach(&kas, (caddr_t)kmem64_base,
27914528Spaulsan 		    (size_t)(kmem64_end - kmem64_base), &kmem64);
27924528Spaulsan 		(void) segkmem_create(&kmem64);
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	/*
27960Sstevel@tonic-gate 	 * We're about to map out /boot.  This is the beginning of the
27970Sstevel@tonic-gate 	 * system resource management transition. We can no longer
27980Sstevel@tonic-gate 	 * call into /boot for I/O or memory allocations.
27990Sstevel@tonic-gate 	 */
28000Sstevel@tonic-gate 	(void) seg_attach(&kas, kernelheap, ekernelheap - kernelheap, &kvseg);
28010Sstevel@tonic-gate 	(void) segkmem_create(&kvseg);
28020Sstevel@tonic-gate 	hblk_alloc_dynamic = 1;
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	/*
28050Sstevel@tonic-gate 	 * we need to preallocate pages for DR operations before enabling large
28060Sstevel@tonic-gate 	 * page kernel heap because of memseg_remap_init() hat_unload() hack.
28070Sstevel@tonic-gate 	 */
28080Sstevel@tonic-gate 	memseg_remap_init();
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	/* at this point we are ready to use large page heap */
28110Sstevel@tonic-gate 	segkmem_heap_lp_init();
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	(void) seg_attach(&kas, (caddr_t)SYSBASE32, SYSLIMIT32 - SYSBASE32,
28140Sstevel@tonic-gate 	    &kvseg32);
28150Sstevel@tonic-gate 	(void) segkmem_create(&kvseg32);
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	/*
28180Sstevel@tonic-gate 	 * Create a segment for the debugger.
28190Sstevel@tonic-gate 	 */
28203446Smrj 	(void) seg_attach(&kas, kdi_segdebugbase, kdi_segdebugsize, &kdebugseg);
28210Sstevel@tonic-gate 	(void) segkmem_create(&kdebugseg);
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	rw_exit(&kas.a_lock);
28240Sstevel@tonic-gate }
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate char obp_tte_str[] =
28270Sstevel@tonic-gate 	"h# %x constant MMU_PAGESHIFT "
28280Sstevel@tonic-gate 	"h# %x constant TTE8K "
28290Sstevel@tonic-gate 	"h# %x constant SFHME_SIZE "
28300Sstevel@tonic-gate 	"h# %x constant SFHME_TTE "
28310Sstevel@tonic-gate 	"h# %x constant HMEBLK_TAG "
28320Sstevel@tonic-gate 	"h# %x constant HMEBLK_NEXT "
28330Sstevel@tonic-gate 	"h# %x constant HMEBLK_MISC "
28340Sstevel@tonic-gate 	"h# %x constant HMEBLK_HME1 "
28350Sstevel@tonic-gate 	"h# %x constant NHMENTS "
28360Sstevel@tonic-gate 	"h# %x constant HBLK_SZMASK "
28370Sstevel@tonic-gate 	"h# %x constant HBLK_RANGE_SHIFT "
28380Sstevel@tonic-gate 	"h# %x constant HMEBP_HBLK "
28398187SPaul.Sandhu@Sun.COM 	"h# %x constant HMEBLK_ENDPA "
28400Sstevel@tonic-gate 	"h# %x constant HMEBUCKET_SIZE "
28410Sstevel@tonic-gate 	"h# %x constant HTAG_SFMMUPSZ "
28424528Spaulsan 	"h# %x constant HTAG_BSPAGE_SHIFT "
28434528Spaulsan 	"h# %x constant HTAG_REHASH_SHIFT "
28444528Spaulsan 	"h# %x constant SFMMU_INVALID_SHMERID "
28450Sstevel@tonic-gate 	"h# %x constant mmu_hashcnt "
28460Sstevel@tonic-gate 	"h# %p constant uhme_hash "
28470Sstevel@tonic-gate 	"h# %p constant khme_hash "
28480Sstevel@tonic-gate 	"h# %x constant UHMEHASH_SZ "
28490Sstevel@tonic-gate 	"h# %x constant KHMEHASH_SZ "
28502241Shuah 	"h# %p constant KCONTEXT "
28510Sstevel@tonic-gate 	"h# %p constant KHATID "
28520Sstevel@tonic-gate 	"h# %x constant ASI_MEM "
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate 	": PHYS-X@ ( phys -- data ) "
28550Sstevel@tonic-gate 	"   ASI_MEM spacex@ "
28560Sstevel@tonic-gate 	"; "
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 	": PHYS-W@ ( phys -- data ) "
28590Sstevel@tonic-gate 	"   ASI_MEM spacew@ "
28600Sstevel@tonic-gate 	"; "
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	": PHYS-L@ ( phys -- data ) "
28630Sstevel@tonic-gate 	"   ASI_MEM spaceL@ "
28640Sstevel@tonic-gate 	"; "
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	": TTE_PAGE_SHIFT ( ttesz -- hmeshift ) "
28670Sstevel@tonic-gate 	"   3 * MMU_PAGESHIFT + "
28680Sstevel@tonic-gate 	"; "
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 	": TTE_IS_VALID ( ttep -- flag ) "
28710Sstevel@tonic-gate 	"   PHYS-X@ 0< "
28720Sstevel@tonic-gate 	"; "
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 	": HME_HASH_SHIFT ( ttesz -- hmeshift ) "
28750Sstevel@tonic-gate 	"   dup TTE8K =  if "
28760Sstevel@tonic-gate 	"      drop HBLK_RANGE_SHIFT "
28770Sstevel@tonic-gate 	"   else "
28780Sstevel@tonic-gate 	"      TTE_PAGE_SHIFT "
28790Sstevel@tonic-gate 	"   then "
28800Sstevel@tonic-gate 	"; "
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	": HME_HASH_BSPAGE ( addr hmeshift -- bspage ) "
28830Sstevel@tonic-gate 	"   tuck >> swap MMU_PAGESHIFT - << "
28840Sstevel@tonic-gate 	"; "
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	": HME_HASH_FUNCTION ( sfmmup addr hmeshift -- hmebp ) "
28870Sstevel@tonic-gate 	"   >> over xor swap                    ( hash sfmmup ) "
28880Sstevel@tonic-gate 	"   KHATID <>  if                       ( hash ) "
28890Sstevel@tonic-gate 	"      UHMEHASH_SZ and                  ( bucket ) "
28900Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * uhme_hash +     ( hmebp ) "
28910Sstevel@tonic-gate 	"   else                                ( hash ) "
28920Sstevel@tonic-gate 	"      KHMEHASH_SZ and                  ( bucket ) "
28930Sstevel@tonic-gate 	"      HMEBUCKET_SIZE * khme_hash +     ( hmebp ) "
28940Sstevel@tonic-gate 	"   then                                ( hmebp ) "
28950Sstevel@tonic-gate 	"; "
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 	": HME_HASH_TABLE_SEARCH "
28980Sstevel@tonic-gate 	"       ( sfmmup hmebp hblktag --  sfmmup null | sfmmup hmeblkp ) "
28990Sstevel@tonic-gate 	"   >r hmebp_hblk + phys-x@ begin ( sfmmup hmeblkp ) ( r: hblktag ) "
29008187SPaul.Sandhu@Sun.COM 	"      dup HMEBLK_ENDPA <> if     ( sfmmup hmeblkp ) ( r: hblktag ) "
29010Sstevel@tonic-gate 	"         dup hmeblk_tag + phys-x@ r@ = if ( sfmmup hmeblkp )	  "
29020Sstevel@tonic-gate 	"	     dup hmeblk_tag + 8 + phys-x@ 2 pick = if		  "
29030Sstevel@tonic-gate 	"		  true 	( sfmmup hmeblkp true ) ( r: hblktag )	  "
29040Sstevel@tonic-gate 	"	     else						  "
29050Sstevel@tonic-gate 	"	     	  hmeblk_next + phys-x@ false 			  "
29060Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
29070Sstevel@tonic-gate 	"	     then  						  "
29080Sstevel@tonic-gate 	"	  else							  "
29090Sstevel@tonic-gate 	"	     hmeblk_next + phys-x@ false 			  "
29100Sstevel@tonic-gate 	"			( sfmmup hmeblkp false ) ( r: hblktag )   "
29110Sstevel@tonic-gate 	"	  then 							  "
29120Sstevel@tonic-gate 	"      else							  "
29138187SPaul.Sandhu@Sun.COM 	"         drop 0 true 						  "
29140Sstevel@tonic-gate 	"      then  							  "
29150Sstevel@tonic-gate 	"   until r> drop 						  "
29160Sstevel@tonic-gate 	"; "
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	": HME_HASH_TAG ( sfmmup rehash addr -- hblktag ) "
29194528Spaulsan 	"   over HME_HASH_SHIFT HME_HASH_BSPAGE  ( sfmmup rehash bspage ) "
29204528Spaulsan 	"   HTAG_BSPAGE_SHIFT <<		 ( sfmmup rehash htag-bspage )"
29214528Spaulsan 	"   swap HTAG_REHASH_SHIFT << or	 ( sfmmup htag-bspage-rehash )"
29224528Spaulsan 	"   SFMMU_INVALID_SHMERID or nip	 ( hblktag ) "
29230Sstevel@tonic-gate 	"; "
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate 	": HBLK_TO_TTEP ( hmeblkp addr -- ttep ) "
29260Sstevel@tonic-gate 	"   over HMEBLK_MISC + PHYS-L@ HBLK_SZMASK and  ( hmeblkp addr ttesz ) "
29270Sstevel@tonic-gate 	"   TTE8K =  if                            ( hmeblkp addr ) "
29280Sstevel@tonic-gate 	"      MMU_PAGESHIFT >> NHMENTS 1- and     ( hmeblkp hme-index ) "
29290Sstevel@tonic-gate 	"   else                                   ( hmeblkp addr ) "
29300Sstevel@tonic-gate 	"      drop 0                              ( hmeblkp 0 ) "
29310Sstevel@tonic-gate 	"   then                                   ( hmeblkp hme-index ) "
29320Sstevel@tonic-gate 	"   SFHME_SIZE * + HMEBLK_HME1 +           ( hmep ) "
29330Sstevel@tonic-gate 	"   SFHME_TTE +                            ( ttep ) "
29340Sstevel@tonic-gate 	"; "
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	": unix-tte ( addr cnum -- false | tte-data true ) "
29372241Shuah 	"    KCONTEXT = if                   ( addr ) "
29382241Shuah 	"	KHATID                       ( addr khatid ) "
29392241Shuah 	"    else                            ( addr ) "
29402241Shuah 	"       drop false exit              ( false ) "
29412241Shuah 	"    then "
29422241Shuah 	"      ( addr khatid ) "
29430Sstevel@tonic-gate 	"      mmu_hashcnt 1+ 1  do           ( addr sfmmup ) "
29440Sstevel@tonic-gate 	"         2dup swap i HME_HASH_SHIFT  "
29450Sstevel@tonic-gate 					"( addr sfmmup sfmmup addr hmeshift ) "
29460Sstevel@tonic-gate 	"         HME_HASH_FUNCTION           ( addr sfmmup hmebp ) "
29470Sstevel@tonic-gate 	"         over i 4 pick               "
29480Sstevel@tonic-gate 				"( addr sfmmup hmebp sfmmup rehash addr ) "
29490Sstevel@tonic-gate 	"         HME_HASH_TAG                ( addr sfmmup hmebp hblktag ) "
29500Sstevel@tonic-gate 	"         HME_HASH_TABLE_SEARCH       "
29510Sstevel@tonic-gate 					"( addr sfmmup { null | hmeblkp } ) "
29520Sstevel@tonic-gate 	"         ?dup  if                    ( addr sfmmup hmeblkp ) "
29530Sstevel@tonic-gate 	"            nip swap HBLK_TO_TTEP    ( ttep ) "
29540Sstevel@tonic-gate 	"            dup TTE_IS_VALID  if     ( valid-ttep ) "
2955*10271SJason.Beloro@Sun.COM 	"               PHYS-X@ true          ( tte-data true ) "
29560Sstevel@tonic-gate 	"            else                     ( invalid-tte ) "
29570Sstevel@tonic-gate 	"               drop false            ( false ) "
29580Sstevel@tonic-gate 	"            then                     ( false | tte-data true ) "
29590Sstevel@tonic-gate 	"            unloop exit              ( false | tte-data true ) "
29600Sstevel@tonic-gate 	"         then                        ( addr sfmmup ) "
29610Sstevel@tonic-gate 	"      loop                           ( addr sfmmup ) "
29620Sstevel@tonic-gate 	"      2drop false                    ( false ) "
29630Sstevel@tonic-gate 	"; "
29640Sstevel@tonic-gate ;
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate void
29670Sstevel@tonic-gate create_va_to_tte(void)
29680Sstevel@tonic-gate {
29690Sstevel@tonic-gate 	char *bp;
29700Sstevel@tonic-gate 	extern int khmehash_num, uhmehash_num;
29710Sstevel@tonic-gate 	extern struct hmehash_bucket *khme_hash, *uhme_hash;
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate #define	OFFSET(type, field)	((uintptr_t)(&((type *)0)->field))
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	bp = (char *)kobj_zalloc(MMU_PAGESIZE, KM_SLEEP);
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 	/*
29780Sstevel@tonic-gate 	 * Teach obp how to parse our sw ttes.
29790Sstevel@tonic-gate 	 */
29800Sstevel@tonic-gate 	(void) sprintf(bp, obp_tte_str,
29810Sstevel@tonic-gate 	    MMU_PAGESHIFT,
29820Sstevel@tonic-gate 	    TTE8K,
29830Sstevel@tonic-gate 	    sizeof (struct sf_hment),
29840Sstevel@tonic-gate 	    OFFSET(struct sf_hment, hme_tte),
29850Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_tag),
29860Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_nextpa),
29870Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_misc),
29880Sstevel@tonic-gate 	    OFFSET(struct hme_blk, hblk_hme),
29890Sstevel@tonic-gate 	    NHMENTS,
29900Sstevel@tonic-gate 	    HBLK_SZMASK,
29910Sstevel@tonic-gate 	    HBLK_RANGE_SHIFT,
29920Sstevel@tonic-gate 	    OFFSET(struct hmehash_bucket, hmeh_nextpa),
29938187SPaul.Sandhu@Sun.COM 	    HMEBLK_ENDPA,
29940Sstevel@tonic-gate 	    sizeof (struct hmehash_bucket),
29950Sstevel@tonic-gate 	    HTAG_SFMMUPSZ,
29964528Spaulsan 	    HTAG_BSPAGE_SHIFT,
29974528Spaulsan 	    HTAG_REHASH_SHIFT,
29984528Spaulsan 	    SFMMU_INVALID_SHMERID,
29990Sstevel@tonic-gate 	    mmu_hashcnt,
30000Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)uhme_hash),
30010Sstevel@tonic-gate 	    (caddr_t)va_to_pa((caddr_t)khme_hash),
30020Sstevel@tonic-gate 	    UHMEHASH_SZ,
30030Sstevel@tonic-gate 	    KHMEHASH_SZ,
30042241Shuah 	    KCONTEXT,
30050Sstevel@tonic-gate 	    KHATID,
3006*10271SJason.Beloro@Sun.COM 	    ASI_MEM);
30070Sstevel@tonic-gate 	prom_interpret(bp, 0, 0, 0, 0, 0);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 	kobj_free(bp, MMU_PAGESIZE);
30100Sstevel@tonic-gate }
30110Sstevel@tonic-gate 
30120Sstevel@tonic-gate void
30130Sstevel@tonic-gate install_va_to_tte(void)
30140Sstevel@tonic-gate {
30150Sstevel@tonic-gate 	/*
30160Sstevel@tonic-gate 	 * advise prom that he can use unix-tte
30170Sstevel@tonic-gate 	 */
30180Sstevel@tonic-gate 	prom_interpret("' unix-tte is va>tte-data", 0, 0, 0, 0, 0);
30190Sstevel@tonic-gate }
30200Sstevel@tonic-gate 
30211253Slq150181 /*
30223928Slq150181  * Here we add "device-type=console" for /os-io node, for currently
30233928Slq150181  * our kernel console output only supports displaying text and
30243928Slq150181  * performing cursor-positioning operations (through kernel framebuffer
30253928Slq150181  * driver) and it doesn't support other functionalities required for a
30263928Slq150181  * standard "display" device as specified in 1275 spec. The main missing
30273928Slq150181  * interface defined by the 1275 spec is "draw-logo".
30283928Slq150181  * also see the comments above prom_stdout_is_framebuffer().
30291253Slq150181  */
30300Sstevel@tonic-gate static char *create_node =
30311492Slq150181 	"\" /\" find-device "
30320Sstevel@tonic-gate 	"new-device "
30330Sstevel@tonic-gate 	"\" os-io\" device-name "
30343928Slq150181 	"\" "OBP_DISPLAY_CONSOLE"\" device-type "
30350Sstevel@tonic-gate 	": cb-r/w  ( adr,len method$ -- #read/#written ) "
30360Sstevel@tonic-gate 	"   2>r swap 2 2r> ['] $callback  catch  if "
30370Sstevel@tonic-gate 	"      2drop 3drop 0 "
30380Sstevel@tonic-gate 	"   then "
30390Sstevel@tonic-gate 	"; "
30400Sstevel@tonic-gate 	": read ( adr,len -- #read ) "
30410Sstevel@tonic-gate 	"       \" read\" ['] cb-r/w catch  if  2drop 2drop -2 exit then "
30420Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
30430Sstevel@tonic-gate 	"       ?dup  if "
30440Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
30450Sstevel@tonic-gate 	"       else "
30460Sstevel@tonic-gate 	"               -2 "
30471492Slq150181 	"       then "
30480Sstevel@tonic-gate 	";    "
30490Sstevel@tonic-gate 	": write ( adr,len -- #written ) "
30500Sstevel@tonic-gate 	"       \" write\" ['] cb-r/w catch  if  2drop 2drop 0 exit  then "
30510Sstevel@tonic-gate 	"       ( retN ... ret1 N ) "
30520Sstevel@tonic-gate 	"       ?dup  if "
30530Sstevel@tonic-gate 	"               swap >r 1-  0  ?do  drop  loop  r> "
30540Sstevel@tonic-gate 	"        else "
30550Sstevel@tonic-gate 	"               0 "
30560Sstevel@tonic-gate 	"       then "
30570Sstevel@tonic-gate 	"; "
30580Sstevel@tonic-gate 	": poll-tty ( -- ) ; "
30590Sstevel@tonic-gate 	": install-abort  ( -- )  ['] poll-tty d# 10 alarm ; "
30600Sstevel@tonic-gate 	": remove-abort ( -- )  ['] poll-tty 0 alarm ; "
30610Sstevel@tonic-gate 	": cb-give/take ( $method -- ) "
30620Sstevel@tonic-gate 	"       0 -rot ['] $callback catch  ?dup  if "
30630Sstevel@tonic-gate 	"               >r 2drop 2drop r> throw "
30640Sstevel@tonic-gate 	"       else "
30650Sstevel@tonic-gate 	"               0  ?do  drop  loop "
30660Sstevel@tonic-gate 	"       then "
30670Sstevel@tonic-gate 	"; "
30680Sstevel@tonic-gate 	": give ( -- )  \" exit-input\" cb-give/take ; "
30690Sstevel@tonic-gate 	": take ( -- )  \" enter-input\" cb-give/take ; "
30700Sstevel@tonic-gate 	": open ( -- ok? )  true ; "
30710Sstevel@tonic-gate 	": close ( -- ) ; "
30720Sstevel@tonic-gate 	"finish-device "
30730Sstevel@tonic-gate 	"device-end ";
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate /*
30761253Slq150181  * Create the OBP input/output node (FCode serial driver).
30771253Slq150181  * It is needed for both USB console keyboard and for
30781253Slq150181  * the kernel terminal emulator.  It is too early to check for a
30791253Slq150181  * kernel console compatible framebuffer now, so we create this
30801253Slq150181  * so that we're ready if we need to enable kernel terminal emulation.
30810Sstevel@tonic-gate  *
30821253Slq150181  * When the USB software takes over the input device at the time
30831253Slq150181  * consconfig runs, OBP's stdin is redirected to this node.
30841253Slq150181  * Whenever the FORTH user interface is used after this switch,
30851253Slq150181  * the node will call back into the kernel for console input.
30861253Slq150181  * If a serial device such as ttya or a UART with a Type 5 keyboard
30871253Slq150181  * attached is used, OBP takes over the serial device when the system
30881253Slq150181  * goes to the debugger after the system is booted.  This sharing
30891253Slq150181  * of the relatively simple serial device is difficult but possible.
30901253Slq150181  * Sharing the USB host controller is impossible due its complexity.
30911253Slq150181  *
30921253Slq150181  * Similarly to USB keyboard input redirection, after consconfig_dacf
30931253Slq150181  * configures a kernel console framebuffer as the standard output
30941253Slq150181  * device, OBP's stdout is switched to to vector through the
30951253Slq150181  * /os-io node into the kernel terminal emulator.
30960Sstevel@tonic-gate  */
30970Sstevel@tonic-gate static void
30981253Slq150181 startup_create_io_node(void)
30990Sstevel@tonic-gate {
31001253Slq150181 	prom_interpret(create_node, 0, 0, 0, 0, 0);
31010Sstevel@tonic-gate }
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate static void
31050Sstevel@tonic-gate do_prom_version_check(void)
31060Sstevel@tonic-gate {
31070Sstevel@tonic-gate 	int i;
3108789Sahrens 	pnode_t node;
31090Sstevel@tonic-gate 	char buf[64];
31100Sstevel@tonic-gate 	static char drev[] = "Down-rev firmware detected%s\n"
31114528Spaulsan 	    "\tPlease upgrade to the following minimum version:\n"
31124528Spaulsan 	    "\t\t%s\n";
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 	i = prom_version_check(buf, sizeof (buf), &node);
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 	if (i == PROM_VER64_OK)
31170Sstevel@tonic-gate 		return;
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	if (i == PROM_VER64_UPGRADE) {
31200Sstevel@tonic-gate 		cmn_err(CE_WARN, drev, "", buf);
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate #ifdef	DEBUG
31230Sstevel@tonic-gate 		prom_enter_mon();	/* Type 'go' to continue */
31240Sstevel@tonic-gate 		cmn_err(CE_WARN, "Booting with down-rev firmware\n");
31250Sstevel@tonic-gate 		return;
31260Sstevel@tonic-gate #else
31270Sstevel@tonic-gate 		halt(0);
31280Sstevel@tonic-gate #endif
31290Sstevel@tonic-gate 	}
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	/*
31320Sstevel@tonic-gate 	 * The other possibility is that this is a server running
31330Sstevel@tonic-gate 	 * good firmware, but down-rev firmware was detected on at
31340Sstevel@tonic-gate 	 * least one other cpu board. We just complain if we see
31350Sstevel@tonic-gate 	 * that.
31360Sstevel@tonic-gate 	 */
31370Sstevel@tonic-gate 	cmn_err(CE_WARN, drev, " on one or more CPU boards", buf);
31380Sstevel@tonic-gate }
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate /*
31420Sstevel@tonic-gate  * Must be defined in platform dependent code.
31430Sstevel@tonic-gate  */
31440Sstevel@tonic-gate extern caddr_t modtext;
31450Sstevel@tonic-gate extern size_t modtext_sz;
31460Sstevel@tonic-gate extern caddr_t moddata;
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate #define	HEAPTEXT_ARENA(addr)	\
31490Sstevel@tonic-gate 	((uintptr_t)(addr) < KERNELBASE + 2 * MMU_PAGESIZE4M ? 0 : \
31500Sstevel@tonic-gate 	(((uintptr_t)(addr) - HEAPTEXT_BASE) / \
31510Sstevel@tonic-gate 	(HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) + 1))
31520Sstevel@tonic-gate 
31530Sstevel@tonic-gate #define	HEAPTEXT_OVERSIZED(addr)	\
31540Sstevel@tonic-gate 	((uintptr_t)(addr) >= HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE)
31550Sstevel@tonic-gate 
31567428SJonathan.Haslam@Sun.COM #define	HEAPTEXT_IN_NUCLEUSDATA(addr) \
31577428SJonathan.Haslam@Sun.COM 	(((uintptr_t)(addr) >= KERNELBASE + 2 * MMU_PAGESIZE4M) && \
31587428SJonathan.Haslam@Sun.COM 	((uintptr_t)(addr) < KERNELBASE + 3 * MMU_PAGESIZE4M))
31597428SJonathan.Haslam@Sun.COM 
31600Sstevel@tonic-gate vmem_t *texthole_source[HEAPTEXT_NARENAS];
31610Sstevel@tonic-gate vmem_t *texthole_arena[HEAPTEXT_NARENAS];
31620Sstevel@tonic-gate kmutex_t texthole_lock;
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate char kern_bootargs[OBP_MAXPATHLEN];
31657656SSherry.Moore@Sun.COM char kern_bootfile[OBP_MAXPATHLEN];
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate void
31680Sstevel@tonic-gate kobj_vmem_init(vmem_t **text_arena, vmem_t **data_arena)
31690Sstevel@tonic-gate {
31700Sstevel@tonic-gate 	uintptr_t addr, limit;
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	addr = HEAPTEXT_BASE;
31730Sstevel@tonic-gate 	limit = addr + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE;
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 	/*
31760Sstevel@tonic-gate 	 * Before we initialize the text_arena, we want to punch holes in the
31770Sstevel@tonic-gate 	 * underlying heaptext_arena.  This guarantees that for any text
31780Sstevel@tonic-gate 	 * address we can find a text hole less than HEAPTEXT_MAPPED away.
31790Sstevel@tonic-gate 	 */
31800Sstevel@tonic-gate 	for (; addr + HEAPTEXT_UNMAPPED <= limit;
31810Sstevel@tonic-gate 	    addr += HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED) {
31820Sstevel@tonic-gate 		(void) vmem_xalloc(heaptext_arena, HEAPTEXT_UNMAPPED, PAGESIZE,
31830Sstevel@tonic-gate 		    0, 0, (void *)addr, (void *)(addr + HEAPTEXT_UNMAPPED),
31840Sstevel@tonic-gate 		    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
31850Sstevel@tonic-gate 	}
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 	/*
31880Sstevel@tonic-gate 	 * Allocate one page at the oversize to break up the text region
31890Sstevel@tonic-gate 	 * from the oversized region.
31900Sstevel@tonic-gate 	 */
31910Sstevel@tonic-gate 	(void) vmem_xalloc(heaptext_arena, PAGESIZE, PAGESIZE, 0, 0,
31920Sstevel@tonic-gate 	    (void *)limit, (void *)(limit + PAGESIZE),
31930Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
31940Sstevel@tonic-gate 
31953791Skchow 	*text_arena = vmem_create("module_text", modtext_sz ? modtext : NULL,
31963791Skchow 	    modtext_sz, sizeof (uintptr_t), segkmem_alloc, segkmem_free,
31970Sstevel@tonic-gate 	    heaptext_arena, 0, VM_SLEEP);
31980Sstevel@tonic-gate 	*data_arena = vmem_create("module_data", moddata, MODDATA, 1,
31990Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap32_arena, 0, VM_SLEEP);
32000Sstevel@tonic-gate }
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate caddr_t
32030Sstevel@tonic-gate kobj_text_alloc(vmem_t *arena, size_t size)
32040Sstevel@tonic-gate {
32050Sstevel@tonic-gate 	caddr_t rval, better;
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	/*
32080Sstevel@tonic-gate 	 * First, try a sleeping allocation.
32090Sstevel@tonic-gate 	 */
32100Sstevel@tonic-gate 	rval = vmem_alloc(arena, size, VM_SLEEP | VM_BESTFIT);
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 	if (size >= HEAPTEXT_MAPPED || !HEAPTEXT_OVERSIZED(rval))
32130Sstevel@tonic-gate 		return (rval);
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 	/*
32160Sstevel@tonic-gate 	 * We didn't get the area that we wanted.  We're going to try to do an
32170Sstevel@tonic-gate 	 * allocation with explicit constraints.
32180Sstevel@tonic-gate 	 */
32190Sstevel@tonic-gate 	better = vmem_xalloc(arena, size, sizeof (uintptr_t), 0, 0, NULL,
32200Sstevel@tonic-gate 	    (void *)(HEAPTEXT_BASE + HEAPTEXT_SIZE - HEAPTEXT_OVERSIZE),
32210Sstevel@tonic-gate 	    VM_NOSLEEP | VM_BESTFIT);
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 	if (better != NULL) {
32240Sstevel@tonic-gate 		/*
32250Sstevel@tonic-gate 		 * That worked.  Free our first attempt and return.
32260Sstevel@tonic-gate 		 */
32270Sstevel@tonic-gate 		vmem_free(arena, rval, size);
32280Sstevel@tonic-gate 		return (better);
32290Sstevel@tonic-gate 	}
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	/*
32320Sstevel@tonic-gate 	 * That didn't work; we'll have to return our first attempt.
32330Sstevel@tonic-gate 	 */
32340Sstevel@tonic-gate 	return (rval);
32350Sstevel@tonic-gate }
32360Sstevel@tonic-gate 
32370Sstevel@tonic-gate caddr_t
32380Sstevel@tonic-gate kobj_texthole_alloc(caddr_t addr, size_t size)
32390Sstevel@tonic-gate {
32400Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
32410Sstevel@tonic-gate 	char c[30];
32420Sstevel@tonic-gate 	uintptr_t base;
32430Sstevel@tonic-gate 
32447428SJonathan.Haslam@Sun.COM 	if (HEAPTEXT_OVERSIZED(addr) || HEAPTEXT_IN_NUCLEUSDATA(addr)) {
32450Sstevel@tonic-gate 		/*
32467428SJonathan.Haslam@Sun.COM 		 * If this is an oversized allocation or it is allocated in
32477428SJonathan.Haslam@Sun.COM 		 * the nucleus data page, there is no text hole available for
32487428SJonathan.Haslam@Sun.COM 		 * it; return NULL.
32490Sstevel@tonic-gate 		 */
32500Sstevel@tonic-gate 		return (NULL);
32510Sstevel@tonic-gate 	}
32520Sstevel@tonic-gate 
32530Sstevel@tonic-gate 	mutex_enter(&texthole_lock);
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	if (texthole_arena[arena] == NULL) {
32560Sstevel@tonic-gate 		ASSERT(texthole_source[arena] == NULL);
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate 		if (arena == 0) {
32590Sstevel@tonic-gate 			texthole_source[0] = vmem_create("module_text_holesrc",
32600Sstevel@tonic-gate 			    (void *)(KERNELBASE + MMU_PAGESIZE4M),
32610Sstevel@tonic-gate 			    MMU_PAGESIZE4M, PAGESIZE, NULL, NULL, NULL,
32620Sstevel@tonic-gate 			    0, VM_SLEEP);
32630Sstevel@tonic-gate 		} else {
32640Sstevel@tonic-gate 			base = HEAPTEXT_BASE +
32650Sstevel@tonic-gate 			    (arena - 1) * (HEAPTEXT_MAPPED + HEAPTEXT_UNMAPPED);
32660Sstevel@tonic-gate 
32670Sstevel@tonic-gate 			(void) snprintf(c, sizeof (c),
32680Sstevel@tonic-gate 			    "heaptext_holesrc_%d", arena);
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate 			texthole_source[arena] = vmem_create(c, (void *)base,
32710Sstevel@tonic-gate 			    HEAPTEXT_UNMAPPED, PAGESIZE, NULL, NULL, NULL,
32720Sstevel@tonic-gate 			    0, VM_SLEEP);
32730Sstevel@tonic-gate 		}
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 		(void) snprintf(c, sizeof (c), "heaptext_hole_%d", arena);
32760Sstevel@tonic-gate 
32770Sstevel@tonic-gate 		texthole_arena[arena] = vmem_create(c, NULL, 0,
32780Sstevel@tonic-gate 		    sizeof (uint32_t), segkmem_alloc_permanent, segkmem_free,
32790Sstevel@tonic-gate 		    texthole_source[arena], 0, VM_SLEEP);
32800Sstevel@tonic-gate 	}
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 	mutex_exit(&texthole_lock);
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
32850Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
32860Sstevel@tonic-gate 	return (vmem_alloc(texthole_arena[arena], size,
32870Sstevel@tonic-gate 	    VM_BESTFIT | VM_NOSLEEP));
32880Sstevel@tonic-gate }
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate void
32910Sstevel@tonic-gate kobj_texthole_free(caddr_t addr, size_t size)
32920Sstevel@tonic-gate {
32930Sstevel@tonic-gate 	int arena = HEAPTEXT_ARENA(addr);
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 	ASSERT(arena >= 0 && arena < HEAPTEXT_NARENAS);
32960Sstevel@tonic-gate 	ASSERT(texthole_arena[arena] != NULL);
32970Sstevel@tonic-gate 	vmem_free(texthole_arena[arena], addr, size);
32980Sstevel@tonic-gate }
32995974Sjm22469 
33005974Sjm22469 void
33015974Sjm22469 release_bootstrap(void)
33025974Sjm22469 {
33035974Sjm22469 	if (&cif_init)
33045974Sjm22469 		cif_init();
33055974Sjm22469 }
3306