Patchwork [27/61] pci: clean up of pci_update_mappings()

login
register
mail settings
Submitter Isaku Yamahata
Date Sept. 30, 2009, 10:18 a.m.
Message ID <1254305917-14784-28-git-send-email-yamahata@valinux.co.jp>
Download mbox | patch
Permalink /patch/34528/
State Superseded
Headers show

Comments

Isaku Yamahata - Sept. 30, 2009, 10:18 a.m.
This patch cleans up pci_update_mappings() making it more readable.
- reduce indent level by one or two
- remove duplicated logic.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
---
 hw/pci.c |  142 ++++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 74 insertions(+), 68 deletions(-)

Patch

diff --git a/hw/pci.c b/hw/pci.c
index beda5ef..6a28fa5 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -440,91 +440,97 @@  void pci_register_bar(PCIDevice *pci_dev, int region_num,
 static void pci_update_mappings(PCIDevice *d)
 {
     PCIIORegion *r;
-    int cmd, i;
+    int i;
+    uint16_t cmd;
+    uint64_t bar;
+    uint64_t max_addr;
     uint64_t last_addr, new_addr;
     uint32_t config_ofs;
 
     cmd = le16_to_cpu(*(uint16_t *)(d->config + PCI_COMMAND));
     for(i = 0; i < PCI_NUM_REGIONS; i++) {
         r = &d->io_regions[i];
+        if (r->size == 0)
+            continue;
+
         if (i == PCI_ROM_SLOT) {
             config_ofs = 0x30;
         } else {
             config_ofs = 0x10 + i * 4;
         }
-        if (r->size != 0) {
+
+        if (pci_bar_is_64bit(r)) {
+            bar = le64_to_cpu(*(uint64_t *)(d->config + config_ofs));
+        } else {
+            bar = le32_to_cpu(*(uint32_t *)(d->config + config_ofs));
+        }
+
+        new_addr = PCI_BAR_UNMAPPED;
+        last_addr = 0;
+        max_addr = 0;
+        if (r->type & PCI_ADDRESS_SPACE_IO) {
+            if (cmd & PCI_COMMAND_IO) {
+                new_addr = bar;
+            }
+
+            /* NOTE: we have only 64K ioports on PC */
+            max_addr = 0x10000;
+        } else {
+            if (cmd & PCI_COMMAND_MEMORY) {
+                new_addr = bar;
+            }
+
+            /* the ROM slot has a specific enable bit */
+            if (i == PCI_ROM_SLOT && !(bar & 1)) {
+                new_addr = PCI_BAR_UNMAPPED;
+            }
+
+            max_addr = PCI_BAR_UNMAPPED;
+            if (!pci_bar_is_64bit(r)) {
+                /* keep old behaviour
+                   without this, PC ide doesn't work well. */
+                max_addr = UINT32_MAX;
+            }
+        }
+
+        new_addr = new_addr & ~(r->size - 1);
+        last_addr = new_addr + r->size - 1;
+
+        /* NOTE: we do not support wrapping */
+        /* XXX: as we cannot support really dynamic
+           mappings, we handle specific values as invalid
+           mappings. */
+        if (last_addr <= new_addr || new_addr == 0 || last_addr >= max_addr) {
+            new_addr = PCI_BAR_UNMAPPED;
+        }
+
+        if (new_addr == r->addr) {
+            continue;
+        }
+
+        /* now do the real mapping */
+        if (r->addr != PCI_BAR_UNMAPPED) {
             if (r->type & PCI_ADDRESS_SPACE_IO) {
-                if (cmd & PCI_COMMAND_IO) {
-                    new_addr = le32_to_cpu(*(uint32_t *)(d->config +
-                                                         config_ofs));
-                    new_addr = new_addr & ~(r->size - 1);
-                    last_addr = new_addr + r->size - 1;
-                    /* NOTE: we have only 64K ioports on PC */
-                    if (last_addr <= new_addr || new_addr == 0 ||
-                        last_addr >= 0x10000) {
-                        new_addr = PCI_BAR_UNMAPPED;
-                    }
+                int class;
+                /* NOTE: specific hack for IDE in PC case:
+                   only one byte must be mapped. */
+                class = d->config[0x0a] | (d->config[0x0b] << 8);
+                if (class == 0x0101 && r->size == 4) {
+                    isa_unassign_ioport(r->addr + 2, 1);
                 } else {
-                    new_addr = PCI_BAR_UNMAPPED;
+                    isa_unassign_ioport(r->addr, r->size);
                 }
             } else {
-                if (cmd & PCI_COMMAND_MEMORY) {
-
-                    if (pci_bar_is_64bit(r)) {
-                        new_addr = le64_to_cpu(*(uint64_t *)(d->config +
-                                                             config_ofs));
-                    } else {
-                        new_addr = le32_to_cpu(*(uint32_t *)(d->config +
-                                                             config_ofs));
-                    }
-                    /* the ROM slot has a specific enable bit */
-                    if (i == PCI_ROM_SLOT && !(new_addr & 1))
-                        goto no_mem_map;
-                    new_addr = new_addr & ~(r->size - 1);
-                    last_addr = new_addr + r->size - 1;
-                    /* NOTE: we do not support wrapping */
-                    /* XXX: as we cannot support really dynamic
-                       mappings, we handle specific values as invalid
-                       mappings. */
-                    if (last_addr <= new_addr || new_addr == 0 ||
-                        last_addr == PCI_BAR_UNMAPPED ||
-
-                        /* keep old behaviour
-                         * without this, PC ide doesn't work well. */
-                        (!pci_bar_is_64bit(r) && last_addr >= UINT32_MAX)) {
-                        new_addr = PCI_BAR_UNMAPPED;
-                    }
-                } else {
-                no_mem_map:
-                    new_addr = PCI_BAR_UNMAPPED;
-                }
-            }
-            /* now do the real mapping */
-            if (new_addr != r->addr) {
-                if (r->addr != PCI_BAR_UNMAPPED) {
-                    if (r->type & PCI_ADDRESS_SPACE_IO) {
-                        int class;
-                        /* NOTE: specific hack for IDE in PC case:
-                           only one byte must be mapped. */
-                        class = d->config[0x0a] | (d->config[0x0b] << 8);
-                        if (class == 0x0101 && r->size == 4) {
-                            isa_unassign_ioport(r->addr + 2, 1);
-                        } else {
-                            isa_unassign_ioport(r->addr, r->size);
-                        }
-                    } else {
-                        cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
-                                                     r->size,
-                                                     IO_MEM_UNASSIGNED);
-                        qemu_unregister_coalesced_mmio(r->addr, r->size);
-                    }
-                }
-                r->addr = new_addr;
-                if (r->addr != PCI_BAR_UNMAPPED) {
-                    r->map_func(d, i, r->addr, r->size, r->type);
-                }
+                cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
+                                             r->size,
+                                             IO_MEM_UNASSIGNED);
+                qemu_unregister_coalesced_mmio(r->addr, r->size);
             }
         }
+        r->addr = new_addr;
+        if (r->addr != PCI_BAR_UNMAPPED) {
+            r->map_func(d, i, r->addr, r->size, r->type);
+        }
     }
 }