xref: /openbmc/qemu/include/hw/mem/nvdimm.h (revision 5964ed56)
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 
30 #define NVDIMM_DEBUG 0
31 #define nvdimm_debug(fmt, ...)                                \
32     do {                                                      \
33         if (NVDIMM_DEBUG) {                                   \
34             fprintf(stderr, "nvdimm: " fmt, ## __VA_ARGS__);  \
35         }                                                     \
36     } while (0)
37 
38 /*
39  * The minimum label data size is required by NVDIMM Namespace
40  * specification, see the chapter 2 Namespaces:
41  *   "NVDIMMs following the NVDIMM Block Mode Specification use an area
42  *    at least 128KB in size, which holds around 1000 labels."
43  */
44 #define MIN_NAMESPACE_LABEL_SIZE      (128UL << 10)
45 
46 #define TYPE_NVDIMM      "nvdimm"
47 #define NVDIMM(obj)      OBJECT_CHECK(NVDIMMDevice, (obj), TYPE_NVDIMM)
48 #define NVDIMM_CLASS(oc) OBJECT_CLASS_CHECK(NVDIMMClass, (oc), TYPE_NVDIMM)
49 #define NVDIMM_GET_CLASS(obj) OBJECT_GET_CLASS(NVDIMMClass, (obj), \
50                                                TYPE_NVDIMM)
51 
52 #define NVDIMM_LABEL_SIZE_PROP "label-size"
53 #define NVDIMM_UUID_PROP       "uuid"
54 #define NVDIMM_UNARMED_PROP    "unarmed"
55 
56 struct NVDIMMDevice {
57     /* private */
58     PCDIMMDevice parent_obj;
59 
60     /* public */
61 
62     /*
63      * the size of label data in NVDIMM device which is presented to
64      * guest via __DSM "Get Namespace Label Size" function.
65      */
66     uint64_t label_size;
67 
68     /*
69      * the address of label data which is read by __DSM "Get Namespace
70      * Label Data" function and written by __DSM "Set Namespace Label
71      * Data" function.
72      */
73     void *label_data;
74 
75     /*
76      * it's the PMEM region in NVDIMM device, which is presented to
77      * guest via ACPI NFIT and _FIT method if NVDIMM hotplug is supported.
78      */
79     MemoryRegion *nvdimm_mr;
80 
81     /*
82      * The 'on' value results in the unarmed flag set in ACPI NFIT,
83      * which can be used to notify guest implicitly that the host
84      * backend (e.g., files on HDD, /dev/pmemX, etc.) cannot guarantee
85      * the guest write persistence.
86      */
87     bool unarmed;
88 
89     /*
90      * The PPC64 - spapr requires each nvdimm device have a uuid.
91      */
92     QemuUUID uuid;
93 };
94 typedef struct NVDIMMDevice NVDIMMDevice;
95 
96 struct NVDIMMClass {
97     /* private */
98     PCDIMMDeviceClass parent_class;
99 
100     /* public */
101 
102     /* read @size bytes from NVDIMM label data at @offset into @buf. */
103     void (*read_label_data)(NVDIMMDevice *nvdimm, void *buf,
104                             uint64_t size, uint64_t offset);
105     /* write @size bytes from @buf to NVDIMM label data at @offset. */
106     void (*write_label_data)(NVDIMMDevice *nvdimm, const void *buf,
107                              uint64_t size, uint64_t offset);
108 };
109 typedef struct NVDIMMClass NVDIMMClass;
110 
111 #define NVDIMM_DSM_MEM_FILE     "etc/acpi/nvdimm-mem"
112 
113 /*
114  * 32 bits IO port starting from 0x0a18 in guest is reserved for
115  * NVDIMM ACPI emulation.
116  */
117 #define NVDIMM_ACPI_IO_BASE     0x0a18
118 #define NVDIMM_ACPI_IO_LEN      4
119 
120 /*
121  * NvdimmFitBuffer:
122  * @fit: FIT structures for present NVDIMMs. It is updated when
123  *   the NVDIMM device is plugged or unplugged.
124  * @dirty: It allows OSPM to detect change and restart read in
125  *   progress if there is any.
126  */
127 struct NvdimmFitBuffer {
128     GArray *fit;
129     bool dirty;
130 };
131 typedef struct NvdimmFitBuffer NvdimmFitBuffer;
132 
133 struct NVDIMMState {
134     /* detect if NVDIMM support is enabled. */
135     bool is_enabled;
136 
137     /* the data of the fw_cfg file NVDIMM_DSM_MEM_FILE. */
138     GArray *dsm_mem;
139 
140     NvdimmFitBuffer fit_buf;
141 
142     /* the IO region used by OSPM to transfer control to QEMU. */
143     MemoryRegion io_mr;
144 
145     /*
146      * Platform capabilities, section 5.2.25.9 of ACPI 6.2 Errata A
147      */
148     int32_t persistence;
149     char    *persistence_string;
150 };
151 typedef struct NVDIMMState NVDIMMState;
152 
153 void nvdimm_init_acpi_state(NVDIMMState *state, MemoryRegion *io,
154                             FWCfgState *fw_cfg, Object *owner);
155 void nvdimm_build_acpi(GArray *table_offsets, GArray *table_data,
156                        BIOSLinker *linker, NVDIMMState *state,
157                        uint32_t ram_slots);
158 void nvdimm_plug(NVDIMMState *state);
159 void nvdimm_acpi_plug_cb(HotplugHandler *hotplug_dev, DeviceState *dev);
160 #endif
161