]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/acpi/nfit.c
libnvdimm, nfit: regions (block-data-window, persistent memory, volatile memory)
[mirror_ubuntu-focal-kernel.git] / drivers / acpi / nfit.c
CommitLineData
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>
62232e45 16#include <linux/ndctl.h>
b94d5230
DW
17#include <linux/list.h>
18#include <linux/acpi.h>
19#include "nfit.h"
20
4d88a97a
DW
21static bool force_enable_dimms;
22module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
23MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
24
b94d5230
DW
25static u8 nfit_uuid[NFIT_UUID_MAX][16];
26
27static const u8 *to_nfit_uuid(enum nfit_uuids id)
28{
29 return nfit_uuid[id];
30}
31
62232e45
DW
32static struct acpi_nfit_desc *to_acpi_nfit_desc(
33 struct nvdimm_bus_descriptor *nd_desc)
34{
35 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
36}
37
38static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
39{
40 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
41
42 /*
43 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
44 * acpi_device.
45 */
46 if (!nd_desc->provider_name
47 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
48 return NULL;
49
50 return to_acpi_device(acpi_desc->dev);
51}
52
b94d5230
DW
53static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
54 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
55 unsigned int buf_len)
56{
62232e45
DW
57 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
58 const struct nd_cmd_desc *desc = NULL;
59 union acpi_object in_obj, in_buf, *out_obj;
60 struct device *dev = acpi_desc->dev;
61 const char *cmd_name, *dimm_name;
62 unsigned long dsm_mask;
63 acpi_handle handle;
64 const u8 *uuid;
65 u32 offset;
66 int rc, i;
67
68 if (nvdimm) {
69 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
70 struct acpi_device *adev = nfit_mem->adev;
71
72 if (!adev)
73 return -ENOTTY;
74 dimm_name = dev_name(&adev->dev);
75 cmd_name = nvdimm_cmd_name(cmd);
76 dsm_mask = nfit_mem->dsm_mask;
77 desc = nd_cmd_dimm_desc(cmd);
78 uuid = to_nfit_uuid(NFIT_DEV_DIMM);
79 handle = adev->handle;
80 } else {
81 struct acpi_device *adev = to_acpi_dev(acpi_desc);
82
83 cmd_name = nvdimm_bus_cmd_name(cmd);
84 dsm_mask = nd_desc->dsm_mask;
85 desc = nd_cmd_bus_desc(cmd);
86 uuid = to_nfit_uuid(NFIT_DEV_BUS);
87 handle = adev->handle;
88 dimm_name = "bus";
89 }
90
91 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
92 return -ENOTTY;
93
94 if (!test_bit(cmd, &dsm_mask))
95 return -ENOTTY;
96
97 in_obj.type = ACPI_TYPE_PACKAGE;
98 in_obj.package.count = 1;
99 in_obj.package.elements = &in_buf;
100 in_buf.type = ACPI_TYPE_BUFFER;
101 in_buf.buffer.pointer = buf;
102 in_buf.buffer.length = 0;
103
104 /* libnvdimm has already validated the input envelope */
105 for (i = 0; i < desc->in_num; i++)
106 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
107 i, buf);
108
109 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
110 dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__,
111 dimm_name, cmd_name, in_buf.buffer.length);
112 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
113 4, in_buf.buffer.pointer, min_t(u32, 128,
114 in_buf.buffer.length), true);
115 }
116
117 out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj);
118 if (!out_obj) {
119 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
120 cmd_name);
121 return -EINVAL;
122 }
123
124 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
125 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
126 __func__, dimm_name, cmd_name, out_obj->type);
127 rc = -EINVAL;
128 goto out;
129 }
130
131 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
132 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
133 dimm_name, cmd_name, out_obj->buffer.length);
134 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
135 4, out_obj->buffer.pointer, min_t(u32, 128,
136 out_obj->buffer.length), true);
137 }
138
139 for (i = 0, offset = 0; i < desc->out_num; i++) {
140 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
141 (u32 *) out_obj->buffer.pointer);
142
143 if (offset + out_size > out_obj->buffer.length) {
144 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
145 __func__, dimm_name, cmd_name, i);
146 break;
147 }
148
149 if (in_buf.buffer.length + offset + out_size > buf_len) {
150 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
151 __func__, dimm_name, cmd_name, i);
152 rc = -ENXIO;
153 goto out;
154 }
155 memcpy(buf + in_buf.buffer.length + offset,
156 out_obj->buffer.pointer + offset, out_size);
157 offset += out_size;
158 }
159 if (offset + in_buf.buffer.length < buf_len) {
160 if (i >= 1) {
161 /*
162 * status valid, return the number of bytes left
163 * unfilled in the output buffer
164 */
165 rc = buf_len - offset - in_buf.buffer.length;
166 } else {
167 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
168 __func__, dimm_name, cmd_name, buf_len,
169 offset);
170 rc = -ENXIO;
171 }
172 } else
173 rc = 0;
174
175 out:
176 ACPI_FREE(out_obj);
177
178 return rc;
b94d5230
DW
179}
180
181static const char *spa_type_name(u16 type)
182{
183 static const char *to_name[] = {
184 [NFIT_SPA_VOLATILE] = "volatile",
185 [NFIT_SPA_PM] = "pmem",
186 [NFIT_SPA_DCR] = "dimm-control-region",
187 [NFIT_SPA_BDW] = "block-data-window",
188 [NFIT_SPA_VDISK] = "volatile-disk",
189 [NFIT_SPA_VCD] = "volatile-cd",
190 [NFIT_SPA_PDISK] = "persistent-disk",
191 [NFIT_SPA_PCD] = "persistent-cd",
192
193 };
194
195 if (type > NFIT_SPA_PCD)
196 return "unknown";
197
198 return to_name[type];
199}
200
201static int nfit_spa_type(struct acpi_nfit_system_address *spa)
202{
203 int i;
204
205 for (i = 0; i < NFIT_UUID_MAX; i++)
206 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
207 return i;
208 return -1;
209}
210
211static bool add_spa(struct acpi_nfit_desc *acpi_desc,
212 struct acpi_nfit_system_address *spa)
213{
214 struct device *dev = acpi_desc->dev;
215 struct nfit_spa *nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa),
216 GFP_KERNEL);
217
218 if (!nfit_spa)
219 return false;
220 INIT_LIST_HEAD(&nfit_spa->list);
221 nfit_spa->spa = spa;
222 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
223 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
224 spa->range_index,
225 spa_type_name(nfit_spa_type(spa)));
226 return true;
227}
228
229static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
230 struct acpi_nfit_memory_map *memdev)
231{
232 struct device *dev = acpi_desc->dev;
233 struct nfit_memdev *nfit_memdev = devm_kzalloc(dev,
234 sizeof(*nfit_memdev), GFP_KERNEL);
235
236 if (!nfit_memdev)
237 return false;
238 INIT_LIST_HEAD(&nfit_memdev->list);
239 nfit_memdev->memdev = memdev;
240 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
241 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
242 __func__, memdev->device_handle, memdev->range_index,
243 memdev->region_index);
244 return true;
245}
246
247static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
248 struct acpi_nfit_control_region *dcr)
249{
250 struct device *dev = acpi_desc->dev;
251 struct nfit_dcr *nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr),
252 GFP_KERNEL);
253
254 if (!nfit_dcr)
255 return false;
256 INIT_LIST_HEAD(&nfit_dcr->list);
257 nfit_dcr->dcr = dcr;
258 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
259 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
260 dcr->region_index, dcr->windows);
261 return true;
262}
263
264static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
265 struct acpi_nfit_data_region *bdw)
266{
267 struct device *dev = acpi_desc->dev;
268 struct nfit_bdw *nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw),
269 GFP_KERNEL);
270
271 if (!nfit_bdw)
272 return false;
273 INIT_LIST_HEAD(&nfit_bdw->list);
274 nfit_bdw->bdw = bdw;
275 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
276 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
277 bdw->region_index, bdw->windows);
278 return true;
279}
280
281static void *add_table(struct acpi_nfit_desc *acpi_desc, void *table,
282 const void *end)
283{
284 struct device *dev = acpi_desc->dev;
285 struct acpi_nfit_header *hdr;
286 void *err = ERR_PTR(-ENOMEM);
287
288 if (table >= end)
289 return NULL;
290
291 hdr = table;
292 switch (hdr->type) {
293 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
294 if (!add_spa(acpi_desc, table))
295 return err;
296 break;
297 case ACPI_NFIT_TYPE_MEMORY_MAP:
298 if (!add_memdev(acpi_desc, table))
299 return err;
300 break;
301 case ACPI_NFIT_TYPE_CONTROL_REGION:
302 if (!add_dcr(acpi_desc, table))
303 return err;
304 break;
305 case ACPI_NFIT_TYPE_DATA_REGION:
306 if (!add_bdw(acpi_desc, table))
307 return err;
308 break;
309 /* TODO */
310 case ACPI_NFIT_TYPE_INTERLEAVE:
311 dev_dbg(dev, "%s: idt\n", __func__);
312 break;
313 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
314 dev_dbg(dev, "%s: flush\n", __func__);
315 break;
316 case ACPI_NFIT_TYPE_SMBIOS:
317 dev_dbg(dev, "%s: smbios\n", __func__);
318 break;
319 default:
320 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
321 break;
322 }
323
324 return table + hdr->length;
325}
326
327static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
328 struct nfit_mem *nfit_mem)
329{
330 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
331 u16 dcr = nfit_mem->dcr->region_index;
332 struct nfit_spa *nfit_spa;
333
334 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
335 u16 range_index = nfit_spa->spa->range_index;
336 int type = nfit_spa_type(nfit_spa->spa);
337 struct nfit_memdev *nfit_memdev;
338
339 if (type != NFIT_SPA_BDW)
340 continue;
341
342 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
343 if (nfit_memdev->memdev->range_index != range_index)
344 continue;
345 if (nfit_memdev->memdev->device_handle != device_handle)
346 continue;
347 if (nfit_memdev->memdev->region_index != dcr)
348 continue;
349
350 nfit_mem->spa_bdw = nfit_spa->spa;
351 return;
352 }
353 }
354
355 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
356 nfit_mem->spa_dcr->range_index);
357 nfit_mem->bdw = NULL;
358}
359
360static int nfit_mem_add(struct acpi_nfit_desc *acpi_desc,
361 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
362{
363 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
364 struct nfit_dcr *nfit_dcr;
365 struct nfit_bdw *nfit_bdw;
366
367 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
368 if (nfit_dcr->dcr->region_index != dcr)
369 continue;
370 nfit_mem->dcr = nfit_dcr->dcr;
371 break;
372 }
373
374 if (!nfit_mem->dcr) {
375 dev_dbg(acpi_desc->dev, "SPA %d missing:%s%s\n",
376 spa->range_index, __to_nfit_memdev(nfit_mem)
377 ? "" : " MEMDEV", nfit_mem->dcr ? "" : " DCR");
378 return -ENODEV;
379 }
380
381 /*
382 * We've found enough to create an nvdimm, optionally
383 * find an associated BDW
384 */
385 list_add(&nfit_mem->list, &acpi_desc->dimms);
386
387 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
388 if (nfit_bdw->bdw->region_index != dcr)
389 continue;
390 nfit_mem->bdw = nfit_bdw->bdw;
391 break;
392 }
393
394 if (!nfit_mem->bdw)
395 return 0;
396
397 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
398 return 0;
399}
400
401static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
402 struct acpi_nfit_system_address *spa)
403{
404 struct nfit_mem *nfit_mem, *found;
405 struct nfit_memdev *nfit_memdev;
406 int type = nfit_spa_type(spa);
407 u16 dcr;
408
409 switch (type) {
410 case NFIT_SPA_DCR:
411 case NFIT_SPA_PM:
412 break;
413 default:
414 return 0;
415 }
416
417 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
418 int rc;
419
420 if (nfit_memdev->memdev->range_index != spa->range_index)
421 continue;
422 found = NULL;
423 dcr = nfit_memdev->memdev->region_index;
424 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
425 if (__to_nfit_memdev(nfit_mem)->region_index == dcr) {
426 found = nfit_mem;
427 break;
428 }
429
430 if (found)
431 nfit_mem = found;
432 else {
433 nfit_mem = devm_kzalloc(acpi_desc->dev,
434 sizeof(*nfit_mem), GFP_KERNEL);
435 if (!nfit_mem)
436 return -ENOMEM;
437 INIT_LIST_HEAD(&nfit_mem->list);
438 }
439
440 if (type == NFIT_SPA_DCR) {
441 /* multiple dimms may share a SPA when interleaved */
442 nfit_mem->spa_dcr = spa;
443 nfit_mem->memdev_dcr = nfit_memdev->memdev;
444 } else {
445 /*
446 * A single dimm may belong to multiple SPA-PM
447 * ranges, record at least one in addition to
448 * any SPA-DCR range.
449 */
450 nfit_mem->memdev_pmem = nfit_memdev->memdev;
451 }
452
453 if (found)
454 continue;
455
456 rc = nfit_mem_add(acpi_desc, nfit_mem, spa);
457 if (rc)
458 return rc;
459 }
460
461 return 0;
462}
463
464static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
465{
466 struct nfit_mem *a = container_of(_a, typeof(*a), list);
467 struct nfit_mem *b = container_of(_b, typeof(*b), list);
468 u32 handleA, handleB;
469
470 handleA = __to_nfit_memdev(a)->device_handle;
471 handleB = __to_nfit_memdev(b)->device_handle;
472 if (handleA < handleB)
473 return -1;
474 else if (handleA > handleB)
475 return 1;
476 return 0;
477}
478
479static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
480{
481 struct nfit_spa *nfit_spa;
482
483 /*
484 * For each SPA-DCR or SPA-PMEM address range find its
485 * corresponding MEMDEV(s). From each MEMDEV find the
486 * corresponding DCR. Then, if we're operating on a SPA-DCR,
487 * try to find a SPA-BDW and a corresponding BDW that references
488 * the DCR. Throw it all into an nfit_mem object. Note, that
489 * BDWs are optional.
490 */
491 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
492 int rc;
493
494 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
495 if (rc)
496 return rc;
497 }
498
499 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
500
501 return 0;
502}
503
45def22c
DW
504static ssize_t revision_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
507 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
508 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
509 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
510
511 return sprintf(buf, "%d\n", acpi_desc->nfit->header.revision);
512}
513static DEVICE_ATTR_RO(revision);
514
515static struct attribute *acpi_nfit_attributes[] = {
516 &dev_attr_revision.attr,
517 NULL,
518};
519
520static struct attribute_group acpi_nfit_attribute_group = {
521 .name = "nfit",
522 .attrs = acpi_nfit_attributes,
523};
524
525static const struct attribute_group *acpi_nfit_attribute_groups[] = {
526 &nvdimm_bus_attribute_group,
527 &acpi_nfit_attribute_group,
528 NULL,
529};
530
e6dfb2de
DW
531static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
532{
533 struct nvdimm *nvdimm = to_nvdimm(dev);
534 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
535
536 return __to_nfit_memdev(nfit_mem);
537}
538
539static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
540{
541 struct nvdimm *nvdimm = to_nvdimm(dev);
542 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
543
544 return nfit_mem->dcr;
545}
546
547static ssize_t handle_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
549{
550 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
551
552 return sprintf(buf, "%#x\n", memdev->device_handle);
553}
554static DEVICE_ATTR_RO(handle);
555
556static ssize_t phys_id_show(struct device *dev,
557 struct device_attribute *attr, char *buf)
558{
559 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
560
561 return sprintf(buf, "%#x\n", memdev->physical_id);
562}
563static DEVICE_ATTR_RO(phys_id);
564
565static ssize_t vendor_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
569
570 return sprintf(buf, "%#x\n", dcr->vendor_id);
571}
572static DEVICE_ATTR_RO(vendor);
573
574static ssize_t rev_id_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576{
577 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
578
579 return sprintf(buf, "%#x\n", dcr->revision_id);
580}
581static DEVICE_ATTR_RO(rev_id);
582
583static ssize_t device_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
586 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
587
588 return sprintf(buf, "%#x\n", dcr->device_id);
589}
590static DEVICE_ATTR_RO(device);
591
592static ssize_t format_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
594{
595 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
596
597 return sprintf(buf, "%#x\n", dcr->code);
598}
599static DEVICE_ATTR_RO(format);
600
601static ssize_t serial_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
603{
604 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
605
606 return sprintf(buf, "%#x\n", dcr->serial_number);
607}
608static DEVICE_ATTR_RO(serial);
609
610static struct attribute *acpi_nfit_dimm_attributes[] = {
611 &dev_attr_handle.attr,
612 &dev_attr_phys_id.attr,
613 &dev_attr_vendor.attr,
614 &dev_attr_device.attr,
615 &dev_attr_format.attr,
616 &dev_attr_serial.attr,
617 &dev_attr_rev_id.attr,
618 NULL,
619};
620
621static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
622 struct attribute *a, int n)
623{
624 struct device *dev = container_of(kobj, struct device, kobj);
625
626 if (to_nfit_dcr(dev))
627 return a->mode;
628 else
629 return 0;
630}
631
632static struct attribute_group acpi_nfit_dimm_attribute_group = {
633 .name = "nfit",
634 .attrs = acpi_nfit_dimm_attributes,
635 .is_visible = acpi_nfit_dimm_attr_visible,
636};
637
638static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
62232e45 639 &nvdimm_attribute_group,
4d88a97a 640 &nd_device_attribute_group,
e6dfb2de
DW
641 &acpi_nfit_dimm_attribute_group,
642 NULL,
643};
644
645static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
646 u32 device_handle)
647{
648 struct nfit_mem *nfit_mem;
649
650 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
651 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
652 return nfit_mem->nvdimm;
653
654 return NULL;
655}
656
62232e45
DW
657static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
658 struct nfit_mem *nfit_mem, u32 device_handle)
659{
660 struct acpi_device *adev, *adev_dimm;
661 struct device *dev = acpi_desc->dev;
662 const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM);
663 unsigned long long sta;
664 int i, rc = -ENODEV;
665 acpi_status status;
666
667 nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en;
668 adev = to_acpi_dev(acpi_desc);
669 if (!adev)
670 return 0;
671
672 adev_dimm = acpi_find_child_device(adev, device_handle, false);
673 nfit_mem->adev = adev_dimm;
674 if (!adev_dimm) {
675 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
676 device_handle);
4d88a97a 677 return force_enable_dimms ? 0 : -ENODEV;
62232e45
DW
678 }
679
680 status = acpi_evaluate_integer(adev_dimm->handle, "_STA", NULL, &sta);
681 if (status == AE_NOT_FOUND) {
682 dev_dbg(dev, "%s missing _STA, assuming enabled...\n",
683 dev_name(&adev_dimm->dev));
684 rc = 0;
685 } else if (ACPI_FAILURE(status))
686 dev_err(dev, "%s failed to retrieve_STA, disabling...\n",
687 dev_name(&adev_dimm->dev));
688 else if ((sta & ACPI_STA_DEVICE_ENABLED) == 0)
689 dev_info(dev, "%s disabled by firmware\n",
690 dev_name(&adev_dimm->dev));
691 else
692 rc = 0;
693
694 for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
695 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
696 set_bit(i, &nfit_mem->dsm_mask);
697
4d88a97a 698 return force_enable_dimms ? 0 : rc;
62232e45
DW
699}
700
e6dfb2de
DW
701static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
702{
703 struct nfit_mem *nfit_mem;
4d88a97a 704 int dimm_count = 0;
e6dfb2de
DW
705
706 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
707 struct nvdimm *nvdimm;
708 unsigned long flags = 0;
709 u32 device_handle;
62232e45 710 int rc;
e6dfb2de
DW
711
712 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
713 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
714 if (nvdimm) {
715 /*
716 * If for some reason we find multiple DCRs the
717 * first one wins
718 */
719 dev_err(acpi_desc->dev, "duplicate DCR detected: %s\n",
720 nvdimm_name(nvdimm));
721 continue;
722 }
723
724 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
725 flags |= NDD_ALIASING;
726
62232e45
DW
727 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
728 if (rc)
729 continue;
730
e6dfb2de 731 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
62232e45
DW
732 acpi_nfit_dimm_attribute_groups,
733 flags, &nfit_mem->dsm_mask);
e6dfb2de
DW
734 if (!nvdimm)
735 return -ENOMEM;
736
737 nfit_mem->nvdimm = nvdimm;
4d88a97a 738 dimm_count++;
e6dfb2de
DW
739 }
740
4d88a97a 741 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
e6dfb2de
DW
742}
743
62232e45
DW
744static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
745{
746 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
747 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
748 struct acpi_device *adev;
749 int i;
750
751 adev = to_acpi_dev(acpi_desc);
752 if (!adev)
753 return;
754
755 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_ARS_STATUS; i++)
756 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
757 set_bit(i, &nd_desc->dsm_mask);
758}
759
1f7df6f8
DW
760static ssize_t range_index_show(struct device *dev,
761 struct device_attribute *attr, char *buf)
762{
763 struct nd_region *nd_region = to_nd_region(dev);
764 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
765
766 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
767}
768static DEVICE_ATTR_RO(range_index);
769
770static struct attribute *acpi_nfit_region_attributes[] = {
771 &dev_attr_range_index.attr,
772 NULL,
773};
774
775static struct attribute_group acpi_nfit_region_attribute_group = {
776 .name = "nfit",
777 .attrs = acpi_nfit_region_attributes,
778};
779
780static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
781 &nd_region_attribute_group,
782 &nd_mapping_attribute_group,
783 &acpi_nfit_region_attribute_group,
784 NULL,
785};
786
787static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
788 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
789 struct acpi_nfit_memory_map *memdev,
790 struct acpi_nfit_system_address *spa)
791{
792 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
793 memdev->device_handle);
794 struct nfit_mem *nfit_mem;
795 int blk_valid = 0;
796
797 if (!nvdimm) {
798 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
799 spa->range_index, memdev->device_handle);
800 return -ENODEV;
801 }
802
803 nd_mapping->nvdimm = nvdimm;
804 switch (nfit_spa_type(spa)) {
805 case NFIT_SPA_PM:
806 case NFIT_SPA_VOLATILE:
807 nd_mapping->start = memdev->address;
808 nd_mapping->size = memdev->region_size;
809 break;
810 case NFIT_SPA_DCR:
811 nfit_mem = nvdimm_provider_data(nvdimm);
812 if (!nfit_mem || !nfit_mem->bdw) {
813 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
814 spa->range_index, nvdimm_name(nvdimm));
815 } else {
816 nd_mapping->size = nfit_mem->bdw->capacity;
817 nd_mapping->start = nfit_mem->bdw->start_address;
818 blk_valid = 1;
819 }
820
821 ndr_desc->nd_mapping = nd_mapping;
822 ndr_desc->num_mappings = blk_valid;
823 if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc))
824 return -ENOMEM;
825 break;
826 }
827
828 return 0;
829}
830
831static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
832 struct nfit_spa *nfit_spa)
833{
834 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
835 struct acpi_nfit_system_address *spa = nfit_spa->spa;
836 struct nfit_memdev *nfit_memdev;
837 struct nd_region_desc ndr_desc;
838 struct nvdimm_bus *nvdimm_bus;
839 struct resource res;
840 int count = 0;
841
842 if (spa->range_index == 0) {
843 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
844 __func__);
845 return 0;
846 }
847
848 memset(&res, 0, sizeof(res));
849 memset(&nd_mappings, 0, sizeof(nd_mappings));
850 memset(&ndr_desc, 0, sizeof(ndr_desc));
851 res.start = spa->address;
852 res.end = res.start + spa->length - 1;
853 ndr_desc.res = &res;
854 ndr_desc.provider_data = nfit_spa;
855 ndr_desc.attr_groups = acpi_nfit_region_attribute_groups;
856 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
857 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
858 struct nd_mapping *nd_mapping;
859 int rc;
860
861 if (memdev->range_index != spa->range_index)
862 continue;
863 if (count >= ND_MAX_MAPPINGS) {
864 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
865 spa->range_index, ND_MAX_MAPPINGS);
866 return -ENXIO;
867 }
868 nd_mapping = &nd_mappings[count++];
869 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, &ndr_desc,
870 memdev, spa);
871 if (rc)
872 return rc;
873 }
874
875 ndr_desc.nd_mapping = nd_mappings;
876 ndr_desc.num_mappings = count;
877 nvdimm_bus = acpi_desc->nvdimm_bus;
878 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
879 if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
880 return -ENOMEM;
881 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
882 if (!nvdimm_volatile_region_create(nvdimm_bus, &ndr_desc))
883 return -ENOMEM;
884 }
885 return 0;
886}
887
888static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
889{
890 struct nfit_spa *nfit_spa;
891
892 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
893 int rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
894
895 if (rc)
896 return rc;
897 }
898 return 0;
899}
900
b94d5230
DW
901static int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
902{
903 struct device *dev = acpi_desc->dev;
904 const void *end;
905 u8 *data;
1f7df6f8 906 int rc;
b94d5230
DW
907
908 INIT_LIST_HEAD(&acpi_desc->spas);
909 INIT_LIST_HEAD(&acpi_desc->dcrs);
910 INIT_LIST_HEAD(&acpi_desc->bdws);
911 INIT_LIST_HEAD(&acpi_desc->memdevs);
912 INIT_LIST_HEAD(&acpi_desc->dimms);
913
914 data = (u8 *) acpi_desc->nfit;
915 end = data + sz;
916 data += sizeof(struct acpi_table_nfit);
917 while (!IS_ERR_OR_NULL(data))
918 data = add_table(acpi_desc, data, end);
919
920 if (IS_ERR(data)) {
921 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
922 PTR_ERR(data));
923 return PTR_ERR(data);
924 }
925
926 if (nfit_mem_init(acpi_desc) != 0)
927 return -ENOMEM;
928
62232e45
DW
929 acpi_nfit_init_dsms(acpi_desc);
930
1f7df6f8
DW
931 rc = acpi_nfit_register_dimms(acpi_desc);
932 if (rc)
933 return rc;
934
935 return acpi_nfit_register_regions(acpi_desc);
b94d5230
DW
936}
937
938static int acpi_nfit_add(struct acpi_device *adev)
939{
940 struct nvdimm_bus_descriptor *nd_desc;
941 struct acpi_nfit_desc *acpi_desc;
942 struct device *dev = &adev->dev;
943 struct acpi_table_header *tbl;
944 acpi_status status = AE_OK;
945 acpi_size sz;
946 int rc;
947
948 status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
949 if (ACPI_FAILURE(status)) {
950 dev_err(dev, "failed to find NFIT\n");
951 return -ENXIO;
952 }
953
954 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
955 if (!acpi_desc)
956 return -ENOMEM;
957
958 dev_set_drvdata(dev, acpi_desc);
959 acpi_desc->dev = dev;
960 acpi_desc->nfit = (struct acpi_table_nfit *) tbl;
961 nd_desc = &acpi_desc->nd_desc;
962 nd_desc->provider_name = "ACPI.NFIT";
963 nd_desc->ndctl = acpi_nfit_ctl;
45def22c 964 nd_desc->attr_groups = acpi_nfit_attribute_groups;
b94d5230
DW
965
966 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
967 if (!acpi_desc->nvdimm_bus)
968 return -ENXIO;
969
970 rc = acpi_nfit_init(acpi_desc, sz);
971 if (rc) {
972 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
973 return rc;
974 }
975 return 0;
976}
977
978static int acpi_nfit_remove(struct acpi_device *adev)
979{
980 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
981
982 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
983 return 0;
984}
985
986static const struct acpi_device_id acpi_nfit_ids[] = {
987 { "ACPI0012", 0 },
988 { "", 0 },
989};
990MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
991
992static struct acpi_driver acpi_nfit_driver = {
993 .name = KBUILD_MODNAME,
994 .ids = acpi_nfit_ids,
995 .ops = {
996 .add = acpi_nfit_add,
997 .remove = acpi_nfit_remove,
998 },
999};
1000
1001static __init int nfit_init(void)
1002{
1003 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
1004 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
1005 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
1006 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
1007 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
1008 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
1009 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
1010
1011 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
1012 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
1013 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
1014 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
1015 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
1016 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
1017 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
1018 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
1019 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
1020 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
1021
1022 return acpi_bus_register_driver(&acpi_nfit_driver);
1023}
1024
1025static __exit void nfit_exit(void)
1026{
1027 acpi_bus_unregister_driver(&acpi_nfit_driver);
1028}
1029
1030module_init(nfit_init);
1031module_exit(nfit_exit);
1032MODULE_LICENSE("GPL v2");
1033MODULE_AUTHOR("Intel Corporation");