From 76d8d9c89c9bb02d66da2971f89e1619a3b024ca Mon Sep 17 00:00:00 2001
From: Blue Swirl <blauwirbel@gmail.com>
Date: Sat, 5 Dec 2009 17:36:17 +0000
Subject: [PATCH] PCI: make pci_mem_base private

Make pci_mem_base private to PCIBus. Add a function to set it.

Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
---
 hw/pci.c |   28 +++++++++++++++++++---------
 hw/pci.h |    4 ++--
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/hw/pci.c b/hw/pci.c
index f2b6cff..95f00f3 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -44,6 +44,7 @@ struct PCIBus {
     void *irq_opaque;
     PCIDevice *devices[256];
     PCIDevice *parent_dev;
+    target_phys_addr_t mem_base;
 
     QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
     QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
@@ -69,7 +70,6 @@ static struct BusInfo pci_bus_info = {
 static void pci_update_mappings(PCIDevice *d);
 static void pci_set_irq(void *opaque, int irq_num, int level);
 
-target_phys_addr_t pci_mem_base;
 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
 
@@ -198,6 +198,11 @@ void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
     bus->hotplug = hotplug;
 }
 
+void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
+{
+    bus->mem_base = base;
+}
+
 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
                          pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
                          void *irq_opaque, int devfn_min, int nirq)
@@ -537,9 +542,11 @@ PCIDevice *pci_register_device(PCIBus *bus, const char *name,
                                      PCI_HEADER_TYPE_NORMAL);
     return pci_dev;
 }
-static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
+
+static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
+                                          target_phys_addr_t addr)
 {
-    return addr + pci_mem_base;
+    return addr + bus->mem_base;
 }
 
 static void pci_unregister_io_regions(PCIDevice *pci_dev)
@@ -554,9 +561,10 @@ static void pci_unregister_io_regions(PCIDevice *pci_dev)
         if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
             isa_unassign_ioport(r->addr, r->filtered_size);
         } else {
-            cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
-                                                     r->filtered_size,
-                                                     IO_MEM_UNASSIGNED);
+            cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
+                                                         r->addr),
+                                         r->filtered_size,
+                                         IO_MEM_UNASSIGNED);
         }
     }
 }
@@ -794,7 +802,7 @@ static void pci_update_mappings(PCIDevice *d)
 {
     PCIIORegion *r;
     int i;
-    pcibus_t new_addr, filtered_size;
+    pcibus_t new_addr, filtered_size, bus_addr;
 
     for(i = 0; i < PCI_NUM_REGIONS; i++) {
         r = &d->io_regions[i];
@@ -816,6 +824,7 @@ static void pci_update_mappings(PCIDevice *d)
             continue;
 
         /* now do the real mapping */
+        bus_addr = r->addr;
         if (r->addr != PCI_BAR_UNMAPPED) {
             if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
                 int class;
@@ -828,7 +837,8 @@ static void pci_update_mappings(PCIDevice *d)
                     isa_unassign_ioport(r->addr, r->filtered_size);
                 }
             } else {
-                cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
+                bus_addr = pci_to_cpu_addr(d->bus, r->addr);
+                cpu_register_physical_memory(bus_addr,
                                              r->filtered_size,
                                              IO_MEM_UNASSIGNED);
                 qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
@@ -844,7 +854,7 @@ static void pci_update_mappings(PCIDevice *d)
              * Teach them such cases, such that filtered_size < size and
              * addr & (size - 1) != 0.
              */
-            r->map_func(d, i, r->addr, r->filtered_size, r->type);
+            r->map_func(d, i, bus_addr, r->filtered_size, r->type);
         }
     }
 }
diff --git a/hw/pci.h b/hw/pci.h
index 3e8abad..b226ec1 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -10,8 +10,6 @@
 
 /* PCI bus */
 
-extern target_phys_addr_t pci_mem_base;
-
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
 #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
 #define PCI_FUNC(devfn)         ((devfn) & 0x07)
@@ -281,6 +279,8 @@ PCIBus *pci_register_bus(DeviceState *parent, const char *name,
                          pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
                          void *irq_opaque, int devfn_min, int nirq);
 
+void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base);
+
 PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
                         const char *default_devaddr);
 PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
-- 
1.5.6.5

