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> 4911752d88SAlan Cox #include <sys/lock.h> 5011752d88SAlan Cox #include <sys/kernel.h> 5111752d88SAlan Cox #include <sys/malloc.h> 5211752d88SAlan Cox #include <sys/mutex.h> 537e226537SAttilio Rao #include <sys/proc.h> 5411752d88SAlan Cox #include <sys/queue.h> 5538d6b2dcSRoger Pau Monné #include <sys/rwlock.h> 5611752d88SAlan Cox #include <sys/sbuf.h> 5711752d88SAlan Cox #include <sys/sysctl.h> 5838d6b2dcSRoger Pau Monné #include <sys/tree.h> 5911752d88SAlan Cox #include <sys/vmmeter.h> 606520495aSAdrian Chadd #include <sys/seq.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> 7011752d88SAlan Cox 716520495aSAdrian Chadd #include <vm/vm_domain.h> 726520495aSAdrian Chadd 73449c2e92SKonstantin Belousov _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX, 74449c2e92SKonstantin Belousov "Too many physsegs."); 7511752d88SAlan Cox 7662d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC 77a3870a18SJohn Baldwin struct mem_affinity *mem_affinity; 78415d7ccaSAdrian Chadd int *mem_locality; 7962d70a81SJohn Baldwin #endif 80a3870a18SJohn Baldwin 817e226537SAttilio Rao int vm_ndomains = 1; 827e226537SAttilio Rao 83449c2e92SKonstantin Belousov struct vm_phys_seg vm_phys_segs[VM_PHYSSEG_MAX]; 84449c2e92SKonstantin Belousov int 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 = 9138d6b2dcSRoger Pau Monné 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é 10438d6b2dcSRoger Pau Monné static struct rwlock vm_phys_fictitious_reg_lock; 105c0432fc3SMark Johnston MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages"); 106b6de32bdSKonstantin Belousov 10711752d88SAlan Cox static struct vm_freelist 1087e226537SAttilio Rao vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL][VM_NFREEORDER]; 10911752d88SAlan Cox 110d866a563SAlan Cox static int vm_nfreelists; 111d866a563SAlan Cox 112d866a563SAlan Cox /* 113d866a563SAlan Cox * Provides the mapping from VM_FREELIST_* to free list indices (flind). 114d866a563SAlan Cox */ 115d866a563SAlan Cox static int vm_freelist_to_flind[VM_NFREELIST]; 116d866a563SAlan Cox 117d866a563SAlan Cox CTASSERT(VM_FREELIST_DEFAULT == 0); 118d866a563SAlan Cox 119d866a563SAlan Cox #ifdef VM_FREELIST_ISADMA 120d866a563SAlan Cox #define VM_ISADMA_BOUNDARY 16777216 121d866a563SAlan Cox #endif 122d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 123d866a563SAlan Cox #define VM_DMA32_BOUNDARY ((vm_paddr_t)1 << 32) 124d866a563SAlan Cox #endif 125d866a563SAlan Cox 126d866a563SAlan Cox /* 127d866a563SAlan Cox * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about 128d866a563SAlan Cox * the ordering of the free list boundaries. 129d866a563SAlan Cox */ 130d866a563SAlan Cox #if defined(VM_ISADMA_BOUNDARY) && defined(VM_LOWMEM_BOUNDARY) 131d866a563SAlan Cox CTASSERT(VM_ISADMA_BOUNDARY < VM_LOWMEM_BOUNDARY); 132d866a563SAlan Cox #endif 133d866a563SAlan Cox #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY) 134d866a563SAlan Cox CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY); 135d866a563SAlan Cox #endif 13611752d88SAlan Cox 13711752d88SAlan Cox static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS); 13811752d88SAlan Cox SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD, 13911752d88SAlan Cox NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info"); 14011752d88SAlan Cox 14111752d88SAlan Cox static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS); 14211752d88SAlan Cox SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD, 14311752d88SAlan Cox NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info"); 14411752d88SAlan Cox 14562d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC 146415d7ccaSAdrian Chadd static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS); 147415d7ccaSAdrian Chadd SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD, 148415d7ccaSAdrian Chadd NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info"); 1496520495aSAdrian Chadd #endif 150415d7ccaSAdrian Chadd 1517e226537SAttilio Rao SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD, 1527e226537SAttilio Rao &vm_ndomains, 0, "Number of physical memory domains available."); 153a3870a18SJohn Baldwin 154c869e672SAlan Cox static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, 155c869e672SAlan Cox u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 156c869e672SAlan Cox vm_paddr_t boundary); 157d866a563SAlan Cox static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain); 158d866a563SAlan Cox static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end); 15911752d88SAlan Cox static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, 16011752d88SAlan Cox int order); 16111752d88SAlan Cox 16238d6b2dcSRoger Pau Monné /* 16338d6b2dcSRoger Pau Monné * Red-black tree helpers for vm fictitious range management. 16438d6b2dcSRoger Pau Monné */ 16538d6b2dcSRoger Pau Monné static inline int 16638d6b2dcSRoger Pau Monné vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p, 16738d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *range) 16838d6b2dcSRoger Pau Monné { 16938d6b2dcSRoger Pau Monné 17038d6b2dcSRoger Pau Monné KASSERT(range->start != 0 && range->end != 0, 17138d6b2dcSRoger Pau Monné ("Invalid range passed on search for vm_fictitious page")); 17238d6b2dcSRoger Pau Monné if (p->start >= range->end) 17338d6b2dcSRoger Pau Monné return (1); 17438d6b2dcSRoger Pau Monné if (p->start < range->start) 17538d6b2dcSRoger Pau Monné return (-1); 17638d6b2dcSRoger Pau Monné 17738d6b2dcSRoger Pau Monné return (0); 17838d6b2dcSRoger Pau Monné } 17938d6b2dcSRoger Pau Monné 18038d6b2dcSRoger Pau Monné static int 18138d6b2dcSRoger Pau Monné vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1, 18238d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *p2) 18338d6b2dcSRoger Pau Monné { 18438d6b2dcSRoger Pau Monné 18538d6b2dcSRoger Pau Monné /* Check if this is a search for a page */ 18638d6b2dcSRoger Pau Monné if (p1->end == 0) 18738d6b2dcSRoger Pau Monné return (vm_phys_fictitious_in_range(p1, p2)); 18838d6b2dcSRoger Pau Monné 18938d6b2dcSRoger Pau Monné KASSERT(p2->end != 0, 19038d6b2dcSRoger Pau Monné ("Invalid range passed as second parameter to vm fictitious comparison")); 19138d6b2dcSRoger Pau Monné 19238d6b2dcSRoger Pau Monné /* Searching to add a new range */ 19338d6b2dcSRoger Pau Monné if (p1->end <= p2->start) 19438d6b2dcSRoger Pau Monné return (-1); 19538d6b2dcSRoger Pau Monné if (p1->start >= p2->end) 19638d6b2dcSRoger Pau Monné return (1); 19738d6b2dcSRoger Pau Monné 19838d6b2dcSRoger Pau Monné panic("Trying to add overlapping vm fictitious ranges:\n" 19938d6b2dcSRoger Pau Monné "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start, 20038d6b2dcSRoger Pau Monné (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end); 20138d6b2dcSRoger Pau Monné } 20238d6b2dcSRoger Pau Monné 203449c2e92SKonstantin Belousov boolean_t 204449c2e92SKonstantin Belousov vm_phys_domain_intersects(long mask, vm_paddr_t low, vm_paddr_t high) 205449c2e92SKonstantin Belousov { 206449c2e92SKonstantin Belousov struct vm_phys_seg *s; 207449c2e92SKonstantin Belousov int idx; 208449c2e92SKonstantin Belousov 209449c2e92SKonstantin Belousov while ((idx = ffsl(mask)) != 0) { 210449c2e92SKonstantin Belousov idx--; /* ffsl counts from 1 */ 211449c2e92SKonstantin Belousov mask &= ~(1UL << idx); 212449c2e92SKonstantin Belousov s = &vm_phys_segs[idx]; 213449c2e92SKonstantin Belousov if (low < s->end && high > s->start) 214449c2e92SKonstantin Belousov return (TRUE); 215449c2e92SKonstantin Belousov } 216449c2e92SKonstantin Belousov return (FALSE); 217449c2e92SKonstantin Belousov } 218449c2e92SKonstantin Belousov 21911752d88SAlan Cox /* 22011752d88SAlan Cox * Outputs the state of the physical memory allocator, specifically, 22111752d88SAlan Cox * the amount of physical memory in each free list. 22211752d88SAlan Cox */ 22311752d88SAlan Cox static int 22411752d88SAlan Cox sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS) 22511752d88SAlan Cox { 22611752d88SAlan Cox struct sbuf sbuf; 22711752d88SAlan Cox struct vm_freelist *fl; 2287e226537SAttilio Rao int dom, error, flind, oind, pind; 22911752d88SAlan Cox 23000f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 23100f0e671SMatthew D Fleming if (error != 0) 23200f0e671SMatthew D Fleming return (error); 2337e226537SAttilio Rao sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req); 2347e226537SAttilio Rao for (dom = 0; dom < vm_ndomains; dom++) { 235eb2f42fbSAlan Cox sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom); 23611752d88SAlan Cox for (flind = 0; flind < vm_nfreelists; flind++) { 237eb2f42fbSAlan Cox sbuf_printf(&sbuf, "\nFREE LIST %d:\n" 23811752d88SAlan Cox "\n ORDER (SIZE) | NUMBER" 23911752d88SAlan Cox "\n ", flind); 24011752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 24111752d88SAlan Cox sbuf_printf(&sbuf, " | POOL %d", pind); 24211752d88SAlan Cox sbuf_printf(&sbuf, "\n-- "); 24311752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 24411752d88SAlan Cox sbuf_printf(&sbuf, "-- -- "); 24511752d88SAlan Cox sbuf_printf(&sbuf, "--\n"); 24611752d88SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) { 247d689bc00SAlan Cox sbuf_printf(&sbuf, " %2d (%6dK)", oind, 24811752d88SAlan Cox 1 << (PAGE_SHIFT - 10 + oind)); 24911752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 2507e226537SAttilio Rao fl = vm_phys_free_queues[dom][flind][pind]; 251eb2f42fbSAlan Cox sbuf_printf(&sbuf, " | %6d", 2527e226537SAttilio Rao fl[oind].lcnt); 25311752d88SAlan Cox } 25411752d88SAlan Cox sbuf_printf(&sbuf, "\n"); 25511752d88SAlan Cox } 2567e226537SAttilio Rao } 25711752d88SAlan Cox } 2584e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 25911752d88SAlan Cox sbuf_delete(&sbuf); 26011752d88SAlan Cox return (error); 26111752d88SAlan Cox } 26211752d88SAlan Cox 26311752d88SAlan Cox /* 26411752d88SAlan Cox * Outputs the set of physical memory segments. 26511752d88SAlan Cox */ 26611752d88SAlan Cox static int 26711752d88SAlan Cox sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS) 26811752d88SAlan Cox { 26911752d88SAlan Cox struct sbuf sbuf; 27011752d88SAlan Cox struct vm_phys_seg *seg; 27111752d88SAlan Cox int error, segind; 27211752d88SAlan Cox 27300f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 27400f0e671SMatthew D Fleming if (error != 0) 27500f0e671SMatthew D Fleming return (error); 2764e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 27711752d88SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 27811752d88SAlan Cox sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind); 27911752d88SAlan Cox seg = &vm_phys_segs[segind]; 28011752d88SAlan Cox sbuf_printf(&sbuf, "start: %#jx\n", 28111752d88SAlan Cox (uintmax_t)seg->start); 28211752d88SAlan Cox sbuf_printf(&sbuf, "end: %#jx\n", 28311752d88SAlan Cox (uintmax_t)seg->end); 284a3870a18SJohn Baldwin sbuf_printf(&sbuf, "domain: %d\n", seg->domain); 28511752d88SAlan Cox sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues); 28611752d88SAlan Cox } 2874e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 28811752d88SAlan Cox sbuf_delete(&sbuf); 28911752d88SAlan Cox return (error); 29011752d88SAlan Cox } 29111752d88SAlan Cox 292415d7ccaSAdrian Chadd /* 293415d7ccaSAdrian Chadd * Return affinity, or -1 if there's no affinity information. 294415d7ccaSAdrian Chadd */ 2956520495aSAdrian Chadd int 296415d7ccaSAdrian Chadd vm_phys_mem_affinity(int f, int t) 297415d7ccaSAdrian Chadd { 298415d7ccaSAdrian Chadd 29962d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC 300415d7ccaSAdrian Chadd if (mem_locality == NULL) 301415d7ccaSAdrian Chadd return (-1); 302415d7ccaSAdrian Chadd if (f >= vm_ndomains || t >= vm_ndomains) 303415d7ccaSAdrian Chadd return (-1); 304415d7ccaSAdrian Chadd return (mem_locality[f * vm_ndomains + t]); 3056520495aSAdrian Chadd #else 3066520495aSAdrian Chadd return (-1); 3076520495aSAdrian Chadd #endif 308415d7ccaSAdrian Chadd } 309415d7ccaSAdrian Chadd 31062d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC 311415d7ccaSAdrian Chadd /* 312415d7ccaSAdrian Chadd * Outputs the VM locality table. 313415d7ccaSAdrian Chadd */ 314415d7ccaSAdrian Chadd static int 315415d7ccaSAdrian Chadd sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS) 316415d7ccaSAdrian Chadd { 317415d7ccaSAdrian Chadd struct sbuf sbuf; 318415d7ccaSAdrian Chadd int error, i, j; 319415d7ccaSAdrian Chadd 320415d7ccaSAdrian Chadd error = sysctl_wire_old_buffer(req, 0); 321415d7ccaSAdrian Chadd if (error != 0) 322415d7ccaSAdrian Chadd return (error); 323415d7ccaSAdrian Chadd sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 324415d7ccaSAdrian Chadd 325415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "\n"); 326415d7ccaSAdrian Chadd 327415d7ccaSAdrian Chadd for (i = 0; i < vm_ndomains; i++) { 328415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "%d: ", i); 329415d7ccaSAdrian Chadd for (j = 0; j < vm_ndomains; j++) { 330415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j)); 331415d7ccaSAdrian Chadd } 332415d7ccaSAdrian Chadd sbuf_printf(&sbuf, "\n"); 333415d7ccaSAdrian Chadd } 334415d7ccaSAdrian Chadd error = sbuf_finish(&sbuf); 335415d7ccaSAdrian Chadd sbuf_delete(&sbuf); 336415d7ccaSAdrian Chadd return (error); 337415d7ccaSAdrian Chadd } 3386520495aSAdrian Chadd #endif 339415d7ccaSAdrian Chadd 3407e226537SAttilio Rao static void 3417e226537SAttilio Rao vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail) 342a3870a18SJohn Baldwin { 343a3870a18SJohn Baldwin 3447e226537SAttilio Rao m->order = order; 3457e226537SAttilio Rao if (tail) 346c325e866SKonstantin Belousov TAILQ_INSERT_TAIL(&fl[order].pl, m, plinks.q); 3477e226537SAttilio Rao else 348c325e866SKonstantin Belousov TAILQ_INSERT_HEAD(&fl[order].pl, m, plinks.q); 3497e226537SAttilio Rao fl[order].lcnt++; 350a3870a18SJohn Baldwin } 3517e226537SAttilio Rao 3527e226537SAttilio Rao static void 3537e226537SAttilio Rao vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order) 3547e226537SAttilio Rao { 3557e226537SAttilio Rao 356c325e866SKonstantin Belousov TAILQ_REMOVE(&fl[order].pl, m, plinks.q); 3577e226537SAttilio Rao fl[order].lcnt--; 3587e226537SAttilio Rao m->order = VM_NFREEORDER; 359a3870a18SJohn Baldwin } 360a3870a18SJohn Baldwin 36111752d88SAlan Cox /* 36211752d88SAlan Cox * Create a physical memory segment. 36311752d88SAlan Cox */ 36411752d88SAlan Cox static void 365d866a563SAlan Cox _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain) 36611752d88SAlan Cox { 36711752d88SAlan Cox struct vm_phys_seg *seg; 36811752d88SAlan Cox 36911752d88SAlan Cox KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX, 37011752d88SAlan Cox ("vm_phys_create_seg: increase VM_PHYSSEG_MAX")); 371*ef435ae7SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 3727e226537SAttilio Rao ("vm_phys_create_seg: invalid domain provided")); 37311752d88SAlan Cox seg = &vm_phys_segs[vm_phys_nsegs++]; 374271f0f12SAlan Cox while (seg > vm_phys_segs && (seg - 1)->start >= end) { 375271f0f12SAlan Cox *seg = *(seg - 1); 376271f0f12SAlan Cox seg--; 377271f0f12SAlan Cox } 37811752d88SAlan Cox seg->start = start; 37911752d88SAlan Cox seg->end = end; 380a3870a18SJohn Baldwin seg->domain = domain; 38111752d88SAlan Cox } 38211752d88SAlan Cox 383a3870a18SJohn Baldwin static void 384d866a563SAlan Cox vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end) 385a3870a18SJohn Baldwin { 38662d70a81SJohn Baldwin #ifdef VM_NUMA_ALLOC 387a3870a18SJohn Baldwin int i; 388a3870a18SJohn Baldwin 389a3870a18SJohn Baldwin if (mem_affinity == NULL) { 390d866a563SAlan Cox _vm_phys_create_seg(start, end, 0); 391a3870a18SJohn Baldwin return; 392a3870a18SJohn Baldwin } 393a3870a18SJohn Baldwin 394a3870a18SJohn Baldwin for (i = 0;; i++) { 395a3870a18SJohn Baldwin if (mem_affinity[i].end == 0) 396a3870a18SJohn Baldwin panic("Reached end of affinity info"); 397a3870a18SJohn Baldwin if (mem_affinity[i].end <= start) 398a3870a18SJohn Baldwin continue; 399a3870a18SJohn Baldwin if (mem_affinity[i].start > start) 400a3870a18SJohn Baldwin panic("No affinity info for start %jx", 401a3870a18SJohn Baldwin (uintmax_t)start); 402a3870a18SJohn Baldwin if (mem_affinity[i].end >= end) { 403d866a563SAlan Cox _vm_phys_create_seg(start, end, 404a3870a18SJohn Baldwin mem_affinity[i].domain); 405a3870a18SJohn Baldwin break; 406a3870a18SJohn Baldwin } 407d866a563SAlan Cox _vm_phys_create_seg(start, mem_affinity[i].end, 408a3870a18SJohn Baldwin mem_affinity[i].domain); 409a3870a18SJohn Baldwin start = mem_affinity[i].end; 410a3870a18SJohn Baldwin } 41162d70a81SJohn Baldwin #else 41262d70a81SJohn Baldwin _vm_phys_create_seg(start, end, 0); 41362d70a81SJohn Baldwin #endif 414a3870a18SJohn Baldwin } 415a3870a18SJohn Baldwin 41611752d88SAlan Cox /* 417271f0f12SAlan Cox * Add a physical memory segment. 418271f0f12SAlan Cox */ 419271f0f12SAlan Cox void 420271f0f12SAlan Cox vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end) 421271f0f12SAlan Cox { 422d866a563SAlan Cox vm_paddr_t paddr; 423271f0f12SAlan Cox 424271f0f12SAlan Cox KASSERT((start & PAGE_MASK) == 0, 425271f0f12SAlan Cox ("vm_phys_define_seg: start is not page aligned")); 426271f0f12SAlan Cox KASSERT((end & PAGE_MASK) == 0, 427271f0f12SAlan Cox ("vm_phys_define_seg: end is not page aligned")); 428d866a563SAlan Cox 429d866a563SAlan Cox /* 430d866a563SAlan Cox * Split the physical memory segment if it spans two or more free 431d866a563SAlan Cox * list boundaries. 432d866a563SAlan Cox */ 433d866a563SAlan Cox paddr = start; 434271f0f12SAlan Cox #ifdef VM_FREELIST_ISADMA 435d866a563SAlan Cox if (paddr < VM_ISADMA_BOUNDARY && end > VM_ISADMA_BOUNDARY) { 436d866a563SAlan Cox vm_phys_create_seg(paddr, VM_ISADMA_BOUNDARY); 437d866a563SAlan Cox paddr = VM_ISADMA_BOUNDARY; 438d866a563SAlan Cox } 439271f0f12SAlan Cox #endif 440d866a563SAlan Cox #ifdef VM_FREELIST_LOWMEM 441d866a563SAlan Cox if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) { 442d866a563SAlan Cox vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY); 443d866a563SAlan Cox paddr = VM_LOWMEM_BOUNDARY; 444d866a563SAlan Cox } 445271f0f12SAlan Cox #endif 446d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 447d866a563SAlan Cox if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) { 448d866a563SAlan Cox vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY); 449d866a563SAlan Cox paddr = VM_DMA32_BOUNDARY; 450d866a563SAlan Cox } 451d866a563SAlan Cox #endif 452d866a563SAlan Cox vm_phys_create_seg(paddr, end); 453271f0f12SAlan Cox } 454271f0f12SAlan Cox 455271f0f12SAlan Cox /* 45611752d88SAlan Cox * Initialize the physical memory allocator. 457d866a563SAlan Cox * 458d866a563SAlan Cox * Requires that vm_page_array is initialized! 45911752d88SAlan Cox */ 46011752d88SAlan Cox void 46111752d88SAlan Cox vm_phys_init(void) 46211752d88SAlan Cox { 46311752d88SAlan Cox struct vm_freelist *fl; 464271f0f12SAlan Cox struct vm_phys_seg *seg; 465d866a563SAlan Cox u_long npages; 466d866a563SAlan Cox int dom, flind, freelist, oind, pind, segind; 46711752d88SAlan Cox 468d866a563SAlan Cox /* 469d866a563SAlan Cox * Compute the number of free lists, and generate the mapping from the 470d866a563SAlan Cox * manifest constants VM_FREELIST_* to the free list indices. 471d866a563SAlan Cox * 472d866a563SAlan Cox * Initially, the entries of vm_freelist_to_flind[] are set to either 473d866a563SAlan Cox * 0 or 1 to indicate which free lists should be created. 474d866a563SAlan Cox */ 475d866a563SAlan Cox npages = 0; 476d866a563SAlan Cox for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) { 477d866a563SAlan Cox seg = &vm_phys_segs[segind]; 478d866a563SAlan Cox #ifdef VM_FREELIST_ISADMA 479d866a563SAlan Cox if (seg->end <= VM_ISADMA_BOUNDARY) 480d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_ISADMA] = 1; 481d866a563SAlan Cox else 482d866a563SAlan Cox #endif 483d866a563SAlan Cox #ifdef VM_FREELIST_LOWMEM 484d866a563SAlan Cox if (seg->end <= VM_LOWMEM_BOUNDARY) 485d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1; 486d866a563SAlan Cox else 487d866a563SAlan Cox #endif 488d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 489d866a563SAlan Cox if ( 490d866a563SAlan Cox #ifdef VM_DMA32_NPAGES_THRESHOLD 491d866a563SAlan Cox /* 492d866a563SAlan Cox * Create the DMA32 free list only if the amount of 493d866a563SAlan Cox * physical memory above physical address 4G exceeds the 494d866a563SAlan Cox * given threshold. 495d866a563SAlan Cox */ 496d866a563SAlan Cox npages > VM_DMA32_NPAGES_THRESHOLD && 497d866a563SAlan Cox #endif 498d866a563SAlan Cox seg->end <= VM_DMA32_BOUNDARY) 499d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_DMA32] = 1; 500d866a563SAlan Cox else 501d866a563SAlan Cox #endif 502d866a563SAlan Cox { 503d866a563SAlan Cox npages += atop(seg->end - seg->start); 504d866a563SAlan Cox vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1; 505d866a563SAlan Cox } 506d866a563SAlan Cox } 507d866a563SAlan Cox /* Change each entry into a running total of the free lists. */ 508d866a563SAlan Cox for (freelist = 1; freelist < VM_NFREELIST; freelist++) { 509d866a563SAlan Cox vm_freelist_to_flind[freelist] += 510d866a563SAlan Cox vm_freelist_to_flind[freelist - 1]; 511d866a563SAlan Cox } 512d866a563SAlan Cox vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1]; 513d866a563SAlan Cox KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists")); 514d866a563SAlan Cox /* Change each entry into a free list index. */ 515d866a563SAlan Cox for (freelist = 0; freelist < VM_NFREELIST; freelist++) 516d866a563SAlan Cox vm_freelist_to_flind[freelist]--; 517d866a563SAlan Cox 518d866a563SAlan Cox /* 519d866a563SAlan Cox * Initialize the first_page and free_queues fields of each physical 520d866a563SAlan Cox * memory segment. 521d866a563SAlan Cox */ 522271f0f12SAlan Cox #ifdef VM_PHYSSEG_SPARSE 523d866a563SAlan Cox npages = 0; 52411752d88SAlan Cox #endif 525271f0f12SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 526271f0f12SAlan Cox seg = &vm_phys_segs[segind]; 527271f0f12SAlan Cox #ifdef VM_PHYSSEG_SPARSE 528d866a563SAlan Cox seg->first_page = &vm_page_array[npages]; 529d866a563SAlan Cox npages += atop(seg->end - seg->start); 530271f0f12SAlan Cox #else 531271f0f12SAlan Cox seg->first_page = PHYS_TO_VM_PAGE(seg->start); 53211752d88SAlan Cox #endif 533d866a563SAlan Cox #ifdef VM_FREELIST_ISADMA 534d866a563SAlan Cox if (seg->end <= VM_ISADMA_BOUNDARY) { 535d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_ISADMA]; 536d866a563SAlan Cox KASSERT(flind >= 0, 537d866a563SAlan Cox ("vm_phys_init: ISADMA flind < 0")); 538d866a563SAlan Cox } else 539d866a563SAlan Cox #endif 540d866a563SAlan Cox #ifdef VM_FREELIST_LOWMEM 541d866a563SAlan Cox if (seg->end <= VM_LOWMEM_BOUNDARY) { 542d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM]; 543d866a563SAlan Cox KASSERT(flind >= 0, 544d866a563SAlan Cox ("vm_phys_init: LOWMEM flind < 0")); 545d866a563SAlan Cox } else 546d866a563SAlan Cox #endif 547d866a563SAlan Cox #ifdef VM_FREELIST_DMA32 548d866a563SAlan Cox if (seg->end <= VM_DMA32_BOUNDARY) { 549d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_DMA32]; 550d866a563SAlan Cox KASSERT(flind >= 0, 551d866a563SAlan Cox ("vm_phys_init: DMA32 flind < 0")); 552d866a563SAlan Cox } else 553d866a563SAlan Cox #endif 554d866a563SAlan Cox { 555d866a563SAlan Cox flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT]; 556d866a563SAlan Cox KASSERT(flind >= 0, 557d866a563SAlan Cox ("vm_phys_init: DEFAULT flind < 0")); 55811752d88SAlan Cox } 559d866a563SAlan Cox seg->free_queues = &vm_phys_free_queues[seg->domain][flind]; 560d866a563SAlan Cox } 561d866a563SAlan Cox 562d866a563SAlan Cox /* 563d866a563SAlan Cox * Initialize the free queues. 564d866a563SAlan Cox */ 5657e226537SAttilio Rao for (dom = 0; dom < vm_ndomains; dom++) { 56611752d88SAlan Cox for (flind = 0; flind < vm_nfreelists; flind++) { 56711752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 5687e226537SAttilio Rao fl = vm_phys_free_queues[dom][flind][pind]; 56911752d88SAlan Cox for (oind = 0; oind < VM_NFREEORDER; oind++) 57011752d88SAlan Cox TAILQ_INIT(&fl[oind].pl); 57111752d88SAlan Cox } 57211752d88SAlan Cox } 573a3870a18SJohn Baldwin } 574d866a563SAlan Cox 57538d6b2dcSRoger Pau Monné rw_init(&vm_phys_fictitious_reg_lock, "vmfctr"); 57611752d88SAlan Cox } 57711752d88SAlan Cox 57811752d88SAlan Cox /* 57911752d88SAlan Cox * Split a contiguous, power of two-sized set of physical pages. 58011752d88SAlan Cox */ 58111752d88SAlan Cox static __inline void 58211752d88SAlan Cox vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order) 58311752d88SAlan Cox { 58411752d88SAlan Cox vm_page_t m_buddy; 58511752d88SAlan Cox 58611752d88SAlan Cox while (oind > order) { 58711752d88SAlan Cox oind--; 58811752d88SAlan Cox m_buddy = &m[1 << oind]; 58911752d88SAlan Cox KASSERT(m_buddy->order == VM_NFREEORDER, 59011752d88SAlan Cox ("vm_phys_split_pages: page %p has unexpected order %d", 59111752d88SAlan Cox m_buddy, m_buddy->order)); 5927e226537SAttilio Rao vm_freelist_add(fl, m_buddy, oind, 0); 59311752d88SAlan Cox } 59411752d88SAlan Cox } 59511752d88SAlan Cox 59611752d88SAlan Cox /* 59711752d88SAlan Cox * Allocate a contiguous, power of two-sized set of physical pages 59811752d88SAlan Cox * from the free lists. 5998941dc44SAlan Cox * 6008941dc44SAlan Cox * The free page queues must be locked. 60111752d88SAlan Cox */ 60211752d88SAlan Cox vm_page_t 603*ef435ae7SJeff Roberson vm_phys_alloc_pages(int domain, int pool, int order) 60411752d88SAlan Cox { 60549ca10d4SJayachandran C. vm_page_t m; 606*ef435ae7SJeff Roberson int flind; 60749ca10d4SJayachandran C. 60849ca10d4SJayachandran C. for (flind = 0; flind < vm_nfreelists; flind++) { 609*ef435ae7SJeff Roberson m = vm_phys_alloc_freelist_pages(domain, flind, pool, order); 61049ca10d4SJayachandran C. if (m != NULL) 61149ca10d4SJayachandran C. return (m); 61249ca10d4SJayachandran C. } 61349ca10d4SJayachandran C. return (NULL); 61449ca10d4SJayachandran C. } 61549ca10d4SJayachandran C. 61649ca10d4SJayachandran C. /* 617d866a563SAlan Cox * Allocate a contiguous, power of two-sized set of physical pages from the 618d866a563SAlan Cox * specified free list. The free list must be specified using one of the 619d866a563SAlan Cox * manifest constants VM_FREELIST_*. 620d866a563SAlan Cox * 621d866a563SAlan Cox * The free page queues must be locked. 62249ca10d4SJayachandran C. */ 62349ca10d4SJayachandran C. vm_page_t 624*ef435ae7SJeff Roberson vm_phys_alloc_freelist_pages(int domain, int flind, int pool, int order) 62549ca10d4SJayachandran C. { 626*ef435ae7SJeff Roberson struct vm_freelist *alt, *fl; 62711752d88SAlan Cox vm_page_t m; 628*ef435ae7SJeff Roberson int oind, pind; 62911752d88SAlan Cox 630*ef435ae7SJeff Roberson KASSERT(domain >= 0 && domain < vm_ndomains, 631*ef435ae7SJeff Roberson ("vm_phys_alloc_freelist_pages: domain %d is out of range", 632*ef435ae7SJeff Roberson domain)); 633*ef435ae7SJeff Roberson KASSERT(flind < VM_NFREELIST, 634d866a563SAlan Cox ("vm_phys_alloc_freelist_pages: freelist %d is out of range", 635*ef435ae7SJeff Roberson flind)); 63611752d88SAlan Cox KASSERT(pool < VM_NFREEPOOL, 63749ca10d4SJayachandran C. ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool)); 63811752d88SAlan Cox KASSERT(order < VM_NFREEORDER, 63949ca10d4SJayachandran C. ("vm_phys_alloc_freelist_pages: order %d is out of range", order)); 6406520495aSAdrian Chadd 64111752d88SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 6427e226537SAttilio Rao fl = &vm_phys_free_queues[domain][flind][pool][0]; 64311752d88SAlan Cox for (oind = order; oind < VM_NFREEORDER; oind++) { 64411752d88SAlan Cox m = TAILQ_FIRST(&fl[oind].pl); 64511752d88SAlan Cox if (m != NULL) { 6467e226537SAttilio Rao vm_freelist_rem(fl, m, oind); 64711752d88SAlan Cox vm_phys_split_pages(m, oind, fl, order); 64811752d88SAlan Cox return (m); 64911752d88SAlan Cox } 65011752d88SAlan Cox } 65111752d88SAlan Cox 65211752d88SAlan Cox /* 65311752d88SAlan Cox * The given pool was empty. Find the largest 65411752d88SAlan Cox * contiguous, power-of-two-sized set of pages in any 65511752d88SAlan Cox * pool. Transfer these pages to the given pool, and 65611752d88SAlan Cox * use them to satisfy the allocation. 65711752d88SAlan Cox */ 65811752d88SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= order; oind--) { 65911752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 6607e226537SAttilio Rao alt = &vm_phys_free_queues[domain][flind][pind][0]; 66111752d88SAlan Cox m = TAILQ_FIRST(&alt[oind].pl); 66211752d88SAlan Cox if (m != NULL) { 6637e226537SAttilio Rao vm_freelist_rem(alt, m, oind); 66411752d88SAlan Cox vm_phys_set_pool(pool, m, oind); 66511752d88SAlan Cox vm_phys_split_pages(m, oind, fl, order); 66611752d88SAlan Cox return (m); 66711752d88SAlan Cox } 66811752d88SAlan Cox } 66911752d88SAlan Cox } 67011752d88SAlan Cox return (NULL); 67111752d88SAlan Cox } 67211752d88SAlan Cox 67311752d88SAlan Cox /* 67411752d88SAlan Cox * Find the vm_page corresponding to the given physical address. 67511752d88SAlan Cox */ 67611752d88SAlan Cox vm_page_t 67711752d88SAlan Cox vm_phys_paddr_to_vm_page(vm_paddr_t pa) 67811752d88SAlan Cox { 67911752d88SAlan Cox struct vm_phys_seg *seg; 68011752d88SAlan Cox int segind; 68111752d88SAlan Cox 68211752d88SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 68311752d88SAlan Cox seg = &vm_phys_segs[segind]; 68411752d88SAlan Cox if (pa >= seg->start && pa < seg->end) 68511752d88SAlan Cox return (&seg->first_page[atop(pa - seg->start)]); 68611752d88SAlan Cox } 687f06a3a36SAndrew Thompson return (NULL); 68811752d88SAlan Cox } 68911752d88SAlan Cox 690b6de32bdSKonstantin Belousov vm_page_t 691b6de32bdSKonstantin Belousov vm_phys_fictitious_to_vm_page(vm_paddr_t pa) 692b6de32bdSKonstantin Belousov { 69338d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg tmp, *seg; 694b6de32bdSKonstantin Belousov vm_page_t m; 695b6de32bdSKonstantin Belousov 696b6de32bdSKonstantin Belousov m = NULL; 69738d6b2dcSRoger Pau Monné tmp.start = pa; 69838d6b2dcSRoger Pau Monné tmp.end = 0; 69938d6b2dcSRoger Pau Monné 70038d6b2dcSRoger Pau Monné rw_rlock(&vm_phys_fictitious_reg_lock); 70138d6b2dcSRoger Pau Monné seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp); 70238d6b2dcSRoger Pau Monné rw_runlock(&vm_phys_fictitious_reg_lock); 70338d6b2dcSRoger Pau Monné if (seg == NULL) 70438d6b2dcSRoger Pau Monné return (NULL); 70538d6b2dcSRoger Pau Monné 706b6de32bdSKonstantin Belousov m = &seg->first_page[atop(pa - seg->start)]; 70738d6b2dcSRoger Pau Monné KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m)); 70838d6b2dcSRoger Pau Monné 709b6de32bdSKonstantin Belousov return (m); 710b6de32bdSKonstantin Belousov } 711b6de32bdSKonstantin Belousov 7125ebe728dSRoger Pau Monné static inline void 7135ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start, 7145ebe728dSRoger Pau Monné long page_count, vm_memattr_t memattr) 7155ebe728dSRoger Pau Monné { 7165ebe728dSRoger Pau Monné long i; 7175ebe728dSRoger Pau Monné 718f93f7cf1SMark Johnston bzero(range, page_count * sizeof(*range)); 7195ebe728dSRoger Pau Monné for (i = 0; i < page_count; i++) { 7205ebe728dSRoger Pau Monné vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr); 7215ebe728dSRoger Pau Monné range[i].oflags &= ~VPO_UNMANAGED; 7225ebe728dSRoger Pau Monné range[i].busy_lock = VPB_UNBUSIED; 7235ebe728dSRoger Pau Monné } 7245ebe728dSRoger Pau Monné } 7255ebe728dSRoger Pau Monné 726b6de32bdSKonstantin Belousov int 727b6de32bdSKonstantin Belousov vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end, 728b6de32bdSKonstantin Belousov vm_memattr_t memattr) 729b6de32bdSKonstantin Belousov { 730b6de32bdSKonstantin Belousov struct vm_phys_fictitious_seg *seg; 731b6de32bdSKonstantin Belousov vm_page_t fp; 7325ebe728dSRoger Pau Monné long page_count; 733b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 7345ebe728dSRoger Pau Monné long pi, pe; 7355ebe728dSRoger Pau Monné long dpage_count; 736b6de32bdSKonstantin Belousov #endif 737b6de32bdSKonstantin Belousov 7385ebe728dSRoger Pau Monné KASSERT(start < end, 7395ebe728dSRoger Pau Monné ("Start of segment isn't less than end (start: %jx end: %jx)", 7405ebe728dSRoger Pau Monné (uintmax_t)start, (uintmax_t)end)); 7415ebe728dSRoger Pau Monné 742b6de32bdSKonstantin Belousov page_count = (end - start) / PAGE_SIZE; 743b6de32bdSKonstantin Belousov 744b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 745b6de32bdSKonstantin Belousov pi = atop(start); 7465ebe728dSRoger Pau Monné pe = atop(end); 7475ebe728dSRoger Pau Monné if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 748b6de32bdSKonstantin Belousov fp = &vm_page_array[pi - first_page]; 7495ebe728dSRoger Pau Monné if ((pe - first_page) > vm_page_array_size) { 7505ebe728dSRoger Pau Monné /* 7515ebe728dSRoger Pau Monné * We have a segment that starts inside 7525ebe728dSRoger Pau Monné * of vm_page_array, but ends outside of it. 7535ebe728dSRoger Pau Monné * 7545ebe728dSRoger Pau Monné * Use vm_page_array pages for those that are 7555ebe728dSRoger Pau Monné * inside of the vm_page_array range, and 7565ebe728dSRoger Pau Monné * allocate the remaining ones. 7575ebe728dSRoger Pau Monné */ 7585ebe728dSRoger Pau Monné dpage_count = vm_page_array_size - (pi - first_page); 7595ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, start, dpage_count, 7605ebe728dSRoger Pau Monné memattr); 7615ebe728dSRoger Pau Monné page_count -= dpage_count; 7625ebe728dSRoger Pau Monné start += ptoa(dpage_count); 7635ebe728dSRoger Pau Monné goto alloc; 7645ebe728dSRoger Pau Monné } 7655ebe728dSRoger Pau Monné /* 7665ebe728dSRoger Pau Monné * We can allocate the full range from vm_page_array, 7675ebe728dSRoger Pau Monné * so there's no need to register the range in the tree. 7685ebe728dSRoger Pau Monné */ 7695ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, start, page_count, memattr); 7705ebe728dSRoger Pau Monné return (0); 7715ebe728dSRoger Pau Monné } else if (pe > first_page && (pe - first_page) < vm_page_array_size) { 7725ebe728dSRoger Pau Monné /* 7735ebe728dSRoger Pau Monné * We have a segment that ends inside of vm_page_array, 7745ebe728dSRoger Pau Monné * but starts outside of it. 7755ebe728dSRoger Pau Monné */ 7765ebe728dSRoger Pau Monné fp = &vm_page_array[0]; 7775ebe728dSRoger Pau Monné dpage_count = pe - first_page; 7785ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count, 7795ebe728dSRoger Pau Monné memattr); 7805ebe728dSRoger Pau Monné end -= ptoa(dpage_count); 7815ebe728dSRoger Pau Monné page_count -= dpage_count; 7825ebe728dSRoger Pau Monné goto alloc; 7835ebe728dSRoger Pau Monné } else if (pi < first_page && pe > (first_page + vm_page_array_size)) { 7845ebe728dSRoger Pau Monné /* 7855ebe728dSRoger Pau Monné * Trying to register a fictitious range that expands before 7865ebe728dSRoger Pau Monné * and after vm_page_array. 7875ebe728dSRoger Pau Monné */ 7885ebe728dSRoger Pau Monné return (EINVAL); 7895ebe728dSRoger Pau Monné } else { 7905ebe728dSRoger Pau Monné alloc: 791b6de32bdSKonstantin Belousov #endif 792b6de32bdSKonstantin Belousov fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES, 793f93f7cf1SMark Johnston M_WAITOK); 7945ebe728dSRoger Pau Monné #ifdef VM_PHYSSEG_DENSE 795b6de32bdSKonstantin Belousov } 7965ebe728dSRoger Pau Monné #endif 7975ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(fp, start, page_count, memattr); 79838d6b2dcSRoger Pau Monné 79938d6b2dcSRoger Pau Monné seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO); 800b6de32bdSKonstantin Belousov seg->start = start; 801b6de32bdSKonstantin Belousov seg->end = end; 802b6de32bdSKonstantin Belousov seg->first_page = fp; 80338d6b2dcSRoger Pau Monné 80438d6b2dcSRoger Pau Monné rw_wlock(&vm_phys_fictitious_reg_lock); 80538d6b2dcSRoger Pau Monné RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg); 80638d6b2dcSRoger Pau Monné rw_wunlock(&vm_phys_fictitious_reg_lock); 80738d6b2dcSRoger Pau Monné 808b6de32bdSKonstantin Belousov return (0); 809b6de32bdSKonstantin Belousov } 810b6de32bdSKonstantin Belousov 811b6de32bdSKonstantin Belousov void 812b6de32bdSKonstantin Belousov vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end) 813b6de32bdSKonstantin Belousov { 81438d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg *seg, tmp; 815b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 8165ebe728dSRoger Pau Monné long pi, pe; 817b6de32bdSKonstantin Belousov #endif 818b6de32bdSKonstantin Belousov 8195ebe728dSRoger Pau Monné KASSERT(start < end, 8205ebe728dSRoger Pau Monné ("Start of segment isn't less than end (start: %jx end: %jx)", 8215ebe728dSRoger Pau Monné (uintmax_t)start, (uintmax_t)end)); 8225ebe728dSRoger Pau Monné 823b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE 824b6de32bdSKonstantin Belousov pi = atop(start); 8255ebe728dSRoger Pau Monné pe = atop(end); 8265ebe728dSRoger Pau Monné if (pi >= first_page && (pi - first_page) < vm_page_array_size) { 8275ebe728dSRoger Pau Monné if ((pe - first_page) <= vm_page_array_size) { 8285ebe728dSRoger Pau Monné /* 8295ebe728dSRoger Pau Monné * This segment was allocated using vm_page_array 8305ebe728dSRoger Pau Monné * only, there's nothing to do since those pages 8315ebe728dSRoger Pau Monné * were never added to the tree. 8325ebe728dSRoger Pau Monné */ 8335ebe728dSRoger Pau Monné return; 8345ebe728dSRoger Pau Monné } 8355ebe728dSRoger Pau Monné /* 8365ebe728dSRoger Pau Monné * We have a segment that starts inside 8375ebe728dSRoger Pau Monné * of vm_page_array, but ends outside of it. 8385ebe728dSRoger Pau Monné * 8395ebe728dSRoger Pau Monné * Calculate how many pages were added to the 8405ebe728dSRoger Pau Monné * tree and free them. 8415ebe728dSRoger Pau Monné */ 8425ebe728dSRoger Pau Monné start = ptoa(first_page + vm_page_array_size); 8435ebe728dSRoger Pau Monné } else if (pe > first_page && (pe - first_page) < vm_page_array_size) { 8445ebe728dSRoger Pau Monné /* 8455ebe728dSRoger Pau Monné * We have a segment that ends inside of vm_page_array, 8465ebe728dSRoger Pau Monné * but starts outside of it. 8475ebe728dSRoger Pau Monné */ 8485ebe728dSRoger Pau Monné end = ptoa(first_page); 8495ebe728dSRoger Pau Monné } else if (pi < first_page && pe > (first_page + vm_page_array_size)) { 8505ebe728dSRoger Pau Monné /* Since it's not possible to register such a range, panic. */ 8515ebe728dSRoger Pau Monné panic( 8525ebe728dSRoger Pau Monné "Unregistering not registered fictitious range [%#jx:%#jx]", 8535ebe728dSRoger Pau Monné (uintmax_t)start, (uintmax_t)end); 8545ebe728dSRoger Pau Monné } 855b6de32bdSKonstantin Belousov #endif 85638d6b2dcSRoger Pau Monné tmp.start = start; 85738d6b2dcSRoger Pau Monné tmp.end = 0; 858b6de32bdSKonstantin Belousov 85938d6b2dcSRoger Pau Monné rw_wlock(&vm_phys_fictitious_reg_lock); 86038d6b2dcSRoger Pau Monné seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp); 86138d6b2dcSRoger Pau Monné if (seg->start != start || seg->end != end) { 86238d6b2dcSRoger Pau Monné rw_wunlock(&vm_phys_fictitious_reg_lock); 86338d6b2dcSRoger Pau Monné panic( 86438d6b2dcSRoger Pau Monné "Unregistering not registered fictitious range [%#jx:%#jx]", 86538d6b2dcSRoger Pau Monné (uintmax_t)start, (uintmax_t)end); 86638d6b2dcSRoger Pau Monné } 86738d6b2dcSRoger Pau Monné RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg); 86838d6b2dcSRoger Pau Monné rw_wunlock(&vm_phys_fictitious_reg_lock); 86938d6b2dcSRoger Pau Monné free(seg->first_page, M_FICT_PAGES); 87038d6b2dcSRoger Pau Monné free(seg, M_FICT_PAGES); 871b6de32bdSKonstantin Belousov } 872b6de32bdSKonstantin Belousov 87311752d88SAlan Cox /* 87411752d88SAlan Cox * Free a contiguous, power of two-sized set of physical pages. 8758941dc44SAlan Cox * 8768941dc44SAlan Cox * The free page queues must be locked. 87711752d88SAlan Cox */ 87811752d88SAlan Cox void 87911752d88SAlan Cox vm_phys_free_pages(vm_page_t m, int order) 88011752d88SAlan Cox { 88111752d88SAlan Cox struct vm_freelist *fl; 88211752d88SAlan Cox struct vm_phys_seg *seg; 8835c1f2cc4SAlan Cox vm_paddr_t pa; 88411752d88SAlan Cox vm_page_t m_buddy; 88511752d88SAlan Cox 88611752d88SAlan Cox KASSERT(m->order == VM_NFREEORDER, 8878941dc44SAlan Cox ("vm_phys_free_pages: page %p has unexpected order %d", 88811752d88SAlan Cox m, m->order)); 88911752d88SAlan Cox KASSERT(m->pool < VM_NFREEPOOL, 8908941dc44SAlan Cox ("vm_phys_free_pages: page %p has unexpected pool %d", 89111752d88SAlan Cox m, m->pool)); 89211752d88SAlan Cox KASSERT(order < VM_NFREEORDER, 8938941dc44SAlan Cox ("vm_phys_free_pages: order %d is out of range", order)); 89411752d88SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 89511752d88SAlan Cox seg = &vm_phys_segs[m->segind]; 8965c1f2cc4SAlan Cox if (order < VM_NFREEORDER - 1) { 8975c1f2cc4SAlan Cox pa = VM_PAGE_TO_PHYS(m); 8985c1f2cc4SAlan Cox do { 8995c1f2cc4SAlan Cox pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order)); 9005c1f2cc4SAlan Cox if (pa < seg->start || pa >= seg->end) 90111752d88SAlan Cox break; 9025c1f2cc4SAlan Cox m_buddy = &seg->first_page[atop(pa - seg->start)]; 90311752d88SAlan Cox if (m_buddy->order != order) 90411752d88SAlan Cox break; 90511752d88SAlan Cox fl = (*seg->free_queues)[m_buddy->pool]; 9067e226537SAttilio Rao vm_freelist_rem(fl, m_buddy, order); 90711752d88SAlan Cox if (m_buddy->pool != m->pool) 90811752d88SAlan Cox vm_phys_set_pool(m->pool, m_buddy, order); 90911752d88SAlan Cox order++; 9105c1f2cc4SAlan Cox pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1); 91111752d88SAlan Cox m = &seg->first_page[atop(pa - seg->start)]; 9125c1f2cc4SAlan Cox } while (order < VM_NFREEORDER - 1); 91311752d88SAlan Cox } 91411752d88SAlan Cox fl = (*seg->free_queues)[m->pool]; 9157e226537SAttilio Rao vm_freelist_add(fl, m, order, 1); 91611752d88SAlan Cox } 91711752d88SAlan Cox 91811752d88SAlan Cox /* 9195c1f2cc4SAlan Cox * Free a contiguous, arbitrarily sized set of physical pages. 9205c1f2cc4SAlan Cox * 9215c1f2cc4SAlan Cox * The free page queues must be locked. 9225c1f2cc4SAlan Cox */ 9235c1f2cc4SAlan Cox void 9245c1f2cc4SAlan Cox vm_phys_free_contig(vm_page_t m, u_long npages) 9255c1f2cc4SAlan Cox { 9265c1f2cc4SAlan Cox u_int n; 9275c1f2cc4SAlan Cox int order; 9285c1f2cc4SAlan Cox 9295c1f2cc4SAlan Cox /* 9305c1f2cc4SAlan Cox * Avoid unnecessary coalescing by freeing the pages in the largest 9315c1f2cc4SAlan Cox * possible power-of-two-sized subsets. 9325c1f2cc4SAlan Cox */ 9335c1f2cc4SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 9345c1f2cc4SAlan Cox for (;; npages -= n) { 9355c1f2cc4SAlan Cox /* 9365c1f2cc4SAlan Cox * Unsigned "min" is used here so that "order" is assigned 9375c1f2cc4SAlan Cox * "VM_NFREEORDER - 1" when "m"'s physical address is zero 9385c1f2cc4SAlan Cox * or the low-order bits of its physical address are zero 9395c1f2cc4SAlan Cox * because the size of a physical address exceeds the size of 9405c1f2cc4SAlan Cox * a long. 9415c1f2cc4SAlan Cox */ 9425c1f2cc4SAlan Cox order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1, 9435c1f2cc4SAlan Cox VM_NFREEORDER - 1); 9445c1f2cc4SAlan Cox n = 1 << order; 9455c1f2cc4SAlan Cox if (npages < n) 9465c1f2cc4SAlan Cox break; 9475c1f2cc4SAlan Cox vm_phys_free_pages(m, order); 9485c1f2cc4SAlan Cox m += n; 9495c1f2cc4SAlan Cox } 9505c1f2cc4SAlan Cox /* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */ 9515c1f2cc4SAlan Cox for (; npages > 0; npages -= n) { 9525c1f2cc4SAlan Cox order = flsl(npages) - 1; 9535c1f2cc4SAlan Cox n = 1 << order; 9545c1f2cc4SAlan Cox vm_phys_free_pages(m, order); 9555c1f2cc4SAlan Cox m += n; 9565c1f2cc4SAlan Cox } 9575c1f2cc4SAlan Cox } 9585c1f2cc4SAlan Cox 9595c1f2cc4SAlan Cox /* 960c869e672SAlan Cox * Scan physical memory between the specified addresses "low" and "high" for a 961c869e672SAlan Cox * run of contiguous physical pages that satisfy the specified conditions, and 962c869e672SAlan Cox * return the lowest page in the run. The specified "alignment" determines 963c869e672SAlan Cox * the alignment of the lowest physical page in the run. If the specified 964c869e672SAlan Cox * "boundary" is non-zero, then the run of physical pages cannot span a 965c869e672SAlan Cox * physical address that is a multiple of "boundary". 966c869e672SAlan Cox * 967c869e672SAlan Cox * "npages" must be greater than zero. Both "alignment" and "boundary" must 968c869e672SAlan Cox * be a power of two. 969c869e672SAlan Cox */ 970c869e672SAlan Cox vm_page_t 971c869e672SAlan Cox vm_phys_scan_contig(u_long npages, vm_paddr_t low, vm_paddr_t high, 972c869e672SAlan Cox u_long alignment, vm_paddr_t boundary, int options) 973c869e672SAlan Cox { 974c869e672SAlan Cox vm_paddr_t pa_end; 975c869e672SAlan Cox vm_page_t m_end, m_run, m_start; 976c869e672SAlan Cox struct vm_phys_seg *seg; 977c869e672SAlan Cox int segind; 978c869e672SAlan Cox 979c869e672SAlan Cox KASSERT(npages > 0, ("npages is 0")); 980c869e672SAlan Cox KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 981c869e672SAlan Cox KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 982c869e672SAlan Cox if (low >= high) 983c869e672SAlan Cox return (NULL); 984c869e672SAlan Cox for (segind = 0; segind < vm_phys_nsegs; segind++) { 985c869e672SAlan Cox seg = &vm_phys_segs[segind]; 986c869e672SAlan Cox if (seg->start >= high) 987c869e672SAlan Cox break; 988c869e672SAlan Cox if (low >= seg->end) 989c869e672SAlan Cox continue; 990c869e672SAlan Cox if (low <= seg->start) 991c869e672SAlan Cox m_start = seg->first_page; 992c869e672SAlan Cox else 993c869e672SAlan Cox m_start = &seg->first_page[atop(low - seg->start)]; 994c869e672SAlan Cox if (high < seg->end) 995c869e672SAlan Cox pa_end = high; 996c869e672SAlan Cox else 997c869e672SAlan Cox pa_end = seg->end; 998c869e672SAlan Cox if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages)) 999c869e672SAlan Cox continue; 1000c869e672SAlan Cox m_end = &seg->first_page[atop(pa_end - seg->start)]; 1001c869e672SAlan Cox m_run = vm_page_scan_contig(npages, m_start, m_end, 1002c869e672SAlan Cox alignment, boundary, options); 1003c869e672SAlan Cox if (m_run != NULL) 1004c869e672SAlan Cox return (m_run); 1005c869e672SAlan Cox } 1006c869e672SAlan Cox return (NULL); 1007c869e672SAlan Cox } 1008c869e672SAlan Cox 1009c869e672SAlan Cox /* 101011752d88SAlan Cox * Set the pool for a contiguous, power of two-sized set of physical pages. 101111752d88SAlan Cox */ 10127bfda801SAlan Cox void 101311752d88SAlan Cox vm_phys_set_pool(int pool, vm_page_t m, int order) 101411752d88SAlan Cox { 101511752d88SAlan Cox vm_page_t m_tmp; 101611752d88SAlan Cox 101711752d88SAlan Cox for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++) 101811752d88SAlan Cox m_tmp->pool = pool; 101911752d88SAlan Cox } 102011752d88SAlan Cox 102111752d88SAlan Cox /* 10229742373aSAlan Cox * Search for the given physical page "m" in the free lists. If the search 10239742373aSAlan Cox * succeeds, remove "m" from the free lists and return TRUE. Otherwise, return 10249742373aSAlan Cox * FALSE, indicating that "m" is not in the free lists. 10257bfda801SAlan Cox * 10267bfda801SAlan Cox * The free page queues must be locked. 10277bfda801SAlan Cox */ 1028e35395ceSAlan Cox boolean_t 10297bfda801SAlan Cox vm_phys_unfree_page(vm_page_t m) 10307bfda801SAlan Cox { 10317bfda801SAlan Cox struct vm_freelist *fl; 10327bfda801SAlan Cox struct vm_phys_seg *seg; 10337bfda801SAlan Cox vm_paddr_t pa, pa_half; 10347bfda801SAlan Cox vm_page_t m_set, m_tmp; 10357bfda801SAlan Cox int order; 10367bfda801SAlan Cox 10377bfda801SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 10387bfda801SAlan Cox 10397bfda801SAlan Cox /* 10407bfda801SAlan Cox * First, find the contiguous, power of two-sized set of free 10417bfda801SAlan Cox * physical pages containing the given physical page "m" and 10427bfda801SAlan Cox * assign it to "m_set". 10437bfda801SAlan Cox */ 10447bfda801SAlan Cox seg = &vm_phys_segs[m->segind]; 10457bfda801SAlan Cox for (m_set = m, order = 0; m_set->order == VM_NFREEORDER && 1046bc8794a1SAlan Cox order < VM_NFREEORDER - 1; ) { 10477bfda801SAlan Cox order++; 10487bfda801SAlan Cox pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order)); 10492fbced65SAlan Cox if (pa >= seg->start) 10507bfda801SAlan Cox m_set = &seg->first_page[atop(pa - seg->start)]; 1051e35395ceSAlan Cox else 1052e35395ceSAlan Cox return (FALSE); 10537bfda801SAlan Cox } 1054e35395ceSAlan Cox if (m_set->order < order) 1055e35395ceSAlan Cox return (FALSE); 1056e35395ceSAlan Cox if (m_set->order == VM_NFREEORDER) 1057e35395ceSAlan Cox return (FALSE); 10587bfda801SAlan Cox KASSERT(m_set->order < VM_NFREEORDER, 10597bfda801SAlan Cox ("vm_phys_unfree_page: page %p has unexpected order %d", 10607bfda801SAlan Cox m_set, m_set->order)); 10617bfda801SAlan Cox 10627bfda801SAlan Cox /* 10637bfda801SAlan Cox * Next, remove "m_set" from the free lists. Finally, extract 10647bfda801SAlan Cox * "m" from "m_set" using an iterative algorithm: While "m_set" 10657bfda801SAlan Cox * is larger than a page, shrink "m_set" by returning the half 10667bfda801SAlan Cox * of "m_set" that does not contain "m" to the free lists. 10677bfda801SAlan Cox */ 10687bfda801SAlan Cox fl = (*seg->free_queues)[m_set->pool]; 10697bfda801SAlan Cox order = m_set->order; 10707e226537SAttilio Rao vm_freelist_rem(fl, m_set, order); 10717bfda801SAlan Cox while (order > 0) { 10727bfda801SAlan Cox order--; 10737bfda801SAlan Cox pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order)); 10747bfda801SAlan Cox if (m->phys_addr < pa_half) 10757bfda801SAlan Cox m_tmp = &seg->first_page[atop(pa_half - seg->start)]; 10767bfda801SAlan Cox else { 10777bfda801SAlan Cox m_tmp = m_set; 10787bfda801SAlan Cox m_set = &seg->first_page[atop(pa_half - seg->start)]; 10797bfda801SAlan Cox } 10807e226537SAttilio Rao vm_freelist_add(fl, m_tmp, order, 0); 10817bfda801SAlan Cox } 10827bfda801SAlan Cox KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency")); 1083e35395ceSAlan Cox return (TRUE); 10847bfda801SAlan Cox } 10857bfda801SAlan Cox 10867bfda801SAlan Cox /* 10872f9f48d6SAlan Cox * Allocate a contiguous set of physical pages of the given size 10882f9f48d6SAlan Cox * "npages" from the free lists. All of the physical pages must be at 10892f9f48d6SAlan Cox * or above the given physical address "low" and below the given 10902f9f48d6SAlan Cox * physical address "high". The given value "alignment" determines the 10912f9f48d6SAlan Cox * alignment of the first physical page in the set. If the given value 10922f9f48d6SAlan Cox * "boundary" is non-zero, then the set of physical pages cannot cross 10932f9f48d6SAlan Cox * any physical address boundary that is a multiple of that value. Both 109411752d88SAlan Cox * "alignment" and "boundary" must be a power of two. 109511752d88SAlan Cox */ 109611752d88SAlan Cox vm_page_t 1097*ef435ae7SJeff Roberson vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high, 10985c1f2cc4SAlan Cox u_long alignment, vm_paddr_t boundary) 109911752d88SAlan Cox { 1100c869e672SAlan Cox vm_paddr_t pa_end, pa_start; 1101c869e672SAlan Cox vm_page_t m_run; 1102c869e672SAlan Cox struct vm_phys_seg *seg; 1103*ef435ae7SJeff Roberson int segind; 110411752d88SAlan Cox 1105c869e672SAlan Cox KASSERT(npages > 0, ("npages is 0")); 1106c869e672SAlan Cox KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 1107c869e672SAlan Cox KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1108fbd80bd0SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1109c869e672SAlan Cox if (low >= high) 1110c869e672SAlan Cox return (NULL); 1111c869e672SAlan Cox m_run = NULL; 1112477bffbeSAlan Cox for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) { 1113c869e672SAlan Cox seg = &vm_phys_segs[segind]; 1114477bffbeSAlan Cox if (seg->start >= high || seg->domain != domain) 111511752d88SAlan Cox continue; 1116477bffbeSAlan Cox if (low >= seg->end) 1117477bffbeSAlan Cox break; 1118c869e672SAlan Cox if (low <= seg->start) 1119c869e672SAlan Cox pa_start = seg->start; 1120c869e672SAlan Cox else 1121c869e672SAlan Cox pa_start = low; 1122c869e672SAlan Cox if (high < seg->end) 1123c869e672SAlan Cox pa_end = high; 1124c869e672SAlan Cox else 1125c869e672SAlan Cox pa_end = seg->end; 1126c869e672SAlan Cox if (pa_end - pa_start < ptoa(npages)) 1127c869e672SAlan Cox continue; 1128c869e672SAlan Cox m_run = vm_phys_alloc_seg_contig(seg, npages, low, high, 1129c869e672SAlan Cox alignment, boundary); 1130c869e672SAlan Cox if (m_run != NULL) 1131c869e672SAlan Cox break; 1132c869e672SAlan Cox } 1133c869e672SAlan Cox return (m_run); 1134c869e672SAlan Cox } 113511752d88SAlan Cox 113611752d88SAlan Cox /* 1137c869e672SAlan Cox * Allocate a run of contiguous physical pages from the free list for the 1138c869e672SAlan Cox * specified segment. 1139c869e672SAlan Cox */ 1140c869e672SAlan Cox static vm_page_t 1141c869e672SAlan Cox vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages, 1142c869e672SAlan Cox vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary) 1143c869e672SAlan Cox { 1144c869e672SAlan Cox struct vm_freelist *fl; 1145c869e672SAlan Cox vm_paddr_t pa, pa_end, size; 1146c869e672SAlan Cox vm_page_t m, m_ret; 1147c869e672SAlan Cox u_long npages_end; 1148c869e672SAlan Cox int oind, order, pind; 1149c869e672SAlan Cox 1150c869e672SAlan Cox KASSERT(npages > 0, ("npages is 0")); 1151c869e672SAlan Cox KASSERT(powerof2(alignment), ("alignment is not a power of 2")); 1152c869e672SAlan Cox KASSERT(powerof2(boundary), ("boundary is not a power of 2")); 1153c869e672SAlan Cox mtx_assert(&vm_page_queue_free_mtx, MA_OWNED); 1154c869e672SAlan Cox /* Compute the queue that is the best fit for npages. */ 1155c869e672SAlan Cox for (order = 0; (1 << order) < npages; order++); 1156c869e672SAlan Cox /* Search for a run satisfying the specified conditions. */ 1157c869e672SAlan Cox size = npages << PAGE_SHIFT; 1158c869e672SAlan Cox for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER; 1159c869e672SAlan Cox oind++) { 1160c869e672SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 1161c869e672SAlan Cox fl = (*seg->free_queues)[pind]; 1162c869e672SAlan Cox TAILQ_FOREACH(m_ret, &fl[oind].pl, plinks.q) { 1163c869e672SAlan Cox /* 116411752d88SAlan Cox * Is the size of this allocation request 116511752d88SAlan Cox * larger than the largest block size? 116611752d88SAlan Cox */ 116711752d88SAlan Cox if (order >= VM_NFREEORDER) { 116811752d88SAlan Cox /* 1169c869e672SAlan Cox * Determine if a sufficient number of 1170c869e672SAlan Cox * subsequent blocks to satisfy the 1171c869e672SAlan Cox * allocation request are free. 117211752d88SAlan Cox */ 117311752d88SAlan Cox pa = VM_PAGE_TO_PHYS(m_ret); 1174c869e672SAlan Cox pa_end = pa + size; 117511752d88SAlan Cox for (;;) { 1176c869e672SAlan Cox pa += 1 << (PAGE_SHIFT + 1177c869e672SAlan Cox VM_NFREEORDER - 1); 1178c869e672SAlan Cox if (pa >= pa_end || 1179c869e672SAlan Cox pa < seg->start || 118011752d88SAlan Cox pa >= seg->end) 118111752d88SAlan Cox break; 1182c869e672SAlan Cox m = &seg->first_page[atop(pa - 1183c869e672SAlan Cox seg->start)]; 1184c869e672SAlan Cox if (m->order != VM_NFREEORDER - 1185c869e672SAlan Cox 1) 118611752d88SAlan Cox break; 118711752d88SAlan Cox } 1188c869e672SAlan Cox /* If not, go to the next block. */ 1189c869e672SAlan Cox if (pa < pa_end) 119011752d88SAlan Cox continue; 119111752d88SAlan Cox } 119211752d88SAlan Cox 119311752d88SAlan Cox /* 1194c869e672SAlan Cox * Determine if the blocks are within the 1195c869e672SAlan Cox * given range, satisfy the given alignment, 1196c869e672SAlan Cox * and do not cross the given boundary. 119711752d88SAlan Cox */ 119811752d88SAlan Cox pa = VM_PAGE_TO_PHYS(m_ret); 1199c869e672SAlan Cox pa_end = pa + size; 1200d9c9c81cSPedro F. Giffuni if (pa >= low && pa_end <= high && 1201d9c9c81cSPedro F. Giffuni (pa & (alignment - 1)) == 0 && 1202d9c9c81cSPedro F. Giffuni rounddown2(pa ^ (pa_end - 1), boundary) == 0) 120311752d88SAlan Cox goto done; 120411752d88SAlan Cox } 120511752d88SAlan Cox } 120611752d88SAlan Cox } 120711752d88SAlan Cox return (NULL); 120811752d88SAlan Cox done: 120911752d88SAlan Cox for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) { 121011752d88SAlan Cox fl = (*seg->free_queues)[m->pool]; 12117e226537SAttilio Rao vm_freelist_rem(fl, m, m->order); 121211752d88SAlan Cox } 121311752d88SAlan Cox if (m_ret->pool != VM_FREEPOOL_DEFAULT) 121411752d88SAlan Cox vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m_ret, oind); 121511752d88SAlan Cox fl = (*seg->free_queues)[m_ret->pool]; 121611752d88SAlan Cox vm_phys_split_pages(m_ret, oind, fl, order); 12175c1f2cc4SAlan Cox /* Return excess pages to the free lists. */ 12185c1f2cc4SAlan Cox npages_end = roundup2(npages, 1 << imin(oind, order)); 12195c1f2cc4SAlan Cox if (npages < npages_end) 12205c1f2cc4SAlan Cox vm_phys_free_contig(&m_ret[npages], npages_end - npages); 122111752d88SAlan Cox return (m_ret); 122211752d88SAlan Cox } 122311752d88SAlan Cox 122411752d88SAlan Cox #ifdef DDB 122511752d88SAlan Cox /* 122611752d88SAlan Cox * Show the number of physical pages in each of the free lists. 122711752d88SAlan Cox */ 122811752d88SAlan Cox DB_SHOW_COMMAND(freepages, db_show_freepages) 122911752d88SAlan Cox { 123011752d88SAlan Cox struct vm_freelist *fl; 12317e226537SAttilio Rao int flind, oind, pind, dom; 123211752d88SAlan Cox 12337e226537SAttilio Rao for (dom = 0; dom < vm_ndomains; dom++) { 12347e226537SAttilio Rao db_printf("DOMAIN: %d\n", dom); 123511752d88SAlan Cox for (flind = 0; flind < vm_nfreelists; flind++) { 123611752d88SAlan Cox db_printf("FREE LIST %d:\n" 123711752d88SAlan Cox "\n ORDER (SIZE) | NUMBER" 123811752d88SAlan Cox "\n ", flind); 123911752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 124011752d88SAlan Cox db_printf(" | POOL %d", pind); 124111752d88SAlan Cox db_printf("\n-- "); 124211752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) 124311752d88SAlan Cox db_printf("-- -- "); 124411752d88SAlan Cox db_printf("--\n"); 124511752d88SAlan Cox for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) { 124611752d88SAlan Cox db_printf(" %2.2d (%6.6dK)", oind, 124711752d88SAlan Cox 1 << (PAGE_SHIFT - 10 + oind)); 124811752d88SAlan Cox for (pind = 0; pind < VM_NFREEPOOL; pind++) { 12497e226537SAttilio Rao fl = vm_phys_free_queues[dom][flind][pind]; 125011752d88SAlan Cox db_printf(" | %6.6d", fl[oind].lcnt); 125111752d88SAlan Cox } 125211752d88SAlan Cox db_printf("\n"); 125311752d88SAlan Cox } 125411752d88SAlan Cox db_printf("\n"); 125511752d88SAlan Cox } 12567e226537SAttilio Rao db_printf("\n"); 12577e226537SAttilio Rao } 125811752d88SAlan Cox } 125911752d88SAlan Cox #endif 1260