From patchwork Mon Nov 1 15:14:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 294592 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id oA1FEJRd018491 for ; Mon, 1 Nov 2010 15:14:19 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757298Ab0KAPOQ (ORCPT ); Mon, 1 Nov 2010 11:14:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:1477 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756688Ab0KAPOP (ORCPT ); Mon, 1 Nov 2010 11:14:15 -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.13.8/8.13.8) with ESMTP id oA1FEAXx023243 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 1 Nov 2010 11:14:10 -0400 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 oA1FE9sH019572; Mon, 1 Nov 2010 11:14:09 -0400 From: Alex Williamson Subject: [PATCH v2 1/2] Minimal RAM API support To: qemu-devel@nongnu.org, anthony@codemonkey.ws, blauwirbel@gmail.com Cc: kvm@vger.kernel.org, alex.williamson@redhat.com, mst@redhat.com, chrisw@redhat.com, ddutile@redhat.com Date: Mon, 01 Nov 2010 09:14:09 -0600 Message-ID: <20101101151330.3927.32277.stgit@s20.home> In-Reply-To: <20101101150701.3927.88854.stgit@s20.home> References: <20101101150701.3927.88854.stgit@s20.home> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Mon, 01 Nov 2010 15:14:19 +0000 (UTC) diff --git a/Makefile.objs b/Makefile.objs index f07fb01..33fae0b 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -154,6 +154,7 @@ hw-obj-y += vl.o loader.o hw-obj-y += virtio.o virtio-console.o hw-obj-y += fw_cfg.o pci.o pci_host.o pcie_host.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 a543b5d..6aa2738 100644 --- a/cpu-common.h +++ b/cpu-common.h @@ -23,6 +23,8 @@ /* 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..2895082 --- /dev/null +++ b/memory.c @@ -0,0 +1,109 @@ +/* + * RAM API + * + * Copyright Red Hat, Inc. 2010 + * + * Authors: + * Alex Williamson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ +#include "memory.h" +#include "range.h" + +typedef struct QemuRamSlot { + target_phys_addr_t start_addr; + ram_addr_t size; + ram_addr_t offset; + QLIST_ENTRY(QemuRamSlot) next; +} QemuRamSlot; + +typedef struct QemuRamSlots { + QLIST_HEAD(slots, QemuRamSlot) slots; +} QemuRamSlots; + +static QemuRamSlots ram_slots = { .slots = QLIST_HEAD_INITIALIZER(ram_slots) }; + +static QemuRamSlot *qemu_ram_find_slot(target_phys_addr_t start_addr, + ram_addr_t size) +{ + QemuRamSlot *slot; + + QLIST_FOREACH(slot, &ram_slots.slots, next) { + if (slot->start_addr == start_addr && slot->size == size) { + return slot; + } + + if (ranges_overlap(start_addr, size, slot->start_addr, slot->size)) { + abort(); + } + } + + return NULL; +} + +int qemu_ram_register(target_phys_addr_t start_addr, ram_addr_t size, + ram_addr_t phys_offset) +{ + QemuRamSlot *slot; + + if (!size) { + return -EINVAL; + } + + assert(!qemu_ram_find_slot(start_addr, size)); + + slot = qemu_mallocz(sizeof(QemuRamSlot)); + + slot->start_addr = start_addr; + slot->size = size; + slot->offset = phys_offset; + + QLIST_INSERT_HEAD(&ram_slots.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) +{ + QemuRamSlot *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) +{ + QemuRamSlot *slot; + + QLIST_FOREACH(slot, &ram_slots.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..0c17ff9 --- /dev/null +++ b/memory.h @@ -0,0 +1,18 @@ +#ifndef QEMU_MEMORY_H +#define QEMU_MEMORY_H + +#include "qemu-common.h" +#include "cpu-common.h" + +int qemu_ram_register(target_phys_addr_t start_addr, ram_addr_t size, + ram_addr_t phys_offset); + +void qemu_ram_unregister(target_phys_addr_t start_addr, ram_addr_t size); + +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); + +int qemu_ram_for_each_slot(void *opaque, qemu_ram_for_each_slot_fn fn); +#endif