xref: /qemu/hw/acpi/nvdimm.c (revision 602b4582)
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
18f1e5e2eeSChetan Pant  * version 2.1 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"
301439f213SDongjiu Geng #include "qemu/uuid.h"
31c3b0cf6eSVishal Verma #include "qapi/error.h"
3287252e1bSXiao Guangrong #include "hw/acpi/acpi.h"
3387252e1bSXiao Guangrong #include "hw/acpi/aml-build.h"
34b9951413SXiao Guangrong #include "hw/acpi/bios-linker-loader.h"
355fe79386SXiao Guangrong #include "hw/nvram/fw_cfg.h"
3687252e1bSXiao Guangrong #include "hw/mem/nvdimm.h"
373f350f6bSShivaprasad G Bhat #include "qemu/nvdimm-utils.h"
3887252e1bSXiao Guangrong 
3987252e1bSXiao Guangrong /*
4087252e1bSXiao Guangrong  * define Byte Addressable Persistent Memory (PM) Region according to
4187252e1bSXiao Guangrong  * ACPI 6.0: 5.2.25.1 System Physical Address Range Structure.
4287252e1bSXiao Guangrong  */
4387252e1bSXiao Guangrong static const uint8_t nvdimm_nfit_spa_uuid[] =
441439f213SDongjiu Geng       UUID_LE(0x66f0d379, 0xb4f3, 0x4074, 0xac, 0x43, 0x0d, 0x33,
4587252e1bSXiao Guangrong               0x18, 0xb7, 0x8c, 0xdb);
4687252e1bSXiao Guangrong 
4787252e1bSXiao Guangrong /*
4887252e1bSXiao Guangrong  * NVDIMM Firmware Interface Table
4987252e1bSXiao Guangrong  * @signature: "NFIT"
5087252e1bSXiao Guangrong  *
5187252e1bSXiao Guangrong  * It provides information that allows OSPM to enumerate NVDIMM present in
5287252e1bSXiao Guangrong  * the platform and associate system physical address ranges created by the
5387252e1bSXiao Guangrong  * NVDIMMs.
5487252e1bSXiao Guangrong  *
5587252e1bSXiao Guangrong  * It is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
5687252e1bSXiao Guangrong  */
5787252e1bSXiao Guangrong struct NvdimmNfitHeader {
5887252e1bSXiao Guangrong     ACPI_TABLE_HEADER_DEF
5987252e1bSXiao Guangrong     uint32_t reserved;
6087252e1bSXiao Guangrong } QEMU_PACKED;
6187252e1bSXiao Guangrong typedef struct NvdimmNfitHeader NvdimmNfitHeader;
6287252e1bSXiao Guangrong 
6387252e1bSXiao Guangrong /*
6487252e1bSXiao Guangrong  * define NFIT structures according to ACPI 6.0: 5.2.25 NVDIMM Firmware
6587252e1bSXiao Guangrong  * Interface Table (NFIT).
6687252e1bSXiao Guangrong  */
6787252e1bSXiao Guangrong 
6887252e1bSXiao Guangrong /*
6987252e1bSXiao Guangrong  * System Physical Address Range Structure
7087252e1bSXiao Guangrong  *
7187252e1bSXiao Guangrong  * It describes the system physical address ranges occupied by NVDIMMs and
7287252e1bSXiao Guangrong  * the types of the regions.
7387252e1bSXiao Guangrong  */
7487252e1bSXiao Guangrong struct NvdimmNfitSpa {
7587252e1bSXiao Guangrong     uint16_t type;
7687252e1bSXiao Guangrong     uint16_t length;
7787252e1bSXiao Guangrong     uint16_t spa_index;
7887252e1bSXiao Guangrong     uint16_t flags;
7987252e1bSXiao Guangrong     uint32_t reserved;
8087252e1bSXiao Guangrong     uint32_t proximity_domain;
8187252e1bSXiao Guangrong     uint8_t type_guid[16];
8287252e1bSXiao Guangrong     uint64_t spa_base;
8387252e1bSXiao Guangrong     uint64_t spa_length;
8487252e1bSXiao Guangrong     uint64_t mem_attr;
8587252e1bSXiao Guangrong } QEMU_PACKED;
8687252e1bSXiao Guangrong typedef struct NvdimmNfitSpa NvdimmNfitSpa;
8787252e1bSXiao Guangrong 
8887252e1bSXiao Guangrong /*
8987252e1bSXiao Guangrong  * Memory Device to System Physical Address Range Mapping Structure
9087252e1bSXiao Guangrong  *
9187252e1bSXiao Guangrong  * It enables identifying each NVDIMM region and the corresponding SPA
9287252e1bSXiao Guangrong  * describing the memory interleave
9387252e1bSXiao Guangrong  */
9487252e1bSXiao Guangrong struct NvdimmNfitMemDev {
9587252e1bSXiao Guangrong     uint16_t type;
9687252e1bSXiao Guangrong     uint16_t length;
9787252e1bSXiao Guangrong     uint32_t nfit_handle;
9887252e1bSXiao Guangrong     uint16_t phys_id;
9987252e1bSXiao Guangrong     uint16_t region_id;
10087252e1bSXiao Guangrong     uint16_t spa_index;
10187252e1bSXiao Guangrong     uint16_t dcr_index;
10287252e1bSXiao Guangrong     uint64_t region_len;
10387252e1bSXiao Guangrong     uint64_t region_offset;
10487252e1bSXiao Guangrong     uint64_t region_dpa;
10587252e1bSXiao Guangrong     uint16_t interleave_index;
10687252e1bSXiao Guangrong     uint16_t interleave_ways;
10787252e1bSXiao Guangrong     uint16_t flags;
10887252e1bSXiao Guangrong     uint16_t reserved;
10987252e1bSXiao Guangrong } QEMU_PACKED;
11087252e1bSXiao Guangrong typedef struct NvdimmNfitMemDev NvdimmNfitMemDev;
11187252e1bSXiao Guangrong 
112cb836434SHaozhong Zhang #define ACPI_NFIT_MEM_NOT_ARMED     (1 << 3)
113cb836434SHaozhong Zhang 
11487252e1bSXiao Guangrong /*
11587252e1bSXiao Guangrong  * NVDIMM Control Region Structure
11687252e1bSXiao Guangrong  *
11787252e1bSXiao Guangrong  * It describes the NVDIMM and if applicable, Block Control Window.
11887252e1bSXiao Guangrong  */
11987252e1bSXiao Guangrong struct NvdimmNfitControlRegion {
12087252e1bSXiao Guangrong     uint16_t type;
12187252e1bSXiao Guangrong     uint16_t length;
12287252e1bSXiao Guangrong     uint16_t dcr_index;
12387252e1bSXiao Guangrong     uint16_t vendor_id;
12487252e1bSXiao Guangrong     uint16_t device_id;
12587252e1bSXiao Guangrong     uint16_t revision_id;
12687252e1bSXiao Guangrong     uint16_t sub_vendor_id;
12787252e1bSXiao Guangrong     uint16_t sub_device_id;
12887252e1bSXiao Guangrong     uint16_t sub_revision_id;
12987252e1bSXiao Guangrong     uint8_t reserved[6];
13087252e1bSXiao Guangrong     uint32_t serial_number;
13187252e1bSXiao Guangrong     uint16_t fic;
13287252e1bSXiao Guangrong     uint16_t num_bcw;
13387252e1bSXiao Guangrong     uint64_t bcw_size;
13487252e1bSXiao Guangrong     uint64_t cmd_offset;
13587252e1bSXiao Guangrong     uint64_t cmd_size;
13687252e1bSXiao Guangrong     uint64_t status_offset;
13787252e1bSXiao Guangrong     uint64_t status_size;
13887252e1bSXiao Guangrong     uint16_t flags;
13987252e1bSXiao Guangrong     uint8_t reserved2[6];
14087252e1bSXiao Guangrong } QEMU_PACKED;
14187252e1bSXiao Guangrong typedef struct NvdimmNfitControlRegion NvdimmNfitControlRegion;
14287252e1bSXiao Guangrong 
14387252e1bSXiao Guangrong /*
1449ab3aad2SRoss Zwisler  * NVDIMM Platform Capabilities Structure
1459ab3aad2SRoss Zwisler  *
1469ab3aad2SRoss Zwisler  * Defined in section 5.2.25.9 of ACPI 6.2 Errata A, September 2017
1479ab3aad2SRoss Zwisler  */
1489ab3aad2SRoss Zwisler struct NvdimmNfitPlatformCaps {
1499ab3aad2SRoss Zwisler     uint16_t type;
1509ab3aad2SRoss Zwisler     uint16_t length;
1519ab3aad2SRoss Zwisler     uint8_t highest_cap;
1529ab3aad2SRoss Zwisler     uint8_t reserved[3];
1539ab3aad2SRoss Zwisler     uint32_t capabilities;
1549ab3aad2SRoss Zwisler     uint8_t reserved2[4];
1559ab3aad2SRoss Zwisler } QEMU_PACKED;
1569ab3aad2SRoss Zwisler typedef struct NvdimmNfitPlatformCaps NvdimmNfitPlatformCaps;
1579ab3aad2SRoss Zwisler 
1589ab3aad2SRoss Zwisler /*
15987252e1bSXiao Guangrong  * Module serial number is a unique number for each device. We use the
16087252e1bSXiao Guangrong  * slot id of NVDIMM device to generate this number so that each device
16187252e1bSXiao Guangrong  * associates with a different number.
16287252e1bSXiao Guangrong  *
16387252e1bSXiao Guangrong  * 0x123456 is a magic number we arbitrarily chose.
16487252e1bSXiao Guangrong  */
16587252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_sn(int slot)
16687252e1bSXiao Guangrong {
16787252e1bSXiao Guangrong     return 0x123456 + slot;
16887252e1bSXiao Guangrong }
16987252e1bSXiao Guangrong 
17087252e1bSXiao Guangrong /*
17187252e1bSXiao Guangrong  * handle is used to uniquely associate nfit_memdev structure with NVDIMM
17287252e1bSXiao Guangrong  * ACPI device - nfit_memdev.nfit_handle matches with the value returned
17387252e1bSXiao Guangrong  * by ACPI device _ADR method.
17487252e1bSXiao Guangrong  *
17587252e1bSXiao Guangrong  * We generate the handle with the slot id of NVDIMM device and reserve
17687252e1bSXiao Guangrong  * 0 for NVDIMM root device.
17787252e1bSXiao Guangrong  */
17887252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_handle(int slot)
17987252e1bSXiao Guangrong {
18087252e1bSXiao Guangrong     return slot + 1;
18187252e1bSXiao Guangrong }
18287252e1bSXiao Guangrong 
18387252e1bSXiao Guangrong /*
18487252e1bSXiao Guangrong  * index uniquely identifies the structure, 0 is reserved which indicates
18587252e1bSXiao Guangrong  * that the structure is not valid or the associated structure is not
18687252e1bSXiao Guangrong  * present.
18787252e1bSXiao Guangrong  *
18887252e1bSXiao Guangrong  * Each NVDIMM device needs two indexes, one for nfit_spa and another for
18987252e1bSXiao Guangrong  * nfit_dc which are generated by the slot id of NVDIMM device.
19087252e1bSXiao Guangrong  */
19187252e1bSXiao Guangrong static uint16_t nvdimm_slot_to_spa_index(int slot)
19287252e1bSXiao Guangrong {
19387252e1bSXiao Guangrong     return (slot + 1) << 1;
19487252e1bSXiao Guangrong }
19587252e1bSXiao Guangrong 
19687252e1bSXiao Guangrong /* See the comments of nvdimm_slot_to_spa_index(). */
19787252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_dcr_index(int slot)
19887252e1bSXiao Guangrong {
19987252e1bSXiao Guangrong     return nvdimm_slot_to_spa_index(slot) + 1;
20087252e1bSXiao Guangrong }
20187252e1bSXiao Guangrong 
2025797dcdcSXiao Guangrong static NVDIMMDevice *nvdimm_get_device_by_handle(uint32_t handle)
2035797dcdcSXiao Guangrong {
2045797dcdcSXiao Guangrong     NVDIMMDevice *nvdimm = NULL;
205cf7c0ff5SXiao Guangrong     GSList *list, *device_list = nvdimm_get_device_list();
2065797dcdcSXiao Guangrong 
2075797dcdcSXiao Guangrong     for (list = device_list; list; list = list->next) {
2085797dcdcSXiao Guangrong         NVDIMMDevice *nvd = list->data;
2095797dcdcSXiao Guangrong         int slot = object_property_get_int(OBJECT(nvd), PC_DIMM_SLOT_PROP,
2105797dcdcSXiao Guangrong                                            NULL);
2115797dcdcSXiao Guangrong 
2125797dcdcSXiao Guangrong         if (nvdimm_slot_to_handle(slot) == handle) {
2135797dcdcSXiao Guangrong             nvdimm = nvd;
2145797dcdcSXiao Guangrong             break;
2155797dcdcSXiao Guangrong         }
2165797dcdcSXiao Guangrong     }
2175797dcdcSXiao Guangrong 
2185797dcdcSXiao Guangrong     g_slist_free(device_list);
2195797dcdcSXiao Guangrong     return nvdimm;
2205797dcdcSXiao Guangrong }
2215797dcdcSXiao Guangrong 
22287252e1bSXiao Guangrong /* ACPI 6.0: 5.2.25.1 System Physical Address Range Structure */
22387252e1bSXiao Guangrong static void
22487252e1bSXiao Guangrong nvdimm_build_structure_spa(GArray *structures, DeviceState *dev)
22587252e1bSXiao Guangrong {
22687252e1bSXiao Guangrong     NvdimmNfitSpa *nfit_spa;
2279ed442b8SMarc-André Lureau     uint64_t addr = object_property_get_uint(OBJECT(dev), PC_DIMM_ADDR_PROP,
22887252e1bSXiao Guangrong                                              NULL);
229b053ef61SMarc-André Lureau     uint64_t size = object_property_get_uint(OBJECT(dev), PC_DIMM_SIZE_PROP,
23087252e1bSXiao Guangrong                                              NULL);
2319ed442b8SMarc-André Lureau     uint32_t node = object_property_get_uint(OBJECT(dev), PC_DIMM_NODE_PROP,
23287252e1bSXiao Guangrong                                              NULL);
23387252e1bSXiao Guangrong     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
23487252e1bSXiao Guangrong                                        NULL);
23587252e1bSXiao Guangrong 
23687252e1bSXiao Guangrong     nfit_spa = acpi_data_push(structures, sizeof(*nfit_spa));
23787252e1bSXiao Guangrong 
23887252e1bSXiao Guangrong     nfit_spa->type = cpu_to_le16(0 /* System Physical Address Range
23987252e1bSXiao Guangrong                                       Structure */);
24087252e1bSXiao Guangrong     nfit_spa->length = cpu_to_le16(sizeof(*nfit_spa));
24187252e1bSXiao Guangrong     nfit_spa->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
24287252e1bSXiao Guangrong 
24387252e1bSXiao Guangrong     /*
24487252e1bSXiao Guangrong      * Control region is strict as all the device info, such as SN, index,
24587252e1bSXiao Guangrong      * is associated with slot id.
24687252e1bSXiao Guangrong      */
24787252e1bSXiao Guangrong     nfit_spa->flags = cpu_to_le16(1 /* Control region is strictly for
24887252e1bSXiao Guangrong                                        management during hot add/online
24987252e1bSXiao Guangrong                                        operation */ |
25087252e1bSXiao Guangrong                                   2 /* Data in Proximity Domain field is
25187252e1bSXiao Guangrong                                        valid*/);
25287252e1bSXiao Guangrong 
25387252e1bSXiao Guangrong     /* NUMA node. */
25487252e1bSXiao Guangrong     nfit_spa->proximity_domain = cpu_to_le32(node);
25587252e1bSXiao Guangrong     /* the region reported as PMEM. */
25687252e1bSXiao Guangrong     memcpy(nfit_spa->type_guid, nvdimm_nfit_spa_uuid,
25787252e1bSXiao Guangrong            sizeof(nvdimm_nfit_spa_uuid));
25887252e1bSXiao Guangrong 
25987252e1bSXiao Guangrong     nfit_spa->spa_base = cpu_to_le64(addr);
26087252e1bSXiao Guangrong     nfit_spa->spa_length = cpu_to_le64(size);
26187252e1bSXiao Guangrong 
26287252e1bSXiao Guangrong     /* It is the PMEM and can be cached as writeback. */
26387252e1bSXiao Guangrong     nfit_spa->mem_attr = cpu_to_le64(0x8ULL /* EFI_MEMORY_WB */ |
26487252e1bSXiao Guangrong                                      0x8000ULL /* EFI_MEMORY_NV */);
26587252e1bSXiao Guangrong }
26687252e1bSXiao Guangrong 
26787252e1bSXiao Guangrong /*
26887252e1bSXiao Guangrong  * ACPI 6.0: 5.2.25.2 Memory Device to System Physical Address Range Mapping
26987252e1bSXiao Guangrong  * Structure
27087252e1bSXiao Guangrong  */
27187252e1bSXiao Guangrong static void
27287252e1bSXiao Guangrong nvdimm_build_structure_memdev(GArray *structures, DeviceState *dev)
27387252e1bSXiao Guangrong {
27487252e1bSXiao Guangrong     NvdimmNfitMemDev *nfit_memdev;
275cb836434SHaozhong Zhang     NVDIMMDevice *nvdimm = NVDIMM(OBJECT(dev));
276b053ef61SMarc-André Lureau     uint64_t size = object_property_get_uint(OBJECT(dev), PC_DIMM_SIZE_PROP,
27787252e1bSXiao Guangrong                                              NULL);
27887252e1bSXiao Guangrong     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
27987252e1bSXiao Guangrong                                             NULL);
28087252e1bSXiao Guangrong     uint32_t handle = nvdimm_slot_to_handle(slot);
28187252e1bSXiao Guangrong 
28287252e1bSXiao Guangrong     nfit_memdev = acpi_data_push(structures, sizeof(*nfit_memdev));
28387252e1bSXiao Guangrong 
28487252e1bSXiao Guangrong     nfit_memdev->type = cpu_to_le16(1 /* Memory Device to System Address
28587252e1bSXiao Guangrong                                          Range Map Structure*/);
28687252e1bSXiao Guangrong     nfit_memdev->length = cpu_to_le16(sizeof(*nfit_memdev));
28787252e1bSXiao Guangrong     nfit_memdev->nfit_handle = cpu_to_le32(handle);
28887252e1bSXiao Guangrong 
28987252e1bSXiao Guangrong     /*
29087252e1bSXiao Guangrong      * associate memory device with System Physical Address Range
29187252e1bSXiao Guangrong      * Structure.
29287252e1bSXiao Guangrong      */
29387252e1bSXiao Guangrong     nfit_memdev->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
29487252e1bSXiao Guangrong     /* associate memory device with Control Region Structure. */
29587252e1bSXiao Guangrong     nfit_memdev->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
29687252e1bSXiao Guangrong 
29787252e1bSXiao Guangrong     /* The memory region on the device. */
29887252e1bSXiao Guangrong     nfit_memdev->region_len = cpu_to_le64(size);
2996ab0c4bdSXiao Guangrong     /* The device address starts from 0. */
3006ab0c4bdSXiao Guangrong     nfit_memdev->region_dpa = cpu_to_le64(0);
30187252e1bSXiao Guangrong 
30287252e1bSXiao Guangrong     /* Only one interleave for PMEM. */
30387252e1bSXiao Guangrong     nfit_memdev->interleave_ways = cpu_to_le16(1);
304cb836434SHaozhong Zhang 
305cb836434SHaozhong Zhang     if (nvdimm->unarmed) {
306cb836434SHaozhong Zhang         nfit_memdev->flags |= cpu_to_le16(ACPI_NFIT_MEM_NOT_ARMED);
307cb836434SHaozhong Zhang     }
30887252e1bSXiao Guangrong }
30987252e1bSXiao Guangrong 
31087252e1bSXiao Guangrong /*
31187252e1bSXiao Guangrong  * ACPI 6.0: 5.2.25.5 NVDIMM Control Region Structure.
31287252e1bSXiao Guangrong  */
31387252e1bSXiao Guangrong static void nvdimm_build_structure_dcr(GArray *structures, DeviceState *dev)
31487252e1bSXiao Guangrong {
31587252e1bSXiao Guangrong     NvdimmNfitControlRegion *nfit_dcr;
31687252e1bSXiao Guangrong     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
31787252e1bSXiao Guangrong                                        NULL);
31887252e1bSXiao Guangrong     uint32_t sn = nvdimm_slot_to_sn(slot);
31987252e1bSXiao Guangrong 
32087252e1bSXiao Guangrong     nfit_dcr = acpi_data_push(structures, sizeof(*nfit_dcr));
32187252e1bSXiao Guangrong 
32287252e1bSXiao Guangrong     nfit_dcr->type = cpu_to_le16(4 /* NVDIMM Control Region Structure */);
32387252e1bSXiao Guangrong     nfit_dcr->length = cpu_to_le16(sizeof(*nfit_dcr));
32487252e1bSXiao Guangrong     nfit_dcr->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
32587252e1bSXiao Guangrong 
32687252e1bSXiao Guangrong     /* vendor: Intel. */
32787252e1bSXiao Guangrong     nfit_dcr->vendor_id = cpu_to_le16(0x8086);
32887252e1bSXiao Guangrong     nfit_dcr->device_id = cpu_to_le16(1);
32987252e1bSXiao Guangrong 
33087252e1bSXiao Guangrong     /* The _DSM method is following Intel's DSM specification. */
33187252e1bSXiao Guangrong     nfit_dcr->revision_id = cpu_to_le16(1 /* Current Revision supported
33287252e1bSXiao Guangrong                                              in ACPI 6.0 is 1. */);
33387252e1bSXiao Guangrong     nfit_dcr->serial_number = cpu_to_le32(sn);
33420fdef58SHaozhong Zhang     nfit_dcr->fic = cpu_to_le16(0x301 /* Format Interface Code:
33520fdef58SHaozhong Zhang                                          Byte addressable, no energy backed.
33620fdef58SHaozhong Zhang                                          See ACPI 6.2, sect 5.2.25.6 and
33720fdef58SHaozhong Zhang                                          JEDEC Annex L Release 3. */);
33887252e1bSXiao Guangrong }
33987252e1bSXiao Guangrong 
3409ab3aad2SRoss Zwisler /*
3419ab3aad2SRoss Zwisler  * ACPI 6.2 Errata A: 5.2.25.9 NVDIMM Platform Capabilities Structure
3429ab3aad2SRoss Zwisler  */
3439ab3aad2SRoss Zwisler static void
3449ab3aad2SRoss Zwisler nvdimm_build_structure_caps(GArray *structures, uint32_t capabilities)
3459ab3aad2SRoss Zwisler {
3469ab3aad2SRoss Zwisler     NvdimmNfitPlatformCaps *nfit_caps;
3479ab3aad2SRoss Zwisler 
3489ab3aad2SRoss Zwisler     nfit_caps = acpi_data_push(structures, sizeof(*nfit_caps));
3499ab3aad2SRoss Zwisler 
3509ab3aad2SRoss Zwisler     nfit_caps->type = cpu_to_le16(7 /* NVDIMM Platform Capabilities */);
3519ab3aad2SRoss Zwisler     nfit_caps->length = cpu_to_le16(sizeof(*nfit_caps));
3529ab3aad2SRoss Zwisler     nfit_caps->highest_cap = 31 - clz32(capabilities);
3539ab3aad2SRoss Zwisler     nfit_caps->capabilities = cpu_to_le32(capabilities);
3549ab3aad2SRoss Zwisler }
3559ab3aad2SRoss Zwisler 
356c1404bdeSEric Auger static GArray *nvdimm_build_device_structure(NVDIMMState *state)
35787252e1bSXiao Guangrong {
358cf7c0ff5SXiao Guangrong     GSList *device_list = nvdimm_get_device_list();
35987252e1bSXiao Guangrong     GArray *structures = g_array_new(false, true /* clear */, 1);
36087252e1bSXiao Guangrong 
36187252e1bSXiao Guangrong     for (; device_list; device_list = device_list->next) {
36287252e1bSXiao Guangrong         DeviceState *dev = device_list->data;
36387252e1bSXiao Guangrong 
36487252e1bSXiao Guangrong         /* build System Physical Address Range Structure. */
36587252e1bSXiao Guangrong         nvdimm_build_structure_spa(structures, dev);
36687252e1bSXiao Guangrong 
36787252e1bSXiao Guangrong         /*
36887252e1bSXiao Guangrong          * build Memory Device to System Physical Address Range Mapping
36987252e1bSXiao Guangrong          * Structure.
37087252e1bSXiao Guangrong          */
37187252e1bSXiao Guangrong         nvdimm_build_structure_memdev(structures, dev);
37287252e1bSXiao Guangrong 
37387252e1bSXiao Guangrong         /* build NVDIMM Control Region Structure. */
37487252e1bSXiao Guangrong         nvdimm_build_structure_dcr(structures, dev);
37587252e1bSXiao Guangrong     }
37675b0713eSXiao Guangrong     g_slist_free(device_list);
37787252e1bSXiao Guangrong 
37811c39b5cSRoss Zwisler     if (state->persistence) {
37911c39b5cSRoss Zwisler         nvdimm_build_structure_caps(structures, state->persistence);
3809ab3aad2SRoss Zwisler     }
3819ab3aad2SRoss Zwisler 
38287252e1bSXiao Guangrong     return structures;
38387252e1bSXiao Guangrong }
38487252e1bSXiao Guangrong 
38575b0713eSXiao Guangrong static void nvdimm_init_fit_buffer(NvdimmFitBuffer *fit_buf)
38675b0713eSXiao Guangrong {
38775b0713eSXiao Guangrong     fit_buf->fit = g_array_new(false, true /* clear */, 1);
38875b0713eSXiao Guangrong }
38975b0713eSXiao Guangrong 
390c1404bdeSEric Auger static void nvdimm_build_fit_buffer(NVDIMMState *state)
39175b0713eSXiao Guangrong {
3929ab3aad2SRoss Zwisler     NvdimmFitBuffer *fit_buf = &state->fit_buf;
3939ab3aad2SRoss Zwisler 
39475b0713eSXiao Guangrong     g_array_free(fit_buf->fit, true);
3959ab3aad2SRoss Zwisler     fit_buf->fit = nvdimm_build_device_structure(state);
39675b0713eSXiao Guangrong     fit_buf->dirty = true;
39775b0713eSXiao Guangrong }
39875b0713eSXiao Guangrong 
399c1404bdeSEric Auger void nvdimm_plug(NVDIMMState *state)
40075b0713eSXiao Guangrong {
4019ab3aad2SRoss Zwisler     nvdimm_build_fit_buffer(state);
40275b0713eSXiao Guangrong }
40375b0713eSXiao Guangrong 
404c1404bdeSEric Auger static void nvdimm_build_nfit(NVDIMMState *state, GArray *table_offsets,
405*602b4582SMarian Postevca                               GArray *table_data, BIOSLinker *linker,
406*602b4582SMarian Postevca                               const char *oem_id, const char *oem_table_id)
40787252e1bSXiao Guangrong {
40875b0713eSXiao Guangrong     NvdimmFitBuffer *fit_buf = &state->fit_buf;
409c8e6c938SHaozhong Zhang     unsigned int header;
41087252e1bSXiao Guangrong 
41187252e1bSXiao Guangrong     acpi_add_table(table_offsets, table_data);
41287252e1bSXiao Guangrong 
41387252e1bSXiao Guangrong     /* NFIT header. */
414c8e6c938SHaozhong Zhang     header = table_data->len;
415c8e6c938SHaozhong Zhang     acpi_data_push(table_data, sizeof(NvdimmNfitHeader));
41687252e1bSXiao Guangrong     /* NVDIMM device structures. */
41775b0713eSXiao Guangrong     g_array_append_vals(table_data, fit_buf->fit->data, fit_buf->fit->len);
41887252e1bSXiao Guangrong 
419c8e6c938SHaozhong Zhang     build_header(linker, table_data,
420c8e6c938SHaozhong Zhang                  (void *)(table_data->data + header), "NFIT",
421*602b4582SMarian Postevca                  sizeof(NvdimmNfitHeader) + fit_buf->fit->len, 1, oem_id,
422*602b4582SMarian Postevca                  oem_table_id);
42387252e1bSXiao Guangrong }
42487252e1bSXiao Guangrong 
425cb88ebd7SXiao Guangrong #define NVDIMM_DSM_MEMORY_SIZE      4096
426cb88ebd7SXiao Guangrong 
42718c440e1SXiao Guangrong struct NvdimmDsmIn {
42818c440e1SXiao Guangrong     uint32_t handle;
42918c440e1SXiao Guangrong     uint32_t revision;
43018c440e1SXiao Guangrong     uint32_t function;
43118c440e1SXiao Guangrong     /* the remaining size in the page is used by arg3. */
43218c440e1SXiao Guangrong     union {
43335c5a52dSPaolo Bonzini         uint8_t arg3[4084];
43418c440e1SXiao Guangrong     };
43518c440e1SXiao Guangrong } QEMU_PACKED;
43618c440e1SXiao Guangrong typedef struct NvdimmDsmIn NvdimmDsmIn;
437cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmIn) != NVDIMM_DSM_MEMORY_SIZE);
43818c440e1SXiao Guangrong 
43918c440e1SXiao Guangrong struct NvdimmDsmOut {
44018c440e1SXiao Guangrong     /* the size of buffer filled by QEMU. */
44118c440e1SXiao Guangrong     uint32_t len;
44235c5a52dSPaolo Bonzini     uint8_t data[4092];
44318c440e1SXiao Guangrong } QEMU_PACKED;
44418c440e1SXiao Guangrong typedef struct NvdimmDsmOut NvdimmDsmOut;
445cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmOut) != NVDIMM_DSM_MEMORY_SIZE);
44618c440e1SXiao Guangrong 
447f7df22deSXiao Guangrong struct NvdimmDsmFunc0Out {
448f7df22deSXiao Guangrong     /* the size of buffer filled by QEMU. */
449f7df22deSXiao Guangrong      uint32_t len;
450f7df22deSXiao Guangrong      uint32_t supported_func;
451f7df22deSXiao Guangrong } QEMU_PACKED;
452f7df22deSXiao Guangrong typedef struct NvdimmDsmFunc0Out NvdimmDsmFunc0Out;
453f7df22deSXiao Guangrong 
454f7df22deSXiao Guangrong struct NvdimmDsmFuncNoPayloadOut {
455f7df22deSXiao Guangrong     /* the size of buffer filled by QEMU. */
456f7df22deSXiao Guangrong      uint32_t len;
457f7df22deSXiao Guangrong      uint32_t func_ret_status;
458f7df22deSXiao Guangrong } QEMU_PACKED;
459f7df22deSXiao Guangrong typedef struct NvdimmDsmFuncNoPayloadOut NvdimmDsmFuncNoPayloadOut;
460f7df22deSXiao Guangrong 
4615797dcdcSXiao Guangrong struct NvdimmFuncGetLabelSizeOut {
4625797dcdcSXiao Guangrong     /* the size of buffer filled by QEMU. */
4635797dcdcSXiao Guangrong     uint32_t len;
4645797dcdcSXiao Guangrong     uint32_t func_ret_status; /* return status code. */
4655797dcdcSXiao Guangrong     uint32_t label_size; /* the size of label data area. */
4665797dcdcSXiao Guangrong     /*
4675797dcdcSXiao Guangrong      * Maximum size of the namespace label data length supported by
4685797dcdcSXiao Guangrong      * the platform in Get/Set Namespace Label Data functions.
4695797dcdcSXiao Guangrong      */
4705797dcdcSXiao Guangrong     uint32_t max_xfer;
4715797dcdcSXiao Guangrong } QEMU_PACKED;
4725797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelSizeOut NvdimmFuncGetLabelSizeOut;
473cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelSizeOut) > NVDIMM_DSM_MEMORY_SIZE);
4745797dcdcSXiao Guangrong 
4752b9e57fcSXiao Guangrong struct NvdimmFuncGetLabelDataIn {
4762b9e57fcSXiao Guangrong     uint32_t offset; /* the offset in the namespace label data area. */
4772b9e57fcSXiao Guangrong     uint32_t length; /* the size of data is to be read via the function. */
4782b9e57fcSXiao Guangrong } QEMU_PACKED;
4792b9e57fcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataIn NvdimmFuncGetLabelDataIn;
4802b9e57fcSXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataIn) +
481cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
4822b9e57fcSXiao Guangrong 
4835797dcdcSXiao Guangrong struct NvdimmFuncGetLabelDataOut {
4845797dcdcSXiao Guangrong     /* the size of buffer filled by QEMU. */
4855797dcdcSXiao Guangrong     uint32_t len;
4865797dcdcSXiao Guangrong     uint32_t func_ret_status; /* return status code. */
487f7795e40SPhilippe Mathieu-Daudé     uint8_t out_buf[]; /* the data got via Get Namesapce Label function. */
4885797dcdcSXiao Guangrong } QEMU_PACKED;
4895797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataOut NvdimmFuncGetLabelDataOut;
490cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataOut) > NVDIMM_DSM_MEMORY_SIZE);
4915797dcdcSXiao Guangrong 
4925797dcdcSXiao Guangrong struct NvdimmFuncSetLabelDataIn {
4935797dcdcSXiao Guangrong     uint32_t offset; /* the offset in the namespace label data area. */
4945797dcdcSXiao Guangrong     uint32_t length; /* the size of data is to be written via the function. */
495f7795e40SPhilippe Mathieu-Daudé     uint8_t in_buf[]; /* the data written to label data area. */
4965797dcdcSXiao Guangrong } QEMU_PACKED;
4975797dcdcSXiao Guangrong typedef struct NvdimmFuncSetLabelDataIn NvdimmFuncSetLabelDataIn;
49814e44198SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncSetLabelDataIn) +
499cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
5005797dcdcSXiao Guangrong 
501806864d9SXiao Guangrong struct NvdimmFuncReadFITIn {
5027adbce63SXiao Guangrong     uint32_t offset; /* the offset into FIT buffer. */
503806864d9SXiao Guangrong } QEMU_PACKED;
504806864d9SXiao Guangrong typedef struct NvdimmFuncReadFITIn NvdimmFuncReadFITIn;
505806864d9SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITIn) +
506cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
507806864d9SXiao Guangrong 
508806864d9SXiao Guangrong struct NvdimmFuncReadFITOut {
509806864d9SXiao Guangrong     /* the size of buffer filled by QEMU. */
510806864d9SXiao Guangrong     uint32_t len;
511806864d9SXiao Guangrong     uint32_t func_ret_status; /* return status code. */
512f7795e40SPhilippe Mathieu-Daudé     uint8_t fit[]; /* the FIT data. */
513806864d9SXiao Guangrong } QEMU_PACKED;
514806864d9SXiao Guangrong typedef struct NvdimmFuncReadFITOut NvdimmFuncReadFITOut;
515cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITOut) > NVDIMM_DSM_MEMORY_SIZE);
516806864d9SXiao Guangrong 
517189f4d56SXiao Guangrong static void
518189f4d56SXiao Guangrong nvdimm_dsm_function0(uint32_t supported_func, hwaddr dsm_mem_addr)
519189f4d56SXiao Guangrong {
520189f4d56SXiao Guangrong     NvdimmDsmFunc0Out func0 = {
521189f4d56SXiao Guangrong         .len = cpu_to_le32(sizeof(func0)),
522189f4d56SXiao Guangrong         .supported_func = cpu_to_le32(supported_func),
523189f4d56SXiao Guangrong     };
524189f4d56SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &func0, sizeof(func0));
525189f4d56SXiao Guangrong }
526189f4d56SXiao Guangrong 
527189f4d56SXiao Guangrong static void
528189f4d56SXiao Guangrong nvdimm_dsm_no_payload(uint32_t func_ret_status, hwaddr dsm_mem_addr)
529189f4d56SXiao Guangrong {
530189f4d56SXiao Guangrong     NvdimmDsmFuncNoPayloadOut out = {
531189f4d56SXiao Guangrong         .len = cpu_to_le32(sizeof(out)),
532189f4d56SXiao Guangrong         .func_ret_status = cpu_to_le32(func_ret_status),
533189f4d56SXiao Guangrong     };
534189f4d56SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &out, sizeof(out));
535189f4d56SXiao Guangrong }
536189f4d56SXiao Guangrong 
537c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_SUCCESS        0 /* Success */
538c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_UNSUPPORT      1 /* Not Supported */
539c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_NOMEMDEV       2 /* Non-Existing Memory Device */
540c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_INVALID        3 /* Invalid Input Parameters */
541c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_FIT_CHANGED    0x100 /* FIT Changed */
542c2fa3075SXiao Guangrong 
543806864d9SXiao Guangrong #define NVDIMM_QEMU_RSVD_HANDLE_ROOT         0x10000
544806864d9SXiao Guangrong 
545806864d9SXiao Guangrong /* Read FIT data, defined in docs/specs/acpi_nvdimm.txt. */
546c1404bdeSEric Auger static void nvdimm_dsm_func_read_fit(NVDIMMState *state, NvdimmDsmIn *in,
547806864d9SXiao Guangrong                                      hwaddr dsm_mem_addr)
548806864d9SXiao Guangrong {
549806864d9SXiao Guangrong     NvdimmFitBuffer *fit_buf = &state->fit_buf;
550806864d9SXiao Guangrong     NvdimmFuncReadFITIn *read_fit;
551806864d9SXiao Guangrong     NvdimmFuncReadFITOut *read_fit_out;
552806864d9SXiao Guangrong     GArray *fit;
553806864d9SXiao Guangrong     uint32_t read_len = 0, func_ret_status;
554806864d9SXiao Guangrong     int size;
555806864d9SXiao Guangrong 
556806864d9SXiao Guangrong     read_fit = (NvdimmFuncReadFITIn *)in->arg3;
557435cc3e4SPeter Maydell     read_fit->offset = le32_to_cpu(read_fit->offset);
558806864d9SXiao Guangrong 
559806864d9SXiao Guangrong     fit = fit_buf->fit;
560806864d9SXiao Guangrong 
56188eed198SXinhao Zhang     nvdimm_debug("Read FIT: offset 0x%x FIT size 0x%x Dirty %s.\n",
562806864d9SXiao Guangrong                  read_fit->offset, fit->len, fit_buf->dirty ? "Yes" : "No");
563806864d9SXiao Guangrong 
564806864d9SXiao Guangrong     if (read_fit->offset > fit->len) {
565c2fa3075SXiao Guangrong         func_ret_status = NVDIMM_DSM_RET_STATUS_INVALID;
566806864d9SXiao Guangrong         goto exit;
567806864d9SXiao Guangrong     }
568806864d9SXiao Guangrong 
569806864d9SXiao Guangrong     /* It is the first time to read FIT. */
570806864d9SXiao Guangrong     if (!read_fit->offset) {
571806864d9SXiao Guangrong         fit_buf->dirty = false;
572806864d9SXiao Guangrong     } else if (fit_buf->dirty) { /* FIT has been changed during RFIT. */
573c2fa3075SXiao Guangrong         func_ret_status = NVDIMM_DSM_RET_STATUS_FIT_CHANGED;
574806864d9SXiao Guangrong         goto exit;
575806864d9SXiao Guangrong     }
576806864d9SXiao Guangrong 
577c2fa3075SXiao Guangrong     func_ret_status = NVDIMM_DSM_RET_STATUS_SUCCESS;
578806864d9SXiao Guangrong     read_len = MIN(fit->len - read_fit->offset,
579cb88ebd7SXiao Guangrong                    NVDIMM_DSM_MEMORY_SIZE - sizeof(NvdimmFuncReadFITOut));
580806864d9SXiao Guangrong 
581806864d9SXiao Guangrong exit:
582806864d9SXiao Guangrong     size = sizeof(NvdimmFuncReadFITOut) + read_len;
583806864d9SXiao Guangrong     read_fit_out = g_malloc(size);
584806864d9SXiao Guangrong 
585806864d9SXiao Guangrong     read_fit_out->len = cpu_to_le32(size);
586806864d9SXiao Guangrong     read_fit_out->func_ret_status = cpu_to_le32(func_ret_status);
587806864d9SXiao Guangrong     memcpy(read_fit_out->fit, fit->data + read_fit->offset, read_len);
588806864d9SXiao Guangrong 
589806864d9SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, read_fit_out, size);
590806864d9SXiao Guangrong 
591806864d9SXiao Guangrong     g_free(read_fit_out);
592806864d9SXiao Guangrong }
593806864d9SXiao Guangrong 
5945a33db78SXiao Guangrong static void
595c1404bdeSEric Auger nvdimm_dsm_handle_reserved_root_method(NVDIMMState *state,
5965a33db78SXiao Guangrong                                        NvdimmDsmIn *in, hwaddr dsm_mem_addr)
597806864d9SXiao Guangrong {
598806864d9SXiao Guangrong     switch (in->function) {
599806864d9SXiao Guangrong     case 0x0:
600806864d9SXiao Guangrong         nvdimm_dsm_function0(0x1 | 1 << 1 /* Read FIT */, dsm_mem_addr);
601806864d9SXiao Guangrong         return;
602806864d9SXiao Guangrong     case 0x1 /* Read FIT */:
603806864d9SXiao Guangrong         nvdimm_dsm_func_read_fit(state, in, dsm_mem_addr);
604806864d9SXiao Guangrong         return;
605806864d9SXiao Guangrong     }
606806864d9SXiao Guangrong 
607c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
608806864d9SXiao Guangrong }
609806864d9SXiao Guangrong 
610189f4d56SXiao Guangrong static void nvdimm_dsm_root(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
611189f4d56SXiao Guangrong {
612189f4d56SXiao Guangrong     /*
613189f4d56SXiao Guangrong      * function 0 is called to inquire which functions are supported by
614189f4d56SXiao Guangrong      * OSPM
615189f4d56SXiao Guangrong      */
616189f4d56SXiao Guangrong     if (!in->function) {
617189f4d56SXiao Guangrong         nvdimm_dsm_function0(0 /* No function supported other than
618189f4d56SXiao Guangrong                                   function 0 */, dsm_mem_addr);
619189f4d56SXiao Guangrong         return;
620189f4d56SXiao Guangrong     }
621189f4d56SXiao Guangrong 
622189f4d56SXiao Guangrong     /* No function except function 0 is supported yet. */
623c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
624189f4d56SXiao Guangrong }
625189f4d56SXiao Guangrong 
6265797dcdcSXiao Guangrong /*
6275797dcdcSXiao Guangrong  * the max transfer size is the max size transferred by both a
6285797dcdcSXiao Guangrong  * 'Get Namespace Label Data' function and a 'Set Namespace Label Data'
6295797dcdcSXiao Guangrong  * function.
6305797dcdcSXiao Guangrong  */
6315797dcdcSXiao Guangrong static uint32_t nvdimm_get_max_xfer_label_size(void)
6325797dcdcSXiao Guangrong {
633cb88ebd7SXiao Guangrong     uint32_t max_get_size, max_set_size, dsm_memory_size;
634cb88ebd7SXiao Guangrong 
635cb88ebd7SXiao Guangrong     dsm_memory_size = NVDIMM_DSM_MEMORY_SIZE;
6365797dcdcSXiao Guangrong 
6375797dcdcSXiao Guangrong     /*
6385797dcdcSXiao Guangrong      * the max data ACPI can read one time which is transferred by
6395797dcdcSXiao Guangrong      * the response of 'Get Namespace Label Data' function.
6405797dcdcSXiao Guangrong      */
6415797dcdcSXiao Guangrong     max_get_size = dsm_memory_size - sizeof(NvdimmFuncGetLabelDataOut);
6425797dcdcSXiao Guangrong 
6435797dcdcSXiao Guangrong     /*
6445797dcdcSXiao Guangrong      * the max data ACPI can write one time which is transferred by
6455797dcdcSXiao Guangrong      * 'Set Namespace Label Data' function.
6465797dcdcSXiao Guangrong      */
6475797dcdcSXiao Guangrong     max_set_size = dsm_memory_size - offsetof(NvdimmDsmIn, arg3) -
6485797dcdcSXiao Guangrong                    sizeof(NvdimmFuncSetLabelDataIn);
6495797dcdcSXiao Guangrong 
6505797dcdcSXiao Guangrong     return MIN(max_get_size, max_set_size);
6515797dcdcSXiao Guangrong }
6525797dcdcSXiao Guangrong 
6535797dcdcSXiao Guangrong /*
6545797dcdcSXiao Guangrong  * DSM Spec Rev1 4.4 Get Namespace Label Size (Function Index 4).
6555797dcdcSXiao Guangrong  *
6565797dcdcSXiao Guangrong  * It gets the size of Namespace Label data area and the max data size
6575797dcdcSXiao Guangrong  * that Get/Set Namespace Label Data functions can transfer.
6585797dcdcSXiao Guangrong  */
6595797dcdcSXiao Guangrong static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm, hwaddr dsm_mem_addr)
6605797dcdcSXiao Guangrong {
6615797dcdcSXiao Guangrong     NvdimmFuncGetLabelSizeOut label_size_out = {
6625797dcdcSXiao Guangrong         .len = cpu_to_le32(sizeof(label_size_out)),
6635797dcdcSXiao Guangrong     };
6645797dcdcSXiao Guangrong     uint32_t label_size, mxfer;
6655797dcdcSXiao Guangrong 
6665797dcdcSXiao Guangrong     label_size = nvdimm->label_size;
6675797dcdcSXiao Guangrong     mxfer = nvdimm_get_max_xfer_label_size();
6685797dcdcSXiao Guangrong 
66988eed198SXinhao Zhang     nvdimm_debug("label_size 0x%x, max_xfer 0x%x.\n", label_size, mxfer);
6705797dcdcSXiao Guangrong 
671c2fa3075SXiao Guangrong     label_size_out.func_ret_status = cpu_to_le32(NVDIMM_DSM_RET_STATUS_SUCCESS);
6725797dcdcSXiao Guangrong     label_size_out.label_size = cpu_to_le32(label_size);
6735797dcdcSXiao Guangrong     label_size_out.max_xfer = cpu_to_le32(mxfer);
6745797dcdcSXiao Guangrong 
6755797dcdcSXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &label_size_out,
6765797dcdcSXiao Guangrong                               sizeof(label_size_out));
6775797dcdcSXiao Guangrong }
6785797dcdcSXiao Guangrong 
6792b9e57fcSXiao Guangrong static uint32_t nvdimm_rw_label_data_check(NVDIMMDevice *nvdimm,
6802b9e57fcSXiao Guangrong                                            uint32_t offset, uint32_t length)
6812b9e57fcSXiao Guangrong {
682c2fa3075SXiao Guangrong     uint32_t ret = NVDIMM_DSM_RET_STATUS_INVALID;
6832b9e57fcSXiao Guangrong 
6842b9e57fcSXiao Guangrong     if (offset + length < offset) {
68588eed198SXinhao Zhang         nvdimm_debug("offset 0x%x + length 0x%x is overflow.\n", offset,
6862b9e57fcSXiao Guangrong                      length);
6872b9e57fcSXiao Guangrong         return ret;
6882b9e57fcSXiao Guangrong     }
6892b9e57fcSXiao Guangrong 
6902b9e57fcSXiao Guangrong     if (nvdimm->label_size < offset + length) {
69188eed198SXinhao Zhang         nvdimm_debug("position 0x%x is beyond label data (len = %" PRIx64 ").\n",
6922b9e57fcSXiao Guangrong                      offset + length, nvdimm->label_size);
6932b9e57fcSXiao Guangrong         return ret;
6942b9e57fcSXiao Guangrong     }
6952b9e57fcSXiao Guangrong 
6962b9e57fcSXiao Guangrong     if (length > nvdimm_get_max_xfer_label_size()) {
69788eed198SXinhao Zhang         nvdimm_debug("length (0x%x) is larger than max_xfer (0x%x).\n",
6982b9e57fcSXiao Guangrong                      length, nvdimm_get_max_xfer_label_size());
6992b9e57fcSXiao Guangrong         return ret;
7002b9e57fcSXiao Guangrong     }
7012b9e57fcSXiao Guangrong 
702c2fa3075SXiao Guangrong     return NVDIMM_DSM_RET_STATUS_SUCCESS;
7032b9e57fcSXiao Guangrong }
7042b9e57fcSXiao Guangrong 
7052b9e57fcSXiao Guangrong /*
7062b9e57fcSXiao Guangrong  * DSM Spec Rev1 4.5 Get Namespace Label Data (Function Index 5).
7072b9e57fcSXiao Guangrong  */
7082b9e57fcSXiao Guangrong static void nvdimm_dsm_get_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
7092b9e57fcSXiao Guangrong                                       hwaddr dsm_mem_addr)
7102b9e57fcSXiao Guangrong {
7112b9e57fcSXiao Guangrong     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
7122b9e57fcSXiao Guangrong     NvdimmFuncGetLabelDataIn *get_label_data;
7132b9e57fcSXiao Guangrong     NvdimmFuncGetLabelDataOut *get_label_data_out;
7142b9e57fcSXiao Guangrong     uint32_t status;
7152b9e57fcSXiao Guangrong     int size;
7162b9e57fcSXiao Guangrong 
7172b9e57fcSXiao Guangrong     get_label_data = (NvdimmFuncGetLabelDataIn *)in->arg3;
718435cc3e4SPeter Maydell     get_label_data->offset = le32_to_cpu(get_label_data->offset);
719435cc3e4SPeter Maydell     get_label_data->length = le32_to_cpu(get_label_data->length);
7202b9e57fcSXiao Guangrong 
72188eed198SXinhao Zhang     nvdimm_debug("Read Label Data: offset 0x%x length 0x%x.\n",
7222b9e57fcSXiao Guangrong                  get_label_data->offset, get_label_data->length);
7232b9e57fcSXiao Guangrong 
7242b9e57fcSXiao Guangrong     status = nvdimm_rw_label_data_check(nvdimm, get_label_data->offset,
7252b9e57fcSXiao Guangrong                                         get_label_data->length);
726c2fa3075SXiao Guangrong     if (status != NVDIMM_DSM_RET_STATUS_SUCCESS) {
7272b9e57fcSXiao Guangrong         nvdimm_dsm_no_payload(status, dsm_mem_addr);
7282b9e57fcSXiao Guangrong         return;
7292b9e57fcSXiao Guangrong     }
7302b9e57fcSXiao Guangrong 
7312b9e57fcSXiao Guangrong     size = sizeof(*get_label_data_out) + get_label_data->length;
732cb88ebd7SXiao Guangrong     assert(size <= NVDIMM_DSM_MEMORY_SIZE);
7332b9e57fcSXiao Guangrong     get_label_data_out = g_malloc(size);
7342b9e57fcSXiao Guangrong 
7352b9e57fcSXiao Guangrong     get_label_data_out->len = cpu_to_le32(size);
736c2fa3075SXiao Guangrong     get_label_data_out->func_ret_status =
737c2fa3075SXiao Guangrong                             cpu_to_le32(NVDIMM_DSM_RET_STATUS_SUCCESS);
7382b9e57fcSXiao Guangrong     nvc->read_label_data(nvdimm, get_label_data_out->out_buf,
7392b9e57fcSXiao Guangrong                          get_label_data->length, get_label_data->offset);
7402b9e57fcSXiao Guangrong 
7412b9e57fcSXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, get_label_data_out, size);
7422b9e57fcSXiao Guangrong     g_free(get_label_data_out);
7432b9e57fcSXiao Guangrong }
7442b9e57fcSXiao Guangrong 
74514e44198SXiao Guangrong /*
74614e44198SXiao Guangrong  * DSM Spec Rev1 4.6 Set Namespace Label Data (Function Index 6).
74714e44198SXiao Guangrong  */
74814e44198SXiao Guangrong static void nvdimm_dsm_set_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
74914e44198SXiao Guangrong                                       hwaddr dsm_mem_addr)
75014e44198SXiao Guangrong {
75114e44198SXiao Guangrong     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
75214e44198SXiao Guangrong     NvdimmFuncSetLabelDataIn *set_label_data;
75314e44198SXiao Guangrong     uint32_t status;
75414e44198SXiao Guangrong 
75514e44198SXiao Guangrong     set_label_data = (NvdimmFuncSetLabelDataIn *)in->arg3;
75614e44198SXiao Guangrong 
757435cc3e4SPeter Maydell     set_label_data->offset = le32_to_cpu(set_label_data->offset);
758435cc3e4SPeter Maydell     set_label_data->length = le32_to_cpu(set_label_data->length);
75914e44198SXiao Guangrong 
76088eed198SXinhao Zhang     nvdimm_debug("Write Label Data: offset 0x%x length 0x%x.\n",
76114e44198SXiao Guangrong                  set_label_data->offset, set_label_data->length);
76214e44198SXiao Guangrong 
76314e44198SXiao Guangrong     status = nvdimm_rw_label_data_check(nvdimm, set_label_data->offset,
76414e44198SXiao Guangrong                                         set_label_data->length);
765c2fa3075SXiao Guangrong     if (status != NVDIMM_DSM_RET_STATUS_SUCCESS) {
76614e44198SXiao Guangrong         nvdimm_dsm_no_payload(status, dsm_mem_addr);
76714e44198SXiao Guangrong         return;
76814e44198SXiao Guangrong     }
76914e44198SXiao Guangrong 
770cb88ebd7SXiao Guangrong     assert(offsetof(NvdimmDsmIn, arg3) + sizeof(*set_label_data) +
771cb88ebd7SXiao Guangrong                     set_label_data->length <= NVDIMM_DSM_MEMORY_SIZE);
77214e44198SXiao Guangrong 
77314e44198SXiao Guangrong     nvc->write_label_data(nvdimm, set_label_data->in_buf,
77414e44198SXiao Guangrong                           set_label_data->length, set_label_data->offset);
775c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_SUCCESS, dsm_mem_addr);
77614e44198SXiao Guangrong }
77714e44198SXiao Guangrong 
778189f4d56SXiao Guangrong static void nvdimm_dsm_device(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
779189f4d56SXiao Guangrong {
7805797dcdcSXiao Guangrong     NVDIMMDevice *nvdimm = nvdimm_get_device_by_handle(in->handle);
7815797dcdcSXiao Guangrong 
782189f4d56SXiao Guangrong     /* See the comments in nvdimm_dsm_root(). */
783189f4d56SXiao Guangrong     if (!in->function) {
7845797dcdcSXiao Guangrong         uint32_t supported_func = 0;
7855797dcdcSXiao Guangrong 
7865797dcdcSXiao Guangrong         if (nvdimm && nvdimm->label_size) {
7875797dcdcSXiao Guangrong             supported_func |= 0x1 /* Bit 0 indicates whether there is
7885797dcdcSXiao Guangrong                                      support for any functions other
7895797dcdcSXiao Guangrong                                      than function 0. */ |
7902b9e57fcSXiao Guangrong                               1 << 4 /* Get Namespace Label Size */ |
79114e44198SXiao Guangrong                               1 << 5 /* Get Namespace Label Data */ |
79214e44198SXiao Guangrong                               1 << 6 /* Set Namespace Label Data */;
7935797dcdcSXiao Guangrong         }
7945797dcdcSXiao Guangrong         nvdimm_dsm_function0(supported_func, dsm_mem_addr);
795189f4d56SXiao Guangrong         return;
796189f4d56SXiao Guangrong     }
797189f4d56SXiao Guangrong 
7985797dcdcSXiao Guangrong     if (!nvdimm) {
799c2fa3075SXiao Guangrong         nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_NOMEMDEV,
8005797dcdcSXiao Guangrong                               dsm_mem_addr);
8015797dcdcSXiao Guangrong         return;
8025797dcdcSXiao Guangrong     }
8035797dcdcSXiao Guangrong 
8045797dcdcSXiao Guangrong     /* Encode DSM function according to DSM Spec Rev1. */
8055797dcdcSXiao Guangrong     switch (in->function) {
8065797dcdcSXiao Guangrong     case 4 /* Get Namespace Label Size */:
8075797dcdcSXiao Guangrong         if (nvdimm->label_size) {
8085797dcdcSXiao Guangrong             nvdimm_dsm_label_size(nvdimm, dsm_mem_addr);
8095797dcdcSXiao Guangrong             return;
8105797dcdcSXiao Guangrong         }
8115797dcdcSXiao Guangrong         break;
8122b9e57fcSXiao Guangrong     case 5 /* Get Namespace Label Data */:
8132b9e57fcSXiao Guangrong         if (nvdimm->label_size) {
8142b9e57fcSXiao Guangrong             nvdimm_dsm_get_label_data(nvdimm, in, dsm_mem_addr);
8152b9e57fcSXiao Guangrong             return;
8162b9e57fcSXiao Guangrong         }
8172b9e57fcSXiao Guangrong         break;
81814e44198SXiao Guangrong     case 0x6 /* Set Namespace Label Data */:
81914e44198SXiao Guangrong         if (nvdimm->label_size) {
82014e44198SXiao Guangrong             nvdimm_dsm_set_label_data(nvdimm, in, dsm_mem_addr);
82114e44198SXiao Guangrong             return;
82214e44198SXiao Guangrong         }
82314e44198SXiao Guangrong         break;
8245797dcdcSXiao Guangrong     }
8255797dcdcSXiao Guangrong 
826c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
827189f4d56SXiao Guangrong }
828189f4d56SXiao Guangrong 
8295fe79386SXiao Guangrong static uint64_t
8305fe79386SXiao Guangrong nvdimm_dsm_read(void *opaque, hwaddr addr, unsigned size)
8315fe79386SXiao Guangrong {
832f7df22deSXiao Guangrong     nvdimm_debug("BUG: we never read _DSM IO Port.\n");
8335fe79386SXiao Guangrong     return 0;
8345fe79386SXiao Guangrong }
8355fe79386SXiao Guangrong 
8365fe79386SXiao Guangrong static void
8375fe79386SXiao Guangrong nvdimm_dsm_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
8385fe79386SXiao Guangrong {
839c1404bdeSEric Auger     NVDIMMState *state = opaque;
840f7df22deSXiao Guangrong     NvdimmDsmIn *in;
841f7df22deSXiao Guangrong     hwaddr dsm_mem_addr = val;
842f7df22deSXiao Guangrong 
84388eed198SXinhao Zhang     nvdimm_debug("dsm memory address 0x%" HWADDR_PRIx ".\n", dsm_mem_addr);
844f7df22deSXiao Guangrong 
845f7df22deSXiao Guangrong     /*
846f7df22deSXiao Guangrong      * The DSM memory is mapped to guest address space so an evil guest
847f7df22deSXiao Guangrong      * can change its content while we are doing DSM emulation. Avoid
848f7df22deSXiao Guangrong      * this by copying DSM memory to QEMU local memory.
849f7df22deSXiao Guangrong      */
85035c5a52dSPaolo Bonzini     in = g_new(NvdimmDsmIn, 1);
85135c5a52dSPaolo Bonzini     cpu_physical_memory_read(dsm_mem_addr, in, sizeof(*in));
852f7df22deSXiao Guangrong 
853435cc3e4SPeter Maydell     in->revision = le32_to_cpu(in->revision);
854435cc3e4SPeter Maydell     in->function = le32_to_cpu(in->function);
855435cc3e4SPeter Maydell     in->handle = le32_to_cpu(in->handle);
856f7df22deSXiao Guangrong 
85788eed198SXinhao Zhang     nvdimm_debug("Revision 0x%x Handler 0x%x Function 0x%x.\n", in->revision,
858f7df22deSXiao Guangrong                  in->handle, in->function);
859f7df22deSXiao Guangrong 
860d15fc53fSXiao Guangrong     if (in->revision != 0x1 /* Currently we only support DSM Spec Rev1. */) {
86188eed198SXinhao Zhang         nvdimm_debug("Revision 0x%x is not supported, expect 0x%x.\n",
862d15fc53fSXiao Guangrong                      in->revision, 0x1);
863c2fa3075SXiao Guangrong         nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
864d15fc53fSXiao Guangrong         goto exit;
865d15fc53fSXiao Guangrong     }
866d15fc53fSXiao Guangrong 
867806864d9SXiao Guangrong     if (in->handle == NVDIMM_QEMU_RSVD_HANDLE_ROOT) {
8685a33db78SXiao Guangrong         nvdimm_dsm_handle_reserved_root_method(state, in, dsm_mem_addr);
869806864d9SXiao Guangrong         goto exit;
870806864d9SXiao Guangrong     }
871806864d9SXiao Guangrong 
872189f4d56SXiao Guangrong      /* Handle 0 is reserved for NVDIMM Root Device. */
873189f4d56SXiao Guangrong     if (!in->handle) {
874189f4d56SXiao Guangrong         nvdimm_dsm_root(in, dsm_mem_addr);
875189f4d56SXiao Guangrong         goto exit;
876f7df22deSXiao Guangrong     }
877f7df22deSXiao Guangrong 
878189f4d56SXiao Guangrong     nvdimm_dsm_device(in, dsm_mem_addr);
879189f4d56SXiao Guangrong 
880189f4d56SXiao Guangrong exit:
881f7df22deSXiao Guangrong     g_free(in);
8825fe79386SXiao Guangrong }
8835fe79386SXiao Guangrong 
8845fe79386SXiao Guangrong static const MemoryRegionOps nvdimm_dsm_ops = {
8855fe79386SXiao Guangrong     .read = nvdimm_dsm_read,
8865fe79386SXiao Guangrong     .write = nvdimm_dsm_write,
8875fe79386SXiao Guangrong     .endianness = DEVICE_LITTLE_ENDIAN,
8885fe79386SXiao Guangrong     .valid = {
8895fe79386SXiao Guangrong         .min_access_size = 4,
8905fe79386SXiao Guangrong         .max_access_size = 4,
8915fe79386SXiao Guangrong     },
8925fe79386SXiao Guangrong };
8935fe79386SXiao Guangrong 
89475f27498SXiao Guangrong void nvdimm_acpi_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev)
89575f27498SXiao Guangrong {
89675f27498SXiao Guangrong     if (dev->hotplugged) {
89775f27498SXiao Guangrong         acpi_send_event(DEVICE(hotplug_dev), ACPI_NVDIMM_HOTPLUG_STATUS);
89875f27498SXiao Guangrong     }
89975f27498SXiao Guangrong }
90075f27498SXiao Guangrong 
901c1404bdeSEric Auger void nvdimm_init_acpi_state(NVDIMMState *state, MemoryRegion *io,
9025c94b826SKwangwoo Lee                             struct AcpiGenericAddress dsm_io,
9035fe79386SXiao Guangrong                             FWCfgState *fw_cfg, Object *owner)
9045fe79386SXiao Guangrong {
9055c94b826SKwangwoo Lee     state->dsm_io = dsm_io;
9065fe79386SXiao Guangrong     memory_region_init_io(&state->io_mr, owner, &nvdimm_dsm_ops, state,
9075c94b826SKwangwoo Lee                           "nvdimm-acpi-io", dsm_io.bit_width >> 3);
9085c94b826SKwangwoo Lee     memory_region_add_subregion(io, dsm_io.address, &state->io_mr);
9095fe79386SXiao Guangrong 
9105fe79386SXiao Guangrong     state->dsm_mem = g_array_new(false, true /* clear */, 1);
91135c5a52dSPaolo Bonzini     acpi_data_push(state->dsm_mem, sizeof(NvdimmDsmIn));
9125fe79386SXiao Guangrong     fw_cfg_add_file(fw_cfg, NVDIMM_DSM_MEM_FILE, state->dsm_mem->data,
9135fe79386SXiao Guangrong                     state->dsm_mem->len);
91475b0713eSXiao Guangrong 
91575b0713eSXiao Guangrong     nvdimm_init_fit_buffer(&state->fit_buf);
9165fe79386SXiao Guangrong }
9175fe79386SXiao Guangrong 
91877286395SXiao Guangrong #define NVDIMM_COMMON_DSM       "NCAL"
919b9951413SXiao Guangrong #define NVDIMM_ACPI_MEM_ADDR    "MEMA"
92077286395SXiao Guangrong 
9213ae66c45SXiao Guangrong #define NVDIMM_DSM_MEMORY       "NRAM"
9223ae66c45SXiao Guangrong #define NVDIMM_DSM_IOPORT       "NPIO"
9233ae66c45SXiao Guangrong 
9243ae66c45SXiao Guangrong #define NVDIMM_DSM_NOTIFY       "NTFI"
9253ae66c45SXiao Guangrong #define NVDIMM_DSM_HANDLE       "HDLE"
9263ae66c45SXiao Guangrong #define NVDIMM_DSM_REVISION     "REVS"
9273ae66c45SXiao Guangrong #define NVDIMM_DSM_FUNCTION     "FUNC"
9283ae66c45SXiao Guangrong #define NVDIMM_DSM_ARG3         "FARG"
9293ae66c45SXiao Guangrong 
9303ae66c45SXiao Guangrong #define NVDIMM_DSM_OUT_BUF_SIZE "RLEN"
9313ae66c45SXiao Guangrong #define NVDIMM_DSM_OUT_BUF      "ODAT"
9323ae66c45SXiao Guangrong 
933806864d9SXiao Guangrong #define NVDIMM_DSM_RFIT_STATUS  "RSTA"
934806864d9SXiao Guangrong 
935806864d9SXiao Guangrong #define NVDIMM_QEMU_RSVD_UUID   "648B9CF2-CDA1-4312-8AD9-49C4AF32BD62"
936806864d9SXiao Guangrong 
9375c94b826SKwangwoo Lee static void nvdimm_build_common_dsm(Aml *dev,
9385c94b826SKwangwoo Lee                                     NVDIMMState *nvdimm_state)
93977286395SXiao Guangrong {
940806864d9SXiao Guangrong     Aml *method, *ifctx, *function, *handle, *uuid, *dsm_mem, *elsectx2;
94190623ebfSXiao Guangrong     Aml *elsectx, *unsupport, *unpatched, *expected_uuid, *uuid_invalid;
942fa1a448dSXiao Guangrong     Aml *pckg, *pckg_index, *pckg_buf, *field, *dsm_out_buf, *dsm_out_buf_size;
94371b0269aSShameer Kolothum     Aml *whilectx, *offset;
94477286395SXiao Guangrong     uint8_t byte_list[1];
9455c94b826SKwangwoo Lee     AmlRegionSpace rs;
94677286395SXiao Guangrong 
947732b530cSXiao Guangrong     method = aml_method(NVDIMM_COMMON_DSM, 5, AML_SERIALIZED);
94890623ebfSXiao Guangrong     uuid = aml_arg(0);
94977286395SXiao Guangrong     function = aml_arg(2);
95090623ebfSXiao Guangrong     handle = aml_arg(4);
951c0b3b863SXiao Guangrong     dsm_mem = aml_local(6);
95248bee476SXiao Guangrong     dsm_out_buf = aml_local(7);
953c0b3b863SXiao Guangrong 
954c0b3b863SXiao Guangrong     aml_append(method, aml_store(aml_name(NVDIMM_ACPI_MEM_ADDR), dsm_mem));
955c0b3b863SXiao Guangrong 
9565c94b826SKwangwoo Lee     if (nvdimm_state->dsm_io.space_id == AML_AS_SYSTEM_IO) {
9575c94b826SKwangwoo Lee         rs = AML_SYSTEM_IO;
9585c94b826SKwangwoo Lee     } else {
9595c94b826SKwangwoo Lee         rs = AML_SYSTEM_MEMORY;
9605c94b826SKwangwoo Lee     }
9615c94b826SKwangwoo Lee 
962c0b3b863SXiao Guangrong     /* map DSM memory and IO into ACPI namespace. */
9635c94b826SKwangwoo Lee     aml_append(method, aml_operation_region(NVDIMM_DSM_IOPORT, rs,
9645c94b826SKwangwoo Lee                aml_int(nvdimm_state->dsm_io.address),
9655c94b826SKwangwoo Lee                nvdimm_state->dsm_io.bit_width >> 3));
9663ae66c45SXiao Guangrong     aml_append(method, aml_operation_region(NVDIMM_DSM_MEMORY,
9673ae66c45SXiao Guangrong                AML_SYSTEM_MEMORY, dsm_mem, sizeof(NvdimmDsmIn)));
968c0b3b863SXiao Guangrong 
969c0b3b863SXiao Guangrong     /*
970c0b3b863SXiao Guangrong      * DSM notifier:
9713ae66c45SXiao Guangrong      * NVDIMM_DSM_NOTIFY: write the address of DSM memory and notify QEMU to
9723ae66c45SXiao Guangrong      *                    emulate the access.
973c0b3b863SXiao Guangrong      *
974c0b3b863SXiao Guangrong      * It is the IO port so that accessing them will cause VM-exit, the
975c0b3b863SXiao Guangrong      * control will be transferred to QEMU.
976c0b3b863SXiao Guangrong      */
9773ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_IOPORT, AML_DWORD_ACC, AML_NOLOCK,
9783ae66c45SXiao Guangrong                       AML_PRESERVE);
9793ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_NOTIFY,
9805c94b826SKwangwoo Lee                nvdimm_state->dsm_io.bit_width));
981c0b3b863SXiao Guangrong     aml_append(method, field);
982c0b3b863SXiao Guangrong 
983c0b3b863SXiao Guangrong     /*
984c0b3b863SXiao Guangrong      * DSM input:
9853ae66c45SXiao Guangrong      * NVDIMM_DSM_HANDLE: store device's handle, it's zero if the _DSM call
9863ae66c45SXiao Guangrong      *                    happens on NVDIMM Root Device.
9873ae66c45SXiao Guangrong      * NVDIMM_DSM_REVISION: store the Arg1 of _DSM call.
9883ae66c45SXiao Guangrong      * NVDIMM_DSM_FUNCTION: store the Arg2 of _DSM call.
9893ae66c45SXiao Guangrong      * NVDIMM_DSM_ARG3: store the Arg3 of _DSM call which is a Package
9903ae66c45SXiao Guangrong      *                  containing function-specific arguments.
991c0b3b863SXiao Guangrong      *
992c0b3b863SXiao Guangrong      * They are RAM mapping on host so that these accesses never cause
993c0b3b863SXiao Guangrong      * VM-EXIT.
994c0b3b863SXiao Guangrong      */
9953ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
9963ae66c45SXiao Guangrong                       AML_PRESERVE);
9973ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_HANDLE,
998c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, handle)) * BITS_PER_BYTE));
9993ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_REVISION,
1000c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, revision)) * BITS_PER_BYTE));
10013ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_FUNCTION,
1002c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, function)) * BITS_PER_BYTE));
10033ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_ARG3,
1004c0b3b863SXiao Guangrong          (sizeof(NvdimmDsmIn) - offsetof(NvdimmDsmIn, arg3)) * BITS_PER_BYTE));
1005c0b3b863SXiao Guangrong     aml_append(method, field);
1006c0b3b863SXiao Guangrong 
1007c0b3b863SXiao Guangrong     /*
1008c0b3b863SXiao Guangrong      * DSM output:
10093ae66c45SXiao Guangrong      * NVDIMM_DSM_OUT_BUF_SIZE: the size of the buffer filled by QEMU.
10103ae66c45SXiao Guangrong      * NVDIMM_DSM_OUT_BUF: the buffer QEMU uses to store the result.
1011c0b3b863SXiao Guangrong      *
1012c0b3b863SXiao Guangrong      * Since the page is reused by both input and out, the input data
1013c0b3b863SXiao Guangrong      * will be lost after storing new result into ODAT so we should fetch
1014c0b3b863SXiao Guangrong      * all the input data before writing the result.
1015c0b3b863SXiao Guangrong      */
10163ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
10173ae66c45SXiao Guangrong                       AML_PRESERVE);
10183ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF_SIZE,
1019c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmOut, len)) * BITS_PER_BYTE));
10203ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF,
1021c0b3b863SXiao Guangrong        (sizeof(NvdimmDsmOut) - offsetof(NvdimmDsmOut, data)) * BITS_PER_BYTE));
1022c0b3b863SXiao Guangrong     aml_append(method, field);
102318c440e1SXiao Guangrong 
102418c440e1SXiao Guangrong     /*
102518c440e1SXiao Guangrong      * do not support any method if DSM memory address has not been
102618c440e1SXiao Guangrong      * patched.
102718c440e1SXiao Guangrong      */
102890623ebfSXiao Guangrong     unpatched = aml_equal(dsm_mem, aml_int(0x0));
102990623ebfSXiao Guangrong 
103090623ebfSXiao Guangrong     expected_uuid = aml_local(0);
103190623ebfSXiao Guangrong 
103290623ebfSXiao Guangrong     ifctx = aml_if(aml_equal(handle, aml_int(0x0)));
103390623ebfSXiao Guangrong     aml_append(ifctx, aml_store(
103490623ebfSXiao Guangrong                aml_touuid("2F10E7A4-9E91-11E4-89D3-123B93F75CBA")
103590623ebfSXiao Guangrong                /* UUID for NVDIMM Root Device */, expected_uuid));
103690623ebfSXiao Guangrong     aml_append(method, ifctx);
103790623ebfSXiao Guangrong     elsectx = aml_else();
1038806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(handle, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT)));
1039806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_touuid(NVDIMM_QEMU_RSVD_UUID
1040806864d9SXiao Guangrong                /* UUID for QEMU internal use */), expected_uuid));
1041806864d9SXiao Guangrong     aml_append(elsectx, ifctx);
1042806864d9SXiao Guangrong     elsectx2 = aml_else();
1043806864d9SXiao Guangrong     aml_append(elsectx2, aml_store(
104490623ebfSXiao Guangrong                aml_touuid("4309AC30-0D11-11E4-9191-0800200C9A66")
104590623ebfSXiao Guangrong                /* UUID for NVDIMM Devices */, expected_uuid));
1046806864d9SXiao Guangrong     aml_append(elsectx, elsectx2);
104790623ebfSXiao Guangrong     aml_append(method, elsectx);
104890623ebfSXiao Guangrong 
104990623ebfSXiao Guangrong     uuid_invalid = aml_lnot(aml_equal(uuid, expected_uuid));
105090623ebfSXiao Guangrong 
105190623ebfSXiao Guangrong     unsupport = aml_if(aml_or(unpatched, uuid_invalid, NULL));
105277286395SXiao Guangrong 
105377286395SXiao Guangrong     /*
105477286395SXiao Guangrong      * function 0 is called to inquire what functions are supported by
105577286395SXiao Guangrong      * OSPM
105677286395SXiao Guangrong      */
105777286395SXiao Guangrong     ifctx = aml_if(aml_equal(function, aml_int(0)));
105877286395SXiao Guangrong     byte_list[0] = 0 /* No function Supported */;
105977286395SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(1, byte_list)));
106090623ebfSXiao Guangrong     aml_append(unsupport, ifctx);
106177286395SXiao Guangrong 
106277286395SXiao Guangrong     /* No function is supported yet. */
1063c2fa3075SXiao Guangrong     byte_list[0] = NVDIMM_DSM_RET_STATUS_UNSUPPORT;
106490623ebfSXiao Guangrong     aml_append(unsupport, aml_return(aml_buffer(1, byte_list)));
106590623ebfSXiao Guangrong     aml_append(method, unsupport);
106677286395SXiao Guangrong 
106718c440e1SXiao Guangrong     /*
106818c440e1SXiao Guangrong      * The HDLE indicates the DSM function is issued from which device,
1069732b530cSXiao Guangrong      * it reserves 0 for root device and is the handle for NVDIMM devices.
1070732b530cSXiao Guangrong      * See the comments in nvdimm_slot_to_handle().
107118c440e1SXiao Guangrong      */
10723ae66c45SXiao Guangrong     aml_append(method, aml_store(handle, aml_name(NVDIMM_DSM_HANDLE)));
10733ae66c45SXiao Guangrong     aml_append(method, aml_store(aml_arg(1), aml_name(NVDIMM_DSM_REVISION)));
1074ac265cacSWei Yang     aml_append(method, aml_store(function, aml_name(NVDIMM_DSM_FUNCTION)));
107518c440e1SXiao Guangrong 
107618c440e1SXiao Guangrong     /*
10774568c948SXiao Guangrong      * The fourth parameter (Arg3) of _DSM is a package which contains
10784568c948SXiao Guangrong      * a buffer, the layout of the buffer is specified by UUID (Arg0),
10794568c948SXiao Guangrong      * Revision ID (Arg1) and Function Index (Arg2) which are documented
10804568c948SXiao Guangrong      * in the DSM Spec.
10814568c948SXiao Guangrong      */
10824568c948SXiao Guangrong     pckg = aml_arg(3);
10834568c948SXiao Guangrong     ifctx = aml_if(aml_and(aml_equal(aml_object_type(pckg),
10844568c948SXiao Guangrong                    aml_int(4 /* Package */)) /* It is a Package? */,
10854568c948SXiao Guangrong                    aml_equal(aml_sizeof(pckg), aml_int(1)) /* 1 element? */,
10864568c948SXiao Guangrong                    NULL));
10874568c948SXiao Guangrong 
10884568c948SXiao Guangrong     pckg_index = aml_local(2);
10894568c948SXiao Guangrong     pckg_buf = aml_local(3);
10904568c948SXiao Guangrong     aml_append(ifctx, aml_store(aml_index(pckg, aml_int(0)), pckg_index));
10914568c948SXiao Guangrong     aml_append(ifctx, aml_store(aml_derefof(pckg_index), pckg_buf));
10923ae66c45SXiao Guangrong     aml_append(ifctx, aml_store(pckg_buf, aml_name(NVDIMM_DSM_ARG3)));
10934568c948SXiao Guangrong     aml_append(method, ifctx);
10944568c948SXiao Guangrong 
10954568c948SXiao Guangrong     /*
109618c440e1SXiao Guangrong      * tell QEMU about the real address of DSM memory, then QEMU
109718c440e1SXiao Guangrong      * gets the control and fills the result in DSM memory.
109818c440e1SXiao Guangrong      */
10993ae66c45SXiao Guangrong     aml_append(method, aml_store(dsm_mem, aml_name(NVDIMM_DSM_NOTIFY)));
110018c440e1SXiao Guangrong 
1101fa1a448dSXiao Guangrong     dsm_out_buf_size = aml_local(1);
1102d51d1d7eSXiao Guangrong     /* RLEN is not included in the payload returned to guest. */
11033ae66c45SXiao Guangrong     aml_append(method, aml_subtract(aml_name(NVDIMM_DSM_OUT_BUF_SIZE),
11043ae66c45SXiao Guangrong                aml_int(4), dsm_out_buf_size));
110571b0269aSShameer Kolothum 
110671b0269aSShameer Kolothum     /*
110771b0269aSShameer Kolothum      * As per ACPI spec 6.3, Table 19-419 Object Conversion Rules, if
110871b0269aSShameer Kolothum      * the Buffer Field <= to the size of an Integer (in bits), it will
110971b0269aSShameer Kolothum      * be treated as an integer. Moreover, the integer size depends on
111071b0269aSShameer Kolothum      * DSDT tables revision number. If revision number is < 2, integer
111171b0269aSShameer Kolothum      * size is 32 bits, otherwise it is 64 bits.
111271b0269aSShameer Kolothum      * Because of this CreateField() canot be used if RLEN < Integer Size.
111371b0269aSShameer Kolothum      *
111471b0269aSShameer Kolothum      * Also please note that APCI ASL operator SizeOf() doesn't support
111571b0269aSShameer Kolothum      * Integer and there isn't any other way to figure out the Integer
111671b0269aSShameer Kolothum      * size. Hence we assume 8 byte as Integer size and if RLEN < 8 bytes,
111771b0269aSShameer Kolothum      * build dsm_out_buf byte by byte.
111871b0269aSShameer Kolothum      */
111971b0269aSShameer Kolothum     ifctx = aml_if(aml_lless(dsm_out_buf_size, aml_int(8)));
112071b0269aSShameer Kolothum     offset = aml_local(2);
112171b0269aSShameer Kolothum     aml_append(ifctx, aml_store(aml_int(0), offset));
112271b0269aSShameer Kolothum     aml_append(ifctx, aml_name_decl("TBUF", aml_buffer(1, NULL)));
112371b0269aSShameer Kolothum     aml_append(ifctx, aml_store(aml_buffer(0, NULL), dsm_out_buf));
112471b0269aSShameer Kolothum 
112571b0269aSShameer Kolothum     whilectx = aml_while(aml_lless(offset, dsm_out_buf_size));
112671b0269aSShameer Kolothum     /* Copy 1 byte at offset from ODAT to temporary buffer(TBUF). */
112771b0269aSShameer Kolothum     aml_append(whilectx, aml_store(aml_derefof(aml_index(
112871b0269aSShameer Kolothum                                    aml_name(NVDIMM_DSM_OUT_BUF), offset)),
112971b0269aSShameer Kolothum                                    aml_index(aml_name("TBUF"), aml_int(0))));
113071b0269aSShameer Kolothum     aml_append(whilectx, aml_concatenate(dsm_out_buf, aml_name("TBUF"),
113171b0269aSShameer Kolothum                                          dsm_out_buf));
113271b0269aSShameer Kolothum     aml_append(whilectx, aml_increment(offset));
113371b0269aSShameer Kolothum     aml_append(ifctx, whilectx);
113471b0269aSShameer Kolothum 
113571b0269aSShameer Kolothum     aml_append(ifctx, aml_return(dsm_out_buf));
113671b0269aSShameer Kolothum     aml_append(method, ifctx);
113771b0269aSShameer Kolothum 
113871b0269aSShameer Kolothum     /* If RLEN >= Integer size, just use CreateField() operator */
1139fa1a448dSXiao Guangrong     aml_append(method, aml_store(aml_shiftleft(dsm_out_buf_size, aml_int(3)),
1140fa1a448dSXiao Guangrong                                  dsm_out_buf_size));
11413ae66c45SXiao Guangrong     aml_append(method, aml_create_field(aml_name(NVDIMM_DSM_OUT_BUF),
11423ae66c45SXiao Guangrong                aml_int(0), dsm_out_buf_size, "OBUF"));
114371b0269aSShameer Kolothum     aml_append(method, aml_return(aml_name("OBUF")));
114471b0269aSShameer Kolothum 
114577286395SXiao Guangrong     aml_append(dev, method);
114677286395SXiao Guangrong }
114777286395SXiao Guangrong 
1148732b530cSXiao Guangrong static void nvdimm_build_device_dsm(Aml *dev, uint32_t handle)
114977286395SXiao Guangrong {
115077286395SXiao Guangrong     Aml *method;
115177286395SXiao Guangrong 
115277286395SXiao Guangrong     method = aml_method("_DSM", 4, AML_NOTSERIALIZED);
1153732b530cSXiao Guangrong     aml_append(method, aml_return(aml_call5(NVDIMM_COMMON_DSM, aml_arg(0),
1154732b530cSXiao Guangrong                                   aml_arg(1), aml_arg(2), aml_arg(3),
1155732b530cSXiao Guangrong                                   aml_int(handle))));
115677286395SXiao Guangrong     aml_append(dev, method);
115777286395SXiao Guangrong }
115877286395SXiao Guangrong 
1159806864d9SXiao Guangrong static void nvdimm_build_fit(Aml *dev)
1160806864d9SXiao Guangrong {
1161806864d9SXiao Guangrong     Aml *method, *pkg, *buf, *buf_size, *offset, *call_result;
1162806864d9SXiao Guangrong     Aml *whilectx, *ifcond, *ifctx, *elsectx, *fit;
1163806864d9SXiao Guangrong 
1164806864d9SXiao Guangrong     buf = aml_local(0);
1165806864d9SXiao Guangrong     buf_size = aml_local(1);
1166806864d9SXiao Guangrong     fit = aml_local(2);
1167806864d9SXiao Guangrong 
1168aef056c1SXiao Guangrong     aml_append(dev, aml_name_decl(NVDIMM_DSM_RFIT_STATUS, aml_int(0)));
1169806864d9SXiao Guangrong 
1170806864d9SXiao Guangrong     /* build helper function, RFIT. */
1171806864d9SXiao Guangrong     method = aml_method("RFIT", 1, AML_SERIALIZED);
1172aef056c1SXiao Guangrong     aml_append(method, aml_name_decl("OFST", aml_int(0)));
1173806864d9SXiao Guangrong 
1174806864d9SXiao Guangrong     /* prepare input package. */
1175806864d9SXiao Guangrong     pkg = aml_package(1);
1176806864d9SXiao Guangrong     aml_append(method, aml_store(aml_arg(0), aml_name("OFST")));
1177806864d9SXiao Guangrong     aml_append(pkg, aml_name("OFST"));
1178806864d9SXiao Guangrong 
1179806864d9SXiao Guangrong     /* call Read_FIT function. */
1180806864d9SXiao Guangrong     call_result = aml_call5(NVDIMM_COMMON_DSM,
1181806864d9SXiao Guangrong                             aml_touuid(NVDIMM_QEMU_RSVD_UUID),
1182806864d9SXiao Guangrong                             aml_int(1) /* Revision 1 */,
1183806864d9SXiao Guangrong                             aml_int(0x1) /* Read FIT */,
1184806864d9SXiao Guangrong                             pkg, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT));
1185806864d9SXiao Guangrong     aml_append(method, aml_store(call_result, buf));
1186806864d9SXiao Guangrong 
1187806864d9SXiao Guangrong     /* handle _DSM result. */
1188806864d9SXiao Guangrong     aml_append(method, aml_create_dword_field(buf,
1189806864d9SXiao Guangrong                aml_int(0) /* offset at byte 0 */, "STAU"));
1190806864d9SXiao Guangrong 
1191806864d9SXiao Guangrong     aml_append(method, aml_store(aml_name("STAU"),
1192806864d9SXiao Guangrong                                  aml_name(NVDIMM_DSM_RFIT_STATUS)));
1193806864d9SXiao Guangrong 
1194806864d9SXiao Guangrong      /* if something is wrong during _DSM. */
1195c2fa3075SXiao Guangrong     ifcond = aml_equal(aml_int(NVDIMM_DSM_RET_STATUS_SUCCESS),
1196c2fa3075SXiao Guangrong                        aml_name("STAU"));
1197806864d9SXiao Guangrong     ifctx = aml_if(aml_lnot(ifcond));
1198806864d9SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1199806864d9SXiao Guangrong     aml_append(method, ifctx);
1200806864d9SXiao Guangrong 
1201806864d9SXiao Guangrong     aml_append(method, aml_store(aml_sizeof(buf), buf_size));
1202806864d9SXiao Guangrong     aml_append(method, aml_subtract(buf_size,
1203806864d9SXiao Guangrong                                     aml_int(4) /* the size of "STAU" */,
1204806864d9SXiao Guangrong                                     buf_size));
1205806864d9SXiao Guangrong 
1206806864d9SXiao Guangrong     /* if we read the end of fit. */
1207806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1208806864d9SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1209806864d9SXiao Guangrong     aml_append(method, ifctx);
1210806864d9SXiao Guangrong 
1211806864d9SXiao Guangrong     aml_append(method, aml_create_field(buf,
1212806864d9SXiao Guangrong                             aml_int(4 * BITS_PER_BYTE), /* offset at byte 4.*/
1213880f3612SXiao Guangrong                             aml_shiftleft(buf_size, aml_int(3)), "BUFF"));
1214806864d9SXiao Guangrong     aml_append(method, aml_return(aml_name("BUFF")));
1215806864d9SXiao Guangrong     aml_append(dev, method);
1216806864d9SXiao Guangrong 
1217806864d9SXiao Guangrong     /* build _FIT. */
1218806864d9SXiao Guangrong     method = aml_method("_FIT", 0, AML_SERIALIZED);
1219806864d9SXiao Guangrong     offset = aml_local(3);
1220806864d9SXiao Guangrong 
1221806864d9SXiao Guangrong     aml_append(method, aml_store(aml_buffer(0, NULL), fit));
1222806864d9SXiao Guangrong     aml_append(method, aml_store(aml_int(0), offset));
1223806864d9SXiao Guangrong 
1224806864d9SXiao Guangrong     whilectx = aml_while(aml_int(1));
1225806864d9SXiao Guangrong     aml_append(whilectx, aml_store(aml_call1("RFIT", offset), buf));
1226806864d9SXiao Guangrong     aml_append(whilectx, aml_store(aml_sizeof(buf), buf_size));
1227806864d9SXiao Guangrong 
1228806864d9SXiao Guangrong     /*
1229806864d9SXiao Guangrong      * if fit buffer was changed during RFIT, read from the beginning
1230806864d9SXiao Guangrong      * again.
1231806864d9SXiao Guangrong      */
1232806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(aml_name(NVDIMM_DSM_RFIT_STATUS),
1233c2fa3075SXiao Guangrong                              aml_int(NVDIMM_DSM_RET_STATUS_FIT_CHANGED)));
1234806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_buffer(0, NULL), fit));
1235806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_int(0), offset));
1236806864d9SXiao Guangrong     aml_append(whilectx, ifctx);
1237806864d9SXiao Guangrong 
1238806864d9SXiao Guangrong     elsectx = aml_else();
1239806864d9SXiao Guangrong 
1240806864d9SXiao Guangrong     /* finish fit read if no data is read out. */
1241806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1242806864d9SXiao Guangrong     aml_append(ifctx, aml_return(fit));
1243806864d9SXiao Guangrong     aml_append(elsectx, ifctx);
1244806864d9SXiao Guangrong 
1245806864d9SXiao Guangrong     /* update the offset. */
1246806864d9SXiao Guangrong     aml_append(elsectx, aml_add(offset, buf_size, offset));
1247806864d9SXiao Guangrong     /* append the data we read out to the fit buffer. */
1248806864d9SXiao Guangrong     aml_append(elsectx, aml_concatenate(fit, buf, fit));
1249806864d9SXiao Guangrong     aml_append(whilectx, elsectx);
1250806864d9SXiao Guangrong     aml_append(method, whilectx);
1251806864d9SXiao Guangrong 
1252806864d9SXiao Guangrong     aml_append(dev, method);
1253806864d9SXiao Guangrong }
1254806864d9SXiao Guangrong 
1255bdfd065bSXiao Guangrong static void nvdimm_build_nvdimm_devices(Aml *root_dev, uint32_t ram_slots)
125677286395SXiao Guangrong {
1257bdfd065bSXiao Guangrong     uint32_t slot;
1258bdfd065bSXiao Guangrong 
1259bdfd065bSXiao Guangrong     for (slot = 0; slot < ram_slots; slot++) {
126077286395SXiao Guangrong         uint32_t handle = nvdimm_slot_to_handle(slot);
126177286395SXiao Guangrong         Aml *nvdimm_dev;
126277286395SXiao Guangrong 
126377286395SXiao Guangrong         nvdimm_dev = aml_device("NV%02X", slot);
126477286395SXiao Guangrong 
126577286395SXiao Guangrong         /*
126677286395SXiao Guangrong          * ACPI 6.0: 9.20 NVDIMM Devices:
126777286395SXiao Guangrong          *
126877286395SXiao Guangrong          * _ADR object that is used to supply OSPM with unique address
126977286395SXiao Guangrong          * of the NVDIMM device. This is done by returning the NFIT Device
127077286395SXiao Guangrong          * handle that is used to identify the associated entries in ACPI
127177286395SXiao Guangrong          * table NFIT or _FIT.
127277286395SXiao Guangrong          */
127377286395SXiao Guangrong         aml_append(nvdimm_dev, aml_name_decl("_ADR", aml_int(handle)));
127477286395SXiao Guangrong 
1275732b530cSXiao Guangrong         nvdimm_build_device_dsm(nvdimm_dev, handle);
127677286395SXiao Guangrong         aml_append(root_dev, nvdimm_dev);
127777286395SXiao Guangrong     }
127877286395SXiao Guangrong }
127977286395SXiao Guangrong 
1280bdfd065bSXiao Guangrong static void nvdimm_build_ssdt(GArray *table_offsets, GArray *table_data,
12815c94b826SKwangwoo Lee                               BIOSLinker *linker,
12825c94b826SKwangwoo Lee                               NVDIMMState *nvdimm_state,
1283*602b4582SMarian Postevca                               uint32_t ram_slots, const char *oem_id)
128477286395SXiao Guangrong {
1285c0b3b863SXiao Guangrong     Aml *ssdt, *sb_scope, *dev;
1286b9951413SXiao Guangrong     int mem_addr_offset, nvdimm_ssdt;
128777286395SXiao Guangrong 
128877286395SXiao Guangrong     acpi_add_table(table_offsets, table_data);
128977286395SXiao Guangrong 
129077286395SXiao Guangrong     ssdt = init_aml_allocator();
129177286395SXiao Guangrong     acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
129277286395SXiao Guangrong 
129377286395SXiao Guangrong     sb_scope = aml_scope("\\_SB");
129477286395SXiao Guangrong 
129577286395SXiao Guangrong     dev = aml_device("NVDR");
129677286395SXiao Guangrong 
129777286395SXiao Guangrong     /*
129877286395SXiao Guangrong      * ACPI 6.0: 9.20 NVDIMM Devices:
129977286395SXiao Guangrong      *
130077286395SXiao Guangrong      * The ACPI Name Space device uses _HID of ACPI0012 to identify the root
130177286395SXiao Guangrong      * NVDIMM interface device. Platform firmware is required to contain one
130277286395SXiao Guangrong      * such device in _SB scope if NVDIMMs support is exposed by platform to
130377286395SXiao Guangrong      * OSPM.
130477286395SXiao Guangrong      * For each NVDIMM present or intended to be supported by platform,
130577286395SXiao Guangrong      * platform firmware also exposes an ACPI Namespace Device under the
130677286395SXiao Guangrong      * root device.
130777286395SXiao Guangrong      */
130877286395SXiao Guangrong     aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0012")));
130977286395SXiao Guangrong 
13105c94b826SKwangwoo Lee     nvdimm_build_common_dsm(dev, nvdimm_state);
1311732b530cSXiao Guangrong 
1312732b530cSXiao Guangrong     /* 0 is reserved for root device. */
1313732b530cSXiao Guangrong     nvdimm_build_device_dsm(dev, 0);
1314806864d9SXiao Guangrong     nvdimm_build_fit(dev);
131577286395SXiao Guangrong 
1316bdfd065bSXiao Guangrong     nvdimm_build_nvdimm_devices(dev, ram_slots);
131777286395SXiao Guangrong 
131877286395SXiao Guangrong     aml_append(sb_scope, dev);
131977286395SXiao Guangrong     aml_append(ssdt, sb_scope);
1320b9951413SXiao Guangrong 
1321b9951413SXiao Guangrong     nvdimm_ssdt = table_data->len;
1322b9951413SXiao Guangrong 
132377286395SXiao Guangrong     /* copy AML table into ACPI tables blob and patch header there */
132477286395SXiao Guangrong     g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
1325b9951413SXiao Guangrong     mem_addr_offset = build_append_named_dword(table_data,
1326b9951413SXiao Guangrong                                                NVDIMM_ACPI_MEM_ADDR);
1327b9951413SXiao Guangrong 
1328ad9671b8SIgor Mammedov     bios_linker_loader_alloc(linker,
13295c94b826SKwangwoo Lee                              NVDIMM_DSM_MEM_FILE, nvdimm_state->dsm_mem,
1330ad9671b8SIgor Mammedov                              sizeof(NvdimmDsmIn), false /* high memory */);
13314678124bSIgor Mammedov     bios_linker_loader_add_pointer(linker,
13324678124bSIgor Mammedov         ACPI_BUILD_TABLE_FILE, mem_addr_offset, sizeof(uint32_t),
13334678124bSIgor Mammedov         NVDIMM_DSM_MEM_FILE, 0);
133477286395SXiao Guangrong     build_header(linker, table_data,
1335b9951413SXiao Guangrong         (void *)(table_data->data + nvdimm_ssdt),
1336*602b4582SMarian Postevca                  "SSDT", table_data->len - nvdimm_ssdt, 1, oem_id, "NVDIMM");
133777286395SXiao Guangrong     free_aml_allocator();
133877286395SXiao Guangrong }
133977286395SXiao Guangrong 
1340c3b0cf6eSVishal Verma void nvdimm_build_srat(GArray *table_data)
1341c3b0cf6eSVishal Verma {
1342c3b0cf6eSVishal Verma     GSList *device_list = nvdimm_get_device_list();
1343c3b0cf6eSVishal Verma 
1344c3b0cf6eSVishal Verma     for (; device_list; device_list = device_list->next) {
1345c3b0cf6eSVishal Verma         AcpiSratMemoryAffinity *numamem = NULL;
1346c3b0cf6eSVishal Verma         DeviceState *dev = device_list->data;
1347c3b0cf6eSVishal Verma         Object *obj = OBJECT(dev);
1348c3b0cf6eSVishal Verma         uint64_t addr, size;
1349c3b0cf6eSVishal Verma         int node;
1350c3b0cf6eSVishal Verma 
1351c3b0cf6eSVishal Verma         node = object_property_get_int(obj, PC_DIMM_NODE_PROP, &error_abort);
1352c3b0cf6eSVishal Verma         addr = object_property_get_uint(obj, PC_DIMM_ADDR_PROP, &error_abort);
1353c3b0cf6eSVishal Verma         size = object_property_get_uint(obj, PC_DIMM_SIZE_PROP, &error_abort);
1354c3b0cf6eSVishal Verma 
1355c3b0cf6eSVishal Verma         numamem = acpi_data_push(table_data, sizeof *numamem);
1356c3b0cf6eSVishal Verma         build_srat_memory(numamem, addr, size, node,
1357c3b0cf6eSVishal Verma                           MEM_AFFINITY_ENABLED | MEM_AFFINITY_NON_VOLATILE);
1358c3b0cf6eSVishal Verma     }
1359c3b0cf6eSVishal Verma     g_slist_free(device_list);
1360c3b0cf6eSVishal Verma }
1361c3b0cf6eSVishal Verma 
136287252e1bSXiao Guangrong void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
1363c1404bdeSEric Auger                        BIOSLinker *linker, NVDIMMState *state,
1364*602b4582SMarian Postevca                        uint32_t ram_slots, const char *oem_id,
1365*602b4582SMarian Postevca                        const char *oem_table_id)
136687252e1bSXiao Guangrong {
1367264813cbSXiao Guangrong     GSList *device_list;
1368bdfd065bSXiao Guangrong 
1369264813cbSXiao Guangrong     /* no nvdimm device can be plugged. */
1370264813cbSXiao Guangrong     if (!ram_slots) {
1371264813cbSXiao Guangrong         return;
1372264813cbSXiao Guangrong     }
1373264813cbSXiao Guangrong 
13745c94b826SKwangwoo Lee     nvdimm_build_ssdt(table_offsets, table_data, linker, state,
1375*602b4582SMarian Postevca                       ram_slots, oem_id);
1376264813cbSXiao Guangrong 
1377cf7c0ff5SXiao Guangrong     device_list = nvdimm_get_device_list();
1378264813cbSXiao Guangrong     /* no NVDIMM device is plugged. */
1379264813cbSXiao Guangrong     if (!device_list) {
1380264813cbSXiao Guangrong         return;
1381bdfd065bSXiao Guangrong     }
1382264813cbSXiao Guangrong 
1383*602b4582SMarian Postevca     nvdimm_build_nfit(state, table_offsets, table_data, linker,
1384*602b4582SMarian Postevca                       oem_id, oem_table_id);
1385264813cbSXiao Guangrong     g_slist_free(device_list);
1386bdfd065bSXiao Guangrong }
1387