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