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