From patchwork Mon Dec 13 20:47:46 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 75405 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 54622B7082 for ; Tue, 14 Dec 2010 07:52:01 +1100 (EST) Received: from localhost ([127.0.0.1]:49409 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PSFLq-0002HL-3v for incoming@patchwork.ozlabs.org; Mon, 13 Dec 2010 15:50:30 -0500 Received: from [140.186.70.92] (port=48550 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PSFJK-0001DS-IE for qemu-devel@nongnu.org; Mon, 13 Dec 2010 15:48:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PSFJG-0006vt-Kb for qemu-devel@nongnu.org; Mon, 13 Dec 2010 15:47:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:1025) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PSFJG-0006vf-Cb for qemu-devel@nongnu.org; Mon, 13 Dec 2010 15:47:50 -0500 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.13.8/8.13.8) with ESMTP id oBDKllP2002326 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 13 Dec 2010 15:47:47 -0500 Received: from s20.home (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id oBDKlkPD011423; Mon, 13 Dec 2010 15:47:46 -0500 From: Alex Williamson To: qemu-devel@nongnu.org, anthony@codemonkey.ws Date: Mon, 13 Dec 2010 13:47:46 -0700 Message-ID: <20101213204741.19493.5495.stgit@s20.home> In-Reply-To: <20101213204540.19493.92979.stgit@s20.home> References: <20101213204540.19493.92979.stgit@s20.home> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: alex.williamson@redhat.com, kvm@vger.kernel.org Subject: [Qemu-devel] [RESEND PATCH v3 1/2] Minimal RAM API support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This adds a minimum chunk of Anthony's RAM API support so that we can identify actual VM RAM versus all the other things that make use of qemu_ram_alloc. Signed-off-by: Alex Williamson --- Makefile.objs | 1 + cpu-common.h | 2 + memory.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ memory.h | 44 ++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 0 deletions(-) create mode 100644 memory.c create mode 100644 memory.h diff --git a/Makefile.objs b/Makefile.objs index cebb945..47f3c3a 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -172,6 +172,7 @@ hw-obj-y += pci.o pci_bridge.o msix.o msi.o hw-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o hw-obj-$(CONFIG_PCI) += ioh3420.o xio3130_upstream.o xio3130_downstream.o hw-obj-y += watchdog.o +hw-obj-y += memory.o hw-obj-$(CONFIG_ISA_MMIO) += isa_mmio.o hw-obj-$(CONFIG_ECC) += ecc.o hw-obj-$(CONFIG_NAND) += nand.o diff --git a/cpu-common.h b/cpu-common.h index 6d4a898..f08f93b 100644 --- a/cpu-common.h +++ b/cpu-common.h @@ -29,6 +29,8 @@ enum device_endian { /* address in the RAM (different from a physical address) */ typedef unsigned long ram_addr_t; +#include "memory.h" + /* memory API */ typedef void CPUWriteMemoryFunc(void *opaque, target_phys_addr_t addr, uint32_t value); diff --git a/memory.c b/memory.c new file mode 100644 index 0000000..742776f --- /dev/null +++ b/memory.c @@ -0,0 +1,97 @@ +/* + * RAM API + * + * Copyright Red Hat, Inc. 2010 + * + * Authors: + * Alex Williamson + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ +#include "memory.h" +#include "range.h" + +typedef struct ram_slot { + target_phys_addr_t start_addr; + ram_addr_t size; + ram_addr_t offset; + QLIST_ENTRY(ram_slot) next; +} ram_slot; + +static QLIST_HEAD(ram_slots, ram_slot) ram_slots = + QLIST_HEAD_INITIALIZER(ram_slots); + +static ram_slot *qemu_ram_find_slot(target_phys_addr_t start_addr, + ram_addr_t size) +{ + ram_slot *slot; + + QLIST_FOREACH(slot, &ram_slots, next) { + if (slot->start_addr == start_addr && slot->size == size) { + return slot; + } + + if (ranges_overlap(start_addr, size, slot->start_addr, slot->size)) { + hw_error("Ram range overlaps existing slot\n"); + } + } + + return NULL; +} + +int qemu_ram_register(target_phys_addr_t start_addr, ram_addr_t size, + ram_addr_t phys_offset) +{ + ram_slot *slot; + + if (!size) { + return -EINVAL; + } + + assert(!qemu_ram_find_slot(start_addr, size)); + + slot = qemu_mallocz(sizeof(ram_slot)); + + slot->start_addr = start_addr; + slot->size = size; + slot->offset = phys_offset; + + QLIST_INSERT_HEAD(&ram_slots, slot, next); + + cpu_register_physical_memory(slot->start_addr, slot->size, slot->offset); + + return 0; +} + +void qemu_ram_unregister(target_phys_addr_t start_addr, ram_addr_t size) +{ + ram_slot *slot; + + if (!size) { + return; + } + + slot = qemu_ram_find_slot(start_addr, size); + assert(slot != NULL); + + QLIST_REMOVE(slot, next); + qemu_free(slot); + cpu_register_physical_memory(start_addr, size, IO_MEM_UNASSIGNED); + + return; +} + +int qemu_ram_for_each_slot(void *opaque, qemu_ram_for_each_slot_fn fn) +{ + ram_slot *slot; + + QLIST_FOREACH(slot, &ram_slots, next) { + int ret = fn(opaque, slot->start_addr, slot->size, slot->offset); + if (ret) { + return ret; + } + } + return 0; +} diff --git a/memory.h b/memory.h new file mode 100644 index 0000000..e7aa5cb --- /dev/null +++ b/memory.h @@ -0,0 +1,44 @@ +#ifndef QEMU_MEMORY_H +#define QEMU_MEMORY_H +/* + * RAM API + * + * Copyright Red Hat, Inc. 2010 + * + * Authors: + * Alex Williamson + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ + +#include "qemu-common.h" +#include "cpu-common.h" + +typedef int (*qemu_ram_for_each_slot_fn)(void *opaque, + target_phys_addr_t start_addr, + ram_addr_t size, + ram_addr_t phys_offset); + +/** + * qemu_ram_register() : Register a region of guest physical memory + * + * The new region must not overlap an existing region. + */ +int qemu_ram_register(target_phys_addr_t start_addr, ram_addr_t size, + ram_addr_t phys_offset); + +/** + * qemu_ram_unregister() : Unregister a region of guest physical memory + */ +void qemu_ram_unregister(target_phys_addr_t start_addr, ram_addr_t size); + +/** + * qemu_ram_for_each_slot() : Call fn() on each registered region + * + * Stop on non-zero return from fn(). + */ +int qemu_ram_for_each_slot(void *opaque, qemu_ram_for_each_slot_fn fn); + +#endif /* QEMU_MEMORY_H */