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/param.h>
55 #include <sys/malloc.h>
59 #include <sys/queue.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/linker.h>
63 #include <sys/msgbuf.h>
65 #include <sys/mutex.h>
66 #include <sys/rwlock.h>
67 #include <sys/sched.h>
69 #include <sys/vmmeter.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_param.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_pager.h>
82 #include <machine/cpu.h>
83 #include <machine/pcb.h>
84 #include <machine/platform.h>
86 #include <machine/tlb.h>
87 #include <machine/spr.h>
88 #include <machine/md_var.h>
89 #include <machine/mmuvar.h>
90 #include <machine/pmap.h>
91 #include <machine/pte.h>
96 #define debugf(fmt, args...) printf(fmt, ##args)
98 #define debugf(fmt, args...)
101 #define TODO panic("%s: not implemented", __func__);
103 extern int dumpsys_minidump;
105 extern unsigned char _etext[];
106 extern unsigned char _end[];
108 extern uint32_t *bootinfo;
111 extern uint32_t bp_ntlb1s;
115 vm_offset_t kernstart;
118 /* Message buffer and tables. */
119 static vm_offset_t data_start;
120 static vm_size_t data_end;
122 /* Phys/avail memory regions. */
123 static struct mem_region *availmem_regions;
124 static int availmem_regions_sz;
125 static struct mem_region *physmem_regions;
126 static int physmem_regions_sz;
128 /* Reserved KVA space and mutex for mmu_booke_zero_page. */
129 static vm_offset_t zero_page_va;
130 static struct mtx zero_page_mutex;
132 static struct mtx tlbivax_mutex;
135 * Reserved KVA space for mmu_booke_zero_page_idle. This is used
136 * by idle thred only, no lock required.
138 static vm_offset_t zero_page_idle_va;
140 /* Reserved KVA space and mutex for mmu_booke_copy_page. */
141 static vm_offset_t copy_page_src_va;
142 static vm_offset_t copy_page_dst_va;
143 static struct mtx copy_page_mutex;
145 /**************************************************************************/
147 /**************************************************************************/
149 static void mmu_booke_enter_locked(mmu_t, pmap_t, vm_offset_t, vm_page_t,
150 vm_prot_t, boolean_t);
152 unsigned int kptbl_min; /* Index of the first kernel ptbl. */
153 unsigned int kernel_ptbls; /* Number of KVA ptbls. */
156 * If user pmap is processed with mmu_booke_remove and the resident count
157 * drops to 0, there are no more pages to remove, so we need not continue.
159 #define PMAP_REMOVE_DONE(pmap) \
160 ((pmap) != kernel_pmap && (pmap)->pm_stats.resident_count == 0)
162 extern void tid_flush(tlbtid_t);
164 /**************************************************************************/
165 /* TLB and TID handling */
166 /**************************************************************************/
168 /* Translation ID busy table */
169 static volatile pmap_t tidbusy[MAXCPU][TID_MAX + 1];
172 * TLB0 capabilities (entry, way numbers etc.). These can vary between e500
173 * core revisions and should be read from h/w registers during early config.
175 uint32_t tlb0_entries;
177 uint32_t tlb0_entries_per_way;
179 #define TLB0_ENTRIES (tlb0_entries)
180 #define TLB0_WAYS (tlb0_ways)
181 #define TLB0_ENTRIES_PER_WAY (tlb0_entries_per_way)
183 #define TLB1_ENTRIES 16
185 /* In-ram copy of the TLB1 */
186 static tlb_entry_t tlb1[TLB1_ENTRIES];
188 /* Next free entry in the TLB1 */
189 static unsigned int tlb1_idx;
190 static vm_offset_t tlb1_map_base = VM_MAX_KERNEL_ADDRESS;
192 static tlbtid_t tid_alloc(struct pmap *);
194 static void tlb_print_entry(int, uint32_t, uint32_t, uint32_t, uint32_t);
196 static int tlb1_set_entry(vm_offset_t, vm_offset_t, vm_size_t, uint32_t);
197 static void tlb1_write_entry(unsigned int);
198 static int tlb1_iomapped(int, vm_paddr_t, vm_size_t, vm_offset_t *);
199 static vm_size_t tlb1_mapin_region(vm_offset_t, vm_paddr_t, vm_size_t);
201 static vm_size_t tsize2size(unsigned int);
202 static unsigned int size2tsize(vm_size_t);
203 static unsigned int ilog2(unsigned int);
205 static void set_mas4_defaults(void);
207 static inline void tlb0_flush_entry(vm_offset_t);
208 static inline unsigned int tlb0_tableidx(vm_offset_t, unsigned int);
210 /**************************************************************************/
211 /* Page table management */
212 /**************************************************************************/
214 static struct rwlock_padalign pvh_global_lock;
216 /* Data for the pv entry allocation mechanism */
217 static uma_zone_t pvzone;
218 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
220 #define PV_ENTRY_ZONE_MIN 2048 /* min pv entries in uma zone */
222 #ifndef PMAP_SHPGPERPROC
223 #define PMAP_SHPGPERPROC 200
226 static void ptbl_init(void);
227 static struct ptbl_buf *ptbl_buf_alloc(void);
228 static void ptbl_buf_free(struct ptbl_buf *);
229 static void ptbl_free_pmap_ptbl(pmap_t, pte_t *);
231 static pte_t *ptbl_alloc(mmu_t, pmap_t, unsigned int);
232 static void ptbl_free(mmu_t, pmap_t, unsigned int);
233 static void ptbl_hold(mmu_t, pmap_t, unsigned int);
234 static int ptbl_unhold(mmu_t, pmap_t, unsigned int);
236 static vm_paddr_t pte_vatopa(mmu_t, pmap_t, vm_offset_t);
237 static pte_t *pte_find(mmu_t, pmap_t, vm_offset_t);
238 static void pte_enter(mmu_t, pmap_t, vm_page_t, vm_offset_t, uint32_t);
239 static int pte_remove(mmu_t, pmap_t, vm_offset_t, uint8_t);
241 static pv_entry_t pv_alloc(void);
242 static void pv_free(pv_entry_t);
243 static void pv_insert(pmap_t, vm_offset_t, vm_page_t);
244 static void pv_remove(pmap_t, vm_offset_t, vm_page_t);
246 /* Number of kva ptbl buffers, each covering one ptbl (PTBL_PAGES). */
247 #define PTBL_BUFS (128 * 16)
250 TAILQ_ENTRY(ptbl_buf) link; /* list link */
251 vm_offset_t kva; /* va of mapping */
254 /* ptbl free list and a lock used for access synchronization. */
255 static TAILQ_HEAD(, ptbl_buf) ptbl_buf_freelist;
256 static struct mtx ptbl_buf_freelist_lock;
258 /* Base address of kva space allocated fot ptbl bufs. */
259 static vm_offset_t ptbl_buf_pool_vabase;
261 /* Pointer to ptbl_buf structures. */
262 static struct ptbl_buf *ptbl_bufs;
264 void pmap_bootstrap_ap(volatile uint32_t *);
267 * Kernel MMU interface
269 static void mmu_booke_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t);
270 static void mmu_booke_clear_modify(mmu_t, vm_page_t);
271 static void mmu_booke_copy(mmu_t, pmap_t, pmap_t, vm_offset_t,
272 vm_size_t, vm_offset_t);
273 static void mmu_booke_copy_page(mmu_t, vm_page_t, vm_page_t);
274 static void mmu_booke_copy_pages(mmu_t, vm_page_t *,
275 vm_offset_t, vm_page_t *, vm_offset_t, int);
276 static void mmu_booke_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t,
277 vm_prot_t, boolean_t);
278 static void mmu_booke_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t,
279 vm_page_t, vm_prot_t);
280 static void mmu_booke_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t,
282 static vm_paddr_t mmu_booke_extract(mmu_t, pmap_t, vm_offset_t);
283 static vm_page_t mmu_booke_extract_and_hold(mmu_t, pmap_t, vm_offset_t,
285 static void mmu_booke_init(mmu_t);
286 static boolean_t mmu_booke_is_modified(mmu_t, vm_page_t);
287 static boolean_t mmu_booke_is_prefaultable(mmu_t, pmap_t, vm_offset_t);
288 static boolean_t mmu_booke_is_referenced(mmu_t, vm_page_t);
289 static int mmu_booke_ts_referenced(mmu_t, vm_page_t);
290 static vm_offset_t mmu_booke_map(mmu_t, vm_offset_t *, vm_paddr_t, vm_paddr_t,
292 static int mmu_booke_mincore(mmu_t, pmap_t, vm_offset_t,
294 static void mmu_booke_object_init_pt(mmu_t, pmap_t, vm_offset_t,
295 vm_object_t, vm_pindex_t, vm_size_t);
296 static boolean_t mmu_booke_page_exists_quick(mmu_t, pmap_t, vm_page_t);
297 static void mmu_booke_page_init(mmu_t, vm_page_t);
298 static int mmu_booke_page_wired_mappings(mmu_t, vm_page_t);
299 static void mmu_booke_pinit(mmu_t, pmap_t);
300 static void mmu_booke_pinit0(mmu_t, pmap_t);
301 static void mmu_booke_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t,
303 static void mmu_booke_qenter(mmu_t, vm_offset_t, vm_page_t *, int);
304 static void mmu_booke_qremove(mmu_t, vm_offset_t, int);
305 static void mmu_booke_release(mmu_t, pmap_t);
306 static void mmu_booke_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t);
307 static void mmu_booke_remove_all(mmu_t, vm_page_t);
308 static void mmu_booke_remove_write(mmu_t, vm_page_t);
309 static void mmu_booke_zero_page(mmu_t, vm_page_t);
310 static void mmu_booke_zero_page_area(mmu_t, vm_page_t, int, int);
311 static void mmu_booke_zero_page_idle(mmu_t, vm_page_t);
312 static void mmu_booke_activate(mmu_t, struct thread *);
313 static void mmu_booke_deactivate(mmu_t, struct thread *);
314 static void mmu_booke_bootstrap(mmu_t, vm_offset_t, vm_offset_t);
315 static void *mmu_booke_mapdev(mmu_t, vm_paddr_t, vm_size_t);
316 static void *mmu_booke_mapdev_attr(mmu_t, vm_paddr_t, vm_size_t, vm_memattr_t);
317 static void mmu_booke_unmapdev(mmu_t, vm_offset_t, vm_size_t);
318 static vm_paddr_t mmu_booke_kextract(mmu_t, vm_offset_t);
319 static void mmu_booke_kenter(mmu_t, vm_offset_t, vm_paddr_t);
320 static void mmu_booke_kenter_attr(mmu_t, vm_offset_t, vm_paddr_t, vm_memattr_t);
321 static void mmu_booke_kremove(mmu_t, vm_offset_t);
322 static boolean_t mmu_booke_dev_direct_mapped(mmu_t, vm_paddr_t, vm_size_t);
323 static void mmu_booke_sync_icache(mmu_t, pmap_t, vm_offset_t,
325 static vm_offset_t mmu_booke_dumpsys_map(mmu_t, struct pmap_md *,
326 vm_size_t, vm_size_t *);
327 static void mmu_booke_dumpsys_unmap(mmu_t, struct pmap_md *,
328 vm_size_t, vm_offset_t);
329 static struct pmap_md *mmu_booke_scan_md(mmu_t, struct pmap_md *);
331 static mmu_method_t mmu_booke_methods[] = {
332 /* pmap dispatcher interface */
333 MMUMETHOD(mmu_change_wiring, mmu_booke_change_wiring),
334 MMUMETHOD(mmu_clear_modify, mmu_booke_clear_modify),
335 MMUMETHOD(mmu_copy, mmu_booke_copy),
336 MMUMETHOD(mmu_copy_page, mmu_booke_copy_page),
337 MMUMETHOD(mmu_copy_pages, mmu_booke_copy_pages),
338 MMUMETHOD(mmu_enter, mmu_booke_enter),
339 MMUMETHOD(mmu_enter_object, mmu_booke_enter_object),
340 MMUMETHOD(mmu_enter_quick, mmu_booke_enter_quick),
341 MMUMETHOD(mmu_extract, mmu_booke_extract),
342 MMUMETHOD(mmu_extract_and_hold, mmu_booke_extract_and_hold),
343 MMUMETHOD(mmu_init, mmu_booke_init),
344 MMUMETHOD(mmu_is_modified, mmu_booke_is_modified),
345 MMUMETHOD(mmu_is_prefaultable, mmu_booke_is_prefaultable),
346 MMUMETHOD(mmu_is_referenced, mmu_booke_is_referenced),
347 MMUMETHOD(mmu_ts_referenced, mmu_booke_ts_referenced),
348 MMUMETHOD(mmu_map, mmu_booke_map),
349 MMUMETHOD(mmu_mincore, mmu_booke_mincore),
350 MMUMETHOD(mmu_object_init_pt, mmu_booke_object_init_pt),
351 MMUMETHOD(mmu_page_exists_quick,mmu_booke_page_exists_quick),
352 MMUMETHOD(mmu_page_init, mmu_booke_page_init),
353 MMUMETHOD(mmu_page_wired_mappings, mmu_booke_page_wired_mappings),
354 MMUMETHOD(mmu_pinit, mmu_booke_pinit),
355 MMUMETHOD(mmu_pinit0, mmu_booke_pinit0),
356 MMUMETHOD(mmu_protect, mmu_booke_protect),
357 MMUMETHOD(mmu_qenter, mmu_booke_qenter),
358 MMUMETHOD(mmu_qremove, mmu_booke_qremove),
359 MMUMETHOD(mmu_release, mmu_booke_release),
360 MMUMETHOD(mmu_remove, mmu_booke_remove),
361 MMUMETHOD(mmu_remove_all, mmu_booke_remove_all),
362 MMUMETHOD(mmu_remove_write, mmu_booke_remove_write),
363 MMUMETHOD(mmu_sync_icache, mmu_booke_sync_icache),
364 MMUMETHOD(mmu_zero_page, mmu_booke_zero_page),
365 MMUMETHOD(mmu_zero_page_area, mmu_booke_zero_page_area),
366 MMUMETHOD(mmu_zero_page_idle, mmu_booke_zero_page_idle),
367 MMUMETHOD(mmu_activate, mmu_booke_activate),
368 MMUMETHOD(mmu_deactivate, mmu_booke_deactivate),
370 /* Internal interfaces */
371 MMUMETHOD(mmu_bootstrap, mmu_booke_bootstrap),
372 MMUMETHOD(mmu_dev_direct_mapped,mmu_booke_dev_direct_mapped),
373 MMUMETHOD(mmu_mapdev, mmu_booke_mapdev),
374 MMUMETHOD(mmu_mapdev_attr, mmu_booke_mapdev_attr),
375 MMUMETHOD(mmu_kenter, mmu_booke_kenter),
376 MMUMETHOD(mmu_kenter_attr, mmu_booke_kenter_attr),
377 MMUMETHOD(mmu_kextract, mmu_booke_kextract),
378 /* MMUMETHOD(mmu_kremove, mmu_booke_kremove), */
379 MMUMETHOD(mmu_unmapdev, mmu_booke_unmapdev),
381 /* dumpsys() support */
382 MMUMETHOD(mmu_dumpsys_map, mmu_booke_dumpsys_map),
383 MMUMETHOD(mmu_dumpsys_unmap, mmu_booke_dumpsys_unmap),
384 MMUMETHOD(mmu_scan_md, mmu_booke_scan_md),
389 MMU_DEF(booke_mmu, MMU_TYPE_BOOKE, mmu_booke_methods, 0);
391 static __inline uint32_t
392 tlb_calc_wimg(vm_offset_t pa, vm_memattr_t ma)
397 if (ma != VM_MEMATTR_DEFAULT) {
399 case VM_MEMATTR_UNCACHEABLE:
400 return (PTE_I | PTE_G);
401 case VM_MEMATTR_WRITE_COMBINING:
402 case VM_MEMATTR_WRITE_BACK:
403 case VM_MEMATTR_PREFETCHABLE:
405 case VM_MEMATTR_WRITE_THROUGH:
406 return (PTE_W | PTE_M);
411 * Assume the page is cache inhibited and access is guarded unless
412 * it's in our available memory array.
414 attrib = _TLB_ENTRY_IO;
415 for (i = 0; i < physmem_regions_sz; i++) {
416 if ((pa >= physmem_regions[i].mr_start) &&
417 (pa < (physmem_regions[i].mr_start +
418 physmem_regions[i].mr_size))) {
419 attrib = _TLB_ENTRY_MEM;
436 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
439 CTR3(KTR_PMAP, "%s: tlb miss LOCK of CPU=%d, "
440 "tlb_lock=%p", __func__, pc->pc_cpuid, pc->pc_booke_tlb_lock);
442 KASSERT((pc->pc_cpuid != PCPU_GET(cpuid)),
443 ("tlb_miss_lock: tried to lock self"));
445 tlb_lock(pc->pc_booke_tlb_lock);
447 CTR1(KTR_PMAP, "%s: locked", __func__);
454 tlb_miss_unlock(void)
462 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
464 CTR2(KTR_PMAP, "%s: tlb miss UNLOCK of CPU=%d",
465 __func__, pc->pc_cpuid);
467 tlb_unlock(pc->pc_booke_tlb_lock);
469 CTR1(KTR_PMAP, "%s: unlocked", __func__);
475 /* Return number of entries in TLB0. */
477 tlb0_get_tlbconf(void)
481 tlb0_cfg = mfspr(SPR_TLB0CFG);
482 tlb0_entries = tlb0_cfg & TLBCFG_NENTRY_MASK;
483 tlb0_ways = (tlb0_cfg & TLBCFG_ASSOC_MASK) >> TLBCFG_ASSOC_SHIFT;
484 tlb0_entries_per_way = tlb0_entries / tlb0_ways;
487 /* Initialize pool of kva ptbl buffers. */
493 CTR3(KTR_PMAP, "%s: s (ptbl_bufs = 0x%08x size 0x%08x)", __func__,
494 (uint32_t)ptbl_bufs, sizeof(struct ptbl_buf) * PTBL_BUFS);
495 CTR3(KTR_PMAP, "%s: s (ptbl_buf_pool_vabase = 0x%08x size = 0x%08x)",
496 __func__, ptbl_buf_pool_vabase, PTBL_BUFS * PTBL_PAGES * PAGE_SIZE);
498 mtx_init(&ptbl_buf_freelist_lock, "ptbl bufs lock", NULL, MTX_DEF);
499 TAILQ_INIT(&ptbl_buf_freelist);
501 for (i = 0; i < PTBL_BUFS; i++) {
502 ptbl_bufs[i].kva = ptbl_buf_pool_vabase + i * PTBL_PAGES * PAGE_SIZE;
503 TAILQ_INSERT_TAIL(&ptbl_buf_freelist, &ptbl_bufs[i], link);
507 /* Get a ptbl_buf from the freelist. */
508 static struct ptbl_buf *
511 struct ptbl_buf *buf;
513 mtx_lock(&ptbl_buf_freelist_lock);
514 buf = TAILQ_FIRST(&ptbl_buf_freelist);
516 TAILQ_REMOVE(&ptbl_buf_freelist, buf, link);
517 mtx_unlock(&ptbl_buf_freelist_lock);
519 CTR2(KTR_PMAP, "%s: buf = %p", __func__, buf);
524 /* Return ptbl buff to free pool. */
526 ptbl_buf_free(struct ptbl_buf *buf)
529 CTR2(KTR_PMAP, "%s: buf = %p", __func__, buf);
531 mtx_lock(&ptbl_buf_freelist_lock);
532 TAILQ_INSERT_TAIL(&ptbl_buf_freelist, buf, link);
533 mtx_unlock(&ptbl_buf_freelist_lock);
537 * Search the list of allocated ptbl bufs and find on list of allocated ptbls
540 ptbl_free_pmap_ptbl(pmap_t pmap, pte_t *ptbl)
542 struct ptbl_buf *pbuf;
544 CTR2(KTR_PMAP, "%s: ptbl = %p", __func__, ptbl);
546 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
548 TAILQ_FOREACH(pbuf, &pmap->pm_ptbl_list, link)
549 if (pbuf->kva == (vm_offset_t)ptbl) {
550 /* Remove from pmap ptbl buf list. */
551 TAILQ_REMOVE(&pmap->pm_ptbl_list, pbuf, link);
553 /* Free corresponding ptbl buf. */
559 /* Allocate page table. */
561 ptbl_alloc(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
563 vm_page_t mtbl[PTBL_PAGES];
565 struct ptbl_buf *pbuf;
570 CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
571 (pmap == kernel_pmap), pdir_idx);
573 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
574 ("ptbl_alloc: invalid pdir_idx"));
575 KASSERT((pmap->pm_pdir[pdir_idx] == NULL),
576 ("pte_alloc: valid ptbl entry exists!"));
578 pbuf = ptbl_buf_alloc();
580 panic("pte_alloc: couldn't alloc kernel virtual memory");
582 ptbl = (pte_t *)pbuf->kva;
584 CTR2(KTR_PMAP, "%s: ptbl kva = %p", __func__, ptbl);
586 /* Allocate ptbl pages, this will sleep! */
587 for (i = 0; i < PTBL_PAGES; i++) {
588 pidx = (PTBL_PAGES * pdir_idx) + i;
589 while ((m = vm_page_alloc(NULL, pidx,
590 VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
593 rw_wunlock(&pvh_global_lock);
595 rw_wlock(&pvh_global_lock);
601 /* Map allocated pages into kernel_pmap. */
602 mmu_booke_qenter(mmu, (vm_offset_t)ptbl, mtbl, PTBL_PAGES);
604 /* Zero whole ptbl. */
605 bzero((caddr_t)ptbl, PTBL_PAGES * PAGE_SIZE);
607 /* Add pbuf to the pmap ptbl bufs list. */
608 TAILQ_INSERT_TAIL(&pmap->pm_ptbl_list, pbuf, link);
613 /* Free ptbl pages and invalidate pdir entry. */
615 ptbl_free(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
623 CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
624 (pmap == kernel_pmap), pdir_idx);
626 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
627 ("ptbl_free: invalid pdir_idx"));
629 ptbl = pmap->pm_pdir[pdir_idx];
631 CTR2(KTR_PMAP, "%s: ptbl = %p", __func__, ptbl);
633 KASSERT((ptbl != NULL), ("ptbl_free: null ptbl"));
636 * Invalidate the pdir entry as soon as possible, so that other CPUs
637 * don't attempt to look up the page tables we are releasing.
639 mtx_lock_spin(&tlbivax_mutex);
642 pmap->pm_pdir[pdir_idx] = NULL;
645 mtx_unlock_spin(&tlbivax_mutex);
647 for (i = 0; i < PTBL_PAGES; i++) {
648 va = ((vm_offset_t)ptbl + (i * PAGE_SIZE));
649 pa = pte_vatopa(mmu, kernel_pmap, va);
650 m = PHYS_TO_VM_PAGE(pa);
651 vm_page_free_zero(m);
652 atomic_subtract_int(&cnt.v_wire_count, 1);
653 mmu_booke_kremove(mmu, va);
656 ptbl_free_pmap_ptbl(pmap, ptbl);
660 * Decrement ptbl pages hold count and attempt to free ptbl pages.
661 * Called when removing pte entry from ptbl.
663 * Return 1 if ptbl pages were freed.
666 ptbl_unhold(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
673 CTR4(KTR_PMAP, "%s: pmap = %p su = %d pdir_idx = %d", __func__, pmap,
674 (pmap == kernel_pmap), pdir_idx);
676 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
677 ("ptbl_unhold: invalid pdir_idx"));
678 KASSERT((pmap != kernel_pmap),
679 ("ptbl_unhold: unholding kernel ptbl!"));
681 ptbl = pmap->pm_pdir[pdir_idx];
683 //debugf("ptbl_unhold: ptbl = 0x%08x\n", (u_int32_t)ptbl);
684 KASSERT(((vm_offset_t)ptbl >= VM_MIN_KERNEL_ADDRESS),
685 ("ptbl_unhold: non kva ptbl"));
687 /* decrement hold count */
688 for (i = 0; i < PTBL_PAGES; i++) {
689 pa = pte_vatopa(mmu, kernel_pmap,
690 (vm_offset_t)ptbl + (i * PAGE_SIZE));
691 m = PHYS_TO_VM_PAGE(pa);
696 * Free ptbl pages if there are no pte etries in this ptbl.
697 * wire_count has the same value for all ptbl pages, so check the last
700 if (m->wire_count == 0) {
701 ptbl_free(mmu, pmap, pdir_idx);
703 //debugf("ptbl_unhold: e (freed ptbl)\n");
711 * Increment hold count for ptbl pages. This routine is used when a new pte
712 * entry is being inserted into the ptbl.
715 ptbl_hold(mmu_t mmu, pmap_t pmap, unsigned int pdir_idx)
722 CTR3(KTR_PMAP, "%s: pmap = %p pdir_idx = %d", __func__, pmap,
725 KASSERT((pdir_idx <= (VM_MAXUSER_ADDRESS / PDIR_SIZE)),
726 ("ptbl_hold: invalid pdir_idx"));
727 KASSERT((pmap != kernel_pmap),
728 ("ptbl_hold: holding kernel ptbl!"));
730 ptbl = pmap->pm_pdir[pdir_idx];
732 KASSERT((ptbl != NULL), ("ptbl_hold: null ptbl"));
734 for (i = 0; i < PTBL_PAGES; i++) {
735 pa = pte_vatopa(mmu, kernel_pmap,
736 (vm_offset_t)ptbl + (i * PAGE_SIZE));
737 m = PHYS_TO_VM_PAGE(pa);
742 /* Allocate pv_entry structure. */
749 if (pv_entry_count > pv_entry_high_water)
751 pv = uma_zalloc(pvzone, M_NOWAIT);
756 /* Free pv_entry structure. */
758 pv_free(pv_entry_t pve)
762 uma_zfree(pvzone, pve);
766 /* Allocate and initialize pv_entry structure. */
768 pv_insert(pmap_t pmap, vm_offset_t va, vm_page_t m)
772 //int su = (pmap == kernel_pmap);
773 //debugf("pv_insert: s (su = %d pmap = 0x%08x va = 0x%08x m = 0x%08x)\n", su,
774 // (u_int32_t)pmap, va, (u_int32_t)m);
778 panic("pv_insert: no pv entries!");
784 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
785 rw_assert(&pvh_global_lock, RA_WLOCKED);
787 TAILQ_INSERT_TAIL(&m->md.pv_list, pve, pv_link);
789 //debugf("pv_insert: e\n");
792 /* Destroy pv entry. */
794 pv_remove(pmap_t pmap, vm_offset_t va, vm_page_t m)
798 //int su = (pmap == kernel_pmap);
799 //debugf("pv_remove: s (su = %d pmap = 0x%08x va = 0x%08x)\n", su, (u_int32_t)pmap, va);
801 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
802 rw_assert(&pvh_global_lock, RA_WLOCKED);
805 TAILQ_FOREACH(pve, &m->md.pv_list, pv_link) {
806 if ((pmap == pve->pv_pmap) && (va == pve->pv_va)) {
807 /* remove from pv_list */
808 TAILQ_REMOVE(&m->md.pv_list, pve, pv_link);
809 if (TAILQ_EMPTY(&m->md.pv_list))
810 vm_page_aflag_clear(m, PGA_WRITEABLE);
812 /* free pv entry struct */
818 //debugf("pv_remove: e\n");
822 * Clean pte entry, try to free page table page if requested.
824 * Return 1 if ptbl pages were freed, otherwise return 0.
827 pte_remove(mmu_t mmu, pmap_t pmap, vm_offset_t va, uint8_t flags)
829 unsigned int pdir_idx = PDIR_IDX(va);
830 unsigned int ptbl_idx = PTBL_IDX(va);
835 //int su = (pmap == kernel_pmap);
836 //debugf("pte_remove: s (su = %d pmap = 0x%08x va = 0x%08x flags = %d)\n",
837 // su, (u_int32_t)pmap, va, flags);
839 ptbl = pmap->pm_pdir[pdir_idx];
840 KASSERT(ptbl, ("pte_remove: null ptbl"));
842 pte = &ptbl[ptbl_idx];
844 if (pte == NULL || !PTE_ISVALID(pte))
847 if (PTE_ISWIRED(pte))
848 pmap->pm_stats.wired_count--;
850 /* Handle managed entry. */
851 if (PTE_ISMANAGED(pte)) {
852 /* Get vm_page_t for mapped pte. */
853 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
855 if (PTE_ISMODIFIED(pte))
858 if (PTE_ISREFERENCED(pte))
859 vm_page_aflag_set(m, PGA_REFERENCED);
861 pv_remove(pmap, va, m);
864 mtx_lock_spin(&tlbivax_mutex);
867 tlb0_flush_entry(va);
872 mtx_unlock_spin(&tlbivax_mutex);
874 pmap->pm_stats.resident_count--;
876 if (flags & PTBL_UNHOLD) {
877 //debugf("pte_remove: e (unhold)\n");
878 return (ptbl_unhold(mmu, pmap, pdir_idx));
881 //debugf("pte_remove: e\n");
886 * Insert PTE for a given page and virtual address.
889 pte_enter(mmu_t mmu, pmap_t pmap, vm_page_t m, vm_offset_t va, uint32_t flags)
891 unsigned int pdir_idx = PDIR_IDX(va);
892 unsigned int ptbl_idx = PTBL_IDX(va);
895 CTR4(KTR_PMAP, "%s: su = %d pmap = %p va = %p", __func__,
896 pmap == kernel_pmap, pmap, va);
898 /* Get the page table pointer. */
899 ptbl = pmap->pm_pdir[pdir_idx];
902 /* Allocate page table pages. */
903 ptbl = ptbl_alloc(mmu, pmap, pdir_idx);
906 * Check if there is valid mapping for requested
907 * va, if there is, remove it.
909 pte = &pmap->pm_pdir[pdir_idx][ptbl_idx];
910 if (PTE_ISVALID(pte)) {
911 pte_remove(mmu, pmap, va, PTBL_HOLD);
914 * pte is not used, increment hold count
917 if (pmap != kernel_pmap)
918 ptbl_hold(mmu, pmap, pdir_idx);
923 * Insert pv_entry into pv_list for mapped page if part of managed
926 if ((m->oflags & VPO_UNMANAGED) == 0) {
927 flags |= PTE_MANAGED;
929 /* Create and insert pv entry. */
930 pv_insert(pmap, va, m);
933 pmap->pm_stats.resident_count++;
935 mtx_lock_spin(&tlbivax_mutex);
938 tlb0_flush_entry(va);
939 if (pmap->pm_pdir[pdir_idx] == NULL) {
941 * If we just allocated a new page table, hook it in
944 pmap->pm_pdir[pdir_idx] = ptbl;
946 pte = &(pmap->pm_pdir[pdir_idx][ptbl_idx]);
947 pte->rpn = VM_PAGE_TO_PHYS(m) & ~PTE_PA_MASK;
948 pte->flags |= (PTE_VALID | flags);
951 mtx_unlock_spin(&tlbivax_mutex);
954 /* Return the pa for the given pmap/va. */
956 pte_vatopa(mmu_t mmu, pmap_t pmap, vm_offset_t va)
961 pte = pte_find(mmu, pmap, va);
962 if ((pte != NULL) && PTE_ISVALID(pte))
963 pa = (PTE_PA(pte) | (va & PTE_PA_MASK));
967 /* Get a pointer to a PTE in a page table. */
969 pte_find(mmu_t mmu, pmap_t pmap, vm_offset_t va)
971 unsigned int pdir_idx = PDIR_IDX(va);
972 unsigned int ptbl_idx = PTBL_IDX(va);
974 KASSERT((pmap != NULL), ("pte_find: invalid pmap"));
976 if (pmap->pm_pdir[pdir_idx])
977 return (&(pmap->pm_pdir[pdir_idx][ptbl_idx]));
982 /**************************************************************************/
984 /**************************************************************************/
987 * This is called during booke_init, before the system is really initialized.
990 mmu_booke_bootstrap(mmu_t mmu, vm_offset_t start, vm_offset_t kernelend)
992 vm_offset_t phys_kernelend;
993 struct mem_region *mp, *mp1;
996 u_int phys_avail_count;
997 vm_size_t physsz, hwphyssz, kstack0_sz;
998 vm_offset_t kernel_pdir, kstack0, va;
999 vm_paddr_t kstack0_phys;
1003 debugf("mmu_booke_bootstrap: entered\n");
1005 /* Initialize invalidation mutex */
1006 mtx_init(&tlbivax_mutex, "tlbivax", NULL, MTX_SPIN);
1008 /* Read TLB0 size and associativity. */
1012 * Align kernel start and end address (kernel image).
1013 * Note that kernel end does not necessarily relate to kernsize.
1014 * kernsize is the size of the kernel that is actually mapped.
1015 * Also note that "start - 1" is deliberate. With SMP, the
1016 * entry point is exactly a page from the actual load address.
1017 * As such, trunc_page() has no effect and we're off by a page.
1018 * Since we always have the ELF header between the load address
1019 * and the entry point, we can safely subtract 1 to compensate.
1021 kernstart = trunc_page(start - 1);
1022 data_start = round_page(kernelend);
1023 data_end = data_start;
1026 * Addresses of preloaded modules (like file systems) use
1027 * physical addresses. Make sure we relocate those into
1028 * virtual addresses.
1030 preload_addr_relocate = kernstart - kernload;
1032 /* Allocate the dynamic per-cpu area. */
1033 dpcpu = (void *)data_end;
1034 data_end += DPCPU_SIZE;
1036 /* Allocate space for the message buffer. */
1037 msgbufp = (struct msgbuf *)data_end;
1038 data_end += msgbufsize;
1039 debugf(" msgbufp at 0x%08x end = 0x%08x\n", (uint32_t)msgbufp,
1042 data_end = round_page(data_end);
1044 /* Allocate space for ptbl_bufs. */
1045 ptbl_bufs = (struct ptbl_buf *)data_end;
1046 data_end += sizeof(struct ptbl_buf) * PTBL_BUFS;
1047 debugf(" ptbl_bufs at 0x%08x end = 0x%08x\n", (uint32_t)ptbl_bufs,
1050 data_end = round_page(data_end);
1052 /* Allocate PTE tables for kernel KVA. */
1053 kernel_pdir = data_end;
1054 kernel_ptbls = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS +
1055 PDIR_SIZE - 1) / PDIR_SIZE;
1056 data_end += kernel_ptbls * PTBL_PAGES * PAGE_SIZE;
1057 debugf(" kernel ptbls: %d\n", kernel_ptbls);
1058 debugf(" kernel pdir at 0x%08x end = 0x%08x\n", kernel_pdir, data_end);
1060 debugf(" data_end: 0x%08x\n", data_end);
1061 if (data_end - kernstart > kernsize) {
1062 kernsize += tlb1_mapin_region(kernstart + kernsize,
1063 kernload + kernsize, (data_end - kernstart) - kernsize);
1065 data_end = kernstart + kernsize;
1066 debugf(" updated data_end: 0x%08x\n", data_end);
1069 * Clear the structures - note we can only do it safely after the
1070 * possible additional TLB1 translations are in place (above) so that
1071 * all range up to the currently calculated 'data_end' is covered.
1073 dpcpu_init(dpcpu, 0);
1074 memset((void *)ptbl_bufs, 0, sizeof(struct ptbl_buf) * PTBL_SIZE);
1075 memset((void *)kernel_pdir, 0, kernel_ptbls * PTBL_PAGES * PAGE_SIZE);
1077 /*******************************************************/
1078 /* Set the start and end of kva. */
1079 /*******************************************************/
1080 virtual_avail = round_page(data_end);
1081 virtual_end = VM_MAX_KERNEL_ADDRESS;
1083 /* Allocate KVA space for page zero/copy operations. */
1084 zero_page_va = virtual_avail;
1085 virtual_avail += PAGE_SIZE;
1086 zero_page_idle_va = virtual_avail;
1087 virtual_avail += PAGE_SIZE;
1088 copy_page_src_va = virtual_avail;
1089 virtual_avail += PAGE_SIZE;
1090 copy_page_dst_va = virtual_avail;
1091 virtual_avail += PAGE_SIZE;
1092 debugf("zero_page_va = 0x%08x\n", zero_page_va);
1093 debugf("zero_page_idle_va = 0x%08x\n", zero_page_idle_va);
1094 debugf("copy_page_src_va = 0x%08x\n", copy_page_src_va);
1095 debugf("copy_page_dst_va = 0x%08x\n", copy_page_dst_va);
1097 /* Initialize page zero/copy mutexes. */
1098 mtx_init(&zero_page_mutex, "mmu_booke_zero_page", NULL, MTX_DEF);
1099 mtx_init(©_page_mutex, "mmu_booke_copy_page", NULL, MTX_DEF);
1101 /* Allocate KVA space for ptbl bufs. */
1102 ptbl_buf_pool_vabase = virtual_avail;
1103 virtual_avail += PTBL_BUFS * PTBL_PAGES * PAGE_SIZE;
1104 debugf("ptbl_buf_pool_vabase = 0x%08x end = 0x%08x\n",
1105 ptbl_buf_pool_vabase, virtual_avail);
1107 /* Calculate corresponding physical addresses for the kernel region. */
1108 phys_kernelend = kernload + kernsize;
1109 debugf("kernel image and allocated data:\n");
1110 debugf(" kernload = 0x%08x\n", kernload);
1111 debugf(" kernstart = 0x%08x\n", kernstart);
1112 debugf(" kernsize = 0x%08x\n", kernsize);
1114 if (sizeof(phys_avail) / sizeof(phys_avail[0]) < availmem_regions_sz)
1115 panic("mmu_booke_bootstrap: phys_avail too small");
1118 * Remove kernel physical address range from avail regions list. Page
1119 * align all regions. Non-page aligned memory isn't very interesting
1120 * to us. Also, sort the entries for ascending addresses.
1123 /* Retrieve phys/avail mem regions */
1124 mem_regions(&physmem_regions, &physmem_regions_sz,
1125 &availmem_regions, &availmem_regions_sz);
1127 cnt = availmem_regions_sz;
1128 debugf("processing avail regions:\n");
1129 for (mp = availmem_regions; mp->mr_size; mp++) {
1131 e = mp->mr_start + mp->mr_size;
1132 debugf(" %08x-%08x -> ", s, e);
1133 /* Check whether this region holds all of the kernel. */
1134 if (s < kernload && e > phys_kernelend) {
1135 availmem_regions[cnt].mr_start = phys_kernelend;
1136 availmem_regions[cnt++].mr_size = e - phys_kernelend;
1139 /* Look whether this regions starts within the kernel. */
1140 if (s >= kernload && s < phys_kernelend) {
1141 if (e <= phys_kernelend)
1145 /* Now look whether this region ends within the kernel. */
1146 if (e > kernload && e <= phys_kernelend) {
1151 /* Now page align the start and size of the region. */
1157 debugf("%08x-%08x = %x\n", s, e, sz);
1159 /* Check whether some memory is left here. */
1163 (cnt - (mp - availmem_regions)) * sizeof(*mp));
1169 /* Do an insertion sort. */
1170 for (mp1 = availmem_regions; mp1 < mp; mp1++)
1171 if (s < mp1->mr_start)
1174 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
1182 availmem_regions_sz = cnt;
1184 /*******************************************************/
1185 /* Steal physical memory for kernel stack from the end */
1186 /* of the first avail region */
1187 /*******************************************************/
1188 kstack0_sz = KSTACK_PAGES * PAGE_SIZE;
1189 kstack0_phys = availmem_regions[0].mr_start +
1190 availmem_regions[0].mr_size;
1191 kstack0_phys -= kstack0_sz;
1192 availmem_regions[0].mr_size -= kstack0_sz;
1194 /*******************************************************/
1195 /* Fill in phys_avail table, based on availmem_regions */
1196 /*******************************************************/
1197 phys_avail_count = 0;
1200 TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz);
1202 debugf("fill in phys_avail:\n");
1203 for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
1205 debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
1206 availmem_regions[i].mr_start,
1207 availmem_regions[i].mr_start +
1208 availmem_regions[i].mr_size,
1209 availmem_regions[i].mr_size);
1211 if (hwphyssz != 0 &&
1212 (physsz + availmem_regions[i].mr_size) >= hwphyssz) {
1213 debugf(" hw.physmem adjust\n");
1214 if (physsz < hwphyssz) {
1215 phys_avail[j] = availmem_regions[i].mr_start;
1217 availmem_regions[i].mr_start +
1225 phys_avail[j] = availmem_regions[i].mr_start;
1226 phys_avail[j + 1] = availmem_regions[i].mr_start +
1227 availmem_regions[i].mr_size;
1229 physsz += availmem_regions[i].mr_size;
1231 physmem = btoc(physsz);
1233 /* Calculate the last available physical address. */
1234 for (i = 0; phys_avail[i + 2] != 0; i += 2)
1236 Maxmem = powerpc_btop(phys_avail[i + 1]);
1238 debugf("Maxmem = 0x%08lx\n", Maxmem);
1239 debugf("phys_avail_count = %d\n", phys_avail_count);
1240 debugf("physsz = 0x%08x physmem = %ld (0x%08lx)\n", physsz, physmem,
1243 /*******************************************************/
1244 /* Initialize (statically allocated) kernel pmap. */
1245 /*******************************************************/
1246 PMAP_LOCK_INIT(kernel_pmap);
1247 kptbl_min = VM_MIN_KERNEL_ADDRESS / PDIR_SIZE;
1249 debugf("kernel_pmap = 0x%08x\n", (uint32_t)kernel_pmap);
1250 debugf("kptbl_min = %d, kernel_ptbls = %d\n", kptbl_min, kernel_ptbls);
1251 debugf("kernel pdir range: 0x%08x - 0x%08x\n",
1252 kptbl_min * PDIR_SIZE, (kptbl_min + kernel_ptbls) * PDIR_SIZE - 1);
1254 /* Initialize kernel pdir */
1255 for (i = 0; i < kernel_ptbls; i++)
1256 kernel_pmap->pm_pdir[kptbl_min + i] =
1257 (pte_t *)(kernel_pdir + (i * PAGE_SIZE * PTBL_PAGES));
1259 for (i = 0; i < MAXCPU; i++) {
1260 kernel_pmap->pm_tid[i] = TID_KERNEL;
1262 /* Initialize each CPU's tidbusy entry 0 with kernel_pmap */
1263 tidbusy[i][0] = kernel_pmap;
1267 * Fill in PTEs covering kernel code and data. They are not required
1268 * for address translation, as this area is covered by static TLB1
1269 * entries, but for pte_vatopa() to work correctly with kernel area
1272 for (va = kernstart; va < data_end; va += PAGE_SIZE) {
1273 pte = &(kernel_pmap->pm_pdir[PDIR_IDX(va)][PTBL_IDX(va)]);
1274 pte->rpn = kernload + (va - kernstart);
1275 pte->flags = PTE_M | PTE_SR | PTE_SW | PTE_SX | PTE_WIRED |
1278 /* Mark kernel_pmap active on all CPUs */
1279 CPU_FILL(&kernel_pmap->pm_active);
1282 * Initialize the global pv list lock.
1284 rw_init(&pvh_global_lock, "pmap pv global");
1286 /*******************************************************/
1288 /*******************************************************/
1290 /* Enter kstack0 into kernel map, provide guard page */
1291 kstack0 = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE;
1292 thread0.td_kstack = kstack0;
1293 thread0.td_kstack_pages = KSTACK_PAGES;
1295 debugf("kstack_sz = 0x%08x\n", kstack0_sz);
1296 debugf("kstack0_phys at 0x%08x - 0x%08x\n",
1297 kstack0_phys, kstack0_phys + kstack0_sz);
1298 debugf("kstack0 at 0x%08x - 0x%08x\n", kstack0, kstack0 + kstack0_sz);
1300 virtual_avail += KSTACK_GUARD_PAGES * PAGE_SIZE + kstack0_sz;
1301 for (i = 0; i < KSTACK_PAGES; i++) {
1302 mmu_booke_kenter(mmu, kstack0, kstack0_phys);
1303 kstack0 += PAGE_SIZE;
1304 kstack0_phys += PAGE_SIZE;
1307 debugf("virtual_avail = %08x\n", virtual_avail);
1308 debugf("virtual_end = %08x\n", virtual_end);
1310 debugf("mmu_booke_bootstrap: exit\n");
1314 pmap_bootstrap_ap(volatile uint32_t *trcp __unused)
1319 * Finish TLB1 configuration: the BSP already set up its TLB1 and we
1320 * have the snapshot of its contents in the s/w tlb1[] table, so use
1321 * these values directly to (re)program AP's TLB1 hardware.
1323 for (i = bp_ntlb1s; i < tlb1_idx; i++) {
1324 /* Skip invalid entries */
1325 if (!(tlb1[i].mas1 & MAS1_VALID))
1328 tlb1_write_entry(i);
1331 set_mas4_defaults();
1335 * Get the physical page address for the given pmap/virtual address.
1338 mmu_booke_extract(mmu_t mmu, pmap_t pmap, vm_offset_t va)
1343 pa = pte_vatopa(mmu, pmap, va);
1350 * Extract the physical page address associated with the given
1351 * kernel virtual address.
1354 mmu_booke_kextract(mmu_t mmu, vm_offset_t va)
1358 /* Check TLB1 mappings */
1359 for (i = 0; i < tlb1_idx; i++) {
1360 if (!(tlb1[i].mas1 & MAS1_VALID))
1362 if (va >= tlb1[i].virt && va < tlb1[i].virt + tlb1[i].size)
1363 return (tlb1[i].phys + (va - tlb1[i].virt));
1366 return (pte_vatopa(mmu, kernel_pmap, va));
1370 * Initialize the pmap module.
1371 * Called by vm_init, to initialize any structures that the pmap
1372 * system needs to map virtual memory.
1375 mmu_booke_init(mmu_t mmu)
1377 int shpgperproc = PMAP_SHPGPERPROC;
1380 * Initialize the address space (zone) for the pv entries. Set a
1381 * high water mark so that the system can recover from excessive
1382 * numbers of pv entries.
1384 pvzone = uma_zcreate("PV ENTRY", sizeof(struct pv_entry), NULL, NULL,
1385 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE);
1387 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1388 pv_entry_max = shpgperproc * maxproc + cnt.v_page_count;
1390 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
1391 pv_entry_high_water = 9 * (pv_entry_max / 10);
1393 uma_zone_reserve_kva(pvzone, pv_entry_max);
1395 /* Pre-fill pvzone with initial number of pv entries. */
1396 uma_prealloc(pvzone, PV_ENTRY_ZONE_MIN);
1398 /* Initialize ptbl allocation. */
1403 * Map a list of wired pages into kernel virtual address space. This is
1404 * intended for temporary mappings which do not need page modification or
1405 * references recorded. Existing mappings in the region are overwritten.
1408 mmu_booke_qenter(mmu_t mmu, vm_offset_t sva, vm_page_t *m, int count)
1413 while (count-- > 0) {
1414 mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(*m));
1421 * Remove page mappings from kernel virtual address space. Intended for
1422 * temporary mappings entered by mmu_booke_qenter.
1425 mmu_booke_qremove(mmu_t mmu, vm_offset_t sva, int count)
1430 while (count-- > 0) {
1431 mmu_booke_kremove(mmu, va);
1437 * Map a wired page into kernel virtual address space.
1440 mmu_booke_kenter(mmu_t mmu, vm_offset_t va, vm_paddr_t pa)
1443 mmu_booke_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT);
1447 mmu_booke_kenter_attr(mmu_t mmu, vm_offset_t va, vm_paddr_t pa, vm_memattr_t ma)
1449 unsigned int pdir_idx = PDIR_IDX(va);
1450 unsigned int ptbl_idx = PTBL_IDX(va);
1454 KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1455 (va <= VM_MAX_KERNEL_ADDRESS)), ("mmu_booke_kenter: invalid va"));
1457 flags = PTE_SR | PTE_SW | PTE_SX | PTE_WIRED | PTE_VALID;
1458 flags |= tlb_calc_wimg(pa, ma);
1460 pte = &(kernel_pmap->pm_pdir[pdir_idx][ptbl_idx]);
1462 mtx_lock_spin(&tlbivax_mutex);
1465 if (PTE_ISVALID(pte)) {
1467 CTR1(KTR_PMAP, "%s: replacing entry!", __func__);
1469 /* Flush entry from TLB0 */
1470 tlb0_flush_entry(va);
1473 pte->rpn = pa & ~PTE_PA_MASK;
1476 //debugf("mmu_booke_kenter: pdir_idx = %d ptbl_idx = %d va=0x%08x "
1477 // "pa=0x%08x rpn=0x%08x flags=0x%08x\n",
1478 // pdir_idx, ptbl_idx, va, pa, pte->rpn, pte->flags);
1480 /* Flush the real memory from the instruction cache. */
1481 if ((flags & (PTE_I | PTE_G)) == 0) {
1482 __syncicache((void *)va, PAGE_SIZE);
1486 mtx_unlock_spin(&tlbivax_mutex);
1490 * Remove a page from kernel page table.
1493 mmu_booke_kremove(mmu_t mmu, vm_offset_t va)
1495 unsigned int pdir_idx = PDIR_IDX(va);
1496 unsigned int ptbl_idx = PTBL_IDX(va);
1499 // CTR2(KTR_PMAP,("%s: s (va = 0x%08x)\n", __func__, va));
1501 KASSERT(((va >= VM_MIN_KERNEL_ADDRESS) &&
1502 (va <= VM_MAX_KERNEL_ADDRESS)),
1503 ("mmu_booke_kremove: invalid va"));
1505 pte = &(kernel_pmap->pm_pdir[pdir_idx][ptbl_idx]);
1507 if (!PTE_ISVALID(pte)) {
1509 CTR1(KTR_PMAP, "%s: invalid pte", __func__);
1514 mtx_lock_spin(&tlbivax_mutex);
1517 /* Invalidate entry in TLB0, update PTE. */
1518 tlb0_flush_entry(va);
1523 mtx_unlock_spin(&tlbivax_mutex);
1527 * Initialize pmap associated with process 0.
1530 mmu_booke_pinit0(mmu_t mmu, pmap_t pmap)
1533 PMAP_LOCK_INIT(pmap);
1534 mmu_booke_pinit(mmu, pmap);
1535 PCPU_SET(curpmap, pmap);
1539 * Initialize a preallocated and zeroed pmap structure,
1540 * such as one in a vmspace structure.
1543 mmu_booke_pinit(mmu_t mmu, pmap_t pmap)
1547 CTR4(KTR_PMAP, "%s: pmap = %p, proc %d '%s'", __func__, pmap,
1548 curthread->td_proc->p_pid, curthread->td_proc->p_comm);
1550 KASSERT((pmap != kernel_pmap), ("pmap_pinit: initializing kernel_pmap"));
1552 for (i = 0; i < MAXCPU; i++)
1553 pmap->pm_tid[i] = TID_NONE;
1554 CPU_ZERO(&kernel_pmap->pm_active);
1555 bzero(&pmap->pm_stats, sizeof(pmap->pm_stats));
1556 bzero(&pmap->pm_pdir, sizeof(pte_t *) * PDIR_NENTRIES);
1557 TAILQ_INIT(&pmap->pm_ptbl_list);
1561 * Release any resources held by the given physical map.
1562 * Called when a pmap initialized by mmu_booke_pinit is being released.
1563 * Should only be called if the map contains no valid mappings.
1566 mmu_booke_release(mmu_t mmu, pmap_t pmap)
1569 KASSERT(pmap->pm_stats.resident_count == 0,
1570 ("pmap_release: pmap resident count %ld != 0",
1571 pmap->pm_stats.resident_count));
1575 * Insert the given physical page at the specified virtual address in the
1576 * target physical map with the protection requested. If specified the page
1577 * will be wired down.
1580 mmu_booke_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1581 vm_prot_t prot, boolean_t wired)
1584 rw_wlock(&pvh_global_lock);
1586 mmu_booke_enter_locked(mmu, pmap, va, m, prot, wired);
1587 rw_wunlock(&pvh_global_lock);
1592 mmu_booke_enter_locked(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1593 vm_prot_t prot, boolean_t wired)
1600 pa = VM_PAGE_TO_PHYS(m);
1601 su = (pmap == kernel_pmap);
1604 //debugf("mmu_booke_enter_locked: s (pmap=0x%08x su=%d tid=%d m=0x%08x va=0x%08x "
1605 // "pa=0x%08x prot=0x%08x wired=%d)\n",
1606 // (u_int32_t)pmap, su, pmap->pm_tid,
1607 // (u_int32_t)m, va, pa, prot, wired);
1610 KASSERT(((va >= virtual_avail) &&
1611 (va <= VM_MAX_KERNEL_ADDRESS)),
1612 ("mmu_booke_enter_locked: kernel pmap, non kernel va"));
1614 KASSERT((va <= VM_MAXUSER_ADDRESS),
1615 ("mmu_booke_enter_locked: user pmap, non user va"));
1617 if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
1618 VM_OBJECT_ASSERT_LOCKED(m->object);
1620 PMAP_LOCK_ASSERT(pmap, MA_OWNED);
1623 * If there is an existing mapping, and the physical address has not
1624 * changed, must be protection or wiring change.
1626 if (((pte = pte_find(mmu, pmap, va)) != NULL) &&
1627 (PTE_ISVALID(pte)) && (PTE_PA(pte) == pa)) {
1630 * Before actually updating pte->flags we calculate and
1631 * prepare its new value in a helper var.
1634 flags &= ~(PTE_UW | PTE_UX | PTE_SW | PTE_SX | PTE_MODIFIED);
1636 /* Wiring change, just update stats. */
1638 if (!PTE_ISWIRED(pte)) {
1640 pmap->pm_stats.wired_count++;
1643 if (PTE_ISWIRED(pte)) {
1644 flags &= ~PTE_WIRED;
1645 pmap->pm_stats.wired_count--;
1649 if (prot & VM_PROT_WRITE) {
1650 /* Add write permissions. */
1655 if ((flags & PTE_MANAGED) != 0)
1656 vm_page_aflag_set(m, PGA_WRITEABLE);
1658 /* Handle modified pages, sense modify status. */
1661 * The PTE_MODIFIED flag could be set by underlying
1662 * TLB misses since we last read it (above), possibly
1663 * other CPUs could update it so we check in the PTE
1664 * directly rather than rely on that saved local flags
1667 if (PTE_ISMODIFIED(pte))
1671 if (prot & VM_PROT_EXECUTE) {
1677 * Check existing flags for execute permissions: if we
1678 * are turning execute permissions on, icache should
1681 if ((pte->flags & (PTE_UX | PTE_SX)) == 0)
1685 flags &= ~PTE_REFERENCED;
1688 * The new flags value is all calculated -- only now actually
1691 mtx_lock_spin(&tlbivax_mutex);
1694 tlb0_flush_entry(va);
1698 mtx_unlock_spin(&tlbivax_mutex);
1702 * If there is an existing mapping, but it's for a different
1703 * physical address, pte_enter() will delete the old mapping.
1705 //if ((pte != NULL) && PTE_ISVALID(pte))
1706 // debugf("mmu_booke_enter_locked: replace\n");
1708 // debugf("mmu_booke_enter_locked: new\n");
1710 /* Now set up the flags and install the new mapping. */
1711 flags = (PTE_SR | PTE_VALID);
1717 if (prot & VM_PROT_WRITE) {
1722 if ((m->oflags & VPO_UNMANAGED) == 0)
1723 vm_page_aflag_set(m, PGA_WRITEABLE);
1726 if (prot & VM_PROT_EXECUTE) {
1732 /* If its wired update stats. */
1734 pmap->pm_stats.wired_count++;
1738 pte_enter(mmu, pmap, m, va, flags);
1740 /* Flush the real memory from the instruction cache. */
1741 if (prot & VM_PROT_EXECUTE)
1745 if (sync && (su || pmap == PCPU_GET(curpmap))) {
1746 __syncicache((void *)va, PAGE_SIZE);
1752 * Maps a sequence of resident pages belonging to the same object.
1753 * The sequence begins with the given page m_start. This page is
1754 * mapped at the given virtual address start. Each subsequent page is
1755 * mapped at a virtual address that is offset from start by the same
1756 * amount as the page is offset from m_start within the object. The
1757 * last page in the sequence is the page with the largest offset from
1758 * m_start that can be mapped at a virtual address less than the given
1759 * virtual address end. Not every virtual page between start and end
1760 * is mapped; only those for which a resident page exists with the
1761 * corresponding offset from m_start are mapped.
1764 mmu_booke_enter_object(mmu_t mmu, pmap_t pmap, vm_offset_t start,
1765 vm_offset_t end, vm_page_t m_start, vm_prot_t prot)
1768 vm_pindex_t diff, psize;
1770 VM_OBJECT_ASSERT_LOCKED(m_start->object);
1772 psize = atop(end - start);
1774 rw_wlock(&pvh_global_lock);
1776 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
1777 mmu_booke_enter_locked(mmu, pmap, start + ptoa(diff), m,
1778 prot & (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1779 m = TAILQ_NEXT(m, listq);
1781 rw_wunlock(&pvh_global_lock);
1786 mmu_booke_enter_quick(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m,
1790 rw_wlock(&pvh_global_lock);
1792 mmu_booke_enter_locked(mmu, pmap, va, m,
1793 prot & (VM_PROT_READ | VM_PROT_EXECUTE), FALSE);
1794 rw_wunlock(&pvh_global_lock);
1799 * Remove the given range of addresses from the specified map.
1801 * It is assumed that the start and end are properly rounded to the page size.
1804 mmu_booke_remove(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_offset_t endva)
1809 int su = (pmap == kernel_pmap);
1811 //debugf("mmu_booke_remove: s (su = %d pmap=0x%08x tid=%d va=0x%08x endva=0x%08x)\n",
1812 // su, (u_int32_t)pmap, pmap->pm_tid, va, endva);
1815 KASSERT(((va >= virtual_avail) &&
1816 (va <= VM_MAX_KERNEL_ADDRESS)),
1817 ("mmu_booke_remove: kernel pmap, non kernel va"));
1819 KASSERT((va <= VM_MAXUSER_ADDRESS),
1820 ("mmu_booke_remove: user pmap, non user va"));
1823 if (PMAP_REMOVE_DONE(pmap)) {
1824 //debugf("mmu_booke_remove: e (empty)\n");
1828 hold_flag = PTBL_HOLD_FLAG(pmap);
1829 //debugf("mmu_booke_remove: hold_flag = %d\n", hold_flag);
1831 rw_wlock(&pvh_global_lock);
1833 for (; va < endva; va += PAGE_SIZE) {
1834 pte = pte_find(mmu, pmap, va);
1835 if ((pte != NULL) && PTE_ISVALID(pte))
1836 pte_remove(mmu, pmap, va, hold_flag);
1839 rw_wunlock(&pvh_global_lock);
1841 //debugf("mmu_booke_remove: e\n");
1845 * Remove physical page from all pmaps in which it resides.
1848 mmu_booke_remove_all(mmu_t mmu, vm_page_t m)
1853 rw_wlock(&pvh_global_lock);
1854 for (pv = TAILQ_FIRST(&m->md.pv_list); pv != NULL; pv = pvn) {
1855 pvn = TAILQ_NEXT(pv, pv_link);
1857 PMAP_LOCK(pv->pv_pmap);
1858 hold_flag = PTBL_HOLD_FLAG(pv->pv_pmap);
1859 pte_remove(mmu, pv->pv_pmap, pv->pv_va, hold_flag);
1860 PMAP_UNLOCK(pv->pv_pmap);
1862 vm_page_aflag_clear(m, PGA_WRITEABLE);
1863 rw_wunlock(&pvh_global_lock);
1867 * Map a range of physical addresses into kernel virtual address space.
1870 mmu_booke_map(mmu_t mmu, vm_offset_t *virt, vm_paddr_t pa_start,
1871 vm_paddr_t pa_end, int prot)
1873 vm_offset_t sva = *virt;
1874 vm_offset_t va = sva;
1876 //debugf("mmu_booke_map: s (sva = 0x%08x pa_start = 0x%08x pa_end = 0x%08x)\n",
1877 // sva, pa_start, pa_end);
1879 while (pa_start < pa_end) {
1880 mmu_booke_kenter(mmu, va, pa_start);
1882 pa_start += PAGE_SIZE;
1886 //debugf("mmu_booke_map: e (va = 0x%08x)\n", va);
1891 * The pmap must be activated before it's address space can be accessed in any
1895 mmu_booke_activate(mmu_t mmu, struct thread *td)
1900 pmap = &td->td_proc->p_vmspace->vm_pmap;
1902 CTR5(KTR_PMAP, "%s: s (td = %p, proc = '%s', id = %d, pmap = 0x%08x)",
1903 __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1905 KASSERT((pmap != kernel_pmap), ("mmu_booke_activate: kernel_pmap!"));
1909 cpuid = PCPU_GET(cpuid);
1910 CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
1911 PCPU_SET(curpmap, pmap);
1913 if (pmap->pm_tid[cpuid] == TID_NONE)
1916 /* Load PID0 register with pmap tid value. */
1917 mtspr(SPR_PID0, pmap->pm_tid[cpuid]);
1918 __asm __volatile("isync");
1922 CTR3(KTR_PMAP, "%s: e (tid = %d for '%s')", __func__,
1923 pmap->pm_tid[PCPU_GET(cpuid)], td->td_proc->p_comm);
1927 * Deactivate the specified process's address space.
1930 mmu_booke_deactivate(mmu_t mmu, struct thread *td)
1934 pmap = &td->td_proc->p_vmspace->vm_pmap;
1936 CTR5(KTR_PMAP, "%s: td=%p, proc = '%s', id = %d, pmap = 0x%08x",
1937 __func__, td, td->td_proc->p_comm, td->td_proc->p_pid, pmap);
1939 CPU_CLR_ATOMIC(PCPU_GET(cpuid), &pmap->pm_active);
1940 PCPU_SET(curpmap, NULL);
1944 * Copy the range specified by src_addr/len
1945 * from the source map to the range dst_addr/len
1946 * in the destination map.
1948 * This routine is only advisory and need not do anything.
1951 mmu_booke_copy(mmu_t mmu, pmap_t dst_pmap, pmap_t src_pmap,
1952 vm_offset_t dst_addr, vm_size_t len, vm_offset_t src_addr)
1958 * Set the physical protection on the specified range of this map as requested.
1961 mmu_booke_protect(mmu_t mmu, pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
1968 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1969 mmu_booke_remove(mmu, pmap, sva, eva);
1973 if (prot & VM_PROT_WRITE)
1977 for (va = sva; va < eva; va += PAGE_SIZE) {
1978 if ((pte = pte_find(mmu, pmap, va)) != NULL) {
1979 if (PTE_ISVALID(pte)) {
1980 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
1982 mtx_lock_spin(&tlbivax_mutex);
1985 /* Handle modified pages. */
1986 if (PTE_ISMODIFIED(pte) && PTE_ISMANAGED(pte))
1989 tlb0_flush_entry(va);
1990 pte->flags &= ~(PTE_UW | PTE_SW | PTE_MODIFIED);
1993 mtx_unlock_spin(&tlbivax_mutex);
2001 * Clear the write and modified bits in each of the given page's mappings.
2004 mmu_booke_remove_write(mmu_t mmu, vm_page_t m)
2009 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2010 ("mmu_booke_remove_write: page %p is not managed", m));
2013 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2014 * set by another thread while the object is locked. Thus,
2015 * if PGA_WRITEABLE is clear, no page table entries need updating.
2017 VM_OBJECT_ASSERT_WLOCKED(m->object);
2018 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2020 rw_wlock(&pvh_global_lock);
2021 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2022 PMAP_LOCK(pv->pv_pmap);
2023 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL) {
2024 if (PTE_ISVALID(pte)) {
2025 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
2027 mtx_lock_spin(&tlbivax_mutex);
2030 /* Handle modified pages. */
2031 if (PTE_ISMODIFIED(pte))
2034 /* Flush mapping from TLB0. */
2035 pte->flags &= ~(PTE_UW | PTE_SW | PTE_MODIFIED);
2038 mtx_unlock_spin(&tlbivax_mutex);
2041 PMAP_UNLOCK(pv->pv_pmap);
2043 vm_page_aflag_clear(m, PGA_WRITEABLE);
2044 rw_wunlock(&pvh_global_lock);
2048 mmu_booke_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz)
2057 va = trunc_page(va);
2058 sz = round_page(sz);
2060 rw_wlock(&pvh_global_lock);
2061 pmap = PCPU_GET(curpmap);
2062 active = (pm == kernel_pmap || pm == pmap) ? 1 : 0;
2065 pte = pte_find(mmu, pm, va);
2066 valid = (pte != NULL && PTE_ISVALID(pte)) ? 1 : 0;
2072 /* Create a mapping in the active pmap. */
2074 m = PHYS_TO_VM_PAGE(pa);
2076 pte_enter(mmu, pmap, m, addr,
2077 PTE_SR | PTE_VALID | PTE_UR);
2078 __syncicache((void *)addr, PAGE_SIZE);
2079 pte_remove(mmu, pmap, addr, PTBL_UNHOLD);
2082 __syncicache((void *)va, PAGE_SIZE);
2087 rw_wunlock(&pvh_global_lock);
2091 * Atomically extract and hold the physical page with the given
2092 * pmap and virtual address pair if that mapping permits the given
2096 mmu_booke_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va,
2108 pte = pte_find(mmu, pmap, va);
2109 if ((pte != NULL) && PTE_ISVALID(pte)) {
2110 if (pmap == kernel_pmap)
2115 if ((pte->flags & pte_wbit) || ((prot & VM_PROT_WRITE) == 0)) {
2116 if (vm_page_pa_tryrelock(pmap, PTE_PA(pte), &pa))
2118 m = PHYS_TO_VM_PAGE(PTE_PA(pte));
2129 * Initialize a vm_page's machine-dependent fields.
2132 mmu_booke_page_init(mmu_t mmu, vm_page_t m)
2135 TAILQ_INIT(&m->md.pv_list);
2139 * mmu_booke_zero_page_area zeros the specified hardware page by
2140 * mapping it into virtual memory and using bzero to clear
2143 * off and size must reside within a single page.
2146 mmu_booke_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size)
2150 /* XXX KASSERT off and size are within a single page? */
2152 mtx_lock(&zero_page_mutex);
2155 mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2156 bzero((caddr_t)va + off, size);
2157 mmu_booke_kremove(mmu, va);
2159 mtx_unlock(&zero_page_mutex);
2163 * mmu_booke_zero_page zeros the specified hardware page.
2166 mmu_booke_zero_page(mmu_t mmu, vm_page_t m)
2169 mmu_booke_zero_page_area(mmu, m, 0, PAGE_SIZE);
2173 * mmu_booke_copy_page copies the specified (machine independent) page by
2174 * mapping the page into virtual memory and using memcopy to copy the page,
2175 * one machine dependent page at a time.
2178 mmu_booke_copy_page(mmu_t mmu, vm_page_t sm, vm_page_t dm)
2180 vm_offset_t sva, dva;
2182 sva = copy_page_src_va;
2183 dva = copy_page_dst_va;
2185 mtx_lock(©_page_mutex);
2186 mmu_booke_kenter(mmu, sva, VM_PAGE_TO_PHYS(sm));
2187 mmu_booke_kenter(mmu, dva, VM_PAGE_TO_PHYS(dm));
2188 memcpy((caddr_t)dva, (caddr_t)sva, PAGE_SIZE);
2189 mmu_booke_kremove(mmu, dva);
2190 mmu_booke_kremove(mmu, sva);
2191 mtx_unlock(©_page_mutex);
2195 mmu_booke_copy_pages(mmu_t mmu, vm_page_t *ma, vm_offset_t a_offset,
2196 vm_page_t *mb, vm_offset_t b_offset, int xfersize)
2199 vm_offset_t a_pg_offset, b_pg_offset;
2202 mtx_lock(©_page_mutex);
2203 while (xfersize > 0) {
2204 a_pg_offset = a_offset & PAGE_MASK;
2205 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
2206 mmu_booke_kenter(mmu, copy_page_src_va,
2207 VM_PAGE_TO_PHYS(ma[a_offset >> PAGE_SHIFT]));
2208 a_cp = (char *)copy_page_src_va + a_pg_offset;
2209 b_pg_offset = b_offset & PAGE_MASK;
2210 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
2211 mmu_booke_kenter(mmu, copy_page_dst_va,
2212 VM_PAGE_TO_PHYS(mb[b_offset >> PAGE_SHIFT]));
2213 b_cp = (char *)copy_page_dst_va + b_pg_offset;
2214 bcopy(a_cp, b_cp, cnt);
2215 mmu_booke_kremove(mmu, copy_page_dst_va);
2216 mmu_booke_kremove(mmu, copy_page_src_va);
2221 mtx_unlock(©_page_mutex);
2225 * mmu_booke_zero_page_idle zeros the specified hardware page by mapping it
2226 * into virtual memory and using bzero to clear its contents. This is intended
2227 * to be called from the vm_pagezero process only and outside of Giant. No
2231 mmu_booke_zero_page_idle(mmu_t mmu, vm_page_t m)
2235 va = zero_page_idle_va;
2236 mmu_booke_kenter(mmu, va, VM_PAGE_TO_PHYS(m));
2237 bzero((caddr_t)va, PAGE_SIZE);
2238 mmu_booke_kremove(mmu, va);
2242 * Return whether or not the specified physical page was modified
2243 * in any of physical maps.
2246 mmu_booke_is_modified(mmu_t mmu, vm_page_t m)
2252 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2253 ("mmu_booke_is_modified: page %p is not managed", m));
2257 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
2258 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE
2259 * is clear, no PTEs can be modified.
2261 VM_OBJECT_ASSERT_WLOCKED(m->object);
2262 if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
2264 rw_wlock(&pvh_global_lock);
2265 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2266 PMAP_LOCK(pv->pv_pmap);
2267 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2269 if (PTE_ISMODIFIED(pte))
2272 PMAP_UNLOCK(pv->pv_pmap);
2276 rw_wunlock(&pvh_global_lock);
2281 * Return whether or not the specified virtual address is eligible
2285 mmu_booke_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t addr)
2292 * Return whether or not the specified physical page was referenced
2293 * in any physical maps.
2296 mmu_booke_is_referenced(mmu_t mmu, vm_page_t m)
2302 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2303 ("mmu_booke_is_referenced: page %p is not managed", m));
2305 rw_wlock(&pvh_global_lock);
2306 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2307 PMAP_LOCK(pv->pv_pmap);
2308 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2310 if (PTE_ISREFERENCED(pte))
2313 PMAP_UNLOCK(pv->pv_pmap);
2317 rw_wunlock(&pvh_global_lock);
2322 * Clear the modify bits on the specified physical page.
2325 mmu_booke_clear_modify(mmu_t mmu, vm_page_t m)
2330 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2331 ("mmu_booke_clear_modify: page %p is not managed", m));
2332 VM_OBJECT_ASSERT_WLOCKED(m->object);
2333 KASSERT(!vm_page_xbusied(m),
2334 ("mmu_booke_clear_modify: page %p is exclusive busied", m));
2337 * If the page is not PG_AWRITEABLE, then no PTEs can be modified.
2338 * If the object containing the page is locked and the page is not
2339 * exclusive busied, then PG_AWRITEABLE cannot be concurrently set.
2341 if ((m->aflags & PGA_WRITEABLE) == 0)
2343 rw_wlock(&pvh_global_lock);
2344 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2345 PMAP_LOCK(pv->pv_pmap);
2346 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2348 mtx_lock_spin(&tlbivax_mutex);
2351 if (pte->flags & (PTE_SW | PTE_UW | PTE_MODIFIED)) {
2352 tlb0_flush_entry(pv->pv_va);
2353 pte->flags &= ~(PTE_SW | PTE_UW | PTE_MODIFIED |
2358 mtx_unlock_spin(&tlbivax_mutex);
2360 PMAP_UNLOCK(pv->pv_pmap);
2362 rw_wunlock(&pvh_global_lock);
2366 * Return a count of reference bits for a page, clearing those bits.
2367 * It is not necessary for every reference bit to be cleared, but it
2368 * is necessary that 0 only be returned when there are truly no
2369 * reference bits set.
2371 * XXX: The exact number of bits to check and clear is a matter that
2372 * should be tested and standardized at some point in the future for
2373 * optimal aging of shared pages.
2376 mmu_booke_ts_referenced(mmu_t mmu, vm_page_t m)
2382 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2383 ("mmu_booke_ts_referenced: page %p is not managed", m));
2385 rw_wlock(&pvh_global_lock);
2386 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2387 PMAP_LOCK(pv->pv_pmap);
2388 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL &&
2390 if (PTE_ISREFERENCED(pte)) {
2391 mtx_lock_spin(&tlbivax_mutex);
2394 tlb0_flush_entry(pv->pv_va);
2395 pte->flags &= ~PTE_REFERENCED;
2398 mtx_unlock_spin(&tlbivax_mutex);
2401 PMAP_UNLOCK(pv->pv_pmap);
2406 PMAP_UNLOCK(pv->pv_pmap);
2408 rw_wunlock(&pvh_global_lock);
2413 * Change wiring attribute for a map/virtual-address pair.
2416 mmu_booke_change_wiring(mmu_t mmu, pmap_t pmap, vm_offset_t va, boolean_t wired)
2421 if ((pte = pte_find(mmu, pmap, va)) != NULL) {
2423 if (!PTE_ISWIRED(pte)) {
2424 pte->flags |= PTE_WIRED;
2425 pmap->pm_stats.wired_count++;
2428 if (PTE_ISWIRED(pte)) {
2429 pte->flags &= ~PTE_WIRED;
2430 pmap->pm_stats.wired_count--;
2438 * Return true if the pmap's pv is one of the first 16 pvs linked to from this
2439 * page. This count may be changed upwards or downwards in the future; it is
2440 * only necessary that true be returned for a small subset of pmaps for proper
2444 mmu_booke_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m)
2450 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
2451 ("mmu_booke_page_exists_quick: page %p is not managed", m));
2454 rw_wlock(&pvh_global_lock);
2455 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2456 if (pv->pv_pmap == pmap) {
2463 rw_wunlock(&pvh_global_lock);
2468 * Return the number of managed mappings to the given physical page that are
2472 mmu_booke_page_wired_mappings(mmu_t mmu, vm_page_t m)
2478 if ((m->oflags & VPO_UNMANAGED) != 0)
2480 rw_wlock(&pvh_global_lock);
2481 TAILQ_FOREACH(pv, &m->md.pv_list, pv_link) {
2482 PMAP_LOCK(pv->pv_pmap);
2483 if ((pte = pte_find(mmu, pv->pv_pmap, pv->pv_va)) != NULL)
2484 if (PTE_ISVALID(pte) && PTE_ISWIRED(pte))
2486 PMAP_UNLOCK(pv->pv_pmap);
2488 rw_wunlock(&pvh_global_lock);
2493 mmu_booke_dev_direct_mapped(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2499 * This currently does not work for entries that
2500 * overlap TLB1 entries.
2502 for (i = 0; i < tlb1_idx; i ++) {
2503 if (tlb1_iomapped(i, pa, size, &va) == 0)
2511 mmu_booke_dumpsys_map(mmu_t mmu, struct pmap_md *md, vm_size_t ofs,
2518 /* Raw physical memory dumps don't have a virtual address. */
2519 if (md->md_vaddr == ~0UL) {
2520 /* We always map a 256MB page at 256M. */
2521 gran = 256 * 1024 * 1024;
2522 pa = md->md_paddr + ofs;
2523 ppa = pa & ~(gran - 1);
2526 tlb1_set_entry(va, ppa, gran, _TLB_ENTRY_IO);
2527 if (*sz > (gran - ofs))
2532 /* Minidumps are based on virtual memory addresses. */
2533 va = md->md_vaddr + ofs;
2534 if (va >= kernstart + kernsize) {
2535 gran = PAGE_SIZE - (va & PAGE_MASK);
2543 mmu_booke_dumpsys_unmap(mmu_t mmu, struct pmap_md *md, vm_size_t ofs,
2547 /* Raw physical memory dumps don't have a virtual address. */
2548 if (md->md_vaddr == ~0UL) {
2550 tlb1[tlb1_idx].mas1 = 0;
2551 tlb1[tlb1_idx].mas2 = 0;
2552 tlb1[tlb1_idx].mas3 = 0;
2553 tlb1_write_entry(tlb1_idx);
2557 /* Minidumps are based on virtual memory addresses. */
2558 /* Nothing to do... */
2562 mmu_booke_scan_md(mmu_t mmu, struct pmap_md *prev)
2564 static struct pmap_md md;
2568 if (dumpsys_minidump) {
2569 md.md_paddr = ~0UL; /* Minidumps use virtual addresses. */
2571 /* 1st: kernel .data and .bss. */
2573 md.md_vaddr = trunc_page((uintptr_t)_etext);
2574 md.md_size = round_page((uintptr_t)_end) - md.md_vaddr;
2577 switch (prev->md_index) {
2579 /* 2nd: msgbuf and tables (see pmap_bootstrap()). */
2581 md.md_vaddr = data_start;
2582 md.md_size = data_end - data_start;
2585 /* 3rd: kernel VM. */
2586 va = prev->md_vaddr + prev->md_size;
2587 /* Find start of next chunk (from va). */
2588 while (va < virtual_end) {
2589 /* Don't dump the buffer cache. */
2590 if (va >= kmi.buffer_sva &&
2591 va < kmi.buffer_eva) {
2592 va = kmi.buffer_eva;
2595 pte = pte_find(mmu, kernel_pmap, va);
2596 if (pte != NULL && PTE_ISVALID(pte))
2600 if (va < virtual_end) {
2603 /* Find last page in chunk. */
2604 while (va < virtual_end) {
2605 /* Don't run into the buffer cache. */
2606 if (va == kmi.buffer_sva)
2608 pte = pte_find(mmu, kernel_pmap, va);
2609 if (pte == NULL || !PTE_ISVALID(pte))
2613 md.md_size = va - md.md_vaddr;
2621 } else { /* minidumps */
2622 mem_regions(&physmem_regions, &physmem_regions_sz,
2623 &availmem_regions, &availmem_regions_sz);
2626 /* first physical chunk. */
2627 md.md_paddr = physmem_regions[0].mr_start;
2628 md.md_size = physmem_regions[0].mr_size;
2631 } else if (md.md_index < physmem_regions_sz) {
2632 md.md_paddr = physmem_regions[md.md_index].mr_start;
2633 md.md_size = physmem_regions[md.md_index].mr_size;
2637 /* There's no next physical chunk. */
2646 * Map a set of physical memory pages into the kernel virtual address space.
2647 * Return a pointer to where it is mapped. This routine is intended to be used
2648 * for mapping device memory, NOT real memory.
2651 mmu_booke_mapdev(mmu_t mmu, vm_paddr_t pa, vm_size_t size)
2654 return (mmu_booke_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT));
2658 mmu_booke_mapdev_attr(mmu_t mmu, vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
2666 * Check if this is premapped in TLB1. Note: this should probably also
2667 * check whether a sequence of TLB1 entries exist that match the
2668 * requirement, but now only checks the easy case.
2670 if (ma == VM_MEMATTR_DEFAULT) {
2671 for (i = 0; i < tlb1_idx; i++) {
2672 if (!(tlb1[i].mas1 & MAS1_VALID))
2674 if (pa >= tlb1[i].phys &&
2675 (pa + size) <= (tlb1[i].phys + tlb1[i].size))
2676 return (void *)(tlb1[i].virt +
2677 (pa - tlb1[i].phys));
2681 size = roundup(size, PAGE_SIZE);
2684 * We leave a hole for device direct mapping between the maximum user
2685 * address (0x8000000) and the minimum KVA address (0xc0000000). If
2686 * devices are in there, just map them 1:1. If not, map them to the
2687 * device mapping area about VM_MAX_KERNEL_ADDRESS. These mapped
2688 * addresses should be pulled from an allocator, but since we do not
2689 * ever free TLB1 entries, it is safe just to increment a counter.
2690 * Note that there isn't a lot of address space here (128 MB) and it
2691 * is not at all difficult to imagine running out, since that is a 4:1
2692 * compression from the 0xc0000000 - 0xf0000000 address space that gets
2695 if (pa >= (VM_MAXUSER_ADDRESS + PAGE_SIZE) &&
2696 (pa + size - 1) < VM_MIN_KERNEL_ADDRESS)
2699 va = atomic_fetchadd_int(&tlb1_map_base, size);
2703 sz = 1 << (ilog2(size) & ~1);
2705 printf("Wiring VA=%x to PA=%x (size=%x), "
2706 "using TLB1[%d]\n", va, pa, sz, tlb1_idx);
2707 tlb1_set_entry(va, pa, sz, tlb_calc_wimg(pa, ma));
2717 * 'Unmap' a range mapped by mmu_booke_mapdev().
2720 mmu_booke_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size)
2722 #ifdef SUPPORTS_SHRINKING_TLB1
2723 vm_offset_t base, offset;
2726 * Unmap only if this is inside kernel virtual space.
2728 if ((va >= VM_MIN_KERNEL_ADDRESS) && (va <= VM_MAX_KERNEL_ADDRESS)) {
2729 base = trunc_page(va);
2730 offset = va & PAGE_MASK;
2731 size = roundup(offset + size, PAGE_SIZE);
2732 kva_free(base, size);
2738 * mmu_booke_object_init_pt preloads the ptes for a given object into the
2739 * specified pmap. This eliminates the blast of soft faults on process startup
2740 * and immediately after an mmap.
2743 mmu_booke_object_init_pt(mmu_t mmu, pmap_t pmap, vm_offset_t addr,
2744 vm_object_t object, vm_pindex_t pindex, vm_size_t size)
2747 VM_OBJECT_ASSERT_WLOCKED(object);
2748 KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
2749 ("mmu_booke_object_init_pt: non-device object"));
2753 * Perform the pmap work for mincore.
2756 mmu_booke_mincore(mmu_t mmu, pmap_t pmap, vm_offset_t addr,
2757 vm_paddr_t *locked_pa)
2760 /* XXX: this should be implemented at some point */
2764 /**************************************************************************/
2766 /**************************************************************************/
2769 * Allocate a TID. If necessary, steal one from someone else.
2770 * The new TID is flushed from the TLB before returning.
2773 tid_alloc(pmap_t pmap)
2778 KASSERT((pmap != kernel_pmap), ("tid_alloc: kernel pmap"));
2780 CTR2(KTR_PMAP, "%s: s (pmap = %p)", __func__, pmap);
2782 thiscpu = PCPU_GET(cpuid);
2784 tid = PCPU_GET(tid_next);
2787 PCPU_SET(tid_next, tid + 1);
2789 /* If we are stealing TID then clear the relevant pmap's field */
2790 if (tidbusy[thiscpu][tid] != NULL) {
2792 CTR2(KTR_PMAP, "%s: warning: stealing tid %d", __func__, tid);
2794 tidbusy[thiscpu][tid]->pm_tid[thiscpu] = TID_NONE;
2796 /* Flush all entries from TLB0 matching this TID. */
2800 tidbusy[thiscpu][tid] = pmap;
2801 pmap->pm_tid[thiscpu] = tid;
2802 __asm __volatile("msync; isync");
2804 CTR3(KTR_PMAP, "%s: e (%02d next = %02d)", __func__, tid,
2805 PCPU_GET(tid_next));
2810 /**************************************************************************/
2812 /**************************************************************************/
2815 tlb_print_entry(int i, uint32_t mas1, uint32_t mas2, uint32_t mas3,
2825 if (mas1 & MAS1_VALID)
2830 if (mas1 & MAS1_IPROT)
2835 as = (mas1 & MAS1_TS_MASK) ? 1 : 0;
2836 tid = MAS1_GETTID(mas1);
2838 tsize = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
2841 size = tsize2size(tsize);
2843 debugf("%3d: (%s) [AS=%d] "
2844 "sz = 0x%08x tsz = %d tid = %d mas1 = 0x%08x "
2845 "mas2(va) = 0x%08x mas3(pa) = 0x%08x mas7 = 0x%08x\n",
2846 i, desc, as, size, tsize, tid, mas1, mas2, mas3, mas7);
2849 /* Convert TLB0 va and way number to tlb0[] table index. */
2850 static inline unsigned int
2851 tlb0_tableidx(vm_offset_t va, unsigned int way)
2855 idx = (way * TLB0_ENTRIES_PER_WAY);
2856 idx += (va & MAS2_TLB0_ENTRY_IDX_MASK) >> MAS2_TLB0_ENTRY_IDX_SHIFT;
2861 * Invalidate TLB0 entry.
2864 tlb0_flush_entry(vm_offset_t va)
2867 CTR2(KTR_PMAP, "%s: s va=0x%08x", __func__, va);
2869 mtx_assert(&tlbivax_mutex, MA_OWNED);
2871 __asm __volatile("tlbivax 0, %0" :: "r"(va & MAS2_EPN_MASK));
2872 __asm __volatile("isync; msync");
2873 __asm __volatile("tlbsync; msync");
2875 CTR1(KTR_PMAP, "%s: e", __func__);
2878 /* Print out contents of the MAS registers for each TLB0 entry */
2880 tlb0_print_tlbentries(void)
2882 uint32_t mas0, mas1, mas2, mas3, mas7;
2883 int entryidx, way, idx;
2885 debugf("TLB0 entries:\n");
2886 for (way = 0; way < TLB0_WAYS; way ++)
2887 for (entryidx = 0; entryidx < TLB0_ENTRIES_PER_WAY; entryidx++) {
2889 mas0 = MAS0_TLBSEL(0) | MAS0_ESEL(way);
2890 mtspr(SPR_MAS0, mas0);
2891 __asm __volatile("isync");
2893 mas2 = entryidx << MAS2_TLB0_ENTRY_IDX_SHIFT;
2894 mtspr(SPR_MAS2, mas2);
2896 __asm __volatile("isync; tlbre");
2898 mas1 = mfspr(SPR_MAS1);
2899 mas2 = mfspr(SPR_MAS2);
2900 mas3 = mfspr(SPR_MAS3);
2901 mas7 = mfspr(SPR_MAS7);
2903 idx = tlb0_tableidx(mas2, way);
2904 tlb_print_entry(idx, mas1, mas2, mas3, mas7);
2908 /**************************************************************************/
2910 /**************************************************************************/
2913 * TLB1 mapping notes:
2915 * TLB1[0] Kernel text and data.
2916 * TLB1[1-15] Additional kernel text and data mappings (if required), PCI
2917 * windows, other devices mappings.
2921 * Write given entry to TLB1 hardware.
2922 * Use 32 bit pa, clear 4 high-order bits of RPN (mas7).
2925 tlb1_write_entry(unsigned int idx)
2927 uint32_t mas0, mas7;
2929 //debugf("tlb1_write_entry: s\n");
2931 /* Clear high order RPN bits */
2935 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(idx);
2936 //debugf("tlb1_write_entry: mas0 = 0x%08x\n", mas0);
2938 mtspr(SPR_MAS0, mas0);
2939 __asm __volatile("isync");
2940 mtspr(SPR_MAS1, tlb1[idx].mas1);
2941 __asm __volatile("isync");
2942 mtspr(SPR_MAS2, tlb1[idx].mas2);
2943 __asm __volatile("isync");
2944 mtspr(SPR_MAS3, tlb1[idx].mas3);
2945 __asm __volatile("isync");
2946 mtspr(SPR_MAS7, mas7);
2947 __asm __volatile("isync; tlbwe; isync; msync");
2949 //debugf("tlb1_write_entry: e\n");
2953 * Return the largest uint value log such that 2^log <= num.
2956 ilog2(unsigned int num)
2960 __asm ("cntlzw %0, %1" : "=r" (lz) : "r" (num));
2965 * Convert TLB TSIZE value to mapped region size.
2968 tsize2size(unsigned int tsize)
2973 * size = 4^tsize * 2^10 = 2^(2 * tsize - 10)
2976 return ((1 << (2 * tsize)) * 1024);
2980 * Convert region size (must be power of 4) to TLB TSIZE value.
2983 size2tsize(vm_size_t size)
2986 return (ilog2(size) / 2 - 5);
2990 * Register permanent kernel mapping in TLB1.
2992 * Entries are created starting from index 0 (current free entry is
2993 * kept in tlb1_idx) and are not supposed to be invalidated.
2996 tlb1_set_entry(vm_offset_t va, vm_offset_t pa, vm_size_t size,
3002 index = atomic_fetchadd_int(&tlb1_idx, 1);
3003 if (index >= TLB1_ENTRIES) {
3004 printf("tlb1_set_entry: TLB1 full!\n");
3008 /* Convert size to TSIZE */
3009 tsize = size2tsize(size);
3011 tid = (TID_KERNEL << MAS1_TID_SHIFT) & MAS1_TID_MASK;
3012 /* XXX TS is hard coded to 0 for now as we only use single address space */
3013 ts = (0 << MAS1_TS_SHIFT) & MAS1_TS_MASK;
3016 * Atomicity is preserved by the atomic increment above since nothing
3017 * is ever removed from tlb1.
3020 tlb1[index].phys = pa;
3021 tlb1[index].virt = va;
3022 tlb1[index].size = size;
3023 tlb1[index].mas1 = MAS1_VALID | MAS1_IPROT | ts | tid;
3024 tlb1[index].mas1 |= ((tsize << MAS1_TSIZE_SHIFT) & MAS1_TSIZE_MASK);
3025 tlb1[index].mas2 = (va & MAS2_EPN_MASK) | flags;
3027 /* Set supervisor RWX permission bits */
3028 tlb1[index].mas3 = (pa & MAS3_RPN) | MAS3_SR | MAS3_SW | MAS3_SX;
3030 tlb1_write_entry(index);
3033 * XXX in general TLB1 updates should be propagated between CPUs,
3034 * since current design assumes to have the same TLB1 set-up on all
3041 * Map in contiguous RAM region into the TLB1 using maximum of
3042 * KERNEL_REGION_MAX_TLB_ENTRIES entries.
3044 * If necessary round up last entry size and return total size
3045 * used by all allocated entries.
3048 tlb1_mapin_region(vm_offset_t va, vm_paddr_t pa, vm_size_t size)
3050 vm_size_t pgs[KERNEL_REGION_MAX_TLB_ENTRIES];
3051 vm_size_t mapped, pgsz, base, mask;
3054 /* Round up to the next 1M */
3055 size = (size + (1 << 20) - 1) & ~((1 << 20) - 1);
3060 pgsz = 64*1024*1024;
3061 while (mapped < size) {
3062 while (mapped < size && idx < KERNEL_REGION_MAX_TLB_ENTRIES) {
3063 while (pgsz > (size - mapped))
3069 /* We under-map. Correct for this. */
3070 if (mapped < size) {
3071 while (pgs[idx - 1] == pgsz) {
3075 /* XXX We may increase beyond out starting point. */
3084 /* Align address to the boundary */
3086 va = (va + mask) & ~mask;
3087 pa = (pa + mask) & ~mask;
3090 for (idx = 0; idx < nents; idx++) {
3092 debugf("%u: %x -> %x, size=%x\n", idx, pa, va, pgsz);
3093 tlb1_set_entry(va, pa, pgsz, _TLB_ENTRY_MEM);
3098 mapped = (va - base);
3099 printf("mapped size 0x%08x (wasted space 0x%08x)\n",
3100 mapped, mapped - size);
3105 * TLB1 initialization routine, to be called after the very first
3106 * assembler level setup done in locore.S.
3111 uint32_t mas0, mas1, mas2, mas3;
3115 if (bootinfo != NULL && bootinfo[0] != 1) {
3116 tlb1_idx = *((uint16_t *)(bootinfo + 8));
3120 /* The first entry/entries are used to map the kernel. */
3121 for (i = 0; i < tlb1_idx; i++) {
3122 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(i);
3123 mtspr(SPR_MAS0, mas0);
3124 __asm __volatile("isync; tlbre");
3126 mas1 = mfspr(SPR_MAS1);
3127 if ((mas1 & MAS1_VALID) == 0)
3130 mas2 = mfspr(SPR_MAS2);
3131 mas3 = mfspr(SPR_MAS3);
3133 tlb1[i].mas1 = mas1;
3134 tlb1[i].mas2 = mfspr(SPR_MAS2);
3135 tlb1[i].mas3 = mas3;
3136 tlb1[i].virt = mas2 & MAS2_EPN_MASK;
3137 tlb1[i].phys = mas3 & MAS3_RPN;
3140 kernload = mas3 & MAS3_RPN;
3142 tsz = (mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3143 tlb1[i].size = (tsz > 0) ? tsize2size(tsz) : 0;
3144 kernsize += tlb1[i].size;
3148 bp_ntlb1s = tlb1_idx;
3151 /* Purge the remaining entries */
3152 for (i = tlb1_idx; i < TLB1_ENTRIES; i++)
3153 tlb1_write_entry(i);
3155 /* Setup TLB miss defaults */
3156 set_mas4_defaults();
3160 pmap_early_io_map(vm_paddr_t pa, vm_size_t size)
3166 KASSERT(!pmap_bootstrapped, ("Do not use after PMAP is up!"));
3168 for (i = 0; i < tlb1_idx; i++) {
3169 if (!(tlb1[i].mas1 & MAS1_VALID))
3171 if (pa >= tlb1[i].phys && (pa + size) <=
3172 (tlb1[i].phys + tlb1[i].size))
3173 return (tlb1[i].virt + (pa - tlb1[i].phys));
3176 pa_base = trunc_page(pa);
3177 size = roundup(size + (pa - pa_base), PAGE_SIZE);
3178 tlb1_map_base = roundup2(tlb1_map_base, 1 << (ilog2(size) & ~1));
3179 va = tlb1_map_base + (pa - pa_base);
3182 sz = 1 << (ilog2(size) & ~1);
3183 tlb1_set_entry(tlb1_map_base, pa_base, sz, _TLB_ENTRY_IO);
3186 tlb1_map_base += sz;
3190 bp_ntlb1s = tlb1_idx;
3197 * Setup MAS4 defaults.
3198 * These values are loaded to MAS0-2 on a TLB miss.
3201 set_mas4_defaults(void)
3205 /* Defaults: TLB0, PID0, TSIZED=4K */
3206 mas4 = MAS4_TLBSELD0;
3207 mas4 |= (TLB_SIZE_4K << MAS4_TSIZED_SHIFT) & MAS4_TSIZED_MASK;
3211 mtspr(SPR_MAS4, mas4);
3212 __asm __volatile("isync");
3216 * Print out contents of the MAS registers for each TLB1 entry
3219 tlb1_print_tlbentries(void)
3221 uint32_t mas0, mas1, mas2, mas3, mas7;
3224 debugf("TLB1 entries:\n");
3225 for (i = 0; i < TLB1_ENTRIES; i++) {
3227 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(i);
3228 mtspr(SPR_MAS0, mas0);
3230 __asm __volatile("isync; tlbre");
3232 mas1 = mfspr(SPR_MAS1);
3233 mas2 = mfspr(SPR_MAS2);
3234 mas3 = mfspr(SPR_MAS3);
3235 mas7 = mfspr(SPR_MAS7);
3237 tlb_print_entry(i, mas1, mas2, mas3, mas7);
3242 * Print out contents of the in-ram tlb1 table.
3245 tlb1_print_entries(void)
3249 debugf("tlb1[] table entries:\n");
3250 for (i = 0; i < TLB1_ENTRIES; i++)
3251 tlb_print_entry(i, tlb1[i].mas1, tlb1[i].mas2, tlb1[i].mas3, 0);
3255 * Return 0 if the physical IO range is encompassed by one of the
3256 * the TLB1 entries, otherwise return related error code.
3259 tlb1_iomapped(int i, vm_paddr_t pa, vm_size_t size, vm_offset_t *va)
3262 vm_paddr_t pa_start;
3264 unsigned int entry_tsize;
3265 vm_size_t entry_size;
3267 *va = (vm_offset_t)NULL;
3269 /* Skip invalid entries */
3270 if (!(tlb1[i].mas1 & MAS1_VALID))
3274 * The entry must be cache-inhibited, guarded, and r/w
3275 * so it can function as an i/o page
3277 prot = tlb1[i].mas2 & (MAS2_I | MAS2_G);
3278 if (prot != (MAS2_I | MAS2_G))
3281 prot = tlb1[i].mas3 & (MAS3_SR | MAS3_SW);
3282 if (prot != (MAS3_SR | MAS3_SW))
3285 /* The address should be within the entry range. */
3286 entry_tsize = (tlb1[i].mas1 & MAS1_TSIZE_MASK) >> MAS1_TSIZE_SHIFT;
3287 KASSERT((entry_tsize), ("tlb1_iomapped: invalid entry tsize"));
3289 entry_size = tsize2size(entry_tsize);
3290 pa_start = tlb1[i].mas3 & MAS3_RPN;
3291 pa_end = pa_start + entry_size - 1;
3293 if ((pa < pa_start) || ((pa + size) > pa_end))
3296 /* Return virtual address of this mapping. */
3297 *va = (tlb1[i].mas2 & MAS2_EPN_MASK) + (pa - pa_start);