xref: /netbsd-src/sys/arch/riscv/include/pmap.h (revision d90047b5d07facf36e6c01dcc0bded8997ce9cc2)
1 /* $NetBSD: pmap.h,v 1.4 2020/03/11 13:30:31 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 2014, 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Thomas (of 3am Software Foundry) and Maxime Villard.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _RISCV_PMAP_H_
33 #define _RISCV_PMAP_H_
34 
35 #ifdef _KERNEL_OPT
36 #include "opt_modular.h"
37 #endif
38 
39 #if !defined(_MODULE)
40 
41 #include <sys/types.h>
42 #include <sys/pool.h>
43 #include <sys/evcnt.h>
44 
45 #include <uvm/uvm_physseg.h>
46 #include <uvm/pmap/vmpagemd.h>
47 
48 #include <riscv/pte.h>
49 
50 #define	PMAP_SEGTABSIZE	NPTEPG
51 
52 #define NBSEG		(PAGE_SIZE * NPTEPG)
53 
54 #ifdef _LP64
55 #define NBXSEG		(NBSEG * NSEGPG)
56 #define XSEGSHIFT	(SEGSHIFT + PGSHIFT - 3)
57 #define XSEGOFSET	(PTE_PPN1 | SEGOFSET)
58 #define SEGSHIFT	(PGSHIFT + PGSHIFT - 3)
59 #else
60 #define SEGSHIFT	(PGSHIFT + PGSHIFT - 2)
61 #endif
62 
63 #define SEGOFSET	(PTE_PPN0|PAGE_MASK)
64 
65 #define KERNEL_PID	0
66 
67 #define PMAP_HWPAGEWALKER		1
68 #define PMAP_TLB_NUM_PIDS		256
69 #define PMAP_TLB_MAX			1
70 #ifdef _LP64
71 #define PMAP_INVALID_PDETAB_ADDRESS	((pmap_pdetab_t *)(VM_MIN_KERNEL_ADDRESS - PAGE_SIZE))
72 #define PMAP_INVALID_SEGTAB_ADDRESS	((pmap_segtab_t *)(VM_MIN_KERNEL_ADDRESS - PAGE_SIZE))
73 #else
74 #define PMAP_INVALID_PDETAB_ADDRESS	((pmap_pdetab_t *)0xdeadbeef)
75 #define PMAP_INVALID_SEGTAB_ADDRESS	((pmap_segtab_t *)0xdeadbeef)
76 #endif
77 #define PMAP_TLB_FLUSH_ASID_ON_RESET	false
78 
79 #define pmap_phys_address(x)		(x)
80 
81 #ifndef __BSD_PTENTRY_T__
82 #define __BSD_PTENTRY_T__
83 #ifdef _LP64
84 #define PRIxPTE         PRIx64
85 #else
86 #define PRIxPTE         PRIx32
87 #endif
88 #endif /* __BSD_PTENTRY_T__ */
89 
90 #define PMAP_NEED_PROCWR
91 static inline void
92 pmap_procwr(struct proc *p, vaddr_t va, vsize_t len)
93 {
94 	__asm __volatile("fence\trw,rw; fence.i");
95 }
96 
97 #include <uvm/pmap/tlb.h>
98 #include <uvm/pmap/pmap_tlb.h>
99 
100 #define PMAP_GROWKERNEL
101 #define PMAP_STEAL_MEMORY
102 
103 #ifdef _KERNEL
104 
105 #define __HAVE_PMAP_MD
106 struct pmap_md {
107 	paddr_t md_ptbr;
108 	pd_entry_t *md_pdetab;
109 };
110 
111 void	pmap_bootstrap(void);
112 
113 struct vm_page *
114         pmap_md_alloc_poolpage(int flags);
115 vaddr_t pmap_md_map_poolpage(paddr_t, vsize_t);
116 void    pmap_md_unmap_poolpage(vaddr_t, vsize_t);
117 bool    pmap_md_direct_mapped_vaddr_p(vaddr_t);
118 bool    pmap_md_io_vaddr_p(vaddr_t);
119 paddr_t pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t);
120 vaddr_t pmap_md_direct_map_paddr(paddr_t);
121 void    pmap_md_init(void);
122 bool    pmap_md_tlb_check_entry(void *, vaddr_t, tlb_asid_t, pt_entry_t);
123 //void    pmap_md_page_syncicache(struct vm_page *, const kcpuset_t *);
124 
125 void	pmap_md_pdetab_activate(struct pmap *);
126 void	pmap_md_pdetab_init(struct pmap *);
127 bool	pmap_md_ok_to_steal_p(const uvm_physseg_t, size_t);
128 
129 extern vaddr_t pmap_direct_base;
130 extern vaddr_t pmap_direct_end;
131 #define PMAP_DIRECT_MAP(pa)	(pmap_direct_base + (pa))
132 #define PMAP_DIRECT_UNMAP(va)	((paddr_t)(va) - pmap_direct_base)
133 
134 #ifdef __PMAP_PRIVATE
135 static inline void
136 pmap_md_page_syncicache(struct vm_page *pg, const kcpuset_t *kc)
137 {
138 	__asm __volatile("fence\trw,rw; fence.i");
139 }
140 
141 /*
142  * Virtual Cache Alias helper routines.  Not a problem for RISCV CPUs.
143  */
144 static inline bool
145 pmap_md_vca_add(struct vm_page *pg, vaddr_t va, pt_entry_t *nptep)
146 {
147 	return false;
148 }
149 
150 static inline void
151 pmap_md_vca_remove(struct vm_page *pg, vaddr_t va)
152 {
153 
154 }
155 
156 static inline void
157 pmap_md_vca_clean(struct vm_page *pg, vaddr_t va, int op)
158 {
159 }
160 
161 static inline size_t
162 pmap_md_tlb_asid_max(void)
163 {
164 	return PMAP_TLB_NUM_PIDS - 1;
165 }
166 #endif /* __PMAP_PRIVATE */
167 #endif /* _KERNEL */
168 
169 #include <uvm/pmap/pmap.h>
170 
171 #endif /* !_MODULE */
172 
173 #if defined(MODULAR) || defined(_MODULE)
174 /*
175  * Define a compatible vm_page_md so that struct vm_page is the same size
176  * whether we are using modules or not.
177  */
178 #ifndef __HAVE_VM_PAGE_MD
179 #define __HAVE_VM_PAGE_MD
180 
181 struct vm_page_md {
182 	uintptr_t mdpg_dummy[3];
183 };
184 __CTASSERT(sizeof(struct vm_page_md) == sizeof(uintptr_t)*3);
185 
186 #endif /* !__HAVE_VM_PAGE_MD */
187 
188 #endif /* MODULAR || _MODULE */
189 
190 #endif /* !_RISCV_PMAP_H_ */
191