xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 581388209405902b56d055f644b4dd124a206112)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
166bc75619SDan Williams #include <linux/libnvdimm.h>
176bc75619SDan Williams #include <linux/vmalloc.h>
186bc75619SDan Williams #include <linux/device.h>
196bc75619SDan Williams #include <linux/module.h>
206bc75619SDan Williams #include <linux/ndctl.h>
216bc75619SDan Williams #include <linux/sizes.h>
226bc75619SDan Williams #include <linux/slab.h>
236bc75619SDan Williams #include <nfit.h>
246bc75619SDan Williams #include <nd.h>
256bc75619SDan Williams #include "nfit_test.h"
266bc75619SDan Williams 
276bc75619SDan Williams /*
286bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
296bc75619SDan Williams  *
306bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
316bc75619SDan Williams  *
326bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
336bc75619SDan Williams  *           +----------+--------------+----------+---------+
346bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
356bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
366bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
376bc75619SDan Williams  *    |      +----------+--------------v----------v         v
386bc75619SDan Williams  * +--+---+                            |                    |
396bc75619SDan Williams  * | cpu0 |                                    region1
406bc75619SDan Williams  * +--+---+                            |                    |
416bc75619SDan Williams  *    |      +-------------------------^----------^         ^
426bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
436bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
446bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
456bc75619SDan Williams  *           +-------------------------+----------+-+-------+
466bc75619SDan Williams  *
476bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
486bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
496bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
506bc75619SDan Williams  *
516bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
526bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
536bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
546bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
556bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
566bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
576bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
586bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
596bc75619SDan Williams  *    names that can be assigned to a namespace.
606bc75619SDan Williams  *
616bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
626bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
636bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
646bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
656bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
666bc75619SDan Williams  *    "blk5.0".
676bc75619SDan Williams  *
686bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
696bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
706bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
716bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
726bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
736bc75619SDan Williams  *
746bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
756bc75619SDan Williams  *
766bc75619SDan Williams  *  region2
776bc75619SDan Williams  * +---------------------+
786bc75619SDan Williams  * |---------------------|
796bc75619SDan Williams  * ||       pm2.0       ||
806bc75619SDan Williams  * |---------------------|
816bc75619SDan Williams  * +---------------------+
826bc75619SDan Williams  *
836bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
846bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
856bc75619SDan Williams  *    reference an NVDIMM.
866bc75619SDan Williams  */
876bc75619SDan Williams enum {
886bc75619SDan Williams 	NUM_PM  = 2,
896bc75619SDan Williams 	NUM_DCR = 4,
906bc75619SDan Williams 	NUM_BDW = NUM_DCR,
916bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
926bc75619SDan Williams 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
936bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
946bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
956bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
966bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
976bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
986bc75619SDan Williams 	BDW_SIZE = 64 << 8,
996bc75619SDan Williams 	DCR_SIZE = 12,
1006bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1016bc75619SDan Williams };
1026bc75619SDan Williams 
1036bc75619SDan Williams struct nfit_test_dcr {
1046bc75619SDan Williams 	__le64 bdw_addr;
1056bc75619SDan Williams 	__le32 bdw_status;
1066bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1076bc75619SDan Williams };
1086bc75619SDan Williams 
1096bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1106bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1116bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1126bc75619SDan Williams 
1136bc75619SDan Williams static u32 handle[NUM_DCR] = {
1146bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1156bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1166bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1176bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
1186bc75619SDan Williams };
1196bc75619SDan Williams 
1206bc75619SDan Williams struct nfit_test {
1216bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1226bc75619SDan Williams 	struct platform_device pdev;
1236bc75619SDan Williams 	struct list_head resources;
1246bc75619SDan Williams 	void *nfit_buf;
1256bc75619SDan Williams 	dma_addr_t nfit_dma;
1266bc75619SDan Williams 	size_t nfit_size;
1276bc75619SDan Williams 	int num_dcr;
1286bc75619SDan Williams 	int num_pm;
1296bc75619SDan Williams 	void **dimm;
1306bc75619SDan Williams 	dma_addr_t *dimm_dma;
1316bc75619SDan Williams 	void **label;
1326bc75619SDan Williams 	dma_addr_t *label_dma;
1336bc75619SDan Williams 	void **spa_set;
1346bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1356bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1366bc75619SDan Williams 	dma_addr_t *dcr_dma;
1376bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1386bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
1396bc75619SDan Williams };
1406bc75619SDan Williams 
1416bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1426bc75619SDan Williams {
1436bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1446bc75619SDan Williams 
1456bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1466bc75619SDan Williams }
1476bc75619SDan Williams 
1486bc75619SDan Williams static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
1496bc75619SDan Williams 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1506bc75619SDan Williams 		unsigned int buf_len)
1516bc75619SDan Williams {
1526bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1536bc75619SDan Williams 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
1546bc75619SDan Williams 	struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1556bc75619SDan Williams 	int i, rc;
1566bc75619SDan Williams 
1576bc75619SDan Williams 	if (!nfit_mem || !test_bit(cmd, &nfit_mem->dsm_mask))
1586bc75619SDan Williams 		return -ENXIO;
1596bc75619SDan Williams 
1606bc75619SDan Williams 	/* lookup label space for the given dimm */
1616bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1626bc75619SDan Williams 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1636bc75619SDan Williams 			break;
1646bc75619SDan Williams 	if (i >= ARRAY_SIZE(handle))
1656bc75619SDan Williams 		return -ENXIO;
1666bc75619SDan Williams 
1676bc75619SDan Williams 	switch (cmd) {
1686bc75619SDan Williams 	case ND_CMD_GET_CONFIG_SIZE: {
1696bc75619SDan Williams 		struct nd_cmd_get_config_size *nd_cmd = buf;
1706bc75619SDan Williams 
1716bc75619SDan Williams 		if (buf_len < sizeof(*nd_cmd))
1726bc75619SDan Williams 			return -EINVAL;
1736bc75619SDan Williams 		nd_cmd->status = 0;
1746bc75619SDan Williams 		nd_cmd->config_size = LABEL_SIZE;
1756bc75619SDan Williams 		nd_cmd->max_xfer = SZ_4K;
1766bc75619SDan Williams 		rc = 0;
1776bc75619SDan Williams 		break;
1786bc75619SDan Williams 	}
1796bc75619SDan Williams 	case ND_CMD_GET_CONFIG_DATA: {
1806bc75619SDan Williams 		struct nd_cmd_get_config_data_hdr *nd_cmd = buf;
1816bc75619SDan Williams 		unsigned int len, offset = nd_cmd->in_offset;
1826bc75619SDan Williams 
1836bc75619SDan Williams 		if (buf_len < sizeof(*nd_cmd))
1846bc75619SDan Williams 			return -EINVAL;
1856bc75619SDan Williams 		if (offset >= LABEL_SIZE)
1866bc75619SDan Williams 			return -EINVAL;
1876bc75619SDan Williams 		if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
1886bc75619SDan Williams 			return -EINVAL;
1896bc75619SDan Williams 
1906bc75619SDan Williams 		nd_cmd->status = 0;
1916bc75619SDan Williams 		len = min(nd_cmd->in_length, LABEL_SIZE - offset);
1926bc75619SDan Williams 		memcpy(nd_cmd->out_buf, t->label[i] + offset, len);
1936bc75619SDan Williams 		rc = buf_len - sizeof(*nd_cmd) - len;
1946bc75619SDan Williams 		break;
1956bc75619SDan Williams 	}
1966bc75619SDan Williams 	case ND_CMD_SET_CONFIG_DATA: {
1976bc75619SDan Williams 		struct nd_cmd_set_config_hdr *nd_cmd = buf;
1986bc75619SDan Williams 		unsigned int len, offset = nd_cmd->in_offset;
1996bc75619SDan Williams 		u32 *status;
2006bc75619SDan Williams 
2016bc75619SDan Williams 		if (buf_len < sizeof(*nd_cmd))
2026bc75619SDan Williams 			return -EINVAL;
2036bc75619SDan Williams 		if (offset >= LABEL_SIZE)
2046bc75619SDan Williams 			return -EINVAL;
2056bc75619SDan Williams 		if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
2066bc75619SDan Williams 			return -EINVAL;
2076bc75619SDan Williams 
2086bc75619SDan Williams 		status = buf + nd_cmd->in_length + sizeof(*nd_cmd);
2096bc75619SDan Williams 		*status = 0;
2106bc75619SDan Williams 		len = min(nd_cmd->in_length, LABEL_SIZE - offset);
2116bc75619SDan Williams 		memcpy(t->label[i] + offset, nd_cmd->in_buf, len);
2126bc75619SDan Williams 		rc = buf_len - sizeof(*nd_cmd) - (len + 4);
2136bc75619SDan Williams 		break;
2146bc75619SDan Williams 	}
2156bc75619SDan Williams 	default:
2166bc75619SDan Williams 		return -ENOTTY;
2176bc75619SDan Williams 	}
2186bc75619SDan Williams 
2196bc75619SDan Williams 	return rc;
2206bc75619SDan Williams }
2216bc75619SDan Williams 
2226bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
2236bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
2246bc75619SDan Williams 
2256bc75619SDan Williams static void release_nfit_res(void *data)
2266bc75619SDan Williams {
2276bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
2286bc75619SDan Williams 	struct resource *res = nfit_res->res;
2296bc75619SDan Williams 
2306bc75619SDan Williams 	spin_lock(&nfit_test_lock);
2316bc75619SDan Williams 	list_del(&nfit_res->list);
2326bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
2336bc75619SDan Williams 
2346bc75619SDan Williams 	if (is_vmalloc_addr(nfit_res->buf))
2356bc75619SDan Williams 		vfree(nfit_res->buf);
2366bc75619SDan Williams 	else
2376bc75619SDan Williams 		dma_free_coherent(nfit_res->dev, resource_size(res),
2386bc75619SDan Williams 				nfit_res->buf, res->start);
2396bc75619SDan Williams 	kfree(res);
2406bc75619SDan Williams 	kfree(nfit_res);
2416bc75619SDan Williams }
2426bc75619SDan Williams 
2436bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
2446bc75619SDan Williams 		void *buf)
2456bc75619SDan Williams {
2466bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
2476bc75619SDan Williams 	struct resource *res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
2486bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
2496bc75619SDan Williams 			GFP_KERNEL);
2506bc75619SDan Williams 	int rc;
2516bc75619SDan Williams 
2526bc75619SDan Williams 	if (!res || !buf || !nfit_res)
2536bc75619SDan Williams 		goto err;
2546bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
2556bc75619SDan Williams 	if (rc)
2566bc75619SDan Williams 		goto err;
2576bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
2586bc75619SDan Williams 	memset(buf, 0, size);
2596bc75619SDan Williams 	nfit_res->dev = dev;
2606bc75619SDan Williams 	nfit_res->buf = buf;
2616bc75619SDan Williams 	nfit_res->res = res;
2626bc75619SDan Williams 	res->start = *dma;
2636bc75619SDan Williams 	res->end = *dma + size - 1;
2646bc75619SDan Williams 	res->name = "NFIT";
2656bc75619SDan Williams 	spin_lock(&nfit_test_lock);
2666bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
2676bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
2686bc75619SDan Williams 
2696bc75619SDan Williams 	return nfit_res->buf;
2706bc75619SDan Williams  err:
2716bc75619SDan Williams 	if (buf && !is_vmalloc_addr(buf))
2726bc75619SDan Williams 		dma_free_coherent(dev, size, buf, *dma);
2736bc75619SDan Williams 	else if (buf)
2746bc75619SDan Williams 		vfree(buf);
2756bc75619SDan Williams 	kfree(res);
2766bc75619SDan Williams 	kfree(nfit_res);
2776bc75619SDan Williams 	return NULL;
2786bc75619SDan Williams }
2796bc75619SDan Williams 
2806bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
2816bc75619SDan Williams {
2826bc75619SDan Williams 	void *buf = vmalloc(size);
2836bc75619SDan Williams 
2846bc75619SDan Williams 	*dma = (unsigned long) buf;
2856bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
2866bc75619SDan Williams }
2876bc75619SDan Williams 
2886bc75619SDan Williams static void *test_alloc_coherent(struct nfit_test *t, size_t size,
2896bc75619SDan Williams 		dma_addr_t *dma)
2906bc75619SDan Williams {
2916bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
2926bc75619SDan Williams 	void *buf = dma_alloc_coherent(dev, size, dma, GFP_KERNEL);
2936bc75619SDan Williams 
2946bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
2956bc75619SDan Williams }
2966bc75619SDan Williams 
2976bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
2986bc75619SDan Williams {
2996bc75619SDan Williams 	int i;
3006bc75619SDan Williams 
3016bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
3026bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
3036bc75619SDan Williams 		struct nfit_test *t = instances[i];
3046bc75619SDan Williams 
3056bc75619SDan Williams 		if (!t)
3066bc75619SDan Williams 			continue;
3076bc75619SDan Williams 		spin_lock(&nfit_test_lock);
3086bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
3096bc75619SDan Williams 			if (addr >= n->res->start && (addr < n->res->start
3106bc75619SDan Williams 						+ resource_size(n->res))) {
3116bc75619SDan Williams 				nfit_res = n;
3126bc75619SDan Williams 				break;
3136bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
3146bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
3156bc75619SDan Williams 						+ resource_size(n->res))) {
3166bc75619SDan Williams 				nfit_res = n;
3176bc75619SDan Williams 				break;
3186bc75619SDan Williams 			}
3196bc75619SDan Williams 		}
3206bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
3216bc75619SDan Williams 		if (nfit_res)
3226bc75619SDan Williams 			return nfit_res;
3236bc75619SDan Williams 	}
3246bc75619SDan Williams 
3256bc75619SDan Williams 	return NULL;
3266bc75619SDan Williams }
3276bc75619SDan Williams 
3286bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
3296bc75619SDan Williams {
3306bc75619SDan Williams 	size_t nfit_size = sizeof(struct acpi_table_nfit)
3316bc75619SDan Williams 			+ sizeof(struct acpi_nfit_system_address) * NUM_SPA
3326bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
3336bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
3346bc75619SDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW;
3356bc75619SDan Williams 	int i;
3366bc75619SDan Williams 
3376bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
3386bc75619SDan Williams 	if (!t->nfit_buf)
3396bc75619SDan Williams 		return -ENOMEM;
3406bc75619SDan Williams 	t->nfit_size = nfit_size;
3416bc75619SDan Williams 
3426bc75619SDan Williams 	t->spa_set[0] = test_alloc_coherent(t, SPA0_SIZE, &t->spa_set_dma[0]);
3436bc75619SDan Williams 	if (!t->spa_set[0])
3446bc75619SDan Williams 		return -ENOMEM;
3456bc75619SDan Williams 
3466bc75619SDan Williams 	t->spa_set[1] = test_alloc_coherent(t, SPA1_SIZE, &t->spa_set_dma[1]);
3476bc75619SDan Williams 	if (!t->spa_set[1])
3486bc75619SDan Williams 		return -ENOMEM;
3496bc75619SDan Williams 
3506bc75619SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
3516bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
3526bc75619SDan Williams 		if (!t->dimm[i])
3536bc75619SDan Williams 			return -ENOMEM;
3546bc75619SDan Williams 
3556bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
3566bc75619SDan Williams 		if (!t->label[i])
3576bc75619SDan Williams 			return -ENOMEM;
3586bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
3596bc75619SDan Williams 	}
3606bc75619SDan Williams 
3616bc75619SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
3626bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
3636bc75619SDan Williams 		if (!t->dcr[i])
3646bc75619SDan Williams 			return -ENOMEM;
3656bc75619SDan Williams 	}
3666bc75619SDan Williams 
3676bc75619SDan Williams 	return 0;
3686bc75619SDan Williams }
3696bc75619SDan Williams 
3706bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
3716bc75619SDan Williams {
3726bc75619SDan Williams 	size_t nfit_size = sizeof(struct acpi_table_nfit)
3736bc75619SDan Williams 		+ sizeof(struct acpi_nfit_system_address)
3746bc75619SDan Williams 		+ sizeof(struct acpi_nfit_memory_map)
3756bc75619SDan Williams 		+ sizeof(struct acpi_nfit_control_region);
3766bc75619SDan Williams 
3776bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
3786bc75619SDan Williams 	if (!t->nfit_buf)
3796bc75619SDan Williams 		return -ENOMEM;
3806bc75619SDan Williams 	t->nfit_size = nfit_size;
3816bc75619SDan Williams 
3826bc75619SDan Williams 	t->spa_set[0] = test_alloc_coherent(t, SPA2_SIZE, &t->spa_set_dma[0]);
3836bc75619SDan Williams 	if (!t->spa_set[0])
3846bc75619SDan Williams 		return -ENOMEM;
3856bc75619SDan Williams 
3866bc75619SDan Williams 	return 0;
3876bc75619SDan Williams }
3886bc75619SDan Williams 
3896bc75619SDan Williams static void nfit_test_init_header(struct acpi_table_nfit *nfit, size_t size)
3906bc75619SDan Williams {
3916bc75619SDan Williams 	memcpy(nfit->header.signature, ACPI_SIG_NFIT, 4);
3926bc75619SDan Williams 	nfit->header.length = size;
3936bc75619SDan Williams 	nfit->header.revision = 1;
3946bc75619SDan Williams 	memcpy(nfit->header.oem_id, "LIBND", 6);
3956bc75619SDan Williams 	memcpy(nfit->header.oem_table_id, "TEST", 5);
3966bc75619SDan Williams 	nfit->header.oem_revision = 1;
3976bc75619SDan Williams 	memcpy(nfit->header.asl_compiler_id, "TST", 4);
3986bc75619SDan Williams 	nfit->header.asl_compiler_revision = 1;
3996bc75619SDan Williams }
4006bc75619SDan Williams 
4016bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
4026bc75619SDan Williams {
4036bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
4046bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
4056bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
4066bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
4076bc75619SDan Williams 	size_t size = t->nfit_size;
4086bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
4096bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
4106bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
4116bc75619SDan Williams 	unsigned int offset;
4126bc75619SDan Williams 
4136bc75619SDan Williams 	nfit_test_init_header(nfit_buf, size);
4146bc75619SDan Williams 
4156bc75619SDan Williams 	/*
4166bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
4176bc75619SDan Williams 	 * does not actually alias the related block-data-window
4186bc75619SDan Williams 	 * regions)
4196bc75619SDan Williams 	 */
4206bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit);
4216bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4226bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4236bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
4246bc75619SDan Williams 	spa->range_index = 0+1;
4256bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
4266bc75619SDan Williams 	spa->length = SPA0_SIZE;
4276bc75619SDan Williams 
4286bc75619SDan Williams 	/*
4296bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
4306bc75619SDan Williams 	 * does not actually alias the related block-data-window
4316bc75619SDan Williams 	 * regions)
4326bc75619SDan Williams 	 */
4336bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa);
4346bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4356bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4366bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
4376bc75619SDan Williams 	spa->range_index = 1+1;
4386bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
4396bc75619SDan Williams 	spa->length = SPA1_SIZE;
4406bc75619SDan Williams 
4416bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
4426bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 2;
4436bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4446bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4456bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
4466bc75619SDan Williams 	spa->range_index = 2+1;
4476bc75619SDan Williams 	spa->address = t->dcr_dma[0];
4486bc75619SDan Williams 	spa->length = DCR_SIZE;
4496bc75619SDan Williams 
4506bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
4516bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 3;
4526bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4536bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4546bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
4556bc75619SDan Williams 	spa->range_index = 3+1;
4566bc75619SDan Williams 	spa->address = t->dcr_dma[1];
4576bc75619SDan Williams 	spa->length = DCR_SIZE;
4586bc75619SDan Williams 
4596bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
4606bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 4;
4616bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4626bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4636bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
4646bc75619SDan Williams 	spa->range_index = 4+1;
4656bc75619SDan Williams 	spa->address = t->dcr_dma[2];
4666bc75619SDan Williams 	spa->length = DCR_SIZE;
4676bc75619SDan Williams 
4686bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
4696bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 5;
4706bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4716bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4726bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
4736bc75619SDan Williams 	spa->range_index = 5+1;
4746bc75619SDan Williams 	spa->address = t->dcr_dma[3];
4756bc75619SDan Williams 	spa->length = DCR_SIZE;
4766bc75619SDan Williams 
4776bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
4786bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 6;
4796bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4806bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4816bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
4826bc75619SDan Williams 	spa->range_index = 6+1;
4836bc75619SDan Williams 	spa->address = t->dimm_dma[0];
4846bc75619SDan Williams 	spa->length = DIMM_SIZE;
4856bc75619SDan Williams 
4866bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
4876bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 7;
4886bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4896bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4906bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
4916bc75619SDan Williams 	spa->range_index = 7+1;
4926bc75619SDan Williams 	spa->address = t->dimm_dma[1];
4936bc75619SDan Williams 	spa->length = DIMM_SIZE;
4946bc75619SDan Williams 
4956bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
4966bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 8;
4976bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
4986bc75619SDan Williams 	spa->header.length = sizeof(*spa);
4996bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
5006bc75619SDan Williams 	spa->range_index = 8+1;
5016bc75619SDan Williams 	spa->address = t->dimm_dma[2];
5026bc75619SDan Williams 	spa->length = DIMM_SIZE;
5036bc75619SDan Williams 
5046bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
5056bc75619SDan Williams 	spa = nfit_buf + sizeof(struct acpi_table_nfit) + sizeof(*spa) * 9;
5066bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
5076bc75619SDan Williams 	spa->header.length = sizeof(*spa);
5086bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
5096bc75619SDan Williams 	spa->range_index = 9+1;
5106bc75619SDan Williams 	spa->address = t->dimm_dma[3];
5116bc75619SDan Williams 	spa->length = DIMM_SIZE;
5126bc75619SDan Williams 
5136bc75619SDan Williams 	offset = sizeof(struct acpi_table_nfit) + sizeof(*spa) * 10;
5146bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
5156bc75619SDan Williams 	memdev = nfit_buf + offset;
5166bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
5176bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
5186bc75619SDan Williams 	memdev->device_handle = handle[0];
5196bc75619SDan Williams 	memdev->physical_id = 0;
5206bc75619SDan Williams 	memdev->region_id = 0;
5216bc75619SDan Williams 	memdev->range_index = 0+1;
5226bc75619SDan Williams 	memdev->region_index = 0+1;
5236bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
5246bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[0];
5256bc75619SDan Williams 	memdev->address = 0;
5266bc75619SDan Williams 	memdev->interleave_index = 0;
5276bc75619SDan Williams 	memdev->interleave_ways = 2;
5286bc75619SDan Williams 
5296bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
5306bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
5316bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
5326bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
5336bc75619SDan Williams 	memdev->device_handle = handle[1];
5346bc75619SDan Williams 	memdev->physical_id = 1;
5356bc75619SDan Williams 	memdev->region_id = 0;
5366bc75619SDan Williams 	memdev->range_index = 0+1;
5376bc75619SDan Williams 	memdev->region_index = 1+1;
5386bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
5396bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[0] + SPA0_SIZE/2;
5406bc75619SDan Williams 	memdev->address = 0;
5416bc75619SDan Williams 	memdev->interleave_index = 0;
5426bc75619SDan Williams 	memdev->interleave_ways = 2;
5436bc75619SDan Williams 
5446bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
5456bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
5466bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
5476bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
5486bc75619SDan Williams 	memdev->device_handle = handle[0];
5496bc75619SDan Williams 	memdev->physical_id = 0;
5506bc75619SDan Williams 	memdev->region_id = 1;
5516bc75619SDan Williams 	memdev->range_index = 1+1;
5526bc75619SDan Williams 	memdev->region_index = 0+1;
5536bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
5546bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1];
5556bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
5566bc75619SDan Williams 	memdev->interleave_index = 0;
5576bc75619SDan Williams 	memdev->interleave_ways = 4;
5586bc75619SDan Williams 
5596bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
5606bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
5616bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
5626bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
5636bc75619SDan Williams 	memdev->device_handle = handle[1];
5646bc75619SDan Williams 	memdev->physical_id = 1;
5656bc75619SDan Williams 	memdev->region_id = 1;
5666bc75619SDan Williams 	memdev->range_index = 1+1;
5676bc75619SDan Williams 	memdev->region_index = 1+1;
5686bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
5696bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1] + SPA1_SIZE/4;
5706bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
5716bc75619SDan Williams 	memdev->interleave_index = 0;
5726bc75619SDan Williams 	memdev->interleave_ways = 4;
5736bc75619SDan Williams 
5746bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
5756bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
5766bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
5776bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
5786bc75619SDan Williams 	memdev->device_handle = handle[2];
5796bc75619SDan Williams 	memdev->physical_id = 2;
5806bc75619SDan Williams 	memdev->region_id = 0;
5816bc75619SDan Williams 	memdev->range_index = 1+1;
5826bc75619SDan Williams 	memdev->region_index = 2+1;
5836bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
5846bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1] + 2*SPA1_SIZE/4;
5856bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
5866bc75619SDan Williams 	memdev->interleave_index = 0;
5876bc75619SDan Williams 	memdev->interleave_ways = 4;
5886bc75619SDan Williams 
5896bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
5906bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
5916bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
5926bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
5936bc75619SDan Williams 	memdev->device_handle = handle[3];
5946bc75619SDan Williams 	memdev->physical_id = 3;
5956bc75619SDan Williams 	memdev->region_id = 0;
5966bc75619SDan Williams 	memdev->range_index = 1+1;
5976bc75619SDan Williams 	memdev->region_index = 3+1;
5986bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
5996bc75619SDan Williams 	memdev->region_offset = t->spa_set_dma[1] + 3*SPA1_SIZE/4;
6006bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
6016bc75619SDan Williams 	memdev->interleave_index = 0;
6026bc75619SDan Williams 	memdev->interleave_ways = 4;
6036bc75619SDan Williams 
6046bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
6056bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
6066bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6076bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6086bc75619SDan Williams 	memdev->device_handle = handle[0];
6096bc75619SDan Williams 	memdev->physical_id = 0;
6106bc75619SDan Williams 	memdev->region_id = 0;
6116bc75619SDan Williams 	memdev->range_index = 2+1;
6126bc75619SDan Williams 	memdev->region_index = 0+1;
6136bc75619SDan Williams 	memdev->region_size = 0;
6146bc75619SDan Williams 	memdev->region_offset = 0;
6156bc75619SDan Williams 	memdev->address = 0;
6166bc75619SDan Williams 	memdev->interleave_index = 0;
6176bc75619SDan Williams 	memdev->interleave_ways = 1;
6186bc75619SDan Williams 
6196bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
6206bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
6216bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6226bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6236bc75619SDan Williams 	memdev->device_handle = handle[1];
6246bc75619SDan Williams 	memdev->physical_id = 1;
6256bc75619SDan Williams 	memdev->region_id = 0;
6266bc75619SDan Williams 	memdev->range_index = 3+1;
6276bc75619SDan Williams 	memdev->region_index = 1+1;
6286bc75619SDan Williams 	memdev->region_size = 0;
6296bc75619SDan Williams 	memdev->region_offset = 0;
6306bc75619SDan Williams 	memdev->address = 0;
6316bc75619SDan Williams 	memdev->interleave_index = 0;
6326bc75619SDan Williams 	memdev->interleave_ways = 1;
6336bc75619SDan Williams 
6346bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
6356bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
6366bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6376bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6386bc75619SDan Williams 	memdev->device_handle = handle[2];
6396bc75619SDan Williams 	memdev->physical_id = 2;
6406bc75619SDan Williams 	memdev->region_id = 0;
6416bc75619SDan Williams 	memdev->range_index = 4+1;
6426bc75619SDan Williams 	memdev->region_index = 2+1;
6436bc75619SDan Williams 	memdev->region_size = 0;
6446bc75619SDan Williams 	memdev->region_offset = 0;
6456bc75619SDan Williams 	memdev->address = 0;
6466bc75619SDan Williams 	memdev->interleave_index = 0;
6476bc75619SDan Williams 	memdev->interleave_ways = 1;
6486bc75619SDan Williams 
6496bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
6506bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
6516bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6526bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6536bc75619SDan Williams 	memdev->device_handle = handle[3];
6546bc75619SDan Williams 	memdev->physical_id = 3;
6556bc75619SDan Williams 	memdev->region_id = 0;
6566bc75619SDan Williams 	memdev->range_index = 5+1;
6576bc75619SDan Williams 	memdev->region_index = 3+1;
6586bc75619SDan Williams 	memdev->region_size = 0;
6596bc75619SDan Williams 	memdev->region_offset = 0;
6606bc75619SDan Williams 	memdev->address = 0;
6616bc75619SDan Williams 	memdev->interleave_index = 0;
6626bc75619SDan Williams 	memdev->interleave_ways = 1;
6636bc75619SDan Williams 
6646bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
6656bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
6666bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6676bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6686bc75619SDan Williams 	memdev->device_handle = handle[0];
6696bc75619SDan Williams 	memdev->physical_id = 0;
6706bc75619SDan Williams 	memdev->region_id = 0;
6716bc75619SDan Williams 	memdev->range_index = 6+1;
6726bc75619SDan Williams 	memdev->region_index = 0+1;
6736bc75619SDan Williams 	memdev->region_size = 0;
6746bc75619SDan Williams 	memdev->region_offset = 0;
6756bc75619SDan Williams 	memdev->address = 0;
6766bc75619SDan Williams 	memdev->interleave_index = 0;
6776bc75619SDan Williams 	memdev->interleave_ways = 1;
6786bc75619SDan Williams 
6796bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
6806bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
6816bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6826bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6836bc75619SDan Williams 	memdev->device_handle = handle[1];
6846bc75619SDan Williams 	memdev->physical_id = 1;
6856bc75619SDan Williams 	memdev->region_id = 0;
6866bc75619SDan Williams 	memdev->range_index = 7+1;
6876bc75619SDan Williams 	memdev->region_index = 1+1;
6886bc75619SDan Williams 	memdev->region_size = 0;
6896bc75619SDan Williams 	memdev->region_offset = 0;
6906bc75619SDan Williams 	memdev->address = 0;
6916bc75619SDan Williams 	memdev->interleave_index = 0;
6926bc75619SDan Williams 	memdev->interleave_ways = 1;
6936bc75619SDan Williams 
6946bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
6956bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
6966bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
6976bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
6986bc75619SDan Williams 	memdev->device_handle = handle[2];
6996bc75619SDan Williams 	memdev->physical_id = 2;
7006bc75619SDan Williams 	memdev->region_id = 0;
7016bc75619SDan Williams 	memdev->range_index = 8+1;
7026bc75619SDan Williams 	memdev->region_index = 2+1;
7036bc75619SDan Williams 	memdev->region_size = 0;
7046bc75619SDan Williams 	memdev->region_offset = 0;
7056bc75619SDan Williams 	memdev->address = 0;
7066bc75619SDan Williams 	memdev->interleave_index = 0;
7076bc75619SDan Williams 	memdev->interleave_ways = 1;
7086bc75619SDan Williams 
7096bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
7106bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
7116bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
7126bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
7136bc75619SDan Williams 	memdev->device_handle = handle[3];
7146bc75619SDan Williams 	memdev->physical_id = 3;
7156bc75619SDan Williams 	memdev->region_id = 0;
7166bc75619SDan Williams 	memdev->range_index = 9+1;
7176bc75619SDan Williams 	memdev->region_index = 3+1;
7186bc75619SDan Williams 	memdev->region_size = 0;
7196bc75619SDan Williams 	memdev->region_offset = 0;
7206bc75619SDan Williams 	memdev->address = 0;
7216bc75619SDan Williams 	memdev->interleave_index = 0;
7226bc75619SDan Williams 	memdev->interleave_ways = 1;
7236bc75619SDan Williams 
7246bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
7256bc75619SDan Williams 	/* dcr-descriptor0 */
7266bc75619SDan Williams 	dcr = nfit_buf + offset;
7276bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
7286bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
7296bc75619SDan Williams 	dcr->region_index = 0+1;
7306bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
7316bc75619SDan Williams 	dcr->device_id = 0;
7326bc75619SDan Williams 	dcr->revision_id = 1;
7336bc75619SDan Williams 	dcr->serial_number = ~handle[0];
7346bc75619SDan Williams 	dcr->windows = 1;
7356bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
7366bc75619SDan Williams 	dcr->command_offset = 0;
7376bc75619SDan Williams 	dcr->command_size = 8;
7386bc75619SDan Williams 	dcr->status_offset = 8;
7396bc75619SDan Williams 	dcr->status_size = 4;
7406bc75619SDan Williams 
7416bc75619SDan Williams 	/* dcr-descriptor1 */
7426bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
7436bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
7446bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
7456bc75619SDan Williams 	dcr->region_index = 1+1;
7466bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
7476bc75619SDan Williams 	dcr->device_id = 0;
7486bc75619SDan Williams 	dcr->revision_id = 1;
7496bc75619SDan Williams 	dcr->serial_number = ~handle[1];
7506bc75619SDan Williams 	dcr->windows = 1;
7516bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
7526bc75619SDan Williams 	dcr->command_offset = 0;
7536bc75619SDan Williams 	dcr->command_size = 8;
7546bc75619SDan Williams 	dcr->status_offset = 8;
7556bc75619SDan Williams 	dcr->status_size = 4;
7566bc75619SDan Williams 
7576bc75619SDan Williams 	/* dcr-descriptor2 */
7586bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
7596bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
7606bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
7616bc75619SDan Williams 	dcr->region_index = 2+1;
7626bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
7636bc75619SDan Williams 	dcr->device_id = 0;
7646bc75619SDan Williams 	dcr->revision_id = 1;
7656bc75619SDan Williams 	dcr->serial_number = ~handle[2];
7666bc75619SDan Williams 	dcr->windows = 1;
7676bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
7686bc75619SDan Williams 	dcr->command_offset = 0;
7696bc75619SDan Williams 	dcr->command_size = 8;
7706bc75619SDan Williams 	dcr->status_offset = 8;
7716bc75619SDan Williams 	dcr->status_size = 4;
7726bc75619SDan Williams 
7736bc75619SDan Williams 	/* dcr-descriptor3 */
7746bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
7756bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
7766bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
7776bc75619SDan Williams 	dcr->region_index = 3+1;
7786bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
7796bc75619SDan Williams 	dcr->device_id = 0;
7806bc75619SDan Williams 	dcr->revision_id = 1;
7816bc75619SDan Williams 	dcr->serial_number = ~handle[3];
7826bc75619SDan Williams 	dcr->windows = 1;
7836bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
7846bc75619SDan Williams 	dcr->command_offset = 0;
7856bc75619SDan Williams 	dcr->command_size = 8;
7866bc75619SDan Williams 	dcr->status_offset = 8;
7876bc75619SDan Williams 	dcr->status_size = 4;
7886bc75619SDan Williams 
7896bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
7906bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
7916bc75619SDan Williams 	bdw = nfit_buf + offset;
7926bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
7936bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
7946bc75619SDan Williams 	bdw->region_index = 0+1;
7956bc75619SDan Williams 	bdw->windows = 1;
7966bc75619SDan Williams 	bdw->offset = 0;
7976bc75619SDan Williams 	bdw->size = BDW_SIZE;
7986bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
7996bc75619SDan Williams 	bdw->start_address = 0;
8006bc75619SDan Williams 
8016bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
8026bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
8036bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
8046bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
8056bc75619SDan Williams 	bdw->region_index = 1+1;
8066bc75619SDan Williams 	bdw->windows = 1;
8076bc75619SDan Williams 	bdw->offset = 0;
8086bc75619SDan Williams 	bdw->size = BDW_SIZE;
8096bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
8106bc75619SDan Williams 	bdw->start_address = 0;
8116bc75619SDan Williams 
8126bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
8136bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
8146bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
8156bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
8166bc75619SDan Williams 	bdw->region_index = 2+1;
8176bc75619SDan Williams 	bdw->windows = 1;
8186bc75619SDan Williams 	bdw->offset = 0;
8196bc75619SDan Williams 	bdw->size = BDW_SIZE;
8206bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
8216bc75619SDan Williams 	bdw->start_address = 0;
8226bc75619SDan Williams 
8236bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
8246bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
8256bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
8266bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
8276bc75619SDan Williams 	bdw->region_index = 3+1;
8286bc75619SDan Williams 	bdw->windows = 1;
8296bc75619SDan Williams 	bdw->offset = 0;
8306bc75619SDan Williams 	bdw->size = BDW_SIZE;
8316bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
8326bc75619SDan Williams 	bdw->start_address = 0;
8336bc75619SDan Williams 
8346bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
8356bc75619SDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_dsm_force_en);
8366bc75619SDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_dsm_force_en);
8376bc75619SDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_dsm_force_en);
8386bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
8396bc75619SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
8406bc75619SDan Williams }
8416bc75619SDan Williams 
8426bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
8436bc75619SDan Williams {
8446bc75619SDan Williams 	size_t size = t->nfit_size, offset;
8456bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
8466bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
8476bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
8486bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
8496bc75619SDan Williams 
8506bc75619SDan Williams 	nfit_test_init_header(nfit_buf, size);
8516bc75619SDan Williams 
8526bc75619SDan Williams 	offset = sizeof(struct acpi_table_nfit);
8536bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
8546bc75619SDan Williams 	spa = nfit_buf + offset;
8556bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
8566bc75619SDan Williams 	spa->header.length = sizeof(*spa);
8576bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
8586bc75619SDan Williams 	spa->range_index = 0+1;
8596bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
8606bc75619SDan Williams 	spa->length = SPA2_SIZE;
8616bc75619SDan Williams 
8626bc75619SDan Williams 	offset += sizeof(*spa);
8636bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
8646bc75619SDan Williams 	memdev = nfit_buf + offset;
8656bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
8666bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
8676bc75619SDan Williams 	memdev->device_handle = 0;
8686bc75619SDan Williams 	memdev->physical_id = 0;
8696bc75619SDan Williams 	memdev->region_id = 0;
8706bc75619SDan Williams 	memdev->range_index = 0+1;
8716bc75619SDan Williams 	memdev->region_index = 0+1;
8726bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
8736bc75619SDan Williams 	memdev->region_offset = 0;
8746bc75619SDan Williams 	memdev->address = 0;
8756bc75619SDan Williams 	memdev->interleave_index = 0;
8766bc75619SDan Williams 	memdev->interleave_ways = 1;
877*58138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
878*58138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
879*58138820SDan Williams 		| ACPI_NFIT_MEM_ARMED;
8806bc75619SDan Williams 
8816bc75619SDan Williams 	offset += sizeof(*memdev);
8826bc75619SDan Williams 	/* dcr-descriptor0 */
8836bc75619SDan Williams 	dcr = nfit_buf + offset;
8846bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
8856bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
8866bc75619SDan Williams 	dcr->region_index = 0+1;
8876bc75619SDan Williams 	dcr->vendor_id = 0xabcd;
8886bc75619SDan Williams 	dcr->device_id = 0;
8896bc75619SDan Williams 	dcr->revision_id = 1;
8906bc75619SDan Williams 	dcr->serial_number = ~0;
8916bc75619SDan Williams 	dcr->code = 0x201;
8926bc75619SDan Williams 	dcr->windows = 0;
8936bc75619SDan Williams 	dcr->window_size = 0;
8946bc75619SDan Williams 	dcr->command_offset = 0;
8956bc75619SDan Williams 	dcr->command_size = 0;
8966bc75619SDan Williams 	dcr->status_offset = 0;
8976bc75619SDan Williams 	dcr->status_size = 0;
8986bc75619SDan Williams }
8996bc75619SDan Williams 
9006bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
9016bc75619SDan Williams 		void *iobuf, u64 len, int rw)
9026bc75619SDan Williams {
9036bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
9046bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
9056bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
9066bc75619SDan Williams 	unsigned int lane;
9076bc75619SDan Williams 
9086bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
9096bc75619SDan Williams 	if (rw)
9106bc75619SDan Williams 		memcpy(mmio->base + dpa, iobuf, len);
9116bc75619SDan Williams 	else
9126bc75619SDan Williams 		memcpy(iobuf, mmio->base + dpa, len);
9136bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
9146bc75619SDan Williams 
9156bc75619SDan Williams 	return 0;
9166bc75619SDan Williams }
9176bc75619SDan Williams 
9186bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
9196bc75619SDan Williams {
9206bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
9216bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
9226bc75619SDan Williams 	struct device *dev = &pdev->dev;
9236bc75619SDan Williams 	struct nfit_test *nfit_test;
9246bc75619SDan Williams 	int rc;
9256bc75619SDan Williams 
9266bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
9276bc75619SDan Williams 
9286bc75619SDan Williams 	/* common alloc */
9296bc75619SDan Williams 	if (nfit_test->num_dcr) {
9306bc75619SDan Williams 		int num = nfit_test->num_dcr;
9316bc75619SDan Williams 
9326bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
9336bc75619SDan Williams 				GFP_KERNEL);
9346bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
9356bc75619SDan Williams 				GFP_KERNEL);
9366bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
9376bc75619SDan Williams 				GFP_KERNEL);
9386bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
9396bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
9406bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
9416bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
9426bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
9436bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
9446bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
9456bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
9466bc75619SDan Williams 				&& nfit_test->dcr_dma)
9476bc75619SDan Williams 			/* pass */;
9486bc75619SDan Williams 		else
9496bc75619SDan Williams 			return -ENOMEM;
9506bc75619SDan Williams 	}
9516bc75619SDan Williams 
9526bc75619SDan Williams 	if (nfit_test->num_pm) {
9536bc75619SDan Williams 		int num = nfit_test->num_pm;
9546bc75619SDan Williams 
9556bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
9566bc75619SDan Williams 				GFP_KERNEL);
9576bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
9586bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
9596bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
9606bc75619SDan Williams 			/* pass */;
9616bc75619SDan Williams 		else
9626bc75619SDan Williams 			return -ENOMEM;
9636bc75619SDan Williams 	}
9646bc75619SDan Williams 
9656bc75619SDan Williams 	/* per-nfit specific alloc */
9666bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
9676bc75619SDan Williams 		return -ENOMEM;
9686bc75619SDan Williams 
9696bc75619SDan Williams 	nfit_test->setup(nfit_test);
9706bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
9716bc75619SDan Williams 	acpi_desc->dev = &pdev->dev;
9726bc75619SDan Williams 	acpi_desc->nfit = nfit_test->nfit_buf;
9736bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
9746bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
9756bc75619SDan Williams 	nd_desc->attr_groups = acpi_nfit_attribute_groups;
9766bc75619SDan Williams 	acpi_desc->nvdimm_bus = nvdimm_bus_register(&pdev->dev, nd_desc);
9776bc75619SDan Williams 	if (!acpi_desc->nvdimm_bus)
9786bc75619SDan Williams 		return -ENXIO;
9796bc75619SDan Williams 
9806bc75619SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_size);
9816bc75619SDan Williams 	if (rc) {
9826bc75619SDan Williams 		nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
9836bc75619SDan Williams 		return rc;
9846bc75619SDan Williams 	}
9856bc75619SDan Williams 
9866bc75619SDan Williams 	return 0;
9876bc75619SDan Williams }
9886bc75619SDan Williams 
9896bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
9906bc75619SDan Williams {
9916bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(&pdev->dev);
9926bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc = &nfit_test->acpi_desc;
9936bc75619SDan Williams 
9946bc75619SDan Williams 	nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
9956bc75619SDan Williams 
9966bc75619SDan Williams 	return 0;
9976bc75619SDan Williams }
9986bc75619SDan Williams 
9996bc75619SDan Williams static void nfit_test_release(struct device *dev)
10006bc75619SDan Williams {
10016bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
10026bc75619SDan Williams 
10036bc75619SDan Williams 	kfree(nfit_test);
10046bc75619SDan Williams }
10056bc75619SDan Williams 
10066bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
10076bc75619SDan Williams 	{ KBUILD_MODNAME },
10086bc75619SDan Williams 	{ },
10096bc75619SDan Williams };
10106bc75619SDan Williams 
10116bc75619SDan Williams static struct platform_driver nfit_test_driver = {
10126bc75619SDan Williams 	.probe = nfit_test_probe,
10136bc75619SDan Williams 	.remove = nfit_test_remove,
10146bc75619SDan Williams 	.driver = {
10156bc75619SDan Williams 		.name = KBUILD_MODNAME,
10166bc75619SDan Williams 	},
10176bc75619SDan Williams 	.id_table = nfit_test_id,
10186bc75619SDan Williams };
10196bc75619SDan Williams 
10206bc75619SDan Williams #ifdef CONFIG_CMA_SIZE_MBYTES
10216bc75619SDan Williams #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
10226bc75619SDan Williams #else
10236bc75619SDan Williams #define CMA_SIZE_MBYTES 0
10246bc75619SDan Williams #endif
10256bc75619SDan Williams 
10266bc75619SDan Williams static __init int nfit_test_init(void)
10276bc75619SDan Williams {
10286bc75619SDan Williams 	int rc, i;
10296bc75619SDan Williams 
10306bc75619SDan Williams 	nfit_test_setup(nfit_test_lookup);
10316bc75619SDan Williams 
10326bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
10336bc75619SDan Williams 		struct nfit_test *nfit_test;
10346bc75619SDan Williams 		struct platform_device *pdev;
10356bc75619SDan Williams 		static int once;
10366bc75619SDan Williams 
10376bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
10386bc75619SDan Williams 		if (!nfit_test) {
10396bc75619SDan Williams 			rc = -ENOMEM;
10406bc75619SDan Williams 			goto err_register;
10416bc75619SDan Williams 		}
10426bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
10436bc75619SDan Williams 		switch (i) {
10446bc75619SDan Williams 		case 0:
10456bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
10466bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
10476bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
10486bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
10496bc75619SDan Williams 			break;
10506bc75619SDan Williams 		case 1:
10516bc75619SDan Williams 			nfit_test->num_pm = 1;
10526bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
10536bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
10546bc75619SDan Williams 			break;
10556bc75619SDan Williams 		default:
10566bc75619SDan Williams 			rc = -EINVAL;
10576bc75619SDan Williams 			goto err_register;
10586bc75619SDan Williams 		}
10596bc75619SDan Williams 		pdev = &nfit_test->pdev;
10606bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
10616bc75619SDan Williams 		pdev->id = i;
10626bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
10636bc75619SDan Williams 		rc = platform_device_register(pdev);
10646bc75619SDan Williams 		if (rc) {
10656bc75619SDan Williams 			put_device(&pdev->dev);
10666bc75619SDan Williams 			goto err_register;
10676bc75619SDan Williams 		}
10686bc75619SDan Williams 
10696bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
10706bc75619SDan Williams 		if (rc)
10716bc75619SDan Williams 			goto err_register;
10726bc75619SDan Williams 
10736bc75619SDan Williams 		instances[i] = nfit_test;
10746bc75619SDan Williams 
10756bc75619SDan Williams 		if (!once++) {
10766bc75619SDan Williams 			dma_addr_t dma;
10776bc75619SDan Williams 			void *buf;
10786bc75619SDan Williams 
10796bc75619SDan Williams 			buf = dma_alloc_coherent(&pdev->dev, SZ_128M, &dma,
10806bc75619SDan Williams 					GFP_KERNEL);
10816bc75619SDan Williams 			if (!buf) {
10826bc75619SDan Williams 				rc = -ENOMEM;
10836bc75619SDan Williams 				dev_warn(&pdev->dev, "need 128M of free cma\n");
10846bc75619SDan Williams 				goto err_register;
10856bc75619SDan Williams 			}
10866bc75619SDan Williams 			dma_free_coherent(&pdev->dev, SZ_128M, buf, dma);
10876bc75619SDan Williams 		}
10886bc75619SDan Williams 	}
10896bc75619SDan Williams 
10906bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
10916bc75619SDan Williams 	if (rc)
10926bc75619SDan Williams 		goto err_register;
10936bc75619SDan Williams 	return 0;
10946bc75619SDan Williams 
10956bc75619SDan Williams  err_register:
10966bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
10976bc75619SDan Williams 		if (instances[i])
10986bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
10996bc75619SDan Williams 	nfit_test_teardown();
11006bc75619SDan Williams 	return rc;
11016bc75619SDan Williams }
11026bc75619SDan Williams 
11036bc75619SDan Williams static __exit void nfit_test_exit(void)
11046bc75619SDan Williams {
11056bc75619SDan Williams 	int i;
11066bc75619SDan Williams 
11076bc75619SDan Williams 	platform_driver_unregister(&nfit_test_driver);
11086bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
11096bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
11106bc75619SDan Williams 	nfit_test_teardown();
11116bc75619SDan Williams }
11126bc75619SDan Williams 
11136bc75619SDan Williams module_init(nfit_test_init);
11146bc75619SDan Williams module_exit(nfit_test_exit);
11156bc75619SDan Williams MODULE_LICENSE("GPL v2");
11166bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
1117