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 "hw/mem/pc-dimm.h" 22 #include "qemu/config-file.h" 23 #include "qapi/visitor.h" 24 #include "qemu/range.h" 25 26 static int pc_dimm_slot2bitmap(Object *obj, void *opaque) 27 { 28 unsigned long *bitmap = opaque; 29 30 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 31 DeviceState *dev = DEVICE(obj); 32 if (dev->realized) { /* count only realized DIMMs */ 33 PCDIMMDevice *d = PC_DIMM(obj); 34 set_bit(d->slot, bitmap); 35 } 36 } 37 38 object_child_foreach(obj, pc_dimm_slot2bitmap, opaque); 39 return 0; 40 } 41 42 int pc_dimm_get_free_slot(const int *hint, int max_slots, Error **errp) 43 { 44 unsigned long *bitmap = bitmap_new(max_slots); 45 int slot = 0; 46 47 object_child_foreach(qdev_get_machine(), pc_dimm_slot2bitmap, bitmap); 48 49 /* check if requested slot is not occupied */ 50 if (hint) { 51 if (*hint >= max_slots) { 52 error_setg(errp, "invalid slot# %d, should be less than %d", 53 *hint, max_slots); 54 } else if (!test_bit(*hint, bitmap)) { 55 slot = *hint; 56 } else { 57 error_setg(errp, "slot %d is busy", *hint); 58 } 59 goto out; 60 } 61 62 /* search for free slot */ 63 slot = find_first_zero_bit(bitmap, max_slots); 64 if (slot == max_slots) { 65 error_setg(errp, "no free slots available"); 66 } 67 out: 68 g_free(bitmap); 69 return slot; 70 } 71 72 static gint pc_dimm_addr_sort(gconstpointer a, gconstpointer b) 73 { 74 PCDIMMDevice *x = PC_DIMM(a); 75 PCDIMMDevice *y = PC_DIMM(b); 76 Int128 diff = int128_sub(int128_make64(x->addr), int128_make64(y->addr)); 77 78 if (int128_lt(diff, int128_zero())) { 79 return -1; 80 } else if (int128_gt(diff, int128_zero())) { 81 return 1; 82 } 83 return 0; 84 } 85 86 static int pc_dimm_built_list(Object *obj, void *opaque) 87 { 88 GSList **list = opaque; 89 90 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 91 DeviceState *dev = DEVICE(obj); 92 if (dev->realized) { /* only realized DIMMs matter */ 93 *list = g_slist_insert_sorted(*list, dev, pc_dimm_addr_sort); 94 } 95 } 96 97 object_child_foreach(obj, pc_dimm_built_list, opaque); 98 return 0; 99 } 100 101 uint64_t pc_dimm_get_free_addr(uint64_t address_space_start, 102 uint64_t address_space_size, 103 uint64_t *hint, uint64_t size, 104 Error **errp) 105 { 106 GSList *list = NULL, *item; 107 uint64_t new_addr, ret = 0; 108 uint64_t address_space_end = address_space_start + address_space_size; 109 110 assert(address_space_end > address_space_size); 111 object_child_foreach(qdev_get_machine(), pc_dimm_built_list, &list); 112 113 if (hint) { 114 new_addr = *hint; 115 } else { 116 new_addr = address_space_start; 117 } 118 119 /* find address range that will fit new DIMM */ 120 for (item = list; item; item = g_slist_next(item)) { 121 PCDIMMDevice *dimm = item->data; 122 uint64_t dimm_size = object_property_get_int(OBJECT(dimm), 123 PC_DIMM_SIZE_PROP, 124 errp); 125 if (errp && *errp) { 126 goto out; 127 } 128 129 if (ranges_overlap(dimm->addr, dimm_size, new_addr, size)) { 130 if (hint) { 131 DeviceState *d = DEVICE(dimm); 132 error_setg(errp, "address range conflicts with '%s'", d->id); 133 goto out; 134 } 135 new_addr = dimm->addr + dimm_size; 136 } 137 } 138 ret = new_addr; 139 140 if (new_addr < address_space_start) { 141 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 142 "] at 0x%" PRIx64, new_addr, size, address_space_start); 143 } else if ((new_addr + size) > address_space_end) { 144 error_setg(errp, "can't add memory [0x%" PRIx64 ":0x%" PRIx64 145 "] beyond 0x%" PRIx64, new_addr, size, address_space_end); 146 } 147 148 out: 149 g_slist_free(list); 150 return ret; 151 } 152 153 static Property pc_dimm_properties[] = { 154 DEFINE_PROP_UINT64(PC_DIMM_ADDR_PROP, PCDIMMDevice, addr, 0), 155 DEFINE_PROP_UINT32(PC_DIMM_NODE_PROP, PCDIMMDevice, node, 0), 156 DEFINE_PROP_INT32(PC_DIMM_SLOT_PROP, PCDIMMDevice, slot, 157 PC_DIMM_UNASSIGNED_SLOT), 158 DEFINE_PROP_END_OF_LIST(), 159 }; 160 161 static void pc_dimm_get_size(Object *obj, Visitor *v, void *opaque, 162 const char *name, Error **errp) 163 { 164 int64_t value; 165 MemoryRegion *mr; 166 PCDIMMDevice *dimm = PC_DIMM(obj); 167 168 mr = host_memory_backend_get_memory(dimm->hostmem, errp); 169 value = memory_region_size(mr); 170 171 visit_type_int(v, &value, name, errp); 172 } 173 174 static void pc_dimm_check_memdev_is_busy(Object *obj, const char *name, 175 Object *val, Error **errp) 176 { 177 MemoryRegion *mr; 178 179 mr = host_memory_backend_get_memory(MEMORY_BACKEND(val), errp); 180 if (memory_region_is_mapped(mr)) { 181 char *path = object_get_canonical_path_component(val); 182 error_setg(errp, "can't use already busy memdev: %s", path); 183 g_free(path); 184 } else { 185 qdev_prop_allow_set_link_before_realize(obj, name, val, errp); 186 } 187 } 188 189 static void pc_dimm_init(Object *obj) 190 { 191 PCDIMMDevice *dimm = PC_DIMM(obj); 192 193 object_property_add(obj, PC_DIMM_SIZE_PROP, "int", pc_dimm_get_size, 194 NULL, NULL, NULL, &error_abort); 195 object_property_add_link(obj, PC_DIMM_MEMDEV_PROP, TYPE_MEMORY_BACKEND, 196 (Object **)&dimm->hostmem, 197 pc_dimm_check_memdev_is_busy, 198 OBJ_PROP_LINK_UNREF_ON_RELEASE, 199 &error_abort); 200 } 201 202 static void pc_dimm_realize(DeviceState *dev, Error **errp) 203 { 204 PCDIMMDevice *dimm = PC_DIMM(dev); 205 206 if (!dimm->hostmem) { 207 error_setg(errp, "'" PC_DIMM_MEMDEV_PROP "' property is not set"); 208 return; 209 } 210 } 211 212 static MemoryRegion *pc_dimm_get_memory_region(PCDIMMDevice *dimm) 213 { 214 return host_memory_backend_get_memory(dimm->hostmem, &error_abort); 215 } 216 217 static void pc_dimm_class_init(ObjectClass *oc, void *data) 218 { 219 DeviceClass *dc = DEVICE_CLASS(oc); 220 PCDIMMDeviceClass *ddc = PC_DIMM_CLASS(oc); 221 222 dc->realize = pc_dimm_realize; 223 dc->props = pc_dimm_properties; 224 225 ddc->get_memory_region = pc_dimm_get_memory_region; 226 } 227 228 static TypeInfo pc_dimm_info = { 229 .name = TYPE_PC_DIMM, 230 .parent = TYPE_DEVICE, 231 .instance_size = sizeof(PCDIMMDevice), 232 .instance_init = pc_dimm_init, 233 .class_init = pc_dimm_class_init, 234 .class_size = sizeof(PCDIMMDeviceClass), 235 }; 236 237 static void pc_dimm_register_types(void) 238 { 239 type_register_static(&pc_dimm_info); 240 } 241 242 type_init(pc_dimm_register_types) 243