]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/arm/arm/pmap.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / arm / arm / pmap.c
1 /* From: $NetBSD: pmap.c,v 1.148 2004/04/03 04:35:48 bsh Exp $ */
2 /*-
3  * Copyright 2004 Olivier Houchard.
4  * Copyright 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Steve C. Woodford for Wasabi Systems, 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 for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 /*-
39  * Copyright (c) 2002-2003 Wasabi Systems, Inc.
40  * Copyright (c) 2001 Richard Earnshaw
41  * Copyright (c) 2001-2002 Christopher Gilbert
42  * All rights reserved.
43  *
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the company nor the name of the author may be used to
50  *    endorse or promote products derived from this software without specific
51  *    prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  */
65 /*-
66  * Copyright (c) 1999 The NetBSD Foundation, Inc.
67  * All rights reserved.
68  *
69  * This code is derived from software contributed to The NetBSD Foundation
70  * by Charles M. Hannum.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce the above copyright
78  *    notice, this list of conditions and the following disclaimer in the
79  *    documentation and/or other materials provided with the distribution.
80  *
81  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
82  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
83  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
84  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
85  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
86  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
87  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
88  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
89  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
90  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
91  * POSSIBILITY OF SUCH DAMAGE.
92  */
93
94 /*-
95  * Copyright (c) 1994-1998 Mark Brinicombe.
96  * Copyright (c) 1994 Brini.
97  * All rights reserved.
98  *
99  * This code is derived from software written for Brini by Mark Brinicombe
100  *
101  * Redistribution and use in source and binary forms, with or without
102  * modification, are permitted provided that the following conditions
103  * are met:
104  * 1. Redistributions of source code must retain the above copyright
105  *    notice, this list of conditions and the following disclaimer.
106  * 2. Redistributions in binary form must reproduce the above copyright
107  *    notice, this list of conditions and the following disclaimer in the
108  *    documentation and/or other materials provided with the distribution.
109  * 3. All advertising materials mentioning features or use of this software
110  *    must display the following acknowledgement:
111  *      This product includes software developed by Mark Brinicombe.
112  * 4. The name of the author may not be used to endorse or promote products
113  *    derived from this software without specific prior written permission.
114  *
115  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
116  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
117  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
118  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
119  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
120  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
121  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
122  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
124  *
125  * RiscBSD kernel project
126  *
127  * pmap.c
128  *
129  * Machine dependant vm stuff
130  *
131  * Created      : 20/09/94
132  */
133
134 /*
135  * Special compilation symbols
136  * PMAP_DEBUG           - Build in pmap_debug_level code
137  */
138 /* Include header files */
139
140 #include "opt_vm.h"
141
142 #include <sys/cdefs.h>
143 __FBSDID("$FreeBSD$");
144 #include <sys/param.h>
145 #include <sys/systm.h>
146 #include <sys/kernel.h>
147 #include <sys/ktr.h>
148 #include <sys/proc.h>
149 #include <sys/malloc.h>
150 #include <sys/msgbuf.h>
151 #include <sys/vmmeter.h>
152 #include <sys/mman.h>
153 #include <sys/smp.h>
154 #include <sys/sched.h>
155
156 #include <vm/vm.h>
157 #include <vm/uma.h>
158 #include <vm/pmap.h>
159 #include <vm/vm_kern.h>
160 #include <vm/vm_object.h>
161 #include <vm/vm_map.h>
162 #include <vm/vm_page.h>
163 #include <vm/vm_pageout.h>
164 #include <vm/vm_extern.h>
165 #include <sys/lock.h>
166 #include <sys/mutex.h>
167 #include <machine/md_var.h>
168 #include <machine/vmparam.h>
169 #include <machine/cpu.h>
170 #include <machine/cpufunc.h>
171 #include <machine/pcb.h>
172
173 #ifdef PMAP_DEBUG
174 #define PDEBUG(_lev_,_stat_) \
175         if (pmap_debug_level >= (_lev_)) \
176                 ((_stat_))
177 #define dprintf printf
178
179 int pmap_debug_level = 0;
180 #define PMAP_INLINE 
181 #else   /* PMAP_DEBUG */
182 #define PDEBUG(_lev_,_stat_) /* Nothing */
183 #define dprintf(x, arg...)
184 #define PMAP_INLINE __inline
185 #endif  /* PMAP_DEBUG */
186
187 extern struct pv_addr systempage;
188 /*
189  * Internal function prototypes
190  */
191 static void pmap_free_pv_entry (pv_entry_t);
192 static pv_entry_t pmap_get_pv_entry(void);
193
194 static void             pmap_enter_locked(pmap_t, vm_offset_t, vm_page_t,
195     vm_prot_t, boolean_t, int);
196 static void             pmap_fix_cache(struct vm_page *, pmap_t, vm_offset_t);
197 static void             pmap_alloc_l1(pmap_t);
198 static void             pmap_free_l1(pmap_t);
199
200 static int              pmap_clearbit(struct vm_page *, u_int);
201
202 static struct l2_bucket *pmap_get_l2_bucket(pmap_t, vm_offset_t);
203 static struct l2_bucket *pmap_alloc_l2_bucket(pmap_t, vm_offset_t);
204 static void             pmap_free_l2_bucket(pmap_t, struct l2_bucket *, u_int);
205 static vm_offset_t      kernel_pt_lookup(vm_paddr_t);
206
207 static MALLOC_DEFINE(M_VMPMAP, "pmap", "PMAP L1");
208
209 vm_offset_t virtual_avail;      /* VA of first avail page (after kernel bss) */
210 vm_offset_t virtual_end;        /* VA of last avail page (end of kernel AS) */
211 vm_offset_t pmap_curmaxkvaddr;
212 vm_paddr_t kernel_l1pa;
213
214 extern void *end;
215 vm_offset_t kernel_vm_end = 0;
216
217 struct pmap kernel_pmap_store;
218
219 static pt_entry_t *csrc_pte, *cdst_pte;
220 static vm_offset_t csrcp, cdstp;
221 static struct mtx cmtx;
222
223 static void             pmap_init_l1(struct l1_ttable *, pd_entry_t *);
224 /*
225  * These routines are called when the CPU type is identified to set up
226  * the PTE prototypes, cache modes, etc.
227  *
228  * The variables are always here, just in case LKMs need to reference
229  * them (though, they shouldn't).
230  */
231
232 pt_entry_t      pte_l1_s_cache_mode;
233 pt_entry_t      pte_l1_s_cache_mode_pt;
234 pt_entry_t      pte_l1_s_cache_mask;
235
236 pt_entry_t      pte_l2_l_cache_mode;
237 pt_entry_t      pte_l2_l_cache_mode_pt;
238 pt_entry_t      pte_l2_l_cache_mask;
239
240 pt_entry_t      pte_l2_s_cache_mode;
241 pt_entry_t      pte_l2_s_cache_mode_pt;
242 pt_entry_t      pte_l2_s_cache_mask;
243
244 pt_entry_t      pte_l2_s_prot_u;
245 pt_entry_t      pte_l2_s_prot_w;
246 pt_entry_t      pte_l2_s_prot_mask;
247
248 pt_entry_t      pte_l1_s_proto;
249 pt_entry_t      pte_l1_c_proto;
250 pt_entry_t      pte_l2_s_proto;
251
252 void            (*pmap_copy_page_func)(vm_paddr_t, vm_paddr_t);
253 void            (*pmap_zero_page_func)(vm_paddr_t, int, int);
254 /*
255  * Which pmap is currently 'live' in the cache
256  *
257  * XXXSCW: Fix for SMP ...
258  */
259 union pmap_cache_state *pmap_cache_state;
260
261 struct msgbuf *msgbufp = 0;
262
263 /*
264  * Crashdump maps.
265  */
266 static caddr_t crashdumpmap;
267
268 extern void bcopy_page(vm_offset_t, vm_offset_t);
269 extern void bzero_page(vm_offset_t);
270
271 extern vm_offset_t alloc_firstaddr;
272
273 char *_tmppt;
274
275 /*
276  * Metadata for L1 translation tables.
277  */
278 struct l1_ttable {
279         /* Entry on the L1 Table list */
280         SLIST_ENTRY(l1_ttable) l1_link;
281
282         /* Entry on the L1 Least Recently Used list */
283         TAILQ_ENTRY(l1_ttable) l1_lru;
284
285         /* Track how many domains are allocated from this L1 */
286         volatile u_int l1_domain_use_count;
287
288         /*
289          * A free-list of domain numbers for this L1.
290          * We avoid using ffs() and a bitmap to track domains since ffs()
291          * is slow on ARM.
292          */
293         u_int8_t l1_domain_first;
294         u_int8_t l1_domain_free[PMAP_DOMAINS];
295
296         /* Physical address of this L1 page table */
297         vm_paddr_t l1_physaddr;
298
299         /* KVA of this L1 page table */
300         pd_entry_t *l1_kva;
301 };
302
303 /*
304  * Convert a virtual address into its L1 table index. That is, the
305  * index used to locate the L2 descriptor table pointer in an L1 table.
306  * This is basically used to index l1->l1_kva[].
307  *
308  * Each L2 descriptor table represents 1MB of VA space.
309  */
310 #define L1_IDX(va)              (((vm_offset_t)(va)) >> L1_S_SHIFT)
311
312 /*
313  * L1 Page Tables are tracked using a Least Recently Used list.
314  *  - New L1s are allocated from the HEAD.
315  *  - Freed L1s are added to the TAIl.
316  *  - Recently accessed L1s (where an 'access' is some change to one of
317  *    the userland pmaps which owns this L1) are moved to the TAIL.
318  */
319 static TAILQ_HEAD(, l1_ttable) l1_lru_list;
320 /*
321  * A list of all L1 tables
322  */
323 static SLIST_HEAD(, l1_ttable) l1_list;
324 static struct mtx l1_lru_lock;
325
326 /*
327  * The l2_dtable tracks L2_BUCKET_SIZE worth of L1 slots.
328  *
329  * This is normally 16MB worth L2 page descriptors for any given pmap.
330  * Reference counts are maintained for L2 descriptors so they can be
331  * freed when empty.
332  */
333 struct l2_dtable {
334         /* The number of L2 page descriptors allocated to this l2_dtable */
335         u_int l2_occupancy;
336
337         /* List of L2 page descriptors */
338         struct l2_bucket {
339                 pt_entry_t *l2b_kva;    /* KVA of L2 Descriptor Table */
340                 vm_paddr_t l2b_phys;    /* Physical address of same */
341                 u_short l2b_l1idx;      /* This L2 table's L1 index */
342                 u_short l2b_occupancy;  /* How many active descriptors */
343         } l2_bucket[L2_BUCKET_SIZE];
344 };
345
346 /* pmap_kenter_internal flags */
347 #define KENTER_CACHE    0x1
348 #define KENTER_USER     0x2
349
350 /*
351  * Given an L1 table index, calculate the corresponding l2_dtable index
352  * and bucket index within the l2_dtable.
353  */
354 #define L2_IDX(l1idx)           (((l1idx) >> L2_BUCKET_LOG2) & \
355                                  (L2_SIZE - 1))
356 #define L2_BUCKET(l1idx)        ((l1idx) & (L2_BUCKET_SIZE - 1))
357
358 /*
359  * Given a virtual address, this macro returns the
360  * virtual address required to drop into the next L2 bucket.
361  */
362 #define L2_NEXT_BUCKET(va)      (((va) & L1_S_FRAME) + L1_S_SIZE)
363
364 /*
365  * L2 allocation.
366  */
367 #define pmap_alloc_l2_dtable()          \
368                 (void*)uma_zalloc(l2table_zone, M_NOWAIT|M_USE_RESERVE)
369 #define pmap_free_l2_dtable(l2)         \
370                 uma_zfree(l2table_zone, l2)
371
372 /*
373  * We try to map the page tables write-through, if possible.  However, not
374  * all CPUs have a write-through cache mode, so on those we have to sync
375  * the cache when we frob page tables.
376  *
377  * We try to evaluate this at compile time, if possible.  However, it's
378  * not always possible to do that, hence this run-time var.
379  */
380 int     pmap_needs_pte_sync;
381
382 /*
383  * Macro to determine if a mapping might be resident in the
384  * instruction cache and/or TLB
385  */
386 #define PV_BEEN_EXECD(f)  (((f) & (PVF_REF | PVF_EXEC)) == (PVF_REF | PVF_EXEC))
387
388 /*
389  * Macro to determine if a mapping might be resident in the
390  * data cache and/or TLB
391  */
392 #define PV_BEEN_REFD(f)   (((f) & PVF_REF) != 0)
393
394 #ifndef PMAP_SHPGPERPROC
395 #define PMAP_SHPGPERPROC 200
396 #endif
397
398 #define pmap_is_current(pm)     ((pm) == pmap_kernel() || \
399             curproc->p_vmspace->vm_map.pmap == (pm))
400 static uma_zone_t pvzone = NULL;
401 uma_zone_t l2zone;
402 static uma_zone_t l2table_zone;
403 static vm_offset_t pmap_kernel_l2dtable_kva;
404 static vm_offset_t pmap_kernel_l2ptp_kva;
405 static vm_paddr_t pmap_kernel_l2ptp_phys;
406 static struct vm_object pvzone_obj;
407 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
408
409 /*
410  * This list exists for the benefit of pmap_map_chunk().  It keeps track
411  * of the kernel L2 tables during bootstrap, so that pmap_map_chunk() can
412  * find them as necessary.
413  *
414  * Note that the data on this list MUST remain valid after initarm() returns,
415  * as pmap_bootstrap() uses it to contruct L2 table metadata.
416  */
417 SLIST_HEAD(, pv_addr) kernel_pt_list = SLIST_HEAD_INITIALIZER(kernel_pt_list);
418
419 static void
420 pmap_init_l1(struct l1_ttable *l1, pd_entry_t *l1pt)
421 {
422         int i;
423
424         l1->l1_kva = l1pt;
425         l1->l1_domain_use_count = 0;
426         l1->l1_domain_first = 0;
427
428         for (i = 0; i < PMAP_DOMAINS; i++)
429                 l1->l1_domain_free[i] = i + 1;
430
431         /*
432          * Copy the kernel's L1 entries to each new L1.
433          */
434         if (l1pt != pmap_kernel()->pm_l1->l1_kva)
435                 memcpy(l1pt, pmap_kernel()->pm_l1->l1_kva, L1_TABLE_SIZE);
436
437         if ((l1->l1_physaddr = pmap_extract(pmap_kernel(), (vm_offset_t)l1pt)) == 0)
438                 panic("pmap_init_l1: can't get PA of L1 at %p", l1pt);
439         SLIST_INSERT_HEAD(&l1_list, l1, l1_link);
440         TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
441 }
442
443 static vm_offset_t
444 kernel_pt_lookup(vm_paddr_t pa)
445 {
446         struct pv_addr *pv;
447
448         SLIST_FOREACH(pv, &kernel_pt_list, pv_list) {
449                 if (pv->pv_pa == pa)
450                         return (pv->pv_va);
451         }
452         return (0);
453 }
454
455 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0
456 void
457 pmap_pte_init_generic(void)
458 {
459
460         pte_l1_s_cache_mode = L1_S_B|L1_S_C;
461         pte_l1_s_cache_mask = L1_S_CACHE_MASK_generic;
462
463         pte_l2_l_cache_mode = L2_B|L2_C;
464         pte_l2_l_cache_mask = L2_L_CACHE_MASK_generic;
465
466         pte_l2_s_cache_mode = L2_B|L2_C;
467         pte_l2_s_cache_mask = L2_S_CACHE_MASK_generic;
468
469         /*
470          * If we have a write-through cache, set B and C.  If
471          * we have a write-back cache, then we assume setting
472          * only C will make those pages write-through.
473          */
474         if (cpufuncs.cf_dcache_wb_range == (void *) cpufunc_nullop) {
475                 pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
476                 pte_l2_l_cache_mode_pt = L2_B|L2_C;
477                 pte_l2_s_cache_mode_pt = L2_B|L2_C;
478         } else {
479                 pte_l1_s_cache_mode_pt = L1_S_C;
480                 pte_l2_l_cache_mode_pt = L2_C;
481                 pte_l2_s_cache_mode_pt = L2_C;
482         }
483
484         pte_l2_s_prot_u = L2_S_PROT_U_generic;
485         pte_l2_s_prot_w = L2_S_PROT_W_generic;
486         pte_l2_s_prot_mask = L2_S_PROT_MASK_generic;
487
488         pte_l1_s_proto = L1_S_PROTO_generic;
489         pte_l1_c_proto = L1_C_PROTO_generic;
490         pte_l2_s_proto = L2_S_PROTO_generic;
491
492         pmap_copy_page_func = pmap_copy_page_generic;
493         pmap_zero_page_func = pmap_zero_page_generic;
494 }
495
496 #if defined(CPU_ARM8)
497 void
498 pmap_pte_init_arm8(void)
499 {
500
501         /*
502          * ARM8 is compatible with generic, but we need to use
503          * the page tables uncached.
504          */
505         pmap_pte_init_generic();
506
507         pte_l1_s_cache_mode_pt = 0;
508         pte_l2_l_cache_mode_pt = 0;
509         pte_l2_s_cache_mode_pt = 0;
510 }
511 #endif /* CPU_ARM8 */
512
513 #if defined(CPU_ARM9) && defined(ARM9_CACHE_WRITE_THROUGH)
514 void
515 pmap_pte_init_arm9(void)
516 {
517
518         /*
519          * ARM9 is compatible with generic, but we want to use
520          * write-through caching for now.
521          */
522         pmap_pte_init_generic();
523
524         pte_l1_s_cache_mode = L1_S_C;
525         pte_l2_l_cache_mode = L2_C;
526         pte_l2_s_cache_mode = L2_C;
527
528         pte_l1_s_cache_mode_pt = L1_S_C;
529         pte_l2_l_cache_mode_pt = L2_C;
530         pte_l2_s_cache_mode_pt = L2_C;
531 }
532 #endif /* CPU_ARM9 */
533 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
534
535 #if defined(CPU_ARM10)
536 void
537 pmap_pte_init_arm10(void)
538 {
539
540         /*
541          * ARM10 is compatible with generic, but we want to use
542          * write-through caching for now.
543          */
544         pmap_pte_init_generic();
545
546         pte_l1_s_cache_mode = L1_S_B | L1_S_C;
547         pte_l2_l_cache_mode = L2_B | L2_C;
548         pte_l2_s_cache_mode = L2_B | L2_C;
549
550         pte_l1_s_cache_mode_pt = L1_S_C;
551         pte_l2_l_cache_mode_pt = L2_C;
552         pte_l2_s_cache_mode_pt = L2_C;
553
554 }
555 #endif /* CPU_ARM10 */
556
557 #if  ARM_MMU_SA1 == 1
558 void
559 pmap_pte_init_sa1(void)
560 {
561
562         /*
563          * The StrongARM SA-1 cache does not have a write-through
564          * mode.  So, do the generic initialization, then reset
565          * the page table cache mode to B=1,C=1, and note that
566          * the PTEs need to be sync'd.
567          */
568         pmap_pte_init_generic();
569
570         pte_l1_s_cache_mode_pt = L1_S_B|L1_S_C;
571         pte_l2_l_cache_mode_pt = L2_B|L2_C;
572         pte_l2_s_cache_mode_pt = L2_B|L2_C;
573
574         pmap_needs_pte_sync = 1;
575 }
576 #endif /* ARM_MMU_SA1 == 1*/
577
578 #if ARM_MMU_XSCALE == 1
579 #if (ARM_NMMUS > 1) || defined (CPU_XSCALE_CORE3)
580 static u_int xscale_use_minidata;
581 #endif
582
583 void
584 pmap_pte_init_xscale(void)
585 {
586         uint32_t auxctl;
587         int write_through = 0;
588
589         pte_l1_s_cache_mode = L1_S_B|L1_S_C|L1_S_XSCALE_P;
590         pte_l1_s_cache_mask = L1_S_CACHE_MASK_xscale;
591
592         pte_l2_l_cache_mode = L2_B|L2_C;
593         pte_l2_l_cache_mask = L2_L_CACHE_MASK_xscale;
594
595         pte_l2_s_cache_mode = L2_B|L2_C;
596         pte_l2_s_cache_mask = L2_S_CACHE_MASK_xscale;
597
598         pte_l1_s_cache_mode_pt = L1_S_C;
599         pte_l2_l_cache_mode_pt = L2_C;
600         pte_l2_s_cache_mode_pt = L2_C;
601 #ifdef XSCALE_CACHE_READ_WRITE_ALLOCATE
602         /*
603          * The XScale core has an enhanced mode where writes that
604          * miss the cache cause a cache line to be allocated.  This
605          * is significantly faster than the traditional, write-through
606          * behavior of this case.
607          */
608         pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_X);
609         pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_X);
610         pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_X);
611 #endif /* XSCALE_CACHE_READ_WRITE_ALLOCATE */
612 #ifdef XSCALE_CACHE_WRITE_THROUGH
613         /*
614          * Some versions of the XScale core have various bugs in
615          * their cache units, the work-around for which is to run
616          * the cache in write-through mode.  Unfortunately, this
617          * has a major (negative) impact on performance.  So, we
618          * go ahead and run fast-and-loose, in the hopes that we
619          * don't line up the planets in a way that will trip the
620          * bugs.
621          *
622          * However, we give you the option to be slow-but-correct.
623          */
624         write_through = 1;
625 #elif defined(XSCALE_CACHE_WRITE_BACK)
626         /* force write back cache mode */
627         write_through = 0;
628 #elif defined(CPU_XSCALE_PXA2X0)
629         /*
630          * Intel PXA2[15]0 processors are known to have a bug in
631          * write-back cache on revision 4 and earlier (stepping
632          * A[01] and B[012]).  Fixed for C0 and later.
633          */
634         {
635                 uint32_t id, type;
636
637                 id = cpufunc_id();
638                 type = id & ~(CPU_ID_XSCALE_COREREV_MASK|CPU_ID_REVISION_MASK);
639
640                 if (type == CPU_ID_PXA250 || type == CPU_ID_PXA210) {
641                         if ((id & CPU_ID_REVISION_MASK) < 5) {
642                                 /* write through for stepping A0-1 and B0-2 */
643                                 write_through = 1;
644                         }
645                 }
646         }
647 #endif /* XSCALE_CACHE_WRITE_THROUGH */
648
649         if (write_through) {
650                 pte_l1_s_cache_mode = L1_S_C;
651                 pte_l2_l_cache_mode = L2_C;
652                 pte_l2_s_cache_mode = L2_C;
653         }
654
655 #if (ARM_NMMUS > 1)
656         xscale_use_minidata = 1;
657 #endif
658
659         pte_l2_s_prot_u = L2_S_PROT_U_xscale;
660         pte_l2_s_prot_w = L2_S_PROT_W_xscale;
661         pte_l2_s_prot_mask = L2_S_PROT_MASK_xscale;
662
663         pte_l1_s_proto = L1_S_PROTO_xscale;
664         pte_l1_c_proto = L1_C_PROTO_xscale;
665         pte_l2_s_proto = L2_S_PROTO_xscale;
666
667 #ifdef CPU_XSCALE_CORE3
668         pmap_copy_page_func = pmap_copy_page_generic;
669         pmap_zero_page_func = pmap_zero_page_generic;
670         xscale_use_minidata = 0;
671         /* Make sure it is L2-cachable */
672         pte_l1_s_cache_mode |= L1_S_XSCALE_TEX(TEX_XSCALE_T);
673         pte_l1_s_cache_mode_pt = pte_l1_s_cache_mode &~ L1_S_XSCALE_P;
674         pte_l2_l_cache_mode |= L2_XSCALE_L_TEX(TEX_XSCALE_T) ;
675         pte_l2_l_cache_mode_pt = pte_l1_s_cache_mode;
676         pte_l2_s_cache_mode |= L2_XSCALE_T_TEX(TEX_XSCALE_T);
677         pte_l2_s_cache_mode_pt = pte_l2_s_cache_mode;
678
679 #else
680         pmap_copy_page_func = pmap_copy_page_xscale;
681         pmap_zero_page_func = pmap_zero_page_xscale;
682 #endif
683
684         /*
685          * Disable ECC protection of page table access, for now.
686          */
687         __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
688         auxctl &= ~XSCALE_AUXCTL_P;
689         __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
690 }
691
692 /*
693  * xscale_setup_minidata:
694  *
695  *      Set up the mini-data cache clean area.  We require the
696  *      caller to allocate the right amount of physically and
697  *      virtually contiguous space.
698  */
699 extern vm_offset_t xscale_minidata_clean_addr;
700 extern vm_size_t xscale_minidata_clean_size; /* already initialized */
701 void
702 xscale_setup_minidata(vm_offset_t l1pt, vm_offset_t va, vm_paddr_t pa)
703 {
704         pd_entry_t *pde = (pd_entry_t *) l1pt;
705         pt_entry_t *pte;
706         vm_size_t size;
707         uint32_t auxctl;
708
709         xscale_minidata_clean_addr = va;
710
711         /* Round it to page size. */
712         size = (xscale_minidata_clean_size + L2_S_OFFSET) & L2_S_FRAME;
713
714         for (; size != 0;
715              va += L2_S_SIZE, pa += L2_S_SIZE, size -= L2_S_SIZE) {
716                 pte = (pt_entry_t *) kernel_pt_lookup(
717                     pde[L1_IDX(va)] & L1_C_ADDR_MASK);
718                 if (pte == NULL)
719                         panic("xscale_setup_minidata: can't find L2 table for "
720                             "VA 0x%08x", (u_int32_t) va);
721                 pte[l2pte_index(va)] =
722                     L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
723                     L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);
724         }
725
726         /*
727          * Configure the mini-data cache for write-back with
728          * read/write-allocate.
729          *
730          * NOTE: In order to reconfigure the mini-data cache, we must
731          * make sure it contains no valid data!  In order to do that,
732          * we must issue a global data cache invalidate command!
733          *
734          * WE ASSUME WE ARE RUNNING UN-CACHED WHEN THIS ROUTINE IS CALLED!
735          * THIS IS VERY IMPORTANT!
736          */
737
738         /* Invalidate data and mini-data. */
739         __asm __volatile("mcr p15, 0, %0, c7, c6, 0" : : "r" (0));
740         __asm __volatile("mrc p15, 0, %0, c1, c0, 1" : "=r" (auxctl));
741         auxctl = (auxctl & ~XSCALE_AUXCTL_MD_MASK) | XSCALE_AUXCTL_MD_WB_RWA;
742         __asm __volatile("mcr p15, 0, %0, c1, c0, 1" : : "r" (auxctl));
743 }
744 #endif
745
746 /*
747  * Allocate an L1 translation table for the specified pmap.
748  * This is called at pmap creation time.
749  */
750 static void
751 pmap_alloc_l1(pmap_t pm)
752 {
753         struct l1_ttable *l1;
754         u_int8_t domain;
755
756         /*
757          * Remove the L1 at the head of the LRU list
758          */
759         mtx_lock(&l1_lru_lock);
760         l1 = TAILQ_FIRST(&l1_lru_list);
761         TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
762
763         /*
764          * Pick the first available domain number, and update
765          * the link to the next number.
766          */
767         domain = l1->l1_domain_first;
768         l1->l1_domain_first = l1->l1_domain_free[domain];
769
770         /*
771          * If there are still free domain numbers in this L1,
772          * put it back on the TAIL of the LRU list.
773          */
774         if (++l1->l1_domain_use_count < PMAP_DOMAINS)
775                 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
776
777         mtx_unlock(&l1_lru_lock);
778
779         /*
780          * Fix up the relevant bits in the pmap structure
781          */
782         pm->pm_l1 = l1;
783         pm->pm_domain = domain + 1;
784 }
785
786 /*
787  * Free an L1 translation table.
788  * This is called at pmap destruction time.
789  */
790 static void
791 pmap_free_l1(pmap_t pm)
792 {
793         struct l1_ttable *l1 = pm->pm_l1;
794
795         mtx_lock(&l1_lru_lock);
796
797         /*
798          * If this L1 is currently on the LRU list, remove it.
799          */
800         if (l1->l1_domain_use_count < PMAP_DOMAINS)
801                 TAILQ_REMOVE(&l1_lru_list, l1, l1_lru);
802
803         /*
804          * Free up the domain number which was allocated to the pmap
805          */
806         l1->l1_domain_free[pm->pm_domain - 1] = l1->l1_domain_first;
807         l1->l1_domain_first = pm->pm_domain - 1;
808         l1->l1_domain_use_count--;
809
810         /*
811          * The L1 now must have at least 1 free domain, so add
812          * it back to the LRU list. If the use count is zero,
813          * put it at the head of the list, otherwise it goes
814          * to the tail.
815          */
816         if (l1->l1_domain_use_count == 0) {
817                 TAILQ_INSERT_HEAD(&l1_lru_list, l1, l1_lru);
818         }       else
819                 TAILQ_INSERT_TAIL(&l1_lru_list, l1, l1_lru);
820
821         mtx_unlock(&l1_lru_lock);
822 }
823
824 /*
825  * Returns a pointer to the L2 bucket associated with the specified pmap
826  * and VA, or NULL if no L2 bucket exists for the address.
827  */
828 static PMAP_INLINE struct l2_bucket *
829 pmap_get_l2_bucket(pmap_t pm, vm_offset_t va)
830 {
831         struct l2_dtable *l2;
832         struct l2_bucket *l2b;
833         u_short l1idx;
834
835         l1idx = L1_IDX(va);
836
837         if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL ||
838             (l2b = &l2->l2_bucket[L2_BUCKET(l1idx)])->l2b_kva == NULL)
839                 return (NULL);
840
841         return (l2b);
842 }
843
844 /*
845  * Returns a pointer to the L2 bucket associated with the specified pmap
846  * and VA.
847  *
848  * If no L2 bucket exists, perform the necessary allocations to put an L2
849  * bucket/page table in place.
850  *
851  * Note that if a new L2 bucket/page was allocated, the caller *must*
852  * increment the bucket occupancy counter appropriately *before* 
853  * releasing the pmap's lock to ensure no other thread or cpu deallocates
854  * the bucket/page in the meantime.
855  */
856 static struct l2_bucket *
857 pmap_alloc_l2_bucket(pmap_t pm, vm_offset_t va)
858 {
859         struct l2_dtable *l2;
860         struct l2_bucket *l2b;
861         u_short l1idx;
862
863         l1idx = L1_IDX(va);
864
865         PMAP_ASSERT_LOCKED(pm);
866         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
867         if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
868                 /*
869                  * No mapping at this address, as there is
870                  * no entry in the L1 table.
871                  * Need to allocate a new l2_dtable.
872                  */
873 again_l2table:
874                 PMAP_UNLOCK(pm);
875                 vm_page_unlock_queues();
876                 if ((l2 = pmap_alloc_l2_dtable()) == NULL) {
877                         vm_page_lock_queues();
878                         PMAP_LOCK(pm);
879                         return (NULL);
880                 }
881                 vm_page_lock_queues();
882                 PMAP_LOCK(pm);
883                 if (pm->pm_l2[L2_IDX(l1idx)] != NULL) {
884                         PMAP_UNLOCK(pm);
885                         vm_page_unlock_queues();
886                         uma_zfree(l2table_zone, l2);
887                         vm_page_lock_queues();
888                         PMAP_LOCK(pm);
889                         l2 = pm->pm_l2[L2_IDX(l1idx)];
890                         if (l2 == NULL)
891                                 goto again_l2table;
892                         /*
893                          * Someone already allocated the l2_dtable while
894                          * we were doing the same.
895                          */
896                 } else {
897                         bzero(l2, sizeof(*l2));
898                         /*
899                          * Link it into the parent pmap
900                          */
901                         pm->pm_l2[L2_IDX(l1idx)] = l2;
902                 }
903         } 
904
905         l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
906
907         /*
908          * Fetch pointer to the L2 page table associated with the address.
909          */
910         if (l2b->l2b_kva == NULL) {
911                 pt_entry_t *ptep;
912
913                 /*
914                  * No L2 page table has been allocated. Chances are, this
915                  * is because we just allocated the l2_dtable, above.
916                  */
917 again_ptep:
918                 PMAP_UNLOCK(pm);
919                 vm_page_unlock_queues();
920                 ptep = (void*)uma_zalloc(l2zone, M_NOWAIT|M_USE_RESERVE);
921                 vm_page_lock_queues();
922                 PMAP_LOCK(pm);
923                 if (l2b->l2b_kva != 0) {
924                         /* We lost the race. */
925                         PMAP_UNLOCK(pm);
926                         vm_page_unlock_queues();
927                         uma_zfree(l2zone, ptep);
928                         vm_page_lock_queues();
929                         PMAP_LOCK(pm);
930                         if (l2b->l2b_kva == 0)
931                                 goto again_ptep;
932                         return (l2b);
933                 }
934                 l2b->l2b_phys = vtophys(ptep);
935                 if (ptep == NULL) {
936                         /*
937                          * Oops, no more L2 page tables available at this
938                          * time. We may need to deallocate the l2_dtable
939                          * if we allocated a new one above.
940                          */
941                         if (l2->l2_occupancy == 0) {
942                                 pm->pm_l2[L2_IDX(l1idx)] = NULL;
943                                 pmap_free_l2_dtable(l2);
944                         }
945                         return (NULL);
946                 }
947
948                 l2->l2_occupancy++;
949                 l2b->l2b_kva = ptep;
950                 l2b->l2b_l1idx = l1idx;
951         }
952
953         return (l2b);
954 }
955
956 static PMAP_INLINE void
957 #ifndef PMAP_INCLUDE_PTE_SYNC
958 pmap_free_l2_ptp(pt_entry_t *l2)
959 #else
960 pmap_free_l2_ptp(boolean_t need_sync, pt_entry_t *l2)
961 #endif
962 {
963 #ifdef PMAP_INCLUDE_PTE_SYNC
964         /*
965          * Note: With a write-back cache, we may need to sync this
966          * L2 table before re-using it.
967          * This is because it may have belonged to a non-current
968          * pmap, in which case the cache syncs would have been
969          * skipped when the pages were being unmapped. If the
970          * L2 table were then to be immediately re-allocated to
971          * the *current* pmap, it may well contain stale mappings
972          * which have not yet been cleared by a cache write-back
973          * and so would still be visible to the mmu.
974          */
975         if (need_sync)
976                 PTE_SYNC_RANGE(l2, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
977 #endif
978         uma_zfree(l2zone, l2);
979 }
980 /*
981  * One or more mappings in the specified L2 descriptor table have just been
982  * invalidated.
983  *
984  * Garbage collect the metadata and descriptor table itself if necessary.
985  *
986  * The pmap lock must be acquired when this is called (not necessary
987  * for the kernel pmap).
988  */
989 static void
990 pmap_free_l2_bucket(pmap_t pm, struct l2_bucket *l2b, u_int count)
991 {
992         struct l2_dtable *l2;
993         pd_entry_t *pl1pd, l1pd;
994         pt_entry_t *ptep;
995         u_short l1idx;
996
997
998         /*
999          * Update the bucket's reference count according to how many
1000          * PTEs the caller has just invalidated.
1001          */
1002         l2b->l2b_occupancy -= count;
1003
1004         /*
1005          * Note:
1006          *
1007          * Level 2 page tables allocated to the kernel pmap are never freed
1008          * as that would require checking all Level 1 page tables and
1009          * removing any references to the Level 2 page table. See also the
1010          * comment elsewhere about never freeing bootstrap L2 descriptors.
1011          *
1012          * We make do with just invalidating the mapping in the L2 table.
1013          *
1014          * This isn't really a big deal in practice and, in fact, leads
1015          * to a performance win over time as we don't need to continually
1016          * alloc/free.
1017          */
1018         if (l2b->l2b_occupancy > 0 || pm == pmap_kernel())
1019                 return;
1020
1021         /*
1022          * There are no more valid mappings in this level 2 page table.
1023          * Go ahead and NULL-out the pointer in the bucket, then
1024          * free the page table.
1025          */
1026         l1idx = l2b->l2b_l1idx;
1027         ptep = l2b->l2b_kva;
1028         l2b->l2b_kva = NULL;
1029
1030         pl1pd = &pm->pm_l1->l1_kva[l1idx];
1031
1032         /*
1033          * If the L1 slot matches the pmap's domain
1034          * number, then invalidate it.
1035          */
1036         l1pd = *pl1pd & (L1_TYPE_MASK | L1_C_DOM_MASK);
1037         if (l1pd == (L1_C_DOM(pm->pm_domain) | L1_TYPE_C)) {
1038                 *pl1pd = 0;
1039                 PTE_SYNC(pl1pd);
1040         }
1041
1042         /*
1043          * Release the L2 descriptor table back to the pool cache.
1044          */
1045 #ifndef PMAP_INCLUDE_PTE_SYNC
1046         pmap_free_l2_ptp(ptep);
1047 #else
1048         pmap_free_l2_ptp(!pmap_is_current(pm), ptep);
1049 #endif
1050
1051         /*
1052          * Update the reference count in the associated l2_dtable
1053          */
1054         l2 = pm->pm_l2[L2_IDX(l1idx)];
1055         if (--l2->l2_occupancy > 0)
1056                 return;
1057
1058         /*
1059          * There are no more valid mappings in any of the Level 1
1060          * slots managed by this l2_dtable. Go ahead and NULL-out
1061          * the pointer in the parent pmap and free the l2_dtable.
1062          */
1063         pm->pm_l2[L2_IDX(l1idx)] = NULL;
1064         pmap_free_l2_dtable(l2);
1065 }
1066
1067 /*
1068  * Pool cache constructors for L2 descriptor tables, metadata and pmap
1069  * structures.
1070  */
1071 static int
1072 pmap_l2ptp_ctor(void *mem, int size, void *arg, int flags)
1073 {
1074 #ifndef PMAP_INCLUDE_PTE_SYNC
1075         struct l2_bucket *l2b;
1076         pt_entry_t *ptep, pte;
1077 #ifdef ARM_USE_SMALL_ALLOC
1078         pd_entry_t *pde;
1079 #endif
1080         vm_offset_t va = (vm_offset_t)mem & ~PAGE_MASK;
1081
1082         /*
1083          * The mappings for these page tables were initially made using
1084          * pmap_kenter() by the pool subsystem. Therefore, the cache-
1085          * mode will not be right for page table mappings. To avoid
1086          * polluting the pmap_kenter() code with a special case for
1087          * page tables, we simply fix up the cache-mode here if it's not
1088          * correct.
1089          */
1090 #ifdef ARM_USE_SMALL_ALLOC
1091         pde = &kernel_pmap->pm_l1->l1_kva[L1_IDX(va)];
1092         if (!l1pte_section_p(*pde)) {
1093 #endif
1094                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
1095                 ptep = &l2b->l2b_kva[l2pte_index(va)];
1096                 pte = *ptep;
1097                 
1098                 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
1099                         /*
1100                          * Page tables must have the cache-mode set to 
1101                          * Write-Thru.
1102                          */
1103                         *ptep = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
1104                         PTE_SYNC(ptep);
1105                         cpu_tlb_flushD_SE(va);
1106                         cpu_cpwait();
1107                 }
1108 #ifdef ARM_USE_SMALL_ALLOC
1109         }
1110 #endif
1111 #endif
1112         memset(mem, 0, L2_TABLE_SIZE_REAL);
1113         PTE_SYNC_RANGE(mem, L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
1114         return (0);
1115 }
1116
1117 /*
1118  * A bunch of routines to conditionally flush the caches/TLB depending
1119  * on whether the specified pmap actually needs to be flushed at any
1120  * given time.
1121  */
1122 static PMAP_INLINE void
1123 pmap_tlb_flushID_SE(pmap_t pm, vm_offset_t va)
1124 {
1125
1126         if (pmap_is_current(pm))
1127                 cpu_tlb_flushID_SE(va);
1128 }
1129
1130 static PMAP_INLINE void
1131 pmap_tlb_flushD_SE(pmap_t pm, vm_offset_t va)
1132 {
1133
1134         if (pmap_is_current(pm))
1135                 cpu_tlb_flushD_SE(va);
1136 }
1137
1138 static PMAP_INLINE void
1139 pmap_tlb_flushID(pmap_t pm)
1140 {
1141
1142         if (pmap_is_current(pm))
1143                 cpu_tlb_flushID();
1144 }
1145 static PMAP_INLINE void
1146 pmap_tlb_flushD(pmap_t pm)
1147 {
1148
1149         if (pmap_is_current(pm))
1150                 cpu_tlb_flushD();
1151 }
1152
1153 static int
1154 pmap_has_valid_mapping(pmap_t pm, vm_offset_t va)
1155 {
1156         pd_entry_t *pde;
1157         pt_entry_t *ptep;
1158
1159         if (pmap_get_pde_pte(pm, va, &pde, &ptep) &&
1160             ptep && ((*ptep & L2_TYPE_MASK) != L2_TYPE_INV))
1161                 return (1);
1162
1163         return (0);
1164 }
1165
1166 static PMAP_INLINE void
1167 pmap_idcache_wbinv_range(pmap_t pm, vm_offset_t va, vm_size_t len)
1168 {
1169         vm_size_t rest;
1170
1171         CTR4(KTR_PMAP, "pmap_dcache_wbinv_range: pmap %p is_kernel %d va 0x%08x"
1172             " len 0x%x ", pm, pm == pmap_kernel(), va, len);
1173
1174         if (pmap_is_current(pm) || pm == pmap_kernel()) {
1175                 rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1176                 while (len > 0) {
1177                         if (pmap_has_valid_mapping(pm, va)) {
1178                                 cpu_idcache_wbinv_range(va, rest);
1179                                 cpu_l2cache_wbinv_range(va, rest);
1180                         }
1181                         len -= rest;
1182                         va += rest;
1183                         rest = MIN(PAGE_SIZE, len);
1184                 }
1185         }
1186 }
1187
1188 static PMAP_INLINE void
1189 pmap_dcache_wb_range(pmap_t pm, vm_offset_t va, vm_size_t len, boolean_t do_inv,
1190     boolean_t rd_only)
1191 {
1192         vm_size_t rest;
1193
1194         CTR4(KTR_PMAP, "pmap_dcache_wb_range: pmap %p is_kernel %d va 0x%08x "
1195             "len 0x%x ", pm, pm == pmap_kernel(), va, len);
1196         CTR2(KTR_PMAP, " do_inv %d rd_only %d", do_inv, rd_only);
1197
1198         if (pmap_is_current(pm)) {
1199                 rest = MIN(PAGE_SIZE - (va & PAGE_MASK), len);
1200                 while (len > 0) {
1201                         if (pmap_has_valid_mapping(pm, va)) {
1202                                 if (do_inv && rd_only) {
1203                                         cpu_dcache_inv_range(va, rest);
1204                                         cpu_l2cache_inv_range(va, rest);
1205                                 } else if (do_inv) {
1206                                         cpu_dcache_wbinv_range(va, rest);
1207                                         cpu_l2cache_wbinv_range(va, rest);
1208                                 } else if (!rd_only) {
1209                                         cpu_dcache_wb_range(va, rest);
1210                                         cpu_l2cache_wb_range(va, rest);
1211                                 }
1212                         }
1213                         len -= rest;
1214                         va += rest;
1215
1216                         rest = MIN(PAGE_SIZE, len);
1217                 }
1218         }
1219 }
1220
1221 static PMAP_INLINE void
1222 pmap_idcache_wbinv_all(pmap_t pm)
1223 {
1224
1225         if (pmap_is_current(pm)) {
1226                 cpu_idcache_wbinv_all();
1227                 cpu_l2cache_wbinv_all();
1228         }
1229 }
1230
1231 #ifdef notyet
1232 static PMAP_INLINE void
1233 pmap_dcache_wbinv_all(pmap_t pm)
1234 {
1235
1236         if (pmap_is_current(pm)) {
1237                 cpu_dcache_wbinv_all();
1238                 cpu_l2cache_wbinv_all();
1239         }
1240 }
1241 #endif
1242
1243 /*
1244  * PTE_SYNC_CURRENT:
1245  *
1246  *     Make sure the pte is written out to RAM.
1247  *     We need to do this for one of two cases:
1248  *       - We're dealing with the kernel pmap
1249  *       - There is no pmap active in the cache/tlb.
1250  *       - The specified pmap is 'active' in the cache/tlb.
1251  */
1252 #ifdef PMAP_INCLUDE_PTE_SYNC
1253 #define PTE_SYNC_CURRENT(pm, ptep)      \
1254 do {                                    \
1255         if (PMAP_NEEDS_PTE_SYNC &&      \
1256             pmap_is_current(pm))        \
1257                 PTE_SYNC(ptep);         \
1258 } while (/*CONSTCOND*/0)
1259 #else
1260 #define PTE_SYNC_CURRENT(pm, ptep)      /* nothing */
1261 #endif
1262
1263 /*
1264  * cacheable == -1 means we must make the entry uncacheable, 1 means
1265  * cacheable;
1266  */
1267 static __inline void
1268 pmap_set_cache_entry(pv_entry_t pv, pmap_t pm, vm_offset_t va, int cacheable)
1269 {
1270         struct l2_bucket *l2b;
1271         pt_entry_t *ptep, pte;
1272
1273         l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
1274         ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
1275
1276         if (cacheable == 1) {
1277                 pte = (*ptep & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode;
1278                 if (l2pte_valid(pte)) {
1279                         if (PV_BEEN_EXECD(pv->pv_flags)) {
1280                                 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1281                         } else if (PV_BEEN_REFD(pv->pv_flags)) {
1282                                 pmap_tlb_flushD_SE(pv->pv_pmap, pv->pv_va);
1283                         }
1284                 }
1285         } else {
1286                 pte = *ptep &~ L2_S_CACHE_MASK;
1287                 if ((va != pv->pv_va || pm != pv->pv_pmap) &&
1288                             l2pte_valid(pte)) {
1289                         if (PV_BEEN_EXECD(pv->pv_flags)) {
1290                                 pmap_idcache_wbinv_range(pv->pv_pmap,
1291                                             pv->pv_va, PAGE_SIZE);
1292                                 pmap_tlb_flushID_SE(pv->pv_pmap, pv->pv_va);
1293                         } else if (PV_BEEN_REFD(pv->pv_flags)) {
1294                                 pmap_dcache_wb_range(pv->pv_pmap,
1295                                             pv->pv_va, PAGE_SIZE, TRUE,
1296                                             (pv->pv_flags & PVF_WRITE) == 0);
1297                                 pmap_tlb_flushD_SE(pv->pv_pmap,
1298                                             pv->pv_va);
1299                         }
1300                 }
1301         }
1302         *ptep = pte;
1303         PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
1304 }
1305
1306 static void
1307 pmap_fix_cache(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1308 {
1309         int pmwc = 0;
1310         int writable = 0, kwritable = 0, uwritable = 0;
1311         int entries = 0, kentries = 0, uentries = 0;
1312         struct pv_entry *pv;
1313
1314         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1315
1316         /* the cache gets written back/invalidated on context switch.
1317          * therefore, if a user page shares an entry in the same page or
1318          * with the kernel map and at least one is writable, then the
1319          * cache entry must be set write-through.
1320          */
1321
1322         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1323                         /* generate a count of the pv_entry uses */
1324                 if (pv->pv_flags & PVF_WRITE) {
1325                         if (pv->pv_pmap == pmap_kernel())
1326                                 kwritable++;
1327                         else if (pv->pv_pmap == pm)
1328                                 uwritable++;
1329                         writable++;
1330                 }
1331                 if (pv->pv_pmap == pmap_kernel())
1332                         kentries++;
1333                 else {
1334                         if (pv->pv_pmap == pm)
1335                                 uentries++;
1336                         entries++;
1337                 }
1338         }
1339                 /*
1340                  * check if the user duplicate mapping has
1341                  * been removed.
1342                  */
1343         if ((pm != pmap_kernel()) && (((uentries > 1) && uwritable) ||
1344             (uwritable > 1)))
1345                         pmwc = 1;
1346
1347         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1348                 /* check for user uncachable conditions - order is important */
1349                 if (pm != pmap_kernel() &&
1350                     (pv->pv_pmap == pm || pv->pv_pmap == pmap_kernel())) {
1351
1352                         if ((uentries > 1 && uwritable) || uwritable > 1) {
1353
1354                                 /* user duplicate mapping */
1355                                 if (pv->pv_pmap != pmap_kernel())
1356                                         pv->pv_flags |= PVF_MWC;
1357
1358                                 if (!(pv->pv_flags & PVF_NC)) {
1359                                         pv->pv_flags |= PVF_NC;
1360                                         pmap_set_cache_entry(pv, pm, va, -1);
1361                                 }
1362                                 continue;
1363                         } else  /* no longer a duplicate user */
1364                                 pv->pv_flags &= ~PVF_MWC;
1365                 }
1366
1367                 /*
1368                  * check for kernel uncachable conditions
1369                  * kernel writable or kernel readable with writable user entry
1370                  */
1371                 if ((kwritable && (entries || kentries > 1)) ||
1372                     (kwritable > 1) ||
1373                     ((kwritable != writable) && kentries &&
1374                      (pv->pv_pmap == pmap_kernel() ||
1375                       (pv->pv_flags & PVF_WRITE) ||
1376                       (pv->pv_flags & PVF_MWC)))) {
1377
1378                         if (!(pv->pv_flags & PVF_NC)) {
1379                                 pv->pv_flags |= PVF_NC;
1380                                 pmap_set_cache_entry(pv, pm, va, -1);
1381                         }
1382                         continue;
1383                 }
1384
1385                         /* kernel and user are cachable */
1386                 if ((pm == pmap_kernel()) && !(pv->pv_flags & PVF_MWC) &&
1387                     (pv->pv_flags & PVF_NC)) {
1388
1389                         pv->pv_flags &= ~PVF_NC;
1390                         pmap_set_cache_entry(pv, pm, va, 1);
1391                         continue;
1392                 }
1393                         /* user is no longer sharable and writable */
1394                 if (pm != pmap_kernel() &&
1395                     (pv->pv_pmap == pm || pv->pv_pmap == pmap_kernel()) &&
1396                     !pmwc && (pv->pv_flags & PVF_NC)) {
1397
1398                         pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1399                         pmap_set_cache_entry(pv, pm, va, 1);
1400                 }
1401         }
1402
1403         if ((kwritable == 0) && (writable == 0)) {
1404                 pg->md.pvh_attrs &= ~PVF_MOD;
1405                 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1406                 return;
1407         }
1408 }
1409
1410 /*
1411  * Modify pte bits for all ptes corresponding to the given physical address.
1412  * We use `maskbits' rather than `clearbits' because we're always passing
1413  * constants and the latter would require an extra inversion at run-time.
1414  */
1415 static int 
1416 pmap_clearbit(struct vm_page *pg, u_int maskbits)
1417 {
1418         struct l2_bucket *l2b;
1419         struct pv_entry *pv;
1420         pt_entry_t *ptep, npte, opte;
1421         pmap_t pm;
1422         vm_offset_t va;
1423         u_int oflags;
1424         int count = 0;
1425
1426         vm_page_lock_queues();
1427
1428         if (maskbits & PVF_WRITE)
1429                 maskbits |= PVF_MOD;
1430         /*
1431          * Clear saved attributes (modify, reference)
1432          */
1433         pg->md.pvh_attrs &= ~(maskbits & (PVF_MOD | PVF_REF));
1434
1435         if (TAILQ_EMPTY(&pg->md.pv_list)) {
1436                 vm_page_unlock_queues();
1437                 return (0);
1438         }
1439
1440         /*
1441          * Loop over all current mappings setting/clearing as appropos
1442          */
1443         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list) {
1444                 va = pv->pv_va;
1445                 pm = pv->pv_pmap;
1446                 oflags = pv->pv_flags;
1447
1448                 if (!(oflags & maskbits)) {
1449                         if ((maskbits & PVF_WRITE) && (pv->pv_flags & PVF_NC)) {
1450                                 /* It is safe to re-enable cacheing here. */
1451                                 PMAP_LOCK(pm);
1452                                 l2b = pmap_get_l2_bucket(pm, va);
1453                                 ptep = &l2b->l2b_kva[l2pte_index(va)];
1454                                 *ptep |= pte_l2_s_cache_mode;
1455                                 PTE_SYNC(ptep);
1456                                 PMAP_UNLOCK(pm);
1457                                 pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1458                                 
1459                         }
1460                         continue;
1461                 }
1462                 pv->pv_flags &= ~maskbits;
1463
1464                 PMAP_LOCK(pm);
1465
1466                 l2b = pmap_get_l2_bucket(pm, va);
1467
1468                 ptep = &l2b->l2b_kva[l2pte_index(va)];
1469                 npte = opte = *ptep;
1470
1471                 if (maskbits & (PVF_WRITE|PVF_MOD)) {
1472                         if ((pv->pv_flags & PVF_NC)) {
1473                                 /* 
1474                                  * Entry is not cacheable:
1475                                  *
1476                                  * Don't turn caching on again if this is a 
1477                                  * modified emulation. This would be
1478                                  * inconsitent with the settings created by
1479                                  * pmap_fix_cache(). Otherwise, it's safe
1480                                  * to re-enable cacheing.
1481                                  *
1482                                  * There's no need to call pmap_fix_cache()
1483                                  * here: all pages are losing their write
1484                                  * permission.
1485                                  */
1486                                 if (maskbits & PVF_WRITE) {
1487                                         npte |= pte_l2_s_cache_mode;
1488                                         pv->pv_flags &= ~(PVF_NC | PVF_MWC);
1489                                 }
1490                         } else
1491                         if (opte & L2_S_PROT_W) {
1492                                 vm_page_dirty(pg);
1493                                 /* 
1494                                  * Entry is writable/cacheable: check if pmap
1495                                  * is current if it is flush it, otherwise it
1496                                  * won't be in the cache
1497                                  */
1498                                 if (PV_BEEN_EXECD(oflags))
1499                                         pmap_idcache_wbinv_range(pm, pv->pv_va,
1500                                             PAGE_SIZE);
1501                                 else
1502                                 if (PV_BEEN_REFD(oflags))
1503                                         pmap_dcache_wb_range(pm, pv->pv_va,
1504                                             PAGE_SIZE,
1505                                             (maskbits & PVF_REF) ? TRUE : FALSE,
1506                                             FALSE);
1507                         }
1508
1509                         /* make the pte read only */
1510                         npte &= ~L2_S_PROT_W;
1511                 }
1512
1513                 if (maskbits & PVF_REF) {
1514                         if ((pv->pv_flags & PVF_NC) == 0 &&
1515                             (maskbits & (PVF_WRITE|PVF_MOD)) == 0) {
1516                                 /*
1517                                  * Check npte here; we may have already
1518                                  * done the wbinv above, and the validity
1519                                  * of the PTE is the same for opte and
1520                                  * npte.
1521                                  */
1522                                 if (npte & L2_S_PROT_W) {
1523                                         if (PV_BEEN_EXECD(oflags))
1524                                                 pmap_idcache_wbinv_range(pm,
1525                                                     pv->pv_va, PAGE_SIZE);
1526                                         else
1527                                         if (PV_BEEN_REFD(oflags))
1528                                                 pmap_dcache_wb_range(pm,
1529                                                     pv->pv_va, PAGE_SIZE,
1530                                                     TRUE, FALSE);
1531                                 } else
1532                                 if ((npte & L2_TYPE_MASK) != L2_TYPE_INV) {
1533                                         /* XXXJRT need idcache_inv_range */
1534                                         if (PV_BEEN_EXECD(oflags))
1535                                                 pmap_idcache_wbinv_range(pm,
1536                                                     pv->pv_va, PAGE_SIZE);
1537                                         else
1538                                         if (PV_BEEN_REFD(oflags))
1539                                                 pmap_dcache_wb_range(pm,
1540                                                     pv->pv_va, PAGE_SIZE,
1541                                                     TRUE, TRUE);
1542                                 }
1543                         }
1544
1545                         /*
1546                          * Make the PTE invalid so that we will take a
1547                          * page fault the next time the mapping is
1548                          * referenced.
1549                          */
1550                         npte &= ~L2_TYPE_MASK;
1551                         npte |= L2_TYPE_INV;
1552                 }
1553
1554                 if (npte != opte) {
1555                         count++;
1556                         *ptep = npte;
1557                         PTE_SYNC(ptep);
1558                         /* Flush the TLB entry if a current pmap. */
1559                         if (PV_BEEN_EXECD(oflags))
1560                                 pmap_tlb_flushID_SE(pm, pv->pv_va);
1561                         else
1562                         if (PV_BEEN_REFD(oflags))
1563                                 pmap_tlb_flushD_SE(pm, pv->pv_va);
1564                 }
1565
1566                 PMAP_UNLOCK(pm);
1567
1568         }
1569
1570         if (maskbits & PVF_WRITE)
1571                 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1572         vm_page_unlock_queues();
1573         return (count);
1574 }
1575
1576 /*
1577  * main pv_entry manipulation functions:
1578  *   pmap_enter_pv: enter a mapping onto a vm_page list
1579  *   pmap_remove_pv: remove a mappiing from a vm_page list
1580  *
1581  * NOTE: pmap_enter_pv expects to lock the pvh itself
1582  *       pmap_remove_pv expects te caller to lock the pvh before calling
1583  */
1584
1585 /*
1586  * pmap_enter_pv: enter a mapping onto a vm_page lst
1587  *
1588  * => caller should hold the proper lock on pmap_main_lock
1589  * => caller should have pmap locked
1590  * => we will gain the lock on the vm_page and allocate the new pv_entry
1591  * => caller should adjust ptp's wire_count before calling
1592  * => caller should not adjust pmap's wire_count
1593  */
1594 static void
1595 pmap_enter_pv(struct vm_page *pg, struct pv_entry *pve, pmap_t pm,
1596     vm_offset_t va, u_int flags)
1597 {
1598
1599         int km;
1600
1601         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1602
1603         if (pg->md.pv_kva) {
1604                 /* PMAP_ASSERT_LOCKED(pmap_kernel()); */
1605                 pve->pv_pmap = pmap_kernel();
1606                 pve->pv_va = pg->md.pv_kva;
1607                 pve->pv_flags = PVF_WRITE | PVF_UNMAN;
1608                 pg->md.pv_kva = 0;
1609
1610                 if (!(km = PMAP_OWNED(pmap_kernel())))
1611                         PMAP_LOCK(pmap_kernel());
1612                 TAILQ_INSERT_HEAD(&pg->md.pv_list, pve, pv_list);
1613                 TAILQ_INSERT_HEAD(&pve->pv_pmap->pm_pvlist, pve, pv_plist);
1614                 PMAP_UNLOCK(pmap_kernel());
1615                 vm_page_unlock_queues();
1616                 if ((pve = pmap_get_pv_entry()) == NULL)
1617                         panic("pmap_kenter_internal: no pv entries");
1618                 vm_page_lock_queues();
1619                 if (km)
1620                         PMAP_LOCK(pmap_kernel());
1621         }
1622
1623         PMAP_ASSERT_LOCKED(pm);
1624         pve->pv_pmap = pm;
1625         pve->pv_va = va;
1626         pve->pv_flags = flags;
1627
1628         TAILQ_INSERT_HEAD(&pg->md.pv_list, pve, pv_list);
1629         TAILQ_INSERT_HEAD(&pm->pm_pvlist, pve, pv_plist);
1630         pg->md.pvh_attrs |= flags & (PVF_REF | PVF_MOD);
1631         if (pve->pv_flags & PVF_WIRED)
1632                 ++pm->pm_stats.wired_count;
1633         vm_page_aflag_set(pg, PGA_REFERENCED);
1634 }
1635
1636 /*
1637  *
1638  * pmap_find_pv: Find a pv entry
1639  *
1640  * => caller should hold lock on vm_page
1641  */
1642 static PMAP_INLINE struct pv_entry *
1643 pmap_find_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1644 {
1645         struct pv_entry *pv;
1646
1647         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1648         TAILQ_FOREACH(pv, &pg->md.pv_list, pv_list)
1649             if (pm == pv->pv_pmap && va == pv->pv_va)
1650                     break;
1651         return (pv);
1652 }
1653
1654 /*
1655  * vector_page_setprot:
1656  *
1657  *      Manipulate the protection of the vector page.
1658  */
1659 void
1660 vector_page_setprot(int prot)
1661 {
1662         struct l2_bucket *l2b;
1663         pt_entry_t *ptep;
1664
1665         l2b = pmap_get_l2_bucket(pmap_kernel(), vector_page);
1666
1667         ptep = &l2b->l2b_kva[l2pte_index(vector_page)];
1668
1669         *ptep = (*ptep & ~L1_S_PROT_MASK) | L2_S_PROT(PTE_KERNEL, prot);
1670         PTE_SYNC(ptep);
1671         cpu_tlb_flushD_SE(vector_page);
1672         cpu_cpwait();
1673 }
1674
1675 /*
1676  * pmap_remove_pv: try to remove a mapping from a pv_list
1677  *
1678  * => caller should hold proper lock on pmap_main_lock
1679  * => pmap should be locked
1680  * => caller should hold lock on vm_page [so that attrs can be adjusted]
1681  * => caller should adjust ptp's wire_count and free PTP if needed
1682  * => caller should NOT adjust pmap's wire_count
1683  * => we return the removed pve
1684  */
1685
1686 static void
1687 pmap_nuke_pv(struct vm_page *pg, pmap_t pm, struct pv_entry *pve)
1688 {
1689
1690         struct pv_entry *pv;
1691         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1692         PMAP_ASSERT_LOCKED(pm);
1693         TAILQ_REMOVE(&pg->md.pv_list, pve, pv_list);
1694         TAILQ_REMOVE(&pm->pm_pvlist, pve, pv_plist);
1695         if (pve->pv_flags & PVF_WIRED)
1696                 --pm->pm_stats.wired_count;
1697         if (pg->md.pvh_attrs & PVF_MOD)
1698                 vm_page_dirty(pg);
1699         if (TAILQ_FIRST(&pg->md.pv_list) == NULL)
1700                 pg->md.pvh_attrs &= ~PVF_REF;
1701         else
1702                 vm_page_aflag_set(pg, PGA_REFERENCED);
1703         if ((pve->pv_flags & PVF_NC) && ((pm == pmap_kernel()) ||
1704              (pve->pv_flags & PVF_WRITE) || !(pve->pv_flags & PVF_MWC)))
1705                 pmap_fix_cache(pg, pm, 0);
1706         else if (pve->pv_flags & PVF_WRITE) {
1707                 TAILQ_FOREACH(pve, &pg->md.pv_list, pv_list)
1708                     if (pve->pv_flags & PVF_WRITE)
1709                             break;
1710                 if (!pve) {
1711                         pg->md.pvh_attrs &= ~PVF_MOD;
1712                         vm_page_aflag_clear(pg, PGA_WRITEABLE);
1713                 }
1714         }
1715         pv = TAILQ_FIRST(&pg->md.pv_list);
1716         if (pv != NULL && (pv->pv_flags & PVF_UNMAN) &&
1717             TAILQ_NEXT(pv, pv_list) == NULL) {
1718                 pm = kernel_pmap;
1719                 pg->md.pv_kva = pv->pv_va;
1720                         /* a recursive pmap_nuke_pv */
1721                 TAILQ_REMOVE(&pg->md.pv_list, pv, pv_list);
1722                 TAILQ_REMOVE(&pm->pm_pvlist, pv, pv_plist);
1723                 if (pv->pv_flags & PVF_WIRED)
1724                         --pm->pm_stats.wired_count;
1725                 pg->md.pvh_attrs &= ~PVF_REF;
1726                 pg->md.pvh_attrs &= ~PVF_MOD;
1727                 vm_page_aflag_clear(pg, PGA_WRITEABLE);
1728                 pmap_free_pv_entry(pv);
1729         }
1730 }
1731
1732 static struct pv_entry *
1733 pmap_remove_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va)
1734 {
1735         struct pv_entry *pve;
1736
1737         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1738         pve = TAILQ_FIRST(&pg->md.pv_list);
1739
1740         while (pve) {
1741                 if (pve->pv_pmap == pm && pve->pv_va == va) {   /* match? */
1742                         pmap_nuke_pv(pg, pm, pve);
1743                         break;
1744                 }
1745                 pve = TAILQ_NEXT(pve, pv_list);
1746         }
1747
1748         if (pve == NULL && pg->md.pv_kva == va)
1749                 pg->md.pv_kva = 0;
1750
1751         return(pve);                            /* return removed pve */
1752 }
1753 /*
1754  *
1755  * pmap_modify_pv: Update pv flags
1756  *
1757  * => caller should hold lock on vm_page [so that attrs can be adjusted]
1758  * => caller should NOT adjust pmap's wire_count
1759  * => we return the old flags
1760  * 
1761  * Modify a physical-virtual mapping in the pv table
1762  */
1763 static u_int
1764 pmap_modify_pv(struct vm_page *pg, pmap_t pm, vm_offset_t va,
1765     u_int clr_mask, u_int set_mask)
1766 {
1767         struct pv_entry *npv;
1768         u_int flags, oflags;
1769
1770         PMAP_ASSERT_LOCKED(pm);
1771         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1772         if ((npv = pmap_find_pv(pg, pm, va)) == NULL)
1773                 return (0);
1774
1775         /*
1776          * There is at least one VA mapping this page.
1777          */
1778
1779         if (clr_mask & (PVF_REF | PVF_MOD))
1780                 pg->md.pvh_attrs |= set_mask & (PVF_REF | PVF_MOD);
1781
1782         oflags = npv->pv_flags;
1783         npv->pv_flags = flags = (oflags & ~clr_mask) | set_mask;
1784
1785         if ((flags ^ oflags) & PVF_WIRED) {
1786                 if (flags & PVF_WIRED)
1787                         ++pm->pm_stats.wired_count;
1788                 else
1789                         --pm->pm_stats.wired_count;
1790         }
1791
1792         if ((flags ^ oflags) & PVF_WRITE)
1793                 pmap_fix_cache(pg, pm, 0);
1794
1795         return (oflags);
1796 }
1797
1798 /* Function to set the debug level of the pmap code */
1799 #ifdef PMAP_DEBUG
1800 void
1801 pmap_debug(int level)
1802 {
1803         pmap_debug_level = level;
1804         dprintf("pmap_debug: level=%d\n", pmap_debug_level);
1805 }
1806 #endif  /* PMAP_DEBUG */
1807
1808 void
1809 pmap_pinit0(struct pmap *pmap)
1810 {
1811         PDEBUG(1, printf("pmap_pinit0: pmap = %08x\n", (u_int32_t) pmap));
1812
1813         dprintf("pmap_pinit0: pmap = %08x, pm_pdir = %08x\n",
1814                 (u_int32_t) pmap, (u_int32_t) pmap->pm_pdir);
1815         bcopy(kernel_pmap, pmap, sizeof(*pmap));
1816         bzero(&pmap->pm_mtx, sizeof(pmap->pm_mtx));
1817         PMAP_LOCK_INIT(pmap);
1818 }
1819
1820 /*
1821  *      Initialize a vm_page's machine-dependent fields.
1822  */
1823 void
1824 pmap_page_init(vm_page_t m)
1825 {
1826
1827         TAILQ_INIT(&m->md.pv_list);
1828 }
1829
1830 /*
1831  *      Initialize the pmap module.
1832  *      Called by vm_init, to initialize any structures that the pmap
1833  *      system needs to map virtual memory.
1834  */
1835 void
1836 pmap_init(void)
1837 {
1838         int shpgperproc = PMAP_SHPGPERPROC;
1839
1840         PDEBUG(1, printf("pmap_init: phys_start = %08x\n", PHYSADDR));
1841
1842         /*
1843          * init the pv free list
1844          */
1845         pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL, 
1846             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1847         /*
1848          * Now it is safe to enable pv_table recording.
1849          */
1850         PDEBUG(1, printf("pmap_init: done!\n"));
1851
1852         TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1853         
1854         pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
1855         pv_entry_high_water = 9 * (pv_entry_max / 10);
1856         l2zone = uma_zcreate("L2 Table", L2_TABLE_SIZE_REAL, pmap_l2ptp_ctor,
1857             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1858         l2table_zone = uma_zcreate("L2 Table", sizeof(struct l2_dtable),
1859             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
1860             UMA_ZONE_VM | UMA_ZONE_NOFREE);
1861
1862         uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
1863
1864 }
1865
1866 int
1867 pmap_fault_fixup(pmap_t pm, vm_offset_t va, vm_prot_t ftype, int user)
1868 {
1869         struct l2_dtable *l2;
1870         struct l2_bucket *l2b;
1871         pd_entry_t *pl1pd, l1pd;
1872         pt_entry_t *ptep, pte;
1873         vm_paddr_t pa;
1874         u_int l1idx;
1875         int rv = 0;
1876
1877         l1idx = L1_IDX(va);
1878         vm_page_lock_queues();
1879         PMAP_LOCK(pm);
1880
1881         /*
1882          * If there is no l2_dtable for this address, then the process
1883          * has no business accessing it.
1884          *
1885          * Note: This will catch userland processes trying to access
1886          * kernel addresses.
1887          */
1888         l2 = pm->pm_l2[L2_IDX(l1idx)];
1889         if (l2 == NULL)
1890                 goto out;
1891
1892         /*
1893          * Likewise if there is no L2 descriptor table
1894          */
1895         l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
1896         if (l2b->l2b_kva == NULL)
1897                 goto out;
1898
1899         /*
1900          * Check the PTE itself.
1901          */
1902         ptep = &l2b->l2b_kva[l2pte_index(va)];
1903         pte = *ptep;
1904         if (pte == 0)
1905                 goto out;
1906
1907         /*
1908          * Catch a userland access to the vector page mapped at 0x0
1909          */
1910         if (user && (pte & L2_S_PROT_U) == 0)
1911                 goto out;
1912         if (va == vector_page)
1913                 goto out;
1914
1915         pa = l2pte_pa(pte);
1916
1917         if ((ftype & VM_PROT_WRITE) && (pte & L2_S_PROT_W) == 0) {
1918                 /*
1919                  * This looks like a good candidate for "page modified"
1920                  * emulation...
1921                  */
1922                 struct pv_entry *pv;
1923                 struct vm_page *pg;
1924
1925                 /* Extract the physical address of the page */
1926                 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL) {
1927                         goto out;
1928                 }
1929                 /* Get the current flags for this page. */
1930
1931                 pv = pmap_find_pv(pg, pm, va);
1932                 if (pv == NULL) {
1933                         goto out;
1934                 }
1935
1936                 /*
1937                  * Do the flags say this page is writable? If not then it
1938                  * is a genuine write fault. If yes then the write fault is
1939                  * our fault as we did not reflect the write access in the
1940                  * PTE. Now we know a write has occurred we can correct this
1941                  * and also set the modified bit
1942                  */
1943                 if ((pv->pv_flags & PVF_WRITE) == 0) {
1944                         goto out;
1945                 }
1946
1947                 pg->md.pvh_attrs |= PVF_REF | PVF_MOD;
1948                 vm_page_dirty(pg);
1949                 pv->pv_flags |= PVF_REF | PVF_MOD;
1950
1951                 /* 
1952                  * Re-enable write permissions for the page.  No need to call
1953                  * pmap_fix_cache(), since this is just a
1954                  * modified-emulation fault, and the PVF_WRITE bit isn't
1955                  * changing. We've already set the cacheable bits based on
1956                  * the assumption that we can write to this page.
1957                  */
1958                 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO | L2_S_PROT_W;
1959                 PTE_SYNC(ptep);
1960                 rv = 1;
1961         } else
1962         if ((pte & L2_TYPE_MASK) == L2_TYPE_INV) {
1963                 /*
1964                  * This looks like a good candidate for "page referenced"
1965                  * emulation.
1966                  */
1967                 struct pv_entry *pv;
1968                 struct vm_page *pg;
1969
1970                 /* Extract the physical address of the page */
1971                 if ((pg = PHYS_TO_VM_PAGE(pa)) == NULL)
1972                         goto out;
1973                 /* Get the current flags for this page. */
1974
1975                 pv = pmap_find_pv(pg, pm, va);
1976                 if (pv == NULL)
1977                         goto out;
1978
1979                 pg->md.pvh_attrs |= PVF_REF;
1980                 pv->pv_flags |= PVF_REF;
1981
1982
1983                 *ptep = (pte & ~L2_TYPE_MASK) | L2_S_PROTO;
1984                 PTE_SYNC(ptep);
1985                 rv = 1;
1986         }
1987
1988         /*
1989          * We know there is a valid mapping here, so simply
1990          * fix up the L1 if necessary.
1991          */
1992         pl1pd = &pm->pm_l1->l1_kva[l1idx];
1993         l1pd = l2b->l2b_phys | L1_C_DOM(pm->pm_domain) | L1_C_PROTO;
1994         if (*pl1pd != l1pd) {
1995                 *pl1pd = l1pd;
1996                 PTE_SYNC(pl1pd);
1997                 rv = 1;
1998         }
1999
2000 #ifdef CPU_SA110
2001         /*
2002          * There are bugs in the rev K SA110.  This is a check for one
2003          * of them.
2004          */
2005         if (rv == 0 && curcpu()->ci_arm_cputype == CPU_ID_SA110 &&
2006             curcpu()->ci_arm_cpurev < 3) {
2007                 /* Always current pmap */
2008                 if (l2pte_valid(pte)) {
2009                         extern int kernel_debug;
2010                         if (kernel_debug & 1) {
2011                                 struct proc *p = curlwp->l_proc;
2012                                 printf("prefetch_abort: page is already "
2013                                     "mapped - pte=%p *pte=%08x\n", ptep, pte);
2014                                 printf("prefetch_abort: pc=%08lx proc=%p "
2015                                     "process=%s\n", va, p, p->p_comm);
2016                                 printf("prefetch_abort: far=%08x fs=%x\n",
2017                                     cpu_faultaddress(), cpu_faultstatus());
2018                         }
2019 #ifdef DDB
2020                         if (kernel_debug & 2)
2021                                 Debugger();
2022 #endif
2023                         rv = 1;
2024                 }
2025         }
2026 #endif /* CPU_SA110 */
2027
2028 #ifdef DEBUG
2029         /*
2030          * If 'rv == 0' at this point, it generally indicates that there is a
2031          * stale TLB entry for the faulting address. This happens when two or
2032          * more processes are sharing an L1. Since we don't flush the TLB on
2033          * a context switch between such processes, we can take domain faults
2034          * for mappings which exist at the same VA in both processes. EVEN IF
2035          * WE'VE RECENTLY FIXED UP THE CORRESPONDING L1 in pmap_enter(), for
2036          * example.
2037          *
2038          * This is extremely likely to happen if pmap_enter() updated the L1
2039          * entry for a recently entered mapping. In this case, the TLB is
2040          * flushed for the new mapping, but there may still be TLB entries for
2041          * other mappings belonging to other processes in the 1MB range
2042          * covered by the L1 entry.
2043          *
2044          * Since 'rv == 0', we know that the L1 already contains the correct
2045          * value, so the fault must be due to a stale TLB entry.
2046          *
2047          * Since we always need to flush the TLB anyway in the case where we
2048          * fixed up the L1, or frobbed the L2 PTE, we effectively deal with
2049          * stale TLB entries dynamically.
2050          *
2051          * However, the above condition can ONLY happen if the current L1 is
2052          * being shared. If it happens when the L1 is unshared, it indicates
2053          * that other parts of the pmap are not doing their job WRT managing
2054          * the TLB.
2055          */
2056         if (rv == 0 && pm->pm_l1->l1_domain_use_count == 1) {
2057                 extern int last_fault_code;
2058                 printf("fixup: pm %p, va 0x%lx, ftype %d - nothing to do!\n",
2059                     pm, va, ftype);
2060                 printf("fixup: l2 %p, l2b %p, ptep %p, pl1pd %p\n",
2061                     l2, l2b, ptep, pl1pd);
2062                 printf("fixup: pte 0x%x, l1pd 0x%x, last code 0x%x\n",
2063                     pte, l1pd, last_fault_code);
2064 #ifdef DDB
2065                 Debugger();
2066 #endif
2067         }
2068 #endif
2069
2070         cpu_tlb_flushID_SE(va);
2071         cpu_cpwait();
2072
2073         rv = 1;
2074
2075 out:
2076         vm_page_unlock_queues();
2077         PMAP_UNLOCK(pm);
2078         return (rv);
2079 }
2080
2081 void
2082 pmap_postinit(void)
2083 {
2084         struct l2_bucket *l2b;
2085         struct l1_ttable *l1;
2086         pd_entry_t *pl1pt;
2087         pt_entry_t *ptep, pte;
2088         vm_offset_t va, eva;
2089         u_int loop, needed;
2090         
2091         needed = (maxproc / PMAP_DOMAINS) + ((maxproc % PMAP_DOMAINS) ? 1 : 0);
2092         needed -= 1;
2093         l1 = malloc(sizeof(*l1) * needed, M_VMPMAP, M_WAITOK);
2094
2095         for (loop = 0; loop < needed; loop++, l1++) {
2096                 /* Allocate a L1 page table */
2097                 va = (vm_offset_t)contigmalloc(L1_TABLE_SIZE, M_VMPMAP, 0, 0x0,
2098                     0xffffffff, L1_TABLE_SIZE, 0);
2099
2100                 if (va == 0)
2101                         panic("Cannot allocate L1 KVM");
2102
2103                 eva = va + L1_TABLE_SIZE;
2104                 pl1pt = (pd_entry_t *)va;
2105                 
2106                 while (va < eva) {
2107                                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2108                                 ptep = &l2b->l2b_kva[l2pte_index(va)];
2109                                 pte = *ptep;
2110                                 pte = (pte & ~L2_S_CACHE_MASK) | pte_l2_s_cache_mode_pt;
2111                                 *ptep = pte;
2112                                 PTE_SYNC(ptep);
2113                                 cpu_tlb_flushD_SE(va);
2114                                 
2115                                 va += PAGE_SIZE;
2116                 }
2117                 pmap_init_l1(l1, pl1pt);
2118         }
2119
2120
2121 #ifdef DEBUG
2122         printf("pmap_postinit: Allocated %d static L1 descriptor tables\n",
2123             needed);
2124 #endif
2125 }
2126
2127 /*
2128  * This is used to stuff certain critical values into the PCB where they
2129  * can be accessed quickly from cpu_switch() et al.
2130  */
2131 void
2132 pmap_set_pcb_pagedir(pmap_t pm, struct pcb *pcb)
2133 {
2134         struct l2_bucket *l2b;
2135
2136         pcb->pcb_pagedir = pm->pm_l1->l1_physaddr;
2137         pcb->pcb_dacr = (DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
2138             (DOMAIN_CLIENT << (pm->pm_domain * 2));
2139
2140         if (vector_page < KERNBASE) {
2141                 pcb->pcb_pl1vec = &pm->pm_l1->l1_kva[L1_IDX(vector_page)];
2142                 l2b = pmap_get_l2_bucket(pm, vector_page);
2143                 pcb->pcb_l1vec = l2b->l2b_phys | L1_C_PROTO |
2144                     L1_C_DOM(pm->pm_domain) | L1_C_DOM(PMAP_DOMAIN_KERNEL);
2145         } else
2146                 pcb->pcb_pl1vec = NULL;
2147 }
2148
2149 void
2150 pmap_activate(struct thread *td)
2151 {
2152         pmap_t pm;
2153         struct pcb *pcb;
2154
2155         pm = vmspace_pmap(td->td_proc->p_vmspace);
2156         pcb = td->td_pcb;
2157
2158         critical_enter();
2159         pmap_set_pcb_pagedir(pm, pcb);
2160
2161         if (td == curthread) {
2162                 u_int cur_dacr, cur_ttb;
2163
2164                 __asm __volatile("mrc p15, 0, %0, c2, c0, 0" : "=r"(cur_ttb));
2165                 __asm __volatile("mrc p15, 0, %0, c3, c0, 0" : "=r"(cur_dacr));
2166
2167                 cur_ttb &= ~(L1_TABLE_SIZE - 1);
2168
2169                 if (cur_ttb == (u_int)pcb->pcb_pagedir &&
2170                     cur_dacr == pcb->pcb_dacr) {
2171                         /*
2172                          * No need to switch address spaces.
2173                          */
2174                         critical_exit();
2175                         return;
2176                 }
2177
2178
2179                 /*
2180                  * We MUST, I repeat, MUST fix up the L1 entry corresponding
2181                  * to 'vector_page' in the incoming L1 table before switching
2182                  * to it otherwise subsequent interrupts/exceptions (including
2183                  * domain faults!) will jump into hyperspace.
2184                  */
2185                 if (pcb->pcb_pl1vec) {
2186
2187                         *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2188                         /*
2189                          * Don't need to PTE_SYNC() at this point since
2190                          * cpu_setttb() is about to flush both the cache
2191                          * and the TLB.
2192                          */
2193                 }
2194
2195                 cpu_domains(pcb->pcb_dacr);
2196                 cpu_setttb(pcb->pcb_pagedir);
2197         }
2198         critical_exit();
2199 }
2200
2201 static int
2202 pmap_set_pt_cache_mode(pd_entry_t *kl1, vm_offset_t va)
2203 {
2204         pd_entry_t *pdep, pde;
2205         pt_entry_t *ptep, pte;
2206         vm_offset_t pa;
2207         int rv = 0;
2208
2209         /*
2210          * Make sure the descriptor itself has the correct cache mode
2211          */
2212         pdep = &kl1[L1_IDX(va)];
2213         pde = *pdep;
2214
2215         if (l1pte_section_p(pde)) {
2216                 if ((pde & L1_S_CACHE_MASK) != pte_l1_s_cache_mode_pt) {
2217                         *pdep = (pde & ~L1_S_CACHE_MASK) |
2218                             pte_l1_s_cache_mode_pt;
2219                         PTE_SYNC(pdep);
2220                         cpu_dcache_wbinv_range((vm_offset_t)pdep,
2221                             sizeof(*pdep));
2222                         cpu_l2cache_wbinv_range((vm_offset_t)pdep,
2223                             sizeof(*pdep));
2224                         rv = 1;
2225                 }
2226         } else {
2227                 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2228                 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2229                 if (ptep == NULL)
2230                         panic("pmap_bootstrap: No L2 for L2 @ va %p\n", ptep);
2231
2232                 ptep = &ptep[l2pte_index(va)];
2233                 pte = *ptep;
2234                 if ((pte & L2_S_CACHE_MASK) != pte_l2_s_cache_mode_pt) {
2235                         *ptep = (pte & ~L2_S_CACHE_MASK) |
2236                             pte_l2_s_cache_mode_pt;
2237                         PTE_SYNC(ptep);
2238                         cpu_dcache_wbinv_range((vm_offset_t)ptep,
2239                             sizeof(*ptep));
2240                         cpu_l2cache_wbinv_range((vm_offset_t)ptep,
2241                             sizeof(*ptep));
2242                         rv = 1;
2243                 }
2244         }
2245
2246         return (rv);
2247 }
2248
2249 static void
2250 pmap_alloc_specials(vm_offset_t *availp, int pages, vm_offset_t *vap, 
2251     pt_entry_t **ptep)
2252 {
2253         vm_offset_t va = *availp;
2254         struct l2_bucket *l2b;
2255
2256         if (ptep) {
2257                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2258                 if (l2b == NULL)
2259                         panic("pmap_alloc_specials: no l2b for 0x%x", va);
2260
2261                 *ptep = &l2b->l2b_kva[l2pte_index(va)];
2262         }
2263
2264         *vap = va;
2265         *availp = va + (PAGE_SIZE * pages);
2266 }
2267
2268 /*
2269  *      Bootstrap the system enough to run with virtual memory.
2270  *
2271  *      On the arm this is called after mapping has already been enabled
2272  *      and just syncs the pmap module with what has already been done.
2273  *      [We can't call it easily with mapping off since the kernel is not
2274  *      mapped with PA == VA, hence we would have to relocate every address
2275  *      from the linked base (virtual) address "KERNBASE" to the actual
2276  *      (physical) address starting relative to 0]
2277  */
2278 #define PMAP_STATIC_L2_SIZE 16
2279 #ifdef ARM_USE_SMALL_ALLOC
2280 extern struct mtx smallalloc_mtx;
2281 #endif
2282
2283 void
2284 pmap_bootstrap(vm_offset_t firstaddr, vm_offset_t lastaddr, struct pv_addr *l1pt)
2285 {
2286         static struct l1_ttable static_l1;
2287         static struct l2_dtable static_l2[PMAP_STATIC_L2_SIZE];
2288         struct l1_ttable *l1 = &static_l1;
2289         struct l2_dtable *l2;
2290         struct l2_bucket *l2b;
2291         pd_entry_t pde;
2292         pd_entry_t *kernel_l1pt = (pd_entry_t *)l1pt->pv_va;
2293         pt_entry_t *ptep;
2294         vm_paddr_t pa;
2295         vm_offset_t va;
2296         vm_size_t size;
2297         int l1idx, l2idx, l2next = 0;
2298
2299         PDEBUG(1, printf("firstaddr = %08x, lastaddr = %08x\n",
2300             firstaddr, lastaddr));
2301         
2302         virtual_avail = firstaddr;
2303         kernel_pmap->pm_l1 = l1;
2304         kernel_l1pa = l1pt->pv_pa;
2305         
2306         /*
2307          * Scan the L1 translation table created by initarm() and create
2308          * the required metadata for all valid mappings found in it.
2309          */
2310         for (l1idx = 0; l1idx < (L1_TABLE_SIZE / sizeof(pd_entry_t)); l1idx++) {
2311                 pde = kernel_l1pt[l1idx];
2312
2313                 /*
2314                  * We're only interested in Coarse mappings.
2315                  * pmap_extract() can deal with section mappings without
2316                  * recourse to checking L2 metadata.
2317                  */
2318                 if ((pde & L1_TYPE_MASK) != L1_TYPE_C)
2319                         continue;
2320
2321                 /*
2322                  * Lookup the KVA of this L2 descriptor table
2323                  */
2324                 pa = (vm_paddr_t)(pde & L1_C_ADDR_MASK);
2325                 ptep = (pt_entry_t *)kernel_pt_lookup(pa);
2326                 
2327                 if (ptep == NULL) {
2328                         panic("pmap_bootstrap: No L2 for va 0x%x, pa 0x%lx",
2329                             (u_int)l1idx << L1_S_SHIFT, (long unsigned int)pa);
2330                 }
2331
2332                 /*
2333                  * Fetch the associated L2 metadata structure.
2334                  * Allocate a new one if necessary.
2335                  */
2336                 if ((l2 = kernel_pmap->pm_l2[L2_IDX(l1idx)]) == NULL) {
2337                         if (l2next == PMAP_STATIC_L2_SIZE)
2338                                 panic("pmap_bootstrap: out of static L2s");
2339                         kernel_pmap->pm_l2[L2_IDX(l1idx)] = l2 = 
2340                             &static_l2[l2next++];
2341                 }
2342
2343                 /*
2344                  * One more L1 slot tracked...
2345                  */
2346                 l2->l2_occupancy++;
2347
2348                 /*
2349                  * Fill in the details of the L2 descriptor in the
2350                  * appropriate bucket.
2351                  */
2352                 l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2353                 l2b->l2b_kva = ptep;
2354                 l2b->l2b_phys = pa;
2355                 l2b->l2b_l1idx = l1idx;
2356
2357                 /*
2358                  * Establish an initial occupancy count for this descriptor
2359                  */
2360                 for (l2idx = 0;
2361                     l2idx < (L2_TABLE_SIZE_REAL / sizeof(pt_entry_t));
2362                     l2idx++) {
2363                         if ((ptep[l2idx] & L2_TYPE_MASK) != L2_TYPE_INV) {
2364                                 l2b->l2b_occupancy++;
2365                         }
2366                 }
2367
2368                 /*
2369                  * Make sure the descriptor itself has the correct cache mode.
2370                  * If not, fix it, but whine about the problem. Port-meisters
2371                  * should consider this a clue to fix up their initarm()
2372                  * function. :)
2373                  */
2374                 if (pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)ptep)) {
2375                         printf("pmap_bootstrap: WARNING! wrong cache mode for "
2376                             "L2 pte @ %p\n", ptep);
2377                 }
2378         }
2379
2380         
2381         /*
2382          * Ensure the primary (kernel) L1 has the correct cache mode for
2383          * a page table. Bitch if it is not correctly set.
2384          */
2385         for (va = (vm_offset_t)kernel_l1pt;
2386             va < ((vm_offset_t)kernel_l1pt + L1_TABLE_SIZE); va += PAGE_SIZE) {
2387                 if (pmap_set_pt_cache_mode(kernel_l1pt, va))
2388                         printf("pmap_bootstrap: WARNING! wrong cache mode for "
2389                             "primary L1 @ 0x%x\n", va);
2390         }
2391
2392         cpu_dcache_wbinv_all();
2393         cpu_l2cache_wbinv_all();
2394         cpu_tlb_flushID();
2395         cpu_cpwait();
2396
2397         PMAP_LOCK_INIT(kernel_pmap);
2398         CPU_FILL(&kernel_pmap->pm_active);
2399         kernel_pmap->pm_domain = PMAP_DOMAIN_KERNEL;
2400         TAILQ_INIT(&kernel_pmap->pm_pvlist);
2401         
2402         /*
2403          * Reserve some special page table entries/VA space for temporary
2404          * mapping of pages.
2405          */
2406 #define SYSMAP(c, p, v, n)                                              \
2407     v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
2408     
2409         pmap_alloc_specials(&virtual_avail, 1, &csrcp, &csrc_pte);
2410         pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)csrc_pte);
2411         pmap_alloc_specials(&virtual_avail, 1, &cdstp, &cdst_pte);
2412         pmap_set_pt_cache_mode(kernel_l1pt, (vm_offset_t)cdst_pte);
2413         size = ((lastaddr - pmap_curmaxkvaddr) + L1_S_OFFSET) / L1_S_SIZE;
2414         pmap_alloc_specials(&virtual_avail,
2415             round_page(size * L2_TABLE_SIZE_REAL) / PAGE_SIZE,
2416             &pmap_kernel_l2ptp_kva, NULL);
2417         
2418         size = (size + (L2_BUCKET_SIZE - 1)) / L2_BUCKET_SIZE;
2419         pmap_alloc_specials(&virtual_avail,
2420             round_page(size * sizeof(struct l2_dtable)) / PAGE_SIZE,
2421             &pmap_kernel_l2dtable_kva, NULL);
2422
2423         pmap_alloc_specials(&virtual_avail,
2424             1, (vm_offset_t*)&_tmppt, NULL);
2425         pmap_alloc_specials(&virtual_avail,
2426             MAXDUMPPGS, (vm_offset_t *)&crashdumpmap, NULL);
2427         SLIST_INIT(&l1_list);
2428         TAILQ_INIT(&l1_lru_list);
2429         mtx_init(&l1_lru_lock, "l1 list lock", NULL, MTX_DEF);
2430         pmap_init_l1(l1, kernel_l1pt);
2431         cpu_dcache_wbinv_all();
2432         cpu_l2cache_wbinv_all();
2433
2434         virtual_avail = round_page(virtual_avail);
2435         virtual_end = lastaddr;
2436         kernel_vm_end = pmap_curmaxkvaddr;
2437         arm_nocache_startaddr = lastaddr;
2438         mtx_init(&cmtx, "TMP mappings mtx", NULL, MTX_DEF);
2439
2440 #ifdef ARM_USE_SMALL_ALLOC
2441         mtx_init(&smallalloc_mtx, "Small alloc page list", NULL, MTX_DEF);
2442         arm_init_smallalloc();
2443 #endif
2444         pmap_set_pcb_pagedir(kernel_pmap, thread0.td_pcb);
2445 }
2446
2447 /***************************************************
2448  * Pmap allocation/deallocation routines.
2449  ***************************************************/
2450
2451 /*
2452  * Release any resources held by the given physical map.
2453  * Called when a pmap initialized by pmap_pinit is being released.
2454  * Should only be called if the map contains no valid mappings.
2455  */
2456 void
2457 pmap_release(pmap_t pmap)
2458 {
2459         struct pcb *pcb;
2460         
2461         pmap_idcache_wbinv_all(pmap);
2462         cpu_l2cache_wbinv_all();
2463         pmap_tlb_flushID(pmap);
2464         cpu_cpwait();
2465         if (vector_page < KERNBASE) {
2466                 struct pcb *curpcb = PCPU_GET(curpcb);
2467                 pcb = thread0.td_pcb;
2468                 if (pmap_is_current(pmap)) {
2469                         /*
2470                          * Frob the L1 entry corresponding to the vector
2471                          * page so that it contains the kernel pmap's domain
2472                          * number. This will ensure pmap_remove() does not
2473                          * pull the current vector page out from under us.
2474                          */
2475                         critical_enter();
2476                         *pcb->pcb_pl1vec = pcb->pcb_l1vec;
2477                         cpu_domains(pcb->pcb_dacr);
2478                         cpu_setttb(pcb->pcb_pagedir);
2479                         critical_exit();
2480                 }
2481                 pmap_remove(pmap, vector_page, vector_page + PAGE_SIZE);
2482                 /*
2483                  * Make sure cpu_switch(), et al, DTRT. This is safe to do
2484                  * since this process has no remaining mappings of its own.
2485                  */
2486                 curpcb->pcb_pl1vec = pcb->pcb_pl1vec;
2487                 curpcb->pcb_l1vec = pcb->pcb_l1vec;
2488                 curpcb->pcb_dacr = pcb->pcb_dacr;
2489                 curpcb->pcb_pagedir = pcb->pcb_pagedir;
2490
2491         }
2492         pmap_free_l1(pmap);
2493         PMAP_LOCK_DESTROY(pmap);
2494         
2495         dprintf("pmap_release()\n");
2496 }
2497
2498
2499
2500 /*
2501  * Helper function for pmap_grow_l2_bucket()
2502  */
2503 static __inline int
2504 pmap_grow_map(vm_offset_t va, pt_entry_t cache_mode, vm_paddr_t *pap)
2505 {
2506         struct l2_bucket *l2b;
2507         pt_entry_t *ptep;
2508         vm_paddr_t pa;
2509         struct vm_page *pg;
2510         
2511         pg = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_WIRED);
2512         if (pg == NULL)
2513                 return (1);
2514         pa = VM_PAGE_TO_PHYS(pg);
2515
2516         if (pap)
2517                 *pap = pa;
2518
2519         l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2520
2521         ptep = &l2b->l2b_kva[l2pte_index(va)];
2522         *ptep = L2_S_PROTO | pa | cache_mode |
2523             L2_S_PROT(PTE_KERNEL, VM_PROT_READ | VM_PROT_WRITE);
2524         PTE_SYNC(ptep);
2525         return (0);
2526 }
2527
2528 /*
2529  * This is the same as pmap_alloc_l2_bucket(), except that it is only
2530  * used by pmap_growkernel().
2531  */
2532 static __inline struct l2_bucket *
2533 pmap_grow_l2_bucket(pmap_t pm, vm_offset_t va)
2534 {
2535         struct l2_dtable *l2;
2536         struct l2_bucket *l2b;
2537         struct l1_ttable *l1;
2538         pd_entry_t *pl1pd;
2539         u_short l1idx;
2540         vm_offset_t nva;
2541
2542         l1idx = L1_IDX(va);
2543
2544         if ((l2 = pm->pm_l2[L2_IDX(l1idx)]) == NULL) {
2545                 /*
2546                  * No mapping at this address, as there is
2547                  * no entry in the L1 table.
2548                  * Need to allocate a new l2_dtable.
2549                  */
2550                 nva = pmap_kernel_l2dtable_kva;
2551                 if ((nva & PAGE_MASK) == 0) {
2552                         /*
2553                          * Need to allocate a backing page
2554                          */
2555                         if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2556                                 return (NULL);
2557                 }
2558
2559                 l2 = (struct l2_dtable *)nva;
2560                 nva += sizeof(struct l2_dtable);
2561
2562                 if ((nva & PAGE_MASK) < (pmap_kernel_l2dtable_kva & 
2563                     PAGE_MASK)) {
2564                         /*
2565                          * The new l2_dtable straddles a page boundary.
2566                          * Map in another page to cover it.
2567                          */
2568                         if (pmap_grow_map(nva, pte_l2_s_cache_mode, NULL))
2569                                 return (NULL);
2570                 }
2571
2572                 pmap_kernel_l2dtable_kva = nva;
2573
2574                 /*
2575                  * Link it into the parent pmap
2576                  */
2577                 pm->pm_l2[L2_IDX(l1idx)] = l2;
2578                 memset(l2, 0, sizeof(*l2));
2579         }
2580
2581         l2b = &l2->l2_bucket[L2_BUCKET(l1idx)];
2582
2583         /*
2584          * Fetch pointer to the L2 page table associated with the address.
2585          */
2586         if (l2b->l2b_kva == NULL) {
2587                 pt_entry_t *ptep;
2588
2589                 /*
2590                  * No L2 page table has been allocated. Chances are, this
2591                  * is because we just allocated the l2_dtable, above.
2592                  */
2593                 nva = pmap_kernel_l2ptp_kva;
2594                 ptep = (pt_entry_t *)nva;
2595                 if ((nva & PAGE_MASK) == 0) {
2596                         /*
2597                          * Need to allocate a backing page
2598                          */
2599                         if (pmap_grow_map(nva, pte_l2_s_cache_mode_pt,
2600                             &pmap_kernel_l2ptp_phys))
2601                                 return (NULL);
2602                         PTE_SYNC_RANGE(ptep, PAGE_SIZE / sizeof(pt_entry_t));
2603                 }
2604                 memset(ptep, 0, L2_TABLE_SIZE_REAL);
2605                 l2->l2_occupancy++;
2606                 l2b->l2b_kva = ptep;
2607                 l2b->l2b_l1idx = l1idx;
2608                 l2b->l2b_phys = pmap_kernel_l2ptp_phys;
2609
2610                 pmap_kernel_l2ptp_kva += L2_TABLE_SIZE_REAL;
2611                 pmap_kernel_l2ptp_phys += L2_TABLE_SIZE_REAL;
2612         }
2613
2614         /* Distribute new L1 entry to all other L1s */
2615         SLIST_FOREACH(l1, &l1_list, l1_link) {
2616                         pl1pd = &l1->l1_kva[L1_IDX(va)];
2617                         *pl1pd = l2b->l2b_phys | L1_C_DOM(PMAP_DOMAIN_KERNEL) |
2618                             L1_C_PROTO;
2619                         PTE_SYNC(pl1pd);
2620         }
2621
2622         return (l2b);
2623 }
2624
2625
2626 /*
2627  * grow the number of kernel page table entries, if needed
2628  */
2629 void
2630 pmap_growkernel(vm_offset_t addr)
2631 {
2632         pmap_t kpm = pmap_kernel();
2633
2634         if (addr <= pmap_curmaxkvaddr)
2635                 return;         /* we are OK */
2636
2637         /*
2638          * whoops!   we need to add kernel PTPs
2639          */
2640
2641         /* Map 1MB at a time */
2642         for (; pmap_curmaxkvaddr < addr; pmap_curmaxkvaddr += L1_S_SIZE)
2643                 pmap_grow_l2_bucket(kpm, pmap_curmaxkvaddr);
2644
2645         /*
2646          * flush out the cache, expensive but growkernel will happen so
2647          * rarely
2648          */
2649         cpu_dcache_wbinv_all();
2650         cpu_l2cache_wbinv_all();
2651         cpu_tlb_flushD();
2652         cpu_cpwait();
2653         kernel_vm_end = pmap_curmaxkvaddr;
2654 }
2655
2656
2657 /*
2658  * Remove all pages from specified address space
2659  * this aids process exit speeds.  Also, this code
2660  * is special cased for current process only, but
2661  * can have the more generic (and slightly slower)
2662  * mode enabled.  This is much faster than pmap_remove
2663  * in the case of running down an entire address space.
2664  */
2665 void
2666 pmap_remove_pages(pmap_t pmap)
2667 {
2668         struct pv_entry *pv, *npv;
2669         struct l2_bucket *l2b = NULL;
2670         vm_page_t m;
2671         pt_entry_t *pt;
2672         
2673         vm_page_lock_queues();
2674         PMAP_LOCK(pmap);
2675         cpu_idcache_wbinv_all();
2676         cpu_l2cache_wbinv_all();
2677         for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2678                 if (pv->pv_flags & PVF_WIRED || pv->pv_flags & PVF_UNMAN) {
2679                         /* Cannot remove wired or unmanaged pages now. */
2680                         npv = TAILQ_NEXT(pv, pv_plist);
2681                         continue;
2682                 }
2683                 pmap->pm_stats.resident_count--;
2684                 l2b = pmap_get_l2_bucket(pmap, pv->pv_va);
2685                 KASSERT(l2b != NULL, ("No L2 bucket in pmap_remove_pages"));
2686                 pt = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
2687                 m = PHYS_TO_VM_PAGE(*pt & L2_ADDR_MASK);
2688 #ifdef ARM_USE_SMALL_ALLOC
2689                 KASSERT((vm_offset_t)m >= alloc_firstaddr, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2690 #else
2691                 KASSERT((vm_offset_t)m >= KERNBASE, ("Trying to access non-existent page va %x pte %x", pv->pv_va, *pt));
2692 #endif
2693                 *pt = 0;
2694                 PTE_SYNC(pt);
2695                 npv = TAILQ_NEXT(pv, pv_plist);
2696                 pmap_nuke_pv(m, pmap, pv);
2697                 if (TAILQ_EMPTY(&m->md.pv_list))
2698                         vm_page_aflag_clear(m, PGA_WRITEABLE);
2699                 pmap_free_pv_entry(pv);
2700                 pmap_free_l2_bucket(pmap, l2b, 1);
2701         }
2702         vm_page_unlock_queues();
2703         cpu_tlb_flushID();
2704         cpu_cpwait();
2705         PMAP_UNLOCK(pmap);
2706 }
2707
2708
2709 /***************************************************
2710  * Low level mapping routines.....
2711  ***************************************************/
2712
2713 #ifdef ARM_HAVE_SUPERSECTIONS
2714 /* Map a super section into the KVA. */
2715
2716 void
2717 pmap_kenter_supersection(vm_offset_t va, uint64_t pa, int flags)
2718 {
2719         pd_entry_t pd = L1_S_PROTO | L1_S_SUPERSEC | (pa & L1_SUP_FRAME) |
2720             (((pa >> 32) & 0xf) << 20) | L1_S_PROT(PTE_KERNEL,
2721             VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2722         struct l1_ttable *l1;   
2723         vm_offset_t va0, va_end;
2724
2725         KASSERT(((va | pa) & L1_SUP_OFFSET) == 0,
2726             ("Not a valid super section mapping"));
2727         if (flags & SECTION_CACHE)
2728                 pd |= pte_l1_s_cache_mode;
2729         else if (flags & SECTION_PT)
2730                 pd |= pte_l1_s_cache_mode_pt;
2731         va0 = va & L1_SUP_FRAME;
2732         va_end = va + L1_SUP_SIZE;
2733         SLIST_FOREACH(l1, &l1_list, l1_link) {
2734                 va = va0;
2735                 for (; va < va_end; va += L1_S_SIZE) {
2736                         l1->l1_kva[L1_IDX(va)] = pd;
2737                         PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2738                 }
2739         }
2740 }
2741 #endif
2742
2743 /* Map a section into the KVA. */
2744
2745 void
2746 pmap_kenter_section(vm_offset_t va, vm_offset_t pa, int flags)
2747 {
2748         pd_entry_t pd = L1_S_PROTO | pa | L1_S_PROT(PTE_KERNEL,
2749             VM_PROT_READ|VM_PROT_WRITE) | L1_S_DOM(PMAP_DOMAIN_KERNEL);
2750         struct l1_ttable *l1;
2751
2752         KASSERT(((va | pa) & L1_S_OFFSET) == 0,
2753             ("Not a valid section mapping"));
2754         if (flags & SECTION_CACHE)
2755                 pd |= pte_l1_s_cache_mode;
2756         else if (flags & SECTION_PT)
2757                 pd |= pte_l1_s_cache_mode_pt;
2758         SLIST_FOREACH(l1, &l1_list, l1_link) {
2759                 l1->l1_kva[L1_IDX(va)] = pd;
2760                 PTE_SYNC(&l1->l1_kva[L1_IDX(va)]);
2761         }
2762 }
2763
2764 /*
2765  * Make a temporary mapping for a physical address.  This is only intended
2766  * to be used for panic dumps.
2767  */
2768 void *
2769 pmap_kenter_temp(vm_paddr_t pa, int i)
2770 {
2771         vm_offset_t va;
2772
2773         va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2774         pmap_kenter(va, pa);
2775         return ((void *)crashdumpmap);
2776 }
2777
2778 /*
2779  * add a wired page to the kva
2780  * note that in order for the mapping to take effect -- you
2781  * should do a invltlb after doing the pmap_kenter...
2782  */
2783 static PMAP_INLINE void
2784 pmap_kenter_internal(vm_offset_t va, vm_offset_t pa, int flags)
2785 {
2786         struct l2_bucket *l2b;
2787         pt_entry_t *pte;
2788         pt_entry_t opte;
2789         struct pv_entry *pve;
2790         vm_page_t m;
2791
2792         PDEBUG(1, printf("pmap_kenter: va = %08x, pa = %08x\n",
2793             (uint32_t) va, (uint32_t) pa));
2794
2795
2796         l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2797         if (l2b == NULL)
2798                 l2b = pmap_grow_l2_bucket(pmap_kernel(), va);
2799         KASSERT(l2b != NULL, ("No L2 Bucket"));
2800         pte = &l2b->l2b_kva[l2pte_index(va)];
2801         opte = *pte;
2802         PDEBUG(1, printf("pmap_kenter: pte = %08x, opte = %08x, npte = %08x\n",
2803             (uint32_t) pte, opte, *pte));
2804         if (l2pte_valid(opte)) {
2805                 pmap_kremove(va);
2806         } else {
2807                 if (opte == 0)
2808                         l2b->l2b_occupancy++;
2809         }
2810         *pte = L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, 
2811             VM_PROT_READ | VM_PROT_WRITE);
2812         if (flags & KENTER_CACHE)
2813                 *pte |= pte_l2_s_cache_mode;
2814         if (flags & KENTER_USER)
2815                 *pte |= L2_S_PROT_U;
2816         PTE_SYNC(pte);
2817
2818                 /* kernel direct mappings can be shared, so use a pv_entry
2819                  * to ensure proper caching.
2820                  *
2821                  * The pvzone is used to delay the recording of kernel
2822                  * mappings until the VM is running.
2823                  * 
2824                  * This expects the physical memory to have vm_page_array entry.
2825                  */
2826         if (pvzone != NULL && (m = vm_phys_paddr_to_vm_page(pa))) {
2827                 vm_page_lock_queues();
2828                 if (!TAILQ_EMPTY(&m->md.pv_list) || m->md.pv_kva) {
2829                         /* release vm_page lock for pv_entry UMA */
2830                         vm_page_unlock_queues();
2831                         if ((pve = pmap_get_pv_entry()) == NULL)
2832                                 panic("pmap_kenter_internal: no pv entries");   
2833                         vm_page_lock_queues();
2834                         PMAP_LOCK(pmap_kernel());
2835                         pmap_enter_pv(m, pve, pmap_kernel(), va,
2836                             PVF_WRITE | PVF_UNMAN);
2837                         pmap_fix_cache(m, pmap_kernel(), va);
2838                         PMAP_UNLOCK(pmap_kernel());
2839                 } else {
2840                         m->md.pv_kva = va;
2841                 }
2842                 vm_page_unlock_queues();
2843         }
2844 }
2845
2846 void
2847 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
2848 {
2849         pmap_kenter_internal(va, pa, KENTER_CACHE);
2850 }
2851
2852 void
2853 pmap_kenter_nocache(vm_offset_t va, vm_paddr_t pa)
2854 {
2855
2856         pmap_kenter_internal(va, pa, 0);
2857 }
2858
2859 void
2860 pmap_kenter_user(vm_offset_t va, vm_paddr_t pa)
2861 {
2862
2863         pmap_kenter_internal(va, pa, KENTER_CACHE|KENTER_USER);
2864         /*
2865          * Call pmap_fault_fixup now, to make sure we'll have no exception
2866          * at the first use of the new address, or bad things will happen,
2867          * as we use one of these addresses in the exception handlers.
2868          */
2869         pmap_fault_fixup(pmap_kernel(), va, VM_PROT_READ|VM_PROT_WRITE, 1);
2870 }
2871
2872 /*
2873  * remove a page from the kernel pagetables
2874  */
2875 void
2876 pmap_kremove(vm_offset_t va)
2877 {
2878         struct l2_bucket *l2b;
2879         pt_entry_t *pte, opte;
2880         struct pv_entry *pve;
2881         vm_page_t m;
2882         vm_offset_t pa;
2883                 
2884         l2b = pmap_get_l2_bucket(pmap_kernel(), va);
2885         if (!l2b)
2886                 return;
2887         KASSERT(l2b != NULL, ("No L2 Bucket"));
2888         pte = &l2b->l2b_kva[l2pte_index(va)];
2889         opte = *pte;
2890         if (l2pte_valid(opte)) {
2891                         /* pa = vtophs(va) taken from pmap_extract() */
2892                 switch (opte & L2_TYPE_MASK) {
2893                 case L2_TYPE_L:
2894                         pa = (opte & L2_L_FRAME) | (va & L2_L_OFFSET);
2895                         break;
2896                 default:
2897                         pa = (opte & L2_S_FRAME) | (va & L2_S_OFFSET);
2898                         break;
2899                 }
2900                         /* note: should never have to remove an allocation
2901                          * before the pvzone is initialized.
2902                          */
2903                 vm_page_lock_queues();
2904                 PMAP_LOCK(pmap_kernel());
2905                 if (pvzone != NULL && (m = vm_phys_paddr_to_vm_page(pa)) &&
2906                     (pve = pmap_remove_pv(m, pmap_kernel(), va)))
2907                         pmap_free_pv_entry(pve); 
2908                 PMAP_UNLOCK(pmap_kernel());
2909                 vm_page_unlock_queues();
2910                 va = va & ~PAGE_MASK;
2911                 cpu_dcache_wbinv_range(va, PAGE_SIZE);
2912                 cpu_l2cache_wbinv_range(va, PAGE_SIZE);
2913                 cpu_tlb_flushD_SE(va);
2914                 cpu_cpwait();
2915                 *pte = 0;
2916         }
2917 }
2918
2919
2920 /*
2921  *      Used to map a range of physical addresses into kernel
2922  *      virtual address space.
2923  *
2924  *      The value passed in '*virt' is a suggested virtual address for
2925  *      the mapping. Architectures which can support a direct-mapped
2926  *      physical to virtual region can return the appropriate address
2927  *      within that region, leaving '*virt' unchanged. Other
2928  *      architectures should map the pages starting at '*virt' and
2929  *      update '*virt' with the first usable address after the mapped
2930  *      region.
2931  */
2932 vm_offset_t
2933 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
2934 {
2935 #ifdef ARM_USE_SMALL_ALLOC
2936         return (arm_ptovirt(start));
2937 #else
2938         vm_offset_t sva = *virt;
2939         vm_offset_t va = sva;
2940
2941         PDEBUG(1, printf("pmap_map: virt = %08x, start = %08x, end = %08x, "
2942             "prot = %d\n", (uint32_t) *virt, (uint32_t) start, (uint32_t) end,
2943             prot));
2944             
2945         while (start < end) {
2946                 pmap_kenter(va, start);
2947                 va += PAGE_SIZE;
2948                 start += PAGE_SIZE;
2949         }
2950         *virt = va;
2951         return (sva);
2952 #endif
2953 }
2954
2955 static void
2956 pmap_wb_page(vm_page_t m)
2957 {
2958         struct pv_entry *pv;
2959
2960         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2961             pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, FALSE,
2962                 (pv->pv_flags & PVF_WRITE) == 0);
2963 }
2964
2965 static void
2966 pmap_inv_page(vm_page_t m)
2967 {
2968         struct pv_entry *pv;
2969
2970         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
2971             pmap_dcache_wb_range(pv->pv_pmap, pv->pv_va, PAGE_SIZE, TRUE, TRUE);
2972 }
2973 /*
2974  * Add a list of wired pages to the kva
2975  * this routine is only used for temporary
2976  * kernel mappings that do not need to have
2977  * page modification or references recorded.
2978  * Note that old mappings are simply written
2979  * over.  The page *must* be wired.
2980  */
2981 void
2982 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
2983 {
2984         int i;
2985
2986         for (i = 0; i < count; i++) {
2987                 pmap_wb_page(m[i]);
2988                 pmap_kenter_internal(va, VM_PAGE_TO_PHYS(m[i]), 
2989                     KENTER_CACHE);
2990                 va += PAGE_SIZE;
2991         }
2992 }
2993
2994
2995 /*
2996  * this routine jerks page mappings from the
2997  * kernel -- it is meant only for temporary mappings.
2998  */
2999 void
3000 pmap_qremove(vm_offset_t va, int count)
3001 {
3002         vm_paddr_t pa;
3003         int i;
3004
3005         for (i = 0; i < count; i++) {
3006                 pa = vtophys(va);
3007                 if (pa) {
3008                         pmap_inv_page(PHYS_TO_VM_PAGE(pa));
3009                         pmap_kremove(va);
3010                 }
3011                 va += PAGE_SIZE;
3012         }
3013 }
3014
3015
3016 /*
3017  * pmap_object_init_pt preloads the ptes for a given object
3018  * into the specified pmap.  This eliminates the blast of soft
3019  * faults on process startup and immediately after an mmap.
3020  */
3021 void
3022 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
3023     vm_pindex_t pindex, vm_size_t size)
3024 {
3025
3026         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
3027         KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
3028             ("pmap_object_init_pt: non-device object"));
3029 }
3030
3031
3032 /*
3033  *      pmap_is_prefaultable:
3034  *
3035  *      Return whether or not the specified virtual address is elgible
3036  *      for prefault.
3037  */
3038 boolean_t
3039 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
3040 {
3041         pd_entry_t *pde;
3042         pt_entry_t *pte;
3043
3044         if (!pmap_get_pde_pte(pmap, addr, &pde, &pte))
3045                 return (FALSE);
3046         KASSERT(pte != NULL, ("Valid mapping but no pte ?"));
3047         if (*pte == 0)
3048                 return (TRUE);
3049         return (FALSE);
3050 }
3051
3052 /*
3053  * Fetch pointers to the PDE/PTE for the given pmap/VA pair.
3054  * Returns TRUE if the mapping exists, else FALSE.
3055  *
3056  * NOTE: This function is only used by a couple of arm-specific modules.
3057  * It is not safe to take any pmap locks here, since we could be right
3058  * in the middle of debugging the pmap anyway...
3059  *
3060  * It is possible for this routine to return FALSE even though a valid
3061  * mapping does exist. This is because we don't lock, so the metadata
3062  * state may be inconsistent.
3063  *
3064  * NOTE: We can return a NULL *ptp in the case where the L1 pde is
3065  * a "section" mapping.
3066  */
3067 boolean_t
3068 pmap_get_pde_pte(pmap_t pm, vm_offset_t va, pd_entry_t **pdp, pt_entry_t **ptp)
3069 {
3070         struct l2_dtable *l2;
3071         pd_entry_t *pl1pd, l1pd;
3072         pt_entry_t *ptep;
3073         u_short l1idx;
3074
3075         if (pm->pm_l1 == NULL)
3076                 return (FALSE);
3077
3078         l1idx = L1_IDX(va);
3079         *pdp = pl1pd = &pm->pm_l1->l1_kva[l1idx];
3080         l1pd = *pl1pd;
3081
3082         if (l1pte_section_p(l1pd)) {
3083                 *ptp = NULL;
3084                 return (TRUE);
3085         }
3086
3087         if (pm->pm_l2 == NULL)
3088                 return (FALSE);
3089
3090         l2 = pm->pm_l2[L2_IDX(l1idx)];
3091
3092         if (l2 == NULL ||
3093             (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3094                 return (FALSE);
3095         }
3096
3097         *ptp = &ptep[l2pte_index(va)];
3098         return (TRUE);
3099 }
3100
3101 /*
3102  *      Routine:        pmap_remove_all
3103  *      Function:
3104  *              Removes this physical page from
3105  *              all physical maps in which it resides.
3106  *              Reflects back modify bits to the pager.
3107  *
3108  *      Notes:
3109  *              Original versions of this routine were very
3110  *              inefficient because they iteratively called
3111  *              pmap_remove (slow...)
3112  */
3113 void
3114 pmap_remove_all(vm_page_t m)
3115 {
3116         pv_entry_t pv;
3117         pt_entry_t *ptep;
3118         struct l2_bucket *l2b;
3119         boolean_t flush = FALSE;
3120         pmap_t curpm;
3121         int flags = 0;
3122
3123         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3124             ("pmap_remove_all: page %p is not managed", m));
3125         if (TAILQ_EMPTY(&m->md.pv_list))
3126                 return;
3127         vm_page_lock_queues();
3128         pmap_remove_write(m);
3129         curpm = vmspace_pmap(curproc->p_vmspace);
3130         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3131                 if (flush == FALSE && (pv->pv_pmap == curpm ||
3132                     pv->pv_pmap == pmap_kernel()))
3133                         flush = TRUE;
3134
3135                 PMAP_LOCK(pv->pv_pmap);
3136                 /*
3137                  * Cached contents were written-back in pmap_remove_write(),
3138                  * but we still have to invalidate the cache entry to make
3139                  * sure stale data are not retrieved when another page will be
3140                  * mapped under this virtual address.
3141                  */
3142                 if (pmap_is_current(pv->pv_pmap)) {
3143                         cpu_dcache_inv_range(pv->pv_va, PAGE_SIZE);
3144                         if (pmap_has_valid_mapping(pv->pv_pmap, pv->pv_va))
3145                                 cpu_l2cache_inv_range(pv->pv_va, PAGE_SIZE);
3146                 }
3147
3148                 if (pv->pv_flags & PVF_UNMAN) {
3149                         /* remove the pv entry, but do not remove the mapping
3150                          * and remember this is a kernel mapped page
3151                          */
3152                         m->md.pv_kva = pv->pv_va;
3153                 } else {
3154                         /* remove the mapping and pv entry */
3155                         l2b = pmap_get_l2_bucket(pv->pv_pmap, pv->pv_va);
3156                         KASSERT(l2b != NULL, ("No l2 bucket"));
3157                         ptep = &l2b->l2b_kva[l2pte_index(pv->pv_va)];
3158                         *ptep = 0;
3159                         PTE_SYNC_CURRENT(pv->pv_pmap, ptep);
3160                         pmap_free_l2_bucket(pv->pv_pmap, l2b, 1);
3161                         pv->pv_pmap->pm_stats.resident_count--;
3162                         flags |= pv->pv_flags;
3163                 }
3164                 pmap_nuke_pv(m, pv->pv_pmap, pv);
3165                 PMAP_UNLOCK(pv->pv_pmap);
3166                 pmap_free_pv_entry(pv);
3167         }
3168
3169         if (flush) {
3170                 if (PV_BEEN_EXECD(flags))
3171                         pmap_tlb_flushID(curpm);
3172                 else
3173                         pmap_tlb_flushD(curpm);
3174         }
3175         vm_page_aflag_clear(m, PGA_WRITEABLE);
3176         vm_page_unlock_queues();
3177 }
3178
3179
3180 /*
3181  *      Set the physical protection on the
3182  *      specified range of this map as requested.
3183  */
3184 void
3185 pmap_protect(pmap_t pm, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
3186 {
3187         struct l2_bucket *l2b;
3188         pt_entry_t *ptep, pte;
3189         vm_offset_t next_bucket;
3190         u_int flags;
3191         int flush;
3192
3193         CTR4(KTR_PMAP, "pmap_protect: pmap %p sva 0x%08x eva 0x%08x prot %x",
3194             pm, sva, eva, prot);
3195
3196         if ((prot & VM_PROT_READ) == 0) {
3197                 pmap_remove(pm, sva, eva);
3198                 return;
3199         }
3200
3201         if (prot & VM_PROT_WRITE) {
3202                 /*
3203                  * If this is a read->write transition, just ignore it and let
3204                  * vm_fault() take care of it later.
3205                  */
3206                 return;
3207         }
3208
3209         vm_page_lock_queues();
3210         PMAP_LOCK(pm);
3211
3212         /*
3213          * OK, at this point, we know we're doing write-protect operation.
3214          * If the pmap is active, write-back the range.
3215          */
3216         pmap_dcache_wb_range(pm, sva, eva - sva, FALSE, FALSE);
3217
3218         flush = ((eva - sva) >= (PAGE_SIZE * 4)) ? 0 : -1;
3219         flags = 0;
3220
3221         while (sva < eva) {
3222                 next_bucket = L2_NEXT_BUCKET(sva);
3223                 if (next_bucket > eva)
3224                         next_bucket = eva;
3225
3226                 l2b = pmap_get_l2_bucket(pm, sva);
3227                 if (l2b == NULL) {
3228                         sva = next_bucket;
3229                         continue;
3230                 }
3231
3232                 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3233
3234                 while (sva < next_bucket) {
3235                         if ((pte = *ptep) != 0 && (pte & L2_S_PROT_W) != 0) {
3236                                 struct vm_page *pg;
3237                                 u_int f;
3238
3239                                 pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3240                                 pte &= ~L2_S_PROT_W;
3241                                 *ptep = pte;
3242                                 PTE_SYNC(ptep);
3243
3244                                 if (pg != NULL) {
3245                                         if (!(pg->oflags & VPO_UNMANAGED)) {
3246                                                 f = pmap_modify_pv(pg, pm, sva,
3247                                                     PVF_WRITE, 0);
3248                                                 vm_page_dirty(pg);
3249                                         } else
3250                                                 f = 0;
3251                                 } else
3252                                         f = PVF_REF | PVF_EXEC;
3253
3254                                 if (flush >= 0) {
3255                                         flush++;
3256                                         flags |= f;
3257                                 } else
3258                                 if (PV_BEEN_EXECD(f))
3259                                         pmap_tlb_flushID_SE(pm, sva);
3260                                 else
3261                                 if (PV_BEEN_REFD(f))
3262                                         pmap_tlb_flushD_SE(pm, sva);
3263                         }
3264
3265                         sva += PAGE_SIZE;
3266                         ptep++;
3267                 }
3268         }
3269
3270
3271         if (flush) {
3272                 if (PV_BEEN_EXECD(flags))
3273                         pmap_tlb_flushID(pm);
3274                 else
3275                 if (PV_BEEN_REFD(flags))
3276                         pmap_tlb_flushD(pm);
3277         }
3278         vm_page_unlock_queues();
3279
3280         PMAP_UNLOCK(pm);
3281 }
3282
3283
3284 /*
3285  *      Insert the given physical page (p) at
3286  *      the specified virtual address (v) in the
3287  *      target physical map with the protection requested.
3288  *
3289  *      If specified, the page will be wired down, meaning
3290  *      that the related pte can not be reclaimed.
3291  *
3292  *      NB:  This is the only routine which MAY NOT lazy-evaluate
3293  *      or lose information.  That is, this routine must actually
3294  *      insert this page into the given map NOW.
3295  */
3296
3297 void
3298 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t m,
3299     vm_prot_t prot, boolean_t wired)
3300 {
3301
3302         vm_page_lock_queues();
3303         PMAP_LOCK(pmap);
3304         pmap_enter_locked(pmap, va, m, prot, wired, M_WAITOK);
3305         vm_page_unlock_queues();
3306         PMAP_UNLOCK(pmap);
3307 }
3308
3309 /*
3310  *      The page queues and pmap must be locked.
3311  */
3312 static void
3313 pmap_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3314     boolean_t wired, int flags)
3315 {
3316         struct l2_bucket *l2b = NULL;
3317         struct vm_page *opg;
3318         struct pv_entry *pve = NULL;
3319         pt_entry_t *ptep, npte, opte;
3320         u_int nflags;
3321         u_int oflags;
3322         vm_paddr_t pa;
3323
3324         PMAP_ASSERT_LOCKED(pmap);
3325         mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3326         if (va == vector_page) {
3327                 pa = systempage.pv_pa;
3328                 m = NULL;
3329         } else {
3330                 KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 ||
3331                     (flags & M_NOWAIT) != 0,
3332                     ("pmap_enter_locked: page %p is not busy", m));
3333                 pa = VM_PAGE_TO_PHYS(m);
3334         }
3335         nflags = 0;
3336         if (prot & VM_PROT_WRITE)
3337                 nflags |= PVF_WRITE;
3338         if (prot & VM_PROT_EXECUTE)
3339                 nflags |= PVF_EXEC;
3340         if (wired)
3341                 nflags |= PVF_WIRED;
3342         PDEBUG(1, printf("pmap_enter: pmap = %08x, va = %08x, m = %08x, prot = %x, "
3343             "wired = %x\n", (uint32_t) pmap, va, (uint32_t) m, prot, wired));
3344             
3345         if (pmap == pmap_kernel()) {
3346                 l2b = pmap_get_l2_bucket(pmap, va);
3347                 if (l2b == NULL)
3348                         l2b = pmap_grow_l2_bucket(pmap, va);
3349         } else {
3350 do_l2b_alloc:
3351                 l2b = pmap_alloc_l2_bucket(pmap, va);
3352                 if (l2b == NULL) {
3353                         if (flags & M_WAITOK) {
3354                                 PMAP_UNLOCK(pmap);
3355                                 vm_page_unlock_queues();
3356                                 VM_WAIT;
3357                                 vm_page_lock_queues();
3358                                 PMAP_LOCK(pmap);
3359                                 goto do_l2b_alloc;
3360                         }
3361                         return;
3362                 }
3363         }
3364
3365         ptep = &l2b->l2b_kva[l2pte_index(va)];
3366                     
3367         opte = *ptep;
3368         npte = pa;
3369         oflags = 0;
3370         if (opte) {
3371                 /*
3372                  * There is already a mapping at this address.
3373                  * If the physical address is different, lookup the
3374                  * vm_page.
3375                  */
3376                 if (l2pte_pa(opte) != pa)
3377                         opg = PHYS_TO_VM_PAGE(l2pte_pa(opte));
3378                 else
3379                         opg = m;
3380         } else
3381                 opg = NULL;
3382
3383         if ((prot & (VM_PROT_ALL)) ||
3384             (!m || m->md.pvh_attrs & PVF_REF)) {
3385                 /*
3386                  * - The access type indicates that we don't need
3387                  *   to do referenced emulation.
3388                  * OR
3389                  * - The physical page has already been referenced
3390                  *   so no need to re-do referenced emulation here.
3391                  */
3392                 npte |= L2_S_PROTO;
3393                 
3394                 nflags |= PVF_REF;
3395                 
3396                 if (m && ((prot & VM_PROT_WRITE) != 0 ||
3397                     (m->md.pvh_attrs & PVF_MOD))) {
3398                         /*
3399                          * This is a writable mapping, and the
3400                          * page's mod state indicates it has
3401                          * already been modified. Make it
3402                          * writable from the outset.
3403                          */
3404                         nflags |= PVF_MOD;
3405                         if (!(m->md.pvh_attrs & PVF_MOD))
3406                                 vm_page_dirty(m);
3407                 }
3408                 if (m && opte)
3409                         vm_page_aflag_set(m, PGA_REFERENCED);
3410         } else {
3411                 /*
3412                  * Need to do page referenced emulation.
3413                  */
3414                 npte |= L2_TYPE_INV;
3415         }
3416         
3417         if (prot & VM_PROT_WRITE) {
3418                 npte |= L2_S_PROT_W;
3419                 if (m != NULL &&
3420                     (m->oflags & VPO_UNMANAGED) == 0)
3421                         vm_page_aflag_set(m, PGA_WRITEABLE);
3422         }
3423         npte |= pte_l2_s_cache_mode;
3424         if (m && m == opg) {
3425                 /*
3426                  * We're changing the attrs of an existing mapping.
3427                  */
3428                 oflags = pmap_modify_pv(m, pmap, va,
3429                     PVF_WRITE | PVF_EXEC | PVF_WIRED |
3430                     PVF_MOD | PVF_REF, nflags);
3431                 
3432                 /*
3433                  * We may need to flush the cache if we're
3434                  * doing rw-ro...
3435                  */
3436                 if (pmap_is_current(pmap) &&
3437                     (oflags & PVF_NC) == 0 &&
3438                     (opte & L2_S_PROT_W) != 0 &&
3439                     (prot & VM_PROT_WRITE) == 0 &&
3440                     (opte & L2_TYPE_MASK) != L2_TYPE_INV) {
3441                         cpu_dcache_wb_range(va, PAGE_SIZE);
3442                         cpu_l2cache_wb_range(va, PAGE_SIZE);
3443                 }
3444         } else {
3445                 /*
3446                  * New mapping, or changing the backing page
3447                  * of an existing mapping.
3448                  */
3449                 if (opg) {
3450                         /*
3451                          * Replacing an existing mapping with a new one.
3452                          * It is part of our managed memory so we
3453                          * must remove it from the PV list
3454                          */
3455                         if ((pve = pmap_remove_pv(opg, pmap, va))) {
3456
3457                         /* note for patch: the oflags/invalidation was moved
3458                          * because PG_FICTITIOUS pages could free the pve
3459                          */
3460                             oflags = pve->pv_flags;
3461                         /*
3462                          * If the old mapping was valid (ref/mod
3463                          * emulation creates 'invalid' mappings
3464                          * initially) then make sure to frob
3465                          * the cache.
3466                          */
3467                             if ((oflags & PVF_NC) == 0 && l2pte_valid(opte)) {
3468                                 if (PV_BEEN_EXECD(oflags)) {
3469                                         pmap_idcache_wbinv_range(pmap, va,
3470                                             PAGE_SIZE);
3471                                 } else
3472                                         if (PV_BEEN_REFD(oflags)) {
3473                                                 pmap_dcache_wb_range(pmap, va,
3474                                                     PAGE_SIZE, TRUE,
3475                                                     (oflags & PVF_WRITE) == 0);
3476                                         }
3477                             }
3478
3479                         /* free/allocate a pv_entry for UNMANAGED pages if
3480                          * this physical page is not/is already mapped.
3481                          */
3482
3483                             if (m && (m->oflags & VPO_UNMANAGED) &&
3484                                   !m->md.pv_kva &&
3485                                  TAILQ_EMPTY(&m->md.pv_list)) {
3486                                 pmap_free_pv_entry(pve);
3487                                 pve = NULL;
3488                             }
3489                         } else if (m &&
3490                                  (!(m->oflags & VPO_UNMANAGED) || m->md.pv_kva ||
3491                                   !TAILQ_EMPTY(&m->md.pv_list)))
3492                                 pve = pmap_get_pv_entry();
3493                 } else if (m &&
3494                            (!(m->oflags & VPO_UNMANAGED) || m->md.pv_kva ||
3495                            !TAILQ_EMPTY(&m->md.pv_list)))
3496                         pve = pmap_get_pv_entry();
3497
3498                 if (m) {
3499                         if ((m->oflags & VPO_UNMANAGED)) {
3500                                 if (!TAILQ_EMPTY(&m->md.pv_list) ||
3501                                     m->md.pv_kva) {
3502                                         KASSERT(pve != NULL, ("No pv"));
3503                                         nflags |= PVF_UNMAN;
3504                                         pmap_enter_pv(m, pve, pmap, va, nflags);
3505                                 } else
3506                                         m->md.pv_kva = va;
3507                         } else {
3508                                 KASSERT(va < kmi.clean_sva ||
3509                                     va >= kmi.clean_eva,
3510                 ("pmap_enter: managed mapping within the clean submap"));
3511                                 KASSERT(pve != NULL, ("No pv"));
3512                                 pmap_enter_pv(m, pve, pmap, va, nflags);
3513                         }
3514                 }
3515         }
3516         /*
3517          * Make sure userland mappings get the right permissions
3518          */
3519         if (pmap != pmap_kernel() && va != vector_page) {
3520                 npte |= L2_S_PROT_U;
3521         }
3522
3523         /*
3524          * Keep the stats up to date
3525          */
3526         if (opte == 0) {
3527                 l2b->l2b_occupancy++;
3528                 pmap->pm_stats.resident_count++;
3529         } 
3530
3531
3532         /*
3533          * If this is just a wiring change, the two PTEs will be
3534          * identical, so there's no need to update the page table.
3535          */
3536         if (npte != opte) {
3537                 boolean_t is_cached = pmap_is_current(pmap);
3538
3539                 *ptep = npte;
3540                 if (is_cached) {
3541                         /*
3542                          * We only need to frob the cache/tlb if this pmap
3543                          * is current
3544                          */
3545                         PTE_SYNC(ptep);
3546                         if (L1_IDX(va) != L1_IDX(vector_page) && 
3547                             l2pte_valid(npte)) {
3548                                 /*
3549                                  * This mapping is likely to be accessed as
3550                                  * soon as we return to userland. Fix up the
3551                                  * L1 entry to avoid taking another
3552                                  * page/domain fault.
3553                                  */
3554                                 pd_entry_t *pl1pd, l1pd;
3555
3556                                 pl1pd = &pmap->pm_l1->l1_kva[L1_IDX(va)];
3557                                 l1pd = l2b->l2b_phys | L1_C_DOM(pmap->pm_domain) |
3558                                     L1_C_PROTO;
3559                                 if (*pl1pd != l1pd) {
3560                                         *pl1pd = l1pd;
3561                                         PTE_SYNC(pl1pd);
3562                                 }
3563                         }
3564                 }
3565
3566                 if (PV_BEEN_EXECD(oflags))
3567                         pmap_tlb_flushID_SE(pmap, va);
3568                 else if (PV_BEEN_REFD(oflags))
3569                         pmap_tlb_flushD_SE(pmap, va);
3570
3571
3572                 if (m)
3573                         pmap_fix_cache(m, pmap, va);
3574         }
3575 }
3576
3577 /*
3578  * Maps a sequence of resident pages belonging to the same object.
3579  * The sequence begins with the given page m_start.  This page is
3580  * mapped at the given virtual address start.  Each subsequent page is
3581  * mapped at a virtual address that is offset from start by the same
3582  * amount as the page is offset from m_start within the object.  The
3583  * last page in the sequence is the page with the largest offset from
3584  * m_start that can be mapped at a virtual address less than the given
3585  * virtual address end.  Not every virtual page between start and end
3586  * is mapped; only those for which a resident page exists with the
3587  * corresponding offset from m_start are mapped.
3588  */
3589 void
3590 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
3591     vm_page_t m_start, vm_prot_t prot)
3592 {
3593         vm_page_t m;
3594         vm_pindex_t diff, psize;
3595
3596         psize = atop(end - start);
3597         m = m_start;
3598         vm_page_lock_queues();
3599         PMAP_LOCK(pmap);
3600         while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
3601                 pmap_enter_locked(pmap, start + ptoa(diff), m, prot &
3602                     (VM_PROT_READ | VM_PROT_EXECUTE), FALSE, M_NOWAIT);
3603                 m = TAILQ_NEXT(m, listq);
3604         }
3605         vm_page_unlock_queues();
3606         PMAP_UNLOCK(pmap);
3607 }
3608
3609 /*
3610  * this code makes some *MAJOR* assumptions:
3611  * 1. Current pmap & pmap exists.
3612  * 2. Not wired.
3613  * 3. Read access.
3614  * 4. No page table pages.
3615  * but is *MUCH* faster than pmap_enter...
3616  */
3617
3618 void
3619 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
3620 {
3621
3622         vm_page_lock_queues();
3623         PMAP_LOCK(pmap);
3624         pmap_enter_locked(pmap, va, m, prot & (VM_PROT_READ | VM_PROT_EXECUTE),
3625             FALSE, M_NOWAIT);
3626         vm_page_unlock_queues();
3627         PMAP_UNLOCK(pmap);
3628 }
3629
3630 /*
3631  *      Routine:        pmap_change_wiring
3632  *      Function:       Change the wiring attribute for a map/virtual-address
3633  *                      pair.
3634  *      In/out conditions:
3635  *                      The mapping must already exist in the pmap.
3636  */
3637 void
3638 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3639 {
3640         struct l2_bucket *l2b;
3641         pt_entry_t *ptep, pte;
3642         vm_page_t pg;
3643
3644         vm_page_lock_queues();
3645         PMAP_LOCK(pmap);
3646         l2b = pmap_get_l2_bucket(pmap, va);
3647         KASSERT(l2b, ("No l2b bucket in pmap_change_wiring"));
3648         ptep = &l2b->l2b_kva[l2pte_index(va)];
3649         pte = *ptep;
3650         pg = PHYS_TO_VM_PAGE(l2pte_pa(pte));
3651         if (pg) 
3652                 pmap_modify_pv(pg, pmap, va, PVF_WIRED, wired ? PVF_WIRED : 0);
3653         vm_page_unlock_queues();
3654         PMAP_UNLOCK(pmap);
3655 }
3656
3657
3658 /*
3659  *      Copy the range specified by src_addr/len
3660  *      from the source map to the range dst_addr/len
3661  *      in the destination map.
3662  *
3663  *      This routine is only advisory and need not do anything.
3664  */
3665 void
3666 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3667     vm_size_t len, vm_offset_t src_addr)
3668 {
3669 }
3670
3671
3672 /*
3673  *      Routine:        pmap_extract
3674  *      Function:
3675  *              Extract the physical page address associated
3676  *              with the given map/virtual_address pair.
3677  */
3678 vm_paddr_t
3679 pmap_extract(pmap_t pm, vm_offset_t va)
3680 {
3681         struct l2_dtable *l2;
3682         pd_entry_t l1pd;
3683         pt_entry_t *ptep, pte;
3684         vm_paddr_t pa;
3685         u_int l1idx;
3686         l1idx = L1_IDX(va);
3687
3688         PMAP_LOCK(pm);
3689         l1pd = pm->pm_l1->l1_kva[l1idx];
3690         if (l1pte_section_p(l1pd)) {
3691                 /*
3692                  * These should only happen for pmap_kernel()
3693                  */
3694                 KASSERT(pm == pmap_kernel(), ("huh"));
3695                 /* XXX: what to do about the bits > 32 ? */
3696                 if (l1pd & L1_S_SUPERSEC) 
3697                         pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3698                 else
3699                         pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3700         } else {
3701                 /*
3702                  * Note that we can't rely on the validity of the L1
3703                  * descriptor as an indication that a mapping exists.
3704                  * We have to look it up in the L2 dtable.
3705                  */
3706                 l2 = pm->pm_l2[L2_IDX(l1idx)];
3707
3708                 if (l2 == NULL ||
3709                     (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3710                         PMAP_UNLOCK(pm);
3711                         return (0);
3712                 }
3713
3714                 ptep = &ptep[l2pte_index(va)];
3715                 pte = *ptep;
3716
3717                 if (pte == 0) {
3718                         PMAP_UNLOCK(pm);
3719                         return (0);
3720                 }
3721
3722                 switch (pte & L2_TYPE_MASK) {
3723                 case L2_TYPE_L:
3724                         pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3725                         break;
3726
3727                 default:
3728                         pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3729                         break;
3730                 }
3731         }
3732
3733         PMAP_UNLOCK(pm);
3734         return (pa);
3735 }
3736
3737 /*
3738  * Atomically extract and hold the physical page with the given
3739  * pmap and virtual address pair if that mapping permits the given
3740  * protection.
3741  *
3742  */
3743 vm_page_t
3744 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
3745 {
3746         struct l2_dtable *l2;
3747         pd_entry_t l1pd;
3748         pt_entry_t *ptep, pte;
3749         vm_paddr_t pa, paddr;
3750         vm_page_t m = NULL;
3751         u_int l1idx;
3752         l1idx = L1_IDX(va);
3753         paddr = 0;
3754
3755         PMAP_LOCK(pmap);
3756 retry:
3757         l1pd = pmap->pm_l1->l1_kva[l1idx];
3758         if (l1pte_section_p(l1pd)) {
3759                 /*
3760                  * These should only happen for pmap_kernel()
3761                  */
3762                 KASSERT(pmap == pmap_kernel(), ("huh"));
3763                 /* XXX: what to do about the bits > 32 ? */
3764                 if (l1pd & L1_S_SUPERSEC) 
3765                         pa = (l1pd & L1_SUP_FRAME) | (va & L1_SUP_OFFSET);
3766                 else
3767                         pa = (l1pd & L1_S_FRAME) | (va & L1_S_OFFSET);
3768                 if (vm_page_pa_tryrelock(pmap, pa & PG_FRAME, &paddr))
3769                         goto retry;
3770                 if (l1pd & L1_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3771                         m = PHYS_TO_VM_PAGE(pa);
3772                         vm_page_hold(m);
3773                 }
3774                         
3775         } else {
3776                 /*
3777                  * Note that we can't rely on the validity of the L1
3778                  * descriptor as an indication that a mapping exists.
3779                  * We have to look it up in the L2 dtable.
3780                  */
3781                 l2 = pmap->pm_l2[L2_IDX(l1idx)];
3782
3783                 if (l2 == NULL ||
3784                     (ptep = l2->l2_bucket[L2_BUCKET(l1idx)].l2b_kva) == NULL) {
3785                         PMAP_UNLOCK(pmap);
3786                         return (NULL);
3787                 }
3788
3789                 ptep = &ptep[l2pte_index(va)];
3790                 pte = *ptep;
3791
3792                 if (pte == 0) {
3793                         PMAP_UNLOCK(pmap);
3794                         return (NULL);
3795                 }
3796                 if (pte & L2_S_PROT_W || (prot & VM_PROT_WRITE) == 0) {
3797                         switch (pte & L2_TYPE_MASK) {
3798                         case L2_TYPE_L:
3799                                 pa = (pte & L2_L_FRAME) | (va & L2_L_OFFSET);
3800                                 break;
3801                                 
3802                         default:
3803                                 pa = (pte & L2_S_FRAME) | (va & L2_S_OFFSET);
3804                                 break;
3805                         }
3806                         if (vm_page_pa_tryrelock(pmap, pa & PG_FRAME, &paddr))
3807                                 goto retry;             
3808                         m = PHYS_TO_VM_PAGE(pa);
3809                         vm_page_hold(m);
3810                 }
3811         }
3812
3813         PMAP_UNLOCK(pmap);
3814         PA_UNLOCK_COND(paddr);
3815         return (m);
3816 }
3817
3818 /*
3819  * Initialize a preallocated and zeroed pmap structure,
3820  * such as one in a vmspace structure.
3821  */
3822
3823 int
3824 pmap_pinit(pmap_t pmap)
3825 {
3826         PDEBUG(1, printf("pmap_pinit: pmap = %08x\n", (uint32_t) pmap));
3827         
3828         PMAP_LOCK_INIT(pmap);
3829         pmap_alloc_l1(pmap);
3830         bzero(pmap->pm_l2, sizeof(pmap->pm_l2));
3831
3832         CPU_ZERO(&pmap->pm_active);
3833                 
3834         TAILQ_INIT(&pmap->pm_pvlist);
3835         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
3836         pmap->pm_stats.resident_count = 1;
3837         if (vector_page < KERNBASE) {
3838                 pmap_enter(pmap, vector_page,
3839                     VM_PROT_READ, PHYS_TO_VM_PAGE(systempage.pv_pa),
3840                     VM_PROT_READ, 1);
3841         } 
3842         return (1);
3843 }
3844
3845
3846 /***************************************************
3847  * page management routines.
3848  ***************************************************/
3849
3850
3851 static void
3852 pmap_free_pv_entry(pv_entry_t pv)
3853 {
3854         pv_entry_count--;
3855         uma_zfree(pvzone, pv);
3856 }
3857
3858
3859 /*
3860  * get a new pv_entry, allocating a block from the system
3861  * when needed.
3862  * the memory allocation is performed bypassing the malloc code
3863  * because of the possibility of allocations at interrupt time.
3864  */
3865 static pv_entry_t
3866 pmap_get_pv_entry(void)
3867 {
3868         pv_entry_t ret_value;
3869         
3870         pv_entry_count++;
3871         if (pv_entry_count > pv_entry_high_water)
3872                 pagedaemon_wakeup();
3873         ret_value = uma_zalloc(pvzone, M_NOWAIT);
3874         return ret_value;
3875 }
3876
3877 /*
3878  *      Remove the given range of addresses from the specified map.
3879  *
3880  *      It is assumed that the start and end are properly
3881  *      rounded to the page size.
3882  */
3883 #define PMAP_REMOVE_CLEAN_LIST_SIZE     3
3884 void
3885 pmap_remove(pmap_t pm, vm_offset_t sva, vm_offset_t eva)
3886 {
3887         struct l2_bucket *l2b;
3888         vm_offset_t next_bucket;
3889         pt_entry_t *ptep;
3890         u_int total;
3891         u_int mappings, is_exec, is_refd;
3892         int flushall = 0;
3893
3894
3895         /*
3896          * we lock in the pmap => pv_head direction
3897          */
3898
3899         vm_page_lock_queues();
3900         PMAP_LOCK(pm);
3901         total = 0;
3902         while (sva < eva) {
3903                 /*
3904                  * Do one L2 bucket's worth at a time.
3905                  */
3906                 next_bucket = L2_NEXT_BUCKET(sva);
3907                 if (next_bucket > eva)
3908                         next_bucket = eva;
3909
3910                 l2b = pmap_get_l2_bucket(pm, sva);
3911                 if (l2b == NULL) {
3912                         sva = next_bucket;
3913                         continue;
3914                 }
3915
3916                 ptep = &l2b->l2b_kva[l2pte_index(sva)];
3917                 mappings = 0;
3918
3919                 while (sva < next_bucket) {
3920                         struct vm_page *pg;
3921                         pt_entry_t pte;
3922                         vm_paddr_t pa;
3923
3924                         pte = *ptep;
3925
3926                         if (pte == 0) {
3927                                 /*
3928                                  * Nothing here, move along
3929                                  */
3930                                 sva += PAGE_SIZE;
3931                                 ptep++;
3932                                 continue;
3933                         }
3934
3935                         pm->pm_stats.resident_count--;
3936                         pa = l2pte_pa(pte);
3937                         is_exec = 0;
3938                         is_refd = 1;
3939
3940                         /*
3941                          * Update flags. In a number of circumstances,
3942                          * we could cluster a lot of these and do a
3943                          * number of sequential pages in one go.
3944                          */
3945                         if ((pg = PHYS_TO_VM_PAGE(pa)) != NULL) {
3946                                 struct pv_entry *pve;
3947
3948                                 pve = pmap_remove_pv(pg, pm, sva);
3949                                 if (pve) {
3950                                         is_exec = PV_BEEN_EXECD(pve->pv_flags);
3951                                         is_refd = PV_BEEN_REFD(pve->pv_flags);
3952                                         pmap_free_pv_entry(pve);
3953                                 }
3954                         }
3955
3956                         if (l2pte_valid(pte) && pmap_is_current(pm)) {
3957                                 if (total < PMAP_REMOVE_CLEAN_LIST_SIZE) {
3958                                         total++;
3959                                         if (is_exec) {
3960                                                 cpu_idcache_wbinv_range(sva,
3961                                                     PAGE_SIZE);
3962                                                 cpu_l2cache_wbinv_range(sva,
3963                                                     PAGE_SIZE);
3964                                                 cpu_tlb_flushID_SE(sva);
3965                                         } else if (is_refd) {
3966                                                 cpu_dcache_wbinv_range(sva,
3967                                                     PAGE_SIZE);
3968                                                 cpu_l2cache_wbinv_range(sva,
3969                                                     PAGE_SIZE);
3970                                                 cpu_tlb_flushD_SE(sva);
3971                                         }
3972                                 } else if (total == PMAP_REMOVE_CLEAN_LIST_SIZE) {
3973                                         /* flushall will also only get set for
3974                                          * for a current pmap
3975                                          */
3976                                         cpu_idcache_wbinv_all();
3977                                         cpu_l2cache_wbinv_all();
3978                                         flushall = 1;
3979                                         total++;
3980                                 }
3981                         }
3982                         *ptep = 0;
3983                         PTE_SYNC(ptep);
3984
3985                         sva += PAGE_SIZE;
3986                         ptep++;
3987                         mappings++;
3988                 }
3989
3990                 pmap_free_l2_bucket(pm, l2b, mappings);
3991         }
3992
3993         vm_page_unlock_queues();
3994         if (flushall)
3995                 cpu_tlb_flushID();
3996         PMAP_UNLOCK(pm);
3997 }
3998
3999 /*
4000  * pmap_zero_page()
4001  * 
4002  * Zero a given physical page by mapping it at a page hook point.
4003  * In doing the zero page op, the page we zero is mapped cachable, as with
4004  * StrongARM accesses to non-cached pages are non-burst making writing
4005  * _any_ bulk data very slow.
4006  */
4007 #if (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined(CPU_XSCALE_CORE3)
4008 void
4009 pmap_zero_page_generic(vm_paddr_t phys, int off, int size)
4010 {
4011 #ifdef ARM_USE_SMALL_ALLOC
4012         char *dstpg;
4013 #endif
4014
4015 #ifdef DEBUG
4016         struct vm_page *pg = PHYS_TO_VM_PAGE(phys);
4017
4018         if (pg->md.pvh_list != NULL)
4019                 panic("pmap_zero_page: page has mappings");
4020 #endif
4021
4022         if (_arm_bzero && size >= _min_bzero_size &&
4023             _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
4024                 return;
4025
4026 #ifdef ARM_USE_SMALL_ALLOC
4027         dstpg = (char *)arm_ptovirt(phys);
4028         if (off || size != PAGE_SIZE) {
4029                 bzero(dstpg + off, size);
4030                 cpu_dcache_wbinv_range((vm_offset_t)(dstpg + off), size);
4031                 cpu_l2cache_wbinv_range((vm_offset_t)(dstpg + off), size);
4032         } else {
4033                 bzero_page((vm_offset_t)dstpg);
4034                 cpu_dcache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
4035                 cpu_l2cache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
4036         }
4037 #else
4038
4039         mtx_lock(&cmtx);
4040         /*
4041          * Hook in the page, zero it, invalidate the TLB as needed.
4042          *
4043          * Note the temporary zero-page mapping must be a non-cached page in
4044          * order to work without corruption when write-allocate is enabled.
4045          */
4046         *cdst_pte = L2_S_PROTO | phys | L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE);
4047         cpu_tlb_flushD_SE(cdstp);
4048         cpu_cpwait();
4049         if (off || size != PAGE_SIZE)
4050                 bzero((void *)(cdstp + off), size);
4051         else
4052                 bzero_page(cdstp);
4053
4054         mtx_unlock(&cmtx);
4055 #endif
4056 }
4057 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4058
4059 #if ARM_MMU_XSCALE == 1
4060 void
4061 pmap_zero_page_xscale(vm_paddr_t phys, int off, int size)
4062 {
4063 #ifdef ARM_USE_SMALL_ALLOC
4064         char *dstpg;
4065 #endif
4066
4067         if (_arm_bzero && size >= _min_bzero_size &&
4068             _arm_bzero((void *)(phys + off), size, IS_PHYSICAL) == 0)
4069                 return;
4070 #ifdef ARM_USE_SMALL_ALLOC
4071         dstpg = (char *)arm_ptovirt(phys);
4072         if (off || size != PAGE_SIZE) {
4073                 bzero(dstpg + off, size);
4074                 cpu_dcache_wbinv_range((vm_offset_t)(dstpg + off), size);
4075         } else {
4076                 bzero_page((vm_offset_t)dstpg);
4077                 cpu_dcache_wbinv_range((vm_offset_t)dstpg, PAGE_SIZE);
4078         }
4079 #else
4080         mtx_lock(&cmtx);
4081         /*
4082          * Hook in the page, zero it, and purge the cache for that
4083          * zeroed page. Invalidate the TLB as needed.
4084          */
4085         *cdst_pte = L2_S_PROTO | phys |
4086             L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4087             L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);       /* mini-data */
4088         PTE_SYNC(cdst_pte);
4089         cpu_tlb_flushD_SE(cdstp);
4090         cpu_cpwait();
4091         if (off || size != PAGE_SIZE)
4092                 bzero((void *)(cdstp + off), size);
4093         else
4094                 bzero_page(cdstp);
4095         mtx_unlock(&cmtx);
4096         xscale_cache_clean_minidata();
4097 #endif
4098 }
4099
4100 /*
4101  * Change the PTEs for the specified kernel mappings such that they
4102  * will use the mini data cache instead of the main data cache.
4103  */
4104 void
4105 pmap_use_minicache(vm_offset_t va, vm_size_t size)
4106 {
4107         struct l2_bucket *l2b;
4108         pt_entry_t *ptep, *sptep, pte;
4109         vm_offset_t next_bucket, eva;
4110
4111 #if (ARM_NMMUS > 1) || defined(CPU_XSCALE_CORE3)
4112         if (xscale_use_minidata == 0)
4113                 return;
4114 #endif
4115
4116         eva = va + size;
4117
4118         while (va < eva) {
4119                 next_bucket = L2_NEXT_BUCKET(va);
4120                 if (next_bucket > eva)
4121                         next_bucket = eva;
4122
4123                 l2b = pmap_get_l2_bucket(pmap_kernel(), va);
4124
4125                 sptep = ptep = &l2b->l2b_kva[l2pte_index(va)];
4126
4127                 while (va < next_bucket) {
4128                         pte = *ptep;
4129                         if (!l2pte_minidata(pte)) {
4130                                 cpu_dcache_wbinv_range(va, PAGE_SIZE);
4131                                 cpu_tlb_flushD_SE(va);
4132                                 *ptep = pte & ~L2_B;
4133                         }
4134                         ptep++;
4135                         va += PAGE_SIZE;
4136                 }
4137                 PTE_SYNC_RANGE(sptep, (u_int)(ptep - sptep));
4138         }
4139         cpu_cpwait();
4140 }
4141 #endif /* ARM_MMU_XSCALE == 1 */
4142
4143 /*
4144  *      pmap_zero_page zeros the specified hardware page by mapping 
4145  *      the page into KVM and using bzero to clear its contents.
4146  */
4147 void
4148 pmap_zero_page(vm_page_t m)
4149 {
4150         pmap_zero_page_func(VM_PAGE_TO_PHYS(m), 0, PAGE_SIZE);
4151 }
4152
4153
4154 /*
4155  *      pmap_zero_page_area zeros the specified hardware page by mapping 
4156  *      the page into KVM and using bzero to clear its contents.
4157  *
4158  *      off and size may not cover an area beyond a single hardware page.
4159  */
4160 void
4161 pmap_zero_page_area(vm_page_t m, int off, int size)
4162 {
4163
4164         pmap_zero_page_func(VM_PAGE_TO_PHYS(m), off, size);
4165 }
4166
4167
4168 /*
4169  *      pmap_zero_page_idle zeros the specified hardware page by mapping 
4170  *      the page into KVM and using bzero to clear its contents.  This
4171  *      is intended to be called from the vm_pagezero process only and
4172  *      outside of Giant.
4173  */
4174 void
4175 pmap_zero_page_idle(vm_page_t m)
4176 {
4177
4178         pmap_zero_page(m);
4179 }
4180
4181 #if 0
4182 /*
4183  * pmap_clean_page()
4184  *
4185  * This is a local function used to work out the best strategy to clean
4186  * a single page referenced by its entry in the PV table. It should be used by
4187  * pmap_copy_page, pmap_zero page and maybe some others later on.
4188  *
4189  * Its policy is effectively:
4190  *  o If there are no mappings, we don't bother doing anything with the cache.
4191  *  o If there is one mapping, we clean just that page.
4192  *  o If there are multiple mappings, we clean the entire cache.
4193  *
4194  * So that some functions can be further optimised, it returns 0 if it didn't
4195  * clean the entire cache, or 1 if it did.
4196  *
4197  * XXX One bug in this routine is that if the pv_entry has a single page
4198  * mapped at 0x00000000 a whole cache clean will be performed rather than
4199  * just the 1 page. Since this should not occur in everyday use and if it does
4200  * it will just result in not the most efficient clean for the page.
4201  *
4202  * We don't yet use this function but may want to.
4203  */
4204 static int
4205 pmap_clean_page(struct pv_entry *pv, boolean_t is_src)
4206 {
4207         pmap_t pm, pm_to_clean = NULL;
4208         struct pv_entry *npv;
4209         u_int cache_needs_cleaning = 0;
4210         u_int flags = 0;
4211         vm_offset_t page_to_clean = 0;
4212
4213         if (pv == NULL) {
4214                 /* nothing mapped in so nothing to flush */
4215                 return (0);
4216         }
4217
4218         /*
4219          * Since we flush the cache each time we change to a different
4220          * user vmspace, we only need to flush the page if it is in the
4221          * current pmap.
4222          */
4223         if (curthread)
4224                 pm = vmspace_pmap(curproc->p_vmspace);
4225         else
4226                 pm = pmap_kernel();
4227
4228         for (npv = pv; npv; npv = TAILQ_NEXT(npv, pv_list)) {
4229                 if (npv->pv_pmap == pmap_kernel() || npv->pv_pmap == pm) {
4230                         flags |= npv->pv_flags;
4231                         /*
4232                          * The page is mapped non-cacheable in 
4233                          * this map.  No need to flush the cache.
4234                          */
4235                         if (npv->pv_flags & PVF_NC) {
4236 #ifdef DIAGNOSTIC
4237                                 if (cache_needs_cleaning)
4238                                         panic("pmap_clean_page: "
4239                                             "cache inconsistency");
4240 #endif
4241                                 break;
4242                         } else if (is_src && (npv->pv_flags & PVF_WRITE) == 0)
4243                                 continue;
4244                         if (cache_needs_cleaning) {
4245                                 page_to_clean = 0;
4246                                 break;
4247                         } else {
4248                                 page_to_clean = npv->pv_va;
4249                                 pm_to_clean = npv->pv_pmap;
4250                         }
4251                         cache_needs_cleaning = 1;
4252                 }
4253         }
4254         if (page_to_clean) {
4255                 if (PV_BEEN_EXECD(flags))
4256                         pmap_idcache_wbinv_range(pm_to_clean, page_to_clean,
4257                             PAGE_SIZE);
4258                 else
4259                         pmap_dcache_wb_range(pm_to_clean, page_to_clean,
4260                             PAGE_SIZE, !is_src, (flags & PVF_WRITE) == 0);
4261         } else if (cache_needs_cleaning) {
4262                 if (PV_BEEN_EXECD(flags))
4263                         pmap_idcache_wbinv_all(pm);
4264                 else
4265                         pmap_dcache_wbinv_all(pm);
4266                 return (1);
4267         }
4268         return (0);
4269 }
4270 #endif
4271
4272 /*
4273  *      pmap_copy_page copies the specified (machine independent)
4274  *      page by mapping the page into virtual memory and using
4275  *      bcopy to copy the page, one machine dependent page at a
4276  *      time.
4277  */
4278
4279 /*
4280  * pmap_copy_page()
4281  *
4282  * Copy one physical page into another, by mapping the pages into
4283  * hook points. The same comment regarding cachability as in
4284  * pmap_zero_page also applies here.
4285  */
4286 #if  (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 || defined (CPU_XSCALE_CORE3)
4287 void
4288 pmap_copy_page_generic(vm_paddr_t src, vm_paddr_t dst)
4289 {
4290 #if 0
4291         struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4292 #endif
4293 #ifdef DEBUG
4294         struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4295
4296         if (dst_pg->md.pvh_list != NULL)
4297                 panic("pmap_copy_page: dst page has mappings");
4298 #endif
4299
4300
4301         /*
4302          * Clean the source page.  Hold the source page's lock for
4303          * the duration of the copy so that no other mappings can
4304          * be created while we have a potentially aliased mapping.
4305          */
4306 #if 0
4307         /*
4308          * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4309          * pmap_copy_page().
4310          */
4311         (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4312 #endif
4313         /*
4314          * Map the pages into the page hook points, copy them, and purge
4315          * the cache for the appropriate page. Invalidate the TLB
4316          * as required.
4317          */
4318         mtx_lock(&cmtx);
4319         *csrc_pte = L2_S_PROTO | src |
4320             L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
4321         PTE_SYNC(csrc_pte);
4322         *cdst_pte = L2_S_PROTO | dst |
4323             L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) | pte_l2_s_cache_mode;
4324         PTE_SYNC(cdst_pte);
4325         cpu_tlb_flushD_SE(csrcp);
4326         cpu_tlb_flushD_SE(cdstp);
4327         cpu_cpwait();
4328         bcopy_page(csrcp, cdstp);
4329         mtx_unlock(&cmtx);
4330         cpu_dcache_inv_range(csrcp, PAGE_SIZE);
4331         cpu_dcache_wbinv_range(cdstp, PAGE_SIZE);
4332         cpu_l2cache_inv_range(csrcp, PAGE_SIZE);
4333         cpu_l2cache_wbinv_range(cdstp, PAGE_SIZE);
4334 }
4335 #endif /* (ARM_MMU_GENERIC + ARM_MMU_SA1) != 0 */
4336
4337 #if ARM_MMU_XSCALE == 1
4338 void
4339 pmap_copy_page_xscale(vm_paddr_t src, vm_paddr_t dst)
4340 {
4341 #if 0
4342         /* XXX: Only needed for pmap_clean_page(), which is commented out. */
4343         struct vm_page *src_pg = PHYS_TO_VM_PAGE(src);
4344 #endif
4345 #ifdef DEBUG
4346         struct vm_page *dst_pg = PHYS_TO_VM_PAGE(dst);
4347
4348         if (dst_pg->md.pvh_list != NULL)
4349                 panic("pmap_copy_page: dst page has mappings");
4350 #endif
4351
4352
4353         /*
4354          * Clean the source page.  Hold the source page's lock for
4355          * the duration of the copy so that no other mappings can
4356          * be created while we have a potentially aliased mapping.
4357          */
4358 #if 0
4359         /*
4360          * XXX: Not needed while we call cpu_dcache_wbinv_all() in
4361          * pmap_copy_page().
4362          */
4363         (void) pmap_clean_page(TAILQ_FIRST(&src_pg->md.pv_list), TRUE);
4364 #endif
4365         /*
4366          * Map the pages into the page hook points, copy them, and purge
4367          * the cache for the appropriate page. Invalidate the TLB
4368          * as required.
4369          */
4370         mtx_lock(&cmtx);
4371         *csrc_pte = L2_S_PROTO | src |
4372             L2_S_PROT(PTE_KERNEL, VM_PROT_READ) |
4373             L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);       /* mini-data */
4374         PTE_SYNC(csrc_pte);
4375         *cdst_pte = L2_S_PROTO | dst |
4376             L2_S_PROT(PTE_KERNEL, VM_PROT_WRITE) |
4377             L2_C | L2_XSCALE_T_TEX(TEX_XSCALE_X);       /* mini-data */
4378         PTE_SYNC(cdst_pte);
4379         cpu_tlb_flushD_SE(csrcp);
4380         cpu_tlb_flushD_SE(cdstp);
4381         cpu_cpwait();
4382         bcopy_page(csrcp, cdstp);
4383         mtx_unlock(&cmtx);
4384         xscale_cache_clean_minidata();
4385 }
4386 #endif /* ARM_MMU_XSCALE == 1 */
4387
4388 void
4389 pmap_copy_page(vm_page_t src, vm_page_t dst)
4390 {
4391 #ifdef ARM_USE_SMALL_ALLOC
4392         vm_offset_t srcpg, dstpg;
4393 #endif
4394
4395         cpu_dcache_wbinv_all();
4396         cpu_l2cache_wbinv_all();
4397         if (_arm_memcpy && PAGE_SIZE >= _min_memcpy_size &&
4398             _arm_memcpy((void *)VM_PAGE_TO_PHYS(dst), 
4399             (void *)VM_PAGE_TO_PHYS(src), PAGE_SIZE, IS_PHYSICAL) == 0)
4400                 return;
4401 #ifdef ARM_USE_SMALL_ALLOC
4402         srcpg = arm_ptovirt(VM_PAGE_TO_PHYS(src));
4403         dstpg = arm_ptovirt(VM_PAGE_TO_PHYS(dst));
4404         bcopy_page(srcpg, dstpg);
4405         cpu_dcache_wbinv_range(dstpg, PAGE_SIZE);
4406         cpu_l2cache_wbinv_range(dstpg, PAGE_SIZE);
4407 #else
4408         pmap_copy_page_func(VM_PAGE_TO_PHYS(src), VM_PAGE_TO_PHYS(dst));
4409 #endif
4410 }
4411
4412
4413
4414
4415 /*
4416  * this routine returns true if a physical page resides
4417  * in the given pmap.
4418  */
4419 boolean_t
4420 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
4421 {
4422         pv_entry_t pv;
4423         int loops = 0;
4424         boolean_t rv;
4425         
4426         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4427             ("pmap_page_exists_quick: page %p is not managed", m));
4428         rv = FALSE;
4429         vm_page_lock_queues();
4430         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4431                 if (pv->pv_pmap == pmap) {
4432                         rv = TRUE;
4433                         break;
4434                 }
4435                 loops++;
4436                 if (loops >= 16)
4437                         break;
4438         }
4439         vm_page_unlock_queues();
4440         return (rv);
4441 }
4442
4443 /*
4444  *      pmap_page_wired_mappings:
4445  *
4446  *      Return the number of managed mappings to the given physical page
4447  *      that are wired.
4448  */
4449 int
4450 pmap_page_wired_mappings(vm_page_t m)
4451 {
4452         pv_entry_t pv;
4453         int count;
4454
4455         count = 0;
4456         if ((m->oflags & VPO_UNMANAGED) != 0)
4457                 return (count);
4458         vm_page_lock_queues();
4459         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list)
4460                 if ((pv->pv_flags & PVF_WIRED) != 0)
4461                         count++;
4462         vm_page_unlock_queues();
4463         return (count);
4464 }
4465
4466 /*
4467  *      pmap_ts_referenced:
4468  *
4469  *      Return the count of reference bits for a page, clearing all of them.
4470  */
4471 int
4472 pmap_ts_referenced(vm_page_t m)
4473 {
4474
4475         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4476             ("pmap_ts_referenced: page %p is not managed", m));
4477         return (pmap_clearbit(m, PVF_REF));
4478 }
4479
4480
4481 boolean_t
4482 pmap_is_modified(vm_page_t m)
4483 {
4484
4485         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4486             ("pmap_is_modified: page %p is not managed", m));
4487         if (m->md.pvh_attrs & PVF_MOD)
4488                 return (TRUE);
4489         
4490         return(FALSE);
4491 }
4492
4493
4494 /*
4495  *      Clear the modify bits on the specified physical page.
4496  */
4497 void
4498 pmap_clear_modify(vm_page_t m)
4499 {
4500
4501         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4502             ("pmap_clear_modify: page %p is not managed", m));
4503         VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
4504         KASSERT((m->oflags & VPO_BUSY) == 0,
4505             ("pmap_clear_modify: page %p is busy", m));
4506
4507         /*
4508          * If the page is not PGA_WRITEABLE, then no mappings can be modified.
4509          * If the object containing the page is locked and the page is not
4510          * VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set.
4511          */
4512         if ((m->aflags & PGA_WRITEABLE) == 0)
4513                 return;
4514         if (m->md.pvh_attrs & PVF_MOD)
4515                 pmap_clearbit(m, PVF_MOD);
4516 }
4517
4518
4519 /*
4520  *      pmap_is_referenced:
4521  *
4522  *      Return whether or not the specified physical page was referenced
4523  *      in any physical maps.
4524  */
4525 boolean_t
4526 pmap_is_referenced(vm_page_t m)
4527 {
4528
4529         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4530             ("pmap_is_referenced: page %p is not managed", m));
4531         return ((m->md.pvh_attrs & PVF_REF) != 0);
4532 }
4533
4534 /*
4535  *      pmap_clear_reference:
4536  *
4537  *      Clear the reference bit on the specified physical page.
4538  */
4539 void
4540 pmap_clear_reference(vm_page_t m)
4541 {
4542
4543         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4544             ("pmap_clear_reference: page %p is not managed", m));
4545         if (m->md.pvh_attrs & PVF_REF) 
4546                 pmap_clearbit(m, PVF_REF);
4547 }
4548
4549
4550 /*
4551  * Clear the write and modified bits in each of the given page's mappings.
4552  */
4553 void
4554 pmap_remove_write(vm_page_t m)
4555 {
4556
4557         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4558             ("pmap_remove_write: page %p is not managed", m));
4559
4560         /*
4561          * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by
4562          * another thread while the object is locked.  Thus, if PGA_WRITEABLE
4563          * is clear, no page table entries need updating.
4564          */
4565         VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
4566         if ((m->oflags & VPO_BUSY) != 0 ||
4567             (m->aflags & PGA_WRITEABLE) != 0)
4568                 pmap_clearbit(m, PVF_WRITE);
4569 }
4570
4571
4572 /*
4573  * perform the pmap work for mincore
4574  */
4575 int
4576 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
4577 {
4578         printf("pmap_mincore()\n");
4579         
4580         return (0);
4581 }
4582
4583
4584 void
4585 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
4586 {
4587 }
4588
4589
4590 /*
4591  *      Increase the starting virtual address of the given mapping if a
4592  *      different alignment might result in more superpage mappings.
4593  */
4594 void
4595 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
4596     vm_offset_t *addr, vm_size_t size)
4597 {
4598 }
4599
4600
4601 /*
4602  * Map a set of physical memory pages into the kernel virtual
4603  * address space. Return a pointer to where it is mapped. This
4604  * routine is intended to be used for mapping device memory,
4605  * NOT real memory.
4606  */
4607 void *
4608 pmap_mapdev(vm_offset_t pa, vm_size_t size)
4609 {
4610         vm_offset_t va, tmpva, offset;
4611         
4612         offset = pa & PAGE_MASK;
4613         size = roundup(size, PAGE_SIZE);
4614         
4615         GIANT_REQUIRED;
4616         
4617         va = kmem_alloc_nofault(kernel_map, size);
4618         if (!va)
4619                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
4620         for (tmpva = va; size > 0;) {
4621                 pmap_kenter_internal(tmpva, pa, 0);
4622                 size -= PAGE_SIZE;
4623                 tmpva += PAGE_SIZE;
4624                 pa += PAGE_SIZE;
4625         }
4626         
4627         return ((void *)(va + offset));
4628 }
4629
4630 #define BOOTSTRAP_DEBUG
4631
4632 /*
4633  * pmap_map_section:
4634  *
4635  *      Create a single section mapping.
4636  */
4637 void
4638 pmap_map_section(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4639     int prot, int cache)
4640 {
4641         pd_entry_t *pde = (pd_entry_t *) l1pt;
4642         pd_entry_t fl;
4643
4644         KASSERT(((va | pa) & L1_S_OFFSET) == 0, ("ouin2"));
4645
4646         switch (cache) {
4647         case PTE_NOCACHE:
4648         default:
4649                 fl = 0;
4650                 break;
4651
4652         case PTE_CACHE:
4653                 fl = pte_l1_s_cache_mode;
4654                 break;
4655
4656         case PTE_PAGETABLE:
4657                 fl = pte_l1_s_cache_mode_pt;
4658                 break;
4659         }
4660
4661         pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4662             L1_S_PROT(PTE_KERNEL, prot) | fl | L1_S_DOM(PMAP_DOMAIN_KERNEL);
4663         PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4664
4665 }
4666
4667 /*
4668  * pmap_link_l2pt:
4669  *
4670  *      Link the L2 page table specified by l2pv.pv_pa into the L1
4671  *      page table at the slot for "va".
4672  */
4673 void
4674 pmap_link_l2pt(vm_offset_t l1pt, vm_offset_t va, struct pv_addr *l2pv)
4675 {
4676         pd_entry_t *pde = (pd_entry_t *) l1pt, proto;
4677         u_int slot = va >> L1_S_SHIFT;
4678
4679         proto = L1_S_DOM(PMAP_DOMAIN_KERNEL) | L1_C_PROTO;
4680
4681 #ifdef VERBOSE_INIT_ARM     
4682         printf("pmap_link_l2pt: pa=0x%x va=0x%x\n", l2pv->pv_pa, l2pv->pv_va);
4683 #endif
4684
4685         pde[slot + 0] = proto | (l2pv->pv_pa + 0x000);
4686
4687         PTE_SYNC(&pde[slot]);
4688
4689         SLIST_INSERT_HEAD(&kernel_pt_list, l2pv, pv_list);
4690
4691         
4692 }
4693
4694 /*
4695  * pmap_map_entry
4696  *
4697  *      Create a single page mapping.
4698  */
4699 void
4700 pmap_map_entry(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa, int prot,
4701     int cache)
4702 {
4703         pd_entry_t *pde = (pd_entry_t *) l1pt;
4704         pt_entry_t fl;
4705         pt_entry_t *pte;
4706
4707         KASSERT(((va | pa) & PAGE_MASK) == 0, ("ouin"));
4708
4709         switch (cache) {
4710         case PTE_NOCACHE:
4711         default:
4712                 fl = 0;
4713                 break;
4714
4715         case PTE_CACHE:
4716                 fl = pte_l2_s_cache_mode;
4717                 break;
4718
4719         case PTE_PAGETABLE:
4720                 fl = pte_l2_s_cache_mode_pt;
4721                 break;
4722         }
4723
4724         if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4725                 panic("pmap_map_entry: no L2 table for VA 0x%08x", va);
4726
4727         pte = (pt_entry_t *) kernel_pt_lookup(pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4728
4729         if (pte == NULL)
4730                 panic("pmap_map_entry: can't find L2 table for VA 0x%08x", va);
4731
4732         pte[l2pte_index(va)] =
4733             L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | fl;
4734         PTE_SYNC(&pte[l2pte_index(va)]);
4735 }
4736
4737 /*
4738  * pmap_map_chunk:
4739  *
4740  *      Map a chunk of memory using the most efficient mappings
4741  *      possible (section. large page, small page) into the
4742  *      provided L1 and L2 tables at the specified virtual address.
4743  */
4744 vm_size_t
4745 pmap_map_chunk(vm_offset_t l1pt, vm_offset_t va, vm_offset_t pa,
4746     vm_size_t size, int prot, int cache)
4747 {
4748         pd_entry_t *pde = (pd_entry_t *) l1pt;
4749         pt_entry_t *pte, f1, f2s, f2l;
4750         vm_size_t resid;  
4751         int i;
4752
4753         resid = (size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
4754
4755         if (l1pt == 0)
4756                 panic("pmap_map_chunk: no L1 table provided");
4757
4758 #ifdef VERBOSE_INIT_ARM     
4759         printf("pmap_map_chunk: pa=0x%x va=0x%x size=0x%x resid=0x%x "
4760             "prot=0x%x cache=%d\n", pa, va, size, resid, prot, cache);
4761 #endif
4762
4763         switch (cache) {
4764         case PTE_NOCACHE:
4765         default:
4766                 f1 = 0;
4767                 f2l = 0;
4768                 f2s = 0;
4769                 break;
4770
4771         case PTE_CACHE:
4772                 f1 = pte_l1_s_cache_mode;
4773                 f2l = pte_l2_l_cache_mode;
4774                 f2s = pte_l2_s_cache_mode;
4775                 break;
4776
4777         case PTE_PAGETABLE:
4778                 f1 = pte_l1_s_cache_mode_pt;
4779                 f2l = pte_l2_l_cache_mode_pt;
4780                 f2s = pte_l2_s_cache_mode_pt;
4781                 break;
4782         }
4783
4784         size = resid;
4785
4786         while (resid > 0) {
4787                 /* See if we can use a section mapping. */
4788                 if (L1_S_MAPPABLE_P(va, pa, resid)) {
4789 #ifdef VERBOSE_INIT_ARM
4790                         printf("S");
4791 #endif
4792                         pde[va >> L1_S_SHIFT] = L1_S_PROTO | pa |
4793                             L1_S_PROT(PTE_KERNEL, prot) | f1 |
4794                             L1_S_DOM(PMAP_DOMAIN_KERNEL);
4795                         PTE_SYNC(&pde[va >> L1_S_SHIFT]);
4796                         va += L1_S_SIZE;
4797                         pa += L1_S_SIZE;
4798                         resid -= L1_S_SIZE;
4799                         continue;
4800                 }
4801
4802                 /*
4803                  * Ok, we're going to use an L2 table.  Make sure
4804                  * one is actually in the corresponding L1 slot
4805                  * for the current VA.
4806                  */
4807                 if ((pde[va >> L1_S_SHIFT] & L1_TYPE_MASK) != L1_TYPE_C)
4808                         panic("pmap_map_chunk: no L2 table for VA 0x%08x", va);
4809
4810                 pte = (pt_entry_t *) kernel_pt_lookup(
4811                     pde[L1_IDX(va)] & L1_C_ADDR_MASK);
4812                 if (pte == NULL)
4813                         panic("pmap_map_chunk: can't find L2 table for VA"
4814                             "0x%08x", va);
4815                 /* See if we can use a L2 large page mapping. */
4816                 if (L2_L_MAPPABLE_P(va, pa, resid)) {
4817 #ifdef VERBOSE_INIT_ARM
4818                         printf("L");
4819 #endif
4820                         for (i = 0; i < 16; i++) {
4821                                 pte[l2pte_index(va) + i] =
4822                                     L2_L_PROTO | pa |
4823                                     L2_L_PROT(PTE_KERNEL, prot) | f2l;
4824                                 PTE_SYNC(&pte[l2pte_index(va) + i]);
4825                         }
4826                         va += L2_L_SIZE;
4827                         pa += L2_L_SIZE;
4828                         resid -= L2_L_SIZE;
4829                         continue;
4830                 }
4831
4832                 /* Use a small page mapping. */
4833 #ifdef VERBOSE_INIT_ARM
4834                 printf("P");
4835 #endif
4836                 pte[l2pte_index(va)] =
4837                     L2_S_PROTO | pa | L2_S_PROT(PTE_KERNEL, prot) | f2s;
4838                 PTE_SYNC(&pte[l2pte_index(va)]);
4839                 va += PAGE_SIZE;
4840                 pa += PAGE_SIZE;
4841                 resid -= PAGE_SIZE;
4842         }
4843 #ifdef VERBOSE_INIT_ARM
4844         printf("\n");
4845 #endif
4846         return (size);
4847
4848 }
4849
4850 /********************** Static device map routines ***************************/
4851
4852 static const struct pmap_devmap *pmap_devmap_table;
4853
4854 /*
4855  * Register the devmap table.  This is provided in case early console
4856  * initialization needs to register mappings created by bootstrap code
4857  * before pmap_devmap_bootstrap() is called.
4858  */
4859 void
4860 pmap_devmap_register(const struct pmap_devmap *table)
4861 {
4862
4863         pmap_devmap_table = table;
4864 }
4865
4866 /*
4867  * Map all of the static regions in the devmap table, and remember
4868  * the devmap table so other parts of the kernel can look up entries
4869  * later.
4870  */
4871 void
4872 pmap_devmap_bootstrap(vm_offset_t l1pt, const struct pmap_devmap *table)
4873 {
4874         int i;
4875
4876         pmap_devmap_table = table;
4877
4878         for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4879 #ifdef VERBOSE_INIT_ARM
4880                 printf("devmap: %08x -> %08x @ %08x\n",
4881                     pmap_devmap_table[i].pd_pa,
4882                     pmap_devmap_table[i].pd_pa +
4883                         pmap_devmap_table[i].pd_size - 1,
4884                     pmap_devmap_table[i].pd_va);
4885 #endif
4886                 pmap_map_chunk(l1pt, pmap_devmap_table[i].pd_va,
4887                     pmap_devmap_table[i].pd_pa,
4888                     pmap_devmap_table[i].pd_size,
4889                     pmap_devmap_table[i].pd_prot,
4890                     pmap_devmap_table[i].pd_cache);
4891         }
4892 }
4893
4894 const struct pmap_devmap *
4895 pmap_devmap_find_pa(vm_paddr_t pa, vm_size_t size)
4896 {
4897         int i;
4898
4899         if (pmap_devmap_table == NULL)
4900                 return (NULL);
4901
4902         for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4903                 if (pa >= pmap_devmap_table[i].pd_pa &&
4904                     pa + size <= pmap_devmap_table[i].pd_pa +
4905                                  pmap_devmap_table[i].pd_size)
4906                         return (&pmap_devmap_table[i]);
4907         }
4908
4909         return (NULL);
4910 }
4911
4912 const struct pmap_devmap *
4913 pmap_devmap_find_va(vm_offset_t va, vm_size_t size)
4914 {
4915         int i;
4916
4917         if (pmap_devmap_table == NULL)
4918                 return (NULL);
4919
4920         for (i = 0; pmap_devmap_table[i].pd_size != 0; i++) {
4921                 if (va >= pmap_devmap_table[i].pd_va &&
4922                     va + size <= pmap_devmap_table[i].pd_va +
4923                                  pmap_devmap_table[i].pd_size)
4924                         return (&pmap_devmap_table[i]);
4925         }
4926
4927         return (NULL);
4928 }
4929