blob: 6e831c4021ff38c0132cf278208f917a05557e79 [file] [log] [blame]
Dan Williams6bc75612015-06-17 17:23:32 -04001/*
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/platform_device.h>
15#include <linux/dma-mapping.h>
16#include <linux/libnvdimm.h>
17#include <linux/vmalloc.h>
18#include <linux/device.h>
19#include <linux/module.h>
Vishal Verma20985162015-10-27 16:58:27 -060020#include <linux/mutex.h>
Dan Williams6bc75612015-06-17 17:23:32 -040021#include <linux/ndctl.h>
22#include <linux/sizes.h>
Vishal Verma20985162015-10-27 16:58:27 -060023#include <linux/list.h>
Dan Williams6bc75612015-06-17 17:23:32 -040024#include <linux/slab.h>
25#include <nfit.h>
26#include <nd.h>
27#include "nfit_test.h"
28
29/*
30 * Generate an NFIT table to describe the following topology:
31 *
32 * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
33 *
34 * (a) (b) DIMM BLK-REGION
35 * +----------+--------------+----------+---------+
36 * +------+ | blk2.0 | pm0.0 | blk2.1 | pm1.0 | 0 region2
37 * | imc0 +--+- - - - - region0 - - - -+----------+ +
38 * +--+---+ | blk3.0 | pm0.0 | blk3.1 | pm1.0 | 1 region3
39 * | +----------+--------------v----------v v
40 * +--+---+ | |
41 * | cpu0 | region1
42 * +--+---+ | |
43 * | +-------------------------^----------^ ^
44 * +--+---+ | blk4.0 | pm1.0 | 2 region4
45 * | imc1 +--+-------------------------+----------+ +
46 * +------+ | blk5.0 | pm1.0 | 3 region5
47 * +-------------------------+----------+-+-------+
48 *
Vishal Verma20985162015-10-27 16:58:27 -060049 * +--+---+
50 * | cpu1 |
51 * +--+---+ (Hotplug DIMM)
52 * | +----------------------------------------------+
53 * +--+---+ | blk6.0/pm7.0 | 4 region6/7
54 * | imc0 +--+----------------------------------------------+
55 * +------+
56 *
57 *
Dan Williams6bc75612015-06-17 17:23:32 -040058 * *) In this layout we have four dimms and two memory controllers in one
59 * socket. Each unique interface (BLK or PMEM) to DPA space
60 * is identified by a region device with a dynamically assigned id.
61 *
62 * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
63 * A single PMEM namespace "pm0.0" is created using half of the
64 * REGION0 SPA-range. REGION0 spans dimm0 and dimm1. PMEM namespace
65 * allocate from from the bottom of a region. The unallocated
66 * portion of REGION0 aliases with REGION2 and REGION3. That
67 * unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
68 * "blk3.0") starting at the base of each DIMM to offset (a) in those
69 * DIMMs. "pm0.0", "blk2.0" and "blk3.0" are free-form readable
70 * names that can be assigned to a namespace.
71 *
72 * *) In the last portion of dimm0 and dimm1 we have an interleaved
73 * SPA range, REGION1, that spans those two dimms as well as dimm2
74 * and dimm3. Some of REGION1 allocated to a PMEM namespace named
75 * "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
76 * dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
77 * "blk5.0".
78 *
79 * *) The portion of dimm2 and dimm3 that do not participate in the
80 * REGION1 interleaved SPA range (i.e. the DPA address below offset
81 * (b) are also included in the "blk4.0" and "blk5.0" namespaces.
82 * Note, that BLK namespaces need not be contiguous in DPA-space, and
83 * can consume aliased capacity from multiple interleave sets.
84 *
85 * BUS1: Legacy NVDIMM (single contiguous range)
86 *
87 * region2
88 * +---------------------+
89 * |---------------------|
90 * || pm2.0 ||
91 * |---------------------|
92 * +---------------------+
93 *
94 * *) A NFIT-table may describe a simple system-physical-address range
95 * with no BLK aliasing. This type of region may optionally
96 * reference an NVDIMM.
97 */
98enum {
Vishal Verma20985162015-10-27 16:58:27 -060099 NUM_PM = 3,
100 NUM_DCR = 5,
Dan Williams6bc75612015-06-17 17:23:32 -0400101 NUM_BDW = NUM_DCR,
102 NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
103 NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
104 DIMM_SIZE = SZ_32M,
105 LABEL_SIZE = SZ_128K,
106 SPA0_SIZE = DIMM_SIZE,
107 SPA1_SIZE = DIMM_SIZE*2,
108 SPA2_SIZE = DIMM_SIZE,
109 BDW_SIZE = 64 << 8,
110 DCR_SIZE = 12,
111 NUM_NFITS = 2, /* permit testing multiple NFITs per system */
112};
113
114struct nfit_test_dcr {
115 __le64 bdw_addr;
116 __le32 bdw_status;
117 __u8 aperature[BDW_SIZE];
118};
119
120#define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
121 (((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
122 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
123
124static u32 handle[NUM_DCR] = {
125 [0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
126 [1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
127 [2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
128 [3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
Vishal Verma20985162015-10-27 16:58:27 -0600129 [4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
Dan Williams6bc75612015-06-17 17:23:32 -0400130};
131
132struct nfit_test {
133 struct acpi_nfit_desc acpi_desc;
134 struct platform_device pdev;
135 struct list_head resources;
136 void *nfit_buf;
137 dma_addr_t nfit_dma;
138 size_t nfit_size;
139 int num_dcr;
140 int num_pm;
141 void **dimm;
142 dma_addr_t *dimm_dma;
Dan Williams9d27a872015-07-10 14:07:03 -0400143 void **flush;
144 dma_addr_t *flush_dma;
Dan Williams6bc75612015-06-17 17:23:32 -0400145 void **label;
146 dma_addr_t *label_dma;
147 void **spa_set;
148 dma_addr_t *spa_set_dma;
149 struct nfit_test_dcr **dcr;
150 dma_addr_t *dcr_dma;
151 int (*alloc)(struct nfit_test *t);
152 void (*setup)(struct nfit_test *t);
Vishal Verma20985162015-10-27 16:58:27 -0600153 int setup_hotplug;
Dan Williams6bc75612015-06-17 17:23:32 -0400154};
155
156static struct nfit_test *to_nfit_test(struct device *dev)
157{
158 struct platform_device *pdev = to_platform_device(dev);
159
160 return container_of(pdev, struct nfit_test, pdev);
161}
162
Vishal Verma39c686b2015-07-09 13:25:36 -0600163static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
164 unsigned int buf_len)
165{
166 if (buf_len < sizeof(*nd_cmd))
167 return -EINVAL;
168
169 nd_cmd->status = 0;
170 nd_cmd->config_size = LABEL_SIZE;
171 nd_cmd->max_xfer = SZ_4K;
172
173 return 0;
174}
175
176static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
177 *nd_cmd, unsigned int buf_len, void *label)
178{
179 unsigned int len, offset = nd_cmd->in_offset;
180 int rc;
181
182 if (buf_len < sizeof(*nd_cmd))
183 return -EINVAL;
184 if (offset >= LABEL_SIZE)
185 return -EINVAL;
186 if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
187 return -EINVAL;
188
189 nd_cmd->status = 0;
190 len = min(nd_cmd->in_length, LABEL_SIZE - offset);
191 memcpy(nd_cmd->out_buf, label + offset, len);
192 rc = buf_len - sizeof(*nd_cmd) - len;
193
194 return rc;
195}
196
197static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
198 unsigned int buf_len, void *label)
199{
200 unsigned int len, offset = nd_cmd->in_offset;
201 u32 *status;
202 int rc;
203
204 if (buf_len < sizeof(*nd_cmd))
205 return -EINVAL;
206 if (offset >= LABEL_SIZE)
207 return -EINVAL;
208 if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
209 return -EINVAL;
210
211 status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
212 *status = 0;
213 len = min(nd_cmd->in_length, LABEL_SIZE - offset);
214 memcpy(label + offset, nd_cmd->in_buf, len);
215 rc = buf_len - sizeof(*nd_cmd) - (len + 4);
216
217 return rc;
218}
219
Dan Williams747ffe12016-02-19 15:21:14 -0800220#define NFIT_TEST_ARS_RECORDS 4
221
Vishal Verma39c686b2015-07-09 13:25:36 -0600222static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
223 unsigned int buf_len)
224{
225 if (buf_len < sizeof(*nd_cmd))
226 return -EINVAL;
227
Dan Williams747ffe12016-02-19 15:21:14 -0800228 nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
229 + NFIT_TEST_ARS_RECORDS * sizeof(struct nd_ars_record);
Vishal Verma39c686b2015-07-09 13:25:36 -0600230 nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
231
232 return 0;
233}
234
235static int nfit_test_cmd_ars_start(struct nd_cmd_ars_start *nd_cmd,
236 unsigned int buf_len)
237{
238 if (buf_len < sizeof(*nd_cmd))
239 return -EINVAL;
240
241 nd_cmd->status = 0;
242
243 return 0;
244}
245
246static int nfit_test_cmd_ars_status(struct nd_cmd_ars_status *nd_cmd,
247 unsigned int buf_len)
248{
249 if (buf_len < sizeof(*nd_cmd))
250 return -EINVAL;
251
Dan Williams747ffe12016-02-19 15:21:14 -0800252 nd_cmd->out_length = sizeof(struct nd_cmd_ars_status);
253 /* TODO: emit error records */
Vishal Verma39c686b2015-07-09 13:25:36 -0600254 nd_cmd->num_records = 0;
Dan Williamsd26f73f2015-12-30 15:01:19 -0800255 nd_cmd->address = 0;
256 nd_cmd->length = -1ULL;
Vishal Verma39c686b2015-07-09 13:25:36 -0600257 nd_cmd->status = 0;
258
259 return 0;
260}
261
Dan Williams6bc75612015-06-17 17:23:32 -0400262static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
263 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
264 unsigned int buf_len)
265{
266 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
267 struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
Vishal Verma39c686b2015-07-09 13:25:36 -0600268 int i, rc = 0;
Dan Williams6bc75612015-06-17 17:23:32 -0400269
Vishal Verma39c686b2015-07-09 13:25:36 -0600270 if (nvdimm) {
271 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
Dan Williams6bc75612015-06-17 17:23:32 -0400272
Vishal Verma39c686b2015-07-09 13:25:36 -0600273 if (!nfit_mem || !test_bit(cmd, &nfit_mem->dsm_mask))
274 return -ENOTTY;
275
276 /* lookup label space for the given dimm */
277 for (i = 0; i < ARRAY_SIZE(handle); i++)
278 if (__to_nfit_memdev(nfit_mem)->device_handle ==
279 handle[i])
280 break;
281 if (i >= ARRAY_SIZE(handle))
282 return -ENXIO;
283
284 switch (cmd) {
285 case ND_CMD_GET_CONFIG_SIZE:
286 rc = nfit_test_cmd_get_config_size(buf, buf_len);
Dan Williams6bc75612015-06-17 17:23:32 -0400287 break;
Vishal Verma39c686b2015-07-09 13:25:36 -0600288 case ND_CMD_GET_CONFIG_DATA:
289 rc = nfit_test_cmd_get_config_data(buf, buf_len,
290 t->label[i]);
291 break;
292 case ND_CMD_SET_CONFIG_DATA:
293 rc = nfit_test_cmd_set_config_data(buf, buf_len,
294 t->label[i]);
295 break;
296 default:
297 return -ENOTTY;
298 }
299 } else {
300 if (!nd_desc || !test_bit(cmd, &nd_desc->dsm_mask))
301 return -ENOTTY;
Dan Williams6bc75612015-06-17 17:23:32 -0400302
Vishal Verma39c686b2015-07-09 13:25:36 -0600303 switch (cmd) {
304 case ND_CMD_ARS_CAP:
305 rc = nfit_test_cmd_ars_cap(buf, buf_len);
306 break;
307 case ND_CMD_ARS_START:
308 rc = nfit_test_cmd_ars_start(buf, buf_len);
309 break;
310 case ND_CMD_ARS_STATUS:
311 rc = nfit_test_cmd_ars_status(buf, buf_len);
312 break;
313 default:
314 return -ENOTTY;
315 }
Dan Williams6bc75612015-06-17 17:23:32 -0400316 }
317
318 return rc;
319}
320
321static DEFINE_SPINLOCK(nfit_test_lock);
322static struct nfit_test *instances[NUM_NFITS];
323
324static void release_nfit_res(void *data)
325{
326 struct nfit_test_resource *nfit_res = data;
327 struct resource *res = nfit_res->res;
328
329 spin_lock(&nfit_test_lock);
330 list_del(&nfit_res->list);
331 spin_unlock(&nfit_test_lock);
332
333 if (is_vmalloc_addr(nfit_res->buf))
334 vfree(nfit_res->buf);
335 else
336 dma_free_coherent(nfit_res->dev, resource_size(res),
337 nfit_res->buf, res->start);
338 kfree(res);
339 kfree(nfit_res);
340}
341
342static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
343 void *buf)
344{
345 struct device *dev = &t->pdev.dev;
346 struct resource *res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
347 struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
348 GFP_KERNEL);
349 int rc;
350
351 if (!res || !buf || !nfit_res)
352 goto err;
353 rc = devm_add_action(dev, release_nfit_res, nfit_res);
354 if (rc)
355 goto err;
356 INIT_LIST_HEAD(&nfit_res->list);
357 memset(buf, 0, size);
358 nfit_res->dev = dev;
359 nfit_res->buf = buf;
360 nfit_res->res = res;
361 res->start = *dma;
362 res->end = *dma + size - 1;
363 res->name = "NFIT";
364 spin_lock(&nfit_test_lock);
365 list_add(&nfit_res->list, &t->resources);
366 spin_unlock(&nfit_test_lock);
367
368 return nfit_res->buf;
369 err:
370 if (buf && !is_vmalloc_addr(buf))
371 dma_free_coherent(dev, size, buf, *dma);
372 else if (buf)
373 vfree(buf);
374 kfree(res);
375 kfree(nfit_res);
376 return NULL;
377}
378
379static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
380{
381 void *buf = vmalloc(size);
382
383 *dma = (unsigned long) buf;
384 return __test_alloc(t, size, dma, buf);
385}
386
387static void *test_alloc_coherent(struct nfit_test *t, size_t size,
388 dma_addr_t *dma)
389{
390 struct device *dev = &t->pdev.dev;
391 void *buf = dma_alloc_coherent(dev, size, dma, GFP_KERNEL);
392
393 return __test_alloc(t, size, dma, buf);
394}
395
396static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
397{
398 int i;
399
400 for (i = 0; i < ARRAY_SIZE(instances); i++) {
401 struct nfit_test_resource *n, *nfit_res = NULL;
402 struct nfit_test *t = instances[i];
403
404 if (!t)
405 continue;
406 spin_lock(&nfit_test_lock);
407 list_for_each_entry(n, &t->resources, list) {
408 if (addr >= n->res->start && (addr < n->res->start
409 + resource_size(n->res))) {
410 nfit_res = n;
411 break;
412 } else if (addr >= (unsigned long) n->buf
413 && (addr < (unsigned long) n->buf
414 + resource_size(n->res))) {
415 nfit_res = n;
416 break;
417 }
418 }
419 spin_unlock(&nfit_test_lock);
420 if (nfit_res)
421 return nfit_res;
422 }
423
424 return NULL;
425}
426
427static int nfit_test0_alloc(struct nfit_test *t)
428{
Linda Knippers6b577c92015-11-20 19:05:49 -0500429 size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
Dan Williams6bc75612015-06-17 17:23:32 -0400430 + sizeof(struct acpi_nfit_memory_map) * NUM_MEM
431 + sizeof(struct acpi_nfit_control_region) * NUM_DCR
Dan Williams9d27a872015-07-10 14:07:03 -0400432 + sizeof(struct acpi_nfit_data_region) * NUM_BDW
433 + sizeof(struct acpi_nfit_flush_address) * NUM_DCR;
Dan Williams6bc75612015-06-17 17:23:32 -0400434 int i;
435
436 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
437 if (!t->nfit_buf)
438 return -ENOMEM;
439 t->nfit_size = nfit_size;
440
441 t->spa_set[0] = test_alloc_coherent(t, SPA0_SIZE, &t->spa_set_dma[0]);
442 if (!t->spa_set[0])
443 return -ENOMEM;
444
445 t->spa_set[1] = test_alloc_coherent(t, SPA1_SIZE, &t->spa_set_dma[1]);
446 if (!t->spa_set[1])
447 return -ENOMEM;
448
Vishal Verma20985162015-10-27 16:58:27 -0600449 t->spa_set[2] = test_alloc_coherent(t, SPA0_SIZE, &t->spa_set_dma[2]);
450 if (!t->spa_set[2])
451 return -ENOMEM;
452
Dan Williams6bc75612015-06-17 17:23:32 -0400453 for (i = 0; i < NUM_DCR; i++) {
454 t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
455 if (!t->dimm[i])
456 return -ENOMEM;
457
458 t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
459 if (!t->label[i])
460 return -ENOMEM;
461 sprintf(t->label[i], "label%d", i);
Dan Williams9d27a872015-07-10 14:07:03 -0400462
463 t->flush[i] = test_alloc(t, 8, &t->flush_dma[i]);
464 if (!t->flush[i])
465 return -ENOMEM;
Dan Williams6bc75612015-06-17 17:23:32 -0400466 }
467
468 for (i = 0; i < NUM_DCR; i++) {
469 t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
470 if (!t->dcr[i])
471 return -ENOMEM;
472 }
473
474 return 0;
475}
476
477static int nfit_test1_alloc(struct nfit_test *t)
478{
Linda Knippers6b577c92015-11-20 19:05:49 -0500479 size_t nfit_size = sizeof(struct acpi_nfit_system_address)
Dan Williams6bc75612015-06-17 17:23:32 -0400480 + sizeof(struct acpi_nfit_memory_map)
481 + sizeof(struct acpi_nfit_control_region);
482
483 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
484 if (!t->nfit_buf)
485 return -ENOMEM;
486 t->nfit_size = nfit_size;
487
488 t->spa_set[0] = test_alloc_coherent(t, SPA2_SIZE, &t->spa_set_dma[0]);
489 if (!t->spa_set[0])
490 return -ENOMEM;
491
492 return 0;
493}
494
Dan Williams6bc75612015-06-17 17:23:32 -0400495static void nfit_test0_setup(struct nfit_test *t)
496{
497 struct nvdimm_bus_descriptor *nd_desc;
498 struct acpi_nfit_desc *acpi_desc;
499 struct acpi_nfit_memory_map *memdev;
500 void *nfit_buf = t->nfit_buf;
Dan Williams6bc75612015-06-17 17:23:32 -0400501 struct acpi_nfit_system_address *spa;
502 struct acpi_nfit_control_region *dcr;
503 struct acpi_nfit_data_region *bdw;
Dan Williams9d27a872015-07-10 14:07:03 -0400504 struct acpi_nfit_flush_address *flush;
Dan Williams6bc75612015-06-17 17:23:32 -0400505 unsigned int offset;
506
Dan Williams6bc75612015-06-17 17:23:32 -0400507 /*
508 * spa0 (interleave first half of dimm0 and dimm1, note storage
509 * does not actually alias the related block-data-window
510 * regions)
511 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500512 spa = nfit_buf;
Dan Williams6bc75612015-06-17 17:23:32 -0400513 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
514 spa->header.length = sizeof(*spa);
515 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
516 spa->range_index = 0+1;
517 spa->address = t->spa_set_dma[0];
518 spa->length = SPA0_SIZE;
519
520 /*
521 * spa1 (interleave last half of the 4 DIMMS, note storage
522 * does not actually alias the related block-data-window
523 * regions)
524 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500525 spa = nfit_buf + sizeof(*spa);
Dan Williams6bc75612015-06-17 17:23:32 -0400526 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
527 spa->header.length = sizeof(*spa);
528 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
529 spa->range_index = 1+1;
530 spa->address = t->spa_set_dma[1];
531 spa->length = SPA1_SIZE;
532
533 /* spa2 (dcr0) dimm0 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500534 spa = nfit_buf + sizeof(*spa) * 2;
Dan Williams6bc75612015-06-17 17:23:32 -0400535 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
536 spa->header.length = sizeof(*spa);
537 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
538 spa->range_index = 2+1;
539 spa->address = t->dcr_dma[0];
540 spa->length = DCR_SIZE;
541
542 /* spa3 (dcr1) dimm1 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500543 spa = nfit_buf + sizeof(*spa) * 3;
Dan Williams6bc75612015-06-17 17:23:32 -0400544 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
545 spa->header.length = sizeof(*spa);
546 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
547 spa->range_index = 3+1;
548 spa->address = t->dcr_dma[1];
549 spa->length = DCR_SIZE;
550
551 /* spa4 (dcr2) dimm2 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500552 spa = nfit_buf + sizeof(*spa) * 4;
Dan Williams6bc75612015-06-17 17:23:32 -0400553 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
554 spa->header.length = sizeof(*spa);
555 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
556 spa->range_index = 4+1;
557 spa->address = t->dcr_dma[2];
558 spa->length = DCR_SIZE;
559
560 /* spa5 (dcr3) dimm3 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500561 spa = nfit_buf + sizeof(*spa) * 5;
Dan Williams6bc75612015-06-17 17:23:32 -0400562 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
563 spa->header.length = sizeof(*spa);
564 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
565 spa->range_index = 5+1;
566 spa->address = t->dcr_dma[3];
567 spa->length = DCR_SIZE;
568
569 /* spa6 (bdw for dcr0) dimm0 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500570 spa = nfit_buf + sizeof(*spa) * 6;
Dan Williams6bc75612015-06-17 17:23:32 -0400571 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
572 spa->header.length = sizeof(*spa);
573 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
574 spa->range_index = 6+1;
575 spa->address = t->dimm_dma[0];
576 spa->length = DIMM_SIZE;
577
578 /* spa7 (bdw for dcr1) dimm1 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500579 spa = nfit_buf + sizeof(*spa) * 7;
Dan Williams6bc75612015-06-17 17:23:32 -0400580 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
581 spa->header.length = sizeof(*spa);
582 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
583 spa->range_index = 7+1;
584 spa->address = t->dimm_dma[1];
585 spa->length = DIMM_SIZE;
586
587 /* spa8 (bdw for dcr2) dimm2 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500588 spa = nfit_buf + sizeof(*spa) * 8;
Dan Williams6bc75612015-06-17 17:23:32 -0400589 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
590 spa->header.length = sizeof(*spa);
591 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
592 spa->range_index = 8+1;
593 spa->address = t->dimm_dma[2];
594 spa->length = DIMM_SIZE;
595
596 /* spa9 (bdw for dcr3) dimm3 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500597 spa = nfit_buf + sizeof(*spa) * 9;
Dan Williams6bc75612015-06-17 17:23:32 -0400598 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
599 spa->header.length = sizeof(*spa);
600 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
601 spa->range_index = 9+1;
602 spa->address = t->dimm_dma[3];
603 spa->length = DIMM_SIZE;
604
Linda Knippers6b577c92015-11-20 19:05:49 -0500605 offset = sizeof(*spa) * 10;
Dan Williams6bc75612015-06-17 17:23:32 -0400606 /* mem-region0 (spa0, dimm0) */
607 memdev = nfit_buf + offset;
608 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
609 memdev->header.length = sizeof(*memdev);
610 memdev->device_handle = handle[0];
611 memdev->physical_id = 0;
612 memdev->region_id = 0;
613 memdev->range_index = 0+1;
614 memdev->region_index = 0+1;
615 memdev->region_size = SPA0_SIZE/2;
616 memdev->region_offset = t->spa_set_dma[0];
617 memdev->address = 0;
618 memdev->interleave_index = 0;
619 memdev->interleave_ways = 2;
620
621 /* mem-region1 (spa0, dimm1) */
622 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
623 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
624 memdev->header.length = sizeof(*memdev);
625 memdev->device_handle = handle[1];
626 memdev->physical_id = 1;
627 memdev->region_id = 0;
628 memdev->range_index = 0+1;
629 memdev->region_index = 1+1;
630 memdev->region_size = SPA0_SIZE/2;
631 memdev->region_offset = t->spa_set_dma[0] + SPA0_SIZE/2;
632 memdev->address = 0;
633 memdev->interleave_index = 0;
634 memdev->interleave_ways = 2;
635
636 /* mem-region2 (spa1, dimm0) */
637 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
638 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
639 memdev->header.length = sizeof(*memdev);
640 memdev->device_handle = handle[0];
641 memdev->physical_id = 0;
642 memdev->region_id = 1;
643 memdev->range_index = 1+1;
644 memdev->region_index = 0+1;
645 memdev->region_size = SPA1_SIZE/4;
646 memdev->region_offset = t->spa_set_dma[1];
647 memdev->address = SPA0_SIZE/2;
648 memdev->interleave_index = 0;
649 memdev->interleave_ways = 4;
650
651 /* mem-region3 (spa1, dimm1) */
652 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
653 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
654 memdev->header.length = sizeof(*memdev);
655 memdev->device_handle = handle[1];
656 memdev->physical_id = 1;
657 memdev->region_id = 1;
658 memdev->range_index = 1+1;
659 memdev->region_index = 1+1;
660 memdev->region_size = SPA1_SIZE/4;
661 memdev->region_offset = t->spa_set_dma[1] + SPA1_SIZE/4;
662 memdev->address = SPA0_SIZE/2;
663 memdev->interleave_index = 0;
664 memdev->interleave_ways = 4;
665
666 /* mem-region4 (spa1, dimm2) */
667 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
668 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
669 memdev->header.length = sizeof(*memdev);
670 memdev->device_handle = handle[2];
671 memdev->physical_id = 2;
672 memdev->region_id = 0;
673 memdev->range_index = 1+1;
674 memdev->region_index = 2+1;
675 memdev->region_size = SPA1_SIZE/4;
676 memdev->region_offset = t->spa_set_dma[1] + 2*SPA1_SIZE/4;
677 memdev->address = SPA0_SIZE/2;
678 memdev->interleave_index = 0;
679 memdev->interleave_ways = 4;
680
681 /* mem-region5 (spa1, dimm3) */
682 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
683 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
684 memdev->header.length = sizeof(*memdev);
685 memdev->device_handle = handle[3];
686 memdev->physical_id = 3;
687 memdev->region_id = 0;
688 memdev->range_index = 1+1;
689 memdev->region_index = 3+1;
690 memdev->region_size = SPA1_SIZE/4;
691 memdev->region_offset = t->spa_set_dma[1] + 3*SPA1_SIZE/4;
692 memdev->address = SPA0_SIZE/2;
693 memdev->interleave_index = 0;
694 memdev->interleave_ways = 4;
695
696 /* mem-region6 (spa/dcr0, dimm0) */
697 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
698 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
699 memdev->header.length = sizeof(*memdev);
700 memdev->device_handle = handle[0];
701 memdev->physical_id = 0;
702 memdev->region_id = 0;
703 memdev->range_index = 2+1;
704 memdev->region_index = 0+1;
705 memdev->region_size = 0;
706 memdev->region_offset = 0;
707 memdev->address = 0;
708 memdev->interleave_index = 0;
709 memdev->interleave_ways = 1;
710
711 /* mem-region7 (spa/dcr1, dimm1) */
712 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
713 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
714 memdev->header.length = sizeof(*memdev);
715 memdev->device_handle = handle[1];
716 memdev->physical_id = 1;
717 memdev->region_id = 0;
718 memdev->range_index = 3+1;
719 memdev->region_index = 1+1;
720 memdev->region_size = 0;
721 memdev->region_offset = 0;
722 memdev->address = 0;
723 memdev->interleave_index = 0;
724 memdev->interleave_ways = 1;
725
726 /* mem-region8 (spa/dcr2, dimm2) */
727 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
728 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
729 memdev->header.length = sizeof(*memdev);
730 memdev->device_handle = handle[2];
731 memdev->physical_id = 2;
732 memdev->region_id = 0;
733 memdev->range_index = 4+1;
734 memdev->region_index = 2+1;
735 memdev->region_size = 0;
736 memdev->region_offset = 0;
737 memdev->address = 0;
738 memdev->interleave_index = 0;
739 memdev->interleave_ways = 1;
740
741 /* mem-region9 (spa/dcr3, dimm3) */
742 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
743 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
744 memdev->header.length = sizeof(*memdev);
745 memdev->device_handle = handle[3];
746 memdev->physical_id = 3;
747 memdev->region_id = 0;
748 memdev->range_index = 5+1;
749 memdev->region_index = 3+1;
750 memdev->region_size = 0;
751 memdev->region_offset = 0;
752 memdev->address = 0;
753 memdev->interleave_index = 0;
754 memdev->interleave_ways = 1;
755
756 /* mem-region10 (spa/bdw0, dimm0) */
757 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
758 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
759 memdev->header.length = sizeof(*memdev);
760 memdev->device_handle = handle[0];
761 memdev->physical_id = 0;
762 memdev->region_id = 0;
763 memdev->range_index = 6+1;
764 memdev->region_index = 0+1;
765 memdev->region_size = 0;
766 memdev->region_offset = 0;
767 memdev->address = 0;
768 memdev->interleave_index = 0;
769 memdev->interleave_ways = 1;
770
771 /* mem-region11 (spa/bdw1, dimm1) */
772 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
773 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
774 memdev->header.length = sizeof(*memdev);
775 memdev->device_handle = handle[1];
776 memdev->physical_id = 1;
777 memdev->region_id = 0;
778 memdev->range_index = 7+1;
779 memdev->region_index = 1+1;
780 memdev->region_size = 0;
781 memdev->region_offset = 0;
782 memdev->address = 0;
783 memdev->interleave_index = 0;
784 memdev->interleave_ways = 1;
785
786 /* mem-region12 (spa/bdw2, dimm2) */
787 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
788 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
789 memdev->header.length = sizeof(*memdev);
790 memdev->device_handle = handle[2];
791 memdev->physical_id = 2;
792 memdev->region_id = 0;
793 memdev->range_index = 8+1;
794 memdev->region_index = 2+1;
795 memdev->region_size = 0;
796 memdev->region_offset = 0;
797 memdev->address = 0;
798 memdev->interleave_index = 0;
799 memdev->interleave_ways = 1;
800
801 /* mem-region13 (spa/dcr3, dimm3) */
802 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
803 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
804 memdev->header.length = sizeof(*memdev);
805 memdev->device_handle = handle[3];
806 memdev->physical_id = 3;
807 memdev->region_id = 0;
808 memdev->range_index = 9+1;
809 memdev->region_index = 3+1;
810 memdev->region_size = 0;
811 memdev->region_offset = 0;
812 memdev->address = 0;
813 memdev->interleave_index = 0;
814 memdev->interleave_ways = 1;
815
816 offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
817 /* dcr-descriptor0 */
818 dcr = nfit_buf + offset;
819 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
820 dcr->header.length = sizeof(struct acpi_nfit_control_region);
821 dcr->region_index = 0+1;
822 dcr->vendor_id = 0xabcd;
823 dcr->device_id = 0;
824 dcr->revision_id = 1;
825 dcr->serial_number = ~handle[0];
Dan Williamsbe26f9a2016-02-01 17:48:42 -0800826 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -0400827 dcr->windows = 1;
828 dcr->window_size = DCR_SIZE;
829 dcr->command_offset = 0;
830 dcr->command_size = 8;
831 dcr->status_offset = 8;
832 dcr->status_size = 4;
833
834 /* dcr-descriptor1 */
835 dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
836 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
837 dcr->header.length = sizeof(struct acpi_nfit_control_region);
838 dcr->region_index = 1+1;
839 dcr->vendor_id = 0xabcd;
840 dcr->device_id = 0;
841 dcr->revision_id = 1;
842 dcr->serial_number = ~handle[1];
Dan Williamsbe26f9a2016-02-01 17:48:42 -0800843 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -0400844 dcr->windows = 1;
845 dcr->window_size = DCR_SIZE;
846 dcr->command_offset = 0;
847 dcr->command_size = 8;
848 dcr->status_offset = 8;
849 dcr->status_size = 4;
850
851 /* dcr-descriptor2 */
852 dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
853 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
854 dcr->header.length = sizeof(struct acpi_nfit_control_region);
855 dcr->region_index = 2+1;
856 dcr->vendor_id = 0xabcd;
857 dcr->device_id = 0;
858 dcr->revision_id = 1;
859 dcr->serial_number = ~handle[2];
Dan Williamsbe26f9a2016-02-01 17:48:42 -0800860 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -0400861 dcr->windows = 1;
862 dcr->window_size = DCR_SIZE;
863 dcr->command_offset = 0;
864 dcr->command_size = 8;
865 dcr->status_offset = 8;
866 dcr->status_size = 4;
867
868 /* dcr-descriptor3 */
869 dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
870 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
871 dcr->header.length = sizeof(struct acpi_nfit_control_region);
872 dcr->region_index = 3+1;
873 dcr->vendor_id = 0xabcd;
874 dcr->device_id = 0;
875 dcr->revision_id = 1;
876 dcr->serial_number = ~handle[3];
Dan Williamsbe26f9a2016-02-01 17:48:42 -0800877 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -0400878 dcr->windows = 1;
879 dcr->window_size = DCR_SIZE;
880 dcr->command_offset = 0;
881 dcr->command_size = 8;
882 dcr->status_offset = 8;
883 dcr->status_size = 4;
884
885 offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
886 /* bdw0 (spa/dcr0, dimm0) */
887 bdw = nfit_buf + offset;
888 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
889 bdw->header.length = sizeof(struct acpi_nfit_data_region);
890 bdw->region_index = 0+1;
891 bdw->windows = 1;
892 bdw->offset = 0;
893 bdw->size = BDW_SIZE;
894 bdw->capacity = DIMM_SIZE;
895 bdw->start_address = 0;
896
897 /* bdw1 (spa/dcr1, dimm1) */
898 bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
899 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
900 bdw->header.length = sizeof(struct acpi_nfit_data_region);
901 bdw->region_index = 1+1;
902 bdw->windows = 1;
903 bdw->offset = 0;
904 bdw->size = BDW_SIZE;
905 bdw->capacity = DIMM_SIZE;
906 bdw->start_address = 0;
907
908 /* bdw2 (spa/dcr2, dimm2) */
909 bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
910 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
911 bdw->header.length = sizeof(struct acpi_nfit_data_region);
912 bdw->region_index = 2+1;
913 bdw->windows = 1;
914 bdw->offset = 0;
915 bdw->size = BDW_SIZE;
916 bdw->capacity = DIMM_SIZE;
917 bdw->start_address = 0;
918
919 /* bdw3 (spa/dcr3, dimm3) */
920 bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
921 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
922 bdw->header.length = sizeof(struct acpi_nfit_data_region);
923 bdw->region_index = 3+1;
924 bdw->windows = 1;
925 bdw->offset = 0;
926 bdw->size = BDW_SIZE;
927 bdw->capacity = DIMM_SIZE;
928 bdw->start_address = 0;
929
Dan Williams9d27a872015-07-10 14:07:03 -0400930 offset = offset + sizeof(struct acpi_nfit_data_region) * 4;
931 /* flush0 (dimm0) */
932 flush = nfit_buf + offset;
933 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
934 flush->header.length = sizeof(struct acpi_nfit_flush_address);
935 flush->device_handle = handle[0];
936 flush->hint_count = 1;
937 flush->hint_address[0] = t->flush_dma[0];
938
939 /* flush1 (dimm1) */
940 flush = nfit_buf + offset + sizeof(struct acpi_nfit_flush_address) * 1;
941 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
942 flush->header.length = sizeof(struct acpi_nfit_flush_address);
943 flush->device_handle = handle[1];
944 flush->hint_count = 1;
945 flush->hint_address[0] = t->flush_dma[1];
946
947 /* flush2 (dimm2) */
948 flush = nfit_buf + offset + sizeof(struct acpi_nfit_flush_address) * 2;
949 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
950 flush->header.length = sizeof(struct acpi_nfit_flush_address);
951 flush->device_handle = handle[2];
952 flush->hint_count = 1;
953 flush->hint_address[0] = t->flush_dma[2];
954
955 /* flush3 (dimm3) */
956 flush = nfit_buf + offset + sizeof(struct acpi_nfit_flush_address) * 3;
957 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
958 flush->header.length = sizeof(struct acpi_nfit_flush_address);
959 flush->device_handle = handle[3];
960 flush->hint_count = 1;
961 flush->hint_address[0] = t->flush_dma[3];
962
Vishal Verma20985162015-10-27 16:58:27 -0600963 if (t->setup_hotplug) {
964 offset = offset + sizeof(struct acpi_nfit_flush_address) * 4;
965 /* dcr-descriptor4 */
966 dcr = nfit_buf + offset;
967 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
968 dcr->header.length = sizeof(struct acpi_nfit_control_region);
969 dcr->region_index = 4+1;
970 dcr->vendor_id = 0xabcd;
971 dcr->device_id = 0;
972 dcr->revision_id = 1;
973 dcr->serial_number = ~handle[4];
Dan Williamsbe26f9a2016-02-01 17:48:42 -0800974 dcr->code = NFIT_FIC_BLK;
Vishal Verma20985162015-10-27 16:58:27 -0600975 dcr->windows = 1;
976 dcr->window_size = DCR_SIZE;
977 dcr->command_offset = 0;
978 dcr->command_size = 8;
979 dcr->status_offset = 8;
980 dcr->status_size = 4;
981
982 offset = offset + sizeof(struct acpi_nfit_control_region);
983 /* bdw4 (spa/dcr4, dimm4) */
984 bdw = nfit_buf + offset;
985 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
986 bdw->header.length = sizeof(struct acpi_nfit_data_region);
987 bdw->region_index = 4+1;
988 bdw->windows = 1;
989 bdw->offset = 0;
990 bdw->size = BDW_SIZE;
991 bdw->capacity = DIMM_SIZE;
992 bdw->start_address = 0;
993
994 offset = offset + sizeof(struct acpi_nfit_data_region);
995 /* spa10 (dcr4) dimm4 */
996 spa = nfit_buf + offset;
997 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
998 spa->header.length = sizeof(*spa);
999 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
1000 spa->range_index = 10+1;
1001 spa->address = t->dcr_dma[4];
1002 spa->length = DCR_SIZE;
1003
1004 /*
1005 * spa11 (single-dimm interleave for hotplug, note storage
1006 * does not actually alias the related block-data-window
1007 * regions)
1008 */
1009 spa = nfit_buf + offset + sizeof(*spa);
1010 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1011 spa->header.length = sizeof(*spa);
1012 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
1013 spa->range_index = 11+1;
1014 spa->address = t->spa_set_dma[2];
1015 spa->length = SPA0_SIZE;
1016
1017 /* spa12 (bdw for dcr4) dimm4 */
1018 spa = nfit_buf + offset + sizeof(*spa) * 2;
1019 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1020 spa->header.length = sizeof(*spa);
1021 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
1022 spa->range_index = 12+1;
1023 spa->address = t->dimm_dma[4];
1024 spa->length = DIMM_SIZE;
1025
1026 offset = offset + sizeof(*spa) * 3;
1027 /* mem-region14 (spa/dcr4, dimm4) */
1028 memdev = nfit_buf + offset;
1029 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1030 memdev->header.length = sizeof(*memdev);
1031 memdev->device_handle = handle[4];
1032 memdev->physical_id = 4;
1033 memdev->region_id = 0;
1034 memdev->range_index = 10+1;
1035 memdev->region_index = 4+1;
1036 memdev->region_size = 0;
1037 memdev->region_offset = 0;
1038 memdev->address = 0;
1039 memdev->interleave_index = 0;
1040 memdev->interleave_ways = 1;
1041
1042 /* mem-region15 (spa0, dimm4) */
1043 memdev = nfit_buf + offset +
1044 sizeof(struct acpi_nfit_memory_map);
1045 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1046 memdev->header.length = sizeof(*memdev);
1047 memdev->device_handle = handle[4];
1048 memdev->physical_id = 4;
1049 memdev->region_id = 0;
1050 memdev->range_index = 11+1;
1051 memdev->region_index = 4+1;
1052 memdev->region_size = SPA0_SIZE;
1053 memdev->region_offset = t->spa_set_dma[2];
1054 memdev->address = 0;
1055 memdev->interleave_index = 0;
1056 memdev->interleave_ways = 1;
1057
1058 /* mem-region16 (spa/dcr4, dimm4) */
1059 memdev = nfit_buf + offset +
1060 sizeof(struct acpi_nfit_memory_map) * 2;
1061 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1062 memdev->header.length = sizeof(*memdev);
1063 memdev->device_handle = handle[4];
1064 memdev->physical_id = 4;
1065 memdev->region_id = 0;
1066 memdev->range_index = 12+1;
1067 memdev->region_index = 4+1;
1068 memdev->region_size = 0;
1069 memdev->region_offset = 0;
1070 memdev->address = 0;
1071 memdev->interleave_index = 0;
1072 memdev->interleave_ways = 1;
1073
1074 offset = offset + sizeof(struct acpi_nfit_memory_map) * 3;
1075 /* flush3 (dimm4) */
1076 flush = nfit_buf + offset;
1077 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1078 flush->header.length = sizeof(struct acpi_nfit_flush_address);
1079 flush->device_handle = handle[4];
1080 flush->hint_count = 1;
1081 flush->hint_address[0] = t->flush_dma[4];
1082 }
1083
Dan Williams6bc75612015-06-17 17:23:32 -04001084 acpi_desc = &t->acpi_desc;
1085 set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_dsm_force_en);
1086 set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_dsm_force_en);
1087 set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_dsm_force_en);
Vishal Verma39c686b2015-07-09 13:25:36 -06001088 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_dsm_force_en);
1089 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_dsm_force_en);
1090 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_dsm_force_en);
Dan Williams6bc75612015-06-17 17:23:32 -04001091 nd_desc = &acpi_desc->nd_desc;
1092 nd_desc->ndctl = nfit_test_ctl;
1093}
1094
1095static void nfit_test1_setup(struct nfit_test *t)
1096{
Linda Knippers6b577c92015-11-20 19:05:49 -05001097 size_t offset;
Dan Williams6bc75612015-06-17 17:23:32 -04001098 void *nfit_buf = t->nfit_buf;
1099 struct acpi_nfit_memory_map *memdev;
1100 struct acpi_nfit_control_region *dcr;
1101 struct acpi_nfit_system_address *spa;
Dan Williamsd26f73f2015-12-30 15:01:19 -08001102 struct nvdimm_bus_descriptor *nd_desc;
1103 struct acpi_nfit_desc *acpi_desc;
Dan Williams6bc75612015-06-17 17:23:32 -04001104
Linda Knippers6b577c92015-11-20 19:05:49 -05001105 offset = 0;
Dan Williams6bc75612015-06-17 17:23:32 -04001106 /* spa0 (flat range with no bdw aliasing) */
1107 spa = nfit_buf + offset;
1108 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1109 spa->header.length = sizeof(*spa);
1110 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
1111 spa->range_index = 0+1;
1112 spa->address = t->spa_set_dma[0];
1113 spa->length = SPA2_SIZE;
1114
1115 offset += sizeof(*spa);
1116 /* mem-region0 (spa0, dimm0) */
1117 memdev = nfit_buf + offset;
1118 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1119 memdev->header.length = sizeof(*memdev);
1120 memdev->device_handle = 0;
1121 memdev->physical_id = 0;
1122 memdev->region_id = 0;
1123 memdev->range_index = 0+1;
1124 memdev->region_index = 0+1;
1125 memdev->region_size = SPA2_SIZE;
1126 memdev->region_offset = 0;
1127 memdev->address = 0;
1128 memdev->interleave_index = 0;
1129 memdev->interleave_ways = 1;
Dan Williams58138822015-06-23 20:08:34 -04001130 memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
1131 | ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
Dan Williamsf4295792015-11-10 15:50:33 -08001132 | ACPI_NFIT_MEM_NOT_ARMED;
Dan Williams6bc75612015-06-17 17:23:32 -04001133
1134 offset += sizeof(*memdev);
1135 /* dcr-descriptor0 */
1136 dcr = nfit_buf + offset;
1137 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1138 dcr->header.length = sizeof(struct acpi_nfit_control_region);
1139 dcr->region_index = 0+1;
1140 dcr->vendor_id = 0xabcd;
1141 dcr->device_id = 0;
1142 dcr->revision_id = 1;
1143 dcr->serial_number = ~0;
Dan Williamsbe26f9a2016-02-01 17:48:42 -08001144 dcr->code = NFIT_FIC_BYTE;
Dan Williams6bc75612015-06-17 17:23:32 -04001145 dcr->windows = 0;
1146 dcr->window_size = 0;
1147 dcr->command_offset = 0;
1148 dcr->command_size = 0;
1149 dcr->status_offset = 0;
1150 dcr->status_size = 0;
Dan Williamsd26f73f2015-12-30 15:01:19 -08001151
1152 acpi_desc = &t->acpi_desc;
1153 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_dsm_force_en);
1154 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_dsm_force_en);
1155 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_dsm_force_en);
1156 nd_desc = &acpi_desc->nd_desc;
1157 nd_desc->ndctl = nfit_test_ctl;
Dan Williams6bc75612015-06-17 17:23:32 -04001158}
1159
1160static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
1161 void *iobuf, u64 len, int rw)
1162{
1163 struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
1164 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1165 struct nd_region *nd_region = &ndbr->nd_region;
1166 unsigned int lane;
1167
1168 lane = nd_region_acquire_lane(nd_region);
1169 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001170 memcpy(mmio->addr.base + dpa, iobuf, len);
1171 else {
1172 memcpy(iobuf, mmio->addr.base + dpa, len);
1173
1174 /* give us some some coverage of the mmio_flush_range() API */
1175 mmio_flush_range(mmio->addr.base + dpa, len);
1176 }
Dan Williams6bc75612015-06-17 17:23:32 -04001177 nd_region_release_lane(nd_region, lane);
1178
1179 return 0;
1180}
1181
1182static int nfit_test_probe(struct platform_device *pdev)
1183{
1184 struct nvdimm_bus_descriptor *nd_desc;
1185 struct acpi_nfit_desc *acpi_desc;
1186 struct device *dev = &pdev->dev;
1187 struct nfit_test *nfit_test;
1188 int rc;
1189
1190 nfit_test = to_nfit_test(&pdev->dev);
1191
1192 /* common alloc */
1193 if (nfit_test->num_dcr) {
1194 int num = nfit_test->num_dcr;
1195
1196 nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
1197 GFP_KERNEL);
1198 nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
1199 GFP_KERNEL);
Dan Williams9d27a872015-07-10 14:07:03 -04001200 nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
1201 GFP_KERNEL);
1202 nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
1203 GFP_KERNEL);
Dan Williams6bc75612015-06-17 17:23:32 -04001204 nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
1205 GFP_KERNEL);
1206 nfit_test->label_dma = devm_kcalloc(dev, num,
1207 sizeof(dma_addr_t), GFP_KERNEL);
1208 nfit_test->dcr = devm_kcalloc(dev, num,
1209 sizeof(struct nfit_test_dcr *), GFP_KERNEL);
1210 nfit_test->dcr_dma = devm_kcalloc(dev, num,
1211 sizeof(dma_addr_t), GFP_KERNEL);
1212 if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
1213 && nfit_test->label_dma && nfit_test->dcr
Dan Williams9d27a872015-07-10 14:07:03 -04001214 && nfit_test->dcr_dma && nfit_test->flush
1215 && nfit_test->flush_dma)
Dan Williams6bc75612015-06-17 17:23:32 -04001216 /* pass */;
1217 else
1218 return -ENOMEM;
1219 }
1220
1221 if (nfit_test->num_pm) {
1222 int num = nfit_test->num_pm;
1223
1224 nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
1225 GFP_KERNEL);
1226 nfit_test->spa_set_dma = devm_kcalloc(dev, num,
1227 sizeof(dma_addr_t), GFP_KERNEL);
1228 if (nfit_test->spa_set && nfit_test->spa_set_dma)
1229 /* pass */;
1230 else
1231 return -ENOMEM;
1232 }
1233
1234 /* per-nfit specific alloc */
1235 if (nfit_test->alloc(nfit_test))
1236 return -ENOMEM;
1237
1238 nfit_test->setup(nfit_test);
1239 acpi_desc = &nfit_test->acpi_desc;
1240 acpi_desc->dev = &pdev->dev;
1241 acpi_desc->nfit = nfit_test->nfit_buf;
1242 acpi_desc->blk_do_io = nfit_test_blk_do_io;
1243 nd_desc = &acpi_desc->nd_desc;
1244 nd_desc->attr_groups = acpi_nfit_attribute_groups;
1245 acpi_desc->nvdimm_bus = nvdimm_bus_register(&pdev->dev, nd_desc);
1246 if (!acpi_desc->nvdimm_bus)
1247 return -ENXIO;
1248
Vishal Verma20985162015-10-27 16:58:27 -06001249 INIT_LIST_HEAD(&acpi_desc->spa_maps);
1250 INIT_LIST_HEAD(&acpi_desc->spas);
1251 INIT_LIST_HEAD(&acpi_desc->dcrs);
1252 INIT_LIST_HEAD(&acpi_desc->bdws);
1253 INIT_LIST_HEAD(&acpi_desc->idts);
1254 INIT_LIST_HEAD(&acpi_desc->flushes);
1255 INIT_LIST_HEAD(&acpi_desc->memdevs);
1256 INIT_LIST_HEAD(&acpi_desc->dimms);
1257 mutex_init(&acpi_desc->spa_map_mutex);
1258 mutex_init(&acpi_desc->init_mutex);
1259
1260 rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_size);
1261 if (rc) {
1262 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1263 return rc;
1264 }
1265
1266 if (nfit_test->setup != nfit_test0_setup)
1267 return 0;
1268
1269 nfit_test->setup_hotplug = 1;
1270 nfit_test->setup(nfit_test);
1271
Dan Williams6bc75612015-06-17 17:23:32 -04001272 rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_size);
1273 if (rc) {
1274 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1275 return rc;
1276 }
1277
1278 return 0;
1279}
1280
1281static int nfit_test_remove(struct platform_device *pdev)
1282{
1283 struct nfit_test *nfit_test = to_nfit_test(&pdev->dev);
1284 struct acpi_nfit_desc *acpi_desc = &nfit_test->acpi_desc;
1285
1286 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
1287
1288 return 0;
1289}
1290
1291static void nfit_test_release(struct device *dev)
1292{
1293 struct nfit_test *nfit_test = to_nfit_test(dev);
1294
1295 kfree(nfit_test);
1296}
1297
1298static const struct platform_device_id nfit_test_id[] = {
1299 { KBUILD_MODNAME },
1300 { },
1301};
1302
1303static struct platform_driver nfit_test_driver = {
1304 .probe = nfit_test_probe,
1305 .remove = nfit_test_remove,
1306 .driver = {
1307 .name = KBUILD_MODNAME,
1308 },
1309 .id_table = nfit_test_id,
1310};
1311
1312#ifdef CONFIG_CMA_SIZE_MBYTES
1313#define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
1314#else
1315#define CMA_SIZE_MBYTES 0
1316#endif
1317
1318static __init int nfit_test_init(void)
1319{
1320 int rc, i;
1321
1322 nfit_test_setup(nfit_test_lookup);
1323
1324 for (i = 0; i < NUM_NFITS; i++) {
1325 struct nfit_test *nfit_test;
1326 struct platform_device *pdev;
1327 static int once;
1328
1329 nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
1330 if (!nfit_test) {
1331 rc = -ENOMEM;
1332 goto err_register;
1333 }
1334 INIT_LIST_HEAD(&nfit_test->resources);
1335 switch (i) {
1336 case 0:
1337 nfit_test->num_pm = NUM_PM;
1338 nfit_test->num_dcr = NUM_DCR;
1339 nfit_test->alloc = nfit_test0_alloc;
1340 nfit_test->setup = nfit_test0_setup;
1341 break;
1342 case 1:
1343 nfit_test->num_pm = 1;
1344 nfit_test->alloc = nfit_test1_alloc;
1345 nfit_test->setup = nfit_test1_setup;
1346 break;
1347 default:
1348 rc = -EINVAL;
1349 goto err_register;
1350 }
1351 pdev = &nfit_test->pdev;
1352 pdev->name = KBUILD_MODNAME;
1353 pdev->id = i;
1354 pdev->dev.release = nfit_test_release;
1355 rc = platform_device_register(pdev);
1356 if (rc) {
1357 put_device(&pdev->dev);
1358 goto err_register;
1359 }
1360
1361 rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1362 if (rc)
1363 goto err_register;
1364
1365 instances[i] = nfit_test;
1366
1367 if (!once++) {
1368 dma_addr_t dma;
1369 void *buf;
1370
1371 buf = dma_alloc_coherent(&pdev->dev, SZ_128M, &dma,
1372 GFP_KERNEL);
1373 if (!buf) {
1374 rc = -ENOMEM;
1375 dev_warn(&pdev->dev, "need 128M of free cma\n");
1376 goto err_register;
1377 }
1378 dma_free_coherent(&pdev->dev, SZ_128M, buf, dma);
1379 }
1380 }
1381
1382 rc = platform_driver_register(&nfit_test_driver);
1383 if (rc)
1384 goto err_register;
1385 return 0;
1386
1387 err_register:
1388 for (i = 0; i < NUM_NFITS; i++)
1389 if (instances[i])
1390 platform_device_unregister(&instances[i]->pdev);
1391 nfit_test_teardown();
1392 return rc;
1393}
1394
1395static __exit void nfit_test_exit(void)
1396{
1397 int i;
1398
1399 platform_driver_unregister(&nfit_test_driver);
1400 for (i = 0; i < NUM_NFITS; i++)
1401 platform_device_unregister(&instances[i]->pdev);
1402 nfit_test_teardown();
1403}
1404
1405module_init(nfit_test_init);
1406module_exit(nfit_test_exit);
1407MODULE_LICENSE("GPL v2");
1408MODULE_AUTHOR("Intel Corporation");