xref: /freebsd/sys/dev/nvdimm/nvdimm_nfit.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2018 Intel Corporation
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/param.h>
28 #include <sys/bio.h>
29 #include <sys/bus.h>
30 #include <sys/malloc.h>
31 #include <sys/uuid.h>
32 
33 #include <contrib/dev/acpica/include/acpi.h>
34 #include <dev/acpica/acpivar.h>
35 #include <dev/nvdimm/nvdimm_var.h>
36 
37 static int
38 uint32_t_compare(const void *a, const void *b)
39 {
40 
41 	return (*(const uint32_t *)a - *(const uint32_t *)b);
42 }
43 
44 static int
45 find_matches(ACPI_TABLE_NFIT *nfitbl, uint16_t type, uint16_t offset,
46     uint64_t mask, uint64_t value, void **ptrs, int ptrs_len)
47 {
48 	ACPI_NFIT_HEADER *h, *end;
49 	uint64_t val;
50 	size_t load_size;
51 	int count;
52 
53 	h = (ACPI_NFIT_HEADER *)(nfitbl + 1);
54 	end = (ACPI_NFIT_HEADER *)((char *)nfitbl +
55 	    nfitbl->Header.Length);
56 	load_size = roundup2(flsl(mask), 8) / 8;
57 	count = 0;
58 
59 	while (h < end) {
60 		if (h->Type == type) {
61 			bcopy((char *)h + offset, &val, load_size);
62 			val &= mask;
63 			if (val == value) {
64 				if (ptrs_len > 0) {
65 					ptrs[count] = h;
66 					ptrs_len--;
67 				}
68 				count++;
69 			}
70 		}
71 		if (h->Length == 0)
72 			break;
73 		h = (ACPI_NFIT_HEADER *)((char *)h + h->Length);
74 	}
75 	return (count);
76 }
77 
78 static void
79 malloc_find_matches(ACPI_TABLE_NFIT *nfitbl, uint16_t type, uint16_t offset,
80     uint64_t mask, uint64_t value, void ***ptrs, int *ptrs_len)
81 {
82 	int count;
83 
84 	count = find_matches(nfitbl, type, offset, mask, value, NULL, 0);
85 	*ptrs_len = count;
86 	if (count == 0) {
87 		*ptrs = NULL;
88 		return;
89 	}
90 	*ptrs = mallocarray(count, sizeof(void *), M_NVDIMM, M_WAITOK);
91 	find_matches(nfitbl, type, offset, mask, value, *ptrs, *ptrs_len);
92 }
93 
94 void
95 acpi_nfit_get_dimm_ids(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t **listp,
96     int *countp)
97 {
98 	ACPI_NFIT_SYSTEM_ADDRESS **spas;
99 	ACPI_NFIT_MEMORY_MAP ***regions;
100 	int i, j, k, maxids, num_spas, *region_counts;
101 
102 	acpi_nfit_get_spa_ranges(nfitbl, &spas, &num_spas);
103 	if (num_spas == 0) {
104 		*listp = NULL;
105 		*countp = 0;
106 		return;
107 	}
108 	regions = mallocarray(num_spas, sizeof(uint16_t *), M_NVDIMM,
109 	    M_WAITOK);
110 	region_counts = mallocarray(num_spas, sizeof(int), M_NVDIMM, M_WAITOK);
111 	for (i = 0; i < num_spas; i++) {
112 		acpi_nfit_get_region_mappings_by_spa_range(nfitbl,
113 		    spas[i]->RangeIndex, &regions[i], &region_counts[i]);
114 	}
115 	maxids = 0;
116 	for (i = 0; i < num_spas; i++) {
117 		maxids += region_counts[i];
118 	}
119 	*listp = mallocarray(maxids, sizeof(nfit_handle_t), M_NVDIMM, M_WAITOK);
120 	k = 0;
121 	for (i = 0; i < num_spas; i++) {
122 		for (j = 0; j < region_counts[i]; j++)
123 			(*listp)[k++] = regions[i][j]->DeviceHandle;
124 	}
125 	qsort((*listp), maxids, sizeof(uint32_t), uint32_t_compare);
126 	i = 0;
127 	for (j = 1; j < maxids; j++) {
128 		if ((*listp)[i] != (*listp)[j])
129 			(*listp)[++i] = (*listp)[j];
130 	}
131 	*countp = i + 1;
132 	free(region_counts, M_NVDIMM);
133 	for (i = 0; i < num_spas; i++)
134 		free(regions[i], M_NVDIMM);
135 	free(regions, M_NVDIMM);
136 	free(spas, M_NVDIMM);
137 }
138 
139 void
140 acpi_nfit_get_spa_range(ACPI_TABLE_NFIT *nfitbl, uint16_t range_index,
141     ACPI_NFIT_SYSTEM_ADDRESS **spa)
142 {
143 
144 	*spa = NULL;
145 	find_matches(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS,
146 	    offsetof(ACPI_NFIT_SYSTEM_ADDRESS, RangeIndex), UINT16_MAX,
147 	    range_index, (void **)spa, 1);
148 }
149 
150 void
151 acpi_nfit_get_spa_ranges(ACPI_TABLE_NFIT *nfitbl,
152     ACPI_NFIT_SYSTEM_ADDRESS ***listp, int *countp)
153 {
154 
155 	malloc_find_matches(nfitbl, ACPI_NFIT_TYPE_SYSTEM_ADDRESS, 0, 0, 0,
156 	    (void ***)listp, countp);
157 }
158 
159 void
160 acpi_nfit_get_region_mappings_by_spa_range(ACPI_TABLE_NFIT *nfitbl,
161     uint16_t spa_range_index, ACPI_NFIT_MEMORY_MAP ***listp, int *countp)
162 {
163 
164 	malloc_find_matches(nfitbl, ACPI_NFIT_TYPE_MEMORY_MAP,
165 	    offsetof(ACPI_NFIT_MEMORY_MAP, RangeIndex), UINT16_MAX,
166 	    spa_range_index, (void ***)listp, countp);
167 }
168 
169 void acpi_nfit_get_control_region(ACPI_TABLE_NFIT *nfitbl,
170     uint16_t control_region_index, ACPI_NFIT_CONTROL_REGION **out)
171 {
172 
173 	*out = NULL;
174 	find_matches(nfitbl, ACPI_NFIT_TYPE_CONTROL_REGION,
175 	    offsetof(ACPI_NFIT_CONTROL_REGION, RegionIndex), UINT16_MAX,
176 	    control_region_index, (void **)out, 1);
177 }
178 
179 void
180 acpi_nfit_get_flush_addrs(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t dimm,
181     uint64_t ***listp, int *countp)
182 {
183 	ACPI_NFIT_FLUSH_ADDRESS *subtable;
184 	int i;
185 
186 	subtable = NULL;
187 	find_matches(nfitbl, ACPI_NFIT_TYPE_FLUSH_ADDRESS,
188 	    offsetof(ACPI_NFIT_FLUSH_ADDRESS, DeviceHandle), UINT32_MAX,
189 	    dimm, (void **)&subtable, 1);
190 	if (subtable == NULL || subtable->HintCount == 0) {
191 		*listp = NULL;
192 		*countp = 0;
193 		return;
194 	}
195 	*countp = subtable->HintCount;
196 	*listp = mallocarray(subtable->HintCount, sizeof(uint64_t *), M_NVDIMM,
197 	    M_WAITOK);
198 	for (i = 0; i < subtable->HintCount; i++)
199 		(*listp)[i] = (uint64_t *)(intptr_t)subtable->HintAddress[i];
200 }
201 
202 void
203 acpi_nfit_get_memory_maps_by_dimm(ACPI_TABLE_NFIT *nfitbl, nfit_handle_t dimm,
204     ACPI_NFIT_MEMORY_MAP ***listp, int *countp)
205 {
206 
207 	malloc_find_matches(nfitbl, ACPI_NFIT_TYPE_MEMORY_MAP,
208 	    offsetof(ACPI_NFIT_MEMORY_MAP, DeviceHandle), UINT32_MAX, dimm,
209 	    (void ***)listp, countp);
210 }
211