nfit: Don't check _STA on NVDIMM devices
[deliverable/linux.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>
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
31static bool force_enable_dimms;
32module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
33MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
34
b94d5230
DW
35static u8 nfit_uuid[NFIT_UUID_MAX][16];
36
6bc75619 37const u8 *to_nfit_uuid(enum nfit_uuids id)
b94d5230
DW
38{
39 return nfit_uuid[id];
40}
6bc75619 41EXPORT_SYMBOL(to_nfit_uuid);
b94d5230 42
62232e45
DW
43static 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
49static 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
64static 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
192static 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
212static 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
222static 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
240static 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
258static 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
275static 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
292static 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
309static 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
326static 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
373static 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
406static 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
479static 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
552static 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
567static 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
592static 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}
601static DEVICE_ATTR_RO(revision);
602
603static struct attribute *acpi_nfit_attributes[] = {
604 &dev_attr_revision.attr,
605 NULL,
606};
607
608static struct attribute_group acpi_nfit_attribute_group = {
609 .name = "nfit",
610 .attrs = acpi_nfit_attributes,
611};
612
6bc75619 613const struct attribute_group *acpi_nfit_attribute_groups[] = {
45def22c
DW
614 &nvdimm_bus_attribute_group,
615 &acpi_nfit_attribute_group,
616 NULL,
617};
6bc75619 618EXPORT_SYMBOL_GPL(acpi_nfit_attribute_groups);
45def22c 619
e6dfb2de
DW
620static 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
628static 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
636static 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}
643static DEVICE_ATTR_RO(handle);
644
645static 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}
652static DEVICE_ATTR_RO(phys_id);
653
654static 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}
661static DEVICE_ATTR_RO(vendor);
662
663static 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}
670static DEVICE_ATTR_RO(rev_id);
671
672static 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}
679static DEVICE_ATTR_RO(device);
680
681static 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}
688static DEVICE_ATTR_RO(format);
689
690static 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}
697static DEVICE_ATTR_RO(serial);
698
58138820
DW
699static 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}
711static DEVICE_ATTR_RO(flags);
712
e6dfb2de
DW
713static 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
725static 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
736static 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
742static 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
749static 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
761static 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);
60e95f43 767 int i;
62232e45
DW
768
769 nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en;
770 adev = to_acpi_dev(acpi_desc);
771 if (!adev)
772 return 0;
773
774 adev_dimm = acpi_find_child_device(adev, device_handle, false);
775 nfit_mem->adev = adev_dimm;
776 if (!adev_dimm) {
777 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
778 device_handle);
4d88a97a 779 return force_enable_dimms ? 0 : -ENODEV;
62232e45
DW
780 }
781
62232e45
DW
782 for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
783 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
784 set_bit(i, &nfit_mem->dsm_mask);
785
60e95f43 786 return 0;
62232e45
DW
787}
788
e6dfb2de
DW
789static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
790{
791 struct nfit_mem *nfit_mem;
4d88a97a 792 int dimm_count = 0;
e6dfb2de
DW
793
794 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
795 struct nvdimm *nvdimm;
796 unsigned long flags = 0;
797 u32 device_handle;
58138820 798 u16 mem_flags;
62232e45 799 int rc;
e6dfb2de
DW
800
801 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
802 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
803 if (nvdimm) {
804 /*
805 * If for some reason we find multiple DCRs the
806 * first one wins
807 */
808 dev_err(acpi_desc->dev, "duplicate DCR detected: %s\n",
809 nvdimm_name(nvdimm));
810 continue;
811 }
812
813 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
814 flags |= NDD_ALIASING;
815
58138820
DW
816 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
817 if (mem_flags & ACPI_NFIT_MEM_ARMED)
818 flags |= NDD_UNARMED;
819
62232e45
DW
820 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
821 if (rc)
822 continue;
823
e6dfb2de 824 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
62232e45
DW
825 acpi_nfit_dimm_attribute_groups,
826 flags, &nfit_mem->dsm_mask);
e6dfb2de
DW
827 if (!nvdimm)
828 return -ENOMEM;
829
830 nfit_mem->nvdimm = nvdimm;
4d88a97a 831 dimm_count++;
58138820
DW
832
833 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
834 continue;
835
836 dev_info(acpi_desc->dev, "%s: failed: %s%s%s%s\n",
837 nvdimm_name(nvdimm),
838 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save " : "",
839 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore " : "",
840 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush " : "",
841 mem_flags & ACPI_NFIT_MEM_ARMED ? "arm " : "");
842
e6dfb2de
DW
843 }
844
4d88a97a 845 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
e6dfb2de
DW
846}
847
62232e45
DW
848static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
849{
850 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
851 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
852 struct acpi_device *adev;
853 int i;
854
39c686b8 855 nd_desc->dsm_mask = acpi_desc->bus_dsm_force_en;
62232e45
DW
856 adev = to_acpi_dev(acpi_desc);
857 if (!adev)
858 return;
859
860 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_ARS_STATUS; i++)
861 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
862 set_bit(i, &nd_desc->dsm_mask);
863}
864
1f7df6f8
DW
865static ssize_t range_index_show(struct device *dev,
866 struct device_attribute *attr, char *buf)
867{
868 struct nd_region *nd_region = to_nd_region(dev);
869 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
870
871 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
872}
873static DEVICE_ATTR_RO(range_index);
874
875static struct attribute *acpi_nfit_region_attributes[] = {
876 &dev_attr_range_index.attr,
877 NULL,
878};
879
880static struct attribute_group acpi_nfit_region_attribute_group = {
881 .name = "nfit",
882 .attrs = acpi_nfit_region_attributes,
883};
884
885static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
886 &nd_region_attribute_group,
887 &nd_mapping_attribute_group,
3d88002e 888 &nd_device_attribute_group,
74ae66c3 889 &nd_numa_attribute_group,
1f7df6f8
DW
890 &acpi_nfit_region_attribute_group,
891 NULL,
892};
893
eaf96153
DW
894/* enough info to uniquely specify an interleave set */
895struct nfit_set_info {
896 struct nfit_set_info_map {
897 u64 region_offset;
898 u32 serial_number;
899 u32 pad;
900 } mapping[0];
901};
902
903static size_t sizeof_nfit_set_info(int num_mappings)
904{
905 return sizeof(struct nfit_set_info)
906 + num_mappings * sizeof(struct nfit_set_info_map);
907}
908
909static int cmp_map(const void *m0, const void *m1)
910{
911 const struct nfit_set_info_map *map0 = m0;
912 const struct nfit_set_info_map *map1 = m1;
913
914 return memcmp(&map0->region_offset, &map1->region_offset,
915 sizeof(u64));
916}
917
918/* Retrieve the nth entry referencing this spa */
919static struct acpi_nfit_memory_map *memdev_from_spa(
920 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
921{
922 struct nfit_memdev *nfit_memdev;
923
924 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
925 if (nfit_memdev->memdev->range_index == range_index)
926 if (n-- == 0)
927 return nfit_memdev->memdev;
928 return NULL;
929}
930
931static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
932 struct nd_region_desc *ndr_desc,
933 struct acpi_nfit_system_address *spa)
934{
935 int i, spa_type = nfit_spa_type(spa);
936 struct device *dev = acpi_desc->dev;
937 struct nd_interleave_set *nd_set;
938 u16 nr = ndr_desc->num_mappings;
939 struct nfit_set_info *info;
940
941 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
942 /* pass */;
943 else
944 return 0;
945
946 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
947 if (!nd_set)
948 return -ENOMEM;
949
950 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
951 if (!info)
952 return -ENOMEM;
953 for (i = 0; i < nr; i++) {
954 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
955 struct nfit_set_info_map *map = &info->mapping[i];
956 struct nvdimm *nvdimm = nd_mapping->nvdimm;
957 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
958 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
959 spa->range_index, i);
960
961 if (!memdev || !nfit_mem->dcr) {
962 dev_err(dev, "%s: failed to find DCR\n", __func__);
963 return -ENODEV;
964 }
965
966 map->region_offset = memdev->region_offset;
967 map->serial_number = nfit_mem->dcr->serial_number;
968 }
969
970 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
971 cmp_map, NULL);
972 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
973 ndr_desc->nd_set = nd_set;
974 devm_kfree(dev, info);
975
976 return 0;
977}
978
047fc8a1
RZ
979static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
980{
981 struct acpi_nfit_interleave *idt = mmio->idt;
982 u32 sub_line_offset, line_index, line_offset;
983 u64 line_no, table_skip_count, table_offset;
984
985 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
986 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
987 line_offset = idt->line_offset[line_index]
988 * mmio->line_size;
989 table_offset = table_skip_count * mmio->table_size;
990
991 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
992}
993
c2ad2954
RZ
994static void wmb_blk(struct nfit_blk *nfit_blk)
995{
996
997 if (nfit_blk->nvdimm_flush) {
998 /*
999 * The first wmb() is needed to 'sfence' all previous writes
1000 * such that they are architecturally visible for the platform
1001 * buffer flush. Note that we've already arranged for pmem
1002 * writes to avoid the cache via arch_memcpy_to_pmem(). The
1003 * final wmb() ensures ordering for the NVDIMM flush write.
1004 */
1005 wmb();
1006 writeq(1, nfit_blk->nvdimm_flush);
1007 wmb();
1008 } else
1009 wmb_pmem();
1010}
1011
047fc8a1
RZ
1012static u64 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1013{
1014 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1015 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1016
1017 if (mmio->num_lines)
1018 offset = to_interleave_offset(offset, mmio);
1019
1020 return readq(mmio->base + offset);
1021}
1022
1023static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1024 resource_size_t dpa, unsigned int len, unsigned int write)
1025{
1026 u64 cmd, offset;
1027 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1028
1029 enum {
1030 BCW_OFFSET_MASK = (1ULL << 48)-1,
1031 BCW_LEN_SHIFT = 48,
1032 BCW_LEN_MASK = (1ULL << 8) - 1,
1033 BCW_CMD_SHIFT = 56,
1034 };
1035
1036 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1037 len = len >> L1_CACHE_SHIFT;
1038 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1039 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1040
1041 offset = nfit_blk->cmd_offset + mmio->size * bw;
1042 if (mmio->num_lines)
1043 offset = to_interleave_offset(offset, mmio);
1044
1045 writeq(cmd, mmio->base + offset);
c2ad2954 1046 wmb_blk(nfit_blk);
f0f2c072
RZ
1047
1048 if (nfit_blk->dimm_flags & ND_BLK_DCR_LATCH)
1049 readq(mmio->base + offset);
047fc8a1
RZ
1050}
1051
1052static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1053 resource_size_t dpa, void *iobuf, size_t len, int rw,
1054 unsigned int lane)
1055{
1056 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1057 unsigned int copied = 0;
1058 u64 base_offset;
1059 int rc;
1060
1061 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1062 + lane * mmio->size;
047fc8a1
RZ
1063 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1064 while (len) {
1065 unsigned int c;
1066 u64 offset;
1067
1068 if (mmio->num_lines) {
1069 u32 line_offset;
1070
1071 offset = to_interleave_offset(base_offset + copied,
1072 mmio);
1073 div_u64_rem(offset, mmio->line_size, &line_offset);
1074 c = min_t(size_t, len, mmio->line_size - line_offset);
1075 } else {
1076 offset = base_offset + nfit_blk->bdw_offset;
1077 c = len;
1078 }
1079
1080 if (rw)
c2ad2954
RZ
1081 memcpy_to_pmem(mmio->aperture + offset,
1082 iobuf + copied, c);
047fc8a1 1083 else
c2ad2954
RZ
1084 memcpy_from_pmem(iobuf + copied,
1085 mmio->aperture + offset, c);
047fc8a1
RZ
1086
1087 copied += c;
1088 len -= c;
1089 }
c2ad2954
RZ
1090
1091 if (rw)
1092 wmb_blk(nfit_blk);
1093
047fc8a1
RZ
1094 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1095 return rc;
1096}
1097
1098static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1099 resource_size_t dpa, void *iobuf, u64 len, int rw)
1100{
1101 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1102 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1103 struct nd_region *nd_region = nfit_blk->nd_region;
1104 unsigned int lane, copied = 0;
1105 int rc = 0;
1106
1107 lane = nd_region_acquire_lane(nd_region);
1108 while (len) {
1109 u64 c = min(len, mmio->size);
1110
1111 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1112 iobuf + copied, c, rw, lane);
1113 if (rc)
1114 break;
1115
1116 copied += c;
1117 len -= c;
1118 }
1119 nd_region_release_lane(nd_region, lane);
1120
1121 return rc;
1122}
1123
1124static void nfit_spa_mapping_release(struct kref *kref)
1125{
1126 struct nfit_spa_mapping *spa_map = to_spa_map(kref);
1127 struct acpi_nfit_system_address *spa = spa_map->spa;
1128 struct acpi_nfit_desc *acpi_desc = spa_map->acpi_desc;
1129
1130 WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1131 dev_dbg(acpi_desc->dev, "%s: SPA%d\n", __func__, spa->range_index);
1132 iounmap(spa_map->iomem);
1133 release_mem_region(spa->address, spa->length);
1134 list_del(&spa_map->list);
1135 kfree(spa_map);
1136}
1137
1138static struct nfit_spa_mapping *find_spa_mapping(
1139 struct acpi_nfit_desc *acpi_desc,
1140 struct acpi_nfit_system_address *spa)
1141{
1142 struct nfit_spa_mapping *spa_map;
1143
1144 WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1145 list_for_each_entry(spa_map, &acpi_desc->spa_maps, list)
1146 if (spa_map->spa == spa)
1147 return spa_map;
1148
1149 return NULL;
1150}
1151
1152static void nfit_spa_unmap(struct acpi_nfit_desc *acpi_desc,
1153 struct acpi_nfit_system_address *spa)
1154{
1155 struct nfit_spa_mapping *spa_map;
1156
1157 mutex_lock(&acpi_desc->spa_map_mutex);
1158 spa_map = find_spa_mapping(acpi_desc, spa);
1159
1160 if (spa_map)
1161 kref_put(&spa_map->kref, nfit_spa_mapping_release);
1162 mutex_unlock(&acpi_desc->spa_map_mutex);
1163}
1164
1165static void __iomem *__nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
c2ad2954 1166 struct acpi_nfit_system_address *spa, enum spa_map_type type)
047fc8a1
RZ
1167{
1168 resource_size_t start = spa->address;
1169 resource_size_t n = spa->length;
1170 struct nfit_spa_mapping *spa_map;
1171 struct resource *res;
1172
1173 WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1174
1175 spa_map = find_spa_mapping(acpi_desc, spa);
1176 if (spa_map) {
1177 kref_get(&spa_map->kref);
1178 return spa_map->iomem;
1179 }
1180
1181 spa_map = kzalloc(sizeof(*spa_map), GFP_KERNEL);
1182 if (!spa_map)
1183 return NULL;
1184
1185 INIT_LIST_HEAD(&spa_map->list);
1186 spa_map->spa = spa;
1187 kref_init(&spa_map->kref);
1188 spa_map->acpi_desc = acpi_desc;
1189
1190 res = request_mem_region(start, n, dev_name(acpi_desc->dev));
1191 if (!res)
1192 goto err_mem;
1193
c2ad2954
RZ
1194 if (type == SPA_MAP_APERTURE) {
1195 /*
1196 * TODO: memremap_pmem() support, but that requires cache
1197 * flushing when the aperture is moved.
1198 */
1199 spa_map->iomem = ioremap_wc(start, n);
1200 } else
1201 spa_map->iomem = ioremap_nocache(start, n);
1202
047fc8a1
RZ
1203 if (!spa_map->iomem)
1204 goto err_map;
1205
1206 list_add_tail(&spa_map->list, &acpi_desc->spa_maps);
1207 return spa_map->iomem;
1208
1209 err_map:
1210 release_mem_region(start, n);
1211 err_mem:
1212 kfree(spa_map);
1213 return NULL;
1214}
1215
1216/**
1217 * nfit_spa_map - interleave-aware managed-mappings of acpi_nfit_system_address ranges
1218 * @nvdimm_bus: NFIT-bus that provided the spa table entry
1219 * @nfit_spa: spa table to map
c2ad2954 1220 * @type: aperture or control region
047fc8a1
RZ
1221 *
1222 * In the case where block-data-window apertures and
1223 * dimm-control-regions are interleaved they will end up sharing a
1224 * single request_mem_region() + ioremap() for the address range. In
1225 * the style of devm nfit_spa_map() mappings are automatically dropped
1226 * when all region devices referencing the same mapping are disabled /
1227 * unbound.
1228 */
1229static void __iomem *nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
c2ad2954 1230 struct acpi_nfit_system_address *spa, enum spa_map_type type)
047fc8a1
RZ
1231{
1232 void __iomem *iomem;
1233
1234 mutex_lock(&acpi_desc->spa_map_mutex);
c2ad2954 1235 iomem = __nfit_spa_map(acpi_desc, spa, type);
047fc8a1
RZ
1236 mutex_unlock(&acpi_desc->spa_map_mutex);
1237
1238 return iomem;
1239}
1240
1241static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1242 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1243{
1244 if (idt) {
1245 mmio->num_lines = idt->line_count;
1246 mmio->line_size = idt->line_size;
1247 if (interleave_ways == 0)
1248 return -ENXIO;
1249 mmio->table_size = mmio->num_lines * interleave_ways
1250 * mmio->line_size;
1251 }
1252
1253 return 0;
1254}
1255
f0f2c072
RZ
1256static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1257 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1258{
1259 struct nd_cmd_dimm_flags flags;
1260 int rc;
1261
1262 memset(&flags, 0, sizeof(flags));
1263 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
1264 sizeof(flags));
1265
1266 if (rc >= 0 && flags.status == 0)
1267 nfit_blk->dimm_flags = flags.flags;
1268 else if (rc == -ENOTTY) {
1269 /* fall back to a conservative default */
1270 nfit_blk->dimm_flags = ND_BLK_DCR_LATCH;
1271 rc = 0;
1272 } else
1273 rc = -ENXIO;
1274
1275 return rc;
1276}
1277
047fc8a1
RZ
1278static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1279 struct device *dev)
1280{
1281 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1282 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1283 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
c2ad2954 1284 struct nfit_flush *nfit_flush;
047fc8a1
RZ
1285 struct nfit_blk_mmio *mmio;
1286 struct nfit_blk *nfit_blk;
1287 struct nfit_mem *nfit_mem;
1288 struct nvdimm *nvdimm;
1289 int rc;
1290
1291 nvdimm = nd_blk_region_to_dimm(ndbr);
1292 nfit_mem = nvdimm_provider_data(nvdimm);
1293 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1294 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1295 nfit_mem ? "" : " nfit_mem",
193ccca4
DW
1296 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1297 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
047fc8a1
RZ
1298 return -ENXIO;
1299 }
1300
1301 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1302 if (!nfit_blk)
1303 return -ENOMEM;
1304 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1305 nfit_blk->nd_region = to_nd_region(dev);
1306
1307 /* map block aperture memory */
1308 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1309 mmio = &nfit_blk->mmio[BDW];
c2ad2954
RZ
1310 mmio->base = nfit_spa_map(acpi_desc, nfit_mem->spa_bdw,
1311 SPA_MAP_APERTURE);
047fc8a1
RZ
1312 if (!mmio->base) {
1313 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1314 nvdimm_name(nvdimm));
1315 return -ENOMEM;
1316 }
1317 mmio->size = nfit_mem->bdw->size;
1318 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1319 mmio->idt = nfit_mem->idt_bdw;
1320 mmio->spa = nfit_mem->spa_bdw;
1321 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1322 nfit_mem->memdev_bdw->interleave_ways);
1323 if (rc) {
1324 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1325 __func__, nvdimm_name(nvdimm));
1326 return rc;
1327 }
1328
1329 /* map block control memory */
1330 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1331 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1332 mmio = &nfit_blk->mmio[DCR];
c2ad2954
RZ
1333 mmio->base = nfit_spa_map(acpi_desc, nfit_mem->spa_dcr,
1334 SPA_MAP_CONTROL);
047fc8a1
RZ
1335 if (!mmio->base) {
1336 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1337 nvdimm_name(nvdimm));
1338 return -ENOMEM;
1339 }
1340 mmio->size = nfit_mem->dcr->window_size;
1341 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1342 mmio->idt = nfit_mem->idt_dcr;
1343 mmio->spa = nfit_mem->spa_dcr;
1344 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1345 nfit_mem->memdev_dcr->interleave_ways);
1346 if (rc) {
1347 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1348 __func__, nvdimm_name(nvdimm));
1349 return rc;
1350 }
1351
f0f2c072
RZ
1352 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1353 if (rc < 0) {
1354 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1355 __func__, nvdimm_name(nvdimm));
1356 return rc;
1357 }
1358
c2ad2954
RZ
1359 nfit_flush = nfit_mem->nfit_flush;
1360 if (nfit_flush && nfit_flush->flush->hint_count != 0) {
1361 nfit_blk->nvdimm_flush = devm_ioremap_nocache(dev,
1362 nfit_flush->flush->hint_address[0], 8);
1363 if (!nfit_blk->nvdimm_flush)
1364 return -ENOMEM;
1365 }
1366
1367 if (!arch_has_pmem_api() && !nfit_blk->nvdimm_flush)
1368 dev_warn(dev, "unable to guarantee persistence of writes\n");
1369
047fc8a1
RZ
1370 if (mmio->line_size == 0)
1371 return 0;
1372
1373 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1374 + 8 > mmio->line_size) {
1375 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1376 return -ENXIO;
1377 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1378 + 8 > mmio->line_size) {
1379 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1380 return -ENXIO;
1381 }
1382
1383 return 0;
1384}
1385
1386static void acpi_nfit_blk_region_disable(struct nvdimm_bus *nvdimm_bus,
1387 struct device *dev)
1388{
1389 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1390 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1391 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1392 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1393 int i;
1394
1395 if (!nfit_blk)
1396 return; /* never enabled */
1397
1398 /* auto-free BLK spa mappings */
1399 for (i = 0; i < 2; i++) {
1400 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[i];
1401
1402 if (mmio->base)
1403 nfit_spa_unmap(acpi_desc, mmio->spa);
1404 }
1405 nd_blk_region_set_provider_data(ndbr, NULL);
1406 /* devm will free nfit_blk */
1407}
1408
1f7df6f8
DW
1409static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
1410 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
1411 struct acpi_nfit_memory_map *memdev,
1412 struct acpi_nfit_system_address *spa)
1413{
1414 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
1415 memdev->device_handle);
047fc8a1 1416 struct nd_blk_region_desc *ndbr_desc;
1f7df6f8
DW
1417 struct nfit_mem *nfit_mem;
1418 int blk_valid = 0;
1419
1420 if (!nvdimm) {
1421 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
1422 spa->range_index, memdev->device_handle);
1423 return -ENODEV;
1424 }
1425
1426 nd_mapping->nvdimm = nvdimm;
1427 switch (nfit_spa_type(spa)) {
1428 case NFIT_SPA_PM:
1429 case NFIT_SPA_VOLATILE:
1430 nd_mapping->start = memdev->address;
1431 nd_mapping->size = memdev->region_size;
1432 break;
1433 case NFIT_SPA_DCR:
1434 nfit_mem = nvdimm_provider_data(nvdimm);
1435 if (!nfit_mem || !nfit_mem->bdw) {
1436 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
1437 spa->range_index, nvdimm_name(nvdimm));
1438 } else {
1439 nd_mapping->size = nfit_mem->bdw->capacity;
1440 nd_mapping->start = nfit_mem->bdw->start_address;
5212e11f 1441 ndr_desc->num_lanes = nfit_mem->bdw->windows;
1f7df6f8
DW
1442 blk_valid = 1;
1443 }
1444
1445 ndr_desc->nd_mapping = nd_mapping;
1446 ndr_desc->num_mappings = blk_valid;
047fc8a1
RZ
1447 ndbr_desc = to_blk_region_desc(ndr_desc);
1448 ndbr_desc->enable = acpi_nfit_blk_region_enable;
1449 ndbr_desc->disable = acpi_nfit_blk_region_disable;
6bc75619 1450 ndbr_desc->do_io = acpi_desc->blk_do_io;
1f7df6f8
DW
1451 if (!nvdimm_blk_region_create(acpi_desc->nvdimm_bus, ndr_desc))
1452 return -ENOMEM;
1453 break;
1454 }
1455
1456 return 0;
1457}
1458
1459static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
1460 struct nfit_spa *nfit_spa)
1461{
1462 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
1463 struct acpi_nfit_system_address *spa = nfit_spa->spa;
047fc8a1
RZ
1464 struct nd_blk_region_desc ndbr_desc;
1465 struct nd_region_desc *ndr_desc;
1f7df6f8 1466 struct nfit_memdev *nfit_memdev;
1f7df6f8
DW
1467 struct nvdimm_bus *nvdimm_bus;
1468 struct resource res;
eaf96153 1469 int count = 0, rc;
1f7df6f8
DW
1470
1471 if (spa->range_index == 0) {
1472 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
1473 __func__);
1474 return 0;
1475 }
1476
1477 memset(&res, 0, sizeof(res));
1478 memset(&nd_mappings, 0, sizeof(nd_mappings));
047fc8a1 1479 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
1f7df6f8
DW
1480 res.start = spa->address;
1481 res.end = res.start + spa->length - 1;
047fc8a1
RZ
1482 ndr_desc = &ndbr_desc.ndr_desc;
1483 ndr_desc->res = &res;
1484 ndr_desc->provider_data = nfit_spa;
1485 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
41d7a6d6
TK
1486 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
1487 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
1488 spa->proximity_domain);
1489 else
1490 ndr_desc->numa_node = NUMA_NO_NODE;
1491
1f7df6f8
DW
1492 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1493 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1494 struct nd_mapping *nd_mapping;
1f7df6f8
DW
1495
1496 if (memdev->range_index != spa->range_index)
1497 continue;
1498 if (count >= ND_MAX_MAPPINGS) {
1499 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
1500 spa->range_index, ND_MAX_MAPPINGS);
1501 return -ENXIO;
1502 }
1503 nd_mapping = &nd_mappings[count++];
047fc8a1 1504 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
1f7df6f8
DW
1505 memdev, spa);
1506 if (rc)
1507 return rc;
1508 }
1509
047fc8a1
RZ
1510 ndr_desc->nd_mapping = nd_mappings;
1511 ndr_desc->num_mappings = count;
1512 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
eaf96153
DW
1513 if (rc)
1514 return rc;
1515
1f7df6f8
DW
1516 nvdimm_bus = acpi_desc->nvdimm_bus;
1517 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
047fc8a1 1518 if (!nvdimm_pmem_region_create(nvdimm_bus, ndr_desc))
1f7df6f8
DW
1519 return -ENOMEM;
1520 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
047fc8a1 1521 if (!nvdimm_volatile_region_create(nvdimm_bus, ndr_desc))
1f7df6f8
DW
1522 return -ENOMEM;
1523 }
1524 return 0;
1525}
1526
1527static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
1528{
1529 struct nfit_spa *nfit_spa;
1530
1531 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1532 int rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
1533
1534 if (rc)
1535 return rc;
1536 }
1537 return 0;
1538}
1539
6bc75619 1540int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
b94d5230
DW
1541{
1542 struct device *dev = acpi_desc->dev;
1543 const void *end;
1544 u8 *data;
1f7df6f8 1545 int rc;
b94d5230 1546
047fc8a1 1547 INIT_LIST_HEAD(&acpi_desc->spa_maps);
b94d5230
DW
1548 INIT_LIST_HEAD(&acpi_desc->spas);
1549 INIT_LIST_HEAD(&acpi_desc->dcrs);
1550 INIT_LIST_HEAD(&acpi_desc->bdws);
047fc8a1 1551 INIT_LIST_HEAD(&acpi_desc->idts);
c2ad2954 1552 INIT_LIST_HEAD(&acpi_desc->flushes);
b94d5230
DW
1553 INIT_LIST_HEAD(&acpi_desc->memdevs);
1554 INIT_LIST_HEAD(&acpi_desc->dimms);
047fc8a1 1555 mutex_init(&acpi_desc->spa_map_mutex);
b94d5230
DW
1556
1557 data = (u8 *) acpi_desc->nfit;
1558 end = data + sz;
1559 data += sizeof(struct acpi_table_nfit);
1560 while (!IS_ERR_OR_NULL(data))
1561 data = add_table(acpi_desc, data, end);
1562
1563 if (IS_ERR(data)) {
1564 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
1565 PTR_ERR(data));
1566 return PTR_ERR(data);
1567 }
1568
1569 if (nfit_mem_init(acpi_desc) != 0)
1570 return -ENOMEM;
1571
62232e45
DW
1572 acpi_nfit_init_dsms(acpi_desc);
1573
1f7df6f8
DW
1574 rc = acpi_nfit_register_dimms(acpi_desc);
1575 if (rc)
1576 return rc;
1577
1578 return acpi_nfit_register_regions(acpi_desc);
b94d5230 1579}
6bc75619 1580EXPORT_SYMBOL_GPL(acpi_nfit_init);
b94d5230
DW
1581
1582static int acpi_nfit_add(struct acpi_device *adev)
1583{
1584 struct nvdimm_bus_descriptor *nd_desc;
1585 struct acpi_nfit_desc *acpi_desc;
1586 struct device *dev = &adev->dev;
1587 struct acpi_table_header *tbl;
1588 acpi_status status = AE_OK;
1589 acpi_size sz;
1590 int rc;
1591
1592 status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
1593 if (ACPI_FAILURE(status)) {
1594 dev_err(dev, "failed to find NFIT\n");
1595 return -ENXIO;
1596 }
1597
1598 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
1599 if (!acpi_desc)
1600 return -ENOMEM;
1601
1602 dev_set_drvdata(dev, acpi_desc);
1603 acpi_desc->dev = dev;
1604 acpi_desc->nfit = (struct acpi_table_nfit *) tbl;
6bc75619 1605 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
b94d5230
DW
1606 nd_desc = &acpi_desc->nd_desc;
1607 nd_desc->provider_name = "ACPI.NFIT";
1608 nd_desc->ndctl = acpi_nfit_ctl;
45def22c 1609 nd_desc->attr_groups = acpi_nfit_attribute_groups;
b94d5230
DW
1610
1611 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, nd_desc);
1612 if (!acpi_desc->nvdimm_bus)
1613 return -ENXIO;
1614
1615 rc = acpi_nfit_init(acpi_desc, sz);
1616 if (rc) {
1617 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1618 return rc;
1619 }
1620 return 0;
1621}
1622
1623static int acpi_nfit_remove(struct acpi_device *adev)
1624{
1625 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
1626
1627 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1628 return 0;
1629}
1630
1631static const struct acpi_device_id acpi_nfit_ids[] = {
1632 { "ACPI0012", 0 },
1633 { "", 0 },
1634};
1635MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
1636
1637static struct acpi_driver acpi_nfit_driver = {
1638 .name = KBUILD_MODNAME,
1639 .ids = acpi_nfit_ids,
1640 .ops = {
1641 .add = acpi_nfit_add,
1642 .remove = acpi_nfit_remove,
1643 },
1644};
1645
1646static __init int nfit_init(void)
1647{
1648 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
1649 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
1650 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
1651 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
1652 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
1653 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
1654 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
1655
1656 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
1657 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
1658 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
1659 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
1660 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
1661 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
1662 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
1663 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
1664 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
1665 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
1666
1667 return acpi_bus_register_driver(&acpi_nfit_driver);
1668}
1669
1670static __exit void nfit_exit(void)
1671{
1672 acpi_bus_unregister_driver(&acpi_nfit_driver);
1673}
1674
1675module_init(nfit_init);
1676module_exit(nfit_exit);
1677MODULE_LICENSE("GPL v2");
1678MODULE_AUTHOR("Intel Corporation");
This page took 0.101995 seconds and 5 git commands to generate.