mmu_oea64.c revision 233529
1/*- 2 * Copyright (c) 2001 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Matt Thomas <matt@3am-software.com> of Allegro Networks, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the NetBSD 19 * Foundation, Inc. and its contributors. 20 * 4. Neither the name of The NetBSD Foundation nor the names of its 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36/*- 37 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 38 * Copyright (C) 1995, 1996 TooLs GmbH. 39 * All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. All advertising materials mentioning features or use of this software 50 * must display the following acknowledgement: 51 * This product includes software developed by TooLs GmbH. 52 * 4. The name of TooLs GmbH may not be used to endorse or promote products 53 * derived from this software without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 56 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 57 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 58 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 61 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 62 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 63 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 64 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 65 * 66 * $NetBSD: pmap.c,v 1.28 2000/03/26 20:42:36 kleink Exp $ 67 */ 68/*- 69 * Copyright (C) 2001 Benno Rice. 70 * All rights reserved. 71 * 72 * Redistribution and use in source and binary forms, with or without 73 * modification, are permitted provided that the following conditions 74 * are met: 75 * 1. Redistributions of source code must retain the above copyright 76 * notice, this list of conditions and the following disclaimer. 77 * 2. Redistributions in binary form must reproduce the above copyright 78 * notice, this list of conditions and the following disclaimer in the 79 * documentation and/or other materials provided with the distribution. 80 * 81 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 82 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 83 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 84 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 85 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 86 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 87 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 88 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 89 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 90 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 91 */ 92 93#include <sys/cdefs.h> 94__FBSDID("$FreeBSD: head/sys/powerpc/aim/mmu_oea64.c 233529 2012-03-27 01:24:18Z nwhitehorn $"); 95 96/* 97 * Manages physical address maps. 98 * 99 * In addition to hardware address maps, this module is called upon to 100 * provide software-use-only maps which may or may not be stored in the 101 * same form as hardware maps. These pseudo-maps are used to store 102 * intermediate results from copy operations to and from address spaces. 103 * 104 * Since the information managed by this module is also stored by the 105 * logical address mapping module, this module may throw away valid virtual 106 * to physical mappings at almost any time. However, invalidations of 107 * mappings must be done as requested. 108 * 109 * In order to cope with hardware architectures which make virtual to 110 * physical map invalidates expensive, this module may delay invalidate 111 * reduced protection operations until such time as they are actually 112 * necessary. This module is given full information as to which processors 113 * are currently using which maps, and to when physical maps must be made 114 * correct. 115 */ 116 117#include "opt_compat.h" 118#include "opt_kstack_pages.h" 119 120#include <sys/param.h> 121#include <sys/kernel.h> 122#include <sys/queue.h> 123#include <sys/cpuset.h> 124#include <sys/ktr.h> 125#include <sys/lock.h> 126#include <sys/msgbuf.h> 127#include <sys/mutex.h> 128#include <sys/proc.h> 129#include <sys/rwlock.h> 130#include <sys/sched.h> 131#include <sys/sysctl.h> 132#include <sys/systm.h> 133#include <sys/vmmeter.h> 134 135#include <sys/kdb.h> 136 137#include <dev/ofw/openfirm.h> 138 139#include <vm/vm.h> 140#include <vm/vm_param.h> 141#include <vm/vm_kern.h> 142#include <vm/vm_page.h> 143#include <vm/vm_map.h> 144#include <vm/vm_object.h> 145#include <vm/vm_extern.h> 146#include <vm/vm_pageout.h> 147#include <vm/vm_pager.h> 148#include <vm/uma.h> 149 150#include <machine/_inttypes.h> 151#include <machine/cpu.h> 152#include <machine/platform.h> 153#include <machine/frame.h> 154#include <machine/md_var.h> 155#include <machine/psl.h> 156#include <machine/bat.h> 157#include <machine/hid.h> 158#include <machine/pte.h> 159#include <machine/sr.h> 160#include <machine/trap.h> 161#include <machine/mmuvar.h> 162 163#include "mmu_oea64.h" 164#include "mmu_if.h" 165#include "moea64_if.h" 166 167void moea64_release_vsid(uint64_t vsid); 168uintptr_t moea64_get_unique_vsid(void); 169 170#define DISABLE_TRANS(msr) msr = mfmsr(); mtmsr(msr & ~PSL_DR) 171#define ENABLE_TRANS(msr) mtmsr(msr) 172 173#define VSID_MAKE(sr, hash) ((sr) | (((hash) & 0xfffff) << 4)) 174#define VSID_TO_HASH(vsid) (((vsid) >> 4) & 0xfffff) 175#define VSID_HASH_MASK 0x0000007fffffffffULL 176 177/* 178 * Locking semantics: 179 * -- Read lock: if no modifications are being made to either the PVO lists 180 * or page table or if any modifications being made result in internal 181 * changes (e.g. wiring, protection) such that the existence of the PVOs 182 * is unchanged and they remain associated with the same pmap (in which 183 * case the changes should be protected by the pmap lock) 184 * -- Write lock: required if PTEs/PVOs are being inserted or removed. 185 */ 186 187#define LOCK_TABLE_RD() rw_rlock(&moea64_table_lock) 188#define UNLOCK_TABLE_RD() rw_runlock(&moea64_table_lock) 189#define LOCK_TABLE_WR() rw_wlock(&moea64_table_lock) 190#define UNLOCK_TABLE_WR() rw_wunlock(&moea64_table_lock) 191 192struct ofw_map { 193 cell_t om_va; 194 cell_t om_len; 195 cell_t om_pa_hi; 196 cell_t om_pa_lo; 197 cell_t om_mode; 198}; 199 200/* 201 * Map of physical memory regions. 202 */ 203static struct mem_region *regions; 204static struct mem_region *pregions; 205static u_int phys_avail_count; 206static int regions_sz, pregions_sz; 207 208extern void bs_remap_earlyboot(void); 209 210/* 211 * Lock for the pteg and pvo tables. 212 */ 213struct rwlock moea64_table_lock; 214struct mtx moea64_slb_mutex; 215 216/* 217 * PTEG data. 218 */ 219u_int moea64_pteg_count; 220u_int moea64_pteg_mask; 221 222/* 223 * PVO data. 224 */ 225struct pvo_head *moea64_pvo_table; /* pvo entries by pteg index */ 226struct pvo_head moea64_pvo_kunmanaged = /* list of unmanaged pages */ 227 LIST_HEAD_INITIALIZER(moea64_pvo_kunmanaged); 228 229uma_zone_t moea64_upvo_zone; /* zone for pvo entries for unmanaged pages */ 230uma_zone_t moea64_mpvo_zone; /* zone for pvo entries for managed pages */ 231 232#define BPVO_POOL_SIZE 327680 233static struct pvo_entry *moea64_bpvo_pool; 234static int moea64_bpvo_pool_index = 0; 235 236#define VSID_NBPW (sizeof(u_int32_t) * 8) 237#ifdef __powerpc64__ 238#define NVSIDS (NPMAPS * 16) 239#define VSID_HASHMASK 0xffffffffUL 240#else 241#define NVSIDS NPMAPS 242#define VSID_HASHMASK 0xfffffUL 243#endif 244static u_int moea64_vsid_bitmap[NVSIDS / VSID_NBPW]; 245 246static boolean_t moea64_initialized = FALSE; 247 248/* 249 * Statistics. 250 */ 251u_int moea64_pte_valid = 0; 252u_int moea64_pte_overflow = 0; 253u_int moea64_pvo_entries = 0; 254u_int moea64_pvo_enter_calls = 0; 255u_int moea64_pvo_remove_calls = 0; 256SYSCTL_INT(_machdep, OID_AUTO, moea64_pte_valid, CTLFLAG_RD, 257 &moea64_pte_valid, 0, ""); 258SYSCTL_INT(_machdep, OID_AUTO, moea64_pte_overflow, CTLFLAG_RD, 259 &moea64_pte_overflow, 0, ""); 260SYSCTL_INT(_machdep, OID_AUTO, moea64_pvo_entries, CTLFLAG_RD, 261 &moea64_pvo_entries, 0, ""); 262SYSCTL_INT(_machdep, OID_AUTO, moea64_pvo_enter_calls, CTLFLAG_RD, 263 &moea64_pvo_enter_calls, 0, ""); 264SYSCTL_INT(_machdep, OID_AUTO, moea64_pvo_remove_calls, CTLFLAG_RD, 265 &moea64_pvo_remove_calls, 0, ""); 266 267vm_offset_t moea64_scratchpage_va[2]; 268struct pvo_entry *moea64_scratchpage_pvo[2]; 269uintptr_t moea64_scratchpage_pte[2]; 270struct mtx moea64_scratchpage_mtx; 271 272uint64_t moea64_large_page_mask = 0; 273int moea64_large_page_size = 0; 274int moea64_large_page_shift = 0; 275 276/* 277 * PVO calls. 278 */ 279static int moea64_pvo_enter(mmu_t, pmap_t, uma_zone_t, struct pvo_head *, 280 vm_offset_t, vm_offset_t, uint64_t, int); 281static void moea64_pvo_remove(mmu_t, struct pvo_entry *); 282static struct pvo_entry *moea64_pvo_find_va(pmap_t, vm_offset_t); 283 284/* 285 * Utility routines. 286 */ 287static void moea64_enter_locked(mmu_t, pmap_t, vm_offset_t, 288 vm_page_t, vm_prot_t, boolean_t); 289static boolean_t moea64_query_bit(mmu_t, vm_page_t, u_int64_t); 290static u_int moea64_clear_bit(mmu_t, vm_page_t, u_int64_t); 291static void moea64_kremove(mmu_t, vm_offset_t); 292static void moea64_syncicache(mmu_t, pmap_t pmap, vm_offset_t va, 293 vm_offset_t pa, vm_size_t sz); 294 295/* 296 * Kernel MMU interface 297 */ 298void moea64_change_wiring(mmu_t, pmap_t, vm_offset_t, boolean_t); 299void moea64_clear_modify(mmu_t, vm_page_t); 300void moea64_clear_reference(mmu_t, vm_page_t); 301void moea64_copy_page(mmu_t, vm_page_t, vm_page_t); 302void moea64_enter(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t, boolean_t); 303void moea64_enter_object(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_page_t, 304 vm_prot_t); 305void moea64_enter_quick(mmu_t, pmap_t, vm_offset_t, vm_page_t, vm_prot_t); 306vm_paddr_t moea64_extract(mmu_t, pmap_t, vm_offset_t); 307vm_page_t moea64_extract_and_hold(mmu_t, pmap_t, vm_offset_t, vm_prot_t); 308void moea64_init(mmu_t); 309boolean_t moea64_is_modified(mmu_t, vm_page_t); 310boolean_t moea64_is_prefaultable(mmu_t, pmap_t, vm_offset_t); 311boolean_t moea64_is_referenced(mmu_t, vm_page_t); 312boolean_t moea64_ts_referenced(mmu_t, vm_page_t); 313vm_offset_t moea64_map(mmu_t, vm_offset_t *, vm_offset_t, vm_offset_t, int); 314boolean_t moea64_page_exists_quick(mmu_t, pmap_t, vm_page_t); 315int moea64_page_wired_mappings(mmu_t, vm_page_t); 316void moea64_pinit(mmu_t, pmap_t); 317void moea64_pinit0(mmu_t, pmap_t); 318void moea64_protect(mmu_t, pmap_t, vm_offset_t, vm_offset_t, vm_prot_t); 319void moea64_qenter(mmu_t, vm_offset_t, vm_page_t *, int); 320void moea64_qremove(mmu_t, vm_offset_t, int); 321void moea64_release(mmu_t, pmap_t); 322void moea64_remove(mmu_t, pmap_t, vm_offset_t, vm_offset_t); 323void moea64_remove_pages(mmu_t, pmap_t); 324void moea64_remove_all(mmu_t, vm_page_t); 325void moea64_remove_write(mmu_t, vm_page_t); 326void moea64_zero_page(mmu_t, vm_page_t); 327void moea64_zero_page_area(mmu_t, vm_page_t, int, int); 328void moea64_zero_page_idle(mmu_t, vm_page_t); 329void moea64_activate(mmu_t, struct thread *); 330void moea64_deactivate(mmu_t, struct thread *); 331void *moea64_mapdev(mmu_t, vm_offset_t, vm_size_t); 332void *moea64_mapdev_attr(mmu_t, vm_offset_t, vm_size_t, vm_memattr_t); 333void moea64_unmapdev(mmu_t, vm_offset_t, vm_size_t); 334vm_offset_t moea64_kextract(mmu_t, vm_offset_t); 335void moea64_page_set_memattr(mmu_t, vm_page_t m, vm_memattr_t ma); 336void moea64_kenter_attr(mmu_t, vm_offset_t, vm_offset_t, vm_memattr_t ma); 337void moea64_kenter(mmu_t, vm_offset_t, vm_offset_t); 338boolean_t moea64_dev_direct_mapped(mmu_t, vm_offset_t, vm_size_t); 339static void moea64_sync_icache(mmu_t, pmap_t, vm_offset_t, vm_size_t); 340 341static mmu_method_t moea64_methods[] = { 342 MMUMETHOD(mmu_change_wiring, moea64_change_wiring), 343 MMUMETHOD(mmu_clear_modify, moea64_clear_modify), 344 MMUMETHOD(mmu_clear_reference, moea64_clear_reference), 345 MMUMETHOD(mmu_copy_page, moea64_copy_page), 346 MMUMETHOD(mmu_enter, moea64_enter), 347 MMUMETHOD(mmu_enter_object, moea64_enter_object), 348 MMUMETHOD(mmu_enter_quick, moea64_enter_quick), 349 MMUMETHOD(mmu_extract, moea64_extract), 350 MMUMETHOD(mmu_extract_and_hold, moea64_extract_and_hold), 351 MMUMETHOD(mmu_init, moea64_init), 352 MMUMETHOD(mmu_is_modified, moea64_is_modified), 353 MMUMETHOD(mmu_is_prefaultable, moea64_is_prefaultable), 354 MMUMETHOD(mmu_is_referenced, moea64_is_referenced), 355 MMUMETHOD(mmu_ts_referenced, moea64_ts_referenced), 356 MMUMETHOD(mmu_map, moea64_map), 357 MMUMETHOD(mmu_page_exists_quick,moea64_page_exists_quick), 358 MMUMETHOD(mmu_page_wired_mappings,moea64_page_wired_mappings), 359 MMUMETHOD(mmu_pinit, moea64_pinit), 360 MMUMETHOD(mmu_pinit0, moea64_pinit0), 361 MMUMETHOD(mmu_protect, moea64_protect), 362 MMUMETHOD(mmu_qenter, moea64_qenter), 363 MMUMETHOD(mmu_qremove, moea64_qremove), 364 MMUMETHOD(mmu_release, moea64_release), 365 MMUMETHOD(mmu_remove, moea64_remove), 366 MMUMETHOD(mmu_remove_pages, moea64_remove_pages), 367 MMUMETHOD(mmu_remove_all, moea64_remove_all), 368 MMUMETHOD(mmu_remove_write, moea64_remove_write), 369 MMUMETHOD(mmu_sync_icache, moea64_sync_icache), 370 MMUMETHOD(mmu_zero_page, moea64_zero_page), 371 MMUMETHOD(mmu_zero_page_area, moea64_zero_page_area), 372 MMUMETHOD(mmu_zero_page_idle, moea64_zero_page_idle), 373 MMUMETHOD(mmu_activate, moea64_activate), 374 MMUMETHOD(mmu_deactivate, moea64_deactivate), 375 MMUMETHOD(mmu_page_set_memattr, moea64_page_set_memattr), 376 377 /* Internal interfaces */ 378 MMUMETHOD(mmu_mapdev, moea64_mapdev), 379 MMUMETHOD(mmu_mapdev_attr, moea64_mapdev_attr), 380 MMUMETHOD(mmu_unmapdev, moea64_unmapdev), 381 MMUMETHOD(mmu_kextract, moea64_kextract), 382 MMUMETHOD(mmu_kenter, moea64_kenter), 383 MMUMETHOD(mmu_kenter_attr, moea64_kenter_attr), 384 MMUMETHOD(mmu_dev_direct_mapped,moea64_dev_direct_mapped), 385 386 { 0, 0 } 387}; 388 389MMU_DEF(oea64_mmu, "mmu_oea64_base", moea64_methods, 0); 390 391static __inline u_int 392va_to_pteg(uint64_t vsid, vm_offset_t addr, int large) 393{ 394 uint64_t hash; 395 int shift; 396 397 shift = large ? moea64_large_page_shift : ADDR_PIDX_SHFT; 398 hash = (vsid & VSID_HASH_MASK) ^ (((uint64_t)addr & ADDR_PIDX) >> 399 shift); 400 return (hash & moea64_pteg_mask); 401} 402 403static __inline struct pvo_head * 404vm_page_to_pvoh(vm_page_t m) 405{ 406 407 return (&m->md.mdpg_pvoh); 408} 409 410static __inline void 411moea64_pte_create(struct lpte *pt, uint64_t vsid, vm_offset_t va, 412 uint64_t pte_lo, int flags) 413{ 414 415 /* 416 * Construct a PTE. Default to IMB initially. Valid bit only gets 417 * set when the real pte is set in memory. 418 * 419 * Note: Don't set the valid bit for correct operation of tlb update. 420 */ 421 pt->pte_hi = (vsid << LPTE_VSID_SHIFT) | 422 (((uint64_t)(va & ADDR_PIDX) >> ADDR_API_SHFT64) & LPTE_API); 423 424 if (flags & PVO_LARGE) 425 pt->pte_hi |= LPTE_BIG; 426 427 pt->pte_lo = pte_lo; 428} 429 430static __inline uint64_t 431moea64_calc_wimg(vm_offset_t pa, vm_memattr_t ma) 432{ 433 uint64_t pte_lo; 434 int i; 435 436 if (ma != VM_MEMATTR_DEFAULT) { 437 switch (ma) { 438 case VM_MEMATTR_UNCACHEABLE: 439 return (LPTE_I | LPTE_G); 440 case VM_MEMATTR_WRITE_COMBINING: 441 case VM_MEMATTR_WRITE_BACK: 442 case VM_MEMATTR_PREFETCHABLE: 443 return (LPTE_I); 444 case VM_MEMATTR_WRITE_THROUGH: 445 return (LPTE_W | LPTE_M); 446 } 447 } 448 449 /* 450 * Assume the page is cache inhibited and access is guarded unless 451 * it's in our available memory array. 452 */ 453 pte_lo = LPTE_I | LPTE_G; 454 for (i = 0; i < pregions_sz; i++) { 455 if ((pa >= pregions[i].mr_start) && 456 (pa < (pregions[i].mr_start + pregions[i].mr_size))) { 457 pte_lo &= ~(LPTE_I | LPTE_G); 458 pte_lo |= LPTE_M; 459 break; 460 } 461 } 462 463 return pte_lo; 464} 465 466/* 467 * Quick sort callout for comparing memory regions. 468 */ 469static int om_cmp(const void *a, const void *b); 470 471static int 472om_cmp(const void *a, const void *b) 473{ 474 const struct ofw_map *mapa; 475 const struct ofw_map *mapb; 476 477 mapa = a; 478 mapb = b; 479 if (mapa->om_pa_hi < mapb->om_pa_hi) 480 return (-1); 481 else if (mapa->om_pa_hi > mapb->om_pa_hi) 482 return (1); 483 else if (mapa->om_pa_lo < mapb->om_pa_lo) 484 return (-1); 485 else if (mapa->om_pa_lo > mapb->om_pa_lo) 486 return (1); 487 else 488 return (0); 489} 490 491static void 492moea64_add_ofw_mappings(mmu_t mmup, phandle_t mmu, size_t sz) 493{ 494 struct ofw_map translations[sz/sizeof(struct ofw_map)]; 495 register_t msr; 496 vm_offset_t off; 497 vm_paddr_t pa_base; 498 int i; 499 500 bzero(translations, sz); 501 if (OF_getprop(mmu, "translations", translations, sz) == -1) 502 panic("moea64_bootstrap: can't get ofw translations"); 503 504 CTR0(KTR_PMAP, "moea64_add_ofw_mappings: translations"); 505 sz /= sizeof(*translations); 506 qsort(translations, sz, sizeof (*translations), om_cmp); 507 508 for (i = 0; i < sz; i++) { 509 CTR3(KTR_PMAP, "translation: pa=%#x va=%#x len=%#x", 510 (uint32_t)(translations[i].om_pa_lo), translations[i].om_va, 511 translations[i].om_len); 512 513 if (translations[i].om_pa_lo % PAGE_SIZE) 514 panic("OFW translation not page-aligned!"); 515 516 pa_base = translations[i].om_pa_lo; 517 518 #ifdef __powerpc64__ 519 pa_base += (vm_offset_t)translations[i].om_pa_hi << 32; 520 #else 521 if (translations[i].om_pa_hi) 522 panic("OFW translations above 32-bit boundary!"); 523 #endif 524 525 /* Now enter the pages for this mapping */ 526 527 DISABLE_TRANS(msr); 528 for (off = 0; off < translations[i].om_len; off += PAGE_SIZE) { 529 if (moea64_pvo_find_va(kernel_pmap, 530 translations[i].om_va + off) != NULL) 531 continue; 532 533 moea64_kenter(mmup, translations[i].om_va + off, 534 pa_base + off); 535 } 536 ENABLE_TRANS(msr); 537 } 538} 539 540#ifdef __powerpc64__ 541static void 542moea64_probe_large_page(void) 543{ 544 uint16_t pvr = mfpvr() >> 16; 545 546 switch (pvr) { 547 case IBM970: 548 case IBM970FX: 549 case IBM970MP: 550 powerpc_sync(); isync(); 551 mtspr(SPR_HID4, mfspr(SPR_HID4) & ~HID4_970_DISABLE_LG_PG); 552 powerpc_sync(); isync(); 553 554 /* FALLTHROUGH */ 555 case IBMCELLBE: 556 moea64_large_page_size = 0x1000000; /* 16 MB */ 557 moea64_large_page_shift = 24; 558 break; 559 default: 560 moea64_large_page_size = 0; 561 } 562 563 moea64_large_page_mask = moea64_large_page_size - 1; 564} 565 566static void 567moea64_bootstrap_slb_prefault(vm_offset_t va, int large) 568{ 569 struct slb *cache; 570 struct slb entry; 571 uint64_t esid, slbe; 572 uint64_t i; 573 574 cache = PCPU_GET(slb); 575 esid = va >> ADDR_SR_SHFT; 576 slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID; 577 578 for (i = 0; i < 64; i++) { 579 if (cache[i].slbe == (slbe | i)) 580 return; 581 } 582 583 entry.slbe = slbe; 584 entry.slbv = KERNEL_VSID(esid) << SLBV_VSID_SHIFT; 585 if (large) 586 entry.slbv |= SLBV_L; 587 588 slb_insert_kernel(entry.slbe, entry.slbv); 589} 590#endif 591 592static void 593moea64_setup_direct_map(mmu_t mmup, vm_offset_t kernelstart, 594 vm_offset_t kernelend) 595{ 596 register_t msr; 597 vm_paddr_t pa; 598 vm_offset_t size, off; 599 uint64_t pte_lo; 600 int i; 601 602 if (moea64_large_page_size == 0) 603 hw_direct_map = 0; 604 605 DISABLE_TRANS(msr); 606 if (hw_direct_map) { 607 LOCK_TABLE_WR(); 608 PMAP_LOCK(kernel_pmap); 609 for (i = 0; i < pregions_sz; i++) { 610 for (pa = pregions[i].mr_start; pa < pregions[i].mr_start + 611 pregions[i].mr_size; pa += moea64_large_page_size) { 612 pte_lo = LPTE_M; 613 614 /* 615 * Set memory access as guarded if prefetch within 616 * the page could exit the available physmem area. 617 */ 618 if (pa & moea64_large_page_mask) { 619 pa &= moea64_large_page_mask; 620 pte_lo |= LPTE_G; 621 } 622 if (pa + moea64_large_page_size > 623 pregions[i].mr_start + pregions[i].mr_size) 624 pte_lo |= LPTE_G; 625 626 moea64_pvo_enter(mmup, kernel_pmap, moea64_upvo_zone, 627 &moea64_pvo_kunmanaged, pa, pa, 628 pte_lo, PVO_WIRED | PVO_LARGE); 629 } 630 } 631 PMAP_UNLOCK(kernel_pmap); 632 UNLOCK_TABLE_WR(); 633 } else { 634 size = sizeof(struct pvo_head) * moea64_pteg_count; 635 off = (vm_offset_t)(moea64_pvo_table); 636 for (pa = off; pa < off + size; pa += PAGE_SIZE) 637 moea64_kenter(mmup, pa, pa); 638 size = BPVO_POOL_SIZE*sizeof(struct pvo_entry); 639 off = (vm_offset_t)(moea64_bpvo_pool); 640 for (pa = off; pa < off + size; pa += PAGE_SIZE) 641 moea64_kenter(mmup, pa, pa); 642 643 /* 644 * Map certain important things, like ourselves. 645 * 646 * NOTE: We do not map the exception vector space. That code is 647 * used only in real mode, and leaving it unmapped allows us to 648 * catch NULL pointer deferences, instead of making NULL a valid 649 * address. 650 */ 651 652 for (pa = kernelstart & ~PAGE_MASK; pa < kernelend; 653 pa += PAGE_SIZE) 654 moea64_kenter(mmup, pa, pa); 655 } 656 ENABLE_TRANS(msr); 657} 658 659void 660moea64_early_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend) 661{ 662 int i, j; 663 vm_size_t physsz, hwphyssz; 664 665#ifndef __powerpc64__ 666 /* We don't have a direct map since there is no BAT */ 667 hw_direct_map = 0; 668 669 /* Make sure battable is zero, since we have no BAT */ 670 for (i = 0; i < 16; i++) { 671 battable[i].batu = 0; 672 battable[i].batl = 0; 673 } 674#else 675 moea64_probe_large_page(); 676 677 /* Use a direct map if we have large page support */ 678 if (moea64_large_page_size > 0) 679 hw_direct_map = 1; 680 else 681 hw_direct_map = 0; 682#endif 683 684 /* Get physical memory regions from firmware */ 685 mem_regions(&pregions, &pregions_sz, ®ions, ®ions_sz); 686 CTR0(KTR_PMAP, "moea64_bootstrap: physical memory"); 687 688 if (sizeof(phys_avail)/sizeof(phys_avail[0]) < regions_sz) 689 panic("moea64_bootstrap: phys_avail too small"); 690 691 phys_avail_count = 0; 692 physsz = 0; 693 hwphyssz = 0; 694 TUNABLE_ULONG_FETCH("hw.physmem", (u_long *) &hwphyssz); 695 for (i = 0, j = 0; i < regions_sz; i++, j += 2) { 696 CTR3(KTR_PMAP, "region: %#x - %#x (%#x)", regions[i].mr_start, 697 regions[i].mr_start + regions[i].mr_size, 698 regions[i].mr_size); 699 if (hwphyssz != 0 && 700 (physsz + regions[i].mr_size) >= hwphyssz) { 701 if (physsz < hwphyssz) { 702 phys_avail[j] = regions[i].mr_start; 703 phys_avail[j + 1] = regions[i].mr_start + 704 hwphyssz - physsz; 705 physsz = hwphyssz; 706 phys_avail_count++; 707 } 708 break; 709 } 710 phys_avail[j] = regions[i].mr_start; 711 phys_avail[j + 1] = regions[i].mr_start + regions[i].mr_size; 712 phys_avail_count++; 713 physsz += regions[i].mr_size; 714 } 715 716 /* Check for overlap with the kernel and exception vectors */ 717 for (j = 0; j < 2*phys_avail_count; j+=2) { 718 if (phys_avail[j] < EXC_LAST) 719 phys_avail[j] += EXC_LAST; 720 721 if (kernelstart >= phys_avail[j] && 722 kernelstart < phys_avail[j+1]) { 723 if (kernelend < phys_avail[j+1]) { 724 phys_avail[2*phys_avail_count] = 725 (kernelend & ~PAGE_MASK) + PAGE_SIZE; 726 phys_avail[2*phys_avail_count + 1] = 727 phys_avail[j+1]; 728 phys_avail_count++; 729 } 730 731 phys_avail[j+1] = kernelstart & ~PAGE_MASK; 732 } 733 734 if (kernelend >= phys_avail[j] && 735 kernelend < phys_avail[j+1]) { 736 if (kernelstart > phys_avail[j]) { 737 phys_avail[2*phys_avail_count] = phys_avail[j]; 738 phys_avail[2*phys_avail_count + 1] = 739 kernelstart & ~PAGE_MASK; 740 phys_avail_count++; 741 } 742 743 phys_avail[j] = (kernelend & ~PAGE_MASK) + PAGE_SIZE; 744 } 745 } 746 747 physmem = btoc(physsz); 748 749#ifdef PTEGCOUNT 750 moea64_pteg_count = PTEGCOUNT; 751#else 752 moea64_pteg_count = 0x1000; 753 754 while (moea64_pteg_count < physmem) 755 moea64_pteg_count <<= 1; 756 757 moea64_pteg_count >>= 1; 758#endif /* PTEGCOUNT */ 759} 760 761void 762moea64_mid_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend) 763{ 764 vm_size_t size; 765 register_t msr; 766 int i; 767 768 /* 769 * Set PTEG mask 770 */ 771 moea64_pteg_mask = moea64_pteg_count - 1; 772 773 /* 774 * Allocate pv/overflow lists. 775 */ 776 size = sizeof(struct pvo_head) * moea64_pteg_count; 777 778 moea64_pvo_table = (struct pvo_head *)moea64_bootstrap_alloc(size, 779 PAGE_SIZE); 780 CTR1(KTR_PMAP, "moea64_bootstrap: PVO table at %p", moea64_pvo_table); 781 782 DISABLE_TRANS(msr); 783 for (i = 0; i < moea64_pteg_count; i++) 784 LIST_INIT(&moea64_pvo_table[i]); 785 ENABLE_TRANS(msr); 786 787 /* 788 * Initialize the lock that synchronizes access to the pteg and pvo 789 * tables. 790 */ 791 rw_init_flags(&moea64_table_lock, "pmap tables", RW_RECURSE); 792 mtx_init(&moea64_slb_mutex, "SLB table", NULL, MTX_DEF); 793 794 /* 795 * Initialise the unmanaged pvo pool. 796 */ 797 moea64_bpvo_pool = (struct pvo_entry *)moea64_bootstrap_alloc( 798 BPVO_POOL_SIZE*sizeof(struct pvo_entry), 0); 799 moea64_bpvo_pool_index = 0; 800 801 /* 802 * Make sure kernel vsid is allocated as well as VSID 0. 803 */ 804 #ifndef __powerpc64__ 805 moea64_vsid_bitmap[(KERNEL_VSIDBITS & (NVSIDS - 1)) / VSID_NBPW] 806 |= 1 << (KERNEL_VSIDBITS % VSID_NBPW); 807 moea64_vsid_bitmap[0] |= 1; 808 #endif 809 810 /* 811 * Initialize the kernel pmap (which is statically allocated). 812 */ 813 #ifdef __powerpc64__ 814 for (i = 0; i < 64; i++) { 815 pcpup->pc_slb[i].slbv = 0; 816 pcpup->pc_slb[i].slbe = 0; 817 } 818 #else 819 for (i = 0; i < 16; i++) 820 kernel_pmap->pm_sr[i] = EMPTY_SEGMENT + i; 821 #endif 822 823 kernel_pmap->pmap_phys = kernel_pmap; 824 CPU_FILL(&kernel_pmap->pm_active); 825 LIST_INIT(&kernel_pmap->pmap_pvo); 826 827 PMAP_LOCK_INIT(kernel_pmap); 828 829 /* 830 * Now map in all the other buffers we allocated earlier 831 */ 832 833 moea64_setup_direct_map(mmup, kernelstart, kernelend); 834} 835 836void 837moea64_late_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend) 838{ 839 ihandle_t mmui; 840 phandle_t chosen; 841 phandle_t mmu; 842 size_t sz; 843 int i; 844 vm_offset_t pa, va; 845 void *dpcpu; 846 847 /* 848 * Set up the Open Firmware pmap and add its mappings if not in real 849 * mode. 850 */ 851 852 chosen = OF_finddevice("/chosen"); 853 if (chosen != -1 && OF_getprop(chosen, "mmu", &mmui, 4) != -1) { 854 mmu = OF_instance_to_package(mmui); 855 if (mmu == -1 || (sz = OF_getproplen(mmu, "translations")) == -1) 856 sz = 0; 857 if (sz > 6144 /* tmpstksz - 2 KB headroom */) 858 panic("moea64_bootstrap: too many ofw translations"); 859 860 if (sz > 0) 861 moea64_add_ofw_mappings(mmup, mmu, sz); 862 } 863 864 /* 865 * Calculate the last available physical address. 866 */ 867 for (i = 0; phys_avail[i + 2] != 0; i += 2) 868 ; 869 Maxmem = powerpc_btop(phys_avail[i + 1]); 870 871 /* 872 * Initialize MMU and remap early physical mappings 873 */ 874 MMU_CPU_BOOTSTRAP(mmup,0); 875 mtmsr(mfmsr() | PSL_DR | PSL_IR); 876 pmap_bootstrapped++; 877 bs_remap_earlyboot(); 878 879 /* 880 * Set the start and end of kva. 881 */ 882 virtual_avail = VM_MIN_KERNEL_ADDRESS; 883 virtual_end = VM_MAX_SAFE_KERNEL_ADDRESS; 884 885 /* 886 * Map the entire KVA range into the SLB. We must not fault there. 887 */ 888 #ifdef __powerpc64__ 889 for (va = virtual_avail; va < virtual_end; va += SEGMENT_LENGTH) 890 moea64_bootstrap_slb_prefault(va, 0); 891 #endif 892 893 /* 894 * Figure out how far we can extend virtual_end into segment 16 895 * without running into existing mappings. Segment 16 is guaranteed 896 * to contain neither RAM nor devices (at least on Apple hardware), 897 * but will generally contain some OFW mappings we should not 898 * step on. 899 */ 900 901 #ifndef __powerpc64__ /* KVA is in high memory on PPC64 */ 902 PMAP_LOCK(kernel_pmap); 903 while (virtual_end < VM_MAX_KERNEL_ADDRESS && 904 moea64_pvo_find_va(kernel_pmap, virtual_end+1) == NULL) 905 virtual_end += PAGE_SIZE; 906 PMAP_UNLOCK(kernel_pmap); 907 #endif 908 909 /* 910 * Allocate a kernel stack with a guard page for thread0 and map it 911 * into the kernel page map. 912 */ 913 pa = moea64_bootstrap_alloc(KSTACK_PAGES * PAGE_SIZE, PAGE_SIZE); 914 va = virtual_avail + KSTACK_GUARD_PAGES * PAGE_SIZE; 915 virtual_avail = va + KSTACK_PAGES * PAGE_SIZE; 916 CTR2(KTR_PMAP, "moea64_bootstrap: kstack0 at %#x (%#x)", pa, va); 917 thread0.td_kstack = va; 918 thread0.td_kstack_pages = KSTACK_PAGES; 919 for (i = 0; i < KSTACK_PAGES; i++) { 920 moea64_kenter(mmup, va, pa); 921 pa += PAGE_SIZE; 922 va += PAGE_SIZE; 923 } 924 925 /* 926 * Allocate virtual address space for the message buffer. 927 */ 928 pa = msgbuf_phys = moea64_bootstrap_alloc(msgbufsize, PAGE_SIZE); 929 msgbufp = (struct msgbuf *)virtual_avail; 930 va = virtual_avail; 931 virtual_avail += round_page(msgbufsize); 932 while (va < virtual_avail) { 933 moea64_kenter(mmup, va, pa); 934 pa += PAGE_SIZE; 935 va += PAGE_SIZE; 936 } 937 938 /* 939 * Allocate virtual address space for the dynamic percpu area. 940 */ 941 pa = moea64_bootstrap_alloc(DPCPU_SIZE, PAGE_SIZE); 942 dpcpu = (void *)virtual_avail; 943 va = virtual_avail; 944 virtual_avail += DPCPU_SIZE; 945 while (va < virtual_avail) { 946 moea64_kenter(mmup, va, pa); 947 pa += PAGE_SIZE; 948 va += PAGE_SIZE; 949 } 950 dpcpu_init(dpcpu, 0); 951 952 /* 953 * Allocate some things for page zeroing. We put this directly 954 * in the page table, marked with LPTE_LOCKED, to avoid any 955 * of the PVO book-keeping or other parts of the VM system 956 * from even knowing that this hack exists. 957 */ 958 959 if (!hw_direct_map) { 960 mtx_init(&moea64_scratchpage_mtx, "pvo zero page", NULL, 961 MTX_DEF); 962 for (i = 0; i < 2; i++) { 963 moea64_scratchpage_va[i] = (virtual_end+1) - PAGE_SIZE; 964 virtual_end -= PAGE_SIZE; 965 966 moea64_kenter(mmup, moea64_scratchpage_va[i], 0); 967 968 moea64_scratchpage_pvo[i] = moea64_pvo_find_va( 969 kernel_pmap, (vm_offset_t)moea64_scratchpage_va[i]); 970 LOCK_TABLE_RD(); 971 moea64_scratchpage_pte[i] = MOEA64_PVO_TO_PTE( 972 mmup, moea64_scratchpage_pvo[i]); 973 moea64_scratchpage_pvo[i]->pvo_pte.lpte.pte_hi 974 |= LPTE_LOCKED; 975 MOEA64_PTE_CHANGE(mmup, moea64_scratchpage_pte[i], 976 &moea64_scratchpage_pvo[i]->pvo_pte.lpte, 977 moea64_scratchpage_pvo[i]->pvo_vpn); 978 UNLOCK_TABLE_RD(); 979 } 980 } 981} 982 983/* 984 * Activate a user pmap. The pmap must be activated before its address 985 * space can be accessed in any way. 986 */ 987void 988moea64_activate(mmu_t mmu, struct thread *td) 989{ 990 pmap_t pm; 991 992 pm = &td->td_proc->p_vmspace->vm_pmap; 993 CPU_SET(PCPU_GET(cpuid), &pm->pm_active); 994 995 #ifdef __powerpc64__ 996 PCPU_SET(userslb, pm->pm_slb); 997 #else 998 PCPU_SET(curpmap, pm->pmap_phys); 999 #endif 1000} 1001 1002void 1003moea64_deactivate(mmu_t mmu, struct thread *td) 1004{ 1005 pmap_t pm; 1006 1007 pm = &td->td_proc->p_vmspace->vm_pmap; 1008 CPU_CLR(PCPU_GET(cpuid), &pm->pm_active); 1009 #ifdef __powerpc64__ 1010 PCPU_SET(userslb, NULL); 1011 #else 1012 PCPU_SET(curpmap, NULL); 1013 #endif 1014} 1015 1016void 1017moea64_change_wiring(mmu_t mmu, pmap_t pm, vm_offset_t va, boolean_t wired) 1018{ 1019 struct pvo_entry *pvo; 1020 uintptr_t pt; 1021 uint64_t vsid; 1022 int i, ptegidx; 1023 1024 LOCK_TABLE_WR(); 1025 PMAP_LOCK(pm); 1026 pvo = moea64_pvo_find_va(pm, va & ~ADDR_POFF); 1027 1028 if (pvo != NULL) { 1029 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 1030 1031 if (wired) { 1032 if ((pvo->pvo_vaddr & PVO_WIRED) == 0) 1033 pm->pm_stats.wired_count++; 1034 pvo->pvo_vaddr |= PVO_WIRED; 1035 pvo->pvo_pte.lpte.pte_hi |= LPTE_WIRED; 1036 } else { 1037 if ((pvo->pvo_vaddr & PVO_WIRED) != 0) 1038 pm->pm_stats.wired_count--; 1039 pvo->pvo_vaddr &= ~PVO_WIRED; 1040 pvo->pvo_pte.lpte.pte_hi &= ~LPTE_WIRED; 1041 } 1042 1043 if (pt != -1) { 1044 /* Update wiring flag in page table. */ 1045 MOEA64_PTE_CHANGE(mmu, pt, &pvo->pvo_pte.lpte, 1046 pvo->pvo_vpn); 1047 } else if (wired) { 1048 /* 1049 * If we are wiring the page, and it wasn't in the 1050 * page table before, add it. 1051 */ 1052 vsid = PVO_VSID(pvo); 1053 ptegidx = va_to_pteg(vsid, PVO_VADDR(pvo), 1054 pvo->pvo_vaddr & PVO_LARGE); 1055 1056 i = MOEA64_PTE_INSERT(mmu, ptegidx, &pvo->pvo_pte.lpte); 1057 1058 if (i >= 0) { 1059 PVO_PTEGIDX_CLR(pvo); 1060 PVO_PTEGIDX_SET(pvo, i); 1061 } 1062 } 1063 1064 } 1065 UNLOCK_TABLE_WR(); 1066 PMAP_UNLOCK(pm); 1067} 1068 1069/* 1070 * This goes through and sets the physical address of our 1071 * special scratch PTE to the PA we want to zero or copy. Because 1072 * of locking issues (this can get called in pvo_enter() by 1073 * the UMA allocator), we can't use most other utility functions here 1074 */ 1075 1076static __inline 1077void moea64_set_scratchpage_pa(mmu_t mmup, int which, vm_offset_t pa) { 1078 1079 KASSERT(!hw_direct_map, ("Using OEA64 scratchpage with a direct map!")); 1080 mtx_assert(&moea64_scratchpage_mtx, MA_OWNED); 1081 1082 moea64_scratchpage_pvo[which]->pvo_pte.lpte.pte_lo &= 1083 ~(LPTE_WIMG | LPTE_RPGN); 1084 moea64_scratchpage_pvo[which]->pvo_pte.lpte.pte_lo |= 1085 moea64_calc_wimg(pa, VM_MEMATTR_DEFAULT) | (uint64_t)pa; 1086 MOEA64_PTE_CHANGE(mmup, moea64_scratchpage_pte[which], 1087 &moea64_scratchpage_pvo[which]->pvo_pte.lpte, 1088 moea64_scratchpage_pvo[which]->pvo_vpn); 1089 isync(); 1090} 1091 1092void 1093moea64_copy_page(mmu_t mmu, vm_page_t msrc, vm_page_t mdst) 1094{ 1095 vm_offset_t dst; 1096 vm_offset_t src; 1097 1098 dst = VM_PAGE_TO_PHYS(mdst); 1099 src = VM_PAGE_TO_PHYS(msrc); 1100 1101 if (hw_direct_map) { 1102 kcopy((void *)src, (void *)dst, PAGE_SIZE); 1103 } else { 1104 mtx_lock(&moea64_scratchpage_mtx); 1105 1106 moea64_set_scratchpage_pa(mmu, 0, src); 1107 moea64_set_scratchpage_pa(mmu, 1, dst); 1108 1109 kcopy((void *)moea64_scratchpage_va[0], 1110 (void *)moea64_scratchpage_va[1], PAGE_SIZE); 1111 1112 mtx_unlock(&moea64_scratchpage_mtx); 1113 } 1114} 1115 1116void 1117moea64_zero_page_area(mmu_t mmu, vm_page_t m, int off, int size) 1118{ 1119 vm_offset_t pa = VM_PAGE_TO_PHYS(m); 1120 1121 if (size + off > PAGE_SIZE) 1122 panic("moea64_zero_page: size + off > PAGE_SIZE"); 1123 1124 if (hw_direct_map) { 1125 bzero((caddr_t)pa + off, size); 1126 } else { 1127 mtx_lock(&moea64_scratchpage_mtx); 1128 moea64_set_scratchpage_pa(mmu, 0, pa); 1129 bzero((caddr_t)moea64_scratchpage_va[0] + off, size); 1130 mtx_unlock(&moea64_scratchpage_mtx); 1131 } 1132} 1133 1134/* 1135 * Zero a page of physical memory by temporarily mapping it 1136 */ 1137void 1138moea64_zero_page(mmu_t mmu, vm_page_t m) 1139{ 1140 vm_offset_t pa = VM_PAGE_TO_PHYS(m); 1141 vm_offset_t va, off; 1142 1143 if (!hw_direct_map) { 1144 mtx_lock(&moea64_scratchpage_mtx); 1145 1146 moea64_set_scratchpage_pa(mmu, 0, pa); 1147 va = moea64_scratchpage_va[0]; 1148 } else { 1149 va = pa; 1150 } 1151 1152 for (off = 0; off < PAGE_SIZE; off += cacheline_size) 1153 __asm __volatile("dcbz 0,%0" :: "r"(va + off)); 1154 1155 if (!hw_direct_map) 1156 mtx_unlock(&moea64_scratchpage_mtx); 1157} 1158 1159void 1160moea64_zero_page_idle(mmu_t mmu, vm_page_t m) 1161{ 1162 1163 moea64_zero_page(mmu, m); 1164} 1165 1166/* 1167 * Map the given physical page at the specified virtual address in the 1168 * target pmap with the protection requested. If specified the page 1169 * will be wired down. 1170 */ 1171void 1172moea64_enter(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, 1173 vm_prot_t prot, boolean_t wired) 1174{ 1175 1176 LOCK_TABLE_WR(); 1177 PMAP_LOCK(pmap); 1178 moea64_enter_locked(mmu, pmap, va, m, prot, wired); 1179 UNLOCK_TABLE_WR(); 1180 PMAP_UNLOCK(pmap); 1181} 1182 1183/* 1184 * Map the given physical page at the specified virtual address in the 1185 * target pmap with the protection requested. If specified the page 1186 * will be wired down. 1187 * 1188 * The table (write) and pmap must be locked. 1189 */ 1190 1191static void 1192moea64_enter_locked(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_page_t m, 1193 vm_prot_t prot, boolean_t wired) 1194{ 1195 struct pvo_head *pvo_head; 1196 uma_zone_t zone; 1197 vm_page_t pg; 1198 uint64_t pte_lo; 1199 u_int pvo_flags; 1200 int error; 1201 1202 if (!moea64_initialized) { 1203 pvo_head = &moea64_pvo_kunmanaged; 1204 pg = NULL; 1205 zone = moea64_upvo_zone; 1206 pvo_flags = 0; 1207 } else { 1208 pvo_head = vm_page_to_pvoh(m); 1209 pg = m; 1210 zone = moea64_mpvo_zone; 1211 pvo_flags = PVO_MANAGED; 1212 } 1213 1214 PMAP_LOCK_ASSERT(pmap, MA_OWNED); 1215 KASSERT((m->oflags & (VPO_UNMANAGED | VPO_BUSY)) != 0 || 1216 VM_OBJECT_LOCKED(m->object), 1217 ("moea64_enter_locked: page %p is not busy", m)); 1218 1219 /* XXX change the pvo head for fake pages */ 1220 if ((m->oflags & VPO_UNMANAGED) != 0) { 1221 pvo_flags &= ~PVO_MANAGED; 1222 pvo_head = &moea64_pvo_kunmanaged; 1223 zone = moea64_upvo_zone; 1224 } 1225 1226 pte_lo = moea64_calc_wimg(VM_PAGE_TO_PHYS(m), pmap_page_get_memattr(m)); 1227 1228 if (prot & VM_PROT_WRITE) { 1229 pte_lo |= LPTE_BW; 1230 if (pmap_bootstrapped && 1231 (m->oflags & VPO_UNMANAGED) == 0) 1232 vm_page_aflag_set(m, PGA_WRITEABLE); 1233 } else 1234 pte_lo |= LPTE_BR; 1235 1236 if ((prot & VM_PROT_EXECUTE) == 0) 1237 pte_lo |= LPTE_NOEXEC; 1238 1239 if (wired) 1240 pvo_flags |= PVO_WIRED; 1241 1242 error = moea64_pvo_enter(mmu, pmap, zone, pvo_head, va, 1243 VM_PAGE_TO_PHYS(m), pte_lo, pvo_flags); 1244 1245 /* 1246 * Flush the page from the instruction cache if this page is 1247 * mapped executable and cacheable. 1248 */ 1249 if ((pte_lo & (LPTE_I | LPTE_G | LPTE_NOEXEC)) == 0) 1250 moea64_syncicache(mmu, pmap, va, VM_PAGE_TO_PHYS(m), PAGE_SIZE); 1251} 1252 1253static void 1254moea64_syncicache(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_offset_t pa, 1255 vm_size_t sz) 1256{ 1257 1258 /* 1259 * This is much trickier than on older systems because 1260 * we can't sync the icache on physical addresses directly 1261 * without a direct map. Instead we check a couple of cases 1262 * where the memory is already mapped in and, failing that, 1263 * use the same trick we use for page zeroing to create 1264 * a temporary mapping for this physical address. 1265 */ 1266 1267 if (!pmap_bootstrapped) { 1268 /* 1269 * If PMAP is not bootstrapped, we are likely to be 1270 * in real mode. 1271 */ 1272 __syncicache((void *)pa, sz); 1273 } else if (pmap == kernel_pmap) { 1274 __syncicache((void *)va, sz); 1275 } else if (hw_direct_map) { 1276 __syncicache((void *)pa, sz); 1277 } else { 1278 /* Use the scratch page to set up a temp mapping */ 1279 1280 mtx_lock(&moea64_scratchpage_mtx); 1281 1282 moea64_set_scratchpage_pa(mmu, 1, pa & ~ADDR_POFF); 1283 __syncicache((void *)(moea64_scratchpage_va[1] + 1284 (va & ADDR_POFF)), sz); 1285 1286 mtx_unlock(&moea64_scratchpage_mtx); 1287 } 1288} 1289 1290/* 1291 * Maps a sequence of resident pages belonging to the same object. 1292 * The sequence begins with the given page m_start. This page is 1293 * mapped at the given virtual address start. Each subsequent page is 1294 * mapped at a virtual address that is offset from start by the same 1295 * amount as the page is offset from m_start within the object. The 1296 * last page in the sequence is the page with the largest offset from 1297 * m_start that can be mapped at a virtual address less than the given 1298 * virtual address end. Not every virtual page between start and end 1299 * is mapped; only those for which a resident page exists with the 1300 * corresponding offset from m_start are mapped. 1301 */ 1302void 1303moea64_enter_object(mmu_t mmu, pmap_t pm, vm_offset_t start, vm_offset_t end, 1304 vm_page_t m_start, vm_prot_t prot) 1305{ 1306 vm_page_t m; 1307 vm_pindex_t diff, psize; 1308 1309 psize = atop(end - start); 1310 m = m_start; 1311 LOCK_TABLE_WR(); 1312 PMAP_LOCK(pm); 1313 while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) { 1314 moea64_enter_locked(mmu, pm, start + ptoa(diff), m, prot & 1315 (VM_PROT_READ | VM_PROT_EXECUTE), FALSE); 1316 m = TAILQ_NEXT(m, listq); 1317 } 1318 UNLOCK_TABLE_WR(); 1319 PMAP_UNLOCK(pm); 1320} 1321 1322void 1323moea64_enter_quick(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_page_t m, 1324 vm_prot_t prot) 1325{ 1326 1327 LOCK_TABLE_WR(); 1328 PMAP_LOCK(pm); 1329 moea64_enter_locked(mmu, pm, va, m, 1330 prot & (VM_PROT_READ | VM_PROT_EXECUTE), FALSE); 1331 UNLOCK_TABLE_WR(); 1332 PMAP_UNLOCK(pm); 1333} 1334 1335vm_paddr_t 1336moea64_extract(mmu_t mmu, pmap_t pm, vm_offset_t va) 1337{ 1338 struct pvo_entry *pvo; 1339 vm_paddr_t pa; 1340 1341 LOCK_TABLE_RD(); 1342 PMAP_LOCK(pm); 1343 pvo = moea64_pvo_find_va(pm, va); 1344 if (pvo == NULL) 1345 pa = 0; 1346 else 1347 pa = (pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN) | 1348 (va - PVO_VADDR(pvo)); 1349 UNLOCK_TABLE_RD(); 1350 PMAP_UNLOCK(pm); 1351 return (pa); 1352} 1353 1354/* 1355 * Atomically extract and hold the physical page with the given 1356 * pmap and virtual address pair if that mapping permits the given 1357 * protection. 1358 */ 1359vm_page_t 1360moea64_extract_and_hold(mmu_t mmu, pmap_t pmap, vm_offset_t va, vm_prot_t prot) 1361{ 1362 struct pvo_entry *pvo; 1363 vm_page_t m; 1364 vm_paddr_t pa; 1365 1366 m = NULL; 1367 pa = 0; 1368 LOCK_TABLE_RD(); 1369 PMAP_LOCK(pmap); 1370retry: 1371 pvo = moea64_pvo_find_va(pmap, va & ~ADDR_POFF); 1372 if (pvo != NULL && (pvo->pvo_pte.lpte.pte_hi & LPTE_VALID) && 1373 ((pvo->pvo_pte.lpte.pte_lo & LPTE_PP) == LPTE_RW || 1374 (prot & VM_PROT_WRITE) == 0)) { 1375 if (vm_page_pa_tryrelock(pmap, 1376 pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN, &pa)) 1377 goto retry; 1378 m = PHYS_TO_VM_PAGE(pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN); 1379 vm_page_hold(m); 1380 } 1381 PA_UNLOCK_COND(pa); 1382 UNLOCK_TABLE_RD(); 1383 PMAP_UNLOCK(pmap); 1384 return (m); 1385} 1386 1387static mmu_t installed_mmu; 1388 1389static void * 1390moea64_uma_page_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait) 1391{ 1392 /* 1393 * This entire routine is a horrible hack to avoid bothering kmem 1394 * for new KVA addresses. Because this can get called from inside 1395 * kmem allocation routines, calling kmem for a new address here 1396 * can lead to multiply locking non-recursive mutexes. 1397 */ 1398 vm_offset_t va; 1399 1400 vm_page_t m; 1401 int pflags, needed_lock; 1402 1403 *flags = UMA_SLAB_PRIV; 1404 needed_lock = !PMAP_LOCKED(kernel_pmap); 1405 1406 if ((wait & (M_NOWAIT|M_USE_RESERVE)) == M_NOWAIT) 1407 pflags = VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED; 1408 else 1409 pflags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED; 1410 if (wait & M_ZERO) 1411 pflags |= VM_ALLOC_ZERO; 1412 1413 for (;;) { 1414 m = vm_page_alloc(NULL, 0, pflags | VM_ALLOC_NOOBJ); 1415 if (m == NULL) { 1416 if (wait & M_NOWAIT) 1417 return (NULL); 1418 VM_WAIT; 1419 } else 1420 break; 1421 } 1422 1423 va = VM_PAGE_TO_PHYS(m); 1424 1425 LOCK_TABLE_WR(); 1426 if (needed_lock) 1427 PMAP_LOCK(kernel_pmap); 1428 1429 moea64_pvo_enter(installed_mmu, kernel_pmap, moea64_upvo_zone, 1430 &moea64_pvo_kunmanaged, va, VM_PAGE_TO_PHYS(m), LPTE_M, 1431 PVO_WIRED | PVO_BOOTSTRAP); 1432 1433 if (needed_lock) 1434 PMAP_UNLOCK(kernel_pmap); 1435 UNLOCK_TABLE_WR(); 1436 1437 if ((wait & M_ZERO) && (m->flags & PG_ZERO) == 0) 1438 bzero((void *)va, PAGE_SIZE); 1439 1440 return (void *)va; 1441} 1442 1443extern int elf32_nxstack; 1444 1445void 1446moea64_init(mmu_t mmu) 1447{ 1448 1449 CTR0(KTR_PMAP, "moea64_init"); 1450 1451 moea64_upvo_zone = uma_zcreate("UPVO entry", sizeof (struct pvo_entry), 1452 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 1453 UMA_ZONE_VM | UMA_ZONE_NOFREE); 1454 moea64_mpvo_zone = uma_zcreate("MPVO entry", sizeof(struct pvo_entry), 1455 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 1456 UMA_ZONE_VM | UMA_ZONE_NOFREE); 1457 1458 if (!hw_direct_map) { 1459 installed_mmu = mmu; 1460 uma_zone_set_allocf(moea64_upvo_zone,moea64_uma_page_alloc); 1461 uma_zone_set_allocf(moea64_mpvo_zone,moea64_uma_page_alloc); 1462 } 1463 1464#ifdef COMPAT_FREEBSD32 1465 elf32_nxstack = 1; 1466#endif 1467 1468 moea64_initialized = TRUE; 1469} 1470 1471boolean_t 1472moea64_is_referenced(mmu_t mmu, vm_page_t m) 1473{ 1474 1475 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1476 ("moea64_is_referenced: page %p is not managed", m)); 1477 return (moea64_query_bit(mmu, m, PTE_REF)); 1478} 1479 1480boolean_t 1481moea64_is_modified(mmu_t mmu, vm_page_t m) 1482{ 1483 1484 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1485 ("moea64_is_modified: page %p is not managed", m)); 1486 1487 /* 1488 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be 1489 * concurrently set while the object is locked. Thus, if PGA_WRITEABLE 1490 * is clear, no PTEs can have LPTE_CHG set. 1491 */ 1492 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1493 if ((m->oflags & VPO_BUSY) == 0 && 1494 (m->aflags & PGA_WRITEABLE) == 0) 1495 return (FALSE); 1496 return (moea64_query_bit(mmu, m, LPTE_CHG)); 1497} 1498 1499boolean_t 1500moea64_is_prefaultable(mmu_t mmu, pmap_t pmap, vm_offset_t va) 1501{ 1502 struct pvo_entry *pvo; 1503 boolean_t rv; 1504 1505 LOCK_TABLE_RD(); 1506 PMAP_LOCK(pmap); 1507 pvo = moea64_pvo_find_va(pmap, va & ~ADDR_POFF); 1508 rv = pvo == NULL || (pvo->pvo_pte.lpte.pte_hi & LPTE_VALID) == 0; 1509 PMAP_UNLOCK(pmap); 1510 UNLOCK_TABLE_RD(); 1511 return (rv); 1512} 1513 1514void 1515moea64_clear_reference(mmu_t mmu, vm_page_t m) 1516{ 1517 1518 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1519 ("moea64_clear_reference: page %p is not managed", m)); 1520 moea64_clear_bit(mmu, m, LPTE_REF); 1521} 1522 1523void 1524moea64_clear_modify(mmu_t mmu, vm_page_t m) 1525{ 1526 1527 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1528 ("moea64_clear_modify: page %p is not managed", m)); 1529 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1530 KASSERT((m->oflags & VPO_BUSY) == 0, 1531 ("moea64_clear_modify: page %p is busy", m)); 1532 1533 /* 1534 * If the page is not PGA_WRITEABLE, then no PTEs can have LPTE_CHG 1535 * set. If the object containing the page is locked and the page is 1536 * not VPO_BUSY, then PGA_WRITEABLE cannot be concurrently set. 1537 */ 1538 if ((m->aflags & PGA_WRITEABLE) == 0) 1539 return; 1540 moea64_clear_bit(mmu, m, LPTE_CHG); 1541} 1542 1543/* 1544 * Clear the write and modified bits in each of the given page's mappings. 1545 */ 1546void 1547moea64_remove_write(mmu_t mmu, vm_page_t m) 1548{ 1549 struct pvo_entry *pvo; 1550 uintptr_t pt; 1551 pmap_t pmap; 1552 uint64_t lo = 0; 1553 1554 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1555 ("moea64_remove_write: page %p is not managed", m)); 1556 1557 /* 1558 * If the page is not VPO_BUSY, then PGA_WRITEABLE cannot be set by 1559 * another thread while the object is locked. Thus, if PGA_WRITEABLE 1560 * is clear, no page table entries need updating. 1561 */ 1562 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 1563 if ((m->oflags & VPO_BUSY) == 0 && 1564 (m->aflags & PGA_WRITEABLE) == 0) 1565 return; 1566 powerpc_sync(); 1567 LOCK_TABLE_RD(); 1568 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 1569 pmap = pvo->pvo_pmap; 1570 PMAP_LOCK(pmap); 1571 if ((pvo->pvo_pte.lpte.pte_lo & LPTE_PP) != LPTE_BR) { 1572 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 1573 pvo->pvo_pte.lpte.pte_lo &= ~LPTE_PP; 1574 pvo->pvo_pte.lpte.pte_lo |= LPTE_BR; 1575 if (pt != -1) { 1576 MOEA64_PTE_SYNCH(mmu, pt, &pvo->pvo_pte.lpte); 1577 lo |= pvo->pvo_pte.lpte.pte_lo; 1578 pvo->pvo_pte.lpte.pte_lo &= ~LPTE_CHG; 1579 MOEA64_PTE_CHANGE(mmu, pt, 1580 &pvo->pvo_pte.lpte, pvo->pvo_vpn); 1581 if (pvo->pvo_pmap == kernel_pmap) 1582 isync(); 1583 } 1584 } 1585 PMAP_UNLOCK(pmap); 1586 } 1587 UNLOCK_TABLE_RD(); 1588 if ((lo & LPTE_CHG) != 0) 1589 vm_page_dirty(m); 1590 vm_page_aflag_clear(m, PGA_WRITEABLE); 1591} 1592 1593/* 1594 * moea64_ts_referenced: 1595 * 1596 * Return a count of reference bits for a page, clearing those bits. 1597 * It is not necessary for every reference bit to be cleared, but it 1598 * is necessary that 0 only be returned when there are truly no 1599 * reference bits set. 1600 * 1601 * XXX: The exact number of bits to check and clear is a matter that 1602 * should be tested and standardized at some point in the future for 1603 * optimal aging of shared pages. 1604 */ 1605boolean_t 1606moea64_ts_referenced(mmu_t mmu, vm_page_t m) 1607{ 1608 1609 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1610 ("moea64_ts_referenced: page %p is not managed", m)); 1611 return (moea64_clear_bit(mmu, m, LPTE_REF)); 1612} 1613 1614/* 1615 * Modify the WIMG settings of all mappings for a page. 1616 */ 1617void 1618moea64_page_set_memattr(mmu_t mmu, vm_page_t m, vm_memattr_t ma) 1619{ 1620 struct pvo_entry *pvo; 1621 struct pvo_head *pvo_head; 1622 uintptr_t pt; 1623 pmap_t pmap; 1624 uint64_t lo; 1625 1626 if ((m->oflags & VPO_UNMANAGED) != 0) { 1627 m->md.mdpg_cache_attrs = ma; 1628 return; 1629 } 1630 1631 pvo_head = vm_page_to_pvoh(m); 1632 lo = moea64_calc_wimg(VM_PAGE_TO_PHYS(m), ma); 1633 LOCK_TABLE_RD(); 1634 LIST_FOREACH(pvo, pvo_head, pvo_vlink) { 1635 pmap = pvo->pvo_pmap; 1636 PMAP_LOCK(pmap); 1637 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 1638 pvo->pvo_pte.lpte.pte_lo &= ~LPTE_WIMG; 1639 pvo->pvo_pte.lpte.pte_lo |= lo; 1640 if (pt != -1) { 1641 MOEA64_PTE_CHANGE(mmu, pt, &pvo->pvo_pte.lpte, 1642 pvo->pvo_vpn); 1643 if (pvo->pvo_pmap == kernel_pmap) 1644 isync(); 1645 } 1646 PMAP_UNLOCK(pmap); 1647 } 1648 UNLOCK_TABLE_RD(); 1649 m->md.mdpg_cache_attrs = ma; 1650} 1651 1652/* 1653 * Map a wired page into kernel virtual address space. 1654 */ 1655void 1656moea64_kenter_attr(mmu_t mmu, vm_offset_t va, vm_offset_t pa, vm_memattr_t ma) 1657{ 1658 uint64_t pte_lo; 1659 int error; 1660 1661 pte_lo = moea64_calc_wimg(pa, ma); 1662 1663 LOCK_TABLE_WR(); 1664 PMAP_LOCK(kernel_pmap); 1665 error = moea64_pvo_enter(mmu, kernel_pmap, moea64_upvo_zone, 1666 &moea64_pvo_kunmanaged, va, pa, pte_lo, PVO_WIRED); 1667 PMAP_UNLOCK(kernel_pmap); 1668 UNLOCK_TABLE_WR(); 1669 1670 if (error != 0 && error != ENOENT) 1671 panic("moea64_kenter: failed to enter va %#zx pa %#zx: %d", va, 1672 pa, error); 1673 1674 /* 1675 * Flush the memory from the instruction cache. 1676 */ 1677 if ((pte_lo & (LPTE_I | LPTE_G)) == 0) 1678 __syncicache((void *)va, PAGE_SIZE); 1679} 1680 1681void 1682moea64_kenter(mmu_t mmu, vm_offset_t va, vm_offset_t pa) 1683{ 1684 1685 moea64_kenter_attr(mmu, va, pa, VM_MEMATTR_DEFAULT); 1686} 1687 1688/* 1689 * Extract the physical page address associated with the given kernel virtual 1690 * address. 1691 */ 1692vm_offset_t 1693moea64_kextract(mmu_t mmu, vm_offset_t va) 1694{ 1695 struct pvo_entry *pvo; 1696 vm_paddr_t pa; 1697 1698 /* 1699 * Shortcut the direct-mapped case when applicable. We never put 1700 * anything but 1:1 mappings below VM_MIN_KERNEL_ADDRESS. 1701 */ 1702 if (va < VM_MIN_KERNEL_ADDRESS) 1703 return (va); 1704 1705 LOCK_TABLE_RD(); 1706 PMAP_LOCK(kernel_pmap); 1707 pvo = moea64_pvo_find_va(kernel_pmap, va); 1708 KASSERT(pvo != NULL, ("moea64_kextract: no addr found for %#" PRIxPTR, 1709 va)); 1710 pa = (pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN) | (va - PVO_VADDR(pvo)); 1711 UNLOCK_TABLE_RD(); 1712 PMAP_UNLOCK(kernel_pmap); 1713 return (pa); 1714} 1715 1716/* 1717 * Remove a wired page from kernel virtual address space. 1718 */ 1719void 1720moea64_kremove(mmu_t mmu, vm_offset_t va) 1721{ 1722 moea64_remove(mmu, kernel_pmap, va, va + PAGE_SIZE); 1723} 1724 1725/* 1726 * Map a range of physical addresses into kernel virtual address space. 1727 * 1728 * The value passed in *virt is a suggested virtual address for the mapping. 1729 * Architectures which can support a direct-mapped physical to virtual region 1730 * can return the appropriate address within that region, leaving '*virt' 1731 * unchanged. We cannot and therefore do not; *virt is updated with the 1732 * first usable address after the mapped region. 1733 */ 1734vm_offset_t 1735moea64_map(mmu_t mmu, vm_offset_t *virt, vm_offset_t pa_start, 1736 vm_offset_t pa_end, int prot) 1737{ 1738 vm_offset_t sva, va; 1739 1740 sva = *virt; 1741 va = sva; 1742 for (; pa_start < pa_end; pa_start += PAGE_SIZE, va += PAGE_SIZE) 1743 moea64_kenter(mmu, va, pa_start); 1744 *virt = va; 1745 1746 return (sva); 1747} 1748 1749/* 1750 * Returns true if the pmap's pv is one of the first 1751 * 16 pvs linked to from this page. This count may 1752 * be changed upwards or downwards in the future; it 1753 * is only necessary that true be returned for a small 1754 * subset of pmaps for proper page aging. 1755 */ 1756boolean_t 1757moea64_page_exists_quick(mmu_t mmu, pmap_t pmap, vm_page_t m) 1758{ 1759 int loops; 1760 struct pvo_entry *pvo; 1761 boolean_t rv; 1762 1763 KASSERT((m->oflags & VPO_UNMANAGED) == 0, 1764 ("moea64_page_exists_quick: page %p is not managed", m)); 1765 loops = 0; 1766 rv = FALSE; 1767 LOCK_TABLE_RD(); 1768 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 1769 if (pvo->pvo_pmap == pmap) { 1770 rv = TRUE; 1771 break; 1772 } 1773 if (++loops >= 16) 1774 break; 1775 } 1776 UNLOCK_TABLE_RD(); 1777 return (rv); 1778} 1779 1780/* 1781 * Return the number of managed mappings to the given physical page 1782 * that are wired. 1783 */ 1784int 1785moea64_page_wired_mappings(mmu_t mmu, vm_page_t m) 1786{ 1787 struct pvo_entry *pvo; 1788 int count; 1789 1790 count = 0; 1791 if ((m->oflags & VPO_UNMANAGED) != 0) 1792 return (count); 1793 LOCK_TABLE_RD(); 1794 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) 1795 if ((pvo->pvo_vaddr & PVO_WIRED) != 0) 1796 count++; 1797 UNLOCK_TABLE_RD(); 1798 return (count); 1799} 1800 1801static uintptr_t moea64_vsidcontext; 1802 1803uintptr_t 1804moea64_get_unique_vsid(void) { 1805 u_int entropy; 1806 register_t hash; 1807 uint32_t mask; 1808 int i; 1809 1810 entropy = 0; 1811 __asm __volatile("mftb %0" : "=r"(entropy)); 1812 1813 mtx_lock(&moea64_slb_mutex); 1814 for (i = 0; i < NVSIDS; i += VSID_NBPW) { 1815 u_int n; 1816 1817 /* 1818 * Create a new value by mutiplying by a prime and adding in 1819 * entropy from the timebase register. This is to make the 1820 * VSID more random so that the PT hash function collides 1821 * less often. (Note that the prime casues gcc to do shifts 1822 * instead of a multiply.) 1823 */ 1824 moea64_vsidcontext = (moea64_vsidcontext * 0x1105) + entropy; 1825 hash = moea64_vsidcontext & (NVSIDS - 1); 1826 if (hash == 0) /* 0 is special, avoid it */ 1827 continue; 1828 n = hash >> 5; 1829 mask = 1 << (hash & (VSID_NBPW - 1)); 1830 hash = (moea64_vsidcontext & VSID_HASHMASK); 1831 if (moea64_vsid_bitmap[n] & mask) { /* collision? */ 1832 /* anything free in this bucket? */ 1833 if (moea64_vsid_bitmap[n] == 0xffffffff) { 1834 entropy = (moea64_vsidcontext >> 20); 1835 continue; 1836 } 1837 i = ffs(~moea64_vsid_bitmap[n]) - 1; 1838 mask = 1 << i; 1839 hash &= VSID_HASHMASK & ~(VSID_NBPW - 1); 1840 hash |= i; 1841 } 1842 KASSERT(!(moea64_vsid_bitmap[n] & mask), 1843 ("Allocating in-use VSID %#zx\n", hash)); 1844 moea64_vsid_bitmap[n] |= mask; 1845 mtx_unlock(&moea64_slb_mutex); 1846 return (hash); 1847 } 1848 1849 mtx_unlock(&moea64_slb_mutex); 1850 panic("%s: out of segments",__func__); 1851} 1852 1853#ifdef __powerpc64__ 1854void 1855moea64_pinit(mmu_t mmu, pmap_t pmap) 1856{ 1857 PMAP_LOCK_INIT(pmap); 1858 LIST_INIT(&pmap->pmap_pvo); 1859 1860 pmap->pm_slb_tree_root = slb_alloc_tree(); 1861 pmap->pm_slb = slb_alloc_user_cache(); 1862 pmap->pm_slb_len = 0; 1863} 1864#else 1865void 1866moea64_pinit(mmu_t mmu, pmap_t pmap) 1867{ 1868 int i; 1869 uint32_t hash; 1870 1871 PMAP_LOCK_INIT(pmap); 1872 LIST_INIT(&pmap->pmap_pvo); 1873 1874 if (pmap_bootstrapped) 1875 pmap->pmap_phys = (pmap_t)moea64_kextract(mmu, 1876 (vm_offset_t)pmap); 1877 else 1878 pmap->pmap_phys = pmap; 1879 1880 /* 1881 * Allocate some segment registers for this pmap. 1882 */ 1883 hash = moea64_get_unique_vsid(); 1884 1885 for (i = 0; i < 16; i++) 1886 pmap->pm_sr[i] = VSID_MAKE(i, hash); 1887 1888 KASSERT(pmap->pm_sr[0] != 0, ("moea64_pinit: pm_sr[0] = 0")); 1889} 1890#endif 1891 1892/* 1893 * Initialize the pmap associated with process 0. 1894 */ 1895void 1896moea64_pinit0(mmu_t mmu, pmap_t pm) 1897{ 1898 moea64_pinit(mmu, pm); 1899 bzero(&pm->pm_stats, sizeof(pm->pm_stats)); 1900} 1901 1902/* 1903 * Set the physical protection on the specified range of this map as requested. 1904 */ 1905static void 1906moea64_pvo_protect(mmu_t mmu, pmap_t pm, struct pvo_entry *pvo, vm_prot_t prot) 1907{ 1908 uintptr_t pt; 1909 uint64_t oldlo; 1910 1911 PMAP_LOCK_ASSERT(pm, MA_OWNED); 1912 1913 /* 1914 * Grab the PTE pointer before we diddle with the cached PTE 1915 * copy. 1916 */ 1917 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 1918 1919 /* 1920 * Change the protection of the page. 1921 */ 1922 oldlo = pvo->pvo_pte.lpte.pte_lo; 1923 pvo->pvo_pte.lpte.pte_lo &= ~LPTE_PP; 1924 pvo->pvo_pte.lpte.pte_lo &= ~LPTE_NOEXEC; 1925 if ((prot & VM_PROT_EXECUTE) == 0) 1926 pvo->pvo_pte.lpte.pte_lo |= LPTE_NOEXEC; 1927 if (prot & VM_PROT_WRITE) 1928 pvo->pvo_pte.lpte.pte_lo |= LPTE_BW; 1929 else 1930 pvo->pvo_pte.lpte.pte_lo |= LPTE_BR; 1931 1932 /* 1933 * If the PVO is in the page table, update that pte as well. 1934 */ 1935 if (pt != -1) { 1936 MOEA64_PTE_CHANGE(mmu, pt, &pvo->pvo_pte.lpte, 1937 pvo->pvo_vpn); 1938 if ((pvo->pvo_pte.lpte.pte_lo & 1939 (LPTE_I | LPTE_G | LPTE_NOEXEC)) == 0) { 1940 moea64_syncicache(mmu, pm, PVO_VADDR(pvo), 1941 pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN, 1942 PAGE_SIZE); 1943 } 1944 } 1945 1946 /* 1947 * Update vm about the REF/CHG bits if the page is managed and we have 1948 * removed write access. 1949 */ 1950 if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED && 1951 (oldlo & LPTE_PP) != LPTE_BR && !(prot && VM_PROT_WRITE)) { 1952 struct vm_page *pg; 1953 1954 pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN); 1955 if (pg != NULL) { 1956 if (pvo->pvo_pte.lpte.pte_lo & LPTE_CHG) 1957 vm_page_dirty(pg); 1958 if (pvo->pvo_pte.lpte.pte_lo & LPTE_REF) 1959 vm_page_aflag_set(pg, PGA_REFERENCED); 1960 } 1961 } 1962} 1963 1964void 1965moea64_protect(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva, 1966 vm_prot_t prot) 1967{ 1968 struct pvo_entry *pvo, *tpvo; 1969 1970 CTR4(KTR_PMAP, "moea64_protect: pm=%p sva=%#x eva=%#x prot=%#x", pm, 1971 sva, eva, prot); 1972 1973 KASSERT(pm == &curproc->p_vmspace->vm_pmap || pm == kernel_pmap, 1974 ("moea64_protect: non current pmap")); 1975 1976 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1977 moea64_remove(mmu, pm, sva, eva); 1978 return; 1979 } 1980 1981 LOCK_TABLE_RD(); 1982 PMAP_LOCK(pm); 1983 if ((eva - sva)/PAGE_SIZE < pm->pm_stats.resident_count) { 1984 for (; sva < eva; sva += PAGE_SIZE) { 1985 pvo = moea64_pvo_find_va(pm, sva); 1986 if (pvo != NULL) 1987 moea64_pvo_protect(mmu, pm, pvo, prot); 1988 } 1989 } else { 1990 LIST_FOREACH_SAFE(pvo, &pm->pmap_pvo, pvo_plink, tpvo) { 1991 if (PVO_VADDR(pvo) < sva || PVO_VADDR(pvo) >= eva) 1992 continue; 1993 moea64_pvo_protect(mmu, pm, pvo, prot); 1994 } 1995 } 1996 UNLOCK_TABLE_RD(); 1997 PMAP_UNLOCK(pm); 1998} 1999 2000/* 2001 * Map a list of wired pages into kernel virtual address space. This is 2002 * intended for temporary mappings which do not need page modification or 2003 * references recorded. Existing mappings in the region are overwritten. 2004 */ 2005void 2006moea64_qenter(mmu_t mmu, vm_offset_t va, vm_page_t *m, int count) 2007{ 2008 while (count-- > 0) { 2009 moea64_kenter(mmu, va, VM_PAGE_TO_PHYS(*m)); 2010 va += PAGE_SIZE; 2011 m++; 2012 } 2013} 2014 2015/* 2016 * Remove page mappings from kernel virtual address space. Intended for 2017 * temporary mappings entered by moea64_qenter. 2018 */ 2019void 2020moea64_qremove(mmu_t mmu, vm_offset_t va, int count) 2021{ 2022 while (count-- > 0) { 2023 moea64_kremove(mmu, va); 2024 va += PAGE_SIZE; 2025 } 2026} 2027 2028void 2029moea64_release_vsid(uint64_t vsid) 2030{ 2031 int idx, mask; 2032 2033 mtx_lock(&moea64_slb_mutex); 2034 idx = vsid & (NVSIDS-1); 2035 mask = 1 << (idx % VSID_NBPW); 2036 idx /= VSID_NBPW; 2037 KASSERT(moea64_vsid_bitmap[idx] & mask, 2038 ("Freeing unallocated VSID %#jx", vsid)); 2039 moea64_vsid_bitmap[idx] &= ~mask; 2040 mtx_unlock(&moea64_slb_mutex); 2041} 2042 2043 2044void 2045moea64_release(mmu_t mmu, pmap_t pmap) 2046{ 2047 2048 /* 2049 * Free segment registers' VSIDs 2050 */ 2051 #ifdef __powerpc64__ 2052 slb_free_tree(pmap); 2053 slb_free_user_cache(pmap->pm_slb); 2054 #else 2055 KASSERT(pmap->pm_sr[0] != 0, ("moea64_release: pm_sr[0] = 0")); 2056 2057 moea64_release_vsid(VSID_TO_HASH(pmap->pm_sr[0])); 2058 #endif 2059 2060 PMAP_LOCK_DESTROY(pmap); 2061} 2062 2063/* 2064 * Remove all pages mapped by the specified pmap 2065 */ 2066void 2067moea64_remove_pages(mmu_t mmu, pmap_t pm) 2068{ 2069 struct pvo_entry *pvo, *tpvo; 2070 2071 LOCK_TABLE_WR(); 2072 PMAP_LOCK(pm); 2073 LIST_FOREACH_SAFE(pvo, &pm->pmap_pvo, pvo_plink, tpvo) { 2074 if (!(pvo->pvo_vaddr & PVO_WIRED)) 2075 moea64_pvo_remove(mmu, pvo); 2076 } 2077 UNLOCK_TABLE_WR(); 2078 PMAP_UNLOCK(pm); 2079} 2080 2081/* 2082 * Remove the given range of addresses from the specified map. 2083 */ 2084void 2085moea64_remove(mmu_t mmu, pmap_t pm, vm_offset_t sva, vm_offset_t eva) 2086{ 2087 struct pvo_entry *pvo, *tpvo; 2088 2089 /* 2090 * Perform an unsynchronized read. This is, however, safe. 2091 */ 2092 if (pm->pm_stats.resident_count == 0) 2093 return; 2094 2095 LOCK_TABLE_WR(); 2096 PMAP_LOCK(pm); 2097 if ((eva - sva)/PAGE_SIZE < pm->pm_stats.resident_count) { 2098 for (; sva < eva; sva += PAGE_SIZE) { 2099 pvo = moea64_pvo_find_va(pm, sva); 2100 if (pvo != NULL) 2101 moea64_pvo_remove(mmu, pvo); 2102 } 2103 } else { 2104 LIST_FOREACH_SAFE(pvo, &pm->pmap_pvo, pvo_plink, tpvo) { 2105 if (PVO_VADDR(pvo) < sva || PVO_VADDR(pvo) >= eva) 2106 continue; 2107 moea64_pvo_remove(mmu, pvo); 2108 } 2109 } 2110 UNLOCK_TABLE_WR(); 2111 PMAP_UNLOCK(pm); 2112} 2113 2114/* 2115 * Remove physical page from all pmaps in which it resides. moea64_pvo_remove() 2116 * will reflect changes in pte's back to the vm_page. 2117 */ 2118void 2119moea64_remove_all(mmu_t mmu, vm_page_t m) 2120{ 2121 struct pvo_head *pvo_head; 2122 struct pvo_entry *pvo, *next_pvo; 2123 pmap_t pmap; 2124 2125 pvo_head = vm_page_to_pvoh(m); 2126 LOCK_TABLE_WR(); 2127 for (pvo = LIST_FIRST(pvo_head); pvo != NULL; pvo = next_pvo) { 2128 next_pvo = LIST_NEXT(pvo, pvo_vlink); 2129 2130 pmap = pvo->pvo_pmap; 2131 PMAP_LOCK(pmap); 2132 moea64_pvo_remove(mmu, pvo); 2133 PMAP_UNLOCK(pmap); 2134 } 2135 UNLOCK_TABLE_WR(); 2136 if ((m->aflags & PGA_WRITEABLE) && moea64_is_modified(mmu, m)) 2137 vm_page_dirty(m); 2138 vm_page_aflag_clear(m, PGA_WRITEABLE); 2139} 2140 2141/* 2142 * Allocate a physical page of memory directly from the phys_avail map. 2143 * Can only be called from moea64_bootstrap before avail start and end are 2144 * calculated. 2145 */ 2146vm_offset_t 2147moea64_bootstrap_alloc(vm_size_t size, u_int align) 2148{ 2149 vm_offset_t s, e; 2150 int i, j; 2151 2152 size = round_page(size); 2153 for (i = 0; phys_avail[i + 1] != 0; i += 2) { 2154 if (align != 0) 2155 s = (phys_avail[i] + align - 1) & ~(align - 1); 2156 else 2157 s = phys_avail[i]; 2158 e = s + size; 2159 2160 if (s < phys_avail[i] || e > phys_avail[i + 1]) 2161 continue; 2162 2163 if (s + size > platform_real_maxaddr()) 2164 continue; 2165 2166 if (s == phys_avail[i]) { 2167 phys_avail[i] += size; 2168 } else if (e == phys_avail[i + 1]) { 2169 phys_avail[i + 1] -= size; 2170 } else { 2171 for (j = phys_avail_count * 2; j > i; j -= 2) { 2172 phys_avail[j] = phys_avail[j - 2]; 2173 phys_avail[j + 1] = phys_avail[j - 1]; 2174 } 2175 2176 phys_avail[i + 3] = phys_avail[i + 1]; 2177 phys_avail[i + 1] = s; 2178 phys_avail[i + 2] = e; 2179 phys_avail_count++; 2180 } 2181 2182 return (s); 2183 } 2184 panic("moea64_bootstrap_alloc: could not allocate memory"); 2185} 2186 2187static int 2188moea64_pvo_enter(mmu_t mmu, pmap_t pm, uma_zone_t zone, 2189 struct pvo_head *pvo_head, vm_offset_t va, vm_offset_t pa, 2190 uint64_t pte_lo, int flags) 2191{ 2192 struct pvo_entry *pvo; 2193 uint64_t vsid; 2194 int first; 2195 u_int ptegidx; 2196 int i; 2197 int bootstrap; 2198 2199 /* 2200 * One nasty thing that can happen here is that the UMA calls to 2201 * allocate new PVOs need to map more memory, which calls pvo_enter(), 2202 * which calls UMA... 2203 * 2204 * We break the loop by detecting recursion and allocating out of 2205 * the bootstrap pool. 2206 */ 2207 2208 first = 0; 2209 bootstrap = (flags & PVO_BOOTSTRAP); 2210 2211 if (!moea64_initialized) 2212 bootstrap = 1; 2213 2214 PMAP_LOCK_ASSERT(pm, MA_OWNED); 2215 rw_assert(&moea64_table_lock, RA_WLOCKED); 2216 2217 /* 2218 * Compute the PTE Group index. 2219 */ 2220 va &= ~ADDR_POFF; 2221 vsid = va_to_vsid(pm, va); 2222 ptegidx = va_to_pteg(vsid, va, flags & PVO_LARGE); 2223 2224 /* 2225 * Remove any existing mapping for this page. Reuse the pvo entry if 2226 * there is a mapping. 2227 */ 2228 moea64_pvo_enter_calls++; 2229 2230 LIST_FOREACH(pvo, &moea64_pvo_table[ptegidx], pvo_olink) { 2231 if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) { 2232 if ((pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN) == pa && 2233 (pvo->pvo_pte.lpte.pte_lo & (LPTE_NOEXEC | LPTE_PP)) 2234 == (pte_lo & (LPTE_NOEXEC | LPTE_PP))) { 2235 if (!(pvo->pvo_pte.lpte.pte_hi & LPTE_VALID)) { 2236 /* Re-insert if spilled */ 2237 i = MOEA64_PTE_INSERT(mmu, ptegidx, 2238 &pvo->pvo_pte.lpte); 2239 if (i >= 0) 2240 PVO_PTEGIDX_SET(pvo, i); 2241 moea64_pte_overflow--; 2242 } 2243 return (0); 2244 } 2245 moea64_pvo_remove(mmu, pvo); 2246 break; 2247 } 2248 } 2249 2250 /* 2251 * If we aren't overwriting a mapping, try to allocate. 2252 */ 2253 if (bootstrap) { 2254 if (moea64_bpvo_pool_index >= BPVO_POOL_SIZE) { 2255 panic("moea64_enter: bpvo pool exhausted, %d, %d, %zd", 2256 moea64_bpvo_pool_index, BPVO_POOL_SIZE, 2257 BPVO_POOL_SIZE * sizeof(struct pvo_entry)); 2258 } 2259 pvo = &moea64_bpvo_pool[moea64_bpvo_pool_index]; 2260 moea64_bpvo_pool_index++; 2261 bootstrap = 1; 2262 } else { 2263 /* 2264 * Note: drop the table lock around the UMA allocation in 2265 * case the UMA allocator needs to manipulate the page 2266 * table. The mapping we are working with is already 2267 * protected by the PMAP lock. 2268 */ 2269 pvo = uma_zalloc(zone, M_NOWAIT); 2270 } 2271 2272 if (pvo == NULL) 2273 return (ENOMEM); 2274 2275 moea64_pvo_entries++; 2276 pvo->pvo_vaddr = va; 2277 pvo->pvo_vpn = (uint64_t)((va & ADDR_PIDX) >> ADDR_PIDX_SHFT) 2278 | (vsid << 16); 2279 pvo->pvo_pmap = pm; 2280 LIST_INSERT_HEAD(&moea64_pvo_table[ptegidx], pvo, pvo_olink); 2281 pvo->pvo_vaddr &= ~ADDR_POFF; 2282 2283 if (flags & PVO_WIRED) 2284 pvo->pvo_vaddr |= PVO_WIRED; 2285 if (pvo_head != &moea64_pvo_kunmanaged) 2286 pvo->pvo_vaddr |= PVO_MANAGED; 2287 if (bootstrap) 2288 pvo->pvo_vaddr |= PVO_BOOTSTRAP; 2289 if (flags & PVO_LARGE) 2290 pvo->pvo_vaddr |= PVO_LARGE; 2291 2292 moea64_pte_create(&pvo->pvo_pte.lpte, vsid, va, 2293 (uint64_t)(pa) | pte_lo, flags); 2294 2295 /* 2296 * Add to pmap list 2297 */ 2298 LIST_INSERT_HEAD(&pm->pmap_pvo, pvo, pvo_plink); 2299 2300 /* 2301 * Remember if the list was empty and therefore will be the first 2302 * item. 2303 */ 2304 if (LIST_FIRST(pvo_head) == NULL) 2305 first = 1; 2306 LIST_INSERT_HEAD(pvo_head, pvo, pvo_vlink); 2307 2308 if (pvo->pvo_vaddr & PVO_WIRED) { 2309 pvo->pvo_pte.lpte.pte_hi |= LPTE_WIRED; 2310 pm->pm_stats.wired_count++; 2311 } 2312 pm->pm_stats.resident_count++; 2313 2314 /* 2315 * We hope this succeeds but it isn't required. 2316 */ 2317 i = MOEA64_PTE_INSERT(mmu, ptegidx, &pvo->pvo_pte.lpte); 2318 if (i >= 0) { 2319 PVO_PTEGIDX_SET(pvo, i); 2320 } else { 2321 panic("moea64_pvo_enter: overflow"); 2322 moea64_pte_overflow++; 2323 } 2324 2325 if (pm == kernel_pmap) 2326 isync(); 2327 2328#ifdef __powerpc64__ 2329 /* 2330 * Make sure all our bootstrap mappings are in the SLB as soon 2331 * as virtual memory is switched on. 2332 */ 2333 if (!pmap_bootstrapped) 2334 moea64_bootstrap_slb_prefault(va, flags & PVO_LARGE); 2335#endif 2336 2337 return (first ? ENOENT : 0); 2338} 2339 2340static void 2341moea64_pvo_remove(mmu_t mmu, struct pvo_entry *pvo) 2342{ 2343 uintptr_t pt; 2344 2345 PMAP_LOCK_ASSERT(pvo->pvo_pmap, MA_OWNED); 2346 rw_assert(&moea64_table_lock, RA_WLOCKED); 2347 2348 /* 2349 * If there is an active pte entry, we need to deactivate it (and 2350 * save the ref & cfg bits). 2351 */ 2352 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 2353 if (pt != -1) { 2354 MOEA64_PTE_UNSET(mmu, pt, &pvo->pvo_pte.lpte, pvo->pvo_vpn); 2355 PVO_PTEGIDX_CLR(pvo); 2356 } else { 2357 moea64_pte_overflow--; 2358 } 2359 2360 /* 2361 * Update our statistics. 2362 */ 2363 pvo->pvo_pmap->pm_stats.resident_count--; 2364 if (pvo->pvo_vaddr & PVO_WIRED) 2365 pvo->pvo_pmap->pm_stats.wired_count--; 2366 2367 /* 2368 * Remove this PVO from the PV and pmap lists. 2369 */ 2370 LIST_REMOVE(pvo, pvo_vlink); 2371 LIST_REMOVE(pvo, pvo_plink); 2372 2373 /* 2374 * Remove this from the overflow list and return it to the pool 2375 * if we aren't going to reuse it. 2376 */ 2377 LIST_REMOVE(pvo, pvo_olink); 2378 2379 /* 2380 * Update vm about the REF/CHG bits if the page is managed. 2381 */ 2382 if ((pvo->pvo_vaddr & PVO_MANAGED) == PVO_MANAGED && 2383 (pvo->pvo_pte.lpte.pte_lo & LPTE_PP) != LPTE_BR) { 2384 struct vm_page *pg; 2385 2386 pg = PHYS_TO_VM_PAGE(pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN); 2387 if (pg != NULL) { 2388 if (pvo->pvo_pte.lpte.pte_lo & LPTE_CHG) 2389 vm_page_dirty(pg); 2390 if (pvo->pvo_pte.lpte.pte_lo & LPTE_REF) 2391 vm_page_aflag_set(pg, PGA_REFERENCED); 2392 if (LIST_EMPTY(vm_page_to_pvoh(pg))) 2393 vm_page_aflag_clear(pg, PGA_WRITEABLE); 2394 } 2395 } 2396 2397 moea64_pvo_entries--; 2398 moea64_pvo_remove_calls++; 2399 2400 if (!(pvo->pvo_vaddr & PVO_BOOTSTRAP)) 2401 uma_zfree((pvo->pvo_vaddr & PVO_MANAGED) ? moea64_mpvo_zone : 2402 moea64_upvo_zone, pvo); 2403} 2404 2405static struct pvo_entry * 2406moea64_pvo_find_va(pmap_t pm, vm_offset_t va) 2407{ 2408 struct pvo_entry *pvo; 2409 int ptegidx; 2410 uint64_t vsid; 2411 #ifdef __powerpc64__ 2412 uint64_t slbv; 2413 2414 if (pm == kernel_pmap) { 2415 slbv = kernel_va_to_slbv(va); 2416 } else { 2417 struct slb *slb; 2418 slb = user_va_to_slb_entry(pm, va); 2419 /* The page is not mapped if the segment isn't */ 2420 if (slb == NULL) 2421 return NULL; 2422 slbv = slb->slbv; 2423 } 2424 2425 vsid = (slbv & SLBV_VSID_MASK) >> SLBV_VSID_SHIFT; 2426 if (slbv & SLBV_L) 2427 va &= ~moea64_large_page_mask; 2428 else 2429 va &= ~ADDR_POFF; 2430 ptegidx = va_to_pteg(vsid, va, slbv & SLBV_L); 2431 #else 2432 va &= ~ADDR_POFF; 2433 vsid = va_to_vsid(pm, va); 2434 ptegidx = va_to_pteg(vsid, va, 0); 2435 #endif 2436 2437 LIST_FOREACH(pvo, &moea64_pvo_table[ptegidx], pvo_olink) { 2438 if (pvo->pvo_pmap == pm && PVO_VADDR(pvo) == va) 2439 break; 2440 } 2441 2442 return (pvo); 2443} 2444 2445static boolean_t 2446moea64_query_bit(mmu_t mmu, vm_page_t m, u_int64_t ptebit) 2447{ 2448 struct pvo_entry *pvo; 2449 uintptr_t pt; 2450 2451 LOCK_TABLE_RD(); 2452 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 2453 /* 2454 * See if we saved the bit off. If so, return success. 2455 */ 2456 if (pvo->pvo_pte.lpte.pte_lo & ptebit) { 2457 UNLOCK_TABLE_RD(); 2458 return (TRUE); 2459 } 2460 } 2461 2462 /* 2463 * No luck, now go through the hard part of looking at the PTEs 2464 * themselves. Sync so that any pending REF/CHG bits are flushed to 2465 * the PTEs. 2466 */ 2467 powerpc_sync(); 2468 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 2469 2470 /* 2471 * See if this pvo has a valid PTE. if so, fetch the 2472 * REF/CHG bits from the valid PTE. If the appropriate 2473 * ptebit is set, return success. 2474 */ 2475 PMAP_LOCK(pvo->pvo_pmap); 2476 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 2477 if (pt != -1) { 2478 MOEA64_PTE_SYNCH(mmu, pt, &pvo->pvo_pte.lpte); 2479 if (pvo->pvo_pte.lpte.pte_lo & ptebit) { 2480 PMAP_UNLOCK(pvo->pvo_pmap); 2481 UNLOCK_TABLE_RD(); 2482 return (TRUE); 2483 } 2484 } 2485 PMAP_UNLOCK(pvo->pvo_pmap); 2486 } 2487 2488 UNLOCK_TABLE_RD(); 2489 return (FALSE); 2490} 2491 2492static u_int 2493moea64_clear_bit(mmu_t mmu, vm_page_t m, u_int64_t ptebit) 2494{ 2495 u_int count; 2496 struct pvo_entry *pvo; 2497 uintptr_t pt; 2498 2499 /* 2500 * Sync so that any pending REF/CHG bits are flushed to the PTEs (so 2501 * we can reset the right ones). note that since the pvo entries and 2502 * list heads are accessed via BAT0 and are never placed in the page 2503 * table, we don't have to worry about further accesses setting the 2504 * REF/CHG bits. 2505 */ 2506 powerpc_sync(); 2507 2508 /* 2509 * For each pvo entry, clear the pvo's ptebit. If this pvo has a 2510 * valid pte clear the ptebit from the valid pte. 2511 */ 2512 count = 0; 2513 LOCK_TABLE_RD(); 2514 LIST_FOREACH(pvo, vm_page_to_pvoh(m), pvo_vlink) { 2515 PMAP_LOCK(pvo->pvo_pmap); 2516 pt = MOEA64_PVO_TO_PTE(mmu, pvo); 2517 if (pt != -1) { 2518 MOEA64_PTE_SYNCH(mmu, pt, &pvo->pvo_pte.lpte); 2519 if (pvo->pvo_pte.lpte.pte_lo & ptebit) { 2520 count++; 2521 MOEA64_PTE_CLEAR(mmu, pt, &pvo->pvo_pte.lpte, 2522 pvo->pvo_vpn, ptebit); 2523 } 2524 } 2525 pvo->pvo_pte.lpte.pte_lo &= ~ptebit; 2526 PMAP_UNLOCK(pvo->pvo_pmap); 2527 } 2528 2529 UNLOCK_TABLE_RD(); 2530 return (count); 2531} 2532 2533boolean_t 2534moea64_dev_direct_mapped(mmu_t mmu, vm_offset_t pa, vm_size_t size) 2535{ 2536 struct pvo_entry *pvo; 2537 vm_offset_t ppa; 2538 int error = 0; 2539 2540 LOCK_TABLE_RD(); 2541 PMAP_LOCK(kernel_pmap); 2542 for (ppa = pa & ~ADDR_POFF; ppa < pa + size; ppa += PAGE_SIZE) { 2543 pvo = moea64_pvo_find_va(kernel_pmap, ppa); 2544 if (pvo == NULL || 2545 (pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN) != ppa) { 2546 error = EFAULT; 2547 break; 2548 } 2549 } 2550 UNLOCK_TABLE_RD(); 2551 PMAP_UNLOCK(kernel_pmap); 2552 2553 return (error); 2554} 2555 2556/* 2557 * Map a set of physical memory pages into the kernel virtual 2558 * address space. Return a pointer to where it is mapped. This 2559 * routine is intended to be used for mapping device memory, 2560 * NOT real memory. 2561 */ 2562void * 2563moea64_mapdev_attr(mmu_t mmu, vm_offset_t pa, vm_size_t size, vm_memattr_t ma) 2564{ 2565 vm_offset_t va, tmpva, ppa, offset; 2566 2567 ppa = trunc_page(pa); 2568 offset = pa & PAGE_MASK; 2569 size = roundup(offset + size, PAGE_SIZE); 2570 2571 va = kmem_alloc_nofault(kernel_map, size); 2572 2573 if (!va) 2574 panic("moea64_mapdev: Couldn't alloc kernel virtual memory"); 2575 2576 for (tmpva = va; size > 0;) { 2577 moea64_kenter_attr(mmu, tmpva, ppa, ma); 2578 size -= PAGE_SIZE; 2579 tmpva += PAGE_SIZE; 2580 ppa += PAGE_SIZE; 2581 } 2582 2583 return ((void *)(va + offset)); 2584} 2585 2586void * 2587moea64_mapdev(mmu_t mmu, vm_offset_t pa, vm_size_t size) 2588{ 2589 2590 return moea64_mapdev_attr(mmu, pa, size, VM_MEMATTR_DEFAULT); 2591} 2592 2593void 2594moea64_unmapdev(mmu_t mmu, vm_offset_t va, vm_size_t size) 2595{ 2596 vm_offset_t base, offset; 2597 2598 base = trunc_page(va); 2599 offset = va & PAGE_MASK; 2600 size = roundup(offset + size, PAGE_SIZE); 2601 2602 kmem_free(kernel_map, base, size); 2603} 2604 2605void 2606moea64_sync_icache(mmu_t mmu, pmap_t pm, vm_offset_t va, vm_size_t sz) 2607{ 2608 struct pvo_entry *pvo; 2609 vm_offset_t lim; 2610 vm_paddr_t pa; 2611 vm_size_t len; 2612 2613 LOCK_TABLE_RD(); 2614 PMAP_LOCK(pm); 2615 while (sz > 0) { 2616 lim = round_page(va); 2617 len = MIN(lim - va, sz); 2618 pvo = moea64_pvo_find_va(pm, va & ~ADDR_POFF); 2619 if (pvo != NULL && !(pvo->pvo_pte.lpte.pte_lo & LPTE_I)) { 2620 pa = (pvo->pvo_pte.lpte.pte_lo & LPTE_RPGN) | 2621 (va & ADDR_POFF); 2622 moea64_syncicache(mmu, pm, va, pa, len); 2623 } 2624 va += len; 2625 sz -= len; 2626 } 2627 UNLOCK_TABLE_RD(); 2628 PMAP_UNLOCK(pm); 2629} 2630