xref: /qemu/hw/mem/nvdimm.c (revision f91005e1)
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/mem/memory-device.h"
32 
33 static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *name,
34                                   void *opaque, Error **errp)
35 {
36     NVDIMMDevice *nvdimm = NVDIMM(obj);
37     uint64_t value = nvdimm->label_size;
38 
39     visit_type_size(v, name, &value, errp);
40 }
41 
42 static void nvdimm_set_label_size(Object *obj, Visitor *v, const char *name,
43                                   void *opaque, Error **errp)
44 {
45     NVDIMMDevice *nvdimm = NVDIMM(obj);
46     Error *local_err = NULL;
47     uint64_t value;
48 
49     if (nvdimm->nvdimm_mr) {
50         error_setg(&local_err, "cannot change property value");
51         goto out;
52     }
53 
54     visit_type_size(v, name, &value, &local_err);
55     if (local_err) {
56         goto out;
57     }
58     if (value < MIN_NAMESPACE_LABEL_SIZE) {
59         error_setg(&local_err, "Property '%s.%s' (0x%" PRIx64 ") is required"
60                    " at least 0x%lx", object_get_typename(obj),
61                    name, value, MIN_NAMESPACE_LABEL_SIZE);
62         goto out;
63     }
64 
65     nvdimm->label_size = value;
66 out:
67     error_propagate(errp, local_err);
68 }
69 
70 static void nvdimm_init(Object *obj)
71 {
72     object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int",
73                         nvdimm_get_label_size, nvdimm_set_label_size, NULL,
74                         NULL, NULL);
75 }
76 
77 static void nvdimm_finalize(Object *obj)
78 {
79     NVDIMMDevice *nvdimm = NVDIMM(obj);
80 
81     g_free(nvdimm->nvdimm_mr);
82 }
83 
84 static void nvdimm_prepare_memory_region(NVDIMMDevice *nvdimm, Error **errp)
85 {
86     PCDIMMDevice *dimm = PC_DIMM(nvdimm);
87     uint64_t align, pmem_size, size;
88     MemoryRegion *mr;
89 
90     g_assert(!nvdimm->nvdimm_mr);
91 
92     if (!dimm->hostmem) {
93         error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
94         return;
95     }
96 
97     mr = host_memory_backend_get_memory(dimm->hostmem);
98     align = memory_region_get_alignment(mr);
99     size = memory_region_size(mr);
100 
101     pmem_size = size - nvdimm->label_size;
102     nvdimm->label_data = memory_region_get_ram_ptr(mr) + pmem_size;
103     pmem_size = QEMU_ALIGN_DOWN(pmem_size, align);
104 
105     if (size <= nvdimm->label_size || !pmem_size) {
106         HostMemoryBackend *hostmem = dimm->hostmem;
107         char *path = object_get_canonical_path_component(OBJECT(hostmem));
108 
109         error_setg(errp, "the size of memdev %s (0x%" PRIx64 ") is too "
110                    "small to contain nvdimm label (0x%" PRIx64 ") and "
111                    "aligned PMEM (0x%" PRIx64 ")",
112                    path, memory_region_size(mr), nvdimm->label_size, align);
113         g_free(path);
114         return;
115     }
116 
117     nvdimm->nvdimm_mr = g_new(MemoryRegion, 1);
118     memory_region_init_alias(nvdimm->nvdimm_mr, OBJECT(dimm),
119                              "nvdimm-memory", mr, 0, pmem_size);
120     memory_region_set_nonvolatile(nvdimm->nvdimm_mr, true);
121     nvdimm->nvdimm_mr->align = align;
122 }
123 
124 static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
125                                                  Error **errp)
126 {
127     NVDIMMDevice *nvdimm = NVDIMM(md);
128     Error *local_err = NULL;
129 
130     if (!nvdimm->nvdimm_mr) {
131         nvdimm_prepare_memory_region(nvdimm, &local_err);
132         if (local_err) {
133             error_propagate(errp, local_err);
134             return NULL;
135         }
136     }
137     return nvdimm->nvdimm_mr;
138 }
139 
140 static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp)
141 {
142     NVDIMMDevice *nvdimm = NVDIMM(dimm);
143 
144     if (!nvdimm->nvdimm_mr) {
145         nvdimm_prepare_memory_region(nvdimm, errp);
146     }
147 }
148 
149 /*
150  * the caller should check the input parameters before calling
151  * label read/write functions.
152  */
153 static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size,
154                                         uint64_t offset)
155 {
156     assert((nvdimm->label_size >= size + offset) && (offset + size > offset));
157 }
158 
159 static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf,
160                                    uint64_t size, uint64_t offset)
161 {
162     nvdimm_validate_rw_label_data(nvdimm, size, offset);
163 
164     memcpy(buf, nvdimm->label_data + offset, size);
165 }
166 
167 static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf,
168                                     uint64_t size, uint64_t offset)
169 {
170     MemoryRegion *mr;
171     PCDIMMDevice *dimm = PC_DIMM(nvdimm);
172     bool is_pmem = object_property_get_bool(OBJECT(dimm->hostmem),
173                                             "pmem", NULL);
174     uint64_t backend_offset;
175 
176     nvdimm_validate_rw_label_data(nvdimm, size, offset);
177 
178     if (!is_pmem) {
179         memcpy(nvdimm->label_data + offset, buf, size);
180     } else {
181         pmem_memcpy_persist(nvdimm->label_data + offset, buf, size);
182     }
183 
184     mr = host_memory_backend_get_memory(dimm->hostmem);
185     backend_offset = memory_region_size(mr) - nvdimm->label_size + offset;
186     memory_region_set_dirty(mr, backend_offset, size);
187 }
188 
189 static Property nvdimm_properties[] = {
190     DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP, NVDIMMDevice, unarmed, false),
191     DEFINE_PROP_END_OF_LIST(),
192 };
193 
194 static void nvdimm_class_init(ObjectClass *oc, void *data)
195 {
196     PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
197     MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
198     NVDIMMClass *nvc = NVDIMM_CLASS(oc);
199     DeviceClass *dc = DEVICE_CLASS(oc);
200 
201     ddc->realize = nvdimm_realize;
202     mdc->get_memory_region = nvdimm_md_get_memory_region;
203     dc->props = nvdimm_properties;
204 
205     nvc->read_label_data = nvdimm_read_label_data;
206     nvc->write_label_data = nvdimm_write_label_data;
207 }
208 
209 static TypeInfo nvdimm_info = {
210     .name          = TYPE_NVDIMM,
211     .parent        = TYPE_PC_DIMM,
212     .class_size    = sizeof(NVDIMMClass),
213     .class_init    = nvdimm_class_init,
214     .instance_size = sizeof(NVDIMMDevice),
215     .instance_init = nvdimm_init,
216     .instance_finalize = nvdimm_finalize,
217 };
218 
219 static void nvdimm_register_types(void)
220 {
221     type_register_static(&nvdimm_info);
222 }
223 
224 type_init(nvdimm_register_types)
225