111752d88SAlan Cox /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 411752d88SAlan Cox * Copyright (c) 2002-2006 Rice University 511752d88SAlan Cox * Copyright (c) 2007 Alan L. Cox <alc@cs.rice.edu> 611752d88SAlan Cox * All rights reserved. 711752d88SAlan Cox * 811752d88SAlan Cox * This software was developed for the FreeBSD Project by Alan L. Cox, 911752d88SAlan Cox * Olivier Crameri, Peter Druschel, Sitaram Iyer, and Juan Navarro. 1011752d88SAlan Cox * 1111752d88SAlan Cox * Redistribution and use in source and binary forms, with or without 1211752d88SAlan Cox * modification, are permitted provided that the following conditions 1311752d88SAlan Cox * are met: 1411752d88SAlan Cox * 1. Redistributions of source code must retain the above copyright 1511752d88SAlan Cox * notice, this list of conditions and the following disclaimer. 1611752d88SAlan Cox * 2. Redistributions in binary form must reproduce the above copyright 1711752d88SAlan Cox * notice, this list of conditions and the following disclaimer in the 1811752d88SAlan Cox * documentation and/or other materials provided with the distribution. 1911752d88SAlan Cox * 2011752d88SAlan Cox * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2111752d88SAlan Cox * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2211752d88SAlan Cox * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2311752d88SAlan Cox * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2411752d88SAlan Cox * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2511752d88SAlan Cox * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2611752d88SAlan Cox * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2711752d88SAlan Cox * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2811752d88SAlan Cox * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2911752d88SAlan Cox * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 3011752d88SAlan Cox * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3111752d88SAlan Cox * POSSIBILITY OF SUCH DAMAGE. 3211752d88SAlan Cox */ 3311752d88SAlan Cox 34fbd80bd0SAlan Cox /* 35fbd80bd0SAlan Cox * Physical memory system implementation 36fbd80bd0SAlan Cox * 37fbd80bd0SAlan Cox * Any external functions defined by this module are only to be used by the 38fbd80bd0SAlan Cox * virtual memory system. 39fbd80bd0SAlan Cox */ 40fbd80bd0SAlan Cox 4111752d88SAlan Cox #include <sys/cdefs.h> 4211752d88SAlan Cox __FBSDID("$FreeBSD$"); 4311752d88SAlan Cox 4411752d88SAlan Cox #include "opt_ddb.h" 45174b5f38SJohn Baldwin #include "opt_vm.h" 4611752d88SAlan Cox 4711752d88SAlan Cox #include <sys/param.h> 4811752d88SAlan Cox #include <sys/systm.h> 49662e7fa8SMark Johnston #include <sys/domainset.h> 5011752d88SAlan Cox #include <sys/lock.h> 5111752d88SAlan Cox #include <sys/kernel.h> 5211752d88SAlan Cox #include <sys/malloc.h> 5311752d88SAlan Cox #include <sys/mutex.h> 547e226537SAttilio Rao #include <sys/proc.h> 5511752d88SAlan Cox #include <sys/queue.h> 5638d6b2dcSRoger Pau Monné #include <sys/rwlock.h> 5711752d88SAlan Cox #include <sys/sbuf.h> 5811752d88SAlan Cox #include <sys/sysctl.h> 5938d6b2dcSRoger Pau Monné #include <sys/tree.h> 6011752d88SAlan Cox #include <sys/vmmeter.h> 6111752d88SAlan Cox 6211752d88SAlan Cox #include <ddb/ddb.h> 6311752d88SAlan Cox 6411752d88SAlan Cox #include <vm/vm.h> 6511752d88SAlan Cox #include <vm/vm_param.h> 6611752d88SAlan Cox #include <vm/vm_kern.h> 6711752d88SAlan Cox #include <vm/vm_object.h> 6811752d88SAlan Cox #include <vm/vm_page.h> 6911752d88SAlan Cox #include <vm/vm_phys.h> 70e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h> 7111752d88SAlan Cox 72449c2e92SKonstantin Belousov _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX, 73449c2e92SKonstantin Belousov "Too many physsegs."); 7411752d88SAlan Cox 75b6715dabSJeff Roberson #ifdef NUMA 76cdfeced8SJeff Roberson struct mem_affinity __read_mostly *mem_affinity; 77cdfeced8SJeff Roberson int __read_mostly *mem_locality; 7862d70a81SJohn Baldwin #endif 79a3870a18SJohn Baldwin 80cdfeced8SJeff Roberson int __read_mostly vm_ndomains = 1; 81463406acSMark Johnston domainset_t __read_mostly all_domains = DOMAINSET_T_INITIALIZER(0x1); 827e226537SAttilio Rao 83cdfeced8SJeff Roberson struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX]; 84cdfeced8SJeff Roberson int __read_mostly vm_phys_nsegs; 8511752d88SAlan Cox 8638d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg; 8738d6b2dcSRoger Pau Monné static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *, 8838d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *); 8938d6b2dcSRoger Pau Monné 9038d6b2dcSRoger Pau Monné RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree = 91b649c2acSDoug Moore RB_INITIALIZER(&vm_phys_fictitious_tree); 9238d6b2dcSRoger Pau Monné 9338d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg { 9438d6b2dcSRoger Pau Monné RB_ENTRY(vm_phys_fictitious_seg) node; 9538d6b2dcSRoger Pau Monné /* Memory region data */ 96b6de32bdSKonstantin Belousov vm_paddr_t start; 97b6de32bdSKonstantin Belousov vm_paddr_t end; 98b6de32bdSKonstantin Belousov vm_page_t first_page; 9938d6b2dcSRoger Pau Monné }; 10038d6b2dcSRoger Pau Monné 10138d6b2dcSRoger Pau Monné RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node, 10238d6b2dcSRoger Pau Monné vm_phys_fictitious_cmp); 10338d6b2dcSRoger Pau Monné 104cdfeced8SJeff Roberson static struct rwlock_padalign vm_phys_fictitious_reg_lock; 105c0432fc3SMark Johnston MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages"); 106b6de32bdSKonstantin Belousov 107cdfeced8SJeff Roberson static struct vm_freelist __aligned(CACHE_LINE_SIZE) 108f2a496d6SKonstantin Belousov vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL] 109f2a496d6SKonstantin Belousov [VM_NFREEORDER_MAX]; 11011752d88SAlan Cox 111cdfeced8SJeff Roberson static int __read_mostly vm_nfreelists; 112d866a563SAlan Cox 113d866a563SAlan Cox /* 11421943937SJeff Roberson * These "avail lists" are globals used to communicate boot-time physical 11521943937SJeff Roberson * memory layout to other parts of the kernel. Each physically contiguous 11621943937SJeff Roberson * region of memory is defined by a start address at an even index and an 11721943937SJeff Roberson * end address at the following odd index. Each list is terminated by a 11821943937SJeff Roberson * pair of zero entries. 11921943937SJeff Roberson * 12021943937SJeff Roberson * dump_avail tells the dump code what regions to include in a crash dump, and 12121943937SJeff Roberson * phys_avail is all of the remaining physical memory that is available for 12221943937SJeff Roberson * the vm system. 12321943937SJeff Roberson * 12421943937SJeff Roberson * Initially dump_avail and phys_avail are identical. Boot time memory 12521943937SJeff Roberson * allocations remove extents from phys_avail that may still be included 12621943937SJeff Roberson * in dumps. 12721943937SJeff Roberson */ 12821943937SJeff Roberson vm_paddr_t phys_avail[PHYS_AVAIL_COUNT]; 12921943937SJeff Roberson vm_paddr_t dump_avail[PHYS_AVAIL_COUNT]; 13021943937SJeff Roberson 13121943937SJeff Roberson /* 132d866a563SAlan Cox * Provides the mapping from VM_FREELIST_* to free list indices (flind). 133d866a563SAlan Cox */ 134cdfeced8SJeff Roberson static int __read_mostly vm_freelist_to_flind[VM_NFREELIST]; 135d866a563SAlan Cox 136d866a563SAlan Cox CTASSERT(VM_FREELIST_DEFAULT == 0); 137d866a563SAlan Cox 138d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 139d866a563SAlan Cox #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32) 140d866a563SAlan Cox #endif 141d866a563SAlan Cox 142d866a563SAlan Cox /* 143d866a563SAlan Cox * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about 144d866a563SAlan Cox * the ordering of the free list boundaries. 145d866a563SAlan Cox */ 146d866a563SAlan Cox #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY) 147d866a563SAlan Cox CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY); 148d866a563SAlan Cox #endif 14911752d88SAlan Cox 15011752d88SAlan Cox static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS); 151*7029da5cSPawel Biernacki SYSCTL_OID(_vm, OID_AUTO, phys_free, 152*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0, 153*7029da5cSPawel Biernacki sysctl_vm_phys_free, "A", 154*7029da5cSPawel Biernacki "Phys Free Info"); 15511752d88SAlan Cox 15611752d88SAlan Cox static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS); 157*7029da5cSPawel Biernacki SYSCTL_OID(_vm, OID_AUTO, phys_segs, 158*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0, 159*7029da5cSPawel Biernacki sysctl_vm_phys_segs, "A", 160*7029da5cSPawel Biernacki "Phys Seg Info"); 16111752d88SAlan Cox 162b6715dabSJeff Roberson #ifdef NUMA 163415d7ccaSAdrian Chadd static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS); 164*7029da5cSPawel Biernacki SYSCTL_OID(_vm, OID_AUTO, phys_locality, 165*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0, 166*7029da5cSPawel Biernacki sysctl_vm_phys_locality, "A", 167*7029da5cSPawel Biernacki "Phys Locality Info"); 1686520495aSAdrian Chadd #endif 169415d7ccaSAdrian Chadd 1707e226537SAttilio Rao SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD, 1717e226537SAttilio Rao &vm_ndomains, 0, "Number of physical memory domains available."); 172a3870a18SJohn Baldwin 173c869e672SAlan Cox static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, 174c869e672SAlan Cox u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 175c869e672SAlan Cox vm_paddr_t boundary); 176d866a563SAlan Cox static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain); 177d866a563SAlan Cox static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end); 17811752d88SAlan Cox static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, 179370a338aSAlan Cox int order, int tail); 18011752d88SAlan Cox 18138d6b2dcSRoger Pau Monné /* 18238d6b2dcSRoger Pau Monné * Red-black tree helpers for vm fictitious range management. 18338d6b2dcSRoger Pau Monné */ 18438d6b2dcSRoger Pau Monné static inline int 18538d6b2dcSRoger Pau Monné vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p, 18638d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *range) 18738d6b2dcSRoger Pau Monné { 18838d6b2dcSRoger Pau Monné 18938d6b2dcSRoger Pau Monné KASSERT(range->start != 0 && range->end != 0, 19038d6b2dcSRoger Pau Monné ("Invalid range passed on search for vm_fictitious page")); 19138d6b2dcSRoger Pau Monné if (p->start >= range->end) 19238d6b2dcSRoger Pau Monné return (1); 19338d6b2dcSRoger Pau Monné if (p->start < range->start) 19438d6b2dcSRoger Pau Monné return (-1); 19538d6b2dcSRoger Pau Monné 19638d6b2dcSRoger Pau Monné return (0); 19738d6b2dcSRoger Pau Monné } 19838d6b2dcSRoger Pau Monné 19938d6b2dcSRoger Pau Monné static int 20038d6b2dcSRoger Pau Monné vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1, 20138d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *p2) 20238d6b2dcSRoger Pau Monné { 20338d6b2dcSRoger Pau Monné 20438d6b2dcSRoger Pau Monné /* Check if this is a search for a page */ 20538d6b2dcSRoger Pau Monné if (p1->end == 0) 20638d6b2dcSRoger Pau Monné return (vm_phys_fictitious_in_range(p1, p2)); 20738d6b2dcSRoger Pau Monné 20838d6b2dcSRoger Pau Monné KASSERT(p2->end != 0, 20938d6b2dcSRoger Pau Monné ("Invalid range passed as second parameter to vm fictitious comparison")); 21038d6b2dcSRoger Pau Monné 21138d6b2dcSRoger Pau Monné /* Searching to add a new range */ 21238d6b2dcSRoger Pau Monné if (p1->end <= p2->start) 21338d6b2dcSRoger Pau Monné return (-1); 21438d6b2dcSRoger Pau Monné if (p1->start >= p2->end) 21538d6b2dcSRoger Pau Monné return (1); 21638d6b2dcSRoger Pau Monné 21738d6b2dcSRoger Pau Monné panic("Trying to add overlapping vm fictitious ranges:\n" 21838d6b2dcSRoger Pau Monné "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start, 21938d6b2dcSRoger Pau Monné (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end); 22038d6b2dcSRoger Pau Monné } 22138d6b2dcSRoger Pau Monné 2226f4acaf4SJeff Roberson int 2236f4acaf4SJeff Roberson vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high) 224449c2e92SKonstantin Belousov { 225b6715dabSJeff Roberson #ifdef NUMA 2266f4acaf4SJeff Roberson domainset_t mask; 2276f4acaf4SJeff Roberson int i; 228449c2e92SKonstantin Belousov 2296f4acaf4SJeff Roberson if (vm_ndomains == 1 || mem_affinity == NULL) 2306f4acaf4SJeff Roberson return (0); 2316f4acaf4SJeff Roberson 2326f4acaf4SJeff Roberson DOMAINSET_ZERO(&mask); 2336f4acaf4SJeff Roberson /* 2346f4acaf4SJeff Roberson * Check for any memory that overlaps low, high. 2356f4acaf4SJeff Roberson */ 2366f4acaf4SJeff Roberson for (i = 0; mem_affinity[i].end != 0; i++) 2376f4acaf4SJeff Roberson if (mem_affinity[i].start <= high && 2386f4acaf4SJeff Roberson mem_affinity[i].end >= low) 2396f4acaf4SJeff Roberson DOMAINSET_SET(mem_affinity[i].domain, &mask); 2406f4acaf4SJeff Roberson if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask)) 2416f4acaf4SJeff Roberson return (prefer); 2426f4acaf4SJeff Roberson if (DOMAINSET_EMPTY(&mask)) 2436f4acaf4SJeff Roberson panic("vm_phys_domain_match: Impossible constraint"); 2446f4acaf4SJeff Roberson return (DOMAINSET_FFS(&mask) - 1); 2456f4acaf4SJeff Roberson #else 2466f4acaf4SJeff Roberson return (0); 2476f4acaf4SJeff Roberson #endif 248449c2e92SKonstantin Belousov } 249449c2e92SKonstantin Belousov 25011752d88SAlan Cox /* 25111752d88SAlan Cox * Outputs the state of the physical memory allocator, specifically, 25211752d88SAlan Cox * the amount of physical memory in each free list. 25311752d88SAlan Cox */ 25411752d88SAlan Cox static int 25511752d88SAlan Cox sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS) 25611752d88SAlan Cox { 25711752d88SAlan Cox struct sbuf sbuf; 25811752d88SAlan Cox struct vm_freelist *fl; 2597e226537SAttilio Rao int dom, error, flind, oind, pind; 26011752d88SAlan Cox 26100f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 26200f0e671SMatthew D Fleming if (error != 0) 26300f0e671SMatthew D Fleming return (error); 2647e226537SAttilio Rao sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req); 2657e226537SAttilio Rao for (dom = 0; dom < vm_ndomains; dom++) { 266eb2f42fbSAlan Cox sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom); 26711752d88SAlan Cox for (flind = 0; flind < vm_nfreelists; flind++) { 268eb2f42fbSAlan Cox sbuf_printf(&sbuf, "\nFREE LIST %d:\n" 26911752d88SAlan Cox "\n ORDER (SIZE) | NUMBER" 27011752d88SAlan Cox "\n ", flind); 27111752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 27211752d88SAlan Cox sbuf_printf(&sbuf, " | POOL %d", pind); 27311752d88SAlan Cox sbuf_printf(&sbuf, "\n-- "); 27411752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 27511752d88SAlan Cox sbuf_printf(&sbuf, "-- -- "); 27611752d88SAlan Cox sbuf_printf(&sbuf, "--\n"); 27711752d88SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) { 278d689bc00SAlan Cox sbuf_printf(&sbuf, " %2d (%6dK)", oind, 27911752d88SAlan Cox 1 << (PAGE_SHIFT - 10 + oind)); 28011752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 2817e226537SAttilio Rao fl = vm_phys_free_queues[dom][flind][pind]; 282eb2f42fbSAlan Cox sbuf_printf(&sbuf, " | %6d", 2837e226537SAttilio Rao fl[oind].lcnt); 28411752d88SAlan Cox } 28511752d88SAlan Cox sbuf_printf(&sbuf, "\n"); 28611752d88SAlan Cox } 2877e226537SAttilio Rao } 28811752d88SAlan Cox } 2894e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 29011752d88SAlan Cox sbuf_delete(&sbuf); 29111752d88SAlan Cox return (error); 29211752d88SAlan Cox } 29311752d88SAlan Cox 29411752d88SAlan Cox /* 29511752d88SAlan Cox * Outputs the set of physical memory segments. 29611752d88SAlan Cox */ 29711752d88SAlan Cox static int 29811752d88SAlan Cox sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS) 29911752d88SAlan Cox { 30011752d88SAlan Cox struct sbuf sbuf; 30111752d88SAlan Cox struct vm_phys_seg *seg; 30211752d88SAlan Cox int error, segind; 30311752d88SAlan Cox 30400f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 30500f0e671SMatthew D Fleming if (error != 0) 30600f0e671SMatthew D Fleming return (error); 3074e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 30811752d88SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 30911752d88SAlan Cox sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind); 31011752d88SAlan Cox seg = &vm_phys_segs[segind]; 31111752d88SAlan Cox sbuf_printf(&sbuf, "start: %#jx\n", 31211752d88SAlan Cox (uintmax_t)seg->start); 31311752d88SAlan Cox sbuf_printf(&sbuf, "end: %#jx\n", 31411752d88SAlan Cox (uintmax_t)seg->end); 315a3870a18SJohn Baldwin sbuf_printf(&sbuf, "domain: %d\n", seg->domain); 31611752d88SAlan Cox sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues); 31711752d88SAlan Cox } 3184e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 31911752d88SAlan Cox sbuf_delete(&sbuf); 32011752d88SAlan Cox return (error); 32111752d88SAlan Cox } 32211752d88SAlan Cox 323415d7ccaSAdrian Chadd /* 324415d7ccaSAdrian Chadd * Return affinity, or -1 if there's no affinity information. 325415d7ccaSAdrian Chadd */ 3266520495aSAdrian Chadd int 327415d7ccaSAdrian Chadd vm_phys_mem_affinity(int f, int t) 328415d7ccaSAdrian Chadd { 329415d7ccaSAdrian Chadd 330b6715dabSJeff Roberson #ifdef NUMA 331415d7ccaSAdrian Chadd if (mem_locality == NULL) 332415d7ccaSAdrian Chadd return (-1); 333415d7ccaSAdrian Chadd if (f >= vm_ndomains || t >= vm_ndomains) 334415d7ccaSAdrian Chadd return (-1); 335415d7ccaSAdrian Chadd return (mem_locality[f * vm_ndomains + t]); 3366520495aSAdrian Chadd #else 3376520495aSAdrian Chadd return (-1); 3386520495aSAdrian Chadd #endif 339415d7ccaSAdrian Chadd } 340415d7ccaSAdrian Chadd 341b6715dabSJeff Roberson #ifdef NUMA 342415d7ccaSAdrian Chadd /* 343415d7ccaSAdrian Chadd * Outputs the VM locality table. 344415d7ccaSAdrian Chadd */ 345415d7ccaSAdrian Chadd static int 346415d7ccaSAdrian Chadd sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS) 347415d7ccaSAdrian Chadd { 348415d7ccaSAdrian Chadd struct sbuf sbuf; 349415d7ccaSAdrian Chadd int error, i, j; 350415d7ccaSAdrian Chadd 351415d7ccaSAdrian Chadd error = sysctl_wire_old_buffer(req, 0); 352415d7ccaSAdrian Chadd if (error != 0) 353415d7ccaSAdrian Chadd return (error); 354415d7ccaSAdrian Chadd sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 355415d7ccaSAdrian Chadd 356415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "\n"); 357415d7ccaSAdrian Chadd 358415d7ccaSAdrian Chadd for (i = 0; i < vm_ndomains; i++) { 359415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "%d: ", i); 360415d7ccaSAdrian Chadd for (j = 0; j < vm_ndomains; j++) { 361415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j)); 362415d7ccaSAdrian Chadd } 363415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "\n"); 364415d7ccaSAdrian Chadd } 365415d7ccaSAdrian Chadd error = sbuf_finish(&sbuf); 366415d7ccaSAdrian Chadd sbuf_delete(&sbuf); 367415d7ccaSAdrian Chadd return (error); 368415d7ccaSAdrian Chadd } 3696520495aSAdrian Chadd #endif 370415d7ccaSAdrian Chadd 3717e226537SAttilio Rao static void 3727e226537SAttilio Rao vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail) 373a3870a18SJohn Baldwin { 374a3870a18SJohn Baldwin 3757e226537SAttilio Rao m->order = order; 3767e226537SAttilio Rao if (tail) 3775cd29d0fSMark Johnston TAILQ_INSERT_TAIL(&fl[order].pl, m, listq); 3787e226537SAttilio Rao else 3795cd29d0fSMark Johnston TAILQ_INSERT_HEAD(&fl[order].pl, m, listq); 3807e226537SAttilio Rao fl[order].lcnt++; 381a3870a18SJohn Baldwin } 3827e226537SAttilio Rao 3837e226537SAttilio Rao static void 3847e226537SAttilio Rao vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order) 3857e226537SAttilio Rao { 3867e226537SAttilio Rao 3875cd29d0fSMark Johnston TAILQ_REMOVE(&fl[order].pl, m, listq); 3887e226537SAttilio Rao fl[order].lcnt--; 3897e226537SAttilio Rao m->order = VM_NFREEORDER; 390a3870a18SJohn Baldwin } 391a3870a18SJohn Baldwin 39211752d88SAlan Cox /* 39311752d88SAlan Cox * Create a physical memory segment. 39411752d88SAlan Cox */ 39511752d88SAlan Cox static void 396d866a563SAlan Cox _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain) 39711752d88SAlan Cox { 39811752d88SAlan Cox struct vm_phys_seg *seg; 39911752d88SAlan Cox 40011752d88SAlan Cox KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX, 40111752d88SAlan Cox ("vm_phys_create_seg: increase VM_PHYSSEG_MAX")); 402ef435ae7SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 4037e226537SAttilio Rao ("vm_phys_create_seg: invalid domain provided")); 40411752d88SAlan Cox seg = &vm_phys_segs[vm_phys_nsegs++]; 405271f0f12SAlan Cox while (seg > vm_phys_segs && (seg - 1)->start >= end) { 406271f0f12SAlan Cox *seg = *(seg - 1); 407271f0f12SAlan Cox seg--; 408271f0f12SAlan Cox } 40911752d88SAlan Cox seg->start = start; 41011752d88SAlan Cox seg->end = end; 411a3870a18SJohn Baldwin seg->domain = domain; 41211752d88SAlan Cox } 41311752d88SAlan Cox 414a3870a18SJohn Baldwin static void 415d866a563SAlan Cox vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end) 416a3870a18SJohn Baldwin { 417b6715dabSJeff Roberson #ifdef NUMA 418a3870a18SJohn Baldwin int i; 419a3870a18SJohn Baldwin 420a3870a18SJohn Baldwin if (mem_affinity == NULL) { 421d866a563SAlan Cox _vm_phys_create_seg(start, end, 0); 422a3870a18SJohn Baldwin return; 423a3870a18SJohn Baldwin } 424a3870a18SJohn Baldwin 425a3870a18SJohn Baldwin for (i = 0;; i++) { 426a3870a18SJohn Baldwin if (mem_affinity[i].end == 0) 427a3870a18SJohn Baldwin panic("Reached end of affinity info"); 428a3870a18SJohn Baldwin if (mem_affinity[i].end <= start) 429a3870a18SJohn Baldwin continue; 430a3870a18SJohn Baldwin if (mem_affinity[i].start > start) 431a3870a18SJohn Baldwin panic("No affinity info for start %jx", 432a3870a18SJohn Baldwin (uintmax_t)start); 433a3870a18SJohn Baldwin if (mem_affinity[i].end >= end) { 434d866a563SAlan Cox _vm_phys_create_seg(start, end, 435a3870a18SJohn Baldwin mem_affinity[i].domain); 436a3870a18SJohn Baldwin break; 437a3870a18SJohn Baldwin } 438d866a563SAlan Cox _vm_phys_create_seg(start, mem_affinity[i].end, 439a3870a18SJohn Baldwin mem_affinity[i].domain); 440a3870a18SJohn Baldwin start = mem_affinity[i].end; 441a3870a18SJohn Baldwin } 44262d70a81SJohn Baldwin #else 44362d70a81SJohn Baldwin _vm_phys_create_seg(start, end, 0); 44462d70a81SJohn Baldwin #endif 445a3870a18SJohn Baldwin } 446a3870a18SJohn Baldwin 44711752d88SAlan Cox /* 448271f0f12SAlan Cox * Add a physical memory segment. 449271f0f12SAlan Cox */ 450271f0f12SAlan Cox void 451271f0f12SAlan Cox vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end) 452271f0f12SAlan Cox { 453d866a563SAlan Cox vm_paddr_t paddr; 454271f0f12SAlan Cox 455271f0f12SAlan Cox KASSERT((start & PAGE_MASK) == 0, 456271f0f12SAlan Cox ("vm_phys_define_seg: start is not page aligned")); 457271f0f12SAlan Cox KASSERT((end & PAGE_MASK) == 0, 458271f0f12SAlan Cox ("vm_phys_define_seg: end is not page aligned")); 459d866a563SAlan Cox 460d866a563SAlan Cox /* 461d866a563SAlan Cox * Split the physical memory segment if it spans two or more free 462d866a563SAlan Cox * list boundaries. 463d866a563SAlan Cox */ 464d866a563SAlan Cox paddr = start; 465d866a563SAlan Cox #ifdef VM_FREELIST_LOWMEM 466d866a563SAlan Cox if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) { 467d866a563SAlan Cox vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY); 468d866a563SAlan Cox paddr = VM_LOWMEM_BOUNDARY; 469d866a563SAlan Cox } 470271f0f12SAlan Cox #endif 471d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 472d866a563SAlan Cox if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) { 473d866a563SAlan Cox vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY); 474d866a563SAlan Cox paddr = VM_DMA32_BOUNDARY; 475d866a563SAlan Cox } 476d866a563SAlan Cox #endif 477d866a563SAlan Cox vm_phys_create_seg(paddr, end); 478271f0f12SAlan Cox } 479271f0f12SAlan Cox 480271f0f12SAlan Cox /* 48111752d88SAlan Cox * Initialize the physical memory allocator. 482d866a563SAlan Cox * 483d866a563SAlan Cox * Requires that vm_page_array is initialized! 48411752d88SAlan Cox */ 48511752d88SAlan Cox void 48611752d88SAlan Cox vm_phys_init(void) 48711752d88SAlan Cox { 48811752d88SAlan Cox struct vm_freelist *fl; 48972aebdd7SAlan Cox struct vm_phys_seg *end_seg, *prev_seg, *seg, *tmp_seg; 490d866a563SAlan Cox u_long npages; 491d866a563SAlan Cox int dom, flind, freelist, oind, pind, segind; 49211752d88SAlan Cox 493d866a563SAlan Cox /* 494d866a563SAlan Cox * Compute the number of free lists, and generate the mapping from the 495d866a563SAlan Cox * manifest constants VM_FREELIST_* to the free list indices. 496d866a563SAlan Cox * 497d866a563SAlan Cox * Initially, the entries of vm_freelist_to_flind[] are set to either 498d866a563SAlan Cox * 0 or 1 to indicate which free lists should be created. 499d866a563SAlan Cox */ 500d866a563SAlan Cox npages = 0; 501d866a563SAlan Cox for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) { 502d866a563SAlan Cox seg = &vm_phys_segs[segind]; 503d866a563SAlan Cox #ifdef VM_FREELIST_LOWMEM 504d866a563SAlan Cox if (seg->end <= VM_LOWMEM_BOUNDARY) 505d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1; 506d866a563SAlan Cox else 507d866a563SAlan Cox #endif 508d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 509d866a563SAlan Cox if ( 510d866a563SAlan Cox #ifdef VM_DMA32_NPAGES_THRESHOLD 511d866a563SAlan Cox /* 512d866a563SAlan Cox * Create the DMA32 free list only if the amount of 513d866a563SAlan Cox * physical memory above physical address 4G exceeds the 514d866a563SAlan Cox * given threshold. 515d866a563SAlan Cox */ 516d866a563SAlan Cox npages > VM_DMA32_NPAGES_THRESHOLD && 517d866a563SAlan Cox #endif 518d866a563SAlan Cox seg->end <= VM_DMA32_BOUNDARY) 519d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_DMA32] = 1; 520d866a563SAlan Cox else 521d866a563SAlan Cox #endif 522d866a563SAlan Cox { 523d866a563SAlan Cox npages += atop(seg->end - seg->start); 524d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1; 525d866a563SAlan Cox } 526d866a563SAlan Cox } 527d866a563SAlan Cox /* Change each entry into a running total of the free lists. */ 528d866a563SAlan Cox for (freelist = 1; freelist < VM_NFREELIST; freelist++) { 529d866a563SAlan Cox vm_freelist_to_flind[freelist] += 530d866a563SAlan Cox vm_freelist_to_flind[freelist - 1]; 531d866a563SAlan Cox } 532d866a563SAlan Cox vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1]; 533d866a563SAlan Cox KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists")); 534d866a563SAlan Cox /* Change each entry into a free list index. */ 535d866a563SAlan Cox for (freelist = 0; freelist < VM_NFREELIST; freelist++) 536d866a563SAlan Cox vm_freelist_to_flind[freelist]--; 537d866a563SAlan Cox 538d866a563SAlan Cox /* 539d866a563SAlan Cox * Initialize the first_page and free_queues fields of each physical 540d866a563SAlan Cox * memory segment. 541d866a563SAlan Cox */ 542271f0f12SAlan Cox #ifdef VM_PHYSSEG_SPARSE 543d866a563SAlan Cox npages = 0; 54411752d88SAlan Cox #endif 545271f0f12SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 546271f0f12SAlan Cox seg = &vm_phys_segs[segind]; 547271f0f12SAlan Cox #ifdef VM_PHYSSEG_SPARSE 548d866a563SAlan Cox seg->first_page = &vm_page_array[npages]; 549d866a563SAlan Cox npages += atop(seg->end - seg->start); 550271f0f12SAlan Cox #else 551271f0f12SAlan Cox seg->first_page = PHYS_TO_VM_PAGE(seg->start); 55211752d88SAlan Cox #endif 553d866a563SAlan Cox #ifdef VM_FREELIST_LOWMEM 554d866a563SAlan Cox if (seg->end <= VM_LOWMEM_BOUNDARY) { 555d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM]; 556d866a563SAlan Cox KASSERT(flind >= 0, 557d866a563SAlan Cox ("vm_phys_init: LOWMEM flind < 0")); 558d866a563SAlan Cox } else 559d866a563SAlan Cox #endif 560d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 561d866a563SAlan Cox if (seg->end <= VM_DMA32_BOUNDARY) { 562d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_DMA32]; 563d866a563SAlan Cox KASSERT(flind >= 0, 564d866a563SAlan Cox ("vm_phys_init: DMA32 flind < 0")); 565d866a563SAlan Cox } else 566d866a563SAlan Cox #endif 567d866a563SAlan Cox { 568d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT]; 569d866a563SAlan Cox KASSERT(flind >= 0, 570d866a563SAlan Cox ("vm_phys_init: DEFAULT flind < 0")); 57111752d88SAlan Cox } 572d866a563SAlan Cox seg->free_queues = &vm_phys_free_queues[seg->domain][flind]; 573d866a563SAlan Cox } 574d866a563SAlan Cox 575d866a563SAlan Cox /* 57672aebdd7SAlan Cox * Coalesce physical memory segments that are contiguous and share the 57772aebdd7SAlan Cox * same per-domain free queues. 57872aebdd7SAlan Cox */ 57972aebdd7SAlan Cox prev_seg = vm_phys_segs; 58072aebdd7SAlan Cox seg = &vm_phys_segs[1]; 58172aebdd7SAlan Cox end_seg = &vm_phys_segs[vm_phys_nsegs]; 58272aebdd7SAlan Cox while (seg < end_seg) { 58372aebdd7SAlan Cox if (prev_seg->end == seg->start && 58472aebdd7SAlan Cox prev_seg->free_queues == seg->free_queues) { 58572aebdd7SAlan Cox prev_seg->end = seg->end; 58672aebdd7SAlan Cox KASSERT(prev_seg->domain == seg->domain, 58772aebdd7SAlan Cox ("vm_phys_init: free queues cannot span domains")); 58872aebdd7SAlan Cox vm_phys_nsegs--; 58972aebdd7SAlan Cox end_seg--; 59072aebdd7SAlan Cox for (tmp_seg = seg; tmp_seg < end_seg; tmp_seg++) 59172aebdd7SAlan Cox *tmp_seg = *(tmp_seg + 1); 59272aebdd7SAlan Cox } else { 59372aebdd7SAlan Cox prev_seg = seg; 59472aebdd7SAlan Cox seg++; 59572aebdd7SAlan Cox } 59672aebdd7SAlan Cox } 59772aebdd7SAlan Cox 59872aebdd7SAlan Cox /* 599d866a563SAlan Cox * Initialize the free queues. 600d866a563SAlan Cox */ 6017e226537SAttilio Rao for (dom = 0; dom < vm_ndomains; dom++) { 60211752d88SAlan Cox for (flind = 0; flind < vm_nfreelists; flind++) { 60311752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 6047e226537SAttilio Rao fl = vm_phys_free_queues[dom][flind][pind]; 60511752d88SAlan Cox for (oind = 0; oind < VM_NFREEORDER; oind++) 60611752d88SAlan Cox TAILQ_INIT(&fl[oind].pl); 60711752d88SAlan Cox } 60811752d88SAlan Cox } 609a3870a18SJohn Baldwin } 610d866a563SAlan Cox 61138d6b2dcSRoger Pau Monné rw_init(&vm_phys_fictitious_reg_lock, "vmfctr"); 61211752d88SAlan Cox } 61311752d88SAlan Cox 61411752d88SAlan Cox /* 615662e7fa8SMark Johnston * Register info about the NUMA topology of the system. 616662e7fa8SMark Johnston * 617662e7fa8SMark Johnston * Invoked by platform-dependent code prior to vm_phys_init(). 618662e7fa8SMark Johnston */ 619662e7fa8SMark Johnston void 620662e7fa8SMark Johnston vm_phys_register_domains(int ndomains, struct mem_affinity *affinity, 621662e7fa8SMark Johnston int *locality) 622662e7fa8SMark Johnston { 623662e7fa8SMark Johnston #ifdef NUMA 624b61f3142SMark Johnston int d, i; 625662e7fa8SMark Johnston 626b61f3142SMark Johnston /* 627b61f3142SMark Johnston * For now the only override value that we support is 1, which 628b61f3142SMark Johnston * effectively disables NUMA-awareness in the allocators. 629b61f3142SMark Johnston */ 630b61f3142SMark Johnston d = 0; 631b61f3142SMark Johnston TUNABLE_INT_FETCH("vm.numa.disabled", &d); 632b61f3142SMark Johnston if (d) 633b61f3142SMark Johnston ndomains = 1; 634b61f3142SMark Johnston 635b61f3142SMark Johnston if (ndomains > 1) { 636662e7fa8SMark Johnston vm_ndomains = ndomains; 637662e7fa8SMark Johnston mem_affinity = affinity; 638662e7fa8SMark Johnston mem_locality = locality; 639b61f3142SMark Johnston } 640662e7fa8SMark Johnston 641662e7fa8SMark Johnston for (i = 0; i < vm_ndomains; i++) 642662e7fa8SMark Johnston DOMAINSET_SET(i, &all_domains); 643662e7fa8SMark Johnston #else 644662e7fa8SMark Johnston (void)ndomains; 645662e7fa8SMark Johnston (void)affinity; 646662e7fa8SMark Johnston (void)locality; 647662e7fa8SMark Johnston #endif 648662e7fa8SMark Johnston } 649662e7fa8SMark Johnston 650c1685086SJeff Roberson int 651c1685086SJeff Roberson _vm_phys_domain(vm_paddr_t pa) 652c1685086SJeff Roberson { 653c1685086SJeff Roberson #ifdef NUMA 654c1685086SJeff Roberson int i; 655c1685086SJeff Roberson 656c1685086SJeff Roberson if (vm_ndomains == 1 || mem_affinity == NULL) 657c1685086SJeff Roberson return (0); 658c1685086SJeff Roberson 659c1685086SJeff Roberson /* 660c1685086SJeff Roberson * Check for any memory that overlaps. 661c1685086SJeff Roberson */ 662c1685086SJeff Roberson for (i = 0; mem_affinity[i].end != 0; i++) 663c1685086SJeff Roberson if (mem_affinity[i].start <= pa && 664c1685086SJeff Roberson mem_affinity[i].end >= pa) 665c1685086SJeff Roberson return (mem_affinity[i].domain); 666c1685086SJeff Roberson #endif 667c1685086SJeff Roberson return (0); 668c1685086SJeff Roberson } 669c1685086SJeff Roberson 670662e7fa8SMark Johnston /* 67111752d88SAlan Cox * Split a contiguous, power of two-sized set of physical pages. 672370a338aSAlan Cox * 673370a338aSAlan Cox * When this function is called by a page allocation function, the caller 674370a338aSAlan Cox * should request insertion at the head unless the order [order, oind) queues 675370a338aSAlan Cox * are known to be empty. The objective being to reduce the likelihood of 676370a338aSAlan Cox * long-term fragmentation by promoting contemporaneous allocation and 677370a338aSAlan Cox * (hopefully) deallocation. 67811752d88SAlan Cox */ 67911752d88SAlan Cox static __inline void 680370a338aSAlan Cox vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order, 681370a338aSAlan Cox int tail) 68211752d88SAlan Cox { 68311752d88SAlan Cox vm_page_t m_buddy; 68411752d88SAlan Cox 68511752d88SAlan Cox while (oind > order) { 68611752d88SAlan Cox oind--; 68711752d88SAlan Cox m_buddy = &m[1 << oind]; 68811752d88SAlan Cox KASSERT(m_buddy->order == VM_NFREEORDER, 68911752d88SAlan Cox ("vm_phys_split_pages: page %p has unexpected order %d", 69011752d88SAlan Cox m_buddy, m_buddy->order)); 691370a338aSAlan Cox vm_freelist_add(fl, m_buddy, oind, tail); 69211752d88SAlan Cox } 69311752d88SAlan Cox } 69411752d88SAlan Cox 69511752d88SAlan Cox /* 6967493904eSAlan Cox * Add the physical pages [m, m + npages) at the end of a power-of-two aligned 6977493904eSAlan Cox * and sized set to the specified free list. 6987493904eSAlan Cox * 6997493904eSAlan Cox * When this function is called by a page allocation function, the caller 7007493904eSAlan Cox * should request insertion at the head unless the lower-order queues are 7017493904eSAlan Cox * known to be empty. The objective being to reduce the likelihood of long- 7027493904eSAlan Cox * term fragmentation by promoting contemporaneous allocation and (hopefully) 7037493904eSAlan Cox * deallocation. 7047493904eSAlan Cox * 7057493904eSAlan Cox * The physical page m's buddy must not be free. 7067493904eSAlan Cox */ 7077493904eSAlan Cox static void 7087493904eSAlan Cox vm_phys_enq_range(vm_page_t m, u_int npages, struct vm_freelist *fl, int tail) 7097493904eSAlan Cox { 7107493904eSAlan Cox u_int n; 7117493904eSAlan Cox int order; 7127493904eSAlan Cox 7137493904eSAlan Cox KASSERT(npages > 0, ("vm_phys_enq_range: npages is 0")); 7147493904eSAlan Cox KASSERT(((VM_PAGE_TO_PHYS(m) + npages * PAGE_SIZE) & 7157493904eSAlan Cox ((PAGE_SIZE << (fls(npages) - 1)) - 1)) == 0, 7167493904eSAlan Cox ("vm_phys_enq_range: page %p and npages %u are misaligned", 7177493904eSAlan Cox m, npages)); 7187493904eSAlan Cox do { 7197493904eSAlan Cox KASSERT(m->order == VM_NFREEORDER, 7207493904eSAlan Cox ("vm_phys_enq_range: page %p has unexpected order %d", 7217493904eSAlan Cox m, m->order)); 7227493904eSAlan Cox order = ffs(npages) - 1; 7237493904eSAlan Cox KASSERT(order < VM_NFREEORDER, 7247493904eSAlan Cox ("vm_phys_enq_range: order %d is out of range", order)); 7257493904eSAlan Cox vm_freelist_add(fl, m, order, tail); 7267493904eSAlan Cox n = 1 << order; 7277493904eSAlan Cox m += n; 7287493904eSAlan Cox npages -= n; 7297493904eSAlan Cox } while (npages > 0); 7307493904eSAlan Cox } 7317493904eSAlan Cox 7327493904eSAlan Cox /* 73389ea39a7SAlan Cox * Tries to allocate the specified number of pages from the specified pool 73489ea39a7SAlan Cox * within the specified domain. Returns the actual number of allocated pages 73589ea39a7SAlan Cox * and a pointer to each page through the array ma[]. 73689ea39a7SAlan Cox * 73732d81f21SAlan Cox * The returned pages may not be physically contiguous. However, in contrast 73832d81f21SAlan Cox * to performing multiple, back-to-back calls to vm_phys_alloc_pages(..., 0), 73932d81f21SAlan Cox * calling this function once to allocate the desired number of pages will 74032d81f21SAlan Cox * avoid wasted time in vm_phys_split_pages(). 74189ea39a7SAlan Cox * 74289ea39a7SAlan Cox * The free page queues for the specified domain must be locked. 74389ea39a7SAlan Cox */ 74489ea39a7SAlan Cox int 74589ea39a7SAlan Cox vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[]) 74689ea39a7SAlan Cox { 74789ea39a7SAlan Cox struct vm_freelist *alt, *fl; 74889ea39a7SAlan Cox vm_page_t m; 74989ea39a7SAlan Cox int avail, end, flind, freelist, i, need, oind, pind; 75089ea39a7SAlan Cox 75189ea39a7SAlan Cox KASSERT(domain >= 0 && domain < vm_ndomains, 75289ea39a7SAlan Cox ("vm_phys_alloc_npages: domain %d is out of range", domain)); 75389ea39a7SAlan Cox KASSERT(pool < VM_NFREEPOOL, 75489ea39a7SAlan Cox ("vm_phys_alloc_npages: pool %d is out of range", pool)); 75589ea39a7SAlan Cox KASSERT(npages <= 1 << (VM_NFREEORDER - 1), 75689ea39a7SAlan Cox ("vm_phys_alloc_npages: npages %d is out of range", npages)); 75789ea39a7SAlan Cox vm_domain_free_assert_locked(VM_DOMAIN(domain)); 75889ea39a7SAlan Cox i = 0; 75989ea39a7SAlan Cox for (freelist = 0; freelist < VM_NFREELIST; freelist++) { 76089ea39a7SAlan Cox flind = vm_freelist_to_flind[freelist]; 76189ea39a7SAlan Cox if (flind < 0) 76289ea39a7SAlan Cox continue; 76389ea39a7SAlan Cox fl = vm_phys_free_queues[domain][flind][pool]; 76489ea39a7SAlan Cox for (oind = 0; oind < VM_NFREEORDER; oind++) { 76589ea39a7SAlan Cox while ((m = TAILQ_FIRST(&fl[oind].pl)) != NULL) { 76689ea39a7SAlan Cox vm_freelist_rem(fl, m, oind); 76789ea39a7SAlan Cox avail = 1 << oind; 76889ea39a7SAlan Cox need = imin(npages - i, avail); 76989ea39a7SAlan Cox for (end = i + need; i < end;) 77089ea39a7SAlan Cox ma[i++] = m++; 77189ea39a7SAlan Cox if (need < avail) { 7727493904eSAlan Cox /* 7737493904eSAlan Cox * Return excess pages to fl. Its 7747493904eSAlan Cox * order [0, oind) queues are empty. 7757493904eSAlan Cox */ 7767493904eSAlan Cox vm_phys_enq_range(m, avail - need, fl, 7777493904eSAlan Cox 1); 77889ea39a7SAlan Cox return (npages); 77989ea39a7SAlan Cox } else if (i == npages) 78089ea39a7SAlan Cox return (npages); 78189ea39a7SAlan Cox } 78289ea39a7SAlan Cox } 78389ea39a7SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) { 78489ea39a7SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 78589ea39a7SAlan Cox alt = vm_phys_free_queues[domain][flind][pind]; 78689ea39a7SAlan Cox while ((m = TAILQ_FIRST(&alt[oind].pl)) != 78789ea39a7SAlan Cox NULL) { 78889ea39a7SAlan Cox vm_freelist_rem(alt, m, oind); 78989ea39a7SAlan Cox vm_phys_set_pool(pool, m, oind); 79089ea39a7SAlan Cox avail = 1 << oind; 79189ea39a7SAlan Cox need = imin(npages - i, avail); 79289ea39a7SAlan Cox for (end = i + need; i < end;) 79389ea39a7SAlan Cox ma[i++] = m++; 79489ea39a7SAlan Cox if (need < avail) { 7957493904eSAlan Cox /* 7967493904eSAlan Cox * Return excess pages to fl. 7977493904eSAlan Cox * Its order [0, oind) queues 7987493904eSAlan Cox * are empty. 7997493904eSAlan Cox */ 8007493904eSAlan Cox vm_phys_enq_range(m, avail - 8017493904eSAlan Cox need, fl, 1); 80289ea39a7SAlan Cox return (npages); 80389ea39a7SAlan Cox } else if (i == npages) 80489ea39a7SAlan Cox return (npages); 80589ea39a7SAlan Cox } 80689ea39a7SAlan Cox } 80789ea39a7SAlan Cox } 80889ea39a7SAlan Cox } 80989ea39a7SAlan Cox return (i); 81089ea39a7SAlan Cox } 81189ea39a7SAlan Cox 81289ea39a7SAlan Cox /* 81311752d88SAlan Cox * Allocate a contiguous, power of two-sized set of physical pages 81411752d88SAlan Cox * from the free lists. 8158941dc44SAlan Cox * 8168941dc44SAlan Cox * The free page queues must be locked. 81711752d88SAlan Cox */ 81811752d88SAlan Cox vm_page_t 819ef435ae7SJeff Roberson vm_phys_alloc_pages(int domain, int pool, int order) 82011752d88SAlan Cox { 82149ca10d4SJayachandran C. vm_page_t m; 8220db2102aSMichael Zhilin int freelist; 82349ca10d4SJayachandran C. 8240db2102aSMichael Zhilin for (freelist = 0; freelist < VM_NFREELIST; freelist++) { 8250db2102aSMichael Zhilin m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order); 82649ca10d4SJayachandran C. if (m != NULL) 82749ca10d4SJayachandran C. return (m); 82849ca10d4SJayachandran C. } 82949ca10d4SJayachandran C. return (NULL); 83049ca10d4SJayachandran C. } 83149ca10d4SJayachandran C. 83249ca10d4SJayachandran C. /* 833d866a563SAlan Cox * Allocate a contiguous, power of two-sized set of physical pages from the 834d866a563SAlan Cox * specified free list. The free list must be specified using one of the 835d866a563SAlan Cox * manifest constants VM_FREELIST_*. 836d866a563SAlan Cox * 837d866a563SAlan Cox * The free page queues must be locked. 83849ca10d4SJayachandran C. */ 83949ca10d4SJayachandran C. vm_page_t 8400db2102aSMichael Zhilin vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order) 84149ca10d4SJayachandran C. { 842ef435ae7SJeff Roberson struct vm_freelist *alt, *fl; 84311752d88SAlan Cox vm_page_t m; 8440db2102aSMichael Zhilin int oind, pind, flind; 84511752d88SAlan Cox 846ef435ae7SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 847ef435ae7SJeff Roberson ("vm_phys_alloc_freelist_pages: domain %d is out of range", 848ef435ae7SJeff Roberson domain)); 8490db2102aSMichael Zhilin KASSERT(freelist < VM_NFREELIST, 850d866a563SAlan Cox ("vm_phys_alloc_freelist_pages: freelist %d is out of range", 8515be93778SAndrew Turner freelist)); 85211752d88SAlan Cox KASSERT(pool < VM_NFREEPOOL, 85349ca10d4SJayachandran C. ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool)); 85411752d88SAlan Cox KASSERT(order < VM_NFREEORDER, 85549ca10d4SJayachandran C. ("vm_phys_alloc_freelist_pages: order %d is out of range", order)); 8566520495aSAdrian Chadd 8570db2102aSMichael Zhilin flind = vm_freelist_to_flind[freelist]; 8580db2102aSMichael Zhilin /* Check if freelist is present */ 8590db2102aSMichael Zhilin if (flind < 0) 8600db2102aSMichael Zhilin return (NULL); 8610db2102aSMichael Zhilin 862e2068d0bSJeff Roberson vm_domain_free_assert_locked(VM_DOMAIN(domain)); 8637e226537SAttilio Rao fl = &vm_phys_free_queues[domain][flind][pool][0]; 86411752d88SAlan Cox for (oind = order; oind < VM_NFREEORDER; oind++) { 86511752d88SAlan Cox m = TAILQ_FIRST(&fl[oind].pl); 86611752d88SAlan Cox if (m != NULL) { 8677e226537SAttilio Rao vm_freelist_rem(fl, m, oind); 868370a338aSAlan Cox /* The order [order, oind) queues are empty. */ 869370a338aSAlan Cox vm_phys_split_pages(m, oind, fl, order, 1); 87011752d88SAlan Cox return (m); 87111752d88SAlan Cox } 87211752d88SAlan Cox } 87311752d88SAlan Cox 87411752d88SAlan Cox /* 87511752d88SAlan Cox * The given pool was empty. Find the largest 87611752d88SAlan Cox * contiguous, power-of-two-sized set of pages in any 87711752d88SAlan Cox * pool. Transfer these pages to the given pool, and 87811752d88SAlan Cox * use them to satisfy the allocation. 87911752d88SAlan Cox */ 88011752d88SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= order; oind--) { 88111752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 8827e226537SAttilio Rao alt = &vm_phys_free_queues[domain][flind][pind][0]; 88311752d88SAlan Cox m = TAILQ_FIRST(&alt[oind].pl); 88411752d88SAlan Cox if (m != NULL) { 8857e226537SAttilio Rao vm_freelist_rem(alt, m, oind); 88611752d88SAlan Cox vm_phys_set_pool(pool, m, oind); 887370a338aSAlan Cox /* The order [order, oind) queues are empty. */ 888370a338aSAlan Cox vm_phys_split_pages(m, oind, fl, order, 1); 88911752d88SAlan Cox return (m); 89011752d88SAlan Cox } 89111752d88SAlan Cox } 89211752d88SAlan Cox } 89311752d88SAlan Cox return (NULL); 89411752d88SAlan Cox } 89511752d88SAlan Cox 89611752d88SAlan Cox /* 89711752d88SAlan Cox * Find the vm_page corresponding to the given physical address. 89811752d88SAlan Cox */ 89911752d88SAlan Cox vm_page_t 90011752d88SAlan Cox vm_phys_paddr_to_vm_page(vm_paddr_t pa) 90111752d88SAlan Cox { 90211752d88SAlan Cox struct vm_phys_seg *seg; 90311752d88SAlan Cox int segind; 90411752d88SAlan Cox 90511752d88SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 90611752d88SAlan Cox seg = &vm_phys_segs[segind]; 90711752d88SAlan Cox if (pa >= seg->start && pa < seg->end) 90811752d88SAlan Cox return (&seg->first_page[atop(pa - seg->start)]); 90911752d88SAlan Cox } 910f06a3a36SAndrew Thompson return (NULL); 91111752d88SAlan Cox } 91211752d88SAlan Cox 913b6de32bdSKonstantin Belousov vm_page_t 914b6de32bdSKonstantin Belousov vm_phys_fictitious_to_vm_page(vm_paddr_t pa) 915b6de32bdSKonstantin Belousov { 91638d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg tmp, *seg; 917b6de32bdSKonstantin Belousov vm_page_t m; 918b6de32bdSKonstantin Belousov 919b6de32bdSKonstantin Belousov m = NULL; 92038d6b2dcSRoger Pau Monné tmp.start = pa; 92138d6b2dcSRoger Pau Monné tmp.end = 0; 92238d6b2dcSRoger Pau Monné 92338d6b2dcSRoger Pau Monné rw_rlock(&vm_phys_fictitious_reg_lock); 92438d6b2dcSRoger Pau Monné seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp); 92538d6b2dcSRoger Pau Monné rw_runlock(&vm_phys_fictitious_reg_lock); 92638d6b2dcSRoger Pau Monné if (seg == NULL) 92738d6b2dcSRoger Pau Monné return (NULL); 92838d6b2dcSRoger Pau Monné 929b6de32bdSKonstantin Belousov m = &seg->first_page[atop(pa - seg->start)]; 93038d6b2dcSRoger Pau Monné KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m)); 93138d6b2dcSRoger Pau Monné 932b6de32bdSKonstantin Belousov return (m); 933b6de32bdSKonstantin Belousov } 934b6de32bdSKonstantin Belousov 9355ebe728dSRoger Pau Monné static inline void 9365ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start, 9375ebe728dSRoger Pau Monné long page_count, vm_memattr_t memattr) 9385ebe728dSRoger Pau Monné { 9395ebe728dSRoger Pau Monné long i; 9405ebe728dSRoger Pau Monné 941f93f7cf1SMark Johnston bzero(range, page_count * sizeof(*range)); 9425ebe728dSRoger Pau Monné for (i = 0; i < page_count; i++) { 9435ebe728dSRoger Pau Monné vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr); 9445ebe728dSRoger Pau Monné range[i].oflags &= ~VPO_UNMANAGED; 9455ebe728dSRoger Pau Monné range[i].busy_lock = VPB_UNBUSIED; 9465ebe728dSRoger Pau Monné } 9475ebe728dSRoger Pau Monné } 9485ebe728dSRoger Pau Monné 949b6de32bdSKonstantin Belousov int 950b6de32bdSKonstantin Belousov vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end, 951b6de32bdSKonstantin Belousov vm_memattr_t memattr) 952b6de32bdSKonstantin Belousov { 953b6de32bdSKonstantin Belousov struct vm_phys_fictitious_seg *seg; 954b6de32bdSKonstantin Belousov vm_page_t fp; 9555ebe728dSRoger Pau Monné long page_count; 956b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 9575ebe728dSRoger Pau Monné long pi, pe; 9585ebe728dSRoger Pau Monné long dpage_count; 959b6de32bdSKonstantin Belousov #endif 960b6de32bdSKonstantin Belousov 9615ebe728dSRoger Pau Monné KASSERT(start < end, 9625ebe728dSRoger Pau Monné ("Start of segment isn't less than end (start: %jx end: %jx)", 9635ebe728dSRoger Pau Monné (uintmax_t)start, (uintmax_t)end)); 9645ebe728dSRoger Pau Monné 965b6de32bdSKonstantin Belousov page_count = (end - start) / PAGE_SIZE; 966b6de32bdSKonstantin Belousov 967b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 968b6de32bdSKonstantin Belousov pi = atop(start); 9695ebe728dSRoger Pau Monné pe = atop(end); 9705ebe728dSRoger Pau Monné if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 971b6de32bdSKonstantin Belousov fp = &vm_page_array[pi - first_page]; 9725ebe728dSRoger Pau Monné if ((pe - first_page) > vm_page_array_size) { 9735ebe728dSRoger Pau Monné /* 9745ebe728dSRoger Pau Monné * We have a segment that starts inside 9755ebe728dSRoger Pau Monné * of vm_page_array, but ends outside of it. 9765ebe728dSRoger Pau Monné * 9775ebe728dSRoger Pau Monné * Use vm_page_array pages for those that are 9785ebe728dSRoger Pau Monné * inside of the vm_page_array range, and 9795ebe728dSRoger Pau Monné * allocate the remaining ones. 9805ebe728dSRoger Pau Monné */ 9815ebe728dSRoger Pau Monné dpage_count = vm_page_array_size - (pi - first_page); 9825ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, start, dpage_count, 9835ebe728dSRoger Pau Monné memattr); 9845ebe728dSRoger Pau Monné page_count -= dpage_count; 9855ebe728dSRoger Pau Monné start += ptoa(dpage_count); 9865ebe728dSRoger Pau Monné goto alloc; 9875ebe728dSRoger Pau Monné } 9885ebe728dSRoger Pau Monné /* 9895ebe728dSRoger Pau Monné * We can allocate the full range from vm_page_array, 9905ebe728dSRoger Pau Monné * so there's no need to register the range in the tree. 9915ebe728dSRoger Pau Monné */ 9925ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, start, page_count, memattr); 9935ebe728dSRoger Pau Monné return (0); 9945ebe728dSRoger Pau Monné } else if (pe > first_page && (pe - first_page) < vm_page_array_size) { 9955ebe728dSRoger Pau Monné /* 9965ebe728dSRoger Pau Monné * We have a segment that ends inside of vm_page_array, 9975ebe728dSRoger Pau Monné * but starts outside of it. 9985ebe728dSRoger Pau Monné */ 9995ebe728dSRoger Pau Monné fp = &vm_page_array[0]; 10005ebe728dSRoger Pau Monné dpage_count = pe - first_page; 10015ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count, 10025ebe728dSRoger Pau Monné memattr); 10035ebe728dSRoger Pau Monné end -= ptoa(dpage_count); 10045ebe728dSRoger Pau Monné page_count -= dpage_count; 10055ebe728dSRoger Pau Monné goto alloc; 10065ebe728dSRoger Pau Monné } else if (pi < first_page && pe > (first_page + vm_page_array_size)) { 10075ebe728dSRoger Pau Monné /* 10085ebe728dSRoger Pau Monné * Trying to register a fictitious range that expands before 10095ebe728dSRoger Pau Monné * and after vm_page_array. 10105ebe728dSRoger Pau Monné */ 10115ebe728dSRoger Pau Monné return (EINVAL); 10125ebe728dSRoger Pau Monné } else { 10135ebe728dSRoger Pau Monné alloc: 1014b6de32bdSKonstantin Belousov #endif 1015b6de32bdSKonstantin Belousov fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES, 1016f93f7cf1SMark Johnston M_WAITOK); 10175ebe728dSRoger Pau Monné #ifdef VM_PHYSSEG_DENSE 1018b6de32bdSKonstantin Belousov } 10195ebe728dSRoger Pau Monné #endif 10205ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, start, page_count, memattr); 102138d6b2dcSRoger Pau Monné 102238d6b2dcSRoger Pau Monné seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO); 1023b6de32bdSKonstantin Belousov seg->start = start; 1024b6de32bdSKonstantin Belousov seg->end = end; 1025b6de32bdSKonstantin Belousov seg->first_page = fp; 102638d6b2dcSRoger Pau Monné 102738d6b2dcSRoger Pau Monné rw_wlock(&vm_phys_fictitious_reg_lock); 102838d6b2dcSRoger Pau Monné RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg); 102938d6b2dcSRoger Pau Monné rw_wunlock(&vm_phys_fictitious_reg_lock); 103038d6b2dcSRoger Pau Monné 1031b6de32bdSKonstantin Belousov return (0); 1032b6de32bdSKonstantin Belousov } 1033b6de32bdSKonstantin Belousov 1034b6de32bdSKonstantin Belousov void 1035b6de32bdSKonstantin Belousov vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end) 1036b6de32bdSKonstantin Belousov { 103738d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *seg, tmp; 1038b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 10395ebe728dSRoger Pau Monné long pi, pe; 1040b6de32bdSKonstantin Belousov #endif 1041b6de32bdSKonstantin Belousov 10425ebe728dSRoger Pau Monné KASSERT(start < end, 10435ebe728dSRoger Pau Monné ("Start of segment isn't less than end (start: %jx end: %jx)", 10445ebe728dSRoger Pau Monné (uintmax_t)start, (uintmax_t)end)); 10455ebe728dSRoger Pau Monné 1046b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 1047b6de32bdSKonstantin Belousov pi = atop(start); 10485ebe728dSRoger Pau Monné pe = atop(end); 10495ebe728dSRoger Pau Monné if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 10505ebe728dSRoger Pau Monné if ((pe - first_page) <= vm_page_array_size) { 10515ebe728dSRoger Pau Monné /* 10525ebe728dSRoger Pau Monné * This segment was allocated using vm_page_array 10535ebe728dSRoger Pau Monné * only, there's nothing to do since those pages 10545ebe728dSRoger Pau Monné * were never added to the tree. 10555ebe728dSRoger Pau Monné */ 10565ebe728dSRoger Pau Monné return; 10575ebe728dSRoger Pau Monné } 10585ebe728dSRoger Pau Monné /* 10595ebe728dSRoger Pau Monné * We have a segment that starts inside 10605ebe728dSRoger Pau Monné * of vm_page_array, but ends outside of it. 10615ebe728dSRoger Pau Monné * 10625ebe728dSRoger Pau Monné * Calculate how many pages were added to the 10635ebe728dSRoger Pau Monné * tree and free them. 10645ebe728dSRoger Pau Monné */ 10655ebe728dSRoger Pau Monné start = ptoa(first_page + vm_page_array_size); 10665ebe728dSRoger Pau Monné } else if (pe > first_page && (pe - first_page) < vm_page_array_size) { 10675ebe728dSRoger Pau Monné /* 10685ebe728dSRoger Pau Monné * We have a segment that ends inside of vm_page_array, 10695ebe728dSRoger Pau Monné * but starts outside of it. 10705ebe728dSRoger Pau Monné */ 10715ebe728dSRoger Pau Monné end = ptoa(first_page); 10725ebe728dSRoger Pau Monné } else if (pi < first_page && pe > (first_page + vm_page_array_size)) { 10735ebe728dSRoger Pau Monné /* Since it's not possible to register such a range, panic. */ 10745ebe728dSRoger Pau Monné panic( 10755ebe728dSRoger Pau Monné "Unregistering not registered fictitious range [%#jx:%#jx]", 10765ebe728dSRoger Pau Monné (uintmax_t)start, (uintmax_t)end); 10775ebe728dSRoger Pau Monné } 1078b6de32bdSKonstantin Belousov #endif 107938d6b2dcSRoger Pau Monné tmp.start = start; 108038d6b2dcSRoger Pau Monné tmp.end = 0; 1081b6de32bdSKonstantin Belousov 108238d6b2dcSRoger Pau Monné rw_wlock(&vm_phys_fictitious_reg_lock); 108338d6b2dcSRoger Pau Monné seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp); 108438d6b2dcSRoger Pau Monné if (seg->start != start || seg->end != end) { 108538d6b2dcSRoger Pau Monné rw_wunlock(&vm_phys_fictitious_reg_lock); 108638d6b2dcSRoger Pau Monné panic( 108738d6b2dcSRoger Pau Monné "Unregistering not registered fictitious range [%#jx:%#jx]", 108838d6b2dcSRoger Pau Monné (uintmax_t)start, (uintmax_t)end); 108938d6b2dcSRoger Pau Monné } 109038d6b2dcSRoger Pau Monné RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg); 109138d6b2dcSRoger Pau Monné rw_wunlock(&vm_phys_fictitious_reg_lock); 109238d6b2dcSRoger Pau Monné free(seg->first_page, M_FICT_PAGES); 109338d6b2dcSRoger Pau Monné free(seg, M_FICT_PAGES); 1094b6de32bdSKonstantin Belousov } 1095b6de32bdSKonstantin Belousov 109611752d88SAlan Cox /* 109711752d88SAlan Cox * Free a contiguous, power of two-sized set of physical pages. 10988941dc44SAlan Cox * 10998941dc44SAlan Cox * The free page queues must be locked. 110011752d88SAlan Cox */ 110111752d88SAlan Cox void 110211752d88SAlan Cox vm_phys_free_pages(vm_page_t m, int order) 110311752d88SAlan Cox { 110411752d88SAlan Cox struct vm_freelist *fl; 110511752d88SAlan Cox struct vm_phys_seg *seg; 11065c1f2cc4SAlan Cox vm_paddr_t pa; 110711752d88SAlan Cox vm_page_t m_buddy; 110811752d88SAlan Cox 110911752d88SAlan Cox KASSERT(m->order == VM_NFREEORDER, 11103921068fSJeff Roberson ("vm_phys_free_pages: page %p has unexpected order %d", 11113921068fSJeff Roberson m, m->order)); 111211752d88SAlan Cox KASSERT(m->pool < VM_NFREEPOOL, 11138941dc44SAlan Cox ("vm_phys_free_pages: page %p has unexpected pool %d", 111411752d88SAlan Cox m, m->pool)); 111511752d88SAlan Cox KASSERT(order < VM_NFREEORDER, 11168941dc44SAlan Cox ("vm_phys_free_pages: order %d is out of range", order)); 111711752d88SAlan Cox seg = &vm_phys_segs[m->segind]; 1118e2068d0bSJeff Roberson vm_domain_free_assert_locked(VM_DOMAIN(seg->domain)); 11195c1f2cc4SAlan Cox if (order < VM_NFREEORDER - 1) { 11205c1f2cc4SAlan Cox pa = VM_PAGE_TO_PHYS(m); 11215c1f2cc4SAlan Cox do { 11225c1f2cc4SAlan Cox pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order)); 11235c1f2cc4SAlan Cox if (pa < seg->start || pa >= seg->end) 112411752d88SAlan Cox break; 11255c1f2cc4SAlan Cox m_buddy = &seg->first_page[atop(pa - seg->start)]; 112611752d88SAlan Cox if (m_buddy->order != order) 112711752d88SAlan Cox break; 112811752d88SAlan Cox fl = (*seg->free_queues)[m_buddy->pool]; 11297e226537SAttilio Rao vm_freelist_rem(fl, m_buddy, order); 113011752d88SAlan Cox if (m_buddy->pool != m->pool) 113111752d88SAlan Cox vm_phys_set_pool(m->pool, m_buddy, order); 113211752d88SAlan Cox order++; 11335c1f2cc4SAlan Cox pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1); 113411752d88SAlan Cox m = &seg->first_page[atop(pa - seg->start)]; 11355c1f2cc4SAlan Cox } while (order < VM_NFREEORDER - 1); 113611752d88SAlan Cox } 113711752d88SAlan Cox fl = (*seg->free_queues)[m->pool]; 11387e226537SAttilio Rao vm_freelist_add(fl, m, order, 1); 113911752d88SAlan Cox } 114011752d88SAlan Cox 114111752d88SAlan Cox /* 1142b8590daeSDoug Moore * Return the largest possible order of a set of pages starting at m. 11435c1f2cc4SAlan Cox */ 1144b8590daeSDoug Moore static int 1145b8590daeSDoug Moore max_order(vm_page_t m) 11465c1f2cc4SAlan Cox { 11475c1f2cc4SAlan Cox 11485c1f2cc4SAlan Cox /* 11495c1f2cc4SAlan Cox * Unsigned "min" is used here so that "order" is assigned 11505c1f2cc4SAlan Cox * "VM_NFREEORDER - 1" when "m"'s physical address is zero 11515c1f2cc4SAlan Cox * or the low-order bits of its physical address are zero 11525c1f2cc4SAlan Cox * because the size of a physical address exceeds the size of 11535c1f2cc4SAlan Cox * a long. 11545c1f2cc4SAlan Cox */ 1155b8590daeSDoug Moore return (min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1, 1156b8590daeSDoug Moore VM_NFREEORDER - 1)); 11575c1f2cc4SAlan Cox } 1158b8590daeSDoug Moore 1159b8590daeSDoug Moore /* 1160b8590daeSDoug Moore * Free a contiguous, arbitrarily sized set of physical pages, without 1161b8590daeSDoug Moore * merging across set boundaries. 1162b8590daeSDoug Moore * 1163b8590daeSDoug Moore * The free page queues must be locked. 1164b8590daeSDoug Moore */ 1165b8590daeSDoug Moore void 1166b8590daeSDoug Moore vm_phys_enqueue_contig(vm_page_t m, u_long npages) 1167b8590daeSDoug Moore { 1168b8590daeSDoug Moore struct vm_freelist *fl; 1169b8590daeSDoug Moore struct vm_phys_seg *seg; 1170b8590daeSDoug Moore vm_page_t m_end; 1171b8590daeSDoug Moore int order; 1172b8590daeSDoug Moore 1173b8590daeSDoug Moore /* 1174b8590daeSDoug Moore * Avoid unnecessary coalescing by freeing the pages in the largest 1175b8590daeSDoug Moore * possible power-of-two-sized subsets. 1176b8590daeSDoug Moore */ 1177b8590daeSDoug Moore vm_domain_free_assert_locked(vm_pagequeue_domain(m)); 1178b8590daeSDoug Moore seg = &vm_phys_segs[m->segind]; 1179b8590daeSDoug Moore fl = (*seg->free_queues)[m->pool]; 1180b8590daeSDoug Moore m_end = m + npages; 1181b8590daeSDoug Moore /* Free blocks of increasing size. */ 1182b8590daeSDoug Moore while ((order = max_order(m)) < VM_NFREEORDER - 1 && 1183b8590daeSDoug Moore m + (1 << order) <= m_end) { 1184b8590daeSDoug Moore KASSERT(seg == &vm_phys_segs[m->segind], 1185b8590daeSDoug Moore ("%s: page range [%p,%p) spans multiple segments", 1186b8590daeSDoug Moore __func__, m_end - npages, m)); 1187b8590daeSDoug Moore vm_freelist_add(fl, m, order, 1); 1188b8590daeSDoug Moore m += 1 << order; 11895c1f2cc4SAlan Cox } 1190b8590daeSDoug Moore /* Free blocks of maximum size. */ 1191b8590daeSDoug Moore while (m + (1 << order) <= m_end) { 1192b8590daeSDoug Moore KASSERT(seg == &vm_phys_segs[m->segind], 1193b8590daeSDoug Moore ("%s: page range [%p,%p) spans multiple segments", 1194b8590daeSDoug Moore __func__, m_end - npages, m)); 1195b8590daeSDoug Moore vm_freelist_add(fl, m, order, 1); 1196b8590daeSDoug Moore m += 1 << order; 1197b8590daeSDoug Moore } 1198b8590daeSDoug Moore /* Free blocks of diminishing size. */ 1199b8590daeSDoug Moore while (m < m_end) { 1200b8590daeSDoug Moore KASSERT(seg == &vm_phys_segs[m->segind], 1201b8590daeSDoug Moore ("%s: page range [%p,%p) spans multiple segments", 1202b8590daeSDoug Moore __func__, m_end - npages, m)); 1203b8590daeSDoug Moore order = flsl(m_end - m) - 1; 1204b8590daeSDoug Moore vm_freelist_add(fl, m, order, 1); 1205b8590daeSDoug Moore m += 1 << order; 1206b8590daeSDoug Moore } 1207b8590daeSDoug Moore } 1208b8590daeSDoug Moore 1209b8590daeSDoug Moore /* 1210b8590daeSDoug Moore * Free a contiguous, arbitrarily sized set of physical pages. 1211b8590daeSDoug Moore * 1212b8590daeSDoug Moore * The free page queues must be locked. 1213b8590daeSDoug Moore */ 1214b8590daeSDoug Moore void 1215b8590daeSDoug Moore vm_phys_free_contig(vm_page_t m, u_long npages) 1216b8590daeSDoug Moore { 1217b8590daeSDoug Moore int order_start, order_end; 1218b8590daeSDoug Moore vm_page_t m_start, m_end; 1219b8590daeSDoug Moore 1220b8590daeSDoug Moore vm_domain_free_assert_locked(vm_pagequeue_domain(m)); 1221b8590daeSDoug Moore 1222b8590daeSDoug Moore m_start = m; 1223b8590daeSDoug Moore order_start = max_order(m_start); 1224b8590daeSDoug Moore if (order_start < VM_NFREEORDER - 1) 1225b8590daeSDoug Moore m_start += 1 << order_start; 1226b8590daeSDoug Moore m_end = m + npages; 1227b8590daeSDoug Moore order_end = max_order(m_end); 1228b8590daeSDoug Moore if (order_end < VM_NFREEORDER - 1) 1229b8590daeSDoug Moore m_end -= 1 << order_end; 1230b8590daeSDoug Moore /* 1231b8590daeSDoug Moore * Avoid unnecessary coalescing by freeing the pages at the start and 1232b8590daeSDoug Moore * end of the range last. 1233b8590daeSDoug Moore */ 1234b8590daeSDoug Moore if (m_start < m_end) 1235b8590daeSDoug Moore vm_phys_enqueue_contig(m_start, m_end - m_start); 1236b8590daeSDoug Moore if (order_start < VM_NFREEORDER - 1) 1237b8590daeSDoug Moore vm_phys_free_pages(m, order_start); 1238b8590daeSDoug Moore if (order_end < VM_NFREEORDER - 1) 1239b8590daeSDoug Moore vm_phys_free_pages(m_end, order_end); 12405c1f2cc4SAlan Cox } 12415c1f2cc4SAlan Cox 12425c1f2cc4SAlan Cox /* 1243c869e672SAlan Cox * Scan physical memory between the specified addresses "low" and "high" for a 1244c869e672SAlan Cox * run of contiguous physical pages that satisfy the specified conditions, and 1245c869e672SAlan Cox * return the lowest page in the run. The specified "alignment" determines 1246c869e672SAlan Cox * the alignment of the lowest physical page in the run. If the specified 1247c869e672SAlan Cox * "boundary" is non-zero, then the run of physical pages cannot span a 1248c869e672SAlan Cox * physical address that is a multiple of "boundary". 1249c869e672SAlan Cox * 1250c869e672SAlan Cox * "npages" must be greater than zero. Both "alignment" and "boundary" must 1251c869e672SAlan Cox * be a power of two. 1252c869e672SAlan Cox */ 1253c869e672SAlan Cox vm_page_t 12543f289c3fSJeff Roberson vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high, 1255c869e672SAlan Cox u_long alignment, vm_paddr_t boundary, int options) 1256c869e672SAlan Cox { 1257c869e672SAlan Cox vm_paddr_t pa_end; 1258c869e672SAlan Cox vm_page_t m_end, m_run, m_start; 1259c869e672SAlan Cox struct vm_phys_seg *seg; 1260c869e672SAlan Cox int segind; 1261c869e672SAlan Cox 1262c869e672SAlan Cox KASSERT(npages > 0, ("npages is 0")); 1263c869e672SAlan Cox KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 1264c869e672SAlan Cox KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1265c869e672SAlan Cox if (low >= high) 1266c869e672SAlan Cox return (NULL); 1267c869e672SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 1268c869e672SAlan Cox seg = &vm_phys_segs[segind]; 12693f289c3fSJeff Roberson if (seg->domain != domain) 12703f289c3fSJeff Roberson continue; 1271c869e672SAlan Cox if (seg->start >= high) 1272c869e672SAlan Cox break; 1273c869e672SAlan Cox if (low >= seg->end) 1274c869e672SAlan Cox continue; 1275c869e672SAlan Cox if (low <= seg->start) 1276c869e672SAlan Cox m_start = seg->first_page; 1277c869e672SAlan Cox else 1278c869e672SAlan Cox m_start = &seg->first_page[atop(low - seg->start)]; 1279c869e672SAlan Cox if (high < seg->end) 1280c869e672SAlan Cox pa_end = high; 1281c869e672SAlan Cox else 1282c869e672SAlan Cox pa_end = seg->end; 1283c869e672SAlan Cox if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages)) 1284c869e672SAlan Cox continue; 1285c869e672SAlan Cox m_end = &seg->first_page[atop(pa_end - seg->start)]; 1286c869e672SAlan Cox m_run = vm_page_scan_contig(npages, m_start, m_end, 1287c869e672SAlan Cox alignment, boundary, options); 1288c869e672SAlan Cox if (m_run != NULL) 1289c869e672SAlan Cox return (m_run); 1290c869e672SAlan Cox } 1291c869e672SAlan Cox return (NULL); 1292c869e672SAlan Cox } 1293c869e672SAlan Cox 1294c869e672SAlan Cox /* 129511752d88SAlan Cox * Set the pool for a contiguous, power of two-sized set of physical pages. 129611752d88SAlan Cox */ 12977bfda801SAlan Cox void 129811752d88SAlan Cox vm_phys_set_pool(int pool, vm_page_t m, int order) 129911752d88SAlan Cox { 130011752d88SAlan Cox vm_page_t m_tmp; 130111752d88SAlan Cox 130211752d88SAlan Cox for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++) 130311752d88SAlan Cox m_tmp->pool = pool; 130411752d88SAlan Cox } 130511752d88SAlan Cox 130611752d88SAlan Cox /* 13079742373aSAlan Cox * Search for the given physical page "m" in the free lists. If the search 13089742373aSAlan Cox * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return 13099742373aSAlan Cox * FALSE, indicating that "m" is not in the free lists. 13107bfda801SAlan Cox * 13117bfda801SAlan Cox * The free page queues must be locked. 13127bfda801SAlan Cox */ 1313e35395ceSAlan Cox boolean_t 13147bfda801SAlan Cox vm_phys_unfree_page(vm_page_t m) 13157bfda801SAlan Cox { 13167bfda801SAlan Cox struct vm_freelist *fl; 13177bfda801SAlan Cox struct vm_phys_seg *seg; 13187bfda801SAlan Cox vm_paddr_t pa, pa_half; 13197bfda801SAlan Cox vm_page_t m_set, m_tmp; 13207bfda801SAlan Cox int order; 13217bfda801SAlan Cox 13227bfda801SAlan Cox /* 13237bfda801SAlan Cox * First, find the contiguous, power of two-sized set of free 13247bfda801SAlan Cox * physical pages containing the given physical page "m" and 13257bfda801SAlan Cox * assign it to "m_set". 13267bfda801SAlan Cox */ 13277bfda801SAlan Cox seg = &vm_phys_segs[m->segind]; 1328e2068d0bSJeff Roberson vm_domain_free_assert_locked(VM_DOMAIN(seg->domain)); 13297bfda801SAlan Cox for (m_set = m, order = 0; m_set->order == VM_NFREEORDER && 1330bc8794a1SAlan Cox order < VM_NFREEORDER - 1; ) { 13317bfda801SAlan Cox order++; 13327bfda801SAlan Cox pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order)); 13332fbced65SAlan Cox if (pa >= seg->start) 13347bfda801SAlan Cox m_set = &seg->first_page[atop(pa - seg->start)]; 1335e35395ceSAlan Cox else 1336e35395ceSAlan Cox return (FALSE); 13377bfda801SAlan Cox } 1338e35395ceSAlan Cox if (m_set->order < order) 1339e35395ceSAlan Cox return (FALSE); 1340e35395ceSAlan Cox if (m_set->order == VM_NFREEORDER) 1341e35395ceSAlan Cox return (FALSE); 13427bfda801SAlan Cox KASSERT(m_set->order < VM_NFREEORDER, 13437bfda801SAlan Cox ("vm_phys_unfree_page: page %p has unexpected order %d", 13447bfda801SAlan Cox m_set, m_set->order)); 13457bfda801SAlan Cox 13467bfda801SAlan Cox /* 13477bfda801SAlan Cox * Next, remove "m_set" from the free lists. Finally, extract 13487bfda801SAlan Cox * "m" from "m_set" using an iterative algorithm: While "m_set" 13497bfda801SAlan Cox * is larger than a page, shrink "m_set" by returning the half 13507bfda801SAlan Cox * of "m_set" that does not contain "m" to the free lists. 13517bfda801SAlan Cox */ 13527bfda801SAlan Cox fl = (*seg->free_queues)[m_set->pool]; 13537bfda801SAlan Cox order = m_set->order; 13547e226537SAttilio Rao vm_freelist_rem(fl, m_set, order); 13557bfda801SAlan Cox while (order > 0) { 13567bfda801SAlan Cox order--; 13577bfda801SAlan Cox pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order)); 13587bfda801SAlan Cox if (m->phys_addr < pa_half) 13597bfda801SAlan Cox m_tmp = &seg->first_page[atop(pa_half - seg->start)]; 13607bfda801SAlan Cox else { 13617bfda801SAlan Cox m_tmp = m_set; 13627bfda801SAlan Cox m_set = &seg->first_page[atop(pa_half - seg->start)]; 13637bfda801SAlan Cox } 13647e226537SAttilio Rao vm_freelist_add(fl, m_tmp, order, 0); 13657bfda801SAlan Cox } 13667bfda801SAlan Cox KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency")); 1367e35395ceSAlan Cox return (TRUE); 13687bfda801SAlan Cox } 13697bfda801SAlan Cox 13707bfda801SAlan Cox /* 13712f9f48d6SAlan Cox * Allocate a contiguous set of physical pages of the given size 13722f9f48d6SAlan Cox * "npages" from the free lists. All of the physical pages must be at 13732f9f48d6SAlan Cox * or above the given physical address "low" and below the given 13742f9f48d6SAlan Cox * physical address "high". The given value "alignment" determines the 13752f9f48d6SAlan Cox * alignment of the first physical page in the set. If the given value 13762f9f48d6SAlan Cox * "boundary" is non-zero, then the set of physical pages cannot cross 13772f9f48d6SAlan Cox * any physical address boundary that is a multiple of that value. Both 137811752d88SAlan Cox * "alignment" and "boundary" must be a power of two. 137911752d88SAlan Cox */ 138011752d88SAlan Cox vm_page_t 1381ef435ae7SJeff Roberson vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high, 13825c1f2cc4SAlan Cox u_long alignment, vm_paddr_t boundary) 138311752d88SAlan Cox { 1384c869e672SAlan Cox vm_paddr_t pa_end, pa_start; 1385c869e672SAlan Cox vm_page_t m_run; 1386c869e672SAlan Cox struct vm_phys_seg *seg; 1387ef435ae7SJeff Roberson int segind; 138811752d88SAlan Cox 1389c869e672SAlan Cox KASSERT(npages > 0, ("npages is 0")); 1390c869e672SAlan Cox KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 1391c869e672SAlan Cox KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1392e2068d0bSJeff Roberson vm_domain_free_assert_locked(VM_DOMAIN(domain)); 1393c869e672SAlan Cox if (low >= high) 1394c869e672SAlan Cox return (NULL); 1395c869e672SAlan Cox m_run = NULL; 1396477bffbeSAlan Cox for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) { 1397c869e672SAlan Cox seg = &vm_phys_segs[segind]; 1398477bffbeSAlan Cox if (seg->start >= high || seg->domain != domain) 139911752d88SAlan Cox continue; 1400477bffbeSAlan Cox if (low >= seg->end) 1401477bffbeSAlan Cox break; 1402c869e672SAlan Cox if (low <= seg->start) 1403c869e672SAlan Cox pa_start = seg->start; 1404c869e672SAlan Cox else 1405c869e672SAlan Cox pa_start = low; 1406c869e672SAlan Cox if (high < seg->end) 1407c869e672SAlan Cox pa_end = high; 1408c869e672SAlan Cox else 1409c869e672SAlan Cox pa_end = seg->end; 1410c869e672SAlan Cox if (pa_end - pa_start < ptoa(npages)) 1411c869e672SAlan Cox continue; 1412c869e672SAlan Cox m_run = vm_phys_alloc_seg_contig(seg, npages, low, high, 1413c869e672SAlan Cox alignment, boundary); 1414c869e672SAlan Cox if (m_run != NULL) 1415c869e672SAlan Cox break; 1416c869e672SAlan Cox } 1417c869e672SAlan Cox return (m_run); 1418c869e672SAlan Cox } 141911752d88SAlan Cox 142011752d88SAlan Cox /* 1421c869e672SAlan Cox * Allocate a run of contiguous physical pages from the free list for the 1422c869e672SAlan Cox * specified segment. 1423c869e672SAlan Cox */ 1424c869e672SAlan Cox static vm_page_t 1425c869e672SAlan Cox vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages, 1426c869e672SAlan Cox vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 1427c869e672SAlan Cox { 1428c869e672SAlan Cox struct vm_freelist *fl; 1429c869e672SAlan Cox vm_paddr_t pa, pa_end, size; 1430c869e672SAlan Cox vm_page_t m, m_ret; 1431c869e672SAlan Cox u_long npages_end; 1432c869e672SAlan Cox int oind, order, pind; 1433c869e672SAlan Cox 1434c869e672SAlan Cox KASSERT(npages > 0, ("npages is 0")); 1435c869e672SAlan Cox KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 1436c869e672SAlan Cox KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1437e2068d0bSJeff Roberson vm_domain_free_assert_locked(VM_DOMAIN(seg->domain)); 1438c869e672SAlan Cox /* Compute the queue that is the best fit for npages. */ 14399161b4deSAlan Cox order = flsl(npages - 1); 1440c869e672SAlan Cox /* Search for a run satisfying the specified conditions. */ 1441c869e672SAlan Cox size = npages << PAGE_SHIFT; 1442c869e672SAlan Cox for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; 1443c869e672SAlan Cox oind++) { 1444c869e672SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 1445c869e672SAlan Cox fl = (*seg->free_queues)[pind]; 14465cd29d0fSMark Johnston TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) { 1447c869e672SAlan Cox /* 144811752d88SAlan Cox * Is the size of this allocation request 144911752d88SAlan Cox * larger than the largest block size? 145011752d88SAlan Cox */ 145111752d88SAlan Cox if (order >= VM_NFREEORDER) { 145211752d88SAlan Cox /* 1453c869e672SAlan Cox * Determine if a sufficient number of 1454c869e672SAlan Cox * subsequent blocks to satisfy the 1455c869e672SAlan Cox * allocation request are free. 145611752d88SAlan Cox */ 145711752d88SAlan Cox pa = VM_PAGE_TO_PHYS(m_ret); 1458c869e672SAlan Cox pa_end = pa + size; 145979e9552eSKonstantin Belousov if (pa_end < pa) 146079e9552eSKonstantin Belousov continue; 146111752d88SAlan Cox for (;;) { 1462c869e672SAlan Cox pa += 1 << (PAGE_SHIFT + 1463c869e672SAlan Cox VM_NFREEORDER - 1); 1464c869e672SAlan Cox if (pa >= pa_end || 1465c869e672SAlan Cox pa < seg->start || 146611752d88SAlan Cox pa >= seg->end) 146711752d88SAlan Cox break; 1468c869e672SAlan Cox m = &seg->first_page[atop(pa - 1469c869e672SAlan Cox seg->start)]; 1470c869e672SAlan Cox if (m->order != VM_NFREEORDER - 1471c869e672SAlan Cox 1) 147211752d88SAlan Cox break; 147311752d88SAlan Cox } 1474c869e672SAlan Cox /* If not, go to the next block. */ 1475c869e672SAlan Cox if (pa < pa_end) 147611752d88SAlan Cox continue; 147711752d88SAlan Cox } 147811752d88SAlan Cox 147911752d88SAlan Cox /* 1480c869e672SAlan Cox * Determine if the blocks are within the 1481c869e672SAlan Cox * given range, satisfy the given alignment, 1482c869e672SAlan Cox * and do not cross the given boundary. 148311752d88SAlan Cox */ 148411752d88SAlan Cox pa = VM_PAGE_TO_PHYS(m_ret); 1485c869e672SAlan Cox pa_end = pa + size; 1486d9c9c81cSPedro F. Giffuni if (pa >= low && pa_end <= high && 1487d9c9c81cSPedro F. Giffuni (pa & (alignment - 1)) == 0 && 1488d9c9c81cSPedro F. Giffuni rounddown2(pa ^ (pa_end - 1), boundary) == 0) 148911752d88SAlan Cox goto done; 149011752d88SAlan Cox } 149111752d88SAlan Cox } 149211752d88SAlan Cox } 149311752d88SAlan Cox return (NULL); 149411752d88SAlan Cox done: 149511752d88SAlan Cox for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) { 149611752d88SAlan Cox fl = (*seg->free_queues)[m->pool]; 14979161b4deSAlan Cox vm_freelist_rem(fl, m, oind); 14989161b4deSAlan Cox if (m->pool != VM_FREEPOOL_DEFAULT) 14999161b4deSAlan Cox vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, oind); 150011752d88SAlan Cox } 15015c1f2cc4SAlan Cox /* Return excess pages to the free lists. */ 15029161b4deSAlan Cox npages_end = roundup2(npages, 1 << oind); 15037493904eSAlan Cox if (npages < npages_end) { 15047493904eSAlan Cox fl = (*seg->free_queues)[VM_FREEPOOL_DEFAULT]; 15057493904eSAlan Cox vm_phys_enq_range(&m_ret[npages], npages_end - npages, fl, 0); 15067493904eSAlan Cox } 150711752d88SAlan Cox return (m_ret); 150811752d88SAlan Cox } 150911752d88SAlan Cox 1510b7565d44SJeff Roberson /* 1511b7565d44SJeff Roberson * Return the index of the first unused slot which may be the terminating 1512b7565d44SJeff Roberson * entry. 1513b7565d44SJeff Roberson */ 1514b7565d44SJeff Roberson static int 1515b7565d44SJeff Roberson vm_phys_avail_count(void) 1516b7565d44SJeff Roberson { 1517b7565d44SJeff Roberson int i; 1518b7565d44SJeff Roberson 1519b7565d44SJeff Roberson for (i = 0; phys_avail[i + 1]; i += 2) 1520b7565d44SJeff Roberson continue; 1521b7565d44SJeff Roberson if (i > PHYS_AVAIL_ENTRIES) 1522b7565d44SJeff Roberson panic("Improperly terminated phys_avail %d entries", i); 1523b7565d44SJeff Roberson 1524b7565d44SJeff Roberson return (i); 1525b7565d44SJeff Roberson } 1526b7565d44SJeff Roberson 1527b7565d44SJeff Roberson /* 1528b7565d44SJeff Roberson * Assert that a phys_avail entry is valid. 1529b7565d44SJeff Roberson */ 1530b7565d44SJeff Roberson static void 1531b7565d44SJeff Roberson vm_phys_avail_check(int i) 1532b7565d44SJeff Roberson { 1533b7565d44SJeff Roberson if (phys_avail[i] & PAGE_MASK) 1534b7565d44SJeff Roberson panic("Unaligned phys_avail[%d]: %#jx", i, 1535b7565d44SJeff Roberson (intmax_t)phys_avail[i]); 1536b7565d44SJeff Roberson if (phys_avail[i+1] & PAGE_MASK) 1537b7565d44SJeff Roberson panic("Unaligned phys_avail[%d + 1]: %#jx", i, 1538b7565d44SJeff Roberson (intmax_t)phys_avail[i]); 1539b7565d44SJeff Roberson if (phys_avail[i + 1] < phys_avail[i]) 1540b7565d44SJeff Roberson panic("phys_avail[%d] start %#jx < end %#jx", i, 1541b7565d44SJeff Roberson (intmax_t)phys_avail[i], (intmax_t)phys_avail[i+1]); 1542b7565d44SJeff Roberson } 1543b7565d44SJeff Roberson 1544b7565d44SJeff Roberson /* 1545b7565d44SJeff Roberson * Return the index of an overlapping phys_avail entry or -1. 1546b7565d44SJeff Roberson */ 1547be3f5f29SJeff Roberson #ifdef NUMA 1548b7565d44SJeff Roberson static int 1549b7565d44SJeff Roberson vm_phys_avail_find(vm_paddr_t pa) 1550b7565d44SJeff Roberson { 1551b7565d44SJeff Roberson int i; 1552b7565d44SJeff Roberson 1553b7565d44SJeff Roberson for (i = 0; phys_avail[i + 1]; i += 2) 1554b7565d44SJeff Roberson if (phys_avail[i] <= pa && phys_avail[i + 1] > pa) 1555b7565d44SJeff Roberson return (i); 1556b7565d44SJeff Roberson return (-1); 1557b7565d44SJeff Roberson } 1558be3f5f29SJeff Roberson #endif 1559b7565d44SJeff Roberson 1560b7565d44SJeff Roberson /* 1561b7565d44SJeff Roberson * Return the index of the largest entry. 1562b7565d44SJeff Roberson */ 1563b7565d44SJeff Roberson int 1564b7565d44SJeff Roberson vm_phys_avail_largest(void) 1565b7565d44SJeff Roberson { 1566b7565d44SJeff Roberson vm_paddr_t sz, largesz; 1567b7565d44SJeff Roberson int largest; 1568b7565d44SJeff Roberson int i; 1569b7565d44SJeff Roberson 1570b7565d44SJeff Roberson largest = 0; 1571b7565d44SJeff Roberson largesz = 0; 1572b7565d44SJeff Roberson for (i = 0; phys_avail[i + 1]; i += 2) { 1573b7565d44SJeff Roberson sz = vm_phys_avail_size(i); 1574b7565d44SJeff Roberson if (sz > largesz) { 1575b7565d44SJeff Roberson largesz = sz; 1576b7565d44SJeff Roberson largest = i; 1577b7565d44SJeff Roberson } 1578b7565d44SJeff Roberson } 1579b7565d44SJeff Roberson 1580b7565d44SJeff Roberson return (largest); 1581b7565d44SJeff Roberson } 1582b7565d44SJeff Roberson 1583b7565d44SJeff Roberson vm_paddr_t 1584b7565d44SJeff Roberson vm_phys_avail_size(int i) 1585b7565d44SJeff Roberson { 1586b7565d44SJeff Roberson 1587b7565d44SJeff Roberson return (phys_avail[i + 1] - phys_avail[i]); 1588b7565d44SJeff Roberson } 1589b7565d44SJeff Roberson 1590b7565d44SJeff Roberson /* 1591b7565d44SJeff Roberson * Split an entry at the address 'pa'. Return zero on success or errno. 1592b7565d44SJeff Roberson */ 1593b7565d44SJeff Roberson static int 1594b7565d44SJeff Roberson vm_phys_avail_split(vm_paddr_t pa, int i) 1595b7565d44SJeff Roberson { 1596b7565d44SJeff Roberson int cnt; 1597b7565d44SJeff Roberson 1598b7565d44SJeff Roberson vm_phys_avail_check(i); 1599b7565d44SJeff Roberson if (pa <= phys_avail[i] || pa >= phys_avail[i + 1]) 1600b7565d44SJeff Roberson panic("vm_phys_avail_split: invalid address"); 1601b7565d44SJeff Roberson cnt = vm_phys_avail_count(); 1602b7565d44SJeff Roberson if (cnt >= PHYS_AVAIL_ENTRIES) 1603b7565d44SJeff Roberson return (ENOSPC); 1604b7565d44SJeff Roberson memmove(&phys_avail[i + 2], &phys_avail[i], 1605b7565d44SJeff Roberson (cnt - i) * sizeof(phys_avail[0])); 1606b7565d44SJeff Roberson phys_avail[i + 1] = pa; 1607b7565d44SJeff Roberson phys_avail[i + 2] = pa; 1608b7565d44SJeff Roberson vm_phys_avail_check(i); 1609b7565d44SJeff Roberson vm_phys_avail_check(i+2); 1610b7565d44SJeff Roberson 1611b7565d44SJeff Roberson return (0); 1612b7565d44SJeff Roberson } 1613b7565d44SJeff Roberson 1614b7565d44SJeff Roberson /* 1615b7565d44SJeff Roberson * This routine allocates NUMA node specific memory before the page 1616b7565d44SJeff Roberson * allocator is bootstrapped. 1617b7565d44SJeff Roberson */ 1618b7565d44SJeff Roberson vm_paddr_t 1619b7565d44SJeff Roberson vm_phys_early_alloc(int domain, size_t alloc_size) 1620b7565d44SJeff Roberson { 1621b7565d44SJeff Roberson int i, mem_index, biggestone; 1622b7565d44SJeff Roberson vm_paddr_t pa, mem_start, mem_end, size, biggestsize, align; 1623b7565d44SJeff Roberson 1624b7565d44SJeff Roberson 1625b7565d44SJeff Roberson /* 1626b7565d44SJeff Roberson * Search the mem_affinity array for the biggest address 1627b7565d44SJeff Roberson * range in the desired domain. This is used to constrain 1628b7565d44SJeff Roberson * the phys_avail selection below. 1629b7565d44SJeff Roberson */ 1630b7565d44SJeff Roberson biggestsize = 0; 1631b7565d44SJeff Roberson mem_index = 0; 1632b7565d44SJeff Roberson mem_start = 0; 1633b7565d44SJeff Roberson mem_end = -1; 1634b7565d44SJeff Roberson #ifdef NUMA 1635b7565d44SJeff Roberson if (mem_affinity != NULL) { 1636b7565d44SJeff Roberson for (i = 0; ; i++) { 1637b7565d44SJeff Roberson size = mem_affinity[i].end - mem_affinity[i].start; 1638b7565d44SJeff Roberson if (size == 0) 1639b7565d44SJeff Roberson break; 1640b7565d44SJeff Roberson if (mem_affinity[i].domain != domain) 1641b7565d44SJeff Roberson continue; 1642b7565d44SJeff Roberson if (size > biggestsize) { 1643b7565d44SJeff Roberson mem_index = i; 1644b7565d44SJeff Roberson biggestsize = size; 1645b7565d44SJeff Roberson } 1646b7565d44SJeff Roberson } 1647b7565d44SJeff Roberson mem_start = mem_affinity[mem_index].start; 1648b7565d44SJeff Roberson mem_end = mem_affinity[mem_index].end; 1649b7565d44SJeff Roberson } 1650b7565d44SJeff Roberson #endif 1651b7565d44SJeff Roberson 1652b7565d44SJeff Roberson /* 1653b7565d44SJeff Roberson * Now find biggest physical segment in within the desired 1654b7565d44SJeff Roberson * numa domain. 1655b7565d44SJeff Roberson */ 1656b7565d44SJeff Roberson biggestsize = 0; 1657b7565d44SJeff Roberson biggestone = 0; 1658b7565d44SJeff Roberson for (i = 0; phys_avail[i + 1] != 0; i += 2) { 1659b7565d44SJeff Roberson /* skip regions that are out of range */ 1660b7565d44SJeff Roberson if (phys_avail[i+1] - alloc_size < mem_start || 1661b7565d44SJeff Roberson phys_avail[i+1] > mem_end) 1662b7565d44SJeff Roberson continue; 1663b7565d44SJeff Roberson size = vm_phys_avail_size(i); 1664b7565d44SJeff Roberson if (size > biggestsize) { 1665b7565d44SJeff Roberson biggestone = i; 1666b7565d44SJeff Roberson biggestsize = size; 1667b7565d44SJeff Roberson } 1668b7565d44SJeff Roberson } 1669b7565d44SJeff Roberson alloc_size = round_page(alloc_size); 1670b7565d44SJeff Roberson 1671b7565d44SJeff Roberson /* 1672b7565d44SJeff Roberson * Grab single pages from the front to reduce fragmentation. 1673b7565d44SJeff Roberson */ 1674b7565d44SJeff Roberson if (alloc_size == PAGE_SIZE) { 1675b7565d44SJeff Roberson pa = phys_avail[biggestone]; 1676b7565d44SJeff Roberson phys_avail[biggestone] += PAGE_SIZE; 1677b7565d44SJeff Roberson vm_phys_avail_check(biggestone); 1678b7565d44SJeff Roberson return (pa); 1679b7565d44SJeff Roberson } 1680b7565d44SJeff Roberson 1681b7565d44SJeff Roberson /* 1682b7565d44SJeff Roberson * Naturally align large allocations. 1683b7565d44SJeff Roberson */ 1684b7565d44SJeff Roberson align = phys_avail[biggestone + 1] & (alloc_size - 1); 1685b7565d44SJeff Roberson if (alloc_size + align > biggestsize) 1686b7565d44SJeff Roberson panic("cannot find a large enough size\n"); 1687b7565d44SJeff Roberson if (align != 0 && 1688b7565d44SJeff Roberson vm_phys_avail_split(phys_avail[biggestone + 1] - align, 1689b7565d44SJeff Roberson biggestone) != 0) 1690b7565d44SJeff Roberson /* Wasting memory. */ 1691b7565d44SJeff Roberson phys_avail[biggestone + 1] -= align; 1692b7565d44SJeff Roberson 1693b7565d44SJeff Roberson phys_avail[biggestone + 1] -= alloc_size; 1694b7565d44SJeff Roberson vm_phys_avail_check(biggestone); 1695b7565d44SJeff Roberson pa = phys_avail[biggestone + 1]; 1696b7565d44SJeff Roberson return (pa); 1697b7565d44SJeff Roberson } 1698b7565d44SJeff Roberson 1699b7565d44SJeff Roberson void 1700b7565d44SJeff Roberson vm_phys_early_startup(void) 1701b7565d44SJeff Roberson { 1702b7565d44SJeff Roberson int i; 1703b7565d44SJeff Roberson 1704b7565d44SJeff Roberson for (i = 0; phys_avail[i + 1] != 0; i += 2) { 1705b7565d44SJeff Roberson phys_avail[i] = round_page(phys_avail[i]); 1706b7565d44SJeff Roberson phys_avail[i + 1] = trunc_page(phys_avail[i + 1]); 1707b7565d44SJeff Roberson } 1708b7565d44SJeff Roberson 1709b7565d44SJeff Roberson #ifdef NUMA 1710b7565d44SJeff Roberson /* Force phys_avail to be split by domain. */ 1711b7565d44SJeff Roberson if (mem_affinity != NULL) { 1712b7565d44SJeff Roberson int idx; 1713b7565d44SJeff Roberson 1714b7565d44SJeff Roberson for (i = 0; mem_affinity[i].end != 0; i++) { 1715b7565d44SJeff Roberson idx = vm_phys_avail_find(mem_affinity[i].start); 1716b7565d44SJeff Roberson if (idx != -1 && 1717b7565d44SJeff Roberson phys_avail[idx] != mem_affinity[i].start) 1718b7565d44SJeff Roberson vm_phys_avail_split(mem_affinity[i].start, idx); 1719b7565d44SJeff Roberson idx = vm_phys_avail_find(mem_affinity[i].end); 1720b7565d44SJeff Roberson if (idx != -1 && 1721b7565d44SJeff Roberson phys_avail[idx] != mem_affinity[i].end) 1722b7565d44SJeff Roberson vm_phys_avail_split(mem_affinity[i].end, idx); 1723b7565d44SJeff Roberson } 1724b7565d44SJeff Roberson } 1725b7565d44SJeff Roberson #endif 1726b7565d44SJeff Roberson } 1727b7565d44SJeff Roberson 172811752d88SAlan Cox #ifdef DDB 172911752d88SAlan Cox /* 173011752d88SAlan Cox * Show the number of physical pages in each of the free lists. 173111752d88SAlan Cox */ 173211752d88SAlan Cox DB_SHOW_COMMAND(freepages, db_show_freepages) 173311752d88SAlan Cox { 173411752d88SAlan Cox struct vm_freelist *fl; 17357e226537SAttilio Rao int flind, oind, pind, dom; 173611752d88SAlan Cox 17377e226537SAttilio Rao for (dom = 0; dom < vm_ndomains; dom++) { 17387e226537SAttilio Rao db_printf("DOMAIN: %d\n", dom); 173911752d88SAlan Cox for (flind = 0; flind < vm_nfreelists; flind++) { 174011752d88SAlan Cox db_printf("FREE LIST %d:\n" 174111752d88SAlan Cox "\n ORDER (SIZE) | NUMBER" 174211752d88SAlan Cox "\n ", flind); 174311752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 174411752d88SAlan Cox db_printf(" | POOL %d", pind); 174511752d88SAlan Cox db_printf("\n-- "); 174611752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 174711752d88SAlan Cox db_printf("-- -- "); 174811752d88SAlan Cox db_printf("--\n"); 174911752d88SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) { 175011752d88SAlan Cox db_printf(" %2.2d (%6.6dK)", oind, 175111752d88SAlan Cox 1 << (PAGE_SHIFT - 10 + oind)); 175211752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 17537e226537SAttilio Rao fl = vm_phys_free_queues[dom][flind][pind]; 175411752d88SAlan Cox db_printf(" | %6.6d", fl[oind].lcnt); 175511752d88SAlan Cox } 175611752d88SAlan Cox db_printf("\n"); 175711752d88SAlan Cox } 175811752d88SAlan Cox db_printf("\n"); 175911752d88SAlan Cox } 17607e226537SAttilio Rao db_printf("\n"); 17617e226537SAttilio Rao } 176211752d88SAlan Cox } 176311752d88SAlan Cox #endif 1764