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"
31 static void nvdimm_get_label_size(Object
*obj
, Visitor
*v
, const char *name
,
32 void *opaque
, Error
**errp
)
34 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
35 uint64_t value
= nvdimm
->label_size
;
37 visit_type_size(v
, name
, &value
, errp
);
40 static void nvdimm_set_label_size(Object
*obj
, Visitor
*v
, const char *name
,
41 void *opaque
, Error
**errp
)
43 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
44 Error
*local_err
= NULL
;
47 if (nvdimm
->nvdimm_mr
) {
48 error_setg(&local_err
, "cannot change property value");
52 visit_type_size(v
, name
, &value
, &local_err
);
56 if (value
< MIN_NAMESPACE_LABEL_SIZE
) {
57 error_setg(&local_err
, "Property '%s.%s' (0x%" PRIx64
") is required"
58 " at least 0x%lx", object_get_typename(obj
),
59 name
, value
, MIN_NAMESPACE_LABEL_SIZE
);
63 nvdimm
->label_size
= value
;
65 error_propagate(errp
, local_err
);
68 static void nvdimm_init(Object
*obj
)
70 object_property_add(obj
, NVDIMM_LABEL_SIZE_PROP
, "int",
71 nvdimm_get_label_size
, nvdimm_set_label_size
, NULL
,
75 static void nvdimm_finalize(Object
*obj
)
77 NVDIMMDevice
*nvdimm
= NVDIMM(obj
);
79 g_free(nvdimm
->nvdimm_mr
);
82 static void nvdimm_prepare_memory_region(NVDIMMDevice
*nvdimm
, Error
**errp
)
84 PCDIMMDevice
*dimm
= PC_DIMM(nvdimm
);
85 uint64_t align
, pmem_size
, size
;
88 g_assert(!nvdimm
->nvdimm_mr
);
91 error_setg(errp
, "'" PC_DIMM_MEMDEV_PROP
"' property must be set");
95 mr
= host_memory_backend_get_memory(dimm
->hostmem
);
96 align
= memory_region_get_alignment(mr
);
97 size
= memory_region_size(mr
);
99 pmem_size
= size
- nvdimm
->label_size
;
100 nvdimm
->label_data
= memory_region_get_ram_ptr(mr
) + pmem_size
;
101 pmem_size
= QEMU_ALIGN_DOWN(pmem_size
, align
);
103 if (size
<= nvdimm
->label_size
|| !pmem_size
) {
104 HostMemoryBackend
*hostmem
= dimm
->hostmem
;
105 char *path
= object_get_canonical_path_component(OBJECT(hostmem
));
107 error_setg(errp
, "the size of memdev %s (0x%" PRIx64
") is too "
108 "small to contain nvdimm label (0x%" PRIx64
") and "
109 "aligned PMEM (0x%" PRIx64
")",
110 path
, memory_region_size(mr
), nvdimm
->label_size
, align
);
115 nvdimm
->nvdimm_mr
= g_new(MemoryRegion
, 1);
116 memory_region_init_alias(nvdimm
->nvdimm_mr
, OBJECT(dimm
),
117 "nvdimm-memory", mr
, 0, pmem_size
);
118 nvdimm
->nvdimm_mr
->align
= align
;
121 static MemoryRegion
*nvdimm_get_memory_region(PCDIMMDevice
*dimm
, Error
**errp
)
123 NVDIMMDevice
*nvdimm
= NVDIMM(dimm
);
124 Error
*local_err
= NULL
;
126 if (!nvdimm
->nvdimm_mr
) {
127 nvdimm_prepare_memory_region(nvdimm
, &local_err
);
129 error_propagate(errp
, local_err
);
133 return nvdimm
->nvdimm_mr
;
136 static void nvdimm_realize(PCDIMMDevice
*dimm
, Error
**errp
)
138 NVDIMMDevice
*nvdimm
= NVDIMM(dimm
);
140 if (!nvdimm
->nvdimm_mr
) {
141 nvdimm_prepare_memory_region(nvdimm
, errp
);
146 * the caller should check the input parameters before calling
147 * label read/write functions.
149 static void nvdimm_validate_rw_label_data(NVDIMMDevice
*nvdimm
, uint64_t size
,
152 assert((nvdimm
->label_size
>= size
+ offset
) && (offset
+ size
> offset
));
155 static void nvdimm_read_label_data(NVDIMMDevice
*nvdimm
, void *buf
,
156 uint64_t size
, uint64_t offset
)
158 nvdimm_validate_rw_label_data(nvdimm
, size
, offset
);
160 memcpy(buf
, nvdimm
->label_data
+ offset
, size
);
163 static void nvdimm_write_label_data(NVDIMMDevice
*nvdimm
, const void *buf
,
164 uint64_t size
, uint64_t offset
)
167 PCDIMMDevice
*dimm
= PC_DIMM(nvdimm
);
168 bool is_pmem
= object_property_get_bool(OBJECT(dimm
->hostmem
),
170 uint64_t backend_offset
;
172 nvdimm_validate_rw_label_data(nvdimm
, size
, offset
);
175 memcpy(nvdimm
->label_data
+ offset
, buf
, size
);
177 pmem_memcpy_persist(nvdimm
->label_data
+ offset
, buf
, size
);
180 mr
= host_memory_backend_get_memory(dimm
->hostmem
);
181 backend_offset
= memory_region_size(mr
) - nvdimm
->label_size
+ offset
;
182 memory_region_set_dirty(mr
, backend_offset
, size
);
185 static Property nvdimm_properties
[] = {
186 DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP
, NVDIMMDevice
, unarmed
, false),
187 DEFINE_PROP_END_OF_LIST(),
190 static void nvdimm_class_init(ObjectClass
*oc
, void *data
)
192 PCDIMMDeviceClass
*ddc
= PC_DIMM_CLASS(oc
);
193 NVDIMMClass
*nvc
= NVDIMM_CLASS(oc
);
194 DeviceClass
*dc
= DEVICE_CLASS(oc
);
196 ddc
->realize
= nvdimm_realize
;
197 ddc
->get_memory_region
= nvdimm_get_memory_region
;
198 dc
->props
= nvdimm_properties
;
200 nvc
->read_label_data
= nvdimm_read_label_data
;
201 nvc
->write_label_data
= nvdimm_write_label_data
;
204 static TypeInfo nvdimm_info
= {
206 .parent
= TYPE_PC_DIMM
,
207 .class_size
= sizeof(NVDIMMClass
),
208 .class_init
= nvdimm_class_init
,
209 .instance_size
= sizeof(NVDIMMDevice
),
210 .instance_init
= nvdimm_init
,
211 .instance_finalize
= nvdimm_finalize
,
214 static void nvdimm_register_types(void)
216 type_register_static(&nvdimm_info
);
219 type_init(nvdimm_register_types
)