2 * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 * Routines for describing and initializing anything related to physical memory.
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <machine/physmem.h>
42 * These structures are used internally to keep track of regions of physical
43 * ram, and regions within the physical ram that need to be excluded. An
44 * exclusion region can be excluded from crash dumps, from the vm pool of pages
45 * that can be allocated, or both, depending on the exclusion flags associated
57 static struct region hwregions[MAX_HWCNT];
58 static struct region exregions[MAX_EXCNT];
64 * These "avail lists" are globals used to communicate physical memory layout to
65 * other parts of the kernel. Within the arrays, each value is the starting
66 * address of a contiguous area of physical address space. The values at even
67 * indexes are areas that contain usable memory and the values at odd indexes
68 * are areas that aren't usable. Each list is terminated by a pair of zero
71 * dump_avail tells the dump code what regions to include in a crash dump, and
72 * phys_avail is the way we hand all the remaining physical ram we haven't used
73 * in early kernel init over to the vm system for allocation management.
75 * We size these arrays to hold twice as many available regions as we allow for
76 * hardware memory regions, to allow for the fact that exclusions can split a
77 * hardware region into two or more available regions. In the real world there
78 * will typically be one or two hardware regions and two or three exclusions.
80 * Each available region in this list occupies two array slots (the start of the
81 * available region and the start of the unavailable region that follows it).
83 #define MAX_AVAIL_REGIONS (MAX_HWCNT * 2)
84 #define MAX_AVAIL_ENTRIES (MAX_AVAIL_REGIONS * 2)
86 vm_paddr_t phys_avail[MAX_AVAIL_ENTRIES + 2]; /* +2 to allow for a pair */
87 vm_paddr_t dump_avail[MAX_AVAIL_ENTRIES + 2]; /* of zeroes to terminate. */
89 /* This is the total number of hardware pages, excluded or not. */
92 /* The address at which the kernel was loaded. Set early in initarm(). */
93 vm_paddr_t arm_physmem_kernaddr;
96 * Print the contents of the physical and excluded region tables using the
97 * provided printf-like output function (which will be either printf or
101 physmem_dump_tables(int (*prfunc)(const char *, ...))
104 uintmax_t addr, size;
105 const unsigned int mbyte = 1024 * 1024;
107 prfunc("Physical memory chunk(s):\n");
108 for (i = 0; i < hwcnt; ++i) {
109 addr = hwregions[i].addr;
110 size = hwregions[i].size;
111 prfunc(" 0x%08jx - 0x%08jx, %5ju MB (%7ju pages)\n", addr,
112 addr + size - 1, size / mbyte, size / PAGE_SIZE);
115 prfunc("Excluded memory regions:\n");
116 for (i = 0; i < excnt; ++i) {
117 addr = exregions[i].addr;
118 size = exregions[i].size;
119 flags = exregions[i].flags;
120 prfunc(" 0x%08jx - 0x%08jx, %5ju MB (%7ju pages) %s %s\n",
121 addr, addr + size - 1, size / mbyte, size / PAGE_SIZE,
122 (flags & EXFLAG_NOALLOC) ? "NoAlloc" : "",
123 (flags & EXFLAG_NODUMP) ? "NoDump" : "");
127 prfunc("Avail lists:\n");
128 for (i = 0; phys_avail[i] != 0; ++i) {
129 prfunc(" phys_avail[%d] 0x%08x\n", i, phys_avail[i]);
131 for (i = 0; dump_avail[i] != 0; ++i) {
132 prfunc(" dump_avail[%d] 0x%08x\n", i, dump_avail[i]);
138 * Print the contents of the static mapping table. Used for bootverbose.
141 arm_physmem_print_tables()
144 physmem_dump_tables(printf);
148 * Walk the list of hardware regions, processing it against the list of
149 * exclusions that contain the given exflags, and generating an "avail list".
151 * Updates the kernel global 'realmem' with the sum of all pages in hw regions.
153 * Returns the number of pages of non-excluded memory added to the avail list.
156 regions_to_avail(vm_paddr_t *avail, uint32_t exflags)
158 size_t acnt, exi, hwi;
159 vm_paddr_t end, start, xend, xstart;
161 const struct region *exp, *hwp;
166 for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) {
168 end = hwp->size + start;
169 realmem += arm32_btop(end - start);
170 for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) {
172 * If the excluded region does not match given flags,
173 * continue checking with the next excluded region.
175 if ((exp->flags & exflags) == 0)
178 xend = exp->size + xstart;
180 * If the excluded region ends before this hw region,
181 * continue checking with the next excluded region.
186 * If the excluded region begins after this hw region
187 * we're done because both lists are sorted.
192 * If the excluded region completely covers this hw
193 * region, shrink this hw region to zero size.
195 if ((start >= xstart) && (end <= xend)) {
201 * If the excluded region falls wholly within this hw
202 * region without abutting or overlapping the beginning
203 * or end, create an available entry from the leading
204 * fragment, then adjust the start of this hw region to
205 * the end of the excluded region, and continue checking
206 * the next excluded region because another exclusion
207 * could affect the remainder of this hw region.
209 if ((xstart > start) && (xend < end)) {
210 avail[acnt++] = start;
211 avail[acnt++] = xstart;
212 availmem += arm32_btop(xstart - start);
217 * We know the excluded region overlaps either the start
218 * or end of this hardware region (but not both), trim
219 * the excluded portion off the appropriate end.
227 * If the trimming actions above left a non-zero size, create an
228 * available entry for it.
231 avail[acnt++] = start;
233 availmem += arm32_btop(end - start);
235 if (acnt >= MAX_AVAIL_ENTRIES)
236 panic("Not enough space in the dump/phys_avail arrays");
243 * Insertion-sort a new entry into a regions list; sorted by start address.
246 insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr,
247 vm_size_t size, uint32_t flags)
250 struct region *ep, *rp;
253 for (i = 0, rp = regions; i < rcnt; ++i, ++rp) {
254 if (addr < rp->addr) {
255 bcopy(rp, rp + 1, (ep - rp) * sizeof(*rp));
265 * Add a hardware memory region.
268 arm_physmem_hardware_region(vm_paddr_t pa, vm_size_t sz)
273 * Filter out the page at PA 0x00000000. The VM can't handle it, as
274 * pmap_extract() == 0 means failure.
282 * Round the starting address up to a page boundary, and truncate the
283 * ending page down to a page boundary.
285 adj = round_page(pa) - pa;
287 sz = trunc_page(sz - adj);
289 if (hwcnt < nitems(hwregions))
290 insert_region(hwregions, hwcnt++, pa, sz, 0);
294 * Add an exclusion region.
296 void arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
301 * Truncate the starting address down to a page boundary, and round the
302 * ending page up to a page boundary.
304 adj = pa - trunc_page(pa);
306 sz = round_page(sz + adj);
308 if (excnt < nitems(exregions))
309 insert_region(exregions, excnt++, pa, sz, exflags);
313 * Process all the regions added earlier into the global avail lists.
316 arm_physmem_init_kernel_globals(void)
319 regions_to_avail(dump_avail, EXFLAG_NODUMP);
320 physmem = regions_to_avail(phys_avail, EXFLAG_NOALLOC);
326 DB_SHOW_COMMAND(physmem, db_show_physmem)
329 physmem_dump_tables(db_printf);