2 * Non-Volatile Dual In-line Memory Module Virtualization Implementation
4 * Copyright(C) 2015 Intel Corporation.
7 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
9 * Currently, it only supports PMEM Virtualization.
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.
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.
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/>
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"
35 static void nvdimm_get_label_size(Object
*obj
, Visitor
*v
, const char *name
,
36 void *opaque
, Error
**errp
)
38 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
39 uint64_t value
= nvdimm
->label_size
;
41 visit_type_size(v
, name
, &value
, errp
);
44 static void nvdimm_set_label_size(Object
*obj
, Visitor
*v
, const char *name
,
45 void *opaque
, Error
**errp
)
47 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
48 Error
*local_err
= NULL
;
51 if (nvdimm
->nvdimm_mr
) {
52 error_setg(&local_err
, "cannot change property value");
56 visit_type_size(v
, name
, &value
, &local_err
);
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
);
67 nvdimm
->label_size
= value
;
69 error_propagate(errp
, local_err
);
72 static void nvdimm_init(Object
*obj
)
74 object_property_add(obj
, NVDIMM_LABEL_SIZE_PROP
, "int",
75 nvdimm_get_label_size
, nvdimm_set_label_size
, NULL
,
79 static void nvdimm_finalize(Object
*obj
)
81 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
83 g_free(nvdimm
->nvdimm_mr
);
86 static void nvdimm_prepare_memory_region(NVDIMMDevice
*nvdimm
, Error
**errp
)
88 PCDIMMDevice
*dimm
= PC_DIMM(nvdimm
);
89 uint64_t align
, pmem_size
, size
;
92 g_assert(!nvdimm
->nvdimm_mr
);
95 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
99 mr
= host_memory_backend_get_memory(dimm
->hostmem
);
100 align
= memory_region_get_alignment(mr
);
101 size
= memory_region_size(mr
);
103 pmem_size
= size
- nvdimm
->label_size
;
104 nvdimm
->label_data
= memory_region_get_ram_ptr(mr
) + pmem_size
;
105 pmem_size
= QEMU_ALIGN_DOWN(pmem_size
, align
);
107 if (size
<= nvdimm
->label_size
|| !pmem_size
) {
108 HostMemoryBackend
*hostmem
= dimm
->hostmem
;
109 char *path
= object_get_canonical_path_component(OBJECT(hostmem
));
111 error_setg(errp
, "the size of memdev %s (0x%" PRIx64
") is too "
112 "small to contain nvdimm label (0x%" PRIx64
") and "
113 "aligned PMEM (0x%" PRIx64
")",
114 path
, memory_region_size(mr
), nvdimm
->label_size
, align
);
119 nvdimm
->nvdimm_mr
= g_new(MemoryRegion
, 1);
120 memory_region_init_alias(nvdimm
->nvdimm_mr
, OBJECT(dimm
),
121 "nvdimm-memory", mr
, 0, pmem_size
);
122 memory_region_set_nonvolatile(nvdimm
->nvdimm_mr
, true);
123 nvdimm
->nvdimm_mr
->align
= align
;
126 static MemoryRegion
*nvdimm_md_get_memory_region(MemoryDeviceState
*md
,
129 NVDIMMDevice
*nvdimm
= NVDIMM(md
);
130 Error
*local_err
= NULL
;
132 if (!nvdimm
->nvdimm_mr
) {
133 nvdimm_prepare_memory_region(nvdimm
, &local_err
);
135 error_propagate(errp
, local_err
);
139 return nvdimm
->nvdimm_mr
;
142 static void nvdimm_realize(PCDIMMDevice
*dimm
, Error
**errp
)
144 NVDIMMDevice
*nvdimm
= NVDIMM(dimm
);
146 if (!nvdimm
->nvdimm_mr
) {
147 nvdimm_prepare_memory_region(nvdimm
, errp
);
152 * the caller should check the input parameters before calling
153 * label read/write functions.
155 static void nvdimm_validate_rw_label_data(NVDIMMDevice
*nvdimm
, uint64_t size
,
158 assert((nvdimm
->label_size
>= size
+ offset
) && (offset
+ size
> offset
));
161 static void nvdimm_read_label_data(NVDIMMDevice
*nvdimm
, void *buf
,
162 uint64_t size
, uint64_t offset
)
164 nvdimm_validate_rw_label_data(nvdimm
, size
, offset
);
166 memcpy(buf
, nvdimm
->label_data
+ offset
, size
);
169 static void nvdimm_write_label_data(NVDIMMDevice
*nvdimm
, const void *buf
,
170 uint64_t size
, uint64_t offset
)
173 PCDIMMDevice
*dimm
= PC_DIMM(nvdimm
);
174 bool is_pmem
= object_property_get_bool(OBJECT(dimm
->hostmem
),
176 uint64_t backend_offset
;
178 nvdimm_validate_rw_label_data(nvdimm
, size
, offset
);
181 memcpy(nvdimm
->label_data
+ offset
, buf
, size
);
183 pmem_memcpy_persist(nvdimm
->label_data
+ offset
, buf
, size
);
186 mr
= host_memory_backend_get_memory(dimm
->hostmem
);
187 backend_offset
= memory_region_size(mr
) - nvdimm
->label_size
+ offset
;
188 memory_region_set_dirty(mr
, backend_offset
, size
);
191 static Property nvdimm_properties
[] = {
192 DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP
, NVDIMMDevice
, unarmed
, false),
193 DEFINE_PROP_END_OF_LIST(),
196 static void nvdimm_class_init(ObjectClass
*oc
, void *data
)
198 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
199 MemoryDeviceClass
*mdc
= MEMORY_DEVICE_CLASS(oc
);
200 NVDIMMClass
*nvc
= NVDIMM_CLASS(oc
);
201 DeviceClass
*dc
= DEVICE_CLASS(oc
);
203 ddc
->realize
= nvdimm_realize
;
204 mdc
->get_memory_region
= nvdimm_md_get_memory_region
;
205 dc
->props
= nvdimm_properties
;
207 nvc
->read_label_data
= nvdimm_read_label_data
;
208 nvc
->write_label_data
= nvdimm_write_label_data
;
211 static TypeInfo nvdimm_info
= {
213 .parent
= TYPE_PC_DIMM
,
214 .class_size
= sizeof(NVDIMMClass
),
215 .class_init
= nvdimm_class_init
,
216 .instance_size
= sizeof(NVDIMMDevice
),
217 .instance_init
= nvdimm_init
,
218 .instance_finalize
= nvdimm_finalize
,
221 static void nvdimm_register_types(void)
223 type_register_static(&nvdimm_info
);
226 type_init(nvdimm_register_types
)