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/pmem.h"
27 #include "qapi/error.h"
28 #include "qapi/visitor.h"
29 #include "hw/mem/nvdimm.h"
30 #include "hw/mem/memory-device.h"
32 static void nvdimm_get_label_size(Object
*obj
, Visitor
*v
, const char *name
,
33 void *opaque
, Error
**errp
)
35 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
36 uint64_t value
= nvdimm
->label_size
;
38 visit_type_size(v
, name
, &value
, errp
);
41 static void nvdimm_set_label_size(Object
*obj
, Visitor
*v
, const char *name
,
42 void *opaque
, Error
**errp
)
44 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
45 Error
*local_err
= NULL
;
48 if (nvdimm
->nvdimm_mr
) {
49 error_setg(&local_err
, "cannot change property value");
53 visit_type_size(v
, name
, &value
, &local_err
);
57 if (value
< MIN_NAMESPACE_LABEL_SIZE
) {
58 error_setg(&local_err
, "Property '%s.%s' (0x%" PRIx64
") is required"
59 " at least 0x%lx", object_get_typename(obj
),
60 name
, value
, MIN_NAMESPACE_LABEL_SIZE
);
64 nvdimm
->label_size
= value
;
66 error_propagate(errp
, local_err
);
69 static void nvdimm_init(Object
*obj
)
71 object_property_add(obj
, NVDIMM_LABEL_SIZE_PROP
, "int",
72 nvdimm_get_label_size
, nvdimm_set_label_size
, NULL
,
76 static void nvdimm_finalize(Object
*obj
)
78 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
80 g_free(nvdimm
->nvdimm_mr
);
83 static void nvdimm_prepare_memory_region(NVDIMMDevice
*nvdimm
, Error
**errp
)
85 PCDIMMDevice
*dimm
= PC_DIMM(nvdimm
);
86 uint64_t align
, pmem_size
, size
;
89 g_assert(!nvdimm
->nvdimm_mr
);
92 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
96 mr
= host_memory_backend_get_memory(dimm
->hostmem
);
97 align
= memory_region_get_alignment(mr
);
98 size
= memory_region_size(mr
);
100 pmem_size
= size
- nvdimm
->label_size
;
101 nvdimm
->label_data
= memory_region_get_ram_ptr(mr
) + pmem_size
;
102 pmem_size
= QEMU_ALIGN_DOWN(pmem_size
, align
);
104 if (size
<= nvdimm
->label_size
|| !pmem_size
) {
105 HostMemoryBackend
*hostmem
= dimm
->hostmem
;
106 char *path
= object_get_canonical_path_component(OBJECT(hostmem
));
108 error_setg(errp
, "the size of memdev %s (0x%" PRIx64
") is too "
109 "small to contain nvdimm label (0x%" PRIx64
") and "
110 "aligned PMEM (0x%" PRIx64
")",
111 path
, memory_region_size(mr
), nvdimm
->label_size
, align
);
116 nvdimm
->nvdimm_mr
= g_new(MemoryRegion
, 1);
117 memory_region_init_alias(nvdimm
->nvdimm_mr
, OBJECT(dimm
),
118 "nvdimm-memory", mr
, 0, pmem_size
);
119 memory_region_set_nonvolatile(nvdimm
->nvdimm_mr
, true);
120 nvdimm
->nvdimm_mr
->align
= align
;
123 static MemoryRegion
*nvdimm_md_get_memory_region(MemoryDeviceState
*md
,
126 NVDIMMDevice
*nvdimm
= NVDIMM(md
);
127 Error
*local_err
= NULL
;
129 if (!nvdimm
->nvdimm_mr
) {
130 nvdimm_prepare_memory_region(nvdimm
, &local_err
);
132 error_propagate(errp
, local_err
);
136 return nvdimm
->nvdimm_mr
;
139 static void nvdimm_realize(PCDIMMDevice
*dimm
, Error
**errp
)
141 NVDIMMDevice
*nvdimm
= NVDIMM(dimm
);
143 if (!nvdimm
->nvdimm_mr
) {
144 nvdimm_prepare_memory_region(nvdimm
, errp
);
149 * the caller should check the input parameters before calling
150 * label read/write functions.
152 static void nvdimm_validate_rw_label_data(NVDIMMDevice
*nvdimm
, uint64_t size
,
155 assert((nvdimm
->label_size
>= size
+ offset
) && (offset
+ size
> offset
));
158 static void nvdimm_read_label_data(NVDIMMDevice
*nvdimm
, void *buf
,
159 uint64_t size
, uint64_t offset
)
161 nvdimm_validate_rw_label_data(nvdimm
, size
, offset
);
163 memcpy(buf
, nvdimm
->label_data
+ offset
, size
);
166 static void nvdimm_write_label_data(NVDIMMDevice
*nvdimm
, const void *buf
,
167 uint64_t size
, uint64_t offset
)
170 PCDIMMDevice
*dimm
= PC_DIMM(nvdimm
);
171 bool is_pmem
= object_property_get_bool(OBJECT(dimm
->hostmem
),
173 uint64_t backend_offset
;
175 nvdimm_validate_rw_label_data(nvdimm
, size
, offset
);
178 memcpy(nvdimm
->label_data
+ offset
, buf
, size
);
180 pmem_memcpy_persist(nvdimm
->label_data
+ offset
, buf
, size
);
183 mr
= host_memory_backend_get_memory(dimm
->hostmem
);
184 backend_offset
= memory_region_size(mr
) - nvdimm
->label_size
+ offset
;
185 memory_region_set_dirty(mr
, backend_offset
, size
);
188 static Property nvdimm_properties
[] = {
189 DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP
, NVDIMMDevice
, unarmed
, false),
190 DEFINE_PROP_END_OF_LIST(),
193 static void nvdimm_class_init(ObjectClass
*oc
, void *data
)
195 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
196 MemoryDeviceClass
*mdc
= MEMORY_DEVICE_CLASS(oc
);
197 NVDIMMClass
*nvc
= NVDIMM_CLASS(oc
);
198 DeviceClass
*dc
= DEVICE_CLASS(oc
);
200 ddc
->realize
= nvdimm_realize
;
201 mdc
->get_memory_region
= nvdimm_md_get_memory_region
;
202 dc
->props
= nvdimm_properties
;
204 nvc
->read_label_data
= nvdimm_read_label_data
;
205 nvc
->write_label_data
= nvdimm_write_label_data
;
208 static TypeInfo nvdimm_info
= {
210 .parent
= TYPE_PC_DIMM
,
211 .class_size
= sizeof(NVDIMMClass
),
212 .class_init
= nvdimm_class_init
,
213 .instance_size
= sizeof(NVDIMMDevice
),
214 .instance_init
= nvdimm_init
,
215 .instance_finalize
= nvdimm_finalize
,
218 static void nvdimm_register_types(void)
220 type_register_static(&nvdimm_info
);
223 type_init(nvdimm_register_types
)