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