pc-dimm.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Dimm device for Memory Hotplug
 *
 * Copyright ProfitBricks GmbH 2012
 * Copyright (C) 2014 Red Hat Inc
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>
 */

P
Peter Maydell 已提交
21
#include "qemu/osdep.h"
22
#include "hw/mem/pc-dimm.h"
23
#include "hw/mem/nvdimm.h"
24
#include "qapi/error.h"
25 26
#include "qemu/config-file.h"
#include "qapi/visitor.h"
27
#include "qemu/range.h"
28
#include "sysemu/numa.h"
29 30
#include "sysemu/kvm.h"
#include "trace.h"
31
#include "hw/virtio/vhost.h"
32

33 34 35 36 37
typedef struct pc_dimms_capacity {
     uint64_t size;
     Error    **errp;
} pc_dimms_capacity;

38
void pc_dimm_memory_plug(DeviceState *dev, MemoryHotplugState *hpms,
39
                         MemoryRegion *mr, uint64_t align, Error **errp)
40 41 42 43
{
    int slot;
    MachineState *machine = MACHINE(qdev_get_machine());
    PCDIMMDevice *dimm = PC_DIMM(dev);
44 45
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
    MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm);
46 47 48 49
    Error *local_err = NULL;
    uint64_t existing_dimms_capacity = 0;
    uint64_t addr;

50 51
    addr = object_property_get_uint(OBJECT(dimm),
                                    PC_DIMM_ADDR_PROP, &local_err);
52 53 54 55 56 57
    if (local_err) {
        goto out;
    }

    addr = pc_dimm_get_free_addr(hpms->base,
                                 memory_region_size(&hpms->mr),
58
                                 !addr ? NULL : &addr, align,
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                                 memory_region_size(mr), &local_err);
    if (local_err) {
        goto out;
    }

    existing_dimms_capacity = pc_existing_dimms_capacity(&local_err);
    if (local_err) {
        goto out;
    }

    if (existing_dimms_capacity + memory_region_size(mr) >
        machine->maxram_size - machine->ram_size) {
        error_setg(&local_err, "not enough space, currently 0x%" PRIx64
                   " in use of total hot pluggable 0x" RAM_ADDR_FMT,
                   existing_dimms_capacity,
                   machine->maxram_size - machine->ram_size);
        goto out;
    }

78
    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (local_err) {
        goto out;
    }
    trace_mhp_pc_dimm_assigned_address(addr);

    slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP, &local_err);
    if (local_err) {
        goto out;
    }

    slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
                                 machine->ram_slots, &local_err);
    if (local_err) {
        goto out;
    }
    object_property_set_int(OBJECT(dev), slot, PC_DIMM_SLOT_PROP, &local_err);
    if (local_err) {
        goto out;
    }
    trace_mhp_pc_dimm_assigned_slot(slot);

    if (kvm_enabled() && !kvm_has_free_slot(machine)) {
        error_setg(&local_err, "hypervisor has no free memory slots left");
        goto out;
    }

105 106 107 108 109 110
    if (!vhost_has_free_slot()) {
        error_setg(&local_err, "a used vhost backend has no free"
                               " memory slots left");
        goto out;
    }

111
    memory_region_add_subregion(&hpms->mr, addr - hpms->base, mr);
112
    vmstate_register_ram(vmstate_mr, dev);
113 114 115 116 117 118 119 120

out:
    error_propagate(errp, local_err);
}

void pc_dimm_memory_unplug(DeviceState *dev, MemoryHotplugState *hpms,
                           MemoryRegion *mr)
{
121
    PCDIMMDevice *dimm = PC_DIMM(dev);
122 123
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
    MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm);
124

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 132
    pc_dimms_capacity *cap = opaque;
    uint64_t *size = &cap->size;
133 134 135 136 137

    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
        DeviceState *dev = DEVICE(obj);

        if (dev->realized) {
138
            (*size) += object_property_get_uint(obj, PC_DIMM_SIZE_PROP,
139
                cap->errp);
140 141
        }

142
        if (cap->errp && *cap->errp) {
143 144 145
            return 1;
        }
    }
146
    object_child_foreach(obj, pc_existing_dimms_capacity_internal, opaque);
147 148
    return 0;
}
149

150 151 152 153 154 155 156 157 158 159 160
uint64_t pc_existing_dimms_capacity(Error **errp)
{
    pc_dimms_capacity cap;

    cap.size = 0;
    cap.errp = errp;

    pc_existing_dimms_capacity_internal(qdev_get_machine(), &cap);
    return cap.size;
}

161 162 163 164 165
uint64_t get_plugged_memory_size(void)
{
    return pc_existing_dimms_capacity(&error_abort);
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static int pc_dimm_slot2bitmap(Object *obj, void *opaque)
{
    unsigned long *bitmap = opaque;

    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
        DeviceState *dev = DEVICE(obj);
        if (dev->realized) { /* count only realized DIMMs */
            PCDIMMDevice *d = PC_DIMM(obj);
            set_bit(d->slot, bitmap);
        }
    }

    object_child_foreach(obj, pc_dimm_slot2bitmap, opaque);
    return 0;
}

int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
{
    unsigned long *bitmap = bitmap_new(max_slots);
    int slot = 0;

    object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap);

    /* check if requested slot is not occupied */
    if (hint) {
        if (*hint >= max_slots) {
            error_setg(errp, "invalid slot# %d, should be less than %d",
                       *hint, max_slots);
        } else if (!test_bit(*hint, bitmap)) {
            slot = *hint;
        } else {
            error_setg(errp, "slot %d is busy", *hint);
        }
        goto out;
    }

    /* search for free slot */
    slot = find_first_zero_bit(bitmap, max_slots);
    if (slot == max_slots) {
        error_setg(errp, "no free slots available");
    }
out:
    g_free(bitmap);
    return slot;
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b)
{
    PCDIMMDevice *x = PC_DIMM(a);
    PCDIMMDevice *y = PC_DIMM(b);
    Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr));

    if (int128_lt(diff, int128_zero())) {
        return -1;
    } else if (int128_gt(diff, int128_zero())) {
        return 1;
    }
    return 0;
}

static int pc_dimm_built_list(Object *obj, void *opaque)
{
    GSList **list = opaque;

    if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
        DeviceState *dev = DEVICE(obj);
        if (dev->realized) { /* only realized DIMMs matter */
            *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort);
        }
    }

    object_child_foreach(obj, pc_dimm_built_list, opaque);
    return 0;
}

241 242 243 244 245 246 247 248 249 250 251 252 253
MemoryDeviceInfoList *qmp_pc_dimm_device_list(void)
{
    GSList *dimms = NULL, *item;
    MemoryDeviceInfoList *list = NULL, *prev = NULL;

    object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &dimms);

    for (item = dimms; item; item = g_slist_next(item)) {
        PCDIMMDevice *dimm = PC_DIMM(item->data);
        Object *obj = OBJECT(dimm);
        MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1);
        MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1);
        PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
254
        bool is_nvdimm = object_dynamic_cast(obj, TYPE_NVDIMM);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        DeviceClass *dc = DEVICE_GET_CLASS(obj);
        DeviceState *dev = DEVICE(obj);

        if (dev->id) {
            di->has_id = true;
            di->id = g_strdup(dev->id);
        }
        di->hotplugged = dev->hotplugged;
        di->hotpluggable = dc->hotpluggable;
        di->addr = dimm->addr;
        di->slot = dimm->slot;
        di->node = dimm->node;
        di->size = object_property_get_uint(obj, PC_DIMM_SIZE_PROP, NULL);
        di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));

270 271 272 273 274 275 276
        if (!is_nvdimm) {
            info->u.dimm.data = di;
            info->type = MEMORY_DEVICE_INFO_KIND_DIMM;
        } else {
            info->u.nvdimm.data = di;
            info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM;
        }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        elem->value = info;
        elem->next = NULL;
        if (prev) {
            prev->next = elem;
        } else {
            list = elem;
        }
        prev = elem;
    }

    g_slist_free(dimms);

    return list;
}

292 293
uint64_t pc_dimm_get_free_addr(uint64_t address_space_start,
                               uint64_t address_space_size,
294 295
                               uint64_t *hint, uint64_t align, uint64_t size,
                               Error **errp)
296 297 298 299 300
{
    GSList *list = NULL, *item;
    uint64_t new_addr, ret = 0;
    uint64_t address_space_end = address_space_start + address_space_size;

301 302
    g_assert(QEMU_ALIGN_UP(address_space_start, align) == address_space_start);

303 304 305 306 307 308
    if (!address_space_size) {
        error_setg(errp, "memory hotplug is not enabled, "
                         "please add maxmem option");
        goto out;
    }

309 310 311 312 313 314 315 316 317 318 319 320
    if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) {
        error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes",
                   align);
        goto out;
    }

    if (QEMU_ALIGN_UP(size, align) != size) {
        error_setg(errp, "backend memory size must be multiple of 0x%"
                   PRIx64, align);
        goto out;
    }

321
    assert(address_space_end > address_space_start);
322 323 324 325 326 327 328 329 330 331 332
    object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list);

    if (hint) {
        new_addr = *hint;
    } else {
        new_addr = address_space_start;
    }

    /* find address range that will fit new DIMM */
    for (item = list; item; item = g_slist_next(item)) {
        PCDIMMDevice *dimm = item->data;
333 334 335
        uint64_t dimm_size = object_property_get_uint(OBJECT(dimm),
                                                      PC_DIMM_SIZE_PROP,
                                                      errp);
336 337 338 339
        if (errp && *errp) {
            goto out;
        }

340
        if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) {
341 342 343 344 345
            if (hint) {
                DeviceState *d = DEVICE(dimm);
                error_setg(errp, "address range conflicts with '%s'", d->id);
                goto out;
            }
346
            new_addr = QEMU_ALIGN_UP(dimm->addr + dimm_size, align);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        }
    }
    ret = new_addr;

    if (new_addr < address_space_start) {
        error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
                   "] at 0x%" PRIx64, new_addr, size, address_space_start);
    } else if ((new_addr + size) > address_space_end) {
        error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64
                   "] beyond 0x%" PRIx64, new_addr, size, address_space_end);
    }

out:
    g_slist_free(list);
    return ret;
}
363 364 365 366 367 368

static Property pc_dimm_properties[] = {
    DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0),
    DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0),
    DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot,
                      PC_DIMM_UNASSIGNED_SLOT),
F
Fam Zheng 已提交
369 370
    DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
                     TYPE_MEMORY_BACKEND, HostMemoryBackend *),
371 372 373
    DEFINE_PROP_END_OF_LIST(),
};

374 375
static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
                             void *opaque, Error **errp)
376
{
377
    uint64_t value;
378 379
    MemoryRegion *mr;
    PCDIMMDevice *dimm = PC_DIMM(obj);
380
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj);
381

382 383 384 385
    mr = ddc->get_memory_region(dimm, errp);
    if (!mr) {
        return;
    }
386 387
    value = memory_region_size(mr);

388
    visit_type_uint64(v, name, &value, errp);
389 390 391 392
}

static void pc_dimm_init(Object *obj)
{
393
    object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
394 395 396 397 398 399
                        NULL, NULL, NULL, &error_abort);
}

static void pc_dimm_realize(DeviceState *dev, Error **errp)
{
    PCDIMMDevice *dimm = PC_DIMM(dev);
400
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
401 402 403 404

    if (!dimm->hostmem) {
        error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
        return;
F
Fam Zheng 已提交
405 406 407 408 409
    } else if (host_memory_backend_is_mapped(dimm->hostmem)) {
        char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem));
        error_setg(errp, "can't use already busy memdev: %s", path);
        g_free(path);
        return;
410
    }
411 412
    if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
        (!nb_numa_nodes && dimm->node)) {
413 414
        error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
                   PRIu32 "' which exceeds the number of numa nodes: %d",
415
                   dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
H
Hu Tao 已提交
416 417
        return;
    }
418 419 420 421

    if (ddc->realize) {
        ddc->realize(dimm, errp);
    }
422 423 424 425 426 427 428 429 430

    host_memory_backend_set_mapped(dimm->hostmem, true);
}

static void pc_dimm_unrealize(DeviceState *dev, Error **errp)
{
    PCDIMMDevice *dimm = PC_DIMM(dev);

    host_memory_backend_set_mapped(dimm->hostmem, false);
431 432
}

433
static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
434
{
435 436 437 438 439 440
    if (!dimm->hostmem) {
        error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
        return NULL;
    }

    return host_memory_backend_get_memory(dimm->hostmem, errp);
441 442
}

443 444 445 446 447
static MemoryRegion *pc_dimm_get_vmstate_memory_region(PCDIMMDevice *dimm)
{
    return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
}

448 449 450 451 452 453
static void pc_dimm_class_init(ObjectClass *oc, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(oc);
    PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);

    dc->realize = pc_dimm_realize;
454
    dc->unrealize = pc_dimm_unrealize;
455
    dc->props = pc_dimm_properties;
456
    dc->desc = "DIMM memory module";
457 458

    ddc->get_memory_region = pc_dimm_get_memory_region;
459
    ddc->get_vmstate_memory_region = pc_dimm_get_vmstate_memory_region;
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
}

static TypeInfo pc_dimm_info = {
    .name          = TYPE_PC_DIMM,
    .parent        = TYPE_DEVICE,
    .instance_size = sizeof(PCDIMMDevice),
    .instance_init = pc_dimm_init,
    .class_init    = pc_dimm_class_init,
    .class_size    = sizeof(PCDIMMDeviceClass),
};

static void pc_dimm_register_types(void)
{
    type_register_static(&pc_dimm_info);
}

type_init(pc_dimm_register_types)