xref: /openbmc/qemu/hw/mem/nvdimm.c (revision 0c4e9931)
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  * Currently, it only supports PMEM Virtualization.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, see <http://www.gnu.org/licenses/>
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qemu/module.h"
27 #include "qemu/pmem.h"
28 #include "qapi/error.h"
29 #include "qapi/visitor.h"
30 #include "hw/mem/nvdimm.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/mem/memory-device.h"
33 #include "sysemu/hostmem.h"
34 
35 static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *name,
36                                   void *opaque, Error **errp)
37 {
38     NVDIMMDevice *nvdimm = NVDIMM(obj);
39     uint64_t value = nvdimm->label_size;
40 
41     visit_type_size(v, name, &value, errp);
42 }
43 
44 static void nvdimm_set_label_size(Object *obj, Visitor *v, const char *name,
45                                   void *opaque, Error **errp)
46 {
47     NVDIMMDevice *nvdimm = NVDIMM(obj);
48     Error *local_err = NULL;
49     uint64_t value;
50 
51     if (nvdimm->nvdimm_mr) {
52         error_setg(&local_err, "cannot change property value");
53         goto out;
54     }
55 
56     visit_type_size(v, name, &value, &local_err);
57     if (local_err) {
58         goto out;
59     }
60     if (value < MIN_NAMESPACE_LABEL_SIZE) {
61         error_setg(&local_err, "Property '%s.%s' (0x%" PRIx64 ") is required"
62                    " at least 0x%lx", object_get_typename(obj),
63                    name, value, MIN_NAMESPACE_LABEL_SIZE);
64         goto out;
65     }
66 
67     nvdimm->label_size = value;
68 out:
69     error_propagate(errp, local_err);
70 }
71 
72 static void nvdimm_get_uuid(Object *obj, Visitor *v, const char *name,
73                                   void *opaque, Error **errp)
74 {
75     NVDIMMDevice *nvdimm = NVDIMM(obj);
76     char *value = NULL;
77 
78     value = qemu_uuid_unparse_strdup(&nvdimm->uuid);
79 
80     visit_type_str(v, name, &value, errp);
81     g_free(value);
82 }
83 
84 
85 static void nvdimm_set_uuid(Object *obj, Visitor *v, const char *name,
86                                   void *opaque, Error **errp)
87 {
88     NVDIMMDevice *nvdimm = NVDIMM(obj);
89     Error *local_err = NULL;
90     char *value;
91 
92     visit_type_str(v, name, &value, &local_err);
93     if (local_err) {
94         goto out;
95     }
96 
97     if (qemu_uuid_parse(value, &nvdimm->uuid) != 0) {
98         error_setg(errp, "Property '%s.%s' has invalid value",
99                    object_get_typename(obj), name);
100         goto out;
101     }
102     g_free(value);
103 
104 out:
105     error_propagate(errp, local_err);
106 }
107 
108 
109 static void nvdimm_init(Object *obj)
110 {
111     object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int",
112                         nvdimm_get_label_size, nvdimm_set_label_size, NULL,
113                         NULL);
114 
115     object_property_add(obj, NVDIMM_UUID_PROP, "QemuUUID", nvdimm_get_uuid,
116                         nvdimm_set_uuid, NULL, NULL);
117 }
118 
119 static void nvdimm_finalize(Object *obj)
120 {
121     NVDIMMDevice *nvdimm = NVDIMM(obj);
122 
123     g_free(nvdimm->nvdimm_mr);
124 }
125 
126 static void nvdimm_prepare_memory_region(NVDIMMDevice *nvdimm, Error **errp)
127 {
128     PCDIMMDevice *dimm = PC_DIMM(nvdimm);
129     uint64_t align, pmem_size, size;
130     MemoryRegion *mr;
131 
132     g_assert(!nvdimm->nvdimm_mr);
133 
134     if (!dimm->hostmem) {
135         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
136         return;
137     }
138 
139     mr = host_memory_backend_get_memory(dimm->hostmem);
140     align = memory_region_get_alignment(mr);
141     size = memory_region_size(mr);
142 
143     pmem_size = size - nvdimm->label_size;
144     nvdimm->label_data = memory_region_get_ram_ptr(mr) + pmem_size;
145     pmem_size = QEMU_ALIGN_DOWN(pmem_size, align);
146 
147     if (size <= nvdimm->label_size || !pmem_size) {
148         HostMemoryBackend *hostmem = dimm->hostmem;
149         char *path = object_get_canonical_path_component(OBJECT(hostmem));
150 
151         error_setg(errp, "the size of memdev %s (0x%" PRIx64 ") is too "
152                    "small to contain nvdimm label (0x%" PRIx64 ") and "
153                    "aligned PMEM (0x%" PRIx64 ")",
154                    path, memory_region_size(mr), nvdimm->label_size, align);
155         g_free(path);
156         return;
157     }
158 
159     nvdimm->nvdimm_mr = g_new(MemoryRegion, 1);
160     memory_region_init_alias(nvdimm->nvdimm_mr, OBJECT(dimm),
161                              "nvdimm-memory", mr, 0, pmem_size);
162     memory_region_set_nonvolatile(nvdimm->nvdimm_mr, true);
163     nvdimm->nvdimm_mr->align = align;
164 }
165 
166 static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
167                                                  Error **errp)
168 {
169     NVDIMMDevice *nvdimm = NVDIMM(md);
170     Error *local_err = NULL;
171 
172     if (!nvdimm->nvdimm_mr) {
173         nvdimm_prepare_memory_region(nvdimm, &local_err);
174         if (local_err) {
175             error_propagate(errp, local_err);
176             return NULL;
177         }
178     }
179     return nvdimm->nvdimm_mr;
180 }
181 
182 static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp)
183 {
184     NVDIMMDevice *nvdimm = NVDIMM(dimm);
185 
186     if (!nvdimm->nvdimm_mr) {
187         nvdimm_prepare_memory_region(nvdimm, errp);
188     }
189 }
190 
191 /*
192  * the caller should check the input parameters before calling
193  * label read/write functions.
194  */
195 static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size,
196                                         uint64_t offset)
197 {
198     assert((nvdimm->label_size >= size + offset) && (offset + size > offset));
199 }
200 
201 static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf,
202                                    uint64_t size, uint64_t offset)
203 {
204     nvdimm_validate_rw_label_data(nvdimm, size, offset);
205 
206     memcpy(buf, nvdimm->label_data + offset, size);
207 }
208 
209 static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf,
210                                     uint64_t size, uint64_t offset)
211 {
212     MemoryRegion *mr;
213     PCDIMMDevice *dimm = PC_DIMM(nvdimm);
214     bool is_pmem = object_property_get_bool(OBJECT(dimm->hostmem),
215                                             "pmem", NULL);
216     uint64_t backend_offset;
217 
218     nvdimm_validate_rw_label_data(nvdimm, size, offset);
219 
220     if (!is_pmem) {
221         memcpy(nvdimm->label_data + offset, buf, size);
222     } else {
223         pmem_memcpy_persist(nvdimm->label_data + offset, buf, size);
224     }
225 
226     mr = host_memory_backend_get_memory(dimm->hostmem);
227     backend_offset = memory_region_size(mr) - nvdimm->label_size + offset;
228     memory_region_set_dirty(mr, backend_offset, size);
229 }
230 
231 static Property nvdimm_properties[] = {
232     DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP, NVDIMMDevice, unarmed, false),
233     DEFINE_PROP_END_OF_LIST(),
234 };
235 
236 static void nvdimm_class_init(ObjectClass *oc, void *data)
237 {
238     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
239     MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
240     NVDIMMClass *nvc = NVDIMM_CLASS(oc);
241     DeviceClass *dc = DEVICE_CLASS(oc);
242 
243     ddc->realize = nvdimm_realize;
244     mdc->get_memory_region = nvdimm_md_get_memory_region;
245     device_class_set_props(dc, nvdimm_properties);
246 
247     nvc->read_label_data = nvdimm_read_label_data;
248     nvc->write_label_data = nvdimm_write_label_data;
249 }
250 
251 static TypeInfo nvdimm_info = {
252     .name          = TYPE_NVDIMM,
253     .parent        = TYPE_PC_DIMM,
254     .class_size    = sizeof(NVDIMMClass),
255     .class_init    = nvdimm_class_init,
256     .instance_size = sizeof(NVDIMMDevice),
257     .instance_init = nvdimm_init,
258     .instance_finalize = nvdimm_finalize,
259 };
260 
261 static void nvdimm_register_types(void)
262 {
263     type_register_static(&nvdimm_info);
264 }
265 
266 type_init(nvdimm_register_types)
267