xref: /openbmc/linux/tools/testing/nvdimm/test/nfit.c (revision 5e096ef3b29e6113a7aa6a7dc4871987efa99bcb)
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>
16d8d378faSDan Williams #include <linux/workqueue.h>
176bc75619SDan Williams #include <linux/libnvdimm.h>
186bc75619SDan Williams #include <linux/vmalloc.h>
196bc75619SDan Williams #include <linux/device.h>
206bc75619SDan Williams #include <linux/module.h>
2120985164SVishal Verma #include <linux/mutex.h>
226bc75619SDan Williams #include <linux/ndctl.h>
236bc75619SDan Williams #include <linux/sizes.h>
2420985164SVishal Verma #include <linux/list.h>
256bc75619SDan Williams #include <linux/slab.h>
26a7de92daSDan Williams #include <nd-core.h>
276bc75619SDan Williams #include <nfit.h>
286bc75619SDan Williams #include <nd.h>
296bc75619SDan Williams #include "nfit_test.h"
306bc75619SDan Williams 
316bc75619SDan Williams /*
326bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
336bc75619SDan Williams  *
346bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
356bc75619SDan Williams  *
366bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
376bc75619SDan Williams  *           +----------+--------------+----------+---------+
386bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
396bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
406bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
416bc75619SDan Williams  *    |      +----------+--------------v----------v         v
426bc75619SDan Williams  * +--+---+                            |                    |
436bc75619SDan Williams  * | cpu0 |                                    region1
446bc75619SDan Williams  * +--+---+                            |                    |
456bc75619SDan Williams  *    |      +-------------------------^----------^         ^
466bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
476bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
486bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
496bc75619SDan Williams  *           +-------------------------+----------+-+-------+
506bc75619SDan Williams  *
5120985164SVishal Verma  * +--+---+
5220985164SVishal Verma  * | cpu1 |
5320985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5420985164SVishal Verma  *    |      +----------------------------------------------+
5520985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5620985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5720985164SVishal Verma  * +------+
5820985164SVishal Verma  *
5920985164SVishal Verma  *
606bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
616bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
626bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
636bc75619SDan Williams  *
646bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
656bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
666bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
676bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
686bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
696bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
706bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
716bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
726bc75619SDan Williams  *    names that can be assigned to a namespace.
736bc75619SDan Williams  *
746bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
756bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
766bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
776bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
786bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
796bc75619SDan Williams  *    "blk5.0".
806bc75619SDan Williams  *
816bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
826bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
836bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
846bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
856bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
866bc75619SDan Williams  *
876bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
886bc75619SDan Williams  *
896bc75619SDan Williams  *  region2
906bc75619SDan Williams  * +---------------------+
916bc75619SDan Williams  * |---------------------|
926bc75619SDan Williams  * ||       pm2.0       ||
936bc75619SDan Williams  * |---------------------|
946bc75619SDan Williams  * +---------------------+
956bc75619SDan Williams  *
966bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
976bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
986bc75619SDan Williams  *    reference an NVDIMM.
996bc75619SDan Williams  */
1006bc75619SDan Williams enum {
10120985164SVishal Verma 	NUM_PM  = 3,
10220985164SVishal Verma 	NUM_DCR = 5,
10385d3fa02SDan Williams 	NUM_HINTS = 8,
1046bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1056bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1066bc75619SDan Williams 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
1076bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1086bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1097bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1106bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1116bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1126bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1136bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1146bc75619SDan Williams 	DCR_SIZE = 12,
1156bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1166bc75619SDan Williams };
1176bc75619SDan Williams 
1186bc75619SDan Williams struct nfit_test_dcr {
1196bc75619SDan Williams 	__le64 bdw_addr;
1206bc75619SDan Williams 	__le32 bdw_status;
1216bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1226bc75619SDan Williams };
1236bc75619SDan Williams 
1246bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1256bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1266bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1276bc75619SDan Williams 
128dafb1048SDan Williams static u32 handle[] = {
1296bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1306bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1316bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1326bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13320985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
134dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
135ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1366bc75619SDan Williams };
1376bc75619SDan Williams 
13873606afdSDan Williams static unsigned long dimm_fail_cmd_flags[NUM_DCR];
13973606afdSDan Williams 
1406bc75619SDan Williams struct nfit_test {
1416bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1426bc75619SDan Williams 	struct platform_device pdev;
1436bc75619SDan Williams 	struct list_head resources;
1446bc75619SDan Williams 	void *nfit_buf;
1456bc75619SDan Williams 	dma_addr_t nfit_dma;
1466bc75619SDan Williams 	size_t nfit_size;
147dafb1048SDan Williams 	int dcr_idx;
1486bc75619SDan Williams 	int num_dcr;
1496bc75619SDan Williams 	int num_pm;
1506bc75619SDan Williams 	void **dimm;
1516bc75619SDan Williams 	dma_addr_t *dimm_dma;
1529d27a87eSDan Williams 	void **flush;
1539d27a87eSDan Williams 	dma_addr_t *flush_dma;
1546bc75619SDan Williams 	void **label;
1556bc75619SDan Williams 	dma_addr_t *label_dma;
1566bc75619SDan Williams 	void **spa_set;
1576bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1586bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1596bc75619SDan Williams 	dma_addr_t *dcr_dma;
1606bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1616bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
16220985164SVishal Verma 	int setup_hotplug;
163c14a868aSDan Williams 	union acpi_object **_fit;
164c14a868aSDan Williams 	dma_addr_t _fit_dma;
165f471f1a7SDan Williams 	struct ars_state {
166f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
167f471f1a7SDan Williams 		unsigned long deadline;
168f471f1a7SDan Williams 		spinlock_t lock;
169f471f1a7SDan Williams 	} ars_state;
170231bf117SDan Williams 	struct device *dimm_dev[NUM_DCR];
1719fb1a190SDave Jiang 	struct badrange badrange;
1729fb1a190SDave Jiang 	struct work_struct work;
1736bc75619SDan Williams };
1746bc75619SDan Williams 
1759fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
1769fb1a190SDave Jiang 
1776bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1786bc75619SDan Williams {
1796bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1806bc75619SDan Williams 
1816bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1826bc75619SDan Williams }
1836bc75619SDan Williams 
18439c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
1856bc75619SDan Williams 		unsigned int buf_len)
1866bc75619SDan Williams {
1876bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
1886bc75619SDan Williams 		return -EINVAL;
18939c686b8SVishal Verma 
1906bc75619SDan Williams 	nd_cmd->status = 0;
1916bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
1926bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
19339c686b8SVishal Verma 
19439c686b8SVishal Verma 	return 0;
1956bc75619SDan Williams }
19639c686b8SVishal Verma 
19739c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
19839c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
19939c686b8SVishal Verma {
2006bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
20139c686b8SVishal Verma 	int rc;
2026bc75619SDan Williams 
2036bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
2046bc75619SDan Williams 		return -EINVAL;
2056bc75619SDan Williams 	if (offset >= LABEL_SIZE)
2066bc75619SDan Williams 		return -EINVAL;
2076bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
2086bc75619SDan Williams 		return -EINVAL;
2096bc75619SDan Williams 
2106bc75619SDan Williams 	nd_cmd->status = 0;
2116bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
21239c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
2136bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
21439c686b8SVishal Verma 
21539c686b8SVishal Verma 	return rc;
2166bc75619SDan Williams }
21739c686b8SVishal Verma 
21839c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
21939c686b8SVishal Verma 		unsigned int buf_len, void *label)
22039c686b8SVishal Verma {
2216bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
2226bc75619SDan Williams 	u32 *status;
22339c686b8SVishal Verma 	int rc;
2246bc75619SDan Williams 
2256bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
2266bc75619SDan Williams 		return -EINVAL;
2276bc75619SDan Williams 	if (offset >= LABEL_SIZE)
2286bc75619SDan Williams 		return -EINVAL;
2296bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
2306bc75619SDan Williams 		return -EINVAL;
2316bc75619SDan Williams 
23239c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
2336bc75619SDan Williams 	*status = 0;
2346bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
23539c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
2366bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
23739c686b8SVishal Verma 
23839c686b8SVishal Verma 	return rc;
2396bc75619SDan Williams }
24039c686b8SVishal Verma 
241d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
242747ffe11SDan Williams 
24339c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
24439c686b8SVishal Verma 		unsigned int buf_len)
24539c686b8SVishal Verma {
2469fb1a190SDave Jiang 	int ars_recs;
2479fb1a190SDave Jiang 
24839c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
24939c686b8SVishal Verma 		return -EINVAL;
25039c686b8SVishal Verma 
2519fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
2529fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
2539fb1a190SDave Jiang 
254747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
2559fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
25639c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
257d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
25839c686b8SVishal Verma 
25939c686b8SVishal Verma 	return 0;
26039c686b8SVishal Verma }
26139c686b8SVishal Verma 
2629fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
2639fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
26439c686b8SVishal Verma {
265f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
266f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
2679fb1a190SDave Jiang 	struct badrange_entry *be;
2689fb1a190SDave Jiang 	u64 end = addr + len - 1;
2699fb1a190SDave Jiang 	int i = 0;
270f471f1a7SDan Williams 
271f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
272f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
273f471f1a7SDan Williams 	ars_status->status = 0;
274f471f1a7SDan Williams 	ars_status->address = addr;
275f471f1a7SDan Williams 	ars_status->length = len;
276f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
2779fb1a190SDave Jiang 
2789fb1a190SDave Jiang 	spin_lock(&badrange->lock);
2799fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
2809fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
2819fb1a190SDave Jiang 		u64 rstart, rend;
2829fb1a190SDave Jiang 
2839fb1a190SDave Jiang 		/* skip entries outside the range */
2849fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
2859fb1a190SDave Jiang 			continue;
2869fb1a190SDave Jiang 
2879fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
2889fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
2899fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
290f471f1a7SDan Williams 		ars_record->handle = 0;
2919fb1a190SDave Jiang 		ars_record->err_address = rstart;
2929fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
2939fb1a190SDave Jiang 		i++;
2949fb1a190SDave Jiang 	}
2959fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
2969fb1a190SDave Jiang 	ars_status->num_records = i;
2979fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
2989fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
299f471f1a7SDan Williams }
300f471f1a7SDan Williams 
3019fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
3029fb1a190SDave Jiang 		struct ars_state *ars_state,
303f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
304f471f1a7SDan Williams 		int *cmd_rc)
305f471f1a7SDan Williams {
306f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
30739c686b8SVishal Verma 		return -EINVAL;
30839c686b8SVishal Verma 
309f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
310f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
311f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
312f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
313f471f1a7SDan Williams 	} else {
314f471f1a7SDan Williams 		ars_start->status = 0;
315f471f1a7SDan Williams 		ars_start->scrub_time = 1;
3169fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
317f471f1a7SDan Williams 				ars_start->length);
318f471f1a7SDan Williams 		*cmd_rc = 0;
319f471f1a7SDan Williams 	}
320f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
32139c686b8SVishal Verma 
32239c686b8SVishal Verma 	return 0;
32339c686b8SVishal Verma }
32439c686b8SVishal Verma 
325f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
326f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
327f471f1a7SDan Williams 		int *cmd_rc)
32839c686b8SVishal Verma {
329f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
33039c686b8SVishal Verma 		return -EINVAL;
33139c686b8SVishal Verma 
332f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
333f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
334f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
335f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
336f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
337f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
338f471f1a7SDan Williams 	} else {
339f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
340f471f1a7SDan Williams 				ars_state->ars_status->out_length);
341f471f1a7SDan Williams 		*cmd_rc = 0;
342f471f1a7SDan Williams 	}
343f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
34439c686b8SVishal Verma 	return 0;
34539c686b8SVishal Verma }
34639c686b8SVishal Verma 
347*5e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
348*5e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
349d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
350d4f32367SDan Williams {
351d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
352d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
353d4f32367SDan Williams 		return -EINVAL;
354d4f32367SDan Williams 
355d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
356d4f32367SDan Williams 		return -EINVAL;
357d4f32367SDan Williams 
358*5e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
359d4f32367SDan Williams 	clear_err->status = 0;
360d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
361d4f32367SDan Williams 	*cmd_rc = 0;
362d4f32367SDan Williams 	return 0;
363d4f32367SDan Williams }
364d4f32367SDan Williams 
36510246dc8SYasunori Goto struct region_search_spa {
36610246dc8SYasunori Goto 	u64 addr;
36710246dc8SYasunori Goto 	struct nd_region *region;
36810246dc8SYasunori Goto };
36910246dc8SYasunori Goto 
37010246dc8SYasunori Goto static int is_region_device(struct device *dev)
37110246dc8SYasunori Goto {
37210246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
37310246dc8SYasunori Goto }
37410246dc8SYasunori Goto 
37510246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
37610246dc8SYasunori Goto {
37710246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
37810246dc8SYasunori Goto 	struct nd_region *nd_region;
37910246dc8SYasunori Goto 	resource_size_t ndr_end;
38010246dc8SYasunori Goto 
38110246dc8SYasunori Goto 	if (!is_region_device(dev))
38210246dc8SYasunori Goto 		return 0;
38310246dc8SYasunori Goto 
38410246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
38510246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
38610246dc8SYasunori Goto 
38710246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
38810246dc8SYasunori Goto 		ctx->region = nd_region;
38910246dc8SYasunori Goto 		return 1;
39010246dc8SYasunori Goto 	}
39110246dc8SYasunori Goto 
39210246dc8SYasunori Goto 	return 0;
39310246dc8SYasunori Goto }
39410246dc8SYasunori Goto 
39510246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
39610246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
39710246dc8SYasunori Goto {
39810246dc8SYasunori Goto 	int ret;
39910246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
40010246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
40110246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
40210246dc8SYasunori Goto 	struct region_search_spa ctx = {
40310246dc8SYasunori Goto 		.addr = spa->spa,
40410246dc8SYasunori Goto 		.region = NULL,
40510246dc8SYasunori Goto 	};
40610246dc8SYasunori Goto 	u64 dpa;
40710246dc8SYasunori Goto 
40810246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
40910246dc8SYasunori Goto 				nfit_test_search_region_spa);
41010246dc8SYasunori Goto 
41110246dc8SYasunori Goto 	if (!ret)
41210246dc8SYasunori Goto 		return -ENODEV;
41310246dc8SYasunori Goto 
41410246dc8SYasunori Goto 	nd_region = ctx.region;
41510246dc8SYasunori Goto 
41610246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
41710246dc8SYasunori Goto 
41810246dc8SYasunori Goto 	/*
41910246dc8SYasunori Goto 	 * last dimm is selected for test
42010246dc8SYasunori Goto 	 */
42110246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
42210246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
42310246dc8SYasunori Goto 
42410246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
42510246dc8SYasunori Goto 	spa->num_nvdimms = 1;
42610246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
42710246dc8SYasunori Goto 
42810246dc8SYasunori Goto 	return 0;
42910246dc8SYasunori Goto }
43010246dc8SYasunori Goto 
43110246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
43210246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
43310246dc8SYasunori Goto {
43410246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
43510246dc8SYasunori Goto 		return -EINVAL;
43610246dc8SYasunori Goto 
43710246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
43810246dc8SYasunori Goto 		spa->status = 2;
43910246dc8SYasunori Goto 
44010246dc8SYasunori Goto 	return 0;
44110246dc8SYasunori Goto }
44210246dc8SYasunori Goto 
443baa51277SDan Williams static int nfit_test_cmd_smart(struct nd_cmd_smart *smart, unsigned int buf_len)
444baa51277SDan Williams {
445baa51277SDan Williams 	static const struct nd_smart_payload smart_data = {
446baa51277SDan Williams 		.flags = ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID
447baa51277SDan Williams 			| ND_SMART_SPARES_VALID | ND_SMART_ALARM_VALID
448baa51277SDan Williams 			| ND_SMART_USED_VALID | ND_SMART_SHUTDOWN_VALID,
449baa51277SDan Williams 		.health = ND_SMART_NON_CRITICAL_HEALTH,
450baa51277SDan Williams 		.temperature = 23 * 16,
451baa51277SDan Williams 		.spares = 75,
452baa51277SDan Williams 		.alarm_flags = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
453baa51277SDan Williams 		.life_used = 5,
454baa51277SDan Williams 		.shutdown_state = 0,
455baa51277SDan Williams 		.vendor_size = 0,
456baa51277SDan Williams 	};
457baa51277SDan Williams 
458baa51277SDan Williams 	if (buf_len < sizeof(*smart))
459baa51277SDan Williams 		return -EINVAL;
460baa51277SDan Williams 	memcpy(smart->data, &smart_data, sizeof(smart_data));
461baa51277SDan Williams 	return 0;
462baa51277SDan Williams }
463baa51277SDan Williams 
464baa51277SDan Williams static int nfit_test_cmd_smart_threshold(struct nd_cmd_smart_threshold *smart_t,
465baa51277SDan Williams 		unsigned int buf_len)
466baa51277SDan Williams {
467baa51277SDan Williams 	static const struct nd_smart_threshold_payload smart_t_data = {
468baa51277SDan Williams 		.alarm_control = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
469baa51277SDan Williams 		.temperature = 40 * 16,
470baa51277SDan Williams 		.spares = 5,
471baa51277SDan Williams 	};
472baa51277SDan Williams 
473baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
474baa51277SDan Williams 		return -EINVAL;
475baa51277SDan Williams 	memcpy(smart_t->data, &smart_t_data, sizeof(smart_t_data));
476baa51277SDan Williams 	return 0;
477baa51277SDan Williams }
478baa51277SDan Williams 
4799fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
4809fb1a190SDave Jiang {
4819fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
4829fb1a190SDave Jiang 
4839fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
4849fb1a190SDave Jiang }
4859fb1a190SDave Jiang 
4869fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
4879fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
4889fb1a190SDave Jiang {
4899fb1a190SDave Jiang 	int rc;
4909fb1a190SDave Jiang 
4919fb1a190SDave Jiang 	if (buf_len < sizeof(*err_inj)) {
4929fb1a190SDave Jiang 		rc = -EINVAL;
4939fb1a190SDave Jiang 		goto err;
4949fb1a190SDave Jiang 	}
4959fb1a190SDave Jiang 
4969fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
4979fb1a190SDave Jiang 		rc = -EINVAL;
4989fb1a190SDave Jiang 		goto err;
4999fb1a190SDave Jiang 	}
5009fb1a190SDave Jiang 
5019fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
5029fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
5039fb1a190SDave Jiang 	if (rc < 0)
5049fb1a190SDave Jiang 		goto err;
5059fb1a190SDave Jiang 
5069fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
5079fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
5089fb1a190SDave Jiang 
5099fb1a190SDave Jiang 	err_inj->status = 0;
5109fb1a190SDave Jiang 	return 0;
5119fb1a190SDave Jiang 
5129fb1a190SDave Jiang err:
5139fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
5149fb1a190SDave Jiang 	return rc;
5159fb1a190SDave Jiang }
5169fb1a190SDave Jiang 
5179fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
5189fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
5199fb1a190SDave Jiang {
5209fb1a190SDave Jiang 	int rc;
5219fb1a190SDave Jiang 
5229fb1a190SDave Jiang 	if (buf_len < sizeof(*err_clr)) {
5239fb1a190SDave Jiang 		rc = -EINVAL;
5249fb1a190SDave Jiang 		goto err;
5259fb1a190SDave Jiang 	}
5269fb1a190SDave Jiang 
5279fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
5289fb1a190SDave Jiang 		rc = -EINVAL;
5299fb1a190SDave Jiang 		goto err;
5309fb1a190SDave Jiang 	}
5319fb1a190SDave Jiang 
5329fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
5339fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
5349fb1a190SDave Jiang 
5359fb1a190SDave Jiang 	err_clr->status = 0;
5369fb1a190SDave Jiang 	return 0;
5379fb1a190SDave Jiang 
5389fb1a190SDave Jiang err:
5399fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
5409fb1a190SDave Jiang 	return rc;
5419fb1a190SDave Jiang }
5429fb1a190SDave Jiang 
5439fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
5449fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
5459fb1a190SDave Jiang 		unsigned int buf_len)
5469fb1a190SDave Jiang {
5479fb1a190SDave Jiang 	struct badrange_entry *be;
5489fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
5499fb1a190SDave Jiang 	int i = 0;
5509fb1a190SDave Jiang 
5519fb1a190SDave Jiang 	err_stat->status = 0;
5529fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
5539fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
5549fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
5559fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
5569fb1a190SDave Jiang 		i++;
5579fb1a190SDave Jiang 		if (i > max)
5589fb1a190SDave Jiang 			break;
5599fb1a190SDave Jiang 	}
5609fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
5619fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
5629fb1a190SDave Jiang 
5639fb1a190SDave Jiang 	return 0;
5649fb1a190SDave Jiang }
5659fb1a190SDave Jiang 
56639c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
56739c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
568aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
56939c686b8SVishal Verma {
57039c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
57139c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
5726634fb06SDan Williams 	unsigned int func = cmd;
573f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
574f471f1a7SDan Williams 
575f471f1a7SDan Williams 	if (!cmd_rc)
576f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
577f471f1a7SDan Williams 	*cmd_rc = 0;
57839c686b8SVishal Verma 
57939c686b8SVishal Verma 	if (nvdimm) {
58039c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
581e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
58239c686b8SVishal Verma 
5836634fb06SDan Williams 		if (!nfit_mem)
5846634fb06SDan Williams 			return -ENOTTY;
5856634fb06SDan Williams 
5866634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
5876634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
5886634fb06SDan Williams 
5896634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
5906634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
5916634fb06SDan Williams 			func = call_pkg->nd_command;
5926634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
5936634fb06SDan Williams 				return -ENOTTY;
5946634fb06SDan Williams 		}
5956634fb06SDan Williams 
5966634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
5976634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
59839c686b8SVishal Verma 			return -ENOTTY;
59939c686b8SVishal Verma 
60039c686b8SVishal Verma 		/* lookup label space for the given dimm */
60139c686b8SVishal Verma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
60239c686b8SVishal Verma 			if (__to_nfit_memdev(nfit_mem)->device_handle ==
60339c686b8SVishal Verma 					handle[i])
60439c686b8SVishal Verma 				break;
60539c686b8SVishal Verma 		if (i >= ARRAY_SIZE(handle))
60639c686b8SVishal Verma 			return -ENXIO;
60739c686b8SVishal Verma 
60873606afdSDan Williams 		if ((1 << func) & dimm_fail_cmd_flags[i])
60973606afdSDan Williams 			return -EIO;
61073606afdSDan Williams 
6116634fb06SDan Williams 		switch (func) {
61239c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
61339c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
61439c686b8SVishal Verma 			break;
61539c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
61639c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
617dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
61839c686b8SVishal Verma 			break;
61939c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
62039c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
621dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
62239c686b8SVishal Verma 			break;
623baa51277SDan Williams 		case ND_CMD_SMART:
624baa51277SDan Williams 			rc = nfit_test_cmd_smart(buf, buf_len);
625baa51277SDan Williams 			break;
626baa51277SDan Williams 		case ND_CMD_SMART_THRESHOLD:
627baa51277SDan Williams 			rc = nfit_test_cmd_smart_threshold(buf, buf_len);
628231bf117SDan Williams 			device_lock(&t->pdev.dev);
629231bf117SDan Williams 			__acpi_nvdimm_notify(t->dimm_dev[i], 0x81);
630231bf117SDan Williams 			device_unlock(&t->pdev.dev);
631baa51277SDan Williams 			break;
6326bc75619SDan Williams 		default:
6336bc75619SDan Williams 			return -ENOTTY;
6346bc75619SDan Williams 		}
63539c686b8SVishal Verma 	} else {
636f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
63710246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
63810246dc8SYasunori Goto 
63910246dc8SYasunori Goto 		if (!nd_desc)
64010246dc8SYasunori Goto 			return -ENOTTY;
64110246dc8SYasunori Goto 
64210246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
64310246dc8SYasunori Goto 			func = call_pkg->nd_command;
64410246dc8SYasunori Goto 
64510246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
64610246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
64710246dc8SYasunori Goto 
64810246dc8SYasunori Goto 			switch (func) {
64910246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
65010246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
65110246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
65210246dc8SYasunori Goto 				return rc;
6539fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
6549fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
6559fb1a190SDave Jiang 					buf_len);
6569fb1a190SDave Jiang 				return rc;
6579fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
6589fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
6599fb1a190SDave Jiang 					buf_len);
6609fb1a190SDave Jiang 				return rc;
6619fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
6629fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
6639fb1a190SDave Jiang 					buf_len);
6649fb1a190SDave Jiang 				return rc;
66510246dc8SYasunori Goto 			default:
66610246dc8SYasunori Goto 				return -ENOTTY;
66710246dc8SYasunori Goto 			}
66810246dc8SYasunori Goto 		}
669f471f1a7SDan Williams 
670e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
67139c686b8SVishal Verma 			return -ENOTTY;
67239c686b8SVishal Verma 
6736634fb06SDan Williams 		switch (func) {
67439c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
67539c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
67639c686b8SVishal Verma 			break;
67739c686b8SVishal Verma 		case ND_CMD_ARS_START:
6789fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
6799fb1a190SDave Jiang 					buf_len, cmd_rc);
68039c686b8SVishal Verma 			break;
68139c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
682f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
683f471f1a7SDan Williams 					cmd_rc);
68439c686b8SVishal Verma 			break;
685d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
686*5e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
687d4f32367SDan Williams 			break;
68839c686b8SVishal Verma 		default:
68939c686b8SVishal Verma 			return -ENOTTY;
69039c686b8SVishal Verma 		}
69139c686b8SVishal Verma 	}
6926bc75619SDan Williams 
6936bc75619SDan Williams 	return rc;
6946bc75619SDan Williams }
6956bc75619SDan Williams 
6966bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
6976bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
6986bc75619SDan Williams 
6996bc75619SDan Williams static void release_nfit_res(void *data)
7006bc75619SDan Williams {
7016bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
7026bc75619SDan Williams 
7036bc75619SDan Williams 	spin_lock(&nfit_test_lock);
7046bc75619SDan Williams 	list_del(&nfit_res->list);
7056bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
7066bc75619SDan Williams 
7076bc75619SDan Williams 	vfree(nfit_res->buf);
7086bc75619SDan Williams 	kfree(nfit_res);
7096bc75619SDan Williams }
7106bc75619SDan Williams 
7116bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
7126bc75619SDan Williams 		void *buf)
7136bc75619SDan Williams {
7146bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
7156bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
7166bc75619SDan Williams 			GFP_KERNEL);
7176bc75619SDan Williams 	int rc;
7186bc75619SDan Williams 
719bd4cd745SDan Williams 	if (!buf || !nfit_res)
7206bc75619SDan Williams 		goto err;
7216bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
7226bc75619SDan Williams 	if (rc)
7236bc75619SDan Williams 		goto err;
7246bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
7256bc75619SDan Williams 	memset(buf, 0, size);
7266bc75619SDan Williams 	nfit_res->dev = dev;
7276bc75619SDan Williams 	nfit_res->buf = buf;
728bd4cd745SDan Williams 	nfit_res->res.start = *dma;
729bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
730bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
731bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
732bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
7336bc75619SDan Williams 	spin_lock(&nfit_test_lock);
7346bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
7356bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
7366bc75619SDan Williams 
7376bc75619SDan Williams 	return nfit_res->buf;
7386bc75619SDan Williams  err:
739ee8520feSDan Williams 	if (buf)
7406bc75619SDan Williams 		vfree(buf);
7416bc75619SDan Williams 	kfree(nfit_res);
7426bc75619SDan Williams 	return NULL;
7436bc75619SDan Williams }
7446bc75619SDan Williams 
7456bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
7466bc75619SDan Williams {
7476bc75619SDan Williams 	void *buf = vmalloc(size);
7486bc75619SDan Williams 
7496bc75619SDan Williams 	*dma = (unsigned long) buf;
7506bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
7516bc75619SDan Williams }
7526bc75619SDan Williams 
7536bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
7546bc75619SDan Williams {
7556bc75619SDan Williams 	int i;
7566bc75619SDan Williams 
7576bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
7586bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
7596bc75619SDan Williams 		struct nfit_test *t = instances[i];
7606bc75619SDan Williams 
7616bc75619SDan Williams 		if (!t)
7626bc75619SDan Williams 			continue;
7636bc75619SDan Williams 		spin_lock(&nfit_test_lock);
7646bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
765bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
766bd4cd745SDan Williams 						+ resource_size(&n->res))) {
7676bc75619SDan Williams 				nfit_res = n;
7686bc75619SDan Williams 				break;
7696bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
7706bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
771bd4cd745SDan Williams 						+ resource_size(&n->res))) {
7726bc75619SDan Williams 				nfit_res = n;
7736bc75619SDan Williams 				break;
7746bc75619SDan Williams 			}
7756bc75619SDan Williams 		}
7766bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
7776bc75619SDan Williams 		if (nfit_res)
7786bc75619SDan Williams 			return nfit_res;
7796bc75619SDan Williams 	}
7806bc75619SDan Williams 
7816bc75619SDan Williams 	return NULL;
7826bc75619SDan Williams }
7836bc75619SDan Williams 
784f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
785f471f1a7SDan Williams {
7869fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
787f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
7889fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
789f471f1a7SDan Williams 	if (!ars_state->ars_status)
790f471f1a7SDan Williams 		return -ENOMEM;
791f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
792f471f1a7SDan Williams 	return 0;
793f471f1a7SDan Williams }
794f471f1a7SDan Williams 
795231bf117SDan Williams static void put_dimms(void *data)
796231bf117SDan Williams {
797231bf117SDan Williams 	struct device **dimm_dev = data;
798231bf117SDan Williams 	int i;
799231bf117SDan Williams 
800231bf117SDan Williams 	for (i = 0; i < NUM_DCR; i++)
801231bf117SDan Williams 		if (dimm_dev[i])
802231bf117SDan Williams 			device_unregister(dimm_dev[i]);
803231bf117SDan Williams }
804231bf117SDan Williams 
805231bf117SDan Williams static struct class *nfit_test_dimm;
806231bf117SDan Williams 
80773606afdSDan Williams static int dimm_name_to_id(struct device *dev)
80873606afdSDan Williams {
80973606afdSDan Williams 	int dimm;
81073606afdSDan Williams 
81173606afdSDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1
81273606afdSDan Williams 			|| dimm >= NUM_DCR || dimm < 0)
81373606afdSDan Williams 		return -ENXIO;
81473606afdSDan Williams 	return dimm;
81573606afdSDan Williams }
81673606afdSDan Williams 
81773606afdSDan Williams 
81873606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
81973606afdSDan Williams 		char *buf)
82073606afdSDan Williams {
82173606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
82273606afdSDan Williams 
82373606afdSDan Williams 	if (dimm < 0)
82473606afdSDan Williams 		return dimm;
82573606afdSDan Williams 
82673606afdSDan Williams 	return sprintf(buf, "%#x", handle[dimm]);
82773606afdSDan Williams }
82873606afdSDan Williams DEVICE_ATTR_RO(handle);
82973606afdSDan Williams 
83073606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
83173606afdSDan Williams 		char *buf)
83273606afdSDan Williams {
83373606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
83473606afdSDan Williams 
83573606afdSDan Williams 	if (dimm < 0)
83673606afdSDan Williams 		return dimm;
83773606afdSDan Williams 
83873606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
83973606afdSDan Williams }
84073606afdSDan Williams 
84173606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
84273606afdSDan Williams 		const char *buf, size_t size)
84373606afdSDan Williams {
84473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
84573606afdSDan Williams 	unsigned long val;
84673606afdSDan Williams 	ssize_t rc;
84773606afdSDan Williams 
84873606afdSDan Williams 	if (dimm < 0)
84973606afdSDan Williams 		return dimm;
85073606afdSDan Williams 
85173606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
85273606afdSDan Williams 	if (rc)
85373606afdSDan Williams 		return rc;
85473606afdSDan Williams 
85573606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
85673606afdSDan Williams 	return size;
85773606afdSDan Williams }
85873606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
85973606afdSDan Williams 
86073606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
86173606afdSDan Williams 	&dev_attr_fail_cmd.attr,
86273606afdSDan Williams 	&dev_attr_handle.attr,
86373606afdSDan Williams 	NULL,
86473606afdSDan Williams };
86573606afdSDan Williams 
86673606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
86773606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
86873606afdSDan Williams };
86973606afdSDan Williams 
87073606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
87173606afdSDan Williams 	&nfit_test_dimm_attribute_group,
87273606afdSDan Williams 	NULL,
87373606afdSDan Williams };
87473606afdSDan Williams 
8756bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
8766bc75619SDan Williams {
8776b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
8786bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
8796bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
8803b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
8813b87356fSDan Williams 					window_size) * NUM_DCR
8829d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
88385d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
88485d3fa02SDan Williams 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR;
8856bc75619SDan Williams 	int i;
8866bc75619SDan Williams 
8876bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
8886bc75619SDan Williams 	if (!t->nfit_buf)
8896bc75619SDan Williams 		return -ENOMEM;
8906bc75619SDan Williams 	t->nfit_size = nfit_size;
8916bc75619SDan Williams 
892ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
8936bc75619SDan Williams 	if (!t->spa_set[0])
8946bc75619SDan Williams 		return -ENOMEM;
8956bc75619SDan Williams 
896ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
8976bc75619SDan Williams 	if (!t->spa_set[1])
8986bc75619SDan Williams 		return -ENOMEM;
8996bc75619SDan Williams 
900ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
90120985164SVishal Verma 	if (!t->spa_set[2])
90220985164SVishal Verma 		return -ENOMEM;
90320985164SVishal Verma 
904dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
9056bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
9066bc75619SDan Williams 		if (!t->dimm[i])
9076bc75619SDan Williams 			return -ENOMEM;
9086bc75619SDan Williams 
9096bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
9106bc75619SDan Williams 		if (!t->label[i])
9116bc75619SDan Williams 			return -ENOMEM;
9126bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
9139d27a87eSDan Williams 
9149d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
9159d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
91685d3fa02SDan Williams 				&t->flush_dma[i]);
9179d27a87eSDan Williams 		if (!t->flush[i])
9189d27a87eSDan Williams 			return -ENOMEM;
9196bc75619SDan Williams 	}
9206bc75619SDan Williams 
921dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
9226bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
9236bc75619SDan Williams 		if (!t->dcr[i])
9246bc75619SDan Williams 			return -ENOMEM;
9256bc75619SDan Williams 	}
9266bc75619SDan Williams 
927c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
928c14a868aSDan Williams 	if (!t->_fit)
929c14a868aSDan Williams 		return -ENOMEM;
930c14a868aSDan Williams 
931231bf117SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t->dimm_dev))
932231bf117SDan Williams 		return -ENOMEM;
933231bf117SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
93473606afdSDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
93573606afdSDan Williams 				&t->pdev.dev, 0, NULL,
93673606afdSDan Williams 				nfit_test_dimm_attribute_groups,
93773606afdSDan Williams 				"test_dimm%d", i);
938231bf117SDan Williams 		if (!t->dimm_dev[i])
939231bf117SDan Williams 			return -ENOMEM;
940231bf117SDan Williams 	}
941231bf117SDan Williams 
942f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
9436bc75619SDan Williams }
9446bc75619SDan Williams 
9456bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
9466bc75619SDan Williams {
9477bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
948ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
949ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
950dafb1048SDan Williams 	int i;
9516bc75619SDan Williams 
9526bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
9536bc75619SDan Williams 	if (!t->nfit_buf)
9546bc75619SDan Williams 		return -ENOMEM;
9556bc75619SDan Williams 	t->nfit_size = nfit_size;
9566bc75619SDan Williams 
957ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
9586bc75619SDan Williams 	if (!t->spa_set[0])
9596bc75619SDan Williams 		return -ENOMEM;
9606bc75619SDan Williams 
961dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
962dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
963dafb1048SDan Williams 		if (!t->label[i])
964dafb1048SDan Williams 			return -ENOMEM;
965dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
966dafb1048SDan Williams 	}
967dafb1048SDan Williams 
9687bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
9697bfe97c7SDan Williams 	if (!t->spa_set[1])
9707bfe97c7SDan Williams 		return -ENOMEM;
9717bfe97c7SDan Williams 
972f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
9736bc75619SDan Williams }
9746bc75619SDan Williams 
9755dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
9765dc68e55SDan Williams {
9775dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
9785dc68e55SDan Williams 	dcr->device_id = 0;
9795dc68e55SDan Williams 	dcr->revision_id = 1;
9805dc68e55SDan Williams 	dcr->valid_fields = 1;
9815dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
9825dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
9835dc68e55SDan Williams }
9845dc68e55SDan Williams 
9856bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
9866bc75619SDan Williams {
98785d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
98885d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
9896bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
9906bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
9916bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
9926bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
9936bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
9946bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
9959d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
99685d3fa02SDan Williams 	unsigned int offset, i;
9976bc75619SDan Williams 
9986bc75619SDan Williams 	/*
9996bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
10006bc75619SDan Williams 	 * does not actually alias the related block-data-window
10016bc75619SDan Williams 	 * regions)
10026bc75619SDan Williams 	 */
10036b577c9dSLinda Knippers 	spa = nfit_buf;
10046bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10056bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10066bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
10076bc75619SDan Williams 	spa->range_index = 0+1;
10086bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
10096bc75619SDan Williams 	spa->length = SPA0_SIZE;
10106bc75619SDan Williams 
10116bc75619SDan Williams 	/*
10126bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
10136bc75619SDan Williams 	 * does not actually alias the related block-data-window
10146bc75619SDan Williams 	 * regions)
10156bc75619SDan Williams 	 */
10166b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa);
10176bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10186bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10196bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
10206bc75619SDan Williams 	spa->range_index = 1+1;
10216bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
10226bc75619SDan Williams 	spa->length = SPA1_SIZE;
10236bc75619SDan Williams 
10246bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
10256b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 2;
10266bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10276bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10286bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
10296bc75619SDan Williams 	spa->range_index = 2+1;
10306bc75619SDan Williams 	spa->address = t->dcr_dma[0];
10316bc75619SDan Williams 	spa->length = DCR_SIZE;
10326bc75619SDan Williams 
10336bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
10346b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 3;
10356bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10366bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10376bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
10386bc75619SDan Williams 	spa->range_index = 3+1;
10396bc75619SDan Williams 	spa->address = t->dcr_dma[1];
10406bc75619SDan Williams 	spa->length = DCR_SIZE;
10416bc75619SDan Williams 
10426bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
10436b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 4;
10446bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10456bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10466bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
10476bc75619SDan Williams 	spa->range_index = 4+1;
10486bc75619SDan Williams 	spa->address = t->dcr_dma[2];
10496bc75619SDan Williams 	spa->length = DCR_SIZE;
10506bc75619SDan Williams 
10516bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
10526b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 5;
10536bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10546bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10556bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
10566bc75619SDan Williams 	spa->range_index = 5+1;
10576bc75619SDan Williams 	spa->address = t->dcr_dma[3];
10586bc75619SDan Williams 	spa->length = DCR_SIZE;
10596bc75619SDan Williams 
10606bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
10616b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 6;
10626bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10636bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10646bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
10656bc75619SDan Williams 	spa->range_index = 6+1;
10666bc75619SDan Williams 	spa->address = t->dimm_dma[0];
10676bc75619SDan Williams 	spa->length = DIMM_SIZE;
10686bc75619SDan Williams 
10696bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
10706b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 7;
10716bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10726bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10736bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
10746bc75619SDan Williams 	spa->range_index = 7+1;
10756bc75619SDan Williams 	spa->address = t->dimm_dma[1];
10766bc75619SDan Williams 	spa->length = DIMM_SIZE;
10776bc75619SDan Williams 
10786bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
10796b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 8;
10806bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10816bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10826bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
10836bc75619SDan Williams 	spa->range_index = 8+1;
10846bc75619SDan Williams 	spa->address = t->dimm_dma[2];
10856bc75619SDan Williams 	spa->length = DIMM_SIZE;
10866bc75619SDan Williams 
10876bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
10886b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 9;
10896bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
10906bc75619SDan Williams 	spa->header.length = sizeof(*spa);
10916bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
10926bc75619SDan Williams 	spa->range_index = 9+1;
10936bc75619SDan Williams 	spa->address = t->dimm_dma[3];
10946bc75619SDan Williams 	spa->length = DIMM_SIZE;
10956bc75619SDan Williams 
10966b577c9dSLinda Knippers 	offset = sizeof(*spa) * 10;
10976bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
10986bc75619SDan Williams 	memdev = nfit_buf + offset;
10996bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11006bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11016bc75619SDan Williams 	memdev->device_handle = handle[0];
11026bc75619SDan Williams 	memdev->physical_id = 0;
11036bc75619SDan Williams 	memdev->region_id = 0;
11046bc75619SDan Williams 	memdev->range_index = 0+1;
11053b87356fSDan Williams 	memdev->region_index = 4+1;
11066bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1107df06a2d5SDan Williams 	memdev->region_offset = 1;
11086bc75619SDan Williams 	memdev->address = 0;
11096bc75619SDan Williams 	memdev->interleave_index = 0;
11106bc75619SDan Williams 	memdev->interleave_ways = 2;
11116bc75619SDan Williams 
11126bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
11136bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
11146bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11156bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11166bc75619SDan Williams 	memdev->device_handle = handle[1];
11176bc75619SDan Williams 	memdev->physical_id = 1;
11186bc75619SDan Williams 	memdev->region_id = 0;
11196bc75619SDan Williams 	memdev->range_index = 0+1;
11203b87356fSDan Williams 	memdev->region_index = 5+1;
11216bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1122df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
11236bc75619SDan Williams 	memdev->address = 0;
11246bc75619SDan Williams 	memdev->interleave_index = 0;
11256bc75619SDan Williams 	memdev->interleave_ways = 2;
1126ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
11276bc75619SDan Williams 
11286bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
11296bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
11306bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11316bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11326bc75619SDan Williams 	memdev->device_handle = handle[0];
11336bc75619SDan Williams 	memdev->physical_id = 0;
11346bc75619SDan Williams 	memdev->region_id = 1;
11356bc75619SDan Williams 	memdev->range_index = 1+1;
11363b87356fSDan Williams 	memdev->region_index = 4+1;
11376bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1138df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
11396bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
11406bc75619SDan Williams 	memdev->interleave_index = 0;
11416bc75619SDan Williams 	memdev->interleave_ways = 4;
1142ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
11436bc75619SDan Williams 
11446bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
11456bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
11466bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11476bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11486bc75619SDan Williams 	memdev->device_handle = handle[1];
11496bc75619SDan Williams 	memdev->physical_id = 1;
11506bc75619SDan Williams 	memdev->region_id = 1;
11516bc75619SDan Williams 	memdev->range_index = 1+1;
11523b87356fSDan Williams 	memdev->region_index = 5+1;
11536bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1154df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
11556bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
11566bc75619SDan Williams 	memdev->interleave_index = 0;
11576bc75619SDan Williams 	memdev->interleave_ways = 4;
11586bc75619SDan Williams 
11596bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
11606bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
11616bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11626bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11636bc75619SDan Williams 	memdev->device_handle = handle[2];
11646bc75619SDan Williams 	memdev->physical_id = 2;
11656bc75619SDan Williams 	memdev->region_id = 0;
11666bc75619SDan Williams 	memdev->range_index = 1+1;
11673b87356fSDan Williams 	memdev->region_index = 6+1;
11686bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1169df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
11706bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
11716bc75619SDan Williams 	memdev->interleave_index = 0;
11726bc75619SDan Williams 	memdev->interleave_ways = 4;
1173ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
11746bc75619SDan Williams 
11756bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
11766bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
11776bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11786bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11796bc75619SDan Williams 	memdev->device_handle = handle[3];
11806bc75619SDan Williams 	memdev->physical_id = 3;
11816bc75619SDan Williams 	memdev->region_id = 0;
11826bc75619SDan Williams 	memdev->range_index = 1+1;
11833b87356fSDan Williams 	memdev->region_index = 7+1;
11846bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1185df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
11866bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
11876bc75619SDan Williams 	memdev->interleave_index = 0;
11886bc75619SDan Williams 	memdev->interleave_ways = 4;
11896bc75619SDan Williams 
11906bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
11916bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
11926bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
11936bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
11946bc75619SDan Williams 	memdev->device_handle = handle[0];
11956bc75619SDan Williams 	memdev->physical_id = 0;
11966bc75619SDan Williams 	memdev->region_id = 0;
11976bc75619SDan Williams 	memdev->range_index = 2+1;
11986bc75619SDan Williams 	memdev->region_index = 0+1;
11996bc75619SDan Williams 	memdev->region_size = 0;
12006bc75619SDan Williams 	memdev->region_offset = 0;
12016bc75619SDan Williams 	memdev->address = 0;
12026bc75619SDan Williams 	memdev->interleave_index = 0;
12036bc75619SDan Williams 	memdev->interleave_ways = 1;
12046bc75619SDan Williams 
12056bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
12066bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
12076bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12086bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12096bc75619SDan Williams 	memdev->device_handle = handle[1];
12106bc75619SDan Williams 	memdev->physical_id = 1;
12116bc75619SDan Williams 	memdev->region_id = 0;
12126bc75619SDan Williams 	memdev->range_index = 3+1;
12136bc75619SDan Williams 	memdev->region_index = 1+1;
12146bc75619SDan Williams 	memdev->region_size = 0;
12156bc75619SDan Williams 	memdev->region_offset = 0;
12166bc75619SDan Williams 	memdev->address = 0;
12176bc75619SDan Williams 	memdev->interleave_index = 0;
12186bc75619SDan Williams 	memdev->interleave_ways = 1;
12196bc75619SDan Williams 
12206bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
12216bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
12226bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12236bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12246bc75619SDan Williams 	memdev->device_handle = handle[2];
12256bc75619SDan Williams 	memdev->physical_id = 2;
12266bc75619SDan Williams 	memdev->region_id = 0;
12276bc75619SDan Williams 	memdev->range_index = 4+1;
12286bc75619SDan Williams 	memdev->region_index = 2+1;
12296bc75619SDan Williams 	memdev->region_size = 0;
12306bc75619SDan Williams 	memdev->region_offset = 0;
12316bc75619SDan Williams 	memdev->address = 0;
12326bc75619SDan Williams 	memdev->interleave_index = 0;
12336bc75619SDan Williams 	memdev->interleave_ways = 1;
12346bc75619SDan Williams 
12356bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
12366bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
12376bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12386bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12396bc75619SDan Williams 	memdev->device_handle = handle[3];
12406bc75619SDan Williams 	memdev->physical_id = 3;
12416bc75619SDan Williams 	memdev->region_id = 0;
12426bc75619SDan Williams 	memdev->range_index = 5+1;
12436bc75619SDan Williams 	memdev->region_index = 3+1;
12446bc75619SDan Williams 	memdev->region_size = 0;
12456bc75619SDan Williams 	memdev->region_offset = 0;
12466bc75619SDan Williams 	memdev->address = 0;
12476bc75619SDan Williams 	memdev->interleave_index = 0;
12486bc75619SDan Williams 	memdev->interleave_ways = 1;
12496bc75619SDan Williams 
12506bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
12516bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
12526bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12536bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12546bc75619SDan Williams 	memdev->device_handle = handle[0];
12556bc75619SDan Williams 	memdev->physical_id = 0;
12566bc75619SDan Williams 	memdev->region_id = 0;
12576bc75619SDan Williams 	memdev->range_index = 6+1;
12586bc75619SDan Williams 	memdev->region_index = 0+1;
12596bc75619SDan Williams 	memdev->region_size = 0;
12606bc75619SDan Williams 	memdev->region_offset = 0;
12616bc75619SDan Williams 	memdev->address = 0;
12626bc75619SDan Williams 	memdev->interleave_index = 0;
12636bc75619SDan Williams 	memdev->interleave_ways = 1;
12646bc75619SDan Williams 
12656bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
12666bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
12676bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12686bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12696bc75619SDan Williams 	memdev->device_handle = handle[1];
12706bc75619SDan Williams 	memdev->physical_id = 1;
12716bc75619SDan Williams 	memdev->region_id = 0;
12726bc75619SDan Williams 	memdev->range_index = 7+1;
12736bc75619SDan Williams 	memdev->region_index = 1+1;
12746bc75619SDan Williams 	memdev->region_size = 0;
12756bc75619SDan Williams 	memdev->region_offset = 0;
12766bc75619SDan Williams 	memdev->address = 0;
12776bc75619SDan Williams 	memdev->interleave_index = 0;
12786bc75619SDan Williams 	memdev->interleave_ways = 1;
12796bc75619SDan Williams 
12806bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
12816bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
12826bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12836bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12846bc75619SDan Williams 	memdev->device_handle = handle[2];
12856bc75619SDan Williams 	memdev->physical_id = 2;
12866bc75619SDan Williams 	memdev->region_id = 0;
12876bc75619SDan Williams 	memdev->range_index = 8+1;
12886bc75619SDan Williams 	memdev->region_index = 2+1;
12896bc75619SDan Williams 	memdev->region_size = 0;
12906bc75619SDan Williams 	memdev->region_offset = 0;
12916bc75619SDan Williams 	memdev->address = 0;
12926bc75619SDan Williams 	memdev->interleave_index = 0;
12936bc75619SDan Williams 	memdev->interleave_ways = 1;
12946bc75619SDan Williams 
12956bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
12966bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
12976bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
12986bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
12996bc75619SDan Williams 	memdev->device_handle = handle[3];
13006bc75619SDan Williams 	memdev->physical_id = 3;
13016bc75619SDan Williams 	memdev->region_id = 0;
13026bc75619SDan Williams 	memdev->range_index = 9+1;
13036bc75619SDan Williams 	memdev->region_index = 3+1;
13046bc75619SDan Williams 	memdev->region_size = 0;
13056bc75619SDan Williams 	memdev->region_offset = 0;
13066bc75619SDan Williams 	memdev->address = 0;
13076bc75619SDan Williams 	memdev->interleave_index = 0;
13086bc75619SDan Williams 	memdev->interleave_ways = 1;
1309ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
13106bc75619SDan Williams 
13116bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
13123b87356fSDan Williams 	/* dcr-descriptor0: blk */
13136bc75619SDan Williams 	dcr = nfit_buf + offset;
13146bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13156bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
13166bc75619SDan Williams 	dcr->region_index = 0+1;
13175dc68e55SDan Williams 	dcr_common_init(dcr);
13186bc75619SDan Williams 	dcr->serial_number = ~handle[0];
1319be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
13206bc75619SDan Williams 	dcr->windows = 1;
13216bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
13226bc75619SDan Williams 	dcr->command_offset = 0;
13236bc75619SDan Williams 	dcr->command_size = 8;
13246bc75619SDan Williams 	dcr->status_offset = 8;
13256bc75619SDan Williams 	dcr->status_size = 4;
13266bc75619SDan Williams 
13273b87356fSDan Williams 	/* dcr-descriptor1: blk */
13286bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
13296bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13306bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
13316bc75619SDan Williams 	dcr->region_index = 1+1;
13325dc68e55SDan Williams 	dcr_common_init(dcr);
13336bc75619SDan Williams 	dcr->serial_number = ~handle[1];
1334be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
13356bc75619SDan Williams 	dcr->windows = 1;
13366bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
13376bc75619SDan Williams 	dcr->command_offset = 0;
13386bc75619SDan Williams 	dcr->command_size = 8;
13396bc75619SDan Williams 	dcr->status_offset = 8;
13406bc75619SDan Williams 	dcr->status_size = 4;
13416bc75619SDan Williams 
13423b87356fSDan Williams 	/* dcr-descriptor2: blk */
13436bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
13446bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13456bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
13466bc75619SDan Williams 	dcr->region_index = 2+1;
13475dc68e55SDan Williams 	dcr_common_init(dcr);
13486bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1349be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
13506bc75619SDan Williams 	dcr->windows = 1;
13516bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
13526bc75619SDan Williams 	dcr->command_offset = 0;
13536bc75619SDan Williams 	dcr->command_size = 8;
13546bc75619SDan Williams 	dcr->status_offset = 8;
13556bc75619SDan Williams 	dcr->status_size = 4;
13566bc75619SDan Williams 
13573b87356fSDan Williams 	/* dcr-descriptor3: blk */
13586bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
13596bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13606bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
13616bc75619SDan Williams 	dcr->region_index = 3+1;
13625dc68e55SDan Williams 	dcr_common_init(dcr);
13636bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1364be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
13656bc75619SDan Williams 	dcr->windows = 1;
13666bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
13676bc75619SDan Williams 	dcr->command_offset = 0;
13686bc75619SDan Williams 	dcr->command_size = 8;
13696bc75619SDan Williams 	dcr->status_offset = 8;
13706bc75619SDan Williams 	dcr->status_size = 4;
13716bc75619SDan Williams 
13726bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
13733b87356fSDan Williams 	/* dcr-descriptor0: pmem */
13743b87356fSDan Williams 	dcr = nfit_buf + offset;
13753b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13763b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
13773b87356fSDan Williams 			window_size);
13783b87356fSDan Williams 	dcr->region_index = 4+1;
13795dc68e55SDan Williams 	dcr_common_init(dcr);
13803b87356fSDan Williams 	dcr->serial_number = ~handle[0];
13813b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
13823b87356fSDan Williams 	dcr->windows = 0;
13833b87356fSDan Williams 
13843b87356fSDan Williams 	/* dcr-descriptor1: pmem */
13853b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
13863b87356fSDan Williams 			window_size);
13873b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
13883b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
13893b87356fSDan Williams 			window_size);
13903b87356fSDan Williams 	dcr->region_index = 5+1;
13915dc68e55SDan Williams 	dcr_common_init(dcr);
13923b87356fSDan Williams 	dcr->serial_number = ~handle[1];
13933b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
13943b87356fSDan Williams 	dcr->windows = 0;
13953b87356fSDan Williams 
13963b87356fSDan Williams 	/* dcr-descriptor2: pmem */
13973b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
13983b87356fSDan Williams 			window_size) * 2;
13993b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
14003b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
14013b87356fSDan Williams 			window_size);
14023b87356fSDan Williams 	dcr->region_index = 6+1;
14035dc68e55SDan Williams 	dcr_common_init(dcr);
14043b87356fSDan Williams 	dcr->serial_number = ~handle[2];
14053b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
14063b87356fSDan Williams 	dcr->windows = 0;
14073b87356fSDan Williams 
14083b87356fSDan Williams 	/* dcr-descriptor3: pmem */
14093b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
14103b87356fSDan Williams 			window_size) * 3;
14113b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
14123b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
14133b87356fSDan Williams 			window_size);
14143b87356fSDan Williams 	dcr->region_index = 7+1;
14155dc68e55SDan Williams 	dcr_common_init(dcr);
14163b87356fSDan Williams 	dcr->serial_number = ~handle[3];
14173b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
14183b87356fSDan Williams 	dcr->windows = 0;
14193b87356fSDan Williams 
14203b87356fSDan Williams 	offset = offset + offsetof(struct acpi_nfit_control_region,
14213b87356fSDan Williams 			window_size) * 4;
14226bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
14236bc75619SDan Williams 	bdw = nfit_buf + offset;
14246bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
14256bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
14266bc75619SDan Williams 	bdw->region_index = 0+1;
14276bc75619SDan Williams 	bdw->windows = 1;
14286bc75619SDan Williams 	bdw->offset = 0;
14296bc75619SDan Williams 	bdw->size = BDW_SIZE;
14306bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
14316bc75619SDan Williams 	bdw->start_address = 0;
14326bc75619SDan Williams 
14336bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
14346bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
14356bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
14366bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
14376bc75619SDan Williams 	bdw->region_index = 1+1;
14386bc75619SDan Williams 	bdw->windows = 1;
14396bc75619SDan Williams 	bdw->offset = 0;
14406bc75619SDan Williams 	bdw->size = BDW_SIZE;
14416bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
14426bc75619SDan Williams 	bdw->start_address = 0;
14436bc75619SDan Williams 
14446bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
14456bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
14466bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
14476bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
14486bc75619SDan Williams 	bdw->region_index = 2+1;
14496bc75619SDan Williams 	bdw->windows = 1;
14506bc75619SDan Williams 	bdw->offset = 0;
14516bc75619SDan Williams 	bdw->size = BDW_SIZE;
14526bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
14536bc75619SDan Williams 	bdw->start_address = 0;
14546bc75619SDan Williams 
14556bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
14566bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
14576bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
14586bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
14596bc75619SDan Williams 	bdw->region_index = 3+1;
14606bc75619SDan Williams 	bdw->windows = 1;
14616bc75619SDan Williams 	bdw->offset = 0;
14626bc75619SDan Williams 	bdw->size = BDW_SIZE;
14636bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
14646bc75619SDan Williams 	bdw->start_address = 0;
14656bc75619SDan Williams 
14669d27a87eSDan Williams 	offset = offset + sizeof(struct acpi_nfit_data_region) * 4;
14679d27a87eSDan Williams 	/* flush0 (dimm0) */
14689d27a87eSDan Williams 	flush = nfit_buf + offset;
14699d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
147085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
14719d27a87eSDan Williams 	flush->device_handle = handle[0];
147285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
147385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
147485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
14759d27a87eSDan Williams 
14769d27a87eSDan Williams 	/* flush1 (dimm1) */
147785d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size * 1;
14789d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
147985d3fa02SDan Williams 	flush->header.length = flush_hint_size;
14809d27a87eSDan Williams 	flush->device_handle = handle[1];
148185d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
148285d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
148385d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
14849d27a87eSDan Williams 
14859d27a87eSDan Williams 	/* flush2 (dimm2) */
148685d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size  * 2;
14879d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
148885d3fa02SDan Williams 	flush->header.length = flush_hint_size;
14899d27a87eSDan Williams 	flush->device_handle = handle[2];
149085d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
149185d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
149285d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
14939d27a87eSDan Williams 
14949d27a87eSDan Williams 	/* flush3 (dimm3) */
149585d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size * 3;
14969d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
149785d3fa02SDan Williams 	flush->header.length = flush_hint_size;
14989d27a87eSDan Williams 	flush->device_handle = handle[3];
149985d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
150085d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
150185d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
15029d27a87eSDan Williams 
150320985164SVishal Verma 	if (t->setup_hotplug) {
150485d3fa02SDan Williams 		offset = offset + flush_hint_size * 4;
15053b87356fSDan Williams 		/* dcr-descriptor4: blk */
150620985164SVishal Verma 		dcr = nfit_buf + offset;
150720985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
150820985164SVishal Verma 		dcr->header.length = sizeof(struct acpi_nfit_control_region);
15093b87356fSDan Williams 		dcr->region_index = 8+1;
15105dc68e55SDan Williams 		dcr_common_init(dcr);
151120985164SVishal Verma 		dcr->serial_number = ~handle[4];
1512be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
151320985164SVishal Verma 		dcr->windows = 1;
151420985164SVishal Verma 		dcr->window_size = DCR_SIZE;
151520985164SVishal Verma 		dcr->command_offset = 0;
151620985164SVishal Verma 		dcr->command_size = 8;
151720985164SVishal Verma 		dcr->status_offset = 8;
151820985164SVishal Verma 		dcr->status_size = 4;
151920985164SVishal Verma 
152020985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_control_region);
15213b87356fSDan Williams 		/* dcr-descriptor4: pmem */
15223b87356fSDan Williams 		dcr = nfit_buf + offset;
15233b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
15243b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
15253b87356fSDan Williams 				window_size);
15263b87356fSDan Williams 		dcr->region_index = 9+1;
15275dc68e55SDan Williams 		dcr_common_init(dcr);
15283b87356fSDan Williams 		dcr->serial_number = ~handle[4];
15293b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
15303b87356fSDan Williams 		dcr->windows = 0;
15313b87356fSDan Williams 
15323b87356fSDan Williams 		offset = offset + offsetof(struct acpi_nfit_control_region,
15333b87356fSDan Williams 				window_size);
153420985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
153520985164SVishal Verma 		bdw = nfit_buf + offset;
153620985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
153720985164SVishal Verma 		bdw->header.length = sizeof(struct acpi_nfit_data_region);
15383b87356fSDan Williams 		bdw->region_index = 8+1;
153920985164SVishal Verma 		bdw->windows = 1;
154020985164SVishal Verma 		bdw->offset = 0;
154120985164SVishal Verma 		bdw->size = BDW_SIZE;
154220985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
154320985164SVishal Verma 		bdw->start_address = 0;
154420985164SVishal Verma 
154520985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_data_region);
154620985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
154720985164SVishal Verma 		spa = nfit_buf + offset;
154820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
154920985164SVishal Verma 		spa->header.length = sizeof(*spa);
155020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
155120985164SVishal Verma 		spa->range_index = 10+1;
155220985164SVishal Verma 		spa->address = t->dcr_dma[4];
155320985164SVishal Verma 		spa->length = DCR_SIZE;
155420985164SVishal Verma 
155520985164SVishal Verma 		/*
155620985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
155720985164SVishal Verma 		 * does not actually alias the related block-data-window
155820985164SVishal Verma 		 * regions)
155920985164SVishal Verma 		 */
156020985164SVishal Verma 		spa = nfit_buf + offset + sizeof(*spa);
156120985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
156220985164SVishal Verma 		spa->header.length = sizeof(*spa);
156320985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
156420985164SVishal Verma 		spa->range_index = 11+1;
156520985164SVishal Verma 		spa->address = t->spa_set_dma[2];
156620985164SVishal Verma 		spa->length = SPA0_SIZE;
156720985164SVishal Verma 
156820985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
156920985164SVishal Verma 		spa = nfit_buf + offset + sizeof(*spa) * 2;
157020985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
157120985164SVishal Verma 		spa->header.length = sizeof(*spa);
157220985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
157320985164SVishal Verma 		spa->range_index = 12+1;
157420985164SVishal Verma 		spa->address = t->dimm_dma[4];
157520985164SVishal Verma 		spa->length = DIMM_SIZE;
157620985164SVishal Verma 
157720985164SVishal Verma 		offset = offset + sizeof(*spa) * 3;
157820985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
157920985164SVishal Verma 		memdev = nfit_buf + offset;
158020985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
158120985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
158220985164SVishal Verma 		memdev->device_handle = handle[4];
158320985164SVishal Verma 		memdev->physical_id = 4;
158420985164SVishal Verma 		memdev->region_id = 0;
158520985164SVishal Verma 		memdev->range_index = 10+1;
15863b87356fSDan Williams 		memdev->region_index = 8+1;
158720985164SVishal Verma 		memdev->region_size = 0;
158820985164SVishal Verma 		memdev->region_offset = 0;
158920985164SVishal Verma 		memdev->address = 0;
159020985164SVishal Verma 		memdev->interleave_index = 0;
159120985164SVishal Verma 		memdev->interleave_ways = 1;
159220985164SVishal Verma 
159320985164SVishal Verma 		/* mem-region15 (spa0, dimm4) */
159420985164SVishal Verma 		memdev = nfit_buf + offset +
159520985164SVishal Verma 				sizeof(struct acpi_nfit_memory_map);
159620985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
159720985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
159820985164SVishal Verma 		memdev->device_handle = handle[4];
159920985164SVishal Verma 		memdev->physical_id = 4;
160020985164SVishal Verma 		memdev->region_id = 0;
160120985164SVishal Verma 		memdev->range_index = 11+1;
16023b87356fSDan Williams 		memdev->region_index = 9+1;
160320985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
1604df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
160520985164SVishal Verma 		memdev->address = 0;
160620985164SVishal Verma 		memdev->interleave_index = 0;
160720985164SVishal Verma 		memdev->interleave_ways = 1;
1608ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
160920985164SVishal Verma 
16103b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
161120985164SVishal Verma 		memdev = nfit_buf + offset +
161220985164SVishal Verma 				sizeof(struct acpi_nfit_memory_map) * 2;
161320985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
161420985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
161520985164SVishal Verma 		memdev->device_handle = handle[4];
161620985164SVishal Verma 		memdev->physical_id = 4;
161720985164SVishal Verma 		memdev->region_id = 0;
161820985164SVishal Verma 		memdev->range_index = 12+1;
16193b87356fSDan Williams 		memdev->region_index = 8+1;
162020985164SVishal Verma 		memdev->region_size = 0;
162120985164SVishal Verma 		memdev->region_offset = 0;
162220985164SVishal Verma 		memdev->address = 0;
162320985164SVishal Verma 		memdev->interleave_index = 0;
162420985164SVishal Verma 		memdev->interleave_ways = 1;
162520985164SVishal Verma 
162620985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_memory_map) * 3;
162720985164SVishal Verma 		/* flush3 (dimm4) */
162820985164SVishal Verma 		flush = nfit_buf + offset;
162920985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
163085d3fa02SDan Williams 		flush->header.length = flush_hint_size;
163120985164SVishal Verma 		flush->device_handle = handle[4];
163285d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
163385d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
163485d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
163585d3fa02SDan Williams 				+ i * sizeof(u64);
163620985164SVishal Verma 	}
163720985164SVishal Verma 
16389fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
16399fb1a190SDave Jiang 			SPA0_SIZE);
1640f471f1a7SDan Williams 
16416bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
1642e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
1643e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1644e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
16451f716d05SDan Williams 	set_bit(ND_CMD_SMART, &acpi_desc->dimm_cmd_force_en);
1646e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1647e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1648e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1649e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
165010246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
16511f716d05SDan Williams 	set_bit(ND_CMD_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
165210246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
16539fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
16549fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
16559fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
16566bc75619SDan Williams }
16576bc75619SDan Williams 
16586bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
16596bc75619SDan Williams {
16606b577c9dSLinda Knippers 	size_t offset;
16616bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
16626bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
16636bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
16646bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
1665d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
16666bc75619SDan Williams 
16676b577c9dSLinda Knippers 	offset = 0;
16686bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
16696bc75619SDan Williams 	spa = nfit_buf + offset;
16706bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
16716bc75619SDan Williams 	spa->header.length = sizeof(*spa);
16726bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
16736bc75619SDan Williams 	spa->range_index = 0+1;
16746bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
16756bc75619SDan Williams 	spa->length = SPA2_SIZE;
16766bc75619SDan Williams 
16777bfe97c7SDan Williams 	/* virtual cd region */
16787bfe97c7SDan Williams 	spa = nfit_buf + sizeof(*spa);
16797bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
16807bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
16817bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
16827bfe97c7SDan Williams 	spa->range_index = 0;
16837bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
16847bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
16857bfe97c7SDan Williams 
16867bfe97c7SDan Williams 	offset += sizeof(*spa) * 2;
16876bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
16886bc75619SDan Williams 	memdev = nfit_buf + offset;
16896bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16906bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
1691dafb1048SDan Williams 	memdev->device_handle = handle[5];
16926bc75619SDan Williams 	memdev->physical_id = 0;
16936bc75619SDan Williams 	memdev->region_id = 0;
16946bc75619SDan Williams 	memdev->range_index = 0+1;
16956bc75619SDan Williams 	memdev->region_index = 0+1;
16966bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
16976bc75619SDan Williams 	memdev->region_offset = 0;
16986bc75619SDan Williams 	memdev->address = 0;
16996bc75619SDan Williams 	memdev->interleave_index = 0;
17006bc75619SDan Williams 	memdev->interleave_ways = 1;
170158138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
170258138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
1703f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
17046bc75619SDan Williams 
17056bc75619SDan Williams 	offset += sizeof(*memdev);
17066bc75619SDan Williams 	/* dcr-descriptor0 */
17076bc75619SDan Williams 	dcr = nfit_buf + offset;
17086bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17093b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
17103b87356fSDan Williams 			window_size);
17116bc75619SDan Williams 	dcr->region_index = 0+1;
17125dc68e55SDan Williams 	dcr_common_init(dcr);
1713dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
1714be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
17156bc75619SDan Williams 	dcr->windows = 0;
1716d26f73f0SDan Williams 
1717ac40b675SDan Williams 	offset += dcr->header.length;
1718ac40b675SDan Williams 	memdev = nfit_buf + offset;
1719ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1720ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
1721ac40b675SDan Williams 	memdev->device_handle = handle[6];
1722ac40b675SDan Williams 	memdev->physical_id = 0;
1723ac40b675SDan Williams 	memdev->region_id = 0;
1724ac40b675SDan Williams 	memdev->range_index = 0;
1725ac40b675SDan Williams 	memdev->region_index = 0+2;
1726ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
1727ac40b675SDan Williams 	memdev->region_offset = 0;
1728ac40b675SDan Williams 	memdev->address = 0;
1729ac40b675SDan Williams 	memdev->interleave_index = 0;
1730ac40b675SDan Williams 	memdev->interleave_ways = 1;
1731ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
1732ac40b675SDan Williams 
1733ac40b675SDan Williams 	/* dcr-descriptor1 */
1734ac40b675SDan Williams 	offset += sizeof(*memdev);
1735ac40b675SDan Williams 	dcr = nfit_buf + offset;
1736ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1737ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
1738ac40b675SDan Williams 			window_size);
1739ac40b675SDan Williams 	dcr->region_index = 0+2;
1740ac40b675SDan Williams 	dcr_common_init(dcr);
1741ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
1742ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
1743ac40b675SDan Williams 	dcr->windows = 0;
1744ac40b675SDan Williams 
17459fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
17469fb1a190SDave Jiang 			SPA2_SIZE);
1747f471f1a7SDan Williams 
1748d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
1749e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1750e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1751e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1752e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
17536bc75619SDan Williams }
17546bc75619SDan Williams 
17556bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
17566bc75619SDan Williams 		void *iobuf, u64 len, int rw)
17576bc75619SDan Williams {
17586bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
17596bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
17606bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
17616bc75619SDan Williams 	unsigned int lane;
17626bc75619SDan Williams 
17636bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
17646bc75619SDan Williams 	if (rw)
176567a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
176667a3e8feSRoss Zwisler 	else {
176767a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
176867a3e8feSRoss Zwisler 
17695deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
17705deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
177167a3e8feSRoss Zwisler 	}
17726bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
17736bc75619SDan Williams 
17746bc75619SDan Williams 	return 0;
17756bc75619SDan Williams }
17766bc75619SDan Williams 
1777a7de92daSDan Williams static unsigned long nfit_ctl_handle;
1778a7de92daSDan Williams 
1779a7de92daSDan Williams union acpi_object *result;
1780a7de92daSDan Williams 
1781a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
178294116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
1783a7de92daSDan Williams {
1784a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
1785a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
1786a7de92daSDan Williams 
1787a7de92daSDan Williams 	return result;
1788a7de92daSDan Williams }
1789a7de92daSDan Williams 
1790a7de92daSDan Williams static int setup_result(void *buf, size_t size)
1791a7de92daSDan Williams {
1792a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
1793a7de92daSDan Williams 	if (!result)
1794a7de92daSDan Williams 		return -ENOMEM;
1795a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
1796a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
1797a7de92daSDan Williams 	result->buffer.length = size;
1798a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
1799a7de92daSDan Williams 	memset(buf, 0, size);
1800a7de92daSDan Williams 	return 0;
1801a7de92daSDan Williams }
1802a7de92daSDan Williams 
1803a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
1804a7de92daSDan Williams {
1805a7de92daSDan Williams 	int rc, cmd_rc;
1806a7de92daSDan Williams 	struct nvdimm *nvdimm;
1807a7de92daSDan Williams 	struct acpi_device *adev;
1808a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
1809a7de92daSDan Williams 	struct nd_ars_record *record;
1810a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
1811a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
1812a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
1813a7de92daSDan Williams 	union {
1814a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
1815a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
1816a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
1817a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
1818a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
1819a7de92daSDan Williams 	} cmds;
1820a7de92daSDan Williams 
1821a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
1822a7de92daSDan Williams 	if (!adev)
1823a7de92daSDan Williams 		return -ENOMEM;
1824a7de92daSDan Williams 	*adev = (struct acpi_device) {
1825a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
1826a7de92daSDan Williams 		.dev = {
1827a7de92daSDan Williams 			.init_name = "test-adev",
1828a7de92daSDan Williams 		},
1829a7de92daSDan Williams 	};
1830a7de92daSDan Williams 
1831a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
1832a7de92daSDan Williams 	if (!acpi_desc)
1833a7de92daSDan Williams 		return -ENOMEM;
1834a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
1835a7de92daSDan Williams 		.nd_desc = {
1836a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
1837a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
1838a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
183910246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
184010246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
1841a7de92daSDan Williams 			.module = THIS_MODULE,
1842a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
1843a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
18449fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
18459fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
18469fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
18479fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
1848a7de92daSDan Williams 		},
1849a7de92daSDan Williams 		.dev = &adev->dev,
1850a7de92daSDan Williams 	};
1851a7de92daSDan Williams 
1852a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
1853a7de92daSDan Williams 	if (!nfit_mem)
1854a7de92daSDan Williams 		return -ENOMEM;
1855a7de92daSDan Williams 
1856a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
1857a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
1858a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
1859a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
1860a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
1861a7de92daSDan Williams 		.adev = adev,
1862a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
1863a7de92daSDan Williams 		.dsm_mask = mask,
1864a7de92daSDan Williams 	};
1865a7de92daSDan Williams 
1866a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
1867a7de92daSDan Williams 	if (!nvdimm)
1868a7de92daSDan Williams 		return -ENOMEM;
1869a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
1870a7de92daSDan Williams 		.provider_data = nfit_mem,
1871a7de92daSDan Williams 		.cmd_mask = mask,
1872a7de92daSDan Williams 		.dev = {
1873a7de92daSDan Williams 			.init_name = "test-dimm",
1874a7de92daSDan Williams 		},
1875a7de92daSDan Williams 	};
1876a7de92daSDan Williams 
1877a7de92daSDan Williams 
1878a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
1879a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
1880a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
1881a7de92daSDan Williams 		.status = 0,
1882a7de92daSDan Williams 		.config_size = SZ_128K,
1883a7de92daSDan Williams 		.max_xfer = SZ_4K,
1884a7de92daSDan Williams 	};
1885a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
1886a7de92daSDan Williams 	if (rc)
1887a7de92daSDan Williams 		return rc;
1888a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
1889a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
1890a7de92daSDan Williams 
1891a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
1892a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
1893a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
1894a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
1895a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
1896a7de92daSDan Williams 		return -EIO;
1897a7de92daSDan Williams 	}
1898a7de92daSDan Williams 
1899a7de92daSDan Williams 
1900a7de92daSDan Williams 	/* test ars_status with zero output */
1901a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
1902a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
1903a7de92daSDan Williams 		.out_length = 0,
1904a7de92daSDan Williams 	};
1905a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
1906a7de92daSDan Williams 	if (rc)
1907a7de92daSDan Williams 		return rc;
1908a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
1909a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
1910a7de92daSDan Williams 
1911a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
1912a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
1913a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
1914a7de92daSDan Williams 		return -EIO;
1915a7de92daSDan Williams 	}
1916a7de92daSDan Williams 
1917a7de92daSDan Williams 
1918a7de92daSDan Williams 	/* test ars_cap with benign extended status */
1919a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
1920a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
1921a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
1922a7de92daSDan Williams 	};
1923a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
1924a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
1925a7de92daSDan Williams 	if (rc)
1926a7de92daSDan Williams 		return rc;
1927a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
1928a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
1929a7de92daSDan Williams 
1930a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
1931a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
1932a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
1933a7de92daSDan Williams 		return -EIO;
1934a7de92daSDan Williams 	}
1935a7de92daSDan Williams 
1936a7de92daSDan Williams 
1937a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
1938a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
1939a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
1940a7de92daSDan Williams 		.out_length = cmd_size - 4,
1941a7de92daSDan Williams 	};
1942a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
1943a7de92daSDan Williams 	*record = (struct nd_ars_record) {
1944a7de92daSDan Williams 		.length = test_val,
1945a7de92daSDan Williams 	};
1946a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
1947a7de92daSDan Williams 	if (rc)
1948a7de92daSDan Williams 		return rc;
1949a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
1950a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
1951a7de92daSDan Williams 
1952a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
1953a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
1954a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
1955a7de92daSDan Williams 		return -EIO;
1956a7de92daSDan Williams 	}
1957a7de92daSDan Williams 
1958a7de92daSDan Williams 
1959a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
1960a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
1961a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
1962a7de92daSDan Williams 		.out_length = cmd_size,
1963a7de92daSDan Williams 	};
1964a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
1965a7de92daSDan Williams 	*record = (struct nd_ars_record) {
1966a7de92daSDan Williams 		.length = test_val,
1967a7de92daSDan Williams 	};
1968a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
1969a7de92daSDan Williams 	if (rc)
1970a7de92daSDan Williams 		return rc;
1971a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
1972a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
1973a7de92daSDan Williams 
1974a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
1975a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
1976a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
1977a7de92daSDan Williams 		return -EIO;
1978a7de92daSDan Williams 	}
1979a7de92daSDan Williams 
1980a7de92daSDan Williams 
1981a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
1982a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
1983a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
1984a7de92daSDan Williams 		.status = 1 << 16,
1985a7de92daSDan Williams 	};
1986a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
1987a7de92daSDan Williams 	if (rc)
1988a7de92daSDan Williams 		return rc;
1989a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
1990a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
1991a7de92daSDan Williams 
1992a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
1993a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
1994a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
1995a7de92daSDan Williams 		return -EIO;
1996a7de92daSDan Williams 	}
1997a7de92daSDan Williams 
1998a7de92daSDan Williams 	return 0;
1999a7de92daSDan Williams }
2000a7de92daSDan Williams 
20016bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
20026bc75619SDan Williams {
20036bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
20046bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
20056bc75619SDan Williams 	struct device *dev = &pdev->dev;
20066bc75619SDan Williams 	struct nfit_test *nfit_test;
2007231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2008c14a868aSDan Williams 	union acpi_object *obj;
20096bc75619SDan Williams 	int rc;
20106bc75619SDan Williams 
2011a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2012a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2013a7de92daSDan Williams 		if (rc)
2014a7de92daSDan Williams 			return rc;
2015a7de92daSDan Williams 	}
2016a7de92daSDan Williams 
20176bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
20186bc75619SDan Williams 
20196bc75619SDan Williams 	/* common alloc */
20206bc75619SDan Williams 	if (nfit_test->num_dcr) {
20216bc75619SDan Williams 		int num = nfit_test->num_dcr;
20226bc75619SDan Williams 
20236bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
20246bc75619SDan Williams 				GFP_KERNEL);
20256bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
20266bc75619SDan Williams 				GFP_KERNEL);
20279d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
20289d27a87eSDan Williams 				GFP_KERNEL);
20299d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
20309d27a87eSDan Williams 				GFP_KERNEL);
20316bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
20326bc75619SDan Williams 				GFP_KERNEL);
20336bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
20346bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
20356bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
20366bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
20376bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
20386bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
20396bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
20406bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
20419d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
20429d27a87eSDan Williams 				&& nfit_test->flush_dma)
20436bc75619SDan Williams 			/* pass */;
20446bc75619SDan Williams 		else
20456bc75619SDan Williams 			return -ENOMEM;
20466bc75619SDan Williams 	}
20476bc75619SDan Williams 
20486bc75619SDan Williams 	if (nfit_test->num_pm) {
20496bc75619SDan Williams 		int num = nfit_test->num_pm;
20506bc75619SDan Williams 
20516bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
20526bc75619SDan Williams 				GFP_KERNEL);
20536bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
20546bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
20556bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
20566bc75619SDan Williams 			/* pass */;
20576bc75619SDan Williams 		else
20586bc75619SDan Williams 			return -ENOMEM;
20596bc75619SDan Williams 	}
20606bc75619SDan Williams 
20616bc75619SDan Williams 	/* per-nfit specific alloc */
20626bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
20636bc75619SDan Williams 		return -ENOMEM;
20646bc75619SDan Williams 
20656bc75619SDan Williams 	nfit_test->setup(nfit_test);
20666bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2067a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
20686bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
20696bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2070a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2071bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2072a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
20736bc75619SDan Williams 
2074e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
2075e7a11b44SDan Williams 			nfit_test->nfit_size);
207658cd71b4SDan Williams 	if (rc)
207720985164SVishal Verma 		return rc;
207820985164SVishal Verma 
2079fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2080fbabd829SDan Williams 	if (rc)
2081fbabd829SDan Williams 		return rc;
2082fbabd829SDan Williams 
208320985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
208420985164SVishal Verma 		return 0;
208520985164SVishal Verma 
208620985164SVishal Verma 	nfit_test->setup_hotplug = 1;
208720985164SVishal Verma 	nfit_test->setup(nfit_test);
208820985164SVishal Verma 
2089c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2090c14a868aSDan Williams 	if (!obj)
2091c14a868aSDan Williams 		return -ENOMEM;
2092c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2093c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2094c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2095c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2096c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2097231bf117SDan Williams 
2098231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2099231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2100231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2101231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2102231bf117SDan Williams 		int i;
2103231bf117SDan Williams 
2104231bf117SDan Williams 		for (i = 0; i < NUM_DCR; i++)
2105231bf117SDan Williams 			if (nfit_handle == handle[i])
2106231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2107231bf117SDan Williams 						nfit_mem);
2108231bf117SDan Williams 	}
2109231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
21106bc75619SDan Williams 
21116bc75619SDan Williams 	return 0;
21126bc75619SDan Williams }
21136bc75619SDan Williams 
21146bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
21156bc75619SDan Williams {
21166bc75619SDan Williams 	return 0;
21176bc75619SDan Williams }
21186bc75619SDan Williams 
21196bc75619SDan Williams static void nfit_test_release(struct device *dev)
21206bc75619SDan Williams {
21216bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
21226bc75619SDan Williams 
21236bc75619SDan Williams 	kfree(nfit_test);
21246bc75619SDan Williams }
21256bc75619SDan Williams 
21266bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
21276bc75619SDan Williams 	{ KBUILD_MODNAME },
21286bc75619SDan Williams 	{ },
21296bc75619SDan Williams };
21306bc75619SDan Williams 
21316bc75619SDan Williams static struct platform_driver nfit_test_driver = {
21326bc75619SDan Williams 	.probe = nfit_test_probe,
21336bc75619SDan Williams 	.remove = nfit_test_remove,
21346bc75619SDan Williams 	.driver = {
21356bc75619SDan Williams 		.name = KBUILD_MODNAME,
21366bc75619SDan Williams 	},
21376bc75619SDan Williams 	.id_table = nfit_test_id,
21386bc75619SDan Williams };
21396bc75619SDan Williams 
21406bc75619SDan Williams static __init int nfit_test_init(void)
21416bc75619SDan Williams {
21426bc75619SDan Williams 	int rc, i;
21436bc75619SDan Williams 
2144a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
2145231bf117SDan Williams 
21469fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
21479fb1a190SDave Jiang 	if (!nfit_wq)
21489fb1a190SDave Jiang 		return -ENOMEM;
21499fb1a190SDave Jiang 
2150a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
2151a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
2152a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
2153a7de92daSDan Williams 		goto err_register;
2154a7de92daSDan Williams 	}
21556bc75619SDan Williams 
21566bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
21576bc75619SDan Williams 		struct nfit_test *nfit_test;
21586bc75619SDan Williams 		struct platform_device *pdev;
21596bc75619SDan Williams 
21606bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
21616bc75619SDan Williams 		if (!nfit_test) {
21626bc75619SDan Williams 			rc = -ENOMEM;
21636bc75619SDan Williams 			goto err_register;
21646bc75619SDan Williams 		}
21656bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
21669fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
21676bc75619SDan Williams 		switch (i) {
21686bc75619SDan Williams 		case 0:
21696bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
2170dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
21716bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
21726bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
21736bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
21746bc75619SDan Williams 			break;
21756bc75619SDan Williams 		case 1:
2176a117699cSYasunori Goto 			nfit_test->num_pm = 2;
2177dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
2178ac40b675SDan Williams 			nfit_test->num_dcr = 2;
21796bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
21806bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
21816bc75619SDan Williams 			break;
21826bc75619SDan Williams 		default:
21836bc75619SDan Williams 			rc = -EINVAL;
21846bc75619SDan Williams 			goto err_register;
21856bc75619SDan Williams 		}
21866bc75619SDan Williams 		pdev = &nfit_test->pdev;
21876bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
21886bc75619SDan Williams 		pdev->id = i;
21896bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
21906bc75619SDan Williams 		rc = platform_device_register(pdev);
21916bc75619SDan Williams 		if (rc) {
21926bc75619SDan Williams 			put_device(&pdev->dev);
21936bc75619SDan Williams 			goto err_register;
21946bc75619SDan Williams 		}
21958b06b884SDan Williams 		get_device(&pdev->dev);
21966bc75619SDan Williams 
21976bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
21986bc75619SDan Williams 		if (rc)
21996bc75619SDan Williams 			goto err_register;
22006bc75619SDan Williams 
22016bc75619SDan Williams 		instances[i] = nfit_test;
22029fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
22036bc75619SDan Williams 	}
22046bc75619SDan Williams 
22056bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
22066bc75619SDan Williams 	if (rc)
22076bc75619SDan Williams 		goto err_register;
22086bc75619SDan Williams 	return 0;
22096bc75619SDan Williams 
22106bc75619SDan Williams  err_register:
22119fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
22126bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
22136bc75619SDan Williams 		if (instances[i])
22146bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
22156bc75619SDan Williams 	nfit_test_teardown();
22168b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
22178b06b884SDan Williams 		if (instances[i])
22188b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
22198b06b884SDan Williams 
22206bc75619SDan Williams 	return rc;
22216bc75619SDan Williams }
22226bc75619SDan Williams 
22236bc75619SDan Williams static __exit void nfit_test_exit(void)
22246bc75619SDan Williams {
22256bc75619SDan Williams 	int i;
22266bc75619SDan Williams 
22279fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
22289fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
22296bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
22306bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
22318b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
22326bc75619SDan Williams 	nfit_test_teardown();
22338b06b884SDan Williams 
22348b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
22358b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
2236231bf117SDan Williams 	class_destroy(nfit_test_dimm);
22376bc75619SDan Williams }
22386bc75619SDan Williams 
22396bc75619SDan Williams module_init(nfit_test_init);
22406bc75619SDan Williams module_exit(nfit_test_exit);
22416bc75619SDan Williams MODULE_LICENSE("GPL v2");
22426bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
2243