xref: /freebsd/sys/riscv/include/pmap.h (revision 87bf66d4a7488c496af110d4d05cc0273d49f82e)
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * the Systems Programming Group of the University of Utah Computer
7  * Science Department and William Jolitz of UUNET Technologies Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef _MACHINE_PMAP_H_
35 #define	_MACHINE_PMAP_H_
36 
37 #include <machine/pte.h>
38 
39 #ifndef LOCORE
40 
41 #include <sys/queue.h>
42 #include <sys/_cpuset.h>
43 #include <sys/_lock.h>
44 #include <sys/_mutex.h>
45 #include <sys/_pv_entry.h>
46 
47 #include <vm/_vm_radix.h>
48 
49 #ifdef _KERNEL
50 
51 #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
52 
53 #endif
54 
55 #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
56 #define	pmap_page_is_write_mapped(m)	(((m)->a.flags & PGA_WRITEABLE) != 0)
57 void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
58 #define	pmap_map_delete(pmap, sva, eva)	pmap_remove(pmap, sva, eva)
59 
60 /*
61  * Pmap stuff
62  */
63 
64 struct md_page {
65 	TAILQ_HEAD(,pv_entry)	pv_list;
66 	int			pv_gen;
67 	vm_memattr_t		pv_memattr;
68 };
69 
70 enum pmap_stage {
71 	PM_INVALID,
72 	PM_STAGE1,
73 	PM_STAGE2,
74 };
75 
76 struct pmap {
77 	struct mtx		pm_mtx;
78 	struct pmap_statistics	pm_stats;	/* pmap statictics */
79 	pd_entry_t		*pm_top;	/* top-level page table page */
80 	u_long			pm_satp;	/* value for SATP register */
81 	cpuset_t		pm_active;	/* active on cpus */
82 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
83 	LIST_ENTRY(pmap)	pm_list;	/* List of all pmaps */
84 	struct vm_radix		pm_root;
85 	enum pmap_stage		pm_stage;
86 };
87 
88 typedef struct pmap *pmap_t;
89 
90 #ifdef _KERNEL
91 extern struct pmap	kernel_pmap_store;
92 #define	kernel_pmap	(&kernel_pmap_store)
93 #define	pmap_kernel()	kernel_pmap
94 
95 #define	PMAP_ASSERT_LOCKED(pmap) \
96 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
97 #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
98 #define	PMAP_LOCK_ASSERT(pmap, type) \
99 				mtx_assert(&(pmap)->pm_mtx, (type))
100 #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
101 #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
102 				    NULL, MTX_DEF | MTX_DUPOK)
103 #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
104 #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
105 #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
106 #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
107 
108 extern vm_offset_t virtual_avail;
109 extern vm_offset_t virtual_end;
110 
111 /*
112  * Macros to test if a mapping is mappable with an L1 Section mapping
113  * or an L2 Large Page mapping.
114  */
115 #define	L1_MAPPABLE_P(va, pa, size)					\
116 	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
117 
118 enum pmap_mode {
119 	PMAP_MODE_SV39,
120 	PMAP_MODE_SV48,
121 };
122 
123 extern enum pmap_mode pmap_mode;
124 
125 /* Check if an address resides in a mappable region. */
126 #define	VIRT_IS_VALID(va)						\
127 	((va) < (pmap_mode == PMAP_MODE_SV39 ? VM_MAX_USER_ADDRESS_SV39 : \
128 	    VM_MAX_USER_ADDRESS_SV48) || (va) >= VM_MIN_KERNEL_ADDRESS)
129 
130 struct thread;
131 
132 #define	pmap_vm_page_alloc_check(m)
133 
134 void	pmap_activate_boot(pmap_t);
135 void	pmap_activate_sw(struct thread *);
136 void	pmap_bootstrap(vm_paddr_t, vm_size_t);
137 int	pmap_change_attr(vm_offset_t va, vm_size_t size, int mode);
138 void	pmap_kenter(vm_offset_t sva, vm_size_t size, vm_paddr_t pa, int mode);
139 void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
140 vm_paddr_t pmap_kextract(vm_offset_t va);
141 void	pmap_kremove(vm_offset_t);
142 void	pmap_kremove_device(vm_offset_t, vm_size_t);
143 void	*pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma);
144 int	pmap_pinit_stage(pmap_t, enum pmap_stage);
145 bool	pmap_page_is_mapped(vm_page_t m);
146 bool	pmap_ps_enabled(pmap_t);
147 
148 void	*pmap_mapdev(vm_paddr_t, vm_size_t);
149 void	*pmap_mapbios(vm_paddr_t, vm_size_t);
150 void	pmap_unmapdev(void *, vm_size_t);
151 void	pmap_unmapbios(void *, vm_size_t);
152 
153 bool	pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, bool);
154 void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, bool);
155 
156 bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
157     pt_entry_t **);
158 
159 int	pmap_fault(pmap_t, vm_offset_t, vm_prot_t);
160 
161 static inline int
162 pmap_vmspace_copy(pmap_t dst_pmap __unused, pmap_t src_pmap __unused)
163 {
164 
165 	return (0);
166 }
167 
168 #endif	/* _KERNEL */
169 
170 #endif	/* !LOCORE */
171 
172 #endif	/* !_MACHINE_PMAP_H_ */
173