xref: /freebsd/sys/arm/include/pmap.h (revision 4c9e27bd0a5f7fda85b0c0bf750575aee300a172)
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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * Derived from hp300 version by Mike Hibler, this version by William
38  * Jolitz uses a recursive map [a pde points to the page directory] to
39  * map the page tables using the pagetables themselves. This is done to
40  * reduce the impact on kernel virtual memory for lots of sparse address
41  * space, and to reduce the cost of memory to each process.
42  *
43  *      from: hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90
44  *      from: @(#)pmap.h        7.4 (Berkeley) 5/12/91
45  * 	from: FreeBSD: src/sys/i386/include/pmap.h,v 1.70 2000/11/30
46  *
47  * $FreeBSD$
48  */
49 
50 #ifndef _MACHINE_PMAP_H_
51 #define _MACHINE_PMAP_H_
52 
53 #include <machine/pte.h>
54 #include <machine/cpuconf.h>
55 /*
56  * Pte related macros
57  */
58 #if ARM_ARCH_6 || ARM_ARCH_7A
59 #ifdef SMP
60 #define PTE_NOCACHE	2
61 #else
62 #define PTE_NOCACHE	1
63 #endif
64 #define PTE_CACHE	6
65 #define PTE_DEVICE	2
66 #define PTE_PAGETABLE	6
67 #else
68 #define PTE_NOCACHE	1
69 #define PTE_CACHE	2
70 #define PTE_DEVICE	PTE_NOCACHE
71 #define PTE_PAGETABLE	3
72 #endif
73 
74 enum mem_type {
75 	STRONG_ORD = 0,
76 	DEVICE_NOSHARE,
77 	DEVICE_SHARE,
78 	NRML_NOCACHE,
79 	NRML_IWT_OWT,
80 	NRML_IWB_OWB,
81 	NRML_IWBA_OWBA
82 };
83 
84 #ifndef LOCORE
85 
86 #include <sys/queue.h>
87 #include <sys/_cpuset.h>
88 #include <sys/_lock.h>
89 #include <sys/_mutex.h>
90 
91 #define PDESIZE		sizeof(pd_entry_t)	/* for assembly files */
92 #define PTESIZE		sizeof(pt_entry_t)	/* for assembly files */
93 
94 #ifdef _KERNEL
95 
96 #define vtophys(va)	pmap_kextract((vm_offset_t)(va))
97 
98 #endif
99 
100 #define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
101 #define	pmap_page_is_write_mapped(m)	(((m)->aflags & PGA_WRITEABLE) != 0)
102 #if (ARM_MMU_V6 + ARM_MMU_V7) > 0
103 boolean_t pmap_page_is_mapped(vm_page_t);
104 #else
105 #define	pmap_page_is_mapped(m)	(!TAILQ_EMPTY(&(m)->md.pv_list))
106 #endif
107 void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
108 
109 /*
110  * Pmap stuff
111  */
112 
113 /*
114  * This structure is used to hold a virtual<->physical address
115  * association and is used mostly by bootstrap code
116  */
117 struct pv_addr {
118 	SLIST_ENTRY(pv_addr) pv_list;
119 	vm_offset_t	pv_va;
120 	vm_paddr_t	pv_pa;
121 };
122 
123 struct	pv_entry;
124 struct	pv_chunk;
125 
126 struct	md_page {
127 	int pvh_attrs;
128 	vm_memattr_t	 pv_memattr;
129 #if (ARM_MMU_V6 + ARM_MMU_V7) == 0
130 	vm_offset_t pv_kva;		/* first kernel VA mapping */
131 #endif
132 	TAILQ_HEAD(,pv_entry)	pv_list;
133 };
134 
135 struct l1_ttable;
136 struct l2_dtable;
137 
138 
139 /*
140  * The number of L2 descriptor tables which can be tracked by an l2_dtable.
141  * A bucket size of 16 provides for 16MB of contiguous virtual address
142  * space per l2_dtable. Most processes will, therefore, require only two or
143  * three of these to map their whole working set.
144  */
145 #define	L2_BUCKET_LOG2	4
146 #define	L2_BUCKET_SIZE	(1 << L2_BUCKET_LOG2)
147 /*
148  * Given the above "L2-descriptors-per-l2_dtable" constant, the number
149  * of l2_dtable structures required to track all possible page descriptors
150  * mappable by an L1 translation table is given by the following constants:
151  */
152 #define	L2_LOG2		((32 - L1_S_SHIFT) - L2_BUCKET_LOG2)
153 #define	L2_SIZE		(1 << L2_LOG2)
154 
155 struct	pmap {
156 	struct mtx		pm_mtx;
157 	u_int8_t		pm_domain;
158 	struct l1_ttable	*pm_l1;
159 	struct l2_dtable	*pm_l2[L2_SIZE];
160 	cpuset_t		pm_active;	/* active on cpus */
161 	struct pmap_statistics	pm_stats;	/* pmap statictics */
162 #if (ARM_MMU_V6 + ARM_MMU_V7) != 0
163 	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
164 #else
165 	TAILQ_HEAD(,pv_entry)	pm_pvlist;	/* list of mappings in pmap */
166 #endif
167 };
168 
169 typedef struct pmap *pmap_t;
170 
171 #ifdef _KERNEL
172 extern struct pmap	kernel_pmap_store;
173 #define kernel_pmap	(&kernel_pmap_store)
174 #define pmap_kernel() kernel_pmap
175 
176 #define	PMAP_ASSERT_LOCKED(pmap) \
177 				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
178 #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
179 #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
180 #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
181 				    NULL, MTX_DEF | MTX_DUPOK)
182 #define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
183 #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
184 #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
185 #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
186 #endif
187 
188 
189 /*
190  * For each vm_page_t, there is a list of all currently valid virtual
191  * mappings of that page.  An entry is a pv_entry_t, the list is pv_list.
192  */
193 typedef struct pv_entry {
194 	vm_offset_t     pv_va;          /* virtual address for mapping */
195 	TAILQ_ENTRY(pv_entry)   pv_list;
196 	int		pv_flags;	/* flags (wired, etc...) */
197 #if (ARM_MMU_V6 + ARM_MMU_V7) == 0
198 	pmap_t          pv_pmap;        /* pmap where mapping lies */
199 	TAILQ_ENTRY(pv_entry)	pv_plist;
200 #endif
201 } *pv_entry_t;
202 
203 /*
204  * pv_entries are allocated in chunks per-process.  This avoids the
205  * need to track per-pmap assignments.
206  */
207 #define	_NPCM	8
208 #define	_NPCPV	252
209 
210 struct pv_chunk {
211 	pmap_t			pc_pmap;
212 	TAILQ_ENTRY(pv_chunk)	pc_list;
213 	uint32_t		pc_map[_NPCM];	/* bitmap; 1 = free */
214 	uint32_t		pc_dummy[3];	/* aligns pv_chunk to 4KB */
215 	TAILQ_ENTRY(pv_chunk)	pc_lru;
216 	struct pv_entry		pc_pventry[_NPCPV];
217 };
218 
219 #ifdef _KERNEL
220 
221 boolean_t pmap_get_pde_pte(pmap_t, vm_offset_t, pd_entry_t **, pt_entry_t **);
222 
223 /*
224  * virtual address to page table entry and
225  * to physical address. Likewise for alternate address space.
226  * Note: these work recursively, thus vtopte of a pte will give
227  * the corresponding pde that in turn maps it.
228  */
229 
230 /*
231  * The current top of kernel VM.
232  */
233 extern vm_offset_t pmap_curmaxkvaddr;
234 
235 struct pcb;
236 
237 void	pmap_set_pcb_pagedir(pmap_t, struct pcb *);
238 /* Virtual address to page table entry */
239 static __inline pt_entry_t *
240 vtopte(vm_offset_t va)
241 {
242 	pd_entry_t *pdep;
243 	pt_entry_t *ptep;
244 
245 	if (pmap_get_pde_pte(pmap_kernel(), va, &pdep, &ptep) == FALSE)
246 		return (NULL);
247 	return (ptep);
248 }
249 
250 extern vm_paddr_t phys_avail[];
251 extern vm_offset_t virtual_avail;
252 extern vm_offset_t virtual_end;
253 
254 void	pmap_bootstrap(vm_offset_t firstaddr, struct pv_addr *l1pt);
255 int	pmap_change_attr(vm_offset_t, vm_size_t, int);
256 void	pmap_kenter(vm_offset_t va, vm_paddr_t pa);
257 void	pmap_kenter_nocache(vm_offset_t va, vm_paddr_t pa);
258 void	pmap_kenter_device(vm_offset_t va, vm_paddr_t pa);
259 void	*pmap_kenter_temp(vm_paddr_t pa, int i);
260 void 	pmap_kenter_user(vm_offset_t va, vm_paddr_t pa);
261 vm_paddr_t pmap_kextract(vm_offset_t va);
262 void	pmap_kremove(vm_offset_t);
263 void	*pmap_mapdev(vm_offset_t, vm_size_t);
264 void	pmap_unmapdev(vm_offset_t, vm_size_t);
265 vm_page_t	pmap_use_pt(pmap_t, vm_offset_t);
266 void	pmap_debug(int);
267 #if (ARM_MMU_V6 + ARM_MMU_V7) == 0
268 void	pmap_map_section(vm_offset_t, vm_offset_t, vm_offset_t, int, int);
269 #endif
270 void	pmap_link_l2pt(vm_offset_t, vm_offset_t, struct pv_addr *);
271 vm_size_t	pmap_map_chunk(vm_offset_t, vm_offset_t, vm_offset_t, vm_size_t, int, int);
272 void
273 pmap_map_entry(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa, int prot,
274     int cache);
275 int pmap_fault_fixup(pmap_t, vm_offset_t, vm_prot_t, int);
276 int pmap_dmap_iscurrent(pmap_t pmap);
277 
278 /*
279  * Definitions for MMU domains
280  */
281 #define	PMAP_DOMAINS		15	/* 15 'user' domains (1-15) */
282 #define	PMAP_DOMAIN_KERNEL	0	/* The kernel uses domain #0 */
283 
284 /*
285  * The new pmap ensures that page-tables are always mapping Write-Thru.
286  * Thus, on some platforms we can run fast and loose and avoid syncing PTEs
287  * on every change.
288  *
289  * Unfortunately, not all CPUs have a write-through cache mode.  So we
290  * define PMAP_NEEDS_PTE_SYNC for C code to conditionally do PTE syncs,
291  * and if there is the chance for PTE syncs to be needed, we define
292  * PMAP_INCLUDE_PTE_SYNC so e.g. assembly code can include (and run)
293  * the code.
294  */
295 extern int pmap_needs_pte_sync;
296 
297 /*
298  * These macros define the various bit masks in the PTE.
299  *
300  * We use these macros since we use different bits on different processor
301  * models.
302  */
303 
304 #define	L1_S_CACHE_MASK_generic	(L1_S_B|L1_S_C)
305 #define	L1_S_CACHE_MASK_xscale	(L1_S_B|L1_S_C|L1_S_XSCALE_TEX(TEX_XSCALE_X)|\
306     				L1_S_XSCALE_TEX(TEX_XSCALE_T))
307 
308 #define	L2_L_CACHE_MASK_generic	(L2_B|L2_C)
309 #define	L2_L_CACHE_MASK_xscale	(L2_B|L2_C|L2_XSCALE_L_TEX(TEX_XSCALE_X) | \
310     				L2_XSCALE_L_TEX(TEX_XSCALE_T))
311 
312 #define	L2_S_PROT_U_generic	(L2_AP(AP_U))
313 #define	L2_S_PROT_W_generic	(L2_AP(AP_W))
314 #define	L2_S_PROT_MASK_generic	(L2_S_PROT_U|L2_S_PROT_W)
315 
316 #define	L2_S_PROT_U_xscale	(L2_AP0(AP_U))
317 #define	L2_S_PROT_W_xscale	(L2_AP0(AP_W))
318 #define	L2_S_PROT_MASK_xscale	(L2_S_PROT_U|L2_S_PROT_W)
319 
320 #define	L2_S_CACHE_MASK_generic	(L2_B|L2_C)
321 #define	L2_S_CACHE_MASK_xscale	(L2_B|L2_C|L2_XSCALE_T_TEX(TEX_XSCALE_X)| \
322     				 L2_XSCALE_T_TEX(TEX_XSCALE_X))
323 
324 #define	L1_S_PROTO_generic	(L1_TYPE_S | L1_S_IMP)
325 #define	L1_S_PROTO_xscale	(L1_TYPE_S)
326 
327 #define	L1_C_PROTO_generic	(L1_TYPE_C | L1_C_IMP2)
328 #define	L1_C_PROTO_xscale	(L1_TYPE_C)
329 
330 #define	L2_L_PROTO		(L2_TYPE_L)
331 
332 #define	L2_S_PROTO_generic	(L2_TYPE_S)
333 #define	L2_S_PROTO_xscale	(L2_TYPE_XSCALE_XS)
334 
335 /*
336  * User-visible names for the ones that vary with MMU class.
337  */
338 #if (ARM_MMU_V6 + ARM_MMU_V7) != 0
339 #define	L2_AP(x)	(L2_AP0(x))
340 #else
341 #define	L2_AP(x)	(L2_AP0(x) | L2_AP1(x) | L2_AP2(x) | L2_AP3(x))
342 #endif
343 
344 #if ARM_NMMUS > 1
345 /* More than one MMU class configured; use variables. */
346 #define	L2_S_PROT_U		pte_l2_s_prot_u
347 #define	L2_S_PROT_W		pte_l2_s_prot_w
348 #define	L2_S_PROT_MASK		pte_l2_s_prot_mask
349 
350 #define	L1_S_CACHE_MASK		pte_l1_s_cache_mask
351 #define	L2_L_CACHE_MASK		pte_l2_l_cache_mask
352 #define	L2_S_CACHE_MASK		pte_l2_s_cache_mask
353 
354 #define	L1_S_PROTO		pte_l1_s_proto
355 #define	L1_C_PROTO		pte_l1_c_proto
356 #define	L2_S_PROTO		pte_l2_s_proto
357 
358 #elif (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
359 #define	L2_S_PROT_U		L2_S_PROT_U_generic
360 #define	L2_S_PROT_W		L2_S_PROT_W_generic
361 #define	L2_S_PROT_MASK		L2_S_PROT_MASK_generic
362 
363 #define	L1_S_CACHE_MASK		L1_S_CACHE_MASK_generic
364 #define	L2_L_CACHE_MASK		L2_L_CACHE_MASK_generic
365 #define	L2_S_CACHE_MASK		L2_S_CACHE_MASK_generic
366 
367 #define	L1_S_PROTO		L1_S_PROTO_generic
368 #define	L1_C_PROTO		L1_C_PROTO_generic
369 #define	L2_S_PROTO		L2_S_PROTO_generic
370 
371 #elif ARM_MMU_XSCALE == 1
372 #define	L2_S_PROT_U		L2_S_PROT_U_xscale
373 #define	L2_S_PROT_W		L2_S_PROT_W_xscale
374 #define	L2_S_PROT_MASK		L2_S_PROT_MASK_xscale
375 
376 #define	L1_S_CACHE_MASK		L1_S_CACHE_MASK_xscale
377 #define	L2_L_CACHE_MASK		L2_L_CACHE_MASK_xscale
378 #define	L2_S_CACHE_MASK		L2_S_CACHE_MASK_xscale
379 
380 #define	L1_S_PROTO		L1_S_PROTO_xscale
381 #define	L1_C_PROTO		L1_C_PROTO_xscale
382 #define	L2_S_PROTO		L2_S_PROTO_xscale
383 
384 #elif (ARM_MMU_V6 + ARM_MMU_V7) != 0
385 /*
386  * AP[2:1] access permissions model:
387  *
388  * AP[2](APX)	- Write Disable
389  * AP[1]	- User Enable
390  * AP[0]	- Reference Flag
391  *
392  * AP[2]     AP[1]     Kernel     User
393  *  0          0        R/W        N
394  *  0          1        R/W       R/W
395  *  1          0         R         N
396  *  1          1         R         R
397  *
398  */
399 #define	L2_S_PROT_R		(0)		/* kernel read */
400 #define	L2_S_PROT_U		(L2_AP0(2))	/* user read */
401 #define L2_S_REF		(L2_AP0(1))	/* reference flag */
402 
403 #define	L2_S_PROT_MASK		(L2_S_PROT_U|L2_S_PROT_R|L2_APX)
404 #define	L2_S_EXECUTABLE(pte)	(!(pte & L2_XN))
405 #define	L2_S_WRITABLE(pte)	(!(pte & L2_APX))
406 #define	L2_S_REFERENCED(pte)	(!!(pte & L2_S_REF))
407 
408 #ifndef SMP
409 #define	L1_S_CACHE_MASK		(L1_S_TEX_MASK|L1_S_B|L1_S_C)
410 #define	L2_L_CACHE_MASK		(L2_L_TEX_MASK|L2_B|L2_C)
411 #define	L2_S_CACHE_MASK		(L2_S_TEX_MASK|L2_B|L2_C)
412 #else
413 #define	L1_S_CACHE_MASK		(L1_S_TEX_MASK|L1_S_B|L1_S_C|L1_SHARED)
414 #define	L2_L_CACHE_MASK		(L2_L_TEX_MASK|L2_B|L2_C|L2_SHARED)
415 #define	L2_S_CACHE_MASK		(L2_S_TEX_MASK|L2_B|L2_C|L2_SHARED)
416 #endif  /* SMP */
417 
418 #define	L1_S_PROTO		(L1_TYPE_S)
419 #define	L1_C_PROTO		(L1_TYPE_C)
420 #define	L2_S_PROTO		(L2_TYPE_S)
421 
422 /*
423  * Promotion to a 1MB (SECTION) mapping requires that the corresponding
424  * 4KB (SMALL) page mappings have identical settings for the following fields:
425  */
426 #define	L2_S_PROMOTE		(L2_S_REF | L2_SHARED | L2_S_PROT_MASK | \
427 				 L2_XN | L2_S_PROTO)
428 
429 /*
430  * In order to compare 1MB (SECTION) entry settings with the 4KB (SMALL)
431  * page mapping it is necessary to read and shift appropriate bits from
432  * L1 entry to positions of the corresponding bits in the L2 entry.
433  */
434 #define L1_S_DEMOTE(l1pd)	((((l1pd) & L1_S_PROTO) >> 0) | \
435 				(((l1pd) & L1_SHARED) >> 6) | \
436 				(((l1pd) & L1_S_REF) >> 6) | \
437 				(((l1pd) & L1_S_PROT_MASK) >> 6) | \
438 				(((l1pd) & L1_S_XN) >> 4))
439 
440 #ifndef SMP
441 #define ARM_L1S_STRONG_ORD	(0)
442 #define ARM_L1S_DEVICE_NOSHARE	(L1_S_TEX(2))
443 #define ARM_L1S_DEVICE_SHARE	(L1_S_B)
444 #define ARM_L1S_NRML_NOCACHE	(L1_S_TEX(1))
445 #define ARM_L1S_NRML_IWT_OWT	(L1_S_C)
446 #define ARM_L1S_NRML_IWB_OWB	(L1_S_C|L1_S_B)
447 #define ARM_L1S_NRML_IWBA_OWBA	(L1_S_TEX(1)|L1_S_C|L1_S_B)
448 
449 #define ARM_L2L_STRONG_ORD	(0)
450 #define ARM_L2L_DEVICE_NOSHARE	(L2_L_TEX(2))
451 #define ARM_L2L_DEVICE_SHARE	(L2_B)
452 #define ARM_L2L_NRML_NOCACHE	(L2_L_TEX(1))
453 #define ARM_L2L_NRML_IWT_OWT	(L2_C)
454 #define ARM_L2L_NRML_IWB_OWB	(L2_C|L2_B)
455 #define ARM_L2L_NRML_IWBA_OWBA	(L2_L_TEX(1)|L2_C|L2_B)
456 
457 #define ARM_L2S_STRONG_ORD	(0)
458 #define ARM_L2S_DEVICE_NOSHARE	(L2_S_TEX(2))
459 #define ARM_L2S_DEVICE_SHARE	(L2_B)
460 #define ARM_L2S_NRML_NOCACHE	(L2_S_TEX(1))
461 #define ARM_L2S_NRML_IWT_OWT	(L2_C)
462 #define ARM_L2S_NRML_IWB_OWB	(L2_C|L2_B)
463 #define ARM_L2S_NRML_IWBA_OWBA	(L2_S_TEX(1)|L2_C|L2_B)
464 #else
465 #define ARM_L1S_STRONG_ORD	(0)
466 #define ARM_L1S_DEVICE_NOSHARE	(L1_S_TEX(2))
467 #define ARM_L1S_DEVICE_SHARE	(L1_S_B)
468 #define ARM_L1S_NRML_NOCACHE	(L1_S_TEX(1)|L1_SHARED)
469 #define ARM_L1S_NRML_IWT_OWT	(L1_S_C|L1_SHARED)
470 #define ARM_L1S_NRML_IWB_OWB	(L1_S_C|L1_S_B|L1_SHARED)
471 #define ARM_L1S_NRML_IWBA_OWBA	(L1_S_TEX(1)|L1_S_C|L1_S_B|L1_SHARED)
472 
473 #define ARM_L2L_STRONG_ORD	(0)
474 #define ARM_L2L_DEVICE_NOSHARE	(L2_L_TEX(2))
475 #define ARM_L2L_DEVICE_SHARE	(L2_B)
476 #define ARM_L2L_NRML_NOCACHE	(L2_L_TEX(1)|L2_SHARED)
477 #define ARM_L2L_NRML_IWT_OWT	(L2_C|L2_SHARED)
478 #define ARM_L2L_NRML_IWB_OWB	(L2_C|L2_B|L2_SHARED)
479 #define ARM_L2L_NRML_IWBA_OWBA	(L2_L_TEX(1)|L2_C|L2_B|L2_SHARED)
480 
481 #define ARM_L2S_STRONG_ORD	(0)
482 #define ARM_L2S_DEVICE_NOSHARE	(L2_S_TEX(2))
483 #define ARM_L2S_DEVICE_SHARE	(L2_B)
484 #define ARM_L2S_NRML_NOCACHE	(L2_S_TEX(1)|L2_SHARED)
485 #define ARM_L2S_NRML_IWT_OWT	(L2_C|L2_SHARED)
486 #define ARM_L2S_NRML_IWB_OWB	(L2_C|L2_B|L2_SHARED)
487 #define ARM_L2S_NRML_IWBA_OWBA	(L2_S_TEX(1)|L2_C|L2_B|L2_SHARED)
488 #endif /* SMP */
489 #endif /* ARM_NMMUS > 1 */
490 
491 #if (ARM_MMU_SA1 == 1) && (ARM_NMMUS == 1)
492 #define	PMAP_NEEDS_PTE_SYNC	1
493 #define	PMAP_INCLUDE_PTE_SYNC
494 #elif defined(CPU_XSCALE_81342) || ARM_ARCH_6 || ARM_ARCH_7A
495 #define PMAP_NEEDS_PTE_SYNC	1
496 #define PMAP_INCLUDE_PTE_SYNC
497 #elif (ARM_MMU_SA1 == 0)
498 #define	PMAP_NEEDS_PTE_SYNC	0
499 #endif
500 
501 /*
502  * These macros return various bits based on kernel/user and protection.
503  * Note that the compiler will usually fold these at compile time.
504  */
505 #if (ARM_MMU_V6 + ARM_MMU_V7) == 0
506 
507 #define	L1_S_PROT_U		(L1_S_AP(AP_U))
508 #define	L1_S_PROT_W		(L1_S_AP(AP_W))
509 #define	L1_S_PROT_MASK		(L1_S_PROT_U|L1_S_PROT_W)
510 #define	L1_S_WRITABLE(pd)	((pd) & L1_S_PROT_W)
511 
512 #define	L1_S_PROT(ku, pr)	((((ku) == PTE_USER) ? L1_S_PROT_U : 0) | \
513 				 (((pr) & VM_PROT_WRITE) ? L1_S_PROT_W : 0))
514 
515 #define	L2_L_PROT_U		(L2_AP(AP_U))
516 #define	L2_L_PROT_W		(L2_AP(AP_W))
517 #define	L2_L_PROT_MASK		(L2_L_PROT_U|L2_L_PROT_W)
518 
519 #define	L2_L_PROT(ku, pr)	((((ku) == PTE_USER) ? L2_L_PROT_U : 0) | \
520 				 (((pr) & VM_PROT_WRITE) ? L2_L_PROT_W : 0))
521 
522 #define	L2_S_PROT(ku, pr)	((((ku) == PTE_USER) ? L2_S_PROT_U : 0) | \
523 				 (((pr) & VM_PROT_WRITE) ? L2_S_PROT_W : 0))
524 #else
525 #define	L1_S_PROT_U		(L1_S_AP(AP_U))
526 #define	L1_S_PROT_W		(L1_S_APX)		/* Write disable */
527 #define	L1_S_PROT_MASK		(L1_S_PROT_W|L1_S_PROT_U)
528 #define	L1_S_REF		(L1_S_AP(AP_REF))	/* Reference flag */
529 #define	L1_S_WRITABLE(pd)	(!((pd) & L1_S_PROT_W))
530 #define	L1_S_REFERENCED(pd)	((pd) & L1_S_REF)
531 
532 #define	L1_S_PROT(ku, pr)	(((((ku) == PTE_KERNEL) ? 0 : L1_S_PROT_U) | \
533 				 (((pr) & VM_PROT_WRITE) ? 0 : L1_S_PROT_W) | \
534 				 (((pr) & VM_PROT_EXECUTE) ? 0 : L1_S_XN)))
535 
536 #define	L2_L_PROT_MASK		(L2_APX|L2_AP0(0x3))
537 #define	L2_L_PROT(ku, pr)	(L2_L_PROT_MASK & ~((((ku) == PTE_KERNEL) ? L2_S_PROT_U : 0) | \
538 				 (((pr) & VM_PROT_WRITE) ? L2_APX : 0)))
539 
540 #define	L2_S_PROT(ku, pr)	(L2_S_PROT_MASK & ~((((ku) == PTE_KERNEL) ? L2_S_PROT_U : 0) | \
541 				 (((pr) & VM_PROT_WRITE) ? L2_APX : 0)))
542 
543 #endif
544 
545 /*
546  * Macros to test if a mapping is mappable with an L1 Section mapping
547  * or an L2 Large Page mapping.
548  */
549 #define	L1_S_MAPPABLE_P(va, pa, size)					\
550 	((((va) | (pa)) & L1_S_OFFSET) == 0 && (size) >= L1_S_SIZE)
551 
552 #define	L2_L_MAPPABLE_P(va, pa, size)					\
553 	((((va) | (pa)) & L2_L_OFFSET) == 0 && (size) >= L2_L_SIZE)
554 
555 /*
556  * Provide a fallback in case we were not able to determine it at
557  * compile-time.
558  */
559 #ifndef PMAP_NEEDS_PTE_SYNC
560 #define	PMAP_NEEDS_PTE_SYNC	pmap_needs_pte_sync
561 #define	PMAP_INCLUDE_PTE_SYNC
562 #endif
563 
564 #ifdef ARM_L2_PIPT
565 #define _sync_l2(pte, size) 	cpu_l2cache_wb_range(vtophys(pte), size)
566 #else
567 #define _sync_l2(pte, size) 	cpu_l2cache_wb_range(pte, size)
568 #endif
569 
570 #define	PTE_SYNC(pte)							\
571 do {									\
572 	if (PMAP_NEEDS_PTE_SYNC) {					\
573 		cpu_dcache_wb_range((vm_offset_t)(pte), sizeof(pt_entry_t));\
574 		cpu_drain_writebuf();					\
575 		_sync_l2((vm_offset_t)(pte), sizeof(pt_entry_t));\
576 	} else								\
577 		cpu_drain_writebuf();					\
578 } while (/*CONSTCOND*/0)
579 
580 #define	PTE_SYNC_RANGE(pte, cnt)					\
581 do {									\
582 	if (PMAP_NEEDS_PTE_SYNC) {					\
583 		cpu_dcache_wb_range((vm_offset_t)(pte),			\
584 		    (cnt) << 2); /* * sizeof(pt_entry_t) */		\
585 		cpu_drain_writebuf();					\
586 		_sync_l2((vm_offset_t)(pte),		 		\
587 		    (cnt) << 2); /* * sizeof(pt_entry_t) */		\
588 	} else								\
589 		cpu_drain_writebuf();					\
590 } while (/*CONSTCOND*/0)
591 
592 extern pt_entry_t		pte_l1_s_cache_mode;
593 extern pt_entry_t		pte_l1_s_cache_mask;
594 
595 extern pt_entry_t		pte_l2_l_cache_mode;
596 extern pt_entry_t		pte_l2_l_cache_mask;
597 
598 extern pt_entry_t		pte_l2_s_cache_mode;
599 extern pt_entry_t		pte_l2_s_cache_mask;
600 
601 extern pt_entry_t		pte_l1_s_cache_mode_pt;
602 extern pt_entry_t		pte_l2_l_cache_mode_pt;
603 extern pt_entry_t		pte_l2_s_cache_mode_pt;
604 
605 extern pt_entry_t		pte_l2_s_prot_u;
606 extern pt_entry_t		pte_l2_s_prot_w;
607 extern pt_entry_t		pte_l2_s_prot_mask;
608 
609 extern pt_entry_t		pte_l1_s_proto;
610 extern pt_entry_t		pte_l1_c_proto;
611 extern pt_entry_t		pte_l2_s_proto;
612 
613 extern void (*pmap_copy_page_func)(vm_paddr_t, vm_paddr_t);
614 extern void (*pmap_copy_page_offs_func)(vm_paddr_t a_phys,
615     vm_offset_t a_offs, vm_paddr_t b_phys, vm_offset_t b_offs, int cnt);
616 extern void (*pmap_zero_page_func)(vm_paddr_t, int, int);
617 
618 #if (ARM_MMU_GENERIC + ARM_MMU_V6 + ARM_MMU_V7 + ARM_MMU_SA1) != 0 || defined(CPU_XSCALE_81342)
619 void	pmap_copy_page_generic(vm_paddr_t, vm_paddr_t);
620 void	pmap_zero_page_generic(vm_paddr_t, int, int);
621 
622 void	pmap_pte_init_generic(void);
623 #if defined(CPU_ARM8)
624 void	pmap_pte_init_arm8(void);
625 #endif
626 #if defined(CPU_ARM9)
627 void	pmap_pte_init_arm9(void);
628 #endif /* CPU_ARM9 */
629 #if defined(CPU_ARM10)
630 void	pmap_pte_init_arm10(void);
631 #endif /* CPU_ARM10 */
632 #if (ARM_MMU_V6 + ARM_MMU_V7) != 0
633 void	pmap_pte_init_mmu_v6(void);
634 #endif /* (ARM_MMU_V6 + ARM_MMU_V7) != 0 */
635 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
636 
637 #if /* ARM_MMU_SA1 == */1
638 void	pmap_pte_init_sa1(void);
639 #endif /* ARM_MMU_SA1 == 1 */
640 
641 #if ARM_MMU_XSCALE == 1
642 void	pmap_copy_page_xscale(vm_paddr_t, vm_paddr_t);
643 void	pmap_zero_page_xscale(vm_paddr_t, int, int);
644 
645 void	pmap_pte_init_xscale(void);
646 
647 void	xscale_setup_minidata(vm_offset_t, vm_offset_t, vm_offset_t);
648 
649 void	pmap_use_minicache(vm_offset_t, vm_size_t);
650 #endif /* ARM_MMU_XSCALE == 1 */
651 #if defined(CPU_XSCALE_81342)
652 #define ARM_HAVE_SUPERSECTIONS
653 #endif
654 
655 #define PTE_KERNEL	0
656 #define PTE_USER	1
657 #define	l1pte_valid(pde)	((pde) != 0)
658 #define	l1pte_section_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_S)
659 #define	l1pte_page_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_C)
660 #define	l1pte_fpage_p(pde)	(((pde) & L1_TYPE_MASK) == L1_TYPE_F)
661 
662 #define l2pte_index(v)		(((v) & L2_ADDR_BITS) >> L2_S_SHIFT)
663 #define	l2pte_valid(pte)	((pte) != 0)
664 #define	l2pte_pa(pte)		((pte) & L2_S_FRAME)
665 #define l2pte_minidata(pte)	(((pte) & \
666 				 (L2_B | L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X)))\
667 				 == (L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X)))
668 
669 /* L1 and L2 page table macros */
670 #define pmap_pde_v(pde)		l1pte_valid(*(pde))
671 #define pmap_pde_section(pde)	l1pte_section_p(*(pde))
672 #define pmap_pde_page(pde)	l1pte_page_p(*(pde))
673 #define pmap_pde_fpage(pde)	l1pte_fpage_p(*(pde))
674 
675 #define	pmap_pte_v(pte)		l2pte_valid(*(pte))
676 #define	pmap_pte_pa(pte)	l2pte_pa(*(pte))
677 
678 /*
679  * Flags that indicate attributes of pages or mappings of pages.
680  *
681  * The PVF_MOD and PVF_REF flags are stored in the mdpage for each
682  * page.  PVF_WIRED, PVF_WRITE, and PVF_NC are kept in individual
683  * pv_entry's for each page.  They live in the same "namespace" so
684  * that we can clear multiple attributes at a time.
685  *
686  * Note the "non-cacheable" flag generally means the page has
687  * multiple mappings in a given address space.
688  */
689 #define	PVF_MOD		0x01		/* page is modified */
690 #define	PVF_REF		0x02		/* page is referenced */
691 #define	PVF_WIRED	0x04		/* mapping is wired */
692 #define	PVF_WRITE	0x08		/* mapping is writable */
693 #define	PVF_EXEC	0x10		/* mapping is executable */
694 #define	PVF_NC		0x20		/* mapping is non-cacheable */
695 #define	PVF_MWC		0x40		/* mapping is used multiple times in userland */
696 #define	PVF_UNMAN	0x80		/* mapping is unmanaged */
697 
698 void vector_page_setprot(int);
699 
700 #define SECTION_CACHE	0x1
701 #define SECTION_PT	0x2
702 void	pmap_kenter_section(vm_offset_t, vm_paddr_t, int flags);
703 #ifdef ARM_HAVE_SUPERSECTIONS
704 void	pmap_kenter_supersection(vm_offset_t, uint64_t, int flags);
705 #endif
706 
707 extern char *_tmppt;
708 
709 void	pmap_postinit(void);
710 
711 #ifdef ARM_USE_SMALL_ALLOC
712 void	arm_add_smallalloc_pages(void *, void *, int, int);
713 vm_offset_t arm_ptovirt(vm_paddr_t);
714 void arm_init_smallalloc(void);
715 struct arm_small_page {
716 	void *addr;
717 	TAILQ_ENTRY(arm_small_page) pg_list;
718 };
719 
720 #endif
721 
722 extern vm_paddr_t dump_avail[];
723 #endif	/* _KERNEL */
724 
725 #endif	/* !LOCORE */
726 
727 #endif	/* !_MACHINE_PMAP_H_ */
728