xref: /freebsd/sys/powerpc/include/pmap.h (revision 6683132d54bd6d589889e43dabdc53d35e38a028)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause AND BSD-4-Clause
3  *
4  * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
5  * All rights reserved.
6  *
7  * Adapted for Freescale's e500 core CPUs.
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. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
23  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 /*-
34  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
35  * Copyright (C) 1995, 1996 TooLs GmbH.
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by TooLs GmbH.
49  * 4. The name of TooLs GmbH may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *	from: $NetBSD: pmap.h,v 1.17 2000/03/30 16:18:24 jdolecek Exp $
64  */
65 
66 #ifndef	_MACHINE_PMAP_H_
67 #define	_MACHINE_PMAP_H_
68 
69 #include <sys/queue.h>
70 #include <sys/tree.h>
71 #include <sys/_cpuset.h>
72 #include <sys/_lock.h>
73 #include <sys/_mutex.h>
74 #include <machine/sr.h>
75 #include <machine/pte.h>
76 #include <machine/slb.h>
77 #include <machine/tlb.h>
78 
79 struct pmap;
80 typedef struct pmap *pmap_t;
81 
82 #if !defined(NPMAPS)
83 #define	NPMAPS		32768
84 #endif /* !defined(NPMAPS) */
85 
86 struct	slbtnode;
87 
88 struct pvo_entry {
89 	LIST_ENTRY(pvo_entry) pvo_vlink;	/* Link to common virt page */
90 #ifndef __powerpc64__
91 	LIST_ENTRY(pvo_entry) pvo_olink;	/* Link to overflow entry */
92 #endif
93 	RB_ENTRY(pvo_entry) pvo_plink;	/* Link to pmap entries */
94 	struct {
95 #ifndef __powerpc64__
96 		/* 32-bit fields */
97 		pte_t	    pte;
98 #endif
99 		/* 64-bit fields */
100 		uintptr_t   slot;
101 		vm_paddr_t  pa;
102 		vm_prot_t   prot;
103 	} pvo_pte;
104 	pmap_t		pvo_pmap;		/* Owning pmap */
105 	vm_offset_t	pvo_vaddr;		/* VA of entry */
106 	uint64_t	pvo_vpn;		/* Virtual page number */
107 };
108 LIST_HEAD(pvo_head, pvo_entry);
109 RB_HEAD(pvo_tree, pvo_entry);
110 int pvo_vaddr_compare(struct pvo_entry *, struct pvo_entry *);
111 RB_PROTOTYPE(pvo_tree, pvo_entry, pvo_plink, pvo_vaddr_compare);
112 
113 /* Used by 32-bit PMAP */
114 #define	PVO_PTEGIDX_MASK	0x007UL		/* which PTEG slot */
115 #define	PVO_PTEGIDX_VALID	0x008UL		/* slot is valid */
116 /* Used by 64-bit PMAP */
117 #define	PVO_HID			0x008UL		/* PVO entry in alternate hash*/
118 /* Used by both */
119 #define	PVO_WIRED		0x010UL		/* PVO entry is wired */
120 #define	PVO_MANAGED		0x020UL		/* PVO entry is managed */
121 #define	PVO_BOOTSTRAP		0x080UL		/* PVO entry allocated during
122 						   bootstrap */
123 #define PVO_DEAD		0x100UL		/* waiting to be deleted */
124 #define PVO_LARGE		0x200UL		/* large page */
125 #define	PVO_VADDR(pvo)		((pvo)->pvo_vaddr & ~ADDR_POFF)
126 #define	PVO_PTEGIDX_GET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK)
127 #define	PVO_PTEGIDX_ISSET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID)
128 #define	PVO_PTEGIDX_CLR(pvo)	\
129 	((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK)))
130 #define	PVO_PTEGIDX_SET(pvo, i)	\
131 	((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID))
132 #define	PVO_VSID(pvo)		((pvo)->pvo_vpn >> 16)
133 
134 struct	pmap {
135 	struct		pmap_statistics	pm_stats;
136 	struct	mtx	pm_mtx;
137 	cpuset_t	pm_active;
138 	union {
139 #ifdef AIM
140 		struct {
141 
142 		    #ifdef __powerpc64__
143 			struct slbtnode	*pm_slb_tree_root;
144 			struct slb	**pm_slb;
145 			int		pm_slb_len;
146 		    #else
147 			register_t	pm_sr[16];
148 		    #endif
149 
150 			struct pmap	*pmap_phys;
151 			struct pvo_tree pmap_pvo;
152 		};
153 #endif
154 #ifdef BOOKE
155 		struct {
156 			/* TID to identify this pmap entries in TLB */
157 			tlbtid_t	pm_tid[MAXCPU];
158 
159 #ifdef __powerpc64__
160 			/*
161 			 * Page table directory,
162 			 * array of pointers to page directories.
163 			 */
164 			pte_t **pm_pp2d[PP2D_NENTRIES];
165 
166 			/* List of allocated pdir bufs (pdir kva regions). */
167 			TAILQ_HEAD(, ptbl_buf)	pm_pdir_list;
168 #else
169 			/*
170 			 * Page table directory,
171 			 * array of pointers to page tables.
172 			 */
173 			pte_t		*pm_pdir[PDIR_NENTRIES];
174 #endif
175 
176 			/* List of allocated ptbl bufs (ptbl kva regions). */
177 			TAILQ_HEAD(, ptbl_buf)	pm_ptbl_list;
178 		};
179 #endif
180 	};
181 };
182 
183 struct pv_entry {
184 	pmap_t pv_pmap;
185 	vm_offset_t pv_va;
186 	TAILQ_ENTRY(pv_entry) pv_link;
187 };
188 typedef struct pv_entry *pv_entry_t;
189 
190 struct	md_page {
191 	union {
192 		struct {
193 			volatile int32_t mdpg_attrs;
194 			vm_memattr_t	 mdpg_cache_attrs;
195 			struct	pvo_head mdpg_pvoh;
196 		};
197 		struct {
198 			TAILQ_HEAD(, pv_entry)	pv_list;
199 			int			pv_tracked;
200 		};
201 	};
202 };
203 
204 #ifdef AIM
205 #define	pmap_page_get_memattr(m)	((m)->md.mdpg_cache_attrs)
206 #define	pmap_page_is_mapped(m)	(!LIST_EMPTY(&(m)->md.mdpg_pvoh))
207 #else
208 #define	pmap_page_get_memattr(m)	VM_MEMATTR_DEFAULT
209 #define	pmap_page_is_mapped(m)	(!TAILQ_EMPTY(&(m)->md.pv_list))
210 #endif
211 
212 /*
213  * Return the VSID corresponding to a given virtual address.
214  * If no VSID is currently defined, it will allocate one, and add
215  * it to a free slot if available.
216  *
217  * NB: The PMAP MUST be locked already.
218  */
219 uint64_t va_to_vsid(pmap_t pm, vm_offset_t va);
220 
221 /* Lock-free, non-allocating lookup routines */
222 uint64_t kernel_va_to_slbv(vm_offset_t va);
223 struct slb *user_va_to_slb_entry(pmap_t pm, vm_offset_t va);
224 
225 uint64_t allocate_user_vsid(pmap_t pm, uint64_t esid, int large);
226 void	free_vsid(pmap_t pm, uint64_t esid, int large);
227 void	slb_insert_user(pmap_t pm, struct slb *slb);
228 void	slb_insert_kernel(uint64_t slbe, uint64_t slbv);
229 
230 struct slbtnode *slb_alloc_tree(void);
231 void     slb_free_tree(pmap_t pm);
232 struct slb **slb_alloc_user_cache(void);
233 void	slb_free_user_cache(struct slb **);
234 
235 extern	struct pmap kernel_pmap_store;
236 #define	kernel_pmap	(&kernel_pmap_store)
237 
238 #ifdef _KERNEL
239 
240 #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
241 #define	PMAP_LOCK_ASSERT(pmap, type) \
242 				mtx_assert(&(pmap)->pm_mtx, (type))
243 #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
244 #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, \
245 				    (pmap == kernel_pmap) ? "kernelpmap" : \
246 				    "pmap", NULL, MTX_DEF)
247 #define	PMAP_LOCKED(pmap)	mtx_owned(&(pmap)->pm_mtx)
248 #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
249 #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
250 #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
251 
252 #define	pmap_page_is_write_mapped(m)	(((m)->aflags & PGA_WRITEABLE) != 0)
253 
254 void		pmap_bootstrap(vm_offset_t, vm_offset_t);
255 void		pmap_kenter(vm_offset_t va, vm_paddr_t pa);
256 void		pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t);
257 void		pmap_kremove(vm_offset_t);
258 void		*pmap_mapdev(vm_paddr_t, vm_size_t);
259 void		*pmap_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t);
260 void		pmap_unmapdev(vm_offset_t, vm_size_t);
261 void		pmap_page_set_memattr(vm_page_t, vm_memattr_t);
262 int		pmap_change_attr(vm_offset_t, vm_size_t, vm_memattr_t);
263 int		pmap_map_user_ptr(pmap_t pm, volatile const void *uaddr,
264 		    void **kaddr, size_t ulen, size_t *klen);
265 int		pmap_decode_kernel_ptr(vm_offset_t addr, int *is_user,
266 		    vm_offset_t *decoded_addr);
267 void		pmap_deactivate(struct thread *);
268 vm_paddr_t	pmap_kextract(vm_offset_t);
269 int		pmap_dev_direct_mapped(vm_paddr_t, vm_size_t);
270 boolean_t	pmap_mmu_install(char *name, int prio);
271 
272 #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
273 
274 #define PHYS_AVAIL_SZ	256	/* Allows up to 16GB Ram on pSeries with
275 				 * logical memory block size of 64MB.
276 				 * For more Ram increase the lmb or this value.
277 				 */
278 
279 extern	vm_paddr_t phys_avail[PHYS_AVAIL_SZ];
280 extern	vm_offset_t virtual_avail;
281 extern	vm_offset_t virtual_end;
282 
283 extern	vm_offset_t msgbuf_phys;
284 
285 extern	int pmap_bootstrapped;
286 
287 vm_offset_t pmap_early_io_map(vm_paddr_t pa, vm_size_t size);
288 void pmap_early_io_unmap(vm_offset_t va, vm_size_t size);
289 void pmap_track_page(pmap_t pmap, vm_offset_t va);
290 
291 static inline int
292 pmap_vmspace_copy(pmap_t dst_pmap __unused, pmap_t src_pmap __unused)
293 {
294 
295 	return (0);
296 }
297 
298 #endif
299 
300 #endif /* !_MACHINE_PMAP_H_ */
301