2014-06-02 17:25:05 +04:00
|
|
|
/*
|
|
|
|
* 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/>
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:30 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-06-02 17:25:05 +04:00
|
|
|
#include "hw/mem/pc-dimm.h"
|
2018-03-11 06:02:12 +03:00
|
|
|
#include "hw/mem/nvdimm.h"
|
2018-04-23 19:51:16 +03:00
|
|
|
#include "hw/mem/memory-device.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2014-06-02 17:25:05 +04:00
|
|
|
#include "qapi/visitor.h"
|
2015-02-08 21:51:16 +03:00
|
|
|
#include "sysemu/numa.h"
|
2015-06-29 11:20:23 +03:00
|
|
|
#include "trace.h"
|
2015-01-27 07:05:01 +03:00
|
|
|
|
2018-06-19 16:41:33 +03:00
|
|
|
static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp);
|
|
|
|
|
2018-10-05 12:20:12 +03:00
|
|
|
void pc_dimm_pre_plug(PCDIMMDevice *dimm, MachineState *machine,
|
2018-08-01 16:34:44 +03:00
|
|
|
const uint64_t *legacy_align, Error **errp)
|
2018-08-01 16:34:41 +03:00
|
|
|
{
|
2018-08-01 16:34:44 +03:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
|
2018-08-01 16:34:41 +03:00
|
|
|
Error *local_err = NULL;
|
2018-08-01 16:34:44 +03:00
|
|
|
MemoryRegion *mr;
|
|
|
|
uint64_t addr, align;
|
2018-08-01 16:34:41 +03:00
|
|
|
int slot;
|
|
|
|
|
2018-10-05 12:20:12 +03:00
|
|
|
slot = object_property_get_int(OBJECT(dimm), PC_DIMM_SLOT_PROP,
|
2018-08-01 16:34:41 +03:00
|
|
|
&error_abort);
|
|
|
|
slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot,
|
|
|
|
machine->ram_slots, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2018-10-05 12:20:12 +03:00
|
|
|
object_property_set_int(OBJECT(dimm), slot, PC_DIMM_SLOT_PROP,
|
|
|
|
&error_abort);
|
2018-08-01 16:34:41 +03:00
|
|
|
trace_mhp_pc_dimm_assigned_slot(slot);
|
2018-08-01 16:34:44 +03:00
|
|
|
|
|
|
|
mr = ddc->get_memory_region(dimm, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
align = legacy_align ? *legacy_align : memory_region_get_alignment(mr);
|
2018-10-05 12:20:12 +03:00
|
|
|
addr = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
|
2018-08-01 16:34:44 +03:00
|
|
|
&error_abort);
|
|
|
|
addr = memory_device_get_free_addr(machine, !addr ? NULL : &addr, align,
|
|
|
|
memory_region_size(mr), &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
trace_mhp_pc_dimm_assigned_address(addr);
|
2018-10-05 12:20:12 +03:00
|
|
|
object_property_set_uint(OBJECT(dimm), addr, PC_DIMM_ADDR_PROP,
|
2018-08-01 16:34:44 +03:00
|
|
|
&error_abort);
|
2018-08-01 16:34:41 +03:00
|
|
|
out:
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:20:12 +03:00
|
|
|
void pc_dimm_plug(PCDIMMDevice *dimm, MachineState *machine, Error **errp)
|
2015-06-29 11:20:23 +03:00
|
|
|
{
|
2016-06-07 15:21:57 +03:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
|
2018-06-19 16:41:37 +03:00
|
|
|
MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
|
|
|
|
&error_abort);
|
2018-06-19 16:41:41 +03:00
|
|
|
MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
|
2015-06-29 11:20:23 +03:00
|
|
|
uint64_t addr;
|
|
|
|
|
2018-10-05 12:20:12 +03:00
|
|
|
addr = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
|
2018-08-01 16:34:44 +03:00
|
|
|
&error_abort);
|
2015-06-29 11:20:23 +03:00
|
|
|
|
2018-04-23 19:51:22 +03:00
|
|
|
memory_device_plug_region(machine, mr, addr);
|
2018-10-05 12:20:12 +03:00
|
|
|
vmstate_register_ram(vmstate_mr, DEVICE(dimm));
|
2015-06-29 11:20:23 +03:00
|
|
|
}
|
|
|
|
|
2018-10-05 12:20:12 +03:00
|
|
|
void pc_dimm_unplug(PCDIMMDevice *dimm, MachineState *machine)
|
2015-06-29 11:20:23 +03:00
|
|
|
{
|
2016-06-07 15:21:57 +03:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
|
2018-06-19 16:41:37 +03:00
|
|
|
MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm,
|
|
|
|
&error_abort);
|
2018-04-23 19:51:18 +03:00
|
|
|
MemoryRegion *mr = ddc->get_memory_region(dimm, &error_abort);
|
2015-06-29 11:20:25 +03:00
|
|
|
|
2018-04-23 19:51:22 +03:00
|
|
|
memory_device_unplug_region(machine, mr);
|
2018-10-05 12:20:12 +03:00
|
|
|
vmstate_unregister_ram(vmstate_mr, DEVICE(dimm));
|
2015-06-29 11:20:23 +03:00
|
|
|
}
|
|
|
|
|
2014-06-02 17:25:14 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:41:33 +03:00
|
|
|
static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp)
|
2014-06-02 17:25:14 +04:00
|
|
|
{
|
2018-04-27 15:05:15 +03:00
|
|
|
unsigned long *bitmap;
|
2014-06-02 17:25:14 +04:00
|
|
|
int slot = 0;
|
|
|
|
|
2018-04-27 15:05:15 +03:00
|
|
|
if (max_slots <= 0) {
|
|
|
|
error_setg(errp, "no slots where allocated, please specify "
|
|
|
|
"the 'slots' option");
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitmap = bitmap_new(max_slots);
|
2014-06-02 17:25:14 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-02 17:25:05 +04:00
|
|
|
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),
|
2017-07-14 05:14:59 +03:00
|
|
|
DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem,
|
|
|
|
TYPE_MEMORY_BACKEND, HostMemoryBackend *),
|
2014-06-02 17:25:05 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
qom: Swap 'name' next to visitor in ObjectPropertyAccessor
Similar to the previous patch, it's nice to have all functions
in the tree that involve a visitor and a name for conversion to
or from QAPI to consistently stick the 'name' parameter next
to the Visitor parameter.
Done by manually changing include/qom/object.h and qom/object.c,
then running this Coccinelle script and touching up the fallout
(Coccinelle insisted on adding some trailing whitespace).
@ rule1 @
identifier fn;
typedef Object, Visitor, Error;
identifier obj, v, opaque, name, errp;
@@
void fn
- (Object *obj, Visitor *v, void *opaque, const char *name,
+ (Object *obj, Visitor *v, const char *name, void *opaque,
Error **errp) { ... }
@@
identifier rule1.fn;
expression obj, v, opaque, name, errp;
@@
fn(obj, v,
- opaque, name,
+ name, opaque,
errp)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-20-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 16:48:55 +03:00
|
|
|
static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name,
|
|
|
|
void *opaque, Error **errp)
|
2014-06-02 17:25:05 +04:00
|
|
|
{
|
2018-10-05 12:20:17 +03:00
|
|
|
Error *local_err = NULL;
|
2017-06-07 19:36:14 +03:00
|
|
|
uint64_t value;
|
2014-06-02 17:25:05 +04:00
|
|
|
|
2018-10-05 12:20:17 +03:00
|
|
|
value = memory_device_get_region_size(MEMORY_DEVICE(obj), &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2017-08-21 09:30:29 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-06-02 17:25:05 +04:00
|
|
|
|
2017-06-07 19:36:14 +03:00
|
|
|
visit_type_uint64(v, name, &value, errp);
|
2014-06-02 17:25:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pc_dimm_init(Object *obj)
|
|
|
|
{
|
2017-06-07 19:36:14 +03:00
|
|
|
object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size,
|
2014-06-02 17:25:05 +04:00
|
|
|
NULL, NULL, NULL, &error_abort);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pc_dimm_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
PCDIMMDevice *dimm = PC_DIMM(dev);
|
2016-05-20 11:19:59 +03:00
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm);
|
2014-06-02 17:25:05 +04:00
|
|
|
|
|
|
|
if (!dimm->hostmem) {
|
|
|
|
error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set");
|
|
|
|
return;
|
2017-07-14 05:14:59 +03:00
|
|
|
} 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;
|
2014-06-02 17:25:05 +04:00
|
|
|
}
|
2015-07-17 15:49:40 +03:00
|
|
|
if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) ||
|
|
|
|
(!nb_numa_nodes && dimm->node)) {
|
2014-08-04 16:21:59 +04:00
|
|
|
error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %"
|
|
|
|
PRIu32 "' which exceeds the number of numa nodes: %d",
|
2015-07-17 15:49:40 +03:00
|
|
|
dimm->node, nb_numa_nodes ? nb_numa_nodes : 1);
|
2014-08-04 12:16:08 +04:00
|
|
|
return;
|
|
|
|
}
|
2016-05-20 11:19:59 +03:00
|
|
|
|
|
|
|
if (ddc->realize) {
|
|
|
|
ddc->realize(dimm, errp);
|
|
|
|
}
|
2016-07-13 07:18:06 +03:00
|
|
|
|
|
|
|
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);
|
2014-06-02 17:25:05 +04:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:30:29 +03:00
|
|
|
static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp)
|
2014-06-02 17:25:05 +04:00
|
|
|
{
|
2017-08-21 09:30:29 +03:00
|
|
|
if (!dimm->hostmem) {
|
|
|
|
error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:41:36 +03:00
|
|
|
return host_memory_backend_get_memory(dimm->hostmem);
|
2014-06-02 17:25:05 +04:00
|
|
|
}
|
|
|
|
|
2018-04-23 19:51:16 +03:00
|
|
|
static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md)
|
|
|
|
{
|
|
|
|
const PCDIMMDevice *dimm = PC_DIMM(md);
|
|
|
|
|
|
|
|
return dimm->addr;
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:20:15 +03:00
|
|
|
static uint64_t pc_dimm_md_get_region_size(const MemoryDeviceState *md,
|
|
|
|
Error **errp)
|
2018-04-23 19:51:16 +03:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
2018-10-05 12:20:15 +03:00
|
|
|
mr = ddc->get_memory_region(dimm, errp);
|
2018-04-23 19:51:16 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 17:25:05 +04:00
|
|
|
static void pc_dimm_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc);
|
2018-04-23 19:51:16 +03:00
|
|
|
MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc);
|
2014-06-02 17:25:05 +04:00
|
|
|
|
|
|
|
dc->realize = pc_dimm_realize;
|
2016-07-13 07:18:06 +03:00
|
|
|
dc->unrealize = pc_dimm_unrealize;
|
2014-06-02 17:25:05 +04:00
|
|
|
dc->props = pc_dimm_properties;
|
2015-03-10 14:25:51 +03:00
|
|
|
dc->desc = "DIMM memory module";
|
2014-06-02 17:25:05 +04:00
|
|
|
|
|
|
|
ddc->get_memory_region = pc_dimm_get_memory_region;
|
2018-06-19 16:41:37 +03:00
|
|
|
ddc->get_vmstate_memory_region = pc_dimm_get_memory_region;
|
2018-04-23 19:51:16 +03:00
|
|
|
|
|
|
|
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;
|
2014-06-02 17:25:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2018-04-23 19:51:16 +03:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ TYPE_MEMORY_DEVICE },
|
|
|
|
{ }
|
|
|
|
},
|
2014-06-02 17:25:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void pc_dimm_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&pc_dimm_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(pc_dimm_register_types)
|