xref: /freebsd/sys/kern/subr_physmem.c (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_acpi.h"
33 #include "opt_ddb.h"
34 
35 /*
36  * Routines for describing and initializing anything related to physical memory.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/physmem.h>
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_phys.h>
46 #include <machine/md_var.h>
47 
48 /*
49  * These structures are used internally to keep track of regions of physical
50  * ram, and regions within the physical ram that need to be excluded.  An
51  * exclusion region can be excluded from crash dumps, from the vm pool of pages
52  * that can be allocated, or both, depending on the exclusion flags associated
53  * with the region.
54  */
55 #ifdef DEV_ACPI
56 #define	MAX_HWCNT	32	/* ACPI needs more regions */
57 #define	MAX_EXCNT	32
58 #else
59 #define	MAX_HWCNT	16
60 #define	MAX_EXCNT	16
61 #endif
62 
63 #if defined(__arm__)
64 #define	MAX_PHYS_ADDR	0xFFFFFFFFull
65 #elif defined(__aarch64__) || defined(__riscv)
66 #define	MAX_PHYS_ADDR	0xFFFFFFFFFFFFFFFFull
67 #endif
68 
69 struct region {
70 	vm_paddr_t	addr;
71 	vm_size_t	size;
72 	uint32_t	flags;
73 };
74 
75 static struct region hwregions[MAX_HWCNT];
76 static struct region exregions[MAX_EXCNT];
77 
78 static size_t hwcnt;
79 static size_t excnt;
80 
81 /*
82  * realmem is the total number of hardware pages, excluded or not.
83  * Maxmem is one greater than the last physical page number.
84  */
85 long realmem;
86 long Maxmem;
87 
88 /*
89  * Print the contents of the physical and excluded region tables using the
90  * provided printf-like output function (which will be either printf or
91  * db_printf).
92  */
93 static void
94 physmem_dump_tables(int (*prfunc)(const char *, ...))
95 {
96 	int flags, i;
97 	uintmax_t addr, size;
98 	const unsigned int mbyte = 1024 * 1024;
99 
100 	prfunc("Physical memory chunk(s):\n");
101 	for (i = 0; i < hwcnt; ++i) {
102 		addr = hwregions[i].addr;
103 		size = hwregions[i].size;
104 		prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages)\n", addr,
105 		    addr + size - 1, size / mbyte, size / PAGE_SIZE);
106 	}
107 
108 	prfunc("Excluded memory regions:\n");
109 	for (i = 0; i < excnt; ++i) {
110 		addr  = exregions[i].addr;
111 		size  = exregions[i].size;
112 		flags = exregions[i].flags;
113 		prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages) %s %s\n",
114 		    addr, addr + size - 1, size / mbyte, size / PAGE_SIZE,
115 		    (flags & EXFLAG_NOALLOC) ? "NoAlloc" : "",
116 		    (flags & EXFLAG_NODUMP)  ? "NoDump" : "");
117 	}
118 
119 #ifdef DEBUG
120 	prfunc("Avail lists:\n");
121 	for (i = 0; phys_avail[i] != 0; ++i) {
122 		prfunc("  phys_avail[%d] 0x%08x\n", i, phys_avail[i]);
123 	}
124 	for (i = 0; dump_avail[i] != 0; ++i) {
125 		prfunc("  dump_avail[%d] 0x%08x\n", i, dump_avail[i]);
126 	}
127 #endif
128 }
129 
130 /*
131  * Print the contents of the static mapping table.  Used for bootverbose.
132  */
133 void
134 physmem_print_tables(void)
135 {
136 
137 	physmem_dump_tables(printf);
138 }
139 
140 /*
141  * Walk the list of hardware regions, processing it against the list of
142  * exclusions that contain the given exflags, and generating an "avail list".
143  *
144  * Updates the value at *pavail with the sum of all pages in all hw regions.
145  *
146  * Returns the number of pages of non-excluded memory added to the avail list.
147  */
148 static size_t
149 regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail,
150     long *pavail, long *prealmem)
151 {
152 	size_t acnt, exi, hwi;
153 	uint64_t end, start, xend, xstart;
154 	long availmem, totalmem;
155 	const struct region *exp, *hwp;
156 
157 	totalmem = 0;
158 	availmem = 0;
159 	acnt = 0;
160 	for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) {
161 		start = hwp->addr;
162 		end   = hwp->size + start;
163 		totalmem += atop((vm_offset_t)(end - start));
164 		for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) {
165 			/*
166 			 * If the excluded region does not match given flags,
167 			 * continue checking with the next excluded region.
168 			 */
169 			if ((exp->flags & exflags) == 0)
170 				continue;
171 			xstart = exp->addr;
172 			xend   = exp->size + xstart;
173 			/*
174 			 * If the excluded region ends before this hw region,
175 			 * continue checking with the next excluded region.
176 			 */
177 			if (xend <= start)
178 				continue;
179 			/*
180 			 * If the excluded region begins after this hw region
181 			 * we're done because both lists are sorted.
182 			 */
183 			if (xstart >= end)
184 				break;
185 			/*
186 			 * If the excluded region completely covers this hw
187 			 * region, shrink this hw region to zero size.
188 			 */
189 			if ((start >= xstart) && (end <= xend)) {
190 				start = xend;
191 				end = xend;
192 				break;
193 			}
194 			/*
195 			 * If the excluded region falls wholly within this hw
196 			 * region without abutting or overlapping the beginning
197 			 * or end, create an available entry from the leading
198 			 * fragment, then adjust the start of this hw region to
199 			 * the end of the excluded region, and continue checking
200 			 * the next excluded region because another exclusion
201 			 * could affect the remainder of this hw region.
202 			 */
203 			if ((xstart > start) && (xend < end)) {
204 				if (acnt > 0 &&
205 				    avail[acnt - 1] == (vm_paddr_t)start) {
206 					avail[acnt - 1] = (vm_paddr_t)xstart;
207 				} else {
208 					avail[acnt++] = (vm_paddr_t)start;
209 					avail[acnt++] = (vm_paddr_t)xstart;
210 				}
211 				availmem += atop((vm_offset_t)(xstart - start));
212 				start = xend;
213 				continue;
214 			}
215 			/*
216 			 * We know the excluded region overlaps either the start
217 			 * or end of this hardware region (but not both), trim
218 			 * the excluded portion off the appropriate end.
219 			 */
220 			if (xstart <= start)
221 				start = xend;
222 			else
223 				end = xstart;
224 		}
225 		/*
226 		 * If the trimming actions above left a non-zero size, create an
227 		 * available entry for it.
228 		 */
229 		if (end > start) {
230 			if (acnt > 0 && avail[acnt - 1] == (vm_paddr_t)start) {
231 				avail[acnt - 1] = (vm_paddr_t)end;
232 			} else {
233 				avail[acnt++] = (vm_paddr_t)start;
234 				avail[acnt++] = (vm_paddr_t)end;
235 			}
236 			availmem += atop((vm_offset_t)(end - start));
237 		}
238 		if (acnt >= maxavail)
239 			panic("Not enough space in the dump/phys_avail arrays");
240 	}
241 
242 	if (pavail != NULL)
243 		*pavail = availmem;
244 	if (prealmem != NULL)
245 		*prealmem = totalmem;
246 	return (acnt);
247 }
248 
249 /*
250  * Insertion-sort a new entry into a regions list; sorted by start address.
251  */
252 static size_t
253 insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr,
254     vm_size_t size, uint32_t flags)
255 {
256 	size_t i;
257 	struct region *ep, *rp;
258 
259 	ep = regions + rcnt;
260 	for (i = 0, rp = regions; i < rcnt; ++i, ++rp) {
261 		if (rp->addr == addr && rp->size == size) /* Pure dup. */
262 			return (rcnt);
263 		if (flags == rp->flags) {
264 			if (addr + size == rp->addr) {
265 				rp->addr = addr;
266 				rp->size += size;
267 				return (rcnt);
268 			} else if (rp->addr + rp->size == addr) {
269 				rp->size += size;
270 				return (rcnt);
271 			}
272 		}
273 		if (addr < rp->addr) {
274 			bcopy(rp, rp + 1, (ep - rp) * sizeof(*rp));
275 			break;
276 		}
277 	}
278 	rp->addr  = addr;
279 	rp->size  = size;
280 	rp->flags = flags;
281 	rcnt++;
282 
283 	return (rcnt);
284 }
285 
286 /*
287  * Add a hardware memory region.
288  */
289 void
290 physmem_hardware_region(uint64_t pa, uint64_t sz)
291 {
292 	vm_offset_t adj;
293 
294 	/*
295 	 * Filter out the page at PA 0x00000000.  The VM can't handle it, as
296 	 * pmap_extract() == 0 means failure.
297 	 */
298 	if (pa == 0) {
299 		if (sz <= PAGE_SIZE)
300 			return;
301 		pa  = PAGE_SIZE;
302 		sz -= PAGE_SIZE;
303 	} else if (pa > MAX_PHYS_ADDR) {
304 		/* This range is past usable memory, ignore it */
305 		return;
306 	}
307 
308 	/*
309 	 * Also filter out the page at the end of the physical address space --
310 	 * if addr is non-zero and addr+size is zero we wrapped to the next byte
311 	 * beyond what vm_paddr_t can express.  That leads to a NULL pointer
312 	 * deref early in startup; work around it by leaving the last page out.
313 	 *
314 	 * XXX This just in:  subtract out a whole megabyte, not just 1 page.
315 	 * Reducing the size by anything less than 1MB results in the NULL
316 	 * pointer deref in _vm_map_lock_read().  Better to give up a megabyte
317 	 * than leave some folks with an unusable system while we investigate.
318 	 */
319 	if ((pa + sz) > (MAX_PHYS_ADDR - 1024 * 1024)) {
320 		sz = MAX_PHYS_ADDR - pa + 1;
321 		if (sz <= 1024 * 1024)
322 			return;
323 		sz -= 1024 * 1024;
324 	}
325 
326 	/*
327 	 * Round the starting address up to a page boundary, and truncate the
328 	 * ending page down to a page boundary.
329 	 */
330 	adj = round_page(pa) - pa;
331 	pa  = round_page(pa);
332 	sz  = trunc_page(sz - adj);
333 
334 	if (sz > 0 && hwcnt < nitems(hwregions))
335 		hwcnt = insert_region(hwregions, hwcnt, pa, sz, 0);
336 }
337 
338 /*
339  * Add an exclusion region.
340  */
341 void
342 physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
343 {
344 	vm_offset_t adj;
345 
346 	/*
347 	 * Truncate the starting address down to a page boundary, and round the
348 	 * ending page up to a page boundary.
349 	 */
350 	adj = pa - trunc_page(pa);
351 	pa  = trunc_page(pa);
352 	sz  = round_page(sz + adj);
353 
354 	if (excnt >= nitems(exregions))
355 		panic("failed to exclude region %#jx-%#jx", (uintmax_t)pa,
356 		    (uintmax_t)(pa + sz));
357 	excnt = insert_region(exregions, excnt, pa, sz, exflags);
358 }
359 
360 size_t
361 physmem_avail(vm_paddr_t *avail, size_t maxavail)
362 {
363 
364 	return (regions_to_avail(avail, EXFLAG_NOALLOC, maxavail, NULL, NULL));
365 }
366 
367 /*
368  * Process all the regions added earlier into the global avail lists.
369  *
370  * Updates the kernel global 'physmem' with the number of physical pages
371  * available for use (all pages not in any exclusion region).
372  *
373  * Updates the kernel global 'Maxmem' with the page number one greater then the
374  * last page of physical memory in the system.
375  */
376 void
377 physmem_init_kernel_globals(void)
378 {
379 	size_t nextidx;
380 
381 	regions_to_avail(dump_avail, EXFLAG_NODUMP, PHYS_AVAIL_ENTRIES, NULL,
382 	    NULL);
383 	nextidx = regions_to_avail(phys_avail, EXFLAG_NOALLOC,
384 	    PHYS_AVAIL_ENTRIES, &physmem, &realmem);
385 	if (nextidx == 0)
386 		panic("No memory entries in phys_avail");
387 	Maxmem = atop(phys_avail[nextidx - 1]);
388 }
389 
390 #ifdef DDB
391 #include <ddb/ddb.h>
392 
393 DB_SHOW_COMMAND(physmem, db_show_physmem)
394 {
395 
396 	physmem_dump_tables(db_printf);
397 }
398 
399 #endif /* DDB */
400