]>
Commit | Line | Data |
---|---|---|
b94d5230 DW |
1 | /* |
2 | * Copyright(c) 2013-2015 Intel Corporation. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | */ | |
13 | #include <linux/list_sort.h> | |
14 | #include <linux/libnvdimm.h> | |
15 | #include <linux/module.h> | |
047fc8a1 | 16 | #include <linux/mutex.h> |
62232e45 | 17 | #include <linux/ndctl.h> |
b94d5230 DW |
18 | #include <linux/list.h> |
19 | #include <linux/acpi.h> | |
eaf96153 | 20 | #include <linux/sort.h> |
c2ad2954 | 21 | #include <linux/pmem.h> |
047fc8a1 | 22 | #include <linux/io.h> |
b94d5230 DW |
23 | #include "nfit.h" |
24 | ||
047fc8a1 RZ |
25 | /* |
26 | * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is | |
27 | * irrelevant. | |
28 | */ | |
29 | #include <asm-generic/io-64-nonatomic-hi-lo.h> | |
30 | ||
4d88a97a DW |
31 | static bool force_enable_dimms; |
32 | module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR); | |
33 | MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status"); | |
34 | ||
b94d5230 DW |
35 | static u8 nfit_uuid[NFIT_UUID_MAX][16]; |
36 | ||
6bc75619 | 37 | const u8 *to_nfit_uuid(enum nfit_uuids id) |
b94d5230 DW |
38 | { |
39 | return nfit_uuid[id]; | |
40 | } | |
6bc75619 | 41 | EXPORT_SYMBOL(to_nfit_uuid); |
b94d5230 | 42 | |
62232e45 DW |
43 | static struct acpi_nfit_desc *to_acpi_nfit_desc( |
44 | struct nvdimm_bus_descriptor *nd_desc) | |
45 | { | |
46 | return container_of(nd_desc, struct acpi_nfit_desc, nd_desc); | |
47 | } | |
48 | ||
49 | static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc) | |
50 | { | |
51 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
52 | ||
53 | /* | |
54 | * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct | |
55 | * acpi_device. | |
56 | */ | |
57 | if (!nd_desc->provider_name | |
58 | || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0) | |
59 | return NULL; | |
60 | ||
61 | return to_acpi_device(acpi_desc->dev); | |
62 | } | |
63 | ||
b94d5230 DW |
64 | static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, |
65 | struct nvdimm *nvdimm, unsigned int cmd, void *buf, | |
66 | unsigned int buf_len) | |
67 | { | |
62232e45 DW |
68 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); |
69 | const struct nd_cmd_desc *desc = NULL; | |
70 | union acpi_object in_obj, in_buf, *out_obj; | |
71 | struct device *dev = acpi_desc->dev; | |
72 | const char *cmd_name, *dimm_name; | |
73 | unsigned long dsm_mask; | |
74 | acpi_handle handle; | |
75 | const u8 *uuid; | |
76 | u32 offset; | |
77 | int rc, i; | |
78 | ||
79 | if (nvdimm) { | |
80 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
81 | struct acpi_device *adev = nfit_mem->adev; | |
82 | ||
83 | if (!adev) | |
84 | return -ENOTTY; | |
047fc8a1 | 85 | dimm_name = nvdimm_name(nvdimm); |
62232e45 DW |
86 | cmd_name = nvdimm_cmd_name(cmd); |
87 | dsm_mask = nfit_mem->dsm_mask; | |
88 | desc = nd_cmd_dimm_desc(cmd); | |
89 | uuid = to_nfit_uuid(NFIT_DEV_DIMM); | |
90 | handle = adev->handle; | |
91 | } else { | |
92 | struct acpi_device *adev = to_acpi_dev(acpi_desc); | |
93 | ||
94 | cmd_name = nvdimm_bus_cmd_name(cmd); | |
95 | dsm_mask = nd_desc->dsm_mask; | |
96 | desc = nd_cmd_bus_desc(cmd); | |
97 | uuid = to_nfit_uuid(NFIT_DEV_BUS); | |
98 | handle = adev->handle; | |
99 | dimm_name = "bus"; | |
100 | } | |
101 | ||
102 | if (!desc || (cmd && (desc->out_num + desc->in_num == 0))) | |
103 | return -ENOTTY; | |
104 | ||
105 | if (!test_bit(cmd, &dsm_mask)) | |
106 | return -ENOTTY; | |
107 | ||
108 | in_obj.type = ACPI_TYPE_PACKAGE; | |
109 | in_obj.package.count = 1; | |
110 | in_obj.package.elements = &in_buf; | |
111 | in_buf.type = ACPI_TYPE_BUFFER; | |
112 | in_buf.buffer.pointer = buf; | |
113 | in_buf.buffer.length = 0; | |
114 | ||
115 | /* libnvdimm has already validated the input envelope */ | |
116 | for (i = 0; i < desc->in_num; i++) | |
117 | in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc, | |
118 | i, buf); | |
119 | ||
120 | if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) { | |
121 | dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__, | |
122 | dimm_name, cmd_name, in_buf.buffer.length); | |
123 | print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, | |
124 | 4, in_buf.buffer.pointer, min_t(u32, 128, | |
125 | in_buf.buffer.length), true); | |
126 | } | |
127 | ||
128 | out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj); | |
129 | if (!out_obj) { | |
130 | dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name, | |
131 | cmd_name); | |
132 | return -EINVAL; | |
133 | } | |
134 | ||
135 | if (out_obj->package.type != ACPI_TYPE_BUFFER) { | |
136 | dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n", | |
137 | __func__, dimm_name, cmd_name, out_obj->type); | |
138 | rc = -EINVAL; | |
139 | goto out; | |
140 | } | |
141 | ||
142 | if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) { | |
143 | dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, | |
144 | dimm_name, cmd_name, out_obj->buffer.length); | |
145 | print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, | |
146 | 4, out_obj->buffer.pointer, min_t(u32, 128, | |
147 | out_obj->buffer.length), true); | |
148 | } | |
149 | ||
150 | for (i = 0, offset = 0; i < desc->out_num; i++) { | |
151 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf, | |
152 | (u32 *) out_obj->buffer.pointer); | |
153 | ||
154 | if (offset + out_size > out_obj->buffer.length) { | |
155 | dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n", | |
156 | __func__, dimm_name, cmd_name, i); | |
157 | break; | |
158 | } | |
159 | ||
160 | if (in_buf.buffer.length + offset + out_size > buf_len) { | |
161 | dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n", | |
162 | __func__, dimm_name, cmd_name, i); | |
163 | rc = -ENXIO; | |
164 | goto out; | |
165 | } | |
166 | memcpy(buf + in_buf.buffer.length + offset, | |
167 | out_obj->buffer.pointer + offset, out_size); | |
168 | offset += out_size; | |
169 | } | |
170 | if (offset + in_buf.buffer.length < buf_len) { | |
171 | if (i >= 1) { | |
172 | /* | |
173 | * status valid, return the number of bytes left | |
174 | * unfilled in the output buffer | |
175 | */ | |
176 | rc = buf_len - offset - in_buf.buffer.length; | |
177 | } else { | |
178 | dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n", | |
179 | __func__, dimm_name, cmd_name, buf_len, | |
180 | offset); | |
181 | rc = -ENXIO; | |
182 | } | |
183 | } else | |
184 | rc = 0; | |
185 | ||
186 | out: | |
187 | ACPI_FREE(out_obj); | |
188 | ||
189 | return rc; | |
b94d5230 DW |
190 | } |
191 | ||
192 | static const char *spa_type_name(u16 type) | |
193 | { | |
194 | static const char *to_name[] = { | |
195 | [NFIT_SPA_VOLATILE] = "volatile", | |
196 | [NFIT_SPA_PM] = "pmem", | |
197 | [NFIT_SPA_DCR] = "dimm-control-region", | |
198 | [NFIT_SPA_BDW] = "block-data-window", | |
199 | [NFIT_SPA_VDISK] = "volatile-disk", | |
200 | [NFIT_SPA_VCD] = "volatile-cd", | |
201 | [NFIT_SPA_PDISK] = "persistent-disk", | |
202 | [NFIT_SPA_PCD] = "persistent-cd", | |
203 | ||
204 | }; | |
205 | ||
206 | if (type > NFIT_SPA_PCD) | |
207 | return "unknown"; | |
208 | ||
209 | return to_name[type]; | |
210 | } | |
211 | ||
212 | static int nfit_spa_type(struct acpi_nfit_system_address *spa) | |
213 | { | |
214 | int i; | |
215 | ||
216 | for (i = 0; i < NFIT_UUID_MAX; i++) | |
217 | if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0) | |
218 | return i; | |
219 | return -1; | |
220 | } | |
221 | ||
222 | static bool add_spa(struct acpi_nfit_desc *acpi_desc, | |
223 | struct acpi_nfit_system_address *spa) | |
224 | { | |
225 | struct device *dev = acpi_desc->dev; | |
226 | struct nfit_spa *nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa), | |
227 | GFP_KERNEL); | |
228 | ||
229 | if (!nfit_spa) | |
230 | return false; | |
231 | INIT_LIST_HEAD(&nfit_spa->list); | |
232 | nfit_spa->spa = spa; | |
233 | list_add_tail(&nfit_spa->list, &acpi_desc->spas); | |
234 | dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__, | |
235 | spa->range_index, | |
236 | spa_type_name(nfit_spa_type(spa))); | |
237 | return true; | |
238 | } | |
239 | ||
240 | static bool add_memdev(struct acpi_nfit_desc *acpi_desc, | |
241 | struct acpi_nfit_memory_map *memdev) | |
242 | { | |
243 | struct device *dev = acpi_desc->dev; | |
244 | struct nfit_memdev *nfit_memdev = devm_kzalloc(dev, | |
245 | sizeof(*nfit_memdev), GFP_KERNEL); | |
246 | ||
247 | if (!nfit_memdev) | |
248 | return false; | |
249 | INIT_LIST_HEAD(&nfit_memdev->list); | |
250 | nfit_memdev->memdev = memdev; | |
251 | list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs); | |
252 | dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n", | |
253 | __func__, memdev->device_handle, memdev->range_index, | |
254 | memdev->region_index); | |
255 | return true; | |
256 | } | |
257 | ||
258 | static bool add_dcr(struct acpi_nfit_desc *acpi_desc, | |
259 | struct acpi_nfit_control_region *dcr) | |
260 | { | |
261 | struct device *dev = acpi_desc->dev; | |
262 | struct nfit_dcr *nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr), | |
263 | GFP_KERNEL); | |
264 | ||
265 | if (!nfit_dcr) | |
266 | return false; | |
267 | INIT_LIST_HEAD(&nfit_dcr->list); | |
268 | nfit_dcr->dcr = dcr; | |
269 | list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs); | |
270 | dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__, | |
271 | dcr->region_index, dcr->windows); | |
272 | return true; | |
273 | } | |
274 | ||
275 | static bool add_bdw(struct acpi_nfit_desc *acpi_desc, | |
276 | struct acpi_nfit_data_region *bdw) | |
277 | { | |
278 | struct device *dev = acpi_desc->dev; | |
279 | struct nfit_bdw *nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw), | |
280 | GFP_KERNEL); | |
281 | ||
282 | if (!nfit_bdw) | |
283 | return false; | |
284 | INIT_LIST_HEAD(&nfit_bdw->list); | |
285 | nfit_bdw->bdw = bdw; | |
286 | list_add_tail(&nfit_bdw->list, &acpi_desc->bdws); | |
287 | dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__, | |
288 | bdw->region_index, bdw->windows); | |
289 | return true; | |
290 | } | |
291 | ||
047fc8a1 RZ |
292 | static bool add_idt(struct acpi_nfit_desc *acpi_desc, |
293 | struct acpi_nfit_interleave *idt) | |
294 | { | |
295 | struct device *dev = acpi_desc->dev; | |
296 | struct nfit_idt *nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt), | |
297 | GFP_KERNEL); | |
298 | ||
299 | if (!nfit_idt) | |
300 | return false; | |
301 | INIT_LIST_HEAD(&nfit_idt->list); | |
302 | nfit_idt->idt = idt; | |
303 | list_add_tail(&nfit_idt->list, &acpi_desc->idts); | |
304 | dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__, | |
305 | idt->interleave_index, idt->line_count); | |
306 | return true; | |
307 | } | |
308 | ||
c2ad2954 RZ |
309 | static bool add_flush(struct acpi_nfit_desc *acpi_desc, |
310 | struct acpi_nfit_flush_address *flush) | |
311 | { | |
312 | struct device *dev = acpi_desc->dev; | |
313 | struct nfit_flush *nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush), | |
314 | GFP_KERNEL); | |
315 | ||
316 | if (!nfit_flush) | |
317 | return false; | |
318 | INIT_LIST_HEAD(&nfit_flush->list); | |
319 | nfit_flush->flush = flush; | |
320 | list_add_tail(&nfit_flush->list, &acpi_desc->flushes); | |
321 | dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__, | |
322 | flush->device_handle, flush->hint_count); | |
323 | return true; | |
324 | } | |
325 | ||
b94d5230 DW |
326 | static void *add_table(struct acpi_nfit_desc *acpi_desc, void *table, |
327 | const void *end) | |
328 | { | |
329 | struct device *dev = acpi_desc->dev; | |
330 | struct acpi_nfit_header *hdr; | |
331 | void *err = ERR_PTR(-ENOMEM); | |
332 | ||
333 | if (table >= end) | |
334 | return NULL; | |
335 | ||
336 | hdr = table; | |
337 | switch (hdr->type) { | |
338 | case ACPI_NFIT_TYPE_SYSTEM_ADDRESS: | |
339 | if (!add_spa(acpi_desc, table)) | |
340 | return err; | |
341 | break; | |
342 | case ACPI_NFIT_TYPE_MEMORY_MAP: | |
343 | if (!add_memdev(acpi_desc, table)) | |
344 | return err; | |
345 | break; | |
346 | case ACPI_NFIT_TYPE_CONTROL_REGION: | |
347 | if (!add_dcr(acpi_desc, table)) | |
348 | return err; | |
349 | break; | |
350 | case ACPI_NFIT_TYPE_DATA_REGION: | |
351 | if (!add_bdw(acpi_desc, table)) | |
352 | return err; | |
353 | break; | |
b94d5230 | 354 | case ACPI_NFIT_TYPE_INTERLEAVE: |
047fc8a1 RZ |
355 | if (!add_idt(acpi_desc, table)) |
356 | return err; | |
b94d5230 DW |
357 | break; |
358 | case ACPI_NFIT_TYPE_FLUSH_ADDRESS: | |
c2ad2954 RZ |
359 | if (!add_flush(acpi_desc, table)) |
360 | return err; | |
b94d5230 DW |
361 | break; |
362 | case ACPI_NFIT_TYPE_SMBIOS: | |
363 | dev_dbg(dev, "%s: smbios\n", __func__); | |
364 | break; | |
365 | default: | |
366 | dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type); | |
367 | break; | |
368 | } | |
369 | ||
370 | return table + hdr->length; | |
371 | } | |
372 | ||
373 | static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc, | |
374 | struct nfit_mem *nfit_mem) | |
375 | { | |
376 | u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle; | |
377 | u16 dcr = nfit_mem->dcr->region_index; | |
378 | struct nfit_spa *nfit_spa; | |
379 | ||
380 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { | |
381 | u16 range_index = nfit_spa->spa->range_index; | |
382 | int type = nfit_spa_type(nfit_spa->spa); | |
383 | struct nfit_memdev *nfit_memdev; | |
384 | ||
385 | if (type != NFIT_SPA_BDW) | |
386 | continue; | |
387 | ||
388 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
389 | if (nfit_memdev->memdev->range_index != range_index) | |
390 | continue; | |
391 | if (nfit_memdev->memdev->device_handle != device_handle) | |
392 | continue; | |
393 | if (nfit_memdev->memdev->region_index != dcr) | |
394 | continue; | |
395 | ||
396 | nfit_mem->spa_bdw = nfit_spa->spa; | |
397 | return; | |
398 | } | |
399 | } | |
400 | ||
401 | dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n", | |
402 | nfit_mem->spa_dcr->range_index); | |
403 | nfit_mem->bdw = NULL; | |
404 | } | |
405 | ||
406 | static int nfit_mem_add(struct acpi_nfit_desc *acpi_desc, | |
407 | struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa) | |
408 | { | |
409 | u16 dcr = __to_nfit_memdev(nfit_mem)->region_index; | |
047fc8a1 | 410 | struct nfit_memdev *nfit_memdev; |
c2ad2954 | 411 | struct nfit_flush *nfit_flush; |
b94d5230 DW |
412 | struct nfit_dcr *nfit_dcr; |
413 | struct nfit_bdw *nfit_bdw; | |
047fc8a1 RZ |
414 | struct nfit_idt *nfit_idt; |
415 | u16 idt_idx, range_index; | |
b94d5230 DW |
416 | |
417 | list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) { | |
418 | if (nfit_dcr->dcr->region_index != dcr) | |
419 | continue; | |
420 | nfit_mem->dcr = nfit_dcr->dcr; | |
421 | break; | |
422 | } | |
423 | ||
424 | if (!nfit_mem->dcr) { | |
425 | dev_dbg(acpi_desc->dev, "SPA %d missing:%s%s\n", | |
426 | spa->range_index, __to_nfit_memdev(nfit_mem) | |
427 | ? "" : " MEMDEV", nfit_mem->dcr ? "" : " DCR"); | |
428 | return -ENODEV; | |
429 | } | |
430 | ||
431 | /* | |
432 | * We've found enough to create an nvdimm, optionally | |
433 | * find an associated BDW | |
434 | */ | |
435 | list_add(&nfit_mem->list, &acpi_desc->dimms); | |
436 | ||
437 | list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) { | |
438 | if (nfit_bdw->bdw->region_index != dcr) | |
439 | continue; | |
440 | nfit_mem->bdw = nfit_bdw->bdw; | |
441 | break; | |
442 | } | |
443 | ||
444 | if (!nfit_mem->bdw) | |
445 | return 0; | |
446 | ||
447 | nfit_mem_find_spa_bdw(acpi_desc, nfit_mem); | |
047fc8a1 RZ |
448 | |
449 | if (!nfit_mem->spa_bdw) | |
450 | return 0; | |
451 | ||
452 | range_index = nfit_mem->spa_bdw->range_index; | |
453 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
454 | if (nfit_memdev->memdev->range_index != range_index || | |
455 | nfit_memdev->memdev->region_index != dcr) | |
456 | continue; | |
457 | nfit_mem->memdev_bdw = nfit_memdev->memdev; | |
458 | idt_idx = nfit_memdev->memdev->interleave_index; | |
459 | list_for_each_entry(nfit_idt, &acpi_desc->idts, list) { | |
460 | if (nfit_idt->idt->interleave_index != idt_idx) | |
461 | continue; | |
462 | nfit_mem->idt_bdw = nfit_idt->idt; | |
463 | break; | |
464 | } | |
c2ad2954 RZ |
465 | |
466 | list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) { | |
467 | if (nfit_flush->flush->device_handle != | |
468 | nfit_memdev->memdev->device_handle) | |
469 | continue; | |
470 | nfit_mem->nfit_flush = nfit_flush; | |
471 | break; | |
472 | } | |
047fc8a1 RZ |
473 | break; |
474 | } | |
475 | ||
b94d5230 DW |
476 | return 0; |
477 | } | |
478 | ||
479 | static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc, | |
480 | struct acpi_nfit_system_address *spa) | |
481 | { | |
482 | struct nfit_mem *nfit_mem, *found; | |
483 | struct nfit_memdev *nfit_memdev; | |
484 | int type = nfit_spa_type(spa); | |
485 | u16 dcr; | |
486 | ||
487 | switch (type) { | |
488 | case NFIT_SPA_DCR: | |
489 | case NFIT_SPA_PM: | |
490 | break; | |
491 | default: | |
492 | return 0; | |
493 | } | |
494 | ||
495 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
496 | int rc; | |
497 | ||
498 | if (nfit_memdev->memdev->range_index != spa->range_index) | |
499 | continue; | |
500 | found = NULL; | |
501 | dcr = nfit_memdev->memdev->region_index; | |
502 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) | |
503 | if (__to_nfit_memdev(nfit_mem)->region_index == dcr) { | |
504 | found = nfit_mem; | |
505 | break; | |
506 | } | |
507 | ||
508 | if (found) | |
509 | nfit_mem = found; | |
510 | else { | |
511 | nfit_mem = devm_kzalloc(acpi_desc->dev, | |
512 | sizeof(*nfit_mem), GFP_KERNEL); | |
513 | if (!nfit_mem) | |
514 | return -ENOMEM; | |
515 | INIT_LIST_HEAD(&nfit_mem->list); | |
516 | } | |
517 | ||
518 | if (type == NFIT_SPA_DCR) { | |
047fc8a1 RZ |
519 | struct nfit_idt *nfit_idt; |
520 | u16 idt_idx; | |
521 | ||
b94d5230 DW |
522 | /* multiple dimms may share a SPA when interleaved */ |
523 | nfit_mem->spa_dcr = spa; | |
524 | nfit_mem->memdev_dcr = nfit_memdev->memdev; | |
047fc8a1 RZ |
525 | idt_idx = nfit_memdev->memdev->interleave_index; |
526 | list_for_each_entry(nfit_idt, &acpi_desc->idts, list) { | |
527 | if (nfit_idt->idt->interleave_index != idt_idx) | |
528 | continue; | |
529 | nfit_mem->idt_dcr = nfit_idt->idt; | |
530 | break; | |
531 | } | |
b94d5230 DW |
532 | } else { |
533 | /* | |
534 | * A single dimm may belong to multiple SPA-PM | |
535 | * ranges, record at least one in addition to | |
536 | * any SPA-DCR range. | |
537 | */ | |
538 | nfit_mem->memdev_pmem = nfit_memdev->memdev; | |
539 | } | |
540 | ||
541 | if (found) | |
542 | continue; | |
543 | ||
544 | rc = nfit_mem_add(acpi_desc, nfit_mem, spa); | |
545 | if (rc) | |
546 | return rc; | |
547 | } | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
552 | static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b) | |
553 | { | |
554 | struct nfit_mem *a = container_of(_a, typeof(*a), list); | |
555 | struct nfit_mem *b = container_of(_b, typeof(*b), list); | |
556 | u32 handleA, handleB; | |
557 | ||
558 | handleA = __to_nfit_memdev(a)->device_handle; | |
559 | handleB = __to_nfit_memdev(b)->device_handle; | |
560 | if (handleA < handleB) | |
561 | return -1; | |
562 | else if (handleA > handleB) | |
563 | return 1; | |
564 | return 0; | |
565 | } | |
566 | ||
567 | static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc) | |
568 | { | |
569 | struct nfit_spa *nfit_spa; | |
570 | ||
571 | /* | |
572 | * For each SPA-DCR or SPA-PMEM address range find its | |
573 | * corresponding MEMDEV(s). From each MEMDEV find the | |
574 | * corresponding DCR. Then, if we're operating on a SPA-DCR, | |
575 | * try to find a SPA-BDW and a corresponding BDW that references | |
576 | * the DCR. Throw it all into an nfit_mem object. Note, that | |
577 | * BDWs are optional. | |
578 | */ | |
579 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { | |
580 | int rc; | |
581 | ||
582 | rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa); | |
583 | if (rc) | |
584 | return rc; | |
585 | } | |
586 | ||
587 | list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp); | |
588 | ||
589 | return 0; | |
590 | } | |
591 | ||
45def22c DW |
592 | static ssize_t revision_show(struct device *dev, |
593 | struct device_attribute *attr, char *buf) | |
594 | { | |
595 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); | |
596 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); | |
597 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); | |
598 | ||
599 | return sprintf(buf, "%d\n", acpi_desc->nfit->header.revision); | |
600 | } | |
601 | static DEVICE_ATTR_RO(revision); | |
602 | ||
603 | static struct attribute *acpi_nfit_attributes[] = { | |
604 | &dev_attr_revision.attr, | |
605 | NULL, | |
606 | }; | |
607 | ||
608 | static struct attribute_group acpi_nfit_attribute_group = { | |
609 | .name = "nfit", | |
610 | .attrs = acpi_nfit_attributes, | |
611 | }; | |
612 | ||
6bc75619 | 613 | const struct attribute_group *acpi_nfit_attribute_groups[] = { |
45def22c DW |
614 | &nvdimm_bus_attribute_group, |
615 | &acpi_nfit_attribute_group, | |
616 | NULL, | |
617 | }; | |
6bc75619 | 618 | EXPORT_SYMBOL_GPL(acpi_nfit_attribute_groups); |
45def22c | 619 | |
e6dfb2de DW |
620 | static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev) |
621 | { | |
622 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
623 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
624 | ||
625 | return __to_nfit_memdev(nfit_mem); | |
626 | } | |
627 | ||
628 | static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev) | |
629 | { | |
630 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
631 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
632 | ||
633 | return nfit_mem->dcr; | |
634 | } | |
635 | ||
636 | static ssize_t handle_show(struct device *dev, | |
637 | struct device_attribute *attr, char *buf) | |
638 | { | |
639 | struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev); | |
640 | ||
641 | return sprintf(buf, "%#x\n", memdev->device_handle); | |
642 | } | |
643 | static DEVICE_ATTR_RO(handle); | |
644 | ||
645 | static ssize_t phys_id_show(struct device *dev, | |
646 | struct device_attribute *attr, char *buf) | |
647 | { | |
648 | struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev); | |
649 | ||
650 | return sprintf(buf, "%#x\n", memdev->physical_id); | |
651 | } | |
652 | static DEVICE_ATTR_RO(phys_id); | |
653 | ||
654 | static ssize_t vendor_show(struct device *dev, | |
655 | struct device_attribute *attr, char *buf) | |
656 | { | |
657 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
658 | ||
659 | return sprintf(buf, "%#x\n", dcr->vendor_id); | |
660 | } | |
661 | static DEVICE_ATTR_RO(vendor); | |
662 | ||
663 | static ssize_t rev_id_show(struct device *dev, | |
664 | struct device_attribute *attr, char *buf) | |
665 | { | |
666 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
667 | ||
668 | return sprintf(buf, "%#x\n", dcr->revision_id); | |
669 | } | |
670 | static DEVICE_ATTR_RO(rev_id); | |
671 | ||
672 | static ssize_t device_show(struct device *dev, | |
673 | struct device_attribute *attr, char *buf) | |
674 | { | |
675 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
676 | ||
677 | return sprintf(buf, "%#x\n", dcr->device_id); | |
678 | } | |
679 | static DEVICE_ATTR_RO(device); | |
680 | ||
681 | static ssize_t format_show(struct device *dev, | |
682 | struct device_attribute *attr, char *buf) | |
683 | { | |
684 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
685 | ||
686 | return sprintf(buf, "%#x\n", dcr->code); | |
687 | } | |
688 | static DEVICE_ATTR_RO(format); | |
689 | ||
690 | static ssize_t serial_show(struct device *dev, | |
691 | struct device_attribute *attr, char *buf) | |
692 | { | |
693 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
694 | ||
695 | return sprintf(buf, "%#x\n", dcr->serial_number); | |
696 | } | |
697 | static DEVICE_ATTR_RO(serial); | |
698 | ||
58138820 DW |
699 | static ssize_t flags_show(struct device *dev, |
700 | struct device_attribute *attr, char *buf) | |
701 | { | |
702 | u16 flags = to_nfit_memdev(dev)->flags; | |
703 | ||
704 | return sprintf(buf, "%s%s%s%s%s\n", | |
705 | flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save " : "", | |
706 | flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore " : "", | |
707 | flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush " : "", | |
708 | flags & ACPI_NFIT_MEM_ARMED ? "arm " : "", | |
709 | flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart " : ""); | |
710 | } | |
711 | static DEVICE_ATTR_RO(flags); | |
712 | ||
e6dfb2de DW |
713 | static struct attribute *acpi_nfit_dimm_attributes[] = { |
714 | &dev_attr_handle.attr, | |
715 | &dev_attr_phys_id.attr, | |
716 | &dev_attr_vendor.attr, | |
717 | &dev_attr_device.attr, | |
718 | &dev_attr_format.attr, | |
719 | &dev_attr_serial.attr, | |
720 | &dev_attr_rev_id.attr, | |
58138820 | 721 | &dev_attr_flags.attr, |
e6dfb2de DW |
722 | NULL, |
723 | }; | |
724 | ||
725 | static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj, | |
726 | struct attribute *a, int n) | |
727 | { | |
728 | struct device *dev = container_of(kobj, struct device, kobj); | |
729 | ||
730 | if (to_nfit_dcr(dev)) | |
731 | return a->mode; | |
732 | else | |
733 | return 0; | |
734 | } | |
735 | ||
736 | static struct attribute_group acpi_nfit_dimm_attribute_group = { | |
737 | .name = "nfit", | |
738 | .attrs = acpi_nfit_dimm_attributes, | |
739 | .is_visible = acpi_nfit_dimm_attr_visible, | |
740 | }; | |
741 | ||
742 | static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = { | |
62232e45 | 743 | &nvdimm_attribute_group, |
4d88a97a | 744 | &nd_device_attribute_group, |
e6dfb2de DW |
745 | &acpi_nfit_dimm_attribute_group, |
746 | NULL, | |
747 | }; | |
748 | ||
749 | static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc, | |
750 | u32 device_handle) | |
751 | { | |
752 | struct nfit_mem *nfit_mem; | |
753 | ||
754 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) | |
755 | if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle) | |
756 | return nfit_mem->nvdimm; | |
757 | ||
758 | return NULL; | |
759 | } | |
760 | ||
62232e45 DW |
761 | static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc, |
762 | struct nfit_mem *nfit_mem, u32 device_handle) | |
763 | { | |
764 | struct acpi_device *adev, *adev_dimm; | |
765 | struct device *dev = acpi_desc->dev; | |
766 | const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM); | |
767 | unsigned long long sta; | |
768 | int i, rc = -ENODEV; | |
769 | acpi_status status; | |
770 | ||
771 | nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en; | |
772 | adev = to_acpi_dev(acpi_desc); | |
773 | if (!adev) | |
774 | return 0; | |
775 | ||
776 | adev_dimm = acpi_find_child_device(adev, device_handle, false); | |
777 | nfit_mem->adev = adev_dimm; | |
778 | if (!adev_dimm) { | |
779 | dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n", | |
780 | device_handle); | |
4d88a97a | 781 | return force_enable_dimms ? 0 : -ENODEV; |
62232e45 DW |
782 | } |
783 | ||
784 | status = acpi_evaluate_integer(adev_dimm->handle, "_STA", NULL, &sta); | |
785 | if (status == AE_NOT_FOUND) { | |
786 | dev_dbg(dev, "%s missing _STA, assuming enabled...\n", | |
787 | dev_name(&adev_dimm->dev)); | |
788 | rc = 0; | |
789 | } else if (ACPI_FAILURE(status)) | |
790 | dev_err(dev, "%s failed to retrieve_STA, disabling...\n", | |
791 | dev_name(&adev_dimm->dev)); | |
792 | else if ((sta & ACPI_STA_DEVICE_ENABLED) == 0) | |
793 | dev_info(dev, "%s disabled by firmware\n", | |
794 | dev_name(&adev_dimm->dev)); | |
795 | else | |
796 | rc = 0; | |
797 | ||
798 | for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++) | |
799 | if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i)) | |
800 | set_bit(i, &nfit_mem->dsm_mask); | |
801 | ||
4d88a97a | 802 | return force_enable_dimms ? 0 : rc; |
62232e45 DW |
803 | } |
804 | ||
e6dfb2de DW |
805 | static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc) |
806 | { | |
807 | struct nfit_mem *nfit_mem; | |
4d88a97a | 808 | int dimm_count = 0; |
e6dfb2de DW |
809 | |
810 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) { | |
811 | struct nvdimm *nvdimm; | |
812 | unsigned long flags = 0; | |
813 | u32 device_handle; | |
58138820 | 814 | u16 mem_flags; |
62232e45 | 815 | int rc; |
e6dfb2de DW |
816 | |
817 | device_handle = __to_nfit_memdev(nfit_mem)->device_handle; | |
818 | nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle); | |
819 | if (nvdimm) { | |
820 | /* | |
821 | * If for some reason we find multiple DCRs the | |
822 | * first one wins | |
823 | */ | |
824 | dev_err(acpi_desc->dev, "duplicate DCR detected: %s\n", | |
825 | nvdimm_name(nvdimm)); | |
826 | continue; | |
827 | } | |
828 | ||
829 | if (nfit_mem->bdw && nfit_mem->memdev_pmem) | |
830 | flags |= NDD_ALIASING; | |
831 | ||
58138820 DW |
832 | mem_flags = __to_nfit_memdev(nfit_mem)->flags; |
833 | if (mem_flags & ACPI_NFIT_MEM_ARMED) | |
834 | flags |= NDD_UNARMED; | |
835 | ||
62232e45 DW |
836 | rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle); |
837 | if (rc) | |
838 | continue; | |
839 | ||
e6dfb2de | 840 | nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem, |
62232e45 DW |
841 | acpi_nfit_dimm_attribute_groups, |
842 | flags, &nfit_mem->dsm_mask); | |
e6dfb2de DW |
843 | if (!nvdimm) |
844 | return -ENOMEM; | |
845 | ||
846 | nfit_mem->nvdimm = nvdimm; | |
4d88a97a | 847 | dimm_count++; |
58138820 DW |
848 | |
849 | if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0) | |
850 | continue; | |
851 | ||
852 | dev_info(acpi_desc->dev, "%s: failed: %s%s%s%s\n", | |
853 | nvdimm_name(nvdimm), | |
854 | mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save " : "", | |
855 | mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore " : "", | |
856 | mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush " : "", | |
857 | mem_flags & ACPI_NFIT_MEM_ARMED ? "arm " : ""); | |
858 | ||
e6dfb2de DW |
859 | } |
860 | ||
4d88a97a | 861 | return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count); |
e6dfb2de DW |
862 | } |
863 | ||
62232e45 DW |
864 | static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc) |
865 | { | |
866 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
867 | const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS); | |
868 | struct acpi_device *adev; | |
869 | int i; | |
870 | ||
871 | adev = to_acpi_dev(acpi_desc); | |
872 | if (!adev) | |
873 | return; | |
874 | ||
875 | for (i = ND_CMD_ARS_CAP; i <= ND_CMD_ARS_STATUS; i++) | |
876 | if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i)) | |
877 | set_bit(i, &nd_desc->dsm_mask); | |
878 | } | |
879 | ||
1f7df6f8 DW |
880 | static ssize_t range_index_show(struct device *dev, |
881 | struct device_attribute *attr, char *buf) | |
882 | { | |
883 | struct nd_region *nd_region = to_nd_region(dev); | |
884 | struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region); | |
885 | ||
886 | return sprintf(buf, "%d\n", nfit_spa->spa->range_index); | |
887 | } | |
888 | static DEVICE_ATTR_RO(range_index); | |
889 | ||
890 | static struct attribute *acpi_nfit_region_attributes[] = { | |
891 | &dev_attr_range_index.attr, | |
892 | NULL, | |
893 | }; | |
894 | ||
895 | static struct attribute_group acpi_nfit_region_attribute_group = { | |
896 | .name = "nfit", | |
897 | .attrs = acpi_nfit_region_attributes, | |
898 | }; | |
899 | ||
900 | static const struct attribute_group *acpi_nfit_region_attribute_groups[] = { | |
901 | &nd_region_attribute_group, | |
902 | &nd_mapping_attribute_group, | |
3d88002e | 903 | &nd_device_attribute_group, |
74ae66c3 | 904 | &nd_numa_attribute_group, |
1f7df6f8 DW |
905 | &acpi_nfit_region_attribute_group, |
906 | NULL, | |
907 | }; | |
908 | ||
eaf96153 DW |
909 | /* enough info to uniquely specify an interleave set */ |
910 | struct nfit_set_info { | |
911 | struct nfit_set_info_map { | |
912 | u64 region_offset; | |
913 | u32 serial_number; | |
914 | u32 pad; | |
915 | } mapping[0]; | |
916 | }; | |
917 | ||
918 | static size_t sizeof_nfit_set_info(int num_mappings) | |
919 | { | |
920 | return sizeof(struct nfit_set_info) | |
921 | + num_mappings * sizeof(struct nfit_set_info_map); | |
922 | } | |
923 | ||
924 | static int cmp_map(const void *m0, const void *m1) | |
925 | { | |
926 | const struct nfit_set_info_map *map0 = m0; | |
927 | const struct nfit_set_info_map *map1 = m1; | |
928 | ||
929 | return memcmp(&map0->region_offset, &map1->region_offset, | |
930 | sizeof(u64)); | |
931 | } | |
932 | ||
933 | /* Retrieve the nth entry referencing this spa */ | |
934 | static struct acpi_nfit_memory_map *memdev_from_spa( | |
935 | struct acpi_nfit_desc *acpi_desc, u16 range_index, int n) | |
936 | { | |
937 | struct nfit_memdev *nfit_memdev; | |
938 | ||
939 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) | |
940 | if (nfit_memdev->memdev->range_index == range_index) | |
941 | if (n-- == 0) | |
942 | return nfit_memdev->memdev; | |
943 | return NULL; | |
944 | } | |
945 | ||
946 | static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, | |
947 | struct nd_region_desc *ndr_desc, | |
948 | struct acpi_nfit_system_address *spa) | |
949 | { | |
950 | int i, spa_type = nfit_spa_type(spa); | |
951 | struct device *dev = acpi_desc->dev; | |
952 | struct nd_interleave_set *nd_set; | |
953 | u16 nr = ndr_desc->num_mappings; | |
954 | struct nfit_set_info *info; | |
955 | ||
956 | if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE) | |
957 | /* pass */; | |
958 | else | |
959 | return 0; | |
960 | ||
961 | nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL); | |
962 | if (!nd_set) | |
963 | return -ENOMEM; | |
964 | ||
965 | info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL); | |
966 | if (!info) | |
967 | return -ENOMEM; | |
968 | for (i = 0; i < nr; i++) { | |
969 | struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i]; | |
970 | struct nfit_set_info_map *map = &info->mapping[i]; | |
971 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
972 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
973 | struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc, | |
974 | spa->range_index, i); | |
975 | ||
976 | if (!memdev || !nfit_mem->dcr) { | |
977 | dev_err(dev, "%s: failed to find DCR\n", __func__); | |
978 | return -ENODEV; | |
979 | } | |
980 | ||
981 | map->region_offset = memdev->region_offset; | |
982 | map->serial_number = nfit_mem->dcr->serial_number; | |
983 | } | |
984 | ||
985 | sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map), | |
986 | cmp_map, NULL); | |
987 | nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0); | |
988 | ndr_desc->nd_set = nd_set; | |
989 | devm_kfree(dev, info); | |
990 | ||
991 | return 0; | |
992 | } | |
993 | ||
047fc8a1 RZ |
994 | static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio) |
995 | { | |
996 | struct acpi_nfit_interleave *idt = mmio->idt; | |
997 | u32 sub_line_offset, line_index, line_offset; | |
998 | u64 line_no, table_skip_count, table_offset; | |
999 | ||
1000 | line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset); | |
1001 | table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index); | |
1002 | line_offset = idt->line_offset[line_index] | |
1003 | * mmio->line_size; | |
1004 | table_offset = table_skip_count * mmio->table_size; | |
1005 | ||
1006 | return mmio->base_offset + line_offset + table_offset + sub_line_offset; | |
1007 | } | |
1008 | ||
c2ad2954 RZ |
1009 | static void wmb_blk(struct nfit_blk *nfit_blk) |
1010 | { | |
1011 | ||
1012 | if (nfit_blk->nvdimm_flush) { | |
1013 | /* | |
1014 | * The first wmb() is needed to 'sfence' all previous writes | |
1015 | * such that they are architecturally visible for the platform | |
1016 | * buffer flush. Note that we've already arranged for pmem | |
1017 | * writes to avoid the cache via arch_memcpy_to_pmem(). The | |
1018 | * final wmb() ensures ordering for the NVDIMM flush write. | |
1019 | */ | |
1020 | wmb(); | |
1021 | writeq(1, nfit_blk->nvdimm_flush); | |
1022 | wmb(); | |
1023 | } else | |
1024 | wmb_pmem(); | |
1025 | } | |
1026 | ||
047fc8a1 RZ |
1027 | static u64 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw) |
1028 | { | |
1029 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR]; | |
1030 | u64 offset = nfit_blk->stat_offset + mmio->size * bw; | |
1031 | ||
1032 | if (mmio->num_lines) | |
1033 | offset = to_interleave_offset(offset, mmio); | |
1034 | ||
1035 | return readq(mmio->base + offset); | |
1036 | } | |
1037 | ||
1038 | static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw, | |
1039 | resource_size_t dpa, unsigned int len, unsigned int write) | |
1040 | { | |
1041 | u64 cmd, offset; | |
1042 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR]; | |
1043 | ||
1044 | enum { | |
1045 | BCW_OFFSET_MASK = (1ULL << 48)-1, | |
1046 | BCW_LEN_SHIFT = 48, | |
1047 | BCW_LEN_MASK = (1ULL << 8) - 1, | |
1048 | BCW_CMD_SHIFT = 56, | |
1049 | }; | |
1050 | ||
1051 | cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK; | |
1052 | len = len >> L1_CACHE_SHIFT; | |
1053 | cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT; | |
1054 | cmd |= ((u64) write) << BCW_CMD_SHIFT; | |
1055 | ||
1056 | offset = nfit_blk->cmd_offset + mmio->size * bw; | |
1057 | if (mmio->num_lines) | |
1058 | offset = to_interleave_offset(offset, mmio); | |
1059 | ||
1060 | writeq(cmd, mmio->base + offset); | |
c2ad2954 | 1061 | wmb_blk(nfit_blk); |
f0f2c072 RZ |
1062 | |
1063 | if (nfit_blk->dimm_flags & ND_BLK_DCR_LATCH) | |
1064 | readq(mmio->base + offset); | |
047fc8a1 RZ |
1065 | } |
1066 | ||
1067 | static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk, | |
1068 | resource_size_t dpa, void *iobuf, size_t len, int rw, | |
1069 | unsigned int lane) | |
1070 | { | |
1071 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW]; | |
1072 | unsigned int copied = 0; | |
1073 | u64 base_offset; | |
1074 | int rc; | |
1075 | ||
1076 | base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES | |
1077 | + lane * mmio->size; | |
047fc8a1 RZ |
1078 | write_blk_ctl(nfit_blk, lane, dpa, len, rw); |
1079 | while (len) { | |
1080 | unsigned int c; | |
1081 | u64 offset; | |
1082 | ||
1083 | if (mmio->num_lines) { | |
1084 | u32 line_offset; | |
1085 | ||
1086 | offset = to_interleave_offset(base_offset + copied, | |
1087 | mmio); | |
1088 | div_u64_rem(offset, mmio->line_size, &line_offset); | |
1089 | c = min_t(size_t, len, mmio->line_size - line_offset); | |
1090 | } else { | |
1091 | offset = base_offset + nfit_blk->bdw_offset; | |
1092 | c = len; | |
1093 | } | |
1094 | ||
1095 | if (rw) | |
c2ad2954 RZ |
1096 | memcpy_to_pmem(mmio->aperture + offset, |
1097 | iobuf + copied, c); | |
047fc8a1 | 1098 | else |
c2ad2954 RZ |
1099 | memcpy_from_pmem(iobuf + copied, |
1100 | mmio->aperture + offset, c); | |
047fc8a1 RZ |
1101 | |
1102 | copied += c; | |
1103 | len -= c; | |
1104 | } | |
c2ad2954 RZ |
1105 | |
1106 | if (rw) | |
1107 | wmb_blk(nfit_blk); | |
1108 | ||
047fc8a1 RZ |
1109 | rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0; |
1110 | return rc; | |
1111 | } | |
1112 | ||
1113 | static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr, | |
1114 | resource_size_t dpa, void *iobuf, u64 len, int rw) | |
1115 | { | |
1116 | struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr); | |
1117 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW]; | |
1118 | struct nd_region *nd_region = nfit_blk->nd_region; | |
1119 | unsigned int lane, copied = 0; | |
1120 | int rc = 0; | |
1121 | ||
1122 | lane = nd_region_acquire_lane(nd_region); | |
1123 | while (len) { | |
1124 | u64 c = min(len, mmio->size); | |
1125 | ||
1126 | rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied, | |
1127 | iobuf + copied, c, rw, lane); | |
1128 | if (rc) | |
1129 | break; | |
1130 | ||
1131 | copied += c; | |
1132 | len -= c; | |
1133 | } | |
1134 | nd_region_release_lane(nd_region, lane); | |
1135 | ||
1136 | return rc; | |
1137 | } | |
1138 | ||
1139 | static void nfit_spa_mapping_release(struct kref *kref) | |
1140 | { | |
1141 | struct nfit_spa_mapping *spa_map = to_spa_map(kref); | |
1142 | struct acpi_nfit_system_address *spa = spa_map->spa; | |
1143 | struct acpi_nfit_desc *acpi_desc = spa_map->acpi_desc; | |
1144 | ||
1145 | WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex)); | |
1146 | dev_dbg(acpi_desc->dev, "%s: SPA%d\n", __func__, spa->range_index); | |
1147 | iounmap(spa_map->iomem); | |
1148 | release_mem_region(spa->address, spa->length); | |
1149 | list_del(&spa_map->list); | |
1150 | kfree(spa_map); | |
1151 | } | |
1152 | ||
1153 | static struct nfit_spa_mapping *find_spa_mapping( | |
1154 | struct acpi_nfit_desc *acpi_desc, | |
1155 | struct acpi_nfit_system_address *spa) | |
1156 | { | |
1157 | struct nfit_spa_mapping *spa_map; | |
1158 | ||
1159 | WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex)); | |
1160 | list_for_each_entry(spa_map, &acpi_desc->spa_maps, list) | |
1161 | if (spa_map->spa == spa) | |
1162 | return spa_map; | |
1163 | ||
1164 | return NULL; | |
1165 | } | |
1166 | ||
1167 | static void nfit_spa_unmap(struct acpi_nfit_desc *acpi_desc, | |
1168 | struct acpi_nfit_system_address *spa) | |
1169 | { | |
1170 | struct nfit_spa_mapping *spa_map; | |
1171 | ||
1172 | mutex_lock(&acpi_desc->spa_map_mutex); | |
1173 | spa_map = find_spa_mapping(acpi_desc, spa); | |
1174 | ||
1175 | if (spa_map) | |
1176 | kref_put(&spa_map->kref, nfit_spa_mapping_release); | |
1177 | mutex_unlock(&acpi_desc->spa_map_mutex); | |
1178 | } | |
1179 | ||
1180 | static void __iomem *__nfit_spa_map(struct acpi_nfit_desc *acpi_desc, | |
c2ad2954 | 1181 | struct acpi_nfit_system_address *spa, enum spa_map_type type) |
047fc8a1 RZ |
1182 | { |
1183 | resource_size_t start = spa->address; | |
1184 | resource_size_t n = spa->length; | |
1185 | struct nfit_spa_mapping *spa_map; | |
1186 | struct resource *res; | |
1187 | ||
1188 | WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex)); | |
1189 | ||
1190 | spa_map = find_spa_mapping(acpi_desc, spa); | |
1191 | if (spa_map) { | |
1192 | kref_get(&spa_map->kref); | |
1193 | return spa_map->iomem; | |
1194 | } | |
1195 | ||
1196 | spa_map = kzalloc(sizeof(*spa_map), GFP_KERNEL); | |
1197 | if (!spa_map) | |
1198 | return NULL; | |
1199 | ||
1200 | INIT_LIST_HEAD(&spa_map->list); | |
1201 | spa_map->spa = spa; | |
1202 | kref_init(&spa_map->kref); | |
1203 | spa_map->acpi_desc = acpi_desc; | |
1204 | ||
1205 | res = request_mem_region(start, n, dev_name(acpi_desc->dev)); | |
1206 | if (!res) | |
1207 | goto err_mem; | |
1208 | ||
c2ad2954 RZ |
1209 | if (type == SPA_MAP_APERTURE) { |
1210 | /* | |
1211 | * TODO: memremap_pmem() support, but that requires cache | |
1212 | * flushing when the aperture is moved. | |
1213 | */ | |
1214 | spa_map->iomem = ioremap_wc(start, n); | |
1215 | } else | |
1216 | spa_map->iomem = ioremap_nocache(start, n); | |
1217 | ||
047fc8a1 RZ |
1218 | if (!spa_map->iomem) |
1219 | goto err_map; | |
1220 | ||
1221 | list_add_tail(&spa_map->list, &acpi_desc->spa_maps); | |
1222 | return spa_map->iomem; | |
1223 | ||
1224 | err_map: | |
1225 | release_mem_region(start, n); | |
1226 | err_mem: | |
1227 | kfree(spa_map); | |
1228 | return NULL; | |
1229 | } | |
1230 | ||
1231 | /** | |
1232 | * nfit_spa_map - interleave-aware managed-mappings of acpi_nfit_system_address ranges | |
1233 | * @nvdimm_bus: NFIT-bus that provided the spa table entry | |
1234 | * @nfit_spa: spa table to map | |
c2ad2954 | 1235 | * @type: aperture or control region |
047fc8a1 RZ |
1236 | * |
1237 | * In the case where block-data-window apertures and | |
1238 | * dimm-control-regions are interleaved they will end up sharing a | |
1239 | * single request_mem_region() + ioremap() for the address range. In | |
1240 | * the style of devm nfit_spa_map() mappings are automatically dropped | |
1241 | * when all region devices referencing the same mapping are disabled / | |
1242 | * unbound. | |
1243 | */ | |
1244 | static void __iomem *nfit_spa_map(struct acpi_nfit_desc *acpi_desc, | |
c2ad2954 | 1245 | struct acpi_nfit_system_address *spa, enum spa_map_type type) |
047fc8a1 RZ |
1246 | { |
1247 | void __iomem *iomem; | |
1248 | ||
1249 | mutex_lock(&acpi_desc->spa_map_mutex); | |
c2ad2954 | 1250 | iomem = __nfit_spa_map(acpi_desc, spa, type); |
047fc8a1 RZ |
1251 | mutex_unlock(&acpi_desc->spa_map_mutex); |
1252 | ||
1253 | return iomem; | |
1254 | } | |
1255 | ||
1256 | static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio, | |
1257 | struct acpi_nfit_interleave *idt, u16 interleave_ways) | |
1258 | { | |
1259 | if (idt) { | |
1260 | mmio->num_lines = idt->line_count; | |
1261 | mmio->line_size = idt->line_size; | |
1262 | if (interleave_ways == 0) | |
1263 | return -ENXIO; | |
1264 | mmio->table_size = mmio->num_lines * interleave_ways | |
1265 | * mmio->line_size; | |
1266 | } | |
1267 | ||
1268 | return 0; | |
1269 | } | |
1270 | ||
f0f2c072 RZ |
1271 | static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc, |
1272 | struct nvdimm *nvdimm, struct nfit_blk *nfit_blk) | |
1273 | { | |
1274 | struct nd_cmd_dimm_flags flags; | |
1275 | int rc; | |
1276 | ||
1277 | memset(&flags, 0, sizeof(flags)); | |
1278 | rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags, | |
1279 | sizeof(flags)); | |
1280 | ||
1281 | if (rc >= 0 && flags.status == 0) | |
1282 | nfit_blk->dimm_flags = flags.flags; | |
1283 | else if (rc == -ENOTTY) { | |
1284 | /* fall back to a conservative default */ | |
1285 | nfit_blk->dimm_flags = ND_BLK_DCR_LATCH; | |
1286 | rc = 0; | |
1287 | } else | |
1288 | rc = -ENXIO; | |
1289 | ||
1290 | return rc; | |
1291 | } | |
1292 | ||
047fc8a1 RZ |
1293 | static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus, |
1294 | struct device *dev) | |
1295 | { | |
1296 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); | |
1297 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); | |
1298 | struct nd_blk_region *ndbr = to_nd_blk_region(dev); | |
c2ad2954 | 1299 | struct nfit_flush *nfit_flush; |
047fc8a1 RZ |
1300 | struct nfit_blk_mmio *mmio; |
1301 | struct nfit_blk *nfit_blk; | |
1302 | struct nfit_mem *nfit_mem; | |
1303 | struct nvdimm *nvdimm; | |
1304 | int rc; | |
1305 | ||
1306 | nvdimm = nd_blk_region_to_dimm(ndbr); | |
1307 | nfit_mem = nvdimm_provider_data(nvdimm); | |
1308 | if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) { | |
1309 | dev_dbg(dev, "%s: missing%s%s%s\n", __func__, | |
1310 | nfit_mem ? "" : " nfit_mem", | |
193ccca4 DW |
1311 | (nfit_mem && nfit_mem->dcr) ? "" : " dcr", |
1312 | (nfit_mem && nfit_mem->bdw) ? "" : " bdw"); | |
047fc8a1 RZ |
1313 | return -ENXIO; |
1314 | } | |
1315 | ||
1316 | nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL); | |
1317 | if (!nfit_blk) | |
1318 | return -ENOMEM; | |
1319 | nd_blk_region_set_provider_data(ndbr, nfit_blk); | |
1320 | nfit_blk->nd_region = to_nd_region(dev); | |
1321 | ||
1322 | /* map block aperture memory */ | |
1323 | nfit_blk->bdw_offset = nfit_mem->bdw->offset; | |
1324 | mmio = &nfit_blk->mmio[BDW]; | |
c2ad2954 RZ |
1325 | mmio->base = nfit_spa_map(acpi_desc, nfit_mem->spa_bdw, |
1326 | SPA_MAP_APERTURE); | |
047fc8a1 RZ |
1327 | if (!mmio->base) { |
1328 | dev_dbg(dev, "%s: %s failed to map bdw\n", __func__, | |
1329 | nvdimm_name(nvdimm)); | |
1330 | return -ENOMEM; | |
1331 | } | |
1332 | mmio->size = nfit_mem->bdw->size; | |
1333 | mmio->base_offset = nfit_mem->memdev_bdw->region_offset; | |
1334 | mmio->idt = nfit_mem->idt_bdw; | |
1335 | mmio->spa = nfit_mem->spa_bdw; | |
1336 | rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw, | |
1337 | nfit_mem->memdev_bdw->interleave_ways); | |
1338 | if (rc) { | |
1339 | dev_dbg(dev, "%s: %s failed to init bdw interleave\n", | |
1340 | __func__, nvdimm_name(nvdimm)); | |
1341 | return rc; | |
1342 | } | |
1343 | ||
1344 | /* map block control memory */ | |
1345 | nfit_blk->cmd_offset = nfit_mem->dcr->command_offset; | |
1346 | nfit_blk->stat_offset = nfit_mem->dcr->status_offset; | |
1347 | mmio = &nfit_blk->mmio[DCR]; | |
c2ad2954 RZ |
1348 | mmio->base = nfit_spa_map(acpi_desc, nfit_mem->spa_dcr, |
1349 | SPA_MAP_CONTROL); | |
047fc8a1 RZ |
1350 | if (!mmio->base) { |
1351 | dev_dbg(dev, "%s: %s failed to map dcr\n", __func__, | |
1352 | nvdimm_name(nvdimm)); | |
1353 | return -ENOMEM; | |
1354 | } | |
1355 | mmio->size = nfit_mem->dcr->window_size; | |
1356 | mmio->base_offset = nfit_mem->memdev_dcr->region_offset; | |
1357 | mmio->idt = nfit_mem->idt_dcr; | |
1358 | mmio->spa = nfit_mem->spa_dcr; | |
1359 | rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr, | |
1360 | nfit_mem->memdev_dcr->interleave_ways); | |
1361 | if (rc) { | |
1362 | dev_dbg(dev, "%s: %s failed to init dcr interleave\n", | |
1363 | __func__, nvdimm_name(nvdimm)); | |
1364 | return rc; | |
1365 | } | |
1366 | ||
f0f2c072 RZ |
1367 | rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk); |
1368 | if (rc < 0) { | |
1369 | dev_dbg(dev, "%s: %s failed get DIMM flags\n", | |
1370 | __func__, nvdimm_name(nvdimm)); | |
1371 | return rc; | |
1372 | } | |
1373 | ||
c2ad2954 RZ |
1374 | nfit_flush = nfit_mem->nfit_flush; |
1375 | if (nfit_flush && nfit_flush->flush->hint_count != 0) { | |
1376 | nfit_blk->nvdimm_flush = devm_ioremap_nocache(dev, | |
1377 | nfit_flush->flush->hint_address[0], 8); | |
1378 | if (!nfit_blk->nvdimm_flush) | |
1379 | return -ENOMEM; | |
1380 | } | |
1381 | ||
1382 | if (!arch_has_pmem_api() && !nfit_blk->nvdimm_flush) | |
1383 | dev_warn(dev, "unable to guarantee persistence of writes\n"); | |
1384 | ||
047fc8a1 RZ |
1385 | if (mmio->line_size == 0) |
1386 | return 0; | |
1387 | ||
1388 | if ((u32) nfit_blk->cmd_offset % mmio->line_size | |
1389 | + 8 > mmio->line_size) { | |
1390 | dev_dbg(dev, "cmd_offset crosses interleave boundary\n"); | |
1391 | return -ENXIO; | |
1392 | } else if ((u32) nfit_blk->stat_offset % mmio->line_size | |
1393 | + 8 > mmio->line_size) { | |
1394 | dev_dbg(dev, "stat_offset crosses interleave boundary\n"); | |
1395 | return -ENXIO; | |
1396 | } | |
1397 | ||
1398 | return 0; | |
1399 | } | |
1400 | ||
1401 | static void acpi_nfit_blk_region_disable(struct nvdimm_bus *nvdimm_bus, | |
1402 | struct device *dev) | |
1403 | { | |
1404 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); | |
1405 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); | |
1406 | struct nd_blk_region *ndbr = to_nd_blk_region(dev); | |
1407 | struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr); | |
1408 | int i; | |
1409 | ||
1410 | if (!nfit_blk) | |
1411 | return; /* never enabled */ | |
1412 | ||
1413 | /* auto-free BLK spa mappings */ | |
1414 | for (i = 0; i < 2; i++) { | |
1415 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[i]; | |
1416 | ||
1417 | if (mmio->base) | |
1418 | nfit_spa_unmap(acpi_desc, mmio->spa); | |
1419 | } | |
1420 | nd_blk_region_set_provider_data(ndbr, NULL); | |
1421 | /* devm will free nfit_blk */ | |
1422 | } | |
1423 | ||
1f7df6f8 DW |
1424 | static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc, |
1425 | struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc, | |
1426 | struct acpi_nfit_memory_map *memdev, | |
1427 | struct acpi_nfit_system_address *spa) | |
1428 | { | |
1429 | struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, | |
1430 | memdev->device_handle); | |
047fc8a1 | 1431 | struct nd_blk_region_desc *ndbr_desc; |
1f7df6f8 DW |
1432 | struct nfit_mem *nfit_mem; |
1433 | int blk_valid = 0; | |
1434 | ||
1435 | if (!nvdimm) { | |
1436 | dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n", | |
1437 | spa->range_index, memdev->device_handle); | |
1438 | return -ENODEV; | |
1439 | } | |
1440 | ||
1441 | nd_mapping->nvdimm = nvdimm; | |
1442 | switch (nfit_spa_type(spa)) { | |
1443 | case NFIT_SPA_PM: | |
1444 | case NFIT_SPA_VOLATILE: | |
1445 | nd_mapping->start = memdev->address; | |
1446 | nd_mapping->size = memdev->region_size; | |
1447 | break; | |
1448 | case NFIT_SPA_DCR: | |
1449 | nfit_mem = nvdimm_provider_data(nvdimm); | |
1450 | if (!nfit_mem || !nfit_mem->bdw) { | |
1451 | dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n", | |
1452 | spa->range_index, nvdimm_name(nvdimm)); | |
1453 | } else { | |
1454 | nd_mapping->size = nfit_mem->bdw->capacity; | |
1455 | nd_mapping->start = nfit_mem->bdw->start_address; | |
5212e11f | 1456 | ndr_desc->num_lanes = nfit_mem->bdw->windows; |
1f7df6f8 DW |
1457 | blk_valid = 1; |
1458 | } | |
1459 | ||
1460 | ndr_desc->nd_mapping = nd_mapping; | |
1461 | ndr_desc->num_mappings = blk_valid; | |
047fc8a1 RZ |
1462 | ndbr_desc = to_blk_region_desc(ndr_desc); |
1463 | ndbr_desc->enable = acpi_nfit_blk_region_enable; | |
1464 | ndbr_desc->disable = acpi_nfit_blk_region_disable; | |
6bc75619 | 1465 | ndbr_desc->do_io = acpi_desc->blk_do_io; |
1f7df6f8 DW |
1466 | if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc)) |
1467 | return -ENOMEM; | |
1468 | break; | |
1469 | } | |
1470 | ||
1471 | return 0; | |
1472 | } | |
1473 | ||
1474 | static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc, | |
1475 | struct nfit_spa *nfit_spa) | |
1476 | { | |
1477 | static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS]; | |
1478 | struct acpi_nfit_system_address *spa = nfit_spa->spa; | |
047fc8a1 RZ |
1479 | struct nd_blk_region_desc ndbr_desc; |
1480 | struct nd_region_desc *ndr_desc; | |
1f7df6f8 | 1481 | struct nfit_memdev *nfit_memdev; |
1f7df6f8 DW |
1482 | struct nvdimm_bus *nvdimm_bus; |
1483 | struct resource res; | |
eaf96153 | 1484 | int count = 0, rc; |
1f7df6f8 DW |
1485 | |
1486 | if (spa->range_index == 0) { | |
1487 | dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n", | |
1488 | __func__); | |
1489 | return 0; | |
1490 | } | |
1491 | ||
1492 | memset(&res, 0, sizeof(res)); | |
1493 | memset(&nd_mappings, 0, sizeof(nd_mappings)); | |
047fc8a1 | 1494 | memset(&ndbr_desc, 0, sizeof(ndbr_desc)); |
1f7df6f8 DW |
1495 | res.start = spa->address; |
1496 | res.end = res.start + spa->length - 1; | |
047fc8a1 RZ |
1497 | ndr_desc = &ndbr_desc.ndr_desc; |
1498 | ndr_desc->res = &res; | |
1499 | ndr_desc->provider_data = nfit_spa; | |
1500 | ndr_desc->attr_groups = acpi_nfit_region_attribute_groups; | |
41d7a6d6 TK |
1501 | if (spa->flags & ACPI_NFIT_PROXIMITY_VALID) |
1502 | ndr_desc->numa_node = acpi_map_pxm_to_online_node( | |
1503 | spa->proximity_domain); | |
1504 | else | |
1505 | ndr_desc->numa_node = NUMA_NO_NODE; | |
1506 | ||
1f7df6f8 DW |
1507 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
1508 | struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev; | |
1509 | struct nd_mapping *nd_mapping; | |
1f7df6f8 DW |
1510 | |
1511 | if (memdev->range_index != spa->range_index) | |
1512 | continue; | |
1513 | if (count >= ND_MAX_MAPPINGS) { | |
1514 | dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n", | |
1515 | spa->range_index, ND_MAX_MAPPINGS); | |
1516 | return -ENXIO; | |
1517 | } | |
1518 | nd_mapping = &nd_mappings[count++]; | |
047fc8a1 | 1519 | rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc, |
1f7df6f8 DW |
1520 | memdev, spa); |
1521 | if (rc) | |
1522 | return rc; | |
1523 | } | |
1524 | ||
047fc8a1 RZ |
1525 | ndr_desc->nd_mapping = nd_mappings; |
1526 | ndr_desc->num_mappings = count; | |
1527 | rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa); | |
eaf96153 DW |
1528 | if (rc) |
1529 | return rc; | |
1530 | ||
1f7df6f8 DW |
1531 | nvdimm_bus = acpi_desc->nvdimm_bus; |
1532 | if (nfit_spa_type(spa) == NFIT_SPA_PM) { | |
047fc8a1 | 1533 | if (!nvdimm_pmem_region_create(nvdimm_bus, ndr_desc)) |
1f7df6f8 DW |
1534 | return -ENOMEM; |
1535 | } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) { | |
047fc8a1 | 1536 | if (!nvdimm_volatile_region_create(nvdimm_bus, ndr_desc)) |
1f7df6f8 DW |
1537 | return -ENOMEM; |
1538 | } | |
1539 | return 0; | |
1540 | } | |
1541 | ||
1542 | static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc) | |
1543 | { | |
1544 | struct nfit_spa *nfit_spa; | |
1545 | ||
1546 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { | |
1547 | int rc = acpi_nfit_register_region(acpi_desc, nfit_spa); | |
1548 | ||
1549 | if (rc) | |
1550 | return rc; | |
1551 | } | |
1552 | return 0; | |
1553 | } | |
1554 | ||
6bc75619 | 1555 | int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz) |
b94d5230 DW |
1556 | { |
1557 | struct device *dev = acpi_desc->dev; | |
1558 | const void *end; | |
1559 | u8 *data; | |
1f7df6f8 | 1560 | int rc; |
b94d5230 | 1561 | |
047fc8a1 | 1562 | INIT_LIST_HEAD(&acpi_desc->spa_maps); |
b94d5230 DW |
1563 | INIT_LIST_HEAD(&acpi_desc->spas); |
1564 | INIT_LIST_HEAD(&acpi_desc->dcrs); | |
1565 | INIT_LIST_HEAD(&acpi_desc->bdws); | |
047fc8a1 | 1566 | INIT_LIST_HEAD(&acpi_desc->idts); |
c2ad2954 | 1567 | INIT_LIST_HEAD(&acpi_desc->flushes); |
b94d5230 DW |
1568 | INIT_LIST_HEAD(&acpi_desc->memdevs); |
1569 | INIT_LIST_HEAD(&acpi_desc->dimms); | |
047fc8a1 | 1570 | mutex_init(&acpi_desc->spa_map_mutex); |
b94d5230 DW |
1571 | |
1572 | data = (u8 *) acpi_desc->nfit; | |
1573 | end = data + sz; | |
1574 | data += sizeof(struct acpi_table_nfit); | |
1575 | while (!IS_ERR_OR_NULL(data)) | |
1576 | data = add_table(acpi_desc, data, end); | |
1577 | ||
1578 | if (IS_ERR(data)) { | |
1579 | dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__, | |
1580 | PTR_ERR(data)); | |
1581 | return PTR_ERR(data); | |
1582 | } | |
1583 | ||
1584 | if (nfit_mem_init(acpi_desc) != 0) | |
1585 | return -ENOMEM; | |
1586 | ||
62232e45 DW |
1587 | acpi_nfit_init_dsms(acpi_desc); |
1588 | ||
1f7df6f8 DW |
1589 | rc = acpi_nfit_register_dimms(acpi_desc); |
1590 | if (rc) | |
1591 | return rc; | |
1592 | ||
1593 | return acpi_nfit_register_regions(acpi_desc); | |
b94d5230 | 1594 | } |
6bc75619 | 1595 | EXPORT_SYMBOL_GPL(acpi_nfit_init); |
b94d5230 DW |
1596 | |
1597 | static int acpi_nfit_add(struct acpi_device *adev) | |
1598 | { | |
1599 | struct nvdimm_bus_descriptor *nd_desc; | |
1600 | struct acpi_nfit_desc *acpi_desc; | |
1601 | struct device *dev = &adev->dev; | |
1602 | struct acpi_table_header *tbl; | |
1603 | acpi_status status = AE_OK; | |
1604 | acpi_size sz; | |
1605 | int rc; | |
1606 | ||
1607 | status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz); | |
1608 | if (ACPI_FAILURE(status)) { | |
1609 | dev_err(dev, "failed to find NFIT\n"); | |
1610 | return -ENXIO; | |
1611 | } | |
1612 | ||
1613 | acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL); | |
1614 | if (!acpi_desc) | |
1615 | return -ENOMEM; | |
1616 | ||
1617 | dev_set_drvdata(dev, acpi_desc); | |
1618 | acpi_desc->dev = dev; | |
1619 | acpi_desc->nfit = (struct acpi_table_nfit *) tbl; | |
6bc75619 | 1620 | acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io; |
b94d5230 DW |
1621 | nd_desc = &acpi_desc->nd_desc; |
1622 | nd_desc->provider_name = "ACPI.NFIT"; | |
1623 | nd_desc->ndctl = acpi_nfit_ctl; | |
45def22c | 1624 | nd_desc->attr_groups = acpi_nfit_attribute_groups; |
b94d5230 DW |
1625 | |
1626 | acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc); | |
1627 | if (!acpi_desc->nvdimm_bus) | |
1628 | return -ENXIO; | |
1629 | ||
1630 | rc = acpi_nfit_init(acpi_desc, sz); | |
1631 | if (rc) { | |
1632 | nvdimm_bus_unregister(acpi_desc->nvdimm_bus); | |
1633 | return rc; | |
1634 | } | |
1635 | return 0; | |
1636 | } | |
1637 | ||
1638 | static int acpi_nfit_remove(struct acpi_device *adev) | |
1639 | { | |
1640 | struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev); | |
1641 | ||
1642 | nvdimm_bus_unregister(acpi_desc->nvdimm_bus); | |
1643 | return 0; | |
1644 | } | |
1645 | ||
1646 | static const struct acpi_device_id acpi_nfit_ids[] = { | |
1647 | { "ACPI0012", 0 }, | |
1648 | { "", 0 }, | |
1649 | }; | |
1650 | MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids); | |
1651 | ||
1652 | static struct acpi_driver acpi_nfit_driver = { | |
1653 | .name = KBUILD_MODNAME, | |
1654 | .ids = acpi_nfit_ids, | |
1655 | .ops = { | |
1656 | .add = acpi_nfit_add, | |
1657 | .remove = acpi_nfit_remove, | |
1658 | }, | |
1659 | }; | |
1660 | ||
1661 | static __init int nfit_init(void) | |
1662 | { | |
1663 | BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40); | |
1664 | BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56); | |
1665 | BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48); | |
1666 | BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20); | |
1667 | BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9); | |
1668 | BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80); | |
1669 | BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40); | |
1670 | ||
1671 | acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]); | |
1672 | acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]); | |
1673 | acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]); | |
1674 | acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]); | |
1675 | acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]); | |
1676 | acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]); | |
1677 | acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]); | |
1678 | acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]); | |
1679 | acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]); | |
1680 | acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]); | |
1681 | ||
1682 | return acpi_bus_register_driver(&acpi_nfit_driver); | |
1683 | } | |
1684 | ||
1685 | static __exit void nfit_exit(void) | |
1686 | { | |
1687 | acpi_bus_unregister_driver(&acpi_nfit_driver); | |
1688 | } | |
1689 | ||
1690 | module_init(nfit_init); | |
1691 | module_exit(nfit_exit); | |
1692 | MODULE_LICENSE("GPL v2"); | |
1693 | MODULE_AUTHOR("Intel Corporation"); |