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 "migration/vmstate.h" 24 #include "hw/mem/nvdimm.h" 25 #include "hw/mem/memory-device.h" 26 #include "qapi/error.h" 27 #include "qapi/visitor.h" 28 #include "qemu/module.h" 29 #include "sysemu/numa.h" 30 #include "trace.h" 31 32 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp); 33 34 void pc_dimm_pre_plug(PCDIMMDevice *dimm, MachineState *machine, 35 const uint64_t *legacy_align, Error **errp) 36 { 37 Error *local_err = NULL; 38 int slot; 39 40 slot = object_property_get_int(OBJECT(dimm), PC_DIMM_SLOT_PROP, 41 &error_abort); 42 if ((slot < 0 || slot >= machine->ram_slots) && 43 slot != PC_DIMM_UNASSIGNED_SLOT) { 44 error_setg(&local_err, "invalid slot number, valid range is [0-%" 45 PRIu64 "]", machine->ram_slots - 1); 46 goto out; 47 } 48 49 slot = pc_dimm_get_free_slot(slot == PC_DIMM_UNASSIGNED_SLOT ? NULL : &slot, 50 machine->ram_slots, &local_err); 51 if (local_err) { 52 goto out; 53 } 54 object_property_set_int(OBJECT(dimm), slot, PC_DIMM_SLOT_PROP, 55 &error_abort); 56 trace_mhp_pc_dimm_assigned_slot(slot); 57 58 memory_device_pre_plug(MEMORY_DEVICE(dimm), machine, legacy_align, 59 &local_err); 60 out: 61 error_propagate(errp, local_err); 62 } 63 64 void pc_dimm_plug(PCDIMMDevice *dimm, MachineState *machine, Error **errp) 65 { 66 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 67 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm, 68 &error_abort); 69 70 memory_device_plug(MEMORY_DEVICE(dimm), machine); 71 vmstate_register_ram(vmstate_mr, DEVICE(dimm)); 72 } 73 74 void pc_dimm_unplug(PCDIMMDevice *dimm, MachineState *machine) 75 { 76 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 77 MemoryRegion *vmstate_mr = ddc->get_vmstate_memory_region(dimm, 78 &error_abort); 79 80 memory_device_unplug(MEMORY_DEVICE(dimm), machine); 81 vmstate_unregister_ram(vmstate_mr, DEVICE(dimm)); 82 } 83 84 static int pc_dimm_slot2bitmap(Object *obj, void *opaque) 85 { 86 unsigned long *bitmap = opaque; 87 88 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 89 DeviceState *dev = DEVICE(obj); 90 if (dev->realized) { /* count only realized DIMMs */ 91 PCDIMMDevice *d = PC_DIMM(obj); 92 set_bit(d->slot, bitmap); 93 } 94 } 95 96 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque); 97 return 0; 98 } 99 100 static int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp) 101 { 102 unsigned long *bitmap; 103 int slot = 0; 104 105 if (max_slots <= 0) { 106 error_setg(errp, "no slots where allocated, please specify " 107 "the 'slots' option"); 108 return slot; 109 } 110 111 bitmap = bitmap_new(max_slots); 112 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap); 113 114 /* check if requested slot is not occupied */ 115 if (hint) { 116 if (*hint >= max_slots) { 117 error_setg(errp, "invalid slot# %d, should be less than %d", 118 *hint, max_slots); 119 } else if (!test_bit(*hint, bitmap)) { 120 slot = *hint; 121 } else { 122 error_setg(errp, "slot %d is busy", *hint); 123 } 124 goto out; 125 } 126 127 /* search for free slot */ 128 slot = find_first_zero_bit(bitmap, max_slots); 129 if (slot == max_slots) { 130 error_setg(errp, "no free slots available"); 131 } 132 out: 133 g_free(bitmap); 134 return slot; 135 } 136 137 static Property pc_dimm_properties[] = { 138 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), 139 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), 140 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot, 141 PC_DIMM_UNASSIGNED_SLOT), 142 DEFINE_PROP_LINK(PC_DIMM_MEMDEV_PROP, PCDIMMDevice, hostmem, 143 TYPE_MEMORY_BACKEND, HostMemoryBackend *), 144 DEFINE_PROP_END_OF_LIST(), 145 }; 146 147 static void pc_dimm_get_size(Object *obj, Visitor *v, const char *name, 148 void *opaque, Error **errp) 149 { 150 Error *local_err = NULL; 151 uint64_t value; 152 153 value = memory_device_get_region_size(MEMORY_DEVICE(obj), &local_err); 154 if (local_err) { 155 error_propagate(errp, local_err); 156 return; 157 } 158 159 visit_type_uint64(v, name, &value, errp); 160 } 161 162 static void pc_dimm_init(Object *obj) 163 { 164 object_property_add(obj, PC_DIMM_SIZE_PROP, "uint64", pc_dimm_get_size, 165 NULL, NULL, NULL, &error_abort); 166 } 167 168 static void pc_dimm_realize(DeviceState *dev, Error **errp) 169 { 170 PCDIMMDevice *dimm = PC_DIMM(dev); 171 PCDIMMDeviceClass *ddc = PC_DIMM_GET_CLASS(dimm); 172 173 if (!dimm->hostmem) { 174 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set"); 175 return; 176 } else if (host_memory_backend_is_mapped(dimm->hostmem)) { 177 char *path = object_get_canonical_path_component(OBJECT(dimm->hostmem)); 178 error_setg(errp, "can't use already busy memdev: %s", path); 179 g_free(path); 180 return; 181 } 182 if (((nb_numa_nodes > 0) && (dimm->node >= nb_numa_nodes)) || 183 (!nb_numa_nodes && dimm->node)) { 184 error_setg(errp, "'DIMM property " PC_DIMM_NODE_PROP " has value %" 185 PRIu32 "' which exceeds the number of numa nodes: %d", 186 dimm->node, nb_numa_nodes ? nb_numa_nodes : 1); 187 return; 188 } 189 190 if (ddc->realize) { 191 ddc->realize(dimm, errp); 192 } 193 194 host_memory_backend_set_mapped(dimm->hostmem, true); 195 } 196 197 static void pc_dimm_unrealize(DeviceState *dev, Error **errp) 198 { 199 PCDIMMDevice *dimm = PC_DIMM(dev); 200 201 host_memory_backend_set_mapped(dimm->hostmem, false); 202 } 203 204 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm, Error **errp) 205 { 206 if (!dimm->hostmem) { 207 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property must be set"); 208 return NULL; 209 } 210 211 return host_memory_backend_get_memory(dimm->hostmem); 212 } 213 214 static uint64_t pc_dimm_md_get_addr(const MemoryDeviceState *md) 215 { 216 return object_property_get_uint(OBJECT(md), PC_DIMM_ADDR_PROP, &error_abort); 217 } 218 219 static void pc_dimm_md_set_addr(MemoryDeviceState *md, uint64_t addr, 220 Error **errp) 221 { 222 object_property_set_uint(OBJECT(md), addr, PC_DIMM_ADDR_PROP, errp); 223 } 224 225 static MemoryRegion *pc_dimm_md_get_memory_region(MemoryDeviceState *md, 226 Error **errp) 227 { 228 return pc_dimm_get_memory_region(PC_DIMM(md), errp); 229 } 230 231 static void pc_dimm_md_fill_device_info(const MemoryDeviceState *md, 232 MemoryDeviceInfo *info) 233 { 234 PCDIMMDeviceInfo *di = g_new0(PCDIMMDeviceInfo, 1); 235 const DeviceClass *dc = DEVICE_GET_CLASS(md); 236 const PCDIMMDevice *dimm = PC_DIMM(md); 237 const DeviceState *dev = DEVICE(md); 238 239 if (dev->id) { 240 di->has_id = true; 241 di->id = g_strdup(dev->id); 242 } 243 di->hotplugged = dev->hotplugged; 244 di->hotpluggable = dc->hotpluggable; 245 di->addr = dimm->addr; 246 di->slot = dimm->slot; 247 di->node = dimm->node; 248 di->size = object_property_get_uint(OBJECT(dimm), PC_DIMM_SIZE_PROP, 249 NULL); 250 di->memdev = object_get_canonical_path(OBJECT(dimm->hostmem)); 251 252 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { 253 info->u.nvdimm.data = di; 254 info->type = MEMORY_DEVICE_INFO_KIND_NVDIMM; 255 } else { 256 info->u.dimm.data = di; 257 info->type = MEMORY_DEVICE_INFO_KIND_DIMM; 258 } 259 } 260 261 static void pc_dimm_class_init(ObjectClass *oc, void *data) 262 { 263 DeviceClass *dc = DEVICE_CLASS(oc); 264 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); 265 MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(oc); 266 267 dc->realize = pc_dimm_realize; 268 dc->unrealize = pc_dimm_unrealize; 269 dc->props = pc_dimm_properties; 270 dc->desc = "DIMM memory module"; 271 272 ddc->get_vmstate_memory_region = pc_dimm_get_memory_region; 273 274 mdc->get_addr = pc_dimm_md_get_addr; 275 mdc->set_addr = pc_dimm_md_set_addr; 276 /* for a dimm plugged_size == region_size */ 277 mdc->get_plugged_size = memory_device_get_region_size; 278 mdc->get_memory_region = pc_dimm_md_get_memory_region; 279 mdc->fill_device_info = pc_dimm_md_fill_device_info; 280 } 281 282 static TypeInfo pc_dimm_info = { 283 .name = TYPE_PC_DIMM, 284 .parent = TYPE_DEVICE, 285 .instance_size = sizeof(PCDIMMDevice), 286 .instance_init = pc_dimm_init, 287 .class_init = pc_dimm_class_init, 288 .class_size = sizeof(PCDIMMDeviceClass), 289 .interfaces = (InterfaceInfo[]) { 290 { TYPE_MEMORY_DEVICE }, 291 { } 292 }, 293 }; 294 295 static void pc_dimm_register_types(void) 296 { 297 type_register_static(&pc_dimm_info); 298 } 299 300 type_init(pc_dimm_register_types) 301