1 /* 2 * Memory Device Interface 3 * 4 * Copyright ProfitBricks GmbH 2012 5 * Copyright (C) 2014 Red Hat Inc 6 * Copyright (c) 2018 Red Hat Inc 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or later. 9 * See the COPYING file in the top-level directory. 10 */ 11 12 #include "qemu/osdep.h" 13 #include "hw/mem/memory-device.h" 14 #include "hw/qdev.h" 15 #include "qapi/error.h" 16 #include "hw/boards.h" 17 #include "qemu/range.h" 18 #include "hw/virtio/vhost.h" 19 #include "sysemu/kvm.h" 20 #include "trace.h" 21 22 static gint memory_device_addr_sort(gconstpointer a, gconstpointer b) 23 { 24 const MemoryDeviceState *md_a = MEMORY_DEVICE(a); 25 const MemoryDeviceState *md_b = MEMORY_DEVICE(b); 26 const MemoryDeviceClass *mdc_a = MEMORY_DEVICE_GET_CLASS(a); 27 const MemoryDeviceClass *mdc_b = MEMORY_DEVICE_GET_CLASS(b); 28 const uint64_t addr_a = mdc_a->get_addr(md_a); 29 const uint64_t addr_b = mdc_b->get_addr(md_b); 30 31 if (addr_a > addr_b) { 32 return 1; 33 } else if (addr_a < addr_b) { 34 return -1; 35 } 36 return 0; 37 } 38 39 static int memory_device_build_list(Object *obj, void *opaque) 40 { 41 GSList **list = opaque; 42 43 if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { 44 DeviceState *dev = DEVICE(obj); 45 if (dev->realized) { /* only realized memory devices matter */ 46 *list = g_slist_insert_sorted(*list, dev, memory_device_addr_sort); 47 } 48 } 49 50 object_child_foreach(obj, memory_device_build_list, opaque); 51 return 0; 52 } 53 54 static int memory_device_used_region_size(Object *obj, void *opaque) 55 { 56 uint64_t *size = opaque; 57 58 if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { 59 const DeviceState *dev = DEVICE(obj); 60 const MemoryDeviceState *md = MEMORY_DEVICE(obj); 61 62 if (dev->realized) { 63 *size += memory_device_get_region_size(md, &error_abort); 64 } 65 } 66 67 object_child_foreach(obj, memory_device_used_region_size, opaque); 68 return 0; 69 } 70 71 static void memory_device_check_addable(MachineState *ms, uint64_t size, 72 Error **errp) 73 { 74 uint64_t used_region_size = 0; 75 76 /* we will need a new memory slot for kvm and vhost */ 77 if (kvm_enabled() && !kvm_has_free_slot(ms)) { 78 error_setg(errp, "hypervisor has no free memory slots left"); 79 return; 80 } 81 if (!vhost_has_free_slot()) { 82 error_setg(errp, "a used vhost backend has no free memory slots left"); 83 return; 84 } 85 86 /* will we exceed the total amount of memory specified */ 87 memory_device_used_region_size(OBJECT(ms), &used_region_size); 88 if (used_region_size + size > ms->maxram_size - ms->ram_size) { 89 error_setg(errp, "not enough space, currently 0x%" PRIx64 90 " in use of total space for memory devices 0x" RAM_ADDR_FMT, 91 used_region_size, ms->maxram_size - ms->ram_size); 92 return; 93 } 94 95 } 96 97 static uint64_t memory_device_get_free_addr(MachineState *ms, 98 const uint64_t *hint, 99 uint64_t align, uint64_t size, 100 Error **errp) 101 { 102 uint64_t address_space_start, address_space_end; 103 GSList *list = NULL, *item; 104 uint64_t new_addr = 0; 105 106 if (!ms->device_memory) { 107 error_setg(errp, "memory devices (e.g. for memory hotplug) are not " 108 "supported by the machine"); 109 return 0; 110 } 111 112 if (!memory_region_size(&ms->device_memory->mr)) { 113 error_setg(errp, "memory devices (e.g. for memory hotplug) are not " 114 "enabled, please specify the maxmem option"); 115 return 0; 116 } 117 address_space_start = ms->device_memory->base; 118 address_space_end = address_space_start + 119 memory_region_size(&ms->device_memory->mr); 120 g_assert(address_space_end >= address_space_start); 121 122 /* address_space_start indicates the maximum alignment we expect */ 123 if (QEMU_ALIGN_UP(address_space_start, align) != address_space_start) { 124 error_setg(errp, "the alignment (0x%" PRIx64 ") is not supported", 125 align); 126 return 0; 127 } 128 129 memory_device_check_addable(ms, size, errp); 130 if (*errp) { 131 return 0; 132 } 133 134 if (hint && QEMU_ALIGN_UP(*hint, align) != *hint) { 135 error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", 136 align); 137 return 0; 138 } 139 140 if (QEMU_ALIGN_UP(size, align) != size) { 141 error_setg(errp, "backend memory size must be multiple of 0x%" 142 PRIx64, align); 143 return 0; 144 } 145 146 if (hint) { 147 new_addr = *hint; 148 if (new_addr < address_space_start) { 149 error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" PRIx64 150 "] before 0x%" PRIx64, new_addr, size, 151 address_space_start); 152 return 0; 153 } else if ((new_addr + size) > address_space_end) { 154 error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" PRIx64 155 "] beyond 0x%" PRIx64, new_addr, size, 156 address_space_end); 157 return 0; 158 } 159 } else { 160 new_addr = address_space_start; 161 } 162 163 /* find address range that will fit new memory device */ 164 object_child_foreach(OBJECT(ms), memory_device_build_list, &list); 165 for (item = list; item; item = g_slist_next(item)) { 166 const MemoryDeviceState *md = item->data; 167 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(OBJECT(md)); 168 uint64_t md_size, md_addr; 169 170 md_addr = mdc->get_addr(md); 171 md_size = memory_device_get_region_size(md, &error_abort); 172 173 if (ranges_overlap(md_addr, md_size, new_addr, size)) { 174 if (hint) { 175 const DeviceState *d = DEVICE(md); 176 error_setg(errp, "address range conflicts with memory device" 177 " id='%s'", d->id ? d->id : "(unnamed)"); 178 goto out; 179 } 180 new_addr = QEMU_ALIGN_UP(md_addr + md_size, align); 181 } 182 } 183 184 if (new_addr + size > address_space_end) { 185 error_setg(errp, "could not find position in guest address space for " 186 "memory device - memory fragmented due to alignments"); 187 goto out; 188 } 189 out: 190 g_slist_free(list); 191 return new_addr; 192 } 193 194 MemoryDeviceInfoList *qmp_memory_device_list(void) 195 { 196 GSList *devices = NULL, *item; 197 MemoryDeviceInfoList *list = NULL, *prev = NULL; 198 199 object_child_foreach(qdev_get_machine(), memory_device_build_list, 200 &devices); 201 202 for (item = devices; item; item = g_slist_next(item)) { 203 const MemoryDeviceState *md = MEMORY_DEVICE(item->data); 204 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(item->data); 205 MemoryDeviceInfoList *elem = g_new0(MemoryDeviceInfoList, 1); 206 MemoryDeviceInfo *info = g_new0(MemoryDeviceInfo, 1); 207 208 mdc->fill_device_info(md, info); 209 210 elem->value = info; 211 elem->next = NULL; 212 if (prev) { 213 prev->next = elem; 214 } else { 215 list = elem; 216 } 217 prev = elem; 218 } 219 220 g_slist_free(devices); 221 222 return list; 223 } 224 225 static int memory_device_plugged_size(Object *obj, void *opaque) 226 { 227 uint64_t *size = opaque; 228 229 if (object_dynamic_cast(obj, TYPE_MEMORY_DEVICE)) { 230 const DeviceState *dev = DEVICE(obj); 231 const MemoryDeviceState *md = MEMORY_DEVICE(obj); 232 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(obj); 233 234 if (dev->realized) { 235 *size += mdc->get_plugged_size(md, &error_abort); 236 } 237 } 238 239 object_child_foreach(obj, memory_device_plugged_size, opaque); 240 return 0; 241 } 242 243 uint64_t get_plugged_memory_size(void) 244 { 245 uint64_t size = 0; 246 247 memory_device_plugged_size(qdev_get_machine(), &size); 248 249 return size; 250 } 251 252 void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms, 253 const uint64_t *legacy_align, Error **errp) 254 { 255 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md); 256 Error *local_err = NULL; 257 uint64_t addr, align; 258 MemoryRegion *mr; 259 260 mr = mdc->get_memory_region(md, &local_err); 261 if (local_err) { 262 goto out; 263 } 264 265 align = legacy_align ? *legacy_align : memory_region_get_alignment(mr); 266 addr = mdc->get_addr(md); 267 addr = memory_device_get_free_addr(ms, !addr ? NULL : &addr, align, 268 memory_region_size(mr), &local_err); 269 if (local_err) { 270 goto out; 271 } 272 mdc->set_addr(md, addr, &local_err); 273 if (!local_err) { 274 trace_memory_device_pre_plug(DEVICE(md)->id ? DEVICE(md)->id : "", 275 addr); 276 } 277 out: 278 error_propagate(errp, local_err); 279 } 280 281 void memory_device_plug(MemoryDeviceState *md, MachineState *ms) 282 { 283 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md); 284 const uint64_t addr = mdc->get_addr(md); 285 MemoryRegion *mr; 286 287 /* 288 * We expect that a previous call to memory_device_pre_plug() succeeded, so 289 * it can't fail at this point. 290 */ 291 mr = mdc->get_memory_region(md, &error_abort); 292 g_assert(ms->device_memory); 293 294 memory_region_add_subregion(&ms->device_memory->mr, 295 addr - ms->device_memory->base, mr); 296 trace_memory_device_plug(DEVICE(md)->id ? DEVICE(md)->id : "", addr); 297 } 298 299 void memory_device_unplug(MemoryDeviceState *md, MachineState *ms) 300 { 301 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md); 302 MemoryRegion *mr; 303 304 /* 305 * We expect that a previous call to memory_device_pre_plug() succeeded, so 306 * it can't fail at this point. 307 */ 308 mr = mdc->get_memory_region(md, &error_abort); 309 g_assert(ms->device_memory); 310 311 memory_region_del_subregion(&ms->device_memory->mr, mr); 312 trace_memory_device_unplug(DEVICE(md)->id ? DEVICE(md)->id : "", 313 mdc->get_addr(md)); 314 } 315 316 uint64_t memory_device_get_region_size(const MemoryDeviceState *md, 317 Error **errp) 318 { 319 const MemoryDeviceClass *mdc = MEMORY_DEVICE_GET_CLASS(md); 320 MemoryRegion *mr; 321 322 /* dropping const here is fine as we don't touch the memory region */ 323 mr = mdc->get_memory_region((MemoryDeviceState *)md, errp); 324 if (!mr) { 325 return 0; 326 } 327 328 return memory_region_size(mr); 329 } 330 331 static const TypeInfo memory_device_info = { 332 .name = TYPE_MEMORY_DEVICE, 333 .parent = TYPE_INTERFACE, 334 .class_size = sizeof(MemoryDeviceClass), 335 }; 336 337 static void memory_device_register_types(void) 338 { 339 type_register_static(&memory_device_info); 340 } 341 342 type_init(memory_device_register_types) 343