From patchwork Tue Jul 23 16:23:07 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Mammedov X-Patchwork-Id: 261145 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 243462C00CF for ; Wed, 24 Jul 2013 02:27:57 +1000 (EST) Received: from localhost ([::1]:46635 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V1fRK-0003WL-Lp for incoming@patchwork.ozlabs.org; Tue, 23 Jul 2013 12:27:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41944) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V1fNs-0006XP-KV for qemu-devel@nongnu.org; Tue, 23 Jul 2013 12:24:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V1fNq-00076l-5a for qemu-devel@nongnu.org; Tue, 23 Jul 2013 12:24:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:21478) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V1fNp-00076d-M6 for qemu-devel@nongnu.org; Tue, 23 Jul 2013 12:24:17 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r6NGOEP5013913 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 23 Jul 2013 12:24:14 -0400 Received: from dell-pet610-01.lab.eng.brq.redhat.com (dell-pet610-01.lab.eng.brq.redhat.com [10.34.42.20]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r6NGNV1Z018088; Tue, 23 Jul 2013 12:24:11 -0400 From: Igor Mammedov To: qemu-devel@nongnu.org Date: Tue, 23 Jul 2013 18:23:07 +0200 Message-Id: <1374596592-7027-12-git-send-email-imammedo@redhat.com> In-Reply-To: <1374596592-7027-1-git-send-email-imammedo@redhat.com> References: <1374596592-7027-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: vasilis.liaskovitis@profitbricks.com, hutao@cn.fujitsu.com, pbonzini@redhat.com Subject: [Qemu-devel] [PATCH 11/16] dimm: add busy address check and address auto-allocation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org - if 'start' property is not specified on -device/device_add command, treat default value as request for assigning DimmDevice to the first free memory region. - if 'start' is provided with -device/device_add command, attempt to use it or fail command if it's already occupied or falls inside of an existing DimmDevice memory region. Signed-off-by: Igor Mammedov --- hw/mem-hotplug/dimm.c | 71 +++++++++++++++++++++++++++++++++++++++++ include/hw/mem-hotplug/dimm.h | 6 +++ 2 files changed, 77 insertions(+), 0 deletions(-) diff --git a/hw/mem-hotplug/dimm.c b/hw/mem-hotplug/dimm.c index c716906..7e3e383 100644 --- a/hw/mem-hotplug/dimm.c +++ b/hw/mem-hotplug/dimm.c @@ -21,6 +21,7 @@ #include "hw/mem-hotplug/dimm.h" #include "qemu/config-file.h" #include "qemu/bitmap.h" +#include "qemu/range.h" static void dimm_bus_initfn(Object *obj) { @@ -67,6 +68,64 @@ out: return slot; } +static gint dimm_bus_addr_sort(gconstpointer a, gconstpointer b) +{ + DimmDevice *x = DIMM(a); + DimmDevice *y = DIMM(b); + + return x->start - y->start; +} + +static int dimm_bus_built_dimm_list(DeviceState *dev, void *opaque) +{ + GSList **list = opaque; + + if (dev->realized) { /* only realized DIMMs matter */ + *list = g_slist_insert_sorted(*list, dev, dimm_bus_addr_sort); + } + return 0; +} + +static hwaddr dimm_bus_get_free_addr(DimmBus *bus, const hwaddr *hint, + uint64_t size, Error **errp) +{ + GSList *list = NULL, *item; + hwaddr new_start, ret; + uint64_t as_size; + + qbus_walk_children(BUS(bus), dimm_bus_built_dimm_list, NULL, &list); + + if (hint) { + new_start = *hint; + } else { + new_start = bus->base; + } + + /* find address range that will fit new DIMM */ + for (item = list; item; item = g_slist_next(item)) { + DimmDevice *dimm = item->data; + if (ranges_overlap(dimm->start, dimm->size, new_start, size)) { + if (hint) { + DeviceState *d = DEVICE(dimm); + error_setg(errp, "address range conflicts with '%s'", d->id); + break; + } + new_start = dimm->start + dimm->size; + } + } + ret = new_start; + + g_slist_free(list); + + as_size = memory_region_size(&bus->as); + if ((new_start + size) > (bus->base + as_size)) { + error_setg(errp, "can't add memory beyond 0x%" PRIx64, + bus->base + as_size); + } + + return ret; +} + static void dimm_bus_register_memory(DimmBus *bus, DimmDevice *dimm, Error **errp) { @@ -85,6 +144,7 @@ static void dimm_bus_class_init(ObjectClass *klass, void *data) } dc->register_memory = dimm_bus_register_memory; dc->get_free_slot = dimm_bus_get_free_slot; + dc->get_free_addr = dimm_bus_get_free_addr; } static const TypeInfo dimm_bus_info = { @@ -111,6 +171,7 @@ static void dimm_realize(DeviceState *dev, Error **errp) BusClass *bc = BUS_GET_CLASS(bus); DimmBusClass *dc = DIMM_BUS_GET_CLASS(bus); int *slot_hint; + hwaddr *start_hint; if (!dev->id) { error_setg(errp, "missing 'id' property"); @@ -128,6 +189,16 @@ static void dimm_realize(DeviceState *dev, Error **errp) return; } + start_hint = !dimm->start ? NULL : &dimm->start; + if (start_hint && (dimm->start < bus->base)) { + error_setg(errp, "can't map DIMM below: 0x%" PRIx64, bus->base); + return; + } + g_assert(dc->get_free_addr); + dimm->start = dc->get_free_addr(bus, start_hint, dimm->size, errp); + if (error_is_set(errp)) { + return; + } memory_region_init_ram(&dimm->mr, dev->id, dimm->size); diff --git a/include/hw/mem-hotplug/dimm.h b/include/hw/mem-hotplug/dimm.h index d8d11a3..55b1571 100644 --- a/include/hw/mem-hotplug/dimm.h +++ b/include/hw/mem-hotplug/dimm.h @@ -32,6 +32,7 @@ /** * DimmBus: * @start: starting physical address, where @DimmDevice is mapped. + * Default value: 0, means that address is auto-allocated. * @size: amount of memory mapped at @start. * @node: numa node to which @DimmDevice is attached. * @slot: slot number into which @DimmDevice is plugged in. @@ -73,6 +74,9 @@ typedef struct DimmBus { * @get_free_slot: returns a not occupied slot number. If @hint is provided, * it tries to return slot specified by @hint if it's not busy or returns * error in @errp. + * @get_free_addr: returns address where @DimmDevice of specified size + * might be mapped. If @hint is specified it returns hinted address if + * region is available or error in @errp. * @register_memory: map @DimmDevice into hot-plugable address space */ typedef struct DimmBusClass { @@ -80,6 +84,8 @@ typedef struct DimmBusClass { int (*get_free_slot)(DimmBus *bus, const int *hint, Error **errp); void (*register_memory)(DimmBus *bus, DimmDevice *dimm, Error **errp); + hwaddr (*get_free_addr)(DimmBus *bus, const hwaddr *hint, + uint64_t size, Error **errp); } DimmBusClass; #endif