blob: 8d79c75d3caeb5622123d639cdbaf3c2394f3696 [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>
Dan Williamsd8d378f2016-08-10 15:59:09 -070016#include <linux/workqueue.h>
Dan Williams6bc75612015-06-17 17:23:32 -040017#include <linux/libnvdimm.h>
18#include <linux/vmalloc.h>
19#include <linux/device.h>
20#include <linux/module.h>
Vishal Verma20985162015-10-27 16:58:27 -060021#include <linux/mutex.h>
Dan Williams6bc75612015-06-17 17:23:32 -040022#include <linux/ndctl.h>
23#include <linux/sizes.h>
Vishal Verma20985162015-10-27 16:58:27 -060024#include <linux/list.h>
Dan Williams6bc75612015-06-17 17:23:32 -040025#include <linux/slab.h>
26#include <nfit.h>
27#include <nd.h>
28#include "nfit_test.h"
29
30/*
31 * Generate an NFIT table to describe the following topology:
32 *
33 * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
34 *
35 * (a) (b) DIMM BLK-REGION
36 * +----------+--------------+----------+---------+
37 * +------+ | blk2.0 | pm0.0 | blk2.1 | pm1.0 | 0 region2
38 * | imc0 +--+- - - - - region0 - - - -+----------+ +
39 * +--+---+ | blk3.0 | pm0.0 | blk3.1 | pm1.0 | 1 region3
40 * | +----------+--------------v----------v v
41 * +--+---+ | |
42 * | cpu0 | region1
43 * +--+---+ | |
44 * | +-------------------------^----------^ ^
45 * +--+---+ | blk4.0 | pm1.0 | 2 region4
46 * | imc1 +--+-------------------------+----------+ +
47 * +------+ | blk5.0 | pm1.0 | 3 region5
48 * +-------------------------+----------+-+-------+
49 *
Vishal Verma20985162015-10-27 16:58:27 -060050 * +--+---+
51 * | cpu1 |
52 * +--+---+ (Hotplug DIMM)
53 * | +----------------------------------------------+
54 * +--+---+ | blk6.0/pm7.0 | 4 region6/7
55 * | imc0 +--+----------------------------------------------+
56 * +------+
57 *
58 *
Dan Williams6bc75612015-06-17 17:23:32 -040059 * *) In this layout we have four dimms and two memory controllers in one
60 * socket. Each unique interface (BLK or PMEM) to DPA space
61 * is identified by a region device with a dynamically assigned id.
62 *
63 * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
64 * A single PMEM namespace "pm0.0" is created using half of the
65 * REGION0 SPA-range. REGION0 spans dimm0 and dimm1. PMEM namespace
66 * allocate from from the bottom of a region. The unallocated
67 * portion of REGION0 aliases with REGION2 and REGION3. That
68 * unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
69 * "blk3.0") starting at the base of each DIMM to offset (a) in those
70 * DIMMs. "pm0.0", "blk2.0" and "blk3.0" are free-form readable
71 * names that can be assigned to a namespace.
72 *
73 * *) In the last portion of dimm0 and dimm1 we have an interleaved
74 * SPA range, REGION1, that spans those two dimms as well as dimm2
75 * and dimm3. Some of REGION1 allocated to a PMEM namespace named
76 * "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
77 * dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
78 * "blk5.0".
79 *
80 * *) The portion of dimm2 and dimm3 that do not participate in the
81 * REGION1 interleaved SPA range (i.e. the DPA address below offset
82 * (b) are also included in the "blk4.0" and "blk5.0" namespaces.
83 * Note, that BLK namespaces need not be contiguous in DPA-space, and
84 * can consume aliased capacity from multiple interleave sets.
85 *
86 * BUS1: Legacy NVDIMM (single contiguous range)
87 *
88 * region2
89 * +---------------------+
90 * |---------------------|
91 * || pm2.0 ||
92 * |---------------------|
93 * +---------------------+
94 *
95 * *) A NFIT-table may describe a simple system-physical-address range
96 * with no BLK aliasing. This type of region may optionally
97 * reference an NVDIMM.
98 */
99enum {
Vishal Verma20985162015-10-27 16:58:27 -0600100 NUM_PM = 3,
101 NUM_DCR = 5,
Dan Williams85d3fa02016-06-02 16:27:21 -0700102 NUM_HINTS = 8,
Dan Williams6bc75612015-06-17 17:23:32 -0400103 NUM_BDW = NUM_DCR,
104 NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
105 NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
106 DIMM_SIZE = SZ_32M,
107 LABEL_SIZE = SZ_128K,
Dan Williams7bfe97c2016-07-14 21:47:00 -0700108 SPA_VCD_SIZE = SZ_4M,
Dan Williams6bc75612015-06-17 17:23:32 -0400109 SPA0_SIZE = DIMM_SIZE,
110 SPA1_SIZE = DIMM_SIZE*2,
111 SPA2_SIZE = DIMM_SIZE,
112 BDW_SIZE = 64 << 8,
113 DCR_SIZE = 12,
114 NUM_NFITS = 2, /* permit testing multiple NFITs per system */
115};
116
117struct nfit_test_dcr {
118 __le64 bdw_addr;
119 __le32 bdw_status;
120 __u8 aperature[BDW_SIZE];
121};
122
123#define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
124 (((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
125 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
126
127static u32 handle[NUM_DCR] = {
128 [0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
129 [1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
130 [2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
131 [3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
Vishal Verma20985162015-10-27 16:58:27 -0600132 [4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
Dan Williams6bc75612015-06-17 17:23:32 -0400133};
134
135struct nfit_test {
136 struct acpi_nfit_desc acpi_desc;
137 struct platform_device pdev;
138 struct list_head resources;
139 void *nfit_buf;
140 dma_addr_t nfit_dma;
141 size_t nfit_size;
142 int num_dcr;
143 int num_pm;
144 void **dimm;
145 dma_addr_t *dimm_dma;
Dan Williams9d27a872015-07-10 14:07:03 -0400146 void **flush;
147 dma_addr_t *flush_dma;
Dan Williams6bc75612015-06-17 17:23:32 -0400148 void **label;
149 dma_addr_t *label_dma;
150 void **spa_set;
151 dma_addr_t *spa_set_dma;
152 struct nfit_test_dcr **dcr;
153 dma_addr_t *dcr_dma;
154 int (*alloc)(struct nfit_test *t);
155 void (*setup)(struct nfit_test *t);
Vishal Verma20985162015-10-27 16:58:27 -0600156 int setup_hotplug;
Dan Williamsc14a8682016-08-18 22:15:04 -0700157 union acpi_object **_fit;
158 dma_addr_t _fit_dma;
Dan Williamsf471f1a2016-02-20 15:12:47 -0800159 struct ars_state {
160 struct nd_cmd_ars_status *ars_status;
161 unsigned long deadline;
162 spinlock_t lock;
163 } ars_state;
Dan Williams6bc75612015-06-17 17:23:32 -0400164};
165
166static struct nfit_test *to_nfit_test(struct device *dev)
167{
168 struct platform_device *pdev = to_platform_device(dev);
169
170 return container_of(pdev, struct nfit_test, pdev);
171}
172
Vishal Verma39c686b2015-07-09 13:25:36 -0600173static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
174 unsigned int buf_len)
175{
176 if (buf_len < sizeof(*nd_cmd))
177 return -EINVAL;
178
179 nd_cmd->status = 0;
180 nd_cmd->config_size = LABEL_SIZE;
181 nd_cmd->max_xfer = SZ_4K;
182
183 return 0;
184}
185
186static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
187 *nd_cmd, unsigned int buf_len, void *label)
188{
189 unsigned int len, offset = nd_cmd->in_offset;
190 int rc;
191
192 if (buf_len < sizeof(*nd_cmd))
193 return -EINVAL;
194 if (offset >= LABEL_SIZE)
195 return -EINVAL;
196 if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
197 return -EINVAL;
198
199 nd_cmd->status = 0;
200 len = min(nd_cmd->in_length, LABEL_SIZE - offset);
201 memcpy(nd_cmd->out_buf, label + offset, len);
202 rc = buf_len - sizeof(*nd_cmd) - len;
203
204 return rc;
205}
206
207static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
208 unsigned int buf_len, void *label)
209{
210 unsigned int len, offset = nd_cmd->in_offset;
211 u32 *status;
212 int rc;
213
214 if (buf_len < sizeof(*nd_cmd))
215 return -EINVAL;
216 if (offset >= LABEL_SIZE)
217 return -EINVAL;
218 if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
219 return -EINVAL;
220
221 status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
222 *status = 0;
223 len = min(nd_cmd->in_length, LABEL_SIZE - offset);
224 memcpy(label + offset, nd_cmd->in_buf, len);
225 rc = buf_len - sizeof(*nd_cmd) - (len + 4);
226
227 return rc;
228}
229
Dan Williams747ffe12016-02-19 15:21:14 -0800230#define NFIT_TEST_ARS_RECORDS 4
Dan Williamsd4f32362016-03-03 16:08:54 -0800231#define NFIT_TEST_CLEAR_ERR_UNIT 256
Dan Williams747ffe12016-02-19 15:21:14 -0800232
Vishal Verma39c686b2015-07-09 13:25:36 -0600233static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
234 unsigned int buf_len)
235{
236 if (buf_len < sizeof(*nd_cmd))
237 return -EINVAL;
238
Dan Williams747ffe12016-02-19 15:21:14 -0800239 nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
240 + NFIT_TEST_ARS_RECORDS * sizeof(struct nd_ars_record);
Vishal Verma39c686b2015-07-09 13:25:36 -0600241 nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
Dan Williamsd4f32362016-03-03 16:08:54 -0800242 nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
Vishal Verma39c686b2015-07-09 13:25:36 -0600243
244 return 0;
245}
246
Dan Williamsf471f1a2016-02-20 15:12:47 -0800247/*
248 * Initialize the ars_state to return an ars_result 1 second in the future with
249 * a 4K error range in the middle of the requested address range.
250 */
251static void post_ars_status(struct ars_state *ars_state, u64 addr, u64 len)
Vishal Verma39c686b2015-07-09 13:25:36 -0600252{
Dan Williamsf471f1a2016-02-20 15:12:47 -0800253 struct nd_cmd_ars_status *ars_status;
254 struct nd_ars_record *ars_record;
255
256 ars_state->deadline = jiffies + 1*HZ;
257 ars_status = ars_state->ars_status;
258 ars_status->status = 0;
259 ars_status->out_length = sizeof(struct nd_cmd_ars_status)
260 + sizeof(struct nd_ars_record);
261 ars_status->address = addr;
262 ars_status->length = len;
263 ars_status->type = ND_ARS_PERSISTENT;
264 ars_status->num_records = 1;
265 ars_record = &ars_status->records[0];
266 ars_record->handle = 0;
267 ars_record->err_address = addr + len / 2;
268 ars_record->length = SZ_4K;
269}
270
271static int nfit_test_cmd_ars_start(struct ars_state *ars_state,
272 struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
273 int *cmd_rc)
274{
275 if (buf_len < sizeof(*ars_start))
Vishal Verma39c686b2015-07-09 13:25:36 -0600276 return -EINVAL;
277
Dan Williamsf471f1a2016-02-20 15:12:47 -0800278 spin_lock(&ars_state->lock);
279 if (time_before(jiffies, ars_state->deadline)) {
280 ars_start->status = NFIT_ARS_START_BUSY;
281 *cmd_rc = -EBUSY;
282 } else {
283 ars_start->status = 0;
284 ars_start->scrub_time = 1;
285 post_ars_status(ars_state, ars_start->address,
286 ars_start->length);
287 *cmd_rc = 0;
288 }
289 spin_unlock(&ars_state->lock);
Vishal Verma39c686b2015-07-09 13:25:36 -0600290
291 return 0;
292}
293
Dan Williamsf471f1a2016-02-20 15:12:47 -0800294static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
295 struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
296 int *cmd_rc)
Vishal Verma39c686b2015-07-09 13:25:36 -0600297{
Dan Williamsf471f1a2016-02-20 15:12:47 -0800298 if (buf_len < ars_state->ars_status->out_length)
Vishal Verma39c686b2015-07-09 13:25:36 -0600299 return -EINVAL;
300
Dan Williamsf471f1a2016-02-20 15:12:47 -0800301 spin_lock(&ars_state->lock);
302 if (time_before(jiffies, ars_state->deadline)) {
303 memset(ars_status, 0, buf_len);
304 ars_status->status = NFIT_ARS_STATUS_BUSY;
305 ars_status->out_length = sizeof(*ars_status);
306 *cmd_rc = -EBUSY;
307 } else {
308 memcpy(ars_status, ars_state->ars_status,
309 ars_state->ars_status->out_length);
310 *cmd_rc = 0;
311 }
312 spin_unlock(&ars_state->lock);
Vishal Verma39c686b2015-07-09 13:25:36 -0600313 return 0;
314}
315
Dan Williamsd4f32362016-03-03 16:08:54 -0800316static int nfit_test_cmd_clear_error(struct nd_cmd_clear_error *clear_err,
317 unsigned int buf_len, int *cmd_rc)
318{
319 const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
320 if (buf_len < sizeof(*clear_err))
321 return -EINVAL;
322
323 if ((clear_err->address & mask) || (clear_err->length & mask))
324 return -EINVAL;
325
326 /*
327 * Report 'all clear' success for all commands even though a new
328 * scrub will find errors again. This is enough to have the
329 * error removed from the 'badblocks' tracking in the pmem
330 * driver.
331 */
332 clear_err->status = 0;
333 clear_err->cleared = clear_err->length;
334 *cmd_rc = 0;
335 return 0;
336}
337
Dan Williamsbaa51272016-04-05 17:40:52 -0700338static int nfit_test_cmd_smart(struct nd_cmd_smart *smart, unsigned int buf_len)
339{
340 static const struct nd_smart_payload smart_data = {
341 .flags = ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID
342 | ND_SMART_SPARES_VALID | ND_SMART_ALARM_VALID
343 | ND_SMART_USED_VALID | ND_SMART_SHUTDOWN_VALID,
344 .health = ND_SMART_NON_CRITICAL_HEALTH,
345 .temperature = 23 * 16,
346 .spares = 75,
347 .alarm_flags = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
348 .life_used = 5,
349 .shutdown_state = 0,
350 .vendor_size = 0,
351 };
352
353 if (buf_len < sizeof(*smart))
354 return -EINVAL;
355 memcpy(smart->data, &smart_data, sizeof(smart_data));
356 return 0;
357}
358
359static int nfit_test_cmd_smart_threshold(struct nd_cmd_smart_threshold *smart_t,
360 unsigned int buf_len)
361{
362 static const struct nd_smart_threshold_payload smart_t_data = {
363 .alarm_control = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
364 .temperature = 40 * 16,
365 .spares = 5,
366 };
367
368 if (buf_len < sizeof(*smart_t))
369 return -EINVAL;
370 memcpy(smart_t->data, &smart_t_data, sizeof(smart_t_data));
371 return 0;
372}
373
Dan Williams6bc75612015-06-17 17:23:32 -0400374static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
375 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
Dan Williamsaef25332016-02-12 17:01:11 -0800376 unsigned int buf_len, int *cmd_rc)
Dan Williams6bc75612015-06-17 17:23:32 -0400377{
378 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
379 struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
Dan Williams6634fb062016-04-27 16:46:15 -0600380 unsigned int func = cmd;
Dan Williamsf471f1a2016-02-20 15:12:47 -0800381 int i, rc = 0, __cmd_rc;
382
383 if (!cmd_rc)
384 cmd_rc = &__cmd_rc;
385 *cmd_rc = 0;
Dan Williams6bc75612015-06-17 17:23:32 -0400386
Vishal Verma39c686b2015-07-09 13:25:36 -0600387 if (nvdimm) {
388 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
Dan Williamse3654ec2016-04-28 16:17:07 -0700389 unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams6bc75612015-06-17 17:23:32 -0400390
Dan Williams6634fb062016-04-27 16:46:15 -0600391 if (!nfit_mem)
392 return -ENOTTY;
393
394 if (cmd == ND_CMD_CALL) {
395 struct nd_cmd_pkg *call_pkg = buf;
396
397 buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
398 buf = (void *) call_pkg->nd_payload;
399 func = call_pkg->nd_command;
400 if (call_pkg->nd_family != nfit_mem->family)
401 return -ENOTTY;
402 }
403
404 if (!test_bit(cmd, &cmd_mask)
405 || !test_bit(func, &nfit_mem->dsm_mask))
Vishal Verma39c686b2015-07-09 13:25:36 -0600406 return -ENOTTY;
407
408 /* lookup label space for the given dimm */
409 for (i = 0; i < ARRAY_SIZE(handle); i++)
410 if (__to_nfit_memdev(nfit_mem)->device_handle ==
411 handle[i])
412 break;
413 if (i >= ARRAY_SIZE(handle))
414 return -ENXIO;
415
Dan Williams6634fb062016-04-27 16:46:15 -0600416 switch (func) {
Vishal Verma39c686b2015-07-09 13:25:36 -0600417 case ND_CMD_GET_CONFIG_SIZE:
418 rc = nfit_test_cmd_get_config_size(buf, buf_len);
Dan Williams6bc75612015-06-17 17:23:32 -0400419 break;
Vishal Verma39c686b2015-07-09 13:25:36 -0600420 case ND_CMD_GET_CONFIG_DATA:
421 rc = nfit_test_cmd_get_config_data(buf, buf_len,
422 t->label[i]);
423 break;
424 case ND_CMD_SET_CONFIG_DATA:
425 rc = nfit_test_cmd_set_config_data(buf, buf_len,
426 t->label[i]);
427 break;
Dan Williamsbaa51272016-04-05 17:40:52 -0700428 case ND_CMD_SMART:
429 rc = nfit_test_cmd_smart(buf, buf_len);
430 break;
431 case ND_CMD_SMART_THRESHOLD:
432 rc = nfit_test_cmd_smart_threshold(buf, buf_len);
433 break;
Vishal Verma39c686b2015-07-09 13:25:36 -0600434 default:
435 return -ENOTTY;
436 }
437 } else {
Dan Williamsf471f1a2016-02-20 15:12:47 -0800438 struct ars_state *ars_state = &t->ars_state;
439
Dan Williamse3654ec2016-04-28 16:17:07 -0700440 if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
Vishal Verma39c686b2015-07-09 13:25:36 -0600441 return -ENOTTY;
Dan Williams6bc75612015-06-17 17:23:32 -0400442
Dan Williams6634fb062016-04-27 16:46:15 -0600443 switch (func) {
Vishal Verma39c686b2015-07-09 13:25:36 -0600444 case ND_CMD_ARS_CAP:
445 rc = nfit_test_cmd_ars_cap(buf, buf_len);
446 break;
447 case ND_CMD_ARS_START:
Dan Williamsf471f1a2016-02-20 15:12:47 -0800448 rc = nfit_test_cmd_ars_start(ars_state, buf, buf_len,
449 cmd_rc);
Vishal Verma39c686b2015-07-09 13:25:36 -0600450 break;
451 case ND_CMD_ARS_STATUS:
Dan Williamsf471f1a2016-02-20 15:12:47 -0800452 rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
453 cmd_rc);
Vishal Verma39c686b2015-07-09 13:25:36 -0600454 break;
Dan Williamsd4f32362016-03-03 16:08:54 -0800455 case ND_CMD_CLEAR_ERROR:
456 rc = nfit_test_cmd_clear_error(buf, buf_len, cmd_rc);
457 break;
Vishal Verma39c686b2015-07-09 13:25:36 -0600458 default:
459 return -ENOTTY;
460 }
Dan Williams6bc75612015-06-17 17:23:32 -0400461 }
462
463 return rc;
464}
465
466static DEFINE_SPINLOCK(nfit_test_lock);
467static struct nfit_test *instances[NUM_NFITS];
468
469static void release_nfit_res(void *data)
470{
471 struct nfit_test_resource *nfit_res = data;
472 struct resource *res = nfit_res->res;
473
474 spin_lock(&nfit_test_lock);
475 list_del(&nfit_res->list);
476 spin_unlock(&nfit_test_lock);
477
Dan Williamsee8520fe2016-06-15 20:34:17 -0700478 vfree(nfit_res->buf);
Dan Williams6bc75612015-06-17 17:23:32 -0400479 kfree(res);
480 kfree(nfit_res);
481}
482
483static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
484 void *buf)
485{
486 struct device *dev = &t->pdev.dev;
487 struct resource *res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
488 struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
489 GFP_KERNEL);
490 int rc;
491
492 if (!res || !buf || !nfit_res)
493 goto err;
494 rc = devm_add_action(dev, release_nfit_res, nfit_res);
495 if (rc)
496 goto err;
497 INIT_LIST_HEAD(&nfit_res->list);
498 memset(buf, 0, size);
499 nfit_res->dev = dev;
500 nfit_res->buf = buf;
501 nfit_res->res = res;
502 res->start = *dma;
503 res->end = *dma + size - 1;
504 res->name = "NFIT";
505 spin_lock(&nfit_test_lock);
506 list_add(&nfit_res->list, &t->resources);
507 spin_unlock(&nfit_test_lock);
508
509 return nfit_res->buf;
510 err:
Dan Williamsee8520fe2016-06-15 20:34:17 -0700511 if (buf)
Dan Williams6bc75612015-06-17 17:23:32 -0400512 vfree(buf);
513 kfree(res);
514 kfree(nfit_res);
515 return NULL;
516}
517
518static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
519{
520 void *buf = vmalloc(size);
521
522 *dma = (unsigned long) buf;
523 return __test_alloc(t, size, dma, buf);
524}
525
Dan Williams6bc75612015-06-17 17:23:32 -0400526static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
527{
528 int i;
529
530 for (i = 0; i < ARRAY_SIZE(instances); i++) {
531 struct nfit_test_resource *n, *nfit_res = NULL;
532 struct nfit_test *t = instances[i];
533
534 if (!t)
535 continue;
536 spin_lock(&nfit_test_lock);
537 list_for_each_entry(n, &t->resources, list) {
538 if (addr >= n->res->start && (addr < n->res->start
539 + resource_size(n->res))) {
540 nfit_res = n;
541 break;
542 } else if (addr >= (unsigned long) n->buf
543 && (addr < (unsigned long) n->buf
544 + resource_size(n->res))) {
545 nfit_res = n;
546 break;
547 }
548 }
549 spin_unlock(&nfit_test_lock);
550 if (nfit_res)
551 return nfit_res;
552 }
553
554 return NULL;
555}
556
Dan Williamsf471f1a2016-02-20 15:12:47 -0800557static int ars_state_init(struct device *dev, struct ars_state *ars_state)
558{
559 ars_state->ars_status = devm_kzalloc(dev,
560 sizeof(struct nd_cmd_ars_status)
561 + sizeof(struct nd_ars_record) * NFIT_TEST_ARS_RECORDS,
562 GFP_KERNEL);
563 if (!ars_state->ars_status)
564 return -ENOMEM;
565 spin_lock_init(&ars_state->lock);
566 return 0;
567}
568
Dan Williams6bc75612015-06-17 17:23:32 -0400569static int nfit_test0_alloc(struct nfit_test *t)
570{
Linda Knippers6b577c92015-11-20 19:05:49 -0500571 size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
Dan Williams6bc75612015-06-17 17:23:32 -0400572 + sizeof(struct acpi_nfit_memory_map) * NUM_MEM
573 + sizeof(struct acpi_nfit_control_region) * NUM_DCR
Dan Williams3b873562016-02-01 17:45:54 -0800574 + offsetof(struct acpi_nfit_control_region,
575 window_size) * NUM_DCR
Dan Williams9d27a872015-07-10 14:07:03 -0400576 + sizeof(struct acpi_nfit_data_region) * NUM_BDW
Dan Williams85d3fa02016-06-02 16:27:21 -0700577 + (sizeof(struct acpi_nfit_flush_address)
578 + sizeof(u64) * NUM_HINTS) * NUM_DCR;
Dan Williams6bc75612015-06-17 17:23:32 -0400579 int i;
580
581 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
582 if (!t->nfit_buf)
583 return -ENOMEM;
584 t->nfit_size = nfit_size;
585
Dan Williamsee8520fe2016-06-15 20:34:17 -0700586 t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
Dan Williams6bc75612015-06-17 17:23:32 -0400587 if (!t->spa_set[0])
588 return -ENOMEM;
589
Dan Williamsee8520fe2016-06-15 20:34:17 -0700590 t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
Dan Williams6bc75612015-06-17 17:23:32 -0400591 if (!t->spa_set[1])
592 return -ENOMEM;
593
Dan Williamsee8520fe2016-06-15 20:34:17 -0700594 t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
Vishal Verma20985162015-10-27 16:58:27 -0600595 if (!t->spa_set[2])
596 return -ENOMEM;
597
Dan Williams6bc75612015-06-17 17:23:32 -0400598 for (i = 0; i < NUM_DCR; i++) {
599 t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
600 if (!t->dimm[i])
601 return -ENOMEM;
602
603 t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
604 if (!t->label[i])
605 return -ENOMEM;
606 sprintf(t->label[i], "label%d", i);
Dan Williams9d27a872015-07-10 14:07:03 -0400607
Dan Williams85d3fa02016-06-02 16:27:21 -0700608 t->flush[i] = test_alloc(t, sizeof(u64) * NUM_HINTS,
609 &t->flush_dma[i]);
Dan Williams9d27a872015-07-10 14:07:03 -0400610 if (!t->flush[i])
611 return -ENOMEM;
Dan Williams6bc75612015-06-17 17:23:32 -0400612 }
613
614 for (i = 0; i < NUM_DCR; i++) {
615 t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
616 if (!t->dcr[i])
617 return -ENOMEM;
618 }
619
Dan Williamsc14a8682016-08-18 22:15:04 -0700620 t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
621 if (!t->_fit)
622 return -ENOMEM;
623
Dan Williamsf471f1a2016-02-20 15:12:47 -0800624 return ars_state_init(&t->pdev.dev, &t->ars_state);
Dan Williams6bc75612015-06-17 17:23:32 -0400625}
626
627static int nfit_test1_alloc(struct nfit_test *t)
628{
Dan Williams7bfe97c2016-07-14 21:47:00 -0700629 size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
Dan Williams6bc75612015-06-17 17:23:32 -0400630 + sizeof(struct acpi_nfit_memory_map)
Dan Williams3b873562016-02-01 17:45:54 -0800631 + offsetof(struct acpi_nfit_control_region, window_size);
Dan Williams6bc75612015-06-17 17:23:32 -0400632
633 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
634 if (!t->nfit_buf)
635 return -ENOMEM;
636 t->nfit_size = nfit_size;
637
Dan Williamsee8520fe2016-06-15 20:34:17 -0700638 t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
Dan Williams6bc75612015-06-17 17:23:32 -0400639 if (!t->spa_set[0])
640 return -ENOMEM;
641
Dan Williams7bfe97c2016-07-14 21:47:00 -0700642 t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
643 if (!t->spa_set[1])
644 return -ENOMEM;
645
Dan Williamsf471f1a2016-02-20 15:12:47 -0800646 return ars_state_init(&t->pdev.dev, &t->ars_state);
Dan Williams6bc75612015-06-17 17:23:32 -0400647}
648
Dan Williams5dc68e52016-07-15 11:27:03 -0700649static void dcr_common_init(struct acpi_nfit_control_region *dcr)
650{
651 dcr->vendor_id = 0xabcd;
652 dcr->device_id = 0;
653 dcr->revision_id = 1;
654 dcr->valid_fields = 1;
655 dcr->manufacturing_location = 0xa;
656 dcr->manufacturing_date = cpu_to_be16(2016);
657}
658
Dan Williams6bc75612015-06-17 17:23:32 -0400659static void nfit_test0_setup(struct nfit_test *t)
660{
Dan Williams85d3fa02016-06-02 16:27:21 -0700661 const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
662 + (sizeof(u64) * NUM_HINTS);
Dan Williams6bc75612015-06-17 17:23:32 -0400663 struct acpi_nfit_desc *acpi_desc;
664 struct acpi_nfit_memory_map *memdev;
665 void *nfit_buf = t->nfit_buf;
Dan Williams6bc75612015-06-17 17:23:32 -0400666 struct acpi_nfit_system_address *spa;
667 struct acpi_nfit_control_region *dcr;
668 struct acpi_nfit_data_region *bdw;
Dan Williams9d27a872015-07-10 14:07:03 -0400669 struct acpi_nfit_flush_address *flush;
Dan Williams85d3fa02016-06-02 16:27:21 -0700670 unsigned int offset, i;
Dan Williams6bc75612015-06-17 17:23:32 -0400671
Dan Williams6bc75612015-06-17 17:23:32 -0400672 /*
673 * spa0 (interleave first half of dimm0 and dimm1, note storage
674 * does not actually alias the related block-data-window
675 * regions)
676 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500677 spa = nfit_buf;
Dan Williams6bc75612015-06-17 17:23:32 -0400678 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
679 spa->header.length = sizeof(*spa);
680 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
681 spa->range_index = 0+1;
682 spa->address = t->spa_set_dma[0];
683 spa->length = SPA0_SIZE;
684
685 /*
686 * spa1 (interleave last half of the 4 DIMMS, note storage
687 * does not actually alias the related block-data-window
688 * regions)
689 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500690 spa = nfit_buf + sizeof(*spa);
Dan Williams6bc75612015-06-17 17:23:32 -0400691 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
692 spa->header.length = sizeof(*spa);
693 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
694 spa->range_index = 1+1;
695 spa->address = t->spa_set_dma[1];
696 spa->length = SPA1_SIZE;
697
698 /* spa2 (dcr0) dimm0 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500699 spa = nfit_buf + sizeof(*spa) * 2;
Dan Williams6bc75612015-06-17 17:23:32 -0400700 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
701 spa->header.length = sizeof(*spa);
702 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
703 spa->range_index = 2+1;
704 spa->address = t->dcr_dma[0];
705 spa->length = DCR_SIZE;
706
707 /* spa3 (dcr1) dimm1 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500708 spa = nfit_buf + sizeof(*spa) * 3;
Dan Williams6bc75612015-06-17 17:23:32 -0400709 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
710 spa->header.length = sizeof(*spa);
711 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
712 spa->range_index = 3+1;
713 spa->address = t->dcr_dma[1];
714 spa->length = DCR_SIZE;
715
716 /* spa4 (dcr2) dimm2 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500717 spa = nfit_buf + sizeof(*spa) * 4;
Dan Williams6bc75612015-06-17 17:23:32 -0400718 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
719 spa->header.length = sizeof(*spa);
720 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
721 spa->range_index = 4+1;
722 spa->address = t->dcr_dma[2];
723 spa->length = DCR_SIZE;
724
725 /* spa5 (dcr3) dimm3 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500726 spa = nfit_buf + sizeof(*spa) * 5;
Dan Williams6bc75612015-06-17 17:23:32 -0400727 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
728 spa->header.length = sizeof(*spa);
729 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
730 spa->range_index = 5+1;
731 spa->address = t->dcr_dma[3];
732 spa->length = DCR_SIZE;
733
734 /* spa6 (bdw for dcr0) dimm0 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500735 spa = nfit_buf + sizeof(*spa) * 6;
Dan Williams6bc75612015-06-17 17:23:32 -0400736 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
737 spa->header.length = sizeof(*spa);
738 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
739 spa->range_index = 6+1;
740 spa->address = t->dimm_dma[0];
741 spa->length = DIMM_SIZE;
742
743 /* spa7 (bdw for dcr1) dimm1 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500744 spa = nfit_buf + sizeof(*spa) * 7;
Dan Williams6bc75612015-06-17 17:23:32 -0400745 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
746 spa->header.length = sizeof(*spa);
747 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
748 spa->range_index = 7+1;
749 spa->address = t->dimm_dma[1];
750 spa->length = DIMM_SIZE;
751
752 /* spa8 (bdw for dcr2) dimm2 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500753 spa = nfit_buf + sizeof(*spa) * 8;
Dan Williams6bc75612015-06-17 17:23:32 -0400754 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
755 spa->header.length = sizeof(*spa);
756 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
757 spa->range_index = 8+1;
758 spa->address = t->dimm_dma[2];
759 spa->length = DIMM_SIZE;
760
761 /* spa9 (bdw for dcr3) dimm3 */
Linda Knippers6b577c92015-11-20 19:05:49 -0500762 spa = nfit_buf + sizeof(*spa) * 9;
Dan Williams6bc75612015-06-17 17:23:32 -0400763 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
764 spa->header.length = sizeof(*spa);
765 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
766 spa->range_index = 9+1;
767 spa->address = t->dimm_dma[3];
768 spa->length = DIMM_SIZE;
769
Linda Knippers6b577c92015-11-20 19:05:49 -0500770 offset = sizeof(*spa) * 10;
Dan Williams6bc75612015-06-17 17:23:32 -0400771 /* mem-region0 (spa0, dimm0) */
772 memdev = nfit_buf + offset;
773 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
774 memdev->header.length = sizeof(*memdev);
775 memdev->device_handle = handle[0];
776 memdev->physical_id = 0;
777 memdev->region_id = 0;
778 memdev->range_index = 0+1;
Dan Williams3b873562016-02-01 17:45:54 -0800779 memdev->region_index = 4+1;
Dan Williams6bc75612015-06-17 17:23:32 -0400780 memdev->region_size = SPA0_SIZE/2;
781 memdev->region_offset = t->spa_set_dma[0];
782 memdev->address = 0;
783 memdev->interleave_index = 0;
784 memdev->interleave_ways = 2;
785
786 /* mem-region1 (spa0, dimm1) */
787 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
788 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
789 memdev->header.length = sizeof(*memdev);
790 memdev->device_handle = handle[1];
791 memdev->physical_id = 1;
792 memdev->region_id = 0;
793 memdev->range_index = 0+1;
Dan Williams3b873562016-02-01 17:45:54 -0800794 memdev->region_index = 5+1;
Dan Williams6bc75612015-06-17 17:23:32 -0400795 memdev->region_size = SPA0_SIZE/2;
796 memdev->region_offset = t->spa_set_dma[0] + SPA0_SIZE/2;
797 memdev->address = 0;
798 memdev->interleave_index = 0;
799 memdev->interleave_ways = 2;
800
801 /* mem-region2 (spa1, dimm0) */
802 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
803 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
804 memdev->header.length = sizeof(*memdev);
805 memdev->device_handle = handle[0];
806 memdev->physical_id = 0;
807 memdev->region_id = 1;
808 memdev->range_index = 1+1;
Dan Williams3b873562016-02-01 17:45:54 -0800809 memdev->region_index = 4+1;
Dan Williams6bc75612015-06-17 17:23:32 -0400810 memdev->region_size = SPA1_SIZE/4;
811 memdev->region_offset = t->spa_set_dma[1];
812 memdev->address = SPA0_SIZE/2;
813 memdev->interleave_index = 0;
814 memdev->interleave_ways = 4;
815
816 /* mem-region3 (spa1, dimm1) */
817 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
818 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
819 memdev->header.length = sizeof(*memdev);
820 memdev->device_handle = handle[1];
821 memdev->physical_id = 1;
822 memdev->region_id = 1;
823 memdev->range_index = 1+1;
Dan Williams3b873562016-02-01 17:45:54 -0800824 memdev->region_index = 5+1;
Dan Williams6bc75612015-06-17 17:23:32 -0400825 memdev->region_size = SPA1_SIZE/4;
826 memdev->region_offset = t->spa_set_dma[1] + SPA1_SIZE/4;
827 memdev->address = SPA0_SIZE/2;
828 memdev->interleave_index = 0;
829 memdev->interleave_ways = 4;
830
831 /* mem-region4 (spa1, dimm2) */
832 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
833 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
834 memdev->header.length = sizeof(*memdev);
835 memdev->device_handle = handle[2];
836 memdev->physical_id = 2;
837 memdev->region_id = 0;
838 memdev->range_index = 1+1;
Dan Williams3b873562016-02-01 17:45:54 -0800839 memdev->region_index = 6+1;
Dan Williams6bc75612015-06-17 17:23:32 -0400840 memdev->region_size = SPA1_SIZE/4;
841 memdev->region_offset = t->spa_set_dma[1] + 2*SPA1_SIZE/4;
842 memdev->address = SPA0_SIZE/2;
843 memdev->interleave_index = 0;
844 memdev->interleave_ways = 4;
845
846 /* mem-region5 (spa1, dimm3) */
847 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
848 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
849 memdev->header.length = sizeof(*memdev);
850 memdev->device_handle = handle[3];
851 memdev->physical_id = 3;
852 memdev->region_id = 0;
853 memdev->range_index = 1+1;
Dan Williams3b873562016-02-01 17:45:54 -0800854 memdev->region_index = 7+1;
Dan Williams6bc75612015-06-17 17:23:32 -0400855 memdev->region_size = SPA1_SIZE/4;
856 memdev->region_offset = t->spa_set_dma[1] + 3*SPA1_SIZE/4;
857 memdev->address = SPA0_SIZE/2;
858 memdev->interleave_index = 0;
859 memdev->interleave_ways = 4;
860
861 /* mem-region6 (spa/dcr0, dimm0) */
862 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
863 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
864 memdev->header.length = sizeof(*memdev);
865 memdev->device_handle = handle[0];
866 memdev->physical_id = 0;
867 memdev->region_id = 0;
868 memdev->range_index = 2+1;
869 memdev->region_index = 0+1;
870 memdev->region_size = 0;
871 memdev->region_offset = 0;
872 memdev->address = 0;
873 memdev->interleave_index = 0;
874 memdev->interleave_ways = 1;
875
876 /* mem-region7 (spa/dcr1, dimm1) */
877 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
878 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
879 memdev->header.length = sizeof(*memdev);
880 memdev->device_handle = handle[1];
881 memdev->physical_id = 1;
882 memdev->region_id = 0;
883 memdev->range_index = 3+1;
884 memdev->region_index = 1+1;
885 memdev->region_size = 0;
886 memdev->region_offset = 0;
887 memdev->address = 0;
888 memdev->interleave_index = 0;
889 memdev->interleave_ways = 1;
890
891 /* mem-region8 (spa/dcr2, dimm2) */
892 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
893 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
894 memdev->header.length = sizeof(*memdev);
895 memdev->device_handle = handle[2];
896 memdev->physical_id = 2;
897 memdev->region_id = 0;
898 memdev->range_index = 4+1;
899 memdev->region_index = 2+1;
900 memdev->region_size = 0;
901 memdev->region_offset = 0;
902 memdev->address = 0;
903 memdev->interleave_index = 0;
904 memdev->interleave_ways = 1;
905
906 /* mem-region9 (spa/dcr3, dimm3) */
907 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
908 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
909 memdev->header.length = sizeof(*memdev);
910 memdev->device_handle = handle[3];
911 memdev->physical_id = 3;
912 memdev->region_id = 0;
913 memdev->range_index = 5+1;
914 memdev->region_index = 3+1;
915 memdev->region_size = 0;
916 memdev->region_offset = 0;
917 memdev->address = 0;
918 memdev->interleave_index = 0;
919 memdev->interleave_ways = 1;
920
921 /* mem-region10 (spa/bdw0, dimm0) */
922 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
923 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
924 memdev->header.length = sizeof(*memdev);
925 memdev->device_handle = handle[0];
926 memdev->physical_id = 0;
927 memdev->region_id = 0;
928 memdev->range_index = 6+1;
929 memdev->region_index = 0+1;
930 memdev->region_size = 0;
931 memdev->region_offset = 0;
932 memdev->address = 0;
933 memdev->interleave_index = 0;
934 memdev->interleave_ways = 1;
935
936 /* mem-region11 (spa/bdw1, dimm1) */
937 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
938 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
939 memdev->header.length = sizeof(*memdev);
940 memdev->device_handle = handle[1];
941 memdev->physical_id = 1;
942 memdev->region_id = 0;
943 memdev->range_index = 7+1;
944 memdev->region_index = 1+1;
945 memdev->region_size = 0;
946 memdev->region_offset = 0;
947 memdev->address = 0;
948 memdev->interleave_index = 0;
949 memdev->interleave_ways = 1;
950
951 /* mem-region12 (spa/bdw2, dimm2) */
952 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
953 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
954 memdev->header.length = sizeof(*memdev);
955 memdev->device_handle = handle[2];
956 memdev->physical_id = 2;
957 memdev->region_id = 0;
958 memdev->range_index = 8+1;
959 memdev->region_index = 2+1;
960 memdev->region_size = 0;
961 memdev->region_offset = 0;
962 memdev->address = 0;
963 memdev->interleave_index = 0;
964 memdev->interleave_ways = 1;
965
966 /* mem-region13 (spa/dcr3, dimm3) */
967 memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
968 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
969 memdev->header.length = sizeof(*memdev);
970 memdev->device_handle = handle[3];
971 memdev->physical_id = 3;
972 memdev->region_id = 0;
973 memdev->range_index = 9+1;
974 memdev->region_index = 3+1;
975 memdev->region_size = 0;
976 memdev->region_offset = 0;
977 memdev->address = 0;
978 memdev->interleave_index = 0;
979 memdev->interleave_ways = 1;
980
981 offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
Dan Williams3b873562016-02-01 17:45:54 -0800982 /* dcr-descriptor0: blk */
Dan Williams6bc75612015-06-17 17:23:32 -0400983 dcr = nfit_buf + offset;
984 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
985 dcr->header.length = sizeof(struct acpi_nfit_control_region);
986 dcr->region_index = 0+1;
Dan Williams5dc68e52016-07-15 11:27:03 -0700987 dcr_common_init(dcr);
Dan Williams6bc75612015-06-17 17:23:32 -0400988 dcr->serial_number = ~handle[0];
Dan Williamsbe26f9a2016-02-01 17:48:42 -0800989 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -0400990 dcr->windows = 1;
991 dcr->window_size = DCR_SIZE;
992 dcr->command_offset = 0;
993 dcr->command_size = 8;
994 dcr->status_offset = 8;
995 dcr->status_size = 4;
996
Dan Williams3b873562016-02-01 17:45:54 -0800997 /* dcr-descriptor1: blk */
Dan Williams6bc75612015-06-17 17:23:32 -0400998 dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
999 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1000 dcr->header.length = sizeof(struct acpi_nfit_control_region);
1001 dcr->region_index = 1+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001002 dcr_common_init(dcr);
Dan Williams6bc75612015-06-17 17:23:32 -04001003 dcr->serial_number = ~handle[1];
Dan Williamsbe26f9a2016-02-01 17:48:42 -08001004 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -04001005 dcr->windows = 1;
1006 dcr->window_size = DCR_SIZE;
1007 dcr->command_offset = 0;
1008 dcr->command_size = 8;
1009 dcr->status_offset = 8;
1010 dcr->status_size = 4;
1011
Dan Williams3b873562016-02-01 17:45:54 -08001012 /* dcr-descriptor2: blk */
Dan Williams6bc75612015-06-17 17:23:32 -04001013 dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
1014 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1015 dcr->header.length = sizeof(struct acpi_nfit_control_region);
1016 dcr->region_index = 2+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001017 dcr_common_init(dcr);
Dan Williams6bc75612015-06-17 17:23:32 -04001018 dcr->serial_number = ~handle[2];
Dan Williamsbe26f9a2016-02-01 17:48:42 -08001019 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -04001020 dcr->windows = 1;
1021 dcr->window_size = DCR_SIZE;
1022 dcr->command_offset = 0;
1023 dcr->command_size = 8;
1024 dcr->status_offset = 8;
1025 dcr->status_size = 4;
1026
Dan Williams3b873562016-02-01 17:45:54 -08001027 /* dcr-descriptor3: blk */
Dan Williams6bc75612015-06-17 17:23:32 -04001028 dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
1029 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1030 dcr->header.length = sizeof(struct acpi_nfit_control_region);
1031 dcr->region_index = 3+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001032 dcr_common_init(dcr);
Dan Williams6bc75612015-06-17 17:23:32 -04001033 dcr->serial_number = ~handle[3];
Dan Williamsbe26f9a2016-02-01 17:48:42 -08001034 dcr->code = NFIT_FIC_BLK;
Dan Williams6bc75612015-06-17 17:23:32 -04001035 dcr->windows = 1;
1036 dcr->window_size = DCR_SIZE;
1037 dcr->command_offset = 0;
1038 dcr->command_size = 8;
1039 dcr->status_offset = 8;
1040 dcr->status_size = 4;
1041
1042 offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
Dan Williams3b873562016-02-01 17:45:54 -08001043 /* dcr-descriptor0: pmem */
1044 dcr = nfit_buf + offset;
1045 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1046 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1047 window_size);
1048 dcr->region_index = 4+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001049 dcr_common_init(dcr);
Dan Williams3b873562016-02-01 17:45:54 -08001050 dcr->serial_number = ~handle[0];
1051 dcr->code = NFIT_FIC_BYTEN;
1052 dcr->windows = 0;
1053
1054 /* dcr-descriptor1: pmem */
1055 dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
1056 window_size);
1057 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1058 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1059 window_size);
1060 dcr->region_index = 5+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001061 dcr_common_init(dcr);
Dan Williams3b873562016-02-01 17:45:54 -08001062 dcr->serial_number = ~handle[1];
1063 dcr->code = NFIT_FIC_BYTEN;
1064 dcr->windows = 0;
1065
1066 /* dcr-descriptor2: pmem */
1067 dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
1068 window_size) * 2;
1069 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1070 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1071 window_size);
1072 dcr->region_index = 6+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001073 dcr_common_init(dcr);
Dan Williams3b873562016-02-01 17:45:54 -08001074 dcr->serial_number = ~handle[2];
1075 dcr->code = NFIT_FIC_BYTEN;
1076 dcr->windows = 0;
1077
1078 /* dcr-descriptor3: pmem */
1079 dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
1080 window_size) * 3;
1081 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1082 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1083 window_size);
1084 dcr->region_index = 7+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001085 dcr_common_init(dcr);
Dan Williams3b873562016-02-01 17:45:54 -08001086 dcr->serial_number = ~handle[3];
1087 dcr->code = NFIT_FIC_BYTEN;
1088 dcr->windows = 0;
1089
1090 offset = offset + offsetof(struct acpi_nfit_control_region,
1091 window_size) * 4;
Dan Williams6bc75612015-06-17 17:23:32 -04001092 /* bdw0 (spa/dcr0, dimm0) */
1093 bdw = nfit_buf + offset;
1094 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1095 bdw->header.length = sizeof(struct acpi_nfit_data_region);
1096 bdw->region_index = 0+1;
1097 bdw->windows = 1;
1098 bdw->offset = 0;
1099 bdw->size = BDW_SIZE;
1100 bdw->capacity = DIMM_SIZE;
1101 bdw->start_address = 0;
1102
1103 /* bdw1 (spa/dcr1, dimm1) */
1104 bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
1105 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1106 bdw->header.length = sizeof(struct acpi_nfit_data_region);
1107 bdw->region_index = 1+1;
1108 bdw->windows = 1;
1109 bdw->offset = 0;
1110 bdw->size = BDW_SIZE;
1111 bdw->capacity = DIMM_SIZE;
1112 bdw->start_address = 0;
1113
1114 /* bdw2 (spa/dcr2, dimm2) */
1115 bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
1116 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1117 bdw->header.length = sizeof(struct acpi_nfit_data_region);
1118 bdw->region_index = 2+1;
1119 bdw->windows = 1;
1120 bdw->offset = 0;
1121 bdw->size = BDW_SIZE;
1122 bdw->capacity = DIMM_SIZE;
1123 bdw->start_address = 0;
1124
1125 /* bdw3 (spa/dcr3, dimm3) */
1126 bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
1127 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1128 bdw->header.length = sizeof(struct acpi_nfit_data_region);
1129 bdw->region_index = 3+1;
1130 bdw->windows = 1;
1131 bdw->offset = 0;
1132 bdw->size = BDW_SIZE;
1133 bdw->capacity = DIMM_SIZE;
1134 bdw->start_address = 0;
1135
Dan Williams9d27a872015-07-10 14:07:03 -04001136 offset = offset + sizeof(struct acpi_nfit_data_region) * 4;
1137 /* flush0 (dimm0) */
1138 flush = nfit_buf + offset;
1139 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
Dan Williams85d3fa02016-06-02 16:27:21 -07001140 flush->header.length = flush_hint_size;
Dan Williams9d27a872015-07-10 14:07:03 -04001141 flush->device_handle = handle[0];
Dan Williams85d3fa02016-06-02 16:27:21 -07001142 flush->hint_count = NUM_HINTS;
1143 for (i = 0; i < NUM_HINTS; i++)
1144 flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
Dan Williams9d27a872015-07-10 14:07:03 -04001145
1146 /* flush1 (dimm1) */
Dan Williams85d3fa02016-06-02 16:27:21 -07001147 flush = nfit_buf + offset + flush_hint_size * 1;
Dan Williams9d27a872015-07-10 14:07:03 -04001148 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
Dan Williams85d3fa02016-06-02 16:27:21 -07001149 flush->header.length = flush_hint_size;
Dan Williams9d27a872015-07-10 14:07:03 -04001150 flush->device_handle = handle[1];
Dan Williams85d3fa02016-06-02 16:27:21 -07001151 flush->hint_count = NUM_HINTS;
1152 for (i = 0; i < NUM_HINTS; i++)
1153 flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
Dan Williams9d27a872015-07-10 14:07:03 -04001154
1155 /* flush2 (dimm2) */
Dan Williams85d3fa02016-06-02 16:27:21 -07001156 flush = nfit_buf + offset + flush_hint_size * 2;
Dan Williams9d27a872015-07-10 14:07:03 -04001157 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
Dan Williams85d3fa02016-06-02 16:27:21 -07001158 flush->header.length = flush_hint_size;
Dan Williams9d27a872015-07-10 14:07:03 -04001159 flush->device_handle = handle[2];
Dan Williams85d3fa02016-06-02 16:27:21 -07001160 flush->hint_count = NUM_HINTS;
1161 for (i = 0; i < NUM_HINTS; i++)
1162 flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
Dan Williams9d27a872015-07-10 14:07:03 -04001163
1164 /* flush3 (dimm3) */
Dan Williams85d3fa02016-06-02 16:27:21 -07001165 flush = nfit_buf + offset + flush_hint_size * 3;
Dan Williams9d27a872015-07-10 14:07:03 -04001166 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
Dan Williams85d3fa02016-06-02 16:27:21 -07001167 flush->header.length = flush_hint_size;
Dan Williams9d27a872015-07-10 14:07:03 -04001168 flush->device_handle = handle[3];
Dan Williams85d3fa02016-06-02 16:27:21 -07001169 flush->hint_count = NUM_HINTS;
1170 for (i = 0; i < NUM_HINTS; i++)
1171 flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
Dan Williams9d27a872015-07-10 14:07:03 -04001172
Vishal Verma20985162015-10-27 16:58:27 -06001173 if (t->setup_hotplug) {
Dan Williams85d3fa02016-06-02 16:27:21 -07001174 offset = offset + flush_hint_size * 4;
Dan Williams3b873562016-02-01 17:45:54 -08001175 /* dcr-descriptor4: blk */
Vishal Verma20985162015-10-27 16:58:27 -06001176 dcr = nfit_buf + offset;
1177 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1178 dcr->header.length = sizeof(struct acpi_nfit_control_region);
Dan Williams3b873562016-02-01 17:45:54 -08001179 dcr->region_index = 8+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001180 dcr_common_init(dcr);
Vishal Verma20985162015-10-27 16:58:27 -06001181 dcr->serial_number = ~handle[4];
Dan Williamsbe26f9a2016-02-01 17:48:42 -08001182 dcr->code = NFIT_FIC_BLK;
Vishal Verma20985162015-10-27 16:58:27 -06001183 dcr->windows = 1;
1184 dcr->window_size = DCR_SIZE;
1185 dcr->command_offset = 0;
1186 dcr->command_size = 8;
1187 dcr->status_offset = 8;
1188 dcr->status_size = 4;
1189
1190 offset = offset + sizeof(struct acpi_nfit_control_region);
Dan Williams3b873562016-02-01 17:45:54 -08001191 /* dcr-descriptor4: pmem */
1192 dcr = nfit_buf + offset;
1193 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1194 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1195 window_size);
1196 dcr->region_index = 9+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001197 dcr_common_init(dcr);
Dan Williams3b873562016-02-01 17:45:54 -08001198 dcr->serial_number = ~handle[4];
1199 dcr->code = NFIT_FIC_BYTEN;
1200 dcr->windows = 0;
1201
1202 offset = offset + offsetof(struct acpi_nfit_control_region,
1203 window_size);
Vishal Verma20985162015-10-27 16:58:27 -06001204 /* bdw4 (spa/dcr4, dimm4) */
1205 bdw = nfit_buf + offset;
1206 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1207 bdw->header.length = sizeof(struct acpi_nfit_data_region);
Dan Williams3b873562016-02-01 17:45:54 -08001208 bdw->region_index = 8+1;
Vishal Verma20985162015-10-27 16:58:27 -06001209 bdw->windows = 1;
1210 bdw->offset = 0;
1211 bdw->size = BDW_SIZE;
1212 bdw->capacity = DIMM_SIZE;
1213 bdw->start_address = 0;
1214
1215 offset = offset + sizeof(struct acpi_nfit_data_region);
1216 /* spa10 (dcr4) dimm4 */
1217 spa = nfit_buf + offset;
1218 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1219 spa->header.length = sizeof(*spa);
1220 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
1221 spa->range_index = 10+1;
1222 spa->address = t->dcr_dma[4];
1223 spa->length = DCR_SIZE;
1224
1225 /*
1226 * spa11 (single-dimm interleave for hotplug, note storage
1227 * does not actually alias the related block-data-window
1228 * regions)
1229 */
1230 spa = nfit_buf + offset + sizeof(*spa);
1231 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1232 spa->header.length = sizeof(*spa);
1233 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
1234 spa->range_index = 11+1;
1235 spa->address = t->spa_set_dma[2];
1236 spa->length = SPA0_SIZE;
1237
1238 /* spa12 (bdw for dcr4) dimm4 */
1239 spa = nfit_buf + offset + sizeof(*spa) * 2;
1240 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1241 spa->header.length = sizeof(*spa);
1242 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
1243 spa->range_index = 12+1;
1244 spa->address = t->dimm_dma[4];
1245 spa->length = DIMM_SIZE;
1246
1247 offset = offset + sizeof(*spa) * 3;
1248 /* mem-region14 (spa/dcr4, dimm4) */
1249 memdev = nfit_buf + offset;
1250 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1251 memdev->header.length = sizeof(*memdev);
1252 memdev->device_handle = handle[4];
1253 memdev->physical_id = 4;
1254 memdev->region_id = 0;
1255 memdev->range_index = 10+1;
Dan Williams3b873562016-02-01 17:45:54 -08001256 memdev->region_index = 8+1;
Vishal Verma20985162015-10-27 16:58:27 -06001257 memdev->region_size = 0;
1258 memdev->region_offset = 0;
1259 memdev->address = 0;
1260 memdev->interleave_index = 0;
1261 memdev->interleave_ways = 1;
1262
1263 /* mem-region15 (spa0, dimm4) */
1264 memdev = nfit_buf + offset +
1265 sizeof(struct acpi_nfit_memory_map);
1266 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1267 memdev->header.length = sizeof(*memdev);
1268 memdev->device_handle = handle[4];
1269 memdev->physical_id = 4;
1270 memdev->region_id = 0;
1271 memdev->range_index = 11+1;
Dan Williams3b873562016-02-01 17:45:54 -08001272 memdev->region_index = 9+1;
Vishal Verma20985162015-10-27 16:58:27 -06001273 memdev->region_size = SPA0_SIZE;
1274 memdev->region_offset = t->spa_set_dma[2];
1275 memdev->address = 0;
1276 memdev->interleave_index = 0;
1277 memdev->interleave_ways = 1;
1278
Dan Williams3b873562016-02-01 17:45:54 -08001279 /* mem-region16 (spa/bdw4, dimm4) */
Vishal Verma20985162015-10-27 16:58:27 -06001280 memdev = nfit_buf + offset +
1281 sizeof(struct acpi_nfit_memory_map) * 2;
1282 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1283 memdev->header.length = sizeof(*memdev);
1284 memdev->device_handle = handle[4];
1285 memdev->physical_id = 4;
1286 memdev->region_id = 0;
1287 memdev->range_index = 12+1;
Dan Williams3b873562016-02-01 17:45:54 -08001288 memdev->region_index = 8+1;
Vishal Verma20985162015-10-27 16:58:27 -06001289 memdev->region_size = 0;
1290 memdev->region_offset = 0;
1291 memdev->address = 0;
1292 memdev->interleave_index = 0;
1293 memdev->interleave_ways = 1;
1294
1295 offset = offset + sizeof(struct acpi_nfit_memory_map) * 3;
1296 /* flush3 (dimm4) */
1297 flush = nfit_buf + offset;
1298 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
Dan Williams85d3fa02016-06-02 16:27:21 -07001299 flush->header.length = flush_hint_size;
Vishal Verma20985162015-10-27 16:58:27 -06001300 flush->device_handle = handle[4];
Dan Williams85d3fa02016-06-02 16:27:21 -07001301 flush->hint_count = NUM_HINTS;
1302 for (i = 0; i < NUM_HINTS; i++)
1303 flush->hint_address[i] = t->flush_dma[4]
1304 + i * sizeof(u64);
Vishal Verma20985162015-10-27 16:58:27 -06001305 }
1306
Dan Williamsf471f1a2016-02-20 15:12:47 -08001307 post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA0_SIZE);
1308
Dan Williams6bc75612015-06-17 17:23:32 -04001309 acpi_desc = &t->acpi_desc;
Dan Williamse3654ec2016-04-28 16:17:07 -07001310 set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
1311 set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1312 set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
Dan Williams1f716d02016-05-18 10:06:59 -07001313 set_bit(ND_CMD_SMART, &acpi_desc->dimm_cmd_force_en);
Dan Williamse3654ec2016-04-28 16:17:07 -07001314 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1315 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1316 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1317 set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
Dan Williams1f716d02016-05-18 10:06:59 -07001318 set_bit(ND_CMD_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
Dan Williams6bc75612015-06-17 17:23:32 -04001319}
1320
1321static void nfit_test1_setup(struct nfit_test *t)
1322{
Linda Knippers6b577c92015-11-20 19:05:49 -05001323 size_t offset;
Dan Williams6bc75612015-06-17 17:23:32 -04001324 void *nfit_buf = t->nfit_buf;
1325 struct acpi_nfit_memory_map *memdev;
1326 struct acpi_nfit_control_region *dcr;
1327 struct acpi_nfit_system_address *spa;
Dan Williamsd26f73f2015-12-30 15:01:19 -08001328 struct acpi_nfit_desc *acpi_desc;
Dan Williams6bc75612015-06-17 17:23:32 -04001329
Linda Knippers6b577c92015-11-20 19:05:49 -05001330 offset = 0;
Dan Williams6bc75612015-06-17 17:23:32 -04001331 /* spa0 (flat range with no bdw aliasing) */
1332 spa = nfit_buf + offset;
1333 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1334 spa->header.length = sizeof(*spa);
1335 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
1336 spa->range_index = 0+1;
1337 spa->address = t->spa_set_dma[0];
1338 spa->length = SPA2_SIZE;
1339
Dan Williams7bfe97c2016-07-14 21:47:00 -07001340 /* virtual cd region */
1341 spa = nfit_buf + sizeof(*spa);
1342 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1343 spa->header.length = sizeof(*spa);
1344 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
1345 spa->range_index = 0;
1346 spa->address = t->spa_set_dma[1];
1347 spa->length = SPA_VCD_SIZE;
1348
1349 offset += sizeof(*spa) * 2;
Dan Williams6bc75612015-06-17 17:23:32 -04001350 /* mem-region0 (spa0, dimm0) */
1351 memdev = nfit_buf + offset;
1352 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1353 memdev->header.length = sizeof(*memdev);
1354 memdev->device_handle = 0;
1355 memdev->physical_id = 0;
1356 memdev->region_id = 0;
1357 memdev->range_index = 0+1;
1358 memdev->region_index = 0+1;
1359 memdev->region_size = SPA2_SIZE;
1360 memdev->region_offset = 0;
1361 memdev->address = 0;
1362 memdev->interleave_index = 0;
1363 memdev->interleave_ways = 1;
Dan Williams58138822015-06-23 20:08:34 -04001364 memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
1365 | ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
Dan Williamsf4295792015-11-10 15:50:33 -08001366 | ACPI_NFIT_MEM_NOT_ARMED;
Dan Williams6bc75612015-06-17 17:23:32 -04001367
1368 offset += sizeof(*memdev);
1369 /* dcr-descriptor0 */
1370 dcr = nfit_buf + offset;
1371 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
Dan Williams3b873562016-02-01 17:45:54 -08001372 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1373 window_size);
Dan Williams6bc75612015-06-17 17:23:32 -04001374 dcr->region_index = 0+1;
Dan Williams5dc68e52016-07-15 11:27:03 -07001375 dcr_common_init(dcr);
Dan Williams6bc75612015-06-17 17:23:32 -04001376 dcr->serial_number = ~0;
Dan Williamsbe26f9a2016-02-01 17:48:42 -08001377 dcr->code = NFIT_FIC_BYTE;
Dan Williams6bc75612015-06-17 17:23:32 -04001378 dcr->windows = 0;
Dan Williamsd26f73f2015-12-30 15:01:19 -08001379
Dan Williamsf471f1a2016-02-20 15:12:47 -08001380 post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA2_SIZE);
1381
Dan Williamsd26f73f2015-12-30 15:01:19 -08001382 acpi_desc = &t->acpi_desc;
Dan Williamse3654ec2016-04-28 16:17:07 -07001383 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1384 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1385 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1386 set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
Dan Williams6bc75612015-06-17 17:23:32 -04001387}
1388
1389static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
1390 void *iobuf, u64 len, int rw)
1391{
1392 struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
1393 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1394 struct nd_region *nd_region = &ndbr->nd_region;
1395 unsigned int lane;
1396
1397 lane = nd_region_acquire_lane(nd_region);
1398 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001399 memcpy(mmio->addr.base + dpa, iobuf, len);
1400 else {
1401 memcpy(iobuf, mmio->addr.base + dpa, len);
1402
1403 /* give us some some coverage of the mmio_flush_range() API */
1404 mmio_flush_range(mmio->addr.base + dpa, len);
1405 }
Dan Williams6bc75612015-06-17 17:23:32 -04001406 nd_region_release_lane(nd_region, lane);
1407
1408 return 0;
1409}
1410
1411static int nfit_test_probe(struct platform_device *pdev)
1412{
1413 struct nvdimm_bus_descriptor *nd_desc;
1414 struct acpi_nfit_desc *acpi_desc;
1415 struct device *dev = &pdev->dev;
1416 struct nfit_test *nfit_test;
Dan Williamsc14a8682016-08-18 22:15:04 -07001417 union acpi_object *obj;
Dan Williams6bc75612015-06-17 17:23:32 -04001418 int rc;
1419
1420 nfit_test = to_nfit_test(&pdev->dev);
1421
1422 /* common alloc */
1423 if (nfit_test->num_dcr) {
1424 int num = nfit_test->num_dcr;
1425
1426 nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
1427 GFP_KERNEL);
1428 nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
1429 GFP_KERNEL);
Dan Williams9d27a872015-07-10 14:07:03 -04001430 nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
1431 GFP_KERNEL);
1432 nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
1433 GFP_KERNEL);
Dan Williams6bc75612015-06-17 17:23:32 -04001434 nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
1435 GFP_KERNEL);
1436 nfit_test->label_dma = devm_kcalloc(dev, num,
1437 sizeof(dma_addr_t), GFP_KERNEL);
1438 nfit_test->dcr = devm_kcalloc(dev, num,
1439 sizeof(struct nfit_test_dcr *), GFP_KERNEL);
1440 nfit_test->dcr_dma = devm_kcalloc(dev, num,
1441 sizeof(dma_addr_t), GFP_KERNEL);
1442 if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
1443 && nfit_test->label_dma && nfit_test->dcr
Dan Williams9d27a872015-07-10 14:07:03 -04001444 && nfit_test->dcr_dma && nfit_test->flush
1445 && nfit_test->flush_dma)
Dan Williams6bc75612015-06-17 17:23:32 -04001446 /* pass */;
1447 else
1448 return -ENOMEM;
1449 }
1450
1451 if (nfit_test->num_pm) {
1452 int num = nfit_test->num_pm;
1453
1454 nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
1455 GFP_KERNEL);
1456 nfit_test->spa_set_dma = devm_kcalloc(dev, num,
1457 sizeof(dma_addr_t), GFP_KERNEL);
1458 if (nfit_test->spa_set && nfit_test->spa_set_dma)
1459 /* pass */;
1460 else
1461 return -ENOMEM;
1462 }
1463
1464 /* per-nfit specific alloc */
1465 if (nfit_test->alloc(nfit_test))
1466 return -ENOMEM;
1467
1468 nfit_test->setup(nfit_test);
1469 acpi_desc = &nfit_test->acpi_desc;
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001470 acpi_nfit_desc_init(acpi_desc, &pdev->dev);
Dan Williams6bc75612015-06-17 17:23:32 -04001471 acpi_desc->blk_do_io = nfit_test_blk_do_io;
1472 nd_desc = &acpi_desc->nd_desc;
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001473 nd_desc->provider_name = NULL;
Dan Williamsbc9775d2016-07-21 20:03:19 -07001474 nd_desc->module = THIS_MODULE;
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001475 nd_desc->ndctl = nfit_test_ctl;
Dan Williams6bc75612015-06-17 17:23:32 -04001476
Dan Williamse7a11b42016-07-14 16:19:55 -07001477 rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
1478 nfit_test->nfit_size);
Dan Williams58cd71b2016-07-21 18:05:36 -07001479 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06001480 return rc;
Vishal Verma20985162015-10-27 16:58:27 -06001481
1482 if (nfit_test->setup != nfit_test0_setup)
1483 return 0;
1484
1485 nfit_test->setup_hotplug = 1;
1486 nfit_test->setup(nfit_test);
1487
Dan Williamsc14a8682016-08-18 22:15:04 -07001488 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
1489 if (!obj)
1490 return -ENOMEM;
1491 obj->type = ACPI_TYPE_BUFFER;
1492 obj->buffer.length = nfit_test->nfit_size;
1493 obj->buffer.pointer = nfit_test->nfit_buf;
1494 *(nfit_test->_fit) = obj;
1495 __acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
Dan Williams6bc75612015-06-17 17:23:32 -04001496 return 0;
1497}
1498
1499static int nfit_test_remove(struct platform_device *pdev)
1500{
Dan Williams6bc75612015-06-17 17:23:32 -04001501 return 0;
1502}
1503
1504static void nfit_test_release(struct device *dev)
1505{
1506 struct nfit_test *nfit_test = to_nfit_test(dev);
1507
1508 kfree(nfit_test);
1509}
1510
1511static const struct platform_device_id nfit_test_id[] = {
1512 { KBUILD_MODNAME },
1513 { },
1514};
1515
1516static struct platform_driver nfit_test_driver = {
1517 .probe = nfit_test_probe,
1518 .remove = nfit_test_remove,
1519 .driver = {
1520 .name = KBUILD_MODNAME,
1521 },
1522 .id_table = nfit_test_id,
1523};
1524
Dan Williams6bc75612015-06-17 17:23:32 -04001525static __init int nfit_test_init(void)
1526{
1527 int rc, i;
1528
1529 nfit_test_setup(nfit_test_lookup);
1530
1531 for (i = 0; i < NUM_NFITS; i++) {
1532 struct nfit_test *nfit_test;
1533 struct platform_device *pdev;
Dan Williams6bc75612015-06-17 17:23:32 -04001534
1535 nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
1536 if (!nfit_test) {
1537 rc = -ENOMEM;
1538 goto err_register;
1539 }
1540 INIT_LIST_HEAD(&nfit_test->resources);
1541 switch (i) {
1542 case 0:
1543 nfit_test->num_pm = NUM_PM;
1544 nfit_test->num_dcr = NUM_DCR;
1545 nfit_test->alloc = nfit_test0_alloc;
1546 nfit_test->setup = nfit_test0_setup;
1547 break;
1548 case 1:
1549 nfit_test->num_pm = 1;
1550 nfit_test->alloc = nfit_test1_alloc;
1551 nfit_test->setup = nfit_test1_setup;
1552 break;
1553 default:
1554 rc = -EINVAL;
1555 goto err_register;
1556 }
1557 pdev = &nfit_test->pdev;
1558 pdev->name = KBUILD_MODNAME;
1559 pdev->id = i;
1560 pdev->dev.release = nfit_test_release;
1561 rc = platform_device_register(pdev);
1562 if (rc) {
1563 put_device(&pdev->dev);
1564 goto err_register;
1565 }
1566
1567 rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1568 if (rc)
1569 goto err_register;
1570
1571 instances[i] = nfit_test;
Dan Williams6bc75612015-06-17 17:23:32 -04001572 }
1573
1574 rc = platform_driver_register(&nfit_test_driver);
1575 if (rc)
1576 goto err_register;
1577 return 0;
1578
1579 err_register:
1580 for (i = 0; i < NUM_NFITS; i++)
1581 if (instances[i])
1582 platform_device_unregister(&instances[i]->pdev);
1583 nfit_test_teardown();
1584 return rc;
1585}
1586
1587static __exit void nfit_test_exit(void)
1588{
1589 int i;
1590
1591 platform_driver_unregister(&nfit_test_driver);
1592 for (i = 0; i < NUM_NFITS; i++)
1593 platform_device_unregister(&instances[i]->pdev);
1594 nfit_test_teardown();
1595}
1596
1597module_init(nfit_test_init);
1598module_exit(nfit_test_exit);
1599MODULE_LICENSE("GPL v2");
1600MODULE_AUTHOR("Intel Corporation");