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