2 * Copyright (C) 2007-2009 Semihalf, Rafal Jaworowski <raj@semihalf.com>
3 * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
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.
34 * Kernel and user threads run within one common virtual address space
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
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/malloc.h>
60 #include <sys/queue.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/msgbuf.h>
65 #include <sys/mutex.h>
67 #include <sys/vmmeter.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_pageout.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_param.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_pager.h>
80 #include <machine/bootinfo.h>
81 #include <machine/cpu.h>
82 #include <machine/pcb.h>
83 #include <machine/platform.h>
85 #include <machine/tlb.h>
86 #include <machine/spr.h>
87 #include <machine/vmparam.h>
88 #include <machine/md_var.h>
89 #include <machine/mmuvar.h>
90 #include <machine/pmap.h>
91 #include <machine/pte.h>
99 #define debugf(fmt, args...) printf(fmt, ##args)
101 #define debugf(fmt, args...)
104 #define TODO panic("%s: not implemented", __func__);
106 #include "opt_sched.h"
108 #error "e500 only works with SCHED_4BSD which uses a global scheduler lock."
110 extern struct mtx sched_lock;
112 extern int dumpsys_minidump;
114 extern unsigned char _etext[];
115 extern unsigned char _end[];
117 /* Kernel physical load address. */
118 extern uint32_t kernload;
119 vm_offset_t kernstart;
122 /* Message buffer and tables. */
123 static vm_offset_t data_start;
124 static vm_size_t data_end;
126 /* Phys/avail memory regions. */
127 static struct mem_region *availmem_regions;
128 static int availmem_regions_sz;
129 static struct mem_region *physmem_regions;
130 static int physmem_regions_sz;
132 /* Reserved KVA space and mutex for mmu_booke_zero_page. */
133 static vm_offset_t zero_page_va;
134 static struct mtx zero_page_mutex;
136 static struct mtx tlbivax_mutex;
139 * Reserved KVA space for mmu_booke_zero_page_idle. This is used
140 * by idle thred only, no lock required.
142 static vm_offset_t zero_page_idle_va;
144 /* Reserved KVA space and mutex for mmu_booke_copy_page. */
145 static vm_offset_t copy_page_src_va;
146 static vm_offset_t copy_page_dst_va;
147 static struct mtx copy_page_mutex;
149 /**************************************************************************/
151 /**************************************************************************/
153 static void mmu_booke_enter_locked(mmu_t, pmap_t, vm_offset_t, vm_page_t,
154 vm_prot_t, boolean_t);
156 unsigned int kptbl_min; /* Index of the first kernel ptbl. */
157 unsigned int kernel_ptbls; /* Number of KVA ptbls. */
160 * If user pmap is processed with mmu_booke_remove and the resident count
161 * drops to 0, there are no more pages to remove, so we need not continue.
163 #define PMAP_REMOVE_DONE(pmap) \
164 ((pmap) != kernel_pmap && (pmap)->pm_stats.resident_count == 0)
166 extern void tlb_lock(uint32_t *);
167 extern void tlb_unlock(uint32_t *);
168 extern void tid_flush(tlbtid_t);
170 /**************************************************************************/
171 /* TLB and TID handling */
172 /**************************************************************************/
174 /* Translation ID busy table */
175 static volatile pmap_t tidbusy[MAXCPU][TID_MAX + 1];
178 * TLB0 capabilities (entry, way numbers etc.). These can vary between e500
179 * core revisions and should be read from h/w registers during early config.
181 uint32_t tlb0_entries;
183 uint32_t tlb0_entries_per_way;
185 #define TLB0_ENTRIES (tlb0_entries)
186 #define TLB0_WAYS (tlb0_ways)
187 #define TLB0_ENTRIES_PER_WAY (tlb0_entries_per_way)
189 #define TLB1_ENTRIES 16
191 /* In-ram copy of the TLB1 */
192 static tlb_entry_t tlb1[TLB1_ENTRIES];
194 /* Next free entry in the TLB1 */
195 static unsigned int tlb1_idx;
197 static tlbtid_t tid_alloc(struct pmap *);
199 static void tlb_print_entry(int, uint32_t, uint32_t, uint32_t, uint32_t);
201 static int tlb1_set_entry(vm_offset_t, vm_offset_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_offset_t, vm_size_t);
206 static vm_size_t tsize2size(unsigned int);
207 static unsigned int size2tsize(vm_size_t);
208 static unsigned int ilog2(unsigned int);
210 static void set_mas4_defaults(void);
212 static inline void tlb0_flush_entry(vm_offset_t);
213 static inline unsigned int tlb0_tableidx(vm_offset_t, unsigned int);
215 /**************************************************************************/
216 /* Page table management */
217 /**************************************************************************/
219 /* Data for the pv entry allocation mechanism */
220 static uma_zone_t pvzone;
221 static struct vm_object pvzone_obj;
222 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
224 #define PV_ENTRY_ZONE_MIN 2048 /* min pv entries in uma zone */
226 #ifndef PMAP_SHPGPERPROC
227 #define PMAP_SHPGPERPROC 200
230 static void ptbl_init(void);
231 static struct ptbl_buf *ptbl_buf_alloc(void);
232 static void ptbl_buf_free(struct ptbl_buf *);
233 static void ptbl_free_pmap_ptbl(pmap_t, pte_t *);
235 static pte_t *ptbl_alloc(mmu_t, pmap_t, unsigned int);
236 static void ptbl_free(mmu_t, pmap_t, unsigned int);
237 static void ptbl_hold(mmu_t, pmap_t, unsigned int);
238 static int ptbl_unhold(mmu_t, pmap_t, unsigned int);
240 static vm_paddr_t pte_vatopa(mmu_t, pmap_t, vm_offset_t);
241 static pte_t *pte_find(mmu_t, pmap_t, vm_offset_t);
242 static void pte_enter(mmu_t, pmap_t, vm_page_t, vm_offset_t, uint32_t);
243 static int pte_remove(mmu_t, pmap_t, vm_offset_t, uint8_t);
245 static pv_entry_t pv_alloc(void);
246 static void pv_free(pv_entry_t);
247 static void pv_insert(pmap_t, vm_offset_t, vm_page_t);
248 static void pv_remove(pmap_t, vm_offset_t, vm_page_t);
250 /* Number of kva ptbl buffers, each covering one ptbl (PTBL_PAGES). */
251 #define PTBL_BUFS (128 * 16)
254 TAILQ_ENTRY(ptbl_buf) link; /* list link */
255 vm_offset_t kva; /* va of mapping */
258 /* ptbl free list and a lock used for access synchronization. */
259 static TAILQ_HEAD(, ptbl_buf) ptbl_buf_freelist;
260 static struct mtx ptbl_buf_freelist_lock;
262 /* Base address of kva space allocated fot ptbl bufs. */
263 static vm_offset_t ptbl_buf_pool_vabase;
265 /* Pointer to ptbl_buf structures. */
266 static struct ptbl_buf *ptbl_bufs;
268 void pmap_bootstrap_ap(volatile uint32_t *);
271 * Kernel MMU interface
273 static void mmu_booke_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
274 static void mmu_booke_clear_modify(mmu_t, vm_page_t);
275 static void mmu_booke_clear_reference(mmu_t, vm_page_t);
276 static void mmu_booke_copy(mmu_t, pmap_t, pmap_t, vm_offset_t,
277 vm_size_t, vm_offset_t);
278 static void mmu_booke_copy_page(mmu_t, vm_page_t, vm_page_t);
279 static void mmu_booke_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t,
280 vm_prot_t, boolean_t);
281 static void mmu_booke_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t,
282 vm_page_t, vm_prot_t);
283 static void mmu_booke_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t,
285 static vm_paddr_t mmu_booke_extract(mmu_t, pmap_t, vm_offset_t);
286 static vm_page_t mmu_booke_extract_and_hold(mmu_t, pmap_t, vm_offset_t,
288 static void mmu_booke_init(mmu_t);
289 static boolean_t mmu_booke_is_modified(mmu_t, vm_page_t);
290 static boolean_t mmu_booke_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
291 static boolean_t mmu_booke_ts_referenced(mmu_t, vm_page_t);
292 static vm_offset_t mmu_booke_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t,
294 static int mmu_booke_mincore(mmu_t, pmap_t, vm_offset_t);
295 static void mmu_booke_object_init_pt(mmu_t, pmap_t, vm_offset_t,
296 vm_object_t, vm_pindex_t, vm_size_t);
297 static boolean_t mmu_booke_page_exists_quick(mmu_t, pmap_t, vm_page_t);
298 static void mmu_booke_page_init(mmu_t, vm_page_t);
299 static int mmu_booke_page_wired_mappings(mmu_t, vm_page_t);
300 static void mmu_booke_pinit(mmu_t, pmap_t);
301 static void mmu_booke_pinit0(mmu_t, pmap_t);
302 static void mmu_booke_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t,
304 static void mmu_booke_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
305 static void mmu_booke_qremove(mmu_t, vm_offset_t, int);
306 static void mmu_booke_release(mmu_t, pmap_t);
307 static void mmu_booke_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
308 static void mmu_booke_remove_all(mmu_t, vm_page_t);
309 static void mmu_booke_remove_write(mmu_t, vm_page_t);
310 static void mmu_booke_zero_page(mmu_t, vm_page_t);
311 static void mmu_booke_zero_page_area(mmu_t, vm_page_t, int, int);
312 static void mmu_booke_zero_page_idle(mmu_t, vm_page_t);
313 static void mmu_booke_activate(mmu_t, struct thread *);
314 static void mmu_booke_deactivate(mmu_t, struct thread *);
315 static void mmu_booke_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
316 static void *mmu_booke_mapdev(mmu_t, vm_offset_t, vm_size_t);
317 static void mmu_booke_unmapdev(mmu_t, vm_offset_t, vm_size_t);
318 static vm_offset_t mmu_booke_kextract(mmu_t, vm_offset_t);
319 static void mmu_booke_kenter(mmu_t, vm_offset_t, vm_offset_t);
320 static void mmu_booke_kremove(mmu_t, vm_offset_t);
321 static boolean_t mmu_booke_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t);
322 static void mmu_booke_sync_icache(mmu_t, pmap_t, vm_offset_t,
324 static vm_offset_t mmu_booke_dumpsys_map(mmu_t, struct pmap_md *,
325 vm_size_t, vm_size_t *);
326 static void mmu_booke_dumpsys_unmap(mmu_t, struct pmap_md *,
327 vm_size_t, vm_offset_t);
328 static struct pmap_md *mmu_booke_scan_md(mmu_t, struct pmap_md *);
330 static mmu_method_t mmu_booke_methods[] = {
331 /* pmap dispatcher interface */
332 MMUMETHOD(mmu_change_wiring, mmu_booke_change_wiring),
333 MMUMETHOD(mmu_clear_modify, mmu_booke_clear_modify),
334 MMUMETHOD(mmu_clear_reference, mmu_booke_clear_reference),
335 MMUMETHOD(mmu_copy, mmu_booke_copy),
336 MMUMETHOD(mmu_copy_page, mmu_booke_copy_page),
337 MMUMETHOD(mmu_enter, mmu_booke_enter),
338 MMUMETHOD(mmu_enter_object, mmu_booke_enter_object),
339 MMUMETHOD(mmu_enter_quick, mmu_booke_enter_quick),
340 MMUMETHOD(mmu_extract, mmu_booke_extract),
341 MMUMETHOD(mmu_extract_and_hold, mmu_booke_extract_and_hold),
342 MMUMETHOD(mmu_init, mmu_booke_init),
343 MMUMETHOD(mmu_is_modified, mmu_booke_is_modified),
344 MMUMETHOD(mmu_is_prefaultable, mmu_booke_is_prefaultable),
345 MMUMETHOD(mmu_ts_referenced, mmu_booke_ts_referenced),
346 MMUMETHOD(mmu_map, mmu_booke_map),
347 MMUMETHOD(mmu_mincore, mmu_booke_mincore),
348 MMUMETHOD(mmu_object_init_pt, mmu_booke_object_init_pt),
349 MMUMETHOD(mmu_page_exists_quick,mmu_booke_page_exists_quick),
350 MMUMETHOD(mmu_page_init, mmu_booke_page_init),
351 MMUMETHOD(mmu_page_wired_mappings, mmu_booke_page_wired_mappings),
352 MMUMETHOD(mmu_pinit, mmu_booke_pinit),
353 MMUMETHOD(mmu_pinit0, mmu_booke_pinit0),
354 MMUMETHOD(mmu_protect, mmu_booke_protect),
355 MMUMETHOD(mmu_qenter, mmu_booke_qenter),
356 MMUMETHOD(mmu_qremove, mmu_booke_qremove),
357 MMUMETHOD(mmu_release, mmu_booke_release),
358 MMUMETHOD(mmu_remove, mmu_booke_remove),
359 MMUMETHOD(mmu_remove_all, mmu_booke_remove_all),
360 MMUMETHOD(mmu_remove_write, mmu_booke_remove_write),
361 MMUMETHOD(mmu_sync_icache, mmu_booke_sync_icache),
362 MMUMETHOD(mmu_zero_page, mmu_booke_zero_page),
363 MMUMETHOD(mmu_zero_page_area, mmu_booke_zero_page_area),
364 MMUMETHOD(mmu_zero_page_idle, mmu_booke_zero_page_idle),
365 MMUMETHOD(mmu_activate, mmu_booke_activate),
366 MMUMETHOD(mmu_deactivate, mmu_booke_deactivate),
368 /* Internal interfaces */
369 MMUMETHOD(mmu_bootstrap, mmu_booke_bootstrap),
370 MMUMETHOD(mmu_dev_direct_mapped,mmu_booke_dev_direct_mapped),
371 MMUMETHOD(mmu_mapdev, mmu_booke_mapdev),
372 MMUMETHOD(mmu_kenter, mmu_booke_kenter),
373 MMUMETHOD(mmu_kextract, mmu_booke_kextract),
374 /* MMUMETHOD(mmu_kremove, mmu_booke_kremove), */
375 MMUMETHOD(mmu_unmapdev, mmu_booke_unmapdev),
377 /* dumpsys() support */
378 MMUMETHOD(mmu_dumpsys_map, mmu_booke_dumpsys_map),
379 MMUMETHOD(mmu_dumpsys_unmap, mmu_booke_dumpsys_unmap),
380 MMUMETHOD(mmu_scan_md, mmu_booke_scan_md),
385 static mmu_def_t booke_mmu = {
401 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
404 CTR3(KTR_PMAP, "%s: tlb miss LOCK of CPU=%d, "
405 "tlb_lock=%p", __func__, pc->pc_cpuid, pc->pc_booke_tlb_lock);
407 KASSERT((pc->pc_cpuid != PCPU_GET(cpuid)),
408 ("tlb_miss_lock: tried to lock self"));
410 tlb_lock(pc->pc_booke_tlb_lock);
412 CTR1(KTR_PMAP, "%s: locked", __func__);
419 tlb_miss_unlock(void)
427 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
429 CTR2(KTR_PMAP, "%s: tlb miss UNLOCK of CPU=%d",
430 __func__, pc->pc_cpuid);
432 tlb_unlock(pc->pc_booke_tlb_lock);
434 CTR1(KTR_PMAP, "%s: unlocked", __func__);
440 /* Return number of entries in TLB0. */
442 tlb0_get_tlbconf(void)
446 tlb0_cfg = mfspr(SPR_TLB0CFG);
447 tlb0_entries = tlb0_cfg & TLBCFG_NENTRY_MASK;
448 tlb0_ways = (tlb0_cfg & TLBCFG_ASSOC_MASK) >> TLBCFG_ASSOC_SHIFT;
449 tlb0_entries_per_way = tlb0_entries / tlb0_ways;
452 /* Initialize pool of kva ptbl buffers. */
458 CTR3(KTR_PMAP, "%s: s (ptbl_bufs = 0x%08x size 0x%08x)", __func__,
459 (uint32_t)ptbl_bufs, sizeof(struct ptbl_buf) * PTBL_BUFS);
460 CTR3(KTR_PMAP, "%s: s (ptbl_buf_pool_vabase = 0x%08x size = 0x%08x)",
461 __func__, ptbl_buf_pool_vabase, PTBL_BUFS * PTBL_PAGES * PAGE_SIZE);
463 mtx_init(&ptbl_buf_freelist_lock, "ptbl bufs lock", NULL, MTX_DEF);
464 TAILQ_INIT(&ptbl_buf_freelist);
466 for (i = 0; i < PTBL_BUFS; i++) {
467 ptbl_bufs[i].kva = ptbl_buf_pool_vabase + i * PTBL_PAGES * PAGE_SIZE;
468 TAILQ_INSERT_TAIL(&ptbl_buf_freelist, &ptbl_bufs[i], link);
472 /* Get a ptbl_buf from the freelist. */
473 static struct ptbl_buf *
476 struct ptbl_buf *buf;
478 mtx_lock(&ptbl_buf_freelist_lock);
479 buf = TAILQ_FIRST(&ptbl_buf_freelist);
481 TAILQ_REMOVE(&ptbl_buf_freelist, buf, link);
482 mtx_unlock(&ptbl_buf_freelist_lock);
484 CTR2(KTR_PMAP, "%s: buf = %p", __func__, buf);
489 /* Return ptbl buff to free pool. */
491 ptbl_buf_free(struct ptbl_buf *buf)
494 CTR2(KTR_PMAP, "%s: buf = %p", __func__, buf);
496 mtx_lock(&ptbl_buf_freelist_lock);
497 TAILQ_INSERT_TAIL(&ptbl_buf_freelist, buf, link);
498 mtx_unlock(&ptbl_buf_freelist_lock);
502 * Search the list of allocated ptbl bufs and find on list of allocated ptbls
505 ptbl_free_pmap_ptbl(pmap_t pmap, pte_t *ptbl)
507 struct ptbl_buf *pbuf;
509 CTR2(KTR_PMAP, "%s: ptbl = %p", __func__, ptbl);
511 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
513 TAILQ_FOREACH(pbuf, &pmap->pm_ptbl_list, link)
514 if (pbuf->kva == (vm_offset_t)ptbl) {
515 /* Remove from pmap ptbl buf list. */
516 TAILQ_REMOVE(&pmap->pm_ptbl_list, pbuf, link);
518 /* Free corresponding ptbl buf. */
524 /* Allocate page table. */
526 ptbl_alloc(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
528 vm_page_t mtbl[PTBL_PAGES];
530 struct ptbl_buf *pbuf;
535 CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
536 (pmap == kernel_pmap), pdir_idx);
538 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
539 ("ptbl_alloc: invalid pdir_idx"));
540 KASSERT((pmap->pm_pdir[pdir_idx] == NULL),
541 ("pte_alloc: valid ptbl entry exists!"));
543 pbuf = ptbl_buf_alloc();
545 panic("pte_alloc: couldn't alloc kernel virtual memory");
547 ptbl = (pte_t *)pbuf->kva;
549 CTR2(KTR_PMAP, "%s: ptbl kva = %p", __func__, ptbl);
551 /* Allocate ptbl pages, this will sleep! */
552 for (i = 0; i < PTBL_PAGES; i++) {
553 pidx = (PTBL_PAGES * pdir_idx) + i;
554 while ((m = vm_page_alloc(NULL, pidx,
555 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
558 vm_page_unlock_queues();
560 vm_page_lock_queues();
566 /* Map allocated pages into kernel_pmap. */
567 mmu_booke_qenter(mmu, (vm_offset_t)ptbl, mtbl, PTBL_PAGES);
569 /* Zero whole ptbl. */
570 bzero((caddr_t)ptbl, PTBL_PAGES * PAGE_SIZE);
572 /* Add pbuf to the pmap ptbl bufs list. */
573 TAILQ_INSERT_TAIL(&pmap->pm_ptbl_list, pbuf, link);
578 /* Free ptbl pages and invalidate pdir entry. */
580 ptbl_free(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
588 CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
589 (pmap == kernel_pmap), pdir_idx);
591 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
592 ("ptbl_free: invalid pdir_idx"));
594 ptbl = pmap->pm_pdir[pdir_idx];
596 CTR2(KTR_PMAP, "%s: ptbl = %p", __func__, ptbl);
598 KASSERT((ptbl != NULL), ("ptbl_free: null ptbl"));
601 * Invalidate the pdir entry as soon as possible, so that other CPUs
602 * don't attempt to look up the page tables we are releasing.
604 mtx_lock_spin(&tlbivax_mutex);
607 pmap->pm_pdir[pdir_idx] = NULL;
610 mtx_unlock_spin(&tlbivax_mutex);
612 for (i = 0; i < PTBL_PAGES; i++) {
613 va = ((vm_offset_t)ptbl + (i * PAGE_SIZE));
614 pa = pte_vatopa(mmu, kernel_pmap, va);
615 m = PHYS_TO_VM_PAGE(pa);
616 vm_page_free_zero(m);
617 atomic_subtract_int(&cnt.v_wire_count, 1);
618 mmu_booke_kremove(mmu, va);
621 ptbl_free_pmap_ptbl(pmap, ptbl);
625 * Decrement ptbl pages hold count and attempt to free ptbl pages.
626 * Called when removing pte entry from ptbl.
628 * Return 1 if ptbl pages were freed.
631 ptbl_unhold(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
638 CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
639 (pmap == kernel_pmap), pdir_idx);
641 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
642 ("ptbl_unhold: invalid pdir_idx"));
643 KASSERT((pmap != kernel_pmap),
644 ("ptbl_unhold: unholding kernel ptbl!"));
646 ptbl = pmap->pm_pdir[pdir_idx];
648 //debugf("ptbl_unhold: ptbl = 0x%08x\n", (u_int32_t)ptbl);
649 KASSERT(((vm_offset_t)ptbl >= VM_MIN_KERNEL_ADDRESS),
650 ("ptbl_unhold: non kva ptbl"));
652 /* decrement hold count */
653 for (i = 0; i < PTBL_PAGES; i++) {
654 pa = pte_vatopa(mmu, kernel_pmap,
655 (vm_offset_t)ptbl + (i * PAGE_SIZE));
656 m = PHYS_TO_VM_PAGE(pa);
661 * Free ptbl pages if there are no pte etries in this ptbl.
662 * wire_count has the same value for all ptbl pages, so check the last
665 if (m->wire_count == 0) {
666 ptbl_free(mmu, pmap, pdir_idx);
668 //debugf("ptbl_unhold: e (freed ptbl)\n");
676 * Increment hold count for ptbl pages. This routine is used when a new pte
677 * entry is being inserted into the ptbl.
680 ptbl_hold(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
687 CTR3(KTR_PMAP, "%s: pmap = %p pdir_idx = %d", __func__, pmap,
690 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
691 ("ptbl_hold: invalid pdir_idx"));
692 KASSERT((pmap != kernel_pmap),
693 ("ptbl_hold: holding kernel ptbl!"));
695 ptbl = pmap->pm_pdir[pdir_idx];
697 KASSERT((ptbl != NULL), ("ptbl_hold: null ptbl"));
699 for (i = 0; i < PTBL_PAGES; i++) {
700 pa = pte_vatopa(mmu, kernel_pmap,
701 (vm_offset_t)ptbl + (i * PAGE_SIZE));
702 m = PHYS_TO_VM_PAGE(pa);
707 /* Allocate pv_entry structure. */
714 if (pv_entry_count > pv_entry_high_water)
716 pv = uma_zalloc(pvzone, M_NOWAIT);
721 /* Free pv_entry structure. */
723 pv_free(pv_entry_t pve)
727 uma_zfree(pvzone, pve);
731 /* Allocate and initialize pv_entry structure. */
733 pv_insert(pmap_t pmap, vm_offset_t va, vm_page_t m)
737 //int su = (pmap == kernel_pmap);
738 //debugf("pv_insert: s (su = %d pmap = 0x%08x va = 0x%08x m = 0x%08x)\n", su,
739 // (u_int32_t)pmap, va, (u_int32_t)m);
743 panic("pv_insert: no pv entries!");
749 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
750 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
752 TAILQ_INSERT_TAIL(&m->md.pv_list, pve, pv_link);
754 //debugf("pv_insert: e\n");
757 /* Destroy pv entry. */
759 pv_remove(pmap_t pmap, vm_offset_t va, vm_page_t m)
763 //int su = (pmap == kernel_pmap);
764 //debugf("pv_remove: s (su = %d pmap = 0x%08x va = 0x%08x)\n", su, (u_int32_t)pmap, va);
766 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
767 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
770 TAILQ_FOREACH(pve, &m->md.pv_list, pv_link) {
771 if ((pmap == pve->pv_pmap) && (va == pve->pv_va)) {
772 /* remove from pv_list */
773 TAILQ_REMOVE(&m->md.pv_list, pve, pv_link);
774 if (TAILQ_EMPTY(&m->md.pv_list))
775 vm_page_flag_clear(m, PG_WRITEABLE);
777 /* free pv entry struct */
783 //debugf("pv_remove: e\n");
787 * Clean pte entry, try to free page table page if requested.
789 * Return 1 if ptbl pages were freed, otherwise return 0.
792 pte_remove(mmu_t mmu, pmap_t pmap, vm_offset_t va, uint8_t flags)
794 unsigned int pdir_idx = PDIR_IDX(va);
795 unsigned int ptbl_idx = PTBL_IDX(va);
800 //int su = (pmap == kernel_pmap);
801 //debugf("pte_remove: s (su = %d pmap = 0x%08x va = 0x%08x flags = %d)\n",
802 // su, (u_int32_t)pmap, va, flags);
804 ptbl = pmap->pm_pdir[pdir_idx];
805 KASSERT(ptbl, ("pte_remove: null ptbl"));
807 pte = &ptbl[ptbl_idx];
809 if (pte == NULL || !PTE_ISVALID(pte))
812 if (PTE_ISWIRED(pte))
813 pmap->pm_stats.wired_count--;
815 /* Handle managed entry. */
816 if (PTE_ISMANAGED(pte)) {
817 /* Get vm_page_t for mapped pte. */
818 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
820 if (PTE_ISMODIFIED(pte))
823 if (PTE_ISREFERENCED(pte))
824 vm_page_flag_set(m, PG_REFERENCED);
826 pv_remove(pmap, va, m);
829 mtx_lock_spin(&tlbivax_mutex);
832 tlb0_flush_entry(va);
837 mtx_unlock_spin(&tlbivax_mutex);
839 pmap->pm_stats.resident_count--;
841 if (flags & PTBL_UNHOLD) {
842 //debugf("pte_remove: e (unhold)\n");
843 return (ptbl_unhold(mmu, pmap, pdir_idx));
846 //debugf("pte_remove: e\n");
851 * Insert PTE for a given page and virtual address.
854 pte_enter(mmu_t mmu, pmap_t pmap, vm_page_t m, vm_offset_t va, uint32_t flags)
856 unsigned int pdir_idx = PDIR_IDX(va);
857 unsigned int ptbl_idx = PTBL_IDX(va);
860 CTR4(KTR_PMAP, "%s: su = %d pmap = %p va = %p", __func__,
861 pmap == kernel_pmap, pmap, va);
863 /* Get the page table pointer. */
864 ptbl = pmap->pm_pdir[pdir_idx];
867 /* Allocate page table pages. */
868 ptbl = ptbl_alloc(mmu, pmap, pdir_idx);
871 * Check if there is valid mapping for requested
872 * va, if there is, remove it.
874 pte = &pmap->pm_pdir[pdir_idx][ptbl_idx];
875 if (PTE_ISVALID(pte)) {
876 pte_remove(mmu, pmap, va, PTBL_HOLD);
879 * pte is not used, increment hold count
882 if (pmap != kernel_pmap)
883 ptbl_hold(mmu, pmap, pdir_idx);
888 * Insert pv_entry into pv_list for mapped page if part of managed
891 if ((m->flags & PG_FICTITIOUS) == 0) {
892 if ((m->flags & PG_UNMANAGED) == 0) {
893 flags |= PTE_MANAGED;
895 /* Create and insert pv entry. */
896 pv_insert(pmap, va, m);
900 pmap->pm_stats.resident_count++;
902 mtx_lock_spin(&tlbivax_mutex);
905 tlb0_flush_entry(va);
906 if (pmap->pm_pdir[pdir_idx] == NULL) {
908 * If we just allocated a new page table, hook it in
911 pmap->pm_pdir[pdir_idx] = ptbl;
913 pte = &(pmap->pm_pdir[pdir_idx][ptbl_idx]);
914 pte->rpn = VM_PAGE_TO_PHYS(m) & ~PTE_PA_MASK;
915 pte->flags |= (PTE_VALID | flags);
918 mtx_unlock_spin(&tlbivax_mutex);
921 /* Return the pa for the given pmap/va. */
923 pte_vatopa(mmu_t mmu, pmap_t pmap, vm_offset_t va)
928 pte = pte_find(mmu, pmap, va);
929 if ((pte != NULL) && PTE_ISVALID(pte))
930 pa = (PTE_PA(pte) | (va & PTE_PA_MASK));
934 /* Get a pointer to a PTE in a page table. */
936 pte_find(mmu_t mmu, pmap_t pmap, vm_offset_t va)
938 unsigned int pdir_idx = PDIR_IDX(va);
939 unsigned int ptbl_idx = PTBL_IDX(va);
941 KASSERT((pmap != NULL), ("pte_find: invalid pmap"));
943 if (pmap->pm_pdir[pdir_idx])
944 return (&(pmap->pm_pdir[pdir_idx][ptbl_idx]));
949 /**************************************************************************/
951 /**************************************************************************/
954 * This is called during e500_init, before the system is really initialized.
957 mmu_booke_bootstrap(mmu_t mmu, vm_offset_t start, vm_offset_t kernelend)
959 vm_offset_t phys_kernelend;
960 struct mem_region *mp, *mp1;
963 u_int phys_avail_count;
964 vm_size_t physsz, hwphyssz, kstack0_sz;
965 vm_offset_t kernel_pdir, kstack0, va;
966 vm_paddr_t kstack0_phys;
970 debugf("mmu_booke_bootstrap: entered\n");
972 /* Initialize invalidation mutex */
973 mtx_init(&tlbivax_mutex, "tlbivax", NULL, MTX_SPIN);
975 /* Read TLB0 size and associativity. */
978 /* Align kernel start and end address (kernel image). */
979 kernstart = trunc_page(start);
980 data_start = round_page(kernelend);
981 kernsize = data_start - kernstart;
983 data_end = data_start;
985 /* Allocate space for the message buffer. */
986 msgbufp = (struct msgbuf *)data_end;
987 data_end += MSGBUF_SIZE;
988 debugf(" msgbufp at 0x%08x end = 0x%08x\n", (uint32_t)msgbufp,
991 data_end = round_page(data_end);
993 /* Allocate the dynamic per-cpu area. */
994 dpcpu = (void *)data_end;
995 data_end += DPCPU_SIZE;
996 dpcpu_init(dpcpu, 0);
998 /* Allocate space for ptbl_bufs. */
999 ptbl_bufs = (struct ptbl_buf *)data_end;
1000 data_end += sizeof(struct ptbl_buf) * PTBL_BUFS;
1001 debugf(" ptbl_bufs at 0x%08x end = 0x%08x\n", (uint32_t)ptbl_bufs,
1004 data_end = round_page(data_end);
1006 /* Allocate PTE tables for kernel KVA. */
1007 kernel_pdir = data_end;
1008 kernel_ptbls = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS +
1009 PDIR_SIZE - 1) / PDIR_SIZE;
1010 data_end += kernel_ptbls * PTBL_PAGES * PAGE_SIZE;
1011 debugf(" kernel ptbls: %d\n", kernel_ptbls);
1012 debugf(" kernel pdir at 0x%08x end = 0x%08x\n", kernel_pdir, data_end);
1014 debugf(" data_end: 0x%08x\n", data_end);
1015 if (data_end - kernstart > 0x1000000) {
1016 data_end = (data_end + 0x3fffff) & ~0x3fffff;
1017 tlb1_mapin_region(kernstart + 0x1000000,
1018 kernload + 0x1000000, data_end - kernstart - 0x1000000);
1020 data_end = (data_end + 0xffffff) & ~0xffffff;
1022 debugf(" updated data_end: 0x%08x\n", data_end);
1024 kernsize += data_end - data_start;
1027 * Clear the structures - note we can only do it safely after the
1028 * possible additional TLB1 translations are in place (above) so that
1029 * all range up to the currently calculated 'data_end' is covered.
1031 memset((void *)ptbl_bufs, 0, sizeof(struct ptbl_buf) * PTBL_SIZE);
1032 memset((void *)kernel_pdir, 0, kernel_ptbls * PTBL_PAGES * PAGE_SIZE);
1034 /*******************************************************/
1035 /* Set the start and end of kva. */
1036 /*******************************************************/
1037 virtual_avail = round_page(data_end);
1038 virtual_end = VM_MAX_KERNEL_ADDRESS;
1040 /* Allocate KVA space for page zero/copy operations. */
1041 zero_page_va = virtual_avail;
1042 virtual_avail += PAGE_SIZE;
1043 zero_page_idle_va = virtual_avail;
1044 virtual_avail += PAGE_SIZE;
1045 copy_page_src_va = virtual_avail;
1046 virtual_avail += PAGE_SIZE;
1047 copy_page_dst_va = virtual_avail;
1048 virtual_avail += PAGE_SIZE;
1049 debugf("zero_page_va = 0x%08x\n", zero_page_va);
1050 debugf("zero_page_idle_va = 0x%08x\n", zero_page_idle_va);
1051 debugf("copy_page_src_va = 0x%08x\n", copy_page_src_va);
1052 debugf("copy_page_dst_va = 0x%08x\n", copy_page_dst_va);
1054 /* Initialize page zero/copy mutexes. */
1055 mtx_init(&zero_page_mutex, "mmu_booke_zero_page", NULL, MTX_DEF);
1056 mtx_init(©_page_mutex, "mmu_booke_copy_page", NULL, MTX_DEF);
1058 /* Allocate KVA space for ptbl bufs. */
1059 ptbl_buf_pool_vabase = virtual_avail;
1060 virtual_avail += PTBL_BUFS * PTBL_PAGES * PAGE_SIZE;
1061 debugf("ptbl_buf_pool_vabase = 0x%08x end = 0x%08x\n",
1062 ptbl_buf_pool_vabase, virtual_avail);
1064 /* Calculate corresponding physical addresses for the kernel region. */
1065 phys_kernelend = kernload + kernsize;
1066 debugf("kernel image and allocated data:\n");
1067 debugf(" kernload = 0x%08x\n", kernload);
1068 debugf(" kernstart = 0x%08x\n", kernstart);
1069 debugf(" kernsize = 0x%08x\n", kernsize);
1071 if (sizeof(phys_avail) / sizeof(phys_avail[0]) < availmem_regions_sz)
1072 panic("mmu_booke_bootstrap: phys_avail too small");
1075 * Remove kernel physical address range from avail regions list. Page
1076 * align all regions. Non-page aligned memory isn't very interesting
1077 * to us. Also, sort the entries for ascending addresses.
1080 /* Retrieve phys/avail mem regions */
1081 mem_regions(&physmem_regions, &physmem_regions_sz,
1082 &availmem_regions, &availmem_regions_sz);
1084 cnt = availmem_regions_sz;
1085 debugf("processing avail regions:\n");
1086 for (mp = availmem_regions; mp->mr_size; mp++) {
1088 e = mp->mr_start + mp->mr_size;
1089 debugf(" %08x-%08x -> ", s, e);
1090 /* Check whether this region holds all of the kernel. */
1091 if (s < kernload && e > phys_kernelend) {
1092 availmem_regions[cnt].mr_start = phys_kernelend;
1093 availmem_regions[cnt++].mr_size = e - phys_kernelend;
1096 /* Look whether this regions starts within the kernel. */
1097 if (s >= kernload && s < phys_kernelend) {
1098 if (e <= phys_kernelend)
1102 /* Now look whether this region ends within the kernel. */
1103 if (e > kernload && e <= phys_kernelend) {
1108 /* Now page align the start and size of the region. */
1114 debugf("%08x-%08x = %x\n", s, e, sz);
1116 /* Check whether some memory is left here. */
1120 (cnt - (mp - availmem_regions)) * sizeof(*mp));
1126 /* Do an insertion sort. */
1127 for (mp1 = availmem_regions; mp1 < mp; mp1++)
1128 if (s < mp1->mr_start)
1131 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
1139 availmem_regions_sz = cnt;
1141 /*******************************************************/
1142 /* Steal physical memory for kernel stack from the end */
1143 /* of the first avail region */
1144 /*******************************************************/
1145 kstack0_sz = KSTACK_PAGES * PAGE_SIZE;
1146 kstack0_phys = availmem_regions[0].mr_start +
1147 availmem_regions[0].mr_size;
1148 kstack0_phys -= kstack0_sz;
1149 availmem_regions[0].mr_size -= kstack0_sz;
1151 /*******************************************************/
1152 /* Fill in phys_avail table, based on availmem_regions */
1153 /*******************************************************/
1154 phys_avail_count = 0;
1157 TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
1159 debugf("fill in phys_avail:\n");
1160 for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
1162 debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
1163 availmem_regions[i].mr_start,
1164 availmem_regions[i].mr_start +
1165 availmem_regions[i].mr_size,
1166 availmem_regions[i].mr_size);
1168 if (hwphyssz != 0 &&
1169 (physsz + availmem_regions[i].mr_size) >= hwphyssz) {
1170 debugf(" hw.physmem adjust\n");
1171 if (physsz < hwphyssz) {
1172 phys_avail[j] = availmem_regions[i].mr_start;
1174 availmem_regions[i].mr_start +
1182 phys_avail[j] = availmem_regions[i].mr_start;
1183 phys_avail[j + 1] = availmem_regions[i].mr_start +
1184 availmem_regions[i].mr_size;
1186 physsz += availmem_regions[i].mr_size;
1188 physmem = btoc(physsz);
1190 /* Calculate the last available physical address. */
1191 for (i = 0; phys_avail[i + 2] != 0; i += 2)
1193 Maxmem = powerpc_btop(phys_avail[i + 1]);
1195 debugf("Maxmem = 0x%08lx\n", Maxmem);
1196 debugf("phys_avail_count = %d\n", phys_avail_count);
1197 debugf("physsz = 0x%08x physmem = %ld (0x%08lx)\n", physsz, physmem,
1200 /*******************************************************/
1201 /* Initialize (statically allocated) kernel pmap. */
1202 /*******************************************************/
1203 PMAP_LOCK_INIT(kernel_pmap);
1204 kptbl_min = VM_MIN_KERNEL_ADDRESS / PDIR_SIZE;
1206 debugf("kernel_pmap = 0x%08x\n", (uint32_t)kernel_pmap);
1207 debugf("kptbl_min = %d, kernel_ptbls = %d\n", kptbl_min, kernel_ptbls);
1208 debugf("kernel pdir range: 0x%08x - 0x%08x\n",
1209 kptbl_min * PDIR_SIZE, (kptbl_min + kernel_ptbls) * PDIR_SIZE - 1);
1211 /* Initialize kernel pdir */
1212 for (i = 0; i < kernel_ptbls; i++)
1213 kernel_pmap->pm_pdir[kptbl_min + i] =
1214 (pte_t *)(kernel_pdir + (i * PAGE_SIZE * PTBL_PAGES));
1216 for (i = 0; i < MAXCPU; i++) {
1217 kernel_pmap->pm_tid[i] = TID_KERNEL;
1219 /* Initialize each CPU's tidbusy entry 0 with kernel_pmap */
1220 tidbusy[i][0] = kernel_pmap;
1224 * Fill in PTEs covering kernel code and data. They are not required
1225 * for address translation, as this area is covered by static TLB1
1226 * entries, but for pte_vatopa() to work correctly with kernel area
1229 for (va = KERNBASE; va < data_end; va += PAGE_SIZE) {
1230 pte = &(kernel_pmap->pm_pdir[PDIR_IDX(va)][PTBL_IDX(va)]);
1231 pte->rpn = kernload + (va - KERNBASE);
1232 pte->flags = PTE_M | PTE_SR | PTE_SW | PTE_SX | PTE_WIRED |
1235 /* Mark kernel_pmap active on all CPUs */
1236 kernel_pmap->pm_active = ~0;
1238 /*******************************************************/
1240 /*******************************************************/
1242 /* Enter kstack0 into kernel map, provide guard page */
1243 kstack0 = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
1244 thread0.td_kstack = kstack0;
1245 thread0.td_kstack_pages = KSTACK_PAGES;
1247 debugf("kstack_sz = 0x%08x\n", kstack0_sz);
1248 debugf("kstack0_phys at 0x%08x - 0x%08x\n",
1249 kstack0_phys, kstack0_phys + kstack0_sz);
1250 debugf("kstack0 at 0x%08x - 0x%08x\n", kstack0, kstack0 + kstack0_sz);
1252 virtual_avail += KSTACK_GUARD_PAGES * PAGE_SIZE + kstack0_sz;
1253 for (i = 0; i < KSTACK_PAGES; i++) {
1254 mmu_booke_kenter(mmu, kstack0, kstack0_phys);
1255 kstack0 += PAGE_SIZE;
1256 kstack0_phys += PAGE_SIZE;
1259 debugf("virtual_avail = %08x\n", virtual_avail);
1260 debugf("virtual_end = %08x\n", virtual_end);
1262 debugf("mmu_booke_bootstrap: exit\n");
1266 pmap_bootstrap_ap(volatile uint32_t *trcp __unused)
1271 * Finish TLB1 configuration: the BSP already set up its TLB1 and we
1272 * have the snapshot of its contents in the s/w tlb1[] table, so use
1273 * these values directly to (re)program AP's TLB1 hardware.
1275 for (i = 0; i < tlb1_idx; i ++) {
1276 /* Skip invalid entries */
1277 if (!(tlb1[i].mas1 & MAS1_VALID))
1280 tlb1_write_entry(i);
1283 set_mas4_defaults();
1287 * Get the physical page address for the given pmap/virtual address.
1290 mmu_booke_extract(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1295 pa = pte_vatopa(mmu, pmap, va);
1302 * Extract the physical page address associated with the given
1303 * kernel virtual address.
1306 mmu_booke_kextract(mmu_t mmu, vm_offset_t va)
1309 return (pte_vatopa(mmu, kernel_pmap, va));
1313 * Initialize the pmap module.
1314 * Called by vm_init, to initialize any structures that the pmap
1315 * system needs to map virtual memory.
1318 mmu_booke_init(mmu_t mmu)
1320 int shpgperproc = PMAP_SHPGPERPROC;
1323 * Initialize the address space (zone) for the pv entries. Set a
1324 * high water mark so that the system can recover from excessive
1325 * numbers of pv entries.
1327 pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
1328 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1330 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1331 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
1333 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1334 pv_entry_high_water = 9 * (pv_entry_max / 10);
1336 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
1338 /* Pre-fill pvzone with initial number of pv entries. */
1339 uma_prealloc(pvzone, PV_ENTRY_ZONE_MIN);
1341 /* Initialize ptbl allocation. */
1346 * Map a list of wired pages into kernel virtual address space. This is
1347 * intended for temporary mappings which do not need page modification or
1348 * references recorded. Existing mappings in the region are overwritten.
1351 mmu_booke_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
1356 while (count-- > 0) {
1357 mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
1364 * Remove page mappings from kernel virtual address space. Intended for
1365 * temporary mappings entered by mmu_booke_qenter.
1368 mmu_booke_qremove(mmu_t mmu, vm_offset_t sva, int count)
1373 while (count-- > 0) {
1374 mmu_booke_kremove(mmu, va);
1380 * Map a wired page into kernel virtual address space.
1383 mmu_booke_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa)
1385 unsigned int pdir_idx = PDIR_IDX(va);
1386 unsigned int ptbl_idx = PTBL_IDX(va);
1390 KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1391 (va <= VM_MAX_KERNEL_ADDRESS)), ("mmu_booke_kenter: invalid va"));
1394 flags |= (PTE_SR | PTE_SW | PTE_SX | PTE_WIRED | PTE_VALID);
1397 pte = &(kernel_pmap->pm_pdir[pdir_idx][ptbl_idx]);
1399 mtx_lock_spin(&tlbivax_mutex);
1402 if (PTE_ISVALID(pte)) {
1404 CTR1(KTR_PMAP, "%s: replacing entry!", __func__);
1406 /* Flush entry from TLB0 */
1407 tlb0_flush_entry(va);
1410 pte->rpn = pa & ~PTE_PA_MASK;
1413 //debugf("mmu_booke_kenter: pdir_idx = %d ptbl_idx = %d va=0x%08x "
1414 // "pa=0x%08x rpn=0x%08x flags=0x%08x\n",
1415 // pdir_idx, ptbl_idx, va, pa, pte->rpn, pte->flags);
1417 /* Flush the real memory from the instruction cache. */
1418 if ((flags & (PTE_I | PTE_G)) == 0) {
1419 __syncicache((void *)va, PAGE_SIZE);
1423 mtx_unlock_spin(&tlbivax_mutex);
1427 * Remove a page from kernel page table.
1430 mmu_booke_kremove(mmu_t mmu, vm_offset_t va)
1432 unsigned int pdir_idx = PDIR_IDX(va);
1433 unsigned int ptbl_idx = PTBL_IDX(va);
1436 // CTR2(KTR_PMAP,("%s: s (va = 0x%08x)\n", __func__, va));
1438 KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1439 (va <= VM_MAX_KERNEL_ADDRESS)),
1440 ("mmu_booke_kremove: invalid va"));
1442 pte = &(kernel_pmap->pm_pdir[pdir_idx][ptbl_idx]);
1444 if (!PTE_ISVALID(pte)) {
1446 CTR1(KTR_PMAP, "%s: invalid pte", __func__);
1451 mtx_lock_spin(&tlbivax_mutex);
1454 /* Invalidate entry in TLB0, update PTE. */
1455 tlb0_flush_entry(va);
1460 mtx_unlock_spin(&tlbivax_mutex);
1464 * Initialize pmap associated with process 0.
1467 mmu_booke_pinit0(mmu_t mmu, pmap_t pmap)
1470 mmu_booke_pinit(mmu, pmap);
1471 PCPU_SET(curpmap, pmap);
1475 * Initialize a preallocated and zeroed pmap structure,
1476 * such as one in a vmspace structure.
1479 mmu_booke_pinit(mmu_t mmu, pmap_t pmap)
1483 CTR4(KTR_PMAP, "%s: pmap = %p, proc %d '%s'", __func__, pmap,
1484 curthread->td_proc->p_pid, curthread->td_proc->p_comm);
1486 KASSERT((pmap != kernel_pmap), ("pmap_pinit: initializing kernel_pmap"));
1488 PMAP_LOCK_INIT(pmap);
1489 for (i = 0; i < MAXCPU; i++)
1490 pmap->pm_tid[i] = TID_NONE;
1491 pmap->pm_active = 0;
1492 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1493 bzero(&pmap->pm_pdir, sizeof(pte_t *) * PDIR_NENTRIES);
1494 TAILQ_INIT(&pmap->pm_ptbl_list);
1498 * Release any resources held by the given physical map.
1499 * Called when a pmap initialized by mmu_booke_pinit is being released.
1500 * Should only be called if the map contains no valid mappings.
1503 mmu_booke_release(mmu_t mmu, pmap_t pmap)
1506 printf("mmu_booke_release: s\n");
1508 KASSERT(pmap->pm_stats.resident_count == 0,
1509 ("pmap_release: pmap resident count %ld != 0",
1510 pmap->pm_stats.resident_count));
1512 PMAP_LOCK_DESTROY(pmap);
1516 * Insert the given physical page at the specified virtual address in the
1517 * target physical map with the protection requested. If specified the page
1518 * will be wired down.
1521 mmu_booke_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1522 vm_prot_t prot, boolean_t wired)
1525 vm_page_lock_queues();
1527 mmu_booke_enter_locked(mmu, pmap, va, m, prot, wired);
1528 vm_page_unlock_queues();
1533 mmu_booke_enter_locked(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1534 vm_prot_t prot, boolean_t wired)
1541 pa = VM_PAGE_TO_PHYS(m);
1542 su = (pmap == kernel_pmap);
1545 //debugf("mmu_booke_enter_locked: s (pmap=0x%08x su=%d tid=%d m=0x%08x va=0x%08x "
1546 // "pa=0x%08x prot=0x%08x wired=%d)\n",
1547 // (u_int32_t)pmap, su, pmap->pm_tid,
1548 // (u_int32_t)m, va, pa, prot, wired);
1551 KASSERT(((va >= virtual_avail) &&
1552 (va <= VM_MAX_KERNEL_ADDRESS)),
1553 ("mmu_booke_enter_locked: kernel pmap, non kernel va"));
1555 KASSERT((va <= VM_MAXUSER_ADDRESS),
1556 ("mmu_booke_enter_locked: user pmap, non user va"));
1559 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1562 * If there is an existing mapping, and the physical address has not
1563 * changed, must be protection or wiring change.
1565 if (((pte = pte_find(mmu, pmap, va)) != NULL) &&
1566 (PTE_ISVALID(pte)) && (PTE_PA(pte) == pa)) {
1569 * Before actually updating pte->flags we calculate and
1570 * prepare its new value in a helper var.
1573 flags &= ~(PTE_UW | PTE_UX | PTE_SW | PTE_SX | PTE_MODIFIED);
1575 /* Wiring change, just update stats. */
1577 if (!PTE_ISWIRED(pte)) {
1579 pmap->pm_stats.wired_count++;
1582 if (PTE_ISWIRED(pte)) {
1583 flags &= ~PTE_WIRED;
1584 pmap->pm_stats.wired_count--;
1588 if (prot & VM_PROT_WRITE) {
1589 /* Add write permissions. */
1594 vm_page_flag_set(m, PG_WRITEABLE);
1596 /* Handle modified pages, sense modify status. */
1599 * The PTE_MODIFIED flag could be set by underlying
1600 * TLB misses since we last read it (above), possibly
1601 * other CPUs could update it so we check in the PTE
1602 * directly rather than rely on that saved local flags
1605 if (PTE_ISMODIFIED(pte))
1609 if (prot & VM_PROT_EXECUTE) {
1615 * Check existing flags for execute permissions: if we
1616 * are turning execute permissions on, icache should
1619 if ((flags & (PTE_UX | PTE_SX)) == 0)
1623 flags &= ~PTE_REFERENCED;
1626 * The new flags value is all calculated -- only now actually
1629 mtx_lock_spin(&tlbivax_mutex);
1632 tlb0_flush_entry(va);
1636 mtx_unlock_spin(&tlbivax_mutex);
1640 * If there is an existing mapping, but it's for a different
1641 * physical address, pte_enter() will delete the old mapping.
1643 //if ((pte != NULL) && PTE_ISVALID(pte))
1644 // debugf("mmu_booke_enter_locked: replace\n");
1646 // debugf("mmu_booke_enter_locked: new\n");
1648 /* Now set up the flags and install the new mapping. */
1649 flags = (PTE_SR | PTE_VALID);
1655 if (prot & VM_PROT_WRITE) {
1660 vm_page_flag_set(m, PG_WRITEABLE);
1663 if (prot & VM_PROT_EXECUTE) {
1669 /* If its wired update stats. */
1671 pmap->pm_stats.wired_count++;
1675 pte_enter(mmu, pmap, m, va, flags);
1677 /* Flush the real memory from the instruction cache. */
1678 if (prot & VM_PROT_EXECUTE)
1682 if (sync && (su || pmap == PCPU_GET(curpmap))) {
1683 __syncicache((void *)va, PAGE_SIZE);
1689 * Maps a sequence of resident pages belonging to the same object.
1690 * The sequence begins with the given page m_start. This page is
1691 * mapped at the given virtual address start. Each subsequent page is
1692 * mapped at a virtual address that is offset from start by the same
1693 * amount as the page is offset from m_start within the object. The
1694 * last page in the sequence is the page with the largest offset from
1695 * m_start that can be mapped at a virtual address less than the given
1696 * virtual address end. Not every virtual page between start and end
1697 * is mapped; only those for which a resident page exists with the
1698 * corresponding offset from m_start are mapped.
1701 mmu_booke_enter_object(mmu_t mmu, pmap_t pmap, vm_offset_t start,
1702 vm_offset_t end, vm_page_t m_start, vm_prot_t prot)
1705 vm_pindex_t diff, psize;
1707 psize = atop(end - start);
1710 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1711 mmu_booke_enter_locked(mmu, pmap, start + ptoa(diff), m,
1712 prot & (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1713 m = TAILQ_NEXT(m, listq);
1719 mmu_booke_enter_quick(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1724 mmu_booke_enter_locked(mmu, pmap, va, m,
1725 prot & (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1730 * Remove the given range of addresses from the specified map.
1732 * It is assumed that the start and end are properly rounded to the page size.
1735 mmu_booke_remove(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_offset_t endva)
1740 int su = (pmap == kernel_pmap);
1742 //debugf("mmu_booke_remove: s (su = %d pmap=0x%08x tid=%d va=0x%08x endva=0x%08x)\n",
1743 // su, (u_int32_t)pmap, pmap->pm_tid, va, endva);
1746 KASSERT(((va >= virtual_avail) &&
1747 (va <= VM_MAX_KERNEL_ADDRESS)),
1748 ("mmu_booke_remove: kernel pmap, non kernel va"));
1750 KASSERT((va <= VM_MAXUSER_ADDRESS),
1751 ("mmu_booke_remove: user pmap, non user va"));
1754 if (PMAP_REMOVE_DONE(pmap)) {
1755 //debugf("mmu_booke_remove: e (empty)\n");
1759 hold_flag = PTBL_HOLD_FLAG(pmap);
1760 //debugf("mmu_booke_remove: hold_flag = %d\n", hold_flag);
1762 vm_page_lock_queues();
1764 for (; va < endva; va += PAGE_SIZE) {
1765 pte = pte_find(mmu, pmap, va);
1766 if ((pte != NULL) && PTE_ISVALID(pte))
1767 pte_remove(mmu, pmap, va, hold_flag);
1770 vm_page_unlock_queues();
1772 //debugf("mmu_booke_remove: e\n");
1776 * Remove physical page from all pmaps in which it resides.
1779 mmu_booke_remove_all(mmu_t mmu, vm_page_t m)
1784 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1786 for (pv = TAILQ_FIRST(&m->md.pv_list); pv != NULL; pv = pvn) {
1787 pvn = TAILQ_NEXT(pv, pv_link);
1789 PMAP_LOCK(pv->pv_pmap);
1790 hold_flag = PTBL_HOLD_FLAG(pv->pv_pmap);
1791 pte_remove(mmu, pv->pv_pmap, pv->pv_va, hold_flag);
1792 PMAP_UNLOCK(pv->pv_pmap);
1794 vm_page_flag_clear(m, PG_WRITEABLE);
1798 * Map a range of physical addresses into kernel virtual address space.
1801 mmu_booke_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start,
1802 vm_offset_t pa_end, int prot)
1804 vm_offset_t sva = *virt;
1805 vm_offset_t va = sva;
1807 //debugf("mmu_booke_map: s (sva = 0x%08x pa_start = 0x%08x pa_end = 0x%08x)\n",
1808 // sva, pa_start, pa_end);
1810 while (pa_start < pa_end) {
1811 mmu_booke_kenter(mmu, va, pa_start);
1813 pa_start += PAGE_SIZE;
1817 //debugf("mmu_booke_map: e (va = 0x%08x)\n", va);
1822 * The pmap must be activated before it's address space can be accessed in any
1826 mmu_booke_activate(mmu_t mmu, struct thread *td)
1830 pmap = &td->td_proc->p_vmspace->vm_pmap;
1832 CTR5(KTR_PMAP, "%s: s (td = %p, proc = '%s', id = %d, pmap = 0x%08x)",
1833 __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1835 KASSERT((pmap != kernel_pmap), ("mmu_booke_activate: kernel_pmap!"));
1837 mtx_lock_spin(&sched_lock);
1839 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
1840 PCPU_SET(curpmap, pmap);
1842 if (pmap->pm_tid[PCPU_GET(cpuid)] == TID_NONE)
1845 /* Load PID0 register with pmap tid value. */
1846 mtspr(SPR_PID0, pmap->pm_tid[PCPU_GET(cpuid)]);
1847 __asm __volatile("isync");
1849 mtx_unlock_spin(&sched_lock);
1851 CTR3(KTR_PMAP, "%s: e (tid = %d for '%s')", __func__,
1852 pmap->pm_tid[PCPU_GET(cpuid)], td->td_proc->p_comm);
1856 * Deactivate the specified process's address space.
1859 mmu_booke_deactivate(mmu_t mmu, struct thread *td)
1863 pmap = &td->td_proc->p_vmspace->vm_pmap;
1865 CTR5(KTR_PMAP, "%s: td=%p, proc = '%s', id = %d, pmap = 0x%08x",
1866 __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1868 atomic_clear_int(&pmap->pm_active, PCPU_GET(cpumask));
1869 PCPU_SET(curpmap, NULL);
1873 * Copy the range specified by src_addr/len
1874 * from the source map to the range dst_addr/len
1875 * in the destination map.
1877 * This routine is only advisory and need not do anything.
1880 mmu_booke_copy(mmu_t mmu, pmap_t dst_pmap, pmap_t src_pmap,
1881 vm_offset_t dst_addr, vm_size_t len, vm_offset_t src_addr)
1887 * Set the physical protection on the specified range of this map as requested.
1890 mmu_booke_protect(mmu_t mmu, pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
1897 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1898 mmu_booke_remove(mmu, pmap, sva, eva);
1902 if (prot & VM_PROT_WRITE)
1905 vm_page_lock_queues();
1907 for (va = sva; va < eva; va += PAGE_SIZE) {
1908 if ((pte = pte_find(mmu, pmap, va)) != NULL) {
1909 if (PTE_ISVALID(pte)) {
1910 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
1912 mtx_lock_spin(&tlbivax_mutex);
1915 /* Handle modified pages. */
1916 if (PTE_ISMODIFIED(pte))
1919 /* Referenced pages. */
1920 if (PTE_ISREFERENCED(pte))
1921 vm_page_flag_set(m, PG_REFERENCED);
1923 tlb0_flush_entry(va);
1924 pte->flags &= ~(PTE_UW | PTE_SW | PTE_MODIFIED |
1928 mtx_unlock_spin(&tlbivax_mutex);
1933 vm_page_unlock_queues();
1937 * Clear the write and modified bits in each of the given page's mappings.
1940 mmu_booke_remove_write(mmu_t mmu, vm_page_t m)
1945 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1946 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0 ||
1947 (m->flags & PG_WRITEABLE) == 0)
1950 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
1951 PMAP_LOCK(pv->pv_pmap);
1952 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
1953 if (PTE_ISVALID(pte)) {
1954 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
1956 mtx_lock_spin(&tlbivax_mutex);
1959 /* Handle modified pages. */
1960 if (PTE_ISMODIFIED(pte))
1963 /* Referenced pages. */
1964 if (PTE_ISREFERENCED(pte))
1965 vm_page_flag_set(m, PG_REFERENCED);
1967 /* Flush mapping from TLB0. */
1968 pte->flags &= ~(PTE_UW | PTE_SW | PTE_MODIFIED |
1972 mtx_unlock_spin(&tlbivax_mutex);
1975 PMAP_UNLOCK(pv->pv_pmap);
1977 vm_page_flag_clear(m, PG_WRITEABLE);
1981 mmu_booke_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
1990 va = trunc_page(va);
1991 sz = round_page(sz);
1993 vm_page_lock_queues();
1994 pmap = PCPU_GET(curpmap);
1995 active = (pm == kernel_pmap || pm == pmap) ? 1 : 0;
1998 pte = pte_find(mmu, pm, va);
1999 valid = (pte != NULL && PTE_ISVALID(pte)) ? 1 : 0;
2005 /* Create a mapping in the active pmap. */
2007 m = PHYS_TO_VM_PAGE(pa);
2009 pte_enter(mmu, pmap, m, addr,
2010 PTE_SR | PTE_VALID | PTE_UR);
2011 __syncicache((void *)addr, PAGE_SIZE);
2012 pte_remove(mmu, pmap, addr, PTBL_UNHOLD);
2015 __syncicache((void *)va, PAGE_SIZE);
2020 vm_page_unlock_queues();
2024 * Atomically extract and hold the physical page with the given
2025 * pmap and virtual address pair if that mapping permits the given
2029 mmu_booke_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va,
2037 vm_page_lock_queues();
2040 pte = pte_find(mmu, pmap, va);
2041 if ((pte != NULL) && PTE_ISVALID(pte)) {
2042 if (pmap == kernel_pmap)
2047 if ((pte->flags & pte_wbit) || ((prot & VM_PROT_WRITE) == 0)) {
2048 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
2053 vm_page_unlock_queues();
2059 * Initialize a vm_page's machine-dependent fields.
2062 mmu_booke_page_init(mmu_t mmu, vm_page_t m)
2065 TAILQ_INIT(&m->md.pv_list);
2069 * mmu_booke_zero_page_area zeros the specified hardware page by
2070 * mapping it into virtual memory and using bzero to clear
2073 * off and size must reside within a single page.
2076 mmu_booke_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
2080 /* XXX KASSERT off and size are within a single page? */
2082 mtx_lock(&zero_page_mutex);
2085 mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2086 bzero((caddr_t)va + off, size);
2087 mmu_booke_kremove(mmu, va);
2089 mtx_unlock(&zero_page_mutex);
2093 * mmu_booke_zero_page zeros the specified hardware page.
2096 mmu_booke_zero_page(mmu_t mmu, vm_page_t m)
2099 mmu_booke_zero_page_area(mmu, m, 0, PAGE_SIZE);
2103 * mmu_booke_copy_page copies the specified (machine independent) page by
2104 * mapping the page into virtual memory and using memcopy to copy the page,
2105 * one machine dependent page at a time.
2108 mmu_booke_copy_page(mmu_t mmu, vm_page_t sm, vm_page_t dm)
2110 vm_offset_t sva, dva;
2112 sva = copy_page_src_va;
2113 dva = copy_page_dst_va;
2115 mtx_lock(©_page_mutex);
2116 mmu_booke_kenter(mmu, sva, VM_PAGE_TO_PHYS(sm));
2117 mmu_booke_kenter(mmu, dva, VM_PAGE_TO_PHYS(dm));
2118 memcpy((caddr_t)dva, (caddr_t)sva, PAGE_SIZE);
2119 mmu_booke_kremove(mmu, dva);
2120 mmu_booke_kremove(mmu, sva);
2121 mtx_unlock(©_page_mutex);
2125 * mmu_booke_zero_page_idle zeros the specified hardware page by mapping it
2126 * into virtual memory and using bzero to clear its contents. This is intended
2127 * to be called from the vm_pagezero process only and outside of Giant. No
2131 mmu_booke_zero_page_idle(mmu_t mmu, vm_page_t m)
2135 va = zero_page_idle_va;
2136 mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2137 bzero((caddr_t)va, PAGE_SIZE);
2138 mmu_booke_kremove(mmu, va);
2142 * Return whether or not the specified physical page was modified
2143 * in any of physical maps.
2146 mmu_booke_is_modified(mmu_t mmu, vm_page_t m)
2151 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2152 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
2155 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2156 PMAP_LOCK(pv->pv_pmap);
2157 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
2158 if (!PTE_ISVALID(pte))
2159 goto make_sure_to_unlock;
2161 if (PTE_ISMODIFIED(pte)) {
2162 PMAP_UNLOCK(pv->pv_pmap);
2166 make_sure_to_unlock:
2167 PMAP_UNLOCK(pv->pv_pmap);
2173 * Return whether or not the specified virtual address is eligible
2177 mmu_booke_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t addr)
2184 * Clear the modify bits on the specified physical page.
2187 mmu_booke_clear_modify(mmu_t mmu, vm_page_t m)
2192 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2193 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
2196 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2197 PMAP_LOCK(pv->pv_pmap);
2198 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
2199 if (!PTE_ISVALID(pte))
2200 goto make_sure_to_unlock;
2202 mtx_lock_spin(&tlbivax_mutex);
2205 if (pte->flags & (PTE_SW | PTE_UW | PTE_MODIFIED)) {
2206 tlb0_flush_entry(pv->pv_va);
2207 pte->flags &= ~(PTE_SW | PTE_UW | PTE_MODIFIED |
2212 mtx_unlock_spin(&tlbivax_mutex);
2214 make_sure_to_unlock:
2215 PMAP_UNLOCK(pv->pv_pmap);
2220 * Return a count of reference bits for a page, clearing those bits.
2221 * It is not necessary for every reference bit to be cleared, but it
2222 * is necessary that 0 only be returned when there are truly no
2223 * reference bits set.
2225 * XXX: The exact number of bits to check and clear is a matter that
2226 * should be tested and standardized at some point in the future for
2227 * optimal aging of shared pages.
2230 mmu_booke_ts_referenced(mmu_t mmu, vm_page_t m)
2236 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2237 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
2241 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2242 PMAP_LOCK(pv->pv_pmap);
2243 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
2244 if (!PTE_ISVALID(pte))
2245 goto make_sure_to_unlock;
2247 if (PTE_ISREFERENCED(pte)) {
2248 mtx_lock_spin(&tlbivax_mutex);
2251 tlb0_flush_entry(pv->pv_va);
2252 pte->flags &= ~PTE_REFERENCED;
2255 mtx_unlock_spin(&tlbivax_mutex);
2258 PMAP_UNLOCK(pv->pv_pmap);
2263 make_sure_to_unlock:
2264 PMAP_UNLOCK(pv->pv_pmap);
2270 * Clear the reference bit on the specified physical page.
2273 mmu_booke_clear_reference(mmu_t mmu, vm_page_t m)
2278 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2279 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
2282 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2283 PMAP_LOCK(pv->pv_pmap);
2284 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
2285 if (!PTE_ISVALID(pte))
2286 goto make_sure_to_unlock;
2288 if (PTE_ISREFERENCED(pte)) {
2289 mtx_lock_spin(&tlbivax_mutex);
2292 tlb0_flush_entry(pv->pv_va);
2293 pte->flags &= ~PTE_REFERENCED;
2296 mtx_unlock_spin(&tlbivax_mutex);
2299 make_sure_to_unlock:
2300 PMAP_UNLOCK(pv->pv_pmap);
2305 * Change wiring attribute for a map/virtual-address pair.
2308 mmu_booke_change_wiring(mmu_t mmu, pmap_t pmap, vm_offset_t va, boolean_t wired)
2313 if ((pte = pte_find(mmu, pmap, va)) != NULL) {
2315 if (!PTE_ISWIRED(pte)) {
2316 pte->flags |= PTE_WIRED;
2317 pmap->pm_stats.wired_count++;
2320 if (PTE_ISWIRED(pte)) {
2321 pte->flags &= ~PTE_WIRED;
2322 pmap->pm_stats.wired_count--;
2330 * Return true if the pmap's pv is one of the first 16 pvs linked to from this
2331 * page. This count may be changed upwards or downwards in the future; it is
2332 * only necessary that true be returned for a small subset of pmaps for proper
2336 mmu_booke_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
2341 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2342 if ((m->flags & (PG_FICTITIOUS | PG_UNMANAGED)) != 0)
2346 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2347 if (pv->pv_pmap == pmap)
2357 * Return the number of managed mappings to the given physical page that are
2361 mmu_booke_page_wired_mappings(mmu_t mmu, vm_page_t m)
2367 if ((m->flags & PG_FICTITIOUS) != 0)
2369 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2371 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2372 PMAP_LOCK(pv->pv_pmap);
2373 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL)
2374 if (PTE_ISVALID(pte) && PTE_ISWIRED(pte))
2376 PMAP_UNLOCK(pv->pv_pmap);
2383 mmu_booke_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2389 * This currently does not work for entries that
2390 * overlap TLB1 entries.
2392 for (i = 0; i < tlb1_idx; i ++) {
2393 if (tlb1_iomapped(i, pa, size, &va) == 0)
2401 mmu_booke_dumpsys_map(mmu_t mmu, struct pmap_md *md, vm_size_t ofs,
2408 /* Raw physical memory dumps don't have a virtual address. */
2409 if (md->md_vaddr == ~0UL) {
2410 /* We always map a 256MB page at 256M. */
2411 gran = 256 * 1024 * 1024;
2412 pa = md->md_paddr + ofs;
2413 ppa = pa & ~(gran - 1);
2416 tlb1_set_entry(va, ppa, gran, _TLB_ENTRY_IO);
2417 if (*sz > (gran - ofs))
2422 /* Minidumps are based on virtual memory addresses. */
2423 va = md->md_vaddr + ofs;
2424 if (va >= kernstart + kernsize) {
2425 gran = PAGE_SIZE - (va & PAGE_MASK);
2433 mmu_booke_dumpsys_unmap(mmu_t mmu, struct pmap_md *md, vm_size_t ofs,
2437 /* Raw physical memory dumps don't have a virtual address. */
2438 if (md->md_vaddr == ~0UL) {
2440 tlb1[tlb1_idx].mas1 = 0;
2441 tlb1[tlb1_idx].mas2 = 0;
2442 tlb1[tlb1_idx].mas3 = 0;
2443 tlb1_write_entry(tlb1_idx);
2447 /* Minidumps are based on virtual memory addresses. */
2448 /* Nothing to do... */
2452 mmu_booke_scan_md(mmu_t mmu, struct pmap_md *prev)
2454 static struct pmap_md md;
2455 struct bi_mem_region *mr;
2459 if (dumpsys_minidump) {
2460 md.md_paddr = ~0UL; /* Minidumps use virtual addresses. */
2462 /* 1st: kernel .data and .bss. */
2464 md.md_vaddr = trunc_page((uintptr_t)_etext);
2465 md.md_size = round_page((uintptr_t)_end) - md.md_vaddr;
2468 switch (prev->md_index) {
2470 /* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2472 md.md_vaddr = data_start;
2473 md.md_size = data_end - data_start;
2476 /* 3rd: kernel VM. */
2477 va = prev->md_vaddr + prev->md_size;
2478 /* Find start of next chunk (from va). */
2479 while (va < virtual_end) {
2480 /* Don't dump the buffer cache. */
2481 if (va >= kmi.buffer_sva &&
2482 va < kmi.buffer_eva) {
2483 va = kmi.buffer_eva;
2486 pte = pte_find(mmu, kernel_pmap, va);
2487 if (pte != NULL && PTE_ISVALID(pte))
2491 if (va < virtual_end) {
2494 /* Find last page in chunk. */
2495 while (va < virtual_end) {
2496 /* Don't run into the buffer cache. */
2497 if (va == kmi.buffer_sva)
2499 pte = pte_find(mmu, kernel_pmap, va);
2500 if (pte == NULL || !PTE_ISVALID(pte))
2504 md.md_size = va - md.md_vaddr;
2512 } else { /* minidumps */
2515 /* first physical chunk. */
2516 md.md_paddr = mr->mem_base;
2517 md.md_size = mr->mem_size;
2520 } else if (md.md_index < bootinfo->bi_mem_reg_no) {
2521 md.md_paddr = mr[md.md_index].mem_base;
2522 md.md_size = mr[md.md_index].mem_size;
2526 /* There's no next physical chunk. */
2535 * Map a set of physical memory pages into the kernel virtual address space.
2536 * Return a pointer to where it is mapped. This routine is intended to be used
2537 * for mapping device memory, NOT real memory.
2540 mmu_booke_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size)
2546 va = (pa >= 0x80000000) ? pa : (0xe2000000 + pa);
2550 sz = 1 << (ilog2(size) & ~1);
2552 printf("Wiring VA=%x to PA=%x (size=%x), "
2553 "using TLB1[%d]\n", va, pa, sz, tlb1_idx);
2554 tlb1_set_entry(va, pa, sz, _TLB_ENTRY_IO);
2564 * 'Unmap' a range mapped by mmu_booke_mapdev().
2567 mmu_booke_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
2569 vm_offset_t base, offset;
2572 * Unmap only if this is inside kernel virtual space.
2574 if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
2575 base = trunc_page(va);
2576 offset = va & PAGE_MASK;
2577 size = roundup(offset + size, PAGE_SIZE);
2578 kmem_free(kernel_map, base, size);
2583 * mmu_booke_object_init_pt preloads the ptes for a given object into the
2584 * specified pmap. This eliminates the blast of soft faults on process startup
2585 * and immediately after an mmap.
2588 mmu_booke_object_init_pt(mmu_t mmu, pmap_t pmap, vm_offset_t addr,
2589 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2592 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2593 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2594 ("mmu_booke_object_init_pt: non-device object"));
2598 * Perform the pmap work for mincore.
2601 mmu_booke_mincore(mmu_t mmu, pmap_t pmap, vm_offset_t addr)
2608 /**************************************************************************/
2610 /**************************************************************************/
2613 * Allocate a TID. If necessary, steal one from someone else.
2614 * The new TID is flushed from the TLB before returning.
2617 tid_alloc(pmap_t pmap)
2622 KASSERT((pmap != kernel_pmap), ("tid_alloc: kernel pmap"));
2624 CTR2(KTR_PMAP, "%s: s (pmap = %p)", __func__, pmap);
2626 thiscpu = PCPU_GET(cpuid);
2628 tid = PCPU_GET(tid_next);
2631 PCPU_SET(tid_next, tid + 1);
2633 /* If we are stealing TID then clear the relevant pmap's field */
2634 if (tidbusy[thiscpu][tid] != NULL) {
2636 CTR2(KTR_PMAP, "%s: warning: stealing tid %d", __func__, tid);
2638 tidbusy[thiscpu][tid]->pm_tid[thiscpu] = TID_NONE;
2640 /* Flush all entries from TLB0 matching this TID. */
2644 tidbusy[thiscpu][tid] = pmap;
2645 pmap->pm_tid[thiscpu] = tid;
2646 __asm __volatile("msync; isync");
2648 CTR3(KTR_PMAP, "%s: e (%02d next = %02d)", __func__, tid,
2649 PCPU_GET(tid_next));
2654 /**************************************************************************/
2656 /**************************************************************************/
2659 tlb_print_entry(int i, uint32_t mas1, uint32_t mas2, uint32_t mas3,
2669 if (mas1 & MAS1_VALID)
2674 if (mas1 & MAS1_IPROT)
2679 as = (mas1 & MAS1_TS_MASK) ? 1 : 0;
2680 tid = MAS1_GETTID(mas1);
2682 tsize = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
2685 size = tsize2size(tsize);
2687 debugf("%3d: (%s) [AS=%d] "
2688 "sz = 0x%08x tsz = %d tid = %d mas1 = 0x%08x "
2689 "mas2(va) = 0x%08x mas3(pa) = 0x%08x mas7 = 0x%08x\n",
2690 i, desc, as, size, tsize, tid, mas1, mas2, mas3, mas7);
2693 /* Convert TLB0 va and way number to tlb0[] table index. */
2694 static inline unsigned int
2695 tlb0_tableidx(vm_offset_t va, unsigned int way)
2699 idx = (way * TLB0_ENTRIES_PER_WAY);
2700 idx += (va & MAS2_TLB0_ENTRY_IDX_MASK) >> MAS2_TLB0_ENTRY_IDX_SHIFT;
2705 * Invalidate TLB0 entry.
2708 tlb0_flush_entry(vm_offset_t va)
2711 CTR2(KTR_PMAP, "%s: s va=0x%08x", __func__, va);
2713 mtx_assert(&tlbivax_mutex, MA_OWNED);
2715 __asm __volatile("tlbivax 0, %0" :: "r"(va & MAS2_EPN_MASK));
2716 __asm __volatile("isync; msync");
2717 __asm __volatile("tlbsync; msync");
2719 CTR1(KTR_PMAP, "%s: e", __func__);
2722 /* Print out contents of the MAS registers for each TLB0 entry */
2724 tlb0_print_tlbentries(void)
2726 uint32_t mas0, mas1, mas2, mas3, mas7;
2727 int entryidx, way, idx;
2729 debugf("TLB0 entries:\n");
2730 for (way = 0; way < TLB0_WAYS; way ++)
2731 for (entryidx = 0; entryidx < TLB0_ENTRIES_PER_WAY; entryidx++) {
2733 mas0 = MAS0_TLBSEL(0) | MAS0_ESEL(way);
2734 mtspr(SPR_MAS0, mas0);
2735 __asm __volatile("isync");
2737 mas2 = entryidx << MAS2_TLB0_ENTRY_IDX_SHIFT;
2738 mtspr(SPR_MAS2, mas2);
2740 __asm __volatile("isync; tlbre");
2742 mas1 = mfspr(SPR_MAS1);
2743 mas2 = mfspr(SPR_MAS2);
2744 mas3 = mfspr(SPR_MAS3);
2745 mas7 = mfspr(SPR_MAS7);
2747 idx = tlb0_tableidx(mas2, way);
2748 tlb_print_entry(idx, mas1, mas2, mas3, mas7);
2752 /**************************************************************************/
2754 /**************************************************************************/
2757 * TLB1 mapping notes:
2760 * TLB1[1] Kernel text and data.
2761 * TLB1[2-15] Additional kernel text and data mappings (if required), PCI
2762 * windows, other devices mappings.
2766 * Write given entry to TLB1 hardware.
2767 * Use 32 bit pa, clear 4 high-order bits of RPN (mas7).
2770 tlb1_write_entry(unsigned int idx)
2772 uint32_t mas0, mas7;
2774 //debugf("tlb1_write_entry: s\n");
2776 /* Clear high order RPN bits */
2780 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(idx);
2781 //debugf("tlb1_write_entry: mas0 = 0x%08x\n", mas0);
2783 mtspr(SPR_MAS0, mas0);
2784 __asm __volatile("isync");
2785 mtspr(SPR_MAS1, tlb1[idx].mas1);
2786 __asm __volatile("isync");
2787 mtspr(SPR_MAS2, tlb1[idx].mas2);
2788 __asm __volatile("isync");
2789 mtspr(SPR_MAS3, tlb1[idx].mas3);
2790 __asm __volatile("isync");
2791 mtspr(SPR_MAS7, mas7);
2792 __asm __volatile("isync; tlbwe; isync; msync");
2794 //debugf("tlb1_write_entry: e\n");
2798 * Return the largest uint value log such that 2^log <= num.
2801 ilog2(unsigned int num)
2805 __asm ("cntlzw %0, %1" : "=r" (lz) : "r" (num));
2810 * Convert TLB TSIZE value to mapped region size.
2813 tsize2size(unsigned int tsize)
2818 * size = 4^tsize * 2^10 = 2^(2 * tsize - 10)
2821 return ((1 << (2 * tsize)) * 1024);
2825 * Convert region size (must be power of 4) to TLB TSIZE value.
2828 size2tsize(vm_size_t size)
2831 return (ilog2(size) / 2 - 5);
2835 * Register permanent kernel mapping in TLB1.
2837 * Entries are created starting from index 0 (current free entry is
2838 * kept in tlb1_idx) and are not supposed to be invalidated.
2841 tlb1_set_entry(vm_offset_t va, vm_offset_t pa, vm_size_t size,
2847 if (tlb1_idx >= TLB1_ENTRIES) {
2848 printf("tlb1_set_entry: TLB1 full!\n");
2852 /* Convert size to TSIZE */
2853 tsize = size2tsize(size);
2855 tid = (TID_KERNEL << MAS1_TID_SHIFT) & MAS1_TID_MASK;
2856 /* XXX TS is hard coded to 0 for now as we only use single address space */
2857 ts = (0 << MAS1_TS_SHIFT) & MAS1_TS_MASK;
2859 /* XXX LOCK tlb1[] */
2861 tlb1[tlb1_idx].mas1 = MAS1_VALID | MAS1_IPROT | ts | tid;
2862 tlb1[tlb1_idx].mas1 |= ((tsize << MAS1_TSIZE_SHIFT) & MAS1_TSIZE_MASK);
2863 tlb1[tlb1_idx].mas2 = (va & MAS2_EPN_MASK) | flags;
2865 /* Set supervisor RWX permission bits */
2866 tlb1[tlb1_idx].mas3 = (pa & MAS3_RPN) | MAS3_SR | MAS3_SW | MAS3_SX;
2868 tlb1_write_entry(tlb1_idx++);
2870 /* XXX UNLOCK tlb1[] */
2873 * XXX in general TLB1 updates should be propagated between CPUs,
2874 * since current design assumes to have the same TLB1 set-up on all
2881 tlb1_entry_size_cmp(const void *a, const void *b)
2883 const vm_size_t *sza;
2884 const vm_size_t *szb;
2890 else if (*sza < *szb)
2897 * Map in contiguous RAM region into the TLB1 using maximum of
2898 * KERNEL_REGION_MAX_TLB_ENTRIES entries.
2900 * If necessary round up last entry size and return total size
2901 * used by all allocated entries.
2904 tlb1_mapin_region(vm_offset_t va, vm_offset_t pa, vm_size_t size)
2906 vm_size_t entry_size[KERNEL_REGION_MAX_TLB_ENTRIES];
2907 vm_size_t mapped_size, sz, esz;
2911 CTR4(KTR_PMAP, "%s: region size = 0x%08x va = 0x%08x pa = 0x%08x",
2912 __func__, size, va, pa);
2916 memset(entry_size, 0, sizeof(entry_size));
2918 /* Calculate entry sizes. */
2919 for (i = 0; i < KERNEL_REGION_MAX_TLB_ENTRIES && sz > 0; i++) {
2921 /* Largest region that is power of 4 and fits within size */
2922 log = ilog2(sz) / 2;
2923 esz = 1 << (2 * log);
2925 /* If this is last entry cover remaining size. */
2926 if (i == KERNEL_REGION_MAX_TLB_ENTRIES - 1) {
2931 entry_size[i] = esz;
2939 /* Sort entry sizes, required to get proper entry address alignment. */
2940 qsort(entry_size, KERNEL_REGION_MAX_TLB_ENTRIES,
2941 sizeof(vm_size_t), tlb1_entry_size_cmp);
2943 /* Load TLB1 entries. */
2944 for (i = 0; i < KERNEL_REGION_MAX_TLB_ENTRIES; i++) {
2945 esz = entry_size[i];
2949 CTR5(KTR_PMAP, "%s: entry %d: sz = 0x%08x (va = 0x%08x "
2950 "pa = 0x%08x)", __func__, tlb1_idx, esz, va, pa);
2952 tlb1_set_entry(va, pa, esz, _TLB_ENTRY_MEM);
2958 CTR3(KTR_PMAP, "%s: mapped size 0x%08x (wasted space 0x%08x)",
2959 __func__, mapped_size, mapped_size - size);
2961 return (mapped_size);
2965 * TLB1 initialization routine, to be called after the very first
2966 * assembler level setup done in locore.S.
2969 tlb1_init(vm_offset_t ccsrbar)
2973 /* TLB1[1] is used to map the kernel. Save that entry. */
2974 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(1);
2975 mtspr(SPR_MAS0, mas0);
2976 __asm __volatile("isync; tlbre");
2978 tlb1[1].mas1 = mfspr(SPR_MAS1);
2979 tlb1[1].mas2 = mfspr(SPR_MAS2);
2980 tlb1[1].mas3 = mfspr(SPR_MAS3);
2982 /* Map in CCSRBAR in TLB1[0] */
2984 tlb1_set_entry(CCSRBAR_VA, ccsrbar, CCSRBAR_SIZE, _TLB_ENTRY_IO);
2986 * Set the next available TLB1 entry index. Note TLB[1] is reserved
2987 * for initial mapping of kernel text+data, which was set early in
2988 * locore, we need to skip this [busy] entry.
2992 /* Setup TLB miss defaults */
2993 set_mas4_defaults();
2997 * Setup MAS4 defaults.
2998 * These values are loaded to MAS0-2 on a TLB miss.
3001 set_mas4_defaults(void)
3005 /* Defaults: TLB0, PID0, TSIZED=4K */
3006 mas4 = MAS4_TLBSELD0;
3007 mas4 |= (TLB_SIZE_4K << MAS4_TSIZED_SHIFT) & MAS4_TSIZED_MASK;
3011 mtspr(SPR_MAS4, mas4);
3012 __asm __volatile("isync");
3016 * Print out contents of the MAS registers for each TLB1 entry
3019 tlb1_print_tlbentries(void)
3021 uint32_t mas0, mas1, mas2, mas3, mas7;
3024 debugf("TLB1 entries:\n");
3025 for (i = 0; i < TLB1_ENTRIES; i++) {
3027 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(i);
3028 mtspr(SPR_MAS0, mas0);
3030 __asm __volatile("isync; tlbre");
3032 mas1 = mfspr(SPR_MAS1);
3033 mas2 = mfspr(SPR_MAS2);
3034 mas3 = mfspr(SPR_MAS3);
3035 mas7 = mfspr(SPR_MAS7);
3037 tlb_print_entry(i, mas1, mas2, mas3, mas7);
3042 * Print out contents of the in-ram tlb1 table.
3045 tlb1_print_entries(void)
3049 debugf("tlb1[] table entries:\n");
3050 for (i = 0; i < TLB1_ENTRIES; i++)
3051 tlb_print_entry(i, tlb1[i].mas1, tlb1[i].mas2, tlb1[i].mas3, 0);
3055 * Return 0 if the physical IO range is encompassed by one of the
3056 * the TLB1 entries, otherwise return related error code.
3059 tlb1_iomapped(int i, vm_paddr_t pa, vm_size_t size, vm_offset_t *va)
3062 vm_paddr_t pa_start;
3064 unsigned int entry_tsize;
3065 vm_size_t entry_size;
3067 *va = (vm_offset_t)NULL;
3069 /* Skip invalid entries */
3070 if (!(tlb1[i].mas1 & MAS1_VALID))
3074 * The entry must be cache-inhibited, guarded, and r/w
3075 * so it can function as an i/o page
3077 prot = tlb1[i].mas2 & (MAS2_I | MAS2_G);
3078 if (prot != (MAS2_I | MAS2_G))
3081 prot = tlb1[i].mas3 & (MAS3_SR | MAS3_SW);
3082 if (prot != (MAS3_SR | MAS3_SW))
3085 /* The address should be within the entry range. */
3086 entry_tsize = (tlb1[i].mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3087 KASSERT((entry_tsize), ("tlb1_iomapped: invalid entry tsize"));
3089 entry_size = tsize2size(entry_tsize);
3090 pa_start = tlb1[i].mas3 & MAS3_RPN;
3091 pa_end = pa_start + entry_size - 1;
3093 if ((pa < pa_start) || ((pa + size) > pa_end))
3096 /* Return virtual address of this mapping. */
3097 *va = (tlb1[i].mas2 & MAS2_EPN_MASK) + (pa - pa_start);