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 "qapi/error.h" 24 #include "qemu/config-file.h" 25 #include "qapi/visitor.h" 26 #include "qemu/range.h" 27 #include "sysemu/numa.h" 28 #include "sysemu/kvm.h" 29 #include "trace.h" 30 #include "hw/virtio/vhost.h" 31 32 typedef struct pc_dimms_capacity { 33 uint64_t size; 34 Error **errp; 35 } pc_dimms_capacity; 36 37 void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms, 38 MemoryRegion *mr, uint64_t align, Error **errp) 39 { 40 int slot; 41 MachineState *machine = MACHINE(qdev_get_machine()); 42 PCDIMMDevice *dimm = PC_DIMM(dev); 43 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 44 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm); 45 Error *local_err = NULL; 46 uint64_t existing_dimms_capacity = 0; 47 uint64_t addr; 48 49 addr = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP, &local_err); 50 if (local_err) { 51 goto out; 52 } 53 54 addr = pc_dimm_get_free_addr(hpms->base, 55 memory_region_size(&hpms->mr), 56 !addr ? NULL : &addr, align, 57 memory_region_size(mr), &local_err); 58 if (local_err) { 59 goto out; 60 } 61 62 existing_dimms_capacity = pc_existing_dimms_capacity(&local_err); 63 if (local_err) { 64 goto out; 65 } 66 67 if (existing_dimms_capacity + memory_region_size(mr) > 68 machine->maxram_size - machine->ram_size) { 69 error_setg(&local_err, "not enough space, currently 0x%" PRIx64 70 " in use of total hot pluggable 0x" RAM_ADDR_FMT, 71 existing_dimms_capacity, 72 machine->maxram_size - machine->ram_size); 73 goto out; 74 } 75 76 object_property_set_int(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err); 77 if (local_err) { 78 goto out; 79 } 80 trace_mhp_pc_dimm_assigned_address(addr); 81 82 slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err); 83 if (local_err) { 84 goto out; 85 } 86 87 slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot, 88 machine->ram_slots, &local_err); 89 if (local_err) { 90 goto out; 91 } 92 object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err); 93 if (local_err) { 94 goto out; 95 } 96 trace_mhp_pc_dimm_assigned_slot(slot); 97 98 if (kvm_enabled() && !kvm_has_free_slot(machine)) { 99 error_setg(&local_err, "hypervisor has no free memory slots left"); 100 goto out; 101 } 102 103 if (!vhost_has_free_slot()) { 104 error_setg(&local_err, "a used vhost backend has no free" 105 " memory slots left"); 106 goto out; 107 } 108 109 memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr); 110 vmstate_register_ram(vmstate_mr, dev); 111 numa_set_mem_node_id(addr, memory_region_size(mr), dimm->node); 112 113 out: 114 error_propagate(errp, local_err); 115 } 116 117 void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms, 118 MemoryRegion *mr) 119 { 120 PCDIMMDevice *dimm = PC_DIMM(dev); 121 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 122 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm); 123 124 numa_unset_mem_node_id(dimm->addr, memory_region_size(mr), dimm->node); 125 memory_region_del_subregion(&hpms->mr, mr); 126 vmstate_unregister_ram(vmstate_mr, dev); 127 } 128 129 static int pc_existing_dimms_capacity_internal(Object *obj, void *opaque) 130 { 131 pc_dimms_capacity *cap = opaque; 132 uint64_t *size = &cap->size; 133 134 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 135 DeviceState *dev = DEVICE(obj); 136 137 if (dev->realized) { 138 (*size) += object_property_get_int(obj, PC_DIMM_SIZE_PROP, 139 cap->errp); 140 } 141 142 if (cap->errp && *cap->errp) { 143 return 1; 144 } 145 } 146 object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque); 147 return 0; 148 } 149 150 uint64_t pc_existing_dimms_capacity(Error **errp) 151 { 152 pc_dimms_capacity cap; 153 154 cap.size = 0; 155 cap.errp = errp; 156 157 pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap); 158 return cap.size; 159 } 160 161 int qmp_pc_dimm_device_list(Object *obj, void *opaque) 162 { 163 MemoryDeviceInfoList ***prev = opaque; 164 165 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 166 DeviceState *dev = DEVICE(obj); 167 168 if (dev->realized) { 169 MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1); 170 MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1); 171 PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1); 172 DeviceClass *dc = DEVICE_GET_CLASS(obj); 173 PCDIMMDevice *dimm = PC_DIMM(obj); 174 175 if (dev->id) { 176 di->has_id = true; 177 di->id = g_strdup(dev->id); 178 } 179 di->hotplugged = dev->hotplugged; 180 di->hotpluggable = dc->hotpluggable; 181 di->addr = dimm->addr; 182 di->slot = dimm->slot; 183 di->node = dimm->node; 184 di->size = object_property_get_int(OBJECT(dimm), PC_DIMM_SIZE_PROP, 185 NULL); 186 di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem)); 187 188 info->u.dimm.data = di; 189 elem->value = info; 190 elem->next = NULL; 191 **prev = elem; 192 *prev = &elem->next; 193 } 194 } 195 196 object_child_foreach(obj, qmp_pc_dimm_device_list, opaque); 197 return 0; 198 } 199 200 static int pc_dimm_slot2bitmap(Object *obj, void *opaque) 201 { 202 unsigned long *bitmap = opaque; 203 204 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 205 DeviceState *dev = DEVICE(obj); 206 if (dev->realized) { /* count only realized DIMMs */ 207 PCDIMMDevice *d = PC_DIMM(obj); 208 set_bit(d->slot, bitmap); 209 } 210 } 211 212 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque); 213 return 0; 214 } 215 216 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp) 217 { 218 unsigned long *bitmap = bitmap_new(max_slots); 219 int slot = 0; 220 221 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap); 222 223 /* check if requested slot is not occupied */ 224 if (hint) { 225 if (*hint >= max_slots) { 226 error_setg(errp, "invalid slot# %d, should be less than %d", 227 *hint, max_slots); 228 } else if (!test_bit(*hint, bitmap)) { 229 slot = *hint; 230 } else { 231 error_setg(errp, "slot %d is busy", *hint); 232 } 233 goto out; 234 } 235 236 /* search for free slot */ 237 slot = find_first_zero_bit(bitmap, max_slots); 238 if (slot == max_slots) { 239 error_setg(errp, "no free slots available"); 240 } 241 out: 242 g_free(bitmap); 243 return slot; 244 } 245 246 static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b) 247 { 248 PCDIMMDevice *x = PC_DIMM(a); 249 PCDIMMDevice *y = PC_DIMM(b); 250 Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr)); 251 252 if (int128_lt(diff, int128_zero())) { 253 return -1; 254 } else if (int128_gt(diff, int128_zero())) { 255 return 1; 256 } 257 return 0; 258 } 259 260 static int pc_dimm_built_list(Object *obj, void *opaque) 261 { 262 GSList **list = opaque; 263 264 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 265 DeviceState *dev = DEVICE(obj); 266 if (dev->realized) { /* only realized DIMMs matter */ 267 *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort); 268 } 269 } 270 271 object_child_foreach(obj, pc_dimm_built_list, opaque); 272 return 0; 273 } 274 275 uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, 276 uint64_t address_space_size, 277 uint64_t *hint, uint64_t align, uint64_t size, 278 Error **errp) 279 { 280 GSList *list = NULL, *item; 281 uint64_t new_addr, ret = 0; 282 uint64_t address_space_end = address_space_start + address_space_size; 283 284 g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start); 285 286 if (!address_space_size) { 287 error_setg(errp, "memory hotplug is not enabled, " 288 "please add maxmem option"); 289 goto out; 290 } 291 292 if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) { 293 error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", 294 align); 295 goto out; 296 } 297 298 if (QEMU_ALIGN_UP(size, align) != size) { 299 error_setg(errp, "backend memory size must be multiple of 0x%" 300 PRIx64, align); 301 goto out; 302 } 303 304 assert(address_space_end > address_space_start); 305 object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list); 306 307 if (hint) { 308 new_addr = *hint; 309 } else { 310 new_addr = address_space_start; 311 } 312 313 /* find address range that will fit new DIMM */ 314 for (item = list; item; item = g_slist_next(item)) { 315 PCDIMMDevice *dimm = item->data; 316 uint64_t dimm_size = object_property_get_int(OBJECT(dimm), 317 PC_DIMM_SIZE_PROP, 318 errp); 319 if (errp && *errp) { 320 goto out; 321 } 322 323 if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) { 324 if (hint) { 325 DeviceState *d = DEVICE(dimm); 326 error_setg(errp, "address range conflicts with '%s'", d->id); 327 goto out; 328 } 329 new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align); 330 } 331 } 332 ret = new_addr; 333 334 if (new_addr < address_space_start) { 335 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 336 "] at 0x%" PRIx64, new_addr, size, address_space_start); 337 } else if ((new_addr + size) > address_space_end) { 338 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 339 "] beyond 0x%" PRIx64, new_addr, size, address_space_end); 340 } 341 342 out: 343 g_slist_free(list); 344 return ret; 345 } 346 347 static Property pc_dimm_properties[] = { 348 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), 349 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), 350 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot, 351 PC_DIMM_UNASSIGNED_SLOT), 352 DEFINE_PROP_END_OF_LIST(), 353 }; 354 355 static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name, 356 void *opaque, Error **errp) 357 { 358 int64_t value; 359 MemoryRegion *mr; 360 PCDIMMDevice *dimm = PC_DIMM(obj); 361 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj); 362 363 mr = ddc->get_memory_region(dimm); 364 value = memory_region_size(mr); 365 366 visit_type_int(v, name, &value, errp); 367 } 368 369 static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name, 370 Object *val, Error **errp) 371 { 372 Error *local_err = NULL; 373 374 if (host_memory_backend_is_mapped(MEMORY_BACKEND(val))) { 375 char *path = object_get_canonical_path_component(val); 376 error_setg(&local_err, "can't use already busy memdev: %s", path); 377 g_free(path); 378 } else { 379 qdev_prop_allow_set_link_before_realize(obj, name, val, &local_err); 380 } 381 382 error_propagate(errp, local_err); 383 } 384 385 static void pc_dimm_init(Object *obj) 386 { 387 PCDIMMDevice *dimm = PC_DIMM(obj); 388 389 object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size, 390 NULL, NULL, NULL, &error_abort); 391 object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND, 392 (Object **)&dimm->hostmem, 393 pc_dimm_check_memdev_is_busy, 394 OBJ_PROP_LINK_UNREF_ON_RELEASE, 395 &error_abort); 396 } 397 398 static void pc_dimm_realize(DeviceState *dev, Error **errp) 399 { 400 PCDIMMDevice *dimm = PC_DIMM(dev); 401 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 402 403 if (!dimm->hostmem) { 404 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set"); 405 return; 406 } 407 if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) || 408 (!nb_numa_nodes && dimm->node)) { 409 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %" 410 PRIu32 "' which exceeds the number of numa nodes: %d", 411 dimm->node, nb_numa_nodes ? nb_numa_nodes : 1); 412 return; 413 } 414 415 if (ddc->realize) { 416 ddc->realize(dimm, errp); 417 } 418 419 host_memory_backend_set_mapped(dimm->hostmem, true); 420 } 421 422 static void pc_dimm_unrealize(DeviceState *dev, Error **errp) 423 { 424 PCDIMMDevice *dimm = PC_DIMM(dev); 425 426 host_memory_backend_set_mapped(dimm->hostmem, false); 427 } 428 429 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm) 430 { 431 return host_memory_backend_get_memory(dimm->hostmem, &error_abort); 432 } 433 434 static MemoryRegion *pc_dimm_get_vmstate_memory_region(PCDIMMDevice *dimm) 435 { 436 return host_memory_backend_get_memory(dimm->hostmem, &error_abort); 437 } 438 439 static void pc_dimm_class_init(ObjectClass *oc, void *data) 440 { 441 DeviceClass *dc = DEVICE_CLASS(oc); 442 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); 443 444 dc->realize = pc_dimm_realize; 445 dc->unrealize = pc_dimm_unrealize; 446 dc->props = pc_dimm_properties; 447 dc->desc = "DIMM memory module"; 448 449 ddc->get_memory_region = pc_dimm_get_memory_region; 450 ddc->get_vmstate_memory_region = pc_dimm_get_vmstate_memory_region; 451 } 452 453 static TypeInfo pc_dimm_info = { 454 .name = TYPE_PC_DIMM, 455 .parent = TYPE_DEVICE, 456 .instance_size = sizeof(PCDIMMDevice), 457 .instance_init = pc_dimm_init, 458 .class_init = pc_dimm_class_init, 459 .class_size = sizeof(PCDIMMDeviceClass), 460 }; 461 462 static void pc_dimm_register_types(void) 463 { 464 type_register_static(&pc_dimm_info); 465 } 466 467 type_init(pc_dimm_register_types) 468