1 /* 2 * Non-Volatile Dual In-line Memory Module Virtualization Implementation 3 * 4 * Copyright(C) 2015 Intel Corporation. 5 * 6 * Author: 7 * Xiao Guangrong <guangrong.xiao@linux.intel.com> 8 * 9 * Currently, it only supports PMEM Virtualization. 10 * 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. 15 * 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. 20 * 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/> 23 */ 24 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" 31 32 static void nvdimm_get_label_size(Object *obj, Visitor *v, const char *name, 33 void *opaque, Error **errp) 34 { 35 NVDIMMDevice *nvdimm = NVDIMM(obj); 36 uint64_t value = nvdimm->label_size; 37 38 visit_type_size(v, name, &value, errp); 39 } 40 41 static void nvdimm_set_label_size(Object *obj, Visitor *v, const char *name, 42 void *opaque, Error **errp) 43 { 44 NVDIMMDevice *nvdimm = NVDIMM(obj); 45 Error *local_err = NULL; 46 uint64_t value; 47 48 if (nvdimm->nvdimm_mr) { 49 error_setg(&local_err, "cannot change property value"); 50 goto out; 51 } 52 53 visit_type_size(v, name, &value, &local_err); 54 if (local_err) { 55 goto out; 56 } 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); 61 goto out; 62 } 63 64 nvdimm->label_size = value; 65 out: 66 error_propagate(errp, local_err); 67 } 68 69 static void nvdimm_init(Object *obj) 70 { 71 object_property_add(obj, NVDIMM_LABEL_SIZE_PROP, "int", 72 nvdimm_get_label_size, nvdimm_set_label_size, NULL, 73 NULL, NULL); 74 } 75 76 static void nvdimm_finalize(Object *obj) 77 { 78 NVDIMMDevice *nvdimm = NVDIMM(obj); 79 80 g_free(nvdimm->nvdimm_mr); 81 } 82 83 static void nvdimm_prepare_memory_region(NVDIMMDevice *nvdimm, Error **errp) 84 { 85 PCDIMMDevice *dimm = PC_DIMM(nvdimm); 86 uint64_t align, pmem_size, size; 87 MemoryRegion *mr; 88 89 g_assert(!nvdimm->nvdimm_mr); 90 91 if (!dimm->hostmem) { 92 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set"); 93 return; 94 } 95 96 mr = host_memory_backend_get_memory(dimm->hostmem); 97 align = memory_region_get_alignment(mr); 98 size = memory_region_size(mr); 99 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); 103 104 if (size <= nvdimm->label_size || !pmem_size) { 105 HostMemoryBackend *hostmem = dimm->hostmem; 106 char *path = object_get_canonical_path_component(OBJECT(hostmem)); 107 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); 112 g_free(path); 113 return; 114 } 115 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 nvdimm->nvdimm_mr->align = align; 120 } 121 122 static MemoryRegion *nvdimm_md_get_memory_region(MemoryDeviceState *md, 123 Error **errp) 124 { 125 NVDIMMDevice *nvdimm = NVDIMM(md); 126 Error *local_err = NULL; 127 128 if (!nvdimm->nvdimm_mr) { 129 nvdimm_prepare_memory_region(nvdimm, &local_err); 130 if (local_err) { 131 error_propagate(errp, local_err); 132 return NULL; 133 } 134 } 135 return nvdimm->nvdimm_mr; 136 } 137 138 static void nvdimm_realize(PCDIMMDevice *dimm, Error **errp) 139 { 140 NVDIMMDevice *nvdimm = NVDIMM(dimm); 141 142 if (!nvdimm->nvdimm_mr) { 143 nvdimm_prepare_memory_region(nvdimm, errp); 144 } 145 } 146 147 /* 148 * the caller should check the input parameters before calling 149 * label read/write functions. 150 */ 151 static void nvdimm_validate_rw_label_data(NVDIMMDevice *nvdimm, uint64_t size, 152 uint64_t offset) 153 { 154 assert((nvdimm->label_size >= size + offset) && (offset + size > offset)); 155 } 156 157 static void nvdimm_read_label_data(NVDIMMDevice *nvdimm, void *buf, 158 uint64_t size, uint64_t offset) 159 { 160 nvdimm_validate_rw_label_data(nvdimm, size, offset); 161 162 memcpy(buf, nvdimm->label_data + offset, size); 163 } 164 165 static void nvdimm_write_label_data(NVDIMMDevice *nvdimm, const void *buf, 166 uint64_t size, uint64_t offset) 167 { 168 MemoryRegion *mr; 169 PCDIMMDevice *dimm = PC_DIMM(nvdimm); 170 bool is_pmem = object_property_get_bool(OBJECT(dimm->hostmem), 171 "pmem", NULL); 172 uint64_t backend_offset; 173 174 nvdimm_validate_rw_label_data(nvdimm, size, offset); 175 176 if (!is_pmem) { 177 memcpy(nvdimm->label_data + offset, buf, size); 178 } else { 179 pmem_memcpy_persist(nvdimm->label_data + offset, buf, size); 180 } 181 182 mr = host_memory_backend_get_memory(dimm->hostmem); 183 backend_offset = memory_region_size(mr) - nvdimm->label_size + offset; 184 memory_region_set_dirty(mr, backend_offset, size); 185 } 186 187 static Property nvdimm_properties[] = { 188 DEFINE_PROP_BOOL(NVDIMM_UNARMED_PROP, NVDIMMDevice, unarmed, false), 189 DEFINE_PROP_END_OF_LIST(), 190 }; 191 192 static void nvdimm_class_init(ObjectClass *oc, void *data) 193 { 194 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); 195 MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc); 196 NVDIMMClass *nvc = NVDIMM_CLASS(oc); 197 DeviceClass *dc = DEVICE_CLASS(oc); 198 199 ddc->realize = nvdimm_realize; 200 mdc->get_memory_region = nvdimm_md_get_memory_region; 201 dc->props = nvdimm_properties; 202 203 nvc->read_label_data = nvdimm_read_label_data; 204 nvc->write_label_data = nvdimm_write_label_data; 205 } 206 207 static TypeInfo nvdimm_info = { 208 .name = TYPE_NVDIMM, 209 .parent = TYPE_PC_DIMM, 210 .class_size = sizeof(NVDIMMClass), 211 .class_init = nvdimm_class_init, 212 .instance_size = sizeof(NVDIMMDevice), 213 .instance_init = nvdimm_init, 214 .instance_finalize = nvdimm_finalize, 215 }; 216 217 static void nvdimm_register_types(void) 218 { 219 type_register_static(&nvdimm_info); 220 } 221 222 type_init(nvdimm_register_types) 223