nvdimm acpi: use common macros instead of magic names
[qemu/kevin.git] / hw / acpi / nvdimm.c
blobbb896c9dcc9ee84a9d16f987220e655071d75a6e
1 /*
2 * NVDIMM ACPI Implementation
4 * Copyright(C) 2015 Intel Corporation.
6 * Author:
7 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
9 * NFIT is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
10 * and the DSM specification can be found at:
11 * http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
13 * Currently, it only supports PMEM Virtualization.
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, see <http://www.gnu.org/licenses/>
29 #include "qemu/osdep.h"
30 #include "hw/acpi/acpi.h"
31 #include "hw/acpi/aml-build.h"
32 #include "hw/acpi/bios-linker-loader.h"
33 #include "hw/nvram/fw_cfg.h"
34 #include "hw/mem/nvdimm.h"
36 static int nvdimm_plugged_device_list(Object *obj, void *opaque)
38 GSList **list = opaque;
40 if (object_dynamic_cast(obj, TYPE_NVDIMM)) {
41 DeviceState *dev = DEVICE(obj);
43 if (dev->realized) { /* only realized NVDIMMs matter */
44 *list = g_slist_append(*list, DEVICE(obj));
48 object_child_foreach(obj, nvdimm_plugged_device_list, opaque);
49 return 0;
53 * inquire plugged NVDIMM devices and link them into the list which is
54 * returned to the caller.
56 * Note: it is the caller's responsibility to free the list to avoid
57 * memory leak.
59 static GSList *nvdimm_get_plugged_device_list(void)
61 GSList *list = NULL;
63 object_child_foreach(qdev_get_machine(), nvdimm_plugged_device_list,
64 &list);
65 return list;
68 #define NVDIMM_UUID_LE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
69 { (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
70 (b) & 0xff, ((b) >> 8) & 0xff, (c) & 0xff, ((c) >> 8) & 0xff, \
71 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }
74 * define Byte Addressable Persistent Memory (PM) Region according to
75 * ACPI 6.0: 5.2.25.1 System Physical Address Range Structure.
77 static const uint8_t nvdimm_nfit_spa_uuid[] =
78 NVDIMM_UUID_LE(0x66f0d379, 0xb4f3, 0x4074, 0xac, 0x43, 0x0d, 0x33,
79 0x18, 0xb7, 0x8c, 0xdb);
82 * NVDIMM Firmware Interface Table
83 * @signature: "NFIT"
85 * It provides information that allows OSPM to enumerate NVDIMM present in
86 * the platform and associate system physical address ranges created by the
87 * NVDIMMs.
89 * It is defined in ACPI 6.0: 5.2.25 NVDIMM Firmware Interface Table (NFIT)
91 struct NvdimmNfitHeader {
92 ACPI_TABLE_HEADER_DEF
93 uint32_t reserved;
94 } QEMU_PACKED;
95 typedef struct NvdimmNfitHeader NvdimmNfitHeader;
98 * define NFIT structures according to ACPI 6.0: 5.2.25 NVDIMM Firmware
99 * Interface Table (NFIT).
103 * System Physical Address Range Structure
105 * It describes the system physical address ranges occupied by NVDIMMs and
106 * the types of the regions.
108 struct NvdimmNfitSpa {
109 uint16_t type;
110 uint16_t length;
111 uint16_t spa_index;
112 uint16_t flags;
113 uint32_t reserved;
114 uint32_t proximity_domain;
115 uint8_t type_guid[16];
116 uint64_t spa_base;
117 uint64_t spa_length;
118 uint64_t mem_attr;
119 } QEMU_PACKED;
120 typedef struct NvdimmNfitSpa NvdimmNfitSpa;
123 * Memory Device to System Physical Address Range Mapping Structure
125 * It enables identifying each NVDIMM region and the corresponding SPA
126 * describing the memory interleave
128 struct NvdimmNfitMemDev {
129 uint16_t type;
130 uint16_t length;
131 uint32_t nfit_handle;
132 uint16_t phys_id;
133 uint16_t region_id;
134 uint16_t spa_index;
135 uint16_t dcr_index;
136 uint64_t region_len;
137 uint64_t region_offset;
138 uint64_t region_dpa;
139 uint16_t interleave_index;
140 uint16_t interleave_ways;
141 uint16_t flags;
142 uint16_t reserved;
143 } QEMU_PACKED;
144 typedef struct NvdimmNfitMemDev NvdimmNfitMemDev;
147 * NVDIMM Control Region Structure
149 * It describes the NVDIMM and if applicable, Block Control Window.
151 struct NvdimmNfitControlRegion {
152 uint16_t type;
153 uint16_t length;
154 uint16_t dcr_index;
155 uint16_t vendor_id;
156 uint16_t device_id;
157 uint16_t revision_id;
158 uint16_t sub_vendor_id;
159 uint16_t sub_device_id;
160 uint16_t sub_revision_id;
161 uint8_t reserved[6];
162 uint32_t serial_number;
163 uint16_t fic;
164 uint16_t num_bcw;
165 uint64_t bcw_size;
166 uint64_t cmd_offset;
167 uint64_t cmd_size;
168 uint64_t status_offset;
169 uint64_t status_size;
170 uint16_t flags;
171 uint8_t reserved2[6];
172 } QEMU_PACKED;
173 typedef struct NvdimmNfitControlRegion NvdimmNfitControlRegion;
176 * Module serial number is a unique number for each device. We use the
177 * slot id of NVDIMM device to generate this number so that each device
178 * associates with a different number.
180 * 0x123456 is a magic number we arbitrarily chose.
182 static uint32_t nvdimm_slot_to_sn(int slot)
184 return 0x123456 + slot;
188 * handle is used to uniquely associate nfit_memdev structure with NVDIMM
189 * ACPI device - nfit_memdev.nfit_handle matches with the value returned
190 * by ACPI device _ADR method.
192 * We generate the handle with the slot id of NVDIMM device and reserve
193 * 0 for NVDIMM root device.
195 static uint32_t nvdimm_slot_to_handle(int slot)
197 return slot + 1;
201 * index uniquely identifies the structure, 0 is reserved which indicates
202 * that the structure is not valid or the associated structure is not
203 * present.
205 * Each NVDIMM device needs two indexes, one for nfit_spa and another for
206 * nfit_dc which are generated by the slot id of NVDIMM device.
208 static uint16_t nvdimm_slot_to_spa_index(int slot)
210 return (slot + 1) << 1;
213 /* See the comments of nvdimm_slot_to_spa_index(). */
214 static uint32_t nvdimm_slot_to_dcr_index(int slot)
216 return nvdimm_slot_to_spa_index(slot) + 1;
219 static NVDIMMDevice *nvdimm_get_device_by_handle(uint32_t handle)
221 NVDIMMDevice *nvdimm = NULL;
222 GSList *list, *device_list = nvdimm_get_plugged_device_list();
224 for (list = device_list; list; list = list->next) {
225 NVDIMMDevice *nvd = list->data;
226 int slot = object_property_get_int(OBJECT(nvd), PC_DIMM_SLOT_PROP,
227 NULL);
229 if (nvdimm_slot_to_handle(slot) == handle) {
230 nvdimm = nvd;
231 break;
235 g_slist_free(device_list);
236 return nvdimm;
239 /* ACPI 6.0: 5.2.25.1 System Physical Address Range Structure */
240 static void
241 nvdimm_build_structure_spa(GArray *structures, DeviceState *dev)
243 NvdimmNfitSpa *nfit_spa;
244 uint64_t addr = object_property_get_int(OBJECT(dev), PC_DIMM_ADDR_PROP,
245 NULL);
246 uint64_t size = object_property_get_int(OBJECT(dev), PC_DIMM_SIZE_PROP,
247 NULL);
248 uint32_t node = object_property_get_int(OBJECT(dev), PC_DIMM_NODE_PROP,
249 NULL);
250 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
251 NULL);
253 nfit_spa = acpi_data_push(structures, sizeof(*nfit_spa));
255 nfit_spa->type = cpu_to_le16(0 /* System Physical Address Range
256 Structure */);
257 nfit_spa->length = cpu_to_le16(sizeof(*nfit_spa));
258 nfit_spa->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
261 * Control region is strict as all the device info, such as SN, index,
262 * is associated with slot id.
264 nfit_spa->flags = cpu_to_le16(1 /* Control region is strictly for
265 management during hot add/online
266 operation */ |
267 2 /* Data in Proximity Domain field is
268 valid*/);
270 /* NUMA node. */
271 nfit_spa->proximity_domain = cpu_to_le32(node);
272 /* the region reported as PMEM. */
273 memcpy(nfit_spa->type_guid, nvdimm_nfit_spa_uuid,
274 sizeof(nvdimm_nfit_spa_uuid));
276 nfit_spa->spa_base = cpu_to_le64(addr);
277 nfit_spa->spa_length = cpu_to_le64(size);
279 /* It is the PMEM and can be cached as writeback. */
280 nfit_spa->mem_attr = cpu_to_le64(0x8ULL /* EFI_MEMORY_WB */ |
281 0x8000ULL /* EFI_MEMORY_NV */);
285 * ACPI 6.0: 5.2.25.2 Memory Device to System Physical Address Range Mapping
286 * Structure
288 static void
289 nvdimm_build_structure_memdev(GArray *structures, DeviceState *dev)
291 NvdimmNfitMemDev *nfit_memdev;
292 uint64_t size = object_property_get_int(OBJECT(dev), PC_DIMM_SIZE_PROP,
293 NULL);
294 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
295 NULL);
296 uint32_t handle = nvdimm_slot_to_handle(slot);
298 nfit_memdev = acpi_data_push(structures, sizeof(*nfit_memdev));
300 nfit_memdev->type = cpu_to_le16(1 /* Memory Device to System Address
301 Range Map Structure*/);
302 nfit_memdev->length = cpu_to_le16(sizeof(*nfit_memdev));
303 nfit_memdev->nfit_handle = cpu_to_le32(handle);
306 * associate memory device with System Physical Address Range
307 * Structure.
309 nfit_memdev->spa_index = cpu_to_le16(nvdimm_slot_to_spa_index(slot));
310 /* associate memory device with Control Region Structure. */
311 nfit_memdev->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
313 /* The memory region on the device. */
314 nfit_memdev->region_len = cpu_to_le64(size);
315 /* The device address starts from 0. */
316 nfit_memdev->region_dpa = cpu_to_le64(0);
318 /* Only one interleave for PMEM. */
319 nfit_memdev->interleave_ways = cpu_to_le16(1);
323 * ACPI 6.0: 5.2.25.5 NVDIMM Control Region Structure.
325 static void nvdimm_build_structure_dcr(GArray *structures, DeviceState *dev)
327 NvdimmNfitControlRegion *nfit_dcr;
328 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
329 NULL);
330 uint32_t sn = nvdimm_slot_to_sn(slot);
332 nfit_dcr = acpi_data_push(structures, sizeof(*nfit_dcr));
334 nfit_dcr->type = cpu_to_le16(4 /* NVDIMM Control Region Structure */);
335 nfit_dcr->length = cpu_to_le16(sizeof(*nfit_dcr));
336 nfit_dcr->dcr_index = cpu_to_le16(nvdimm_slot_to_dcr_index(slot));
338 /* vendor: Intel. */
339 nfit_dcr->vendor_id = cpu_to_le16(0x8086);
340 nfit_dcr->device_id = cpu_to_le16(1);
342 /* The _DSM method is following Intel's DSM specification. */
343 nfit_dcr->revision_id = cpu_to_le16(1 /* Current Revision supported
344 in ACPI 6.0 is 1. */);
345 nfit_dcr->serial_number = cpu_to_le32(sn);
346 nfit_dcr->fic = cpu_to_le16(0x201 /* Format Interface Code. See Chapter
347 2: NVDIMM Device Specific Method
348 (DSM) in DSM Spec Rev1.*/);
351 static GArray *nvdimm_build_device_structure(GSList *device_list)
353 GArray *structures = g_array_new(false, true /* clear */, 1);
355 for (; device_list; device_list = device_list->next) {
356 DeviceState *dev = device_list->data;
358 /* build System Physical Address Range Structure. */
359 nvdimm_build_structure_spa(structures, dev);
362 * build Memory Device to System Physical Address Range Mapping
363 * Structure.
365 nvdimm_build_structure_memdev(structures, dev);
367 /* build NVDIMM Control Region Structure. */
368 nvdimm_build_structure_dcr(structures, dev);
371 return structures;
374 static void nvdimm_build_nfit(GSList *device_list, GArray *table_offsets,
375 GArray *table_data, BIOSLinker *linker)
377 GArray *structures = nvdimm_build_device_structure(device_list);
378 unsigned int header;
380 acpi_add_table(table_offsets, table_data);
382 /* NFIT header. */
383 header = table_data->len;
384 acpi_data_push(table_data, sizeof(NvdimmNfitHeader));
385 /* NVDIMM device structures. */
386 g_array_append_vals(table_data, structures->data, structures->len);
388 build_header(linker, table_data,
389 (void *)(table_data->data + header), "NFIT",
390 sizeof(NvdimmNfitHeader) + structures->len, 1, NULL, NULL);
391 g_array_free(structures, true);
394 struct NvdimmDsmIn {
395 uint32_t handle;
396 uint32_t revision;
397 uint32_t function;
398 /* the remaining size in the page is used by arg3. */
399 union {
400 uint8_t arg3[4084];
402 } QEMU_PACKED;
403 typedef struct NvdimmDsmIn NvdimmDsmIn;
404 QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmIn) != 4096);
406 struct NvdimmDsmOut {
407 /* the size of buffer filled by QEMU. */
408 uint32_t len;
409 uint8_t data[4092];
410 } QEMU_PACKED;
411 typedef struct NvdimmDsmOut NvdimmDsmOut;
412 QEMU_BUILD_BUG_ON(sizeof(NvdimmDsmOut) != 4096);
414 struct NvdimmDsmFunc0Out {
415 /* the size of buffer filled by QEMU. */
416 uint32_t len;
417 uint32_t supported_func;
418 } QEMU_PACKED;
419 typedef struct NvdimmDsmFunc0Out NvdimmDsmFunc0Out;
421 struct NvdimmDsmFuncNoPayloadOut {
422 /* the size of buffer filled by QEMU. */
423 uint32_t len;
424 uint32_t func_ret_status;
425 } QEMU_PACKED;
426 typedef struct NvdimmDsmFuncNoPayloadOut NvdimmDsmFuncNoPayloadOut;
428 struct NvdimmFuncGetLabelSizeOut {
429 /* the size of buffer filled by QEMU. */
430 uint32_t len;
431 uint32_t func_ret_status; /* return status code. */
432 uint32_t label_size; /* the size of label data area. */
434 * Maximum size of the namespace label data length supported by
435 * the platform in Get/Set Namespace Label Data functions.
437 uint32_t max_xfer;
438 } QEMU_PACKED;
439 typedef struct NvdimmFuncGetLabelSizeOut NvdimmFuncGetLabelSizeOut;
440 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelSizeOut) > 4096);
442 struct NvdimmFuncGetLabelDataIn {
443 uint32_t offset; /* the offset in the namespace label data area. */
444 uint32_t length; /* the size of data is to be read via the function. */
445 } QEMU_PACKED;
446 typedef struct NvdimmFuncGetLabelDataIn NvdimmFuncGetLabelDataIn;
447 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataIn) +
448 offsetof(NvdimmDsmIn, arg3) > 4096);
450 struct NvdimmFuncGetLabelDataOut {
451 /* the size of buffer filled by QEMU. */
452 uint32_t len;
453 uint32_t func_ret_status; /* return status code. */
454 uint8_t out_buf[0]; /* the data got via Get Namesapce Label function. */
455 } QEMU_PACKED;
456 typedef struct NvdimmFuncGetLabelDataOut NvdimmFuncGetLabelDataOut;
457 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncGetLabelDataOut) > 4096);
459 struct NvdimmFuncSetLabelDataIn {
460 uint32_t offset; /* the offset in the namespace label data area. */
461 uint32_t length; /* the size of data is to be written via the function. */
462 uint8_t in_buf[0]; /* the data written to label data area. */
463 } QEMU_PACKED;
464 typedef struct NvdimmFuncSetLabelDataIn NvdimmFuncSetLabelDataIn;
465 QEMU_BUILD_BUG_ON(sizeof(NvdimmFuncSetLabelDataIn) +
466 offsetof(NvdimmDsmIn, arg3) > 4096);
468 static void
469 nvdimm_dsm_function0(uint32_t supported_func, hwaddr dsm_mem_addr)
471 NvdimmDsmFunc0Out func0 = {
472 .len = cpu_to_le32(sizeof(func0)),
473 .supported_func = cpu_to_le32(supported_func),
475 cpu_physical_memory_write(dsm_mem_addr, &func0, sizeof(func0));
478 static void
479 nvdimm_dsm_no_payload(uint32_t func_ret_status, hwaddr dsm_mem_addr)
481 NvdimmDsmFuncNoPayloadOut out = {
482 .len = cpu_to_le32(sizeof(out)),
483 .func_ret_status = cpu_to_le32(func_ret_status),
485 cpu_physical_memory_write(dsm_mem_addr, &out, sizeof(out));
488 static void nvdimm_dsm_root(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
491 * function 0 is called to inquire which functions are supported by
492 * OSPM
494 if (!in->function) {
495 nvdimm_dsm_function0(0 /* No function supported other than
496 function 0 */, dsm_mem_addr);
497 return;
500 /* No function except function 0 is supported yet. */
501 nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
505 * the max transfer size is the max size transferred by both a
506 * 'Get Namespace Label Data' function and a 'Set Namespace Label Data'
507 * function.
509 static uint32_t nvdimm_get_max_xfer_label_size(void)
511 uint32_t max_get_size, max_set_size, dsm_memory_size = 4096;
514 * the max data ACPI can read one time which is transferred by
515 * the response of 'Get Namespace Label Data' function.
517 max_get_size = dsm_memory_size - sizeof(NvdimmFuncGetLabelDataOut);
520 * the max data ACPI can write one time which is transferred by
521 * 'Set Namespace Label Data' function.
523 max_set_size = dsm_memory_size - offsetof(NvdimmDsmIn, arg3) -
524 sizeof(NvdimmFuncSetLabelDataIn);
526 return MIN(max_get_size, max_set_size);
530 * DSM Spec Rev1 4.4 Get Namespace Label Size (Function Index 4).
532 * It gets the size of Namespace Label data area and the max data size
533 * that Get/Set Namespace Label Data functions can transfer.
535 static void nvdimm_dsm_label_size(NVDIMMDevice *nvdimm, hwaddr dsm_mem_addr)
537 NvdimmFuncGetLabelSizeOut label_size_out = {
538 .len = cpu_to_le32(sizeof(label_size_out)),
540 uint32_t label_size, mxfer;
542 label_size = nvdimm->label_size;
543 mxfer = nvdimm_get_max_xfer_label_size();
545 nvdimm_debug("label_size %#x, max_xfer %#x.\n", label_size, mxfer);
547 label_size_out.func_ret_status = cpu_to_le32(0 /* Success */);
548 label_size_out.label_size = cpu_to_le32(label_size);
549 label_size_out.max_xfer = cpu_to_le32(mxfer);
551 cpu_physical_memory_write(dsm_mem_addr, &label_size_out,
552 sizeof(label_size_out));
555 static uint32_t nvdimm_rw_label_data_check(NVDIMMDevice *nvdimm,
556 uint32_t offset, uint32_t length)
558 uint32_t ret = 3 /* Invalid Input Parameters */;
560 if (offset + length < offset) {
561 nvdimm_debug("offset %#x + length %#x is overflow.\n", offset,
562 length);
563 return ret;
566 if (nvdimm->label_size < offset + length) {
567 nvdimm_debug("position %#x is beyond label data (len = %" PRIx64 ").\n",
568 offset + length, nvdimm->label_size);
569 return ret;
572 if (length > nvdimm_get_max_xfer_label_size()) {
573 nvdimm_debug("length (%#x) is larger than max_xfer (%#x).\n",
574 length, nvdimm_get_max_xfer_label_size());
575 return ret;
578 return 0 /* Success */;
582 * DSM Spec Rev1 4.5 Get Namespace Label Data (Function Index 5).
584 static void nvdimm_dsm_get_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
585 hwaddr dsm_mem_addr)
587 NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
588 NvdimmFuncGetLabelDataIn *get_label_data;
589 NvdimmFuncGetLabelDataOut *get_label_data_out;
590 uint32_t status;
591 int size;
593 get_label_data = (NvdimmFuncGetLabelDataIn *)in->arg3;
594 le32_to_cpus(&get_label_data->offset);
595 le32_to_cpus(&get_label_data->length);
597 nvdimm_debug("Read Label Data: offset %#x length %#x.\n",
598 get_label_data->offset, get_label_data->length);
600 status = nvdimm_rw_label_data_check(nvdimm, get_label_data->offset,
601 get_label_data->length);
602 if (status != 0 /* Success */) {
603 nvdimm_dsm_no_payload(status, dsm_mem_addr);
604 return;
607 size = sizeof(*get_label_data_out) + get_label_data->length;
608 assert(size <= 4096);
609 get_label_data_out = g_malloc(size);
611 get_label_data_out->len = cpu_to_le32(size);
612 get_label_data_out->func_ret_status = cpu_to_le32(0 /* Success */);
613 nvc->read_label_data(nvdimm, get_label_data_out->out_buf,
614 get_label_data->length, get_label_data->offset);
616 cpu_physical_memory_write(dsm_mem_addr, get_label_data_out, size);
617 g_free(get_label_data_out);
621 * DSM Spec Rev1 4.6 Set Namespace Label Data (Function Index 6).
623 static void nvdimm_dsm_set_label_data(NVDIMMDevice *nvdimm, NvdimmDsmIn *in,
624 hwaddr dsm_mem_addr)
626 NVDIMMClass *nvc = NVDIMM_GET_CLASS(nvdimm);
627 NvdimmFuncSetLabelDataIn *set_label_data;
628 uint32_t status;
630 set_label_data = (NvdimmFuncSetLabelDataIn *)in->arg3;
632 le32_to_cpus(&set_label_data->offset);
633 le32_to_cpus(&set_label_data->length);
635 nvdimm_debug("Write Label Data: offset %#x length %#x.\n",
636 set_label_data->offset, set_label_data->length);
638 status = nvdimm_rw_label_data_check(nvdimm, set_label_data->offset,
639 set_label_data->length);
640 if (status != 0 /* Success */) {
641 nvdimm_dsm_no_payload(status, dsm_mem_addr);
642 return;
645 assert(sizeof(*in) + sizeof(*set_label_data) + set_label_data->length <=
646 4096);
648 nvc->write_label_data(nvdimm, set_label_data->in_buf,
649 set_label_data->length, set_label_data->offset);
650 nvdimm_dsm_no_payload(0 /* Success */, dsm_mem_addr);
653 static void nvdimm_dsm_device(NvdimmDsmIn *in, hwaddr dsm_mem_addr)
655 NVDIMMDevice *nvdimm = nvdimm_get_device_by_handle(in->handle);
657 /* See the comments in nvdimm_dsm_root(). */
658 if (!in->function) {
659 uint32_t supported_func = 0;
661 if (nvdimm && nvdimm->label_size) {
662 supported_func |= 0x1 /* Bit 0 indicates whether there is
663 support for any functions other
664 than function 0. */ |
665 1 << 4 /* Get Namespace Label Size */ |
666 1 << 5 /* Get Namespace Label Data */ |
667 1 << 6 /* Set Namespace Label Data */;
669 nvdimm_dsm_function0(supported_func, dsm_mem_addr);
670 return;
673 if (!nvdimm) {
674 nvdimm_dsm_no_payload(2 /* Non-Existing Memory Device */,
675 dsm_mem_addr);
676 return;
679 /* Encode DSM function according to DSM Spec Rev1. */
680 switch (in->function) {
681 case 4 /* Get Namespace Label Size */:
682 if (nvdimm->label_size) {
683 nvdimm_dsm_label_size(nvdimm, dsm_mem_addr);
684 return;
686 break;
687 case 5 /* Get Namespace Label Data */:
688 if (nvdimm->label_size) {
689 nvdimm_dsm_get_label_data(nvdimm, in, dsm_mem_addr);
690 return;
692 break;
693 case 0x6 /* Set Namespace Label Data */:
694 if (nvdimm->label_size) {
695 nvdimm_dsm_set_label_data(nvdimm, in, dsm_mem_addr);
696 return;
698 break;
701 nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
704 static uint64_t
705 nvdimm_dsm_read(void *opaque, hwaddr addr, unsigned size)
707 nvdimm_debug("BUG: we never read _DSM IO Port.\n");
708 return 0;
711 static void
712 nvdimm_dsm_write(void *opaque, hwaddr addr, uint64_t val, unsigned size)
714 NvdimmDsmIn *in;
715 hwaddr dsm_mem_addr = val;
717 nvdimm_debug("dsm memory address %#" HWADDR_PRIx ".\n", dsm_mem_addr);
720 * The DSM memory is mapped to guest address space so an evil guest
721 * can change its content while we are doing DSM emulation. Avoid
722 * this by copying DSM memory to QEMU local memory.
724 in = g_new(NvdimmDsmIn, 1);
725 cpu_physical_memory_read(dsm_mem_addr, in, sizeof(*in));
727 le32_to_cpus(&in->revision);
728 le32_to_cpus(&in->function);
729 le32_to_cpus(&in->handle);
731 nvdimm_debug("Revision %#x Handler %#x Function %#x.\n", in->revision,
732 in->handle, in->function);
734 if (in->revision != 0x1 /* Currently we only support DSM Spec Rev1. */) {
735 nvdimm_debug("Revision %#x is not supported, expect %#x.\n",
736 in->revision, 0x1);
737 nvdimm_dsm_no_payload(1 /* Not Supported */, dsm_mem_addr);
738 goto exit;
741 /* Handle 0 is reserved for NVDIMM Root Device. */
742 if (!in->handle) {
743 nvdimm_dsm_root(in, dsm_mem_addr);
744 goto exit;
747 nvdimm_dsm_device(in, dsm_mem_addr);
749 exit:
750 g_free(in);
753 static const MemoryRegionOps nvdimm_dsm_ops = {
754 .read = nvdimm_dsm_read,
755 .write = nvdimm_dsm_write,
756 .endianness = DEVICE_LITTLE_ENDIAN,
757 .valid = {
758 .min_access_size = 4,
759 .max_access_size = 4,
763 void nvdimm_init_acpi_state(AcpiNVDIMMState *state, MemoryRegion *io,
764 FWCfgState *fw_cfg, Object *owner)
766 memory_region_init_io(&state->io_mr, owner, &nvdimm_dsm_ops, state,
767 "nvdimm-acpi-io", NVDIMM_ACPI_IO_LEN);
768 memory_region_add_subregion(io, NVDIMM_ACPI_IO_BASE, &state->io_mr);
770 state->dsm_mem = g_array_new(false, true /* clear */, 1);
771 acpi_data_push(state->dsm_mem, sizeof(NvdimmDsmIn));
772 fw_cfg_add_file(fw_cfg, NVDIMM_DSM_MEM_FILE, state->dsm_mem->data,
773 state->dsm_mem->len);
776 #define NVDIMM_COMMON_DSM "NCAL"
777 #define NVDIMM_ACPI_MEM_ADDR "MEMA"
779 #define NVDIMM_DSM_MEMORY "NRAM"
780 #define NVDIMM_DSM_IOPORT "NPIO"
782 #define NVDIMM_DSM_NOTIFY "NTFI"
783 #define NVDIMM_DSM_HANDLE "HDLE"
784 #define NVDIMM_DSM_REVISION "REVS"
785 #define NVDIMM_DSM_FUNCTION "FUNC"
786 #define NVDIMM_DSM_ARG3 "FARG"
788 #define NVDIMM_DSM_OUT_BUF_SIZE "RLEN"
789 #define NVDIMM_DSM_OUT_BUF "ODAT"
791 static void nvdimm_build_common_dsm(Aml *dev)
793 Aml *method, *ifctx, *function, *handle, *uuid, *dsm_mem;
794 Aml *elsectx, *unsupport, *unpatched, *expected_uuid, *uuid_invalid;
795 Aml *pckg, *pckg_index, *pckg_buf, *field, *dsm_out_buf, *dsm_out_buf_size;
796 uint8_t byte_list[1];
798 method = aml_method(NVDIMM_COMMON_DSM, 5, AML_SERIALIZED);
799 uuid = aml_arg(0);
800 function = aml_arg(2);
801 handle = aml_arg(4);
802 dsm_mem = aml_local(6);
803 dsm_out_buf = aml_local(7);
805 aml_append(method, aml_store(aml_name(NVDIMM_ACPI_MEM_ADDR), dsm_mem));
807 /* map DSM memory and IO into ACPI namespace. */
808 aml_append(method, aml_operation_region(NVDIMM_DSM_IOPORT, AML_SYSTEM_IO,
809 aml_int(NVDIMM_ACPI_IO_BASE), NVDIMM_ACPI_IO_LEN));
810 aml_append(method, aml_operation_region(NVDIMM_DSM_MEMORY,
811 AML_SYSTEM_MEMORY, dsm_mem, sizeof(NvdimmDsmIn)));
814 * DSM notifier:
815 * NVDIMM_DSM_NOTIFY: write the address of DSM memory and notify QEMU to
816 * emulate the access.
818 * It is the IO port so that accessing them will cause VM-exit, the
819 * control will be transferred to QEMU.
821 field = aml_field(NVDIMM_DSM_IOPORT, AML_DWORD_ACC, AML_NOLOCK,
822 AML_PRESERVE);
823 aml_append(field, aml_named_field(NVDIMM_DSM_NOTIFY,
824 sizeof(uint32_t) * BITS_PER_BYTE));
825 aml_append(method, field);
828 * DSM input:
829 * NVDIMM_DSM_HANDLE: store device's handle, it's zero if the _DSM call
830 * happens on NVDIMM Root Device.
831 * NVDIMM_DSM_REVISION: store the Arg1 of _DSM call.
832 * NVDIMM_DSM_FUNCTION: store the Arg2 of _DSM call.
833 * NVDIMM_DSM_ARG3: store the Arg3 of _DSM call which is a Package
834 * containing function-specific arguments.
836 * They are RAM mapping on host so that these accesses never cause
837 * VM-EXIT.
839 field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
840 AML_PRESERVE);
841 aml_append(field, aml_named_field(NVDIMM_DSM_HANDLE,
842 sizeof(typeof_field(NvdimmDsmIn, handle)) * BITS_PER_BYTE));
843 aml_append(field, aml_named_field(NVDIMM_DSM_REVISION,
844 sizeof(typeof_field(NvdimmDsmIn, revision)) * BITS_PER_BYTE));
845 aml_append(field, aml_named_field(NVDIMM_DSM_FUNCTION,
846 sizeof(typeof_field(NvdimmDsmIn, function)) * BITS_PER_BYTE));
847 aml_append(field, aml_named_field(NVDIMM_DSM_ARG3,
848 (sizeof(NvdimmDsmIn) - offsetof(NvdimmDsmIn, arg3)) * BITS_PER_BYTE));
849 aml_append(method, field);
852 * DSM output:
853 * NVDIMM_DSM_OUT_BUF_SIZE: the size of the buffer filled by QEMU.
854 * NVDIMM_DSM_OUT_BUF: the buffer QEMU uses to store the result.
856 * Since the page is reused by both input and out, the input data
857 * will be lost after storing new result into ODAT so we should fetch
858 * all the input data before writing the result.
860 field = aml_field(NVDIMM_DSM_MEMORY, AML_DWORD_ACC, AML_NOLOCK,
861 AML_PRESERVE);
862 aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF_SIZE,
863 sizeof(typeof_field(NvdimmDsmOut, len)) * BITS_PER_BYTE));
864 aml_append(field, aml_named_field(NVDIMM_DSM_OUT_BUF,
865 (sizeof(NvdimmDsmOut) - offsetof(NvdimmDsmOut, data)) * BITS_PER_BYTE));
866 aml_append(method, field);
869 * do not support any method if DSM memory address has not been
870 * patched.
872 unpatched = aml_equal(dsm_mem, aml_int(0x0));
874 expected_uuid = aml_local(0);
876 ifctx = aml_if(aml_equal(handle, aml_int(0x0)));
877 aml_append(ifctx, aml_store(
878 aml_touuid("2F10E7A4-9E91-11E4-89D3-123B93F75CBA")
879 /* UUID for NVDIMM Root Device */, expected_uuid));
880 aml_append(method, ifctx);
881 elsectx = aml_else();
882 aml_append(elsectx, aml_store(
883 aml_touuid("4309AC30-0D11-11E4-9191-0800200C9A66")
884 /* UUID for NVDIMM Devices */, expected_uuid));
885 aml_append(method, elsectx);
887 uuid_invalid = aml_lnot(aml_equal(uuid, expected_uuid));
889 unsupport = aml_if(aml_or(unpatched, uuid_invalid, NULL));
892 * function 0 is called to inquire what functions are supported by
893 * OSPM
895 ifctx = aml_if(aml_equal(function, aml_int(0)));
896 byte_list[0] = 0 /* No function Supported */;
897 aml_append(ifctx, aml_return(aml_buffer(1, byte_list)));
898 aml_append(unsupport, ifctx);
900 /* No function is supported yet. */
901 byte_list[0] = 1 /* Not Supported */;
902 aml_append(unsupport, aml_return(aml_buffer(1, byte_list)));
903 aml_append(method, unsupport);
906 * The HDLE indicates the DSM function is issued from which device,
907 * it reserves 0 for root device and is the handle for NVDIMM devices.
908 * See the comments in nvdimm_slot_to_handle().
910 aml_append(method, aml_store(handle, aml_name(NVDIMM_DSM_HANDLE)));
911 aml_append(method, aml_store(aml_arg(1), aml_name(NVDIMM_DSM_REVISION)));
912 aml_append(method, aml_store(aml_arg(2), aml_name(NVDIMM_DSM_FUNCTION)));
915 * The fourth parameter (Arg3) of _DSM is a package which contains
916 * a buffer, the layout of the buffer is specified by UUID (Arg0),
917 * Revision ID (Arg1) and Function Index (Arg2) which are documented
918 * in the DSM Spec.
920 pckg = aml_arg(3);
921 ifctx = aml_if(aml_and(aml_equal(aml_object_type(pckg),
922 aml_int(4 /* Package */)) /* It is a Package? */,
923 aml_equal(aml_sizeof(pckg), aml_int(1)) /* 1 element? */,
924 NULL));
926 pckg_index = aml_local(2);
927 pckg_buf = aml_local(3);
928 aml_append(ifctx, aml_store(aml_index(pckg, aml_int(0)), pckg_index));
929 aml_append(ifctx, aml_store(aml_derefof(pckg_index), pckg_buf));
930 aml_append(ifctx, aml_store(pckg_buf, aml_name(NVDIMM_DSM_ARG3)));
931 aml_append(method, ifctx);
934 * tell QEMU about the real address of DSM memory, then QEMU
935 * gets the control and fills the result in DSM memory.
937 aml_append(method, aml_store(dsm_mem, aml_name(NVDIMM_DSM_NOTIFY)));
939 dsm_out_buf_size = aml_local(1);
940 /* RLEN is not included in the payload returned to guest. */
941 aml_append(method, aml_subtract(aml_name(NVDIMM_DSM_OUT_BUF_SIZE),
942 aml_int(4), dsm_out_buf_size));
943 aml_append(method, aml_store(aml_shiftleft(dsm_out_buf_size, aml_int(3)),
944 dsm_out_buf_size));
945 aml_append(method, aml_create_field(aml_name(NVDIMM_DSM_OUT_BUF),
946 aml_int(0), dsm_out_buf_size, "OBUF"));
947 aml_append(method, aml_concatenate(aml_buffer(0, NULL), aml_name("OBUF"),
948 dsm_out_buf));
949 aml_append(method, aml_return(dsm_out_buf));
950 aml_append(dev, method);
953 static void nvdimm_build_device_dsm(Aml *dev, uint32_t handle)
955 Aml *method;
957 method = aml_method("_DSM", 4, AML_NOTSERIALIZED);
958 aml_append(method, aml_return(aml_call5(NVDIMM_COMMON_DSM, aml_arg(0),
959 aml_arg(1), aml_arg(2), aml_arg(3),
960 aml_int(handle))));
961 aml_append(dev, method);
964 static void nvdimm_build_nvdimm_devices(GSList *device_list, Aml *root_dev)
966 for (; device_list; device_list = device_list->next) {
967 DeviceState *dev = device_list->data;
968 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
969 NULL);
970 uint32_t handle = nvdimm_slot_to_handle(slot);
971 Aml *nvdimm_dev;
973 nvdimm_dev = aml_device("NV%02X", slot);
976 * ACPI 6.0: 9.20 NVDIMM Devices:
978 * _ADR object that is used to supply OSPM with unique address
979 * of the NVDIMM device. This is done by returning the NFIT Device
980 * handle that is used to identify the associated entries in ACPI
981 * table NFIT or _FIT.
983 aml_append(nvdimm_dev, aml_name_decl("_ADR", aml_int(handle)));
985 nvdimm_build_device_dsm(nvdimm_dev, handle);
986 aml_append(root_dev, nvdimm_dev);
990 static void nvdimm_build_ssdt(GSList *device_list, GArray *table_offsets,
991 GArray *table_data, BIOSLinker *linker,
992 GArray *dsm_dma_arrea)
994 Aml *ssdt, *sb_scope, *dev;
995 int mem_addr_offset, nvdimm_ssdt;
997 acpi_add_table(table_offsets, table_data);
999 ssdt = init_aml_allocator();
1000 acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
1002 sb_scope = aml_scope("\\_SB");
1004 dev = aml_device("NVDR");
1007 * ACPI 6.0: 9.20 NVDIMM Devices:
1009 * The ACPI Name Space device uses _HID of ACPI0012 to identify the root
1010 * NVDIMM interface device. Platform firmware is required to contain one
1011 * such device in _SB scope if NVDIMMs support is exposed by platform to
1012 * OSPM.
1013 * For each NVDIMM present or intended to be supported by platform,
1014 * platform firmware also exposes an ACPI Namespace Device under the
1015 * root device.
1017 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0012")));
1019 nvdimm_build_common_dsm(dev);
1021 /* 0 is reserved for root device. */
1022 nvdimm_build_device_dsm(dev, 0);
1024 nvdimm_build_nvdimm_devices(device_list, dev);
1026 aml_append(sb_scope, dev);
1027 aml_append(ssdt, sb_scope);
1029 nvdimm_ssdt = table_data->len;
1031 /* copy AML table into ACPI tables blob and patch header there */
1032 g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
1033 mem_addr_offset = build_append_named_dword(table_data,
1034 NVDIMM_ACPI_MEM_ADDR);
1036 bios_linker_loader_alloc(linker,
1037 NVDIMM_DSM_MEM_FILE, dsm_dma_arrea,
1038 sizeof(NvdimmDsmIn), false /* high memory */);
1039 bios_linker_loader_add_pointer(linker,
1040 ACPI_BUILD_TABLE_FILE, mem_addr_offset, sizeof(uint32_t),
1041 NVDIMM_DSM_MEM_FILE, 0);
1042 build_header(linker, table_data,
1043 (void *)(table_data->data + nvdimm_ssdt),
1044 "SSDT", table_data->len - nvdimm_ssdt, 1, NULL, "NVDIMM");
1045 free_aml_allocator();
1048 void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
1049 BIOSLinker *linker, GArray *dsm_dma_arrea)
1051 GSList *device_list;
1053 /* no NVDIMM device is plugged. */
1054 device_list = nvdimm_get_plugged_device_list();
1055 if (!device_list) {
1056 return;
1058 nvdimm_build_nfit(device_list, table_offsets, table_data, linker);
1059 nvdimm_build_ssdt(device_list, table_offsets, table_data, linker,
1060 dsm_dma_arrea);
1061 g_slist_free(device_list);