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