2015-12-02 10:20:56 +03:00
|
|
|
/*
|
|
|
|
* Non-Volatile Dual In-line Memory Module Virtualization Implementation
|
|
|
|
*
|
|
|
|
* Copyright(C) 2015 Intel Corporation.
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Xiao Guangrong <guangrong.xiao@linux.intel.com>
|
|
|
|
*
|
|
|
|
* Currently, it only supports PMEM Virtualization.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:30 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2018-07-18 10:48:01 +03:00
|
|
|
#include "qemu/pmem.h"
|
2016-06-07 15:21:58 +03:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qapi/visitor.h"
|
2015-12-02 10:20:56 +03:00
|
|
|
#include "hw/mem/nvdimm.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2018-10-05 12:20:18 +03:00
|
|
|
#include "hw/mem/memory-device.h"
|
2019-08-12 08:23:54 +03:00
|
|
|
#include "sysemu/hostmem.h"
|
2015-12-02 10:20:56 +03:00
|
|
|
|
2016-06-07 15:21:58 +03:00
|
|
|
static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(obj);
|
|
|
|
uint64_t value = nvdimm->label_size;
|
|
|
|
|
|
|
|
visit_type_size(v, name, &value, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvdimm_set_label_size(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(obj);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
uint64_t value;
|
|
|
|
|
2018-06-19 16:41:39 +03:00
|
|
|
if (nvdimm->nvdimm_mr) {
|
2016-06-07 15:21:58 +03:00
|
|
|
error_setg(&local_err, "cannot change property value");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
visit_type_size(v, name, &value, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (value < MIN_NAMESPACE_LABEL_SIZE) {
|
|
|
|
error_setg(&local_err, "Property '%s.%s' (0x%" PRIx64 ") is required"
|
|
|
|
" at least 0x%lx", object_get_typename(obj),
|
|
|
|
name, value, MIN_NAMESPACE_LABEL_SIZE);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvdimm->label_size = value;
|
|
|
|
out:
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
}
|
|
|
|
|
2020-02-10 07:56:13 +03:00
|
|
|
static void nvdimm_get_uuid(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(obj);
|
|
|
|
char *value = NULL;
|
|
|
|
|
|
|
|
value = qemu_uuid_unparse_strdup(&nvdimm->uuid);
|
|
|
|
|
|
|
|
visit_type_str(v, name, &value, errp);
|
|
|
|
g_free(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void nvdimm_set_uuid(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
|
|
|
{
|
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(obj);
|
|
|
|
Error *local_err = NULL;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
visit_type_str(v, name, &value, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemu_uuid_parse(value, &nvdimm->uuid) != 0) {
|
|
|
|
error_setg(errp, "Property '%s.%s' has invalid value",
|
|
|
|
object_get_typename(obj), name);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
g_free(value);
|
|
|
|
|
|
|
|
out:
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-07 15:21:58 +03:00
|
|
|
static void nvdimm_init(Object *obj)
|
|
|
|
{
|
2018-05-21 19:32:00 +03:00
|
|
|
object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int",
|
2016-06-07 15:21:58 +03:00
|
|
|
nvdimm_get_label_size, nvdimm_set_label_size, NULL,
|
|
|
|
NULL, NULL);
|
2020-02-10 07:56:13 +03:00
|
|
|
|
|
|
|
object_property_add(obj, NVDIMM_UUID_PROP, "QemuUUID", nvdimm_get_uuid,
|
|
|
|
nvdimm_set_uuid, NULL, NULL, NULL);
|
2016-06-07 15:21:58 +03:00
|
|
|
}
|
|
|
|
|
2018-06-19 16:41:39 +03:00
|
|
|
static void nvdimm_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(obj);
|
|
|
|
|
|
|
|
g_free(nvdimm->nvdimm_mr);
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:41:40 +03:00
|
|
|
static void nvdimm_prepare_memory_region(NVDIMMDevice *nvdimm, Error **errp)
|
2016-06-07 15:21:58 +03:00
|
|
|
{
|
2018-06-19 16:41:40 +03:00
|
|
|
PCDIMMDevice *dimm = PC_DIMM(nvdimm);
|
|
|
|
uint64_t align, pmem_size, size;
|
|
|
|
MemoryRegion *mr;
|
2016-06-07 15:21:58 +03:00
|
|
|
|
2018-06-19 16:41:40 +03:00
|
|
|
g_assert(!nvdimm->nvdimm_mr);
|
2016-06-07 15:21:58 +03:00
|
|
|
|
2018-06-19 16:41:40 +03:00
|
|
|
if (!dimm->hostmem) {
|
|
|
|
error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-07 15:21:58 +03:00
|
|
|
|
2018-06-19 16:41:40 +03:00
|
|
|
mr = host_memory_backend_get_memory(dimm->hostmem);
|
2016-06-07 15:21:58 +03:00
|
|
|
align = memory_region_get_alignment(mr);
|
2018-06-19 16:41:40 +03:00
|
|
|
size = memory_region_size(mr);
|
2016-06-07 15:21:58 +03:00
|
|
|
|
|
|
|
pmem_size = size - nvdimm->label_size;
|
|
|
|
nvdimm->label_data = memory_region_get_ram_ptr(mr) + pmem_size;
|
|
|
|
pmem_size = QEMU_ALIGN_DOWN(pmem_size, align);
|
|
|
|
|
|
|
|
if (size <= nvdimm->label_size || !pmem_size) {
|
|
|
|
HostMemoryBackend *hostmem = dimm->hostmem;
|
|
|
|
char *path = object_get_canonical_path_component(OBJECT(hostmem));
|
|
|
|
|
|
|
|
error_setg(errp, "the size of memdev %s (0x%" PRIx64 ") is too "
|
|
|
|
"small to contain nvdimm label (0x%" PRIx64 ") and "
|
|
|
|
"aligned PMEM (0x%" PRIx64 ")",
|
|
|
|
path, memory_region_size(mr), nvdimm->label_size, align);
|
2016-06-28 12:06:46 +03:00
|
|
|
g_free(path);
|
2016-06-07 15:21:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:41:39 +03:00
|
|
|
nvdimm->nvdimm_mr = g_new(MemoryRegion, 1);
|
|
|
|
memory_region_init_alias(nvdimm->nvdimm_mr, OBJECT(dimm),
|
2016-06-07 15:21:58 +03:00
|
|
|
"nvdimm-memory", mr, 0, pmem_size);
|
nvdimm: set non-volatile on the memory region
qemu-system-x86_64 -machine pc,nvdimm -m 2G,slots=4,maxmem=16G -enable-kvm -monitor stdio -object memory-backend-file,id=mem1,share=on,mem-path=/tmp/foo,size=1G -device nvdimm,id=nvdimm1,memdev=mem1
HMP info mtree command reflects the flag with "nv-" prefix on memory type:
(qemu) info mtree
0000000100000000-000000013fffffff (prio 0, nv-i/o): alias nvdimm-memory @/objects/mem1 0000000000000000-000000003fffffff
(qemu) info mtree -f
0000000100000000-000000013fffffff (prio 0, nv-ram): /objects/mem1
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20181003114454.5662-3-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-10-03 14:44:53 +03:00
|
|
|
memory_region_set_nonvolatile(nvdimm->nvdimm_mr, true);
|
2018-06-19 16:41:39 +03:00
|
|
|
nvdimm->nvdimm_mr->align = align;
|
2016-06-07 15:21:58 +03:00
|
|
|
}
|
|
|
|
|
2018-10-05 12:20:18 +03:00
|
|
|
static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md,
|
|
|
|
Error **errp)
|
2018-06-19 16:41:40 +03:00
|
|
|
{
|
2018-10-05 12:20:18 +03:00
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(md);
|
2018-06-19 16:41:40 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
if (!nvdimm->nvdimm_mr) {
|
|
|
|
nvdimm_prepare_memory_region(nvdimm, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nvdimm->nvdimm_mr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp)
|
|
|
|
{
|
|
|
|
NVDIMMDevice *nvdimm = NVDIMM(dimm);
|
|
|
|
|
|
|
|
if (!nvdimm->nvdimm_mr) {
|
|
|
|
nvdimm_prepare_memory_region(nvdimm, errp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:21:58 +03:00
|
|
|
/*
|
|
|
|
* the caller should check the input parameters before calling
|
|
|
|
* label read/write functions.
|
|
|
|
*/
|
|
|
|
static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size,
|
|
|
|
uint64_t offset)
|
|
|
|
{
|
|
|
|
assert((nvdimm->label_size >= size + offset) && (offset + size > offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf,
|
|
|
|
uint64_t size, uint64_t offset)
|
|
|
|
{
|
|
|
|
nvdimm_validate_rw_label_data(nvdimm, size, offset);
|
|
|
|
|
|
|
|
memcpy(buf, nvdimm->label_data + offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf,
|
|
|
|
uint64_t size, uint64_t offset)
|
|
|
|
{
|
|
|
|
MemoryRegion *mr;
|
|
|
|
PCDIMMDevice *dimm = PC_DIMM(nvdimm);
|
2018-07-18 10:48:01 +03:00
|
|
|
bool is_pmem = object_property_get_bool(OBJECT(dimm->hostmem),
|
|
|
|
"pmem", NULL);
|
2016-06-07 15:21:58 +03:00
|
|
|
uint64_t backend_offset;
|
|
|
|
|
|
|
|
nvdimm_validate_rw_label_data(nvdimm, size, offset);
|
|
|
|
|
2018-07-18 10:48:01 +03:00
|
|
|
if (!is_pmem) {
|
|
|
|
memcpy(nvdimm->label_data + offset, buf, size);
|
|
|
|
} else {
|
|
|
|
pmem_memcpy_persist(nvdimm->label_data + offset, buf, size);
|
|
|
|
}
|
2016-06-07 15:21:58 +03:00
|
|
|
|
2018-06-19 16:41:36 +03:00
|
|
|
mr = host_memory_backend_get_memory(dimm->hostmem);
|
2016-06-07 15:21:58 +03:00
|
|
|
backend_offset = memory_region_size(mr) - nvdimm->label_size + offset;
|
|
|
|
memory_region_set_dirty(mr, backend_offset, size);
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:41:38 +03:00
|
|
|
static Property nvdimm_properties[] = {
|
|
|
|
DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP, NVDIMMDevice, unarmed, false),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2015-12-02 10:20:56 +03:00
|
|
|
static void nvdimm_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
2016-06-07 15:21:58 +03:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
|
2018-10-05 12:20:18 +03:00
|
|
|
MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
|
2016-06-07 15:21:58 +03:00
|
|
|
NVDIMMClass *nvc = NVDIMM_CLASS(oc);
|
2018-06-19 16:41:38 +03:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2015-12-02 10:20:56 +03:00
|
|
|
|
2016-06-07 15:21:58 +03:00
|
|
|
ddc->realize = nvdimm_realize;
|
2018-10-05 12:20:18 +03:00
|
|
|
mdc->get_memory_region = nvdimm_md_get_memory_region;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, nvdimm_properties);
|
2016-06-07 15:21:58 +03:00
|
|
|
|
|
|
|
nvc->read_label_data = nvdimm_read_label_data;
|
|
|
|
nvc->write_label_data = nvdimm_write_label_data;
|
2015-12-02 10:20:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static TypeInfo nvdimm_info = {
|
|
|
|
.name = TYPE_NVDIMM,
|
|
|
|
.parent = TYPE_PC_DIMM,
|
2016-06-07 15:21:58 +03:00
|
|
|
.class_size = sizeof(NVDIMMClass),
|
2015-12-02 10:20:56 +03:00
|
|
|
.class_init = nvdimm_class_init,
|
2016-06-07 15:21:58 +03:00
|
|
|
.instance_size = sizeof(NVDIMMDevice),
|
|
|
|
.instance_init = nvdimm_init,
|
2018-06-19 16:41:39 +03:00
|
|
|
.instance_finalize = nvdimm_finalize,
|
2015-12-02 10:20:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void nvdimm_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&nvdimm_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(nvdimm_register_types)
|