xref: /qemu/include/hw/mem/nvdimm.h (revision 336d354b)
1 /*
2  * Non-Volatile Dual In-line Memory Module Virtualization Implementation
3  *
4  * Copyright(C) 2015 Intel Corporation.
5  *
6  * Author:
7  *  Xiao Guangrong <guangrong.xiao@linux.intel.com>
8  *
9  * NVDIMM specifications and some documents can be found at:
10  * NVDIMM ACPI device and NFIT are introduced in ACPI 6:
11  *      http://www.uefi.org/sites/default/files/resources/ACPI_6.0.pdf
12  * NVDIMM Namespace specification:
13  *      http://pmem.io/documents/NVDIMM_Namespace_Spec.pdf
14  * DSM Interface Example:
15  *      http://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
16  * Driver Writer's Guide:
17  *      http://pmem.io/documents/NVDIMM_Driver_Writers_Guide.pdf
18  *
19  * This work is licensed under the terms of the GNU GPL, version 2 or later.
20  * See the COPYING file in the top-level directory.
21  */
22 
23 #ifndef QEMU_NVDIMM_H
24 #define QEMU_NVDIMM_H
25 
26 #include "hw/mem/pc-dimm.h"
27 #include "hw/acpi/bios-linker-loader.h"
28 #include "qemu/uuid.h"
29 #include "hw/acpi/aml-build.h"
30 #include "qom/object.h"
31 
32 #define NVDIMM_DEBUG 0
33 #define nvdimm_debug(fmt, ...)                                \
34     do {                                                      \
35         if (NVDIMM_DEBUG) {                                   \
36             fprintf(stderr, "nvdimm: " fmt, ## __VA_ARGS__);  \
37         }                                                     \
38     } while (0)
39 
40 /*
41  * The minimum label data size is required by NVDIMM Namespace
42  * specification, see the chapter 2 Namespaces:
43  *   "NVDIMMs following the NVDIMM Block Mode Specification use an area
44  *    at least 128KB in size, which holds around 1000 labels."
45  */
46 #define MIN_NAMESPACE_LABEL_SIZE      (128UL << 10)
47 
48 #define TYPE_NVDIMM      "nvdimm"
49 OBJECT_DECLARE_TYPE(NVDIMMDevice, NVDIMMClass, NVDIMM)
50 
51 #define NVDIMM_LABEL_SIZE_PROP "label-size"
52 #define NVDIMM_UUID_PROP       "uuid"
53 #define NVDIMM_UNARMED_PROP    "unarmed"
54 
55 struct NVDIMMDevice {
56     /* private */
57     PCDIMMDevice parent_obj;
58 
59     /* public */
60 
61     /*
62      * the size of label data in NVDIMM device which is presented to
63      * guest via __DSM "Get Namespace Label Size" function.
64      */
65     uint64_t label_size;
66 
67     /*
68      * the address of label data which is read by __DSM "Get Namespace
69      * Label Data" function and written by __DSM "Set Namespace Label
70      * Data" function.
71      */
72     void *label_data;
73 
74     /*
75      * it's the PMEM region in NVDIMM device, which is presented to
76      * guest via ACPI NFIT and _FIT method if NVDIMM hotplug is supported.
77      */
78     MemoryRegion *nvdimm_mr;
79 
80     /*
81      * The 'on' value results in the unarmed flag set in ACPI NFIT,
82      * which can be used to notify guest implicitly that the host
83      * backend (e.g., files on HDD, /dev/pmemX, etc.) cannot guarantee
84      * the guest write persistence.
85      */
86     bool unarmed;
87 
88     /*
89      * The PPC64 - spapr requires each nvdimm device have a uuid.
90      */
91     QemuUUID uuid;
92 };
93 
94 struct NVDIMMClass {
95     /* private */
96     PCDIMMDeviceClass parent_class;
97 
98     /* public */
99 
100     /* read @size bytes from NVDIMM label data at @offset into @buf. */
101     void (*read_label_data)(NVDIMMDevice *nvdimm, void *buf,
102                             uint64_t size, uint64_t offset);
103     /* write @size bytes from @buf to NVDIMM label data at @offset. */
104     void (*write_label_data)(NVDIMMDevice *nvdimm, const void *buf,
105                              uint64_t size, uint64_t offset);
106     void (*realize)(NVDIMMDevice *nvdimm, Error **errp);
107     void (*unrealize)(NVDIMMDevice *nvdimm);
108 };
109 
110 #define NVDIMM_DSM_MEM_FILE     "etc/acpi/nvdimm-mem"
111 
112 /*
113  * 32 bits IO port starting from 0x0a18 in guest is reserved for
114  * NVDIMM ACPI emulation.
115  */
116 #define NVDIMM_ACPI_IO_BASE     0x0a18
117 #define NVDIMM_ACPI_IO_LEN      4
118 
119 /*
120  * NvdimmFitBuffer:
121  * @fit: FIT structures for present NVDIMMs. It is updated when
122  *   the NVDIMM device is plugged or unplugged.
123  * @dirty: It allows OSPM to detect change and restart read in
124  *   progress if there is any.
125  */
126 struct NvdimmFitBuffer {
127     GArray *fit;
128     bool dirty;
129 };
130 typedef struct NvdimmFitBuffer NvdimmFitBuffer;
131 
132 struct NVDIMMState {
133     /* detect if NVDIMM support is enabled. */
134     bool is_enabled;
135 
136     /* the data of the fw_cfg file NVDIMM_DSM_MEM_FILE. */
137     GArray *dsm_mem;
138 
139     NvdimmFitBuffer fit_buf;
140 
141     /* the IO region used by OSPM to transfer control to QEMU. */
142     MemoryRegion io_mr;
143 
144     /*
145      * Platform capabilities, section 5.2.25.9 of ACPI 6.2 Errata A
146      */
147     int32_t persistence;
148     char    *persistence_string;
149     struct AcpiGenericAddress dsm_io;
150 };
151 typedef struct NVDIMMState NVDIMMState;
152 
153 void nvdimm_init_acpi_state(NVDIMMState *state, MemoryRegion *io,
154                             struct AcpiGenericAddress dsm_io,
155                             FWCfgState *fw_cfg, Object *owner);
156 void nvdimm_build_srat(GArray *table_data);
157 void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
158                        BIOSLinker *linker, NVDIMMState *state,
159                        uint32_t ram_slots, const char *oem_id,
160                        const char *oem_table_id);
161 void nvdimm_plug(NVDIMMState *state);
162 void nvdimm_acpi_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev);
163 #endif
164