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