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