]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/physmem.c
MFC r351649 Check for region dups.
[FreeBSD/FreeBSD.git] / sys / arm / arm / physmem.c
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 <vm/vm.h>
42 #include <machine/md_var.h>
43 #include <arm/include/physmem.h>
44
45 /*
46  * These structures are used internally to keep track of regions of physical
47  * ram, and regions within the physical ram that need to be excluded.  An
48  * exclusion region can be excluded from crash dumps, from the vm pool of pages
49  * that can be allocated, or both, depending on the exclusion flags associated
50  * with the region.
51  */
52 #ifdef DEV_ACPI
53 #define MAX_HWCNT       32      /* ACPI needs more regions */
54 #define MAX_EXCNT       32
55 #else
56 #define MAX_HWCNT       16
57 #define MAX_EXCNT       16
58 #endif
59
60 #if defined(__arm__)
61 #define MAX_PHYS_ADDR   0xFFFFFFFFull
62 #define pm_btop(x)      arm32_btop(x)
63 #elif defined(__aarch64__)
64 #define MAX_PHYS_ADDR   0xFFFFFFFFFFFFFFFFull
65 #define pm_btop(x)      arm64_btop(x)
66 #endif
67
68 struct region {
69         vm_paddr_t      addr;
70         vm_size_t       size;
71         uint32_t        flags;
72 };
73
74 static struct region hwregions[MAX_HWCNT];
75 static struct region exregions[MAX_EXCNT];
76
77 static size_t hwcnt;
78 static size_t excnt;
79
80 /*
81  * These "avail lists" are globals used to communicate physical memory layout to
82  * other parts of the kernel.  Within the arrays, each value is the starting
83  * address of a contiguous area of physical address space.  The values at even
84  * indexes are areas that contain usable memory and the values at odd indexes
85  * are areas that aren't usable.  Each list is terminated by a pair of zero
86  * entries.
87  *
88  * dump_avail tells the dump code what regions to include in a crash dump, and
89  * phys_avail is the way we hand all the remaining physical ram we haven't used
90  * in early kernel init over to the vm system for allocation management.
91  *
92  * We size these arrays to hold twice as many available regions as we allow for
93  * hardware memory regions, to allow for the fact that exclusions can split a
94  * hardware region into two or more available regions.  In the real world there
95  * will typically be one or two hardware regions and two or three exclusions.
96  *
97  * Each available region in this list occupies two array slots (the start of the
98  * available region and the start of the unavailable region that follows it).
99  */
100 #define MAX_AVAIL_REGIONS       (MAX_HWCNT * 2)
101 #define MAX_AVAIL_ENTRIES       (MAX_AVAIL_REGIONS * 2)
102
103 vm_paddr_t phys_avail[MAX_AVAIL_ENTRIES + 2]; /* +2 to allow for a pair  */
104 vm_paddr_t dump_avail[MAX_AVAIL_ENTRIES + 2]; /* of zeroes to terminate. */
105
106 /*
107  * realmem is the total number of hardware pages, excluded or not.
108  * Maxmem is one greater than the last physical page number.
109  */
110 long realmem;
111 long Maxmem;
112
113 /* The address at which the kernel was loaded.  Set early in initarm(). */
114 vm_paddr_t arm_physmem_kernaddr;
115
116 /*
117  * Print the contents of the physical and excluded region tables using the
118  * provided printf-like output function (which will be either printf or
119  * db_printf).
120  */
121 static void
122 physmem_dump_tables(int (*prfunc)(const char *, ...))
123 {
124         int flags, i;
125         uintmax_t addr, size;
126         const unsigned int mbyte = 1024 * 1024;
127
128         prfunc("Physical memory chunk(s):\n");
129         for (i = 0; i < hwcnt; ++i) {
130                 addr = hwregions[i].addr;
131                 size = hwregions[i].size;
132                 prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages)\n", addr,
133                     addr + size - 1, size / mbyte, size / PAGE_SIZE);
134         }
135
136         prfunc("Excluded memory regions:\n");
137         for (i = 0; i < excnt; ++i) {
138                 addr  = exregions[i].addr;
139                 size  = exregions[i].size;
140                 flags = exregions[i].flags;
141                 prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages) %s %s\n",
142                     addr, addr + size - 1, size / mbyte, size / PAGE_SIZE,
143                     (flags & EXFLAG_NOALLOC) ? "NoAlloc" : "",
144                     (flags & EXFLAG_NODUMP)  ? "NoDump" : "");
145         }
146
147 #ifdef DEBUG
148         prfunc("Avail lists:\n");
149         for (i = 0; phys_avail[i] != 0; ++i) {
150                 prfunc("  phys_avail[%d] 0x%08x\n", i, phys_avail[i]);
151         }
152         for (i = 0; dump_avail[i] != 0; ++i) {
153                 prfunc("  dump_avail[%d] 0x%08x\n", i, dump_avail[i]);
154         }
155 #endif
156 }
157
158 /*
159  * Print the contents of the static mapping table.  Used for bootverbose.
160  */
161 void
162 arm_physmem_print_tables(void)
163 {
164
165         physmem_dump_tables(printf);
166 }
167
168 /*
169  * Walk the list of hardware regions, processing it against the list of
170  * exclusions that contain the given exflags, and generating an "avail list".
171  *
172  * Updates the value at *pavail with the sum of all pages in all hw regions.
173  *
174  * Returns the number of pages of non-excluded memory added to the avail list.
175  */
176 static size_t
177 regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail,
178     long *pavail, long *prealmem)
179 {
180         size_t acnt, exi, hwi;
181         uint64_t end, start, xend, xstart;
182         long availmem, totalmem;
183         const struct region *exp, *hwp;
184
185         totalmem = 0;
186         availmem = 0;
187         acnt = 0;
188         for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) {
189                 start = hwp->addr;
190                 end   = hwp->size + start;
191                 totalmem += pm_btop((vm_offset_t)(end - start));
192                 for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) {
193                         /*
194                          * If the excluded region does not match given flags,
195                          * continue checking with the next excluded region.
196                          */
197                         if ((exp->flags & exflags) == 0)
198                                 continue;
199                         xstart = exp->addr;
200                         xend   = exp->size + xstart;
201                         /*
202                          * If the excluded region ends before this hw region,
203                          * continue checking with the next excluded region.
204                          */
205                         if (xend <= start)
206                                 continue;
207                         /*
208                          * If the excluded region begins after this hw region
209                          * we're done because both lists are sorted.
210                          */
211                         if (xstart >= end)
212                                 break;
213                         /*
214                          * If the excluded region completely covers this hw
215                          * region, shrink this hw region to zero size.
216                          */
217                         if ((start >= xstart) && (end <= xend)) {
218                                 start = xend;
219                                 end = xend;
220                                 break;
221                         }
222                         /*
223                          * If the excluded region falls wholly within this hw
224                          * region without abutting or overlapping the beginning
225                          * or end, create an available entry from the leading
226                          * fragment, then adjust the start of this hw region to
227                          * the end of the excluded region, and continue checking
228                          * the next excluded region because another exclusion
229                          * could affect the remainder of this hw region.
230                          */
231                         if ((xstart > start) && (xend < end)) {
232                                 if (acnt > 0 &&
233                                     avail[acnt - 1] == (vm_paddr_t)start) {
234                                         avail[acnt - 1] = (vm_paddr_t)xstart;
235                                 } else {
236                                         avail[acnt++] = (vm_paddr_t)start;
237                                         avail[acnt++] = (vm_paddr_t)xstart;
238                                 }
239                                 availmem +=
240                                     pm_btop((vm_offset_t)(xstart - start));
241                                 start = xend;
242                                 continue;
243                         }
244                         /*
245                          * We know the excluded region overlaps either the start
246                          * or end of this hardware region (but not both), trim
247                          * the excluded portion off the appropriate end.
248                          */
249                         if (xstart <= start)
250                                 start = xend;
251                         else
252                                 end = xstart;
253                 }
254                 /*
255                  * If the trimming actions above left a non-zero size, create an
256                  * available entry for it.
257                  */
258                 if (end > start) {
259                         if (acnt > 0 && avail[acnt - 1] == (vm_paddr_t)start) {
260                                 avail[acnt - 1] = (vm_paddr_t)end;
261                         } else {
262                                 avail[acnt++] = (vm_paddr_t)start;
263                                 avail[acnt++] = (vm_paddr_t)end;
264                         }
265                         availmem += pm_btop((vm_offset_t)(end - start));
266                 }
267                 if (acnt >= maxavail)
268                         panic("Not enough space in the dump/phys_avail arrays");
269         }
270
271         if (pavail != NULL)
272                 *pavail = availmem;
273         if (prealmem != NULL)
274                 *prealmem = totalmem;
275         return (acnt);
276 }
277
278 /*
279  * Insertion-sort a new entry into a regions list; sorted by start address.
280  */
281 static size_t
282 insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr,
283     vm_size_t size, uint32_t flags)
284 {
285         size_t i;
286         struct region *ep, *rp;
287
288         ep = regions + rcnt;
289         for (i = 0, rp = regions; i < rcnt; ++i, ++rp) {
290                 if (rp->addr == addr && rp->size == size) /* Pure dup. */
291                         return (rcnt);
292                 if (flags == rp->flags) {
293                         if (addr + size == rp->addr) {
294                                 rp->addr = addr;
295                                 rp->size += size;
296                                 return (rcnt);
297                         } else if (rp->addr + rp->size == addr) {
298                                 rp->size += size;
299                                 return (rcnt);
300                         }
301                 }
302                 if (addr < rp->addr) {
303                         bcopy(rp, rp + 1, (ep - rp) * sizeof(*rp));
304                         break;
305                 }
306         }
307         rp->addr  = addr;
308         rp->size  = size;
309         rp->flags = flags;
310         rcnt++;
311
312         return (rcnt);
313 }
314
315 /*
316  * Add a hardware memory region.
317  */
318 void
319 arm_physmem_hardware_region(uint64_t pa, uint64_t sz)
320 {
321         vm_offset_t adj;
322
323         /*
324          * Filter out the page at PA 0x00000000.  The VM can't handle it, as
325          * pmap_extract() == 0 means failure.
326          */
327         if (pa == 0) {
328                 if (sz <= PAGE_SIZE)
329                         return;
330                 pa  = PAGE_SIZE;
331                 sz -= PAGE_SIZE;
332         } else if (pa > MAX_PHYS_ADDR) {
333                 /* This range is past usable memory, ignore it */
334                 return;
335         }
336
337         /*
338          * Also filter out the page at the end of the physical address space --
339          * if addr is non-zero and addr+size is zero we wrapped to the next byte
340          * beyond what vm_paddr_t can express.  That leads to a NULL pointer
341          * deref early in startup; work around it by leaving the last page out.
342          *
343          * XXX This just in:  subtract out a whole megabyte, not just 1 page.
344          * Reducing the size by anything less than 1MB results in the NULL
345          * pointer deref in _vm_map_lock_read().  Better to give up a megabyte
346          * than leave some folks with an unusable system while we investigate.
347          */
348         if ((pa + sz) > (MAX_PHYS_ADDR - 1024 * 1024)) {
349                 sz = MAX_PHYS_ADDR - pa + 1;
350                 if (sz <= 1024 * 1024)
351                         return;
352                 sz -= 1024 * 1024;
353         }
354
355         /*
356          * Round the starting address up to a page boundary, and truncate the
357          * ending page down to a page boundary.
358          */
359         adj = round_page(pa) - pa;
360         pa  = round_page(pa);
361         sz  = trunc_page(sz - adj);
362
363         if (sz > 0 && hwcnt < nitems(hwregions))
364                 hwcnt = insert_region(hwregions, hwcnt, pa, sz, 0);
365 }
366
367 /*
368  * Add an exclusion region.
369  */
370 void
371 arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
372 {
373         vm_offset_t adj;
374
375         /*
376          * Truncate the starting address down to a page boundary, and round the
377          * ending page up to a page boundary.
378          */
379         adj = pa - trunc_page(pa);
380         pa  = trunc_page(pa);
381         sz  = round_page(sz + adj);
382
383         if (excnt >= nitems(exregions))
384                 panic("failed to exclude region %#jx-%#jx", (uintmax_t)pa,
385                     (uintmax_t)(pa + sz));
386         excnt = insert_region(exregions, excnt, pa, sz, exflags);
387 }
388
389 size_t
390 arm_physmem_avail(vm_paddr_t *avail, size_t maxavail)
391 {
392
393         return (regions_to_avail(avail, EXFLAG_NOALLOC, maxavail, NULL, NULL));
394 }
395
396 /*
397  * Process all the regions added earlier into the global avail lists.
398  *
399  * Updates the kernel global 'physmem' with the number of physical pages
400  * available for use (all pages not in any exclusion region).
401  *
402  * Updates the kernel global 'Maxmem' with the page number one greater then the
403  * last page of physical memory in the system.
404  */
405 void
406 arm_physmem_init_kernel_globals(void)
407 {
408         size_t nextidx;
409
410         regions_to_avail(dump_avail, EXFLAG_NODUMP, MAX_AVAIL_ENTRIES, NULL,
411             NULL);
412         nextidx = regions_to_avail(phys_avail, EXFLAG_NOALLOC,
413             MAX_AVAIL_ENTRIES, &physmem, &realmem);
414         if (nextidx == 0)
415                 panic("No memory entries in phys_avail");
416         Maxmem = atop(phys_avail[nextidx - 1]);
417 }
418
419 #ifdef DDB
420 #include <ddb/ddb.h>
421
422 DB_SHOW_COMMAND(physmem, db_show_physmem)
423 {
424
425         physmem_dump_tables(db_printf);
426 }
427
428 #endif /* DDB */
429