libnvdimm: support for legacy (non-aliasing) nvdimms
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / nfit.c
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>
16 #include <linux/ndctl.h>
17 #include <linux/list.h>
18 #include <linux/acpi.h>
19 #include "nfit.h"
20
21 static bool force_enable_dimms;
22 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
23 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
24
25 static u8 nfit_uuid[NFIT_UUID_MAX][16];
26
27 static const u8 *to_nfit_uuid(enum nfit_uuids id)
28 {
29         return nfit_uuid[id];
30 }
31
32 static 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
38 static 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
53 static 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 {
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;
179 }
180
181 static 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
201 static 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
211 static 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
229 static 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
247 static 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
264 static 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
281 static 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
327 static 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
360 static 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
401 static 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
464 static 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
479 static 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
504 static 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 }
513 static DEVICE_ATTR_RO(revision);
514
515 static struct attribute *acpi_nfit_attributes[] = {
516         &dev_attr_revision.attr,
517         NULL,
518 };
519
520 static struct attribute_group acpi_nfit_attribute_group = {
521         .name = "nfit",
522         .attrs = acpi_nfit_attributes,
523 };
524
525 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
526         &nvdimm_bus_attribute_group,
527         &acpi_nfit_attribute_group,
528         NULL,
529 };
530
531 static 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
539 static 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
547 static 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 }
554 static DEVICE_ATTR_RO(handle);
555
556 static 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 }
563 static DEVICE_ATTR_RO(phys_id);
564
565 static 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 }
572 static DEVICE_ATTR_RO(vendor);
573
574 static 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 }
581 static DEVICE_ATTR_RO(rev_id);
582
583 static 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 }
590 static DEVICE_ATTR_RO(device);
591
592 static 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 }
599 static DEVICE_ATTR_RO(format);
600
601 static 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 }
608 static DEVICE_ATTR_RO(serial);
609
610 static 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
621 static 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
632 static 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
638 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
639         &nvdimm_attribute_group,
640         &nd_device_attribute_group,
641         &acpi_nfit_dimm_attribute_group,
642         NULL,
643 };
644
645 static 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
657 static 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);
677                 return force_enable_dimms ? 0 : -ENODEV;
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
698         return force_enable_dimms ? 0 : rc;
699 }
700
701 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
702 {
703         struct nfit_mem *nfit_mem;
704         int dimm_count = 0;
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;
710                 int rc;
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
727                 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
728                 if (rc)
729                         continue;
730
731                 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
732                                 acpi_nfit_dimm_attribute_groups,
733                                 flags, &nfit_mem->dsm_mask);
734                 if (!nvdimm)
735                         return -ENOMEM;
736
737                 nfit_mem->nvdimm = nvdimm;
738                 dimm_count++;
739         }
740
741         return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
742 }
743
744 static 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
760 static 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 }
768 static DEVICE_ATTR_RO(range_index);
769
770 static struct attribute *acpi_nfit_region_attributes[] = {
771         &dev_attr_range_index.attr,
772         NULL,
773 };
774
775 static struct attribute_group acpi_nfit_region_attribute_group = {
776         .name = "nfit",
777         .attrs = acpi_nfit_region_attributes,
778 };
779
780 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
781         &nd_region_attribute_group,
782         &nd_mapping_attribute_group,
783         &nd_device_attribute_group,
784         &acpi_nfit_region_attribute_group,
785         NULL,
786 };
787
788 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
789                 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
790                 struct acpi_nfit_memory_map *memdev,
791                 struct acpi_nfit_system_address *spa)
792 {
793         struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
794                         memdev->device_handle);
795         struct nfit_mem *nfit_mem;
796         int blk_valid = 0;
797
798         if (!nvdimm) {
799                 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
800                                 spa->range_index, memdev->device_handle);
801                 return -ENODEV;
802         }
803
804         nd_mapping->nvdimm = nvdimm;
805         switch (nfit_spa_type(spa)) {
806         case NFIT_SPA_PM:
807         case NFIT_SPA_VOLATILE:
808                 nd_mapping->start = memdev->address;
809                 nd_mapping->size = memdev->region_size;
810                 break;
811         case NFIT_SPA_DCR:
812                 nfit_mem = nvdimm_provider_data(nvdimm);
813                 if (!nfit_mem || !nfit_mem->bdw) {
814                         dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
815                                         spa->range_index, nvdimm_name(nvdimm));
816                 } else {
817                         nd_mapping->size = nfit_mem->bdw->capacity;
818                         nd_mapping->start = nfit_mem->bdw->start_address;
819                         blk_valid = 1;
820                 }
821
822                 ndr_desc->nd_mapping = nd_mapping;
823                 ndr_desc->num_mappings = blk_valid;
824                 if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc))
825                         return -ENOMEM;
826                 break;
827         }
828
829         return 0;
830 }
831
832 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
833                 struct nfit_spa *nfit_spa)
834 {
835         static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
836         struct acpi_nfit_system_address *spa = nfit_spa->spa;
837         struct nfit_memdev *nfit_memdev;
838         struct nd_region_desc ndr_desc;
839         struct nvdimm_bus *nvdimm_bus;
840         struct resource res;
841         int count = 0;
842
843         if (spa->range_index == 0) {
844                 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
845                                 __func__);
846                 return 0;
847         }
848
849         memset(&res, 0, sizeof(res));
850         memset(&nd_mappings, 0, sizeof(nd_mappings));
851         memset(&ndr_desc, 0, sizeof(ndr_desc));
852         res.start = spa->address;
853         res.end = res.start + spa->length - 1;
854         ndr_desc.res = &res;
855         ndr_desc.provider_data = nfit_spa;
856         ndr_desc.attr_groups = acpi_nfit_region_attribute_groups;
857         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
858                 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
859                 struct nd_mapping *nd_mapping;
860                 int rc;
861
862                 if (memdev->range_index != spa->range_index)
863                         continue;
864                 if (count >= ND_MAX_MAPPINGS) {
865                         dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
866                                         spa->range_index, ND_MAX_MAPPINGS);
867                         return -ENXIO;
868                 }
869                 nd_mapping = &nd_mappings[count++];
870                 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, &ndr_desc,
871                                 memdev, spa);
872                 if (rc)
873                         return rc;
874         }
875
876         ndr_desc.nd_mapping = nd_mappings;
877         ndr_desc.num_mappings = count;
878         nvdimm_bus = acpi_desc->nvdimm_bus;
879         if (nfit_spa_type(spa) == NFIT_SPA_PM) {
880                 if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
881                         return -ENOMEM;
882         } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
883                 if (!nvdimm_volatile_region_create(nvdimm_bus, &ndr_desc))
884                         return -ENOMEM;
885         }
886         return 0;
887 }
888
889 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
890 {
891         struct nfit_spa *nfit_spa;
892
893         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
894                 int rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
895
896                 if (rc)
897                         return rc;
898         }
899         return 0;
900 }
901
902 static int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
903 {
904         struct device *dev = acpi_desc->dev;
905         const void *end;
906         u8 *data;
907         int rc;
908
909         INIT_LIST_HEAD(&acpi_desc->spas);
910         INIT_LIST_HEAD(&acpi_desc->dcrs);
911         INIT_LIST_HEAD(&acpi_desc->bdws);
912         INIT_LIST_HEAD(&acpi_desc->memdevs);
913         INIT_LIST_HEAD(&acpi_desc->dimms);
914
915         data = (u8 *) acpi_desc->nfit;
916         end = data + sz;
917         data += sizeof(struct acpi_table_nfit);
918         while (!IS_ERR_OR_NULL(data))
919                 data = add_table(acpi_desc, data, end);
920
921         if (IS_ERR(data)) {
922                 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
923                                 PTR_ERR(data));
924                 return PTR_ERR(data);
925         }
926
927         if (nfit_mem_init(acpi_desc) != 0)
928                 return -ENOMEM;
929
930         acpi_nfit_init_dsms(acpi_desc);
931
932         rc = acpi_nfit_register_dimms(acpi_desc);
933         if (rc)
934                 return rc;
935
936         return acpi_nfit_register_regions(acpi_desc);
937 }
938
939 static int acpi_nfit_add(struct acpi_device *adev)
940 {
941         struct nvdimm_bus_descriptor *nd_desc;
942         struct acpi_nfit_desc *acpi_desc;
943         struct device *dev = &adev->dev;
944         struct acpi_table_header *tbl;
945         acpi_status status = AE_OK;
946         acpi_size sz;
947         int rc;
948
949         status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
950         if (ACPI_FAILURE(status)) {
951                 dev_err(dev, "failed to find NFIT\n");
952                 return -ENXIO;
953         }
954
955         acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
956         if (!acpi_desc)
957                 return -ENOMEM;
958
959         dev_set_drvdata(dev, acpi_desc);
960         acpi_desc->dev = dev;
961         acpi_desc->nfit = (struct acpi_table_nfit *) tbl;
962         nd_desc = &acpi_desc->nd_desc;
963         nd_desc->provider_name = "ACPI.NFIT";
964         nd_desc->ndctl = acpi_nfit_ctl;
965         nd_desc->attr_groups = acpi_nfit_attribute_groups;
966
967         acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
968         if (!acpi_desc->nvdimm_bus)
969                 return -ENXIO;
970
971         rc = acpi_nfit_init(acpi_desc, sz);
972         if (rc) {
973                 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
974                 return rc;
975         }
976         return 0;
977 }
978
979 static int acpi_nfit_remove(struct acpi_device *adev)
980 {
981         struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
982
983         nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
984         return 0;
985 }
986
987 static const struct acpi_device_id acpi_nfit_ids[] = {
988         { "ACPI0012", 0 },
989         { "", 0 },
990 };
991 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
992
993 static struct acpi_driver acpi_nfit_driver = {
994         .name = KBUILD_MODNAME,
995         .ids = acpi_nfit_ids,
996         .ops = {
997                 .add = acpi_nfit_add,
998                 .remove = acpi_nfit_remove,
999         },
1000 };
1001
1002 static __init int nfit_init(void)
1003 {
1004         BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
1005         BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
1006         BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
1007         BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
1008         BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
1009         BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
1010         BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
1011
1012         acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
1013         acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
1014         acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
1015         acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
1016         acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
1017         acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
1018         acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
1019         acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
1020         acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
1021         acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
1022
1023         return acpi_bus_register_driver(&acpi_nfit_driver);
1024 }
1025
1026 static __exit void nfit_exit(void)
1027 {
1028         acpi_bus_unregister_driver(&acpi_nfit_driver);
1029 }
1030
1031 module_init(nfit_init);
1032 module_exit(nfit_exit);
1033 MODULE_LICENSE("GPL v2");
1034 MODULE_AUTHOR("Intel Corporation");