xref: /qemu/hw/acpi/nvdimm.c (revision 1439f213)
187252e1bSXiao Guangrong /*
287252e1bSXiao Guangrong  * NVDIMM ACPI Implementation
387252e1bSXiao Guangrong  *
487252e1bSXiao Guangrong  * Copyright(C) 2015 Intel Corporation.
587252e1bSXiao Guangrong  *
687252e1bSXiao Guangrong  * Author:
787252e1bSXiao Guangrong  *  Xiao Guangrong <guangrong.xiao@linux.intel.com>
887252e1bSXiao Guangrong  *
987252e1bSXiao Guangrong  * NFIT is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
1087252e1bSXiao Guangrong  * and the DSM specification can be found at:
1187252e1bSXiao Guangrong  *       http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
1287252e1bSXiao Guangrong  *
1387252e1bSXiao Guangrong  * Currently, it only supports PMEM Virtualization.
1487252e1bSXiao Guangrong  *
1587252e1bSXiao Guangrong  * This library is free software; you can redistribute it and/or
1687252e1bSXiao Guangrong  * modify it under the terms of the GNU Lesser General Public
1787252e1bSXiao Guangrong  * License as published by the Free Software Foundation; either
1887252e1bSXiao Guangrong  * version 2 of the License, or (at your option) any later version.
1987252e1bSXiao Guangrong  *
2087252e1bSXiao Guangrong  * This library is distributed in the hope that it will be useful,
2187252e1bSXiao Guangrong  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2287252e1bSXiao Guangrong  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2387252e1bSXiao Guangrong  * Lesser General Public License for more details.
2487252e1bSXiao Guangrong  *
2587252e1bSXiao Guangrong  * You should have received a copy of the GNU Lesser General Public
2687252e1bSXiao Guangrong  * License along with this library; if not, see <http://www.gnu.org/licenses/>
2787252e1bSXiao Guangrong  */
2887252e1bSXiao Guangrong 
29b6a0aa05SPeter Maydell #include "qemu/osdep.h"
30*1439f213SDongjiu Geng #include "qemu/uuid.h"
3187252e1bSXiao Guangrong #include "hw/acpi/acpi.h"
3287252e1bSXiao Guangrong #include "hw/acpi/aml-build.h"
33b9951413SXiao Guangrong #include "hw/acpi/bios-linker-loader.h"
345fe79386SXiao Guangrong #include "hw/nvram/fw_cfg.h"
3587252e1bSXiao Guangrong #include "hw/mem/nvdimm.h"
363f350f6bSShivaprasad G Bhat #include "qemu/nvdimm-utils.h"
3787252e1bSXiao Guangrong 
3887252e1bSXiao Guangrong /*
3987252e1bSXiao Guangrong  * define Byte Addressable Persistent Memory (PM) Region according to
4087252e1bSXiao Guangrong  * ACPI 6.0: 5.2.25.1 System Physical Address Range Structure.
4187252e1bSXiao Guangrong  */
4287252e1bSXiao Guangrong static const uint8_t nvdimm_nfit_spa_uuid[] =
43*1439f213SDongjiu Geng       UUID_LE(0x66f0d379, 0xb4f3, 0x4074, 0xac, 0x43, 0x0d, 0x33,
4487252e1bSXiao Guangrong               0x18, 0xb7, 0x8c, 0xdb);
4587252e1bSXiao Guangrong 
4687252e1bSXiao Guangrong /*
4787252e1bSXiao Guangrong  * NVDIMM Firmware Interface Table
4887252e1bSXiao Guangrong  * @signature: "NFIT"
4987252e1bSXiao Guangrong  *
5087252e1bSXiao Guangrong  * It provides information that allows OSPM to enumerate NVDIMM present in
5187252e1bSXiao Guangrong  * the platform and associate system physical address ranges created by the
5287252e1bSXiao Guangrong  * NVDIMMs.
5387252e1bSXiao Guangrong  *
5487252e1bSXiao Guangrong  * It is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
5587252e1bSXiao Guangrong  */
5687252e1bSXiao Guangrong struct NvdimmNfitHeader {
5787252e1bSXiao Guangrong     ACPI_TABLE_HEADER_DEF
5887252e1bSXiao Guangrong     uint32_t reserved;
5987252e1bSXiao Guangrong } QEMU_PACKED;
6087252e1bSXiao Guangrong typedef struct NvdimmNfitHeader NvdimmNfitHeader;
6187252e1bSXiao Guangrong 
6287252e1bSXiao Guangrong /*
6387252e1bSXiao Guangrong  * define NFIT structures according to ACPI 6.0: 5.2.25 NVDIMM Firmware
6487252e1bSXiao Guangrong  * Interface Table (NFIT).
6587252e1bSXiao Guangrong  */
6687252e1bSXiao Guangrong 
6787252e1bSXiao Guangrong /*
6887252e1bSXiao Guangrong  * System Physical Address Range Structure
6987252e1bSXiao Guangrong  *
7087252e1bSXiao Guangrong  * It describes the system physical address ranges occupied by NVDIMMs and
7187252e1bSXiao Guangrong  * the types of the regions.
7287252e1bSXiao Guangrong  */
7387252e1bSXiao Guangrong struct NvdimmNfitSpa {
7487252e1bSXiao Guangrong     uint16_t type;
7587252e1bSXiao Guangrong     uint16_t length;
7687252e1bSXiao Guangrong     uint16_t spa_index;
7787252e1bSXiao Guangrong     uint16_t flags;
7887252e1bSXiao Guangrong     uint32_t reserved;
7987252e1bSXiao Guangrong     uint32_t proximity_domain;
8087252e1bSXiao Guangrong     uint8_t type_guid[16];
8187252e1bSXiao Guangrong     uint64_t spa_base;
8287252e1bSXiao Guangrong     uint64_t spa_length;
8387252e1bSXiao Guangrong     uint64_t mem_attr;
8487252e1bSXiao Guangrong } QEMU_PACKED;
8587252e1bSXiao Guangrong typedef struct NvdimmNfitSpa NvdimmNfitSpa;
8687252e1bSXiao Guangrong 
8787252e1bSXiao Guangrong /*
8887252e1bSXiao Guangrong  * Memory Device to System Physical Address Range Mapping Structure
8987252e1bSXiao Guangrong  *
9087252e1bSXiao Guangrong  * It enables identifying each NVDIMM region and the corresponding SPA
9187252e1bSXiao Guangrong  * describing the memory interleave
9287252e1bSXiao Guangrong  */
9387252e1bSXiao Guangrong struct NvdimmNfitMemDev {
9487252e1bSXiao Guangrong     uint16_t type;
9587252e1bSXiao Guangrong     uint16_t length;
9687252e1bSXiao Guangrong     uint32_t nfit_handle;
9787252e1bSXiao Guangrong     uint16_t phys_id;
9887252e1bSXiao Guangrong     uint16_t region_id;
9987252e1bSXiao Guangrong     uint16_t spa_index;
10087252e1bSXiao Guangrong     uint16_t dcr_index;
10187252e1bSXiao Guangrong     uint64_t region_len;
10287252e1bSXiao Guangrong     uint64_t region_offset;
10387252e1bSXiao Guangrong     uint64_t region_dpa;
10487252e1bSXiao Guangrong     uint16_t interleave_index;
10587252e1bSXiao Guangrong     uint16_t interleave_ways;
10687252e1bSXiao Guangrong     uint16_t flags;
10787252e1bSXiao Guangrong     uint16_t reserved;
10887252e1bSXiao Guangrong } QEMU_PACKED;
10987252e1bSXiao Guangrong typedef struct NvdimmNfitMemDev NvdimmNfitMemDev;
11087252e1bSXiao Guangrong 
111cb836434SHaozhong Zhang #define ACPI_NFIT_MEM_NOT_ARMED     (1 << 3)
112cb836434SHaozhong Zhang 
11387252e1bSXiao Guangrong /*
11487252e1bSXiao Guangrong  * NVDIMM Control Region Structure
11587252e1bSXiao Guangrong  *
11687252e1bSXiao Guangrong  * It describes the NVDIMM and if applicable, Block Control Window.
11787252e1bSXiao Guangrong  */
11887252e1bSXiao Guangrong struct NvdimmNfitControlRegion {
11987252e1bSXiao Guangrong     uint16_t type;
12087252e1bSXiao Guangrong     uint16_t length;
12187252e1bSXiao Guangrong     uint16_t dcr_index;
12287252e1bSXiao Guangrong     uint16_t vendor_id;
12387252e1bSXiao Guangrong     uint16_t device_id;
12487252e1bSXiao Guangrong     uint16_t revision_id;
12587252e1bSXiao Guangrong     uint16_t sub_vendor_id;
12687252e1bSXiao Guangrong     uint16_t sub_device_id;
12787252e1bSXiao Guangrong     uint16_t sub_revision_id;
12887252e1bSXiao Guangrong     uint8_t reserved[6];
12987252e1bSXiao Guangrong     uint32_t serial_number;
13087252e1bSXiao Guangrong     uint16_t fic;
13187252e1bSXiao Guangrong     uint16_t num_bcw;
13287252e1bSXiao Guangrong     uint64_t bcw_size;
13387252e1bSXiao Guangrong     uint64_t cmd_offset;
13487252e1bSXiao Guangrong     uint64_t cmd_size;
13587252e1bSXiao Guangrong     uint64_t status_offset;
13687252e1bSXiao Guangrong     uint64_t status_size;
13787252e1bSXiao Guangrong     uint16_t flags;
13887252e1bSXiao Guangrong     uint8_t reserved2[6];
13987252e1bSXiao Guangrong } QEMU_PACKED;
14087252e1bSXiao Guangrong typedef struct NvdimmNfitControlRegion NvdimmNfitControlRegion;
14187252e1bSXiao Guangrong 
14287252e1bSXiao Guangrong /*
1439ab3aad2SRoss Zwisler  * NVDIMM Platform Capabilities Structure
1449ab3aad2SRoss Zwisler  *
1459ab3aad2SRoss Zwisler  * Defined in section 5.2.25.9 of ACPI 6.2 Errata A, September 2017
1469ab3aad2SRoss Zwisler  */
1479ab3aad2SRoss Zwisler struct NvdimmNfitPlatformCaps {
1489ab3aad2SRoss Zwisler     uint16_t type;
1499ab3aad2SRoss Zwisler     uint16_t length;
1509ab3aad2SRoss Zwisler     uint8_t highest_cap;
1519ab3aad2SRoss Zwisler     uint8_t reserved[3];
1529ab3aad2SRoss Zwisler     uint32_t capabilities;
1539ab3aad2SRoss Zwisler     uint8_t reserved2[4];
1549ab3aad2SRoss Zwisler } QEMU_PACKED;
1559ab3aad2SRoss Zwisler typedef struct NvdimmNfitPlatformCaps NvdimmNfitPlatformCaps;
1569ab3aad2SRoss Zwisler 
1579ab3aad2SRoss Zwisler /*
15887252e1bSXiao Guangrong  * Module serial number is a unique number for each device. We use the
15987252e1bSXiao Guangrong  * slot id of NVDIMM device to generate this number so that each device
16087252e1bSXiao Guangrong  * associates with a different number.
16187252e1bSXiao Guangrong  *
16287252e1bSXiao Guangrong  * 0x123456 is a magic number we arbitrarily chose.
16387252e1bSXiao Guangrong  */
16487252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_sn(int slot)
16587252e1bSXiao Guangrong {
16687252e1bSXiao Guangrong     return 0x123456 + slot;
16787252e1bSXiao Guangrong }
16887252e1bSXiao Guangrong 
16987252e1bSXiao Guangrong /*
17087252e1bSXiao Guangrong  * handle is used to uniquely associate nfit_memdev structure with NVDIMM
17187252e1bSXiao Guangrong  * ACPI device - nfit_memdev.nfit_handle matches with the value returned
17287252e1bSXiao Guangrong  * by ACPI device _ADR method.
17387252e1bSXiao Guangrong  *
17487252e1bSXiao Guangrong  * We generate the handle with the slot id of NVDIMM device and reserve
17587252e1bSXiao Guangrong  * 0 for NVDIMM root device.
17687252e1bSXiao Guangrong  */
17787252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_handle(int slot)
17887252e1bSXiao Guangrong {
17987252e1bSXiao Guangrong     return slot + 1;
18087252e1bSXiao Guangrong }
18187252e1bSXiao Guangrong 
18287252e1bSXiao Guangrong /*
18387252e1bSXiao Guangrong  * index uniquely identifies the structure, 0 is reserved which indicates
18487252e1bSXiao Guangrong  * that the structure is not valid or the associated structure is not
18587252e1bSXiao Guangrong  * present.
18687252e1bSXiao Guangrong  *
18787252e1bSXiao Guangrong  * Each NVDIMM device needs two indexes, one for nfit_spa and another for
18887252e1bSXiao Guangrong  * nfit_dc which are generated by the slot id of NVDIMM device.
18987252e1bSXiao Guangrong  */
19087252e1bSXiao Guangrong static uint16_t nvdimm_slot_to_spa_index(int slot)
19187252e1bSXiao Guangrong {
19287252e1bSXiao Guangrong     return (slot + 1) << 1;
19387252e1bSXiao Guangrong }
19487252e1bSXiao Guangrong 
19587252e1bSXiao Guangrong /* See the comments of nvdimm_slot_to_spa_index(). */
19687252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_dcr_index(int slot)
19787252e1bSXiao Guangrong {
19887252e1bSXiao Guangrong     return nvdimm_slot_to_spa_index(slot) + 1;
19987252e1bSXiao Guangrong }
20087252e1bSXiao Guangrong 
2015797dcdcSXiao Guangrong static NVDIMMDevice *nvdimm_get_device_by_handle(uint32_t handle)
2025797dcdcSXiao Guangrong {
2035797dcdcSXiao Guangrong     NVDIMMDevice *nvdimm = NULL;
204cf7c0ff5SXiao Guangrong     GSList *list, *device_list = nvdimm_get_device_list();
2055797dcdcSXiao Guangrong 
2065797dcdcSXiao Guangrong     for (list = device_list; list; list = list->next) {
2075797dcdcSXiao Guangrong         NVDIMMDevice *nvd = list->data;
2085797dcdcSXiao Guangrong         int slot = object_property_get_int(OBJECT(nvd), PC_DIMM_SLOT_PROP,
2095797dcdcSXiao Guangrong                                            NULL);
2105797dcdcSXiao Guangrong 
2115797dcdcSXiao Guangrong         if (nvdimm_slot_to_handle(slot) == handle) {
2125797dcdcSXiao Guangrong             nvdimm = nvd;
2135797dcdcSXiao Guangrong             break;
2145797dcdcSXiao Guangrong         }
2155797dcdcSXiao Guangrong     }
2165797dcdcSXiao Guangrong 
2175797dcdcSXiao Guangrong     g_slist_free(device_list);
2185797dcdcSXiao Guangrong     return nvdimm;
2195797dcdcSXiao Guangrong }
2205797dcdcSXiao Guangrong 
22187252e1bSXiao Guangrong /* ACPI 6.0: 5.2.25.1 System Physical Address Range Structure */
22287252e1bSXiao Guangrong static void
22387252e1bSXiao Guangrong nvdimm_build_structure_spa(GArray *structures, DeviceState *dev)
22487252e1bSXiao Guangrong {
22587252e1bSXiao Guangrong     NvdimmNfitSpa *nfit_spa;
2269ed442b8SMarc-André Lureau     uint64_t addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
22787252e1bSXiao Guangrong                                              NULL);
228b053ef61SMarc-André Lureau     uint64_t size = object_property_get_uint(OBJECT(dev), PC_DIMM_SIZE_PROP,
22987252e1bSXiao Guangrong                                              NULL);
2309ed442b8SMarc-André Lureau     uint32_t node = object_property_get_uint(OBJECT(dev), PC_DIMM_NODE_PROP,
23187252e1bSXiao Guangrong                                              NULL);
23287252e1bSXiao Guangrong     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
23387252e1bSXiao Guangrong                                        NULL);
23487252e1bSXiao Guangrong 
23587252e1bSXiao Guangrong     nfit_spa = acpi_data_push(structures, sizeof(*nfit_spa));
23687252e1bSXiao Guangrong 
23787252e1bSXiao Guangrong     nfit_spa->type = cpu_to_le16(0 /* System Physical Address Range
23887252e1bSXiao Guangrong                                       Structure */);
23987252e1bSXiao Guangrong     nfit_spa->length = cpu_to_le16(sizeof(*nfit_spa));
24087252e1bSXiao Guangrong     nfit_spa->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
24187252e1bSXiao Guangrong 
24287252e1bSXiao Guangrong     /*
24387252e1bSXiao Guangrong      * Control region is strict as all the device info, such as SN, index,
24487252e1bSXiao Guangrong      * is associated with slot id.
24587252e1bSXiao Guangrong      */
24687252e1bSXiao Guangrong     nfit_spa->flags = cpu_to_le16(1 /* Control region is strictly for
24787252e1bSXiao Guangrong                                        management during hot add/online
24887252e1bSXiao Guangrong                                        operation */ |
24987252e1bSXiao Guangrong                                   2 /* Data in Proximity Domain field is
25087252e1bSXiao Guangrong                                        valid*/);
25187252e1bSXiao Guangrong 
25287252e1bSXiao Guangrong     /* NUMA node. */
25387252e1bSXiao Guangrong     nfit_spa->proximity_domain = cpu_to_le32(node);
25487252e1bSXiao Guangrong     /* the region reported as PMEM. */
25587252e1bSXiao Guangrong     memcpy(nfit_spa->type_guid, nvdimm_nfit_spa_uuid,
25687252e1bSXiao Guangrong            sizeof(nvdimm_nfit_spa_uuid));
25787252e1bSXiao Guangrong 
25887252e1bSXiao Guangrong     nfit_spa->spa_base = cpu_to_le64(addr);
25987252e1bSXiao Guangrong     nfit_spa->spa_length = cpu_to_le64(size);
26087252e1bSXiao Guangrong 
26187252e1bSXiao Guangrong     /* It is the PMEM and can be cached as writeback. */
26287252e1bSXiao Guangrong     nfit_spa->mem_attr = cpu_to_le64(0x8ULL /* EFI_MEMORY_WB */ |
26387252e1bSXiao Guangrong                                      0x8000ULL /* EFI_MEMORY_NV */);
26487252e1bSXiao Guangrong }
26587252e1bSXiao Guangrong 
26687252e1bSXiao Guangrong /*
26787252e1bSXiao Guangrong  * ACPI 6.0: 5.2.25.2 Memory Device to System Physical Address Range Mapping
26887252e1bSXiao Guangrong  * Structure
26987252e1bSXiao Guangrong  */
27087252e1bSXiao Guangrong static void
27187252e1bSXiao Guangrong nvdimm_build_structure_memdev(GArray *structures, DeviceState *dev)
27287252e1bSXiao Guangrong {
27387252e1bSXiao Guangrong     NvdimmNfitMemDev *nfit_memdev;
274cb836434SHaozhong Zhang     NVDIMMDevice *nvdimm = NVDIMM(OBJECT(dev));
275b053ef61SMarc-André Lureau     uint64_t size = object_property_get_uint(OBJECT(dev), PC_DIMM_SIZE_PROP,
27687252e1bSXiao Guangrong                                              NULL);
27787252e1bSXiao Guangrong     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
27887252e1bSXiao Guangrong                                             NULL);
27987252e1bSXiao Guangrong     uint32_t handle = nvdimm_slot_to_handle(slot);
28087252e1bSXiao Guangrong 
28187252e1bSXiao Guangrong     nfit_memdev = acpi_data_push(structures, sizeof(*nfit_memdev));
28287252e1bSXiao Guangrong 
28387252e1bSXiao Guangrong     nfit_memdev->type = cpu_to_le16(1 /* Memory Device to System Address
28487252e1bSXiao Guangrong                                          Range Map Structure*/);
28587252e1bSXiao Guangrong     nfit_memdev->length = cpu_to_le16(sizeof(*nfit_memdev));
28687252e1bSXiao Guangrong     nfit_memdev->nfit_handle = cpu_to_le32(handle);
28787252e1bSXiao Guangrong 
28887252e1bSXiao Guangrong     /*
28987252e1bSXiao Guangrong      * associate memory device with System Physical Address Range
29087252e1bSXiao Guangrong      * Structure.
29187252e1bSXiao Guangrong      */
29287252e1bSXiao Guangrong     nfit_memdev->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
29387252e1bSXiao Guangrong     /* associate memory device with Control Region Structure. */
29487252e1bSXiao Guangrong     nfit_memdev->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
29587252e1bSXiao Guangrong 
29687252e1bSXiao Guangrong     /* The memory region on the device. */
29787252e1bSXiao Guangrong     nfit_memdev->region_len = cpu_to_le64(size);
2986ab0c4bdSXiao Guangrong     /* The device address starts from 0. */
2996ab0c4bdSXiao Guangrong     nfit_memdev->region_dpa = cpu_to_le64(0);
30087252e1bSXiao Guangrong 
30187252e1bSXiao Guangrong     /* Only one interleave for PMEM. */
30287252e1bSXiao Guangrong     nfit_memdev->interleave_ways = cpu_to_le16(1);
303cb836434SHaozhong Zhang 
304cb836434SHaozhong Zhang     if (nvdimm->unarmed) {
305cb836434SHaozhong Zhang         nfit_memdev->flags |= cpu_to_le16(ACPI_NFIT_MEM_NOT_ARMED);
306cb836434SHaozhong Zhang     }
30787252e1bSXiao Guangrong }
30887252e1bSXiao Guangrong 
30987252e1bSXiao Guangrong /*
31087252e1bSXiao Guangrong  * ACPI 6.0: 5.2.25.5 NVDIMM Control Region Structure.
31187252e1bSXiao Guangrong  */
31287252e1bSXiao Guangrong static void nvdimm_build_structure_dcr(GArray *structures, DeviceState *dev)
31387252e1bSXiao Guangrong {
31487252e1bSXiao Guangrong     NvdimmNfitControlRegion *nfit_dcr;
31587252e1bSXiao Guangrong     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
31687252e1bSXiao Guangrong                                        NULL);
31787252e1bSXiao Guangrong     uint32_t sn = nvdimm_slot_to_sn(slot);
31887252e1bSXiao Guangrong 
31987252e1bSXiao Guangrong     nfit_dcr = acpi_data_push(structures, sizeof(*nfit_dcr));
32087252e1bSXiao Guangrong 
32187252e1bSXiao Guangrong     nfit_dcr->type = cpu_to_le16(4 /* NVDIMM Control Region Structure */);
32287252e1bSXiao Guangrong     nfit_dcr->length = cpu_to_le16(sizeof(*nfit_dcr));
32387252e1bSXiao Guangrong     nfit_dcr->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
32487252e1bSXiao Guangrong 
32587252e1bSXiao Guangrong     /* vendor: Intel. */
32687252e1bSXiao Guangrong     nfit_dcr->vendor_id = cpu_to_le16(0x8086);
32787252e1bSXiao Guangrong     nfit_dcr->device_id = cpu_to_le16(1);
32887252e1bSXiao Guangrong 
32987252e1bSXiao Guangrong     /* The _DSM method is following Intel's DSM specification. */
33087252e1bSXiao Guangrong     nfit_dcr->revision_id = cpu_to_le16(1 /* Current Revision supported
33187252e1bSXiao Guangrong                                              in ACPI 6.0 is 1. */);
33287252e1bSXiao Guangrong     nfit_dcr->serial_number = cpu_to_le32(sn);
33320fdef58SHaozhong Zhang     nfit_dcr->fic = cpu_to_le16(0x301 /* Format Interface Code:
33420fdef58SHaozhong Zhang                                          Byte addressable, no energy backed.
33520fdef58SHaozhong Zhang                                          See ACPI 6.2, sect 5.2.25.6 and
33620fdef58SHaozhong Zhang                                          JEDEC Annex L Release 3. */);
33787252e1bSXiao Guangrong }
33887252e1bSXiao Guangrong 
3399ab3aad2SRoss Zwisler /*
3409ab3aad2SRoss Zwisler  * ACPI 6.2 Errata A: 5.2.25.9 NVDIMM Platform Capabilities Structure
3419ab3aad2SRoss Zwisler  */
3429ab3aad2SRoss Zwisler static void
3439ab3aad2SRoss Zwisler nvdimm_build_structure_caps(GArray *structures, uint32_t capabilities)
3449ab3aad2SRoss Zwisler {
3459ab3aad2SRoss Zwisler     NvdimmNfitPlatformCaps *nfit_caps;
3469ab3aad2SRoss Zwisler 
3479ab3aad2SRoss Zwisler     nfit_caps = acpi_data_push(structures, sizeof(*nfit_caps));
3489ab3aad2SRoss Zwisler 
3499ab3aad2SRoss Zwisler     nfit_caps->type = cpu_to_le16(7 /* NVDIMM Platform Capabilities */);
3509ab3aad2SRoss Zwisler     nfit_caps->length = cpu_to_le16(sizeof(*nfit_caps));
3519ab3aad2SRoss Zwisler     nfit_caps->highest_cap = 31 - clz32(capabilities);
3529ab3aad2SRoss Zwisler     nfit_caps->capabilities = cpu_to_le32(capabilities);
3539ab3aad2SRoss Zwisler }
3549ab3aad2SRoss Zwisler 
355c1404bdeSEric Auger static GArray *nvdimm_build_device_structure(NVDIMMState *state)
35687252e1bSXiao Guangrong {
357cf7c0ff5SXiao Guangrong     GSList *device_list = nvdimm_get_device_list();
35887252e1bSXiao Guangrong     GArray *structures = g_array_new(false, true /* clear */, 1);
35987252e1bSXiao Guangrong 
36087252e1bSXiao Guangrong     for (; device_list; device_list = device_list->next) {
36187252e1bSXiao Guangrong         DeviceState *dev = device_list->data;
36287252e1bSXiao Guangrong 
36387252e1bSXiao Guangrong         /* build System Physical Address Range Structure. */
36487252e1bSXiao Guangrong         nvdimm_build_structure_spa(structures, dev);
36587252e1bSXiao Guangrong 
36687252e1bSXiao Guangrong         /*
36787252e1bSXiao Guangrong          * build Memory Device to System Physical Address Range Mapping
36887252e1bSXiao Guangrong          * Structure.
36987252e1bSXiao Guangrong          */
37087252e1bSXiao Guangrong         nvdimm_build_structure_memdev(structures, dev);
37187252e1bSXiao Guangrong 
37287252e1bSXiao Guangrong         /* build NVDIMM Control Region Structure. */
37387252e1bSXiao Guangrong         nvdimm_build_structure_dcr(structures, dev);
37487252e1bSXiao Guangrong     }
37575b0713eSXiao Guangrong     g_slist_free(device_list);
37687252e1bSXiao Guangrong 
37711c39b5cSRoss Zwisler     if (state->persistence) {
37811c39b5cSRoss Zwisler         nvdimm_build_structure_caps(structures, state->persistence);
3799ab3aad2SRoss Zwisler     }
3809ab3aad2SRoss Zwisler 
38187252e1bSXiao Guangrong     return structures;
38287252e1bSXiao Guangrong }
38387252e1bSXiao Guangrong 
38475b0713eSXiao Guangrong static void nvdimm_init_fit_buffer(NvdimmFitBuffer *fit_buf)
38575b0713eSXiao Guangrong {
38675b0713eSXiao Guangrong     fit_buf->fit = g_array_new(false, true /* clear */, 1);
38775b0713eSXiao Guangrong }
38875b0713eSXiao Guangrong 
389c1404bdeSEric Auger static void nvdimm_build_fit_buffer(NVDIMMState *state)
39075b0713eSXiao Guangrong {
3919ab3aad2SRoss Zwisler     NvdimmFitBuffer *fit_buf = &state->fit_buf;
3929ab3aad2SRoss Zwisler 
39375b0713eSXiao Guangrong     g_array_free(fit_buf->fit, true);
3949ab3aad2SRoss Zwisler     fit_buf->fit = nvdimm_build_device_structure(state);
39575b0713eSXiao Guangrong     fit_buf->dirty = true;
39675b0713eSXiao Guangrong }
39775b0713eSXiao Guangrong 
398c1404bdeSEric Auger void nvdimm_plug(NVDIMMState *state)
39975b0713eSXiao Guangrong {
4009ab3aad2SRoss Zwisler     nvdimm_build_fit_buffer(state);
40175b0713eSXiao Guangrong }
40275b0713eSXiao Guangrong 
403c1404bdeSEric Auger static void nvdimm_build_nfit(NVDIMMState *state, GArray *table_offsets,
4040e9b9edaSIgor Mammedov                               GArray *table_data, BIOSLinker *linker)
40587252e1bSXiao Guangrong {
40675b0713eSXiao Guangrong     NvdimmFitBuffer *fit_buf = &state->fit_buf;
407c8e6c938SHaozhong Zhang     unsigned int header;
40887252e1bSXiao Guangrong 
40987252e1bSXiao Guangrong     acpi_add_table(table_offsets, table_data);
41087252e1bSXiao Guangrong 
41187252e1bSXiao Guangrong     /* NFIT header. */
412c8e6c938SHaozhong Zhang     header = table_data->len;
413c8e6c938SHaozhong Zhang     acpi_data_push(table_data, sizeof(NvdimmNfitHeader));
41487252e1bSXiao Guangrong     /* NVDIMM device structures. */
41575b0713eSXiao Guangrong     g_array_append_vals(table_data, fit_buf->fit->data, fit_buf->fit->len);
41687252e1bSXiao Guangrong 
417c8e6c938SHaozhong Zhang     build_header(linker, table_data,
418c8e6c938SHaozhong Zhang                  (void *)(table_data->data + header), "NFIT",
41975b0713eSXiao Guangrong                  sizeof(NvdimmNfitHeader) + fit_buf->fit->len, 1, NULL, NULL);
42087252e1bSXiao Guangrong }
42187252e1bSXiao Guangrong 
422cb88ebd7SXiao Guangrong #define NVDIMM_DSM_MEMORY_SIZE      4096
423cb88ebd7SXiao Guangrong 
42418c440e1SXiao Guangrong struct NvdimmDsmIn {
42518c440e1SXiao Guangrong     uint32_t handle;
42618c440e1SXiao Guangrong     uint32_t revision;
42718c440e1SXiao Guangrong     uint32_t function;
42818c440e1SXiao Guangrong     /* the remaining size in the page is used by arg3. */
42918c440e1SXiao Guangrong     union {
43035c5a52dSPaolo Bonzini         uint8_t arg3[4084];
43118c440e1SXiao Guangrong     };
43218c440e1SXiao Guangrong } QEMU_PACKED;
43318c440e1SXiao Guangrong typedef struct NvdimmDsmIn NvdimmDsmIn;
434cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmIn) != NVDIMM_DSM_MEMORY_SIZE);
43518c440e1SXiao Guangrong 
43618c440e1SXiao Guangrong struct NvdimmDsmOut {
43718c440e1SXiao Guangrong     /* the size of buffer filled by QEMU. */
43818c440e1SXiao Guangrong     uint32_t len;
43935c5a52dSPaolo Bonzini     uint8_t data[4092];
44018c440e1SXiao Guangrong } QEMU_PACKED;
44118c440e1SXiao Guangrong typedef struct NvdimmDsmOut NvdimmDsmOut;
442cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmOut) != NVDIMM_DSM_MEMORY_SIZE);
44318c440e1SXiao Guangrong 
444f7df22deSXiao Guangrong struct NvdimmDsmFunc0Out {
445f7df22deSXiao Guangrong     /* the size of buffer filled by QEMU. */
446f7df22deSXiao Guangrong      uint32_t len;
447f7df22deSXiao Guangrong      uint32_t supported_func;
448f7df22deSXiao Guangrong } QEMU_PACKED;
449f7df22deSXiao Guangrong typedef struct NvdimmDsmFunc0Out NvdimmDsmFunc0Out;
450f7df22deSXiao Guangrong 
451f7df22deSXiao Guangrong struct NvdimmDsmFuncNoPayloadOut {
452f7df22deSXiao Guangrong     /* the size of buffer filled by QEMU. */
453f7df22deSXiao Guangrong      uint32_t len;
454f7df22deSXiao Guangrong      uint32_t func_ret_status;
455f7df22deSXiao Guangrong } QEMU_PACKED;
456f7df22deSXiao Guangrong typedef struct NvdimmDsmFuncNoPayloadOut NvdimmDsmFuncNoPayloadOut;
457f7df22deSXiao Guangrong 
4585797dcdcSXiao Guangrong struct NvdimmFuncGetLabelSizeOut {
4595797dcdcSXiao Guangrong     /* the size of buffer filled by QEMU. */
4605797dcdcSXiao Guangrong     uint32_t len;
4615797dcdcSXiao Guangrong     uint32_t func_ret_status; /* return status code. */
4625797dcdcSXiao Guangrong     uint32_t label_size; /* the size of label data area. */
4635797dcdcSXiao Guangrong     /*
4645797dcdcSXiao Guangrong      * Maximum size of the namespace label data length supported by
4655797dcdcSXiao Guangrong      * the platform in Get/Set Namespace Label Data functions.
4665797dcdcSXiao Guangrong      */
4675797dcdcSXiao Guangrong     uint32_t max_xfer;
4685797dcdcSXiao Guangrong } QEMU_PACKED;
4695797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelSizeOut NvdimmFuncGetLabelSizeOut;
470cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelSizeOut) > NVDIMM_DSM_MEMORY_SIZE);
4715797dcdcSXiao Guangrong 
4722b9e57fcSXiao Guangrong struct NvdimmFuncGetLabelDataIn {
4732b9e57fcSXiao Guangrong     uint32_t offset; /* the offset in the namespace label data area. */
4742b9e57fcSXiao Guangrong     uint32_t length; /* the size of data is to be read via the function. */
4752b9e57fcSXiao Guangrong } QEMU_PACKED;
4762b9e57fcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataIn NvdimmFuncGetLabelDataIn;
4772b9e57fcSXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataIn) +
478cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
4792b9e57fcSXiao Guangrong 
4805797dcdcSXiao Guangrong struct NvdimmFuncGetLabelDataOut {
4815797dcdcSXiao Guangrong     /* the size of buffer filled by QEMU. */
4825797dcdcSXiao Guangrong     uint32_t len;
4835797dcdcSXiao Guangrong     uint32_t func_ret_status; /* return status code. */
484f7795e40SPhilippe Mathieu-Daudé     uint8_t out_buf[]; /* the data got via Get Namesapce Label function. */
4855797dcdcSXiao Guangrong } QEMU_PACKED;
4865797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataOut NvdimmFuncGetLabelDataOut;
487cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataOut) > NVDIMM_DSM_MEMORY_SIZE);
4885797dcdcSXiao Guangrong 
4895797dcdcSXiao Guangrong struct NvdimmFuncSetLabelDataIn {
4905797dcdcSXiao Guangrong     uint32_t offset; /* the offset in the namespace label data area. */
4915797dcdcSXiao Guangrong     uint32_t length; /* the size of data is to be written via the function. */
492f7795e40SPhilippe Mathieu-Daudé     uint8_t in_buf[]; /* the data written to label data area. */
4935797dcdcSXiao Guangrong } QEMU_PACKED;
4945797dcdcSXiao Guangrong typedef struct NvdimmFuncSetLabelDataIn NvdimmFuncSetLabelDataIn;
49514e44198SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncSetLabelDataIn) +
496cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
4975797dcdcSXiao Guangrong 
498806864d9SXiao Guangrong struct NvdimmFuncReadFITIn {
4997adbce63SXiao Guangrong     uint32_t offset; /* the offset into FIT buffer. */
500806864d9SXiao Guangrong } QEMU_PACKED;
501806864d9SXiao Guangrong typedef struct NvdimmFuncReadFITIn NvdimmFuncReadFITIn;
502806864d9SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITIn) +
503cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
504806864d9SXiao Guangrong 
505806864d9SXiao Guangrong struct NvdimmFuncReadFITOut {
506806864d9SXiao Guangrong     /* the size of buffer filled by QEMU. */
507806864d9SXiao Guangrong     uint32_t len;
508806864d9SXiao Guangrong     uint32_t func_ret_status; /* return status code. */
509f7795e40SPhilippe Mathieu-Daudé     uint8_t fit[]; /* the FIT data. */
510806864d9SXiao Guangrong } QEMU_PACKED;
511806864d9SXiao Guangrong typedef struct NvdimmFuncReadFITOut NvdimmFuncReadFITOut;
512cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITOut) > NVDIMM_DSM_MEMORY_SIZE);
513806864d9SXiao Guangrong 
514189f4d56SXiao Guangrong static void
515189f4d56SXiao Guangrong nvdimm_dsm_function0(uint32_t supported_func, hwaddr dsm_mem_addr)
516189f4d56SXiao Guangrong {
517189f4d56SXiao Guangrong     NvdimmDsmFunc0Out func0 = {
518189f4d56SXiao Guangrong         .len = cpu_to_le32(sizeof(func0)),
519189f4d56SXiao Guangrong         .supported_func = cpu_to_le32(supported_func),
520189f4d56SXiao Guangrong     };
521189f4d56SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &func0, sizeof(func0));
522189f4d56SXiao Guangrong }
523189f4d56SXiao Guangrong 
524189f4d56SXiao Guangrong static void
525189f4d56SXiao Guangrong nvdimm_dsm_no_payload(uint32_t func_ret_status, hwaddr dsm_mem_addr)
526189f4d56SXiao Guangrong {
527189f4d56SXiao Guangrong     NvdimmDsmFuncNoPayloadOut out = {
528189f4d56SXiao Guangrong         .len = cpu_to_le32(sizeof(out)),
529189f4d56SXiao Guangrong         .func_ret_status = cpu_to_le32(func_ret_status),
530189f4d56SXiao Guangrong     };
531189f4d56SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &out, sizeof(out));
532189f4d56SXiao Guangrong }
533189f4d56SXiao Guangrong 
534c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_SUCCESS        0 /* Success */
535c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_UNSUPPORT      1 /* Not Supported */
536c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_NOMEMDEV       2 /* Non-Existing Memory Device */
537c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_INVALID        3 /* Invalid Input Parameters */
538c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_FIT_CHANGED    0x100 /* FIT Changed */
539c2fa3075SXiao Guangrong 
540806864d9SXiao Guangrong #define NVDIMM_QEMU_RSVD_HANDLE_ROOT         0x10000
541806864d9SXiao Guangrong 
542806864d9SXiao Guangrong /* Read FIT data, defined in docs/specs/acpi_nvdimm.txt. */
543c1404bdeSEric Auger static void nvdimm_dsm_func_read_fit(NVDIMMState *state, NvdimmDsmIn *in,
544806864d9SXiao Guangrong                                      hwaddr dsm_mem_addr)
545806864d9SXiao Guangrong {
546806864d9SXiao Guangrong     NvdimmFitBuffer *fit_buf = &state->fit_buf;
547806864d9SXiao Guangrong     NvdimmFuncReadFITIn *read_fit;
548806864d9SXiao Guangrong     NvdimmFuncReadFITOut *read_fit_out;
549806864d9SXiao Guangrong     GArray *fit;
550806864d9SXiao Guangrong     uint32_t read_len = 0, func_ret_status;
551806864d9SXiao Guangrong     int size;
552806864d9SXiao Guangrong 
553806864d9SXiao Guangrong     read_fit = (NvdimmFuncReadFITIn *)in->arg3;
554435cc3e4SPeter Maydell     read_fit->offset = le32_to_cpu(read_fit->offset);
555806864d9SXiao Guangrong 
556806864d9SXiao Guangrong     fit = fit_buf->fit;
557806864d9SXiao Guangrong 
558806864d9SXiao Guangrong     nvdimm_debug("Read FIT: offset %#x FIT size %#x Dirty %s.\n",
559806864d9SXiao Guangrong                  read_fit->offset, fit->len, fit_buf->dirty ? "Yes" : "No");
560806864d9SXiao Guangrong 
561806864d9SXiao Guangrong     if (read_fit->offset > fit->len) {
562c2fa3075SXiao Guangrong         func_ret_status = NVDIMM_DSM_RET_STATUS_INVALID;
563806864d9SXiao Guangrong         goto exit;
564806864d9SXiao Guangrong     }
565806864d9SXiao Guangrong 
566806864d9SXiao Guangrong     /* It is the first time to read FIT. */
567806864d9SXiao Guangrong     if (!read_fit->offset) {
568806864d9SXiao Guangrong         fit_buf->dirty = false;
569806864d9SXiao Guangrong     } else if (fit_buf->dirty) { /* FIT has been changed during RFIT. */
570c2fa3075SXiao Guangrong         func_ret_status = NVDIMM_DSM_RET_STATUS_FIT_CHANGED;
571806864d9SXiao Guangrong         goto exit;
572806864d9SXiao Guangrong     }
573806864d9SXiao Guangrong 
574c2fa3075SXiao Guangrong     func_ret_status = NVDIMM_DSM_RET_STATUS_SUCCESS;
575806864d9SXiao Guangrong     read_len = MIN(fit->len - read_fit->offset,
576cb88ebd7SXiao Guangrong                    NVDIMM_DSM_MEMORY_SIZE - sizeof(NvdimmFuncReadFITOut));
577806864d9SXiao Guangrong 
578806864d9SXiao Guangrong exit:
579806864d9SXiao Guangrong     size = sizeof(NvdimmFuncReadFITOut) + read_len;
580806864d9SXiao Guangrong     read_fit_out = g_malloc(size);
581806864d9SXiao Guangrong 
582806864d9SXiao Guangrong     read_fit_out->len = cpu_to_le32(size);
583806864d9SXiao Guangrong     read_fit_out->func_ret_status = cpu_to_le32(func_ret_status);
584806864d9SXiao Guangrong     memcpy(read_fit_out->fit, fit->data + read_fit->offset, read_len);
585806864d9SXiao Guangrong 
586806864d9SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, read_fit_out, size);
587806864d9SXiao Guangrong 
588806864d9SXiao Guangrong     g_free(read_fit_out);
589806864d9SXiao Guangrong }
590806864d9SXiao Guangrong 
5915a33db78SXiao Guangrong static void
592c1404bdeSEric Auger nvdimm_dsm_handle_reserved_root_method(NVDIMMState *state,
5935a33db78SXiao Guangrong                                        NvdimmDsmIn *in, hwaddr dsm_mem_addr)
594806864d9SXiao Guangrong {
595806864d9SXiao Guangrong     switch (in->function) {
596806864d9SXiao Guangrong     case 0x0:
597806864d9SXiao Guangrong         nvdimm_dsm_function0(0x1 | 1 << 1 /* Read FIT */, dsm_mem_addr);
598806864d9SXiao Guangrong         return;
599806864d9SXiao Guangrong     case 0x1 /* Read FIT */:
600806864d9SXiao Guangrong         nvdimm_dsm_func_read_fit(state, in, dsm_mem_addr);
601806864d9SXiao Guangrong         return;
602806864d9SXiao Guangrong     }
603806864d9SXiao Guangrong 
604c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
605806864d9SXiao Guangrong }
606806864d9SXiao Guangrong 
607189f4d56SXiao Guangrong static void nvdimm_dsm_root(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
608189f4d56SXiao Guangrong {
609189f4d56SXiao Guangrong     /*
610189f4d56SXiao Guangrong      * function 0 is called to inquire which functions are supported by
611189f4d56SXiao Guangrong      * OSPM
612189f4d56SXiao Guangrong      */
613189f4d56SXiao Guangrong     if (!in->function) {
614189f4d56SXiao Guangrong         nvdimm_dsm_function0(0 /* No function supported other than
615189f4d56SXiao Guangrong                                   function 0 */, dsm_mem_addr);
616189f4d56SXiao Guangrong         return;
617189f4d56SXiao Guangrong     }
618189f4d56SXiao Guangrong 
619189f4d56SXiao Guangrong     /* No function except function 0 is supported yet. */
620c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
621189f4d56SXiao Guangrong }
622189f4d56SXiao Guangrong 
6235797dcdcSXiao Guangrong /*
6245797dcdcSXiao Guangrong  * the max transfer size is the max size transferred by both a
6255797dcdcSXiao Guangrong  * 'Get Namespace Label Data' function and a 'Set Namespace Label Data'
6265797dcdcSXiao Guangrong  * function.
6275797dcdcSXiao Guangrong  */
6285797dcdcSXiao Guangrong static uint32_t nvdimm_get_max_xfer_label_size(void)
6295797dcdcSXiao Guangrong {
630cb88ebd7SXiao Guangrong     uint32_t max_get_size, max_set_size, dsm_memory_size;
631cb88ebd7SXiao Guangrong 
632cb88ebd7SXiao Guangrong     dsm_memory_size = NVDIMM_DSM_MEMORY_SIZE;
6335797dcdcSXiao Guangrong 
6345797dcdcSXiao Guangrong     /*
6355797dcdcSXiao Guangrong      * the max data ACPI can read one time which is transferred by
6365797dcdcSXiao Guangrong      * the response of 'Get Namespace Label Data' function.
6375797dcdcSXiao Guangrong      */
6385797dcdcSXiao Guangrong     max_get_size = dsm_memory_size - sizeof(NvdimmFuncGetLabelDataOut);
6395797dcdcSXiao Guangrong 
6405797dcdcSXiao Guangrong     /*
6415797dcdcSXiao Guangrong      * the max data ACPI can write one time which is transferred by
6425797dcdcSXiao Guangrong      * 'Set Namespace Label Data' function.
6435797dcdcSXiao Guangrong      */
6445797dcdcSXiao Guangrong     max_set_size = dsm_memory_size - offsetof(NvdimmDsmIn, arg3) -
6455797dcdcSXiao Guangrong                    sizeof(NvdimmFuncSetLabelDataIn);
6465797dcdcSXiao Guangrong 
6475797dcdcSXiao Guangrong     return MIN(max_get_size, max_set_size);
6485797dcdcSXiao Guangrong }
6495797dcdcSXiao Guangrong 
6505797dcdcSXiao Guangrong /*
6515797dcdcSXiao Guangrong  * DSM Spec Rev1 4.4 Get Namespace Label Size (Function Index 4).
6525797dcdcSXiao Guangrong  *
6535797dcdcSXiao Guangrong  * It gets the size of Namespace Label data area and the max data size
6545797dcdcSXiao Guangrong  * that Get/Set Namespace Label Data functions can transfer.
6555797dcdcSXiao Guangrong  */
6565797dcdcSXiao Guangrong static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm, hwaddr dsm_mem_addr)
6575797dcdcSXiao Guangrong {
6585797dcdcSXiao Guangrong     NvdimmFuncGetLabelSizeOut label_size_out = {
6595797dcdcSXiao Guangrong         .len = cpu_to_le32(sizeof(label_size_out)),
6605797dcdcSXiao Guangrong     };
6615797dcdcSXiao Guangrong     uint32_t label_size, mxfer;
6625797dcdcSXiao Guangrong 
6635797dcdcSXiao Guangrong     label_size = nvdimm->label_size;
6645797dcdcSXiao Guangrong     mxfer = nvdimm_get_max_xfer_label_size();
6655797dcdcSXiao Guangrong 
6665797dcdcSXiao Guangrong     nvdimm_debug("label_size %#x, max_xfer %#x.\n", label_size, mxfer);
6675797dcdcSXiao Guangrong 
668c2fa3075SXiao Guangrong     label_size_out.func_ret_status = cpu_to_le32(NVDIMM_DSM_RET_STATUS_SUCCESS);
6695797dcdcSXiao Guangrong     label_size_out.label_size = cpu_to_le32(label_size);
6705797dcdcSXiao Guangrong     label_size_out.max_xfer = cpu_to_le32(mxfer);
6715797dcdcSXiao Guangrong 
6725797dcdcSXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &label_size_out,
6735797dcdcSXiao Guangrong                               sizeof(label_size_out));
6745797dcdcSXiao Guangrong }
6755797dcdcSXiao Guangrong 
6762b9e57fcSXiao Guangrong static uint32_t nvdimm_rw_label_data_check(NVDIMMDevice *nvdimm,
6772b9e57fcSXiao Guangrong                                            uint32_t offset, uint32_t length)
6782b9e57fcSXiao Guangrong {
679c2fa3075SXiao Guangrong     uint32_t ret = NVDIMM_DSM_RET_STATUS_INVALID;
6802b9e57fcSXiao Guangrong 
6812b9e57fcSXiao Guangrong     if (offset + length < offset) {
6822b9e57fcSXiao Guangrong         nvdimm_debug("offset %#x + length %#x is overflow.\n", offset,
6832b9e57fcSXiao Guangrong                      length);
6842b9e57fcSXiao Guangrong         return ret;
6852b9e57fcSXiao Guangrong     }
6862b9e57fcSXiao Guangrong 
6872b9e57fcSXiao Guangrong     if (nvdimm->label_size < offset + length) {
6882b9e57fcSXiao Guangrong         nvdimm_debug("position %#x is beyond label data (len = %" PRIx64 ").\n",
6892b9e57fcSXiao Guangrong                      offset + length, nvdimm->label_size);
6902b9e57fcSXiao Guangrong         return ret;
6912b9e57fcSXiao Guangrong     }
6922b9e57fcSXiao Guangrong 
6932b9e57fcSXiao Guangrong     if (length > nvdimm_get_max_xfer_label_size()) {
6942b9e57fcSXiao Guangrong         nvdimm_debug("length (%#x) is larger than max_xfer (%#x).\n",
6952b9e57fcSXiao Guangrong                      length, nvdimm_get_max_xfer_label_size());
6962b9e57fcSXiao Guangrong         return ret;
6972b9e57fcSXiao Guangrong     }
6982b9e57fcSXiao Guangrong 
699c2fa3075SXiao Guangrong     return NVDIMM_DSM_RET_STATUS_SUCCESS;
7002b9e57fcSXiao Guangrong }
7012b9e57fcSXiao Guangrong 
7022b9e57fcSXiao Guangrong /*
7032b9e57fcSXiao Guangrong  * DSM Spec Rev1 4.5 Get Namespace Label Data (Function Index 5).
7042b9e57fcSXiao Guangrong  */
7052b9e57fcSXiao Guangrong static void nvdimm_dsm_get_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
7062b9e57fcSXiao Guangrong                                       hwaddr dsm_mem_addr)
7072b9e57fcSXiao Guangrong {
7082b9e57fcSXiao Guangrong     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
7092b9e57fcSXiao Guangrong     NvdimmFuncGetLabelDataIn *get_label_data;
7102b9e57fcSXiao Guangrong     NvdimmFuncGetLabelDataOut *get_label_data_out;
7112b9e57fcSXiao Guangrong     uint32_t status;
7122b9e57fcSXiao Guangrong     int size;
7132b9e57fcSXiao Guangrong 
7142b9e57fcSXiao Guangrong     get_label_data = (NvdimmFuncGetLabelDataIn *)in->arg3;
715435cc3e4SPeter Maydell     get_label_data->offset = le32_to_cpu(get_label_data->offset);
716435cc3e4SPeter Maydell     get_label_data->length = le32_to_cpu(get_label_data->length);
7172b9e57fcSXiao Guangrong 
7182b9e57fcSXiao Guangrong     nvdimm_debug("Read Label Data: offset %#x length %#x.\n",
7192b9e57fcSXiao Guangrong                  get_label_data->offset, get_label_data->length);
7202b9e57fcSXiao Guangrong 
7212b9e57fcSXiao Guangrong     status = nvdimm_rw_label_data_check(nvdimm, get_label_data->offset,
7222b9e57fcSXiao Guangrong                                         get_label_data->length);
723c2fa3075SXiao Guangrong     if (status != NVDIMM_DSM_RET_STATUS_SUCCESS) {
7242b9e57fcSXiao Guangrong         nvdimm_dsm_no_payload(status, dsm_mem_addr);
7252b9e57fcSXiao Guangrong         return;
7262b9e57fcSXiao Guangrong     }
7272b9e57fcSXiao Guangrong 
7282b9e57fcSXiao Guangrong     size = sizeof(*get_label_data_out) + get_label_data->length;
729cb88ebd7SXiao Guangrong     assert(size <= NVDIMM_DSM_MEMORY_SIZE);
7302b9e57fcSXiao Guangrong     get_label_data_out = g_malloc(size);
7312b9e57fcSXiao Guangrong 
7322b9e57fcSXiao Guangrong     get_label_data_out->len = cpu_to_le32(size);
733c2fa3075SXiao Guangrong     get_label_data_out->func_ret_status =
734c2fa3075SXiao Guangrong                             cpu_to_le32(NVDIMM_DSM_RET_STATUS_SUCCESS);
7352b9e57fcSXiao Guangrong     nvc->read_label_data(nvdimm, get_label_data_out->out_buf,
7362b9e57fcSXiao Guangrong                          get_label_data->length, get_label_data->offset);
7372b9e57fcSXiao Guangrong 
7382b9e57fcSXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, get_label_data_out, size);
7392b9e57fcSXiao Guangrong     g_free(get_label_data_out);
7402b9e57fcSXiao Guangrong }
7412b9e57fcSXiao Guangrong 
74214e44198SXiao Guangrong /*
74314e44198SXiao Guangrong  * DSM Spec Rev1 4.6 Set Namespace Label Data (Function Index 6).
74414e44198SXiao Guangrong  */
74514e44198SXiao Guangrong static void nvdimm_dsm_set_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
74614e44198SXiao Guangrong                                       hwaddr dsm_mem_addr)
74714e44198SXiao Guangrong {
74814e44198SXiao Guangrong     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
74914e44198SXiao Guangrong     NvdimmFuncSetLabelDataIn *set_label_data;
75014e44198SXiao Guangrong     uint32_t status;
75114e44198SXiao Guangrong 
75214e44198SXiao Guangrong     set_label_data = (NvdimmFuncSetLabelDataIn *)in->arg3;
75314e44198SXiao Guangrong 
754435cc3e4SPeter Maydell     set_label_data->offset = le32_to_cpu(set_label_data->offset);
755435cc3e4SPeter Maydell     set_label_data->length = le32_to_cpu(set_label_data->length);
75614e44198SXiao Guangrong 
75714e44198SXiao Guangrong     nvdimm_debug("Write Label Data: offset %#x length %#x.\n",
75814e44198SXiao Guangrong                  set_label_data->offset, set_label_data->length);
75914e44198SXiao Guangrong 
76014e44198SXiao Guangrong     status = nvdimm_rw_label_data_check(nvdimm, set_label_data->offset,
76114e44198SXiao Guangrong                                         set_label_data->length);
762c2fa3075SXiao Guangrong     if (status != NVDIMM_DSM_RET_STATUS_SUCCESS) {
76314e44198SXiao Guangrong         nvdimm_dsm_no_payload(status, dsm_mem_addr);
76414e44198SXiao Guangrong         return;
76514e44198SXiao Guangrong     }
76614e44198SXiao Guangrong 
767cb88ebd7SXiao Guangrong     assert(offsetof(NvdimmDsmIn, arg3) + sizeof(*set_label_data) +
768cb88ebd7SXiao Guangrong                     set_label_data->length <= NVDIMM_DSM_MEMORY_SIZE);
76914e44198SXiao Guangrong 
77014e44198SXiao Guangrong     nvc->write_label_data(nvdimm, set_label_data->in_buf,
77114e44198SXiao Guangrong                           set_label_data->length, set_label_data->offset);
772c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_SUCCESS, dsm_mem_addr);
77314e44198SXiao Guangrong }
77414e44198SXiao Guangrong 
775189f4d56SXiao Guangrong static void nvdimm_dsm_device(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
776189f4d56SXiao Guangrong {
7775797dcdcSXiao Guangrong     NVDIMMDevice *nvdimm = nvdimm_get_device_by_handle(in->handle);
7785797dcdcSXiao Guangrong 
779189f4d56SXiao Guangrong     /* See the comments in nvdimm_dsm_root(). */
780189f4d56SXiao Guangrong     if (!in->function) {
7815797dcdcSXiao Guangrong         uint32_t supported_func = 0;
7825797dcdcSXiao Guangrong 
7835797dcdcSXiao Guangrong         if (nvdimm && nvdimm->label_size) {
7845797dcdcSXiao Guangrong             supported_func |= 0x1 /* Bit 0 indicates whether there is
7855797dcdcSXiao Guangrong                                      support for any functions other
7865797dcdcSXiao Guangrong                                      than function 0. */ |
7872b9e57fcSXiao Guangrong                               1 << 4 /* Get Namespace Label Size */ |
78814e44198SXiao Guangrong                               1 << 5 /* Get Namespace Label Data */ |
78914e44198SXiao Guangrong                               1 << 6 /* Set Namespace Label Data */;
7905797dcdcSXiao Guangrong         }
7915797dcdcSXiao Guangrong         nvdimm_dsm_function0(supported_func, dsm_mem_addr);
792189f4d56SXiao Guangrong         return;
793189f4d56SXiao Guangrong     }
794189f4d56SXiao Guangrong 
7955797dcdcSXiao Guangrong     if (!nvdimm) {
796c2fa3075SXiao Guangrong         nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_NOMEMDEV,
7975797dcdcSXiao Guangrong                               dsm_mem_addr);
7985797dcdcSXiao Guangrong         return;
7995797dcdcSXiao Guangrong     }
8005797dcdcSXiao Guangrong 
8015797dcdcSXiao Guangrong     /* Encode DSM function according to DSM Spec Rev1. */
8025797dcdcSXiao Guangrong     switch (in->function) {
8035797dcdcSXiao Guangrong     case 4 /* Get Namespace Label Size */:
8045797dcdcSXiao Guangrong         if (nvdimm->label_size) {
8055797dcdcSXiao Guangrong             nvdimm_dsm_label_size(nvdimm, dsm_mem_addr);
8065797dcdcSXiao Guangrong             return;
8075797dcdcSXiao Guangrong         }
8085797dcdcSXiao Guangrong         break;
8092b9e57fcSXiao Guangrong     case 5 /* Get Namespace Label Data */:
8102b9e57fcSXiao Guangrong         if (nvdimm->label_size) {
8112b9e57fcSXiao Guangrong             nvdimm_dsm_get_label_data(nvdimm, in, dsm_mem_addr);
8122b9e57fcSXiao Guangrong             return;
8132b9e57fcSXiao Guangrong         }
8142b9e57fcSXiao Guangrong         break;
81514e44198SXiao Guangrong     case 0x6 /* Set Namespace Label Data */:
81614e44198SXiao Guangrong         if (nvdimm->label_size) {
81714e44198SXiao Guangrong             nvdimm_dsm_set_label_data(nvdimm, in, dsm_mem_addr);
81814e44198SXiao Guangrong             return;
81914e44198SXiao Guangrong         }
82014e44198SXiao Guangrong         break;
8215797dcdcSXiao Guangrong     }
8225797dcdcSXiao Guangrong 
823c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
824189f4d56SXiao Guangrong }
825189f4d56SXiao Guangrong 
8265fe79386SXiao Guangrong static uint64_t
8275fe79386SXiao Guangrong nvdimm_dsm_read(void *opaque, hwaddr addr, unsigned size)
8285fe79386SXiao Guangrong {
829f7df22deSXiao Guangrong     nvdimm_debug("BUG: we never read _DSM IO Port.\n");
8305fe79386SXiao Guangrong     return 0;
8315fe79386SXiao Guangrong }
8325fe79386SXiao Guangrong 
8335fe79386SXiao Guangrong static void
8345fe79386SXiao Guangrong nvdimm_dsm_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
8355fe79386SXiao Guangrong {
836c1404bdeSEric Auger     NVDIMMState *state = opaque;
837f7df22deSXiao Guangrong     NvdimmDsmIn *in;
838f7df22deSXiao Guangrong     hwaddr dsm_mem_addr = val;
839f7df22deSXiao Guangrong 
840f7df22deSXiao Guangrong     nvdimm_debug("dsm memory address %#" HWADDR_PRIx ".\n", dsm_mem_addr);
841f7df22deSXiao Guangrong 
842f7df22deSXiao Guangrong     /*
843f7df22deSXiao Guangrong      * The DSM memory is mapped to guest address space so an evil guest
844f7df22deSXiao Guangrong      * can change its content while we are doing DSM emulation. Avoid
845f7df22deSXiao Guangrong      * this by copying DSM memory to QEMU local memory.
846f7df22deSXiao Guangrong      */
84735c5a52dSPaolo Bonzini     in = g_new(NvdimmDsmIn, 1);
84835c5a52dSPaolo Bonzini     cpu_physical_memory_read(dsm_mem_addr, in, sizeof(*in));
849f7df22deSXiao Guangrong 
850435cc3e4SPeter Maydell     in->revision = le32_to_cpu(in->revision);
851435cc3e4SPeter Maydell     in->function = le32_to_cpu(in->function);
852435cc3e4SPeter Maydell     in->handle = le32_to_cpu(in->handle);
853f7df22deSXiao Guangrong 
854f7df22deSXiao Guangrong     nvdimm_debug("Revision %#x Handler %#x Function %#x.\n", in->revision,
855f7df22deSXiao Guangrong                  in->handle, in->function);
856f7df22deSXiao Guangrong 
857d15fc53fSXiao Guangrong     if (in->revision != 0x1 /* Currently we only support DSM Spec Rev1. */) {
858d15fc53fSXiao Guangrong         nvdimm_debug("Revision %#x is not supported, expect %#x.\n",
859d15fc53fSXiao Guangrong                      in->revision, 0x1);
860c2fa3075SXiao Guangrong         nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
861d15fc53fSXiao Guangrong         goto exit;
862d15fc53fSXiao Guangrong     }
863d15fc53fSXiao Guangrong 
864806864d9SXiao Guangrong     if (in->handle == NVDIMM_QEMU_RSVD_HANDLE_ROOT) {
8655a33db78SXiao Guangrong         nvdimm_dsm_handle_reserved_root_method(state, in, dsm_mem_addr);
866806864d9SXiao Guangrong         goto exit;
867806864d9SXiao Guangrong     }
868806864d9SXiao Guangrong 
869189f4d56SXiao Guangrong      /* Handle 0 is reserved for NVDIMM Root Device. */
870189f4d56SXiao Guangrong     if (!in->handle) {
871189f4d56SXiao Guangrong         nvdimm_dsm_root(in, dsm_mem_addr);
872189f4d56SXiao Guangrong         goto exit;
873f7df22deSXiao Guangrong     }
874f7df22deSXiao Guangrong 
875189f4d56SXiao Guangrong     nvdimm_dsm_device(in, dsm_mem_addr);
876189f4d56SXiao Guangrong 
877189f4d56SXiao Guangrong exit:
878f7df22deSXiao Guangrong     g_free(in);
8795fe79386SXiao Guangrong }
8805fe79386SXiao Guangrong 
8815fe79386SXiao Guangrong static const MemoryRegionOps nvdimm_dsm_ops = {
8825fe79386SXiao Guangrong     .read = nvdimm_dsm_read,
8835fe79386SXiao Guangrong     .write = nvdimm_dsm_write,
8845fe79386SXiao Guangrong     .endianness = DEVICE_LITTLE_ENDIAN,
8855fe79386SXiao Guangrong     .valid = {
8865fe79386SXiao Guangrong         .min_access_size = 4,
8875fe79386SXiao Guangrong         .max_access_size = 4,
8885fe79386SXiao Guangrong     },
8895fe79386SXiao Guangrong };
8905fe79386SXiao Guangrong 
89175f27498SXiao Guangrong void nvdimm_acpi_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev)
89275f27498SXiao Guangrong {
89375f27498SXiao Guangrong     if (dev->hotplugged) {
89475f27498SXiao Guangrong         acpi_send_event(DEVICE(hotplug_dev), ACPI_NVDIMM_HOTPLUG_STATUS);
89575f27498SXiao Guangrong     }
89675f27498SXiao Guangrong }
89775f27498SXiao Guangrong 
898c1404bdeSEric Auger void nvdimm_init_acpi_state(NVDIMMState *state, MemoryRegion *io,
8995c94b826SKwangwoo Lee                             struct AcpiGenericAddress dsm_io,
9005fe79386SXiao Guangrong                             FWCfgState *fw_cfg, Object *owner)
9015fe79386SXiao Guangrong {
9025c94b826SKwangwoo Lee     state->dsm_io = dsm_io;
9035fe79386SXiao Guangrong     memory_region_init_io(&state->io_mr, owner, &nvdimm_dsm_ops, state,
9045c94b826SKwangwoo Lee                           "nvdimm-acpi-io", dsm_io.bit_width >> 3);
9055c94b826SKwangwoo Lee     memory_region_add_subregion(io, dsm_io.address, &state->io_mr);
9065fe79386SXiao Guangrong 
9075fe79386SXiao Guangrong     state->dsm_mem = g_array_new(false, true /* clear */, 1);
90835c5a52dSPaolo Bonzini     acpi_data_push(state->dsm_mem, sizeof(NvdimmDsmIn));
9095fe79386SXiao Guangrong     fw_cfg_add_file(fw_cfg, NVDIMM_DSM_MEM_FILE, state->dsm_mem->data,
9105fe79386SXiao Guangrong                     state->dsm_mem->len);
91175b0713eSXiao Guangrong 
91275b0713eSXiao Guangrong     nvdimm_init_fit_buffer(&state->fit_buf);
9135fe79386SXiao Guangrong }
9145fe79386SXiao Guangrong 
91577286395SXiao Guangrong #define NVDIMM_COMMON_DSM       "NCAL"
916b9951413SXiao Guangrong #define NVDIMM_ACPI_MEM_ADDR    "MEMA"
91777286395SXiao Guangrong 
9183ae66c45SXiao Guangrong #define NVDIMM_DSM_MEMORY       "NRAM"
9193ae66c45SXiao Guangrong #define NVDIMM_DSM_IOPORT       "NPIO"
9203ae66c45SXiao Guangrong 
9213ae66c45SXiao Guangrong #define NVDIMM_DSM_NOTIFY       "NTFI"
9223ae66c45SXiao Guangrong #define NVDIMM_DSM_HANDLE       "HDLE"
9233ae66c45SXiao Guangrong #define NVDIMM_DSM_REVISION     "REVS"
9243ae66c45SXiao Guangrong #define NVDIMM_DSM_FUNCTION     "FUNC"
9253ae66c45SXiao Guangrong #define NVDIMM_DSM_ARG3         "FARG"
9263ae66c45SXiao Guangrong 
9273ae66c45SXiao Guangrong #define NVDIMM_DSM_OUT_BUF_SIZE "RLEN"
9283ae66c45SXiao Guangrong #define NVDIMM_DSM_OUT_BUF      "ODAT"
9293ae66c45SXiao Guangrong 
930806864d9SXiao Guangrong #define NVDIMM_DSM_RFIT_STATUS  "RSTA"
931806864d9SXiao Guangrong 
932806864d9SXiao Guangrong #define NVDIMM_QEMU_RSVD_UUID   "648B9CF2-CDA1-4312-8AD9-49C4AF32BD62"
933806864d9SXiao Guangrong 
9345c94b826SKwangwoo Lee static void nvdimm_build_common_dsm(Aml *dev,
9355c94b826SKwangwoo Lee                                     NVDIMMState *nvdimm_state)
93677286395SXiao Guangrong {
937806864d9SXiao Guangrong     Aml *method, *ifctx, *function, *handle, *uuid, *dsm_mem, *elsectx2;
93890623ebfSXiao Guangrong     Aml *elsectx, *unsupport, *unpatched, *expected_uuid, *uuid_invalid;
939fa1a448dSXiao Guangrong     Aml *pckg, *pckg_index, *pckg_buf, *field, *dsm_out_buf, *dsm_out_buf_size;
94071b0269aSShameer Kolothum     Aml *whilectx, *offset;
94177286395SXiao Guangrong     uint8_t byte_list[1];
9425c94b826SKwangwoo Lee     AmlRegionSpace rs;
94377286395SXiao Guangrong 
944732b530cSXiao Guangrong     method = aml_method(NVDIMM_COMMON_DSM, 5, AML_SERIALIZED);
94590623ebfSXiao Guangrong     uuid = aml_arg(0);
94677286395SXiao Guangrong     function = aml_arg(2);
94790623ebfSXiao Guangrong     handle = aml_arg(4);
948c0b3b863SXiao Guangrong     dsm_mem = aml_local(6);
94948bee476SXiao Guangrong     dsm_out_buf = aml_local(7);
950c0b3b863SXiao Guangrong 
951c0b3b863SXiao Guangrong     aml_append(method, aml_store(aml_name(NVDIMM_ACPI_MEM_ADDR), dsm_mem));
952c0b3b863SXiao Guangrong 
9535c94b826SKwangwoo Lee     if (nvdimm_state->dsm_io.space_id == AML_AS_SYSTEM_IO) {
9545c94b826SKwangwoo Lee         rs = AML_SYSTEM_IO;
9555c94b826SKwangwoo Lee     } else {
9565c94b826SKwangwoo Lee         rs = AML_SYSTEM_MEMORY;
9575c94b826SKwangwoo Lee     }
9585c94b826SKwangwoo Lee 
959c0b3b863SXiao Guangrong     /* map DSM memory and IO into ACPI namespace. */
9605c94b826SKwangwoo Lee     aml_append(method, aml_operation_region(NVDIMM_DSM_IOPORT, rs,
9615c94b826SKwangwoo Lee                aml_int(nvdimm_state->dsm_io.address),
9625c94b826SKwangwoo Lee                nvdimm_state->dsm_io.bit_width >> 3));
9633ae66c45SXiao Guangrong     aml_append(method, aml_operation_region(NVDIMM_DSM_MEMORY,
9643ae66c45SXiao Guangrong                AML_SYSTEM_MEMORY, dsm_mem, sizeof(NvdimmDsmIn)));
965c0b3b863SXiao Guangrong 
966c0b3b863SXiao Guangrong     /*
967c0b3b863SXiao Guangrong      * DSM notifier:
9683ae66c45SXiao Guangrong      * NVDIMM_DSM_NOTIFY: write the address of DSM memory and notify QEMU to
9693ae66c45SXiao Guangrong      *                    emulate the access.
970c0b3b863SXiao Guangrong      *
971c0b3b863SXiao Guangrong      * It is the IO port so that accessing them will cause VM-exit, the
972c0b3b863SXiao Guangrong      * control will be transferred to QEMU.
973c0b3b863SXiao Guangrong      */
9743ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_IOPORT, AML_DWORD_ACC, AML_NOLOCK,
9753ae66c45SXiao Guangrong                       AML_PRESERVE);
9763ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_NOTIFY,
9775c94b826SKwangwoo Lee                nvdimm_state->dsm_io.bit_width));
978c0b3b863SXiao Guangrong     aml_append(method, field);
979c0b3b863SXiao Guangrong 
980c0b3b863SXiao Guangrong     /*
981c0b3b863SXiao Guangrong      * DSM input:
9823ae66c45SXiao Guangrong      * NVDIMM_DSM_HANDLE: store device's handle, it's zero if the _DSM call
9833ae66c45SXiao Guangrong      *                    happens on NVDIMM Root Device.
9843ae66c45SXiao Guangrong      * NVDIMM_DSM_REVISION: store the Arg1 of _DSM call.
9853ae66c45SXiao Guangrong      * NVDIMM_DSM_FUNCTION: store the Arg2 of _DSM call.
9863ae66c45SXiao Guangrong      * NVDIMM_DSM_ARG3: store the Arg3 of _DSM call which is a Package
9873ae66c45SXiao Guangrong      *                  containing function-specific arguments.
988c0b3b863SXiao Guangrong      *
989c0b3b863SXiao Guangrong      * They are RAM mapping on host so that these accesses never cause
990c0b3b863SXiao Guangrong      * VM-EXIT.
991c0b3b863SXiao Guangrong      */
9923ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
9933ae66c45SXiao Guangrong                       AML_PRESERVE);
9943ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_HANDLE,
995c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, handle)) * BITS_PER_BYTE));
9963ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_REVISION,
997c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, revision)) * BITS_PER_BYTE));
9983ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_FUNCTION,
999c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, function)) * BITS_PER_BYTE));
10003ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_ARG3,
1001c0b3b863SXiao Guangrong          (sizeof(NvdimmDsmIn) - offsetof(NvdimmDsmIn, arg3)) * BITS_PER_BYTE));
1002c0b3b863SXiao Guangrong     aml_append(method, field);
1003c0b3b863SXiao Guangrong 
1004c0b3b863SXiao Guangrong     /*
1005c0b3b863SXiao Guangrong      * DSM output:
10063ae66c45SXiao Guangrong      * NVDIMM_DSM_OUT_BUF_SIZE: the size of the buffer filled by QEMU.
10073ae66c45SXiao Guangrong      * NVDIMM_DSM_OUT_BUF: the buffer QEMU uses to store the result.
1008c0b3b863SXiao Guangrong      *
1009c0b3b863SXiao Guangrong      * Since the page is reused by both input and out, the input data
1010c0b3b863SXiao Guangrong      * will be lost after storing new result into ODAT so we should fetch
1011c0b3b863SXiao Guangrong      * all the input data before writing the result.
1012c0b3b863SXiao Guangrong      */
10133ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
10143ae66c45SXiao Guangrong                       AML_PRESERVE);
10153ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF_SIZE,
1016c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmOut, len)) * BITS_PER_BYTE));
10173ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF,
1018c0b3b863SXiao Guangrong        (sizeof(NvdimmDsmOut) - offsetof(NvdimmDsmOut, data)) * BITS_PER_BYTE));
1019c0b3b863SXiao Guangrong     aml_append(method, field);
102018c440e1SXiao Guangrong 
102118c440e1SXiao Guangrong     /*
102218c440e1SXiao Guangrong      * do not support any method if DSM memory address has not been
102318c440e1SXiao Guangrong      * patched.
102418c440e1SXiao Guangrong      */
102590623ebfSXiao Guangrong     unpatched = aml_equal(dsm_mem, aml_int(0x0));
102690623ebfSXiao Guangrong 
102790623ebfSXiao Guangrong     expected_uuid = aml_local(0);
102890623ebfSXiao Guangrong 
102990623ebfSXiao Guangrong     ifctx = aml_if(aml_equal(handle, aml_int(0x0)));
103090623ebfSXiao Guangrong     aml_append(ifctx, aml_store(
103190623ebfSXiao Guangrong                aml_touuid("2F10E7A4-9E91-11E4-89D3-123B93F75CBA")
103290623ebfSXiao Guangrong                /* UUID for NVDIMM Root Device */, expected_uuid));
103390623ebfSXiao Guangrong     aml_append(method, ifctx);
103490623ebfSXiao Guangrong     elsectx = aml_else();
1035806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(handle, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT)));
1036806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_touuid(NVDIMM_QEMU_RSVD_UUID
1037806864d9SXiao Guangrong                /* UUID for QEMU internal use */), expected_uuid));
1038806864d9SXiao Guangrong     aml_append(elsectx, ifctx);
1039806864d9SXiao Guangrong     elsectx2 = aml_else();
1040806864d9SXiao Guangrong     aml_append(elsectx2, aml_store(
104190623ebfSXiao Guangrong                aml_touuid("4309AC30-0D11-11E4-9191-0800200C9A66")
104290623ebfSXiao Guangrong                /* UUID for NVDIMM Devices */, expected_uuid));
1043806864d9SXiao Guangrong     aml_append(elsectx, elsectx2);
104490623ebfSXiao Guangrong     aml_append(method, elsectx);
104590623ebfSXiao Guangrong 
104690623ebfSXiao Guangrong     uuid_invalid = aml_lnot(aml_equal(uuid, expected_uuid));
104790623ebfSXiao Guangrong 
104890623ebfSXiao Guangrong     unsupport = aml_if(aml_or(unpatched, uuid_invalid, NULL));
104977286395SXiao Guangrong 
105077286395SXiao Guangrong     /*
105177286395SXiao Guangrong      * function 0 is called to inquire what functions are supported by
105277286395SXiao Guangrong      * OSPM
105377286395SXiao Guangrong      */
105477286395SXiao Guangrong     ifctx = aml_if(aml_equal(function, aml_int(0)));
105577286395SXiao Guangrong     byte_list[0] = 0 /* No function Supported */;
105677286395SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(1, byte_list)));
105790623ebfSXiao Guangrong     aml_append(unsupport, ifctx);
105877286395SXiao Guangrong 
105977286395SXiao Guangrong     /* No function is supported yet. */
1060c2fa3075SXiao Guangrong     byte_list[0] = NVDIMM_DSM_RET_STATUS_UNSUPPORT;
106190623ebfSXiao Guangrong     aml_append(unsupport, aml_return(aml_buffer(1, byte_list)));
106290623ebfSXiao Guangrong     aml_append(method, unsupport);
106377286395SXiao Guangrong 
106418c440e1SXiao Guangrong     /*
106518c440e1SXiao Guangrong      * The HDLE indicates the DSM function is issued from which device,
1066732b530cSXiao Guangrong      * it reserves 0 for root device and is the handle for NVDIMM devices.
1067732b530cSXiao Guangrong      * See the comments in nvdimm_slot_to_handle().
106818c440e1SXiao Guangrong      */
10693ae66c45SXiao Guangrong     aml_append(method, aml_store(handle, aml_name(NVDIMM_DSM_HANDLE)));
10703ae66c45SXiao Guangrong     aml_append(method, aml_store(aml_arg(1), aml_name(NVDIMM_DSM_REVISION)));
1071ac265cacSWei Yang     aml_append(method, aml_store(function, aml_name(NVDIMM_DSM_FUNCTION)));
107218c440e1SXiao Guangrong 
107318c440e1SXiao Guangrong     /*
10744568c948SXiao Guangrong      * The fourth parameter (Arg3) of _DSM is a package which contains
10754568c948SXiao Guangrong      * a buffer, the layout of the buffer is specified by UUID (Arg0),
10764568c948SXiao Guangrong      * Revision ID (Arg1) and Function Index (Arg2) which are documented
10774568c948SXiao Guangrong      * in the DSM Spec.
10784568c948SXiao Guangrong      */
10794568c948SXiao Guangrong     pckg = aml_arg(3);
10804568c948SXiao Guangrong     ifctx = aml_if(aml_and(aml_equal(aml_object_type(pckg),
10814568c948SXiao Guangrong                    aml_int(4 /* Package */)) /* It is a Package? */,
10824568c948SXiao Guangrong                    aml_equal(aml_sizeof(pckg), aml_int(1)) /* 1 element? */,
10834568c948SXiao Guangrong                    NULL));
10844568c948SXiao Guangrong 
10854568c948SXiao Guangrong     pckg_index = aml_local(2);
10864568c948SXiao Guangrong     pckg_buf = aml_local(3);
10874568c948SXiao Guangrong     aml_append(ifctx, aml_store(aml_index(pckg, aml_int(0)), pckg_index));
10884568c948SXiao Guangrong     aml_append(ifctx, aml_store(aml_derefof(pckg_index), pckg_buf));
10893ae66c45SXiao Guangrong     aml_append(ifctx, aml_store(pckg_buf, aml_name(NVDIMM_DSM_ARG3)));
10904568c948SXiao Guangrong     aml_append(method, ifctx);
10914568c948SXiao Guangrong 
10924568c948SXiao Guangrong     /*
109318c440e1SXiao Guangrong      * tell QEMU about the real address of DSM memory, then QEMU
109418c440e1SXiao Guangrong      * gets the control and fills the result in DSM memory.
109518c440e1SXiao Guangrong      */
10963ae66c45SXiao Guangrong     aml_append(method, aml_store(dsm_mem, aml_name(NVDIMM_DSM_NOTIFY)));
109718c440e1SXiao Guangrong 
1098fa1a448dSXiao Guangrong     dsm_out_buf_size = aml_local(1);
1099d51d1d7eSXiao Guangrong     /* RLEN is not included in the payload returned to guest. */
11003ae66c45SXiao Guangrong     aml_append(method, aml_subtract(aml_name(NVDIMM_DSM_OUT_BUF_SIZE),
11013ae66c45SXiao Guangrong                aml_int(4), dsm_out_buf_size));
110271b0269aSShameer Kolothum 
110371b0269aSShameer Kolothum     /*
110471b0269aSShameer Kolothum      * As per ACPI spec 6.3, Table 19-419 Object Conversion Rules, if
110571b0269aSShameer Kolothum      * the Buffer Field <= to the size of an Integer (in bits), it will
110671b0269aSShameer Kolothum      * be treated as an integer. Moreover, the integer size depends on
110771b0269aSShameer Kolothum      * DSDT tables revision number. If revision number is < 2, integer
110871b0269aSShameer Kolothum      * size is 32 bits, otherwise it is 64 bits.
110971b0269aSShameer Kolothum      * Because of this CreateField() canot be used if RLEN < Integer Size.
111071b0269aSShameer Kolothum      *
111171b0269aSShameer Kolothum      * Also please note that APCI ASL operator SizeOf() doesn't support
111271b0269aSShameer Kolothum      * Integer and there isn't any other way to figure out the Integer
111371b0269aSShameer Kolothum      * size. Hence we assume 8 byte as Integer size and if RLEN < 8 bytes,
111471b0269aSShameer Kolothum      * build dsm_out_buf byte by byte.
111571b0269aSShameer Kolothum      */
111671b0269aSShameer Kolothum     ifctx = aml_if(aml_lless(dsm_out_buf_size, aml_int(8)));
111771b0269aSShameer Kolothum     offset = aml_local(2);
111871b0269aSShameer Kolothum     aml_append(ifctx, aml_store(aml_int(0), offset));
111971b0269aSShameer Kolothum     aml_append(ifctx, aml_name_decl("TBUF", aml_buffer(1, NULL)));
112071b0269aSShameer Kolothum     aml_append(ifctx, aml_store(aml_buffer(0, NULL), dsm_out_buf));
112171b0269aSShameer Kolothum 
112271b0269aSShameer Kolothum     whilectx = aml_while(aml_lless(offset, dsm_out_buf_size));
112371b0269aSShameer Kolothum     /* Copy 1 byte at offset from ODAT to temporary buffer(TBUF). */
112471b0269aSShameer Kolothum     aml_append(whilectx, aml_store(aml_derefof(aml_index(
112571b0269aSShameer Kolothum                                    aml_name(NVDIMM_DSM_OUT_BUF), offset)),
112671b0269aSShameer Kolothum                                    aml_index(aml_name("TBUF"), aml_int(0))));
112771b0269aSShameer Kolothum     aml_append(whilectx, aml_concatenate(dsm_out_buf, aml_name("TBUF"),
112871b0269aSShameer Kolothum                                          dsm_out_buf));
112971b0269aSShameer Kolothum     aml_append(whilectx, aml_increment(offset));
113071b0269aSShameer Kolothum     aml_append(ifctx, whilectx);
113171b0269aSShameer Kolothum 
113271b0269aSShameer Kolothum     aml_append(ifctx, aml_return(dsm_out_buf));
113371b0269aSShameer Kolothum     aml_append(method, ifctx);
113471b0269aSShameer Kolothum 
113571b0269aSShameer Kolothum     /* If RLEN >= Integer size, just use CreateField() operator */
1136fa1a448dSXiao Guangrong     aml_append(method, aml_store(aml_shiftleft(dsm_out_buf_size, aml_int(3)),
1137fa1a448dSXiao Guangrong                                  dsm_out_buf_size));
11383ae66c45SXiao Guangrong     aml_append(method, aml_create_field(aml_name(NVDIMM_DSM_OUT_BUF),
11393ae66c45SXiao Guangrong                aml_int(0), dsm_out_buf_size, "OBUF"));
114071b0269aSShameer Kolothum     aml_append(method, aml_return(aml_name("OBUF")));
114171b0269aSShameer Kolothum 
114277286395SXiao Guangrong     aml_append(dev, method);
114377286395SXiao Guangrong }
114477286395SXiao Guangrong 
1145732b530cSXiao Guangrong static void nvdimm_build_device_dsm(Aml *dev, uint32_t handle)
114677286395SXiao Guangrong {
114777286395SXiao Guangrong     Aml *method;
114877286395SXiao Guangrong 
114977286395SXiao Guangrong     method = aml_method("_DSM", 4, AML_NOTSERIALIZED);
1150732b530cSXiao Guangrong     aml_append(method, aml_return(aml_call5(NVDIMM_COMMON_DSM, aml_arg(0),
1151732b530cSXiao Guangrong                                   aml_arg(1), aml_arg(2), aml_arg(3),
1152732b530cSXiao Guangrong                                   aml_int(handle))));
115377286395SXiao Guangrong     aml_append(dev, method);
115477286395SXiao Guangrong }
115577286395SXiao Guangrong 
1156806864d9SXiao Guangrong static void nvdimm_build_fit(Aml *dev)
1157806864d9SXiao Guangrong {
1158806864d9SXiao Guangrong     Aml *method, *pkg, *buf, *buf_size, *offset, *call_result;
1159806864d9SXiao Guangrong     Aml *whilectx, *ifcond, *ifctx, *elsectx, *fit;
1160806864d9SXiao Guangrong 
1161806864d9SXiao Guangrong     buf = aml_local(0);
1162806864d9SXiao Guangrong     buf_size = aml_local(1);
1163806864d9SXiao Guangrong     fit = aml_local(2);
1164806864d9SXiao Guangrong 
1165aef056c1SXiao Guangrong     aml_append(dev, aml_name_decl(NVDIMM_DSM_RFIT_STATUS, aml_int(0)));
1166806864d9SXiao Guangrong 
1167806864d9SXiao Guangrong     /* build helper function, RFIT. */
1168806864d9SXiao Guangrong     method = aml_method("RFIT", 1, AML_SERIALIZED);
1169aef056c1SXiao Guangrong     aml_append(method, aml_name_decl("OFST", aml_int(0)));
1170806864d9SXiao Guangrong 
1171806864d9SXiao Guangrong     /* prepare input package. */
1172806864d9SXiao Guangrong     pkg = aml_package(1);
1173806864d9SXiao Guangrong     aml_append(method, aml_store(aml_arg(0), aml_name("OFST")));
1174806864d9SXiao Guangrong     aml_append(pkg, aml_name("OFST"));
1175806864d9SXiao Guangrong 
1176806864d9SXiao Guangrong     /* call Read_FIT function. */
1177806864d9SXiao Guangrong     call_result = aml_call5(NVDIMM_COMMON_DSM,
1178806864d9SXiao Guangrong                             aml_touuid(NVDIMM_QEMU_RSVD_UUID),
1179806864d9SXiao Guangrong                             aml_int(1) /* Revision 1 */,
1180806864d9SXiao Guangrong                             aml_int(0x1) /* Read FIT */,
1181806864d9SXiao Guangrong                             pkg, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT));
1182806864d9SXiao Guangrong     aml_append(method, aml_store(call_result, buf));
1183806864d9SXiao Guangrong 
1184806864d9SXiao Guangrong     /* handle _DSM result. */
1185806864d9SXiao Guangrong     aml_append(method, aml_create_dword_field(buf,
1186806864d9SXiao Guangrong                aml_int(0) /* offset at byte 0 */, "STAU"));
1187806864d9SXiao Guangrong 
1188806864d9SXiao Guangrong     aml_append(method, aml_store(aml_name("STAU"),
1189806864d9SXiao Guangrong                                  aml_name(NVDIMM_DSM_RFIT_STATUS)));
1190806864d9SXiao Guangrong 
1191806864d9SXiao Guangrong      /* if something is wrong during _DSM. */
1192c2fa3075SXiao Guangrong     ifcond = aml_equal(aml_int(NVDIMM_DSM_RET_STATUS_SUCCESS),
1193c2fa3075SXiao Guangrong                        aml_name("STAU"));
1194806864d9SXiao Guangrong     ifctx = aml_if(aml_lnot(ifcond));
1195806864d9SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1196806864d9SXiao Guangrong     aml_append(method, ifctx);
1197806864d9SXiao Guangrong 
1198806864d9SXiao Guangrong     aml_append(method, aml_store(aml_sizeof(buf), buf_size));
1199806864d9SXiao Guangrong     aml_append(method, aml_subtract(buf_size,
1200806864d9SXiao Guangrong                                     aml_int(4) /* the size of "STAU" */,
1201806864d9SXiao Guangrong                                     buf_size));
1202806864d9SXiao Guangrong 
1203806864d9SXiao Guangrong     /* if we read the end of fit. */
1204806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1205806864d9SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1206806864d9SXiao Guangrong     aml_append(method, ifctx);
1207806864d9SXiao Guangrong 
1208806864d9SXiao Guangrong     aml_append(method, aml_create_field(buf,
1209806864d9SXiao Guangrong                             aml_int(4 * BITS_PER_BYTE), /* offset at byte 4.*/
1210880f3612SXiao Guangrong                             aml_shiftleft(buf_size, aml_int(3)), "BUFF"));
1211806864d9SXiao Guangrong     aml_append(method, aml_return(aml_name("BUFF")));
1212806864d9SXiao Guangrong     aml_append(dev, method);
1213806864d9SXiao Guangrong 
1214806864d9SXiao Guangrong     /* build _FIT. */
1215806864d9SXiao Guangrong     method = aml_method("_FIT", 0, AML_SERIALIZED);
1216806864d9SXiao Guangrong     offset = aml_local(3);
1217806864d9SXiao Guangrong 
1218806864d9SXiao Guangrong     aml_append(method, aml_store(aml_buffer(0, NULL), fit));
1219806864d9SXiao Guangrong     aml_append(method, aml_store(aml_int(0), offset));
1220806864d9SXiao Guangrong 
1221806864d9SXiao Guangrong     whilectx = aml_while(aml_int(1));
1222806864d9SXiao Guangrong     aml_append(whilectx, aml_store(aml_call1("RFIT", offset), buf));
1223806864d9SXiao Guangrong     aml_append(whilectx, aml_store(aml_sizeof(buf), buf_size));
1224806864d9SXiao Guangrong 
1225806864d9SXiao Guangrong     /*
1226806864d9SXiao Guangrong      * if fit buffer was changed during RFIT, read from the beginning
1227806864d9SXiao Guangrong      * again.
1228806864d9SXiao Guangrong      */
1229806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(aml_name(NVDIMM_DSM_RFIT_STATUS),
1230c2fa3075SXiao Guangrong                              aml_int(NVDIMM_DSM_RET_STATUS_FIT_CHANGED)));
1231806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_buffer(0, NULL), fit));
1232806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_int(0), offset));
1233806864d9SXiao Guangrong     aml_append(whilectx, ifctx);
1234806864d9SXiao Guangrong 
1235806864d9SXiao Guangrong     elsectx = aml_else();
1236806864d9SXiao Guangrong 
1237806864d9SXiao Guangrong     /* finish fit read if no data is read out. */
1238806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1239806864d9SXiao Guangrong     aml_append(ifctx, aml_return(fit));
1240806864d9SXiao Guangrong     aml_append(elsectx, ifctx);
1241806864d9SXiao Guangrong 
1242806864d9SXiao Guangrong     /* update the offset. */
1243806864d9SXiao Guangrong     aml_append(elsectx, aml_add(offset, buf_size, offset));
1244806864d9SXiao Guangrong     /* append the data we read out to the fit buffer. */
1245806864d9SXiao Guangrong     aml_append(elsectx, aml_concatenate(fit, buf, fit));
1246806864d9SXiao Guangrong     aml_append(whilectx, elsectx);
1247806864d9SXiao Guangrong     aml_append(method, whilectx);
1248806864d9SXiao Guangrong 
1249806864d9SXiao Guangrong     aml_append(dev, method);
1250806864d9SXiao Guangrong }
1251806864d9SXiao Guangrong 
1252bdfd065bSXiao Guangrong static void nvdimm_build_nvdimm_devices(Aml *root_dev, uint32_t ram_slots)
125377286395SXiao Guangrong {
1254bdfd065bSXiao Guangrong     uint32_t slot;
1255bdfd065bSXiao Guangrong 
1256bdfd065bSXiao Guangrong     for (slot = 0; slot < ram_slots; slot++) {
125777286395SXiao Guangrong         uint32_t handle = nvdimm_slot_to_handle(slot);
125877286395SXiao Guangrong         Aml *nvdimm_dev;
125977286395SXiao Guangrong 
126077286395SXiao Guangrong         nvdimm_dev = aml_device("NV%02X", slot);
126177286395SXiao Guangrong 
126277286395SXiao Guangrong         /*
126377286395SXiao Guangrong          * ACPI 6.0: 9.20 NVDIMM Devices:
126477286395SXiao Guangrong          *
126577286395SXiao Guangrong          * _ADR object that is used to supply OSPM with unique address
126677286395SXiao Guangrong          * of the NVDIMM device. This is done by returning the NFIT Device
126777286395SXiao Guangrong          * handle that is used to identify the associated entries in ACPI
126877286395SXiao Guangrong          * table NFIT or _FIT.
126977286395SXiao Guangrong          */
127077286395SXiao Guangrong         aml_append(nvdimm_dev, aml_name_decl("_ADR", aml_int(handle)));
127177286395SXiao Guangrong 
1272732b530cSXiao Guangrong         nvdimm_build_device_dsm(nvdimm_dev, handle);
127377286395SXiao Guangrong         aml_append(root_dev, nvdimm_dev);
127477286395SXiao Guangrong     }
127577286395SXiao Guangrong }
127677286395SXiao Guangrong 
1277bdfd065bSXiao Guangrong static void nvdimm_build_ssdt(GArray *table_offsets, GArray *table_data,
12785c94b826SKwangwoo Lee                               BIOSLinker *linker,
12795c94b826SKwangwoo Lee                               NVDIMMState *nvdimm_state,
1280bdfd065bSXiao Guangrong                               uint32_t ram_slots)
128177286395SXiao Guangrong {
1282c0b3b863SXiao Guangrong     Aml *ssdt, *sb_scope, *dev;
1283b9951413SXiao Guangrong     int mem_addr_offset, nvdimm_ssdt;
128477286395SXiao Guangrong 
128577286395SXiao Guangrong     acpi_add_table(table_offsets, table_data);
128677286395SXiao Guangrong 
128777286395SXiao Guangrong     ssdt = init_aml_allocator();
128877286395SXiao Guangrong     acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
128977286395SXiao Guangrong 
129077286395SXiao Guangrong     sb_scope = aml_scope("\\_SB");
129177286395SXiao Guangrong 
129277286395SXiao Guangrong     dev = aml_device("NVDR");
129377286395SXiao Guangrong 
129477286395SXiao Guangrong     /*
129577286395SXiao Guangrong      * ACPI 6.0: 9.20 NVDIMM Devices:
129677286395SXiao Guangrong      *
129777286395SXiao Guangrong      * The ACPI Name Space device uses _HID of ACPI0012 to identify the root
129877286395SXiao Guangrong      * NVDIMM interface device. Platform firmware is required to contain one
129977286395SXiao Guangrong      * such device in _SB scope if NVDIMMs support is exposed by platform to
130077286395SXiao Guangrong      * OSPM.
130177286395SXiao Guangrong      * For each NVDIMM present or intended to be supported by platform,
130277286395SXiao Guangrong      * platform firmware also exposes an ACPI Namespace Device under the
130377286395SXiao Guangrong      * root device.
130477286395SXiao Guangrong      */
130577286395SXiao Guangrong     aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0012")));
130677286395SXiao Guangrong 
13075c94b826SKwangwoo Lee     nvdimm_build_common_dsm(dev, nvdimm_state);
1308732b530cSXiao Guangrong 
1309732b530cSXiao Guangrong     /* 0 is reserved for root device. */
1310732b530cSXiao Guangrong     nvdimm_build_device_dsm(dev, 0);
1311806864d9SXiao Guangrong     nvdimm_build_fit(dev);
131277286395SXiao Guangrong 
1313bdfd065bSXiao Guangrong     nvdimm_build_nvdimm_devices(dev, ram_slots);
131477286395SXiao Guangrong 
131577286395SXiao Guangrong     aml_append(sb_scope, dev);
131677286395SXiao Guangrong     aml_append(ssdt, sb_scope);
1317b9951413SXiao Guangrong 
1318b9951413SXiao Guangrong     nvdimm_ssdt = table_data->len;
1319b9951413SXiao Guangrong 
132077286395SXiao Guangrong     /* copy AML table into ACPI tables blob and patch header there */
132177286395SXiao Guangrong     g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
1322b9951413SXiao Guangrong     mem_addr_offset = build_append_named_dword(table_data,
1323b9951413SXiao Guangrong                                                NVDIMM_ACPI_MEM_ADDR);
1324b9951413SXiao Guangrong 
1325ad9671b8SIgor Mammedov     bios_linker_loader_alloc(linker,
13265c94b826SKwangwoo Lee                              NVDIMM_DSM_MEM_FILE, nvdimm_state->dsm_mem,
1327ad9671b8SIgor Mammedov                              sizeof(NvdimmDsmIn), false /* high memory */);
13284678124bSIgor Mammedov     bios_linker_loader_add_pointer(linker,
13294678124bSIgor Mammedov         ACPI_BUILD_TABLE_FILE, mem_addr_offset, sizeof(uint32_t),
13304678124bSIgor Mammedov         NVDIMM_DSM_MEM_FILE, 0);
133177286395SXiao Guangrong     build_header(linker, table_data,
1332b9951413SXiao Guangrong         (void *)(table_data->data + nvdimm_ssdt),
1333b9951413SXiao Guangrong         "SSDT", table_data->len - nvdimm_ssdt, 1, NULL, "NVDIMM");
133477286395SXiao Guangrong     free_aml_allocator();
133577286395SXiao Guangrong }
133677286395SXiao Guangrong 
133787252e1bSXiao Guangrong void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
1338c1404bdeSEric Auger                        BIOSLinker *linker, NVDIMMState *state,
1339bdfd065bSXiao Guangrong                        uint32_t ram_slots)
134087252e1bSXiao Guangrong {
1341264813cbSXiao Guangrong     GSList *device_list;
1342bdfd065bSXiao Guangrong 
1343264813cbSXiao Guangrong     /* no nvdimm device can be plugged. */
1344264813cbSXiao Guangrong     if (!ram_slots) {
1345264813cbSXiao Guangrong         return;
1346264813cbSXiao Guangrong     }
1347264813cbSXiao Guangrong 
13485c94b826SKwangwoo Lee     nvdimm_build_ssdt(table_offsets, table_data, linker, state,
1349bdfd065bSXiao Guangrong                       ram_slots);
1350264813cbSXiao Guangrong 
1351cf7c0ff5SXiao Guangrong     device_list = nvdimm_get_device_list();
1352264813cbSXiao Guangrong     /* no NVDIMM device is plugged. */
1353264813cbSXiao Guangrong     if (!device_list) {
1354264813cbSXiao Guangrong         return;
1355bdfd065bSXiao Guangrong     }
1356264813cbSXiao Guangrong 
1357264813cbSXiao Guangrong     nvdimm_build_nfit(state, table_offsets, table_data, linker);
1358264813cbSXiao Guangrong     g_slist_free(device_list);
1359bdfd065bSXiao Guangrong }
1360