2 * Copyright (c) 2018 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
33 #include <sys/malloc.h>
36 #include <contrib/dev/acpica/include/acpi.h>
37 #include <dev/acpica/acpivar.h>
38 #include <dev/nvdimm/nvdimm_var.h>
41 uint32_t_compare(const void *a, const void *b)
44 return (*(const uint32_t *)a - *(const uint32_t *)b);
48 find_matches(ACPI_TABLE_NFIT *nfitbl, uint16_t type, uint16_t offset,
49 uint64_t mask, uint64_t value, void **ptrs, int ptrs_len)
51 ACPI_NFIT_HEADER *h, *end;
56 h = (ACPI_NFIT_HEADER *)(nfitbl + 1);
57 end = (ACPI_NFIT_HEADER *)((char *)nfitbl +
58 nfitbl->Header.Length);
59 load_size = roundup2(flsl(mask), 8) / 8;
63 if (h->Type == type) {
64 bcopy((char *)h + offset, &val, load_size);
76 h = (ACPI_NFIT_HEADER *)((char *)h + h->Length);
82 malloc_find_matches(ACPI_TABLE_NFIT *nfitbl, uint16_t type, uint16_t offset,
83 uint64_t mask, uint64_t value, void ***ptrs, int *ptrs_len)
87 count = find_matches(nfitbl, type, offset, mask, value, NULL, 0);
93 *ptrs = mallocarray(count, sizeof(void *), M_NVDIMM, M_WAITOK);
94 find_matches(nfitbl, type, offset, mask, value, *ptrs, *ptrs_len);
98 acpi_nfit_get_dimm_ids(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t **listp,
101 ACPI_NFIT_SYSTEM_ADDRESS **spas;
102 ACPI_NFIT_MEMORY_MAP ***regions;
103 int i, j, k, maxids, num_spas, *region_counts;
105 acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
111 regions = mallocarray(num_spas, sizeof(uint16_t *), M_NVDIMM,
113 region_counts = mallocarray(num_spas, sizeof(int), M_NVDIMM, M_WAITOK);
114 for (i = 0; i < num_spas; i++) {
115 acpi_nfit_get_region_mappings_by_spa_range(nfitbl,
116 spas[i]->RangeIndex, ®ions[i], ®ion_counts[i]);
119 for (i = 0; i < num_spas; i++) {
120 maxids += region_counts[i];
122 *listp = mallocarray(maxids, sizeof(nfit_handle_t), M_NVDIMM, M_WAITOK);
124 for (i = 0; i < num_spas; i++) {
125 for (j = 0; j < region_counts[i]; j++)
126 (*listp)[k++] = regions[i][j]->DeviceHandle;
128 qsort((*listp), maxids, sizeof(uint32_t), uint32_t_compare);
130 for (j = 1; j < maxids; j++) {
131 if ((*listp)[i] != (*listp)[j])
132 (*listp)[++i] = (*listp)[j];
135 free(region_counts, M_NVDIMM);
136 for (i = 0; i < num_spas; i++)
137 free(regions[i], M_NVDIMM);
138 free(regions, M_NVDIMM);
139 free(spas, M_NVDIMM);
143 acpi_nfit_get_spa_range(ACPI_TABLE_NFIT *nfitbl, uint16_t range_index,
144 ACPI_NFIT_SYSTEM_ADDRESS **spa)
148 find_matches(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS,
149 offsetof(ACPI_NFIT_SYSTEM_ADDRESS, RangeIndex), UINT16_MAX,
150 range_index, (void **)spa, 1);
154 acpi_nfit_get_spa_ranges(ACPI_TABLE_NFIT *nfitbl,
155 ACPI_NFIT_SYSTEM_ADDRESS ***listp, int *countp)
158 malloc_find_matches(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS, 0, 0, 0,
159 (void ***)listp, countp);
163 acpi_nfit_get_region_mappings_by_spa_range(ACPI_TABLE_NFIT *nfitbl,
164 uint16_t spa_range_index, ACPI_NFIT_MEMORY_MAP ***listp, int *countp)
167 malloc_find_matches(nfitbl, ACPI_NFIT_TYPE_MEMORY_MAP,
168 offsetof(ACPI_NFIT_MEMORY_MAP, RangeIndex), UINT16_MAX,
169 spa_range_index, (void ***)listp, countp);
172 void acpi_nfit_get_control_region(ACPI_TABLE_NFIT *nfitbl,
173 uint16_t control_region_index, ACPI_NFIT_CONTROL_REGION **out)
177 find_matches(nfitbl, ACPI_NFIT_TYPE_CONTROL_REGION,
178 offsetof(ACPI_NFIT_CONTROL_REGION, RegionIndex), UINT16_MAX,
179 control_region_index, (void **)out, 1);
183 acpi_nfit_get_flush_addrs(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t dimm,
184 uint64_t ***listp, int *countp)
186 ACPI_NFIT_FLUSH_ADDRESS *subtable;
190 find_matches(nfitbl, ACPI_NFIT_TYPE_FLUSH_ADDRESS,
191 offsetof(ACPI_NFIT_FLUSH_ADDRESS, DeviceHandle), UINT32_MAX,
192 dimm, (void **)&subtable, 1);
193 if (subtable == NULL || subtable->HintCount == 0) {
198 *countp = subtable->HintCount;
199 *listp = mallocarray(subtable->HintCount, sizeof(uint64_t *), M_NVDIMM,
201 for (i = 0; i < subtable->HintCount; i++)
202 (*listp)[i] = (uint64_t *)(intptr_t)subtable->HintAddress[i];