2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Dell EMC Isilon
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/bitstring.h>
36 #include <sys/kernel.h>
37 #include <sys/linker.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
44 #include <vm/vm_param.h>
46 #include <machine/metadata.h>
47 #include <machine/pc/bios.h>
49 #include <contrib/dev/acpica/include/acpi.h>
51 #include <dev/nvdimm/nvdimm_var.h>
53 struct nvdimm_e820_bus {
54 SLIST_HEAD(, SPA_mapping) spas;
57 #define NVDIMM_E820 "nvdimm_e820"
59 static MALLOC_DEFINE(M_NVDIMM_E820, NVDIMM_E820, "NVDIMM e820 bus memory");
61 static const struct bios_smap *smapbase;
65 } pram_segments[VM_PHYSSEG_MAX];
66 static unsigned pram_nreg;
69 nvdimm_e820_dump_prams(device_t dev, const char *func, int hintunit)
76 sbuf_new(&sb, buffer, sizeof(buffer), SBUF_FIXEDLEN);
77 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
79 sbuf_printf(&sb, "%s: %s: ", device_get_nameunit(dev), func);
81 sbuf_cat(&sb, "Found BIOS PRAM regions: ");
83 sbuf_printf(&sb, "Remaining unallocated PRAM regions after "
84 "hint %d: ", hintunit);
86 for (i = 0; i < pram_nreg; i++) {
87 if (pram_segments[i].size == 0)
93 sbuf_printf(&sb, "0x%jx-0x%jx",
94 (uintmax_t)pram_segments[i].start,
95 (uintmax_t)pram_segments[i].start + pram_segments[i].size
100 sbuf_cat(&sb, "<none>");
101 sbuf_putc(&sb, '\n');
107 nvdimm_e820_create_spas(device_t dev)
109 static const vm_size_t HINT_ALL = (vm_size_t)-1;
111 ACPI_NFIT_SYSTEM_ADDRESS nfit_sa;
112 struct SPA_mapping *spa_mapping;
113 enum SPA_mapping_type spa_type;
114 struct nvdimm_e820_bus *sc;
115 const char *hinttype;
116 long hintaddrl, hintsizel;
122 sc = device_get_softc(dev);
124 nfit_sa = (ACPI_NFIT_SYSTEM_ADDRESS) { 0 };
127 nvdimm_e820_dump_prams(dev, __func__, -1);
130 resource_long_value("nvdimm_spa", i, "maddr", &hintaddrl) == 0;
132 if (resource_long_value("nvdimm_spa", i, "msize", &hintsizel)
134 device_printf(dev, "hint.nvdimm_spa.%u missing msize\n",
139 hintaddr = (vm_paddr_t)hintaddrl;
140 hintsize = (vm_size_t)hintsizel;
141 if ((hintaddr & PAGE_MASK) != 0 ||
142 ((hintsize & PAGE_MASK) != 0 && hintsize != HINT_ALL)) {
143 device_printf(dev, "hint.nvdimm_spa.%u addr or size "
144 "not page aligned\n", i);
148 if (resource_string_value("nvdimm_spa", i, "type", &hinttype)
150 device_printf(dev, "hint.nvdimm_spa.%u missing type\n",
154 spa_type = nvdimm_spa_type_from_name(hinttype);
155 if (spa_type == SPA_TYPE_UNKNOWN) {
156 device_printf(dev, "hint.nvdimm_spa%u.type does not "
157 "match any known SPA types\n", i);
161 for (j = 0; j < pram_nreg; j++) {
162 if (pram_segments[j].start <= hintaddr &&
163 (hintsize == HINT_ALL ||
164 (pram_segments[j].start + pram_segments[j].size) >=
165 (hintaddr + hintsize)))
169 if (j == pram_nreg) {
170 device_printf(dev, "hint.nvdimm_spa%u hint does not "
171 "match any region\n", i);
175 /* Carve off "SPA" from available regions. */
176 if (pram_segments[j].start == hintaddr) {
177 /* Easy case first: beginning of segment. */
178 if (hintsize == HINT_ALL)
179 hintsize = pram_segments[j].size;
180 pram_segments[j].start += hintsize;
181 pram_segments[j].size -= hintsize;
182 /* We might leave an empty segment; who cares. */
183 } else if (hintsize == HINT_ALL ||
184 (pram_segments[j].start + pram_segments[j].size) ==
185 (hintaddr + hintsize)) {
186 /* 2nd easy case: end of segment. */
187 if (hintsize == HINT_ALL)
188 hintsize = pram_segments[j].size -
189 (hintaddr - pram_segments[j].start);
190 pram_segments[j].size -= hintsize;
192 /* Hard case: mid segment. */
193 if (pram_nreg == nitems(pram_segments)) {
194 /* Improbable, but handle gracefully. */
195 device_printf(dev, "Ran out of %zu segments\n",
196 nitems(pram_segments));
201 if (j != pram_nreg - 1) {
202 memmove(&pram_segments[j + 2],
203 &pram_segments[j + 1],
204 (pram_nreg - 1 - j) *
205 sizeof(pram_segments[0]));
209 pram_segments[j + 1].start = hintaddr + hintsize;
210 pram_segments[j + 1].size =
211 (pram_segments[j].start + pram_segments[j].size) -
212 (hintaddr + hintsize);
213 pram_segments[j].size = hintaddr -
214 pram_segments[j].start;
218 nvdimm_e820_dump_prams(dev, __func__, (int)i);
220 spa_mapping = malloc(sizeof(*spa_mapping), M_NVDIMM_E820,
223 /* Mock up a super primitive table for nvdimm_spa_init(). */
224 nfit_sa.RangeIndex = i;
226 nfit_sa.Address = hintaddr;
227 nfit_sa.Length = hintsize;
228 nfit_sa.MemoryMapping = EFI_MD_ATTR_WB | EFI_MD_ATTR_WT |
231 error = nvdimm_spa_init(spa_mapping, &nfit_sa, spa_type);
233 nvdimm_spa_fini(spa_mapping);
234 free(spa_mapping, M_NVDIMM_E820);
238 SLIST_INSERT_HEAD(&sc->spas, spa_mapping, link);
244 nvdimm_e820_remove_spas(device_t dev)
246 struct nvdimm_e820_bus *sc;
247 struct SPA_mapping *spa, *next;
249 sc = device_get_softc(dev);
251 SLIST_FOREACH_SAFE(spa, &sc->spas, link, next) {
252 nvdimm_spa_fini(spa);
253 SLIST_REMOVE_HEAD(&sc->spas, link);
254 free(spa, M_NVDIMM_E820);
260 nvdimm_e820_identify(driver_t *driver __unused, device_t parent)
265 if (resource_disabled(NVDIMM_E820, 0))
267 /* Just create a single instance of the fake bus. */
268 if (device_find_child(parent, NVDIMM_E820, -1) != NULL)
271 kmdp = preload_search_by_type("elf kernel");
273 kmdp = preload_search_by_type("elf64 kernel");
274 smapbase = (const void *)preload_search_info(kmdp,
275 MODINFO_METADATA | MODINFOMD_SMAP);
277 /* Only supports BIOS SMAP for now. */
278 if (smapbase == NULL)
281 child = BUS_ADD_CHILD(parent, 0, NVDIMM_E820, -1);
283 device_printf(parent, "add %s child failed\n", NVDIMM_E820);
287 nvdimm_e820_probe(device_t dev)
290 * nexus panics if a child doesn't have ivars. BUS_ADD_CHILD uses
291 * nexus_add_child, which creates fuckin ivars. but sometimes if you
292 * unload and reload nvdimm_e820, the device node stays but the ivars
293 * are deleted??? avoid trivial panic but this is a kludge.
295 if (device_get_ivars(dev) == NULL)
299 device_set_desc(dev, "Legacy e820 NVDIMM root device");
300 return (BUS_PROBE_NOWILDCARD);
304 nvdimm_e820_attach(device_t dev)
306 const struct bios_smap *smapend, *smap;
311 smapsize = *((const uint32_t *)smapbase - 1);
312 smapend = (const void *)((const char *)smapbase + smapsize);
314 for (nregions = 0, smap = smapbase; smap < smapend; smap++) {
315 if (smap->type != SMAP_TYPE_PRAM || smap->length == 0)
317 pram_segments[nregions].start = smap->base;
318 pram_segments[nregions].size = smap->length;
320 device_printf(dev, "Found PRAM 0x%jx +0x%jx\n",
321 (uintmax_t)smap->base, (uintmax_t)smap->length);
327 device_printf(dev, "No e820 PRAM regions detected\n");
330 pram_nreg = nregions;
332 error = nvdimm_e820_create_spas(dev);
337 nvdimm_e820_detach(device_t dev)
341 error = nvdimm_e820_remove_spas(dev);
345 static device_method_t nvdimm_e820_methods[] = {
346 DEVMETHOD(device_identify, nvdimm_e820_identify),
347 DEVMETHOD(device_probe, nvdimm_e820_probe),
348 DEVMETHOD(device_attach, nvdimm_e820_attach),
349 DEVMETHOD(device_detach, nvdimm_e820_detach),
353 static driver_t nvdimm_e820_driver = {
356 sizeof(struct nvdimm_e820_bus),
359 static devclass_t nvdimm_e820_devclass;
362 nvdimm_e820_chainevh(struct module *m, int e, void *arg __unused)
365 device_t dev, parent;
366 int i, error, maxunit;
370 dc = nvdimm_e820_devclass;
371 maxunit = devclass_get_maxunit(dc);
372 for (i = 0; i < maxunit; i++) {
373 dev = devclass_get_device(dc, i);
376 parent = device_get_parent(dev);
377 if (parent == NULL) {
378 /* Not sure how this would happen. */
381 error = device_delete_child(parent, dev);
387 /* Prevent compiler warning about unhandled cases. */
393 DRIVER_MODULE(nvdimm_e820, nexus, nvdimm_e820_driver, nvdimm_e820_devclass,
394 nvdimm_e820_chainevh, NULL);