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