xref: /freebsd/sys/powerpc/booke/pmap.c (revision 0fe0fe112fa6cc220f14a1a9196b51fdc79edc72)
1 /*-
2  * Copyright (C) 2007-2009 Semihalf, Rafal Jaworowski <raj@semihalf.com>
3  * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
18  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * Some hw specific parts of this pmap were derived or influenced
27  * by NetBSD's ibm4xx pmap module. More generic code is shared with
28  * a few other pmap modules from the FreeBSD tree.
29  */
30 
31  /*
32   * VM layout notes:
33   *
34   * Kernel and user threads run within one common virtual address space
35   * defined by AS=0.
36   *
37   * Virtual address space layout:
38   * -----------------------------
39   * 0x0000_0000 - 0xafff_ffff	: user process
40   * 0xb000_0000 - 0xbfff_ffff	: pmap_mapdev()-ed area (PCI/PCIE etc.)
41   * 0xc000_0000 - 0xc0ff_ffff	: kernel reserved
42   *   0xc000_0000 - data_end	: kernel code+data, env, metadata etc.
43   * 0xc100_0000 - 0xfeef_ffff	: KVA
44   *   0xc100_0000 - 0xc100_3fff : reserved for page zero/copy
45   *   0xc100_4000 - 0xc200_3fff : reserved for ptbl bufs
46   *   0xc200_4000 - 0xc200_8fff : guard page + kstack0
47   *   0xc200_9000 - 0xfeef_ffff	: actual free KVA space
48   * 0xfef0_0000 - 0xffff_ffff	: I/O devices region
49   */
50 
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53 
54 #include "opt_kstack_pages.h"
55 
56 #include <sys/param.h>
57 #include <sys/conf.h>
58 #include <sys/malloc.h>
59 #include <sys/ktr.h>
60 #include <sys/proc.h>
61 #include <sys/user.h>
62 #include <sys/queue.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/kerneldump.h>
66 #include <sys/linker.h>
67 #include <sys/msgbuf.h>
68 #include <sys/lock.h>
69 #include <sys/mutex.h>
70 #include <sys/rwlock.h>
71 #include <sys/sched.h>
72 #include <sys/smp.h>
73 #include <sys/vmmeter.h>
74 
75 #include <vm/vm.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_pageout.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_pager.h>
84 #include <vm/uma.h>
85 
86 #include <machine/cpu.h>
87 #include <machine/pcb.h>
88 #include <machine/platform.h>
89 
90 #include <machine/tlb.h>
91 #include <machine/spr.h>
92 #include <machine/md_var.h>
93 #include <machine/mmuvar.h>
94 #include <machine/pmap.h>
95 #include <machine/pte.h>
96 
97 #include "mmu_if.h"
98 
99 #ifdef  DEBUG
100 #define debugf(fmt, args...) printf(fmt, ##args)
101 #else
102 #define debugf(fmt, args...)
103 #endif
104 
105 #define TODO			panic("%s: not implemented", __func__);
106 
107 extern unsigned char _etext[];
108 extern unsigned char _end[];
109 
110 extern uint32_t *bootinfo;
111 
112 #ifdef SMP
113 extern uint32_t bp_ntlb1s;
114 #endif
115 
116 vm_paddr_t kernload;
117 vm_offset_t kernstart;
118 vm_size_t kernsize;
119 
120 /* Message buffer and tables. */
121 static vm_offset_t data_start;
122 static vm_size_t data_end;
123 
124 /* Phys/avail memory regions. */
125 static struct mem_region *availmem_regions;
126 static int availmem_regions_sz;
127 static struct mem_region *physmem_regions;
128 static int physmem_regions_sz;
129 
130 /* Reserved KVA space and mutex for mmu_booke_zero_page. */
131 static vm_offset_t zero_page_va;
132 static struct mtx zero_page_mutex;
133 
134 static struct mtx tlbivax_mutex;
135 
136 /*
137  * Reserved KVA space for mmu_booke_zero_page_idle. This is used
138  * by idle thred only, no lock required.
139  */
140 static vm_offset_t zero_page_idle_va;
141 
142 /* Reserved KVA space and mutex for mmu_booke_copy_page. */
143 static vm_offset_t copy_page_src_va;
144 static vm_offset_t copy_page_dst_va;
145 static struct mtx copy_page_mutex;
146 
147 /**************************************************************************/
148 /* PMAP */
149 /**************************************************************************/
150 
151 static int mmu_booke_enter_locked(mmu_t, pmap_t, vm_offset_t, vm_page_t,
152     vm_prot_t, u_int flags, int8_t psind);
153 
154 unsigned int kptbl_min;		/* Index of the first kernel ptbl. */
155 unsigned int kernel_ptbls;	/* Number of KVA ptbls. */
156 
157 /*
158  * If user pmap is processed with mmu_booke_remove and the resident count
159  * drops to 0, there are no more pages to remove, so we need not continue.
160  */
161 #define PMAP_REMOVE_DONE(pmap) \
162 	((pmap) != kernel_pmap && (pmap)->pm_stats.resident_count == 0)
163 
164 extern int elf32_nxstack;
165 
166 /**************************************************************************/
167 /* TLB and TID handling */
168 /**************************************************************************/
169 
170 /* Translation ID busy table */
171 static volatile pmap_t tidbusy[MAXCPU][TID_MAX + 1];
172 
173 /*
174  * TLB0 capabilities (entry, way numbers etc.). These can vary between e500
175  * core revisions and should be read from h/w registers during early config.
176  */
177 uint32_t tlb0_entries;
178 uint32_t tlb0_ways;
179 uint32_t tlb0_entries_per_way;
180 uint32_t tlb1_entries;
181 
182 #define TLB0_ENTRIES		(tlb0_entries)
183 #define TLB0_WAYS		(tlb0_ways)
184 #define TLB0_ENTRIES_PER_WAY	(tlb0_entries_per_way)
185 
186 #define TLB1_ENTRIES (tlb1_entries)
187 #define TLB1_MAXENTRIES	64
188 
189 /* In-ram copy of the TLB1 */
190 static tlb_entry_t tlb1[TLB1_MAXENTRIES];
191 
192 /* Next free entry in the TLB1 */
193 static unsigned int tlb1_idx;
194 static vm_offset_t tlb1_map_base = VM_MAX_KERNEL_ADDRESS;
195 
196 static tlbtid_t tid_alloc(struct pmap *);
197 static void tid_flush(tlbtid_t tid);
198 
199 static void tlb_print_entry(int, uint32_t, uint32_t, uint32_t, uint32_t);
200 
201 static int tlb1_set_entry(vm_offset_t, vm_paddr_t, vm_size_t, uint32_t);
202 static void tlb1_write_entry(unsigned int);
203 static int tlb1_iomapped(int, vm_paddr_t, vm_size_t, vm_offset_t *);
204 static vm_size_t tlb1_mapin_region(vm_offset_t, vm_paddr_t, vm_size_t);
205 
206 static vm_size_t tsize2size(unsigned int);
207 static unsigned int size2tsize(vm_size_t);
208 static unsigned int ilog2(unsigned int);
209 
210 static void set_mas4_defaults(void);
211 
212 static inline void tlb0_flush_entry(vm_offset_t);
213 static inline unsigned int tlb0_tableidx(vm_offset_t, unsigned int);
214 
215 /**************************************************************************/
216 /* Page table management */
217 /**************************************************************************/
218 
219 static struct rwlock_padalign pvh_global_lock;
220 
221 /* Data for the pv entry allocation mechanism */
222 static uma_zone_t pvzone;
223 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
224 
225 #define PV_ENTRY_ZONE_MIN	2048	/* min pv entries in uma zone */
226 
227 #ifndef PMAP_SHPGPERPROC
228 #define PMAP_SHPGPERPROC	200
229 #endif
230 
231 static void ptbl_init(void);
232 static struct ptbl_buf *ptbl_buf_alloc(void);
233 static void ptbl_buf_free(struct ptbl_buf *);
234 static void ptbl_free_pmap_ptbl(pmap_t, pte_t *);
235 
236 static pte_t *ptbl_alloc(mmu_t, pmap_t, unsigned int, boolean_t);
237 static void ptbl_free(mmu_t, pmap_t, unsigned int);
238 static void ptbl_hold(mmu_t, pmap_t, unsigned int);
239 static int ptbl_unhold(mmu_t, pmap_t, unsigned int);
240 
241 static vm_paddr_t pte_vatopa(mmu_t, pmap_t, vm_offset_t);
242 static pte_t *pte_find(mmu_t, pmap_t, vm_offset_t);
243 static int pte_enter(mmu_t, pmap_t, vm_page_t, vm_offset_t, uint32_t, boolean_t);
244 static int pte_remove(mmu_t, pmap_t, vm_offset_t, uint8_t);
245 static void kernel_pte_alloc(vm_offset_t data_end, vm_offset_t addr,
246 			     vm_offset_t pdir);
247 
248 static pv_entry_t pv_alloc(void);
249 static void pv_free(pv_entry_t);
250 static void pv_insert(pmap_t, vm_offset_t, vm_page_t);
251 static void pv_remove(pmap_t, vm_offset_t, vm_page_t);
252 
253 static void booke_pmap_init_qpages(void);
254 
255 /* Number of kva ptbl buffers, each covering one ptbl (PTBL_PAGES). */
256 #define PTBL_BUFS		(128 * 16)
257 
258 struct ptbl_buf {
259 	TAILQ_ENTRY(ptbl_buf) link;	/* list link */
260 	vm_offset_t kva;		/* va of mapping */
261 };
262 
263 /* ptbl free list and a lock used for access synchronization. */
264 static TAILQ_HEAD(, ptbl_buf) ptbl_buf_freelist;
265 static struct mtx ptbl_buf_freelist_lock;
266 
267 /* Base address of kva space allocated fot ptbl bufs. */
268 static vm_offset_t ptbl_buf_pool_vabase;
269 
270 /* Pointer to ptbl_buf structures. */
271 static struct ptbl_buf *ptbl_bufs;
272 
273 #ifdef SMP
274 void pmap_bootstrap_ap(volatile uint32_t *);
275 #endif
276 
277 /*
278  * Kernel MMU interface
279  */
280 static void		mmu_booke_clear_modify(mmu_t, vm_page_t);
281 static void		mmu_booke_copy(mmu_t, pmap_t, pmap_t, vm_offset_t,
282     vm_size_t, vm_offset_t);
283 static void		mmu_booke_copy_page(mmu_t, vm_page_t, vm_page_t);
284 static void		mmu_booke_copy_pages(mmu_t, vm_page_t *,
285     vm_offset_t, vm_page_t *, vm_offset_t, int);
286 static int		mmu_booke_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t,
287     vm_prot_t, u_int flags, int8_t psind);
288 static void		mmu_booke_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t,
289     vm_page_t, vm_prot_t);
290 static void		mmu_booke_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t,
291     vm_prot_t);
292 static vm_paddr_t	mmu_booke_extract(mmu_t, pmap_t, vm_offset_t);
293 static vm_page_t	mmu_booke_extract_and_hold(mmu_t, pmap_t, vm_offset_t,
294     vm_prot_t);
295 static void		mmu_booke_init(mmu_t);
296 static boolean_t	mmu_booke_is_modified(mmu_t, vm_page_t);
297 static boolean_t	mmu_booke_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
298 static boolean_t	mmu_booke_is_referenced(mmu_t, vm_page_t);
299 static int		mmu_booke_ts_referenced(mmu_t, vm_page_t);
300 static vm_offset_t	mmu_booke_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t,
301     int);
302 static int		mmu_booke_mincore(mmu_t, pmap_t, vm_offset_t,
303     vm_paddr_t *);
304 static void		mmu_booke_object_init_pt(mmu_t, pmap_t, vm_offset_t,
305     vm_object_t, vm_pindex_t, vm_size_t);
306 static boolean_t	mmu_booke_page_exists_quick(mmu_t, pmap_t, vm_page_t);
307 static void		mmu_booke_page_init(mmu_t, vm_page_t);
308 static int		mmu_booke_page_wired_mappings(mmu_t, vm_page_t);
309 static void		mmu_booke_pinit(mmu_t, pmap_t);
310 static void		mmu_booke_pinit0(mmu_t, pmap_t);
311 static void		mmu_booke_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t,
312     vm_prot_t);
313 static void		mmu_booke_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
314 static void		mmu_booke_qremove(mmu_t, vm_offset_t, int);
315 static void		mmu_booke_release(mmu_t, pmap_t);
316 static void		mmu_booke_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
317 static void		mmu_booke_remove_all(mmu_t, vm_page_t);
318 static void		mmu_booke_remove_write(mmu_t, vm_page_t);
319 static void		mmu_booke_unwire(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
320 static void		mmu_booke_zero_page(mmu_t, vm_page_t);
321 static void		mmu_booke_zero_page_area(mmu_t, vm_page_t, int, int);
322 static void		mmu_booke_zero_page_idle(mmu_t, vm_page_t);
323 static void		mmu_booke_activate(mmu_t, struct thread *);
324 static void		mmu_booke_deactivate(mmu_t, struct thread *);
325 static void		mmu_booke_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
326 static void		*mmu_booke_mapdev(mmu_t, vm_paddr_t, vm_size_t);
327 static void		*mmu_booke_mapdev_attr(mmu_t, vm_paddr_t, vm_size_t, vm_memattr_t);
328 static void		mmu_booke_unmapdev(mmu_t, vm_offset_t, vm_size_t);
329 static vm_paddr_t	mmu_booke_kextract(mmu_t, vm_offset_t);
330 static void		mmu_booke_kenter(mmu_t, vm_offset_t, vm_paddr_t);
331 static void		mmu_booke_kenter_attr(mmu_t, vm_offset_t, vm_paddr_t, vm_memattr_t);
332 static void		mmu_booke_kremove(mmu_t, vm_offset_t);
333 static boolean_t	mmu_booke_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t);
334 static void		mmu_booke_sync_icache(mmu_t, pmap_t, vm_offset_t,
335     vm_size_t);
336 static void		mmu_booke_dumpsys_map(mmu_t, vm_paddr_t pa, size_t,
337     void **);
338 static void		mmu_booke_dumpsys_unmap(mmu_t, vm_paddr_t pa, size_t,
339     void *);
340 static void		mmu_booke_scan_init(mmu_t);
341 static vm_offset_t	mmu_booke_quick_enter_page(mmu_t mmu, vm_page_t m);
342 static void		mmu_booke_quick_remove_page(mmu_t mmu, vm_offset_t addr);
343 
344 static mmu_method_t mmu_booke_methods[] = {
345 	/* pmap dispatcher interface */
346 	MMUMETHOD(mmu_clear_modify,	mmu_booke_clear_modify),
347 	MMUMETHOD(mmu_copy,		mmu_booke_copy),
348 	MMUMETHOD(mmu_copy_page,	mmu_booke_copy_page),
349 	MMUMETHOD(mmu_copy_pages,	mmu_booke_copy_pages),
350 	MMUMETHOD(mmu_enter,		mmu_booke_enter),
351 	MMUMETHOD(mmu_enter_object,	mmu_booke_enter_object),
352 	MMUMETHOD(mmu_enter_quick,	mmu_booke_enter_quick),
353 	MMUMETHOD(mmu_extract,		mmu_booke_extract),
354 	MMUMETHOD(mmu_extract_and_hold,	mmu_booke_extract_and_hold),
355 	MMUMETHOD(mmu_init,		mmu_booke_init),
356 	MMUMETHOD(mmu_is_modified,	mmu_booke_is_modified),
357 	MMUMETHOD(mmu_is_prefaultable,	mmu_booke_is_prefaultable),
358 	MMUMETHOD(mmu_is_referenced,	mmu_booke_is_referenced),
359 	MMUMETHOD(mmu_ts_referenced,	mmu_booke_ts_referenced),
360 	MMUMETHOD(mmu_map,		mmu_booke_map),
361 	MMUMETHOD(mmu_mincore,		mmu_booke_mincore),
362 	MMUMETHOD(mmu_object_init_pt,	mmu_booke_object_init_pt),
363 	MMUMETHOD(mmu_page_exists_quick,mmu_booke_page_exists_quick),
364 	MMUMETHOD(mmu_page_init,	mmu_booke_page_init),
365 	MMUMETHOD(mmu_page_wired_mappings, mmu_booke_page_wired_mappings),
366 	MMUMETHOD(mmu_pinit,		mmu_booke_pinit),
367 	MMUMETHOD(mmu_pinit0,		mmu_booke_pinit0),
368 	MMUMETHOD(mmu_protect,		mmu_booke_protect),
369 	MMUMETHOD(mmu_qenter,		mmu_booke_qenter),
370 	MMUMETHOD(mmu_qremove,		mmu_booke_qremove),
371 	MMUMETHOD(mmu_release,		mmu_booke_release),
372 	MMUMETHOD(mmu_remove,		mmu_booke_remove),
373 	MMUMETHOD(mmu_remove_all,	mmu_booke_remove_all),
374 	MMUMETHOD(mmu_remove_write,	mmu_booke_remove_write),
375 	MMUMETHOD(mmu_sync_icache,	mmu_booke_sync_icache),
376 	MMUMETHOD(mmu_unwire,		mmu_booke_unwire),
377 	MMUMETHOD(mmu_zero_page,	mmu_booke_zero_page),
378 	MMUMETHOD(mmu_zero_page_area,	mmu_booke_zero_page_area),
379 	MMUMETHOD(mmu_zero_page_idle,	mmu_booke_zero_page_idle),
380 	MMUMETHOD(mmu_activate,		mmu_booke_activate),
381 	MMUMETHOD(mmu_deactivate,	mmu_booke_deactivate),
382 	MMUMETHOD(mmu_quick_enter_page, mmu_booke_quick_enter_page),
383 	MMUMETHOD(mmu_quick_remove_page, mmu_booke_quick_remove_page),
384 
385 	/* Internal interfaces */
386 	MMUMETHOD(mmu_bootstrap,	mmu_booke_bootstrap),
387 	MMUMETHOD(mmu_dev_direct_mapped,mmu_booke_dev_direct_mapped),
388 	MMUMETHOD(mmu_mapdev,		mmu_booke_mapdev),
389 	MMUMETHOD(mmu_mapdev_attr,	mmu_booke_mapdev_attr),
390 	MMUMETHOD(mmu_kenter,		mmu_booke_kenter),
391 	MMUMETHOD(mmu_kenter_attr,	mmu_booke_kenter_attr),
392 	MMUMETHOD(mmu_kextract,		mmu_booke_kextract),
393 /*	MMUMETHOD(mmu_kremove,		mmu_booke_kremove),	*/
394 	MMUMETHOD(mmu_unmapdev,		mmu_booke_unmapdev),
395 
396 	/* dumpsys() support */
397 	MMUMETHOD(mmu_dumpsys_map,	mmu_booke_dumpsys_map),
398 	MMUMETHOD(mmu_dumpsys_unmap,	mmu_booke_dumpsys_unmap),
399 	MMUMETHOD(mmu_scan_init,	mmu_booke_scan_init),
400 
401 	{ 0, 0 }
402 };
403 
404 MMU_DEF(booke_mmu, MMU_TYPE_BOOKE, mmu_booke_methods, 0);
405 
406 static __inline uint32_t
407 tlb_calc_wimg(vm_paddr_t pa, vm_memattr_t ma)
408 {
409 	uint32_t attrib;
410 	int i;
411 
412 	if (ma != VM_MEMATTR_DEFAULT) {
413 		switch (ma) {
414 		case VM_MEMATTR_UNCACHEABLE:
415 			return (MAS2_I | MAS2_G);
416 		case VM_MEMATTR_WRITE_COMBINING:
417 		case VM_MEMATTR_WRITE_BACK:
418 		case VM_MEMATTR_PREFETCHABLE:
419 			return (MAS2_I);
420 		case VM_MEMATTR_WRITE_THROUGH:
421 			return (MAS2_W | MAS2_M);
422 		}
423 	}
424 
425 	/*
426 	 * Assume the page is cache inhibited and access is guarded unless
427 	 * it's in our available memory array.
428 	 */
429 	attrib = _TLB_ENTRY_IO;
430 	for (i = 0; i < physmem_regions_sz; i++) {
431 		if ((pa >= physmem_regions[i].mr_start) &&
432 		    (pa < (physmem_regions[i].mr_start +
433 		     physmem_regions[i].mr_size))) {
434 			attrib = _TLB_ENTRY_MEM;
435 			break;
436 		}
437 	}
438 
439 	return (attrib);
440 }
441 
442 static inline void
443 tlb_miss_lock(void)
444 {
445 #ifdef SMP
446 	struct pcpu *pc;
447 
448 	if (!smp_started)
449 		return;
450 
451 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
452 		if (pc != pcpup) {
453 
454 			CTR3(KTR_PMAP, "%s: tlb miss LOCK of CPU=%d, "
455 			    "tlb_lock=%p", __func__, pc->pc_cpuid, pc->pc_booke_tlb_lock);
456 
457 			KASSERT((pc->pc_cpuid != PCPU_GET(cpuid)),
458 			    ("tlb_miss_lock: tried to lock self"));
459 
460 			tlb_lock(pc->pc_booke_tlb_lock);
461 
462 			CTR1(KTR_PMAP, "%s: locked", __func__);
463 		}
464 	}
465 #endif
466 }
467 
468 static inline void
469 tlb_miss_unlock(void)
470 {
471 #ifdef SMP
472 	struct pcpu *pc;
473 
474 	if (!smp_started)
475 		return;
476 
477 	STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
478 		if (pc != pcpup) {
479 			CTR2(KTR_PMAP, "%s: tlb miss UNLOCK of CPU=%d",
480 			    __func__, pc->pc_cpuid);
481 
482 			tlb_unlock(pc->pc_booke_tlb_lock);
483 
484 			CTR1(KTR_PMAP, "%s: unlocked", __func__);
485 		}
486 	}
487 #endif
488 }
489 
490 /* Return number of entries in TLB0. */
491 static __inline void
492 tlb0_get_tlbconf(void)
493 {
494 	uint32_t tlb0_cfg;
495 
496 	tlb0_cfg = mfspr(SPR_TLB0CFG);
497 	tlb0_entries = tlb0_cfg & TLBCFG_NENTRY_MASK;
498 	tlb0_ways = (tlb0_cfg & TLBCFG_ASSOC_MASK) >> TLBCFG_ASSOC_SHIFT;
499 	tlb0_entries_per_way = tlb0_entries / tlb0_ways;
500 }
501 
502 /* Return number of entries in TLB1. */
503 static __inline void
504 tlb1_get_tlbconf(void)
505 {
506 	uint32_t tlb1_cfg;
507 
508 	tlb1_cfg = mfspr(SPR_TLB1CFG);
509 	tlb1_entries = tlb1_cfg & TLBCFG_NENTRY_MASK;
510 }
511 
512 /**************************************************************************/
513 /* Page table related */
514 /**************************************************************************/
515 
516 /* Initialize pool of kva ptbl buffers. */
517 static void
518 ptbl_init(void)
519 {
520 	int i;
521 
522 	CTR3(KTR_PMAP, "%s: s (ptbl_bufs = 0x%08x size 0x%08x)", __func__,
523 	    (uint32_t)ptbl_bufs, sizeof(struct ptbl_buf) * PTBL_BUFS);
524 	CTR3(KTR_PMAP, "%s: s (ptbl_buf_pool_vabase = 0x%08x size = 0x%08x)",
525 	    __func__, ptbl_buf_pool_vabase, PTBL_BUFS * PTBL_PAGES * PAGE_SIZE);
526 
527 	mtx_init(&ptbl_buf_freelist_lock, "ptbl bufs lock", NULL, MTX_DEF);
528 	TAILQ_INIT(&ptbl_buf_freelist);
529 
530 	for (i = 0; i < PTBL_BUFS; i++) {
531 		ptbl_bufs[i].kva = ptbl_buf_pool_vabase + i * PTBL_PAGES * PAGE_SIZE;
532 		TAILQ_INSERT_TAIL(&ptbl_buf_freelist, &ptbl_bufs[i], link);
533 	}
534 }
535 
536 /* Get a ptbl_buf from the freelist. */
537 static struct ptbl_buf *
538 ptbl_buf_alloc(void)
539 {
540 	struct ptbl_buf *buf;
541 
542 	mtx_lock(&ptbl_buf_freelist_lock);
543 	buf = TAILQ_FIRST(&ptbl_buf_freelist);
544 	if (buf != NULL)
545 		TAILQ_REMOVE(&ptbl_buf_freelist, buf, link);
546 	mtx_unlock(&ptbl_buf_freelist_lock);
547 
548 	CTR2(KTR_PMAP, "%s: buf = %p", __func__, buf);
549 
550 	return (buf);
551 }
552 
553 /* Return ptbl buff to free pool. */
554 static void
555 ptbl_buf_free(struct ptbl_buf *buf)
556 {
557 
558 	CTR2(KTR_PMAP, "%s: buf = %p", __func__, buf);
559 
560 	mtx_lock(&ptbl_buf_freelist_lock);
561 	TAILQ_INSERT_TAIL(&ptbl_buf_freelist, buf, link);
562 	mtx_unlock(&ptbl_buf_freelist_lock);
563 }
564 
565 /*
566  * Search the list of allocated ptbl bufs and find on list of allocated ptbls
567  */
568 static void
569 ptbl_free_pmap_ptbl(pmap_t pmap, pte_t *ptbl)
570 {
571 	struct ptbl_buf *pbuf;
572 
573 	CTR2(KTR_PMAP, "%s: ptbl = %p", __func__, ptbl);
574 
575 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
576 
577 	TAILQ_FOREACH(pbuf, &pmap->pm_ptbl_list, link)
578 		if (pbuf->kva == (vm_offset_t)ptbl) {
579 			/* Remove from pmap ptbl buf list. */
580 			TAILQ_REMOVE(&pmap->pm_ptbl_list, pbuf, link);
581 
582 			/* Free corresponding ptbl buf. */
583 			ptbl_buf_free(pbuf);
584 			break;
585 		}
586 }
587 
588 /* Allocate page table. */
589 static pte_t *
590 ptbl_alloc(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx, boolean_t nosleep)
591 {
592 	vm_page_t mtbl[PTBL_PAGES];
593 	vm_page_t m;
594 	struct ptbl_buf *pbuf;
595 	unsigned int pidx;
596 	pte_t *ptbl;
597 	int i, j;
598 
599 	CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
600 	    (pmap == kernel_pmap), pdir_idx);
601 
602 	KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
603 	    ("ptbl_alloc: invalid pdir_idx"));
604 	KASSERT((pmap->pm_pdir[pdir_idx] == NULL),
605 	    ("pte_alloc: valid ptbl entry exists!"));
606 
607 	pbuf = ptbl_buf_alloc();
608 	if (pbuf == NULL)
609 		panic("pte_alloc: couldn't alloc kernel virtual memory");
610 
611 	ptbl = (pte_t *)pbuf->kva;
612 
613 	CTR2(KTR_PMAP, "%s: ptbl kva = %p", __func__, ptbl);
614 
615 	/* Allocate ptbl pages, this will sleep! */
616 	for (i = 0; i < PTBL_PAGES; i++) {
617 		pidx = (PTBL_PAGES * pdir_idx) + i;
618 		while ((m = vm_page_alloc(NULL, pidx,
619 		    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
620 			PMAP_UNLOCK(pmap);
621 			rw_wunlock(&pvh_global_lock);
622 			if (nosleep) {
623 				ptbl_free_pmap_ptbl(pmap, ptbl);
624 				for (j = 0; j < i; j++)
625 					vm_page_free(mtbl[j]);
626 				atomic_subtract_int(&vm_cnt.v_wire_count, i);
627 				return (NULL);
628 			}
629 			VM_WAIT;
630 			rw_wlock(&pvh_global_lock);
631 			PMAP_LOCK(pmap);
632 		}
633 		mtbl[i] = m;
634 	}
635 
636 	/* Map allocated pages into kernel_pmap. */
637 	mmu_booke_qenter(mmu, (vm_offset_t)ptbl, mtbl, PTBL_PAGES);
638 
639 	/* Zero whole ptbl. */
640 	bzero((caddr_t)ptbl, PTBL_PAGES * PAGE_SIZE);
641 
642 	/* Add pbuf to the pmap ptbl bufs list. */
643 	TAILQ_INSERT_TAIL(&pmap->pm_ptbl_list, pbuf, link);
644 
645 	return (ptbl);
646 }
647 
648 /* Free ptbl pages and invalidate pdir entry. */
649 static void
650 ptbl_free(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
651 {
652 	pte_t *ptbl;
653 	vm_paddr_t pa;
654 	vm_offset_t va;
655 	vm_page_t m;
656 	int i;
657 
658 	CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
659 	    (pmap == kernel_pmap), pdir_idx);
660 
661 	KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
662 	    ("ptbl_free: invalid pdir_idx"));
663 
664 	ptbl = pmap->pm_pdir[pdir_idx];
665 
666 	CTR2(KTR_PMAP, "%s: ptbl = %p", __func__, ptbl);
667 
668 	KASSERT((ptbl != NULL), ("ptbl_free: null ptbl"));
669 
670 	/*
671 	 * Invalidate the pdir entry as soon as possible, so that other CPUs
672 	 * don't attempt to look up the page tables we are releasing.
673 	 */
674 	mtx_lock_spin(&tlbivax_mutex);
675 	tlb_miss_lock();
676 
677 	pmap->pm_pdir[pdir_idx] = NULL;
678 
679 	tlb_miss_unlock();
680 	mtx_unlock_spin(&tlbivax_mutex);
681 
682 	for (i = 0; i < PTBL_PAGES; i++) {
683 		va = ((vm_offset_t)ptbl + (i * PAGE_SIZE));
684 		pa = pte_vatopa(mmu, kernel_pmap, va);
685 		m = PHYS_TO_VM_PAGE(pa);
686 		vm_page_free_zero(m);
687 		atomic_subtract_int(&vm_cnt.v_wire_count, 1);
688 		mmu_booke_kremove(mmu, va);
689 	}
690 
691 	ptbl_free_pmap_ptbl(pmap, ptbl);
692 }
693 
694 /*
695  * Decrement ptbl pages hold count and attempt to free ptbl pages.
696  * Called when removing pte entry from ptbl.
697  *
698  * Return 1 if ptbl pages were freed.
699  */
700 static int
701 ptbl_unhold(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
702 {
703 	pte_t *ptbl;
704 	vm_paddr_t pa;
705 	vm_page_t m;
706 	int i;
707 
708 	CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
709 	    (pmap == kernel_pmap), pdir_idx);
710 
711 	KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
712 	    ("ptbl_unhold: invalid pdir_idx"));
713 	KASSERT((pmap != kernel_pmap),
714 	    ("ptbl_unhold: unholding kernel ptbl!"));
715 
716 	ptbl = pmap->pm_pdir[pdir_idx];
717 
718 	//debugf("ptbl_unhold: ptbl = 0x%08x\n", (u_int32_t)ptbl);
719 	KASSERT(((vm_offset_t)ptbl >= VM_MIN_KERNEL_ADDRESS),
720 	    ("ptbl_unhold: non kva ptbl"));
721 
722 	/* decrement hold count */
723 	for (i = 0; i < PTBL_PAGES; i++) {
724 		pa = pte_vatopa(mmu, kernel_pmap,
725 		    (vm_offset_t)ptbl + (i * PAGE_SIZE));
726 		m = PHYS_TO_VM_PAGE(pa);
727 		m->wire_count--;
728 	}
729 
730 	/*
731 	 * Free ptbl pages if there are no pte etries in this ptbl.
732 	 * wire_count has the same value for all ptbl pages, so check the last
733 	 * page.
734 	 */
735 	if (m->wire_count == 0) {
736 		ptbl_free(mmu, pmap, pdir_idx);
737 
738 		//debugf("ptbl_unhold: e (freed ptbl)\n");
739 		return (1);
740 	}
741 
742 	return (0);
743 }
744 
745 /*
746  * Increment hold count for ptbl pages. This routine is used when a new pte
747  * entry is being inserted into the ptbl.
748  */
749 static void
750 ptbl_hold(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
751 {
752 	vm_paddr_t pa;
753 	pte_t *ptbl;
754 	vm_page_t m;
755 	int i;
756 
757 	CTR3(KTR_PMAP, "%s: pmap = %p pdir_idx = %d", __func__, pmap,
758 	    pdir_idx);
759 
760 	KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
761 	    ("ptbl_hold: invalid pdir_idx"));
762 	KASSERT((pmap != kernel_pmap),
763 	    ("ptbl_hold: holding kernel ptbl!"));
764 
765 	ptbl = pmap->pm_pdir[pdir_idx];
766 
767 	KASSERT((ptbl != NULL), ("ptbl_hold: null ptbl"));
768 
769 	for (i = 0; i < PTBL_PAGES; i++) {
770 		pa = pte_vatopa(mmu, kernel_pmap,
771 		    (vm_offset_t)ptbl + (i * PAGE_SIZE));
772 		m = PHYS_TO_VM_PAGE(pa);
773 		m->wire_count++;
774 	}
775 }
776 
777 /* Allocate pv_entry structure. */
778 pv_entry_t
779 pv_alloc(void)
780 {
781 	pv_entry_t pv;
782 
783 	pv_entry_count++;
784 	if (pv_entry_count > pv_entry_high_water)
785 		pagedaemon_wakeup();
786 	pv = uma_zalloc(pvzone, M_NOWAIT);
787 
788 	return (pv);
789 }
790 
791 /* Free pv_entry structure. */
792 static __inline void
793 pv_free(pv_entry_t pve)
794 {
795 
796 	pv_entry_count--;
797 	uma_zfree(pvzone, pve);
798 }
799 
800 
801 /* Allocate and initialize pv_entry structure. */
802 static void
803 pv_insert(pmap_t pmap, vm_offset_t va, vm_page_t m)
804 {
805 	pv_entry_t pve;
806 
807 	//int su = (pmap == kernel_pmap);
808 	//debugf("pv_insert: s (su = %d pmap = 0x%08x va = 0x%08x m = 0x%08x)\n", su,
809 	//	(u_int32_t)pmap, va, (u_int32_t)m);
810 
811 	pve = pv_alloc();
812 	if (pve == NULL)
813 		panic("pv_insert: no pv entries!");
814 
815 	pve->pv_pmap = pmap;
816 	pve->pv_va = va;
817 
818 	/* add to pv_list */
819 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
820 	rw_assert(&pvh_global_lock, RA_WLOCKED);
821 
822 	TAILQ_INSERT_TAIL(&m->md.pv_list, pve, pv_link);
823 
824 	//debugf("pv_insert: e\n");
825 }
826 
827 /* Destroy pv entry. */
828 static void
829 pv_remove(pmap_t pmap, vm_offset_t va, vm_page_t m)
830 {
831 	pv_entry_t pve;
832 
833 	//int su = (pmap == kernel_pmap);
834 	//debugf("pv_remove: s (su = %d pmap = 0x%08x va = 0x%08x)\n", su, (u_int32_t)pmap, va);
835 
836 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
837 	rw_assert(&pvh_global_lock, RA_WLOCKED);
838 
839 	/* find pv entry */
840 	TAILQ_FOREACH(pve, &m->md.pv_list, pv_link) {
841 		if ((pmap == pve->pv_pmap) && (va == pve->pv_va)) {
842 			/* remove from pv_list */
843 			TAILQ_REMOVE(&m->md.pv_list, pve, pv_link);
844 			if (TAILQ_EMPTY(&m->md.pv_list))
845 				vm_page_aflag_clear(m, PGA_WRITEABLE);
846 
847 			/* free pv entry struct */
848 			pv_free(pve);
849 			break;
850 		}
851 	}
852 
853 	//debugf("pv_remove: e\n");
854 }
855 
856 /*
857  * Clean pte entry, try to free page table page if requested.
858  *
859  * Return 1 if ptbl pages were freed, otherwise return 0.
860  */
861 static int
862 pte_remove(mmu_t mmu, pmap_t pmap, vm_offset_t va, uint8_t flags)
863 {
864 	unsigned int pdir_idx = PDIR_IDX(va);
865 	unsigned int ptbl_idx = PTBL_IDX(va);
866 	vm_page_t m;
867 	pte_t *ptbl;
868 	pte_t *pte;
869 
870 	//int su = (pmap == kernel_pmap);
871 	//debugf("pte_remove: s (su = %d pmap = 0x%08x va = 0x%08x flags = %d)\n",
872 	//		su, (u_int32_t)pmap, va, flags);
873 
874 	ptbl = pmap->pm_pdir[pdir_idx];
875 	KASSERT(ptbl, ("pte_remove: null ptbl"));
876 
877 	pte = &ptbl[ptbl_idx];
878 
879 	if (pte == NULL || !PTE_ISVALID(pte))
880 		return (0);
881 
882 	if (PTE_ISWIRED(pte))
883 		pmap->pm_stats.wired_count--;
884 
885 	/* Handle managed entry. */
886 	if (PTE_ISMANAGED(pte)) {
887 		/* Get vm_page_t for mapped pte. */
888 		m = PHYS_TO_VM_PAGE(PTE_PA(pte));
889 
890 		if (PTE_ISMODIFIED(pte))
891 			vm_page_dirty(m);
892 
893 		if (PTE_ISREFERENCED(pte))
894 			vm_page_aflag_set(m, PGA_REFERENCED);
895 
896 		pv_remove(pmap, va, m);
897 	}
898 
899 	mtx_lock_spin(&tlbivax_mutex);
900 	tlb_miss_lock();
901 
902 	tlb0_flush_entry(va);
903 	*pte = 0;
904 
905 	tlb_miss_unlock();
906 	mtx_unlock_spin(&tlbivax_mutex);
907 
908 	pmap->pm_stats.resident_count--;
909 
910 	if (flags & PTBL_UNHOLD) {
911 		//debugf("pte_remove: e (unhold)\n");
912 		return (ptbl_unhold(mmu, pmap, pdir_idx));
913 	}
914 
915 	//debugf("pte_remove: e\n");
916 	return (0);
917 }
918 
919 /*
920  * Insert PTE for a given page and virtual address.
921  */
922 static int
923 pte_enter(mmu_t mmu, pmap_t pmap, vm_page_t m, vm_offset_t va, uint32_t flags,
924     boolean_t nosleep)
925 {
926 	unsigned int pdir_idx = PDIR_IDX(va);
927 	unsigned int ptbl_idx = PTBL_IDX(va);
928 	pte_t *ptbl, *pte;
929 
930 	CTR4(KTR_PMAP, "%s: su = %d pmap = %p va = %p", __func__,
931 	    pmap == kernel_pmap, pmap, va);
932 
933 	/* Get the page table pointer. */
934 	ptbl = pmap->pm_pdir[pdir_idx];
935 
936 	if (ptbl == NULL) {
937 		/* Allocate page table pages. */
938 		ptbl = ptbl_alloc(mmu, pmap, pdir_idx, nosleep);
939 		if (ptbl == NULL) {
940 			KASSERT(nosleep, ("nosleep and NULL ptbl"));
941 			return (ENOMEM);
942 		}
943 	} else {
944 		/*
945 		 * Check if there is valid mapping for requested
946 		 * va, if there is, remove it.
947 		 */
948 		pte = &pmap->pm_pdir[pdir_idx][ptbl_idx];
949 		if (PTE_ISVALID(pte)) {
950 			pte_remove(mmu, pmap, va, PTBL_HOLD);
951 		} else {
952 			/*
953 			 * pte is not used, increment hold count
954 			 * for ptbl pages.
955 			 */
956 			if (pmap != kernel_pmap)
957 				ptbl_hold(mmu, pmap, pdir_idx);
958 		}
959 	}
960 
961 	/*
962 	 * Insert pv_entry into pv_list for mapped page if part of managed
963 	 * memory.
964 	 */
965 	if ((m->oflags & VPO_UNMANAGED) == 0) {
966 		flags |= PTE_MANAGED;
967 
968 		/* Create and insert pv entry. */
969 		pv_insert(pmap, va, m);
970 	}
971 
972 	pmap->pm_stats.resident_count++;
973 
974 	mtx_lock_spin(&tlbivax_mutex);
975 	tlb_miss_lock();
976 
977 	tlb0_flush_entry(va);
978 	if (pmap->pm_pdir[pdir_idx] == NULL) {
979 		/*
980 		 * If we just allocated a new page table, hook it in
981 		 * the pdir.
982 		 */
983 		pmap->pm_pdir[pdir_idx] = ptbl;
984 	}
985 	pte = &(pmap->pm_pdir[pdir_idx][ptbl_idx]);
986 	*pte = PTE_RPN_FROM_PA(VM_PAGE_TO_PHYS(m));
987 	*pte |= (PTE_VALID | flags | PTE_PS_4KB); /* 4KB pages only */
988 
989 	tlb_miss_unlock();
990 	mtx_unlock_spin(&tlbivax_mutex);
991 	return (0);
992 }
993 
994 /* Return the pa for the given pmap/va. */
995 static vm_paddr_t
996 pte_vatopa(mmu_t mmu, pmap_t pmap, vm_offset_t va)
997 {
998 	vm_paddr_t pa = 0;
999 	pte_t *pte;
1000 
1001 	pte = pte_find(mmu, pmap, va);
1002 	if ((pte != NULL) && PTE_ISVALID(pte))
1003 		pa = (PTE_PA(pte) | (va & PTE_PA_MASK));
1004 	return (pa);
1005 }
1006 
1007 /* Get a pointer to a PTE in a page table. */
1008 static pte_t *
1009 pte_find(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1010 {
1011 	unsigned int pdir_idx = PDIR_IDX(va);
1012 	unsigned int ptbl_idx = PTBL_IDX(va);
1013 
1014 	KASSERT((pmap != NULL), ("pte_find: invalid pmap"));
1015 
1016 	if (pmap->pm_pdir[pdir_idx])
1017 		return (&(pmap->pm_pdir[pdir_idx][ptbl_idx]));
1018 
1019 	return (NULL);
1020 }
1021 
1022 /* Set up kernel page tables. */
1023 static void
1024 kernel_pte_alloc(vm_offset_t data_end, vm_offset_t addr, vm_offset_t pdir)
1025 {
1026 	int		i;
1027 	vm_offset_t	va;
1028 	pte_t		*pte;
1029 
1030 	/* Initialize kernel pdir */
1031 	for (i = 0; i < kernel_ptbls; i++)
1032 		kernel_pmap->pm_pdir[kptbl_min + i] =
1033 		    (pte_t *)(pdir + (i * PAGE_SIZE * PTBL_PAGES));
1034 
1035 	/*
1036 	 * Fill in PTEs covering kernel code and data. They are not required
1037 	 * for address translation, as this area is covered by static TLB1
1038 	 * entries, but for pte_vatopa() to work correctly with kernel area
1039 	 * addresses.
1040 	 */
1041 	for (va = addr; va < data_end; va += PAGE_SIZE) {
1042 		pte = &(kernel_pmap->pm_pdir[PDIR_IDX(va)][PTBL_IDX(va)]);
1043 		*pte = PTE_RPN_FROM_PA(kernload + (va - kernstart));
1044 		*pte |= PTE_M | PTE_SR | PTE_SW | PTE_SX | PTE_WIRED |
1045 		    PTE_VALID | PTE_PS_4KB;
1046 	}
1047 }
1048 
1049 /**************************************************************************/
1050 /* PMAP related */
1051 /**************************************************************************/
1052 
1053 /*
1054  * This is called during booke_init, before the system is really initialized.
1055  */
1056 static void
1057 mmu_booke_bootstrap(mmu_t mmu, vm_offset_t start, vm_offset_t kernelend)
1058 {
1059 	vm_paddr_t phys_kernelend;
1060 	struct mem_region *mp, *mp1;
1061 	int cnt, i, j;
1062 	vm_paddr_t s, e, sz;
1063 	vm_paddr_t physsz, hwphyssz;
1064 	u_int phys_avail_count;
1065 	vm_size_t kstack0_sz;
1066 	vm_offset_t kernel_pdir, kstack0;
1067 	vm_paddr_t kstack0_phys;
1068 	void *dpcpu;
1069 
1070 	debugf("mmu_booke_bootstrap: entered\n");
1071 
1072 	/* Set interesting system properties */
1073 	hw_direct_map = 0;
1074 	elf32_nxstack = 1;
1075 
1076 	/* Initialize invalidation mutex */
1077 	mtx_init(&tlbivax_mutex, "tlbivax", NULL, MTX_SPIN);
1078 
1079 	/* Read TLB0 size and associativity. */
1080 	tlb0_get_tlbconf();
1081 
1082 	/*
1083 	 * Align kernel start and end address (kernel image).
1084 	 * Note that kernel end does not necessarily relate to kernsize.
1085 	 * kernsize is the size of the kernel that is actually mapped.
1086 	 */
1087 	kernstart = trunc_page(start);
1088 	data_start = round_page(kernelend);
1089 	data_end = data_start;
1090 
1091 	/*
1092 	 * Addresses of preloaded modules (like file systems) use
1093 	 * physical addresses. Make sure we relocate those into
1094 	 * virtual addresses.
1095 	 */
1096 	preload_addr_relocate = kernstart - kernload;
1097 
1098 	/* Allocate the dynamic per-cpu area. */
1099 	dpcpu = (void *)data_end;
1100 	data_end += DPCPU_SIZE;
1101 
1102 	/* Allocate space for the message buffer. */
1103 	msgbufp = (struct msgbuf *)data_end;
1104 	data_end += msgbufsize;
1105 	debugf(" msgbufp at 0x%08x end = 0x%08x\n", (uint32_t)msgbufp,
1106 	    data_end);
1107 
1108 	data_end = round_page(data_end);
1109 
1110 	/* Allocate space for ptbl_bufs. */
1111 	ptbl_bufs = (struct ptbl_buf *)data_end;
1112 	data_end += sizeof(struct ptbl_buf) * PTBL_BUFS;
1113 	debugf(" ptbl_bufs at 0x%08x end = 0x%08x\n", (uint32_t)ptbl_bufs,
1114 	    data_end);
1115 
1116 	data_end = round_page(data_end);
1117 
1118 	/* Allocate PTE tables for kernel KVA. */
1119 	kernel_pdir = data_end;
1120 	kernel_ptbls = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS +
1121 	    PDIR_SIZE - 1) / PDIR_SIZE;
1122 	data_end += kernel_ptbls * PTBL_PAGES * PAGE_SIZE;
1123 	debugf(" kernel ptbls: %d\n", kernel_ptbls);
1124 	debugf(" kernel pdir at 0x%08x end = 0x%08x\n", kernel_pdir, data_end);
1125 
1126 	debugf(" data_end: 0x%08x\n", data_end);
1127 	if (data_end - kernstart > kernsize) {
1128 		kernsize += tlb1_mapin_region(kernstart + kernsize,
1129 		    kernload + kernsize, (data_end - kernstart) - kernsize);
1130 	}
1131 	data_end = kernstart + kernsize;
1132 	debugf(" updated data_end: 0x%08x\n", data_end);
1133 
1134 	/*
1135 	 * Clear the structures - note we can only do it safely after the
1136 	 * possible additional TLB1 translations are in place (above) so that
1137 	 * all range up to the currently calculated 'data_end' is covered.
1138 	 */
1139 	dpcpu_init(dpcpu, 0);
1140 	memset((void *)ptbl_bufs, 0, sizeof(struct ptbl_buf) * PTBL_SIZE);
1141 	memset((void *)kernel_pdir, 0, kernel_ptbls * PTBL_PAGES * PAGE_SIZE);
1142 
1143 	/*******************************************************/
1144 	/* Set the start and end of kva. */
1145 	/*******************************************************/
1146 	virtual_avail = round_page(data_end);
1147 	virtual_end = VM_MAX_KERNEL_ADDRESS;
1148 
1149 	/* Allocate KVA space for page zero/copy operations. */
1150 	zero_page_va = virtual_avail;
1151 	virtual_avail += PAGE_SIZE;
1152 	zero_page_idle_va = virtual_avail;
1153 	virtual_avail += PAGE_SIZE;
1154 	copy_page_src_va = virtual_avail;
1155 	virtual_avail += PAGE_SIZE;
1156 	copy_page_dst_va = virtual_avail;
1157 	virtual_avail += PAGE_SIZE;
1158 	debugf("zero_page_va = 0x%08x\n", zero_page_va);
1159 	debugf("zero_page_idle_va = 0x%08x\n", zero_page_idle_va);
1160 	debugf("copy_page_src_va = 0x%08x\n", copy_page_src_va);
1161 	debugf("copy_page_dst_va = 0x%08x\n", copy_page_dst_va);
1162 
1163 	/* Initialize page zero/copy mutexes. */
1164 	mtx_init(&zero_page_mutex, "mmu_booke_zero_page", NULL, MTX_DEF);
1165 	mtx_init(&copy_page_mutex, "mmu_booke_copy_page", NULL, MTX_DEF);
1166 
1167 	/* Allocate KVA space for ptbl bufs. */
1168 	ptbl_buf_pool_vabase = virtual_avail;
1169 	virtual_avail += PTBL_BUFS * PTBL_PAGES * PAGE_SIZE;
1170 	debugf("ptbl_buf_pool_vabase = 0x%08x end = 0x%08x\n",
1171 	    ptbl_buf_pool_vabase, virtual_avail);
1172 
1173 	/* Calculate corresponding physical addresses for the kernel region. */
1174 	phys_kernelend = kernload + kernsize;
1175 	debugf("kernel image and allocated data:\n");
1176 	debugf(" kernload    = 0x%09llx\n", (uint64_t)kernload);
1177 	debugf(" kernstart   = 0x%08x\n", kernstart);
1178 	debugf(" kernsize    = 0x%08x\n", kernsize);
1179 
1180 	if (sizeof(phys_avail) / sizeof(phys_avail[0]) < availmem_regions_sz)
1181 		panic("mmu_booke_bootstrap: phys_avail too small");
1182 
1183 	/*
1184 	 * Remove kernel physical address range from avail regions list. Page
1185 	 * align all regions.  Non-page aligned memory isn't very interesting
1186 	 * to us.  Also, sort the entries for ascending addresses.
1187 	 */
1188 
1189 	/* Retrieve phys/avail mem regions */
1190 	mem_regions(&physmem_regions, &physmem_regions_sz,
1191 	    &availmem_regions, &availmem_regions_sz);
1192 	sz = 0;
1193 	cnt = availmem_regions_sz;
1194 	debugf("processing avail regions:\n");
1195 	for (mp = availmem_regions; mp->mr_size; mp++) {
1196 		s = mp->mr_start;
1197 		e = mp->mr_start + mp->mr_size;
1198 		debugf(" %09jx-%09jx -> ", (uintmax_t)s, (uintmax_t)e);
1199 		/* Check whether this region holds all of the kernel. */
1200 		if (s < kernload && e > phys_kernelend) {
1201 			availmem_regions[cnt].mr_start = phys_kernelend;
1202 			availmem_regions[cnt++].mr_size = e - phys_kernelend;
1203 			e = kernload;
1204 		}
1205 		/* Look whether this regions starts within the kernel. */
1206 		if (s >= kernload && s < phys_kernelend) {
1207 			if (e <= phys_kernelend)
1208 				goto empty;
1209 			s = phys_kernelend;
1210 		}
1211 		/* Now look whether this region ends within the kernel. */
1212 		if (e > kernload && e <= phys_kernelend) {
1213 			if (s >= kernload)
1214 				goto empty;
1215 			e = kernload;
1216 		}
1217 		/* Now page align the start and size of the region. */
1218 		s = round_page(s);
1219 		e = trunc_page(e);
1220 		if (e < s)
1221 			e = s;
1222 		sz = e - s;
1223 		debugf("%09jx-%09jx = %jx\n",
1224 		    (uintmax_t)s, (uintmax_t)e, (uintmax_t)sz);
1225 
1226 		/* Check whether some memory is left here. */
1227 		if (sz == 0) {
1228 		empty:
1229 			memmove(mp, mp + 1,
1230 			    (cnt - (mp - availmem_regions)) * sizeof(*mp));
1231 			cnt--;
1232 			mp--;
1233 			continue;
1234 		}
1235 
1236 		/* Do an insertion sort. */
1237 		for (mp1 = availmem_regions; mp1 < mp; mp1++)
1238 			if (s < mp1->mr_start)
1239 				break;
1240 		if (mp1 < mp) {
1241 			memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
1242 			mp1->mr_start = s;
1243 			mp1->mr_size = sz;
1244 		} else {
1245 			mp->mr_start = s;
1246 			mp->mr_size = sz;
1247 		}
1248 	}
1249 	availmem_regions_sz = cnt;
1250 
1251 	/*******************************************************/
1252 	/* Steal physical memory for kernel stack from the end */
1253 	/* of the first avail region                           */
1254 	/*******************************************************/
1255 	kstack0_sz = kstack_pages * PAGE_SIZE;
1256 	kstack0_phys = availmem_regions[0].mr_start +
1257 	    availmem_regions[0].mr_size;
1258 	kstack0_phys -= kstack0_sz;
1259 	availmem_regions[0].mr_size -= kstack0_sz;
1260 
1261 	/*******************************************************/
1262 	/* Fill in phys_avail table, based on availmem_regions */
1263 	/*******************************************************/
1264 	phys_avail_count = 0;
1265 	physsz = 0;
1266 	hwphyssz = 0;
1267 	TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
1268 
1269 	debugf("fill in phys_avail:\n");
1270 	for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
1271 
1272 		debugf(" region: 0x%jx - 0x%jx (0x%jx)\n",
1273 		    (uintmax_t)availmem_regions[i].mr_start,
1274 		    (uintmax_t)availmem_regions[i].mr_start +
1275 		        availmem_regions[i].mr_size,
1276 		    (uintmax_t)availmem_regions[i].mr_size);
1277 
1278 		if (hwphyssz != 0 &&
1279 		    (physsz + availmem_regions[i].mr_size) >= hwphyssz) {
1280 			debugf(" hw.physmem adjust\n");
1281 			if (physsz < hwphyssz) {
1282 				phys_avail[j] = availmem_regions[i].mr_start;
1283 				phys_avail[j + 1] =
1284 				    availmem_regions[i].mr_start +
1285 				    hwphyssz - physsz;
1286 				physsz = hwphyssz;
1287 				phys_avail_count++;
1288 			}
1289 			break;
1290 		}
1291 
1292 		phys_avail[j] = availmem_regions[i].mr_start;
1293 		phys_avail[j + 1] = availmem_regions[i].mr_start +
1294 		    availmem_regions[i].mr_size;
1295 		phys_avail_count++;
1296 		physsz += availmem_regions[i].mr_size;
1297 	}
1298 	physmem = btoc(physsz);
1299 
1300 	/* Calculate the last available physical address. */
1301 	for (i = 0; phys_avail[i + 2] != 0; i += 2)
1302 		;
1303 	Maxmem = powerpc_btop(phys_avail[i + 1]);
1304 
1305 	debugf("Maxmem = 0x%08lx\n", Maxmem);
1306 	debugf("phys_avail_count = %d\n", phys_avail_count);
1307 	debugf("physsz = 0x%09jx physmem = %jd (0x%09jx)\n",
1308 	    (uintmax_t)physsz, (uintmax_t)physmem, (uintmax_t)physmem);
1309 
1310 	/*******************************************************/
1311 	/* Initialize (statically allocated) kernel pmap. */
1312 	/*******************************************************/
1313 	PMAP_LOCK_INIT(kernel_pmap);
1314 	kptbl_min = VM_MIN_KERNEL_ADDRESS / PDIR_SIZE;
1315 
1316 	debugf("kernel_pmap = 0x%08x\n", (uint32_t)kernel_pmap);
1317 	debugf("kptbl_min = %d, kernel_ptbls = %d\n", kptbl_min, kernel_ptbls);
1318 	debugf("kernel pdir range: 0x%08x - 0x%08x\n",
1319 	    kptbl_min * PDIR_SIZE, (kptbl_min + kernel_ptbls) * PDIR_SIZE - 1);
1320 
1321 	kernel_pte_alloc(data_end, kernstart, kernel_pdir);
1322 	for (i = 0; i < MAXCPU; i++) {
1323 		kernel_pmap->pm_tid[i] = TID_KERNEL;
1324 
1325 		/* Initialize each CPU's tidbusy entry 0 with kernel_pmap */
1326 		tidbusy[i][TID_KERNEL] = kernel_pmap;
1327 	}
1328 
1329 	/* Mark kernel_pmap active on all CPUs */
1330 	CPU_FILL(&kernel_pmap->pm_active);
1331 
1332  	/*
1333 	 * Initialize the global pv list lock.
1334 	 */
1335 	rw_init(&pvh_global_lock, "pmap pv global");
1336 
1337 	/*******************************************************/
1338 	/* Final setup */
1339 	/*******************************************************/
1340 
1341 	/* Enter kstack0 into kernel map, provide guard page */
1342 	kstack0 = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
1343 	thread0.td_kstack = kstack0;
1344 	thread0.td_kstack_pages = kstack_pages;
1345 
1346 	debugf("kstack_sz = 0x%08x\n", kstack0_sz);
1347 	debugf("kstack0_phys at 0x%09llx - 0x%09llx\n",
1348 	    kstack0_phys, kstack0_phys + kstack0_sz);
1349 	debugf("kstack0 at 0x%08x - 0x%08x\n", kstack0, kstack0 + kstack0_sz);
1350 
1351 	virtual_avail += KSTACK_GUARD_PAGES * PAGE_SIZE + kstack0_sz;
1352 	for (i = 0; i < kstack_pages; i++) {
1353 		mmu_booke_kenter(mmu, kstack0, kstack0_phys);
1354 		kstack0 += PAGE_SIZE;
1355 		kstack0_phys += PAGE_SIZE;
1356 	}
1357 
1358 	pmap_bootstrapped = 1;
1359 
1360 	debugf("virtual_avail = %08x\n", virtual_avail);
1361 	debugf("virtual_end   = %08x\n", virtual_end);
1362 
1363 	debugf("mmu_booke_bootstrap: exit\n");
1364 }
1365 
1366 #ifdef SMP
1367 void
1368 pmap_bootstrap_ap(volatile uint32_t *trcp __unused)
1369 {
1370 	int i;
1371 
1372 	/*
1373 	 * Finish TLB1 configuration: the BSP already set up its TLB1 and we
1374 	 * have the snapshot of its contents in the s/w tlb1[] table, so use
1375 	 * these values directly to (re)program AP's TLB1 hardware.
1376 	 */
1377 	for (i = bp_ntlb1s; i < tlb1_idx; i++) {
1378 		/* Skip invalid entries */
1379 		if (!(tlb1[i].mas1 & MAS1_VALID))
1380 			continue;
1381 
1382 		tlb1_write_entry(i);
1383 	}
1384 
1385 	set_mas4_defaults();
1386 }
1387 #endif
1388 
1389 static void
1390 booke_pmap_init_qpages(void)
1391 {
1392 	struct pcpu *pc;
1393 	int i;
1394 
1395 	CPU_FOREACH(i) {
1396 		pc = pcpu_find(i);
1397 		pc->pc_qmap_addr = kva_alloc(PAGE_SIZE);
1398 		if (pc->pc_qmap_addr == 0)
1399 			panic("pmap_init_qpages: unable to allocate KVA");
1400 	}
1401 }
1402 
1403 SYSINIT(qpages_init, SI_SUB_CPU, SI_ORDER_ANY, booke_pmap_init_qpages, NULL);
1404 
1405 /*
1406  * Get the physical page address for the given pmap/virtual address.
1407  */
1408 static vm_paddr_t
1409 mmu_booke_extract(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1410 {
1411 	vm_paddr_t pa;
1412 
1413 	PMAP_LOCK(pmap);
1414 	pa = pte_vatopa(mmu, pmap, va);
1415 	PMAP_UNLOCK(pmap);
1416 
1417 	return (pa);
1418 }
1419 
1420 /*
1421  * Extract the physical page address associated with the given
1422  * kernel virtual address.
1423  */
1424 static vm_paddr_t
1425 mmu_booke_kextract(mmu_t mmu, vm_offset_t va)
1426 {
1427 	int i;
1428 
1429 	/* Check TLB1 mappings */
1430 	for (i = 0; i < tlb1_idx; i++) {
1431 		if (!(tlb1[i].mas1 & MAS1_VALID))
1432 			continue;
1433 		if (va >= tlb1[i].virt && va < tlb1[i].virt + tlb1[i].size)
1434 			return (tlb1[i].phys + (va - tlb1[i].virt));
1435 	}
1436 
1437 	return (pte_vatopa(mmu, kernel_pmap, va));
1438 }
1439 
1440 /*
1441  * Initialize the pmap module.
1442  * Called by vm_init, to initialize any structures that the pmap
1443  * system needs to map virtual memory.
1444  */
1445 static void
1446 mmu_booke_init(mmu_t mmu)
1447 {
1448 	int shpgperproc = PMAP_SHPGPERPROC;
1449 
1450 	/*
1451 	 * Initialize the address space (zone) for the pv entries.  Set a
1452 	 * high water mark so that the system can recover from excessive
1453 	 * numbers of pv entries.
1454 	 */
1455 	pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
1456 	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1457 
1458 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1459 	pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1460 
1461 	TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1462 	pv_entry_high_water = 9 * (pv_entry_max / 10);
1463 
1464 	uma_zone_reserve_kva(pvzone, pv_entry_max);
1465 
1466 	/* Pre-fill pvzone with initial number of pv entries. */
1467 	uma_prealloc(pvzone, PV_ENTRY_ZONE_MIN);
1468 
1469 	/* Initialize ptbl allocation. */
1470 	ptbl_init();
1471 }
1472 
1473 /*
1474  * Map a list of wired pages into kernel virtual address space.  This is
1475  * intended for temporary mappings which do not need page modification or
1476  * references recorded.  Existing mappings in the region are overwritten.
1477  */
1478 static void
1479 mmu_booke_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
1480 {
1481 	vm_offset_t va;
1482 
1483 	va = sva;
1484 	while (count-- > 0) {
1485 		mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
1486 		va += PAGE_SIZE;
1487 		m++;
1488 	}
1489 }
1490 
1491 /*
1492  * Remove page mappings from kernel virtual address space.  Intended for
1493  * temporary mappings entered by mmu_booke_qenter.
1494  */
1495 static void
1496 mmu_booke_qremove(mmu_t mmu, vm_offset_t sva, int count)
1497 {
1498 	vm_offset_t va;
1499 
1500 	va = sva;
1501 	while (count-- > 0) {
1502 		mmu_booke_kremove(mmu, va);
1503 		va += PAGE_SIZE;
1504 	}
1505 }
1506 
1507 /*
1508  * Map a wired page into kernel virtual address space.
1509  */
1510 static void
1511 mmu_booke_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa)
1512 {
1513 
1514 	mmu_booke_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1515 }
1516 
1517 static void
1518 mmu_booke_kenter_attr(mmu_t mmu, vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
1519 {
1520 	uint32_t flags;
1521 	pte_t *pte;
1522 
1523 	KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1524 	    (va <= VM_MAX_KERNEL_ADDRESS)), ("mmu_booke_kenter: invalid va"));
1525 
1526 	flags = PTE_SR | PTE_SW | PTE_SX | PTE_WIRED | PTE_VALID;
1527 	flags |= tlb_calc_wimg(pa, ma) << PTE_MAS2_SHIFT;
1528 	flags |= PTE_PS_4KB;
1529 
1530 	pte = pte_find(mmu, kernel_pmap, va);
1531 
1532 	mtx_lock_spin(&tlbivax_mutex);
1533 	tlb_miss_lock();
1534 
1535 	if (PTE_ISVALID(pte)) {
1536 
1537 		CTR1(KTR_PMAP, "%s: replacing entry!", __func__);
1538 
1539 		/* Flush entry from TLB0 */
1540 		tlb0_flush_entry(va);
1541 	}
1542 
1543 	*pte = PTE_RPN_FROM_PA(pa) | flags;
1544 
1545 	//debugf("mmu_booke_kenter: pdir_idx = %d ptbl_idx = %d va=0x%08x "
1546 	//		"pa=0x%08x rpn=0x%08x flags=0x%08x\n",
1547 	//		pdir_idx, ptbl_idx, va, pa, pte->rpn, pte->flags);
1548 
1549 	/* Flush the real memory from the instruction cache. */
1550 	if ((flags & (PTE_I | PTE_G)) == 0)
1551 		__syncicache((void *)va, PAGE_SIZE);
1552 
1553 	tlb_miss_unlock();
1554 	mtx_unlock_spin(&tlbivax_mutex);
1555 }
1556 
1557 /*
1558  * Remove a page from kernel page table.
1559  */
1560 static void
1561 mmu_booke_kremove(mmu_t mmu, vm_offset_t va)
1562 {
1563 	pte_t *pte;
1564 
1565 	CTR2(KTR_PMAP,"%s: s (va = 0x%08x)\n", __func__, va);
1566 
1567 	KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1568 	    (va <= VM_MAX_KERNEL_ADDRESS)),
1569 	    ("mmu_booke_kremove: invalid va"));
1570 
1571 	pte = pte_find(mmu, kernel_pmap, va);
1572 
1573 	if (!PTE_ISVALID(pte)) {
1574 
1575 		CTR1(KTR_PMAP, "%s: invalid pte", __func__);
1576 
1577 		return;
1578 	}
1579 
1580 	mtx_lock_spin(&tlbivax_mutex);
1581 	tlb_miss_lock();
1582 
1583 	/* Invalidate entry in TLB0, update PTE. */
1584 	tlb0_flush_entry(va);
1585 	*pte = 0;
1586 
1587 	tlb_miss_unlock();
1588 	mtx_unlock_spin(&tlbivax_mutex);
1589 }
1590 
1591 /*
1592  * Initialize pmap associated with process 0.
1593  */
1594 static void
1595 mmu_booke_pinit0(mmu_t mmu, pmap_t pmap)
1596 {
1597 
1598 	PMAP_LOCK_INIT(pmap);
1599 	mmu_booke_pinit(mmu, pmap);
1600 	PCPU_SET(curpmap, pmap);
1601 }
1602 
1603 /*
1604  * Initialize a preallocated and zeroed pmap structure,
1605  * such as one in a vmspace structure.
1606  */
1607 static void
1608 mmu_booke_pinit(mmu_t mmu, pmap_t pmap)
1609 {
1610 	int i;
1611 
1612 	CTR4(KTR_PMAP, "%s: pmap = %p, proc %d '%s'", __func__, pmap,
1613 	    curthread->td_proc->p_pid, curthread->td_proc->p_comm);
1614 
1615 	KASSERT((pmap != kernel_pmap), ("pmap_pinit: initializing kernel_pmap"));
1616 
1617 	for (i = 0; i < MAXCPU; i++)
1618 		pmap->pm_tid[i] = TID_NONE;
1619 	CPU_ZERO(&kernel_pmap->pm_active);
1620 	bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1621 	bzero(&pmap->pm_pdir, sizeof(pte_t *) * PDIR_NENTRIES);
1622 	TAILQ_INIT(&pmap->pm_ptbl_list);
1623 }
1624 
1625 /*
1626  * Release any resources held by the given physical map.
1627  * Called when a pmap initialized by mmu_booke_pinit is being released.
1628  * Should only be called if the map contains no valid mappings.
1629  */
1630 static void
1631 mmu_booke_release(mmu_t mmu, pmap_t pmap)
1632 {
1633 
1634 	KASSERT(pmap->pm_stats.resident_count == 0,
1635 	    ("pmap_release: pmap resident count %ld != 0",
1636 	    pmap->pm_stats.resident_count));
1637 }
1638 
1639 /*
1640  * Insert the given physical page at the specified virtual address in the
1641  * target physical map with the protection requested. If specified the page
1642  * will be wired down.
1643  */
1644 static int
1645 mmu_booke_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1646     vm_prot_t prot, u_int flags, int8_t psind)
1647 {
1648 	int error;
1649 
1650 	rw_wlock(&pvh_global_lock);
1651 	PMAP_LOCK(pmap);
1652 	error = mmu_booke_enter_locked(mmu, pmap, va, m, prot, flags, psind);
1653 	rw_wunlock(&pvh_global_lock);
1654 	PMAP_UNLOCK(pmap);
1655 	return (error);
1656 }
1657 
1658 static int
1659 mmu_booke_enter_locked(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1660     vm_prot_t prot, u_int pmap_flags, int8_t psind __unused)
1661 {
1662 	pte_t *pte;
1663 	vm_paddr_t pa;
1664 	uint32_t flags;
1665 	int error, su, sync;
1666 
1667 	pa = VM_PAGE_TO_PHYS(m);
1668 	su = (pmap == kernel_pmap);
1669 	sync = 0;
1670 
1671 	//debugf("mmu_booke_enter_locked: s (pmap=0x%08x su=%d tid=%d m=0x%08x va=0x%08x "
1672 	//		"pa=0x%08x prot=0x%08x flags=%#x)\n",
1673 	//		(u_int32_t)pmap, su, pmap->pm_tid,
1674 	//		(u_int32_t)m, va, pa, prot, flags);
1675 
1676 	if (su) {
1677 		KASSERT(((va >= virtual_avail) &&
1678 		    (va <= VM_MAX_KERNEL_ADDRESS)),
1679 		    ("mmu_booke_enter_locked: kernel pmap, non kernel va"));
1680 	} else {
1681 		KASSERT((va <= VM_MAXUSER_ADDRESS),
1682 		    ("mmu_booke_enter_locked: user pmap, non user va"));
1683 	}
1684 	if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
1685 		VM_OBJECT_ASSERT_LOCKED(m->object);
1686 
1687 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1688 
1689 	/*
1690 	 * If there is an existing mapping, and the physical address has not
1691 	 * changed, must be protection or wiring change.
1692 	 */
1693 	if (((pte = pte_find(mmu, pmap, va)) != NULL) &&
1694 	    (PTE_ISVALID(pte)) && (PTE_PA(pte) == pa)) {
1695 
1696 		/*
1697 		 * Before actually updating pte->flags we calculate and
1698 		 * prepare its new value in a helper var.
1699 		 */
1700 		flags = *pte;
1701 		flags &= ~(PTE_UW | PTE_UX | PTE_SW | PTE_SX | PTE_MODIFIED);
1702 
1703 		/* Wiring change, just update stats. */
1704 		if ((pmap_flags & PMAP_ENTER_WIRED) != 0) {
1705 			if (!PTE_ISWIRED(pte)) {
1706 				flags |= PTE_WIRED;
1707 				pmap->pm_stats.wired_count++;
1708 			}
1709 		} else {
1710 			if (PTE_ISWIRED(pte)) {
1711 				flags &= ~PTE_WIRED;
1712 				pmap->pm_stats.wired_count--;
1713 			}
1714 		}
1715 
1716 		if (prot & VM_PROT_WRITE) {
1717 			/* Add write permissions. */
1718 			flags |= PTE_SW;
1719 			if (!su)
1720 				flags |= PTE_UW;
1721 
1722 			if ((flags & PTE_MANAGED) != 0)
1723 				vm_page_aflag_set(m, PGA_WRITEABLE);
1724 		} else {
1725 			/* Handle modified pages, sense modify status. */
1726 
1727 			/*
1728 			 * The PTE_MODIFIED flag could be set by underlying
1729 			 * TLB misses since we last read it (above), possibly
1730 			 * other CPUs could update it so we check in the PTE
1731 			 * directly rather than rely on that saved local flags
1732 			 * copy.
1733 			 */
1734 			if (PTE_ISMODIFIED(pte))
1735 				vm_page_dirty(m);
1736 		}
1737 
1738 		if (prot & VM_PROT_EXECUTE) {
1739 			flags |= PTE_SX;
1740 			if (!su)
1741 				flags |= PTE_UX;
1742 
1743 			/*
1744 			 * Check existing flags for execute permissions: if we
1745 			 * are turning execute permissions on, icache should
1746 			 * be flushed.
1747 			 */
1748 			if ((*pte & (PTE_UX | PTE_SX)) == 0)
1749 				sync++;
1750 		}
1751 
1752 		flags &= ~PTE_REFERENCED;
1753 
1754 		/*
1755 		 * The new flags value is all calculated -- only now actually
1756 		 * update the PTE.
1757 		 */
1758 		mtx_lock_spin(&tlbivax_mutex);
1759 		tlb_miss_lock();
1760 
1761 		tlb0_flush_entry(va);
1762 		*pte = flags;
1763 
1764 		tlb_miss_unlock();
1765 		mtx_unlock_spin(&tlbivax_mutex);
1766 
1767 	} else {
1768 		/*
1769 		 * If there is an existing mapping, but it's for a different
1770 		 * physical address, pte_enter() will delete the old mapping.
1771 		 */
1772 		//if ((pte != NULL) && PTE_ISVALID(pte))
1773 		//	debugf("mmu_booke_enter_locked: replace\n");
1774 		//else
1775 		//	debugf("mmu_booke_enter_locked: new\n");
1776 
1777 		/* Now set up the flags and install the new mapping. */
1778 		flags = (PTE_SR | PTE_VALID);
1779 		flags |= PTE_M;
1780 
1781 		if (!su)
1782 			flags |= PTE_UR;
1783 
1784 		if (prot & VM_PROT_WRITE) {
1785 			flags |= PTE_SW;
1786 			if (!su)
1787 				flags |= PTE_UW;
1788 
1789 			if ((m->oflags & VPO_UNMANAGED) == 0)
1790 				vm_page_aflag_set(m, PGA_WRITEABLE);
1791 		}
1792 
1793 		if (prot & VM_PROT_EXECUTE) {
1794 			flags |= PTE_SX;
1795 			if (!su)
1796 				flags |= PTE_UX;
1797 		}
1798 
1799 		/* If its wired update stats. */
1800 		if ((pmap_flags & PMAP_ENTER_WIRED) != 0)
1801 			flags |= PTE_WIRED;
1802 
1803 		error = pte_enter(mmu, pmap, m, va, flags,
1804 		    (pmap_flags & PMAP_ENTER_NOSLEEP) != 0);
1805 		if (error != 0)
1806 			return (KERN_RESOURCE_SHORTAGE);
1807 
1808 		if ((flags & PMAP_ENTER_WIRED) != 0)
1809 			pmap->pm_stats.wired_count++;
1810 
1811 		/* Flush the real memory from the instruction cache. */
1812 		if (prot & VM_PROT_EXECUTE)
1813 			sync++;
1814 	}
1815 
1816 	if (sync && (su || pmap == PCPU_GET(curpmap))) {
1817 		__syncicache((void *)va, PAGE_SIZE);
1818 		sync = 0;
1819 	}
1820 
1821 	return (KERN_SUCCESS);
1822 }
1823 
1824 /*
1825  * Maps a sequence of resident pages belonging to the same object.
1826  * The sequence begins with the given page m_start.  This page is
1827  * mapped at the given virtual address start.  Each subsequent page is
1828  * mapped at a virtual address that is offset from start by the same
1829  * amount as the page is offset from m_start within the object.  The
1830  * last page in the sequence is the page with the largest offset from
1831  * m_start that can be mapped at a virtual address less than the given
1832  * virtual address end.  Not every virtual page between start and end
1833  * is mapped; only those for which a resident page exists with the
1834  * corresponding offset from m_start are mapped.
1835  */
1836 static void
1837 mmu_booke_enter_object(mmu_t mmu, pmap_t pmap, vm_offset_t start,
1838     vm_offset_t end, vm_page_t m_start, vm_prot_t prot)
1839 {
1840 	vm_page_t m;
1841 	vm_pindex_t diff, psize;
1842 
1843 	VM_OBJECT_ASSERT_LOCKED(m_start->object);
1844 
1845 	psize = atop(end - start);
1846 	m = m_start;
1847 	rw_wlock(&pvh_global_lock);
1848 	PMAP_LOCK(pmap);
1849 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1850 		mmu_booke_enter_locked(mmu, pmap, start + ptoa(diff), m,
1851 		    prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1852 		    PMAP_ENTER_NOSLEEP, 0);
1853 		m = TAILQ_NEXT(m, listq);
1854 	}
1855 	rw_wunlock(&pvh_global_lock);
1856 	PMAP_UNLOCK(pmap);
1857 }
1858 
1859 static void
1860 mmu_booke_enter_quick(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1861     vm_prot_t prot)
1862 {
1863 
1864 	rw_wlock(&pvh_global_lock);
1865 	PMAP_LOCK(pmap);
1866 	mmu_booke_enter_locked(mmu, pmap, va, m,
1867 	    prot & (VM_PROT_READ | VM_PROT_EXECUTE), PMAP_ENTER_NOSLEEP,
1868 	    0);
1869 	rw_wunlock(&pvh_global_lock);
1870 	PMAP_UNLOCK(pmap);
1871 }
1872 
1873 /*
1874  * Remove the given range of addresses from the specified map.
1875  *
1876  * It is assumed that the start and end are properly rounded to the page size.
1877  */
1878 static void
1879 mmu_booke_remove(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_offset_t endva)
1880 {
1881 	pte_t *pte;
1882 	uint8_t hold_flag;
1883 
1884 	int su = (pmap == kernel_pmap);
1885 
1886 	//debugf("mmu_booke_remove: s (su = %d pmap=0x%08x tid=%d va=0x%08x endva=0x%08x)\n",
1887 	//		su, (u_int32_t)pmap, pmap->pm_tid, va, endva);
1888 
1889 	if (su) {
1890 		KASSERT(((va >= virtual_avail) &&
1891 		    (va <= VM_MAX_KERNEL_ADDRESS)),
1892 		    ("mmu_booke_remove: kernel pmap, non kernel va"));
1893 	} else {
1894 		KASSERT((va <= VM_MAXUSER_ADDRESS),
1895 		    ("mmu_booke_remove: user pmap, non user va"));
1896 	}
1897 
1898 	if (PMAP_REMOVE_DONE(pmap)) {
1899 		//debugf("mmu_booke_remove: e (empty)\n");
1900 		return;
1901 	}
1902 
1903 	hold_flag = PTBL_HOLD_FLAG(pmap);
1904 	//debugf("mmu_booke_remove: hold_flag = %d\n", hold_flag);
1905 
1906 	rw_wlock(&pvh_global_lock);
1907 	PMAP_LOCK(pmap);
1908 	for (; va < endva; va += PAGE_SIZE) {
1909 		pte = pte_find(mmu, pmap, va);
1910 		if ((pte != NULL) && PTE_ISVALID(pte))
1911 			pte_remove(mmu, pmap, va, hold_flag);
1912 	}
1913 	PMAP_UNLOCK(pmap);
1914 	rw_wunlock(&pvh_global_lock);
1915 
1916 	//debugf("mmu_booke_remove: e\n");
1917 }
1918 
1919 /*
1920  * Remove physical page from all pmaps in which it resides.
1921  */
1922 static void
1923 mmu_booke_remove_all(mmu_t mmu, vm_page_t m)
1924 {
1925 	pv_entry_t pv, pvn;
1926 	uint8_t hold_flag;
1927 
1928 	rw_wlock(&pvh_global_lock);
1929 	for (pv = TAILQ_FIRST(&m->md.pv_list); pv != NULL; pv = pvn) {
1930 		pvn = TAILQ_NEXT(pv, pv_link);
1931 
1932 		PMAP_LOCK(pv->pv_pmap);
1933 		hold_flag = PTBL_HOLD_FLAG(pv->pv_pmap);
1934 		pte_remove(mmu, pv->pv_pmap, pv->pv_va, hold_flag);
1935 		PMAP_UNLOCK(pv->pv_pmap);
1936 	}
1937 	vm_page_aflag_clear(m, PGA_WRITEABLE);
1938 	rw_wunlock(&pvh_global_lock);
1939 }
1940 
1941 /*
1942  * Map a range of physical addresses into kernel virtual address space.
1943  */
1944 static vm_offset_t
1945 mmu_booke_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start,
1946     vm_paddr_t pa_end, int prot)
1947 {
1948 	vm_offset_t sva = *virt;
1949 	vm_offset_t va = sva;
1950 
1951 	//debugf("mmu_booke_map: s (sva = 0x%08x pa_start = 0x%08x pa_end = 0x%08x)\n",
1952 	//		sva, pa_start, pa_end);
1953 
1954 	while (pa_start < pa_end) {
1955 		mmu_booke_kenter(mmu, va, pa_start);
1956 		va += PAGE_SIZE;
1957 		pa_start += PAGE_SIZE;
1958 	}
1959 	*virt = va;
1960 
1961 	//debugf("mmu_booke_map: e (va = 0x%08x)\n", va);
1962 	return (sva);
1963 }
1964 
1965 /*
1966  * The pmap must be activated before it's address space can be accessed in any
1967  * way.
1968  */
1969 static void
1970 mmu_booke_activate(mmu_t mmu, struct thread *td)
1971 {
1972 	pmap_t pmap;
1973 	u_int cpuid;
1974 
1975 	pmap = &td->td_proc->p_vmspace->vm_pmap;
1976 
1977 	CTR5(KTR_PMAP, "%s: s (td = %p, proc = '%s', id = %d, pmap = 0x%08x)",
1978 	    __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1979 
1980 	KASSERT((pmap != kernel_pmap), ("mmu_booke_activate: kernel_pmap!"));
1981 
1982 	sched_pin();
1983 
1984 	cpuid = PCPU_GET(cpuid);
1985 	CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
1986 	PCPU_SET(curpmap, pmap);
1987 
1988 	if (pmap->pm_tid[cpuid] == TID_NONE)
1989 		tid_alloc(pmap);
1990 
1991 	/* Load PID0 register with pmap tid value. */
1992 	mtspr(SPR_PID0, pmap->pm_tid[cpuid]);
1993 	__asm __volatile("isync");
1994 
1995 	mtspr(SPR_DBCR0, td->td_pcb->pcb_cpu.booke.dbcr0);
1996 
1997 	sched_unpin();
1998 
1999 	CTR3(KTR_PMAP, "%s: e (tid = %d for '%s')", __func__,
2000 	    pmap->pm_tid[PCPU_GET(cpuid)], td->td_proc->p_comm);
2001 }
2002 
2003 /*
2004  * Deactivate the specified process's address space.
2005  */
2006 static void
2007 mmu_booke_deactivate(mmu_t mmu, struct thread *td)
2008 {
2009 	pmap_t pmap;
2010 
2011 	pmap = &td->td_proc->p_vmspace->vm_pmap;
2012 
2013 	CTR5(KTR_PMAP, "%s: td=%p, proc = '%s', id = %d, pmap = 0x%08x",
2014 	    __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
2015 
2016 	td->td_pcb->pcb_cpu.booke.dbcr0 = mfspr(SPR_DBCR0);
2017 
2018 	CPU_CLR_ATOMIC(PCPU_GET(cpuid), &pmap->pm_active);
2019 	PCPU_SET(curpmap, NULL);
2020 }
2021 
2022 /*
2023  * Copy the range specified by src_addr/len
2024  * from the source map to the range dst_addr/len
2025  * in the destination map.
2026  *
2027  * This routine is only advisory and need not do anything.
2028  */
2029 static void
2030 mmu_booke_copy(mmu_t mmu, pmap_t dst_pmap, pmap_t src_pmap,
2031     vm_offset_t dst_addr, vm_size_t len, vm_offset_t src_addr)
2032 {
2033 
2034 }
2035 
2036 /*
2037  * Set the physical protection on the specified range of this map as requested.
2038  */
2039 static void
2040 mmu_booke_protect(mmu_t mmu, pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
2041     vm_prot_t prot)
2042 {
2043 	vm_offset_t va;
2044 	vm_page_t m;
2045 	pte_t *pte;
2046 
2047 	if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2048 		mmu_booke_remove(mmu, pmap, sva, eva);
2049 		return;
2050 	}
2051 
2052 	if (prot & VM_PROT_WRITE)
2053 		return;
2054 
2055 	PMAP_LOCK(pmap);
2056 	for (va = sva; va < eva; va += PAGE_SIZE) {
2057 		if ((pte = pte_find(mmu, pmap, va)) != NULL) {
2058 			if (PTE_ISVALID(pte)) {
2059 				m = PHYS_TO_VM_PAGE(PTE_PA(pte));
2060 
2061 				mtx_lock_spin(&tlbivax_mutex);
2062 				tlb_miss_lock();
2063 
2064 				/* Handle modified pages. */
2065 				if (PTE_ISMODIFIED(pte) && PTE_ISMANAGED(pte))
2066 					vm_page_dirty(m);
2067 
2068 				tlb0_flush_entry(va);
2069 				*pte &= ~(PTE_UW | PTE_SW | PTE_MODIFIED);
2070 
2071 				tlb_miss_unlock();
2072 				mtx_unlock_spin(&tlbivax_mutex);
2073 			}
2074 		}
2075 	}
2076 	PMAP_UNLOCK(pmap);
2077 }
2078 
2079 /*
2080  * Clear the write and modified bits in each of the given page's mappings.
2081  */
2082 static void
2083 mmu_booke_remove_write(mmu_t mmu, vm_page_t m)
2084 {
2085 	pv_entry_t pv;
2086 	pte_t *pte;
2087 
2088 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2089 	    ("mmu_booke_remove_write: page %p is not managed", m));
2090 
2091 	/*
2092 	 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2093 	 * set by another thread while the object is locked.  Thus,
2094 	 * if PGA_WRITEABLE is clear, no page table entries need updating.
2095 	 */
2096 	VM_OBJECT_ASSERT_WLOCKED(m->object);
2097 	if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2098 		return;
2099 	rw_wlock(&pvh_global_lock);
2100 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2101 		PMAP_LOCK(pv->pv_pmap);
2102 		if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
2103 			if (PTE_ISVALID(pte)) {
2104 				m = PHYS_TO_VM_PAGE(PTE_PA(pte));
2105 
2106 				mtx_lock_spin(&tlbivax_mutex);
2107 				tlb_miss_lock();
2108 
2109 				/* Handle modified pages. */
2110 				if (PTE_ISMODIFIED(pte))
2111 					vm_page_dirty(m);
2112 
2113 				/* Flush mapping from TLB0. */
2114 				*pte &= ~(PTE_UW | PTE_SW | PTE_MODIFIED);
2115 
2116 				tlb_miss_unlock();
2117 				mtx_unlock_spin(&tlbivax_mutex);
2118 			}
2119 		}
2120 		PMAP_UNLOCK(pv->pv_pmap);
2121 	}
2122 	vm_page_aflag_clear(m, PGA_WRITEABLE);
2123 	rw_wunlock(&pvh_global_lock);
2124 }
2125 
2126 static void
2127 mmu_booke_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
2128 {
2129 	pte_t *pte;
2130 	pmap_t pmap;
2131 	vm_page_t m;
2132 	vm_offset_t addr;
2133 	vm_paddr_t pa = 0;
2134 	int active, valid;
2135 
2136 	va = trunc_page(va);
2137 	sz = round_page(sz);
2138 
2139 	rw_wlock(&pvh_global_lock);
2140 	pmap = PCPU_GET(curpmap);
2141 	active = (pm == kernel_pmap || pm == pmap) ? 1 : 0;
2142 	while (sz > 0) {
2143 		PMAP_LOCK(pm);
2144 		pte = pte_find(mmu, pm, va);
2145 		valid = (pte != NULL && PTE_ISVALID(pte)) ? 1 : 0;
2146 		if (valid)
2147 			pa = PTE_PA(pte);
2148 		PMAP_UNLOCK(pm);
2149 		if (valid) {
2150 			if (!active) {
2151 				/* Create a mapping in the active pmap. */
2152 				addr = 0;
2153 				m = PHYS_TO_VM_PAGE(pa);
2154 				PMAP_LOCK(pmap);
2155 				pte_enter(mmu, pmap, m, addr,
2156 				    PTE_SR | PTE_VALID | PTE_UR, FALSE);
2157 				__syncicache((void *)addr, PAGE_SIZE);
2158 				pte_remove(mmu, pmap, addr, PTBL_UNHOLD);
2159 				PMAP_UNLOCK(pmap);
2160 			} else
2161 				__syncicache((void *)va, PAGE_SIZE);
2162 		}
2163 		va += PAGE_SIZE;
2164 		sz -= PAGE_SIZE;
2165 	}
2166 	rw_wunlock(&pvh_global_lock);
2167 }
2168 
2169 /*
2170  * Atomically extract and hold the physical page with the given
2171  * pmap and virtual address pair if that mapping permits the given
2172  * protection.
2173  */
2174 static vm_page_t
2175 mmu_booke_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va,
2176     vm_prot_t prot)
2177 {
2178 	pte_t *pte;
2179 	vm_page_t m;
2180 	uint32_t pte_wbit;
2181 	vm_paddr_t pa;
2182 
2183 	m = NULL;
2184 	pa = 0;
2185 	PMAP_LOCK(pmap);
2186 retry:
2187 	pte = pte_find(mmu, pmap, va);
2188 	if ((pte != NULL) && PTE_ISVALID(pte)) {
2189 		if (pmap == kernel_pmap)
2190 			pte_wbit = PTE_SW;
2191 		else
2192 			pte_wbit = PTE_UW;
2193 
2194 		if ((*pte & pte_wbit) || ((prot & VM_PROT_WRITE) == 0)) {
2195 			if (vm_page_pa_tryrelock(pmap, PTE_PA(pte), &pa))
2196 				goto retry;
2197 			m = PHYS_TO_VM_PAGE(PTE_PA(pte));
2198 			vm_page_hold(m);
2199 		}
2200 	}
2201 
2202 	PA_UNLOCK_COND(pa);
2203 	PMAP_UNLOCK(pmap);
2204 	return (m);
2205 }
2206 
2207 /*
2208  * Initialize a vm_page's machine-dependent fields.
2209  */
2210 static void
2211 mmu_booke_page_init(mmu_t mmu, vm_page_t m)
2212 {
2213 
2214 	TAILQ_INIT(&m->md.pv_list);
2215 }
2216 
2217 /*
2218  * mmu_booke_zero_page_area zeros the specified hardware page by
2219  * mapping it into virtual memory and using bzero to clear
2220  * its contents.
2221  *
2222  * off and size must reside within a single page.
2223  */
2224 static void
2225 mmu_booke_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
2226 {
2227 	vm_offset_t va;
2228 
2229 	/* XXX KASSERT off and size are within a single page? */
2230 
2231 	mtx_lock(&zero_page_mutex);
2232 	va = zero_page_va;
2233 
2234 	mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2235 	bzero((caddr_t)va + off, size);
2236 	mmu_booke_kremove(mmu, va);
2237 
2238 	mtx_unlock(&zero_page_mutex);
2239 }
2240 
2241 /*
2242  * mmu_booke_zero_page zeros the specified hardware page.
2243  */
2244 static void
2245 mmu_booke_zero_page(mmu_t mmu, vm_page_t m)
2246 {
2247 	vm_offset_t off, va;
2248 
2249 	mtx_lock(&zero_page_mutex);
2250 	va = zero_page_va;
2251 
2252 	mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2253 	for (off = 0; off < PAGE_SIZE; off += cacheline_size)
2254 		__asm __volatile("dcbz 0,%0" :: "r"(va + off));
2255 	mmu_booke_kremove(mmu, va);
2256 
2257 	mtx_unlock(&zero_page_mutex);
2258 }
2259 
2260 /*
2261  * mmu_booke_copy_page copies the specified (machine independent) page by
2262  * mapping the page into virtual memory and using memcopy to copy the page,
2263  * one machine dependent page at a time.
2264  */
2265 static void
2266 mmu_booke_copy_page(mmu_t mmu, vm_page_t sm, vm_page_t dm)
2267 {
2268 	vm_offset_t sva, dva;
2269 
2270 	sva = copy_page_src_va;
2271 	dva = copy_page_dst_va;
2272 
2273 	mtx_lock(&copy_page_mutex);
2274 	mmu_booke_kenter(mmu, sva, VM_PAGE_TO_PHYS(sm));
2275 	mmu_booke_kenter(mmu, dva, VM_PAGE_TO_PHYS(dm));
2276 	memcpy((caddr_t)dva, (caddr_t)sva, PAGE_SIZE);
2277 	mmu_booke_kremove(mmu, dva);
2278 	mmu_booke_kremove(mmu, sva);
2279 	mtx_unlock(&copy_page_mutex);
2280 }
2281 
2282 static inline void
2283 mmu_booke_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset,
2284     vm_page_t *mb, vm_offset_t b_offset, int xfersize)
2285 {
2286 	void *a_cp, *b_cp;
2287 	vm_offset_t a_pg_offset, b_pg_offset;
2288 	int cnt;
2289 
2290 	mtx_lock(&copy_page_mutex);
2291 	while (xfersize > 0) {
2292 		a_pg_offset = a_offset & PAGE_MASK;
2293 		cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
2294 		mmu_booke_kenter(mmu, copy_page_src_va,
2295 		    VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]));
2296 		a_cp = (char *)copy_page_src_va + a_pg_offset;
2297 		b_pg_offset = b_offset & PAGE_MASK;
2298 		cnt = min(cnt, PAGE_SIZE - b_pg_offset);
2299 		mmu_booke_kenter(mmu, copy_page_dst_va,
2300 		    VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]));
2301 		b_cp = (char *)copy_page_dst_va + b_pg_offset;
2302 		bcopy(a_cp, b_cp, cnt);
2303 		mmu_booke_kremove(mmu, copy_page_dst_va);
2304 		mmu_booke_kremove(mmu, copy_page_src_va);
2305 		a_offset += cnt;
2306 		b_offset += cnt;
2307 		xfersize -= cnt;
2308 	}
2309 	mtx_unlock(&copy_page_mutex);
2310 }
2311 
2312 /*
2313  * mmu_booke_zero_page_idle zeros the specified hardware page by mapping it
2314  * into virtual memory and using bzero to clear its contents. This is intended
2315  * to be called from the vm_pagezero process only and outside of Giant. No
2316  * lock is required.
2317  */
2318 static void
2319 mmu_booke_zero_page_idle(mmu_t mmu, vm_page_t m)
2320 {
2321 	vm_offset_t va;
2322 
2323 	va = zero_page_idle_va;
2324 	mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2325 	bzero((caddr_t)va, PAGE_SIZE);
2326 	mmu_booke_kremove(mmu, va);
2327 }
2328 
2329 static vm_offset_t
2330 mmu_booke_quick_enter_page(mmu_t mmu, vm_page_t m)
2331 {
2332 	vm_paddr_t paddr;
2333 	vm_offset_t qaddr;
2334 	uint32_t flags;
2335 	pte_t *pte;
2336 
2337 	paddr = VM_PAGE_TO_PHYS(m);
2338 
2339 	flags = PTE_SR | PTE_SW | PTE_SX | PTE_WIRED | PTE_VALID;
2340 	flags |= tlb_calc_wimg(paddr, pmap_page_get_memattr(m)) << PTE_MAS2_SHIFT;
2341 	flags |= PTE_PS_4KB;
2342 
2343 	critical_enter();
2344 	qaddr = PCPU_GET(qmap_addr);
2345 
2346 	pte = pte_find(mmu, kernel_pmap, qaddr);
2347 
2348 	KASSERT(*pte == 0, ("mmu_booke_quick_enter_page: PTE busy"));
2349 
2350 	/*
2351 	 * XXX: tlbivax is broadcast to other cores, but qaddr should
2352  	 * not be present in other TLBs.  Is there a better instruction
2353 	 * sequence to use? Or just forget it & use mmu_booke_kenter()...
2354 	 */
2355 	__asm __volatile("tlbivax 0, %0" :: "r"(qaddr & MAS2_EPN_MASK));
2356 	__asm __volatile("isync; msync");
2357 
2358 	*pte = PTE_RPN_FROM_PA(paddr) | flags;
2359 
2360 	/* Flush the real memory from the instruction cache. */
2361 	if ((flags & (PTE_I | PTE_G)) == 0)
2362 		__syncicache((void *)qaddr, PAGE_SIZE);
2363 
2364 	return (qaddr);
2365 }
2366 
2367 static void
2368 mmu_booke_quick_remove_page(mmu_t mmu, vm_offset_t addr)
2369 {
2370 	pte_t *pte;
2371 
2372 	pte = pte_find(mmu, kernel_pmap, addr);
2373 
2374 	KASSERT(PCPU_GET(qmap_addr) == addr,
2375 	    ("mmu_booke_quick_remove_page: invalid address"));
2376 	KASSERT(*pte != 0,
2377 	    ("mmu_booke_quick_remove_page: PTE not in use"));
2378 
2379 	*pte = 0;
2380 	critical_exit();
2381 }
2382 
2383 /*
2384  * Return whether or not the specified physical page was modified
2385  * in any of physical maps.
2386  */
2387 static boolean_t
2388 mmu_booke_is_modified(mmu_t mmu, vm_page_t m)
2389 {
2390 	pte_t *pte;
2391 	pv_entry_t pv;
2392 	boolean_t rv;
2393 
2394 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2395 	    ("mmu_booke_is_modified: page %p is not managed", m));
2396 	rv = FALSE;
2397 
2398 	/*
2399 	 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2400 	 * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
2401 	 * is clear, no PTEs can be modified.
2402 	 */
2403 	VM_OBJECT_ASSERT_WLOCKED(m->object);
2404 	if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2405 		return (rv);
2406 	rw_wlock(&pvh_global_lock);
2407 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2408 		PMAP_LOCK(pv->pv_pmap);
2409 		if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2410 		    PTE_ISVALID(pte)) {
2411 			if (PTE_ISMODIFIED(pte))
2412 				rv = TRUE;
2413 		}
2414 		PMAP_UNLOCK(pv->pv_pmap);
2415 		if (rv)
2416 			break;
2417 	}
2418 	rw_wunlock(&pvh_global_lock);
2419 	return (rv);
2420 }
2421 
2422 /*
2423  * Return whether or not the specified virtual address is eligible
2424  * for prefault.
2425  */
2426 static boolean_t
2427 mmu_booke_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t addr)
2428 {
2429 
2430 	return (FALSE);
2431 }
2432 
2433 /*
2434  * Return whether or not the specified physical page was referenced
2435  * in any physical maps.
2436  */
2437 static boolean_t
2438 mmu_booke_is_referenced(mmu_t mmu, vm_page_t m)
2439 {
2440 	pte_t *pte;
2441 	pv_entry_t pv;
2442 	boolean_t rv;
2443 
2444 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2445 	    ("mmu_booke_is_referenced: page %p is not managed", m));
2446 	rv = FALSE;
2447 	rw_wlock(&pvh_global_lock);
2448 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2449 		PMAP_LOCK(pv->pv_pmap);
2450 		if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2451 		    PTE_ISVALID(pte)) {
2452 			if (PTE_ISREFERENCED(pte))
2453 				rv = TRUE;
2454 		}
2455 		PMAP_UNLOCK(pv->pv_pmap);
2456 		if (rv)
2457 			break;
2458 	}
2459 	rw_wunlock(&pvh_global_lock);
2460 	return (rv);
2461 }
2462 
2463 /*
2464  * Clear the modify bits on the specified physical page.
2465  */
2466 static void
2467 mmu_booke_clear_modify(mmu_t mmu, vm_page_t m)
2468 {
2469 	pte_t *pte;
2470 	pv_entry_t pv;
2471 
2472 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2473 	    ("mmu_booke_clear_modify: page %p is not managed", m));
2474 	VM_OBJECT_ASSERT_WLOCKED(m->object);
2475 	KASSERT(!vm_page_xbusied(m),
2476 	    ("mmu_booke_clear_modify: page %p is exclusive busied", m));
2477 
2478 	/*
2479 	 * If the page is not PG_AWRITEABLE, then no PTEs can be modified.
2480 	 * If the object containing the page is locked and the page is not
2481 	 * exclusive busied, then PG_AWRITEABLE cannot be concurrently set.
2482 	 */
2483 	if ((m->aflags & PGA_WRITEABLE) == 0)
2484 		return;
2485 	rw_wlock(&pvh_global_lock);
2486 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2487 		PMAP_LOCK(pv->pv_pmap);
2488 		if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2489 		    PTE_ISVALID(pte)) {
2490 			mtx_lock_spin(&tlbivax_mutex);
2491 			tlb_miss_lock();
2492 
2493 			if (*pte & (PTE_SW | PTE_UW | PTE_MODIFIED)) {
2494 				tlb0_flush_entry(pv->pv_va);
2495 				*pte &= ~(PTE_SW | PTE_UW | PTE_MODIFIED |
2496 				    PTE_REFERENCED);
2497 			}
2498 
2499 			tlb_miss_unlock();
2500 			mtx_unlock_spin(&tlbivax_mutex);
2501 		}
2502 		PMAP_UNLOCK(pv->pv_pmap);
2503 	}
2504 	rw_wunlock(&pvh_global_lock);
2505 }
2506 
2507 /*
2508  * Return a count of reference bits for a page, clearing those bits.
2509  * It is not necessary for every reference bit to be cleared, but it
2510  * is necessary that 0 only be returned when there are truly no
2511  * reference bits set.
2512  *
2513  * XXX: The exact number of bits to check and clear is a matter that
2514  * should be tested and standardized at some point in the future for
2515  * optimal aging of shared pages.
2516  */
2517 static int
2518 mmu_booke_ts_referenced(mmu_t mmu, vm_page_t m)
2519 {
2520 	pte_t *pte;
2521 	pv_entry_t pv;
2522 	int count;
2523 
2524 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2525 	    ("mmu_booke_ts_referenced: page %p is not managed", m));
2526 	count = 0;
2527 	rw_wlock(&pvh_global_lock);
2528 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2529 		PMAP_LOCK(pv->pv_pmap);
2530 		if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2531 		    PTE_ISVALID(pte)) {
2532 			if (PTE_ISREFERENCED(pte)) {
2533 				mtx_lock_spin(&tlbivax_mutex);
2534 				tlb_miss_lock();
2535 
2536 				tlb0_flush_entry(pv->pv_va);
2537 				*pte &= ~PTE_REFERENCED;
2538 
2539 				tlb_miss_unlock();
2540 				mtx_unlock_spin(&tlbivax_mutex);
2541 
2542 				if (++count > 4) {
2543 					PMAP_UNLOCK(pv->pv_pmap);
2544 					break;
2545 				}
2546 			}
2547 		}
2548 		PMAP_UNLOCK(pv->pv_pmap);
2549 	}
2550 	rw_wunlock(&pvh_global_lock);
2551 	return (count);
2552 }
2553 
2554 /*
2555  * Clear the wired attribute from the mappings for the specified range of
2556  * addresses in the given pmap.  Every valid mapping within that range must
2557  * have the wired attribute set.  In contrast, invalid mappings cannot have
2558  * the wired attribute set, so they are ignored.
2559  *
2560  * The wired attribute of the page table entry is not a hardware feature, so
2561  * there is no need to invalidate any TLB entries.
2562  */
2563 static void
2564 mmu_booke_unwire(mmu_t mmu, pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2565 {
2566 	vm_offset_t va;
2567 	pte_t *pte;
2568 
2569 	PMAP_LOCK(pmap);
2570 	for (va = sva; va < eva; va += PAGE_SIZE) {
2571 		if ((pte = pte_find(mmu, pmap, va)) != NULL &&
2572 		    PTE_ISVALID(pte)) {
2573 			if (!PTE_ISWIRED(pte))
2574 				panic("mmu_booke_unwire: pte %p isn't wired",
2575 				    pte);
2576 			*pte &= ~PTE_WIRED;
2577 			pmap->pm_stats.wired_count--;
2578 		}
2579 	}
2580 	PMAP_UNLOCK(pmap);
2581 
2582 }
2583 
2584 /*
2585  * Return true if the pmap's pv is one of the first 16 pvs linked to from this
2586  * page.  This count may be changed upwards or downwards in the future; it is
2587  * only necessary that true be returned for a small subset of pmaps for proper
2588  * page aging.
2589  */
2590 static boolean_t
2591 mmu_booke_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
2592 {
2593 	pv_entry_t pv;
2594 	int loops;
2595 	boolean_t rv;
2596 
2597 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2598 	    ("mmu_booke_page_exists_quick: page %p is not managed", m));
2599 	loops = 0;
2600 	rv = FALSE;
2601 	rw_wlock(&pvh_global_lock);
2602 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2603 		if (pv->pv_pmap == pmap) {
2604 			rv = TRUE;
2605 			break;
2606 		}
2607 		if (++loops >= 16)
2608 			break;
2609 	}
2610 	rw_wunlock(&pvh_global_lock);
2611 	return (rv);
2612 }
2613 
2614 /*
2615  * Return the number of managed mappings to the given physical page that are
2616  * wired.
2617  */
2618 static int
2619 mmu_booke_page_wired_mappings(mmu_t mmu, vm_page_t m)
2620 {
2621 	pv_entry_t pv;
2622 	pte_t *pte;
2623 	int count = 0;
2624 
2625 	if ((m->oflags & VPO_UNMANAGED) != 0)
2626 		return (count);
2627 	rw_wlock(&pvh_global_lock);
2628 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2629 		PMAP_LOCK(pv->pv_pmap);
2630 		if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL)
2631 			if (PTE_ISVALID(pte) && PTE_ISWIRED(pte))
2632 				count++;
2633 		PMAP_UNLOCK(pv->pv_pmap);
2634 	}
2635 	rw_wunlock(&pvh_global_lock);
2636 	return (count);
2637 }
2638 
2639 static int
2640 mmu_booke_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2641 {
2642 	int i;
2643 	vm_offset_t va;
2644 
2645 	/*
2646 	 * This currently does not work for entries that
2647 	 * overlap TLB1 entries.
2648 	 */
2649 	for (i = 0; i < tlb1_idx; i ++) {
2650 		if (tlb1_iomapped(i, pa, size, &va) == 0)
2651 			return (0);
2652 	}
2653 
2654 	return (EFAULT);
2655 }
2656 
2657 void
2658 mmu_booke_dumpsys_map(mmu_t mmu, vm_paddr_t pa, size_t sz, void **va)
2659 {
2660 	vm_paddr_t ppa;
2661 	vm_offset_t ofs;
2662 	vm_size_t gran;
2663 
2664 	/* Minidumps are based on virtual memory addresses. */
2665 	if (do_minidump) {
2666 		*va = (void *)(vm_offset_t)pa;
2667 		return;
2668 	}
2669 
2670 	/* Raw physical memory dumps don't have a virtual address. */
2671 	/* We always map a 256MB page at 256M. */
2672 	gran = 256 * 1024 * 1024;
2673 	ppa = pa & ~(gran - 1);
2674 	ofs = pa - ppa;
2675 	*va = (void *)gran;
2676 	tlb1_set_entry((vm_offset_t)va, ppa, gran, _TLB_ENTRY_IO);
2677 
2678 	if (sz > (gran - ofs))
2679 		tlb1_set_entry((vm_offset_t)(va + gran), ppa + gran, gran,
2680 		    _TLB_ENTRY_IO);
2681 }
2682 
2683 void
2684 mmu_booke_dumpsys_unmap(mmu_t mmu, vm_paddr_t pa, size_t sz, void *va)
2685 {
2686 	vm_paddr_t ppa;
2687 	vm_offset_t ofs;
2688 	vm_size_t gran;
2689 
2690 	/* Minidumps are based on virtual memory addresses. */
2691 	/* Nothing to do... */
2692 	if (do_minidump)
2693 		return;
2694 
2695 	/* Raw physical memory dumps don't have a virtual address. */
2696 	tlb1_idx--;
2697 	tlb1[tlb1_idx].mas1 = 0;
2698 	tlb1[tlb1_idx].mas2 = 0;
2699 	tlb1[tlb1_idx].mas3 = 0;
2700 	tlb1_write_entry(tlb1_idx);
2701 
2702 	gran = 256 * 1024 * 1024;
2703 	ppa = pa & ~(gran - 1);
2704 	ofs = pa - ppa;
2705 	if (sz > (gran - ofs)) {
2706 		tlb1_idx--;
2707 		tlb1[tlb1_idx].mas1 = 0;
2708 		tlb1[tlb1_idx].mas2 = 0;
2709 		tlb1[tlb1_idx].mas3 = 0;
2710 		tlb1_write_entry(tlb1_idx);
2711 	}
2712 }
2713 
2714 extern struct dump_pa dump_map[PHYS_AVAIL_SZ + 1];
2715 
2716 void
2717 mmu_booke_scan_init(mmu_t mmu)
2718 {
2719 	vm_offset_t va;
2720 	pte_t *pte;
2721 	int i;
2722 
2723 	if (!do_minidump) {
2724 		/* Initialize phys. segments for dumpsys(). */
2725 		memset(&dump_map, 0, sizeof(dump_map));
2726 		mem_regions(&physmem_regions, &physmem_regions_sz, &availmem_regions,
2727 		    &availmem_regions_sz);
2728 		for (i = 0; i < physmem_regions_sz; i++) {
2729 			dump_map[i].pa_start = physmem_regions[i].mr_start;
2730 			dump_map[i].pa_size = physmem_regions[i].mr_size;
2731 		}
2732 		return;
2733 	}
2734 
2735 	/* Virtual segments for minidumps: */
2736 	memset(&dump_map, 0, sizeof(dump_map));
2737 
2738 	/* 1st: kernel .data and .bss. */
2739 	dump_map[0].pa_start = trunc_page((uintptr_t)_etext);
2740 	dump_map[0].pa_size =
2741 	    round_page((uintptr_t)_end) - dump_map[0].pa_start;
2742 
2743 	/* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2744 	dump_map[1].pa_start = data_start;
2745 	dump_map[1].pa_size = data_end - data_start;
2746 
2747 	/* 3rd: kernel VM. */
2748 	va = dump_map[1].pa_start + dump_map[1].pa_size;
2749 	/* Find start of next chunk (from va). */
2750 	while (va < virtual_end) {
2751 		/* Don't dump the buffer cache. */
2752 		if (va >= kmi.buffer_sva && va < kmi.buffer_eva) {
2753 			va = kmi.buffer_eva;
2754 			continue;
2755 		}
2756 		pte = pte_find(mmu, kernel_pmap, va);
2757 		if (pte != NULL && PTE_ISVALID(pte))
2758 			break;
2759 		va += PAGE_SIZE;
2760 	}
2761 	if (va < virtual_end) {
2762 		dump_map[2].pa_start = va;
2763 		va += PAGE_SIZE;
2764 		/* Find last page in chunk. */
2765 		while (va < virtual_end) {
2766 			/* Don't run into the buffer cache. */
2767 			if (va == kmi.buffer_sva)
2768 				break;
2769 			pte = pte_find(mmu, kernel_pmap, va);
2770 			if (pte == NULL || !PTE_ISVALID(pte))
2771 				break;
2772 			va += PAGE_SIZE;
2773 		}
2774 		dump_map[2].pa_size = va - dump_map[2].pa_start;
2775 	}
2776 }
2777 
2778 /*
2779  * Map a set of physical memory pages into the kernel virtual address space.
2780  * Return a pointer to where it is mapped. This routine is intended to be used
2781  * for mapping device memory, NOT real memory.
2782  */
2783 static void *
2784 mmu_booke_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2785 {
2786 
2787 	return (mmu_booke_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2788 }
2789 
2790 static void *
2791 mmu_booke_mapdev_attr(mmu_t mmu, vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
2792 {
2793 	void *res;
2794 	uintptr_t va;
2795 	vm_size_t sz;
2796 	int i;
2797 
2798 	/*
2799 	 * Check if this is premapped in TLB1. Note: this should probably also
2800 	 * check whether a sequence of TLB1 entries exist that match the
2801 	 * requirement, but now only checks the easy case.
2802 	 */
2803 	if (ma == VM_MEMATTR_DEFAULT) {
2804 		for (i = 0; i < tlb1_idx; i++) {
2805 			if (!(tlb1[i].mas1 & MAS1_VALID))
2806 				continue;
2807 			if (pa >= tlb1[i].phys &&
2808 			    (pa + size) <= (tlb1[i].phys + tlb1[i].size))
2809 				return (void *)(tlb1[i].virt +
2810 				    (vm_offset_t)(pa - tlb1[i].phys));
2811 		}
2812 	}
2813 
2814 	size = roundup(size, PAGE_SIZE);
2815 
2816 	/*
2817 	 * We leave a hole for device direct mapping between the maximum user
2818 	 * address (0x8000000) and the minimum KVA address (0xc0000000). If
2819 	 * devices are in there, just map them 1:1. If not, map them to the
2820 	 * device mapping area about VM_MAX_KERNEL_ADDRESS. These mapped
2821 	 * addresses should be pulled from an allocator, but since we do not
2822 	 * ever free TLB1 entries, it is safe just to increment a counter.
2823 	 * Note that there isn't a lot of address space here (128 MB) and it
2824 	 * is not at all difficult to imagine running out, since that is a 4:1
2825 	 * compression from the 0xc0000000 - 0xf0000000 address space that gets
2826 	 * mapped there.
2827 	 */
2828 	if (pa >= (VM_MAXUSER_ADDRESS + PAGE_SIZE) &&
2829 	    (pa + size - 1) < VM_MIN_KERNEL_ADDRESS)
2830 		va = pa;
2831 	else
2832 		va = atomic_fetchadd_int(&tlb1_map_base, size);
2833 	res = (void *)va;
2834 
2835 	do {
2836 		sz = 1 << (ilog2(size) & ~1);
2837 		if (va % sz != 0) {
2838 			do {
2839 				sz >>= 2;
2840 			} while (va % sz != 0);
2841 		}
2842 		if (bootverbose)
2843 			printf("Wiring VA=%x to PA=%jx (size=%x), "
2844 			    "using TLB1[%d]\n", va, (uintmax_t)pa, sz, tlb1_idx);
2845 		tlb1_set_entry(va, pa, sz, tlb_calc_wimg(pa, ma));
2846 		size -= sz;
2847 		pa += sz;
2848 		va += sz;
2849 	} while (size > 0);
2850 
2851 	return (res);
2852 }
2853 
2854 /*
2855  * 'Unmap' a range mapped by mmu_booke_mapdev().
2856  */
2857 static void
2858 mmu_booke_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
2859 {
2860 #ifdef SUPPORTS_SHRINKING_TLB1
2861 	vm_offset_t base, offset;
2862 
2863 	/*
2864 	 * Unmap only if this is inside kernel virtual space.
2865 	 */
2866 	if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
2867 		base = trunc_page(va);
2868 		offset = va & PAGE_MASK;
2869 		size = roundup(offset + size, PAGE_SIZE);
2870 		kva_free(base, size);
2871 	}
2872 #endif
2873 }
2874 
2875 /*
2876  * mmu_booke_object_init_pt preloads the ptes for a given object into the
2877  * specified pmap. This eliminates the blast of soft faults on process startup
2878  * and immediately after an mmap.
2879  */
2880 static void
2881 mmu_booke_object_init_pt(mmu_t mmu, pmap_t pmap, vm_offset_t addr,
2882     vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2883 {
2884 
2885 	VM_OBJECT_ASSERT_WLOCKED(object);
2886 	KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2887 	    ("mmu_booke_object_init_pt: non-device object"));
2888 }
2889 
2890 /*
2891  * Perform the pmap work for mincore.
2892  */
2893 static int
2894 mmu_booke_mincore(mmu_t mmu, pmap_t pmap, vm_offset_t addr,
2895     vm_paddr_t *locked_pa)
2896 {
2897 
2898 	/* XXX: this should be implemented at some point */
2899 	return (0);
2900 }
2901 
2902 /**************************************************************************/
2903 /* TID handling */
2904 /**************************************************************************/
2905 
2906 /*
2907  * Allocate a TID. If necessary, steal one from someone else.
2908  * The new TID is flushed from the TLB before returning.
2909  */
2910 static tlbtid_t
2911 tid_alloc(pmap_t pmap)
2912 {
2913 	tlbtid_t tid;
2914 	int thiscpu;
2915 
2916 	KASSERT((pmap != kernel_pmap), ("tid_alloc: kernel pmap"));
2917 
2918 	CTR2(KTR_PMAP, "%s: s (pmap = %p)", __func__, pmap);
2919 
2920 	thiscpu = PCPU_GET(cpuid);
2921 
2922 	tid = PCPU_GET(tid_next);
2923 	if (tid > TID_MAX)
2924 		tid = TID_MIN;
2925 	PCPU_SET(tid_next, tid + 1);
2926 
2927 	/* If we are stealing TID then clear the relevant pmap's field */
2928 	if (tidbusy[thiscpu][tid] != NULL) {
2929 
2930 		CTR2(KTR_PMAP, "%s: warning: stealing tid %d", __func__, tid);
2931 
2932 		tidbusy[thiscpu][tid]->pm_tid[thiscpu] = TID_NONE;
2933 
2934 		/* Flush all entries from TLB0 matching this TID. */
2935 		tid_flush(tid);
2936 	}
2937 
2938 	tidbusy[thiscpu][tid] = pmap;
2939 	pmap->pm_tid[thiscpu] = tid;
2940 	__asm __volatile("msync; isync");
2941 
2942 	CTR3(KTR_PMAP, "%s: e (%02d next = %02d)", __func__, tid,
2943 	    PCPU_GET(tid_next));
2944 
2945 	return (tid);
2946 }
2947 
2948 /**************************************************************************/
2949 /* TLB0 handling */
2950 /**************************************************************************/
2951 
2952 static void
2953 tlb_print_entry(int i, uint32_t mas1, uint32_t mas2, uint32_t mas3,
2954     uint32_t mas7)
2955 {
2956 	int as;
2957 	char desc[3];
2958 	tlbtid_t tid;
2959 	vm_size_t size;
2960 	unsigned int tsize;
2961 
2962 	desc[2] = '\0';
2963 	if (mas1 & MAS1_VALID)
2964 		desc[0] = 'V';
2965 	else
2966 		desc[0] = ' ';
2967 
2968 	if (mas1 & MAS1_IPROT)
2969 		desc[1] = 'P';
2970 	else
2971 		desc[1] = ' ';
2972 
2973 	as = (mas1 & MAS1_TS_MASK) ? 1 : 0;
2974 	tid = MAS1_GETTID(mas1);
2975 
2976 	tsize = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
2977 	size = 0;
2978 	if (tsize)
2979 		size = tsize2size(tsize);
2980 
2981 	debugf("%3d: (%s) [AS=%d] "
2982 	    "sz = 0x%08x tsz = %d tid = %d mas1 = 0x%08x "
2983 	    "mas2(va) = 0x%08x mas3(pa) = 0x%08x mas7 = 0x%08x\n",
2984 	    i, desc, as, size, tsize, tid, mas1, mas2, mas3, mas7);
2985 }
2986 
2987 /* Convert TLB0 va and way number to tlb0[] table index. */
2988 static inline unsigned int
2989 tlb0_tableidx(vm_offset_t va, unsigned int way)
2990 {
2991 	unsigned int idx;
2992 
2993 	idx = (way * TLB0_ENTRIES_PER_WAY);
2994 	idx += (va & MAS2_TLB0_ENTRY_IDX_MASK) >> MAS2_TLB0_ENTRY_IDX_SHIFT;
2995 	return (idx);
2996 }
2997 
2998 /*
2999  * Invalidate TLB0 entry.
3000  */
3001 static inline void
3002 tlb0_flush_entry(vm_offset_t va)
3003 {
3004 
3005 	CTR2(KTR_PMAP, "%s: s va=0x%08x", __func__, va);
3006 
3007 	mtx_assert(&tlbivax_mutex, MA_OWNED);
3008 
3009 	__asm __volatile("tlbivax 0, %0" :: "r"(va & MAS2_EPN_MASK));
3010 	__asm __volatile("isync; msync");
3011 	__asm __volatile("tlbsync; msync");
3012 
3013 	CTR1(KTR_PMAP, "%s: e", __func__);
3014 }
3015 
3016 /* Print out contents of the MAS registers for each TLB0 entry */
3017 void
3018 tlb0_print_tlbentries(void)
3019 {
3020 	uint32_t mas0, mas1, mas2, mas3, mas7;
3021 	int entryidx, way, idx;
3022 
3023 	debugf("TLB0 entries:\n");
3024 	for (way = 0; way < TLB0_WAYS; way ++)
3025 		for (entryidx = 0; entryidx < TLB0_ENTRIES_PER_WAY; entryidx++) {
3026 
3027 			mas0 = MAS0_TLBSEL(0) | MAS0_ESEL(way);
3028 			mtspr(SPR_MAS0, mas0);
3029 			__asm __volatile("isync");
3030 
3031 			mas2 = entryidx << MAS2_TLB0_ENTRY_IDX_SHIFT;
3032 			mtspr(SPR_MAS2, mas2);
3033 
3034 			__asm __volatile("isync; tlbre");
3035 
3036 			mas1 = mfspr(SPR_MAS1);
3037 			mas2 = mfspr(SPR_MAS2);
3038 			mas3 = mfspr(SPR_MAS3);
3039 			mas7 = mfspr(SPR_MAS7);
3040 
3041 			idx = tlb0_tableidx(mas2, way);
3042 			tlb_print_entry(idx, mas1, mas2, mas3, mas7);
3043 		}
3044 }
3045 
3046 /**************************************************************************/
3047 /* TLB1 handling */
3048 /**************************************************************************/
3049 
3050 /*
3051  * TLB1 mapping notes:
3052  *
3053  * TLB1[0]	Kernel text and data.
3054  * TLB1[1-15]	Additional kernel text and data mappings (if required), PCI
3055  *		windows, other devices mappings.
3056  */
3057 
3058 /*
3059  * Write given entry to TLB1 hardware.
3060  * Use 32 bit pa, clear 4 high-order bits of RPN (mas7).
3061  */
3062 static void
3063 tlb1_write_entry(unsigned int idx)
3064 {
3065 	uint32_t mas0;
3066 
3067 	//debugf("tlb1_write_entry: s\n");
3068 
3069 	/* Select entry */
3070 	mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(idx);
3071 	//debugf("tlb1_write_entry: mas0 = 0x%08x\n", mas0);
3072 
3073 	mtspr(SPR_MAS0, mas0);
3074 	__asm __volatile("isync");
3075 	mtspr(SPR_MAS1, tlb1[idx].mas1);
3076 	__asm __volatile("isync");
3077 	mtspr(SPR_MAS2, tlb1[idx].mas2);
3078 	__asm __volatile("isync");
3079 	mtspr(SPR_MAS3, tlb1[idx].mas3);
3080 	__asm __volatile("isync");
3081 	switch ((mfpvr() >> 16) & 0xFFFF) {
3082 	case FSL_E500mc:
3083 	case FSL_E5500:
3084 		mtspr(SPR_MAS8, 0);
3085 		__asm __volatile("isync");
3086 		/* FALLTHROUGH */
3087 	case FSL_E500v2:
3088 		mtspr(SPR_MAS7, tlb1[idx].mas7);
3089 		__asm __volatile("isync");
3090 		break;
3091 	default:
3092 		break;
3093 	}
3094 
3095 	__asm __volatile("tlbwe; isync; msync");
3096 
3097 	//debugf("tlb1_write_entry: e\n");
3098 }
3099 
3100 /*
3101  * Return the largest uint value log such that 2^log <= num.
3102  */
3103 static unsigned int
3104 ilog2(unsigned int num)
3105 {
3106 	int lz;
3107 
3108 	__asm ("cntlzw %0, %1" : "=r" (lz) : "r" (num));
3109 	return (31 - lz);
3110 }
3111 
3112 /*
3113  * Convert TLB TSIZE value to mapped region size.
3114  */
3115 static vm_size_t
3116 tsize2size(unsigned int tsize)
3117 {
3118 
3119 	/*
3120 	 * size = 4^tsize KB
3121 	 * size = 4^tsize * 2^10 = 2^(2 * tsize - 10)
3122 	 */
3123 
3124 	return ((1 << (2 * tsize)) * 1024);
3125 }
3126 
3127 /*
3128  * Convert region size (must be power of 4) to TLB TSIZE value.
3129  */
3130 static unsigned int
3131 size2tsize(vm_size_t size)
3132 {
3133 
3134 	return (ilog2(size) / 2 - 5);
3135 }
3136 
3137 /*
3138  * Register permanent kernel mapping in TLB1.
3139  *
3140  * Entries are created starting from index 0 (current free entry is
3141  * kept in tlb1_idx) and are not supposed to be invalidated.
3142  */
3143 static int
3144 tlb1_set_entry(vm_offset_t va, vm_paddr_t pa, vm_size_t size,
3145     uint32_t flags)
3146 {
3147 	uint32_t ts, tid;
3148 	int tsize, index;
3149 
3150 	index = atomic_fetchadd_int(&tlb1_idx, 1);
3151 	if (index >= TLB1_ENTRIES) {
3152 		printf("tlb1_set_entry: TLB1 full!\n");
3153 		return (-1);
3154 	}
3155 
3156 	/* Convert size to TSIZE */
3157 	tsize = size2tsize(size);
3158 
3159 	tid = (TID_KERNEL << MAS1_TID_SHIFT) & MAS1_TID_MASK;
3160 	/* XXX TS is hard coded to 0 for now as we only use single address space */
3161 	ts = (0 << MAS1_TS_SHIFT) & MAS1_TS_MASK;
3162 
3163 	/*
3164 	 * Atomicity is preserved by the atomic increment above since nothing
3165 	 * is ever removed from tlb1.
3166 	 */
3167 
3168 	tlb1[index].phys = pa;
3169 	tlb1[index].virt = va;
3170 	tlb1[index].size = size;
3171 	tlb1[index].mas1 = MAS1_VALID | MAS1_IPROT | ts | tid;
3172 	tlb1[index].mas1 |= ((tsize << MAS1_TSIZE_SHIFT) & MAS1_TSIZE_MASK);
3173 	tlb1[index].mas2 = (va & MAS2_EPN_MASK) | flags;
3174 
3175 	/* Set supervisor RWX permission bits */
3176 	tlb1[index].mas3 = (pa & MAS3_RPN) | MAS3_SR | MAS3_SW | MAS3_SX;
3177 	tlb1[index].mas7 = (pa >> 32) & MAS7_RPN;
3178 
3179 	tlb1_write_entry(index);
3180 
3181 	/*
3182 	 * XXX in general TLB1 updates should be propagated between CPUs,
3183 	 * since current design assumes to have the same TLB1 set-up on all
3184 	 * cores.
3185 	 */
3186 	return (0);
3187 }
3188 
3189 /*
3190  * Map in contiguous RAM region into the TLB1 using maximum of
3191  * KERNEL_REGION_MAX_TLB_ENTRIES entries.
3192  *
3193  * If necessary round up last entry size and return total size
3194  * used by all allocated entries.
3195  */
3196 vm_size_t
3197 tlb1_mapin_region(vm_offset_t va, vm_paddr_t pa, vm_size_t size)
3198 {
3199 	vm_size_t pgs[KERNEL_REGION_MAX_TLB_ENTRIES];
3200 	vm_size_t mapped, pgsz, base, mask;
3201 	int idx, nents;
3202 
3203 	/* Round up to the next 1M */
3204 	size = (size + (1 << 20) - 1) & ~((1 << 20) - 1);
3205 
3206 	mapped = 0;
3207 	idx = 0;
3208 	base = va;
3209 	pgsz = 64*1024*1024;
3210 	while (mapped < size) {
3211 		while (mapped < size && idx < KERNEL_REGION_MAX_TLB_ENTRIES) {
3212 			while (pgsz > (size - mapped))
3213 				pgsz >>= 2;
3214 			pgs[idx++] = pgsz;
3215 			mapped += pgsz;
3216 		}
3217 
3218 		/* We under-map. Correct for this. */
3219 		if (mapped < size) {
3220 			while (pgs[idx - 1] == pgsz) {
3221 				idx--;
3222 				mapped -= pgsz;
3223 			}
3224 			/* XXX We may increase beyond out starting point. */
3225 			pgsz <<= 2;
3226 			pgs[idx++] = pgsz;
3227 			mapped += pgsz;
3228 		}
3229 	}
3230 
3231 	nents = idx;
3232 	mask = pgs[0] - 1;
3233 	/* Align address to the boundary */
3234 	if (va & mask) {
3235 		va = (va + mask) & ~mask;
3236 		pa = (pa + mask) & ~mask;
3237 	}
3238 
3239 	for (idx = 0; idx < nents; idx++) {
3240 		pgsz = pgs[idx];
3241 		debugf("%u: %llx -> %x, size=%x\n", idx, pa, va, pgsz);
3242 		tlb1_set_entry(va, pa, pgsz, _TLB_ENTRY_MEM);
3243 		pa += pgsz;
3244 		va += pgsz;
3245 	}
3246 
3247 	mapped = (va - base);
3248 #ifdef __powerpc64__
3249 	printf("mapped size 0x%016lx (wasted space 0x%16lx)\n",
3250 #else
3251 	printf("mapped size 0x%08x (wasted space 0x%08x)\n",
3252 #endif
3253 	    mapped, mapped - size);
3254 	return (mapped);
3255 }
3256 
3257 /*
3258  * TLB1 initialization routine, to be called after the very first
3259  * assembler level setup done in locore.S.
3260  */
3261 void
3262 tlb1_init()
3263 {
3264 	uint32_t mas0, mas1, mas2, mas3, mas7;
3265 	uint32_t tsz;
3266 	int i;
3267 
3268 	tlb1_idx = 1;
3269 
3270 	tlb1_get_tlbconf();
3271 
3272 	mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(0);
3273 	mtspr(SPR_MAS0, mas0);
3274 	__asm __volatile("isync; tlbre");
3275 
3276 	mas1 = mfspr(SPR_MAS1);
3277 	mas2 = mfspr(SPR_MAS2);
3278 	mas3 = mfspr(SPR_MAS3);
3279 	mas7 = mfspr(SPR_MAS7);
3280 
3281 	tlb1[0].mas1 = mas1;
3282 	tlb1[0].mas2 = mfspr(SPR_MAS2);
3283 	tlb1[0].mas3 = mas3;
3284 	tlb1[0].mas7 = mas7;
3285 	tlb1[0].virt = mas2 & MAS2_EPN_MASK;
3286 	tlb1[0].phys =  ((vm_paddr_t)(mas7 & MAS7_RPN) << 32) |
3287 	    (mas3 & MAS3_RPN);
3288 
3289 	kernload = tlb1[0].phys;
3290 
3291 	tsz = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3292 	tlb1[0].size = (tsz > 0) ? tsize2size(tsz) : 0;
3293 	kernsize += tlb1[0].size;
3294 
3295 #ifdef SMP
3296 	bp_ntlb1s = tlb1_idx;
3297 #endif
3298 
3299 	/* Purge the remaining entries */
3300 	for (i = tlb1_idx; i < TLB1_ENTRIES; i++)
3301 		tlb1_write_entry(i);
3302 
3303 	/* Setup TLB miss defaults */
3304 	set_mas4_defaults();
3305 }
3306 
3307 vm_offset_t
3308 pmap_early_io_map(vm_paddr_t pa, vm_size_t size)
3309 {
3310 	vm_paddr_t pa_base;
3311 	vm_offset_t va, sz;
3312 	int i;
3313 
3314 	KASSERT(!pmap_bootstrapped, ("Do not use after PMAP is up!"));
3315 
3316 	for (i = 0; i < tlb1_idx; i++) {
3317 		if (!(tlb1[i].mas1 & MAS1_VALID))
3318 			continue;
3319 		if (pa >= tlb1[i].phys && (pa + size) <=
3320 		    (tlb1[i].phys + tlb1[i].size))
3321 			return (tlb1[i].virt + (pa - tlb1[i].phys));
3322 	}
3323 
3324 	pa_base = rounddown(pa, PAGE_SIZE);
3325 	size = roundup(size + (pa - pa_base), PAGE_SIZE);
3326 	tlb1_map_base = roundup2(tlb1_map_base, 1 << (ilog2(size) & ~1));
3327 	va = tlb1_map_base + (pa - pa_base);
3328 
3329 	do {
3330 		sz = 1 << (ilog2(size) & ~1);
3331 		tlb1_set_entry(tlb1_map_base, pa_base, sz, _TLB_ENTRY_IO);
3332 		size -= sz;
3333 		pa_base += sz;
3334 		tlb1_map_base += sz;
3335 	} while (size > 0);
3336 
3337 #ifdef SMP
3338 	bp_ntlb1s = tlb1_idx;
3339 #endif
3340 
3341 	return (va);
3342 }
3343 
3344 /*
3345  * Setup MAS4 defaults.
3346  * These values are loaded to MAS0-2 on a TLB miss.
3347  */
3348 static void
3349 set_mas4_defaults(void)
3350 {
3351 	uint32_t mas4;
3352 
3353 	/* Defaults: TLB0, PID0, TSIZED=4K */
3354 	mas4 = MAS4_TLBSELD0;
3355 	mas4 |= (TLB_SIZE_4K << MAS4_TSIZED_SHIFT) & MAS4_TSIZED_MASK;
3356 #ifdef SMP
3357 	mas4 |= MAS4_MD;
3358 #endif
3359 	mtspr(SPR_MAS4, mas4);
3360 	__asm __volatile("isync");
3361 }
3362 
3363 /*
3364  * Print out contents of the MAS registers for each TLB1 entry
3365  */
3366 void
3367 tlb1_print_tlbentries(void)
3368 {
3369 	uint32_t mas0, mas1, mas2, mas3, mas7;
3370 	int i;
3371 
3372 	debugf("TLB1 entries:\n");
3373 	for (i = 0; i < TLB1_ENTRIES; i++) {
3374 
3375 		mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(i);
3376 		mtspr(SPR_MAS0, mas0);
3377 
3378 		__asm __volatile("isync; tlbre");
3379 
3380 		mas1 = mfspr(SPR_MAS1);
3381 		mas2 = mfspr(SPR_MAS2);
3382 		mas3 = mfspr(SPR_MAS3);
3383 		mas7 = mfspr(SPR_MAS7);
3384 
3385 		tlb_print_entry(i, mas1, mas2, mas3, mas7);
3386 	}
3387 }
3388 
3389 /*
3390  * Print out contents of the in-ram tlb1 table.
3391  */
3392 void
3393 tlb1_print_entries(void)
3394 {
3395 	int i;
3396 
3397 	debugf("tlb1[] table entries:\n");
3398 	for (i = 0; i < TLB1_ENTRIES; i++)
3399 		tlb_print_entry(i, tlb1[i].mas1, tlb1[i].mas2, tlb1[i].mas3,
3400 		    tlb1[i].mas7);
3401 }
3402 
3403 /*
3404  * Return 0 if the physical IO range is encompassed by one of the
3405  * the TLB1 entries, otherwise return related error code.
3406  */
3407 static int
3408 tlb1_iomapped(int i, vm_paddr_t pa, vm_size_t size, vm_offset_t *va)
3409 {
3410 	uint32_t prot;
3411 	vm_paddr_t pa_start;
3412 	vm_paddr_t pa_end;
3413 	unsigned int entry_tsize;
3414 	vm_size_t entry_size;
3415 
3416 	*va = (vm_offset_t)NULL;
3417 
3418 	/* Skip invalid entries */
3419 	if (!(tlb1[i].mas1 & MAS1_VALID))
3420 		return (EINVAL);
3421 
3422 	/*
3423 	 * The entry must be cache-inhibited, guarded, and r/w
3424 	 * so it can function as an i/o page
3425 	 */
3426 	prot = tlb1[i].mas2 & (MAS2_I | MAS2_G);
3427 	if (prot != (MAS2_I | MAS2_G))
3428 		return (EPERM);
3429 
3430 	prot = tlb1[i].mas3 & (MAS3_SR | MAS3_SW);
3431 	if (prot != (MAS3_SR | MAS3_SW))
3432 		return (EPERM);
3433 
3434 	/* The address should be within the entry range. */
3435 	entry_tsize = (tlb1[i].mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3436 	KASSERT((entry_tsize), ("tlb1_iomapped: invalid entry tsize"));
3437 
3438 	entry_size = tsize2size(entry_tsize);
3439 	pa_start = (((vm_paddr_t)tlb1[i].mas7 & MAS7_RPN) << 32) |
3440 	    (tlb1[i].mas3 & MAS3_RPN);
3441 	pa_end = pa_start + entry_size;
3442 
3443 	if ((pa < pa_start) || ((pa + size) > pa_end))
3444 		return (ERANGE);
3445 
3446 	/* Return virtual address of this mapping. */
3447 	*va = (tlb1[i].mas2 & MAS2_EPN_MASK) + (pa - pa_start);
3448 	return (0);
3449 }
3450 
3451 /*
3452  * Invalidate all TLB0 entries which match the given TID. Note this is
3453  * dedicated for cases when invalidations should NOT be propagated to other
3454  * CPUs.
3455  */
3456 static void
3457 tid_flush(tlbtid_t tid)
3458 {
3459 	register_t msr;
3460 	uint32_t mas0, mas1, mas2;
3461 	int entry, way;
3462 
3463 
3464 	/* Don't evict kernel translations */
3465 	if (tid == TID_KERNEL)
3466 		return;
3467 
3468 	msr = mfmsr();
3469 	__asm __volatile("wrteei 0");
3470 
3471 	for (way = 0; way < TLB0_WAYS; way++)
3472 		for (entry = 0; entry < TLB0_ENTRIES_PER_WAY; entry++) {
3473 
3474 			mas0 = MAS0_TLBSEL(0) | MAS0_ESEL(way);
3475 			mtspr(SPR_MAS0, mas0);
3476 			__asm __volatile("isync");
3477 
3478 			mas2 = entry << MAS2_TLB0_ENTRY_IDX_SHIFT;
3479 			mtspr(SPR_MAS2, mas2);
3480 
3481 			__asm __volatile("isync; tlbre");
3482 
3483 			mas1 = mfspr(SPR_MAS1);
3484 
3485 			if (!(mas1 & MAS1_VALID))
3486 				continue;
3487 			if (((mas1 & MAS1_TID_MASK) >> MAS1_TID_SHIFT) != tid)
3488 				continue;
3489 			mas1 &= ~MAS1_VALID;
3490 			mtspr(SPR_MAS1, mas1);
3491 			__asm __volatile("isync; tlbwe; isync; msync");
3492 		}
3493 	mtmsr(msr);
3494 }
3495