xref: /qemu/hw/acpi/nvdimm.c (revision f1e5e2ee)
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
18*f1e5e2eeSChetan 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,
4050e9b9edaSIgor Mammedov                               GArray *table_data, BIOSLinker *linker)
40687252e1bSXiao Guangrong {
40775b0713eSXiao Guangrong     NvdimmFitBuffer *fit_buf = &state->fit_buf;
408c8e6c938SHaozhong Zhang     unsigned int header;
40987252e1bSXiao Guangrong 
41087252e1bSXiao Guangrong     acpi_add_table(table_offsets, table_data);
41187252e1bSXiao Guangrong 
41287252e1bSXiao Guangrong     /* NFIT header. */
413c8e6c938SHaozhong Zhang     header = table_data->len;
414c8e6c938SHaozhong Zhang     acpi_data_push(table_data, sizeof(NvdimmNfitHeader));
41587252e1bSXiao Guangrong     /* NVDIMM device structures. */
41675b0713eSXiao Guangrong     g_array_append_vals(table_data, fit_buf->fit->data, fit_buf->fit->len);
41787252e1bSXiao Guangrong 
418c8e6c938SHaozhong Zhang     build_header(linker, table_data,
419c8e6c938SHaozhong Zhang                  (void *)(table_data->data + header), "NFIT",
42075b0713eSXiao Guangrong                  sizeof(NvdimmNfitHeader) + fit_buf->fit->len, 1, NULL, NULL);
42187252e1bSXiao Guangrong }
42287252e1bSXiao Guangrong 
423cb88ebd7SXiao Guangrong #define NVDIMM_DSM_MEMORY_SIZE      4096
424cb88ebd7SXiao Guangrong 
42518c440e1SXiao Guangrong struct NvdimmDsmIn {
42618c440e1SXiao Guangrong     uint32_t handle;
42718c440e1SXiao Guangrong     uint32_t revision;
42818c440e1SXiao Guangrong     uint32_t function;
42918c440e1SXiao Guangrong     /* the remaining size in the page is used by arg3. */
43018c440e1SXiao Guangrong     union {
43135c5a52dSPaolo Bonzini         uint8_t arg3[4084];
43218c440e1SXiao Guangrong     };
43318c440e1SXiao Guangrong } QEMU_PACKED;
43418c440e1SXiao Guangrong typedef struct NvdimmDsmIn NvdimmDsmIn;
435cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmIn) != NVDIMM_DSM_MEMORY_SIZE);
43618c440e1SXiao Guangrong 
43718c440e1SXiao Guangrong struct NvdimmDsmOut {
43818c440e1SXiao Guangrong     /* the size of buffer filled by QEMU. */
43918c440e1SXiao Guangrong     uint32_t len;
44035c5a52dSPaolo Bonzini     uint8_t data[4092];
44118c440e1SXiao Guangrong } QEMU_PACKED;
44218c440e1SXiao Guangrong typedef struct NvdimmDsmOut NvdimmDsmOut;
443cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmOut) != NVDIMM_DSM_MEMORY_SIZE);
44418c440e1SXiao Guangrong 
445f7df22deSXiao Guangrong struct NvdimmDsmFunc0Out {
446f7df22deSXiao Guangrong     /* the size of buffer filled by QEMU. */
447f7df22deSXiao Guangrong      uint32_t len;
448f7df22deSXiao Guangrong      uint32_t supported_func;
449f7df22deSXiao Guangrong } QEMU_PACKED;
450f7df22deSXiao Guangrong typedef struct NvdimmDsmFunc0Out NvdimmDsmFunc0Out;
451f7df22deSXiao Guangrong 
452f7df22deSXiao Guangrong struct NvdimmDsmFuncNoPayloadOut {
453f7df22deSXiao Guangrong     /* the size of buffer filled by QEMU. */
454f7df22deSXiao Guangrong      uint32_t len;
455f7df22deSXiao Guangrong      uint32_t func_ret_status;
456f7df22deSXiao Guangrong } QEMU_PACKED;
457f7df22deSXiao Guangrong typedef struct NvdimmDsmFuncNoPayloadOut NvdimmDsmFuncNoPayloadOut;
458f7df22deSXiao Guangrong 
4595797dcdcSXiao Guangrong struct NvdimmFuncGetLabelSizeOut {
4605797dcdcSXiao Guangrong     /* the size of buffer filled by QEMU. */
4615797dcdcSXiao Guangrong     uint32_t len;
4625797dcdcSXiao Guangrong     uint32_t func_ret_status; /* return status code. */
4635797dcdcSXiao Guangrong     uint32_t label_size; /* the size of label data area. */
4645797dcdcSXiao Guangrong     /*
4655797dcdcSXiao Guangrong      * Maximum size of the namespace label data length supported by
4665797dcdcSXiao Guangrong      * the platform in Get/Set Namespace Label Data functions.
4675797dcdcSXiao Guangrong      */
4685797dcdcSXiao Guangrong     uint32_t max_xfer;
4695797dcdcSXiao Guangrong } QEMU_PACKED;
4705797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelSizeOut NvdimmFuncGetLabelSizeOut;
471cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelSizeOut) > NVDIMM_DSM_MEMORY_SIZE);
4725797dcdcSXiao Guangrong 
4732b9e57fcSXiao Guangrong struct NvdimmFuncGetLabelDataIn {
4742b9e57fcSXiao Guangrong     uint32_t offset; /* the offset in the namespace label data area. */
4752b9e57fcSXiao Guangrong     uint32_t length; /* the size of data is to be read via the function. */
4762b9e57fcSXiao Guangrong } QEMU_PACKED;
4772b9e57fcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataIn NvdimmFuncGetLabelDataIn;
4782b9e57fcSXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataIn) +
479cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
4802b9e57fcSXiao Guangrong 
4815797dcdcSXiao Guangrong struct NvdimmFuncGetLabelDataOut {
4825797dcdcSXiao Guangrong     /* the size of buffer filled by QEMU. */
4835797dcdcSXiao Guangrong     uint32_t len;
4845797dcdcSXiao Guangrong     uint32_t func_ret_status; /* return status code. */
485f7795e40SPhilippe Mathieu-Daudé     uint8_t out_buf[]; /* the data got via Get Namesapce Label function. */
4865797dcdcSXiao Guangrong } QEMU_PACKED;
4875797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataOut NvdimmFuncGetLabelDataOut;
488cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataOut) > NVDIMM_DSM_MEMORY_SIZE);
4895797dcdcSXiao Guangrong 
4905797dcdcSXiao Guangrong struct NvdimmFuncSetLabelDataIn {
4915797dcdcSXiao Guangrong     uint32_t offset; /* the offset in the namespace label data area. */
4925797dcdcSXiao Guangrong     uint32_t length; /* the size of data is to be written via the function. */
493f7795e40SPhilippe Mathieu-Daudé     uint8_t in_buf[]; /* the data written to label data area. */
4945797dcdcSXiao Guangrong } QEMU_PACKED;
4955797dcdcSXiao Guangrong typedef struct NvdimmFuncSetLabelDataIn NvdimmFuncSetLabelDataIn;
49614e44198SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncSetLabelDataIn) +
497cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
4985797dcdcSXiao Guangrong 
499806864d9SXiao Guangrong struct NvdimmFuncReadFITIn {
5007adbce63SXiao Guangrong     uint32_t offset; /* the offset into FIT buffer. */
501806864d9SXiao Guangrong } QEMU_PACKED;
502806864d9SXiao Guangrong typedef struct NvdimmFuncReadFITIn NvdimmFuncReadFITIn;
503806864d9SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITIn) +
504cb88ebd7SXiao Guangrong                   offsetof(NvdimmDsmIn, arg3) > NVDIMM_DSM_MEMORY_SIZE);
505806864d9SXiao Guangrong 
506806864d9SXiao Guangrong struct NvdimmFuncReadFITOut {
507806864d9SXiao Guangrong     /* the size of buffer filled by QEMU. */
508806864d9SXiao Guangrong     uint32_t len;
509806864d9SXiao Guangrong     uint32_t func_ret_status; /* return status code. */
510f7795e40SPhilippe Mathieu-Daudé     uint8_t fit[]; /* the FIT data. */
511806864d9SXiao Guangrong } QEMU_PACKED;
512806864d9SXiao Guangrong typedef struct NvdimmFuncReadFITOut NvdimmFuncReadFITOut;
513cb88ebd7SXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncReadFITOut) > NVDIMM_DSM_MEMORY_SIZE);
514806864d9SXiao Guangrong 
515189f4d56SXiao Guangrong static void
516189f4d56SXiao Guangrong nvdimm_dsm_function0(uint32_t supported_func, hwaddr dsm_mem_addr)
517189f4d56SXiao Guangrong {
518189f4d56SXiao Guangrong     NvdimmDsmFunc0Out func0 = {
519189f4d56SXiao Guangrong         .len = cpu_to_le32(sizeof(func0)),
520189f4d56SXiao Guangrong         .supported_func = cpu_to_le32(supported_func),
521189f4d56SXiao Guangrong     };
522189f4d56SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &func0, sizeof(func0));
523189f4d56SXiao Guangrong }
524189f4d56SXiao Guangrong 
525189f4d56SXiao Guangrong static void
526189f4d56SXiao Guangrong nvdimm_dsm_no_payload(uint32_t func_ret_status, hwaddr dsm_mem_addr)
527189f4d56SXiao Guangrong {
528189f4d56SXiao Guangrong     NvdimmDsmFuncNoPayloadOut out = {
529189f4d56SXiao Guangrong         .len = cpu_to_le32(sizeof(out)),
530189f4d56SXiao Guangrong         .func_ret_status = cpu_to_le32(func_ret_status),
531189f4d56SXiao Guangrong     };
532189f4d56SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &out, sizeof(out));
533189f4d56SXiao Guangrong }
534189f4d56SXiao Guangrong 
535c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_SUCCESS        0 /* Success */
536c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_UNSUPPORT      1 /* Not Supported */
537c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_NOMEMDEV       2 /* Non-Existing Memory Device */
538c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_INVALID        3 /* Invalid Input Parameters */
539c2fa3075SXiao Guangrong #define NVDIMM_DSM_RET_STATUS_FIT_CHANGED    0x100 /* FIT Changed */
540c2fa3075SXiao Guangrong 
541806864d9SXiao Guangrong #define NVDIMM_QEMU_RSVD_HANDLE_ROOT         0x10000
542806864d9SXiao Guangrong 
543806864d9SXiao Guangrong /* Read FIT data, defined in docs/specs/acpi_nvdimm.txt. */
544c1404bdeSEric Auger static void nvdimm_dsm_func_read_fit(NVDIMMState *state, NvdimmDsmIn *in,
545806864d9SXiao Guangrong                                      hwaddr dsm_mem_addr)
546806864d9SXiao Guangrong {
547806864d9SXiao Guangrong     NvdimmFitBuffer *fit_buf = &state->fit_buf;
548806864d9SXiao Guangrong     NvdimmFuncReadFITIn *read_fit;
549806864d9SXiao Guangrong     NvdimmFuncReadFITOut *read_fit_out;
550806864d9SXiao Guangrong     GArray *fit;
551806864d9SXiao Guangrong     uint32_t read_len = 0, func_ret_status;
552806864d9SXiao Guangrong     int size;
553806864d9SXiao Guangrong 
554806864d9SXiao Guangrong     read_fit = (NvdimmFuncReadFITIn *)in->arg3;
555435cc3e4SPeter Maydell     read_fit->offset = le32_to_cpu(read_fit->offset);
556806864d9SXiao Guangrong 
557806864d9SXiao Guangrong     fit = fit_buf->fit;
558806864d9SXiao Guangrong 
55988eed198SXinhao Zhang     nvdimm_debug("Read FIT: offset 0x%x FIT size 0x%x Dirty %s.\n",
560806864d9SXiao Guangrong                  read_fit->offset, fit->len, fit_buf->dirty ? "Yes" : "No");
561806864d9SXiao Guangrong 
562806864d9SXiao Guangrong     if (read_fit->offset > fit->len) {
563c2fa3075SXiao Guangrong         func_ret_status = NVDIMM_DSM_RET_STATUS_INVALID;
564806864d9SXiao Guangrong         goto exit;
565806864d9SXiao Guangrong     }
566806864d9SXiao Guangrong 
567806864d9SXiao Guangrong     /* It is the first time to read FIT. */
568806864d9SXiao Guangrong     if (!read_fit->offset) {
569806864d9SXiao Guangrong         fit_buf->dirty = false;
570806864d9SXiao Guangrong     } else if (fit_buf->dirty) { /* FIT has been changed during RFIT. */
571c2fa3075SXiao Guangrong         func_ret_status = NVDIMM_DSM_RET_STATUS_FIT_CHANGED;
572806864d9SXiao Guangrong         goto exit;
573806864d9SXiao Guangrong     }
574806864d9SXiao Guangrong 
575c2fa3075SXiao Guangrong     func_ret_status = NVDIMM_DSM_RET_STATUS_SUCCESS;
576806864d9SXiao Guangrong     read_len = MIN(fit->len - read_fit->offset,
577cb88ebd7SXiao Guangrong                    NVDIMM_DSM_MEMORY_SIZE - sizeof(NvdimmFuncReadFITOut));
578806864d9SXiao Guangrong 
579806864d9SXiao Guangrong exit:
580806864d9SXiao Guangrong     size = sizeof(NvdimmFuncReadFITOut) + read_len;
581806864d9SXiao Guangrong     read_fit_out = g_malloc(size);
582806864d9SXiao Guangrong 
583806864d9SXiao Guangrong     read_fit_out->len = cpu_to_le32(size);
584806864d9SXiao Guangrong     read_fit_out->func_ret_status = cpu_to_le32(func_ret_status);
585806864d9SXiao Guangrong     memcpy(read_fit_out->fit, fit->data + read_fit->offset, read_len);
586806864d9SXiao Guangrong 
587806864d9SXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, read_fit_out, size);
588806864d9SXiao Guangrong 
589806864d9SXiao Guangrong     g_free(read_fit_out);
590806864d9SXiao Guangrong }
591806864d9SXiao Guangrong 
5925a33db78SXiao Guangrong static void
593c1404bdeSEric Auger nvdimm_dsm_handle_reserved_root_method(NVDIMMState *state,
5945a33db78SXiao Guangrong                                        NvdimmDsmIn *in, hwaddr dsm_mem_addr)
595806864d9SXiao Guangrong {
596806864d9SXiao Guangrong     switch (in->function) {
597806864d9SXiao Guangrong     case 0x0:
598806864d9SXiao Guangrong         nvdimm_dsm_function0(0x1 | 1 << 1 /* Read FIT */, dsm_mem_addr);
599806864d9SXiao Guangrong         return;
600806864d9SXiao Guangrong     case 0x1 /* Read FIT */:
601806864d9SXiao Guangrong         nvdimm_dsm_func_read_fit(state, in, dsm_mem_addr);
602806864d9SXiao Guangrong         return;
603806864d9SXiao Guangrong     }
604806864d9SXiao Guangrong 
605c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
606806864d9SXiao Guangrong }
607806864d9SXiao Guangrong 
608189f4d56SXiao Guangrong static void nvdimm_dsm_root(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
609189f4d56SXiao Guangrong {
610189f4d56SXiao Guangrong     /*
611189f4d56SXiao Guangrong      * function 0 is called to inquire which functions are supported by
612189f4d56SXiao Guangrong      * OSPM
613189f4d56SXiao Guangrong      */
614189f4d56SXiao Guangrong     if (!in->function) {
615189f4d56SXiao Guangrong         nvdimm_dsm_function0(0 /* No function supported other than
616189f4d56SXiao Guangrong                                   function 0 */, dsm_mem_addr);
617189f4d56SXiao Guangrong         return;
618189f4d56SXiao Guangrong     }
619189f4d56SXiao Guangrong 
620189f4d56SXiao Guangrong     /* No function except function 0 is supported yet. */
621c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
622189f4d56SXiao Guangrong }
623189f4d56SXiao Guangrong 
6245797dcdcSXiao Guangrong /*
6255797dcdcSXiao Guangrong  * the max transfer size is the max size transferred by both a
6265797dcdcSXiao Guangrong  * 'Get Namespace Label Data' function and a 'Set Namespace Label Data'
6275797dcdcSXiao Guangrong  * function.
6285797dcdcSXiao Guangrong  */
6295797dcdcSXiao Guangrong static uint32_t nvdimm_get_max_xfer_label_size(void)
6305797dcdcSXiao Guangrong {
631cb88ebd7SXiao Guangrong     uint32_t max_get_size, max_set_size, dsm_memory_size;
632cb88ebd7SXiao Guangrong 
633cb88ebd7SXiao Guangrong     dsm_memory_size = NVDIMM_DSM_MEMORY_SIZE;
6345797dcdcSXiao Guangrong 
6355797dcdcSXiao Guangrong     /*
6365797dcdcSXiao Guangrong      * the max data ACPI can read one time which is transferred by
6375797dcdcSXiao Guangrong      * the response of 'Get Namespace Label Data' function.
6385797dcdcSXiao Guangrong      */
6395797dcdcSXiao Guangrong     max_get_size = dsm_memory_size - sizeof(NvdimmFuncGetLabelDataOut);
6405797dcdcSXiao Guangrong 
6415797dcdcSXiao Guangrong     /*
6425797dcdcSXiao Guangrong      * the max data ACPI can write one time which is transferred by
6435797dcdcSXiao Guangrong      * 'Set Namespace Label Data' function.
6445797dcdcSXiao Guangrong      */
6455797dcdcSXiao Guangrong     max_set_size = dsm_memory_size - offsetof(NvdimmDsmIn, arg3) -
6465797dcdcSXiao Guangrong                    sizeof(NvdimmFuncSetLabelDataIn);
6475797dcdcSXiao Guangrong 
6485797dcdcSXiao Guangrong     return MIN(max_get_size, max_set_size);
6495797dcdcSXiao Guangrong }
6505797dcdcSXiao Guangrong 
6515797dcdcSXiao Guangrong /*
6525797dcdcSXiao Guangrong  * DSM Spec Rev1 4.4 Get Namespace Label Size (Function Index 4).
6535797dcdcSXiao Guangrong  *
6545797dcdcSXiao Guangrong  * It gets the size of Namespace Label data area and the max data size
6555797dcdcSXiao Guangrong  * that Get/Set Namespace Label Data functions can transfer.
6565797dcdcSXiao Guangrong  */
6575797dcdcSXiao Guangrong static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm, hwaddr dsm_mem_addr)
6585797dcdcSXiao Guangrong {
6595797dcdcSXiao Guangrong     NvdimmFuncGetLabelSizeOut label_size_out = {
6605797dcdcSXiao Guangrong         .len = cpu_to_le32(sizeof(label_size_out)),
6615797dcdcSXiao Guangrong     };
6625797dcdcSXiao Guangrong     uint32_t label_size, mxfer;
6635797dcdcSXiao Guangrong 
6645797dcdcSXiao Guangrong     label_size = nvdimm->label_size;
6655797dcdcSXiao Guangrong     mxfer = nvdimm_get_max_xfer_label_size();
6665797dcdcSXiao Guangrong 
66788eed198SXinhao Zhang     nvdimm_debug("label_size 0x%x, max_xfer 0x%x.\n", label_size, mxfer);
6685797dcdcSXiao Guangrong 
669c2fa3075SXiao Guangrong     label_size_out.func_ret_status = cpu_to_le32(NVDIMM_DSM_RET_STATUS_SUCCESS);
6705797dcdcSXiao Guangrong     label_size_out.label_size = cpu_to_le32(label_size);
6715797dcdcSXiao Guangrong     label_size_out.max_xfer = cpu_to_le32(mxfer);
6725797dcdcSXiao Guangrong 
6735797dcdcSXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, &label_size_out,
6745797dcdcSXiao Guangrong                               sizeof(label_size_out));
6755797dcdcSXiao Guangrong }
6765797dcdcSXiao Guangrong 
6772b9e57fcSXiao Guangrong static uint32_t nvdimm_rw_label_data_check(NVDIMMDevice *nvdimm,
6782b9e57fcSXiao Guangrong                                            uint32_t offset, uint32_t length)
6792b9e57fcSXiao Guangrong {
680c2fa3075SXiao Guangrong     uint32_t ret = NVDIMM_DSM_RET_STATUS_INVALID;
6812b9e57fcSXiao Guangrong 
6822b9e57fcSXiao Guangrong     if (offset + length < offset) {
68388eed198SXinhao Zhang         nvdimm_debug("offset 0x%x + length 0x%x is overflow.\n", offset,
6842b9e57fcSXiao Guangrong                      length);
6852b9e57fcSXiao Guangrong         return ret;
6862b9e57fcSXiao Guangrong     }
6872b9e57fcSXiao Guangrong 
6882b9e57fcSXiao Guangrong     if (nvdimm->label_size < offset + length) {
68988eed198SXinhao Zhang         nvdimm_debug("position 0x%x is beyond label data (len = %" PRIx64 ").\n",
6902b9e57fcSXiao Guangrong                      offset + length, nvdimm->label_size);
6912b9e57fcSXiao Guangrong         return ret;
6922b9e57fcSXiao Guangrong     }
6932b9e57fcSXiao Guangrong 
6942b9e57fcSXiao Guangrong     if (length > nvdimm_get_max_xfer_label_size()) {
69588eed198SXinhao Zhang         nvdimm_debug("length (0x%x) is larger than max_xfer (0x%x).\n",
6962b9e57fcSXiao Guangrong                      length, nvdimm_get_max_xfer_label_size());
6972b9e57fcSXiao Guangrong         return ret;
6982b9e57fcSXiao Guangrong     }
6992b9e57fcSXiao Guangrong 
700c2fa3075SXiao Guangrong     return NVDIMM_DSM_RET_STATUS_SUCCESS;
7012b9e57fcSXiao Guangrong }
7022b9e57fcSXiao Guangrong 
7032b9e57fcSXiao Guangrong /*
7042b9e57fcSXiao Guangrong  * DSM Spec Rev1 4.5 Get Namespace Label Data (Function Index 5).
7052b9e57fcSXiao Guangrong  */
7062b9e57fcSXiao Guangrong static void nvdimm_dsm_get_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
7072b9e57fcSXiao Guangrong                                       hwaddr dsm_mem_addr)
7082b9e57fcSXiao Guangrong {
7092b9e57fcSXiao Guangrong     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
7102b9e57fcSXiao Guangrong     NvdimmFuncGetLabelDataIn *get_label_data;
7112b9e57fcSXiao Guangrong     NvdimmFuncGetLabelDataOut *get_label_data_out;
7122b9e57fcSXiao Guangrong     uint32_t status;
7132b9e57fcSXiao Guangrong     int size;
7142b9e57fcSXiao Guangrong 
7152b9e57fcSXiao Guangrong     get_label_data = (NvdimmFuncGetLabelDataIn *)in->arg3;
716435cc3e4SPeter Maydell     get_label_data->offset = le32_to_cpu(get_label_data->offset);
717435cc3e4SPeter Maydell     get_label_data->length = le32_to_cpu(get_label_data->length);
7182b9e57fcSXiao Guangrong 
71988eed198SXinhao Zhang     nvdimm_debug("Read Label Data: offset 0x%x length 0x%x.\n",
7202b9e57fcSXiao Guangrong                  get_label_data->offset, get_label_data->length);
7212b9e57fcSXiao Guangrong 
7222b9e57fcSXiao Guangrong     status = nvdimm_rw_label_data_check(nvdimm, get_label_data->offset,
7232b9e57fcSXiao Guangrong                                         get_label_data->length);
724c2fa3075SXiao Guangrong     if (status != NVDIMM_DSM_RET_STATUS_SUCCESS) {
7252b9e57fcSXiao Guangrong         nvdimm_dsm_no_payload(status, dsm_mem_addr);
7262b9e57fcSXiao Guangrong         return;
7272b9e57fcSXiao Guangrong     }
7282b9e57fcSXiao Guangrong 
7292b9e57fcSXiao Guangrong     size = sizeof(*get_label_data_out) + get_label_data->length;
730cb88ebd7SXiao Guangrong     assert(size <= NVDIMM_DSM_MEMORY_SIZE);
7312b9e57fcSXiao Guangrong     get_label_data_out = g_malloc(size);
7322b9e57fcSXiao Guangrong 
7332b9e57fcSXiao Guangrong     get_label_data_out->len = cpu_to_le32(size);
734c2fa3075SXiao Guangrong     get_label_data_out->func_ret_status =
735c2fa3075SXiao Guangrong                             cpu_to_le32(NVDIMM_DSM_RET_STATUS_SUCCESS);
7362b9e57fcSXiao Guangrong     nvc->read_label_data(nvdimm, get_label_data_out->out_buf,
7372b9e57fcSXiao Guangrong                          get_label_data->length, get_label_data->offset);
7382b9e57fcSXiao Guangrong 
7392b9e57fcSXiao Guangrong     cpu_physical_memory_write(dsm_mem_addr, get_label_data_out, size);
7402b9e57fcSXiao Guangrong     g_free(get_label_data_out);
7412b9e57fcSXiao Guangrong }
7422b9e57fcSXiao Guangrong 
74314e44198SXiao Guangrong /*
74414e44198SXiao Guangrong  * DSM Spec Rev1 4.6 Set Namespace Label Data (Function Index 6).
74514e44198SXiao Guangrong  */
74614e44198SXiao Guangrong static void nvdimm_dsm_set_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
74714e44198SXiao Guangrong                                       hwaddr dsm_mem_addr)
74814e44198SXiao Guangrong {
74914e44198SXiao Guangrong     NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
75014e44198SXiao Guangrong     NvdimmFuncSetLabelDataIn *set_label_data;
75114e44198SXiao Guangrong     uint32_t status;
75214e44198SXiao Guangrong 
75314e44198SXiao Guangrong     set_label_data = (NvdimmFuncSetLabelDataIn *)in->arg3;
75414e44198SXiao Guangrong 
755435cc3e4SPeter Maydell     set_label_data->offset = le32_to_cpu(set_label_data->offset);
756435cc3e4SPeter Maydell     set_label_data->length = le32_to_cpu(set_label_data->length);
75714e44198SXiao Guangrong 
75888eed198SXinhao Zhang     nvdimm_debug("Write Label Data: offset 0x%x length 0x%x.\n",
75914e44198SXiao Guangrong                  set_label_data->offset, set_label_data->length);
76014e44198SXiao Guangrong 
76114e44198SXiao Guangrong     status = nvdimm_rw_label_data_check(nvdimm, set_label_data->offset,
76214e44198SXiao Guangrong                                         set_label_data->length);
763c2fa3075SXiao Guangrong     if (status != NVDIMM_DSM_RET_STATUS_SUCCESS) {
76414e44198SXiao Guangrong         nvdimm_dsm_no_payload(status, dsm_mem_addr);
76514e44198SXiao Guangrong         return;
76614e44198SXiao Guangrong     }
76714e44198SXiao Guangrong 
768cb88ebd7SXiao Guangrong     assert(offsetof(NvdimmDsmIn, arg3) + sizeof(*set_label_data) +
769cb88ebd7SXiao Guangrong                     set_label_data->length <= NVDIMM_DSM_MEMORY_SIZE);
77014e44198SXiao Guangrong 
77114e44198SXiao Guangrong     nvc->write_label_data(nvdimm, set_label_data->in_buf,
77214e44198SXiao Guangrong                           set_label_data->length, set_label_data->offset);
773c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_SUCCESS, dsm_mem_addr);
77414e44198SXiao Guangrong }
77514e44198SXiao Guangrong 
776189f4d56SXiao Guangrong static void nvdimm_dsm_device(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
777189f4d56SXiao Guangrong {
7785797dcdcSXiao Guangrong     NVDIMMDevice *nvdimm = nvdimm_get_device_by_handle(in->handle);
7795797dcdcSXiao Guangrong 
780189f4d56SXiao Guangrong     /* See the comments in nvdimm_dsm_root(). */
781189f4d56SXiao Guangrong     if (!in->function) {
7825797dcdcSXiao Guangrong         uint32_t supported_func = 0;
7835797dcdcSXiao Guangrong 
7845797dcdcSXiao Guangrong         if (nvdimm && nvdimm->label_size) {
7855797dcdcSXiao Guangrong             supported_func |= 0x1 /* Bit 0 indicates whether there is
7865797dcdcSXiao Guangrong                                      support for any functions other
7875797dcdcSXiao Guangrong                                      than function 0. */ |
7882b9e57fcSXiao Guangrong                               1 << 4 /* Get Namespace Label Size */ |
78914e44198SXiao Guangrong                               1 << 5 /* Get Namespace Label Data */ |
79014e44198SXiao Guangrong                               1 << 6 /* Set Namespace Label Data */;
7915797dcdcSXiao Guangrong         }
7925797dcdcSXiao Guangrong         nvdimm_dsm_function0(supported_func, dsm_mem_addr);
793189f4d56SXiao Guangrong         return;
794189f4d56SXiao Guangrong     }
795189f4d56SXiao Guangrong 
7965797dcdcSXiao Guangrong     if (!nvdimm) {
797c2fa3075SXiao Guangrong         nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_NOMEMDEV,
7985797dcdcSXiao Guangrong                               dsm_mem_addr);
7995797dcdcSXiao Guangrong         return;
8005797dcdcSXiao Guangrong     }
8015797dcdcSXiao Guangrong 
8025797dcdcSXiao Guangrong     /* Encode DSM function according to DSM Spec Rev1. */
8035797dcdcSXiao Guangrong     switch (in->function) {
8045797dcdcSXiao Guangrong     case 4 /* Get Namespace Label Size */:
8055797dcdcSXiao Guangrong         if (nvdimm->label_size) {
8065797dcdcSXiao Guangrong             nvdimm_dsm_label_size(nvdimm, dsm_mem_addr);
8075797dcdcSXiao Guangrong             return;
8085797dcdcSXiao Guangrong         }
8095797dcdcSXiao Guangrong         break;
8102b9e57fcSXiao Guangrong     case 5 /* Get Namespace Label Data */:
8112b9e57fcSXiao Guangrong         if (nvdimm->label_size) {
8122b9e57fcSXiao Guangrong             nvdimm_dsm_get_label_data(nvdimm, in, dsm_mem_addr);
8132b9e57fcSXiao Guangrong             return;
8142b9e57fcSXiao Guangrong         }
8152b9e57fcSXiao Guangrong         break;
81614e44198SXiao Guangrong     case 0x6 /* Set Namespace Label Data */:
81714e44198SXiao Guangrong         if (nvdimm->label_size) {
81814e44198SXiao Guangrong             nvdimm_dsm_set_label_data(nvdimm, in, dsm_mem_addr);
81914e44198SXiao Guangrong             return;
82014e44198SXiao Guangrong         }
82114e44198SXiao Guangrong         break;
8225797dcdcSXiao Guangrong     }
8235797dcdcSXiao Guangrong 
824c2fa3075SXiao Guangrong     nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
825189f4d56SXiao Guangrong }
826189f4d56SXiao Guangrong 
8275fe79386SXiao Guangrong static uint64_t
8285fe79386SXiao Guangrong nvdimm_dsm_read(void *opaque, hwaddr addr, unsigned size)
8295fe79386SXiao Guangrong {
830f7df22deSXiao Guangrong     nvdimm_debug("BUG: we never read _DSM IO Port.\n");
8315fe79386SXiao Guangrong     return 0;
8325fe79386SXiao Guangrong }
8335fe79386SXiao Guangrong 
8345fe79386SXiao Guangrong static void
8355fe79386SXiao Guangrong nvdimm_dsm_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
8365fe79386SXiao Guangrong {
837c1404bdeSEric Auger     NVDIMMState *state = opaque;
838f7df22deSXiao Guangrong     NvdimmDsmIn *in;
839f7df22deSXiao Guangrong     hwaddr dsm_mem_addr = val;
840f7df22deSXiao Guangrong 
84188eed198SXinhao Zhang     nvdimm_debug("dsm memory address 0x%" HWADDR_PRIx ".\n", dsm_mem_addr);
842f7df22deSXiao Guangrong 
843f7df22deSXiao Guangrong     /*
844f7df22deSXiao Guangrong      * The DSM memory is mapped to guest address space so an evil guest
845f7df22deSXiao Guangrong      * can change its content while we are doing DSM emulation. Avoid
846f7df22deSXiao Guangrong      * this by copying DSM memory to QEMU local memory.
847f7df22deSXiao Guangrong      */
84835c5a52dSPaolo Bonzini     in = g_new(NvdimmDsmIn, 1);
84935c5a52dSPaolo Bonzini     cpu_physical_memory_read(dsm_mem_addr, in, sizeof(*in));
850f7df22deSXiao Guangrong 
851435cc3e4SPeter Maydell     in->revision = le32_to_cpu(in->revision);
852435cc3e4SPeter Maydell     in->function = le32_to_cpu(in->function);
853435cc3e4SPeter Maydell     in->handle = le32_to_cpu(in->handle);
854f7df22deSXiao Guangrong 
85588eed198SXinhao Zhang     nvdimm_debug("Revision 0x%x Handler 0x%x Function 0x%x.\n", in->revision,
856f7df22deSXiao Guangrong                  in->handle, in->function);
857f7df22deSXiao Guangrong 
858d15fc53fSXiao Guangrong     if (in->revision != 0x1 /* Currently we only support DSM Spec Rev1. */) {
85988eed198SXinhao Zhang         nvdimm_debug("Revision 0x%x is not supported, expect 0x%x.\n",
860d15fc53fSXiao Guangrong                      in->revision, 0x1);
861c2fa3075SXiao Guangrong         nvdimm_dsm_no_payload(NVDIMM_DSM_RET_STATUS_UNSUPPORT, dsm_mem_addr);
862d15fc53fSXiao Guangrong         goto exit;
863d15fc53fSXiao Guangrong     }
864d15fc53fSXiao Guangrong 
865806864d9SXiao Guangrong     if (in->handle == NVDIMM_QEMU_RSVD_HANDLE_ROOT) {
8665a33db78SXiao Guangrong         nvdimm_dsm_handle_reserved_root_method(state, in, dsm_mem_addr);
867806864d9SXiao Guangrong         goto exit;
868806864d9SXiao Guangrong     }
869806864d9SXiao Guangrong 
870189f4d56SXiao Guangrong      /* Handle 0 is reserved for NVDIMM Root Device. */
871189f4d56SXiao Guangrong     if (!in->handle) {
872189f4d56SXiao Guangrong         nvdimm_dsm_root(in, dsm_mem_addr);
873189f4d56SXiao Guangrong         goto exit;
874f7df22deSXiao Guangrong     }
875f7df22deSXiao Guangrong 
876189f4d56SXiao Guangrong     nvdimm_dsm_device(in, dsm_mem_addr);
877189f4d56SXiao Guangrong 
878189f4d56SXiao Guangrong exit:
879f7df22deSXiao Guangrong     g_free(in);
8805fe79386SXiao Guangrong }
8815fe79386SXiao Guangrong 
8825fe79386SXiao Guangrong static const MemoryRegionOps nvdimm_dsm_ops = {
8835fe79386SXiao Guangrong     .read = nvdimm_dsm_read,
8845fe79386SXiao Guangrong     .write = nvdimm_dsm_write,
8855fe79386SXiao Guangrong     .endianness = DEVICE_LITTLE_ENDIAN,
8865fe79386SXiao Guangrong     .valid = {
8875fe79386SXiao Guangrong         .min_access_size = 4,
8885fe79386SXiao Guangrong         .max_access_size = 4,
8895fe79386SXiao Guangrong     },
8905fe79386SXiao Guangrong };
8915fe79386SXiao Guangrong 
89275f27498SXiao Guangrong void nvdimm_acpi_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev)
89375f27498SXiao Guangrong {
89475f27498SXiao Guangrong     if (dev->hotplugged) {
89575f27498SXiao Guangrong         acpi_send_event(DEVICE(hotplug_dev), ACPI_NVDIMM_HOTPLUG_STATUS);
89675f27498SXiao Guangrong     }
89775f27498SXiao Guangrong }
89875f27498SXiao Guangrong 
899c1404bdeSEric Auger void nvdimm_init_acpi_state(NVDIMMState *state, MemoryRegion *io,
9005c94b826SKwangwoo Lee                             struct AcpiGenericAddress dsm_io,
9015fe79386SXiao Guangrong                             FWCfgState *fw_cfg, Object *owner)
9025fe79386SXiao Guangrong {
9035c94b826SKwangwoo Lee     state->dsm_io = dsm_io;
9045fe79386SXiao Guangrong     memory_region_init_io(&state->io_mr, owner, &nvdimm_dsm_ops, state,
9055c94b826SKwangwoo Lee                           "nvdimm-acpi-io", dsm_io.bit_width >> 3);
9065c94b826SKwangwoo Lee     memory_region_add_subregion(io, dsm_io.address, &state->io_mr);
9075fe79386SXiao Guangrong 
9085fe79386SXiao Guangrong     state->dsm_mem = g_array_new(false, true /* clear */, 1);
90935c5a52dSPaolo Bonzini     acpi_data_push(state->dsm_mem, sizeof(NvdimmDsmIn));
9105fe79386SXiao Guangrong     fw_cfg_add_file(fw_cfg, NVDIMM_DSM_MEM_FILE, state->dsm_mem->data,
9115fe79386SXiao Guangrong                     state->dsm_mem->len);
91275b0713eSXiao Guangrong 
91375b0713eSXiao Guangrong     nvdimm_init_fit_buffer(&state->fit_buf);
9145fe79386SXiao Guangrong }
9155fe79386SXiao Guangrong 
91677286395SXiao Guangrong #define NVDIMM_COMMON_DSM       "NCAL"
917b9951413SXiao Guangrong #define NVDIMM_ACPI_MEM_ADDR    "MEMA"
91877286395SXiao Guangrong 
9193ae66c45SXiao Guangrong #define NVDIMM_DSM_MEMORY       "NRAM"
9203ae66c45SXiao Guangrong #define NVDIMM_DSM_IOPORT       "NPIO"
9213ae66c45SXiao Guangrong 
9223ae66c45SXiao Guangrong #define NVDIMM_DSM_NOTIFY       "NTFI"
9233ae66c45SXiao Guangrong #define NVDIMM_DSM_HANDLE       "HDLE"
9243ae66c45SXiao Guangrong #define NVDIMM_DSM_REVISION     "REVS"
9253ae66c45SXiao Guangrong #define NVDIMM_DSM_FUNCTION     "FUNC"
9263ae66c45SXiao Guangrong #define NVDIMM_DSM_ARG3         "FARG"
9273ae66c45SXiao Guangrong 
9283ae66c45SXiao Guangrong #define NVDIMM_DSM_OUT_BUF_SIZE "RLEN"
9293ae66c45SXiao Guangrong #define NVDIMM_DSM_OUT_BUF      "ODAT"
9303ae66c45SXiao Guangrong 
931806864d9SXiao Guangrong #define NVDIMM_DSM_RFIT_STATUS  "RSTA"
932806864d9SXiao Guangrong 
933806864d9SXiao Guangrong #define NVDIMM_QEMU_RSVD_UUID   "648B9CF2-CDA1-4312-8AD9-49C4AF32BD62"
934806864d9SXiao Guangrong 
9355c94b826SKwangwoo Lee static void nvdimm_build_common_dsm(Aml *dev,
9365c94b826SKwangwoo Lee                                     NVDIMMState *nvdimm_state)
93777286395SXiao Guangrong {
938806864d9SXiao Guangrong     Aml *method, *ifctx, *function, *handle, *uuid, *dsm_mem, *elsectx2;
93990623ebfSXiao Guangrong     Aml *elsectx, *unsupport, *unpatched, *expected_uuid, *uuid_invalid;
940fa1a448dSXiao Guangrong     Aml *pckg, *pckg_index, *pckg_buf, *field, *dsm_out_buf, *dsm_out_buf_size;
94171b0269aSShameer Kolothum     Aml *whilectx, *offset;
94277286395SXiao Guangrong     uint8_t byte_list[1];
9435c94b826SKwangwoo Lee     AmlRegionSpace rs;
94477286395SXiao Guangrong 
945732b530cSXiao Guangrong     method = aml_method(NVDIMM_COMMON_DSM, 5, AML_SERIALIZED);
94690623ebfSXiao Guangrong     uuid = aml_arg(0);
94777286395SXiao Guangrong     function = aml_arg(2);
94890623ebfSXiao Guangrong     handle = aml_arg(4);
949c0b3b863SXiao Guangrong     dsm_mem = aml_local(6);
95048bee476SXiao Guangrong     dsm_out_buf = aml_local(7);
951c0b3b863SXiao Guangrong 
952c0b3b863SXiao Guangrong     aml_append(method, aml_store(aml_name(NVDIMM_ACPI_MEM_ADDR), dsm_mem));
953c0b3b863SXiao Guangrong 
9545c94b826SKwangwoo Lee     if (nvdimm_state->dsm_io.space_id == AML_AS_SYSTEM_IO) {
9555c94b826SKwangwoo Lee         rs = AML_SYSTEM_IO;
9565c94b826SKwangwoo Lee     } else {
9575c94b826SKwangwoo Lee         rs = AML_SYSTEM_MEMORY;
9585c94b826SKwangwoo Lee     }
9595c94b826SKwangwoo Lee 
960c0b3b863SXiao Guangrong     /* map DSM memory and IO into ACPI namespace. */
9615c94b826SKwangwoo Lee     aml_append(method, aml_operation_region(NVDIMM_DSM_IOPORT, rs,
9625c94b826SKwangwoo Lee                aml_int(nvdimm_state->dsm_io.address),
9635c94b826SKwangwoo Lee                nvdimm_state->dsm_io.bit_width >> 3));
9643ae66c45SXiao Guangrong     aml_append(method, aml_operation_region(NVDIMM_DSM_MEMORY,
9653ae66c45SXiao Guangrong                AML_SYSTEM_MEMORY, dsm_mem, sizeof(NvdimmDsmIn)));
966c0b3b863SXiao Guangrong 
967c0b3b863SXiao Guangrong     /*
968c0b3b863SXiao Guangrong      * DSM notifier:
9693ae66c45SXiao Guangrong      * NVDIMM_DSM_NOTIFY: write the address of DSM memory and notify QEMU to
9703ae66c45SXiao Guangrong      *                    emulate the access.
971c0b3b863SXiao Guangrong      *
972c0b3b863SXiao Guangrong      * It is the IO port so that accessing them will cause VM-exit, the
973c0b3b863SXiao Guangrong      * control will be transferred to QEMU.
974c0b3b863SXiao Guangrong      */
9753ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_IOPORT, AML_DWORD_ACC, AML_NOLOCK,
9763ae66c45SXiao Guangrong                       AML_PRESERVE);
9773ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_NOTIFY,
9785c94b826SKwangwoo Lee                nvdimm_state->dsm_io.bit_width));
979c0b3b863SXiao Guangrong     aml_append(method, field);
980c0b3b863SXiao Guangrong 
981c0b3b863SXiao Guangrong     /*
982c0b3b863SXiao Guangrong      * DSM input:
9833ae66c45SXiao Guangrong      * NVDIMM_DSM_HANDLE: store device's handle, it's zero if the _DSM call
9843ae66c45SXiao Guangrong      *                    happens on NVDIMM Root Device.
9853ae66c45SXiao Guangrong      * NVDIMM_DSM_REVISION: store the Arg1 of _DSM call.
9863ae66c45SXiao Guangrong      * NVDIMM_DSM_FUNCTION: store the Arg2 of _DSM call.
9873ae66c45SXiao Guangrong      * NVDIMM_DSM_ARG3: store the Arg3 of _DSM call which is a Package
9883ae66c45SXiao Guangrong      *                  containing function-specific arguments.
989c0b3b863SXiao Guangrong      *
990c0b3b863SXiao Guangrong      * They are RAM mapping on host so that these accesses never cause
991c0b3b863SXiao Guangrong      * VM-EXIT.
992c0b3b863SXiao Guangrong      */
9933ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
9943ae66c45SXiao Guangrong                       AML_PRESERVE);
9953ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_HANDLE,
996c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, handle)) * BITS_PER_BYTE));
9973ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_REVISION,
998c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, revision)) * BITS_PER_BYTE));
9993ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_FUNCTION,
1000c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmIn, function)) * BITS_PER_BYTE));
10013ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_ARG3,
1002c0b3b863SXiao Guangrong          (sizeof(NvdimmDsmIn) - offsetof(NvdimmDsmIn, arg3)) * BITS_PER_BYTE));
1003c0b3b863SXiao Guangrong     aml_append(method, field);
1004c0b3b863SXiao Guangrong 
1005c0b3b863SXiao Guangrong     /*
1006c0b3b863SXiao Guangrong      * DSM output:
10073ae66c45SXiao Guangrong      * NVDIMM_DSM_OUT_BUF_SIZE: the size of the buffer filled by QEMU.
10083ae66c45SXiao Guangrong      * NVDIMM_DSM_OUT_BUF: the buffer QEMU uses to store the result.
1009c0b3b863SXiao Guangrong      *
1010c0b3b863SXiao Guangrong      * Since the page is reused by both input and out, the input data
1011c0b3b863SXiao Guangrong      * will be lost after storing new result into ODAT so we should fetch
1012c0b3b863SXiao Guangrong      * all the input data before writing the result.
1013c0b3b863SXiao Guangrong      */
10143ae66c45SXiao Guangrong     field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
10153ae66c45SXiao Guangrong                       AML_PRESERVE);
10163ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF_SIZE,
1017c0b3b863SXiao Guangrong                sizeof(typeof_field(NvdimmDsmOut, len)) * BITS_PER_BYTE));
10183ae66c45SXiao Guangrong     aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF,
1019c0b3b863SXiao Guangrong        (sizeof(NvdimmDsmOut) - offsetof(NvdimmDsmOut, data)) * BITS_PER_BYTE));
1020c0b3b863SXiao Guangrong     aml_append(method, field);
102118c440e1SXiao Guangrong 
102218c440e1SXiao Guangrong     /*
102318c440e1SXiao Guangrong      * do not support any method if DSM memory address has not been
102418c440e1SXiao Guangrong      * patched.
102518c440e1SXiao Guangrong      */
102690623ebfSXiao Guangrong     unpatched = aml_equal(dsm_mem, aml_int(0x0));
102790623ebfSXiao Guangrong 
102890623ebfSXiao Guangrong     expected_uuid = aml_local(0);
102990623ebfSXiao Guangrong 
103090623ebfSXiao Guangrong     ifctx = aml_if(aml_equal(handle, aml_int(0x0)));
103190623ebfSXiao Guangrong     aml_append(ifctx, aml_store(
103290623ebfSXiao Guangrong                aml_touuid("2F10E7A4-9E91-11E4-89D3-123B93F75CBA")
103390623ebfSXiao Guangrong                /* UUID for NVDIMM Root Device */, expected_uuid));
103490623ebfSXiao Guangrong     aml_append(method, ifctx);
103590623ebfSXiao Guangrong     elsectx = aml_else();
1036806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(handle, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT)));
1037806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_touuid(NVDIMM_QEMU_RSVD_UUID
1038806864d9SXiao Guangrong                /* UUID for QEMU internal use */), expected_uuid));
1039806864d9SXiao Guangrong     aml_append(elsectx, ifctx);
1040806864d9SXiao Guangrong     elsectx2 = aml_else();
1041806864d9SXiao Guangrong     aml_append(elsectx2, aml_store(
104290623ebfSXiao Guangrong                aml_touuid("4309AC30-0D11-11E4-9191-0800200C9A66")
104390623ebfSXiao Guangrong                /* UUID for NVDIMM Devices */, expected_uuid));
1044806864d9SXiao Guangrong     aml_append(elsectx, elsectx2);
104590623ebfSXiao Guangrong     aml_append(method, elsectx);
104690623ebfSXiao Guangrong 
104790623ebfSXiao Guangrong     uuid_invalid = aml_lnot(aml_equal(uuid, expected_uuid));
104890623ebfSXiao Guangrong 
104990623ebfSXiao Guangrong     unsupport = aml_if(aml_or(unpatched, uuid_invalid, NULL));
105077286395SXiao Guangrong 
105177286395SXiao Guangrong     /*
105277286395SXiao Guangrong      * function 0 is called to inquire what functions are supported by
105377286395SXiao Guangrong      * OSPM
105477286395SXiao Guangrong      */
105577286395SXiao Guangrong     ifctx = aml_if(aml_equal(function, aml_int(0)));
105677286395SXiao Guangrong     byte_list[0] = 0 /* No function Supported */;
105777286395SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(1, byte_list)));
105890623ebfSXiao Guangrong     aml_append(unsupport, ifctx);
105977286395SXiao Guangrong 
106077286395SXiao Guangrong     /* No function is supported yet. */
1061c2fa3075SXiao Guangrong     byte_list[0] = NVDIMM_DSM_RET_STATUS_UNSUPPORT;
106290623ebfSXiao Guangrong     aml_append(unsupport, aml_return(aml_buffer(1, byte_list)));
106390623ebfSXiao Guangrong     aml_append(method, unsupport);
106477286395SXiao Guangrong 
106518c440e1SXiao Guangrong     /*
106618c440e1SXiao Guangrong      * The HDLE indicates the DSM function is issued from which device,
1067732b530cSXiao Guangrong      * it reserves 0 for root device and is the handle for NVDIMM devices.
1068732b530cSXiao Guangrong      * See the comments in nvdimm_slot_to_handle().
106918c440e1SXiao Guangrong      */
10703ae66c45SXiao Guangrong     aml_append(method, aml_store(handle, aml_name(NVDIMM_DSM_HANDLE)));
10713ae66c45SXiao Guangrong     aml_append(method, aml_store(aml_arg(1), aml_name(NVDIMM_DSM_REVISION)));
1072ac265cacSWei Yang     aml_append(method, aml_store(function, aml_name(NVDIMM_DSM_FUNCTION)));
107318c440e1SXiao Guangrong 
107418c440e1SXiao Guangrong     /*
10754568c948SXiao Guangrong      * The fourth parameter (Arg3) of _DSM is a package which contains
10764568c948SXiao Guangrong      * a buffer, the layout of the buffer is specified by UUID (Arg0),
10774568c948SXiao Guangrong      * Revision ID (Arg1) and Function Index (Arg2) which are documented
10784568c948SXiao Guangrong      * in the DSM Spec.
10794568c948SXiao Guangrong      */
10804568c948SXiao Guangrong     pckg = aml_arg(3);
10814568c948SXiao Guangrong     ifctx = aml_if(aml_and(aml_equal(aml_object_type(pckg),
10824568c948SXiao Guangrong                    aml_int(4 /* Package */)) /* It is a Package? */,
10834568c948SXiao Guangrong                    aml_equal(aml_sizeof(pckg), aml_int(1)) /* 1 element? */,
10844568c948SXiao Guangrong                    NULL));
10854568c948SXiao Guangrong 
10864568c948SXiao Guangrong     pckg_index = aml_local(2);
10874568c948SXiao Guangrong     pckg_buf = aml_local(3);
10884568c948SXiao Guangrong     aml_append(ifctx, aml_store(aml_index(pckg, aml_int(0)), pckg_index));
10894568c948SXiao Guangrong     aml_append(ifctx, aml_store(aml_derefof(pckg_index), pckg_buf));
10903ae66c45SXiao Guangrong     aml_append(ifctx, aml_store(pckg_buf, aml_name(NVDIMM_DSM_ARG3)));
10914568c948SXiao Guangrong     aml_append(method, ifctx);
10924568c948SXiao Guangrong 
10934568c948SXiao Guangrong     /*
109418c440e1SXiao Guangrong      * tell QEMU about the real address of DSM memory, then QEMU
109518c440e1SXiao Guangrong      * gets the control and fills the result in DSM memory.
109618c440e1SXiao Guangrong      */
10973ae66c45SXiao Guangrong     aml_append(method, aml_store(dsm_mem, aml_name(NVDIMM_DSM_NOTIFY)));
109818c440e1SXiao Guangrong 
1099fa1a448dSXiao Guangrong     dsm_out_buf_size = aml_local(1);
1100d51d1d7eSXiao Guangrong     /* RLEN is not included in the payload returned to guest. */
11013ae66c45SXiao Guangrong     aml_append(method, aml_subtract(aml_name(NVDIMM_DSM_OUT_BUF_SIZE),
11023ae66c45SXiao Guangrong                aml_int(4), dsm_out_buf_size));
110371b0269aSShameer Kolothum 
110471b0269aSShameer Kolothum     /*
110571b0269aSShameer Kolothum      * As per ACPI spec 6.3, Table 19-419 Object Conversion Rules, if
110671b0269aSShameer Kolothum      * the Buffer Field <= to the size of an Integer (in bits), it will
110771b0269aSShameer Kolothum      * be treated as an integer. Moreover, the integer size depends on
110871b0269aSShameer Kolothum      * DSDT tables revision number. If revision number is < 2, integer
110971b0269aSShameer Kolothum      * size is 32 bits, otherwise it is 64 bits.
111071b0269aSShameer Kolothum      * Because of this CreateField() canot be used if RLEN < Integer Size.
111171b0269aSShameer Kolothum      *
111271b0269aSShameer Kolothum      * Also please note that APCI ASL operator SizeOf() doesn't support
111371b0269aSShameer Kolothum      * Integer and there isn't any other way to figure out the Integer
111471b0269aSShameer Kolothum      * size. Hence we assume 8 byte as Integer size and if RLEN < 8 bytes,
111571b0269aSShameer Kolothum      * build dsm_out_buf byte by byte.
111671b0269aSShameer Kolothum      */
111771b0269aSShameer Kolothum     ifctx = aml_if(aml_lless(dsm_out_buf_size, aml_int(8)));
111871b0269aSShameer Kolothum     offset = aml_local(2);
111971b0269aSShameer Kolothum     aml_append(ifctx, aml_store(aml_int(0), offset));
112071b0269aSShameer Kolothum     aml_append(ifctx, aml_name_decl("TBUF", aml_buffer(1, NULL)));
112171b0269aSShameer Kolothum     aml_append(ifctx, aml_store(aml_buffer(0, NULL), dsm_out_buf));
112271b0269aSShameer Kolothum 
112371b0269aSShameer Kolothum     whilectx = aml_while(aml_lless(offset, dsm_out_buf_size));
112471b0269aSShameer Kolothum     /* Copy 1 byte at offset from ODAT to temporary buffer(TBUF). */
112571b0269aSShameer Kolothum     aml_append(whilectx, aml_store(aml_derefof(aml_index(
112671b0269aSShameer Kolothum                                    aml_name(NVDIMM_DSM_OUT_BUF), offset)),
112771b0269aSShameer Kolothum                                    aml_index(aml_name("TBUF"), aml_int(0))));
112871b0269aSShameer Kolothum     aml_append(whilectx, aml_concatenate(dsm_out_buf, aml_name("TBUF"),
112971b0269aSShameer Kolothum                                          dsm_out_buf));
113071b0269aSShameer Kolothum     aml_append(whilectx, aml_increment(offset));
113171b0269aSShameer Kolothum     aml_append(ifctx, whilectx);
113271b0269aSShameer Kolothum 
113371b0269aSShameer Kolothum     aml_append(ifctx, aml_return(dsm_out_buf));
113471b0269aSShameer Kolothum     aml_append(method, ifctx);
113571b0269aSShameer Kolothum 
113671b0269aSShameer Kolothum     /* If RLEN >= Integer size, just use CreateField() operator */
1137fa1a448dSXiao Guangrong     aml_append(method, aml_store(aml_shiftleft(dsm_out_buf_size, aml_int(3)),
1138fa1a448dSXiao Guangrong                                  dsm_out_buf_size));
11393ae66c45SXiao Guangrong     aml_append(method, aml_create_field(aml_name(NVDIMM_DSM_OUT_BUF),
11403ae66c45SXiao Guangrong                aml_int(0), dsm_out_buf_size, "OBUF"));
114171b0269aSShameer Kolothum     aml_append(method, aml_return(aml_name("OBUF")));
114271b0269aSShameer Kolothum 
114377286395SXiao Guangrong     aml_append(dev, method);
114477286395SXiao Guangrong }
114577286395SXiao Guangrong 
1146732b530cSXiao Guangrong static void nvdimm_build_device_dsm(Aml *dev, uint32_t handle)
114777286395SXiao Guangrong {
114877286395SXiao Guangrong     Aml *method;
114977286395SXiao Guangrong 
115077286395SXiao Guangrong     method = aml_method("_DSM", 4, AML_NOTSERIALIZED);
1151732b530cSXiao Guangrong     aml_append(method, aml_return(aml_call5(NVDIMM_COMMON_DSM, aml_arg(0),
1152732b530cSXiao Guangrong                                   aml_arg(1), aml_arg(2), aml_arg(3),
1153732b530cSXiao Guangrong                                   aml_int(handle))));
115477286395SXiao Guangrong     aml_append(dev, method);
115577286395SXiao Guangrong }
115677286395SXiao Guangrong 
1157806864d9SXiao Guangrong static void nvdimm_build_fit(Aml *dev)
1158806864d9SXiao Guangrong {
1159806864d9SXiao Guangrong     Aml *method, *pkg, *buf, *buf_size, *offset, *call_result;
1160806864d9SXiao Guangrong     Aml *whilectx, *ifcond, *ifctx, *elsectx, *fit;
1161806864d9SXiao Guangrong 
1162806864d9SXiao Guangrong     buf = aml_local(0);
1163806864d9SXiao Guangrong     buf_size = aml_local(1);
1164806864d9SXiao Guangrong     fit = aml_local(2);
1165806864d9SXiao Guangrong 
1166aef056c1SXiao Guangrong     aml_append(dev, aml_name_decl(NVDIMM_DSM_RFIT_STATUS, aml_int(0)));
1167806864d9SXiao Guangrong 
1168806864d9SXiao Guangrong     /* build helper function, RFIT. */
1169806864d9SXiao Guangrong     method = aml_method("RFIT", 1, AML_SERIALIZED);
1170aef056c1SXiao Guangrong     aml_append(method, aml_name_decl("OFST", aml_int(0)));
1171806864d9SXiao Guangrong 
1172806864d9SXiao Guangrong     /* prepare input package. */
1173806864d9SXiao Guangrong     pkg = aml_package(1);
1174806864d9SXiao Guangrong     aml_append(method, aml_store(aml_arg(0), aml_name("OFST")));
1175806864d9SXiao Guangrong     aml_append(pkg, aml_name("OFST"));
1176806864d9SXiao Guangrong 
1177806864d9SXiao Guangrong     /* call Read_FIT function. */
1178806864d9SXiao Guangrong     call_result = aml_call5(NVDIMM_COMMON_DSM,
1179806864d9SXiao Guangrong                             aml_touuid(NVDIMM_QEMU_RSVD_UUID),
1180806864d9SXiao Guangrong                             aml_int(1) /* Revision 1 */,
1181806864d9SXiao Guangrong                             aml_int(0x1) /* Read FIT */,
1182806864d9SXiao Guangrong                             pkg, aml_int(NVDIMM_QEMU_RSVD_HANDLE_ROOT));
1183806864d9SXiao Guangrong     aml_append(method, aml_store(call_result, buf));
1184806864d9SXiao Guangrong 
1185806864d9SXiao Guangrong     /* handle _DSM result. */
1186806864d9SXiao Guangrong     aml_append(method, aml_create_dword_field(buf,
1187806864d9SXiao Guangrong                aml_int(0) /* offset at byte 0 */, "STAU"));
1188806864d9SXiao Guangrong 
1189806864d9SXiao Guangrong     aml_append(method, aml_store(aml_name("STAU"),
1190806864d9SXiao Guangrong                                  aml_name(NVDIMM_DSM_RFIT_STATUS)));
1191806864d9SXiao Guangrong 
1192806864d9SXiao Guangrong      /* if something is wrong during _DSM. */
1193c2fa3075SXiao Guangrong     ifcond = aml_equal(aml_int(NVDIMM_DSM_RET_STATUS_SUCCESS),
1194c2fa3075SXiao Guangrong                        aml_name("STAU"));
1195806864d9SXiao Guangrong     ifctx = aml_if(aml_lnot(ifcond));
1196806864d9SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1197806864d9SXiao Guangrong     aml_append(method, ifctx);
1198806864d9SXiao Guangrong 
1199806864d9SXiao Guangrong     aml_append(method, aml_store(aml_sizeof(buf), buf_size));
1200806864d9SXiao Guangrong     aml_append(method, aml_subtract(buf_size,
1201806864d9SXiao Guangrong                                     aml_int(4) /* the size of "STAU" */,
1202806864d9SXiao Guangrong                                     buf_size));
1203806864d9SXiao Guangrong 
1204806864d9SXiao Guangrong     /* if we read the end of fit. */
1205806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1206806864d9SXiao Guangrong     aml_append(ifctx, aml_return(aml_buffer(0, NULL)));
1207806864d9SXiao Guangrong     aml_append(method, ifctx);
1208806864d9SXiao Guangrong 
1209806864d9SXiao Guangrong     aml_append(method, aml_create_field(buf,
1210806864d9SXiao Guangrong                             aml_int(4 * BITS_PER_BYTE), /* offset at byte 4.*/
1211880f3612SXiao Guangrong                             aml_shiftleft(buf_size, aml_int(3)), "BUFF"));
1212806864d9SXiao Guangrong     aml_append(method, aml_return(aml_name("BUFF")));
1213806864d9SXiao Guangrong     aml_append(dev, method);
1214806864d9SXiao Guangrong 
1215806864d9SXiao Guangrong     /* build _FIT. */
1216806864d9SXiao Guangrong     method = aml_method("_FIT", 0, AML_SERIALIZED);
1217806864d9SXiao Guangrong     offset = aml_local(3);
1218806864d9SXiao Guangrong 
1219806864d9SXiao Guangrong     aml_append(method, aml_store(aml_buffer(0, NULL), fit));
1220806864d9SXiao Guangrong     aml_append(method, aml_store(aml_int(0), offset));
1221806864d9SXiao Guangrong 
1222806864d9SXiao Guangrong     whilectx = aml_while(aml_int(1));
1223806864d9SXiao Guangrong     aml_append(whilectx, aml_store(aml_call1("RFIT", offset), buf));
1224806864d9SXiao Guangrong     aml_append(whilectx, aml_store(aml_sizeof(buf), buf_size));
1225806864d9SXiao Guangrong 
1226806864d9SXiao Guangrong     /*
1227806864d9SXiao Guangrong      * if fit buffer was changed during RFIT, read from the beginning
1228806864d9SXiao Guangrong      * again.
1229806864d9SXiao Guangrong      */
1230806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(aml_name(NVDIMM_DSM_RFIT_STATUS),
1231c2fa3075SXiao Guangrong                              aml_int(NVDIMM_DSM_RET_STATUS_FIT_CHANGED)));
1232806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_buffer(0, NULL), fit));
1233806864d9SXiao Guangrong     aml_append(ifctx, aml_store(aml_int(0), offset));
1234806864d9SXiao Guangrong     aml_append(whilectx, ifctx);
1235806864d9SXiao Guangrong 
1236806864d9SXiao Guangrong     elsectx = aml_else();
1237806864d9SXiao Guangrong 
1238806864d9SXiao Guangrong     /* finish fit read if no data is read out. */
1239806864d9SXiao Guangrong     ifctx = aml_if(aml_equal(buf_size, aml_int(0)));
1240806864d9SXiao Guangrong     aml_append(ifctx, aml_return(fit));
1241806864d9SXiao Guangrong     aml_append(elsectx, ifctx);
1242806864d9SXiao Guangrong 
1243806864d9SXiao Guangrong     /* update the offset. */
1244806864d9SXiao Guangrong     aml_append(elsectx, aml_add(offset, buf_size, offset));
1245806864d9SXiao Guangrong     /* append the data we read out to the fit buffer. */
1246806864d9SXiao Guangrong     aml_append(elsectx, aml_concatenate(fit, buf, fit));
1247806864d9SXiao Guangrong     aml_append(whilectx, elsectx);
1248806864d9SXiao Guangrong     aml_append(method, whilectx);
1249806864d9SXiao Guangrong 
1250806864d9SXiao Guangrong     aml_append(dev, method);
1251806864d9SXiao Guangrong }
1252806864d9SXiao Guangrong 
1253bdfd065bSXiao Guangrong static void nvdimm_build_nvdimm_devices(Aml *root_dev, uint32_t ram_slots)
125477286395SXiao Guangrong {
1255bdfd065bSXiao Guangrong     uint32_t slot;
1256bdfd065bSXiao Guangrong 
1257bdfd065bSXiao Guangrong     for (slot = 0; slot < ram_slots; slot++) {
125877286395SXiao Guangrong         uint32_t handle = nvdimm_slot_to_handle(slot);
125977286395SXiao Guangrong         Aml *nvdimm_dev;
126077286395SXiao Guangrong 
126177286395SXiao Guangrong         nvdimm_dev = aml_device("NV%02X", slot);
126277286395SXiao Guangrong 
126377286395SXiao Guangrong         /*
126477286395SXiao Guangrong          * ACPI 6.0: 9.20 NVDIMM Devices:
126577286395SXiao Guangrong          *
126677286395SXiao Guangrong          * _ADR object that is used to supply OSPM with unique address
126777286395SXiao Guangrong          * of the NVDIMM device. This is done by returning the NFIT Device
126877286395SXiao Guangrong          * handle that is used to identify the associated entries in ACPI
126977286395SXiao Guangrong          * table NFIT or _FIT.
127077286395SXiao Guangrong          */
127177286395SXiao Guangrong         aml_append(nvdimm_dev, aml_name_decl("_ADR", aml_int(handle)));
127277286395SXiao Guangrong 
1273732b530cSXiao Guangrong         nvdimm_build_device_dsm(nvdimm_dev, handle);
127477286395SXiao Guangrong         aml_append(root_dev, nvdimm_dev);
127577286395SXiao Guangrong     }
127677286395SXiao Guangrong }
127777286395SXiao Guangrong 
1278bdfd065bSXiao Guangrong static void nvdimm_build_ssdt(GArray *table_offsets, GArray *table_data,
12795c94b826SKwangwoo Lee                               BIOSLinker *linker,
12805c94b826SKwangwoo Lee                               NVDIMMState *nvdimm_state,
1281bdfd065bSXiao Guangrong                               uint32_t ram_slots)
128277286395SXiao Guangrong {
1283c0b3b863SXiao Guangrong     Aml *ssdt, *sb_scope, *dev;
1284b9951413SXiao Guangrong     int mem_addr_offset, nvdimm_ssdt;
128577286395SXiao Guangrong 
128677286395SXiao Guangrong     acpi_add_table(table_offsets, table_data);
128777286395SXiao Guangrong 
128877286395SXiao Guangrong     ssdt = init_aml_allocator();
128977286395SXiao Guangrong     acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
129077286395SXiao Guangrong 
129177286395SXiao Guangrong     sb_scope = aml_scope("\\_SB");
129277286395SXiao Guangrong 
129377286395SXiao Guangrong     dev = aml_device("NVDR");
129477286395SXiao Guangrong 
129577286395SXiao Guangrong     /*
129677286395SXiao Guangrong      * ACPI 6.0: 9.20 NVDIMM Devices:
129777286395SXiao Guangrong      *
129877286395SXiao Guangrong      * The ACPI Name Space device uses _HID of ACPI0012 to identify the root
129977286395SXiao Guangrong      * NVDIMM interface device. Platform firmware is required to contain one
130077286395SXiao Guangrong      * such device in _SB scope if NVDIMMs support is exposed by platform to
130177286395SXiao Guangrong      * OSPM.
130277286395SXiao Guangrong      * For each NVDIMM present or intended to be supported by platform,
130377286395SXiao Guangrong      * platform firmware also exposes an ACPI Namespace Device under the
130477286395SXiao Guangrong      * root device.
130577286395SXiao Guangrong      */
130677286395SXiao Guangrong     aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0012")));
130777286395SXiao Guangrong 
13085c94b826SKwangwoo Lee     nvdimm_build_common_dsm(dev, nvdimm_state);
1309732b530cSXiao Guangrong 
1310732b530cSXiao Guangrong     /* 0 is reserved for root device. */
1311732b530cSXiao Guangrong     nvdimm_build_device_dsm(dev, 0);
1312806864d9SXiao Guangrong     nvdimm_build_fit(dev);
131377286395SXiao Guangrong 
1314bdfd065bSXiao Guangrong     nvdimm_build_nvdimm_devices(dev, ram_slots);
131577286395SXiao Guangrong 
131677286395SXiao Guangrong     aml_append(sb_scope, dev);
131777286395SXiao Guangrong     aml_append(ssdt, sb_scope);
1318b9951413SXiao Guangrong 
1319b9951413SXiao Guangrong     nvdimm_ssdt = table_data->len;
1320b9951413SXiao Guangrong 
132177286395SXiao Guangrong     /* copy AML table into ACPI tables blob and patch header there */
132277286395SXiao Guangrong     g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
1323b9951413SXiao Guangrong     mem_addr_offset = build_append_named_dword(table_data,
1324b9951413SXiao Guangrong                                                NVDIMM_ACPI_MEM_ADDR);
1325b9951413SXiao Guangrong 
1326ad9671b8SIgor Mammedov     bios_linker_loader_alloc(linker,
13275c94b826SKwangwoo Lee                              NVDIMM_DSM_MEM_FILE, nvdimm_state->dsm_mem,
1328ad9671b8SIgor Mammedov                              sizeof(NvdimmDsmIn), false /* high memory */);
13294678124bSIgor Mammedov     bios_linker_loader_add_pointer(linker,
13304678124bSIgor Mammedov         ACPI_BUILD_TABLE_FILE, mem_addr_offset, sizeof(uint32_t),
13314678124bSIgor Mammedov         NVDIMM_DSM_MEM_FILE, 0);
133277286395SXiao Guangrong     build_header(linker, table_data,
1333b9951413SXiao Guangrong         (void *)(table_data->data + nvdimm_ssdt),
1334b9951413SXiao Guangrong         "SSDT", table_data->len - nvdimm_ssdt, 1, NULL, "NVDIMM");
133577286395SXiao Guangrong     free_aml_allocator();
133677286395SXiao Guangrong }
133777286395SXiao Guangrong 
1338c3b0cf6eSVishal Verma void nvdimm_build_srat(GArray *table_data)
1339c3b0cf6eSVishal Verma {
1340c3b0cf6eSVishal Verma     GSList *device_list = nvdimm_get_device_list();
1341c3b0cf6eSVishal Verma 
1342c3b0cf6eSVishal Verma     for (; device_list; device_list = device_list->next) {
1343c3b0cf6eSVishal Verma         AcpiSratMemoryAffinity *numamem = NULL;
1344c3b0cf6eSVishal Verma         DeviceState *dev = device_list->data;
1345c3b0cf6eSVishal Verma         Object *obj = OBJECT(dev);
1346c3b0cf6eSVishal Verma         uint64_t addr, size;
1347c3b0cf6eSVishal Verma         int node;
1348c3b0cf6eSVishal Verma 
1349c3b0cf6eSVishal Verma         node = object_property_get_int(obj, PC_DIMM_NODE_PROP, &error_abort);
1350c3b0cf6eSVishal Verma         addr = object_property_get_uint(obj, PC_DIMM_ADDR_PROP, &error_abort);
1351c3b0cf6eSVishal Verma         size = object_property_get_uint(obj, PC_DIMM_SIZE_PROP, &error_abort);
1352c3b0cf6eSVishal Verma 
1353c3b0cf6eSVishal Verma         numamem = acpi_data_push(table_data, sizeof *numamem);
1354c3b0cf6eSVishal Verma         build_srat_memory(numamem, addr, size, node,
1355c3b0cf6eSVishal Verma                           MEM_AFFINITY_ENABLED | MEM_AFFINITY_NON_VOLATILE);
1356c3b0cf6eSVishal Verma     }
1357c3b0cf6eSVishal Verma     g_slist_free(device_list);
1358c3b0cf6eSVishal Verma }
1359c3b0cf6eSVishal Verma 
136087252e1bSXiao Guangrong void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
1361c1404bdeSEric Auger                        BIOSLinker *linker, NVDIMMState *state,
1362bdfd065bSXiao Guangrong                        uint32_t ram_slots)
136387252e1bSXiao Guangrong {
1364264813cbSXiao Guangrong     GSList *device_list;
1365bdfd065bSXiao Guangrong 
1366264813cbSXiao Guangrong     /* no nvdimm device can be plugged. */
1367264813cbSXiao Guangrong     if (!ram_slots) {
1368264813cbSXiao Guangrong         return;
1369264813cbSXiao Guangrong     }
1370264813cbSXiao Guangrong 
13715c94b826SKwangwoo Lee     nvdimm_build_ssdt(table_offsets, table_data, linker, state,
1372bdfd065bSXiao Guangrong                       ram_slots);
1373264813cbSXiao Guangrong 
1374cf7c0ff5SXiao Guangrong     device_list = nvdimm_get_device_list();
1375264813cbSXiao Guangrong     /* no NVDIMM device is plugged. */
1376264813cbSXiao Guangrong     if (!device_list) {
1377264813cbSXiao Guangrong         return;
1378bdfd065bSXiao Guangrong     }
1379264813cbSXiao Guangrong 
1380264813cbSXiao Guangrong     nvdimm_build_nfit(state, table_offsets, table_data, linker);
1381264813cbSXiao Guangrong     g_slist_free(device_list);
1382bdfd065bSXiao Guangrong }
1383