pc-dimm.c 9.6 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 "hw/mem/memory-device.h"
25
#include "qapi/error.h"
26
#include "qapi/visitor.h"
27
#include "sysemu/numa.h"
28
#include "trace.h"
29

30
void pc_dimm_memory_plug(DeviceState *dev, MachineState *machine,
31
                         uint64_t align, Error **errp)
32 33 34
{
    int slot;
    PCDIMMDevice *dimm = PC_DIMM(dev);
35 36
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
    MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm);
37
    Error *local_err = NULL;
38
    MemoryRegion *mr;
39 40
    uint64_t addr;

41 42 43 44 45
    mr = ddc->get_memory_region(dimm, &local_err);
    if (local_err) {
        goto out;
    }

46 47
    addr = object_property_get_uint(OBJECT(dimm),
                                    PC_DIMM_ADDR_PROP, &local_err);
48 49 50 51
    if (local_err) {
        goto out;
    }

52 53
    addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
                                       memory_region_size(mr), &local_err);
54 55 56 57
    if (local_err) {
        goto out;
    }

58
    object_property_set_uint(OBJECT(dev), addr, PC_DIMM_ADDR_PROP, &local_err);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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);

80
    memory_device_plug_region(machine, mr, addr);
81
    vmstate_register_ram(vmstate_mr, dev);
82 83 84 85 86

out:
    error_propagate(errp, local_err);
}

87
void pc_dimm_memory_unplug(DeviceState *dev, MachineState *machine)
88
{
89
    PCDIMMDevice *dimm = PC_DIMM(dev);
90 91
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
    MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm);
92
    MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
93

94
    memory_device_unplug_region(machine, mr);
95
    vmstate_unregister_ram(vmstate_mr, dev);
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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)
{
116
    unsigned long *bitmap;
117 118
    int slot = 0;

119 120 121 122 123 124 125
    if (max_slots <= 0) {
        error_setg(errp, "no slots where allocated, please specify "
                   "the 'slots' option");
        return slot;
    }

    bitmap = bitmap_new(max_slots);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    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;
}

151 152 153 154 155
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 已提交
156 157
    DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
                     TYPE_MEMORY_BACKEND, HostMemoryBackend *),
158 159 160
    DEFINE_PROP_END_OF_LIST(),
};

161 162
static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
                             void *opaque, Error **errp)
163
{
164
    uint64_t value;
165 166
    MemoryRegion *mr;
    PCDIMMDevice *dimm = PC_DIMM(obj);
167
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(obj);
168

169 170 171 172
    mr = ddc->get_memory_region(dimm, errp);
    if (!mr) {
        return;
    }
173 174
    value = memory_region_size(mr);

175
    visit_type_uint64(v, name, &value, errp);
176 177 178 179
}

static void pc_dimm_init(Object *obj)
{
180
    object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
181 182 183 184 185 186
                        NULL, NULL, NULL, &error_abort);
}

static void pc_dimm_realize(DeviceState *dev, Error **errp)
{
    PCDIMMDevice *dimm = PC_DIMM(dev);
187
    PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
188 189 190 191

    if (!dimm->hostmem) {
        error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
        return;
F
Fam Zheng 已提交
192 193 194 195 196
    } 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;
197
    }
198 199
    if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
        (!nb_numa_nodes && dimm->node)) {
200 201
        error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
                   PRIu32 "' which exceeds the number of numa nodes: %d",
202
                   dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
H
Hu Tao 已提交
203 204
        return;
    }
205 206 207 208

    if (ddc->realize) {
        ddc->realize(dimm, errp);
    }
209 210 211 212 213 214 215 216 217

    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);
218 219
}

220
static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
221
{
222 223 224 225 226 227
    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);
228 229
}

230 231 232 233 234
static MemoryRegion *pc_dimm_get_vmstate_memory_region(PCDIMMDevice *dimm)
{
    return host_memory_backend_get_memory(dimm->hostmem, &error_abort);
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md)
{
    const PCDIMMDevice *dimm = PC_DIMM(md);

    return dimm->addr;
}

static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState *md)
{
    /* dropping const here is fine as we don't touch the memory region */
    PCDIMMDevice *dimm = PC_DIMM(md);
    const PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(md);
    MemoryRegion *mr;

    mr = ddc->get_memory_region(dimm, &error_abort);
    if (!mr) {
        return 0;
    }

    return memory_region_size(mr);
}

static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md,
                                        MemoryDeviceInfo *info)
{
    PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1);
    const DeviceClass *dc = DEVICE_GET_CLASS(md);
    const PCDIMMDevice *dimm = PC_DIMM(md);
    const DeviceState *dev = DEVICE(md);

    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(OBJECT(dimm), PC_DIMM_SIZE_PROP,
                                        NULL);
    di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem));

    if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
        info->u.nvdimm.data = di;
        info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM;
    } else {
        info->u.dimm.data = di;
        info->type = MEMORY_DEVICE_INFO_KIND_DIMM;
    }
}

287 288 289 290
static void pc_dimm_class_init(ObjectClass *oc, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(oc);
    PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
291
    MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
292 293

    dc->realize = pc_dimm_realize;
294
    dc->unrealize = pc_dimm_unrealize;
295
    dc->props = pc_dimm_properties;
296
    dc->desc = "DIMM memory module";
297 298

    ddc->get_memory_region = pc_dimm_get_memory_region;
299
    ddc->get_vmstate_memory_region = pc_dimm_get_vmstate_memory_region;
300 301 302 303 304 305

    mdc->get_addr = pc_dimm_md_get_addr;
    /* for a dimm plugged_size == region_size */
    mdc->get_plugged_size = pc_dimm_md_get_region_size;
    mdc->get_region_size = pc_dimm_md_get_region_size;
    mdc->fill_device_info = pc_dimm_md_fill_device_info;
306 307 308 309 310 311 312 313 314
}

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),
315 316 317 318
    .interfaces = (InterfaceInfo[]) {
        { TYPE_MEMORY_DEVICE },
        { }
    },
319 320 321 322 323 324 325 326
};

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

type_init(pc_dimm_register_types)