1261643Sian/*-
2261643Sian * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
3261643Sian * All rights excluded.
4261643Sian *
5261643Sian * Redistribution and use in source and binary forms, with or without
6261643Sian * modification, are permitted provided that the following conditions
7261643Sian * are met:
8261643Sian * 1. Redistributions of source code must retain the above copyright
9261643Sian *    notice, this list of conditions and the following disclaimer.
10261643Sian * 2. Redistributions in binary form must reproduce the above copyright
11261643Sian *    notice, this list of conditions and the following disclaimer in the
12261643Sian *    documentation and/or other materials provided with the distribution.
13261643Sian *
14261643Sian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15261643Sian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16261643Sian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17261643Sian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18261643Sian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19261643Sian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20261643Sian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21261643Sian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22261643Sian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23261643Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24261643Sian * SUCH DAMAGE.
25261643Sian */
26261643Sian
27261643Sian#include <sys/cdefs.h>
28261643Sian__FBSDID("$FreeBSD: releng/10.3/sys/arm/arm/physmem.c 294685 2016-01-24 22:00:36Z ian $");
29261643Sian
30261643Sian#include "opt_ddb.h"
31261643Sian
32261643Sian/*
33261643Sian * Routines for describing and initializing anything related to physical memory.
34261643Sian */
35261643Sian
36261643Sian#include <sys/param.h>
37261643Sian#include <sys/systm.h>
38261643Sian#include <vm/vm.h>
39278730Sian#include <machine/md_var.h>
40261643Sian#include <machine/physmem.h>
41261643Sian
42261643Sian/*
43261643Sian * These structures are used internally to keep track of regions of physical
44261643Sian * ram, and regions within the physical ram that need to be excluded.  An
45261643Sian * exclusion region can be excluded from crash dumps, from the vm pool of pages
46261643Sian * that can be allocated, or both, depending on the exclusion flags associated
47261643Sian * with the region.
48261643Sian */
49261643Sian#define	MAX_HWCNT	10
50261643Sian#define	MAX_EXCNT	10
51261643Sian
52261643Sianstruct region {
53266194Sian	vm_paddr_t	addr;
54261643Sian	vm_size_t	size;
55261643Sian	uint32_t	flags;
56261643Sian};
57261643Sian
58261643Sianstatic struct region hwregions[MAX_HWCNT];
59261643Sianstatic struct region exregions[MAX_EXCNT];
60261643Sian
61261643Sianstatic size_t hwcnt;
62261643Sianstatic size_t excnt;
63261643Sian
64261643Sian/*
65261643Sian * These "avail lists" are globals used to communicate physical memory layout to
66261643Sian * other parts of the kernel.  Within the arrays, each value is the starting
67261643Sian * address of a contiguous area of physical address space.  The values at even
68261643Sian * indexes are areas that contain usable memory and the values at odd indexes
69261643Sian * are areas that aren't usable.  Each list is terminated by a pair of zero
70261643Sian * entries.
71261643Sian *
72261643Sian * dump_avail tells the dump code what regions to include in a crash dump, and
73261643Sian * phys_avail is the way we hand all the remaining physical ram we haven't used
74261643Sian * in early kernel init over to the vm system for allocation management.
75261643Sian *
76261643Sian * We size these arrays to hold twice as many available regions as we allow for
77261643Sian * hardware memory regions, to allow for the fact that exclusions can split a
78261643Sian * hardware region into two or more available regions.  In the real world there
79261643Sian * will typically be one or two hardware regions and two or three exclusions.
80261643Sian *
81261643Sian * Each available region in this list occupies two array slots (the start of the
82261643Sian * available region and the start of the unavailable region that follows it).
83261643Sian */
84261643Sian#define	MAX_AVAIL_REGIONS	(MAX_HWCNT * 2)
85261643Sian#define	MAX_AVAIL_ENTRIES	(MAX_AVAIL_REGIONS * 2)
86261643Sian
87261643Sianvm_paddr_t phys_avail[MAX_AVAIL_ENTRIES + 2]; /* +2 to allow for a pair  */
88261643Sianvm_paddr_t dump_avail[MAX_AVAIL_ENTRIES + 2]; /* of zeroes to terminate. */
89261643Sian
90278730Sian/*
91278730Sian * realmem is the total number of hardware pages, excluded or not.
92278730Sian * Maxmem is one greater than the last physical page number.
93278730Sian */
94261643Sianlong realmem;
95278730Sianlong Maxmem;
96261643Sian
97266194Sian/* The address at which the kernel was loaded.  Set early in initarm(). */
98266194Sianvm_paddr_t arm_physmem_kernaddr;
99266194Sian
100261643Sian/*
101261643Sian * Print the contents of the physical and excluded region tables using the
102261643Sian * provided printf-like output function (which will be either printf or
103261643Sian * db_printf).
104261643Sian */
105261643Sianstatic void
106261643Sianphysmem_dump_tables(int (*prfunc)(const char *, ...))
107261643Sian{
108261643Sian	int flags, i;
109261643Sian	uintmax_t addr, size;
110261643Sian	const unsigned int mbyte = 1024 * 1024;
111261643Sian
112261643Sian	prfunc("Physical memory chunk(s):\n");
113261643Sian	for (i = 0; i < hwcnt; ++i) {
114261643Sian		addr = hwregions[i].addr;
115261643Sian		size = hwregions[i].size;
116261643Sian		prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages)\n", addr,
117261643Sian		    addr + size - 1, size / mbyte, size / PAGE_SIZE);
118261643Sian	}
119261643Sian
120261643Sian	prfunc("Excluded memory regions:\n");
121261643Sian	for (i = 0; i < excnt; ++i) {
122261643Sian		addr  = exregions[i].addr;
123261643Sian		size  = exregions[i].size;
124261643Sian		flags = exregions[i].flags;
125261643Sian		prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages) %s %s\n",
126261643Sian		    addr, addr + size - 1, size / mbyte, size / PAGE_SIZE,
127261643Sian		    (flags & EXFLAG_NOALLOC) ? "NoAlloc" : "",
128261643Sian		    (flags & EXFLAG_NODUMP)  ? "NoDump" : "");
129261643Sian	}
130266194Sian
131266194Sian#ifdef DEBUG
132266194Sian	prfunc("Avail lists:\n");
133266194Sian	for (i = 0; phys_avail[i] != 0; ++i) {
134266194Sian		prfunc("  phys_avail[%d] 0x%08x\n", i, phys_avail[i]);
135266194Sian	}
136266194Sian	for (i = 0; dump_avail[i] != 0; ++i) {
137266194Sian		prfunc("  dump_avail[%d] 0x%08x\n", i, dump_avail[i]);
138266194Sian	}
139266194Sian#endif
140261643Sian}
141261643Sian
142261643Sian/*
143261643Sian * Print the contents of the static mapping table.  Used for bootverbose.
144261643Sian */
145261643Sianvoid
146261643Sianarm_physmem_print_tables()
147261643Sian{
148261643Sian
149261643Sian	physmem_dump_tables(printf);
150261643Sian}
151261643Sian
152261643Sian/*
153261643Sian * Walk the list of hardware regions, processing it against the list of
154261643Sian * exclusions that contain the given exflags, and generating an "avail list".
155261643Sian *
156283319Sian * Updates the value at *pavail with the sum of all pages in all hw regions.
157261643Sian *
158261643Sian * Returns the number of pages of non-excluded memory added to the avail list.
159261643Sian */
160278730Sianstatic size_t
161278730Sianregions_to_avail(vm_paddr_t *avail, uint32_t exflags, long *pavail)
162261643Sian{
163261643Sian	size_t acnt, exi, hwi;
164294685Sian	uint64_t end, start, xend, xstart;
165261643Sian	long availmem;
166261643Sian	const struct region *exp, *hwp;
167261643Sian
168261643Sian	realmem = 0;
169261643Sian	availmem = 0;
170261643Sian	acnt = 0;
171261643Sian	for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) {
172261643Sian		start = hwp->addr;
173261643Sian		end   = hwp->size + start;
174294685Sian		realmem += arm32_btop((vm_offset_t)(end - start));
175261643Sian		for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) {
176273676Sian			/*
177273676Sian			 * If the excluded region does not match given flags,
178273676Sian			 * continue checking with the next excluded region.
179273676Sian			 */
180273676Sian			if ((exp->flags & exflags) == 0)
181273676Sian				continue;
182261643Sian			xstart = exp->addr;
183261643Sian			xend   = exp->size + xstart;
184261643Sian			/*
185261643Sian			 * If the excluded region ends before this hw region,
186261643Sian			 * continue checking with the next excluded region.
187261643Sian			 */
188261643Sian			if (xend <= start)
189261643Sian				continue;
190261643Sian			/*
191261643Sian			 * If the excluded region begins after this hw region
192261643Sian			 * we're done because both lists are sorted.
193261643Sian			 */
194261643Sian			if (xstart >= end)
195261643Sian				break;
196261643Sian			/*
197261643Sian			 * If the excluded region completely covers this hw
198261643Sian			 * region, shrink this hw region to zero size.
199261643Sian			 */
200261643Sian			if ((start >= xstart) && (end <= xend)) {
201261643Sian				start = xend;
202261643Sian				end = xend;
203261643Sian				break;
204261643Sian			}
205261643Sian			/*
206261643Sian			 * If the excluded region falls wholly within this hw
207261643Sian			 * region without abutting or overlapping the beginning
208261643Sian			 * or end, create an available entry from the leading
209261643Sian			 * fragment, then adjust the start of this hw region to
210261643Sian			 * the end of the excluded region, and continue checking
211261643Sian			 * the next excluded region because another exclusion
212261643Sian			 * could affect the remainder of this hw region.
213261643Sian			 */
214261643Sian			if ((xstart > start) && (xend < end)) {
215294685Sian				avail[acnt++] = (vm_paddr_t)start;
216294685Sian				avail[acnt++] = (vm_paddr_t)xstart;
217294685Sian				availmem +=
218294685Sian				    arm32_btop((vm_offset_t)(xstart - start));
219261643Sian				start = xend;
220261643Sian				continue;
221261643Sian			}
222261643Sian			/*
223266194Sian			 * We know the excluded region overlaps either the start
224266194Sian			 * or end of this hardware region (but not both), trim
225266194Sian			 * the excluded portion off the appropriate end.
226261643Sian			 */
227266194Sian			if (xstart <= start)
228266194Sian				start = xend;
229266194Sian			else
230261643Sian				end = xstart;
231261643Sian		}
232261643Sian		/*
233261643Sian		 * If the trimming actions above left a non-zero size, create an
234261643Sian		 * available entry for it.
235261643Sian		 */
236261643Sian		if (end > start) {
237294685Sian			avail[acnt++] = (vm_paddr_t)start;
238294685Sian			avail[acnt++] = (vm_paddr_t)end;
239294685Sian			availmem += arm32_btop((vm_offset_t)(end - start));
240261643Sian		}
241261643Sian		if (acnt >= MAX_AVAIL_ENTRIES)
242261643Sian			panic("Not enough space in the dump/phys_avail arrays");
243261643Sian	}
244261643Sian
245278730Sian	if (pavail)
246278730Sian		*pavail = availmem;
247278730Sian	return (acnt);
248261643Sian}
249261643Sian
250261643Sian/*
251261643Sian * Insertion-sort a new entry into a regions list; sorted by start address.
252261643Sian */
253261643Sianstatic void
254266194Sianinsert_region(struct region *regions, size_t rcnt, vm_paddr_t addr,
255261643Sian    vm_size_t size, uint32_t flags)
256261643Sian{
257261643Sian	size_t i;
258261643Sian	struct region *ep, *rp;
259261643Sian
260261643Sian	ep = regions + rcnt;
261261643Sian	for (i = 0, rp = regions; i < rcnt; ++i, ++rp) {
262261643Sian		if (addr < rp->addr) {
263261643Sian			bcopy(rp, rp + 1, (ep - rp) * sizeof(*rp));
264261643Sian			break;
265261643Sian		}
266261643Sian	}
267261643Sian	rp->addr  = addr;
268261643Sian	rp->size  = size;
269261643Sian	rp->flags = flags;
270261643Sian}
271261643Sian
272261643Sian/*
273261643Sian * Add a hardware memory region.
274261643Sian */
275261643Sianvoid
276266194Sianarm_physmem_hardware_region(vm_paddr_t pa, vm_size_t sz)
277261643Sian{
278261643Sian	vm_offset_t adj;
279261643Sian
280261643Sian	/*
281261643Sian	 * Filter out the page at PA 0x00000000.  The VM can't handle it, as
282261643Sian	 * pmap_extract() == 0 means failure.
283294685Sian	 *
284294685Sian	 * Also filter out the page at the end of the physical address space --
285294685Sian	 * if addr is non-zero and addr+size is zero we wrapped to the next byte
286294685Sian	 * beyond what vm_paddr_t can express.  That leads to a NULL pointer
287294685Sian	 * deref early in startup; work around it by leaving the last page out.
288294685Sian	 *
289294685Sian	 * XXX This just in:  subtract out a whole megabyte, not just 1 page.
290294685Sian	 * Reducing the size by anything less than 1MB results in the NULL
291294685Sian	 * pointer deref in _vm_map_lock_read().  Better to give up a megabyte
292294685Sian	 * than leave some folks with an unusable system while we investigate.
293261643Sian	 */
294261643Sian	if (pa == 0) {
295261643Sian		pa  = PAGE_SIZE;
296261643Sian		sz -= PAGE_SIZE;
297294685Sian	} else if (pa + sz == 0) {
298294685Sian		sz -= 1024 * 1024;
299261643Sian	}
300261643Sian
301261643Sian	/*
302261643Sian	 * Round the starting address up to a page boundary, and truncate the
303261643Sian	 * ending page down to a page boundary.
304261643Sian	 */
305261643Sian	adj = round_page(pa) - pa;
306261643Sian	pa  = round_page(pa);
307261643Sian	sz  = trunc_page(sz - adj);
308261643Sian
309261643Sian	if (hwcnt < nitems(hwregions))
310261643Sian		insert_region(hwregions, hwcnt++, pa, sz, 0);
311261643Sian}
312261643Sian
313261643Sian/*
314261643Sian * Add an exclusion region.
315261643Sian */
316266194Sianvoid arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
317261643Sian{
318261643Sian	vm_offset_t adj;
319261643Sian
320261643Sian	/*
321261643Sian	 * Truncate the starting address down to a page boundary, and round the
322261643Sian	 * ending page up to a page boundary.
323261643Sian	 */
324261643Sian	adj = pa - trunc_page(pa);
325261643Sian	pa  = trunc_page(pa);
326261643Sian	sz  = round_page(sz + adj);
327261643Sian
328261643Sian	if (excnt < nitems(exregions))
329261643Sian		insert_region(exregions, excnt++, pa, sz, exflags);
330261643Sian}
331261643Sian
332261643Sian/*
333261643Sian * Process all the regions added earlier into the global avail lists.
334278730Sian *
335278730Sian * Updates the kernel global 'physmem' with the number of physical pages
336278730Sian * available for use (all pages not in any exclusion region).
337278730Sian *
338278730Sian * Updates the kernel global 'Maxmem' with the page number one greater then the
339278730Sian * last page of physical memory in the system.
340261643Sian */
341261643Sianvoid
342261643Sianarm_physmem_init_kernel_globals(void)
343261643Sian{
344278730Sian	size_t nextidx;
345261643Sian
346278730Sian	regions_to_avail(dump_avail, EXFLAG_NODUMP, NULL);
347278730Sian	nextidx = regions_to_avail(phys_avail, EXFLAG_NOALLOC, &physmem);
348278730Sian	if (nextidx == 0)
349278730Sian		panic("No memory entries in phys_avail");
350278730Sian	Maxmem = atop(phys_avail[nextidx - 1]);
351261643Sian}
352261643Sian
353261643Sian#ifdef DDB
354261643Sian#include <ddb/ddb.h>
355261643Sian
356261643SianDB_SHOW_COMMAND(physmem, db_show_physmem)
357261643Sian{
358261643Sian
359261643Sian	physmem_dump_tables(db_printf);
360261643Sian}
361261643Sian
362261643Sian#endif /* DDB */
363261643Sian
364