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