187252e1bSXiao Guangrong /* 287252e1bSXiao Guangrong * NVDIMM ACPI Implementation 387252e1bSXiao Guangrong * 487252e1bSXiao Guangrong * Copyright(C) 2015 Intel Corporation. 587252e1bSXiao Guangrong * 687252e1bSXiao Guangrong * Author: 787252e1bSXiao Guangrong * Xiao Guangrong <guangrong.xiao@linux.intel.com> 887252e1bSXiao Guangrong * 987252e1bSXiao Guangrong * NFIT is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT) 1087252e1bSXiao Guangrong * and the DSM specification can be found at: 1187252e1bSXiao Guangrong * http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf 1287252e1bSXiao Guangrong * 1387252e1bSXiao Guangrong * Currently, it only supports PMEM Virtualization. 1487252e1bSXiao Guangrong * 1587252e1bSXiao Guangrong * This library is free software; you can redistribute it and/or 1687252e1bSXiao Guangrong * modify it under the terms of the GNU Lesser General Public 1787252e1bSXiao Guangrong * License as published by the Free Software Foundation; either 1887252e1bSXiao Guangrong * version 2 of the License, or (at your option) any later version. 1987252e1bSXiao Guangrong * 2087252e1bSXiao Guangrong * This library is distributed in the hope that it will be useful, 2187252e1bSXiao Guangrong * but WITHOUT ANY WARRANTY; without even the implied warranty of 2287252e1bSXiao Guangrong * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 2387252e1bSXiao Guangrong * Lesser General Public License for more details. 2487252e1bSXiao Guangrong * 2587252e1bSXiao Guangrong * You should have received a copy of the GNU Lesser General Public 2687252e1bSXiao Guangrong * License along with this library; if not, see <http://www.gnu.org/licenses/> 2787252e1bSXiao Guangrong */ 2887252e1bSXiao Guangrong 29b6a0aa05SPeter Maydell #include "qemu/osdep.h" 3087252e1bSXiao Guangrong #include "hw/acpi/acpi.h" 3187252e1bSXiao Guangrong #include "hw/acpi/aml-build.h" 32b9951413SXiao Guangrong #include "hw/acpi/bios-linker-loader.h" 335fe79386SXiao Guangrong #include "hw/nvram/fw_cfg.h" 3487252e1bSXiao Guangrong #include "hw/mem/nvdimm.h" 3587252e1bSXiao Guangrong 3687252e1bSXiao Guangrong static int nvdimm_plugged_device_list(Object *obj, void *opaque) 3787252e1bSXiao Guangrong { 3887252e1bSXiao Guangrong GSList **list = opaque; 3987252e1bSXiao Guangrong 4087252e1bSXiao Guangrong if (object_dynamic_cast(obj, TYPE_NVDIMM)) { 4187252e1bSXiao Guangrong DeviceState *dev = DEVICE(obj); 4287252e1bSXiao Guangrong 4387252e1bSXiao Guangrong if (dev->realized) { /* only realized NVDIMMs matter */ 4487252e1bSXiao Guangrong *list = g_slist_append(*list, DEVICE(obj)); 4587252e1bSXiao Guangrong } 4687252e1bSXiao Guangrong } 4787252e1bSXiao Guangrong 4887252e1bSXiao Guangrong object_child_foreach(obj, nvdimm_plugged_device_list, opaque); 4987252e1bSXiao Guangrong return 0; 5087252e1bSXiao Guangrong } 5187252e1bSXiao Guangrong 5287252e1bSXiao Guangrong /* 5387252e1bSXiao Guangrong * inquire plugged NVDIMM devices and link them into the list which is 5487252e1bSXiao Guangrong * returned to the caller. 5587252e1bSXiao Guangrong * 5687252e1bSXiao Guangrong * Note: it is the caller's responsibility to free the list to avoid 5787252e1bSXiao Guangrong * memory leak. 5887252e1bSXiao Guangrong */ 5987252e1bSXiao Guangrong static GSList *nvdimm_get_plugged_device_list(void) 6087252e1bSXiao Guangrong { 6187252e1bSXiao Guangrong GSList *list = NULL; 6287252e1bSXiao Guangrong 6387252e1bSXiao Guangrong object_child_foreach(qdev_get_machine(), nvdimm_plugged_device_list, 6487252e1bSXiao Guangrong &list); 6587252e1bSXiao Guangrong return list; 6687252e1bSXiao Guangrong } 6787252e1bSXiao Guangrong 6887252e1bSXiao Guangrong #define NVDIMM_UUID_LE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ 6987252e1bSXiao Guangrong { (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ 7087252e1bSXiao Guangrong (b) & 0xff, ((b) >> 8) & 0xff, (c) & 0xff, ((c) >> 8) & 0xff, \ 7187252e1bSXiao Guangrong (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } 7287252e1bSXiao Guangrong 7387252e1bSXiao Guangrong /* 7487252e1bSXiao Guangrong * define Byte Addressable Persistent Memory (PM) Region according to 7587252e1bSXiao Guangrong * ACPI 6.0: 5.2.25.1 System Physical Address Range Structure. 7687252e1bSXiao Guangrong */ 7787252e1bSXiao Guangrong static const uint8_t nvdimm_nfit_spa_uuid[] = 7887252e1bSXiao Guangrong NVDIMM_UUID_LE(0x66f0d379, 0xb4f3, 0x4074, 0xac, 0x43, 0x0d, 0x33, 7987252e1bSXiao Guangrong 0x18, 0xb7, 0x8c, 0xdb); 8087252e1bSXiao Guangrong 8187252e1bSXiao Guangrong /* 8287252e1bSXiao Guangrong * NVDIMM Firmware Interface Table 8387252e1bSXiao Guangrong * @signature: "NFIT" 8487252e1bSXiao Guangrong * 8587252e1bSXiao Guangrong * It provides information that allows OSPM to enumerate NVDIMM present in 8687252e1bSXiao Guangrong * the platform and associate system physical address ranges created by the 8787252e1bSXiao Guangrong * NVDIMMs. 8887252e1bSXiao Guangrong * 8987252e1bSXiao Guangrong * It is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT) 9087252e1bSXiao Guangrong */ 9187252e1bSXiao Guangrong struct NvdimmNfitHeader { 9287252e1bSXiao Guangrong ACPI_TABLE_HEADER_DEF 9387252e1bSXiao Guangrong uint32_t reserved; 9487252e1bSXiao Guangrong } QEMU_PACKED; 9587252e1bSXiao Guangrong typedef struct NvdimmNfitHeader NvdimmNfitHeader; 9687252e1bSXiao Guangrong 9787252e1bSXiao Guangrong /* 9887252e1bSXiao Guangrong * define NFIT structures according to ACPI 6.0: 5.2.25 NVDIMM Firmware 9987252e1bSXiao Guangrong * Interface Table (NFIT). 10087252e1bSXiao Guangrong */ 10187252e1bSXiao Guangrong 10287252e1bSXiao Guangrong /* 10387252e1bSXiao Guangrong * System Physical Address Range Structure 10487252e1bSXiao Guangrong * 10587252e1bSXiao Guangrong * It describes the system physical address ranges occupied by NVDIMMs and 10687252e1bSXiao Guangrong * the types of the regions. 10787252e1bSXiao Guangrong */ 10887252e1bSXiao Guangrong struct NvdimmNfitSpa { 10987252e1bSXiao Guangrong uint16_t type; 11087252e1bSXiao Guangrong uint16_t length; 11187252e1bSXiao Guangrong uint16_t spa_index; 11287252e1bSXiao Guangrong uint16_t flags; 11387252e1bSXiao Guangrong uint32_t reserved; 11487252e1bSXiao Guangrong uint32_t proximity_domain; 11587252e1bSXiao Guangrong uint8_t type_guid[16]; 11687252e1bSXiao Guangrong uint64_t spa_base; 11787252e1bSXiao Guangrong uint64_t spa_length; 11887252e1bSXiao Guangrong uint64_t mem_attr; 11987252e1bSXiao Guangrong } QEMU_PACKED; 12087252e1bSXiao Guangrong typedef struct NvdimmNfitSpa NvdimmNfitSpa; 12187252e1bSXiao Guangrong 12287252e1bSXiao Guangrong /* 12387252e1bSXiao Guangrong * Memory Device to System Physical Address Range Mapping Structure 12487252e1bSXiao Guangrong * 12587252e1bSXiao Guangrong * It enables identifying each NVDIMM region and the corresponding SPA 12687252e1bSXiao Guangrong * describing the memory interleave 12787252e1bSXiao Guangrong */ 12887252e1bSXiao Guangrong struct NvdimmNfitMemDev { 12987252e1bSXiao Guangrong uint16_t type; 13087252e1bSXiao Guangrong uint16_t length; 13187252e1bSXiao Guangrong uint32_t nfit_handle; 13287252e1bSXiao Guangrong uint16_t phys_id; 13387252e1bSXiao Guangrong uint16_t region_id; 13487252e1bSXiao Guangrong uint16_t spa_index; 13587252e1bSXiao Guangrong uint16_t dcr_index; 13687252e1bSXiao Guangrong uint64_t region_len; 13787252e1bSXiao Guangrong uint64_t region_offset; 13887252e1bSXiao Guangrong uint64_t region_dpa; 13987252e1bSXiao Guangrong uint16_t interleave_index; 14087252e1bSXiao Guangrong uint16_t interleave_ways; 14187252e1bSXiao Guangrong uint16_t flags; 14287252e1bSXiao Guangrong uint16_t reserved; 14387252e1bSXiao Guangrong } QEMU_PACKED; 14487252e1bSXiao Guangrong typedef struct NvdimmNfitMemDev NvdimmNfitMemDev; 14587252e1bSXiao Guangrong 14687252e1bSXiao Guangrong /* 14787252e1bSXiao Guangrong * NVDIMM Control Region Structure 14887252e1bSXiao Guangrong * 14987252e1bSXiao Guangrong * It describes the NVDIMM and if applicable, Block Control Window. 15087252e1bSXiao Guangrong */ 15187252e1bSXiao Guangrong struct NvdimmNfitControlRegion { 15287252e1bSXiao Guangrong uint16_t type; 15387252e1bSXiao Guangrong uint16_t length; 15487252e1bSXiao Guangrong uint16_t dcr_index; 15587252e1bSXiao Guangrong uint16_t vendor_id; 15687252e1bSXiao Guangrong uint16_t device_id; 15787252e1bSXiao Guangrong uint16_t revision_id; 15887252e1bSXiao Guangrong uint16_t sub_vendor_id; 15987252e1bSXiao Guangrong uint16_t sub_device_id; 16087252e1bSXiao Guangrong uint16_t sub_revision_id; 16187252e1bSXiao Guangrong uint8_t reserved[6]; 16287252e1bSXiao Guangrong uint32_t serial_number; 16387252e1bSXiao Guangrong uint16_t fic; 16487252e1bSXiao Guangrong uint16_t num_bcw; 16587252e1bSXiao Guangrong uint64_t bcw_size; 16687252e1bSXiao Guangrong uint64_t cmd_offset; 16787252e1bSXiao Guangrong uint64_t cmd_size; 16887252e1bSXiao Guangrong uint64_t status_offset; 16987252e1bSXiao Guangrong uint64_t status_size; 17087252e1bSXiao Guangrong uint16_t flags; 17187252e1bSXiao Guangrong uint8_t reserved2[6]; 17287252e1bSXiao Guangrong } QEMU_PACKED; 17387252e1bSXiao Guangrong typedef struct NvdimmNfitControlRegion NvdimmNfitControlRegion; 17487252e1bSXiao Guangrong 17587252e1bSXiao Guangrong /* 17687252e1bSXiao Guangrong * Module serial number is a unique number for each device. We use the 17787252e1bSXiao Guangrong * slot id of NVDIMM device to generate this number so that each device 17887252e1bSXiao Guangrong * associates with a different number. 17987252e1bSXiao Guangrong * 18087252e1bSXiao Guangrong * 0x123456 is a magic number we arbitrarily chose. 18187252e1bSXiao Guangrong */ 18287252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_sn(int slot) 18387252e1bSXiao Guangrong { 18487252e1bSXiao Guangrong return 0x123456 + slot; 18587252e1bSXiao Guangrong } 18687252e1bSXiao Guangrong 18787252e1bSXiao Guangrong /* 18887252e1bSXiao Guangrong * handle is used to uniquely associate nfit_memdev structure with NVDIMM 18987252e1bSXiao Guangrong * ACPI device - nfit_memdev.nfit_handle matches with the value returned 19087252e1bSXiao Guangrong * by ACPI device _ADR method. 19187252e1bSXiao Guangrong * 19287252e1bSXiao Guangrong * We generate the handle with the slot id of NVDIMM device and reserve 19387252e1bSXiao Guangrong * 0 for NVDIMM root device. 19487252e1bSXiao Guangrong */ 19587252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_handle(int slot) 19687252e1bSXiao Guangrong { 19787252e1bSXiao Guangrong return slot + 1; 19887252e1bSXiao Guangrong } 19987252e1bSXiao Guangrong 20087252e1bSXiao Guangrong /* 20187252e1bSXiao Guangrong * index uniquely identifies the structure, 0 is reserved which indicates 20287252e1bSXiao Guangrong * that the structure is not valid or the associated structure is not 20387252e1bSXiao Guangrong * present. 20487252e1bSXiao Guangrong * 20587252e1bSXiao Guangrong * Each NVDIMM device needs two indexes, one for nfit_spa and another for 20687252e1bSXiao Guangrong * nfit_dc which are generated by the slot id of NVDIMM device. 20787252e1bSXiao Guangrong */ 20887252e1bSXiao Guangrong static uint16_t nvdimm_slot_to_spa_index(int slot) 20987252e1bSXiao Guangrong { 21087252e1bSXiao Guangrong return (slot + 1) << 1; 21187252e1bSXiao Guangrong } 21287252e1bSXiao Guangrong 21387252e1bSXiao Guangrong /* See the comments of nvdimm_slot_to_spa_index(). */ 21487252e1bSXiao Guangrong static uint32_t nvdimm_slot_to_dcr_index(int slot) 21587252e1bSXiao Guangrong { 21687252e1bSXiao Guangrong return nvdimm_slot_to_spa_index(slot) + 1; 21787252e1bSXiao Guangrong } 21887252e1bSXiao Guangrong 219*5797dcdcSXiao Guangrong static NVDIMMDevice *nvdimm_get_device_by_handle(uint32_t handle) 220*5797dcdcSXiao Guangrong { 221*5797dcdcSXiao Guangrong NVDIMMDevice *nvdimm = NULL; 222*5797dcdcSXiao Guangrong GSList *list, *device_list = nvdimm_get_plugged_device_list(); 223*5797dcdcSXiao Guangrong 224*5797dcdcSXiao Guangrong for (list = device_list; list; list = list->next) { 225*5797dcdcSXiao Guangrong NVDIMMDevice *nvd = list->data; 226*5797dcdcSXiao Guangrong int slot = object_property_get_int(OBJECT(nvd), PC_DIMM_SLOT_PROP, 227*5797dcdcSXiao Guangrong NULL); 228*5797dcdcSXiao Guangrong 229*5797dcdcSXiao Guangrong if (nvdimm_slot_to_handle(slot) == handle) { 230*5797dcdcSXiao Guangrong nvdimm = nvd; 231*5797dcdcSXiao Guangrong break; 232*5797dcdcSXiao Guangrong } 233*5797dcdcSXiao Guangrong } 234*5797dcdcSXiao Guangrong 235*5797dcdcSXiao Guangrong g_slist_free(device_list); 236*5797dcdcSXiao Guangrong return nvdimm; 237*5797dcdcSXiao Guangrong } 238*5797dcdcSXiao Guangrong 23987252e1bSXiao Guangrong /* ACPI 6.0: 5.2.25.1 System Physical Address Range Structure */ 24087252e1bSXiao Guangrong static void 24187252e1bSXiao Guangrong nvdimm_build_structure_spa(GArray *structures, DeviceState *dev) 24287252e1bSXiao Guangrong { 24387252e1bSXiao Guangrong NvdimmNfitSpa *nfit_spa; 24487252e1bSXiao Guangrong uint64_t addr = object_property_get_int(OBJECT(dev), PC_DIMM_ADDR_PROP, 24587252e1bSXiao Guangrong NULL); 24687252e1bSXiao Guangrong uint64_t size = object_property_get_int(OBJECT(dev), PC_DIMM_SIZE_PROP, 24787252e1bSXiao Guangrong NULL); 24887252e1bSXiao Guangrong uint32_t node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP, 24987252e1bSXiao Guangrong NULL); 25087252e1bSXiao Guangrong int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, 25187252e1bSXiao Guangrong NULL); 25287252e1bSXiao Guangrong 25387252e1bSXiao Guangrong nfit_spa = acpi_data_push(structures, sizeof(*nfit_spa)); 25487252e1bSXiao Guangrong 25587252e1bSXiao Guangrong nfit_spa->type = cpu_to_le16(0 /* System Physical Address Range 25687252e1bSXiao Guangrong Structure */); 25787252e1bSXiao Guangrong nfit_spa->length = cpu_to_le16(sizeof(*nfit_spa)); 25887252e1bSXiao Guangrong nfit_spa->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot)); 25987252e1bSXiao Guangrong 26087252e1bSXiao Guangrong /* 26187252e1bSXiao Guangrong * Control region is strict as all the device info, such as SN, index, 26287252e1bSXiao Guangrong * is associated with slot id. 26387252e1bSXiao Guangrong */ 26487252e1bSXiao Guangrong nfit_spa->flags = cpu_to_le16(1 /* Control region is strictly for 26587252e1bSXiao Guangrong management during hot add/online 26687252e1bSXiao Guangrong operation */ | 26787252e1bSXiao Guangrong 2 /* Data in Proximity Domain field is 26887252e1bSXiao Guangrong valid*/); 26987252e1bSXiao Guangrong 27087252e1bSXiao Guangrong /* NUMA node. */ 27187252e1bSXiao Guangrong nfit_spa->proximity_domain = cpu_to_le32(node); 27287252e1bSXiao Guangrong /* the region reported as PMEM. */ 27387252e1bSXiao Guangrong memcpy(nfit_spa->type_guid, nvdimm_nfit_spa_uuid, 27487252e1bSXiao Guangrong sizeof(nvdimm_nfit_spa_uuid)); 27587252e1bSXiao Guangrong 27687252e1bSXiao Guangrong nfit_spa->spa_base = cpu_to_le64(addr); 27787252e1bSXiao Guangrong nfit_spa->spa_length = cpu_to_le64(size); 27887252e1bSXiao Guangrong 27987252e1bSXiao Guangrong /* It is the PMEM and can be cached as writeback. */ 28087252e1bSXiao Guangrong nfit_spa->mem_attr = cpu_to_le64(0x8ULL /* EFI_MEMORY_WB */ | 28187252e1bSXiao Guangrong 0x8000ULL /* EFI_MEMORY_NV */); 28287252e1bSXiao Guangrong } 28387252e1bSXiao Guangrong 28487252e1bSXiao Guangrong /* 28587252e1bSXiao Guangrong * ACPI 6.0: 5.2.25.2 Memory Device to System Physical Address Range Mapping 28687252e1bSXiao Guangrong * Structure 28787252e1bSXiao Guangrong */ 28887252e1bSXiao Guangrong static void 28987252e1bSXiao Guangrong nvdimm_build_structure_memdev(GArray *structures, DeviceState *dev) 29087252e1bSXiao Guangrong { 29187252e1bSXiao Guangrong NvdimmNfitMemDev *nfit_memdev; 29287252e1bSXiao Guangrong uint64_t addr = object_property_get_int(OBJECT(dev), PC_DIMM_ADDR_PROP, 29387252e1bSXiao Guangrong NULL); 29487252e1bSXiao Guangrong uint64_t size = object_property_get_int(OBJECT(dev), PC_DIMM_SIZE_PROP, 29587252e1bSXiao Guangrong NULL); 29687252e1bSXiao Guangrong int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, 29787252e1bSXiao Guangrong NULL); 29887252e1bSXiao Guangrong uint32_t handle = nvdimm_slot_to_handle(slot); 29987252e1bSXiao Guangrong 30087252e1bSXiao Guangrong nfit_memdev = acpi_data_push(structures, sizeof(*nfit_memdev)); 30187252e1bSXiao Guangrong 30287252e1bSXiao Guangrong nfit_memdev->type = cpu_to_le16(1 /* Memory Device to System Address 30387252e1bSXiao Guangrong Range Map Structure*/); 30487252e1bSXiao Guangrong nfit_memdev->length = cpu_to_le16(sizeof(*nfit_memdev)); 30587252e1bSXiao Guangrong nfit_memdev->nfit_handle = cpu_to_le32(handle); 30687252e1bSXiao Guangrong 30787252e1bSXiao Guangrong /* 30887252e1bSXiao Guangrong * associate memory device with System Physical Address Range 30987252e1bSXiao Guangrong * Structure. 31087252e1bSXiao Guangrong */ 31187252e1bSXiao Guangrong nfit_memdev->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot)); 31287252e1bSXiao Guangrong /* associate memory device with Control Region Structure. */ 31387252e1bSXiao Guangrong nfit_memdev->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot)); 31487252e1bSXiao Guangrong 31587252e1bSXiao Guangrong /* The memory region on the device. */ 31687252e1bSXiao Guangrong nfit_memdev->region_len = cpu_to_le64(size); 31787252e1bSXiao Guangrong nfit_memdev->region_dpa = cpu_to_le64(addr); 31887252e1bSXiao Guangrong 31987252e1bSXiao Guangrong /* Only one interleave for PMEM. */ 32087252e1bSXiao Guangrong nfit_memdev->interleave_ways = cpu_to_le16(1); 32187252e1bSXiao Guangrong } 32287252e1bSXiao Guangrong 32387252e1bSXiao Guangrong /* 32487252e1bSXiao Guangrong * ACPI 6.0: 5.2.25.5 NVDIMM Control Region Structure. 32587252e1bSXiao Guangrong */ 32687252e1bSXiao Guangrong static void nvdimm_build_structure_dcr(GArray *structures, DeviceState *dev) 32787252e1bSXiao Guangrong { 32887252e1bSXiao Guangrong NvdimmNfitControlRegion *nfit_dcr; 32987252e1bSXiao Guangrong int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, 33087252e1bSXiao Guangrong NULL); 33187252e1bSXiao Guangrong uint32_t sn = nvdimm_slot_to_sn(slot); 33287252e1bSXiao Guangrong 33387252e1bSXiao Guangrong nfit_dcr = acpi_data_push(structures, sizeof(*nfit_dcr)); 33487252e1bSXiao Guangrong 33587252e1bSXiao Guangrong nfit_dcr->type = cpu_to_le16(4 /* NVDIMM Control Region Structure */); 33687252e1bSXiao Guangrong nfit_dcr->length = cpu_to_le16(sizeof(*nfit_dcr)); 33787252e1bSXiao Guangrong nfit_dcr->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot)); 33887252e1bSXiao Guangrong 33987252e1bSXiao Guangrong /* vendor: Intel. */ 34087252e1bSXiao Guangrong nfit_dcr->vendor_id = cpu_to_le16(0x8086); 34187252e1bSXiao Guangrong nfit_dcr->device_id = cpu_to_le16(1); 34287252e1bSXiao Guangrong 34387252e1bSXiao Guangrong /* The _DSM method is following Intel's DSM specification. */ 34487252e1bSXiao Guangrong nfit_dcr->revision_id = cpu_to_le16(1 /* Current Revision supported 34587252e1bSXiao Guangrong in ACPI 6.0 is 1. */); 34687252e1bSXiao Guangrong nfit_dcr->serial_number = cpu_to_le32(sn); 34787252e1bSXiao Guangrong nfit_dcr->fic = cpu_to_le16(0x201 /* Format Interface Code. See Chapter 34887252e1bSXiao Guangrong 2: NVDIMM Device Specific Method 34987252e1bSXiao Guangrong (DSM) in DSM Spec Rev1.*/); 35087252e1bSXiao Guangrong } 35187252e1bSXiao Guangrong 35287252e1bSXiao Guangrong static GArray *nvdimm_build_device_structure(GSList *device_list) 35387252e1bSXiao Guangrong { 35487252e1bSXiao Guangrong GArray *structures = g_array_new(false, true /* clear */, 1); 35587252e1bSXiao Guangrong 35687252e1bSXiao Guangrong for (; device_list; device_list = device_list->next) { 35787252e1bSXiao Guangrong DeviceState *dev = device_list->data; 35887252e1bSXiao Guangrong 35987252e1bSXiao Guangrong /* build System Physical Address Range Structure. */ 36087252e1bSXiao Guangrong nvdimm_build_structure_spa(structures, dev); 36187252e1bSXiao Guangrong 36287252e1bSXiao Guangrong /* 36387252e1bSXiao Guangrong * build Memory Device to System Physical Address Range Mapping 36487252e1bSXiao Guangrong * Structure. 36587252e1bSXiao Guangrong */ 36687252e1bSXiao Guangrong nvdimm_build_structure_memdev(structures, dev); 36787252e1bSXiao Guangrong 36887252e1bSXiao Guangrong /* build NVDIMM Control Region Structure. */ 36987252e1bSXiao Guangrong nvdimm_build_structure_dcr(structures, dev); 37087252e1bSXiao Guangrong } 37187252e1bSXiao Guangrong 37287252e1bSXiao Guangrong return structures; 37387252e1bSXiao Guangrong } 37487252e1bSXiao Guangrong 37587252e1bSXiao Guangrong static void nvdimm_build_nfit(GSList *device_list, GArray *table_offsets, 3760e9b9edaSIgor Mammedov GArray *table_data, BIOSLinker *linker) 37787252e1bSXiao Guangrong { 37887252e1bSXiao Guangrong GArray *structures = nvdimm_build_device_structure(device_list); 379c8e6c938SHaozhong Zhang unsigned int header; 38087252e1bSXiao Guangrong 38187252e1bSXiao Guangrong acpi_add_table(table_offsets, table_data); 38287252e1bSXiao Guangrong 38387252e1bSXiao Guangrong /* NFIT header. */ 384c8e6c938SHaozhong Zhang header = table_data->len; 385c8e6c938SHaozhong Zhang acpi_data_push(table_data, sizeof(NvdimmNfitHeader)); 38687252e1bSXiao Guangrong /* NVDIMM device structures. */ 38787252e1bSXiao Guangrong g_array_append_vals(table_data, structures->data, structures->len); 38887252e1bSXiao Guangrong 389c8e6c938SHaozhong Zhang build_header(linker, table_data, 390c8e6c938SHaozhong Zhang (void *)(table_data->data + header), "NFIT", 39137ad223cSLaszlo Ersek sizeof(NvdimmNfitHeader) + structures->len, 1, NULL, NULL); 39287252e1bSXiao Guangrong g_array_free(structures, true); 39387252e1bSXiao Guangrong } 39487252e1bSXiao Guangrong 39518c440e1SXiao Guangrong struct NvdimmDsmIn { 39618c440e1SXiao Guangrong uint32_t handle; 39718c440e1SXiao Guangrong uint32_t revision; 39818c440e1SXiao Guangrong uint32_t function; 39918c440e1SXiao Guangrong /* the remaining size in the page is used by arg3. */ 40018c440e1SXiao Guangrong union { 40135c5a52dSPaolo Bonzini uint8_t arg3[4084]; 40218c440e1SXiao Guangrong }; 40318c440e1SXiao Guangrong } QEMU_PACKED; 40418c440e1SXiao Guangrong typedef struct NvdimmDsmIn NvdimmDsmIn; 40535c5a52dSPaolo Bonzini QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmIn) != 4096); 40618c440e1SXiao Guangrong 40718c440e1SXiao Guangrong struct NvdimmDsmOut { 40818c440e1SXiao Guangrong /* the size of buffer filled by QEMU. */ 40918c440e1SXiao Guangrong uint32_t len; 41035c5a52dSPaolo Bonzini uint8_t data[4092]; 41118c440e1SXiao Guangrong } QEMU_PACKED; 41218c440e1SXiao Guangrong typedef struct NvdimmDsmOut NvdimmDsmOut; 41335c5a52dSPaolo Bonzini QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmOut) != 4096); 41418c440e1SXiao Guangrong 415f7df22deSXiao Guangrong struct NvdimmDsmFunc0Out { 416f7df22deSXiao Guangrong /* the size of buffer filled by QEMU. */ 417f7df22deSXiao Guangrong uint32_t len; 418f7df22deSXiao Guangrong uint32_t supported_func; 419f7df22deSXiao Guangrong } QEMU_PACKED; 420f7df22deSXiao Guangrong typedef struct NvdimmDsmFunc0Out NvdimmDsmFunc0Out; 421f7df22deSXiao Guangrong 422f7df22deSXiao Guangrong struct NvdimmDsmFuncNoPayloadOut { 423f7df22deSXiao Guangrong /* the size of buffer filled by QEMU. */ 424f7df22deSXiao Guangrong uint32_t len; 425f7df22deSXiao Guangrong uint32_t func_ret_status; 426f7df22deSXiao Guangrong } QEMU_PACKED; 427f7df22deSXiao Guangrong typedef struct NvdimmDsmFuncNoPayloadOut NvdimmDsmFuncNoPayloadOut; 428f7df22deSXiao Guangrong 429*5797dcdcSXiao Guangrong struct NvdimmFuncGetLabelSizeOut { 430*5797dcdcSXiao Guangrong /* the size of buffer filled by QEMU. */ 431*5797dcdcSXiao Guangrong uint32_t len; 432*5797dcdcSXiao Guangrong uint32_t func_ret_status; /* return status code. */ 433*5797dcdcSXiao Guangrong uint32_t label_size; /* the size of label data area. */ 434*5797dcdcSXiao Guangrong /* 435*5797dcdcSXiao Guangrong * Maximum size of the namespace label data length supported by 436*5797dcdcSXiao Guangrong * the platform in Get/Set Namespace Label Data functions. 437*5797dcdcSXiao Guangrong */ 438*5797dcdcSXiao Guangrong uint32_t max_xfer; 439*5797dcdcSXiao Guangrong } QEMU_PACKED; 440*5797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelSizeOut NvdimmFuncGetLabelSizeOut; 441*5797dcdcSXiao Guangrong QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelSizeOut) > 4096); 442*5797dcdcSXiao Guangrong 443*5797dcdcSXiao Guangrong struct NvdimmFuncGetLabelDataOut { 444*5797dcdcSXiao Guangrong /* the size of buffer filled by QEMU. */ 445*5797dcdcSXiao Guangrong uint32_t len; 446*5797dcdcSXiao Guangrong uint32_t func_ret_status; /* return status code. */ 447*5797dcdcSXiao Guangrong uint8_t out_buf[0]; /* the data got via Get Namesapce Label function. */ 448*5797dcdcSXiao Guangrong } QEMU_PACKED; 449*5797dcdcSXiao Guangrong typedef struct NvdimmFuncGetLabelDataOut NvdimmFuncGetLabelDataOut; 450*5797dcdcSXiao Guangrong 451*5797dcdcSXiao Guangrong struct NvdimmFuncSetLabelDataIn { 452*5797dcdcSXiao Guangrong uint32_t offset; /* the offset in the namespace label data area. */ 453*5797dcdcSXiao Guangrong uint32_t length; /* the size of data is to be written via the function. */ 454*5797dcdcSXiao Guangrong uint8_t in_buf[0]; /* the data written to label data area. */ 455*5797dcdcSXiao Guangrong } QEMU_PACKED; 456*5797dcdcSXiao Guangrong typedef struct NvdimmFuncSetLabelDataIn NvdimmFuncSetLabelDataIn; 457*5797dcdcSXiao Guangrong 458189f4d56SXiao Guangrong static void 459189f4d56SXiao Guangrong nvdimm_dsm_function0(uint32_t supported_func, hwaddr dsm_mem_addr) 460189f4d56SXiao Guangrong { 461189f4d56SXiao Guangrong NvdimmDsmFunc0Out func0 = { 462189f4d56SXiao Guangrong .len = cpu_to_le32(sizeof(func0)), 463189f4d56SXiao Guangrong .supported_func = cpu_to_le32(supported_func), 464189f4d56SXiao Guangrong }; 465189f4d56SXiao Guangrong cpu_physical_memory_write(dsm_mem_addr, &func0, sizeof(func0)); 466189f4d56SXiao Guangrong } 467189f4d56SXiao Guangrong 468189f4d56SXiao Guangrong static void 469189f4d56SXiao Guangrong nvdimm_dsm_no_payload(uint32_t func_ret_status, hwaddr dsm_mem_addr) 470189f4d56SXiao Guangrong { 471189f4d56SXiao Guangrong NvdimmDsmFuncNoPayloadOut out = { 472189f4d56SXiao Guangrong .len = cpu_to_le32(sizeof(out)), 473189f4d56SXiao Guangrong .func_ret_status = cpu_to_le32(func_ret_status), 474189f4d56SXiao Guangrong }; 475189f4d56SXiao Guangrong cpu_physical_memory_write(dsm_mem_addr, &out, sizeof(out)); 476189f4d56SXiao Guangrong } 477189f4d56SXiao Guangrong 478189f4d56SXiao Guangrong static void nvdimm_dsm_root(NvdimmDsmIn *in, hwaddr dsm_mem_addr) 479189f4d56SXiao Guangrong { 480189f4d56SXiao Guangrong /* 481189f4d56SXiao Guangrong * function 0 is called to inquire which functions are supported by 482189f4d56SXiao Guangrong * OSPM 483189f4d56SXiao Guangrong */ 484189f4d56SXiao Guangrong if (!in->function) { 485189f4d56SXiao Guangrong nvdimm_dsm_function0(0 /* No function supported other than 486189f4d56SXiao Guangrong function 0 */, dsm_mem_addr); 487189f4d56SXiao Guangrong return; 488189f4d56SXiao Guangrong } 489189f4d56SXiao Guangrong 490189f4d56SXiao Guangrong /* No function except function 0 is supported yet. */ 491189f4d56SXiao Guangrong nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr); 492189f4d56SXiao Guangrong } 493189f4d56SXiao Guangrong 494*5797dcdcSXiao Guangrong /* 495*5797dcdcSXiao Guangrong * the max transfer size is the max size transferred by both a 496*5797dcdcSXiao Guangrong * 'Get Namespace Label Data' function and a 'Set Namespace Label Data' 497*5797dcdcSXiao Guangrong * function. 498*5797dcdcSXiao Guangrong */ 499*5797dcdcSXiao Guangrong static uint32_t nvdimm_get_max_xfer_label_size(void) 500*5797dcdcSXiao Guangrong { 501*5797dcdcSXiao Guangrong uint32_t max_get_size, max_set_size, dsm_memory_size = 4096; 502*5797dcdcSXiao Guangrong 503*5797dcdcSXiao Guangrong /* 504*5797dcdcSXiao Guangrong * the max data ACPI can read one time which is transferred by 505*5797dcdcSXiao Guangrong * the response of 'Get Namespace Label Data' function. 506*5797dcdcSXiao Guangrong */ 507*5797dcdcSXiao Guangrong max_get_size = dsm_memory_size - sizeof(NvdimmFuncGetLabelDataOut); 508*5797dcdcSXiao Guangrong 509*5797dcdcSXiao Guangrong /* 510*5797dcdcSXiao Guangrong * the max data ACPI can write one time which is transferred by 511*5797dcdcSXiao Guangrong * 'Set Namespace Label Data' function. 512*5797dcdcSXiao Guangrong */ 513*5797dcdcSXiao Guangrong max_set_size = dsm_memory_size - offsetof(NvdimmDsmIn, arg3) - 514*5797dcdcSXiao Guangrong sizeof(NvdimmFuncSetLabelDataIn); 515*5797dcdcSXiao Guangrong 516*5797dcdcSXiao Guangrong return MIN(max_get_size, max_set_size); 517*5797dcdcSXiao Guangrong } 518*5797dcdcSXiao Guangrong 519*5797dcdcSXiao Guangrong /* 520*5797dcdcSXiao Guangrong * DSM Spec Rev1 4.4 Get Namespace Label Size (Function Index 4). 521*5797dcdcSXiao Guangrong * 522*5797dcdcSXiao Guangrong * It gets the size of Namespace Label data area and the max data size 523*5797dcdcSXiao Guangrong * that Get/Set Namespace Label Data functions can transfer. 524*5797dcdcSXiao Guangrong */ 525*5797dcdcSXiao Guangrong static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm, hwaddr dsm_mem_addr) 526*5797dcdcSXiao Guangrong { 527*5797dcdcSXiao Guangrong NvdimmFuncGetLabelSizeOut label_size_out = { 528*5797dcdcSXiao Guangrong .len = cpu_to_le32(sizeof(label_size_out)), 529*5797dcdcSXiao Guangrong }; 530*5797dcdcSXiao Guangrong uint32_t label_size, mxfer; 531*5797dcdcSXiao Guangrong 532*5797dcdcSXiao Guangrong label_size = nvdimm->label_size; 533*5797dcdcSXiao Guangrong mxfer = nvdimm_get_max_xfer_label_size(); 534*5797dcdcSXiao Guangrong 535*5797dcdcSXiao Guangrong nvdimm_debug("label_size %#x, max_xfer %#x.\n", label_size, mxfer); 536*5797dcdcSXiao Guangrong 537*5797dcdcSXiao Guangrong label_size_out.func_ret_status = cpu_to_le32(0 /* Success */); 538*5797dcdcSXiao Guangrong label_size_out.label_size = cpu_to_le32(label_size); 539*5797dcdcSXiao Guangrong label_size_out.max_xfer = cpu_to_le32(mxfer); 540*5797dcdcSXiao Guangrong 541*5797dcdcSXiao Guangrong cpu_physical_memory_write(dsm_mem_addr, &label_size_out, 542*5797dcdcSXiao Guangrong sizeof(label_size_out)); 543*5797dcdcSXiao Guangrong } 544*5797dcdcSXiao Guangrong 545189f4d56SXiao Guangrong static void nvdimm_dsm_device(NvdimmDsmIn *in, hwaddr dsm_mem_addr) 546189f4d56SXiao Guangrong { 547*5797dcdcSXiao Guangrong NVDIMMDevice *nvdimm = nvdimm_get_device_by_handle(in->handle); 548*5797dcdcSXiao Guangrong 549189f4d56SXiao Guangrong /* See the comments in nvdimm_dsm_root(). */ 550189f4d56SXiao Guangrong if (!in->function) { 551*5797dcdcSXiao Guangrong uint32_t supported_func = 0; 552*5797dcdcSXiao Guangrong 553*5797dcdcSXiao Guangrong if (nvdimm && nvdimm->label_size) { 554*5797dcdcSXiao Guangrong supported_func |= 0x1 /* Bit 0 indicates whether there is 555*5797dcdcSXiao Guangrong support for any functions other 556*5797dcdcSXiao Guangrong than function 0. */ | 557*5797dcdcSXiao Guangrong 1 << 4 /* Get Namespace Label Size */; 558*5797dcdcSXiao Guangrong } 559*5797dcdcSXiao Guangrong nvdimm_dsm_function0(supported_func, dsm_mem_addr); 560189f4d56SXiao Guangrong return; 561189f4d56SXiao Guangrong } 562189f4d56SXiao Guangrong 563*5797dcdcSXiao Guangrong if (!nvdimm) { 564*5797dcdcSXiao Guangrong nvdimm_dsm_no_payload(2 /* Non-Existing Memory Device */, 565*5797dcdcSXiao Guangrong dsm_mem_addr); 566*5797dcdcSXiao Guangrong return; 567*5797dcdcSXiao Guangrong } 568*5797dcdcSXiao Guangrong 569*5797dcdcSXiao Guangrong /* Encode DSM function according to DSM Spec Rev1. */ 570*5797dcdcSXiao Guangrong switch (in->function) { 571*5797dcdcSXiao Guangrong case 4 /* Get Namespace Label Size */: 572*5797dcdcSXiao Guangrong if (nvdimm->label_size) { 573*5797dcdcSXiao Guangrong nvdimm_dsm_label_size(nvdimm, dsm_mem_addr); 574*5797dcdcSXiao Guangrong return; 575*5797dcdcSXiao Guangrong } 576*5797dcdcSXiao Guangrong break; 577*5797dcdcSXiao Guangrong } 578*5797dcdcSXiao Guangrong 579189f4d56SXiao Guangrong nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr); 580189f4d56SXiao Guangrong } 581189f4d56SXiao Guangrong 5825fe79386SXiao Guangrong static uint64_t 5835fe79386SXiao Guangrong nvdimm_dsm_read(void *opaque, hwaddr addr, unsigned size) 5845fe79386SXiao Guangrong { 585f7df22deSXiao Guangrong nvdimm_debug("BUG: we never read _DSM IO Port.\n"); 5865fe79386SXiao Guangrong return 0; 5875fe79386SXiao Guangrong } 5885fe79386SXiao Guangrong 5895fe79386SXiao Guangrong static void 5905fe79386SXiao Guangrong nvdimm_dsm_write(void *opaque, hwaddr addr, uint64_t val, unsigned size) 5915fe79386SXiao Guangrong { 592f7df22deSXiao Guangrong NvdimmDsmIn *in; 593f7df22deSXiao Guangrong hwaddr dsm_mem_addr = val; 594f7df22deSXiao Guangrong 595f7df22deSXiao Guangrong nvdimm_debug("dsm memory address %#" HWADDR_PRIx ".\n", dsm_mem_addr); 596f7df22deSXiao Guangrong 597f7df22deSXiao Guangrong /* 598f7df22deSXiao Guangrong * The DSM memory is mapped to guest address space so an evil guest 599f7df22deSXiao Guangrong * can change its content while we are doing DSM emulation. Avoid 600f7df22deSXiao Guangrong * this by copying DSM memory to QEMU local memory. 601f7df22deSXiao Guangrong */ 60235c5a52dSPaolo Bonzini in = g_new(NvdimmDsmIn, 1); 60335c5a52dSPaolo Bonzini cpu_physical_memory_read(dsm_mem_addr, in, sizeof(*in)); 604f7df22deSXiao Guangrong 605f7df22deSXiao Guangrong le32_to_cpus(&in->revision); 606f7df22deSXiao Guangrong le32_to_cpus(&in->function); 607f7df22deSXiao Guangrong le32_to_cpus(&in->handle); 608f7df22deSXiao Guangrong 609f7df22deSXiao Guangrong nvdimm_debug("Revision %#x Handler %#x Function %#x.\n", in->revision, 610f7df22deSXiao Guangrong in->handle, in->function); 611f7df22deSXiao Guangrong 612d15fc53fSXiao Guangrong if (in->revision != 0x1 /* Currently we only support DSM Spec Rev1. */) { 613d15fc53fSXiao Guangrong nvdimm_debug("Revision %#x is not supported, expect %#x.\n", 614d15fc53fSXiao Guangrong in->revision, 0x1); 615d15fc53fSXiao Guangrong nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr); 616d15fc53fSXiao Guangrong goto exit; 617d15fc53fSXiao Guangrong } 618d15fc53fSXiao Guangrong 619189f4d56SXiao Guangrong /* Handle 0 is reserved for NVDIMM Root Device. */ 620189f4d56SXiao Guangrong if (!in->handle) { 621189f4d56SXiao Guangrong nvdimm_dsm_root(in, dsm_mem_addr); 622189f4d56SXiao Guangrong goto exit; 623f7df22deSXiao Guangrong } 624f7df22deSXiao Guangrong 625189f4d56SXiao Guangrong nvdimm_dsm_device(in, dsm_mem_addr); 626189f4d56SXiao Guangrong 627189f4d56SXiao Guangrong exit: 628f7df22deSXiao Guangrong g_free(in); 6295fe79386SXiao Guangrong } 6305fe79386SXiao Guangrong 6315fe79386SXiao Guangrong static const MemoryRegionOps nvdimm_dsm_ops = { 6325fe79386SXiao Guangrong .read = nvdimm_dsm_read, 6335fe79386SXiao Guangrong .write = nvdimm_dsm_write, 6345fe79386SXiao Guangrong .endianness = DEVICE_LITTLE_ENDIAN, 6355fe79386SXiao Guangrong .valid = { 6365fe79386SXiao Guangrong .min_access_size = 4, 6375fe79386SXiao Guangrong .max_access_size = 4, 6385fe79386SXiao Guangrong }, 6395fe79386SXiao Guangrong }; 6405fe79386SXiao Guangrong 6415fe79386SXiao Guangrong void nvdimm_init_acpi_state(AcpiNVDIMMState *state, MemoryRegion *io, 6425fe79386SXiao Guangrong FWCfgState *fw_cfg, Object *owner) 6435fe79386SXiao Guangrong { 6445fe79386SXiao Guangrong memory_region_init_io(&state->io_mr, owner, &nvdimm_dsm_ops, state, 6455fe79386SXiao Guangrong "nvdimm-acpi-io", NVDIMM_ACPI_IO_LEN); 6465fe79386SXiao Guangrong memory_region_add_subregion(io, NVDIMM_ACPI_IO_BASE, &state->io_mr); 6475fe79386SXiao Guangrong 6485fe79386SXiao Guangrong state->dsm_mem = g_array_new(false, true /* clear */, 1); 64935c5a52dSPaolo Bonzini acpi_data_push(state->dsm_mem, sizeof(NvdimmDsmIn)); 6505fe79386SXiao Guangrong fw_cfg_add_file(fw_cfg, NVDIMM_DSM_MEM_FILE, state->dsm_mem->data, 6515fe79386SXiao Guangrong state->dsm_mem->len); 6525fe79386SXiao Guangrong } 6535fe79386SXiao Guangrong 65477286395SXiao Guangrong #define NVDIMM_COMMON_DSM "NCAL" 655b9951413SXiao Guangrong #define NVDIMM_ACPI_MEM_ADDR "MEMA" 65677286395SXiao Guangrong 65777286395SXiao Guangrong static void nvdimm_build_common_dsm(Aml *dev) 65877286395SXiao Guangrong { 65990623ebfSXiao Guangrong Aml *method, *ifctx, *function, *handle, *uuid, *dsm_mem, *result_size; 66090623ebfSXiao Guangrong Aml *elsectx, *unsupport, *unpatched, *expected_uuid, *uuid_invalid; 6614568c948SXiao Guangrong Aml *pckg, *pckg_index, *pckg_buf; 66277286395SXiao Guangrong uint8_t byte_list[1]; 66377286395SXiao Guangrong 664732b530cSXiao Guangrong method = aml_method(NVDIMM_COMMON_DSM, 5, AML_SERIALIZED); 66590623ebfSXiao Guangrong uuid = aml_arg(0); 66677286395SXiao Guangrong function = aml_arg(2); 66790623ebfSXiao Guangrong handle = aml_arg(4); 66818c440e1SXiao Guangrong dsm_mem = aml_name(NVDIMM_ACPI_MEM_ADDR); 66918c440e1SXiao Guangrong 67018c440e1SXiao Guangrong /* 67118c440e1SXiao Guangrong * do not support any method if DSM memory address has not been 67218c440e1SXiao Guangrong * patched. 67318c440e1SXiao Guangrong */ 67490623ebfSXiao Guangrong unpatched = aml_equal(dsm_mem, aml_int(0x0)); 67590623ebfSXiao Guangrong 67690623ebfSXiao Guangrong expected_uuid = aml_local(0); 67790623ebfSXiao Guangrong 67890623ebfSXiao Guangrong ifctx = aml_if(aml_equal(handle, aml_int(0x0))); 67990623ebfSXiao Guangrong aml_append(ifctx, aml_store( 68090623ebfSXiao Guangrong aml_touuid("2F10E7A4-9E91-11E4-89D3-123B93F75CBA") 68190623ebfSXiao Guangrong /* UUID for NVDIMM Root Device */, expected_uuid)); 68290623ebfSXiao Guangrong aml_append(method, ifctx); 68390623ebfSXiao Guangrong elsectx = aml_else(); 68490623ebfSXiao Guangrong aml_append(elsectx, aml_store( 68590623ebfSXiao Guangrong aml_touuid("4309AC30-0D11-11E4-9191-0800200C9A66") 68690623ebfSXiao Guangrong /* UUID for NVDIMM Devices */, expected_uuid)); 68790623ebfSXiao Guangrong aml_append(method, elsectx); 68890623ebfSXiao Guangrong 68990623ebfSXiao Guangrong uuid_invalid = aml_lnot(aml_equal(uuid, expected_uuid)); 69090623ebfSXiao Guangrong 69190623ebfSXiao Guangrong unsupport = aml_if(aml_or(unpatched, uuid_invalid, NULL)); 69277286395SXiao Guangrong 69377286395SXiao Guangrong /* 69477286395SXiao Guangrong * function 0 is called to inquire what functions are supported by 69577286395SXiao Guangrong * OSPM 69677286395SXiao Guangrong */ 69777286395SXiao Guangrong ifctx = aml_if(aml_equal(function, aml_int(0))); 69877286395SXiao Guangrong byte_list[0] = 0 /* No function Supported */; 69977286395SXiao Guangrong aml_append(ifctx, aml_return(aml_buffer(1, byte_list))); 70090623ebfSXiao Guangrong aml_append(unsupport, ifctx); 70177286395SXiao Guangrong 70277286395SXiao Guangrong /* No function is supported yet. */ 70377286395SXiao Guangrong byte_list[0] = 1 /* Not Supported */; 70490623ebfSXiao Guangrong aml_append(unsupport, aml_return(aml_buffer(1, byte_list))); 70590623ebfSXiao Guangrong aml_append(method, unsupport); 70677286395SXiao Guangrong 70718c440e1SXiao Guangrong /* 70818c440e1SXiao Guangrong * The HDLE indicates the DSM function is issued from which device, 709732b530cSXiao Guangrong * it reserves 0 for root device and is the handle for NVDIMM devices. 710732b530cSXiao Guangrong * See the comments in nvdimm_slot_to_handle(). 71118c440e1SXiao Guangrong */ 71290623ebfSXiao Guangrong aml_append(method, aml_store(handle, aml_name("HDLE"))); 71318c440e1SXiao Guangrong aml_append(method, aml_store(aml_arg(1), aml_name("REVS"))); 71418c440e1SXiao Guangrong aml_append(method, aml_store(aml_arg(2), aml_name("FUNC"))); 71518c440e1SXiao Guangrong 71618c440e1SXiao Guangrong /* 7174568c948SXiao Guangrong * The fourth parameter (Arg3) of _DSM is a package which contains 7184568c948SXiao Guangrong * a buffer, the layout of the buffer is specified by UUID (Arg0), 7194568c948SXiao Guangrong * Revision ID (Arg1) and Function Index (Arg2) which are documented 7204568c948SXiao Guangrong * in the DSM Spec. 7214568c948SXiao Guangrong */ 7224568c948SXiao Guangrong pckg = aml_arg(3); 7234568c948SXiao Guangrong ifctx = aml_if(aml_and(aml_equal(aml_object_type(pckg), 7244568c948SXiao Guangrong aml_int(4 /* Package */)) /* It is a Package? */, 7254568c948SXiao Guangrong aml_equal(aml_sizeof(pckg), aml_int(1)) /* 1 element? */, 7264568c948SXiao Guangrong NULL)); 7274568c948SXiao Guangrong 7284568c948SXiao Guangrong pckg_index = aml_local(2); 7294568c948SXiao Guangrong pckg_buf = aml_local(3); 7304568c948SXiao Guangrong aml_append(ifctx, aml_store(aml_index(pckg, aml_int(0)), pckg_index)); 7314568c948SXiao Guangrong aml_append(ifctx, aml_store(aml_derefof(pckg_index), pckg_buf)); 7324568c948SXiao Guangrong aml_append(ifctx, aml_store(pckg_buf, aml_name("ARG3"))); 7334568c948SXiao Guangrong aml_append(method, ifctx); 7344568c948SXiao Guangrong 7354568c948SXiao Guangrong /* 73618c440e1SXiao Guangrong * tell QEMU about the real address of DSM memory, then QEMU 73718c440e1SXiao Guangrong * gets the control and fills the result in DSM memory. 73818c440e1SXiao Guangrong */ 73918c440e1SXiao Guangrong aml_append(method, aml_store(dsm_mem, aml_name("NTFI"))); 74018c440e1SXiao Guangrong 74118c440e1SXiao Guangrong result_size = aml_local(1); 74218c440e1SXiao Guangrong aml_append(method, aml_store(aml_name("RLEN"), result_size)); 74318c440e1SXiao Guangrong aml_append(method, aml_store(aml_shiftleft(result_size, aml_int(3)), 74418c440e1SXiao Guangrong result_size)); 74518c440e1SXiao Guangrong aml_append(method, aml_create_field(aml_name("ODAT"), aml_int(0), 74618c440e1SXiao Guangrong result_size, "OBUF")); 74718c440e1SXiao Guangrong aml_append(method, aml_concatenate(aml_buffer(0, NULL), aml_name("OBUF"), 74818c440e1SXiao Guangrong aml_arg(6))); 74918c440e1SXiao Guangrong aml_append(method, aml_return(aml_arg(6))); 75077286395SXiao Guangrong aml_append(dev, method); 75177286395SXiao Guangrong } 75277286395SXiao Guangrong 753732b530cSXiao Guangrong static void nvdimm_build_device_dsm(Aml *dev, uint32_t handle) 75477286395SXiao Guangrong { 75577286395SXiao Guangrong Aml *method; 75677286395SXiao Guangrong 75777286395SXiao Guangrong method = aml_method("_DSM", 4, AML_NOTSERIALIZED); 758732b530cSXiao Guangrong aml_append(method, aml_return(aml_call5(NVDIMM_COMMON_DSM, aml_arg(0), 759732b530cSXiao Guangrong aml_arg(1), aml_arg(2), aml_arg(3), 760732b530cSXiao Guangrong aml_int(handle)))); 76177286395SXiao Guangrong aml_append(dev, method); 76277286395SXiao Guangrong } 76377286395SXiao Guangrong 76477286395SXiao Guangrong static void nvdimm_build_nvdimm_devices(GSList *device_list, Aml *root_dev) 76577286395SXiao Guangrong { 76677286395SXiao Guangrong for (; device_list; device_list = device_list->next) { 76777286395SXiao Guangrong DeviceState *dev = device_list->data; 76877286395SXiao Guangrong int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, 76977286395SXiao Guangrong NULL); 77077286395SXiao Guangrong uint32_t handle = nvdimm_slot_to_handle(slot); 77177286395SXiao Guangrong Aml *nvdimm_dev; 77277286395SXiao Guangrong 77377286395SXiao Guangrong nvdimm_dev = aml_device("NV%02X", slot); 77477286395SXiao Guangrong 77577286395SXiao Guangrong /* 77677286395SXiao Guangrong * ACPI 6.0: 9.20 NVDIMM Devices: 77777286395SXiao Guangrong * 77877286395SXiao Guangrong * _ADR object that is used to supply OSPM with unique address 77977286395SXiao Guangrong * of the NVDIMM device. This is done by returning the NFIT Device 78077286395SXiao Guangrong * handle that is used to identify the associated entries in ACPI 78177286395SXiao Guangrong * table NFIT or _FIT. 78277286395SXiao Guangrong */ 78377286395SXiao Guangrong aml_append(nvdimm_dev, aml_name_decl("_ADR", aml_int(handle))); 78477286395SXiao Guangrong 785732b530cSXiao Guangrong nvdimm_build_device_dsm(nvdimm_dev, handle); 78677286395SXiao Guangrong aml_append(root_dev, nvdimm_dev); 78777286395SXiao Guangrong } 78877286395SXiao Guangrong } 78977286395SXiao Guangrong 79077286395SXiao Guangrong static void nvdimm_build_ssdt(GSList *device_list, GArray *table_offsets, 791ad9671b8SIgor Mammedov GArray *table_data, BIOSLinker *linker, 792ad9671b8SIgor Mammedov GArray *dsm_dma_arrea) 79377286395SXiao Guangrong { 79418c440e1SXiao Guangrong Aml *ssdt, *sb_scope, *dev, *field; 795b9951413SXiao Guangrong int mem_addr_offset, nvdimm_ssdt; 79677286395SXiao Guangrong 79777286395SXiao Guangrong acpi_add_table(table_offsets, table_data); 79877286395SXiao Guangrong 79977286395SXiao Guangrong ssdt = init_aml_allocator(); 80077286395SXiao Guangrong acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader)); 80177286395SXiao Guangrong 80277286395SXiao Guangrong sb_scope = aml_scope("\\_SB"); 80377286395SXiao Guangrong 80477286395SXiao Guangrong dev = aml_device("NVDR"); 80577286395SXiao Guangrong 80677286395SXiao Guangrong /* 80777286395SXiao Guangrong * ACPI 6.0: 9.20 NVDIMM Devices: 80877286395SXiao Guangrong * 80977286395SXiao Guangrong * The ACPI Name Space device uses _HID of ACPI0012 to identify the root 81077286395SXiao Guangrong * NVDIMM interface device. Platform firmware is required to contain one 81177286395SXiao Guangrong * such device in _SB scope if NVDIMMs support is exposed by platform to 81277286395SXiao Guangrong * OSPM. 81377286395SXiao Guangrong * For each NVDIMM present or intended to be supported by platform, 81477286395SXiao Guangrong * platform firmware also exposes an ACPI Namespace Device under the 81577286395SXiao Guangrong * root device. 81677286395SXiao Guangrong */ 81777286395SXiao Guangrong aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0012"))); 81877286395SXiao Guangrong 81918c440e1SXiao Guangrong /* map DSM memory and IO into ACPI namespace. */ 82018c440e1SXiao Guangrong aml_append(dev, aml_operation_region("NPIO", AML_SYSTEM_IO, 82118c440e1SXiao Guangrong aml_int(NVDIMM_ACPI_IO_BASE), NVDIMM_ACPI_IO_LEN)); 82218c440e1SXiao Guangrong aml_append(dev, aml_operation_region("NRAM", AML_SYSTEM_MEMORY, 82335c5a52dSPaolo Bonzini aml_name(NVDIMM_ACPI_MEM_ADDR), sizeof(NvdimmDsmIn))); 82418c440e1SXiao Guangrong 82518c440e1SXiao Guangrong /* 82618c440e1SXiao Guangrong * DSM notifier: 82718c440e1SXiao Guangrong * NTFI: write the address of DSM memory and notify QEMU to emulate 82818c440e1SXiao Guangrong * the access. 82918c440e1SXiao Guangrong * 83018c440e1SXiao Guangrong * It is the IO port so that accessing them will cause VM-exit, the 83118c440e1SXiao Guangrong * control will be transferred to QEMU. 83218c440e1SXiao Guangrong */ 83318c440e1SXiao Guangrong field = aml_field("NPIO", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE); 83418c440e1SXiao Guangrong aml_append(field, aml_named_field("NTFI", 83518c440e1SXiao Guangrong sizeof(uint32_t) * BITS_PER_BYTE)); 83618c440e1SXiao Guangrong aml_append(dev, field); 83718c440e1SXiao Guangrong 83818c440e1SXiao Guangrong /* 83918c440e1SXiao Guangrong * DSM input: 84018c440e1SXiao Guangrong * HDLE: store device's handle, it's zero if the _DSM call happens 84118c440e1SXiao Guangrong * on NVDIMM Root Device. 84218c440e1SXiao Guangrong * REVS: store the Arg1 of _DSM call. 84318c440e1SXiao Guangrong * FUNC: store the Arg2 of _DSM call. 84418c440e1SXiao Guangrong * ARG3: store the Arg3 of _DSM call. 84518c440e1SXiao Guangrong * 84618c440e1SXiao Guangrong * They are RAM mapping on host so that these accesses never cause 84718c440e1SXiao Guangrong * VM-EXIT. 84818c440e1SXiao Guangrong */ 84918c440e1SXiao Guangrong field = aml_field("NRAM", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE); 85018c440e1SXiao Guangrong aml_append(field, aml_named_field("HDLE", 85118c440e1SXiao Guangrong sizeof(typeof_field(NvdimmDsmIn, handle)) * BITS_PER_BYTE)); 85218c440e1SXiao Guangrong aml_append(field, aml_named_field("REVS", 85318c440e1SXiao Guangrong sizeof(typeof_field(NvdimmDsmIn, revision)) * BITS_PER_BYTE)); 85418c440e1SXiao Guangrong aml_append(field, aml_named_field("FUNC", 85518c440e1SXiao Guangrong sizeof(typeof_field(NvdimmDsmIn, function)) * BITS_PER_BYTE)); 85618c440e1SXiao Guangrong aml_append(field, aml_named_field("ARG3", 85735c5a52dSPaolo Bonzini (sizeof(NvdimmDsmIn) - offsetof(NvdimmDsmIn, arg3)) * BITS_PER_BYTE)); 85818c440e1SXiao Guangrong aml_append(dev, field); 85918c440e1SXiao Guangrong 86018c440e1SXiao Guangrong /* 86118c440e1SXiao Guangrong * DSM output: 86218c440e1SXiao Guangrong * RLEN: the size of the buffer filled by QEMU. 86318c440e1SXiao Guangrong * ODAT: the buffer QEMU uses to store the result. 86418c440e1SXiao Guangrong * 86518c440e1SXiao Guangrong * Since the page is reused by both input and out, the input data 86618c440e1SXiao Guangrong * will be lost after storing new result into ODAT so we should fetch 86718c440e1SXiao Guangrong * all the input data before writing the result. 86818c440e1SXiao Guangrong */ 86918c440e1SXiao Guangrong field = aml_field("NRAM", AML_DWORD_ACC, AML_NOLOCK, AML_PRESERVE); 87018c440e1SXiao Guangrong aml_append(field, aml_named_field("RLEN", 87118c440e1SXiao Guangrong sizeof(typeof_field(NvdimmDsmOut, len)) * BITS_PER_BYTE)); 87218c440e1SXiao Guangrong aml_append(field, aml_named_field("ODAT", 87335c5a52dSPaolo Bonzini (sizeof(NvdimmDsmOut) - offsetof(NvdimmDsmOut, data)) * BITS_PER_BYTE)); 87418c440e1SXiao Guangrong aml_append(dev, field); 87518c440e1SXiao Guangrong 87677286395SXiao Guangrong nvdimm_build_common_dsm(dev); 877732b530cSXiao Guangrong 878732b530cSXiao Guangrong /* 0 is reserved for root device. */ 879732b530cSXiao Guangrong nvdimm_build_device_dsm(dev, 0); 88077286395SXiao Guangrong 88177286395SXiao Guangrong nvdimm_build_nvdimm_devices(device_list, dev); 88277286395SXiao Guangrong 88377286395SXiao Guangrong aml_append(sb_scope, dev); 88477286395SXiao Guangrong aml_append(ssdt, sb_scope); 885b9951413SXiao Guangrong 886b9951413SXiao Guangrong nvdimm_ssdt = table_data->len; 887b9951413SXiao Guangrong 88877286395SXiao Guangrong /* copy AML table into ACPI tables blob and patch header there */ 88977286395SXiao Guangrong g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len); 890b9951413SXiao Guangrong mem_addr_offset = build_append_named_dword(table_data, 891b9951413SXiao Guangrong NVDIMM_ACPI_MEM_ADDR); 892b9951413SXiao Guangrong 893ad9671b8SIgor Mammedov bios_linker_loader_alloc(linker, 894ad9671b8SIgor Mammedov NVDIMM_DSM_MEM_FILE, dsm_dma_arrea, 895ad9671b8SIgor Mammedov sizeof(NvdimmDsmIn), false /* high memory */); 8964678124bSIgor Mammedov bios_linker_loader_add_pointer(linker, 8974678124bSIgor Mammedov ACPI_BUILD_TABLE_FILE, mem_addr_offset, sizeof(uint32_t), 8984678124bSIgor Mammedov NVDIMM_DSM_MEM_FILE, 0); 89977286395SXiao Guangrong build_header(linker, table_data, 900b9951413SXiao Guangrong (void *)(table_data->data + nvdimm_ssdt), 901b9951413SXiao Guangrong "SSDT", table_data->len - nvdimm_ssdt, 1, NULL, "NVDIMM"); 90277286395SXiao Guangrong free_aml_allocator(); 90377286395SXiao Guangrong } 90477286395SXiao Guangrong 90587252e1bSXiao Guangrong void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data, 906ad9671b8SIgor Mammedov BIOSLinker *linker, GArray *dsm_dma_arrea) 90787252e1bSXiao Guangrong { 90887252e1bSXiao Guangrong GSList *device_list; 90987252e1bSXiao Guangrong 91087252e1bSXiao Guangrong /* no NVDIMM device is plugged. */ 91187252e1bSXiao Guangrong device_list = nvdimm_get_plugged_device_list(); 91287252e1bSXiao Guangrong if (!device_list) { 91387252e1bSXiao Guangrong return; 91487252e1bSXiao Guangrong } 91587252e1bSXiao Guangrong nvdimm_build_nfit(device_list, table_offsets, table_data, linker); 916ad9671b8SIgor Mammedov nvdimm_build_ssdt(device_list, table_offsets, table_data, linker, 917ad9671b8SIgor Mammedov dsm_dma_arrea); 91887252e1bSXiao Guangrong g_slist_free(device_list); 91987252e1bSXiao Guangrong } 920