xref: /openbmc/qemu/hw/mem/nvdimm.c (revision ee95fae0)
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.1 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     uint64_t value;
49 
50     if (nvdimm->nvdimm_mr) {
51         error_setg(errp, "cannot change property value");
52         return;
53     }
54 
55     if (!visit_type_size(v, name, &value, errp)) {
56         return;
57     }
58     if (value < MIN_NAMESPACE_LABEL_SIZE) {
59         error_setg(errp, "Property '%s.%s' (0x%" PRIx64 ") is required"
60                    " at least 0x%lx", object_get_typename(obj), name, value,
61                    MIN_NAMESPACE_LABEL_SIZE);
62         return;
63     }
64 
65     nvdimm->label_size = value;
66 }
67 
68 static void nvdimm_get_uuid(Object *obj, Visitor *v, const char *name,
69                                   void *opaque, Error **errp)
70 {
71     NVDIMMDevice *nvdimm = NVDIMM(obj);
72     char *value = NULL;
73 
74     value = qemu_uuid_unparse_strdup(&nvdimm->uuid);
75 
76     visit_type_str(v, name, &value, errp);
77     g_free(value);
78 }
79 
80 
81 static void nvdimm_set_uuid(Object *obj, Visitor *v, const char *name,
82                                   void *opaque, Error **errp)
83 {
84     NVDIMMDevice *nvdimm = NVDIMM(obj);
85     char *value;
86 
87     if (!visit_type_str(v, name, &value, errp)) {
88         return;
89     }
90 
91     if (qemu_uuid_parse(value, &nvdimm->uuid) != 0) {
92         error_setg(errp, "Property '%s.%s' has invalid value",
93                    object_get_typename(obj), name);
94     }
95 
96     g_free(value);
97 }
98 
99 
100 static void nvdimm_init(Object *obj)
101 {
102     object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int",
103                         nvdimm_get_label_size, nvdimm_set_label_size, NULL,
104                         NULL);
105 
106     object_property_add(obj, NVDIMM_UUID_PROP, "QemuUUID", nvdimm_get_uuid,
107                         nvdimm_set_uuid, NULL, NULL);
108 }
109 
110 static void nvdimm_finalize(Object *obj)
111 {
112     NVDIMMDevice *nvdimm = NVDIMM(obj);
113 
114     g_free(nvdimm->nvdimm_mr);
115 }
116 
117 static void nvdimm_prepare_memory_region(NVDIMMDevice *nvdimm, Error **errp)
118 {
119     PCDIMMDevice *dimm = PC_DIMM(nvdimm);
120     uint64_t align, pmem_size, size;
121     MemoryRegion *mr;
122 
123     g_assert(!nvdimm->nvdimm_mr);
124 
125     if (!dimm->hostmem) {
126         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
127         return;
128     }
129 
130     mr = host_memory_backend_get_memory(dimm->hostmem);
131     align = memory_region_get_alignment(mr);
132     size = memory_region_size(mr);
133 
134     pmem_size = size - nvdimm->label_size;
135     nvdimm->label_data = memory_region_get_ram_ptr(mr) + pmem_size;
136     pmem_size = QEMU_ALIGN_DOWN(pmem_size, align);
137 
138     if (size <= nvdimm->label_size || !pmem_size) {
139         HostMemoryBackend *hostmem = dimm->hostmem;
140 
141         error_setg(errp, "the size of memdev %s (0x%" PRIx64 ") is too "
142                    "small to contain nvdimm label (0x%" PRIx64 ") and "
143                    "aligned PMEM (0x%" PRIx64 ")",
144                    object_get_canonical_path_component(OBJECT(hostmem)),
145                    memory_region_size(mr), nvdimm->label_size, align);
146         return;
147     }
148 
149     if (!nvdimm->unarmed && memory_region_is_rom(mr)) {
150         HostMemoryBackend *hostmem = dimm->hostmem;
151 
152         error_setg(errp, "'unarmed' property must be 'on' since memdev %s "
153                    "is read-only",
154                    object_get_canonical_path_component(OBJECT(hostmem)));
155         return;
156     }
157     if (memory_region_is_rom(mr)) {
158         nvdimm->readonly = true;
159     }
160 
161     nvdimm->nvdimm_mr = g_new(MemoryRegion, 1);
162     memory_region_init_alias(nvdimm->nvdimm_mr, OBJECT(dimm),
163                              "nvdimm-memory", mr, 0, pmem_size);
164     memory_region_set_nonvolatile(nvdimm->nvdimm_mr, true);
165     nvdimm->nvdimm_mr->align = align;
166 }
167 
168 static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
169                                                  Error **errp)
170 {
171     NVDIMMDevice *nvdimm = NVDIMM(md);
172     Error *local_err = NULL;
173 
174     if (!nvdimm->nvdimm_mr) {
175         nvdimm_prepare_memory_region(nvdimm, &local_err);
176         if (local_err) {
177             error_propagate(errp, local_err);
178             return NULL;
179         }
180     }
181     return nvdimm->nvdimm_mr;
182 }
183 
184 static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp)
185 {
186     NVDIMMDevice *nvdimm = NVDIMM(dimm);
187     NVDIMMClass *ndc = NVDIMM_GET_CLASS(nvdimm);
188 
189     if (!nvdimm->nvdimm_mr) {
190         nvdimm_prepare_memory_region(nvdimm, errp);
191     }
192 
193     if (ndc->realize) {
194         ndc->realize(nvdimm, errp);
195     }
196 }
197 
198 static void nvdimm_unrealize(PCDIMMDevice *dimm)
199 {
200     NVDIMMDevice *nvdimm = NVDIMM(dimm);
201     NVDIMMClass *ndc = NVDIMM_GET_CLASS(nvdimm);
202 
203     if (ndc->unrealize) {
204         ndc->unrealize(nvdimm);
205     }
206 }
207 
208 /*
209  * the caller should check the input parameters before calling
210  * label read/write functions.
211  */
212 static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size,
213                                         uint64_t offset, bool is_write)
214 {
215     assert((nvdimm->label_size >= size + offset) && (offset + size > offset));
216     assert(!is_write || !nvdimm->readonly);
217 }
218 
219 static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf,
220                                    uint64_t size, uint64_t offset)
221 {
222     nvdimm_validate_rw_label_data(nvdimm, size, offset, false);
223 
224     memcpy(buf, nvdimm->label_data + offset, size);
225 }
226 
227 static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf,
228                                     uint64_t size, uint64_t offset)
229 {
230     MemoryRegion *mr;
231     PCDIMMDevice *dimm = PC_DIMM(nvdimm);
232     bool is_pmem = object_property_get_bool(OBJECT(dimm->hostmem),
233                                             "pmem", NULL);
234     uint64_t backend_offset;
235 
236     nvdimm_validate_rw_label_data(nvdimm, size, offset, true);
237 
238     if (!is_pmem) {
239         memcpy(nvdimm->label_data + offset, buf, size);
240     } else {
241         pmem_memcpy_persist(nvdimm->label_data + offset, buf, size);
242     }
243 
244     mr = host_memory_backend_get_memory(dimm->hostmem);
245     backend_offset = memory_region_size(mr) - nvdimm->label_size + offset;
246     memory_region_set_dirty(mr, backend_offset, size);
247 }
248 
249 static Property nvdimm_properties[] = {
250     DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP, NVDIMMDevice, unarmed, false),
251     DEFINE_PROP_END_OF_LIST(),
252 };
253 
254 static void nvdimm_class_init(ObjectClass *oc, void *data)
255 {
256     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
257     MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
258     NVDIMMClass *nvc = NVDIMM_CLASS(oc);
259     DeviceClass *dc = DEVICE_CLASS(oc);
260 
261     ddc->realize = nvdimm_realize;
262     ddc->unrealize = nvdimm_unrealize;
263     mdc->get_memory_region = nvdimm_md_get_memory_region;
264     device_class_set_props(dc, nvdimm_properties);
265 
266     nvc->read_label_data = nvdimm_read_label_data;
267     nvc->write_label_data = nvdimm_write_label_data;
268     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
269 }
270 
271 static const TypeInfo nvdimm_info = {
272     .name          = TYPE_NVDIMM,
273     .parent        = TYPE_PC_DIMM,
274     .class_size    = sizeof(NVDIMMClass),
275     .class_init    = nvdimm_class_init,
276     .instance_size = sizeof(NVDIMMDevice),
277     .instance_init = nvdimm_init,
278     .instance_finalize = nvdimm_finalize,
279 };
280 
281 static void nvdimm_register_types(void)
282 {
283     type_register_static(&nvdimm_info);
284 }
285 
286 type_init(nvdimm_register_types)
287