1 /* 2 * Dimm device for Memory Hotplug 3 * 4 * Copyright ProfitBricks GmbH 2012 5 * Copyright (C) 2014 Red Hat Inc 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, see <http://www.gnu.org/licenses/> 19 */ 20 21 #include "qemu/osdep.h" 22 #include "hw/mem/pc-dimm.h" 23 #include "hw/mem/nvdimm.h" 24 #include "hw/mem/memory-device.h" 25 #include "qapi/error.h" 26 #include "qapi/visitor.h" 27 #include "sysemu/numa.h" 28 #include "trace.h" 29 30 typedef struct pc_dimms_capacity { 31 uint64_t size; 32 Error **errp; 33 } pc_dimms_capacity; 34 35 void pc_dimm_memory_plug(DeviceState *dev, MachineState *machine, 36 uint64_t align, Error **errp) 37 { 38 int slot; 39 PCDIMMDevice *dimm = PC_DIMM(dev); 40 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 41 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm); 42 Error *local_err = NULL; 43 MemoryRegion *mr; 44 uint64_t addr; 45 46 mr = ddc->get_memory_region(dimm, &local_err); 47 if (local_err) { 48 goto out; 49 } 50 51 addr = object_property_get_uint(OBJECT(dimm), 52 PC_DIMM_ADDR_PROP, &local_err); 53 if (local_err) { 54 goto out; 55 } 56 57 addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align, 58 memory_region_size(mr), &local_err); 59 if (local_err) { 60 goto out; 61 } 62 63 object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err); 64 if (local_err) { 65 goto out; 66 } 67 trace_mhp_pc_dimm_assigned_address(addr); 68 69 slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err); 70 if (local_err) { 71 goto out; 72 } 73 74 slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot, 75 machine->ram_slots, &local_err); 76 if (local_err) { 77 goto out; 78 } 79 object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err); 80 if (local_err) { 81 goto out; 82 } 83 trace_mhp_pc_dimm_assigned_slot(slot); 84 85 memory_device_plug_region(machine, mr, addr); 86 vmstate_register_ram(vmstate_mr, dev); 87 88 out: 89 error_propagate(errp, local_err); 90 } 91 92 void pc_dimm_memory_unplug(DeviceState *dev, MachineState *machine) 93 { 94 PCDIMMDevice *dimm = PC_DIMM(dev); 95 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 96 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm); 97 MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort); 98 99 memory_device_unplug_region(machine, mr); 100 vmstate_unregister_ram(vmstate_mr, dev); 101 } 102 103 static int pc_dimm_slot2bitmap(Object *obj, void *opaque) 104 { 105 unsigned long *bitmap = opaque; 106 107 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 108 DeviceState *dev = DEVICE(obj); 109 if (dev->realized) { /* count only realized DIMMs */ 110 PCDIMMDevice *d = PC_DIMM(obj); 111 set_bit(d->slot, bitmap); 112 } 113 } 114 115 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque); 116 return 0; 117 } 118 119 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp) 120 { 121 unsigned long *bitmap; 122 int slot = 0; 123 124 if (max_slots <= 0) { 125 error_setg(errp, "no slots where allocated, please specify " 126 "the 'slots' option"); 127 return slot; 128 } 129 130 bitmap = bitmap_new(max_slots); 131 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap); 132 133 /* check if requested slot is not occupied */ 134 if (hint) { 135 if (*hint >= max_slots) { 136 error_setg(errp, "invalid slot# %d, should be less than %d", 137 *hint, max_slots); 138 } else if (!test_bit(*hint, bitmap)) { 139 slot = *hint; 140 } else { 141 error_setg(errp, "slot %d is busy", *hint); 142 } 143 goto out; 144 } 145 146 /* search for free slot */ 147 slot = find_first_zero_bit(bitmap, max_slots); 148 if (slot == max_slots) { 149 error_setg(errp, "no free slots available"); 150 } 151 out: 152 g_free(bitmap); 153 return slot; 154 } 155 156 static Property pc_dimm_properties[] = { 157 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), 158 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), 159 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot, 160 PC_DIMM_UNASSIGNED_SLOT), 161 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem, 162 TYPE_MEMORY_BACKEND, HostMemoryBackend *), 163 DEFINE_PROP_END_OF_LIST(), 164 }; 165 166 static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name, 167 void *opaque, Error **errp) 168 { 169 uint64_t value; 170 MemoryRegion *mr; 171 PCDIMMDevice *dimm = PC_DIMM(obj); 172 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj); 173 174 mr = ddc->get_memory_region(dimm, errp); 175 if (!mr) { 176 return; 177 } 178 value = memory_region_size(mr); 179 180 visit_type_uint64(v, name, &value, errp); 181 } 182 183 static void pc_dimm_init(Object *obj) 184 { 185 object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size, 186 NULL, NULL, NULL, &error_abort); 187 } 188 189 static void pc_dimm_realize(DeviceState *dev, Error **errp) 190 { 191 PCDIMMDevice *dimm = PC_DIMM(dev); 192 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 193 194 if (!dimm->hostmem) { 195 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set"); 196 return; 197 } else if (host_memory_backend_is_mapped(dimm->hostmem)) { 198 char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem)); 199 error_setg(errp, "can't use already busy memdev: %s", path); 200 g_free(path); 201 return; 202 } 203 if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) || 204 (!nb_numa_nodes && dimm->node)) { 205 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %" 206 PRIu32 "' which exceeds the number of numa nodes: %d", 207 dimm->node, nb_numa_nodes ? nb_numa_nodes : 1); 208 return; 209 } 210 211 if (ddc->realize) { 212 ddc->realize(dimm, errp); 213 } 214 215 host_memory_backend_set_mapped(dimm->hostmem, true); 216 } 217 218 static void pc_dimm_unrealize(DeviceState *dev, Error **errp) 219 { 220 PCDIMMDevice *dimm = PC_DIMM(dev); 221 222 host_memory_backend_set_mapped(dimm->hostmem, false); 223 } 224 225 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp) 226 { 227 if (!dimm->hostmem) { 228 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set"); 229 return NULL; 230 } 231 232 return host_memory_backend_get_memory(dimm->hostmem, errp); 233 } 234 235 static MemoryRegion *pc_dimm_get_vmstate_memory_region(PCDIMMDevice *dimm) 236 { 237 return host_memory_backend_get_memory(dimm->hostmem, &error_abort); 238 } 239 240 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md) 241 { 242 const PCDIMMDevice *dimm = PC_DIMM(md); 243 244 return dimm->addr; 245 } 246 247 static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState *md) 248 { 249 /* dropping const here is fine as we don't touch the memory region */ 250 PCDIMMDevice *dimm = PC_DIMM(md); 251 const PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(md); 252 MemoryRegion *mr; 253 254 mr = ddc->get_memory_region(dimm, &error_abort); 255 if (!mr) { 256 return 0; 257 } 258 259 return memory_region_size(mr); 260 } 261 262 static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md, 263 MemoryDeviceInfo *info) 264 { 265 PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1); 266 const DeviceClass *dc = DEVICE_GET_CLASS(md); 267 const PCDIMMDevice *dimm = PC_DIMM(md); 268 const DeviceState *dev = DEVICE(md); 269 270 if (dev->id) { 271 di->has_id = true; 272 di->id = g_strdup(dev->id); 273 } 274 di->hotplugged = dev->hotplugged; 275 di->hotpluggable = dc->hotpluggable; 276 di->addr = dimm->addr; 277 di->slot = dimm->slot; 278 di->node = dimm->node; 279 di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP, 280 NULL); 281 di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem)); 282 283 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { 284 info->u.nvdimm.data = di; 285 info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM; 286 } else { 287 info->u.dimm.data = di; 288 info->type = MEMORY_DEVICE_INFO_KIND_DIMM; 289 } 290 } 291 292 static void pc_dimm_class_init(ObjectClass *oc, void *data) 293 { 294 DeviceClass *dc = DEVICE_CLASS(oc); 295 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); 296 MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc); 297 298 dc->realize = pc_dimm_realize; 299 dc->unrealize = pc_dimm_unrealize; 300 dc->props = pc_dimm_properties; 301 dc->desc = "DIMM memory module"; 302 303 ddc->get_memory_region = pc_dimm_get_memory_region; 304 ddc->get_vmstate_memory_region = pc_dimm_get_vmstate_memory_region; 305 306 mdc->get_addr = pc_dimm_md_get_addr; 307 /* for a dimm plugged_size == region_size */ 308 mdc->get_plugged_size = pc_dimm_md_get_region_size; 309 mdc->get_region_size = pc_dimm_md_get_region_size; 310 mdc->fill_device_info = pc_dimm_md_fill_device_info; 311 } 312 313 static TypeInfo pc_dimm_info = { 314 .name = TYPE_PC_DIMM, 315 .parent = TYPE_DEVICE, 316 .instance_size = sizeof(PCDIMMDevice), 317 .instance_init = pc_dimm_init, 318 .class_init = pc_dimm_class_init, 319 .class_size = sizeof(PCDIMMDeviceClass), 320 .interfaces = (InterfaceInfo[]) { 321 { TYPE_MEMORY_DEVICE }, 322 { } 323 }, 324 }; 325 326 static void pc_dimm_register_types(void) 327 { 328 type_register_static(&pc_dimm_info); 329 } 330 331 type_init(pc_dimm_register_types) 332