hw/arm/smmuv3: Abort on vfio or vhost case
[qemu/ar7.git] / hw / mem / nvdimm.c
blobacb656b67250badae6a3885654771d72300c8c60
1 /*
2 * Non-Volatile Dual In-line Memory Module Virtualization Implementation
4 * Copyright(C) 2015 Intel Corporation.
6 * Author:
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 "qapi/error.h"
27 #include "qapi/visitor.h"
28 #include "hw/mem/nvdimm.h"
30 static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *name,
31 void *opaque, Error **errp)
33 NVDIMMDevice *nvdimm = NVDIMM(obj);
34 uint64_t value = nvdimm->label_size;
36 visit_type_size(v, name, &value, errp);
39 static void nvdimm_set_label_size(Object *obj, Visitor *v, const char *name,
40 void *opaque, Error **errp)
42 NVDIMMDevice *nvdimm = NVDIMM(obj);
43 Error *local_err = NULL;
44 uint64_t value;
46 if (memory_region_size(&nvdimm->nvdimm_mr)) {
47 error_setg(&local_err, "cannot change property value");
48 goto out;
51 visit_type_size(v, name, &value, &local_err);
52 if (local_err) {
53 goto out;
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;
62 nvdimm->label_size = value;
63 out:
64 error_propagate(errp, local_err);
67 static bool nvdimm_get_unarmed(Object *obj, Error **errp)
69 NVDIMMDevice *nvdimm = NVDIMM(obj);
71 return nvdimm->unarmed;
74 static void nvdimm_set_unarmed(Object *obj, bool value, Error **errp)
76 NVDIMMDevice *nvdimm = NVDIMM(obj);
77 Error *local_err = NULL;
79 if (memory_region_size(&nvdimm->nvdimm_mr)) {
80 error_setg(&local_err, "cannot change property value");
81 goto out;
84 nvdimm->unarmed = value;
86 out:
87 error_propagate(errp, local_err);
90 static void nvdimm_init(Object *obj)
92 object_property_add(obj, NVDIMM_LABLE_SIZE_PROP, "int",
93 nvdimm_get_label_size, nvdimm_set_label_size, NULL,
94 NULL, NULL);
95 object_property_add_bool(obj, NVDIMM_UNARMED_PROP,
96 nvdimm_get_unarmed, nvdimm_set_unarmed, NULL);
99 static MemoryRegion *nvdimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
101 NVDIMMDevice *nvdimm = NVDIMM(dimm);
103 return &nvdimm->nvdimm_mr;
106 static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp)
108 MemoryRegion *mr = host_memory_backend_get_memory(dimm->hostmem, errp);
109 NVDIMMDevice *nvdimm = NVDIMM(dimm);
110 uint64_t align, pmem_size, size = memory_region_size(mr);
112 align = memory_region_get_alignment(mr);
114 pmem_size = size - nvdimm->label_size;
115 nvdimm->label_data = memory_region_get_ram_ptr(mr) + pmem_size;
116 pmem_size = QEMU_ALIGN_DOWN(pmem_size, align);
118 if (size <= nvdimm->label_size || !pmem_size) {
119 HostMemoryBackend *hostmem = dimm->hostmem;
120 char *path = object_get_canonical_path_component(OBJECT(hostmem));
122 error_setg(errp, "the size of memdev %s (0x%" PRIx64 ") is too "
123 "small to contain nvdimm label (0x%" PRIx64 ") and "
124 "aligned PMEM (0x%" PRIx64 ")",
125 path, memory_region_size(mr), nvdimm->label_size, align);
126 g_free(path);
127 return;
130 memory_region_init_alias(&nvdimm->nvdimm_mr, OBJECT(dimm),
131 "nvdimm-memory", mr, 0, pmem_size);
132 nvdimm->nvdimm_mr.align = align;
136 * the caller should check the input parameters before calling
137 * label read/write functions.
139 static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size,
140 uint64_t offset)
142 assert((nvdimm->label_size >= size + offset) && (offset + size > offset));
145 static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf,
146 uint64_t size, uint64_t offset)
148 nvdimm_validate_rw_label_data(nvdimm, size, offset);
150 memcpy(buf, nvdimm->label_data + offset, size);
153 static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf,
154 uint64_t size, uint64_t offset)
156 MemoryRegion *mr;
157 PCDIMMDevice *dimm = PC_DIMM(nvdimm);
158 uint64_t backend_offset;
160 nvdimm_validate_rw_label_data(nvdimm, size, offset);
162 memcpy(nvdimm->label_data + offset, buf, size);
164 mr = host_memory_backend_get_memory(dimm->hostmem, &error_abort);
165 backend_offset = memory_region_size(mr) - nvdimm->label_size + offset;
166 memory_region_set_dirty(mr, backend_offset, size);
169 static MemoryRegion *nvdimm_get_vmstate_memory_region(PCDIMMDevice *dimm)
171 return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
174 static void nvdimm_class_init(ObjectClass *oc, void *data)
176 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
177 NVDIMMClass *nvc = NVDIMM_CLASS(oc);
179 ddc->realize = nvdimm_realize;
180 ddc->get_memory_region = nvdimm_get_memory_region;
181 ddc->get_vmstate_memory_region = nvdimm_get_vmstate_memory_region;
183 nvc->read_label_data = nvdimm_read_label_data;
184 nvc->write_label_data = nvdimm_write_label_data;
187 static TypeInfo nvdimm_info = {
188 .name = TYPE_NVDIMM,
189 .parent = TYPE_PC_DIMM,
190 .class_size = sizeof(NVDIMMClass),
191 .class_init = nvdimm_class_init,
192 .instance_size = sizeof(NVDIMMDevice),
193 .instance_init = nvdimm_init,
196 static void nvdimm_register_types(void)
198 type_register_static(&nvdimm_info);
201 type_init(nvdimm_register_types)