xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 85d3fa02e4f8b4f59b9ae3f6f7d8312a1dcf28c8)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
166bc75619SDan Williams #include <linux/libnvdimm.h>
176bc75619SDan Williams #include <linux/vmalloc.h>
186bc75619SDan Williams #include <linux/device.h>
196bc75619SDan Williams #include <linux/module.h>
2020985164SVishal Verma #include <linux/mutex.h>
216bc75619SDan Williams #include <linux/ndctl.h>
226bc75619SDan Williams #include <linux/sizes.h>
2320985164SVishal Verma #include <linux/list.h>
246bc75619SDan Williams #include <linux/slab.h>
256bc75619SDan Williams #include <nfit.h>
266bc75619SDan Williams #include <nd.h>
276bc75619SDan Williams #include "nfit_test.h"
286bc75619SDan Williams 
296bc75619SDan Williams /*
306bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
316bc75619SDan Williams  *
326bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
336bc75619SDan Williams  *
346bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
356bc75619SDan Williams  *           +----------+--------------+----------+---------+
366bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
376bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
386bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
396bc75619SDan Williams  *    |      +----------+--------------v----------v         v
406bc75619SDan Williams  * +--+---+                            |                    |
416bc75619SDan Williams  * | cpu0 |                                    region1
426bc75619SDan Williams  * +--+---+                            |                    |
436bc75619SDan Williams  *    |      +-------------------------^----------^         ^
446bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
456bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
466bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
476bc75619SDan Williams  *           +-------------------------+----------+-+-------+
486bc75619SDan Williams  *
4920985164SVishal Verma  * +--+---+
5020985164SVishal Verma  * | cpu1 |
5120985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5220985164SVishal Verma  *    |      +----------------------------------------------+
5320985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5420985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5520985164SVishal Verma  * +------+
5620985164SVishal Verma  *
5720985164SVishal Verma  *
586bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
596bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
606bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
616bc75619SDan Williams  *
626bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
636bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
646bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
656bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
666bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
676bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
686bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
696bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
706bc75619SDan Williams  *    names that can be assigned to a namespace.
716bc75619SDan Williams  *
726bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
736bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
746bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
756bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
766bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
776bc75619SDan Williams  *    "blk5.0".
786bc75619SDan Williams  *
796bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
806bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
816bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
826bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
836bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
846bc75619SDan Williams  *
856bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
866bc75619SDan Williams  *
876bc75619SDan Williams  *  region2
886bc75619SDan Williams  * +---------------------+
896bc75619SDan Williams  * |---------------------|
906bc75619SDan Williams  * ||       pm2.0       ||
916bc75619SDan Williams  * |---------------------|
926bc75619SDan Williams  * +---------------------+
936bc75619SDan Williams  *
946bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
956bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
966bc75619SDan Williams  *    reference an NVDIMM.
976bc75619SDan Williams  */
986bc75619SDan Williams enum {
9920985164SVishal Verma 	NUM_PM  = 3,
10020985164SVishal Verma 	NUM_DCR = 5,
101*85d3fa02SDan Williams 	NUM_HINTS = 8,
1026bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1036bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1046bc75619SDan Williams 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
1056bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1066bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1076bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1086bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1096bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1106bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1116bc75619SDan Williams 	DCR_SIZE = 12,
1126bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1136bc75619SDan Williams };
1146bc75619SDan Williams 
1156bc75619SDan Williams struct nfit_test_dcr {
1166bc75619SDan Williams 	__le64 bdw_addr;
1176bc75619SDan Williams 	__le32 bdw_status;
1186bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1196bc75619SDan Williams };
1206bc75619SDan Williams 
1216bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1226bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1236bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1246bc75619SDan Williams 
1256bc75619SDan Williams static u32 handle[NUM_DCR] = {
1266bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1276bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1286bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1296bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13020985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
1316bc75619SDan Williams };
1326bc75619SDan Williams 
1336bc75619SDan Williams struct nfit_test {
1346bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1356bc75619SDan Williams 	struct platform_device pdev;
1366bc75619SDan Williams 	struct list_head resources;
1376bc75619SDan Williams 	void *nfit_buf;
1386bc75619SDan Williams 	dma_addr_t nfit_dma;
1396bc75619SDan Williams 	size_t nfit_size;
1406bc75619SDan Williams 	int num_dcr;
1416bc75619SDan Williams 	int num_pm;
1426bc75619SDan Williams 	void **dimm;
1436bc75619SDan Williams 	dma_addr_t *dimm_dma;
1449d27a87eSDan Williams 	void **flush;
1459d27a87eSDan Williams 	dma_addr_t *flush_dma;
1466bc75619SDan Williams 	void **label;
1476bc75619SDan Williams 	dma_addr_t *label_dma;
1486bc75619SDan Williams 	void **spa_set;
1496bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1506bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1516bc75619SDan Williams 	dma_addr_t *dcr_dma;
1526bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1536bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
15420985164SVishal Verma 	int setup_hotplug;
155f471f1a7SDan Williams 	struct ars_state {
156f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
157f471f1a7SDan Williams 		unsigned long deadline;
158f471f1a7SDan Williams 		spinlock_t lock;
159f471f1a7SDan Williams 	} ars_state;
1606bc75619SDan Williams };
1616bc75619SDan Williams 
1626bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1636bc75619SDan Williams {
1646bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1656bc75619SDan Williams 
1666bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1676bc75619SDan Williams }
1686bc75619SDan Williams 
16939c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
1706bc75619SDan Williams 		unsigned int buf_len)
1716bc75619SDan Williams {
1726bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
1736bc75619SDan Williams 		return -EINVAL;
17439c686b8SVishal Verma 
1756bc75619SDan Williams 	nd_cmd->status = 0;
1766bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
1776bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
17839c686b8SVishal Verma 
17939c686b8SVishal Verma 	return 0;
1806bc75619SDan Williams }
18139c686b8SVishal Verma 
18239c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
18339c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
18439c686b8SVishal Verma {
1856bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
18639c686b8SVishal Verma 	int rc;
1876bc75619SDan Williams 
1886bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
1896bc75619SDan Williams 		return -EINVAL;
1906bc75619SDan Williams 	if (offset >= LABEL_SIZE)
1916bc75619SDan Williams 		return -EINVAL;
1926bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
1936bc75619SDan Williams 		return -EINVAL;
1946bc75619SDan Williams 
1956bc75619SDan Williams 	nd_cmd->status = 0;
1966bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
19739c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
1986bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
19939c686b8SVishal Verma 
20039c686b8SVishal Verma 	return rc;
2016bc75619SDan Williams }
20239c686b8SVishal Verma 
20339c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
20439c686b8SVishal Verma 		unsigned int buf_len, void *label)
20539c686b8SVishal Verma {
2066bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
2076bc75619SDan Williams 	u32 *status;
20839c686b8SVishal Verma 	int rc;
2096bc75619SDan Williams 
2106bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
2116bc75619SDan Williams 		return -EINVAL;
2126bc75619SDan Williams 	if (offset >= LABEL_SIZE)
2136bc75619SDan Williams 		return -EINVAL;
2146bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
2156bc75619SDan Williams 		return -EINVAL;
2166bc75619SDan Williams 
21739c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
2186bc75619SDan Williams 	*status = 0;
2196bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
22039c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
2216bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
22239c686b8SVishal Verma 
22339c686b8SVishal Verma 	return rc;
2246bc75619SDan Williams }
22539c686b8SVishal Verma 
226747ffe11SDan Williams #define NFIT_TEST_ARS_RECORDS 4
227d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
228747ffe11SDan Williams 
22939c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
23039c686b8SVishal Verma 		unsigned int buf_len)
23139c686b8SVishal Verma {
23239c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
23339c686b8SVishal Verma 		return -EINVAL;
23439c686b8SVishal Verma 
235747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
236747ffe11SDan Williams 		+ NFIT_TEST_ARS_RECORDS * sizeof(struct nd_ars_record);
23739c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
238d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
23939c686b8SVishal Verma 
24039c686b8SVishal Verma 	return 0;
24139c686b8SVishal Verma }
24239c686b8SVishal Verma 
243f471f1a7SDan Williams /*
244f471f1a7SDan Williams  * Initialize the ars_state to return an ars_result 1 second in the future with
245f471f1a7SDan Williams  * a 4K error range in the middle of the requested address range.
246f471f1a7SDan Williams  */
247f471f1a7SDan Williams static void post_ars_status(struct ars_state *ars_state, u64 addr, u64 len)
24839c686b8SVishal Verma {
249f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
250f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
251f471f1a7SDan Williams 
252f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
253f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
254f471f1a7SDan Williams 	ars_status->status = 0;
255f471f1a7SDan Williams 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
256f471f1a7SDan Williams 		+ sizeof(struct nd_ars_record);
257f471f1a7SDan Williams 	ars_status->address = addr;
258f471f1a7SDan Williams 	ars_status->length = len;
259f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
260f471f1a7SDan Williams 	ars_status->num_records = 1;
261f471f1a7SDan Williams 	ars_record = &ars_status->records[0];
262f471f1a7SDan Williams 	ars_record->handle = 0;
263f471f1a7SDan Williams 	ars_record->err_address = addr + len / 2;
264f471f1a7SDan Williams 	ars_record->length = SZ_4K;
265f471f1a7SDan Williams }
266f471f1a7SDan Williams 
267f471f1a7SDan Williams static int nfit_test_cmd_ars_start(struct ars_state *ars_state,
268f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
269f471f1a7SDan Williams 		int *cmd_rc)
270f471f1a7SDan Williams {
271f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
27239c686b8SVishal Verma 		return -EINVAL;
27339c686b8SVishal Verma 
274f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
275f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
276f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
277f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
278f471f1a7SDan Williams 	} else {
279f471f1a7SDan Williams 		ars_start->status = 0;
280f471f1a7SDan Williams 		ars_start->scrub_time = 1;
281f471f1a7SDan Williams 		post_ars_status(ars_state, ars_start->address,
282f471f1a7SDan Williams 				ars_start->length);
283f471f1a7SDan Williams 		*cmd_rc = 0;
284f471f1a7SDan Williams 	}
285f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
28639c686b8SVishal Verma 
28739c686b8SVishal Verma 	return 0;
28839c686b8SVishal Verma }
28939c686b8SVishal Verma 
290f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
291f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
292f471f1a7SDan Williams 		int *cmd_rc)
29339c686b8SVishal Verma {
294f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
29539c686b8SVishal Verma 		return -EINVAL;
29639c686b8SVishal Verma 
297f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
298f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
299f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
300f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
301f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
302f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
303f471f1a7SDan Williams 	} else {
304f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
305f471f1a7SDan Williams 				ars_state->ars_status->out_length);
306f471f1a7SDan Williams 		*cmd_rc = 0;
307f471f1a7SDan Williams 	}
308f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
30939c686b8SVishal Verma 	return 0;
31039c686b8SVishal Verma }
31139c686b8SVishal Verma 
312d4f32367SDan Williams static int nfit_test_cmd_clear_error(struct nd_cmd_clear_error *clear_err,
313d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
314d4f32367SDan Williams {
315d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
316d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
317d4f32367SDan Williams 		return -EINVAL;
318d4f32367SDan Williams 
319d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
320d4f32367SDan Williams 		return -EINVAL;
321d4f32367SDan Williams 
322d4f32367SDan Williams 	/*
323d4f32367SDan Williams 	 * Report 'all clear' success for all commands even though a new
324d4f32367SDan Williams 	 * scrub will find errors again.  This is enough to have the
325d4f32367SDan Williams 	 * error removed from the 'badblocks' tracking in the pmem
326d4f32367SDan Williams 	 * driver.
327d4f32367SDan Williams 	 */
328d4f32367SDan Williams 	clear_err->status = 0;
329d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
330d4f32367SDan Williams 	*cmd_rc = 0;
331d4f32367SDan Williams 	return 0;
332d4f32367SDan Williams }
333d4f32367SDan Williams 
334baa51277SDan Williams static int nfit_test_cmd_smart(struct nd_cmd_smart *smart, unsigned int buf_len)
335baa51277SDan Williams {
336baa51277SDan Williams 	static const struct nd_smart_payload smart_data = {
337baa51277SDan Williams 		.flags = ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID
338baa51277SDan Williams 			| ND_SMART_SPARES_VALID | ND_SMART_ALARM_VALID
339baa51277SDan Williams 			| ND_SMART_USED_VALID | ND_SMART_SHUTDOWN_VALID,
340baa51277SDan Williams 		.health = ND_SMART_NON_CRITICAL_HEALTH,
341baa51277SDan Williams 		.temperature = 23 * 16,
342baa51277SDan Williams 		.spares = 75,
343baa51277SDan Williams 		.alarm_flags = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
344baa51277SDan Williams 		.life_used = 5,
345baa51277SDan Williams 		.shutdown_state = 0,
346baa51277SDan Williams 		.vendor_size = 0,
347baa51277SDan Williams 	};
348baa51277SDan Williams 
349baa51277SDan Williams 	if (buf_len < sizeof(*smart))
350baa51277SDan Williams 		return -EINVAL;
351baa51277SDan Williams 	memcpy(smart->data, &smart_data, sizeof(smart_data));
352baa51277SDan Williams 	return 0;
353baa51277SDan Williams }
354baa51277SDan Williams 
355baa51277SDan Williams static int nfit_test_cmd_smart_threshold(struct nd_cmd_smart_threshold *smart_t,
356baa51277SDan Williams 		unsigned int buf_len)
357baa51277SDan Williams {
358baa51277SDan Williams 	static const struct nd_smart_threshold_payload smart_t_data = {
359baa51277SDan Williams 		.alarm_control = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
360baa51277SDan Williams 		.temperature = 40 * 16,
361baa51277SDan Williams 		.spares = 5,
362baa51277SDan Williams 	};
363baa51277SDan Williams 
364baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
365baa51277SDan Williams 		return -EINVAL;
366baa51277SDan Williams 	memcpy(smart_t->data, &smart_t_data, sizeof(smart_t_data));
367baa51277SDan Williams 	return 0;
368baa51277SDan Williams }
369baa51277SDan Williams 
37039c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
37139c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
372aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
37339c686b8SVishal Verma {
37439c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
37539c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
3766634fb06SDan Williams 	unsigned int func = cmd;
377f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
378f471f1a7SDan Williams 
379f471f1a7SDan Williams 	if (!cmd_rc)
380f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
381f471f1a7SDan Williams 	*cmd_rc = 0;
38239c686b8SVishal Verma 
38339c686b8SVishal Verma 	if (nvdimm) {
38439c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
385e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
38639c686b8SVishal Verma 
3876634fb06SDan Williams 		if (!nfit_mem)
3886634fb06SDan Williams 			return -ENOTTY;
3896634fb06SDan Williams 
3906634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
3916634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
3926634fb06SDan Williams 
3936634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
3946634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
3956634fb06SDan Williams 			func = call_pkg->nd_command;
3966634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
3976634fb06SDan Williams 				return -ENOTTY;
3986634fb06SDan Williams 		}
3996634fb06SDan Williams 
4006634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
4016634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
40239c686b8SVishal Verma 			return -ENOTTY;
40339c686b8SVishal Verma 
40439c686b8SVishal Verma 		/* lookup label space for the given dimm */
40539c686b8SVishal Verma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
40639c686b8SVishal Verma 			if (__to_nfit_memdev(nfit_mem)->device_handle ==
40739c686b8SVishal Verma 					handle[i])
40839c686b8SVishal Verma 				break;
40939c686b8SVishal Verma 		if (i >= ARRAY_SIZE(handle))
41039c686b8SVishal Verma 			return -ENXIO;
41139c686b8SVishal Verma 
4126634fb06SDan Williams 		switch (func) {
41339c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
41439c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
41539c686b8SVishal Verma 			break;
41639c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
41739c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
41839c686b8SVishal Verma 				t->label[i]);
41939c686b8SVishal Verma 			break;
42039c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
42139c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
42239c686b8SVishal Verma 				t->label[i]);
42339c686b8SVishal Verma 			break;
424baa51277SDan Williams 		case ND_CMD_SMART:
425baa51277SDan Williams 			rc = nfit_test_cmd_smart(buf, buf_len);
426baa51277SDan Williams 			break;
427baa51277SDan Williams 		case ND_CMD_SMART_THRESHOLD:
428baa51277SDan Williams 			rc = nfit_test_cmd_smart_threshold(buf, buf_len);
429baa51277SDan Williams 			break;
4306bc75619SDan Williams 		default:
4316bc75619SDan Williams 			return -ENOTTY;
4326bc75619SDan Williams 		}
43339c686b8SVishal Verma 	} else {
434f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
435f471f1a7SDan Williams 
436e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
43739c686b8SVishal Verma 			return -ENOTTY;
43839c686b8SVishal Verma 
4396634fb06SDan Williams 		switch (func) {
44039c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
44139c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
44239c686b8SVishal Verma 			break;
44339c686b8SVishal Verma 		case ND_CMD_ARS_START:
444f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_start(ars_state, buf, buf_len,
445f471f1a7SDan Williams 					cmd_rc);
44639c686b8SVishal Verma 			break;
44739c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
448f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
449f471f1a7SDan Williams 					cmd_rc);
45039c686b8SVishal Verma 			break;
451d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
452d4f32367SDan Williams 			rc = nfit_test_cmd_clear_error(buf, buf_len, cmd_rc);
453d4f32367SDan Williams 			break;
45439c686b8SVishal Verma 		default:
45539c686b8SVishal Verma 			return -ENOTTY;
45639c686b8SVishal Verma 		}
45739c686b8SVishal Verma 	}
4586bc75619SDan Williams 
4596bc75619SDan Williams 	return rc;
4606bc75619SDan Williams }
4616bc75619SDan Williams 
4626bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
4636bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
4646bc75619SDan Williams 
4656bc75619SDan Williams static void release_nfit_res(void *data)
4666bc75619SDan Williams {
4676bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
4686bc75619SDan Williams 	struct resource *res = nfit_res->res;
4696bc75619SDan Williams 
4706bc75619SDan Williams 	spin_lock(&nfit_test_lock);
4716bc75619SDan Williams 	list_del(&nfit_res->list);
4726bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
4736bc75619SDan Williams 
4746bc75619SDan Williams 	vfree(nfit_res->buf);
4756bc75619SDan Williams 	kfree(res);
4766bc75619SDan Williams 	kfree(nfit_res);
4776bc75619SDan Williams }
4786bc75619SDan Williams 
4796bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
4806bc75619SDan Williams 		void *buf)
4816bc75619SDan Williams {
4826bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
4836bc75619SDan Williams 	struct resource *res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
4846bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
4856bc75619SDan Williams 			GFP_KERNEL);
4866bc75619SDan Williams 	int rc;
4876bc75619SDan Williams 
4886bc75619SDan Williams 	if (!res || !buf || !nfit_res)
4896bc75619SDan Williams 		goto err;
4906bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
4916bc75619SDan Williams 	if (rc)
4926bc75619SDan Williams 		goto err;
4936bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
4946bc75619SDan Williams 	memset(buf, 0, size);
4956bc75619SDan Williams 	nfit_res->dev = dev;
4966bc75619SDan Williams 	nfit_res->buf = buf;
4976bc75619SDan Williams 	nfit_res->res = res;
4986bc75619SDan Williams 	res->start = *dma;
4996bc75619SDan Williams 	res->end = *dma + size - 1;
5006bc75619SDan Williams 	res->name = "NFIT";
5016bc75619SDan Williams 	spin_lock(&nfit_test_lock);
5026bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
5036bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
5046bc75619SDan Williams 
5056bc75619SDan Williams 	return nfit_res->buf;
5066bc75619SDan Williams  err:
507ee8520feSDan Williams 	if (buf)
5086bc75619SDan Williams 		vfree(buf);
5096bc75619SDan Williams 	kfree(res);
5106bc75619SDan Williams 	kfree(nfit_res);
5116bc75619SDan Williams 	return NULL;
5126bc75619SDan Williams }
5136bc75619SDan Williams 
5146bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
5156bc75619SDan Williams {
5166bc75619SDan Williams 	void *buf = vmalloc(size);
5176bc75619SDan Williams 
5186bc75619SDan Williams 	*dma = (unsigned long) buf;
5196bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
5206bc75619SDan Williams }
5216bc75619SDan Williams 
5226bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
5236bc75619SDan Williams {
5246bc75619SDan Williams 	int i;
5256bc75619SDan Williams 
5266bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
5276bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
5286bc75619SDan Williams 		struct nfit_test *t = instances[i];
5296bc75619SDan Williams 
5306bc75619SDan Williams 		if (!t)
5316bc75619SDan Williams 			continue;
5326bc75619SDan Williams 		spin_lock(&nfit_test_lock);
5336bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
5346bc75619SDan Williams 			if (addr >= n->res->start && (addr < n->res->start
5356bc75619SDan Williams 						+ resource_size(n->res))) {
5366bc75619SDan Williams 				nfit_res = n;
5376bc75619SDan Williams 				break;
5386bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
5396bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
5406bc75619SDan Williams 						+ resource_size(n->res))) {
5416bc75619SDan Williams 				nfit_res = n;
5426bc75619SDan Williams 				break;
5436bc75619SDan Williams 			}
5446bc75619SDan Williams 		}
5456bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
5466bc75619SDan Williams 		if (nfit_res)
5476bc75619SDan Williams 			return nfit_res;
5486bc75619SDan Williams 	}
5496bc75619SDan Williams 
5506bc75619SDan Williams 	return NULL;
5516bc75619SDan Williams }
5526bc75619SDan Williams 
553f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
554f471f1a7SDan Williams {
555f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
556f471f1a7SDan Williams 			sizeof(struct nd_cmd_ars_status)
557f471f1a7SDan Williams 			+ sizeof(struct nd_ars_record) * NFIT_TEST_ARS_RECORDS,
558f471f1a7SDan Williams 			GFP_KERNEL);
559f471f1a7SDan Williams 	if (!ars_state->ars_status)
560f471f1a7SDan Williams 		return -ENOMEM;
561f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
562f471f1a7SDan Williams 	return 0;
563f471f1a7SDan Williams }
564f471f1a7SDan Williams 
5656bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
5666bc75619SDan Williams {
5676b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
5686bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
5696bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
5703b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
5713b87356fSDan Williams 					window_size) * NUM_DCR
5729d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
573*85d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
574*85d3fa02SDan Williams 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR;
5756bc75619SDan Williams 	int i;
5766bc75619SDan Williams 
5776bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
5786bc75619SDan Williams 	if (!t->nfit_buf)
5796bc75619SDan Williams 		return -ENOMEM;
5806bc75619SDan Williams 	t->nfit_size = nfit_size;
5816bc75619SDan Williams 
582ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
5836bc75619SDan Williams 	if (!t->spa_set[0])
5846bc75619SDan Williams 		return -ENOMEM;
5856bc75619SDan Williams 
586ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
5876bc75619SDan Williams 	if (!t->spa_set[1])
5886bc75619SDan Williams 		return -ENOMEM;
5896bc75619SDan Williams 
590ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
59120985164SVishal Verma 	if (!t->spa_set[2])
59220985164SVishal Verma 		return -ENOMEM;
59320985164SVishal Verma 
5946bc75619SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
5956bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
5966bc75619SDan Williams 		if (!t->dimm[i])
5976bc75619SDan Williams 			return -ENOMEM;
5986bc75619SDan Williams 
5996bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
6006bc75619SDan Williams 		if (!t->label[i])
6016bc75619SDan Williams 			return -ENOMEM;
6026bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
6039d27a87eSDan Williams 
604*85d3fa02SDan Williams 		t->flush[i] = test_alloc(t, sizeof(u64) * NUM_HINTS,
605*85d3fa02SDan Williams 				&t->flush_dma[i]);
6069d27a87eSDan Williams 		if (!t->flush[i])
6079d27a87eSDan Williams 			return -ENOMEM;
6086bc75619SDan Williams 	}
6096bc75619SDan Williams 
6106bc75619SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
6116bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
6126bc75619SDan Williams 		if (!t->dcr[i])
6136bc75619SDan Williams 			return -ENOMEM;
6146bc75619SDan Williams 	}
6156bc75619SDan Williams 
616f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
6176bc75619SDan Williams }
6186bc75619SDan Williams 
6196bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
6206bc75619SDan Williams {
6216b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address)
6226bc75619SDan Williams 		+ sizeof(struct acpi_nfit_memory_map)
6233b87356fSDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size);
6246bc75619SDan Williams 
6256bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
6266bc75619SDan Williams 	if (!t->nfit_buf)
6276bc75619SDan Williams 		return -ENOMEM;
6286bc75619SDan Williams 	t->nfit_size = nfit_size;
6296bc75619SDan Williams 
630ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
6316bc75619SDan Williams 	if (!t->spa_set[0])
6326bc75619SDan Williams 		return -ENOMEM;
6336bc75619SDan Williams 
634f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
6356bc75619SDan Williams }
6366bc75619SDan Williams 
6376bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
6386bc75619SDan Williams {
639*85d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
640*85d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
6416bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
6426bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
6436bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
6446bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
6456bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
6466bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
6479d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
648*85d3fa02SDan Williams 	unsigned int offset, i;
6496bc75619SDan Williams 
6506bc75619SDan Williams 	/*
6516bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
6526bc75619SDan Williams 	 * does not actually alias the related block-data-window
6536bc75619SDan Williams 	 * regions)
6546bc75619SDan Williams 	 */
6556b577c9dSLinda Knippers 	spa = nfit_buf;
6566bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
6576bc75619SDan Williams 	spa->header.length = sizeof(*spa);
6586bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
6596bc75619SDan Williams 	spa->range_index = 0+1;
6606bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
6616bc75619SDan Williams 	spa->length = SPA0_SIZE;
6626bc75619SDan Williams 
6636bc75619SDan Williams 	/*
6646bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
6656bc75619SDan Williams 	 * does not actually alias the related block-data-window
6666bc75619SDan Williams 	 * regions)
6676bc75619SDan Williams 	 */
6686b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa);
6696bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
6706bc75619SDan Williams 	spa->header.length = sizeof(*spa);
6716bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
6726bc75619SDan Williams 	spa->range_index = 1+1;
6736bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
6746bc75619SDan Williams 	spa->length = SPA1_SIZE;
6756bc75619SDan Williams 
6766bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
6776b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 2;
6786bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
6796bc75619SDan Williams 	spa->header.length = sizeof(*spa);
6806bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
6816bc75619SDan Williams 	spa->range_index = 2+1;
6826bc75619SDan Williams 	spa->address = t->dcr_dma[0];
6836bc75619SDan Williams 	spa->length = DCR_SIZE;
6846bc75619SDan Williams 
6856bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
6866b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 3;
6876bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
6886bc75619SDan Williams 	spa->header.length = sizeof(*spa);
6896bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
6906bc75619SDan Williams 	spa->range_index = 3+1;
6916bc75619SDan Williams 	spa->address = t->dcr_dma[1];
6926bc75619SDan Williams 	spa->length = DCR_SIZE;
6936bc75619SDan Williams 
6946bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
6956b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 4;
6966bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
6976bc75619SDan Williams 	spa->header.length = sizeof(*spa);
6986bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
6996bc75619SDan Williams 	spa->range_index = 4+1;
7006bc75619SDan Williams 	spa->address = t->dcr_dma[2];
7016bc75619SDan Williams 	spa->length = DCR_SIZE;
7026bc75619SDan Williams 
7036bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
7046b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 5;
7056bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
7066bc75619SDan Williams 	spa->header.length = sizeof(*spa);
7076bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
7086bc75619SDan Williams 	spa->range_index = 5+1;
7096bc75619SDan Williams 	spa->address = t->dcr_dma[3];
7106bc75619SDan Williams 	spa->length = DCR_SIZE;
7116bc75619SDan Williams 
7126bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
7136b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 6;
7146bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
7156bc75619SDan Williams 	spa->header.length = sizeof(*spa);
7166bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
7176bc75619SDan Williams 	spa->range_index = 6+1;
7186bc75619SDan Williams 	spa->address = t->dimm_dma[0];
7196bc75619SDan Williams 	spa->length = DIMM_SIZE;
7206bc75619SDan Williams 
7216bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
7226b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 7;
7236bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
7246bc75619SDan Williams 	spa->header.length = sizeof(*spa);
7256bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
7266bc75619SDan Williams 	spa->range_index = 7+1;
7276bc75619SDan Williams 	spa->address = t->dimm_dma[1];
7286bc75619SDan Williams 	spa->length = DIMM_SIZE;
7296bc75619SDan Williams 
7306bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
7316b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 8;
7326bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
7336bc75619SDan Williams 	spa->header.length = sizeof(*spa);
7346bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
7356bc75619SDan Williams 	spa->range_index = 8+1;
7366bc75619SDan Williams 	spa->address = t->dimm_dma[2];
7376bc75619SDan Williams 	spa->length = DIMM_SIZE;
7386bc75619SDan Williams 
7396bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
7406b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 9;
7416bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
7426bc75619SDan Williams 	spa->header.length = sizeof(*spa);
7436bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
7446bc75619SDan Williams 	spa->range_index = 9+1;
7456bc75619SDan Williams 	spa->address = t->dimm_dma[3];
7466bc75619SDan Williams 	spa->length = DIMM_SIZE;
7476bc75619SDan Williams 
7486b577c9dSLinda Knippers 	offset = sizeof(*spa) * 10;
7496bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
7506bc75619SDan Williams 	memdev = nfit_buf + offset;
7516bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
7526bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
7536bc75619SDan Williams 	memdev->device_handle = handle[0];
7546bc75619SDan Williams 	memdev->physical_id = 0;
7556bc75619SDan Williams 	memdev->region_id = 0;
7566bc75619SDan Williams 	memdev->range_index = 0+1;
7573b87356fSDan Williams 	memdev->region_index = 4+1;
7586bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
7596bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[0];
7606bc75619SDan Williams 	memdev->address = 0;
7616bc75619SDan Williams 	memdev->interleave_index = 0;
7626bc75619SDan Williams 	memdev->interleave_ways = 2;
7636bc75619SDan Williams 
7646bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
7656bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
7666bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
7676bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
7686bc75619SDan Williams 	memdev->device_handle = handle[1];
7696bc75619SDan Williams 	memdev->physical_id = 1;
7706bc75619SDan Williams 	memdev->region_id = 0;
7716bc75619SDan Williams 	memdev->range_index = 0+1;
7723b87356fSDan Williams 	memdev->region_index = 5+1;
7736bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
7746bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[0] + SPA0_SIZE/2;
7756bc75619SDan Williams 	memdev->address = 0;
7766bc75619SDan Williams 	memdev->interleave_index = 0;
7776bc75619SDan Williams 	memdev->interleave_ways = 2;
7786bc75619SDan Williams 
7796bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
7806bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
7816bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
7826bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
7836bc75619SDan Williams 	memdev->device_handle = handle[0];
7846bc75619SDan Williams 	memdev->physical_id = 0;
7856bc75619SDan Williams 	memdev->region_id = 1;
7866bc75619SDan Williams 	memdev->range_index = 1+1;
7873b87356fSDan Williams 	memdev->region_index = 4+1;
7886bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
7896bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1];
7906bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
7916bc75619SDan Williams 	memdev->interleave_index = 0;
7926bc75619SDan Williams 	memdev->interleave_ways = 4;
7936bc75619SDan Williams 
7946bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
7956bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
7966bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
7976bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
7986bc75619SDan Williams 	memdev->device_handle = handle[1];
7996bc75619SDan Williams 	memdev->physical_id = 1;
8006bc75619SDan Williams 	memdev->region_id = 1;
8016bc75619SDan Williams 	memdev->range_index = 1+1;
8023b87356fSDan Williams 	memdev->region_index = 5+1;
8036bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
8046bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1] + SPA1_SIZE/4;
8056bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
8066bc75619SDan Williams 	memdev->interleave_index = 0;
8076bc75619SDan Williams 	memdev->interleave_ways = 4;
8086bc75619SDan Williams 
8096bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
8106bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
8116bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8126bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8136bc75619SDan Williams 	memdev->device_handle = handle[2];
8146bc75619SDan Williams 	memdev->physical_id = 2;
8156bc75619SDan Williams 	memdev->region_id = 0;
8166bc75619SDan Williams 	memdev->range_index = 1+1;
8173b87356fSDan Williams 	memdev->region_index = 6+1;
8186bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
8196bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1] + 2*SPA1_SIZE/4;
8206bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
8216bc75619SDan Williams 	memdev->interleave_index = 0;
8226bc75619SDan Williams 	memdev->interleave_ways = 4;
8236bc75619SDan Williams 
8246bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
8256bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
8266bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8276bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8286bc75619SDan Williams 	memdev->device_handle = handle[3];
8296bc75619SDan Williams 	memdev->physical_id = 3;
8306bc75619SDan Williams 	memdev->region_id = 0;
8316bc75619SDan Williams 	memdev->range_index = 1+1;
8323b87356fSDan Williams 	memdev->region_index = 7+1;
8336bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
8346bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1] + 3*SPA1_SIZE/4;
8356bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
8366bc75619SDan Williams 	memdev->interleave_index = 0;
8376bc75619SDan Williams 	memdev->interleave_ways = 4;
8386bc75619SDan Williams 
8396bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
8406bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
8416bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8426bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8436bc75619SDan Williams 	memdev->device_handle = handle[0];
8446bc75619SDan Williams 	memdev->physical_id = 0;
8456bc75619SDan Williams 	memdev->region_id = 0;
8466bc75619SDan Williams 	memdev->range_index = 2+1;
8476bc75619SDan Williams 	memdev->region_index = 0+1;
8486bc75619SDan Williams 	memdev->region_size = 0;
8496bc75619SDan Williams 	memdev->region_offset = 0;
8506bc75619SDan Williams 	memdev->address = 0;
8516bc75619SDan Williams 	memdev->interleave_index = 0;
8526bc75619SDan Williams 	memdev->interleave_ways = 1;
8536bc75619SDan Williams 
8546bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
8556bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
8566bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8576bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8586bc75619SDan Williams 	memdev->device_handle = handle[1];
8596bc75619SDan Williams 	memdev->physical_id = 1;
8606bc75619SDan Williams 	memdev->region_id = 0;
8616bc75619SDan Williams 	memdev->range_index = 3+1;
8626bc75619SDan Williams 	memdev->region_index = 1+1;
8636bc75619SDan Williams 	memdev->region_size = 0;
8646bc75619SDan Williams 	memdev->region_offset = 0;
8656bc75619SDan Williams 	memdev->address = 0;
8666bc75619SDan Williams 	memdev->interleave_index = 0;
8676bc75619SDan Williams 	memdev->interleave_ways = 1;
8686bc75619SDan Williams 
8696bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
8706bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
8716bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8726bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8736bc75619SDan Williams 	memdev->device_handle = handle[2];
8746bc75619SDan Williams 	memdev->physical_id = 2;
8756bc75619SDan Williams 	memdev->region_id = 0;
8766bc75619SDan Williams 	memdev->range_index = 4+1;
8776bc75619SDan Williams 	memdev->region_index = 2+1;
8786bc75619SDan Williams 	memdev->region_size = 0;
8796bc75619SDan Williams 	memdev->region_offset = 0;
8806bc75619SDan Williams 	memdev->address = 0;
8816bc75619SDan Williams 	memdev->interleave_index = 0;
8826bc75619SDan Williams 	memdev->interleave_ways = 1;
8836bc75619SDan Williams 
8846bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
8856bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
8866bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8876bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8886bc75619SDan Williams 	memdev->device_handle = handle[3];
8896bc75619SDan Williams 	memdev->physical_id = 3;
8906bc75619SDan Williams 	memdev->region_id = 0;
8916bc75619SDan Williams 	memdev->range_index = 5+1;
8926bc75619SDan Williams 	memdev->region_index = 3+1;
8936bc75619SDan Williams 	memdev->region_size = 0;
8946bc75619SDan Williams 	memdev->region_offset = 0;
8956bc75619SDan Williams 	memdev->address = 0;
8966bc75619SDan Williams 	memdev->interleave_index = 0;
8976bc75619SDan Williams 	memdev->interleave_ways = 1;
8986bc75619SDan Williams 
8996bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
9006bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
9016bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
9026bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
9036bc75619SDan Williams 	memdev->device_handle = handle[0];
9046bc75619SDan Williams 	memdev->physical_id = 0;
9056bc75619SDan Williams 	memdev->region_id = 0;
9066bc75619SDan Williams 	memdev->range_index = 6+1;
9076bc75619SDan Williams 	memdev->region_index = 0+1;
9086bc75619SDan Williams 	memdev->region_size = 0;
9096bc75619SDan Williams 	memdev->region_offset = 0;
9106bc75619SDan Williams 	memdev->address = 0;
9116bc75619SDan Williams 	memdev->interleave_index = 0;
9126bc75619SDan Williams 	memdev->interleave_ways = 1;
9136bc75619SDan Williams 
9146bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
9156bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
9166bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
9176bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
9186bc75619SDan Williams 	memdev->device_handle = handle[1];
9196bc75619SDan Williams 	memdev->physical_id = 1;
9206bc75619SDan Williams 	memdev->region_id = 0;
9216bc75619SDan Williams 	memdev->range_index = 7+1;
9226bc75619SDan Williams 	memdev->region_index = 1+1;
9236bc75619SDan Williams 	memdev->region_size = 0;
9246bc75619SDan Williams 	memdev->region_offset = 0;
9256bc75619SDan Williams 	memdev->address = 0;
9266bc75619SDan Williams 	memdev->interleave_index = 0;
9276bc75619SDan Williams 	memdev->interleave_ways = 1;
9286bc75619SDan Williams 
9296bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
9306bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
9316bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
9326bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
9336bc75619SDan Williams 	memdev->device_handle = handle[2];
9346bc75619SDan Williams 	memdev->physical_id = 2;
9356bc75619SDan Williams 	memdev->region_id = 0;
9366bc75619SDan Williams 	memdev->range_index = 8+1;
9376bc75619SDan Williams 	memdev->region_index = 2+1;
9386bc75619SDan Williams 	memdev->region_size = 0;
9396bc75619SDan Williams 	memdev->region_offset = 0;
9406bc75619SDan Williams 	memdev->address = 0;
9416bc75619SDan Williams 	memdev->interleave_index = 0;
9426bc75619SDan Williams 	memdev->interleave_ways = 1;
9436bc75619SDan Williams 
9446bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
9456bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
9466bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
9476bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
9486bc75619SDan Williams 	memdev->device_handle = handle[3];
9496bc75619SDan Williams 	memdev->physical_id = 3;
9506bc75619SDan Williams 	memdev->region_id = 0;
9516bc75619SDan Williams 	memdev->range_index = 9+1;
9526bc75619SDan Williams 	memdev->region_index = 3+1;
9536bc75619SDan Williams 	memdev->region_size = 0;
9546bc75619SDan Williams 	memdev->region_offset = 0;
9556bc75619SDan Williams 	memdev->address = 0;
9566bc75619SDan Williams 	memdev->interleave_index = 0;
9576bc75619SDan Williams 	memdev->interleave_ways = 1;
9586bc75619SDan Williams 
9596bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
9603b87356fSDan Williams 	/* dcr-descriptor0: blk */
9616bc75619SDan Williams 	dcr = nfit_buf + offset;
9626bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
9636bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
9646bc75619SDan Williams 	dcr->region_index = 0+1;
9656bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
9666bc75619SDan Williams 	dcr->device_id = 0;
9676bc75619SDan Williams 	dcr->revision_id = 1;
9686bc75619SDan Williams 	dcr->serial_number = ~handle[0];
969be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
9706bc75619SDan Williams 	dcr->windows = 1;
9716bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
9726bc75619SDan Williams 	dcr->command_offset = 0;
9736bc75619SDan Williams 	dcr->command_size = 8;
9746bc75619SDan Williams 	dcr->status_offset = 8;
9756bc75619SDan Williams 	dcr->status_size = 4;
9766bc75619SDan Williams 
9773b87356fSDan Williams 	/* dcr-descriptor1: blk */
9786bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
9796bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
9806bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
9816bc75619SDan Williams 	dcr->region_index = 1+1;
9826bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
9836bc75619SDan Williams 	dcr->device_id = 0;
9846bc75619SDan Williams 	dcr->revision_id = 1;
9856bc75619SDan Williams 	dcr->serial_number = ~handle[1];
986be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
9876bc75619SDan Williams 	dcr->windows = 1;
9886bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
9896bc75619SDan Williams 	dcr->command_offset = 0;
9906bc75619SDan Williams 	dcr->command_size = 8;
9916bc75619SDan Williams 	dcr->status_offset = 8;
9926bc75619SDan Williams 	dcr->status_size = 4;
9936bc75619SDan Williams 
9943b87356fSDan Williams 	/* dcr-descriptor2: blk */
9956bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
9966bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
9976bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
9986bc75619SDan Williams 	dcr->region_index = 2+1;
9996bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
10006bc75619SDan Williams 	dcr->device_id = 0;
10016bc75619SDan Williams 	dcr->revision_id = 1;
10026bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1003be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
10046bc75619SDan Williams 	dcr->windows = 1;
10056bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
10066bc75619SDan Williams 	dcr->command_offset = 0;
10076bc75619SDan Williams 	dcr->command_size = 8;
10086bc75619SDan Williams 	dcr->status_offset = 8;
10096bc75619SDan Williams 	dcr->status_size = 4;
10106bc75619SDan Williams 
10113b87356fSDan Williams 	/* dcr-descriptor3: blk */
10126bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
10136bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
10146bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
10156bc75619SDan Williams 	dcr->region_index = 3+1;
10166bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
10176bc75619SDan Williams 	dcr->device_id = 0;
10186bc75619SDan Williams 	dcr->revision_id = 1;
10196bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1020be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
10216bc75619SDan Williams 	dcr->windows = 1;
10226bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
10236bc75619SDan Williams 	dcr->command_offset = 0;
10246bc75619SDan Williams 	dcr->command_size = 8;
10256bc75619SDan Williams 	dcr->status_offset = 8;
10266bc75619SDan Williams 	dcr->status_size = 4;
10276bc75619SDan Williams 
10286bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
10293b87356fSDan Williams 	/* dcr-descriptor0: pmem */
10303b87356fSDan Williams 	dcr = nfit_buf + offset;
10313b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
10323b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
10333b87356fSDan Williams 			window_size);
10343b87356fSDan Williams 	dcr->region_index = 4+1;
10353b87356fSDan Williams 	dcr->vendor_id = 0xabcd;
10363b87356fSDan Williams 	dcr->device_id = 0;
10373b87356fSDan Williams 	dcr->revision_id = 1;
10383b87356fSDan Williams 	dcr->serial_number = ~handle[0];
10393b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
10403b87356fSDan Williams 	dcr->windows = 0;
10413b87356fSDan Williams 
10423b87356fSDan Williams 	/* dcr-descriptor1: pmem */
10433b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
10443b87356fSDan Williams 			window_size);
10453b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
10463b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
10473b87356fSDan Williams 			window_size);
10483b87356fSDan Williams 	dcr->region_index = 5+1;
10493b87356fSDan Williams 	dcr->vendor_id = 0xabcd;
10503b87356fSDan Williams 	dcr->device_id = 0;
10513b87356fSDan Williams 	dcr->revision_id = 1;
10523b87356fSDan Williams 	dcr->serial_number = ~handle[1];
10533b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
10543b87356fSDan Williams 	dcr->windows = 0;
10553b87356fSDan Williams 
10563b87356fSDan Williams 	/* dcr-descriptor2: pmem */
10573b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
10583b87356fSDan Williams 			window_size) * 2;
10593b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
10603b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
10613b87356fSDan Williams 			window_size);
10623b87356fSDan Williams 	dcr->region_index = 6+1;
10633b87356fSDan Williams 	dcr->vendor_id = 0xabcd;
10643b87356fSDan Williams 	dcr->device_id = 0;
10653b87356fSDan Williams 	dcr->revision_id = 1;
10663b87356fSDan Williams 	dcr->serial_number = ~handle[2];
10673b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
10683b87356fSDan Williams 	dcr->windows = 0;
10693b87356fSDan Williams 
10703b87356fSDan Williams 	/* dcr-descriptor3: pmem */
10713b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
10723b87356fSDan Williams 			window_size) * 3;
10733b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
10743b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
10753b87356fSDan Williams 			window_size);
10763b87356fSDan Williams 	dcr->region_index = 7+1;
10773b87356fSDan Williams 	dcr->vendor_id = 0xabcd;
10783b87356fSDan Williams 	dcr->device_id = 0;
10793b87356fSDan Williams 	dcr->revision_id = 1;
10803b87356fSDan Williams 	dcr->serial_number = ~handle[3];
10813b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
10823b87356fSDan Williams 	dcr->windows = 0;
10833b87356fSDan Williams 
10843b87356fSDan Williams 	offset = offset + offsetof(struct acpi_nfit_control_region,
10853b87356fSDan Williams 			window_size) * 4;
10866bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
10876bc75619SDan Williams 	bdw = nfit_buf + offset;
10886bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
10896bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
10906bc75619SDan Williams 	bdw->region_index = 0+1;
10916bc75619SDan Williams 	bdw->windows = 1;
10926bc75619SDan Williams 	bdw->offset = 0;
10936bc75619SDan Williams 	bdw->size = BDW_SIZE;
10946bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
10956bc75619SDan Williams 	bdw->start_address = 0;
10966bc75619SDan Williams 
10976bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
10986bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
10996bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
11006bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
11016bc75619SDan Williams 	bdw->region_index = 1+1;
11026bc75619SDan Williams 	bdw->windows = 1;
11036bc75619SDan Williams 	bdw->offset = 0;
11046bc75619SDan Williams 	bdw->size = BDW_SIZE;
11056bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
11066bc75619SDan Williams 	bdw->start_address = 0;
11076bc75619SDan Williams 
11086bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
11096bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
11106bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
11116bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
11126bc75619SDan Williams 	bdw->region_index = 2+1;
11136bc75619SDan Williams 	bdw->windows = 1;
11146bc75619SDan Williams 	bdw->offset = 0;
11156bc75619SDan Williams 	bdw->size = BDW_SIZE;
11166bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
11176bc75619SDan Williams 	bdw->start_address = 0;
11186bc75619SDan Williams 
11196bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
11206bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
11216bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
11226bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
11236bc75619SDan Williams 	bdw->region_index = 3+1;
11246bc75619SDan Williams 	bdw->windows = 1;
11256bc75619SDan Williams 	bdw->offset = 0;
11266bc75619SDan Williams 	bdw->size = BDW_SIZE;
11276bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
11286bc75619SDan Williams 	bdw->start_address = 0;
11296bc75619SDan Williams 
11309d27a87eSDan Williams 	offset = offset + sizeof(struct acpi_nfit_data_region) * 4;
11319d27a87eSDan Williams 	/* flush0 (dimm0) */
11329d27a87eSDan Williams 	flush = nfit_buf + offset;
11339d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1134*85d3fa02SDan Williams 	flush->header.length = flush_hint_size;
11359d27a87eSDan Williams 	flush->device_handle = handle[0];
1136*85d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
1137*85d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
1138*85d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
11399d27a87eSDan Williams 
11409d27a87eSDan Williams 	/* flush1 (dimm1) */
1141*85d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size * 1;
11429d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1143*85d3fa02SDan Williams 	flush->header.length = flush_hint_size;
11449d27a87eSDan Williams 	flush->device_handle = handle[1];
1145*85d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
1146*85d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
1147*85d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
11489d27a87eSDan Williams 
11499d27a87eSDan Williams 	/* flush2 (dimm2) */
1150*85d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size  * 2;
11519d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1152*85d3fa02SDan Williams 	flush->header.length = flush_hint_size;
11539d27a87eSDan Williams 	flush->device_handle = handle[2];
1154*85d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
1155*85d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
1156*85d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
11579d27a87eSDan Williams 
11589d27a87eSDan Williams 	/* flush3 (dimm3) */
1159*85d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size * 3;
11609d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1161*85d3fa02SDan Williams 	flush->header.length = flush_hint_size;
11629d27a87eSDan Williams 	flush->device_handle = handle[3];
1163*85d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
1164*85d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
1165*85d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
11669d27a87eSDan Williams 
116720985164SVishal Verma 	if (t->setup_hotplug) {
1168*85d3fa02SDan Williams 		offset = offset + flush_hint_size * 4;
11693b87356fSDan Williams 		/* dcr-descriptor4: blk */
117020985164SVishal Verma 		dcr = nfit_buf + offset;
117120985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
117220985164SVishal Verma 		dcr->header.length = sizeof(struct acpi_nfit_control_region);
11733b87356fSDan Williams 		dcr->region_index = 8+1;
117420985164SVishal Verma 		dcr->vendor_id = 0xabcd;
117520985164SVishal Verma 		dcr->device_id = 0;
117620985164SVishal Verma 		dcr->revision_id = 1;
117720985164SVishal Verma 		dcr->serial_number = ~handle[4];
1178be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
117920985164SVishal Verma 		dcr->windows = 1;
118020985164SVishal Verma 		dcr->window_size = DCR_SIZE;
118120985164SVishal Verma 		dcr->command_offset = 0;
118220985164SVishal Verma 		dcr->command_size = 8;
118320985164SVishal Verma 		dcr->status_offset = 8;
118420985164SVishal Verma 		dcr->status_size = 4;
118520985164SVishal Verma 
118620985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_control_region);
11873b87356fSDan Williams 		/* dcr-descriptor4: pmem */
11883b87356fSDan Williams 		dcr = nfit_buf + offset;
11893b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
11903b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
11913b87356fSDan Williams 				window_size);
11923b87356fSDan Williams 		dcr->region_index = 9+1;
11933b87356fSDan Williams 		dcr->vendor_id = 0xabcd;
11943b87356fSDan Williams 		dcr->device_id = 0;
11953b87356fSDan Williams 		dcr->revision_id = 1;
11963b87356fSDan Williams 		dcr->serial_number = ~handle[4];
11973b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
11983b87356fSDan Williams 		dcr->windows = 0;
11993b87356fSDan Williams 
12003b87356fSDan Williams 		offset = offset + offsetof(struct acpi_nfit_control_region,
12013b87356fSDan Williams 				window_size);
120220985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
120320985164SVishal Verma 		bdw = nfit_buf + offset;
120420985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
120520985164SVishal Verma 		bdw->header.length = sizeof(struct acpi_nfit_data_region);
12063b87356fSDan Williams 		bdw->region_index = 8+1;
120720985164SVishal Verma 		bdw->windows = 1;
120820985164SVishal Verma 		bdw->offset = 0;
120920985164SVishal Verma 		bdw->size = BDW_SIZE;
121020985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
121120985164SVishal Verma 		bdw->start_address = 0;
121220985164SVishal Verma 
121320985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_data_region);
121420985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
121520985164SVishal Verma 		spa = nfit_buf + offset;
121620985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
121720985164SVishal Verma 		spa->header.length = sizeof(*spa);
121820985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
121920985164SVishal Verma 		spa->range_index = 10+1;
122020985164SVishal Verma 		spa->address = t->dcr_dma[4];
122120985164SVishal Verma 		spa->length = DCR_SIZE;
122220985164SVishal Verma 
122320985164SVishal Verma 		/*
122420985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
122520985164SVishal Verma 		 * does not actually alias the related block-data-window
122620985164SVishal Verma 		 * regions)
122720985164SVishal Verma 		 */
122820985164SVishal Verma 		spa = nfit_buf + offset + sizeof(*spa);
122920985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
123020985164SVishal Verma 		spa->header.length = sizeof(*spa);
123120985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
123220985164SVishal Verma 		spa->range_index = 11+1;
123320985164SVishal Verma 		spa->address = t->spa_set_dma[2];
123420985164SVishal Verma 		spa->length = SPA0_SIZE;
123520985164SVishal Verma 
123620985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
123720985164SVishal Verma 		spa = nfit_buf + offset + sizeof(*spa) * 2;
123820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
123920985164SVishal Verma 		spa->header.length = sizeof(*spa);
124020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
124120985164SVishal Verma 		spa->range_index = 12+1;
124220985164SVishal Verma 		spa->address = t->dimm_dma[4];
124320985164SVishal Verma 		spa->length = DIMM_SIZE;
124420985164SVishal Verma 
124520985164SVishal Verma 		offset = offset + sizeof(*spa) * 3;
124620985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
124720985164SVishal Verma 		memdev = nfit_buf + offset;
124820985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
124920985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
125020985164SVishal Verma 		memdev->device_handle = handle[4];
125120985164SVishal Verma 		memdev->physical_id = 4;
125220985164SVishal Verma 		memdev->region_id = 0;
125320985164SVishal Verma 		memdev->range_index = 10+1;
12543b87356fSDan Williams 		memdev->region_index = 8+1;
125520985164SVishal Verma 		memdev->region_size = 0;
125620985164SVishal Verma 		memdev->region_offset = 0;
125720985164SVishal Verma 		memdev->address = 0;
125820985164SVishal Verma 		memdev->interleave_index = 0;
125920985164SVishal Verma 		memdev->interleave_ways = 1;
126020985164SVishal Verma 
126120985164SVishal Verma 		/* mem-region15 (spa0, dimm4) */
126220985164SVishal Verma 		memdev = nfit_buf + offset +
126320985164SVishal Verma 				sizeof(struct acpi_nfit_memory_map);
126420985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
126520985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
126620985164SVishal Verma 		memdev->device_handle = handle[4];
126720985164SVishal Verma 		memdev->physical_id = 4;
126820985164SVishal Verma 		memdev->region_id = 0;
126920985164SVishal Verma 		memdev->range_index = 11+1;
12703b87356fSDan Williams 		memdev->region_index = 9+1;
127120985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
127220985164SVishal Verma 		memdev->region_offset = t->spa_set_dma[2];
127320985164SVishal Verma 		memdev->address = 0;
127420985164SVishal Verma 		memdev->interleave_index = 0;
127520985164SVishal Verma 		memdev->interleave_ways = 1;
127620985164SVishal Verma 
12773b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
127820985164SVishal Verma 		memdev = nfit_buf + offset +
127920985164SVishal Verma 				sizeof(struct acpi_nfit_memory_map) * 2;
128020985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
128120985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
128220985164SVishal Verma 		memdev->device_handle = handle[4];
128320985164SVishal Verma 		memdev->physical_id = 4;
128420985164SVishal Verma 		memdev->region_id = 0;
128520985164SVishal Verma 		memdev->range_index = 12+1;
12863b87356fSDan Williams 		memdev->region_index = 8+1;
128720985164SVishal Verma 		memdev->region_size = 0;
128820985164SVishal Verma 		memdev->region_offset = 0;
128920985164SVishal Verma 		memdev->address = 0;
129020985164SVishal Verma 		memdev->interleave_index = 0;
129120985164SVishal Verma 		memdev->interleave_ways = 1;
129220985164SVishal Verma 
129320985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_memory_map) * 3;
129420985164SVishal Verma 		/* flush3 (dimm4) */
129520985164SVishal Verma 		flush = nfit_buf + offset;
129620985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1297*85d3fa02SDan Williams 		flush->header.length = flush_hint_size;
129820985164SVishal Verma 		flush->device_handle = handle[4];
1299*85d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
1300*85d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
1301*85d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
1302*85d3fa02SDan Williams 				+ i * sizeof(u64);
130320985164SVishal Verma 	}
130420985164SVishal Verma 
1305f471f1a7SDan Williams 	post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA0_SIZE);
1306f471f1a7SDan Williams 
13076bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
1308e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
1309e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1310e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
13111f716d05SDan Williams 	set_bit(ND_CMD_SMART, &acpi_desc->dimm_cmd_force_en);
1312e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1313e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1314e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1315e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
13161f716d05SDan Williams 	set_bit(ND_CMD_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
13176bc75619SDan Williams }
13186bc75619SDan Williams 
13196bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
13206bc75619SDan Williams {
13216b577c9dSLinda Knippers 	size_t offset;
13226bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
13236bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
13246bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
13256bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
1326d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
13276bc75619SDan Williams 
13286b577c9dSLinda Knippers 	offset = 0;
13296bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
13306bc75619SDan Williams 	spa = nfit_buf + offset;
13316bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
13326bc75619SDan Williams 	spa->header.length = sizeof(*spa);
13336bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
13346bc75619SDan Williams 	spa->range_index = 0+1;
13356bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
13366bc75619SDan Williams 	spa->length = SPA2_SIZE;
13376bc75619SDan Williams 
13386bc75619SDan Williams 	offset += sizeof(*spa);
13396bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
13406bc75619SDan Williams 	memdev = nfit_buf + offset;
13416bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
13426bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
13436bc75619SDan Williams 	memdev->device_handle = 0;
13446bc75619SDan Williams 	memdev->physical_id = 0;
13456bc75619SDan Williams 	memdev->region_id = 0;
13466bc75619SDan Williams 	memdev->range_index = 0+1;
13476bc75619SDan Williams 	memdev->region_index = 0+1;
13486bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
13496bc75619SDan Williams 	memdev->region_offset = 0;
13506bc75619SDan Williams 	memdev->address = 0;
13516bc75619SDan Williams 	memdev->interleave_index = 0;
13526bc75619SDan Williams 	memdev->interleave_ways = 1;
135358138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
135458138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
1355f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
13566bc75619SDan Williams 
13576bc75619SDan Williams 	offset += sizeof(*memdev);
13586bc75619SDan Williams 	/* dcr-descriptor0 */
13596bc75619SDan Williams 	dcr = nfit_buf + offset;
13606bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13613b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
13623b87356fSDan Williams 			window_size);
13636bc75619SDan Williams 	dcr->region_index = 0+1;
13646bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
13656bc75619SDan Williams 	dcr->device_id = 0;
13666bc75619SDan Williams 	dcr->revision_id = 1;
13676bc75619SDan Williams 	dcr->serial_number = ~0;
1368be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
13696bc75619SDan Williams 	dcr->windows = 0;
1370d26f73f0SDan Williams 
1371f471f1a7SDan Williams 	post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA2_SIZE);
1372f471f1a7SDan Williams 
1373d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
1374e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1375e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1376e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1377e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
13786bc75619SDan Williams }
13796bc75619SDan Williams 
13806bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
13816bc75619SDan Williams 		void *iobuf, u64 len, int rw)
13826bc75619SDan Williams {
13836bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
13846bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
13856bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
13866bc75619SDan Williams 	unsigned int lane;
13876bc75619SDan Williams 
13886bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
13896bc75619SDan Williams 	if (rw)
139067a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
139167a3e8feSRoss Zwisler 	else {
139267a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
139367a3e8feSRoss Zwisler 
139467a3e8feSRoss Zwisler 		/* give us some some coverage of the mmio_flush_range() API */
139567a3e8feSRoss Zwisler 		mmio_flush_range(mmio->addr.base + dpa, len);
139667a3e8feSRoss Zwisler 	}
13976bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
13986bc75619SDan Williams 
13996bc75619SDan Williams 	return 0;
14006bc75619SDan Williams }
14016bc75619SDan Williams 
14026bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
14036bc75619SDan Williams {
14046bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
14056bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
14066bc75619SDan Williams 	struct device *dev = &pdev->dev;
14076bc75619SDan Williams 	struct nfit_test *nfit_test;
14086bc75619SDan Williams 	int rc;
14096bc75619SDan Williams 
14106bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
14116bc75619SDan Williams 
14126bc75619SDan Williams 	/* common alloc */
14136bc75619SDan Williams 	if (nfit_test->num_dcr) {
14146bc75619SDan Williams 		int num = nfit_test->num_dcr;
14156bc75619SDan Williams 
14166bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
14176bc75619SDan Williams 				GFP_KERNEL);
14186bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
14196bc75619SDan Williams 				GFP_KERNEL);
14209d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
14219d27a87eSDan Williams 				GFP_KERNEL);
14229d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
14239d27a87eSDan Williams 				GFP_KERNEL);
14246bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
14256bc75619SDan Williams 				GFP_KERNEL);
14266bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
14276bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
14286bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
14296bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
14306bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
14316bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
14326bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
14336bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
14349d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
14359d27a87eSDan Williams 				&& nfit_test->flush_dma)
14366bc75619SDan Williams 			/* pass */;
14376bc75619SDan Williams 		else
14386bc75619SDan Williams 			return -ENOMEM;
14396bc75619SDan Williams 	}
14406bc75619SDan Williams 
14416bc75619SDan Williams 	if (nfit_test->num_pm) {
14426bc75619SDan Williams 		int num = nfit_test->num_pm;
14436bc75619SDan Williams 
14446bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
14456bc75619SDan Williams 				GFP_KERNEL);
14466bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
14476bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
14486bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
14496bc75619SDan Williams 			/* pass */;
14506bc75619SDan Williams 		else
14516bc75619SDan Williams 			return -ENOMEM;
14526bc75619SDan Williams 	}
14536bc75619SDan Williams 
14546bc75619SDan Williams 	/* per-nfit specific alloc */
14556bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
14566bc75619SDan Williams 		return -ENOMEM;
14576bc75619SDan Williams 
14586bc75619SDan Williams 	nfit_test->setup(nfit_test);
14596bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
1460a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
14616bc75619SDan Williams 	acpi_desc->nfit = nfit_test->nfit_buf;
14626bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
14636bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
1464a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
1465a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
14666bc75619SDan Williams 	acpi_desc->nvdimm_bus = nvdimm_bus_register(&pdev->dev, nd_desc);
14676bc75619SDan Williams 	if (!acpi_desc->nvdimm_bus)
14686bc75619SDan Williams 		return -ENXIO;
14696bc75619SDan Williams 
147020985164SVishal Verma 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_size);
147120985164SVishal Verma 	if (rc) {
147220985164SVishal Verma 		nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
147320985164SVishal Verma 		return rc;
147420985164SVishal Verma 	}
147520985164SVishal Verma 
147620985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
147720985164SVishal Verma 		return 0;
147820985164SVishal Verma 
147920985164SVishal Verma 	nfit_test->setup_hotplug = 1;
148020985164SVishal Verma 	nfit_test->setup(nfit_test);
148120985164SVishal Verma 
14826bc75619SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_size);
14836bc75619SDan Williams 	if (rc) {
14846bc75619SDan Williams 		nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
14856bc75619SDan Williams 		return rc;
14866bc75619SDan Williams 	}
14876bc75619SDan Williams 
14886bc75619SDan Williams 	return 0;
14896bc75619SDan Williams }
14906bc75619SDan Williams 
14916bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
14926bc75619SDan Williams {
14936bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(&pdev->dev);
14946bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc = &nfit_test->acpi_desc;
14956bc75619SDan Williams 
14966bc75619SDan Williams 	nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
14976bc75619SDan Williams 
14986bc75619SDan Williams 	return 0;
14996bc75619SDan Williams }
15006bc75619SDan Williams 
15016bc75619SDan Williams static void nfit_test_release(struct device *dev)
15026bc75619SDan Williams {
15036bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
15046bc75619SDan Williams 
15056bc75619SDan Williams 	kfree(nfit_test);
15066bc75619SDan Williams }
15076bc75619SDan Williams 
15086bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
15096bc75619SDan Williams 	{ KBUILD_MODNAME },
15106bc75619SDan Williams 	{ },
15116bc75619SDan Williams };
15126bc75619SDan Williams 
15136bc75619SDan Williams static struct platform_driver nfit_test_driver = {
15146bc75619SDan Williams 	.probe = nfit_test_probe,
15156bc75619SDan Williams 	.remove = nfit_test_remove,
15166bc75619SDan Williams 	.driver = {
15176bc75619SDan Williams 		.name = KBUILD_MODNAME,
15186bc75619SDan Williams 	},
15196bc75619SDan Williams 	.id_table = nfit_test_id,
15206bc75619SDan Williams };
15216bc75619SDan Williams 
15226bc75619SDan Williams static __init int nfit_test_init(void)
15236bc75619SDan Williams {
15246bc75619SDan Williams 	int rc, i;
15256bc75619SDan Williams 
15266bc75619SDan Williams 	nfit_test_setup(nfit_test_lookup);
15276bc75619SDan Williams 
15286bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
15296bc75619SDan Williams 		struct nfit_test *nfit_test;
15306bc75619SDan Williams 		struct platform_device *pdev;
15316bc75619SDan Williams 
15326bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
15336bc75619SDan Williams 		if (!nfit_test) {
15346bc75619SDan Williams 			rc = -ENOMEM;
15356bc75619SDan Williams 			goto err_register;
15366bc75619SDan Williams 		}
15376bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
15386bc75619SDan Williams 		switch (i) {
15396bc75619SDan Williams 		case 0:
15406bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
15416bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
15426bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
15436bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
15446bc75619SDan Williams 			break;
15456bc75619SDan Williams 		case 1:
15466bc75619SDan Williams 			nfit_test->num_pm = 1;
15476bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
15486bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
15496bc75619SDan Williams 			break;
15506bc75619SDan Williams 		default:
15516bc75619SDan Williams 			rc = -EINVAL;
15526bc75619SDan Williams 			goto err_register;
15536bc75619SDan Williams 		}
15546bc75619SDan Williams 		pdev = &nfit_test->pdev;
15556bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
15566bc75619SDan Williams 		pdev->id = i;
15576bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
15586bc75619SDan Williams 		rc = platform_device_register(pdev);
15596bc75619SDan Williams 		if (rc) {
15606bc75619SDan Williams 			put_device(&pdev->dev);
15616bc75619SDan Williams 			goto err_register;
15626bc75619SDan Williams 		}
15636bc75619SDan Williams 
15646bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15656bc75619SDan Williams 		if (rc)
15666bc75619SDan Williams 			goto err_register;
15676bc75619SDan Williams 
15686bc75619SDan Williams 		instances[i] = nfit_test;
15696bc75619SDan Williams 	}
15706bc75619SDan Williams 
15716bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
15726bc75619SDan Williams 	if (rc)
15736bc75619SDan Williams 		goto err_register;
15746bc75619SDan Williams 	return 0;
15756bc75619SDan Williams 
15766bc75619SDan Williams  err_register:
15776bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
15786bc75619SDan Williams 		if (instances[i])
15796bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
15806bc75619SDan Williams 	nfit_test_teardown();
15816bc75619SDan Williams 	return rc;
15826bc75619SDan Williams }
15836bc75619SDan Williams 
15846bc75619SDan Williams static __exit void nfit_test_exit(void)
15856bc75619SDan Williams {
15866bc75619SDan Williams 	int i;
15876bc75619SDan Williams 
15886bc75619SDan Williams 	platform_driver_unregister(&nfit_test_driver);
15896bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
15906bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
15916bc75619SDan Williams 	nfit_test_teardown();
15926bc75619SDan Williams }
15936bc75619SDan Williams 
15946bc75619SDan Williams module_init(nfit_test_init);
15956bc75619SDan Williams module_exit(nfit_test_exit);
15966bc75619SDan Williams MODULE_LICENSE("GPL v2");
15976bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
1598