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