@@ -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
@@ -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);
new file mode 100644
@@ -0,0 +1,97 @@
+/*
+ * RAM API
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * 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;
+}
new file mode 100644
@@ -0,0 +1,44 @@
+#ifndef QEMU_MEMORY_H
+#define QEMU_MEMORY_H
+/*
+ * RAM API
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * 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 */