xref: /netbsd-src/sys/arch/riscv/include/vmparam.h (revision 75b842b84762e0251223b719bf77fbef2e651bf6)
1*75b842b8Sskrll /*	$NetBSD: vmparam.h,v 1.14 2023/05/07 12:41:48 skrll Exp $	*/
26cf6fe02Smatt 
36cf6fe02Smatt /*-
4e0a18223Sskrll  * Copyright (c) 2014, 2020 The NetBSD Foundation, Inc.
56cf6fe02Smatt  * All rights reserved.
66cf6fe02Smatt  *
76cf6fe02Smatt  * This code is derived from software contributed to The NetBSD Foundation
8e0a18223Sskrll  * by Matt Thomas of 3am Software Foundry, and Nick Hudson.
96cf6fe02Smatt  *
106cf6fe02Smatt  * Redistribution and use in source and binary forms, with or without
116cf6fe02Smatt  * modification, are permitted provided that the following conditions
126cf6fe02Smatt  * are met:
136cf6fe02Smatt  * 1. Redistributions of source code must retain the above copyright
146cf6fe02Smatt  *    notice, this list of conditions and the following disclaimer.
156cf6fe02Smatt  * 2. Redistributions in binary form must reproduce the above copyright
166cf6fe02Smatt  *    notice, this list of conditions and the following disclaimer in the
176cf6fe02Smatt  *    documentation and/or other materials provided with the distribution.
186cf6fe02Smatt  *
196cf6fe02Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206cf6fe02Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216cf6fe02Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226cf6fe02Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236cf6fe02Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246cf6fe02Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256cf6fe02Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266cf6fe02Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276cf6fe02Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286cf6fe02Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296cf6fe02Smatt  * POSSIBILITY OF SUCH DAMAGE.
306cf6fe02Smatt  */
316cf6fe02Smatt 
326cf6fe02Smatt #ifndef _RISCV_VMPARAM_H_
336cf6fe02Smatt #define	_RISCV_VMPARAM_H_
346cf6fe02Smatt 
356cf6fe02Smatt #include <riscv/param.h>
366cf6fe02Smatt 
376cf6fe02Smatt #ifdef _KERNEL_OPT
386cf6fe02Smatt #include "opt_multiprocessor.h"
396cf6fe02Smatt #endif
406cf6fe02Smatt 
416cf6fe02Smatt /*
426cf6fe02Smatt  * Machine dependent VM constants for RISCV.
436cf6fe02Smatt  */
446cf6fe02Smatt 
45e0a18223Sskrll /*
46e0a18223Sskrll  * We use a 4K page on both RV64 and RV32 systems.
47e0a18223Sskrll  * Override PAGE_* definitions to compile-time constants.
48e0a18223Sskrll  */
496cf6fe02Smatt #define	PAGE_SHIFT	PGSHIFT
506cf6fe02Smatt #define	PAGE_SIZE	(1 << PAGE_SHIFT)
516cf6fe02Smatt #define	PAGE_MASK	(PAGE_SIZE - 1)
526cf6fe02Smatt 
536cf6fe02Smatt /*
546cf6fe02Smatt  * USRSTACK is the top (end) of the user stack.
556cf6fe02Smatt  *
566cf6fe02Smatt  * USRSTACK needs to start a page below the maxuser address so that a memory
576cf6fe02Smatt  * access with a maximum displacement (0x7ff) won't cross into the kernel's
586cf6fe02Smatt  * address space.  We use PAGE_SIZE instead of 0x800 since these need to be
596cf6fe02Smatt  * page-aligned.
606cf6fe02Smatt  */
616cf6fe02Smatt #define	USRSTACK	(VM_MAXUSER_ADDRESS - PAGE_SIZE) /* Start of user stack */
62e6f1ec26Smrg #define	USRSTACK32	((uint32_t)VM_MAXUSER_ADDRESS32 - PAGE_SIZE)
636cf6fe02Smatt 
646cf6fe02Smatt /*
656cf6fe02Smatt  * Virtual memory related constants, all in bytes
666cf6fe02Smatt  */
676cf6fe02Smatt #ifndef MAXTSIZ
686cf6fe02Smatt #define	MAXTSIZ		(128*1024*1024)		/* max text size */
696cf6fe02Smatt #endif
706cf6fe02Smatt #ifndef DFLDSIZ
716cf6fe02Smatt #define	DFLDSIZ		(256*1024*1024)		/* initial data size limit */
726cf6fe02Smatt #endif
736cf6fe02Smatt #ifndef MAXDSIZ
746cf6fe02Smatt #define	MAXDSIZ		(1536*1024*1024)	/* max data size */
756cf6fe02Smatt #endif
766cf6fe02Smatt #ifndef	DFLSSIZ
772bf5a486Ssimonb #define	DFLSSIZ		(4*1024*1024)		/* initial stack size limit */
786cf6fe02Smatt #endif
796cf6fe02Smatt #ifndef	MAXSSIZ
806cf6fe02Smatt #define	MAXSSIZ		(120*1024*1024)		/* max stack size */
816cf6fe02Smatt #endif
826cf6fe02Smatt 
836cf6fe02Smatt /*
846cf6fe02Smatt  * Virtual memory related constants, all in bytes
856cf6fe02Smatt  */
866cf6fe02Smatt #ifndef DFLDSIZ32
876cf6fe02Smatt #define	DFLDSIZ32	DFLDSIZ			/* initial data size limit */
886cf6fe02Smatt #endif
896cf6fe02Smatt #ifndef MAXDSIZ32
906cf6fe02Smatt #define	MAXDSIZ32	MAXDSIZ			/* max data size */
916cf6fe02Smatt #endif
926cf6fe02Smatt #ifndef	DFLSSIZ32
936cf6fe02Smatt #define	DFLSSIZ32	DFLTSIZ			/* initial stack size limit */
946cf6fe02Smatt #endif
956cf6fe02Smatt #ifndef	MAXSSIZ32
966cf6fe02Smatt #define	MAXSSIZ32	MAXSSIZ			/* max stack size */
976cf6fe02Smatt #endif
986cf6fe02Smatt 
996cf6fe02Smatt /*
1006cf6fe02Smatt  * PTEs for mapping user space into the kernel for phyio operations.
1016cf6fe02Smatt  * The default PTE number is enough to cover 8 disks * MAXBSIZE.
1026cf6fe02Smatt  */
1036cf6fe02Smatt #ifndef USRIOSIZE
1046cf6fe02Smatt #define USRIOSIZE	(MAXBSIZE/PAGE_SIZE * 8)
1056cf6fe02Smatt #endif
1066cf6fe02Smatt 
1075bd7eba2Smaxv /*
1085bd7eba2Smaxv  * User/kernel map constants.
1095bd7eba2Smaxv  */
110*75b842b8Sskrll #define VM_MIN_ADDRESS		((vaddr_t)PAGE_SIZE)
1110932c5e1Sskrll #ifdef _LP64	/* Sv39 / Sv48 / Sv57 */
112e0a18223Sskrll /*
113*75b842b8Sskrll  * SV39 gives 1 << (39 - 1) address space to kernel and same to userland.
114*75b842b8Sskrll  * This is 256GiB each. Split the kernel space in two and use the top half
115*75b842b8Sskrll  * for direct map.
116*75b842b8Sskrll  *
117e0a18223Sskrll  * kernel virtual space layout:
118e0a18223Sskrll  *   0xffff_ffc0_0000_0000  -   64GiB  KERNEL VM Space (inc. text/data/bss)
119e0a18223Sskrll  *  (0xffff_ffc0_4000_0000      +1GiB) KERNEL VM start of KVA
120e0a18223Sskrll  *  (0xffff_ffd0_0000_0000      64GiB) reserved
121e0a18223Sskrll  *   0xffff_ffe0_0000_0000  -  128GiB  direct mapping
122e0a18223Sskrll  */
1235bd7eba2Smaxv #define VM_MAXUSER_ADDRESS	((vaddr_t)0x0000004000000000 - 16 * PAGE_SIZE)
1242c41f311Sskrll #define VM_MIN_KERNEL_ADDRESS	((vaddr_t)0xffffffc000000000)
125e0a18223Sskrll #define VM_MAX_KERNEL_ADDRESS	((vaddr_t)0xffffffd000000000)
126e0a18223Sskrll 
1275bd7eba2Smaxv #else		/* Sv32 */
128*75b842b8Sskrll /*
129*75b842b8Sskrll  * kernel virtual space layout:
130*75b842b8Sskrll  *   0x8000_0000  -   64GiB  KERNEL VM Space (inc. text/data/bss)
131*75b842b8Sskrll  *  (0x4000_0000      +1GiB) KERNEL VM start of KVA
132*75b842b8Sskrll  *  (0x0000_0000      64GiB) reserved
133*75b842b8Sskrll  */
134*75b842b8Sskrll 
135*75b842b8Sskrll /*
136*75b842b8Sskrll  * kernel virtual space layout without direct map (common case)
137*75b842b8Sskrll  *
138*75b842b8Sskrll  *   0x8000_0000 -  256MB kernel text/data/bss
139*75b842b8Sskrll  *   0x9000_0000 - 1536MB Kernel VM Space
140*75b842b8Sskrll  *   0xf000_0000 -  256MB IO
141*75b842b8Sskrll  *
142*75b842b8Sskrll  * kernel virtual space layout with KASAN
143*75b842b8Sskrll  *
144*75b842b8Sskrll  *   0x8000_0000 -  256MB kernel text/data/bss
145*75b842b8Sskrll  *   0x9000_0000 -  768MB Kernel VM Space
146*75b842b8Sskrll  *   0xc000_0000 -  128MB (KASAN SHADOW MAP)
147*75b842b8Sskrll  *   0xc800_0000 -  640MB (spare)
148*75b842b8Sskrll  *   0xf000_0000 -  256MB IO
149*75b842b8Sskrll  *
150*75b842b8Sskrll  * kernel virtual space layout with direct map (1GB limited)
151*75b842b8Sskrll  *   0x8000_0000 - 1024MB kernel text/data/bss and direct map start
152*75b842b8Sskrll  *   0xc000_0000 -  768MB Kernel VM Space
153*75b842b8Sskrll  *   0xf000_0000 -  256MB IO
154*75b842b8Sskrll  *
155*75b842b8Sskrll  */
156*75b842b8Sskrll 
157*75b842b8Sskrll 
158*75b842b8Sskrll 
159*75b842b8Sskrll #define VM_MAXUSER_ADDRESS	((vaddr_t)-0x7fffffff-1)/* 0xffff_ffff_8000_0000 */
160*75b842b8Sskrll #define VM_MIN_KERNEL_ADDRESS	((vaddr_t)-0x7fffffff-1)/* 0xffff_ffff_8000_0000 */
161*75b842b8Sskrll #define VM_MAX_KERNEL_ADDRESS	((vaddr_t)-0x10000000)	/* 0xffff_ffff_f000_0000 */
162e0a18223Sskrll 
1636cf6fe02Smatt #endif
16417949b8dSskrll #define VM_KERNEL_BASE		VM_MIN_KERNEL_ADDRESS
16517949b8dSskrll #define VM_KERNEL_SIZE		0x2000000	/* 32 MiB (8 / 16 megapages) */
16617949b8dSskrll #define VM_KERNEL_DTB_BASE	(VM_KERNEL_BASE + VM_KERNEL_SIZE)
167ce041175Sskrll #define VM_KERNEL_DTB_SIZE	0x1000000	/* 16 MiB (4 / 8 megapages) */
168ce041175Sskrll #define VM_KERNEL_IO_BASE	(VM_KERNEL_DTB_BASE + VM_KERNEL_DTB_SIZE)
169ce041175Sskrll #define VM_KERNEL_IO_SIZE	0x1000000	/* 16 MiB (4 / 8 megapages) */
17017949b8dSskrll 
171ce041175Sskrll #define VM_KERNEL_RESERVED	(VM_KERNEL_SIZE + VM_KERNEL_DTB_SIZE + VM_KERNEL_IO_SIZE)
17217949b8dSskrll 
17317949b8dSskrll #define VM_KERNEL_VM_BASE	(VM_MIN_KERNEL_ADDRESS + VM_KERNEL_RESERVED)
17417949b8dSskrll #define VM_KERNEL_VM_SIZE	(VM_MAX_KERNEL_ADDRESS - VM_KERNEL_VM_BASE)
175e0a18223Sskrll 
1766cf6fe02Smatt #define VM_MAX_ADDRESS		VM_MAXUSER_ADDRESS
177e6f1ec26Smrg #define VM_MAXUSER_ADDRESS32	((vaddr_t)(1UL << 31))/* 0x0000000080000000 */
1786cf6fe02Smatt 
179e0a18223Sskrll #ifdef _LP64
180e0a18223Sskrll /*
181e0a18223Sskrll  * Since we have the address space, we map all of physical memory (RAM)
182dfbce3f9Ssimonb  * using gigapages on SV39, terapages on SV48 and petapages on SV57.
183e0a18223Sskrll  */
184e0a18223Sskrll #define RISCV_DIRECTMAP_MASK	((vaddr_t) 0xffffffe000000000L)
185dfbce3f9Ssimonb #define RISCV_DIRECTMAP_SIZE	(-RISCV_DIRECTMAP_MASK - PAGE_SIZE)	/* 128GiB */
186e0a18223Sskrll #define RISCV_DIRECTMAP_START	RISCV_DIRECTMAP_MASK
187e0a18223Sskrll #define RISCV_DIRECTMAP_END	(RISCV_DIRECTMAP_START + RISCV_DIRECTMAP_SIZE)
188*75b842b8Sskrll #define RISCV_DIRECTMAP_P(va)	(((vaddr_t) (va) & RISCV_DIRECTMAP_MASK) == RISCV_DIRECTMAP_MASK)
189e0a18223Sskrll #define RISCV_PA_TO_KVA(pa)	((vaddr_t) ((pa) | RISCV_DIRECTMAP_START))
190e0a18223Sskrll #define RISCV_KVA_TO_PA(va)	((paddr_t) ((va) & ~RISCV_DIRECTMAP_MASK))
191e0a18223Sskrll #endif
192e0a18223Sskrll 
1936cf6fe02Smatt /*
1946cf6fe02Smatt  * The address to which unspecified mapping requests default
1956cf6fe02Smatt  */
1966cf6fe02Smatt #define __USE_TOPDOWN_VM
1976cf6fe02Smatt 
1986cf6fe02Smatt #define VM_DEFAULT_ADDRESS_TOPDOWN(da, sz) \
199b77121f1Sjoerg     trunc_page(USRSTACK - MAXSSIZ - (sz) - user_stack_guard_size)
2006cf6fe02Smatt #define VM_DEFAULT_ADDRESS_BOTTOMUP(da, sz) \
2016cf6fe02Smatt     round_page((vaddr_t)(da) + (vsize_t)maxdmap)
2026cf6fe02Smatt 
2036cf6fe02Smatt #define VM_DEFAULT_ADDRESS32_TOPDOWN(da, sz) \
2040bc4a2daSjoerg     trunc_page(USRSTACK32 - MAXSSIZ32 - (sz) - user_stack_guard_size)
2056cf6fe02Smatt #define VM_DEFAULT_ADDRESS32_BOTTOMUP(da, sz) \
2066cf6fe02Smatt     round_page((vaddr_t)(da) + (vsize_t)MAXDSIZ32)
2076cf6fe02Smatt 
2086cf6fe02Smatt /* virtual sizes (bytes) for various kernel submaps */
2096cf6fe02Smatt #define VM_PHYS_SIZE		(USRIOSIZE*PAGE_SIZE)
2106cf6fe02Smatt 
211*75b842b8Sskrll /*
212*75b842b8Sskrll  * max number of non-contig chunks of physical RAM you can have
213*75b842b8Sskrll  */
214*75b842b8Sskrll #define VM_PHYSSEG_MAX		64
215*75b842b8Sskrll 
216*75b842b8Sskrll /*
217*75b842b8Sskrll  * when converting a physical address to a vm_page structure, we
218*75b842b8Sskrll  * want to use a binary search on the chunks of physical memory
219*75b842b8Sskrll  * to find our RAM
220*75b842b8Sskrll  */
2216cf6fe02Smatt #define	VM_PHYSSEG_STRAT	VM_PSTRAT_BSEARCH
2226cf6fe02Smatt 
2236cf6fe02Smatt #ifndef VM_NFREELIST
2246cf6fe02Smatt #define	VM_NFREELIST		2	/* 2 distinct memory segments */
2256cf6fe02Smatt #define VM_FREELIST_DEFAULT	0
2266cf6fe02Smatt #define VM_FREELIST_DIRECTMAP	1
2276cf6fe02Smatt #endif
2286cf6fe02Smatt 
2296cf6fe02Smatt #ifdef _KERNEL
2306cf6fe02Smatt #ifdef _LP64
2316cf6fe02Smatt void *	cpu_uarea_alloc(bool);
2326cf6fe02Smatt bool	cpu_uarea_free(void *);
2336cf6fe02Smatt #endif
2346cf6fe02Smatt #endif
2356cf6fe02Smatt 
2366cf6fe02Smatt #endif /* ! _RISCV_VMPARAM_H_ */
237