2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2007-2009 Semihalf, Rafal Jaworowski <raj@semihalf.com>
5 * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Some hw specific parts of this pmap were derived or influenced
29 * by NetBSD's ibm4xx pmap module. More generic code is shared with
30 * a few other pmap modules from the FreeBSD tree.
36 * Kernel and user threads run within one common virtual address space
40 * Virtual address space layout:
41 * -----------------------------
42 * 0x0000_0000 - 0x7fff_ffff : user process
43 * 0x8000_0000 - 0xbfff_ffff : pmap_mapdev()-ed area (PCI/PCIE etc.)
44 * 0xc000_0000 - 0xc0ff_ffff : kernel reserved
45 * 0xc000_0000 - data_end : kernel code+data, env, metadata etc.
46 * 0xc100_0000 - 0xffff_ffff : KVA
47 * 0xc100_0000 - 0xc100_3fff : reserved for page zero/copy
48 * 0xc100_4000 - 0xc200_3fff : reserved for ptbl bufs
49 * 0xc200_4000 - 0xc200_8fff : guard page + kstack0
50 * 0xc200_9000 - 0xfeef_ffff : actual free KVA space
53 * Virtual address space layout:
54 * -----------------------------
55 * 0x0000_0000_0000_0000 - 0xbfff_ffff_ffff_ffff : user process
56 * 0x0000_0000_0000_0000 - 0x8fff_ffff_ffff_ffff : text, data, heap, maps, libraries
57 * 0x9000_0000_0000_0000 - 0xafff_ffff_ffff_ffff : mmio region
58 * 0xb000_0000_0000_0000 - 0xbfff_ffff_ffff_ffff : stack
59 * 0xc000_0000_0000_0000 - 0xcfff_ffff_ffff_ffff : kernel reserved
60 * 0xc000_0000_0000_0000 - endkernel-1 : kernel code & data
61 * endkernel - msgbufp-1 : flat device tree
62 * msgbufp - kernel_pdir-1 : message buffer
63 * kernel_pdir - kernel_pp2d-1 : kernel page directory
64 * kernel_pp2d - . : kernel pointers to page directory
65 * pmap_zero_copy_min - crashdumpmap-1 : reserved for page zero/copy
66 * crashdumpmap - ptbl_buf_pool_vabase-1 : reserved for ptbl bufs
67 * ptbl_buf_pool_vabase - virtual_avail-1 : user page directories and page tables
68 * virtual_avail - 0xcfff_ffff_ffff_ffff : actual free KVA space
69 * 0xd000_0000_0000_0000 - 0xdfff_ffff_ffff_ffff : coprocessor region
70 * 0xe000_0000_0000_0000 - 0xefff_ffff_ffff_ffff : mmio region
71 * 0xf000_0000_0000_0000 - 0xffff_ffff_ffff_ffff : direct map
72 * 0xf000_0000_0000_0000 - +Maxmem : physmem map
73 * - 0xffff_ffff_ffff_ffff : device direct map
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
80 #include "opt_kstack_pages.h"
82 #include <sys/param.h>
84 #include <sys/malloc.h>
88 #include <sys/queue.h>
89 #include <sys/systm.h>
90 #include <sys/kernel.h>
91 #include <sys/kerneldump.h>
92 #include <sys/linker.h>
93 #include <sys/msgbuf.h>
95 #include <sys/mutex.h>
96 #include <sys/rwlock.h>
97 #include <sys/sched.h>
99 #include <sys/vmmeter.h>
102 #include <vm/vm_param.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_kern.h>
105 #include <vm/vm_pageout.h>
106 #include <vm/vm_extern.h>
107 #include <vm/vm_object.h>
108 #include <vm/vm_map.h>
109 #include <vm/vm_pager.h>
110 #include <vm/vm_phys.h>
111 #include <vm/vm_pagequeue.h>
112 #include <vm/vm_dumpset.h>
115 #include <machine/_inttypes.h>
116 #include <machine/cpu.h>
117 #include <machine/pcb.h>
118 #include <machine/platform.h>
120 #include <machine/tlb.h>
121 #include <machine/spr.h>
122 #include <machine/md_var.h>
123 #include <machine/mmuvar.h>
124 #include <machine/pmap.h>
125 #include <machine/pte.h>
129 #define SPARSE_MAPDEV
131 /* Use power-of-two mappings in mmu_booke_mapdev(), to save entries. */
132 #define POW2_MAPPINGS
135 #define debugf(fmt, args...) printf(fmt, ##args)
137 #define debugf(fmt, args...)
141 #define PRI0ptrX "016lx"
143 #define PRI0ptrX "08x"
146 #define TODO panic("%s: not implemented", __func__);
148 extern unsigned char _etext[];
149 extern unsigned char _end[];
151 extern uint32_t *bootinfo;
154 vm_offset_t kernstart;
157 /* Message buffer and tables. */
158 static vm_offset_t data_start;
159 static vm_size_t data_end;
161 /* Phys/avail memory regions. */
162 static struct mem_region *availmem_regions;
163 static int availmem_regions_sz;
164 static struct mem_region *physmem_regions;
165 static int physmem_regions_sz;
167 #ifndef __powerpc64__
168 /* Reserved KVA space and mutex for mmu_booke_zero_page. */
169 static vm_offset_t zero_page_va;
170 static struct mtx zero_page_mutex;
172 /* Reserved KVA space and mutex for mmu_booke_copy_page. */
173 static vm_offset_t copy_page_src_va;
174 static vm_offset_t copy_page_dst_va;
175 static struct mtx copy_page_mutex;
178 static struct mtx tlbivax_mutex;
180 /**************************************************************************/
182 /**************************************************************************/
184 static int mmu_booke_enter_locked(pmap_t, vm_offset_t, vm_page_t,
185 vm_prot_t, u_int flags, int8_t psind);
187 unsigned int kptbl_min; /* Index of the first kernel ptbl. */
188 static uma_zone_t ptbl_root_zone;
191 * If user pmap is processed with mmu_booke_remove and the resident count
192 * drops to 0, there are no more pages to remove, so we need not continue.
194 #define PMAP_REMOVE_DONE(pmap) \
195 ((pmap) != kernel_pmap && (pmap)->pm_stats.resident_count == 0)
197 #if defined(COMPAT_FREEBSD32) || !defined(__powerpc64__)
198 extern int elf32_nxstack;
201 /**************************************************************************/
202 /* TLB and TID handling */
203 /**************************************************************************/
205 /* Translation ID busy table */
206 static volatile pmap_t tidbusy[MAXCPU][TID_MAX + 1];
209 * TLB0 capabilities (entry, way numbers etc.). These can vary between e500
210 * core revisions and should be read from h/w registers during early config.
212 uint32_t tlb0_entries;
214 uint32_t tlb0_entries_per_way;
215 uint32_t tlb1_entries;
217 #define TLB0_ENTRIES (tlb0_entries)
218 #define TLB0_WAYS (tlb0_ways)
219 #define TLB0_ENTRIES_PER_WAY (tlb0_entries_per_way)
221 #define TLB1_ENTRIES (tlb1_entries)
223 static tlbtid_t tid_alloc(struct pmap *);
227 static void tlb_print_entry(int, uint32_t, uint64_t, uint32_t, uint32_t);
229 static void tlb_print_entry(int, uint32_t, uint32_t, uint32_t, uint32_t);
233 static void tlb1_read_entry(tlb_entry_t *, unsigned int);
234 static void tlb1_write_entry(tlb_entry_t *, unsigned int);
235 static int tlb1_iomapped(int, vm_paddr_t, vm_size_t, vm_offset_t *);
236 static vm_size_t tlb1_mapin_region(vm_offset_t, vm_paddr_t, vm_size_t, int);
238 static __inline uint32_t tlb_calc_wimg(vm_paddr_t pa, vm_memattr_t ma);
240 static vm_size_t tsize2size(unsigned int);
241 static unsigned int size2tsize(vm_size_t);
242 static unsigned long ilog2(unsigned long);
244 static void set_mas4_defaults(void);
246 static inline void tlb0_flush_entry(vm_offset_t);
247 static inline unsigned int tlb0_tableidx(vm_offset_t, unsigned int);
249 /**************************************************************************/
250 /* Page table management */
251 /**************************************************************************/
253 static struct rwlock_padalign pvh_global_lock;
255 /* Data for the pv entry allocation mechanism */
256 static uma_zone_t pvzone;
257 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
259 #define PV_ENTRY_ZONE_MIN 2048 /* min pv entries in uma zone */
261 #ifndef PMAP_SHPGPERPROC
262 #define PMAP_SHPGPERPROC 200
265 static vm_paddr_t pte_vatopa(pmap_t, vm_offset_t);
266 static int pte_enter(pmap_t, vm_page_t, vm_offset_t, uint32_t, boolean_t);
267 static int pte_remove(pmap_t, vm_offset_t, uint8_t);
268 static pte_t *pte_find(pmap_t, vm_offset_t);
269 static void kernel_pte_alloc(vm_offset_t, vm_offset_t);
271 static pv_entry_t pv_alloc(void);
272 static void pv_free(pv_entry_t);
273 static void pv_insert(pmap_t, vm_offset_t, vm_page_t);
274 static void pv_remove(pmap_t, vm_offset_t, vm_page_t);
276 static void booke_pmap_init_qpages(void);
278 static inline void tlb_miss_lock(void);
279 static inline void tlb_miss_unlock(void);
282 extern tlb_entry_t __boot_tlb1[];
283 void pmap_bootstrap_ap(volatile uint32_t *);
287 * Kernel MMU interface
289 static void mmu_booke_clear_modify(vm_page_t);
290 static void mmu_booke_copy(pmap_t, pmap_t, vm_offset_t,
291 vm_size_t, vm_offset_t);
292 static void mmu_booke_copy_page(vm_page_t, vm_page_t);
293 static void mmu_booke_copy_pages(vm_page_t *,
294 vm_offset_t, vm_page_t *, vm_offset_t, int);
295 static int mmu_booke_enter(pmap_t, vm_offset_t, vm_page_t,
296 vm_prot_t, u_int flags, int8_t psind);
297 static void mmu_booke_enter_object(pmap_t, vm_offset_t, vm_offset_t,
298 vm_page_t, vm_prot_t);
299 static void mmu_booke_enter_quick(pmap_t, vm_offset_t, vm_page_t,
301 static vm_paddr_t mmu_booke_extract(pmap_t, vm_offset_t);
302 static vm_page_t mmu_booke_extract_and_hold(pmap_t, vm_offset_t,
304 static void mmu_booke_init(void);
305 static boolean_t mmu_booke_is_modified(vm_page_t);
306 static boolean_t mmu_booke_is_prefaultable(pmap_t, vm_offset_t);
307 static boolean_t mmu_booke_is_referenced(vm_page_t);
308 static int mmu_booke_ts_referenced(vm_page_t);
309 static vm_offset_t mmu_booke_map(vm_offset_t *, vm_paddr_t, vm_paddr_t,
311 static int mmu_booke_mincore(pmap_t, vm_offset_t,
313 static void mmu_booke_object_init_pt(pmap_t, vm_offset_t,
314 vm_object_t, vm_pindex_t, vm_size_t);
315 static boolean_t mmu_booke_page_exists_quick(pmap_t, vm_page_t);
316 static void mmu_booke_page_init(vm_page_t);
317 static int mmu_booke_page_wired_mappings(vm_page_t);
318 static int mmu_booke_pinit(pmap_t);
319 static void mmu_booke_pinit0(pmap_t);
320 static void mmu_booke_protect(pmap_t, vm_offset_t, vm_offset_t,
322 static void mmu_booke_qenter(vm_offset_t, vm_page_t *, int);
323 static void mmu_booke_qremove(vm_offset_t, int);
324 static void mmu_booke_release(pmap_t);
325 static void mmu_booke_remove(pmap_t, vm_offset_t, vm_offset_t);
326 static void mmu_booke_remove_all(vm_page_t);
327 static void mmu_booke_remove_write(vm_page_t);
328 static void mmu_booke_unwire(pmap_t, vm_offset_t, vm_offset_t);
329 static void mmu_booke_zero_page(vm_page_t);
330 static void mmu_booke_zero_page_area(vm_page_t, int, int);
331 static void mmu_booke_activate(struct thread *);
332 static void mmu_booke_deactivate(struct thread *);
333 static void mmu_booke_bootstrap(vm_offset_t, vm_offset_t);
334 static void *mmu_booke_mapdev(vm_paddr_t, vm_size_t);
335 static void *mmu_booke_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t);
336 static void mmu_booke_unmapdev(vm_offset_t, vm_size_t);
337 static vm_paddr_t mmu_booke_kextract(vm_offset_t);
338 static void mmu_booke_kenter(vm_offset_t, vm_paddr_t);
339 static void mmu_booke_kenter_attr(vm_offset_t, vm_paddr_t, vm_memattr_t);
340 static void mmu_booke_kremove(vm_offset_t);
341 static boolean_t mmu_booke_dev_direct_mapped(vm_paddr_t, vm_size_t);
342 static void mmu_booke_sync_icache(pmap_t, vm_offset_t,
344 static void mmu_booke_dumpsys_map(vm_paddr_t pa, size_t,
346 static void mmu_booke_dumpsys_unmap(vm_paddr_t pa, size_t,
348 static void mmu_booke_scan_init(void);
349 static vm_offset_t mmu_booke_quick_enter_page(vm_page_t m);
350 static void mmu_booke_quick_remove_page(vm_offset_t addr);
351 static int mmu_booke_change_attr(vm_offset_t addr,
352 vm_size_t sz, vm_memattr_t mode);
353 static int mmu_booke_decode_kernel_ptr(vm_offset_t addr,
354 int *is_user, vm_offset_t *decoded_addr);
355 static void mmu_booke_page_array_startup(long);
356 static boolean_t mmu_booke_page_is_mapped(vm_page_t m);
357 static bool mmu_booke_ps_enabled(pmap_t pmap);
359 static struct pmap_funcs mmu_booke_methods = {
360 /* pmap dispatcher interface */
361 .clear_modify = mmu_booke_clear_modify,
362 .copy = mmu_booke_copy,
363 .copy_page = mmu_booke_copy_page,
364 .copy_pages = mmu_booke_copy_pages,
365 .enter = mmu_booke_enter,
366 .enter_object = mmu_booke_enter_object,
367 .enter_quick = mmu_booke_enter_quick,
368 .extract = mmu_booke_extract,
369 .extract_and_hold = mmu_booke_extract_and_hold,
370 .init = mmu_booke_init,
371 .is_modified = mmu_booke_is_modified,
372 .is_prefaultable = mmu_booke_is_prefaultable,
373 .is_referenced = mmu_booke_is_referenced,
374 .ts_referenced = mmu_booke_ts_referenced,
375 .map = mmu_booke_map,
376 .mincore = mmu_booke_mincore,
377 .object_init_pt = mmu_booke_object_init_pt,
378 .page_exists_quick = mmu_booke_page_exists_quick,
379 .page_init = mmu_booke_page_init,
380 .page_wired_mappings = mmu_booke_page_wired_mappings,
381 .pinit = mmu_booke_pinit,
382 .pinit0 = mmu_booke_pinit0,
383 .protect = mmu_booke_protect,
384 .qenter = mmu_booke_qenter,
385 .qremove = mmu_booke_qremove,
386 .release = mmu_booke_release,
387 .remove = mmu_booke_remove,
388 .remove_all = mmu_booke_remove_all,
389 .remove_write = mmu_booke_remove_write,
390 .sync_icache = mmu_booke_sync_icache,
391 .unwire = mmu_booke_unwire,
392 .zero_page = mmu_booke_zero_page,
393 .zero_page_area = mmu_booke_zero_page_area,
394 .activate = mmu_booke_activate,
395 .deactivate = mmu_booke_deactivate,
396 .quick_enter_page = mmu_booke_quick_enter_page,
397 .quick_remove_page = mmu_booke_quick_remove_page,
398 .page_array_startup = mmu_booke_page_array_startup,
399 .page_is_mapped = mmu_booke_page_is_mapped,
400 .ps_enabled = mmu_booke_ps_enabled,
402 /* Internal interfaces */
403 .bootstrap = mmu_booke_bootstrap,
404 .dev_direct_mapped = mmu_booke_dev_direct_mapped,
405 .mapdev = mmu_booke_mapdev,
406 .mapdev_attr = mmu_booke_mapdev_attr,
407 .kenter = mmu_booke_kenter,
408 .kenter_attr = mmu_booke_kenter_attr,
409 .kextract = mmu_booke_kextract,
410 .kremove = mmu_booke_kremove,
411 .unmapdev = mmu_booke_unmapdev,
412 .change_attr = mmu_booke_change_attr,
413 .decode_kernel_ptr = mmu_booke_decode_kernel_ptr,
415 /* dumpsys() support */
416 .dumpsys_map_chunk = mmu_booke_dumpsys_map,
417 .dumpsys_unmap_chunk = mmu_booke_dumpsys_unmap,
418 .dumpsys_pa_init = mmu_booke_scan_init,
421 MMU_DEF(booke_mmu, MMU_TYPE_BOOKE, mmu_booke_methods);
429 static vm_offset_t tlb1_map_base = VM_MAPDEV_BASE;
431 static __inline uint32_t
432 tlb_calc_wimg(vm_paddr_t pa, vm_memattr_t ma)
437 if (ma != VM_MEMATTR_DEFAULT) {
439 case VM_MEMATTR_UNCACHEABLE:
440 return (MAS2_I | MAS2_G);
441 case VM_MEMATTR_WRITE_COMBINING:
442 case VM_MEMATTR_WRITE_BACK:
443 case VM_MEMATTR_PREFETCHABLE:
445 case VM_MEMATTR_WRITE_THROUGH:
446 return (MAS2_W | MAS2_M);
447 case VM_MEMATTR_CACHEABLE:
453 * Assume the page is cache inhibited and access is guarded unless
454 * it's in our available memory array.
456 attrib = _TLB_ENTRY_IO;
457 for (i = 0; i < physmem_regions_sz; i++) {
458 if ((pa >= physmem_regions[i].mr_start) &&
459 (pa < (physmem_regions[i].mr_start +
460 physmem_regions[i].mr_size))) {
461 attrib = _TLB_ENTRY_MEM;
478 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
480 CTR3(KTR_PMAP, "%s: tlb miss LOCK of CPU=%d, "
481 "tlb_lock=%p", __func__, pc->pc_cpuid, pc->pc_booke.tlb_lock);
483 KASSERT((pc->pc_cpuid != PCPU_GET(cpuid)),
484 ("tlb_miss_lock: tried to lock self"));
486 tlb_lock(pc->pc_booke.tlb_lock);
488 CTR1(KTR_PMAP, "%s: locked", __func__);
495 tlb_miss_unlock(void)
503 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
505 CTR2(KTR_PMAP, "%s: tlb miss UNLOCK of CPU=%d",
506 __func__, pc->pc_cpuid);
508 tlb_unlock(pc->pc_booke.tlb_lock);
510 CTR1(KTR_PMAP, "%s: unlocked", __func__);
516 /* Return number of entries in TLB0. */
518 tlb0_get_tlbconf(void)
522 tlb0_cfg = mfspr(SPR_TLB0CFG);
523 tlb0_entries = tlb0_cfg & TLBCFG_NENTRY_MASK;
524 tlb0_ways = (tlb0_cfg & TLBCFG_ASSOC_MASK) >> TLBCFG_ASSOC_SHIFT;
525 tlb0_entries_per_way = tlb0_entries / tlb0_ways;
528 /* Return number of entries in TLB1. */
530 tlb1_get_tlbconf(void)
534 tlb1_cfg = mfspr(SPR_TLB1CFG);
535 tlb1_entries = tlb1_cfg & TLBCFG_NENTRY_MASK;
538 /**************************************************************************/
539 /* Page table related */
540 /**************************************************************************/
542 /* Allocate pv_entry structure. */
549 if (pv_entry_count > pv_entry_high_water)
550 pagedaemon_wakeup(0); /* XXX powerpc NUMA */
551 pv = uma_zalloc(pvzone, M_NOWAIT);
556 /* Free pv_entry structure. */
558 pv_free(pv_entry_t pve)
562 uma_zfree(pvzone, pve);
565 /* Allocate and initialize pv_entry structure. */
567 pv_insert(pmap_t pmap, vm_offset_t va, vm_page_t m)
571 //int su = (pmap == kernel_pmap);
572 //debugf("pv_insert: s (su = %d pmap = 0x%08x va = 0x%08x m = 0x%08x)\n", su,
573 // (u_int32_t)pmap, va, (u_int32_t)m);
577 panic("pv_insert: no pv entries!");
583 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
584 rw_assert(&pvh_global_lock, RA_WLOCKED);
586 TAILQ_INSERT_TAIL(&m->md.pv_list, pve, pv_link);
588 //debugf("pv_insert: e\n");
591 /* Destroy pv entry. */
593 pv_remove(pmap_t pmap, vm_offset_t va, vm_page_t m)
597 //int su = (pmap == kernel_pmap);
598 //debugf("pv_remove: s (su = %d pmap = 0x%08x va = 0x%08x)\n", su, (u_int32_t)pmap, va);
600 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
601 rw_assert(&pvh_global_lock, RA_WLOCKED);
604 TAILQ_FOREACH(pve, &m->md.pv_list, pv_link) {
605 if ((pmap == pve->pv_pmap) && (va == pve->pv_va)) {
606 /* remove from pv_list */
607 TAILQ_REMOVE(&m->md.pv_list, pve, pv_link);
608 if (TAILQ_EMPTY(&m->md.pv_list))
609 vm_page_aflag_clear(m, PGA_WRITEABLE);
611 /* free pv entry struct */
617 //debugf("pv_remove: e\n");
620 /**************************************************************************/
622 /**************************************************************************/
625 * This is called during booke_init, before the system is really initialized.
628 mmu_booke_bootstrap(vm_offset_t start, vm_offset_t kernelend)
630 vm_paddr_t phys_kernelend;
631 struct mem_region *mp, *mp1;
634 vm_paddr_t physsz, hwphyssz;
635 u_int phys_avail_count;
636 vm_size_t kstack0_sz;
637 vm_paddr_t kstack0_phys;
641 debugf("mmu_booke_bootstrap: entered\n");
643 /* Set interesting system properties */
649 #if defined(COMPAT_FREEBSD32) || !defined(__powerpc64__)
653 /* Initialize invalidation mutex */
654 mtx_init(&tlbivax_mutex, "tlbivax", NULL, MTX_SPIN);
656 /* Read TLB0 size and associativity. */
660 * Align kernel start and end address (kernel image).
661 * Note that kernel end does not necessarily relate to kernsize.
662 * kernsize is the size of the kernel that is actually mapped.
664 data_start = round_page(kernelend);
665 data_end = data_start;
667 /* Allocate the dynamic per-cpu area. */
668 dpcpu = (void *)data_end;
669 data_end += DPCPU_SIZE;
671 /* Allocate space for the message buffer. */
672 msgbufp = (struct msgbuf *)data_end;
673 data_end += msgbufsize;
674 debugf(" msgbufp at 0x%"PRI0ptrX" end = 0x%"PRI0ptrX"\n",
675 (uintptr_t)msgbufp, data_end);
677 data_end = round_page(data_end);
678 data_end = round_page(mmu_booke_alloc_kernel_pgtables(data_end));
680 /* Retrieve phys/avail mem regions */
681 mem_regions(&physmem_regions, &physmem_regions_sz,
682 &availmem_regions, &availmem_regions_sz);
684 if (PHYS_AVAIL_ENTRIES < availmem_regions_sz)
685 panic("mmu_booke_bootstrap: phys_avail too small");
687 data_end = round_page(data_end);
688 vm_page_array = (vm_page_t)data_end;
690 * Get a rough idea (upper bound) on the size of the page array. The
691 * vm_page_array will not handle any more pages than we have in the
692 * avail_regions array, and most likely much less.
695 for (mp = availmem_regions; mp->mr_size; mp++) {
698 sz = (round_page(sz) / (PAGE_SIZE + sizeof(struct vm_page)));
699 data_end += round_page(sz * sizeof(struct vm_page));
701 /* Pre-round up to 1MB. This wastes some space, but saves TLB entries */
702 data_end = roundup2(data_end, 1 << 20);
704 debugf(" data_end: 0x%"PRI0ptrX"\n", data_end);
705 debugf(" kernstart: %#zx\n", kernstart);
706 debugf(" kernsize: %#zx\n", kernsize);
708 if (data_end - kernstart > kernsize) {
709 kernsize += tlb1_mapin_region(kernstart + kernsize,
710 kernload + kernsize, (data_end - kernstart) - kernsize,
713 data_end = kernstart + kernsize;
714 debugf(" updated data_end: 0x%"PRI0ptrX"\n", data_end);
717 * Clear the structures - note we can only do it safely after the
718 * possible additional TLB1 translations are in place (above) so that
719 * all range up to the currently calculated 'data_end' is covered.
721 bzero((void *)data_start, data_end - data_start);
722 dpcpu_init(dpcpu, 0);
724 /*******************************************************/
725 /* Set the start and end of kva. */
726 /*******************************************************/
727 virtual_avail = round_page(data_end);
728 virtual_end = VM_MAX_KERNEL_ADDRESS;
730 #ifndef __powerpc64__
731 /* Allocate KVA space for page zero/copy operations. */
732 zero_page_va = virtual_avail;
733 virtual_avail += PAGE_SIZE;
734 copy_page_src_va = virtual_avail;
735 virtual_avail += PAGE_SIZE;
736 copy_page_dst_va = virtual_avail;
737 virtual_avail += PAGE_SIZE;
738 debugf("zero_page_va = 0x%"PRI0ptrX"\n", zero_page_va);
739 debugf("copy_page_src_va = 0x%"PRI0ptrX"\n", copy_page_src_va);
740 debugf("copy_page_dst_va = 0x%"PRI0ptrX"\n", copy_page_dst_va);
742 /* Initialize page zero/copy mutexes. */
743 mtx_init(&zero_page_mutex, "mmu_booke_zero_page", NULL, MTX_DEF);
744 mtx_init(©_page_mutex, "mmu_booke_copy_page", NULL, MTX_DEF);
746 /* Allocate KVA space for ptbl bufs. */
747 ptbl_buf_pool_vabase = virtual_avail;
748 virtual_avail += PTBL_BUFS * PTBL_PAGES * PAGE_SIZE;
749 debugf("ptbl_buf_pool_vabase = 0x%"PRI0ptrX" end = 0x%"PRI0ptrX"\n",
750 ptbl_buf_pool_vabase, virtual_avail);
753 /* Calculate corresponding physical addresses for the kernel region. */
754 phys_kernelend = kernload + kernsize;
755 debugf("kernel image and allocated data:\n");
756 debugf(" kernload = 0x%09jx\n", (uintmax_t)kernload);
757 debugf(" kernstart = 0x%"PRI0ptrX"\n", kernstart);
758 debugf(" kernsize = 0x%"PRI0ptrX"\n", kernsize);
761 * Remove kernel physical address range from avail regions list. Page
762 * align all regions. Non-page aligned memory isn't very interesting
763 * to us. Also, sort the entries for ascending addresses.
767 cnt = availmem_regions_sz;
768 debugf("processing avail regions:\n");
769 for (mp = availmem_regions; mp->mr_size; mp++) {
771 e = mp->mr_start + mp->mr_size;
772 debugf(" %09jx-%09jx -> ", (uintmax_t)s, (uintmax_t)e);
773 /* Check whether this region holds all of the kernel. */
774 if (s < kernload && e > phys_kernelend) {
775 availmem_regions[cnt].mr_start = phys_kernelend;
776 availmem_regions[cnt++].mr_size = e - phys_kernelend;
779 /* Look whether this regions starts within the kernel. */
780 if (s >= kernload && s < phys_kernelend) {
781 if (e <= phys_kernelend)
785 /* Now look whether this region ends within the kernel. */
786 if (e > kernload && e <= phys_kernelend) {
791 /* Now page align the start and size of the region. */
797 debugf("%09jx-%09jx = %jx\n",
798 (uintmax_t)s, (uintmax_t)e, (uintmax_t)sz);
800 /* Check whether some memory is left here. */
804 (cnt - (mp - availmem_regions)) * sizeof(*mp));
810 /* Do an insertion sort. */
811 for (mp1 = availmem_regions; mp1 < mp; mp1++)
812 if (s < mp1->mr_start)
815 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
823 availmem_regions_sz = cnt;
825 /*******************************************************/
826 /* Steal physical memory for kernel stack from the end */
827 /* of the first avail region */
828 /*******************************************************/
829 kstack0_sz = kstack_pages * PAGE_SIZE;
830 kstack0_phys = availmem_regions[0].mr_start +
831 availmem_regions[0].mr_size;
832 kstack0_phys -= kstack0_sz;
833 availmem_regions[0].mr_size -= kstack0_sz;
835 /*******************************************************/
836 /* Fill in phys_avail table, based on availmem_regions */
837 /*******************************************************/
838 phys_avail_count = 0;
841 TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
843 debugf("fill in phys_avail:\n");
844 for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
845 debugf(" region: 0x%jx - 0x%jx (0x%jx)\n",
846 (uintmax_t)availmem_regions[i].mr_start,
847 (uintmax_t)availmem_regions[i].mr_start +
848 availmem_regions[i].mr_size,
849 (uintmax_t)availmem_regions[i].mr_size);
852 (physsz + availmem_regions[i].mr_size) >= hwphyssz) {
853 debugf(" hw.physmem adjust\n");
854 if (physsz < hwphyssz) {
855 phys_avail[j] = availmem_regions[i].mr_start;
857 availmem_regions[i].mr_start +
861 dump_avail[j] = phys_avail[j];
862 dump_avail[j + 1] = phys_avail[j + 1];
867 phys_avail[j] = availmem_regions[i].mr_start;
868 phys_avail[j + 1] = availmem_regions[i].mr_start +
869 availmem_regions[i].mr_size;
871 physsz += availmem_regions[i].mr_size;
872 dump_avail[j] = phys_avail[j];
873 dump_avail[j + 1] = phys_avail[j + 1];
875 physmem = btoc(physsz);
877 /* Calculate the last available physical address. */
878 for (i = 0; phys_avail[i + 2] != 0; i += 2)
880 Maxmem = powerpc_btop(phys_avail[i + 1]);
882 debugf("Maxmem = 0x%08lx\n", Maxmem);
883 debugf("phys_avail_count = %d\n", phys_avail_count);
884 debugf("physsz = 0x%09jx physmem = %jd (0x%09jx)\n",
885 (uintmax_t)physsz, (uintmax_t)physmem, (uintmax_t)physmem);
889 * Map the physical memory contiguously in TLB1.
890 * Round so it fits into a single mapping.
892 tlb1_mapin_region(DMAP_BASE_ADDRESS, 0,
893 phys_avail[i + 1], _TLB_ENTRY_MEM);
896 /*******************************************************/
897 /* Initialize (statically allocated) kernel pmap. */
898 /*******************************************************/
899 PMAP_LOCK_INIT(kernel_pmap);
901 debugf("kernel_pmap = 0x%"PRI0ptrX"\n", (uintptr_t)kernel_pmap);
902 kernel_pte_alloc(virtual_avail, kernstart);
903 for (i = 0; i < MAXCPU; i++) {
904 kernel_pmap->pm_tid[i] = TID_KERNEL;
906 /* Initialize each CPU's tidbusy entry 0 with kernel_pmap */
907 tidbusy[i][TID_KERNEL] = kernel_pmap;
910 /* Mark kernel_pmap active on all CPUs */
911 CPU_FILL(&kernel_pmap->pm_active);
914 * Initialize the global pv list lock.
916 rw_init(&pvh_global_lock, "pmap pv global");
918 /*******************************************************/
920 /*******************************************************/
922 /* Enter kstack0 into kernel map, provide guard page */
923 kstack0 = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
924 thread0.td_kstack = kstack0;
925 thread0.td_kstack_pages = kstack_pages;
927 debugf("kstack_sz = 0x%08jx\n", (uintmax_t)kstack0_sz);
928 debugf("kstack0_phys at 0x%09jx - 0x%09jx\n",
929 (uintmax_t)kstack0_phys, (uintmax_t)kstack0_phys + kstack0_sz);
930 debugf("kstack0 at 0x%"PRI0ptrX" - 0x%"PRI0ptrX"\n",
931 kstack0, kstack0 + kstack0_sz);
933 virtual_avail += KSTACK_GUARD_PAGES * PAGE_SIZE + kstack0_sz;
934 for (i = 0; i < kstack_pages; i++) {
935 mmu_booke_kenter(kstack0, kstack0_phys);
936 kstack0 += PAGE_SIZE;
937 kstack0_phys += PAGE_SIZE;
940 pmap_bootstrapped = 1;
942 debugf("virtual_avail = %"PRI0ptrX"\n", virtual_avail);
943 debugf("virtual_end = %"PRI0ptrX"\n", virtual_end);
945 debugf("mmu_booke_bootstrap: exit\n");
955 /* Prepare TLB1 image for AP processors */
957 for (i = 0; i < TLB1_ENTRIES; i++) {
958 tlb1_read_entry(&tmp, i);
960 if ((tmp.mas1 & MAS1_VALID) && (tmp.mas2 & _TLB_ENTRY_SHARED))
961 memcpy(e++, &tmp, sizeof(tmp));
966 pmap_bootstrap_ap(volatile uint32_t *trcp __unused)
971 * Finish TLB1 configuration: the BSP already set up its TLB1 and we
972 * have the snapshot of its contents in the s/w __boot_tlb1[] table
973 * created by tlb1_ap_prep(), so use these values directly to
974 * (re)program AP's TLB1 hardware.
976 * Start at index 1 because index 0 has the kernel map.
978 for (i = 1; i < TLB1_ENTRIES; i++) {
979 if (__boot_tlb1[i].mas1 & MAS1_VALID)
980 tlb1_write_entry(&__boot_tlb1[i], i);
988 booke_pmap_init_qpages(void)
995 pc->pc_qmap_addr = kva_alloc(PAGE_SIZE);
996 if (pc->pc_qmap_addr == 0)
997 panic("pmap_init_qpages: unable to allocate KVA");
1001 SYSINIT(qpages_init, SI_SUB_CPU, SI_ORDER_ANY, booke_pmap_init_qpages, NULL);
1004 * Get the physical page address for the given pmap/virtual address.
1007 mmu_booke_extract(pmap_t pmap, vm_offset_t va)
1012 pa = pte_vatopa(pmap, va);
1019 * Extract the physical page address associated with the given
1020 * kernel virtual address.
1023 mmu_booke_kextract(vm_offset_t va)
1029 #ifdef __powerpc64__
1030 if (va >= DMAP_BASE_ADDRESS && va <= DMAP_MAX_ADDRESS)
1031 return (DMAP_TO_PHYS(va));
1034 if (va >= VM_MIN_KERNEL_ADDRESS && va <= VM_MAX_KERNEL_ADDRESS)
1035 p = pte_vatopa(kernel_pmap, va);
1038 /* Check TLB1 mappings */
1039 for (i = 0; i < TLB1_ENTRIES; i++) {
1040 tlb1_read_entry(&e, i);
1041 if (!(e.mas1 & MAS1_VALID))
1043 if (va >= e.virt && va < e.virt + e.size)
1044 return (e.phys + (va - e.virt));
1052 * Initialize the pmap module.
1053 * Called by vm_init, to initialize any structures that the pmap
1054 * system needs to map virtual memory.
1059 int shpgperproc = PMAP_SHPGPERPROC;
1062 * Initialize the address space (zone) for the pv entries. Set a
1063 * high water mark so that the system can recover from excessive
1064 * numbers of pv entries.
1066 pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
1067 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1069 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1070 pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1072 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1073 pv_entry_high_water = 9 * (pv_entry_max / 10);
1075 uma_zone_reserve_kva(pvzone, pv_entry_max);
1077 /* Pre-fill pvzone with initial number of pv entries. */
1078 uma_prealloc(pvzone, PV_ENTRY_ZONE_MIN);
1080 /* Create a UMA zone for page table roots. */
1081 ptbl_root_zone = uma_zcreate("pmap root", PMAP_ROOT_SIZE,
1082 NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, UMA_ZONE_VM);
1084 /* Initialize ptbl allocation. */
1089 * Map a list of wired pages into kernel virtual address space. This is
1090 * intended for temporary mappings which do not need page modification or
1091 * references recorded. Existing mappings in the region are overwritten.
1094 mmu_booke_qenter(vm_offset_t sva, vm_page_t *m, int count)
1099 while (count-- > 0) {
1100 mmu_booke_kenter(va, VM_PAGE_TO_PHYS(*m));
1107 * Remove page mappings from kernel virtual address space. Intended for
1108 * temporary mappings entered by mmu_booke_qenter.
1111 mmu_booke_qremove(vm_offset_t sva, int count)
1116 while (count-- > 0) {
1117 mmu_booke_kremove(va);
1123 * Map a wired page into kernel virtual address space.
1126 mmu_booke_kenter(vm_offset_t va, vm_paddr_t pa)
1129 mmu_booke_kenter_attr(va, pa, VM_MEMATTR_DEFAULT);
1133 mmu_booke_kenter_attr(vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
1138 KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1139 (va <= VM_MAX_KERNEL_ADDRESS)), ("mmu_booke_kenter: invalid va"));
1141 flags = PTE_SR | PTE_SW | PTE_SX | PTE_WIRED | PTE_VALID;
1142 flags |= tlb_calc_wimg(pa, ma) << PTE_MAS2_SHIFT;
1143 flags |= PTE_PS_4KB;
1145 pte = pte_find(kernel_pmap, va);
1146 KASSERT((pte != NULL), ("mmu_booke_kenter: invalid va. NULL PTE"));
1148 mtx_lock_spin(&tlbivax_mutex);
1151 if (PTE_ISVALID(pte)) {
1152 CTR1(KTR_PMAP, "%s: replacing entry!", __func__);
1154 /* Flush entry from TLB0 */
1155 tlb0_flush_entry(va);
1158 *pte = PTE_RPN_FROM_PA(pa) | flags;
1160 //debugf("mmu_booke_kenter: pdir_idx = %d ptbl_idx = %d va=0x%08x "
1161 // "pa=0x%08x rpn=0x%08x flags=0x%08x\n",
1162 // pdir_idx, ptbl_idx, va, pa, pte->rpn, pte->flags);
1164 /* Flush the real memory from the instruction cache. */
1165 if ((flags & (PTE_I | PTE_G)) == 0)
1166 __syncicache((void *)va, PAGE_SIZE);
1169 mtx_unlock_spin(&tlbivax_mutex);
1173 * Remove a page from kernel page table.
1176 mmu_booke_kremove(vm_offset_t va)
1180 CTR2(KTR_PMAP,"%s: s (va = 0x%"PRI0ptrX")\n", __func__, va);
1182 KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1183 (va <= VM_MAX_KERNEL_ADDRESS)),
1184 ("mmu_booke_kremove: invalid va"));
1186 pte = pte_find(kernel_pmap, va);
1188 if (!PTE_ISVALID(pte)) {
1189 CTR1(KTR_PMAP, "%s: invalid pte", __func__);
1194 mtx_lock_spin(&tlbivax_mutex);
1197 /* Invalidate entry in TLB0, update PTE. */
1198 tlb0_flush_entry(va);
1202 mtx_unlock_spin(&tlbivax_mutex);
1206 * Figure out where a given kernel pointer (usually in a fault) points
1207 * to from the VM's perspective, potentially remapping into userland's
1211 mmu_booke_decode_kernel_ptr(vm_offset_t addr, int *is_user,
1212 vm_offset_t *decoded_addr)
1215 if (trunc_page(addr) <= VM_MAXUSER_ADDRESS)
1220 *decoded_addr = addr;
1225 mmu_booke_page_is_mapped(vm_page_t m)
1228 return (!TAILQ_EMPTY(&(m)->md.pv_list));
1232 mmu_booke_ps_enabled(pmap_t pmap __unused)
1238 * Initialize pmap associated with process 0.
1241 mmu_booke_pinit0(pmap_t pmap)
1244 PMAP_LOCK_INIT(pmap);
1245 mmu_booke_pinit(pmap);
1246 PCPU_SET(curpmap, pmap);
1250 * Insert the given physical page at the specified virtual address in the
1251 * target physical map with the protection requested. If specified the page
1252 * will be wired down.
1255 mmu_booke_enter(pmap_t pmap, vm_offset_t va, vm_page_t m,
1256 vm_prot_t prot, u_int flags, int8_t psind)
1260 rw_wlock(&pvh_global_lock);
1262 error = mmu_booke_enter_locked(pmap, va, m, prot, flags, psind);
1264 rw_wunlock(&pvh_global_lock);
1269 mmu_booke_enter_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
1270 vm_prot_t prot, u_int pmap_flags, int8_t psind __unused)
1275 int error, su, sync;
1277 pa = VM_PAGE_TO_PHYS(m);
1278 su = (pmap == kernel_pmap);
1281 //debugf("mmu_booke_enter_locked: s (pmap=0x%08x su=%d tid=%d m=0x%08x va=0x%08x "
1282 // "pa=0x%08x prot=0x%08x flags=%#x)\n",
1283 // (u_int32_t)pmap, su, pmap->pm_tid,
1284 // (u_int32_t)m, va, pa, prot, flags);
1287 KASSERT(((va >= virtual_avail) &&
1288 (va <= VM_MAX_KERNEL_ADDRESS)),
1289 ("mmu_booke_enter_locked: kernel pmap, non kernel va"));
1291 KASSERT((va <= VM_MAXUSER_ADDRESS),
1292 ("mmu_booke_enter_locked: user pmap, non user va"));
1294 if ((m->oflags & VPO_UNMANAGED) == 0) {
1295 if ((pmap_flags & PMAP_ENTER_QUICK_LOCKED) == 0)
1296 VM_PAGE_OBJECT_BUSY_ASSERT(m);
1298 VM_OBJECT_ASSERT_LOCKED(m->object);
1301 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1304 * If there is an existing mapping, and the physical address has not
1305 * changed, must be protection or wiring change.
1307 if (((pte = pte_find(pmap, va)) != NULL) &&
1308 (PTE_ISVALID(pte)) && (PTE_PA(pte) == pa)) {
1311 * Before actually updating pte->flags we calculate and
1312 * prepare its new value in a helper var.
1315 flags &= ~(PTE_UW | PTE_UX | PTE_SW | PTE_SX | PTE_MODIFIED);
1317 /* Wiring change, just update stats. */
1318 if ((pmap_flags & PMAP_ENTER_WIRED) != 0) {
1319 if (!PTE_ISWIRED(pte)) {
1321 pmap->pm_stats.wired_count++;
1324 if (PTE_ISWIRED(pte)) {
1325 flags &= ~PTE_WIRED;
1326 pmap->pm_stats.wired_count--;
1330 if (prot & VM_PROT_WRITE) {
1331 /* Add write permissions. */
1336 if ((flags & PTE_MANAGED) != 0)
1337 vm_page_aflag_set(m, PGA_WRITEABLE);
1339 /* Handle modified pages, sense modify status. */
1342 * The PTE_MODIFIED flag could be set by underlying
1343 * TLB misses since we last read it (above), possibly
1344 * other CPUs could update it so we check in the PTE
1345 * directly rather than rely on that saved local flags
1348 if (PTE_ISMODIFIED(pte))
1352 if (prot & VM_PROT_EXECUTE) {
1358 * Check existing flags for execute permissions: if we
1359 * are turning execute permissions on, icache should
1362 if ((*pte & (PTE_UX | PTE_SX)) == 0)
1366 flags &= ~PTE_REFERENCED;
1369 * The new flags value is all calculated -- only now actually
1372 mtx_lock_spin(&tlbivax_mutex);
1375 tlb0_flush_entry(va);
1376 *pte &= ~PTE_FLAGS_MASK;
1380 mtx_unlock_spin(&tlbivax_mutex);
1384 * If there is an existing mapping, but it's for a different
1385 * physical address, pte_enter() will delete the old mapping.
1387 //if ((pte != NULL) && PTE_ISVALID(pte))
1388 // debugf("mmu_booke_enter_locked: replace\n");
1390 // debugf("mmu_booke_enter_locked: new\n");
1392 /* Now set up the flags and install the new mapping. */
1393 flags = (PTE_SR | PTE_VALID);
1399 if (prot & VM_PROT_WRITE) {
1404 if ((m->oflags & VPO_UNMANAGED) == 0)
1405 vm_page_aflag_set(m, PGA_WRITEABLE);
1408 if (prot & VM_PROT_EXECUTE) {
1414 /* If its wired update stats. */
1415 if ((pmap_flags & PMAP_ENTER_WIRED) != 0)
1418 error = pte_enter(pmap, m, va, flags,
1419 (pmap_flags & PMAP_ENTER_NOSLEEP) != 0);
1421 return (KERN_RESOURCE_SHORTAGE);
1423 if ((flags & PMAP_ENTER_WIRED) != 0)
1424 pmap->pm_stats.wired_count++;
1426 /* Flush the real memory from the instruction cache. */
1427 if (prot & VM_PROT_EXECUTE)
1431 if (sync && (su || pmap == PCPU_GET(curpmap))) {
1432 __syncicache((void *)va, PAGE_SIZE);
1436 return (KERN_SUCCESS);
1440 * Maps a sequence of resident pages belonging to the same object.
1441 * The sequence begins with the given page m_start. This page is
1442 * mapped at the given virtual address start. Each subsequent page is
1443 * mapped at a virtual address that is offset from start by the same
1444 * amount as the page is offset from m_start within the object. The
1445 * last page in the sequence is the page with the largest offset from
1446 * m_start that can be mapped at a virtual address less than the given
1447 * virtual address end. Not every virtual page between start and end
1448 * is mapped; only those for which a resident page exists with the
1449 * corresponding offset from m_start are mapped.
1452 mmu_booke_enter_object(pmap_t pmap, vm_offset_t start,
1453 vm_offset_t end, vm_page_t m_start, vm_prot_t prot)
1456 vm_pindex_t diff, psize;
1458 VM_OBJECT_ASSERT_LOCKED(m_start->object);
1460 psize = atop(end - start);
1462 rw_wlock(&pvh_global_lock);
1464 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1465 mmu_booke_enter_locked(pmap, start + ptoa(diff), m,
1466 prot & (VM_PROT_READ | VM_PROT_EXECUTE),
1467 PMAP_ENTER_NOSLEEP | PMAP_ENTER_QUICK_LOCKED, 0);
1468 m = TAILQ_NEXT(m, listq);
1471 rw_wunlock(&pvh_global_lock);
1475 mmu_booke_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m,
1479 rw_wlock(&pvh_global_lock);
1481 mmu_booke_enter_locked(pmap, va, m,
1482 prot & (VM_PROT_READ | VM_PROT_EXECUTE), PMAP_ENTER_NOSLEEP |
1483 PMAP_ENTER_QUICK_LOCKED, 0);
1485 rw_wunlock(&pvh_global_lock);
1489 * Remove the given range of addresses from the specified map.
1491 * It is assumed that the start and end are properly rounded to the page size.
1494 mmu_booke_remove(pmap_t pmap, vm_offset_t va, vm_offset_t endva)
1499 int su = (pmap == kernel_pmap);
1501 //debugf("mmu_booke_remove: s (su = %d pmap=0x%08x tid=%d va=0x%08x endva=0x%08x)\n",
1502 // su, (u_int32_t)pmap, pmap->pm_tid, va, endva);
1505 KASSERT(((va >= virtual_avail) &&
1506 (va <= VM_MAX_KERNEL_ADDRESS)),
1507 ("mmu_booke_remove: kernel pmap, non kernel va"));
1509 KASSERT((va <= VM_MAXUSER_ADDRESS),
1510 ("mmu_booke_remove: user pmap, non user va"));
1513 if (PMAP_REMOVE_DONE(pmap)) {
1514 //debugf("mmu_booke_remove: e (empty)\n");
1518 hold_flag = PTBL_HOLD_FLAG(pmap);
1519 //debugf("mmu_booke_remove: hold_flag = %d\n", hold_flag);
1521 rw_wlock(&pvh_global_lock);
1523 for (; va < endva; va += PAGE_SIZE) {
1524 pte = pte_find_next(pmap, &va);
1525 if ((pte == NULL) || !PTE_ISVALID(pte))
1529 pte_remove(pmap, va, hold_flag);
1532 rw_wunlock(&pvh_global_lock);
1534 //debugf("mmu_booke_remove: e\n");
1538 * Remove physical page from all pmaps in which it resides.
1541 mmu_booke_remove_all(vm_page_t m)
1546 rw_wlock(&pvh_global_lock);
1547 TAILQ_FOREACH_SAFE(pv, &m->md.pv_list, pv_link, pvn) {
1548 PMAP_LOCK(pv->pv_pmap);
1549 hold_flag = PTBL_HOLD_FLAG(pv->pv_pmap);
1550 pte_remove(pv->pv_pmap, pv->pv_va, hold_flag);
1551 PMAP_UNLOCK(pv->pv_pmap);
1553 vm_page_aflag_clear(m, PGA_WRITEABLE);
1554 rw_wunlock(&pvh_global_lock);
1558 * Map a range of physical addresses into kernel virtual address space.
1561 mmu_booke_map(vm_offset_t *virt, vm_paddr_t pa_start,
1562 vm_paddr_t pa_end, int prot)
1564 vm_offset_t sva = *virt;
1565 vm_offset_t va = sva;
1567 #ifdef __powerpc64__
1568 /* XXX: Handle memory not starting at 0x0. */
1569 if (pa_end < ctob(Maxmem))
1570 return (PHYS_TO_DMAP(pa_start));
1573 while (pa_start < pa_end) {
1574 mmu_booke_kenter(va, pa_start);
1576 pa_start += PAGE_SIZE;
1584 * The pmap must be activated before it's address space can be accessed in any
1588 mmu_booke_activate(struct thread *td)
1593 pmap = &td->td_proc->p_vmspace->vm_pmap;
1595 CTR5(KTR_PMAP, "%s: s (td = %p, proc = '%s', id = %d, pmap = 0x%"PRI0ptrX")",
1596 __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1598 KASSERT((pmap != kernel_pmap), ("mmu_booke_activate: kernel_pmap!"));
1602 cpuid = PCPU_GET(cpuid);
1603 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
1604 PCPU_SET(curpmap, pmap);
1606 if (pmap->pm_tid[cpuid] == TID_NONE)
1609 /* Load PID0 register with pmap tid value. */
1610 mtspr(SPR_PID0, pmap->pm_tid[cpuid]);
1611 __asm __volatile("isync");
1613 mtspr(SPR_DBCR0, td->td_pcb->pcb_cpu.booke.dbcr0);
1617 CTR3(KTR_PMAP, "%s: e (tid = %d for '%s')", __func__,
1618 pmap->pm_tid[PCPU_GET(cpuid)], td->td_proc->p_comm);
1622 * Deactivate the specified process's address space.
1625 mmu_booke_deactivate(struct thread *td)
1629 pmap = &td->td_proc->p_vmspace->vm_pmap;
1631 CTR5(KTR_PMAP, "%s: td=%p, proc = '%s', id = %d, pmap = 0x%"PRI0ptrX,
1632 __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1634 td->td_pcb->pcb_cpu.booke.dbcr0 = mfspr(SPR_DBCR0);
1636 CPU_CLR_ATOMIC(PCPU_GET(cpuid), &pmap->pm_active);
1637 PCPU_SET(curpmap, NULL);
1641 * Copy the range specified by src_addr/len
1642 * from the source map to the range dst_addr/len
1643 * in the destination map.
1645 * This routine is only advisory and need not do anything.
1648 mmu_booke_copy(pmap_t dst_pmap, pmap_t src_pmap,
1649 vm_offset_t dst_addr, vm_size_t len, vm_offset_t src_addr)
1655 * Set the physical protection on the specified range of this map as requested.
1658 mmu_booke_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
1665 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1666 mmu_booke_remove(pmap, sva, eva);
1670 if (prot & VM_PROT_WRITE)
1674 for (va = sva; va < eva; va += PAGE_SIZE) {
1675 if ((pte = pte_find(pmap, va)) != NULL) {
1676 if (PTE_ISVALID(pte)) {
1677 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
1679 mtx_lock_spin(&tlbivax_mutex);
1682 /* Handle modified pages. */
1683 if (PTE_ISMODIFIED(pte) && PTE_ISMANAGED(pte))
1686 tlb0_flush_entry(va);
1687 *pte &= ~(PTE_UW | PTE_SW | PTE_MODIFIED);
1690 mtx_unlock_spin(&tlbivax_mutex);
1698 * Clear the write and modified bits in each of the given page's mappings.
1701 mmu_booke_remove_write(vm_page_t m)
1706 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1707 ("mmu_booke_remove_write: page %p is not managed", m));
1708 vm_page_assert_busied(m);
1710 if (!pmap_page_is_write_mapped(m))
1712 rw_wlock(&pvh_global_lock);
1713 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
1714 PMAP_LOCK(pv->pv_pmap);
1715 if ((pte = pte_find(pv->pv_pmap, pv->pv_va)) != NULL) {
1716 if (PTE_ISVALID(pte)) {
1717 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
1719 mtx_lock_spin(&tlbivax_mutex);
1722 /* Handle modified pages. */
1723 if (PTE_ISMODIFIED(pte))
1726 /* Flush mapping from TLB0. */
1727 *pte &= ~(PTE_UW | PTE_SW | PTE_MODIFIED);
1730 mtx_unlock_spin(&tlbivax_mutex);
1733 PMAP_UNLOCK(pv->pv_pmap);
1735 vm_page_aflag_clear(m, PGA_WRITEABLE);
1736 rw_wunlock(&pvh_global_lock);
1740 * Atomically extract and hold the physical page with the given
1741 * pmap and virtual address pair if that mapping permits the given
1745 mmu_booke_extract_and_hold(pmap_t pmap, vm_offset_t va,
1754 pte = pte_find(pmap, va);
1755 if ((pte != NULL) && PTE_ISVALID(pte)) {
1756 if (pmap == kernel_pmap)
1761 if ((*pte & pte_wbit) != 0 || (prot & VM_PROT_WRITE) == 0) {
1762 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
1763 if (!vm_page_wire_mapped(m))
1772 * Initialize a vm_page's machine-dependent fields.
1775 mmu_booke_page_init(vm_page_t m)
1778 m->md.pv_tracked = 0;
1779 TAILQ_INIT(&m->md.pv_list);
1783 * Return whether or not the specified physical page was modified
1784 * in any of physical maps.
1787 mmu_booke_is_modified(vm_page_t m)
1793 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1794 ("mmu_booke_is_modified: page %p is not managed", m));
1798 * If the page is not busied then this check is racy.
1800 if (!pmap_page_is_write_mapped(m))
1803 rw_wlock(&pvh_global_lock);
1804 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
1805 PMAP_LOCK(pv->pv_pmap);
1806 if ((pte = pte_find(pv->pv_pmap, pv->pv_va)) != NULL &&
1808 if (PTE_ISMODIFIED(pte))
1811 PMAP_UNLOCK(pv->pv_pmap);
1815 rw_wunlock(&pvh_global_lock);
1820 * Return whether or not the specified virtual address is eligible
1824 mmu_booke_is_prefaultable(pmap_t pmap, vm_offset_t addr)
1831 * Return whether or not the specified physical page was referenced
1832 * in any physical maps.
1835 mmu_booke_is_referenced(vm_page_t m)
1841 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1842 ("mmu_booke_is_referenced: page %p is not managed", m));
1844 rw_wlock(&pvh_global_lock);
1845 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
1846 PMAP_LOCK(pv->pv_pmap);
1847 if ((pte = pte_find(pv->pv_pmap, pv->pv_va)) != NULL &&
1849 if (PTE_ISREFERENCED(pte))
1852 PMAP_UNLOCK(pv->pv_pmap);
1856 rw_wunlock(&pvh_global_lock);
1861 * Clear the modify bits on the specified physical page.
1864 mmu_booke_clear_modify(vm_page_t m)
1869 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1870 ("mmu_booke_clear_modify: page %p is not managed", m));
1871 vm_page_assert_busied(m);
1873 if (!pmap_page_is_write_mapped(m))
1876 rw_wlock(&pvh_global_lock);
1877 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
1878 PMAP_LOCK(pv->pv_pmap);
1879 if ((pte = pte_find(pv->pv_pmap, pv->pv_va)) != NULL &&
1881 mtx_lock_spin(&tlbivax_mutex);
1884 if (*pte & (PTE_SW | PTE_UW | PTE_MODIFIED)) {
1885 tlb0_flush_entry(pv->pv_va);
1886 *pte &= ~(PTE_SW | PTE_UW | PTE_MODIFIED |
1891 mtx_unlock_spin(&tlbivax_mutex);
1893 PMAP_UNLOCK(pv->pv_pmap);
1895 rw_wunlock(&pvh_global_lock);
1899 * Return a count of reference bits for a page, clearing those bits.
1900 * It is not necessary for every reference bit to be cleared, but it
1901 * is necessary that 0 only be returned when there are truly no
1902 * reference bits set.
1904 * As an optimization, update the page's dirty field if a modified bit is
1905 * found while counting reference bits. This opportunistic update can be
1906 * performed at low cost and can eliminate the need for some future calls
1907 * to pmap_is_modified(). However, since this function stops after
1908 * finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
1909 * dirty pages. Those dirty pages will only be detected by a future call
1910 * to pmap_is_modified().
1913 mmu_booke_ts_referenced(vm_page_t m)
1919 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1920 ("mmu_booke_ts_referenced: page %p is not managed", m));
1922 rw_wlock(&pvh_global_lock);
1923 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
1924 PMAP_LOCK(pv->pv_pmap);
1925 if ((pte = pte_find(pv->pv_pmap, pv->pv_va)) != NULL &&
1927 if (PTE_ISMODIFIED(pte))
1929 if (PTE_ISREFERENCED(pte)) {
1930 mtx_lock_spin(&tlbivax_mutex);
1933 tlb0_flush_entry(pv->pv_va);
1934 *pte &= ~PTE_REFERENCED;
1937 mtx_unlock_spin(&tlbivax_mutex);
1939 if (++count >= PMAP_TS_REFERENCED_MAX) {
1940 PMAP_UNLOCK(pv->pv_pmap);
1945 PMAP_UNLOCK(pv->pv_pmap);
1947 rw_wunlock(&pvh_global_lock);
1952 * Clear the wired attribute from the mappings for the specified range of
1953 * addresses in the given pmap. Every valid mapping within that range must
1954 * have the wired attribute set. In contrast, invalid mappings cannot have
1955 * the wired attribute set, so they are ignored.
1957 * The wired attribute of the page table entry is not a hardware feature, so
1958 * there is no need to invalidate any TLB entries.
1961 mmu_booke_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1967 for (va = sva; va < eva; va += PAGE_SIZE) {
1968 if ((pte = pte_find(pmap, va)) != NULL &&
1970 if (!PTE_ISWIRED(pte))
1971 panic("mmu_booke_unwire: pte %p isn't wired",
1974 pmap->pm_stats.wired_count--;
1982 * Return true if the pmap's pv is one of the first 16 pvs linked to from this
1983 * page. This count may be changed upwards or downwards in the future; it is
1984 * only necessary that true be returned for a small subset of pmaps for proper
1988 mmu_booke_page_exists_quick(pmap_t pmap, vm_page_t m)
1994 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
1995 ("mmu_booke_page_exists_quick: page %p is not managed", m));
1998 rw_wlock(&pvh_global_lock);
1999 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2000 if (pv->pv_pmap == pmap) {
2007 rw_wunlock(&pvh_global_lock);
2012 * Return the number of managed mappings to the given physical page that are
2016 mmu_booke_page_wired_mappings(vm_page_t m)
2022 if ((m->oflags & VPO_UNMANAGED) != 0)
2024 rw_wlock(&pvh_global_lock);
2025 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2026 PMAP_LOCK(pv->pv_pmap);
2027 if ((pte = pte_find(pv->pv_pmap, pv->pv_va)) != NULL)
2028 if (PTE_ISVALID(pte) && PTE_ISWIRED(pte))
2030 PMAP_UNLOCK(pv->pv_pmap);
2032 rw_wunlock(&pvh_global_lock);
2037 mmu_booke_dev_direct_mapped(vm_paddr_t pa, vm_size_t size)
2043 * This currently does not work for entries that
2044 * overlap TLB1 entries.
2046 for (i = 0; i < TLB1_ENTRIES; i ++) {
2047 if (tlb1_iomapped(i, pa, size, &va) == 0)
2055 mmu_booke_dumpsys_map(vm_paddr_t pa, size_t sz, void **va)
2061 /* Minidumps are based on virtual memory addresses. */
2063 *va = (void *)(vm_offset_t)pa;
2067 /* Raw physical memory dumps don't have a virtual address. */
2068 /* We always map a 256MB page at 256M. */
2069 gran = 256 * 1024 * 1024;
2070 ppa = rounddown2(pa, gran);
2073 tlb1_set_entry((vm_offset_t)va, ppa, gran, _TLB_ENTRY_IO);
2075 if (sz > (gran - ofs))
2076 tlb1_set_entry((vm_offset_t)(va + gran), ppa + gran, gran,
2081 mmu_booke_dumpsys_unmap(vm_paddr_t pa, size_t sz, void *va)
2089 /* Minidumps are based on virtual memory addresses. */
2090 /* Nothing to do... */
2094 for (i = 0; i < TLB1_ENTRIES; i++) {
2095 tlb1_read_entry(&e, i);
2096 if (!(e.mas1 & MAS1_VALID))
2100 /* Raw physical memory dumps don't have a virtual address. */
2105 tlb1_write_entry(&e, i);
2107 gran = 256 * 1024 * 1024;
2108 ppa = rounddown2(pa, gran);
2110 if (sz > (gran - ofs)) {
2115 tlb1_write_entry(&e, i);
2119 extern struct dump_pa dump_map[PHYS_AVAIL_SZ + 1];
2122 mmu_booke_scan_init()
2129 /* Initialize phys. segments for dumpsys(). */
2130 memset(&dump_map, 0, sizeof(dump_map));
2131 mem_regions(&physmem_regions, &physmem_regions_sz, &availmem_regions,
2132 &availmem_regions_sz);
2133 for (i = 0; i < physmem_regions_sz; i++) {
2134 dump_map[i].pa_start = physmem_regions[i].mr_start;
2135 dump_map[i].pa_size = physmem_regions[i].mr_size;
2140 /* Virtual segments for minidumps: */
2141 memset(&dump_map, 0, sizeof(dump_map));
2143 /* 1st: kernel .data and .bss. */
2144 dump_map[0].pa_start = trunc_page((uintptr_t)_etext);
2145 dump_map[0].pa_size =
2146 round_page((uintptr_t)_end) - dump_map[0].pa_start;
2148 /* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2149 dump_map[1].pa_start = data_start;
2150 dump_map[1].pa_size = data_end - data_start;
2152 /* 3rd: kernel VM. */
2153 va = dump_map[1].pa_start + dump_map[1].pa_size;
2154 /* Find start of next chunk (from va). */
2155 while (va < virtual_end) {
2156 /* Don't dump the buffer cache. */
2157 if (va >= kmi.buffer_sva && va < kmi.buffer_eva) {
2158 va = kmi.buffer_eva;
2161 pte = pte_find(kernel_pmap, va);
2162 if (pte != NULL && PTE_ISVALID(pte))
2166 if (va < virtual_end) {
2167 dump_map[2].pa_start = va;
2169 /* Find last page in chunk. */
2170 while (va < virtual_end) {
2171 /* Don't run into the buffer cache. */
2172 if (va == kmi.buffer_sva)
2174 pte = pte_find(kernel_pmap, va);
2175 if (pte == NULL || !PTE_ISVALID(pte))
2179 dump_map[2].pa_size = va - dump_map[2].pa_start;
2184 * Map a set of physical memory pages into the kernel virtual address space.
2185 * Return a pointer to where it is mapped. This routine is intended to be used
2186 * for mapping device memory, NOT real memory.
2189 mmu_booke_mapdev(vm_paddr_t pa, vm_size_t size)
2192 return (mmu_booke_mapdev_attr(pa, size, VM_MEMATTR_DEFAULT));
2196 tlb1_find_pa(vm_paddr_t pa, tlb_entry_t *e)
2200 for (i = 0; i < TLB1_ENTRIES; i++) {
2201 tlb1_read_entry(e, i);
2202 if ((e->mas1 & MAS1_VALID) == 0)
2211 mmu_booke_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
2215 #ifndef __powerpc64__
2218 uintptr_t va, retva;
2224 * Check if this is premapped in TLB1.
2229 wimge = tlb_calc_wimg(pa, ma);
2230 for (i = 0; i < TLB1_ENTRIES; i++) {
2231 tlb1_read_entry(&e, i);
2232 if (!(e.mas1 & MAS1_VALID))
2234 if (wimge != (e.mas2 & (MAS2_WIMGE_MASK & ~_TLB_ENTRY_SHARED)))
2236 if (tmppa >= e.phys && tmppa < e.phys + e.size) {
2237 va = e.virt + (pa - e.phys);
2238 tmppa = e.phys + e.size;
2239 sz -= MIN(sz, e.size - (pa - e.phys));
2240 while (sz > 0 && (i = tlb1_find_pa(tmppa, &e)) != -1) {
2241 if (wimge != (e.mas2 & (MAS2_WIMGE_MASK & ~_TLB_ENTRY_SHARED)))
2243 sz -= MIN(sz, e.size);
2244 tmppa = e.phys + e.size;
2248 return ((void *)va);
2252 size = roundup(size, PAGE_SIZE);
2254 #ifdef __powerpc64__
2255 KASSERT(pa < VM_MAPDEV_PA_MAX,
2256 ("Unsupported physical address! %lx", pa));
2257 va = VM_MAPDEV_BASE + pa;
2259 #ifdef POW2_MAPPINGS
2261 * Align the mapping to a power of 2 size, taking into account that we
2262 * may need to increase the size multiple times to satisfy the size and
2263 * alignment requirements.
2265 * This works in the general case because it's very rare (near never?)
2266 * to have different access properties (WIMG) within a single
2267 * power-of-two region. If a design does call for that, POW2_MAPPINGS
2268 * can be undefined, and exact mappings will be used instead.
2271 size = roundup2(size, 1 << ilog2(size));
2272 while (rounddown2(va, size) + size < va + sz)
2274 va = rounddown2(va, size);
2275 pa = rounddown2(pa, size);
2279 * The device mapping area is between VM_MAXUSER_ADDRESS and
2280 * VM_MIN_KERNEL_ADDRESS. This gives 1GB of device addressing.
2282 #ifdef SPARSE_MAPDEV
2284 * With a sparse mapdev, align to the largest starting region. This
2285 * could feasibly be optimized for a 'best-fit' alignment, but that
2286 * calculation could be very costly.
2287 * Align to the smaller of:
2288 * - first set bit in overlap of (pa & size mask)
2289 * - largest size envelope
2291 * It's possible the device mapping may start at a PA that's not larger
2292 * than the size mask, so we need to offset in to maximize the TLB entry
2293 * range and minimize the number of used TLB entries.
2296 tmpva = tlb1_map_base;
2297 sz = ffsl((~((1 << flsl(size-1)) - 1)) & pa);
2298 sz = sz ? min(roundup(sz + 3, 4), flsl(size) - 1) : flsl(size) - 1;
2299 va = roundup(tlb1_map_base, 1 << sz) | (((1 << sz) - 1) & pa);
2300 } while (!atomic_cmpset_int(&tlb1_map_base, tmpva, va + size));
2302 va = atomic_fetchadd_int(&tlb1_map_base, size);
2306 if (tlb1_mapin_region(va, pa, size, tlb_calc_wimg(pa, ma)) != size)
2309 return ((void *)retva);
2313 * 'Unmap' a range mapped by mmu_booke_mapdev().
2316 mmu_booke_unmapdev(vm_offset_t va, vm_size_t size)
2318 #ifdef SUPPORTS_SHRINKING_TLB1
2319 vm_offset_t base, offset;
2322 * Unmap only if this is inside kernel virtual space.
2324 if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
2325 base = trunc_page(va);
2326 offset = va & PAGE_MASK;
2327 size = roundup(offset + size, PAGE_SIZE);
2328 mmu_booke_qremove(base, atop(size));
2329 kva_free(base, size);
2335 * mmu_booke_object_init_pt preloads the ptes for a given object into the
2336 * specified pmap. This eliminates the blast of soft faults on process startup
2337 * and immediately after an mmap.
2340 mmu_booke_object_init_pt(pmap_t pmap, vm_offset_t addr,
2341 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2344 VM_OBJECT_ASSERT_WLOCKED(object);
2345 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2346 ("mmu_booke_object_init_pt: non-device object"));
2350 * Perform the pmap work for mincore.
2353 mmu_booke_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *pap)
2356 /* XXX: this should be implemented at some point */
2361 mmu_booke_change_attr(vm_offset_t addr, vm_size_t sz, vm_memattr_t mode)
2368 addr = trunc_page(addr);
2370 /* Only allow changes to mapped kernel addresses. This includes:
2372 * - DMAP (powerpc64)
2375 if (addr <= VM_MAXUSER_ADDRESS ||
2376 #ifdef __powerpc64__
2377 (addr >= tlb1_map_base && addr < DMAP_BASE_ADDRESS) ||
2378 (addr > DMAP_MAX_ADDRESS && addr < VM_MIN_KERNEL_ADDRESS) ||
2380 (addr >= tlb1_map_base && addr < VM_MIN_KERNEL_ADDRESS) ||
2382 (addr > VM_MAX_KERNEL_ADDRESS))
2385 /* Check TLB1 mappings */
2386 for (i = 0; i < TLB1_ENTRIES; i++) {
2387 tlb1_read_entry(&e, i);
2388 if (!(e.mas1 & MAS1_VALID))
2390 if (addr >= e.virt && addr < e.virt + e.size)
2393 if (i < TLB1_ENTRIES) {
2394 /* Only allow full mappings to be modified for now. */
2395 /* Validate the range. */
2396 for (j = i, va = addr; va < addr + sz; va += e.size, j++) {
2397 tlb1_read_entry(&e, j);
2398 if (va != e.virt || (sz - (va - addr) < e.size))
2401 for (va = addr; va < addr + sz; va += e.size, i++) {
2402 tlb1_read_entry(&e, i);
2403 e.mas2 &= ~MAS2_WIMGE_MASK;
2404 e.mas2 |= tlb_calc_wimg(e.phys, mode);
2407 * Write it out to the TLB. Should really re-sync with other
2410 tlb1_write_entry(&e, i);
2415 /* Not in TLB1, try through pmap */
2416 /* First validate the range. */
2417 for (va = addr; va < addr + sz; va += PAGE_SIZE) {
2418 pte = pte_find(kernel_pmap, va);
2419 if (pte == NULL || !PTE_ISVALID(pte))
2423 mtx_lock_spin(&tlbivax_mutex);
2425 for (va = addr; va < addr + sz; va += PAGE_SIZE) {
2426 pte = pte_find(kernel_pmap, va);
2427 *pte &= ~(PTE_MAS2_MASK << PTE_MAS2_SHIFT);
2428 *pte |= tlb_calc_wimg(PTE_PA(pte), mode) << PTE_MAS2_SHIFT;
2429 tlb0_flush_entry(va);
2432 mtx_unlock_spin(&tlbivax_mutex);
2438 mmu_booke_page_array_startup(long pages)
2440 vm_page_array_size = pages;
2443 /**************************************************************************/
2445 /**************************************************************************/
2448 * Allocate a TID. If necessary, steal one from someone else.
2449 * The new TID is flushed from the TLB before returning.
2452 tid_alloc(pmap_t pmap)
2457 KASSERT((pmap != kernel_pmap), ("tid_alloc: kernel pmap"));
2459 CTR2(KTR_PMAP, "%s: s (pmap = %p)", __func__, pmap);
2461 thiscpu = PCPU_GET(cpuid);
2463 tid = PCPU_GET(booke.tid_next);
2466 PCPU_SET(booke.tid_next, tid + 1);
2468 /* If we are stealing TID then clear the relevant pmap's field */
2469 if (tidbusy[thiscpu][tid] != NULL) {
2470 CTR2(KTR_PMAP, "%s: warning: stealing tid %d", __func__, tid);
2472 tidbusy[thiscpu][tid]->pm_tid[thiscpu] = TID_NONE;
2474 /* Flush all entries from TLB0 matching this TID. */
2478 tidbusy[thiscpu][tid] = pmap;
2479 pmap->pm_tid[thiscpu] = tid;
2480 __asm __volatile("msync; isync");
2482 CTR3(KTR_PMAP, "%s: e (%02d next = %02d)", __func__, tid,
2483 PCPU_GET(booke.tid_next));
2488 /**************************************************************************/
2490 /**************************************************************************/
2492 /* Convert TLB0 va and way number to tlb0[] table index. */
2493 static inline unsigned int
2494 tlb0_tableidx(vm_offset_t va, unsigned int way)
2498 idx = (way * TLB0_ENTRIES_PER_WAY);
2499 idx += (va & MAS2_TLB0_ENTRY_IDX_MASK) >> MAS2_TLB0_ENTRY_IDX_SHIFT;
2504 * Invalidate TLB0 entry.
2507 tlb0_flush_entry(vm_offset_t va)
2510 CTR2(KTR_PMAP, "%s: s va=0x%08x", __func__, va);
2512 mtx_assert(&tlbivax_mutex, MA_OWNED);
2514 __asm __volatile("tlbivax 0, %0" :: "r"(va & MAS2_EPN_MASK));
2515 __asm __volatile("isync; msync");
2516 __asm __volatile("tlbsync; msync");
2518 CTR1(KTR_PMAP, "%s: e", __func__);
2521 /**************************************************************************/
2523 /**************************************************************************/
2526 * TLB1 mapping notes:
2528 * TLB1[0] Kernel text and data.
2529 * TLB1[1-15] Additional kernel text and data mappings (if required), PCI
2530 * windows, other devices mappings.
2534 * Read an entry from given TLB1 slot.
2537 tlb1_read_entry(tlb_entry_t *entry, unsigned int slot)
2542 KASSERT((entry != NULL), ("%s(): Entry is NULL!", __func__));
2545 __asm __volatile("wrteei 0");
2547 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(slot);
2548 mtspr(SPR_MAS0, mas0);
2549 __asm __volatile("isync; tlbre");
2551 entry->mas1 = mfspr(SPR_MAS1);
2552 entry->mas2 = mfspr(SPR_MAS2);
2553 entry->mas3 = mfspr(SPR_MAS3);
2555 switch ((mfpvr() >> 16) & 0xFFFF) {
2560 entry->mas7 = mfspr(SPR_MAS7);
2566 __asm __volatile("wrtee %0" :: "r"(msr));
2568 entry->virt = entry->mas2 & MAS2_EPN_MASK;
2569 entry->phys = ((vm_paddr_t)(entry->mas7 & MAS7_RPN) << 32) |
2570 (entry->mas3 & MAS3_RPN);
2572 tsize2size((entry->mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT);
2575 struct tlbwrite_args {
2581 tlb1_find_free(void)
2586 for (i = 0; i < TLB1_ENTRIES; i++) {
2587 tlb1_read_entry(&e, i);
2588 if ((e.mas1 & MAS1_VALID) == 0)
2595 tlb1_purge_va_range(vm_offset_t va, vm_size_t size)
2600 for (i = 0; i < TLB1_ENTRIES; i++) {
2601 tlb1_read_entry(&e, i);
2602 if ((e.mas1 & MAS1_VALID) == 0)
2604 if ((e.mas2 & MAS2_EPN_MASK) >= va &&
2605 (e.mas2 & MAS2_EPN_MASK) < va + size) {
2606 mtspr(SPR_MAS1, e.mas1 & ~MAS1_VALID);
2607 __asm __volatile("isync; tlbwe; isync; msync");
2613 tlb1_write_entry_int(void *arg)
2615 struct tlbwrite_args *args = arg;
2620 tlb1_purge_va_range(args->e->virt, args->e->size);
2621 idx = tlb1_find_free();
2623 panic("No free TLB1 entries!\n");
2626 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(idx);
2628 mtspr(SPR_MAS0, mas0);
2629 mtspr(SPR_MAS1, args->e->mas1);
2630 mtspr(SPR_MAS2, args->e->mas2);
2631 mtspr(SPR_MAS3, args->e->mas3);
2632 switch ((mfpvr() >> 16) & 0xFFFF) {
2639 mtspr(SPR_MAS7, args->e->mas7);
2645 __asm __volatile("isync; tlbwe; isync; msync");
2650 tlb1_write_entry_sync(void *arg)
2652 /* Empty synchronization point for smp_rendezvous(). */
2656 * Write given entry to TLB1 hardware.
2659 tlb1_write_entry(tlb_entry_t *e, unsigned int idx)
2661 struct tlbwrite_args args;
2667 if ((e->mas2 & _TLB_ENTRY_SHARED) && smp_started) {
2669 smp_rendezvous(tlb1_write_entry_sync,
2670 tlb1_write_entry_int,
2671 tlb1_write_entry_sync, &args);
2678 __asm __volatile("wrteei 0");
2679 tlb1_write_entry_int(&args);
2680 __asm __volatile("wrtee %0" :: "r"(msr));
2685 * Convert TLB TSIZE value to mapped region size.
2688 tsize2size(unsigned int tsize)
2693 * size = 4^tsize * 2^10 = 2^(2 * tsize - 10)
2696 return ((1 << (2 * tsize)) * 1024);
2700 * Convert region size (must be power of 4) to TLB TSIZE value.
2703 size2tsize(vm_size_t size)
2706 return (ilog2(size) / 2 - 5);
2710 * Register permanent kernel mapping in TLB1.
2712 * Entries are created starting from index 0 (current free entry is
2713 * kept in tlb1_idx) and are not supposed to be invalidated.
2716 tlb1_set_entry(vm_offset_t va, vm_paddr_t pa, vm_size_t size,
2723 /* First try to update an existing entry. */
2724 for (index = 0; index < TLB1_ENTRIES; index++) {
2725 tlb1_read_entry(&e, index);
2726 /* Check if we're just updating the flags, and update them. */
2727 if (e.phys == pa && e.virt == va && e.size == size) {
2728 e.mas2 = (va & MAS2_EPN_MASK) | flags;
2729 tlb1_write_entry(&e, index);
2734 /* Convert size to TSIZE */
2735 tsize = size2tsize(size);
2737 tid = (TID_KERNEL << MAS1_TID_SHIFT) & MAS1_TID_MASK;
2738 /* XXX TS is hard coded to 0 for now as we only use single address space */
2739 ts = (0 << MAS1_TS_SHIFT) & MAS1_TS_MASK;
2744 e.mas1 = MAS1_VALID | MAS1_IPROT | ts | tid;
2745 e.mas1 |= ((tsize << MAS1_TSIZE_SHIFT) & MAS1_TSIZE_MASK);
2746 e.mas2 = (va & MAS2_EPN_MASK) | flags;
2748 /* Set supervisor RWX permission bits */
2749 e.mas3 = (pa & MAS3_RPN) | MAS3_SR | MAS3_SW | MAS3_SX;
2750 e.mas7 = (pa >> 32) & MAS7_RPN;
2752 tlb1_write_entry(&e, -1);
2758 * Map in contiguous RAM region into the TLB1.
2761 tlb1_mapin_region(vm_offset_t va, vm_paddr_t pa, vm_size_t size, int wimge)
2764 vm_size_t mapped, sz, ssize;
2771 sz = 1UL << (ilog2(size) & ~1);
2772 /* Align size to PA */
2776 } while (pa % sz != 0);
2778 /* Now align from there to VA */
2782 } while (va % sz != 0);
2784 #ifdef __powerpc64__
2786 * Clamp TLB1 entries to 4G.
2788 * While the e6500 supports up to 1TB mappings, the e5500
2789 * only supports up to 4G mappings. (0b1011)
2791 * If any e6500 machines capable of supporting a very
2792 * large amount of memory appear in the future, we can
2795 * For now, though, since we have plenty of space in TLB1,
2796 * always avoid creating entries larger than 4GB.
2798 sz = MIN(sz, 1UL << 32);
2801 printf("Wiring VA=%p to PA=%jx (size=%lx)\n",
2802 (void *)va, (uintmax_t)pa, (long)sz);
2803 if (tlb1_set_entry(va, pa, sz,
2804 _TLB_ENTRY_SHARED | wimge) < 0)
2811 mapped = (va - base);
2813 printf("mapped size 0x%"PRIxPTR" (wasted space 0x%"PRIxPTR")\n",
2814 mapped, mapped - ssize);
2820 * TLB1 initialization routine, to be called after the very first
2821 * assembler level setup done in locore.S.
2827 uint32_t mas0, mas1, mas3, mas7;
2832 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(0);
2833 mtspr(SPR_MAS0, mas0);
2834 __asm __volatile("isync; tlbre");
2836 mas1 = mfspr(SPR_MAS1);
2837 mas2 = mfspr(SPR_MAS2);
2838 mas3 = mfspr(SPR_MAS3);
2839 mas7 = mfspr(SPR_MAS7);
2841 kernload = ((vm_paddr_t)(mas7 & MAS7_RPN) << 32) |
2844 tsz = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
2845 kernsize += (tsz > 0) ? tsize2size(tsz) : 0;
2846 kernstart = trunc_page(mas2);
2848 /* Setup TLB miss defaults */
2849 set_mas4_defaults();
2853 * pmap_early_io_unmap() should be used in short conjunction with
2854 * pmap_early_io_map(), as in the following snippet:
2856 * x = pmap_early_io_map(...);
2857 * <do something with x>
2858 * pmap_early_io_unmap(x, size);
2860 * And avoiding more allocations between.
2863 pmap_early_io_unmap(vm_offset_t va, vm_size_t size)
2869 size = roundup(size, PAGE_SIZE);
2871 for (i = 0; i < TLB1_ENTRIES && size > 0; i++) {
2872 tlb1_read_entry(&e, i);
2873 if (!(e.mas1 & MAS1_VALID))
2875 if (va <= e.virt && (va + isize) >= (e.virt + e.size)) {
2877 e.mas1 &= ~MAS1_VALID;
2878 tlb1_write_entry(&e, i);
2881 if (tlb1_map_base == va + isize)
2882 tlb1_map_base -= isize;
2886 pmap_early_io_map(vm_paddr_t pa, vm_size_t size)
2893 KASSERT(!pmap_bootstrapped, ("Do not use after PMAP is up!"));
2895 for (i = 0; i < TLB1_ENTRIES; i++) {
2896 tlb1_read_entry(&e, i);
2897 if (!(e.mas1 & MAS1_VALID))
2899 if (pa >= e.phys && (pa + size) <=
2901 return (e.virt + (pa - e.phys));
2904 pa_base = rounddown(pa, PAGE_SIZE);
2905 size = roundup(size + (pa - pa_base), PAGE_SIZE);
2906 tlb1_map_base = roundup2(tlb1_map_base, 1 << (ilog2(size) & ~1));
2907 va = tlb1_map_base + (pa - pa_base);
2910 sz = 1 << (ilog2(size) & ~1);
2911 tlb1_set_entry(tlb1_map_base, pa_base, sz,
2912 _TLB_ENTRY_SHARED | _TLB_ENTRY_IO);
2915 tlb1_map_base += sz;
2922 pmap_track_page(pmap_t pmap, vm_offset_t va)
2926 struct pv_entry *pve;
2928 va = trunc_page(va);
2929 pa = pmap_kextract(va);
2930 page = PHYS_TO_VM_PAGE(pa);
2932 rw_wlock(&pvh_global_lock);
2935 TAILQ_FOREACH(pve, &page->md.pv_list, pv_link) {
2936 if ((pmap == pve->pv_pmap) && (va == pve->pv_va)) {
2940 page->md.pv_tracked = true;
2941 pv_insert(pmap, va, page);
2944 rw_wunlock(&pvh_global_lock);
2948 * Setup MAS4 defaults.
2949 * These values are loaded to MAS0-2 on a TLB miss.
2952 set_mas4_defaults(void)
2956 /* Defaults: TLB0, PID0, TSIZED=4K */
2957 mas4 = MAS4_TLBSELD0;
2958 mas4 |= (TLB_SIZE_4K << MAS4_TSIZED_SHIFT) & MAS4_TSIZED_MASK;
2962 mtspr(SPR_MAS4, mas4);
2963 __asm __volatile("isync");
2967 * Return 0 if the physical IO range is encompassed by one of the
2968 * the TLB1 entries, otherwise return related error code.
2971 tlb1_iomapped(int i, vm_paddr_t pa, vm_size_t size, vm_offset_t *va)
2974 vm_paddr_t pa_start;
2976 unsigned int entry_tsize;
2977 vm_size_t entry_size;
2980 *va = (vm_offset_t)NULL;
2982 tlb1_read_entry(&e, i);
2983 /* Skip invalid entries */
2984 if (!(e.mas1 & MAS1_VALID))
2988 * The entry must be cache-inhibited, guarded, and r/w
2989 * so it can function as an i/o page
2991 prot = e.mas2 & (MAS2_I | MAS2_G);
2992 if (prot != (MAS2_I | MAS2_G))
2995 prot = e.mas3 & (MAS3_SR | MAS3_SW);
2996 if (prot != (MAS3_SR | MAS3_SW))
2999 /* The address should be within the entry range. */
3000 entry_tsize = (e.mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3001 KASSERT((entry_tsize), ("tlb1_iomapped: invalid entry tsize"));
3003 entry_size = tsize2size(entry_tsize);
3004 pa_start = (((vm_paddr_t)e.mas7 & MAS7_RPN) << 32) |
3005 (e.mas3 & MAS3_RPN);
3006 pa_end = pa_start + entry_size;
3008 if ((pa < pa_start) || ((pa + size) > pa_end))
3011 /* Return virtual address of this mapping. */
3012 *va = (e.mas2 & MAS2_EPN_MASK) + (pa - pa_start);
3017 /* Print out contents of the MAS registers for each TLB0 entry */
3019 #ifdef __powerpc64__
3020 tlb_print_entry(int i, uint32_t mas1, uint64_t mas2, uint32_t mas3,
3022 tlb_print_entry(int i, uint32_t mas1, uint32_t mas2, uint32_t mas3,
3033 if (mas1 & MAS1_VALID)
3038 if (mas1 & MAS1_IPROT)
3043 as = (mas1 & MAS1_TS_MASK) ? 1 : 0;
3044 tid = MAS1_GETTID(mas1);
3046 tsize = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3049 size = tsize2size(tsize);
3051 printf("%3d: (%s) [AS=%d] "
3052 "sz = 0x%jx tsz = %d tid = %d mas1 = 0x%08x "
3053 "mas2(va) = 0x%"PRI0ptrX" mas3(pa) = 0x%08x mas7 = 0x%08x\n",
3054 i, desc, as, (uintmax_t)size, tsize, tid, mas1, mas2, mas3, mas7);
3057 DB_SHOW_COMMAND(tlb0, tlb0_print_tlbentries)
3059 uint32_t mas0, mas1, mas3, mas7;
3060 #ifdef __powerpc64__
3065 int entryidx, way, idx;
3067 printf("TLB0 entries:\n");
3068 for (way = 0; way < TLB0_WAYS; way ++)
3069 for (entryidx = 0; entryidx < TLB0_ENTRIES_PER_WAY; entryidx++) {
3070 mas0 = MAS0_TLBSEL(0) | MAS0_ESEL(way);
3071 mtspr(SPR_MAS0, mas0);
3073 mas2 = entryidx << MAS2_TLB0_ENTRY_IDX_SHIFT;
3074 mtspr(SPR_MAS2, mas2);
3076 __asm __volatile("isync; tlbre");
3078 mas1 = mfspr(SPR_MAS1);
3079 mas2 = mfspr(SPR_MAS2);
3080 mas3 = mfspr(SPR_MAS3);
3081 mas7 = mfspr(SPR_MAS7);
3083 idx = tlb0_tableidx(mas2, way);
3084 tlb_print_entry(idx, mas1, mas2, mas3, mas7);
3089 * Print out contents of the MAS registers for each TLB1 entry
3091 DB_SHOW_COMMAND(tlb1, tlb1_print_tlbentries)
3093 uint32_t mas0, mas1, mas3, mas7;
3094 #ifdef __powerpc64__
3101 printf("TLB1 entries:\n");
3102 for (i = 0; i < TLB1_ENTRIES; i++) {
3103 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(i);
3104 mtspr(SPR_MAS0, mas0);
3106 __asm __volatile("isync; tlbre");
3108 mas1 = mfspr(SPR_MAS1);
3109 mas2 = mfspr(SPR_MAS2);
3110 mas3 = mfspr(SPR_MAS3);
3111 mas7 = mfspr(SPR_MAS7);
3113 tlb_print_entry(i, mas1, mas2, mas3, mas7);