xref: /freebsd/sys/vm/vm_phys.c (revision f2a496d667f3c4500b68dc6c751a0e103d56a447)
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>
616520495aSAdrian Chadd #include <sys/seq.h>
6211752d88SAlan Cox 
6311752d88SAlan Cox #include <ddb/ddb.h>
6411752d88SAlan Cox 
6511752d88SAlan Cox #include <vm/vm.h>
6611752d88SAlan Cox #include <vm/vm_param.h>
6711752d88SAlan Cox #include <vm/vm_kern.h>
6811752d88SAlan Cox #include <vm/vm_object.h>
6911752d88SAlan Cox #include <vm/vm_page.h>
7011752d88SAlan Cox #include <vm/vm_phys.h>
71e2068d0bSJeff Roberson #include <vm/vm_pagequeue.h>
7211752d88SAlan Cox 
73449c2e92SKonstantin Belousov _Static_assert(sizeof(long) * NBBY >= VM_PHYSSEG_MAX,
74449c2e92SKonstantin Belousov     "Too many physsegs.");
7511752d88SAlan Cox 
76b6715dabSJeff Roberson #ifdef NUMA
77cdfeced8SJeff Roberson struct mem_affinity __read_mostly *mem_affinity;
78cdfeced8SJeff Roberson int __read_mostly *mem_locality;
7962d70a81SJohn Baldwin #endif
80a3870a18SJohn Baldwin 
81cdfeced8SJeff Roberson int __read_mostly vm_ndomains = 1;
82463406acSMark Johnston domainset_t __read_mostly all_domains = DOMAINSET_T_INITIALIZER(0x1);
837e226537SAttilio Rao 
84cdfeced8SJeff Roberson struct vm_phys_seg __read_mostly vm_phys_segs[VM_PHYSSEG_MAX];
85cdfeced8SJeff Roberson int __read_mostly vm_phys_nsegs;
8611752d88SAlan Cox 
8738d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg;
8838d6b2dcSRoger Pau Monné static int vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *,
8938d6b2dcSRoger Pau Monné     struct vm_phys_fictitious_seg *);
9038d6b2dcSRoger Pau Monné 
9138d6b2dcSRoger Pau Monné RB_HEAD(fict_tree, vm_phys_fictitious_seg) vm_phys_fictitious_tree =
9238d6b2dcSRoger Pau Monné     RB_INITIALIZER(_vm_phys_fictitious_tree);
9338d6b2dcSRoger Pau Monné 
9438d6b2dcSRoger Pau Monné struct vm_phys_fictitious_seg {
9538d6b2dcSRoger Pau Monné 	RB_ENTRY(vm_phys_fictitious_seg) node;
9638d6b2dcSRoger Pau Monné 	/* Memory region data */
97b6de32bdSKonstantin Belousov 	vm_paddr_t	start;
98b6de32bdSKonstantin Belousov 	vm_paddr_t	end;
99b6de32bdSKonstantin Belousov 	vm_page_t	first_page;
10038d6b2dcSRoger Pau Monné };
10138d6b2dcSRoger Pau Monné 
10238d6b2dcSRoger Pau Monné RB_GENERATE_STATIC(fict_tree, vm_phys_fictitious_seg, node,
10338d6b2dcSRoger Pau Monné     vm_phys_fictitious_cmp);
10438d6b2dcSRoger Pau Monné 
105cdfeced8SJeff Roberson static struct rwlock_padalign vm_phys_fictitious_reg_lock;
106c0432fc3SMark Johnston MALLOC_DEFINE(M_FICT_PAGES, "vm_fictitious", "Fictitious VM pages");
107b6de32bdSKonstantin Belousov 
108cdfeced8SJeff Roberson static struct vm_freelist __aligned(CACHE_LINE_SIZE)
109*f2a496d6SKonstantin Belousov     vm_phys_free_queues[MAXMEMDOM][VM_NFREELIST][VM_NFREEPOOL]
110*f2a496d6SKonstantin Belousov     [VM_NFREEORDER_MAX];
11111752d88SAlan Cox 
112cdfeced8SJeff Roberson static int __read_mostly vm_nfreelists;
113d866a563SAlan Cox 
114d866a563SAlan Cox /*
115d866a563SAlan Cox  * Provides the mapping from VM_FREELIST_* to free list indices (flind).
116d866a563SAlan Cox  */
117cdfeced8SJeff Roberson static int __read_mostly vm_freelist_to_flind[VM_NFREELIST];
118d866a563SAlan Cox 
119d866a563SAlan Cox CTASSERT(VM_FREELIST_DEFAULT == 0);
120d866a563SAlan Cox 
121d866a563SAlan Cox #ifdef VM_FREELIST_DMA32
122d866a563SAlan Cox #define	VM_DMA32_BOUNDARY	((vm_paddr_t)1 << 32)
123d866a563SAlan Cox #endif
124d866a563SAlan Cox 
125d866a563SAlan Cox /*
126d866a563SAlan Cox  * Enforce the assumptions made by vm_phys_add_seg() and vm_phys_init() about
127d866a563SAlan Cox  * the ordering of the free list boundaries.
128d866a563SAlan Cox  */
129d866a563SAlan Cox #if defined(VM_LOWMEM_BOUNDARY) && defined(VM_DMA32_BOUNDARY)
130d866a563SAlan Cox CTASSERT(VM_LOWMEM_BOUNDARY < VM_DMA32_BOUNDARY);
131d866a563SAlan Cox #endif
13211752d88SAlan Cox 
13311752d88SAlan Cox static int sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS);
13411752d88SAlan Cox SYSCTL_OID(_vm, OID_AUTO, phys_free, CTLTYPE_STRING | CTLFLAG_RD,
13511752d88SAlan Cox     NULL, 0, sysctl_vm_phys_free, "A", "Phys Free Info");
13611752d88SAlan Cox 
13711752d88SAlan Cox static int sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS);
13811752d88SAlan Cox SYSCTL_OID(_vm, OID_AUTO, phys_segs, CTLTYPE_STRING | CTLFLAG_RD,
13911752d88SAlan Cox     NULL, 0, sysctl_vm_phys_segs, "A", "Phys Seg Info");
14011752d88SAlan Cox 
141b6715dabSJeff Roberson #ifdef NUMA
142415d7ccaSAdrian Chadd static int sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS);
143415d7ccaSAdrian Chadd SYSCTL_OID(_vm, OID_AUTO, phys_locality, CTLTYPE_STRING | CTLFLAG_RD,
144415d7ccaSAdrian Chadd     NULL, 0, sysctl_vm_phys_locality, "A", "Phys Locality Info");
1456520495aSAdrian Chadd #endif
146415d7ccaSAdrian Chadd 
1477e226537SAttilio Rao SYSCTL_INT(_vm, OID_AUTO, ndomains, CTLFLAG_RD,
1487e226537SAttilio Rao     &vm_ndomains, 0, "Number of physical memory domains available.");
149a3870a18SJohn Baldwin 
150c869e672SAlan Cox static vm_page_t vm_phys_alloc_seg_contig(struct vm_phys_seg *seg,
151c869e672SAlan Cox     u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
152c869e672SAlan Cox     vm_paddr_t boundary);
153d866a563SAlan Cox static void _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain);
154d866a563SAlan Cox static void vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end);
15511752d88SAlan Cox static void vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl,
156370a338aSAlan Cox     int order, int tail);
15711752d88SAlan Cox 
15838d6b2dcSRoger Pau Monné /*
15938d6b2dcSRoger Pau Monné  * Red-black tree helpers for vm fictitious range management.
16038d6b2dcSRoger Pau Monné  */
16138d6b2dcSRoger Pau Monné static inline int
16238d6b2dcSRoger Pau Monné vm_phys_fictitious_in_range(struct vm_phys_fictitious_seg *p,
16338d6b2dcSRoger Pau Monné     struct vm_phys_fictitious_seg *range)
16438d6b2dcSRoger Pau Monné {
16538d6b2dcSRoger Pau Monné 
16638d6b2dcSRoger Pau Monné 	KASSERT(range->start != 0 && range->end != 0,
16738d6b2dcSRoger Pau Monné 	    ("Invalid range passed on search for vm_fictitious page"));
16838d6b2dcSRoger Pau Monné 	if (p->start >= range->end)
16938d6b2dcSRoger Pau Monné 		return (1);
17038d6b2dcSRoger Pau Monné 	if (p->start < range->start)
17138d6b2dcSRoger Pau Monné 		return (-1);
17238d6b2dcSRoger Pau Monné 
17338d6b2dcSRoger Pau Monné 	return (0);
17438d6b2dcSRoger Pau Monné }
17538d6b2dcSRoger Pau Monné 
17638d6b2dcSRoger Pau Monné static int
17738d6b2dcSRoger Pau Monné vm_phys_fictitious_cmp(struct vm_phys_fictitious_seg *p1,
17838d6b2dcSRoger Pau Monné     struct vm_phys_fictitious_seg *p2)
17938d6b2dcSRoger Pau Monné {
18038d6b2dcSRoger Pau Monné 
18138d6b2dcSRoger Pau Monné 	/* Check if this is a search for a page */
18238d6b2dcSRoger Pau Monné 	if (p1->end == 0)
18338d6b2dcSRoger Pau Monné 		return (vm_phys_fictitious_in_range(p1, p2));
18438d6b2dcSRoger Pau Monné 
18538d6b2dcSRoger Pau Monné 	KASSERT(p2->end != 0,
18638d6b2dcSRoger Pau Monné     ("Invalid range passed as second parameter to vm fictitious comparison"));
18738d6b2dcSRoger Pau Monné 
18838d6b2dcSRoger Pau Monné 	/* Searching to add a new range */
18938d6b2dcSRoger Pau Monné 	if (p1->end <= p2->start)
19038d6b2dcSRoger Pau Monné 		return (-1);
19138d6b2dcSRoger Pau Monné 	if (p1->start >= p2->end)
19238d6b2dcSRoger Pau Monné 		return (1);
19338d6b2dcSRoger Pau Monné 
19438d6b2dcSRoger Pau Monné 	panic("Trying to add overlapping vm fictitious ranges:\n"
19538d6b2dcSRoger Pau Monné 	    "[%#jx:%#jx] and [%#jx:%#jx]", (uintmax_t)p1->start,
19638d6b2dcSRoger Pau Monné 	    (uintmax_t)p1->end, (uintmax_t)p2->start, (uintmax_t)p2->end);
19738d6b2dcSRoger Pau Monné }
19838d6b2dcSRoger Pau Monné 
1996f4acaf4SJeff Roberson int
2006f4acaf4SJeff Roberson vm_phys_domain_match(int prefer, vm_paddr_t low, vm_paddr_t high)
201449c2e92SKonstantin Belousov {
202b6715dabSJeff Roberson #ifdef NUMA
2036f4acaf4SJeff Roberson 	domainset_t mask;
2046f4acaf4SJeff Roberson 	int i;
205449c2e92SKonstantin Belousov 
2066f4acaf4SJeff Roberson 	if (vm_ndomains == 1 || mem_affinity == NULL)
2076f4acaf4SJeff Roberson 		return (0);
2086f4acaf4SJeff Roberson 
2096f4acaf4SJeff Roberson 	DOMAINSET_ZERO(&mask);
2106f4acaf4SJeff Roberson 	/*
2116f4acaf4SJeff Roberson 	 * Check for any memory that overlaps low, high.
2126f4acaf4SJeff Roberson 	 */
2136f4acaf4SJeff Roberson 	for (i = 0; mem_affinity[i].end != 0; i++)
2146f4acaf4SJeff Roberson 		if (mem_affinity[i].start <= high &&
2156f4acaf4SJeff Roberson 		    mem_affinity[i].end >= low)
2166f4acaf4SJeff Roberson 			DOMAINSET_SET(mem_affinity[i].domain, &mask);
2176f4acaf4SJeff Roberson 	if (prefer != -1 && DOMAINSET_ISSET(prefer, &mask))
2186f4acaf4SJeff Roberson 		return (prefer);
2196f4acaf4SJeff Roberson 	if (DOMAINSET_EMPTY(&mask))
2206f4acaf4SJeff Roberson 		panic("vm_phys_domain_match:  Impossible constraint");
2216f4acaf4SJeff Roberson 	return (DOMAINSET_FFS(&mask) - 1);
2226f4acaf4SJeff Roberson #else
2236f4acaf4SJeff Roberson 	return (0);
2246f4acaf4SJeff Roberson #endif
225449c2e92SKonstantin Belousov }
226449c2e92SKonstantin Belousov 
22711752d88SAlan Cox /*
22811752d88SAlan Cox  * Outputs the state of the physical memory allocator, specifically,
22911752d88SAlan Cox  * the amount of physical memory in each free list.
23011752d88SAlan Cox  */
23111752d88SAlan Cox static int
23211752d88SAlan Cox sysctl_vm_phys_free(SYSCTL_HANDLER_ARGS)
23311752d88SAlan Cox {
23411752d88SAlan Cox 	struct sbuf sbuf;
23511752d88SAlan Cox 	struct vm_freelist *fl;
2367e226537SAttilio Rao 	int dom, error, flind, oind, pind;
23711752d88SAlan Cox 
23800f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
23900f0e671SMatthew D Fleming 	if (error != 0)
24000f0e671SMatthew D Fleming 		return (error);
2417e226537SAttilio Rao 	sbuf_new_for_sysctl(&sbuf, NULL, 128 * vm_ndomains, req);
2427e226537SAttilio Rao 	for (dom = 0; dom < vm_ndomains; dom++) {
243eb2f42fbSAlan Cox 		sbuf_printf(&sbuf,"\nDOMAIN %d:\n", dom);
24411752d88SAlan Cox 		for (flind = 0; flind < vm_nfreelists; flind++) {
245eb2f42fbSAlan Cox 			sbuf_printf(&sbuf, "\nFREE LIST %d:\n"
24611752d88SAlan Cox 			    "\n  ORDER (SIZE)  |  NUMBER"
24711752d88SAlan Cox 			    "\n              ", flind);
24811752d88SAlan Cox 			for (pind = 0; pind < VM_NFREEPOOL; pind++)
24911752d88SAlan Cox 				sbuf_printf(&sbuf, "  |  POOL %d", pind);
25011752d88SAlan Cox 			sbuf_printf(&sbuf, "\n--            ");
25111752d88SAlan Cox 			for (pind = 0; pind < VM_NFREEPOOL; pind++)
25211752d88SAlan Cox 				sbuf_printf(&sbuf, "-- --      ");
25311752d88SAlan Cox 			sbuf_printf(&sbuf, "--\n");
25411752d88SAlan Cox 			for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
255d689bc00SAlan Cox 				sbuf_printf(&sbuf, "  %2d (%6dK)", oind,
25611752d88SAlan Cox 				    1 << (PAGE_SHIFT - 10 + oind));
25711752d88SAlan Cox 				for (pind = 0; pind < VM_NFREEPOOL; pind++) {
2587e226537SAttilio Rao 				fl = vm_phys_free_queues[dom][flind][pind];
259eb2f42fbSAlan Cox 					sbuf_printf(&sbuf, "  |  %6d",
2607e226537SAttilio Rao 					    fl[oind].lcnt);
26111752d88SAlan Cox 				}
26211752d88SAlan Cox 				sbuf_printf(&sbuf, "\n");
26311752d88SAlan Cox 			}
2647e226537SAttilio Rao 		}
26511752d88SAlan Cox 	}
2664e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
26711752d88SAlan Cox 	sbuf_delete(&sbuf);
26811752d88SAlan Cox 	return (error);
26911752d88SAlan Cox }
27011752d88SAlan Cox 
27111752d88SAlan Cox /*
27211752d88SAlan Cox  * Outputs the set of physical memory segments.
27311752d88SAlan Cox  */
27411752d88SAlan Cox static int
27511752d88SAlan Cox sysctl_vm_phys_segs(SYSCTL_HANDLER_ARGS)
27611752d88SAlan Cox {
27711752d88SAlan Cox 	struct sbuf sbuf;
27811752d88SAlan Cox 	struct vm_phys_seg *seg;
27911752d88SAlan Cox 	int error, segind;
28011752d88SAlan Cox 
28100f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
28200f0e671SMatthew D Fleming 	if (error != 0)
28300f0e671SMatthew D Fleming 		return (error);
2844e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
28511752d88SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
28611752d88SAlan Cox 		sbuf_printf(&sbuf, "\nSEGMENT %d:\n\n", segind);
28711752d88SAlan Cox 		seg = &vm_phys_segs[segind];
28811752d88SAlan Cox 		sbuf_printf(&sbuf, "start:     %#jx\n",
28911752d88SAlan Cox 		    (uintmax_t)seg->start);
29011752d88SAlan Cox 		sbuf_printf(&sbuf, "end:       %#jx\n",
29111752d88SAlan Cox 		    (uintmax_t)seg->end);
292a3870a18SJohn Baldwin 		sbuf_printf(&sbuf, "domain:    %d\n", seg->domain);
29311752d88SAlan Cox 		sbuf_printf(&sbuf, "free list: %p\n", seg->free_queues);
29411752d88SAlan Cox 	}
2954e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
29611752d88SAlan Cox 	sbuf_delete(&sbuf);
29711752d88SAlan Cox 	return (error);
29811752d88SAlan Cox }
29911752d88SAlan Cox 
300415d7ccaSAdrian Chadd /*
301415d7ccaSAdrian Chadd  * Return affinity, or -1 if there's no affinity information.
302415d7ccaSAdrian Chadd  */
3036520495aSAdrian Chadd int
304415d7ccaSAdrian Chadd vm_phys_mem_affinity(int f, int t)
305415d7ccaSAdrian Chadd {
306415d7ccaSAdrian Chadd 
307b6715dabSJeff Roberson #ifdef NUMA
308415d7ccaSAdrian Chadd 	if (mem_locality == NULL)
309415d7ccaSAdrian Chadd 		return (-1);
310415d7ccaSAdrian Chadd 	if (f >= vm_ndomains || t >= vm_ndomains)
311415d7ccaSAdrian Chadd 		return (-1);
312415d7ccaSAdrian Chadd 	return (mem_locality[f * vm_ndomains + t]);
3136520495aSAdrian Chadd #else
3146520495aSAdrian Chadd 	return (-1);
3156520495aSAdrian Chadd #endif
316415d7ccaSAdrian Chadd }
317415d7ccaSAdrian Chadd 
318b6715dabSJeff Roberson #ifdef NUMA
319415d7ccaSAdrian Chadd /*
320415d7ccaSAdrian Chadd  * Outputs the VM locality table.
321415d7ccaSAdrian Chadd  */
322415d7ccaSAdrian Chadd static int
323415d7ccaSAdrian Chadd sysctl_vm_phys_locality(SYSCTL_HANDLER_ARGS)
324415d7ccaSAdrian Chadd {
325415d7ccaSAdrian Chadd 	struct sbuf sbuf;
326415d7ccaSAdrian Chadd 	int error, i, j;
327415d7ccaSAdrian Chadd 
328415d7ccaSAdrian Chadd 	error = sysctl_wire_old_buffer(req, 0);
329415d7ccaSAdrian Chadd 	if (error != 0)
330415d7ccaSAdrian Chadd 		return (error);
331415d7ccaSAdrian Chadd 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
332415d7ccaSAdrian Chadd 
333415d7ccaSAdrian Chadd 	sbuf_printf(&sbuf, "\n");
334415d7ccaSAdrian Chadd 
335415d7ccaSAdrian Chadd 	for (i = 0; i < vm_ndomains; i++) {
336415d7ccaSAdrian Chadd 		sbuf_printf(&sbuf, "%d: ", i);
337415d7ccaSAdrian Chadd 		for (j = 0; j < vm_ndomains; j++) {
338415d7ccaSAdrian Chadd 			sbuf_printf(&sbuf, "%d ", vm_phys_mem_affinity(i, j));
339415d7ccaSAdrian Chadd 		}
340415d7ccaSAdrian Chadd 		sbuf_printf(&sbuf, "\n");
341415d7ccaSAdrian Chadd 	}
342415d7ccaSAdrian Chadd 	error = sbuf_finish(&sbuf);
343415d7ccaSAdrian Chadd 	sbuf_delete(&sbuf);
344415d7ccaSAdrian Chadd 	return (error);
345415d7ccaSAdrian Chadd }
3466520495aSAdrian Chadd #endif
347415d7ccaSAdrian Chadd 
3487e226537SAttilio Rao static void
3497e226537SAttilio Rao vm_freelist_add(struct vm_freelist *fl, vm_page_t m, int order, int tail)
350a3870a18SJohn Baldwin {
351a3870a18SJohn Baldwin 
3527e226537SAttilio Rao 	m->order = order;
3537e226537SAttilio Rao 	if (tail)
3545cd29d0fSMark Johnston 		TAILQ_INSERT_TAIL(&fl[order].pl, m, listq);
3557e226537SAttilio Rao 	else
3565cd29d0fSMark Johnston 		TAILQ_INSERT_HEAD(&fl[order].pl, m, listq);
3577e226537SAttilio Rao 	fl[order].lcnt++;
358a3870a18SJohn Baldwin }
3597e226537SAttilio Rao 
3607e226537SAttilio Rao static void
3617e226537SAttilio Rao vm_freelist_rem(struct vm_freelist *fl, vm_page_t m, int order)
3627e226537SAttilio Rao {
3637e226537SAttilio Rao 
3645cd29d0fSMark Johnston 	TAILQ_REMOVE(&fl[order].pl, m, listq);
3657e226537SAttilio Rao 	fl[order].lcnt--;
3667e226537SAttilio Rao 	m->order = VM_NFREEORDER;
367a3870a18SJohn Baldwin }
368a3870a18SJohn Baldwin 
36911752d88SAlan Cox /*
37011752d88SAlan Cox  * Create a physical memory segment.
37111752d88SAlan Cox  */
37211752d88SAlan Cox static void
373d866a563SAlan Cox _vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end, int domain)
37411752d88SAlan Cox {
37511752d88SAlan Cox 	struct vm_phys_seg *seg;
37611752d88SAlan Cox 
37711752d88SAlan Cox 	KASSERT(vm_phys_nsegs < VM_PHYSSEG_MAX,
37811752d88SAlan Cox 	    ("vm_phys_create_seg: increase VM_PHYSSEG_MAX"));
379ef435ae7SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
3807e226537SAttilio Rao 	    ("vm_phys_create_seg: invalid domain provided"));
38111752d88SAlan Cox 	seg = &vm_phys_segs[vm_phys_nsegs++];
382271f0f12SAlan Cox 	while (seg > vm_phys_segs && (seg - 1)->start >= end) {
383271f0f12SAlan Cox 		*seg = *(seg - 1);
384271f0f12SAlan Cox 		seg--;
385271f0f12SAlan Cox 	}
38611752d88SAlan Cox 	seg->start = start;
38711752d88SAlan Cox 	seg->end = end;
388a3870a18SJohn Baldwin 	seg->domain = domain;
38911752d88SAlan Cox }
39011752d88SAlan Cox 
391a3870a18SJohn Baldwin static void
392d866a563SAlan Cox vm_phys_create_seg(vm_paddr_t start, vm_paddr_t end)
393a3870a18SJohn Baldwin {
394b6715dabSJeff Roberson #ifdef NUMA
395a3870a18SJohn Baldwin 	int i;
396a3870a18SJohn Baldwin 
397a3870a18SJohn Baldwin 	if (mem_affinity == NULL) {
398d866a563SAlan Cox 		_vm_phys_create_seg(start, end, 0);
399a3870a18SJohn Baldwin 		return;
400a3870a18SJohn Baldwin 	}
401a3870a18SJohn Baldwin 
402a3870a18SJohn Baldwin 	for (i = 0;; i++) {
403a3870a18SJohn Baldwin 		if (mem_affinity[i].end == 0)
404a3870a18SJohn Baldwin 			panic("Reached end of affinity info");
405a3870a18SJohn Baldwin 		if (mem_affinity[i].end <= start)
406a3870a18SJohn Baldwin 			continue;
407a3870a18SJohn Baldwin 		if (mem_affinity[i].start > start)
408a3870a18SJohn Baldwin 			panic("No affinity info for start %jx",
409a3870a18SJohn Baldwin 			    (uintmax_t)start);
410a3870a18SJohn Baldwin 		if (mem_affinity[i].end >= end) {
411d866a563SAlan Cox 			_vm_phys_create_seg(start, end,
412a3870a18SJohn Baldwin 			    mem_affinity[i].domain);
413a3870a18SJohn Baldwin 			break;
414a3870a18SJohn Baldwin 		}
415d866a563SAlan Cox 		_vm_phys_create_seg(start, mem_affinity[i].end,
416a3870a18SJohn Baldwin 		    mem_affinity[i].domain);
417a3870a18SJohn Baldwin 		start = mem_affinity[i].end;
418a3870a18SJohn Baldwin 	}
41962d70a81SJohn Baldwin #else
42062d70a81SJohn Baldwin 	_vm_phys_create_seg(start, end, 0);
42162d70a81SJohn Baldwin #endif
422a3870a18SJohn Baldwin }
423a3870a18SJohn Baldwin 
42411752d88SAlan Cox /*
425271f0f12SAlan Cox  * Add a physical memory segment.
426271f0f12SAlan Cox  */
427271f0f12SAlan Cox void
428271f0f12SAlan Cox vm_phys_add_seg(vm_paddr_t start, vm_paddr_t end)
429271f0f12SAlan Cox {
430d866a563SAlan Cox 	vm_paddr_t paddr;
431271f0f12SAlan Cox 
432271f0f12SAlan Cox 	KASSERT((start & PAGE_MASK) == 0,
433271f0f12SAlan Cox 	    ("vm_phys_define_seg: start is not page aligned"));
434271f0f12SAlan Cox 	KASSERT((end & PAGE_MASK) == 0,
435271f0f12SAlan Cox 	    ("vm_phys_define_seg: end is not page aligned"));
436d866a563SAlan Cox 
437d866a563SAlan Cox 	/*
438d866a563SAlan Cox 	 * Split the physical memory segment if it spans two or more free
439d866a563SAlan Cox 	 * list boundaries.
440d866a563SAlan Cox 	 */
441d866a563SAlan Cox 	paddr = start;
442d866a563SAlan Cox #ifdef	VM_FREELIST_LOWMEM
443d866a563SAlan Cox 	if (paddr < VM_LOWMEM_BOUNDARY && end > VM_LOWMEM_BOUNDARY) {
444d866a563SAlan Cox 		vm_phys_create_seg(paddr, VM_LOWMEM_BOUNDARY);
445d866a563SAlan Cox 		paddr = VM_LOWMEM_BOUNDARY;
446d866a563SAlan Cox 	}
447271f0f12SAlan Cox #endif
448d866a563SAlan Cox #ifdef	VM_FREELIST_DMA32
449d866a563SAlan Cox 	if (paddr < VM_DMA32_BOUNDARY && end > VM_DMA32_BOUNDARY) {
450d866a563SAlan Cox 		vm_phys_create_seg(paddr, VM_DMA32_BOUNDARY);
451d866a563SAlan Cox 		paddr = VM_DMA32_BOUNDARY;
452d866a563SAlan Cox 	}
453d866a563SAlan Cox #endif
454d866a563SAlan Cox 	vm_phys_create_seg(paddr, end);
455271f0f12SAlan Cox }
456271f0f12SAlan Cox 
457271f0f12SAlan Cox /*
45811752d88SAlan Cox  * Initialize the physical memory allocator.
459d866a563SAlan Cox  *
460d866a563SAlan Cox  * Requires that vm_page_array is initialized!
46111752d88SAlan Cox  */
46211752d88SAlan Cox void
46311752d88SAlan Cox vm_phys_init(void)
46411752d88SAlan Cox {
46511752d88SAlan Cox 	struct vm_freelist *fl;
46672aebdd7SAlan Cox 	struct vm_phys_seg *end_seg, *prev_seg, *seg, *tmp_seg;
467d866a563SAlan Cox 	u_long npages;
468d866a563SAlan Cox 	int dom, flind, freelist, oind, pind, segind;
46911752d88SAlan Cox 
470d866a563SAlan Cox 	/*
471d866a563SAlan Cox 	 * Compute the number of free lists, and generate the mapping from the
472d866a563SAlan Cox 	 * manifest constants VM_FREELIST_* to the free list indices.
473d866a563SAlan Cox 	 *
474d866a563SAlan Cox 	 * Initially, the entries of vm_freelist_to_flind[] are set to either
475d866a563SAlan Cox 	 * 0 or 1 to indicate which free lists should be created.
476d866a563SAlan Cox 	 */
477d866a563SAlan Cox 	npages = 0;
478d866a563SAlan Cox 	for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
479d866a563SAlan Cox 		seg = &vm_phys_segs[segind];
480d866a563SAlan Cox #ifdef	VM_FREELIST_LOWMEM
481d866a563SAlan Cox 		if (seg->end <= VM_LOWMEM_BOUNDARY)
482d866a563SAlan Cox 			vm_freelist_to_flind[VM_FREELIST_LOWMEM] = 1;
483d866a563SAlan Cox 		else
484d866a563SAlan Cox #endif
485d866a563SAlan Cox #ifdef	VM_FREELIST_DMA32
486d866a563SAlan Cox 		if (
487d866a563SAlan Cox #ifdef	VM_DMA32_NPAGES_THRESHOLD
488d866a563SAlan Cox 		    /*
489d866a563SAlan Cox 		     * Create the DMA32 free list only if the amount of
490d866a563SAlan Cox 		     * physical memory above physical address 4G exceeds the
491d866a563SAlan Cox 		     * given threshold.
492d866a563SAlan Cox 		     */
493d866a563SAlan Cox 		    npages > VM_DMA32_NPAGES_THRESHOLD &&
494d866a563SAlan Cox #endif
495d866a563SAlan Cox 		    seg->end <= VM_DMA32_BOUNDARY)
496d866a563SAlan Cox 			vm_freelist_to_flind[VM_FREELIST_DMA32] = 1;
497d866a563SAlan Cox 		else
498d866a563SAlan Cox #endif
499d866a563SAlan Cox 		{
500d866a563SAlan Cox 			npages += atop(seg->end - seg->start);
501d866a563SAlan Cox 			vm_freelist_to_flind[VM_FREELIST_DEFAULT] = 1;
502d866a563SAlan Cox 		}
503d866a563SAlan Cox 	}
504d866a563SAlan Cox 	/* Change each entry into a running total of the free lists. */
505d866a563SAlan Cox 	for (freelist = 1; freelist < VM_NFREELIST; freelist++) {
506d866a563SAlan Cox 		vm_freelist_to_flind[freelist] +=
507d866a563SAlan Cox 		    vm_freelist_to_flind[freelist - 1];
508d866a563SAlan Cox 	}
509d866a563SAlan Cox 	vm_nfreelists = vm_freelist_to_flind[VM_NFREELIST - 1];
510d866a563SAlan Cox 	KASSERT(vm_nfreelists > 0, ("vm_phys_init: no free lists"));
511d866a563SAlan Cox 	/* Change each entry into a free list index. */
512d866a563SAlan Cox 	for (freelist = 0; freelist < VM_NFREELIST; freelist++)
513d866a563SAlan Cox 		vm_freelist_to_flind[freelist]--;
514d866a563SAlan Cox 
515d866a563SAlan Cox 	/*
516d866a563SAlan Cox 	 * Initialize the first_page and free_queues fields of each physical
517d866a563SAlan Cox 	 * memory segment.
518d866a563SAlan Cox 	 */
519271f0f12SAlan Cox #ifdef VM_PHYSSEG_SPARSE
520d866a563SAlan Cox 	npages = 0;
52111752d88SAlan Cox #endif
522271f0f12SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
523271f0f12SAlan Cox 		seg = &vm_phys_segs[segind];
524271f0f12SAlan Cox #ifdef VM_PHYSSEG_SPARSE
525d866a563SAlan Cox 		seg->first_page = &vm_page_array[npages];
526d866a563SAlan Cox 		npages += atop(seg->end - seg->start);
527271f0f12SAlan Cox #else
528271f0f12SAlan Cox 		seg->first_page = PHYS_TO_VM_PAGE(seg->start);
52911752d88SAlan Cox #endif
530d866a563SAlan Cox #ifdef	VM_FREELIST_LOWMEM
531d866a563SAlan Cox 		if (seg->end <= VM_LOWMEM_BOUNDARY) {
532d866a563SAlan Cox 			flind = vm_freelist_to_flind[VM_FREELIST_LOWMEM];
533d866a563SAlan Cox 			KASSERT(flind >= 0,
534d866a563SAlan Cox 			    ("vm_phys_init: LOWMEM flind < 0"));
535d866a563SAlan Cox 		} else
536d866a563SAlan Cox #endif
537d866a563SAlan Cox #ifdef	VM_FREELIST_DMA32
538d866a563SAlan Cox 		if (seg->end <= VM_DMA32_BOUNDARY) {
539d866a563SAlan Cox 			flind = vm_freelist_to_flind[VM_FREELIST_DMA32];
540d866a563SAlan Cox 			KASSERT(flind >= 0,
541d866a563SAlan Cox 			    ("vm_phys_init: DMA32 flind < 0"));
542d866a563SAlan Cox 		} else
543d866a563SAlan Cox #endif
544d866a563SAlan Cox 		{
545d866a563SAlan Cox 			flind = vm_freelist_to_flind[VM_FREELIST_DEFAULT];
546d866a563SAlan Cox 			KASSERT(flind >= 0,
547d866a563SAlan Cox 			    ("vm_phys_init: DEFAULT flind < 0"));
54811752d88SAlan Cox 		}
549d866a563SAlan Cox 		seg->free_queues = &vm_phys_free_queues[seg->domain][flind];
550d866a563SAlan Cox 	}
551d866a563SAlan Cox 
552d866a563SAlan Cox 	/*
55372aebdd7SAlan Cox 	 * Coalesce physical memory segments that are contiguous and share the
55472aebdd7SAlan Cox 	 * same per-domain free queues.
55572aebdd7SAlan Cox 	 */
55672aebdd7SAlan Cox 	prev_seg = vm_phys_segs;
55772aebdd7SAlan Cox 	seg = &vm_phys_segs[1];
55872aebdd7SAlan Cox 	end_seg = &vm_phys_segs[vm_phys_nsegs];
55972aebdd7SAlan Cox 	while (seg < end_seg) {
56072aebdd7SAlan Cox 		if (prev_seg->end == seg->start &&
56172aebdd7SAlan Cox 		    prev_seg->free_queues == seg->free_queues) {
56272aebdd7SAlan Cox 			prev_seg->end = seg->end;
56372aebdd7SAlan Cox 			KASSERT(prev_seg->domain == seg->domain,
56472aebdd7SAlan Cox 			    ("vm_phys_init: free queues cannot span domains"));
56572aebdd7SAlan Cox 			vm_phys_nsegs--;
56672aebdd7SAlan Cox 			end_seg--;
56772aebdd7SAlan Cox 			for (tmp_seg = seg; tmp_seg < end_seg; tmp_seg++)
56872aebdd7SAlan Cox 				*tmp_seg = *(tmp_seg + 1);
56972aebdd7SAlan Cox 		} else {
57072aebdd7SAlan Cox 			prev_seg = seg;
57172aebdd7SAlan Cox 			seg++;
57272aebdd7SAlan Cox 		}
57372aebdd7SAlan Cox 	}
57472aebdd7SAlan Cox 
57572aebdd7SAlan Cox 	/*
576d866a563SAlan Cox 	 * Initialize the free queues.
577d866a563SAlan Cox 	 */
5787e226537SAttilio Rao 	for (dom = 0; dom < vm_ndomains; dom++) {
57911752d88SAlan Cox 		for (flind = 0; flind < vm_nfreelists; flind++) {
58011752d88SAlan Cox 			for (pind = 0; pind < VM_NFREEPOOL; pind++) {
5817e226537SAttilio Rao 				fl = vm_phys_free_queues[dom][flind][pind];
58211752d88SAlan Cox 				for (oind = 0; oind < VM_NFREEORDER; oind++)
58311752d88SAlan Cox 					TAILQ_INIT(&fl[oind].pl);
58411752d88SAlan Cox 			}
58511752d88SAlan Cox 		}
586a3870a18SJohn Baldwin 	}
587d866a563SAlan Cox 
58838d6b2dcSRoger Pau Monné 	rw_init(&vm_phys_fictitious_reg_lock, "vmfctr");
58911752d88SAlan Cox }
59011752d88SAlan Cox 
59111752d88SAlan Cox /*
592662e7fa8SMark Johnston  * Register info about the NUMA topology of the system.
593662e7fa8SMark Johnston  *
594662e7fa8SMark Johnston  * Invoked by platform-dependent code prior to vm_phys_init().
595662e7fa8SMark Johnston  */
596662e7fa8SMark Johnston void
597662e7fa8SMark Johnston vm_phys_register_domains(int ndomains, struct mem_affinity *affinity,
598662e7fa8SMark Johnston     int *locality)
599662e7fa8SMark Johnston {
600662e7fa8SMark Johnston #ifdef NUMA
601b61f3142SMark Johnston 	int d, i;
602662e7fa8SMark Johnston 
603b61f3142SMark Johnston 	/*
604b61f3142SMark Johnston 	 * For now the only override value that we support is 1, which
605b61f3142SMark Johnston 	 * effectively disables NUMA-awareness in the allocators.
606b61f3142SMark Johnston 	 */
607b61f3142SMark Johnston 	d = 0;
608b61f3142SMark Johnston 	TUNABLE_INT_FETCH("vm.numa.disabled", &d);
609b61f3142SMark Johnston 	if (d)
610b61f3142SMark Johnston 		ndomains = 1;
611b61f3142SMark Johnston 
612b61f3142SMark Johnston 	if (ndomains > 1) {
613662e7fa8SMark Johnston 		vm_ndomains = ndomains;
614662e7fa8SMark Johnston 		mem_affinity = affinity;
615662e7fa8SMark Johnston 		mem_locality = locality;
616b61f3142SMark Johnston 	}
617662e7fa8SMark Johnston 
618662e7fa8SMark Johnston 	for (i = 0; i < vm_ndomains; i++)
619662e7fa8SMark Johnston 		DOMAINSET_SET(i, &all_domains);
620662e7fa8SMark Johnston #else
621662e7fa8SMark Johnston 	(void)ndomains;
622662e7fa8SMark Johnston 	(void)affinity;
623662e7fa8SMark Johnston 	(void)locality;
624662e7fa8SMark Johnston #endif
625662e7fa8SMark Johnston }
626662e7fa8SMark Johnston 
627662e7fa8SMark Johnston /*
62811752d88SAlan Cox  * Split a contiguous, power of two-sized set of physical pages.
629370a338aSAlan Cox  *
630370a338aSAlan Cox  * When this function is called by a page allocation function, the caller
631370a338aSAlan Cox  * should request insertion at the head unless the order [order, oind) queues
632370a338aSAlan Cox  * are known to be empty.  The objective being to reduce the likelihood of
633370a338aSAlan Cox  * long-term fragmentation by promoting contemporaneous allocation and
634370a338aSAlan Cox  * (hopefully) deallocation.
63511752d88SAlan Cox  */
63611752d88SAlan Cox static __inline void
637370a338aSAlan Cox vm_phys_split_pages(vm_page_t m, int oind, struct vm_freelist *fl, int order,
638370a338aSAlan Cox     int tail)
63911752d88SAlan Cox {
64011752d88SAlan Cox 	vm_page_t m_buddy;
64111752d88SAlan Cox 
64211752d88SAlan Cox 	while (oind > order) {
64311752d88SAlan Cox 		oind--;
64411752d88SAlan Cox 		m_buddy = &m[1 << oind];
64511752d88SAlan Cox 		KASSERT(m_buddy->order == VM_NFREEORDER,
64611752d88SAlan Cox 		    ("vm_phys_split_pages: page %p has unexpected order %d",
64711752d88SAlan Cox 		    m_buddy, m_buddy->order));
648370a338aSAlan Cox 		vm_freelist_add(fl, m_buddy, oind, tail);
64911752d88SAlan Cox         }
65011752d88SAlan Cox }
65111752d88SAlan Cox 
65211752d88SAlan Cox /*
6537493904eSAlan Cox  * Add the physical pages [m, m + npages) at the end of a power-of-two aligned
6547493904eSAlan Cox  * and sized set to the specified free list.
6557493904eSAlan Cox  *
6567493904eSAlan Cox  * When this function is called by a page allocation function, the caller
6577493904eSAlan Cox  * should request insertion at the head unless the lower-order queues are
6587493904eSAlan Cox  * known to be empty.  The objective being to reduce the likelihood of long-
6597493904eSAlan Cox  * term fragmentation by promoting contemporaneous allocation and (hopefully)
6607493904eSAlan Cox  * deallocation.
6617493904eSAlan Cox  *
6627493904eSAlan Cox  * The physical page m's buddy must not be free.
6637493904eSAlan Cox  */
6647493904eSAlan Cox static void
6657493904eSAlan Cox vm_phys_enq_range(vm_page_t m, u_int npages, struct vm_freelist *fl, int tail)
6667493904eSAlan Cox {
6677493904eSAlan Cox 	u_int n;
6687493904eSAlan Cox 	int order;
6697493904eSAlan Cox 
6707493904eSAlan Cox 	KASSERT(npages > 0, ("vm_phys_enq_range: npages is 0"));
6717493904eSAlan Cox 	KASSERT(((VM_PAGE_TO_PHYS(m) + npages * PAGE_SIZE) &
6727493904eSAlan Cox 	    ((PAGE_SIZE << (fls(npages) - 1)) - 1)) == 0,
6737493904eSAlan Cox 	    ("vm_phys_enq_range: page %p and npages %u are misaligned",
6747493904eSAlan Cox 	    m, npages));
6757493904eSAlan Cox 	do {
6767493904eSAlan Cox 		KASSERT(m->order == VM_NFREEORDER,
6777493904eSAlan Cox 		    ("vm_phys_enq_range: page %p has unexpected order %d",
6787493904eSAlan Cox 		    m, m->order));
6797493904eSAlan Cox 		order = ffs(npages) - 1;
6807493904eSAlan Cox 		KASSERT(order < VM_NFREEORDER,
6817493904eSAlan Cox 		    ("vm_phys_enq_range: order %d is out of range", order));
6827493904eSAlan Cox 		vm_freelist_add(fl, m, order, tail);
6837493904eSAlan Cox 		n = 1 << order;
6847493904eSAlan Cox 		m += n;
6857493904eSAlan Cox 		npages -= n;
6867493904eSAlan Cox 	} while (npages > 0);
6877493904eSAlan Cox }
6887493904eSAlan Cox 
6897493904eSAlan Cox /*
69089ea39a7SAlan Cox  * Tries to allocate the specified number of pages from the specified pool
69189ea39a7SAlan Cox  * within the specified domain.  Returns the actual number of allocated pages
69289ea39a7SAlan Cox  * and a pointer to each page through the array ma[].
69389ea39a7SAlan Cox  *
69432d81f21SAlan Cox  * The returned pages may not be physically contiguous.  However, in contrast
69532d81f21SAlan Cox  * to performing multiple, back-to-back calls to vm_phys_alloc_pages(..., 0),
69632d81f21SAlan Cox  * calling this function once to allocate the desired number of pages will
69732d81f21SAlan Cox  * avoid wasted time in vm_phys_split_pages().
69889ea39a7SAlan Cox  *
69989ea39a7SAlan Cox  * The free page queues for the specified domain must be locked.
70089ea39a7SAlan Cox  */
70189ea39a7SAlan Cox int
70289ea39a7SAlan Cox vm_phys_alloc_npages(int domain, int pool, int npages, vm_page_t ma[])
70389ea39a7SAlan Cox {
70489ea39a7SAlan Cox 	struct vm_freelist *alt, *fl;
70589ea39a7SAlan Cox 	vm_page_t m;
70689ea39a7SAlan Cox 	int avail, end, flind, freelist, i, need, oind, pind;
70789ea39a7SAlan Cox 
70889ea39a7SAlan Cox 	KASSERT(domain >= 0 && domain < vm_ndomains,
70989ea39a7SAlan Cox 	    ("vm_phys_alloc_npages: domain %d is out of range", domain));
71089ea39a7SAlan Cox 	KASSERT(pool < VM_NFREEPOOL,
71189ea39a7SAlan Cox 	    ("vm_phys_alloc_npages: pool %d is out of range", pool));
71289ea39a7SAlan Cox 	KASSERT(npages <= 1 << (VM_NFREEORDER - 1),
71389ea39a7SAlan Cox 	    ("vm_phys_alloc_npages: npages %d is out of range", npages));
71489ea39a7SAlan Cox 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
71589ea39a7SAlan Cox 	i = 0;
71689ea39a7SAlan Cox 	for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
71789ea39a7SAlan Cox 		flind = vm_freelist_to_flind[freelist];
71889ea39a7SAlan Cox 		if (flind < 0)
71989ea39a7SAlan Cox 			continue;
72089ea39a7SAlan Cox 		fl = vm_phys_free_queues[domain][flind][pool];
72189ea39a7SAlan Cox 		for (oind = 0; oind < VM_NFREEORDER; oind++) {
72289ea39a7SAlan Cox 			while ((m = TAILQ_FIRST(&fl[oind].pl)) != NULL) {
72389ea39a7SAlan Cox 				vm_freelist_rem(fl, m, oind);
72489ea39a7SAlan Cox 				avail = 1 << oind;
72589ea39a7SAlan Cox 				need = imin(npages - i, avail);
72689ea39a7SAlan Cox 				for (end = i + need; i < end;)
72789ea39a7SAlan Cox 					ma[i++] = m++;
72889ea39a7SAlan Cox 				if (need < avail) {
7297493904eSAlan Cox 					/*
7307493904eSAlan Cox 					 * Return excess pages to fl.  Its
7317493904eSAlan Cox 					 * order [0, oind) queues are empty.
7327493904eSAlan Cox 					 */
7337493904eSAlan Cox 					vm_phys_enq_range(m, avail - need, fl,
7347493904eSAlan Cox 					    1);
73589ea39a7SAlan Cox 					return (npages);
73689ea39a7SAlan Cox 				} else if (i == npages)
73789ea39a7SAlan Cox 					return (npages);
73889ea39a7SAlan Cox 			}
73989ea39a7SAlan Cox 		}
74089ea39a7SAlan Cox 		for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
74189ea39a7SAlan Cox 			for (pind = 0; pind < VM_NFREEPOOL; pind++) {
74289ea39a7SAlan Cox 				alt = vm_phys_free_queues[domain][flind][pind];
74389ea39a7SAlan Cox 				while ((m = TAILQ_FIRST(&alt[oind].pl)) !=
74489ea39a7SAlan Cox 				    NULL) {
74589ea39a7SAlan Cox 					vm_freelist_rem(alt, m, oind);
74689ea39a7SAlan Cox 					vm_phys_set_pool(pool, m, oind);
74789ea39a7SAlan Cox 					avail = 1 << oind;
74889ea39a7SAlan Cox 					need = imin(npages - i, avail);
74989ea39a7SAlan Cox 					for (end = i + need; i < end;)
75089ea39a7SAlan Cox 						ma[i++] = m++;
75189ea39a7SAlan Cox 					if (need < avail) {
7527493904eSAlan Cox 						/*
7537493904eSAlan Cox 						 * Return excess pages to fl.
7547493904eSAlan Cox 						 * Its order [0, oind) queues
7557493904eSAlan Cox 						 * are empty.
7567493904eSAlan Cox 						 */
7577493904eSAlan Cox 						vm_phys_enq_range(m, avail -
7587493904eSAlan Cox 						    need, fl, 1);
75989ea39a7SAlan Cox 						return (npages);
76089ea39a7SAlan Cox 					} else if (i == npages)
76189ea39a7SAlan Cox 						return (npages);
76289ea39a7SAlan Cox 				}
76389ea39a7SAlan Cox 			}
76489ea39a7SAlan Cox 		}
76589ea39a7SAlan Cox 	}
76689ea39a7SAlan Cox 	return (i);
76789ea39a7SAlan Cox }
76889ea39a7SAlan Cox 
76989ea39a7SAlan Cox /*
77011752d88SAlan Cox  * Allocate a contiguous, power of two-sized set of physical pages
77111752d88SAlan Cox  * from the free lists.
7728941dc44SAlan Cox  *
7738941dc44SAlan Cox  * The free page queues must be locked.
77411752d88SAlan Cox  */
77511752d88SAlan Cox vm_page_t
776ef435ae7SJeff Roberson vm_phys_alloc_pages(int domain, int pool, int order)
77711752d88SAlan Cox {
77849ca10d4SJayachandran C. 	vm_page_t m;
7790db2102aSMichael Zhilin 	int freelist;
78049ca10d4SJayachandran C. 
7810db2102aSMichael Zhilin 	for (freelist = 0; freelist < VM_NFREELIST; freelist++) {
7820db2102aSMichael Zhilin 		m = vm_phys_alloc_freelist_pages(domain, freelist, pool, order);
78349ca10d4SJayachandran C. 		if (m != NULL)
78449ca10d4SJayachandran C. 			return (m);
78549ca10d4SJayachandran C. 	}
78649ca10d4SJayachandran C. 	return (NULL);
78749ca10d4SJayachandran C. }
78849ca10d4SJayachandran C. 
78949ca10d4SJayachandran C. /*
790d866a563SAlan Cox  * Allocate a contiguous, power of two-sized set of physical pages from the
791d866a563SAlan Cox  * specified free list.  The free list must be specified using one of the
792d866a563SAlan Cox  * manifest constants VM_FREELIST_*.
793d866a563SAlan Cox  *
794d866a563SAlan Cox  * The free page queues must be locked.
79549ca10d4SJayachandran C.  */
79649ca10d4SJayachandran C. vm_page_t
7970db2102aSMichael Zhilin vm_phys_alloc_freelist_pages(int domain, int freelist, int pool, int order)
79849ca10d4SJayachandran C. {
799ef435ae7SJeff Roberson 	struct vm_freelist *alt, *fl;
80011752d88SAlan Cox 	vm_page_t m;
8010db2102aSMichael Zhilin 	int oind, pind, flind;
80211752d88SAlan Cox 
803ef435ae7SJeff Roberson 	KASSERT(domain >= 0 && domain < vm_ndomains,
804ef435ae7SJeff Roberson 	    ("vm_phys_alloc_freelist_pages: domain %d is out of range",
805ef435ae7SJeff Roberson 	    domain));
8060db2102aSMichael Zhilin 	KASSERT(freelist < VM_NFREELIST,
807d866a563SAlan Cox 	    ("vm_phys_alloc_freelist_pages: freelist %d is out of range",
8085be93778SAndrew Turner 	    freelist));
80911752d88SAlan Cox 	KASSERT(pool < VM_NFREEPOOL,
81049ca10d4SJayachandran C. 	    ("vm_phys_alloc_freelist_pages: pool %d is out of range", pool));
81111752d88SAlan Cox 	KASSERT(order < VM_NFREEORDER,
81249ca10d4SJayachandran C. 	    ("vm_phys_alloc_freelist_pages: order %d is out of range", order));
8136520495aSAdrian Chadd 
8140db2102aSMichael Zhilin 	flind = vm_freelist_to_flind[freelist];
8150db2102aSMichael Zhilin 	/* Check if freelist is present */
8160db2102aSMichael Zhilin 	if (flind < 0)
8170db2102aSMichael Zhilin 		return (NULL);
8180db2102aSMichael Zhilin 
819e2068d0bSJeff Roberson 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
8207e226537SAttilio Rao 	fl = &vm_phys_free_queues[domain][flind][pool][0];
82111752d88SAlan Cox 	for (oind = order; oind < VM_NFREEORDER; oind++) {
82211752d88SAlan Cox 		m = TAILQ_FIRST(&fl[oind].pl);
82311752d88SAlan Cox 		if (m != NULL) {
8247e226537SAttilio Rao 			vm_freelist_rem(fl, m, oind);
825370a338aSAlan Cox 			/* The order [order, oind) queues are empty. */
826370a338aSAlan Cox 			vm_phys_split_pages(m, oind, fl, order, 1);
82711752d88SAlan Cox 			return (m);
82811752d88SAlan Cox 		}
82911752d88SAlan Cox 	}
83011752d88SAlan Cox 
83111752d88SAlan Cox 	/*
83211752d88SAlan Cox 	 * The given pool was empty.  Find the largest
83311752d88SAlan Cox 	 * contiguous, power-of-two-sized set of pages in any
83411752d88SAlan Cox 	 * pool.  Transfer these pages to the given pool, and
83511752d88SAlan Cox 	 * use them to satisfy the allocation.
83611752d88SAlan Cox 	 */
83711752d88SAlan Cox 	for (oind = VM_NFREEORDER - 1; oind >= order; oind--) {
83811752d88SAlan Cox 		for (pind = 0; pind < VM_NFREEPOOL; pind++) {
8397e226537SAttilio Rao 			alt = &vm_phys_free_queues[domain][flind][pind][0];
84011752d88SAlan Cox 			m = TAILQ_FIRST(&alt[oind].pl);
84111752d88SAlan Cox 			if (m != NULL) {
8427e226537SAttilio Rao 				vm_freelist_rem(alt, m, oind);
84311752d88SAlan Cox 				vm_phys_set_pool(pool, m, oind);
844370a338aSAlan Cox 				/* The order [order, oind) queues are empty. */
845370a338aSAlan Cox 				vm_phys_split_pages(m, oind, fl, order, 1);
84611752d88SAlan Cox 				return (m);
84711752d88SAlan Cox 			}
84811752d88SAlan Cox 		}
84911752d88SAlan Cox 	}
85011752d88SAlan Cox 	return (NULL);
85111752d88SAlan Cox }
85211752d88SAlan Cox 
85311752d88SAlan Cox /*
85411752d88SAlan Cox  * Find the vm_page corresponding to the given physical address.
85511752d88SAlan Cox  */
85611752d88SAlan Cox vm_page_t
85711752d88SAlan Cox vm_phys_paddr_to_vm_page(vm_paddr_t pa)
85811752d88SAlan Cox {
85911752d88SAlan Cox 	struct vm_phys_seg *seg;
86011752d88SAlan Cox 	int segind;
86111752d88SAlan Cox 
86211752d88SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
86311752d88SAlan Cox 		seg = &vm_phys_segs[segind];
86411752d88SAlan Cox 		if (pa >= seg->start && pa < seg->end)
86511752d88SAlan Cox 			return (&seg->first_page[atop(pa - seg->start)]);
86611752d88SAlan Cox 	}
867f06a3a36SAndrew Thompson 	return (NULL);
86811752d88SAlan Cox }
86911752d88SAlan Cox 
870b6de32bdSKonstantin Belousov vm_page_t
871b6de32bdSKonstantin Belousov vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
872b6de32bdSKonstantin Belousov {
87338d6b2dcSRoger Pau Monné 	struct vm_phys_fictitious_seg tmp, *seg;
874b6de32bdSKonstantin Belousov 	vm_page_t m;
875b6de32bdSKonstantin Belousov 
876b6de32bdSKonstantin Belousov 	m = NULL;
87738d6b2dcSRoger Pau Monné 	tmp.start = pa;
87838d6b2dcSRoger Pau Monné 	tmp.end = 0;
87938d6b2dcSRoger Pau Monné 
88038d6b2dcSRoger Pau Monné 	rw_rlock(&vm_phys_fictitious_reg_lock);
88138d6b2dcSRoger Pau Monné 	seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
88238d6b2dcSRoger Pau Monné 	rw_runlock(&vm_phys_fictitious_reg_lock);
88338d6b2dcSRoger Pau Monné 	if (seg == NULL)
88438d6b2dcSRoger Pau Monné 		return (NULL);
88538d6b2dcSRoger Pau Monné 
886b6de32bdSKonstantin Belousov 	m = &seg->first_page[atop(pa - seg->start)];
88738d6b2dcSRoger Pau Monné 	KASSERT((m->flags & PG_FICTITIOUS) != 0, ("%p not fictitious", m));
88838d6b2dcSRoger Pau Monné 
889b6de32bdSKonstantin Belousov 	return (m);
890b6de32bdSKonstantin Belousov }
891b6de32bdSKonstantin Belousov 
8925ebe728dSRoger Pau Monné static inline void
8935ebe728dSRoger Pau Monné vm_phys_fictitious_init_range(vm_page_t range, vm_paddr_t start,
8945ebe728dSRoger Pau Monné     long page_count, vm_memattr_t memattr)
8955ebe728dSRoger Pau Monné {
8965ebe728dSRoger Pau Monné 	long i;
8975ebe728dSRoger Pau Monné 
898f93f7cf1SMark Johnston 	bzero(range, page_count * sizeof(*range));
8995ebe728dSRoger Pau Monné 	for (i = 0; i < page_count; i++) {
9005ebe728dSRoger Pau Monné 		vm_page_initfake(&range[i], start + PAGE_SIZE * i, memattr);
9015ebe728dSRoger Pau Monné 		range[i].oflags &= ~VPO_UNMANAGED;
9025ebe728dSRoger Pau Monné 		range[i].busy_lock = VPB_UNBUSIED;
9035ebe728dSRoger Pau Monné 	}
9045ebe728dSRoger Pau Monné }
9055ebe728dSRoger Pau Monné 
906b6de32bdSKonstantin Belousov int
907b6de32bdSKonstantin Belousov vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
908b6de32bdSKonstantin Belousov     vm_memattr_t memattr)
909b6de32bdSKonstantin Belousov {
910b6de32bdSKonstantin Belousov 	struct vm_phys_fictitious_seg *seg;
911b6de32bdSKonstantin Belousov 	vm_page_t fp;
9125ebe728dSRoger Pau Monné 	long page_count;
913b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE
9145ebe728dSRoger Pau Monné 	long pi, pe;
9155ebe728dSRoger Pau Monné 	long dpage_count;
916b6de32bdSKonstantin Belousov #endif
917b6de32bdSKonstantin Belousov 
9185ebe728dSRoger Pau Monné 	KASSERT(start < end,
9195ebe728dSRoger Pau Monné 	    ("Start of segment isn't less than end (start: %jx end: %jx)",
9205ebe728dSRoger Pau Monné 	    (uintmax_t)start, (uintmax_t)end));
9215ebe728dSRoger Pau Monné 
922b6de32bdSKonstantin Belousov 	page_count = (end - start) / PAGE_SIZE;
923b6de32bdSKonstantin Belousov 
924b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE
925b6de32bdSKonstantin Belousov 	pi = atop(start);
9265ebe728dSRoger Pau Monné 	pe = atop(end);
9275ebe728dSRoger Pau Monné 	if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
928b6de32bdSKonstantin Belousov 		fp = &vm_page_array[pi - first_page];
9295ebe728dSRoger Pau Monné 		if ((pe - first_page) > vm_page_array_size) {
9305ebe728dSRoger Pau Monné 			/*
9315ebe728dSRoger Pau Monné 			 * We have a segment that starts inside
9325ebe728dSRoger Pau Monné 			 * of vm_page_array, but ends outside of it.
9335ebe728dSRoger Pau Monné 			 *
9345ebe728dSRoger Pau Monné 			 * Use vm_page_array pages for those that are
9355ebe728dSRoger Pau Monné 			 * inside of the vm_page_array range, and
9365ebe728dSRoger Pau Monné 			 * allocate the remaining ones.
9375ebe728dSRoger Pau Monné 			 */
9385ebe728dSRoger Pau Monné 			dpage_count = vm_page_array_size - (pi - first_page);
9395ebe728dSRoger Pau Monné 			vm_phys_fictitious_init_range(fp, start, dpage_count,
9405ebe728dSRoger Pau Monné 			    memattr);
9415ebe728dSRoger Pau Monné 			page_count -= dpage_count;
9425ebe728dSRoger Pau Monné 			start += ptoa(dpage_count);
9435ebe728dSRoger Pau Monné 			goto alloc;
9445ebe728dSRoger Pau Monné 		}
9455ebe728dSRoger Pau Monné 		/*
9465ebe728dSRoger Pau Monné 		 * We can allocate the full range from vm_page_array,
9475ebe728dSRoger Pau Monné 		 * so there's no need to register the range in the tree.
9485ebe728dSRoger Pau Monné 		 */
9495ebe728dSRoger Pau Monné 		vm_phys_fictitious_init_range(fp, start, page_count, memattr);
9505ebe728dSRoger Pau Monné 		return (0);
9515ebe728dSRoger Pau Monné 	} else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
9525ebe728dSRoger Pau Monné 		/*
9535ebe728dSRoger Pau Monné 		 * We have a segment that ends inside of vm_page_array,
9545ebe728dSRoger Pau Monné 		 * but starts outside of it.
9555ebe728dSRoger Pau Monné 		 */
9565ebe728dSRoger Pau Monné 		fp = &vm_page_array[0];
9575ebe728dSRoger Pau Monné 		dpage_count = pe - first_page;
9585ebe728dSRoger Pau Monné 		vm_phys_fictitious_init_range(fp, ptoa(first_page), dpage_count,
9595ebe728dSRoger Pau Monné 		    memattr);
9605ebe728dSRoger Pau Monné 		end -= ptoa(dpage_count);
9615ebe728dSRoger Pau Monné 		page_count -= dpage_count;
9625ebe728dSRoger Pau Monné 		goto alloc;
9635ebe728dSRoger Pau Monné 	} else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
9645ebe728dSRoger Pau Monné 		/*
9655ebe728dSRoger Pau Monné 		 * Trying to register a fictitious range that expands before
9665ebe728dSRoger Pau Monné 		 * and after vm_page_array.
9675ebe728dSRoger Pau Monné 		 */
9685ebe728dSRoger Pau Monné 		return (EINVAL);
9695ebe728dSRoger Pau Monné 	} else {
9705ebe728dSRoger Pau Monné alloc:
971b6de32bdSKonstantin Belousov #endif
972b6de32bdSKonstantin Belousov 		fp = malloc(page_count * sizeof(struct vm_page), M_FICT_PAGES,
973f93f7cf1SMark Johnston 		    M_WAITOK);
9745ebe728dSRoger Pau Monné #ifdef VM_PHYSSEG_DENSE
975b6de32bdSKonstantin Belousov 	}
9765ebe728dSRoger Pau Monné #endif
9775ebe728dSRoger Pau Monné 	vm_phys_fictitious_init_range(fp, start, page_count, memattr);
97838d6b2dcSRoger Pau Monné 
97938d6b2dcSRoger Pau Monné 	seg = malloc(sizeof(*seg), M_FICT_PAGES, M_WAITOK | M_ZERO);
980b6de32bdSKonstantin Belousov 	seg->start = start;
981b6de32bdSKonstantin Belousov 	seg->end = end;
982b6de32bdSKonstantin Belousov 	seg->first_page = fp;
98338d6b2dcSRoger Pau Monné 
98438d6b2dcSRoger Pau Monné 	rw_wlock(&vm_phys_fictitious_reg_lock);
98538d6b2dcSRoger Pau Monné 	RB_INSERT(fict_tree, &vm_phys_fictitious_tree, seg);
98638d6b2dcSRoger Pau Monné 	rw_wunlock(&vm_phys_fictitious_reg_lock);
98738d6b2dcSRoger Pau Monné 
988b6de32bdSKonstantin Belousov 	return (0);
989b6de32bdSKonstantin Belousov }
990b6de32bdSKonstantin Belousov 
991b6de32bdSKonstantin Belousov void
992b6de32bdSKonstantin Belousov vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
993b6de32bdSKonstantin Belousov {
99438d6b2dcSRoger Pau Monné 	struct vm_phys_fictitious_seg *seg, tmp;
995b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE
9965ebe728dSRoger Pau Monné 	long pi, pe;
997b6de32bdSKonstantin Belousov #endif
998b6de32bdSKonstantin Belousov 
9995ebe728dSRoger Pau Monné 	KASSERT(start < end,
10005ebe728dSRoger Pau Monné 	    ("Start of segment isn't less than end (start: %jx end: %jx)",
10015ebe728dSRoger Pau Monné 	    (uintmax_t)start, (uintmax_t)end));
10025ebe728dSRoger Pau Monné 
1003b6de32bdSKonstantin Belousov #ifdef VM_PHYSSEG_DENSE
1004b6de32bdSKonstantin Belousov 	pi = atop(start);
10055ebe728dSRoger Pau Monné 	pe = atop(end);
10065ebe728dSRoger Pau Monné 	if (pi >= first_page && (pi - first_page) < vm_page_array_size) {
10075ebe728dSRoger Pau Monné 		if ((pe - first_page) <= vm_page_array_size) {
10085ebe728dSRoger Pau Monné 			/*
10095ebe728dSRoger Pau Monné 			 * This segment was allocated using vm_page_array
10105ebe728dSRoger Pau Monné 			 * only, there's nothing to do since those pages
10115ebe728dSRoger Pau Monné 			 * were never added to the tree.
10125ebe728dSRoger Pau Monné 			 */
10135ebe728dSRoger Pau Monné 			return;
10145ebe728dSRoger Pau Monné 		}
10155ebe728dSRoger Pau Monné 		/*
10165ebe728dSRoger Pau Monné 		 * We have a segment that starts inside
10175ebe728dSRoger Pau Monné 		 * of vm_page_array, but ends outside of it.
10185ebe728dSRoger Pau Monné 		 *
10195ebe728dSRoger Pau Monné 		 * Calculate how many pages were added to the
10205ebe728dSRoger Pau Monné 		 * tree and free them.
10215ebe728dSRoger Pau Monné 		 */
10225ebe728dSRoger Pau Monné 		start = ptoa(first_page + vm_page_array_size);
10235ebe728dSRoger Pau Monné 	} else if (pe > first_page && (pe - first_page) < vm_page_array_size) {
10245ebe728dSRoger Pau Monné 		/*
10255ebe728dSRoger Pau Monné 		 * We have a segment that ends inside of vm_page_array,
10265ebe728dSRoger Pau Monné 		 * but starts outside of it.
10275ebe728dSRoger Pau Monné 		 */
10285ebe728dSRoger Pau Monné 		end = ptoa(first_page);
10295ebe728dSRoger Pau Monné 	} else if (pi < first_page && pe > (first_page + vm_page_array_size)) {
10305ebe728dSRoger Pau Monné 		/* Since it's not possible to register such a range, panic. */
10315ebe728dSRoger Pau Monné 		panic(
10325ebe728dSRoger Pau Monné 		    "Unregistering not registered fictitious range [%#jx:%#jx]",
10335ebe728dSRoger Pau Monné 		    (uintmax_t)start, (uintmax_t)end);
10345ebe728dSRoger Pau Monné 	}
1035b6de32bdSKonstantin Belousov #endif
103638d6b2dcSRoger Pau Monné 	tmp.start = start;
103738d6b2dcSRoger Pau Monné 	tmp.end = 0;
1038b6de32bdSKonstantin Belousov 
103938d6b2dcSRoger Pau Monné 	rw_wlock(&vm_phys_fictitious_reg_lock);
104038d6b2dcSRoger Pau Monné 	seg = RB_FIND(fict_tree, &vm_phys_fictitious_tree, &tmp);
104138d6b2dcSRoger Pau Monné 	if (seg->start != start || seg->end != end) {
104238d6b2dcSRoger Pau Monné 		rw_wunlock(&vm_phys_fictitious_reg_lock);
104338d6b2dcSRoger Pau Monné 		panic(
104438d6b2dcSRoger Pau Monné 		    "Unregistering not registered fictitious range [%#jx:%#jx]",
104538d6b2dcSRoger Pau Monné 		    (uintmax_t)start, (uintmax_t)end);
104638d6b2dcSRoger Pau Monné 	}
104738d6b2dcSRoger Pau Monné 	RB_REMOVE(fict_tree, &vm_phys_fictitious_tree, seg);
104838d6b2dcSRoger Pau Monné 	rw_wunlock(&vm_phys_fictitious_reg_lock);
104938d6b2dcSRoger Pau Monné 	free(seg->first_page, M_FICT_PAGES);
105038d6b2dcSRoger Pau Monné 	free(seg, M_FICT_PAGES);
1051b6de32bdSKonstantin Belousov }
1052b6de32bdSKonstantin Belousov 
105311752d88SAlan Cox /*
105411752d88SAlan Cox  * Free a contiguous, power of two-sized set of physical pages.
10558941dc44SAlan Cox  *
10568941dc44SAlan Cox  * The free page queues must be locked.
105711752d88SAlan Cox  */
105811752d88SAlan Cox void
105911752d88SAlan Cox vm_phys_free_pages(vm_page_t m, int order)
106011752d88SAlan Cox {
106111752d88SAlan Cox 	struct vm_freelist *fl;
106211752d88SAlan Cox 	struct vm_phys_seg *seg;
10635c1f2cc4SAlan Cox 	vm_paddr_t pa;
106411752d88SAlan Cox 	vm_page_t m_buddy;
106511752d88SAlan Cox 
106611752d88SAlan Cox 	KASSERT(m->order == VM_NFREEORDER,
10678941dc44SAlan Cox 	    ("vm_phys_free_pages: page %p has unexpected order %d",
106811752d88SAlan Cox 	    m, m->order));
106911752d88SAlan Cox 	KASSERT(m->pool < VM_NFREEPOOL,
10708941dc44SAlan Cox 	    ("vm_phys_free_pages: page %p has unexpected pool %d",
107111752d88SAlan Cox 	    m, m->pool));
107211752d88SAlan Cox 	KASSERT(order < VM_NFREEORDER,
10738941dc44SAlan Cox 	    ("vm_phys_free_pages: order %d is out of range", order));
107411752d88SAlan Cox 	seg = &vm_phys_segs[m->segind];
1075e2068d0bSJeff Roberson 	vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
10765c1f2cc4SAlan Cox 	if (order < VM_NFREEORDER - 1) {
10775c1f2cc4SAlan Cox 		pa = VM_PAGE_TO_PHYS(m);
10785c1f2cc4SAlan Cox 		do {
10795c1f2cc4SAlan Cox 			pa ^= ((vm_paddr_t)1 << (PAGE_SHIFT + order));
10805c1f2cc4SAlan Cox 			if (pa < seg->start || pa >= seg->end)
108111752d88SAlan Cox 				break;
10825c1f2cc4SAlan Cox 			m_buddy = &seg->first_page[atop(pa - seg->start)];
108311752d88SAlan Cox 			if (m_buddy->order != order)
108411752d88SAlan Cox 				break;
108511752d88SAlan Cox 			fl = (*seg->free_queues)[m_buddy->pool];
10867e226537SAttilio Rao 			vm_freelist_rem(fl, m_buddy, order);
108711752d88SAlan Cox 			if (m_buddy->pool != m->pool)
108811752d88SAlan Cox 				vm_phys_set_pool(m->pool, m_buddy, order);
108911752d88SAlan Cox 			order++;
10905c1f2cc4SAlan Cox 			pa &= ~(((vm_paddr_t)1 << (PAGE_SHIFT + order)) - 1);
109111752d88SAlan Cox 			m = &seg->first_page[atop(pa - seg->start)];
10925c1f2cc4SAlan Cox 		} while (order < VM_NFREEORDER - 1);
109311752d88SAlan Cox 	}
109411752d88SAlan Cox 	fl = (*seg->free_queues)[m->pool];
10957e226537SAttilio Rao 	vm_freelist_add(fl, m, order, 1);
109611752d88SAlan Cox }
109711752d88SAlan Cox 
109811752d88SAlan Cox /*
10995c1f2cc4SAlan Cox  * Free a contiguous, arbitrarily sized set of physical pages.
11005c1f2cc4SAlan Cox  *
11015c1f2cc4SAlan Cox  * The free page queues must be locked.
11025c1f2cc4SAlan Cox  */
11035c1f2cc4SAlan Cox void
11045c1f2cc4SAlan Cox vm_phys_free_contig(vm_page_t m, u_long npages)
11055c1f2cc4SAlan Cox {
11065c1f2cc4SAlan Cox 	u_int n;
11075c1f2cc4SAlan Cox 	int order;
11085c1f2cc4SAlan Cox 
11095c1f2cc4SAlan Cox 	/*
11105c1f2cc4SAlan Cox 	 * Avoid unnecessary coalescing by freeing the pages in the largest
11115c1f2cc4SAlan Cox 	 * possible power-of-two-sized subsets.
11125c1f2cc4SAlan Cox 	 */
1113e2068d0bSJeff Roberson 	vm_domain_free_assert_locked(vm_pagequeue_domain(m));
11145c1f2cc4SAlan Cox 	for (;; npages -= n) {
11155c1f2cc4SAlan Cox 		/*
11165c1f2cc4SAlan Cox 		 * Unsigned "min" is used here so that "order" is assigned
11175c1f2cc4SAlan Cox 		 * "VM_NFREEORDER - 1" when "m"'s physical address is zero
11185c1f2cc4SAlan Cox 		 * or the low-order bits of its physical address are zero
11195c1f2cc4SAlan Cox 		 * because the size of a physical address exceeds the size of
11205c1f2cc4SAlan Cox 		 * a long.
11215c1f2cc4SAlan Cox 		 */
11225c1f2cc4SAlan Cox 		order = min(ffsl(VM_PAGE_TO_PHYS(m) >> PAGE_SHIFT) - 1,
11235c1f2cc4SAlan Cox 		    VM_NFREEORDER - 1);
11245c1f2cc4SAlan Cox 		n = 1 << order;
11255c1f2cc4SAlan Cox 		if (npages < n)
11265c1f2cc4SAlan Cox 			break;
11275c1f2cc4SAlan Cox 		vm_phys_free_pages(m, order);
11285c1f2cc4SAlan Cox 		m += n;
11295c1f2cc4SAlan Cox 	}
11305c1f2cc4SAlan Cox 	/* The residual "npages" is less than "1 << (VM_NFREEORDER - 1)". */
11315c1f2cc4SAlan Cox 	for (; npages > 0; npages -= n) {
11325c1f2cc4SAlan Cox 		order = flsl(npages) - 1;
11335c1f2cc4SAlan Cox 		n = 1 << order;
11345c1f2cc4SAlan Cox 		vm_phys_free_pages(m, order);
11355c1f2cc4SAlan Cox 		m += n;
11365c1f2cc4SAlan Cox 	}
11375c1f2cc4SAlan Cox }
11385c1f2cc4SAlan Cox 
11395c1f2cc4SAlan Cox /*
1140c869e672SAlan Cox  * Scan physical memory between the specified addresses "low" and "high" for a
1141c869e672SAlan Cox  * run of contiguous physical pages that satisfy the specified conditions, and
1142c869e672SAlan Cox  * return the lowest page in the run.  The specified "alignment" determines
1143c869e672SAlan Cox  * the alignment of the lowest physical page in the run.  If the specified
1144c869e672SAlan Cox  * "boundary" is non-zero, then the run of physical pages cannot span a
1145c869e672SAlan Cox  * physical address that is a multiple of "boundary".
1146c869e672SAlan Cox  *
1147c869e672SAlan Cox  * "npages" must be greater than zero.  Both "alignment" and "boundary" must
1148c869e672SAlan Cox  * be a power of two.
1149c869e672SAlan Cox  */
1150c869e672SAlan Cox vm_page_t
11513f289c3fSJeff Roberson vm_phys_scan_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
1152c869e672SAlan Cox     u_long alignment, vm_paddr_t boundary, int options)
1153c869e672SAlan Cox {
1154c869e672SAlan Cox 	vm_paddr_t pa_end;
1155c869e672SAlan Cox 	vm_page_t m_end, m_run, m_start;
1156c869e672SAlan Cox 	struct vm_phys_seg *seg;
1157c869e672SAlan Cox 	int segind;
1158c869e672SAlan Cox 
1159c869e672SAlan Cox 	KASSERT(npages > 0, ("npages is 0"));
1160c869e672SAlan Cox 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1161c869e672SAlan Cox 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1162c869e672SAlan Cox 	if (low >= high)
1163c869e672SAlan Cox 		return (NULL);
1164c869e672SAlan Cox 	for (segind = 0; segind < vm_phys_nsegs; segind++) {
1165c869e672SAlan Cox 		seg = &vm_phys_segs[segind];
11663f289c3fSJeff Roberson 		if (seg->domain != domain)
11673f289c3fSJeff Roberson 			continue;
1168c869e672SAlan Cox 		if (seg->start >= high)
1169c869e672SAlan Cox 			break;
1170c869e672SAlan Cox 		if (low >= seg->end)
1171c869e672SAlan Cox 			continue;
1172c869e672SAlan Cox 		if (low <= seg->start)
1173c869e672SAlan Cox 			m_start = seg->first_page;
1174c869e672SAlan Cox 		else
1175c869e672SAlan Cox 			m_start = &seg->first_page[atop(low - seg->start)];
1176c869e672SAlan Cox 		if (high < seg->end)
1177c869e672SAlan Cox 			pa_end = high;
1178c869e672SAlan Cox 		else
1179c869e672SAlan Cox 			pa_end = seg->end;
1180c869e672SAlan Cox 		if (pa_end - VM_PAGE_TO_PHYS(m_start) < ptoa(npages))
1181c869e672SAlan Cox 			continue;
1182c869e672SAlan Cox 		m_end = &seg->first_page[atop(pa_end - seg->start)];
1183c869e672SAlan Cox 		m_run = vm_page_scan_contig(npages, m_start, m_end,
1184c869e672SAlan Cox 		    alignment, boundary, options);
1185c869e672SAlan Cox 		if (m_run != NULL)
1186c869e672SAlan Cox 			return (m_run);
1187c869e672SAlan Cox 	}
1188c869e672SAlan Cox 	return (NULL);
1189c869e672SAlan Cox }
1190c869e672SAlan Cox 
1191c869e672SAlan Cox /*
119211752d88SAlan Cox  * Set the pool for a contiguous, power of two-sized set of physical pages.
119311752d88SAlan Cox  */
11947bfda801SAlan Cox void
119511752d88SAlan Cox vm_phys_set_pool(int pool, vm_page_t m, int order)
119611752d88SAlan Cox {
119711752d88SAlan Cox 	vm_page_t m_tmp;
119811752d88SAlan Cox 
119911752d88SAlan Cox 	for (m_tmp = m; m_tmp < &m[1 << order]; m_tmp++)
120011752d88SAlan Cox 		m_tmp->pool = pool;
120111752d88SAlan Cox }
120211752d88SAlan Cox 
120311752d88SAlan Cox /*
12049742373aSAlan Cox  * Search for the given physical page "m" in the free lists.  If the search
12059742373aSAlan Cox  * succeeds, remove "m" from the free lists and return TRUE.  Otherwise, return
12069742373aSAlan Cox  * FALSE, indicating that "m" is not in the free lists.
12077bfda801SAlan Cox  *
12087bfda801SAlan Cox  * The free page queues must be locked.
12097bfda801SAlan Cox  */
1210e35395ceSAlan Cox boolean_t
12117bfda801SAlan Cox vm_phys_unfree_page(vm_page_t m)
12127bfda801SAlan Cox {
12137bfda801SAlan Cox 	struct vm_freelist *fl;
12147bfda801SAlan Cox 	struct vm_phys_seg *seg;
12157bfda801SAlan Cox 	vm_paddr_t pa, pa_half;
12167bfda801SAlan Cox 	vm_page_t m_set, m_tmp;
12177bfda801SAlan Cox 	int order;
12187bfda801SAlan Cox 
12197bfda801SAlan Cox 	/*
12207bfda801SAlan Cox 	 * First, find the contiguous, power of two-sized set of free
12217bfda801SAlan Cox 	 * physical pages containing the given physical page "m" and
12227bfda801SAlan Cox 	 * assign it to "m_set".
12237bfda801SAlan Cox 	 */
12247bfda801SAlan Cox 	seg = &vm_phys_segs[m->segind];
1225e2068d0bSJeff Roberson 	vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
12267bfda801SAlan Cox 	for (m_set = m, order = 0; m_set->order == VM_NFREEORDER &&
1227bc8794a1SAlan Cox 	    order < VM_NFREEORDER - 1; ) {
12287bfda801SAlan Cox 		order++;
12297bfda801SAlan Cox 		pa = m->phys_addr & (~(vm_paddr_t)0 << (PAGE_SHIFT + order));
12302fbced65SAlan Cox 		if (pa >= seg->start)
12317bfda801SAlan Cox 			m_set = &seg->first_page[atop(pa - seg->start)];
1232e35395ceSAlan Cox 		else
1233e35395ceSAlan Cox 			return (FALSE);
12347bfda801SAlan Cox 	}
1235e35395ceSAlan Cox 	if (m_set->order < order)
1236e35395ceSAlan Cox 		return (FALSE);
1237e35395ceSAlan Cox 	if (m_set->order == VM_NFREEORDER)
1238e35395ceSAlan Cox 		return (FALSE);
12397bfda801SAlan Cox 	KASSERT(m_set->order < VM_NFREEORDER,
12407bfda801SAlan Cox 	    ("vm_phys_unfree_page: page %p has unexpected order %d",
12417bfda801SAlan Cox 	    m_set, m_set->order));
12427bfda801SAlan Cox 
12437bfda801SAlan Cox 	/*
12447bfda801SAlan Cox 	 * Next, remove "m_set" from the free lists.  Finally, extract
12457bfda801SAlan Cox 	 * "m" from "m_set" using an iterative algorithm: While "m_set"
12467bfda801SAlan Cox 	 * is larger than a page, shrink "m_set" by returning the half
12477bfda801SAlan Cox 	 * of "m_set" that does not contain "m" to the free lists.
12487bfda801SAlan Cox 	 */
12497bfda801SAlan Cox 	fl = (*seg->free_queues)[m_set->pool];
12507bfda801SAlan Cox 	order = m_set->order;
12517e226537SAttilio Rao 	vm_freelist_rem(fl, m_set, order);
12527bfda801SAlan Cox 	while (order > 0) {
12537bfda801SAlan Cox 		order--;
12547bfda801SAlan Cox 		pa_half = m_set->phys_addr ^ (1 << (PAGE_SHIFT + order));
12557bfda801SAlan Cox 		if (m->phys_addr < pa_half)
12567bfda801SAlan Cox 			m_tmp = &seg->first_page[atop(pa_half - seg->start)];
12577bfda801SAlan Cox 		else {
12587bfda801SAlan Cox 			m_tmp = m_set;
12597bfda801SAlan Cox 			m_set = &seg->first_page[atop(pa_half - seg->start)];
12607bfda801SAlan Cox 		}
12617e226537SAttilio Rao 		vm_freelist_add(fl, m_tmp, order, 0);
12627bfda801SAlan Cox 	}
12637bfda801SAlan Cox 	KASSERT(m_set == m, ("vm_phys_unfree_page: fatal inconsistency"));
1264e35395ceSAlan Cox 	return (TRUE);
12657bfda801SAlan Cox }
12667bfda801SAlan Cox 
12677bfda801SAlan Cox /*
12682f9f48d6SAlan Cox  * Allocate a contiguous set of physical pages of the given size
12692f9f48d6SAlan Cox  * "npages" from the free lists.  All of the physical pages must be at
12702f9f48d6SAlan Cox  * or above the given physical address "low" and below the given
12712f9f48d6SAlan Cox  * physical address "high".  The given value "alignment" determines the
12722f9f48d6SAlan Cox  * alignment of the first physical page in the set.  If the given value
12732f9f48d6SAlan Cox  * "boundary" is non-zero, then the set of physical pages cannot cross
12742f9f48d6SAlan Cox  * any physical address boundary that is a multiple of that value.  Both
127511752d88SAlan Cox  * "alignment" and "boundary" must be a power of two.
127611752d88SAlan Cox  */
127711752d88SAlan Cox vm_page_t
1278ef435ae7SJeff Roberson vm_phys_alloc_contig(int domain, u_long npages, vm_paddr_t low, vm_paddr_t high,
12795c1f2cc4SAlan Cox     u_long alignment, vm_paddr_t boundary)
128011752d88SAlan Cox {
1281c869e672SAlan Cox 	vm_paddr_t pa_end, pa_start;
1282c869e672SAlan Cox 	vm_page_t m_run;
1283c869e672SAlan Cox 	struct vm_phys_seg *seg;
1284ef435ae7SJeff Roberson 	int segind;
128511752d88SAlan Cox 
1286c869e672SAlan Cox 	KASSERT(npages > 0, ("npages is 0"));
1287c869e672SAlan Cox 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1288c869e672SAlan Cox 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1289e2068d0bSJeff Roberson 	vm_domain_free_assert_locked(VM_DOMAIN(domain));
1290c869e672SAlan Cox 	if (low >= high)
1291c869e672SAlan Cox 		return (NULL);
1292c869e672SAlan Cox 	m_run = NULL;
1293477bffbeSAlan Cox 	for (segind = vm_phys_nsegs - 1; segind >= 0; segind--) {
1294c869e672SAlan Cox 		seg = &vm_phys_segs[segind];
1295477bffbeSAlan Cox 		if (seg->start >= high || seg->domain != domain)
129611752d88SAlan Cox 			continue;
1297477bffbeSAlan Cox 		if (low >= seg->end)
1298477bffbeSAlan Cox 			break;
1299c869e672SAlan Cox 		if (low <= seg->start)
1300c869e672SAlan Cox 			pa_start = seg->start;
1301c869e672SAlan Cox 		else
1302c869e672SAlan Cox 			pa_start = low;
1303c869e672SAlan Cox 		if (high < seg->end)
1304c869e672SAlan Cox 			pa_end = high;
1305c869e672SAlan Cox 		else
1306c869e672SAlan Cox 			pa_end = seg->end;
1307c869e672SAlan Cox 		if (pa_end - pa_start < ptoa(npages))
1308c869e672SAlan Cox 			continue;
1309c869e672SAlan Cox 		m_run = vm_phys_alloc_seg_contig(seg, npages, low, high,
1310c869e672SAlan Cox 		    alignment, boundary);
1311c869e672SAlan Cox 		if (m_run != NULL)
1312c869e672SAlan Cox 			break;
1313c869e672SAlan Cox 	}
1314c869e672SAlan Cox 	return (m_run);
1315c869e672SAlan Cox }
131611752d88SAlan Cox 
131711752d88SAlan Cox /*
1318c869e672SAlan Cox  * Allocate a run of contiguous physical pages from the free list for the
1319c869e672SAlan Cox  * specified segment.
1320c869e672SAlan Cox  */
1321c869e672SAlan Cox static vm_page_t
1322c869e672SAlan Cox vm_phys_alloc_seg_contig(struct vm_phys_seg *seg, u_long npages,
1323c869e672SAlan Cox     vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary)
1324c869e672SAlan Cox {
1325c869e672SAlan Cox 	struct vm_freelist *fl;
1326c869e672SAlan Cox 	vm_paddr_t pa, pa_end, size;
1327c869e672SAlan Cox 	vm_page_t m, m_ret;
1328c869e672SAlan Cox 	u_long npages_end;
1329c869e672SAlan Cox 	int oind, order, pind;
1330c869e672SAlan Cox 
1331c869e672SAlan Cox 	KASSERT(npages > 0, ("npages is 0"));
1332c869e672SAlan Cox 	KASSERT(powerof2(alignment), ("alignment is not a power of 2"));
1333c869e672SAlan Cox 	KASSERT(powerof2(boundary), ("boundary is not a power of 2"));
1334e2068d0bSJeff Roberson 	vm_domain_free_assert_locked(VM_DOMAIN(seg->domain));
1335c869e672SAlan Cox 	/* Compute the queue that is the best fit for npages. */
13369161b4deSAlan Cox 	order = flsl(npages - 1);
1337c869e672SAlan Cox 	/* Search for a run satisfying the specified conditions. */
1338c869e672SAlan Cox 	size = npages << PAGE_SHIFT;
1339c869e672SAlan Cox 	for (oind = min(order, VM_NFREEORDER - 1); oind < VM_NFREEORDER;
1340c869e672SAlan Cox 	    oind++) {
1341c869e672SAlan Cox 		for (pind = 0; pind < VM_NFREEPOOL; pind++) {
1342c869e672SAlan Cox 			fl = (*seg->free_queues)[pind];
13435cd29d0fSMark Johnston 			TAILQ_FOREACH(m_ret, &fl[oind].pl, listq) {
1344c869e672SAlan Cox 				/*
134511752d88SAlan Cox 				 * Is the size of this allocation request
134611752d88SAlan Cox 				 * larger than the largest block size?
134711752d88SAlan Cox 				 */
134811752d88SAlan Cox 				if (order >= VM_NFREEORDER) {
134911752d88SAlan Cox 					/*
1350c869e672SAlan Cox 					 * Determine if a sufficient number of
1351c869e672SAlan Cox 					 * subsequent blocks to satisfy the
1352c869e672SAlan Cox 					 * allocation request are free.
135311752d88SAlan Cox 					 */
135411752d88SAlan Cox 					pa = VM_PAGE_TO_PHYS(m_ret);
1355c869e672SAlan Cox 					pa_end = pa + size;
135679e9552eSKonstantin Belousov 					if (pa_end < pa)
135779e9552eSKonstantin Belousov 						continue;
135811752d88SAlan Cox 					for (;;) {
1359c869e672SAlan Cox 						pa += 1 << (PAGE_SHIFT +
1360c869e672SAlan Cox 						    VM_NFREEORDER - 1);
1361c869e672SAlan Cox 						if (pa >= pa_end ||
1362c869e672SAlan Cox 						    pa < seg->start ||
136311752d88SAlan Cox 						    pa >= seg->end)
136411752d88SAlan Cox 							break;
1365c869e672SAlan Cox 						m = &seg->first_page[atop(pa -
1366c869e672SAlan Cox 						    seg->start)];
1367c869e672SAlan Cox 						if (m->order != VM_NFREEORDER -
1368c869e672SAlan Cox 						    1)
136911752d88SAlan Cox 							break;
137011752d88SAlan Cox 					}
1371c869e672SAlan Cox 					/* If not, go to the next block. */
1372c869e672SAlan Cox 					if (pa < pa_end)
137311752d88SAlan Cox 						continue;
137411752d88SAlan Cox 				}
137511752d88SAlan Cox 
137611752d88SAlan Cox 				/*
1377c869e672SAlan Cox 				 * Determine if the blocks are within the
1378c869e672SAlan Cox 				 * given range, satisfy the given alignment,
1379c869e672SAlan Cox 				 * and do not cross the given boundary.
138011752d88SAlan Cox 				 */
138111752d88SAlan Cox 				pa = VM_PAGE_TO_PHYS(m_ret);
1382c869e672SAlan Cox 				pa_end = pa + size;
1383d9c9c81cSPedro F. Giffuni 				if (pa >= low && pa_end <= high &&
1384d9c9c81cSPedro F. Giffuni 				    (pa & (alignment - 1)) == 0 &&
1385d9c9c81cSPedro F. Giffuni 				    rounddown2(pa ^ (pa_end - 1), boundary) == 0)
138611752d88SAlan Cox 					goto done;
138711752d88SAlan Cox 			}
138811752d88SAlan Cox 		}
138911752d88SAlan Cox 	}
139011752d88SAlan Cox 	return (NULL);
139111752d88SAlan Cox done:
139211752d88SAlan Cox 	for (m = m_ret; m < &m_ret[npages]; m = &m[1 << oind]) {
139311752d88SAlan Cox 		fl = (*seg->free_queues)[m->pool];
13949161b4deSAlan Cox 		vm_freelist_rem(fl, m, oind);
13959161b4deSAlan Cox 		if (m->pool != VM_FREEPOOL_DEFAULT)
13969161b4deSAlan Cox 			vm_phys_set_pool(VM_FREEPOOL_DEFAULT, m, oind);
139711752d88SAlan Cox 	}
13985c1f2cc4SAlan Cox 	/* Return excess pages to the free lists. */
13999161b4deSAlan Cox 	npages_end = roundup2(npages, 1 << oind);
14007493904eSAlan Cox 	if (npages < npages_end) {
14017493904eSAlan Cox 		fl = (*seg->free_queues)[VM_FREEPOOL_DEFAULT];
14027493904eSAlan Cox 		vm_phys_enq_range(&m_ret[npages], npages_end - npages, fl, 0);
14037493904eSAlan Cox 	}
140411752d88SAlan Cox 	return (m_ret);
140511752d88SAlan Cox }
140611752d88SAlan Cox 
140711752d88SAlan Cox #ifdef DDB
140811752d88SAlan Cox /*
140911752d88SAlan Cox  * Show the number of physical pages in each of the free lists.
141011752d88SAlan Cox  */
141111752d88SAlan Cox DB_SHOW_COMMAND(freepages, db_show_freepages)
141211752d88SAlan Cox {
141311752d88SAlan Cox 	struct vm_freelist *fl;
14147e226537SAttilio Rao 	int flind, oind, pind, dom;
141511752d88SAlan Cox 
14167e226537SAttilio Rao 	for (dom = 0; dom < vm_ndomains; dom++) {
14177e226537SAttilio Rao 		db_printf("DOMAIN: %d\n", dom);
141811752d88SAlan Cox 		for (flind = 0; flind < vm_nfreelists; flind++) {
141911752d88SAlan Cox 			db_printf("FREE LIST %d:\n"
142011752d88SAlan Cox 			    "\n  ORDER (SIZE)  |  NUMBER"
142111752d88SAlan Cox 			    "\n              ", flind);
142211752d88SAlan Cox 			for (pind = 0; pind < VM_NFREEPOOL; pind++)
142311752d88SAlan Cox 				db_printf("  |  POOL %d", pind);
142411752d88SAlan Cox 			db_printf("\n--            ");
142511752d88SAlan Cox 			for (pind = 0; pind < VM_NFREEPOOL; pind++)
142611752d88SAlan Cox 				db_printf("-- --      ");
142711752d88SAlan Cox 			db_printf("--\n");
142811752d88SAlan Cox 			for (oind = VM_NFREEORDER - 1; oind >= 0; oind--) {
142911752d88SAlan Cox 				db_printf("  %2.2d (%6.6dK)", oind,
143011752d88SAlan Cox 				    1 << (PAGE_SHIFT - 10 + oind));
143111752d88SAlan Cox 				for (pind = 0; pind < VM_NFREEPOOL; pind++) {
14327e226537SAttilio Rao 				fl = vm_phys_free_queues[dom][flind][pind];
143311752d88SAlan Cox 					db_printf("  |  %6.6d", fl[oind].lcnt);
143411752d88SAlan Cox 				}
143511752d88SAlan Cox 				db_printf("\n");
143611752d88SAlan Cox 			}
143711752d88SAlan Cox 			db_printf("\n");
143811752d88SAlan Cox 		}
14397e226537SAttilio Rao 		db_printf("\n");
14407e226537SAttilio Rao 	}
144111752d88SAlan Cox }
144211752d88SAlan Cox #endif
1443