fdt_memory.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: fdt_memory.c,v 1.1.2.2 2020/12/14 14:38:05 thorpej Exp $ */
2 1.1.2.2 thorpej
3 1.1.2.2 thorpej /*-
4 1.1.2.2 thorpej * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1.2.2 thorpej * All rights reserved.
6 1.1.2.2 thorpej *
7 1.1.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 thorpej * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.1.2.2 thorpej *
10 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
12 1.1.2.2 thorpej * are met:
13 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.2.2 thorpej *
19 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 thorpej */
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #include "opt_fdt.h"
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #include <sys/cdefs.h>
35 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_memory.c,v 1.1.2.2 2020/12/14 14:38:05 thorpej Exp $");
36 1.1.2.2 thorpej
37 1.1.2.2 thorpej #include <sys/param.h>
38 1.1.2.2 thorpej #include <sys/queue.h>
39 1.1.2.2 thorpej
40 1.1.2.2 thorpej #include <libfdt.h>
41 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
42 1.1.2.2 thorpej #include <dev/fdt/fdt_memory.h>
43 1.1.2.2 thorpej
44 1.1.2.2 thorpej #ifndef FDT_MEMORY_RANGES
45 1.1.2.2 thorpej #define FDT_MEMORY_RANGES 256
46 1.1.2.2 thorpej #endif
47 1.1.2.2 thorpej
48 1.1.2.2 thorpej struct fdt_memory_range {
49 1.1.2.2 thorpej struct fdt_memory mr_mem;
50 1.1.2.2 thorpej bool mr_used;
51 1.1.2.2 thorpej TAILQ_ENTRY(fdt_memory_range) mr_list;
52 1.1.2.2 thorpej };
53 1.1.2.2 thorpej
54 1.1.2.2 thorpej static TAILQ_HEAD(fdt_memory_rangehead, fdt_memory_range) fdt_memory_ranges =
55 1.1.2.2 thorpej TAILQ_HEAD_INITIALIZER(fdt_memory_ranges);
56 1.1.2.2 thorpej
57 1.1.2.2 thorpej static struct fdt_memory_range fdt_memory_range_pool[FDT_MEMORY_RANGES];
58 1.1.2.2 thorpej
59 1.1.2.2 thorpej static struct fdt_memory_range *
60 1.1.2.2 thorpej fdt_memory_range_alloc(void)
61 1.1.2.2 thorpej {
62 1.1.2.2 thorpej for (size_t n = 0; n < FDT_MEMORY_RANGES; n++)
63 1.1.2.2 thorpej if (!fdt_memory_range_pool[n].mr_used) {
64 1.1.2.2 thorpej fdt_memory_range_pool[n].mr_used = true;
65 1.1.2.2 thorpej return &fdt_memory_range_pool[n];
66 1.1.2.2 thorpej }
67 1.1.2.2 thorpej
68 1.1.2.2 thorpej printf("%s: no free memory ranges, increase FDT_MEMORY_RANGES!\n", __func__);
69 1.1.2.2 thorpej return NULL;
70 1.1.2.2 thorpej }
71 1.1.2.2 thorpej
72 1.1.2.2 thorpej static void
73 1.1.2.2 thorpej fdt_memory_range_free(struct fdt_memory_range *mr)
74 1.1.2.2 thorpej {
75 1.1.2.2 thorpej mr->mr_used = false;
76 1.1.2.2 thorpej }
77 1.1.2.2 thorpej
78 1.1.2.2 thorpej /*
79 1.1.2.2 thorpej * Get all of physical memory, including holes.
80 1.1.2.2 thorpej */
81 1.1.2.2 thorpej void
82 1.1.2.2 thorpej fdt_memory_get(uint64_t *pstart, uint64_t *pend)
83 1.1.2.2 thorpej {
84 1.1.2.2 thorpej const int memory = OF_finddevice("/memory");
85 1.1.2.2 thorpej uint64_t cur_addr, cur_size;
86 1.1.2.2 thorpej int index;
87 1.1.2.2 thorpej
88 1.1.2.2 thorpej for (index = 0;
89 1.1.2.2 thorpej fdtbus_get_reg64(memory, index, &cur_addr, &cur_size) == 0;
90 1.1.2.2 thorpej index++) {
91 1.1.2.2 thorpej fdt_memory_add_range(cur_addr, cur_size);
92 1.1.2.2 thorpej
93 1.1.2.2 thorpej /* Assume the first entry is the start of memory */
94 1.1.2.2 thorpej if (index == 0) {
95 1.1.2.2 thorpej *pstart = cur_addr;
96 1.1.2.2 thorpej *pend = cur_addr + cur_size;
97 1.1.2.2 thorpej continue;
98 1.1.2.2 thorpej }
99 1.1.2.2 thorpej if (cur_addr + cur_size > *pend)
100 1.1.2.2 thorpej *pend = cur_addr + cur_size;
101 1.1.2.2 thorpej }
102 1.1.2.2 thorpej if (index == 0)
103 1.1.2.2 thorpej panic("Cannot determine memory size");
104 1.1.2.2 thorpej }
105 1.1.2.2 thorpej
106 1.1.2.2 thorpej /*
107 1.1.2.2 thorpej * Exclude memory ranges from memory config from the device tree
108 1.1.2.2 thorpej */
109 1.1.2.2 thorpej void
110 1.1.2.2 thorpej fdt_memory_remove_reserved(uint64_t min_addr, uint64_t max_addr)
111 1.1.2.2 thorpej {
112 1.1.2.2 thorpej uint64_t lstart = 0, lend = 0;
113 1.1.2.2 thorpej uint64_t addr, size;
114 1.1.2.2 thorpej int index, error;
115 1.1.2.2 thorpej
116 1.1.2.2 thorpej const int num = fdt_num_mem_rsv(fdtbus_get_data());
117 1.1.2.2 thorpej for (index = 0; index <= num; index++) {
118 1.1.2.2 thorpej error = fdt_get_mem_rsv(fdtbus_get_data(), index,
119 1.1.2.2 thorpej &addr, &size);
120 1.1.2.2 thorpej if (error != 0)
121 1.1.2.2 thorpej continue;
122 1.1.2.2 thorpej if (lstart <= addr && addr <= lend) {
123 1.1.2.2 thorpej size -= (lend - addr);
124 1.1.2.2 thorpej addr = lend;
125 1.1.2.2 thorpej }
126 1.1.2.2 thorpej if (size == 0)
127 1.1.2.2 thorpej continue;
128 1.1.2.2 thorpej if (addr + size <= min_addr)
129 1.1.2.2 thorpej continue;
130 1.1.2.2 thorpej if (addr >= max_addr)
131 1.1.2.2 thorpej continue;
132 1.1.2.2 thorpej if (addr < min_addr) {
133 1.1.2.2 thorpej size -= (min_addr - addr);
134 1.1.2.2 thorpej addr = min_addr;
135 1.1.2.2 thorpej }
136 1.1.2.2 thorpej if (addr + size > max_addr)
137 1.1.2.2 thorpej size = max_addr - addr;
138 1.1.2.2 thorpej fdt_memory_remove_range(addr, size);
139 1.1.2.2 thorpej lstart = addr;
140 1.1.2.2 thorpej lend = addr + size;
141 1.1.2.2 thorpej }
142 1.1.2.2 thorpej }
143 1.1.2.2 thorpej
144 1.1.2.2 thorpej void
145 1.1.2.2 thorpej fdt_memory_add_range(uint64_t start, uint64_t size)
146 1.1.2.2 thorpej {
147 1.1.2.2 thorpej struct fdt_memory_range *mr, *prev, *cur, *tmp;
148 1.1.2.2 thorpej bool inserted = false;
149 1.1.2.2 thorpej
150 1.1.2.2 thorpej mr = fdt_memory_range_alloc();
151 1.1.2.2 thorpej if (mr == NULL)
152 1.1.2.2 thorpej return;
153 1.1.2.2 thorpej
154 1.1.2.2 thorpej mr->mr_mem.start = start;
155 1.1.2.2 thorpej mr->mr_mem.end = start + size;
156 1.1.2.2 thorpej
157 1.1.2.2 thorpej /*
158 1.1.2.2 thorpej * Add the new range to the list of sorted ranges.
159 1.1.2.2 thorpej */
160 1.1.2.2 thorpej TAILQ_FOREACH(cur, &fdt_memory_ranges, mr_list)
161 1.1.2.2 thorpej if (mr->mr_mem.start <= cur->mr_mem.start) {
162 1.1.2.2 thorpej TAILQ_INSERT_BEFORE(cur, mr, mr_list);
163 1.1.2.2 thorpej inserted = true;
164 1.1.2.2 thorpej break;
165 1.1.2.2 thorpej }
166 1.1.2.2 thorpej if (!inserted)
167 1.1.2.2 thorpej TAILQ_INSERT_TAIL(&fdt_memory_ranges, mr, mr_list);
168 1.1.2.2 thorpej
169 1.1.2.2 thorpej /*
170 1.1.2.2 thorpej * Remove overlaps.
171 1.1.2.2 thorpej */
172 1.1.2.2 thorpej TAILQ_FOREACH_SAFE(mr, &fdt_memory_ranges, mr_list, tmp) {
173 1.1.2.2 thorpej prev = TAILQ_PREV(mr, fdt_memory_rangehead, mr_list);
174 1.1.2.2 thorpej if (prev && prev->mr_mem.end > mr->mr_mem.start) {
175 1.1.2.2 thorpej mr->mr_mem.start = prev->mr_mem.end;
176 1.1.2.2 thorpej if (mr->mr_mem.start >= mr->mr_mem.end) {
177 1.1.2.2 thorpej TAILQ_REMOVE(&fdt_memory_ranges, mr, mr_list);
178 1.1.2.2 thorpej fdt_memory_range_free(mr);
179 1.1.2.2 thorpej }
180 1.1.2.2 thorpej }
181 1.1.2.2 thorpej }
182 1.1.2.2 thorpej
183 1.1.2.2 thorpej /*
184 1.1.2.2 thorpej * Combine adjacent ranges.
185 1.1.2.2 thorpej */
186 1.1.2.2 thorpej TAILQ_FOREACH_SAFE(mr, &fdt_memory_ranges, mr_list, tmp) {
187 1.1.2.2 thorpej prev = TAILQ_PREV(mr, fdt_memory_rangehead, mr_list);
188 1.1.2.2 thorpej if (prev && prev->mr_mem.end == mr->mr_mem.start) {
189 1.1.2.2 thorpej prev->mr_mem.end = mr->mr_mem.end;
190 1.1.2.2 thorpej TAILQ_REMOVE(&fdt_memory_ranges, mr, mr_list);
191 1.1.2.2 thorpej fdt_memory_range_free(mr);
192 1.1.2.2 thorpej }
193 1.1.2.2 thorpej }
194 1.1.2.2 thorpej }
195 1.1.2.2 thorpej
196 1.1.2.2 thorpej void
197 1.1.2.2 thorpej fdt_memory_remove_range(uint64_t start, uint64_t size)
198 1.1.2.2 thorpej {
199 1.1.2.2 thorpej struct fdt_memory_range *mr, *next, *tmp;
200 1.1.2.2 thorpej const uint64_t end = start + size;
201 1.1.2.2 thorpej
202 1.1.2.2 thorpej TAILQ_FOREACH_SAFE(mr, &fdt_memory_ranges, mr_list, tmp) {
203 1.1.2.2 thorpej if (start <= mr->mr_mem.start && end >= mr->mr_mem.end) {
204 1.1.2.2 thorpej /*
205 1.1.2.2 thorpej * Removed range completely covers this range,
206 1.1.2.2 thorpej * just remove it.
207 1.1.2.2 thorpej */
208 1.1.2.2 thorpej TAILQ_REMOVE(&fdt_memory_ranges, mr, mr_list);
209 1.1.2.2 thorpej fdt_memory_range_free(mr);
210 1.1.2.2 thorpej } else if (start > mr->mr_mem.start && end < mr->mr_mem.end) {
211 1.1.2.2 thorpej /*
212 1.1.2.2 thorpej * Removed range is completely contained by this range,
213 1.1.2.2 thorpej * split it.
214 1.1.2.2 thorpej */
215 1.1.2.2 thorpej next = fdt_memory_range_alloc();
216 1.1.2.2 thorpej if (next == NULL)
217 1.1.2.2 thorpej panic("fdt_memory_remove_range");
218 1.1.2.2 thorpej next->mr_mem.start = end;
219 1.1.2.2 thorpej next->mr_mem.end = mr->mr_mem.end;
220 1.1.2.2 thorpej mr->mr_mem.end = start;
221 1.1.2.2 thorpej TAILQ_INSERT_AFTER(&fdt_memory_ranges, mr, next, mr_list);
222 1.1.2.2 thorpej } else if (start <= mr->mr_mem.start && end > mr->mr_mem.start && end < mr->mr_mem.end) {
223 1.1.2.2 thorpej /*
224 1.1.2.2 thorpej * Partial overlap at the beginning of the range.
225 1.1.2.2 thorpej */
226 1.1.2.2 thorpej mr->mr_mem.start = end;
227 1.1.2.2 thorpej } else if (start > mr->mr_mem.start && start < mr->mr_mem.end && end >= mr->mr_mem.end) {
228 1.1.2.2 thorpej /*
229 1.1.2.2 thorpej * Partial overlap at the end of the range.
230 1.1.2.2 thorpej */
231 1.1.2.2 thorpej mr->mr_mem.end = start;
232 1.1.2.2 thorpej }
233 1.1.2.2 thorpej KASSERT(mr->mr_mem.start < mr->mr_mem.end);
234 1.1.2.2 thorpej }
235 1.1.2.2 thorpej }
236 1.1.2.2 thorpej
237 1.1.2.2 thorpej void
238 1.1.2.2 thorpej fdt_memory_foreach(void (*fn)(const struct fdt_memory *, void *), void *arg)
239 1.1.2.2 thorpej {
240 1.1.2.2 thorpej struct fdt_memory_range *mr;
241 1.1.2.2 thorpej
242 1.1.2.2 thorpej TAILQ_FOREACH(mr, &fdt_memory_ranges, mr_list)
243 1.1.2.2 thorpej fn(&mr->mr_mem, arg);
244 1.1.2.2 thorpej }
245