Skip to contentSkip to conversation

Redesign of pciinit.c (take 3)

PATCH In review Alexey Korolev 4 participants 13 messages Mar 28, 2012 – Apr 4, 2012

lore
Share & tools
lore
#0 lore raw

cover Redesign of pciinit.c (take 3)

Hi,

This patch series redesigns the existing pciinit.c code and introduces linked list operations.
Changes are more about structures definitions rather than functionality. There are no more arrays of bases and counts in new implementation. The new implementation is based on dynamic allocation of pci_region_entry structures.
Each pci_region_entry structure could be a PCI bar or a downstream PCI region (bridge). Each entry has a set of attributes: type (IO, MEM, PREFMEM), is64bit, size, base address, PCI device owner, and a pointer to the pci_bus it belongs to.

1. Introduce List operations
2. Add pci_region_entry and linked lists operations in place while still using the array system to do the allocations.
3. Switch to lists operations
4. Get rid of size element from bus->r structure, now we use entry->size for the same purpose

 src/pci.h     |    5 -
 src/pciinit.c |  258 ++++++++++++++++++++++++++-------------------------------
 src/util.h    |   21 +++++
 3 files changed, 137 insertions(+), 147 deletions(-)
#1 lore raw

PATCH 1/4 Add basic linked list operations

This linked list implementation is partially based on kernel code. So it should be quite stable

---
 src/util.h |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
src/util.h +21 -0
index 70d3c4c..17df3cf 100644
@@ -195,6 +195,27 @@ struct descloc_s {
     u32 addr;
 } PACKED;
 
+// Double linked lists with a single pointer list head
+#define list_foreach_entry_safe(head, next, entry)    \
+        for (entry = head; entry && ({next=entry->next; 1;}); \
+            entry = next)
+
+#define list_del(entry) \
+       do { \
+           *(entry)->pprev = (entry)->next; \
+           if ((entry)->next) \
+               (entry)->next->pprev = (entry)->pprev; \
+       } while (0)
+
+#define list_add_head(phead, entry) \
+       do { \
+           (entry)->next = *(phead); \
+           if (*(phead)) \
+               (*(phead))->pprev = &(entry)->next; \
+           *(phead) = entry; \
+           (entry)->pprev = phead; \
+       } while (0)
+
 // util.c
 void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
 struct bregs;
Signature · 1 line
1.7.5.4
#2 lore raw

Re: [Qemu-devel] [SeaBIOS] [PATCH 1/4] Add basic linked list operations

Quoted 2 lines · Alexey Korolev

This linked list implementation is partially based on kernel code. So it should be quite stable

How about just copying the file?

I've used the linux kernel list implementation elsewhere too and it worked just fine with only minor tweaks (remove some likely()/unlikely() macros IIRC).

cheers,
Gerd

#3 lore raw

Re: [Qemu-devel] [SeaBIOS] [PATCH 1/4] Add basic linked list operations

Quoted 8 lines
Quoted 3 lines

This linked list implementation is partially based on kernel code. So it should be quite stable

How about just copying the file?

I've used the linux kernel list implementation elsewhere too and it worked just fine with only minor tweaks (remove some likely()/unlikely() macros IIRC).

Right, in "take-2" Kevin has suggested very similar.
It will be done as a separate patch, because the changes are not just about pciinit.c, they also simplify pmm.c and stack.c a bit. In this patch I'm submitting only few necessary functions for list operations.

#4 lore raw

Re: [Qemu-devel] [SeaBIOS] [PATCH 1/4] Add basic linked list operations

In reply to Gerd Hoffmann
Quoted 9 lines · Gerd Hoffmann
Quoted 2 lines · Alexey Korolev

This linked list implementation is partially based on kernel code. So it should be quite stable

How about just copying the file?

I've used the linux kernel list implementation elsewhere too and it worked just fine with only minor tweaks (remove some likely()/unlikely() macros IIRC).

Linux is GPLv2 while SeaBIOS is LGPLv3, so some care should be taken.

-Kevin

#5 lore raw

Re: [Qemu-devel] [SeaBIOS] [PATCH 1/4] Add basic linked list operations

Quoted 5 lines · Kevin O'Connor
Quoted 4 lines · Gerd Hoffmann
Quoted 2 lines · Alexey Korolev

This linked list implementation is partially based on kernel code. So it should be quite stable

Quoted 7 lines
Quoted 5 lines

How about just copying the file?

I've used the linux kernel list implementation elsewhere too and it worked just fine with only minor tweaks (remove some likely()/unlikely() macros IIRC).

Linux is GPLv2 while SeaBIOS is LGPLv3, so some care should be taken.

FWIW In Xen we decided to go with the BSD list macros in our LGPL library interfaces to avoid a similar sort issue.
http://xenbits.xen.org/hg/xen-unstable.hg/file/74d2af0b56ea/tools/include/xen-external
has that plus a handy sed script to namespace the interface, although I don't suppose that will matter inside seabios.

Ian.

#6 lore raw

PATCH 2/4 Added a pci_region_entry structure

In reply to Alexey Korolev

In this patch the pci_region_entry structure is introduced. The pci_device->bars are removed. The information from pci_region_entry is used to program pci bars.

---
 src/pci.h     |    5 --
 src/pciinit.c |  116 ++++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 86 insertions(+), 35 deletions(-)
src/pci.h +0 -5
index a2a5a4c..5598100 100644
@@ -51,11 +51,6 @@ struct pci_device {
     u8 prog_if, revision;
     u8 header_type;
     u8 secondary_bus;
-    struct {
-        u32 addr;
-        u32 size;
-        int is64;
-    } bars[PCI_NUM_REGIONS];
 
     // Local information on device.
     int have_driver;
src/pciinit.c +86 -30

175 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#7 lore raw

PATCH 3/4 Switch from array based resource allocation to list

In reply to Alexey Korolev

In this patch instead of array based resource allocation approach we calculate resource addresses linked lists of pci_region_entry structures.

---
 src/pciinit.c |  179 ++++++++++++++++-----------------------------------------
 1 files changed, 50 insertions(+), 129 deletions(-)
src/pciinit.c +50 -129
index 6a285c9..85fe823 100644
@@ -12,9 +12,8 @@
 #include "pci_regs.h" // PCI_COMMAND
 #include "xen.h" // usingXen
 
-#define PCI_IO_INDEX_SHIFT 2
-#define PCI_MEM_INDEX_SHIFT 12
-
+#define PCI_DEVICE_IO_MIN         0x4
+#define PCI_DEVICE_MEM_MIN     0x1000
 #define PCI_BRIDGE_IO_MIN      0x1000
 #define PCI_BRIDGE_MEM_MIN   0x100000
 
@@ -48,38 +47,14 @@ struct pci_region_entry {
 struct pci_bus {
     struct {
         /* pci region stats */
-        u32 count[32 - PCI_MEM_INDEX_SHIFT];
         u32 sum, max;
-        /* seconday bus region sizes */
         u32 size;
-        /* pci region assignments */
-        u32 bases[32 - PCI_MEM_INDEX_SHIFT];
         u32 base;
         struct pci_region_entry *list;
     } r[PCI_REGION_TYPE_COUNT];
     struct pci_device *bus_dev;
 };
 
-static int pci_size_to_index(u32 size, enum pci_region_type type)
-{
-    int index = __fls(size);
-    int shift = (type == PCI_REGION_TYPE_IO) ?
-        PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
-
-    if (index < shift)
-        index = shift;
-    index -= shift;
-    return index;
-}
-
-static u32 pci_index_to_size(int index, enum pci_region_type type)
-{
-    int shift = (type == PCI_REGION_TYPE_IO) ?
-        PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
-
-    return 0x1 << (index + shift);
-}
-
 static enum pci_region_type pci_addr_to_type(u32 addr)
 {
     if (addr & PCI_BASE_ADDRESS_SPACE_IO)
@@ -387,19 +362,11 @@ pci_region_create_entry(struct pci_bus *bus, struct pci_device *dev,
     entry->size = size;
     list_add_head(&bus->r[type].list, entry);
     entry->parent_bus = bus;
-    return entry;
-}
-
-static void pci_bios_bus_reserve(struct pci_bus *bus, int type, u32 size)
-{
-    u32 index;
 
-    index = pci_size_to_index(size, type);
-    size = pci_index_to_size(index, type);
-    bus->r[type].count[index]++;
     bus->r[type].sum += size;
     if (bus->r[type].max < size)
         bus->r[type].max = size;
+    return entry;
 }
 
 static int pci_bios_check_devices(struct pci_bus *busses)
@@ -423,14 +390,12 @@ static int pci_bios_check_devices(struct pci_bus *busses)
                 continue;
             int type = pci_addr_to_type(val);
             int min_size = (type == PCI_REGION_TYPE_IO) ?
-                     (1<<PCI_IO_INDEX_SHIFT) : (1<<PCI_MEM_INDEX_SHIFT);
+                             PCI_DEVICE_IO_MIN : PCI_DEVICE_MEM_MIN;
             size = (size > min_size) ? size : min_size;
             int is64bit = (!(val & PCI_BASE_ADDRESS_SPACE_IO) &&
                                  (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
                                  == PCI_BASE_ADDRESS_MEM_TYPE_64);
 
-            pci_bios_bus_reserve(bus, pci_addr_to_type(val), size);
-
             entry = pci_region_create_entry(bus, pci, size, type, is64bit);
             if (!entry)
                 return -1;
@@ -456,7 +421,6 @@ static int pci_bios_check_devices(struct pci_bus *busses)
             if (s->r[type].size < limit)
                 s->r[type].size = limit;
             s->r[type].size = pci_size_roundup(s->r[type].size);
-            pci_bios_bus_reserve(parent, type, s->r[type].size);
             entry = pci_region_create_entry(parent, s->bus_dev,
                                             s->r[type].size, type, 0);
             if (!entry)
@@ -496,112 +460,69 @@ static int pci_bios_init_root_regions(struct pci_bus *bus, u32 start, u32 end)
 
 
 /****************************************************************
- * BAR assignment
+ * Map pci region entries
  ****************************************************************/
 
-static void pci_bios_init_bus_bases(struct pci_bus *bus)
+static void pci_region_map_one_entry(struct pci_region_entry *entry)
 {
-    u32 base, newbase, size;
-    int type, i;
-
-    for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
-        dprintf(1, "  type %s max %x sum %x base %x\n", region_type_name[type],
-                bus->r[type].max, bus->r[type].sum, bus->r[type].base);
-        base = bus->r[type].base;
-        for (i = ARRAY_SIZE(bus->r[type].count)-1; i >= 0; i--) {
-            size = pci_index_to_size(i, type);
-            if (!bus->r[type].count[i])
-                continue;
-            newbase = base + size * bus->r[type].count[i];
-            dprintf(1, "    size %8x: %d bar(s), %8x -> %8x\n",
-                    size, bus->r[type].count[i], base, newbase - 1);
-            bus->r[type].bases[i] = base;
-            base = newbase;
-        }
+    if (!entry->this_bus ) {
+        dprintf(1, "PCI: bdf %d bar %d\tsize\t0x%08x\tbase 0x%x type %s\n",
+                      entry->dev->bdf, entry->bar, entry->size, entry->base,
+                      region_type_name[entry->type]);
+
+        pci_set_io_region_addr(entry->dev, entry->bar, entry->base);
+        if (entry->is64bit)
+            pci_set_io_region_addr(entry->dev, entry->bar + 1, 0);
+        return;
     }
-}
-
-static u32 pci_bios_bus_get_addr(struct pci_bus *bus, int type, u32 size)
-{
-    u32 index, addr;
-
-    index = pci_size_to_index(size, type);
-    addr = bus->r[type].bases[index];
-    bus->r[type].bases[index] += pci_index_to_size(index, type);
-    return addr;
-}
 
-#define PCI_IO_SHIFT            8
-#define PCI_MEMORY_SHIFT        16
-#define PCI_PREF_MEMORY_SHIFT   16
+    entry->this_bus->r[entry->type].base = entry->base;
+    dprintf(1, "PCI-2-PCI bus %d\t\tsize\t0x%08x\tbase 0x%x type %s\n",
+                  entry->dev->secondary_bus, entry->size, entry->base,
+                  region_type_name[entry->type]);
 
-static void pci_bios_map_devices(struct pci_bus *busses)
-{
-    // Setup bases for root bus.
-    dprintf(1, "PCI: init bases bus 0 (primary)\n");
-    pci_bios_init_bus_bases(&busses[0]);
-
-    // Map regions on each secondary bus.
-    int secondary_bus;
-    for (secondary_bus=1; secondary_bus<=MaxPCIBus; secondary_bus++) {
-        struct pci_bus *s = &busses[secondary_bus];
-        if (!s->bus_dev)
-            continue;
-        u16 bdf = s->bus_dev->bdf;
-        struct pci_bus *parent = &busses[pci_bdf_to_bus(bdf)];
-        int type;
-        for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
-            s->r[type].base = pci_bios_bus_get_addr(
-                parent, type, s->r[type].size);
-        }
-        dprintf(1, "PCI: init bases bus %d (secondary)\n", secondary_bus);
-        pci_bios_init_bus_bases(s);
-
-        u32 base = s->r[PCI_REGION_TYPE_IO].base;
-        u32 limit = base + s->r[PCI_REGION_TYPE_IO].size - 1;
-        pci_config_writeb(bdf, PCI_IO_BASE, base >> PCI_IO_SHIFT);
+    u16 bdf = entry->dev->bdf;
+    u32 base = entry->base;
+    u32 limit = entry->base + entry->size - 1;
+    if (entry->type == PCI_REGION_TYPE_IO) {
+        pci_config_writeb(bdf, PCI_IO_BASE, base >> 8);
         pci_config_writew(bdf, PCI_IO_BASE_UPPER16, 0);
-        pci_config_writeb(bdf, PCI_IO_LIMIT, limit >> PCI_IO_SHIFT);
+        pci_config_writeb(bdf, PCI_IO_LIMIT, limit >> 8);
         pci_config_writew(bdf, PCI_IO_LIMIT_UPPER16, 0);
-
-        base = s->r[PCI_REGION_TYPE_MEM].base;
-        limit = base + s->r[PCI_REGION_TYPE_MEM].size - 1;
-        pci_config_writew(bdf, PCI_MEMORY_BASE, base >> PCI_MEMORY_SHIFT);
-        pci_config_writew(bdf, PCI_MEMORY_LIMIT, limit >> PCI_MEMORY_SHIFT);
-
-        base = s->r[PCI_REGION_TYPE_PREFMEM].base;
-        limit = base + s->r[PCI_REGION_TYPE_PREFMEM].size - 1;
-        pci_config_writew(bdf, PCI_PREF_MEMORY_BASE, base >> PCI_PREF_MEMORY_SHIFT);
-        pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT, limit >> PCI_PREF_MEMORY_SHIFT);
+    }
+    if (entry->type == PCI_REGION_TYPE_MEM) {
+        pci_config_writew(bdf, PCI_MEMORY_BASE, base >> 16);
+        pci_config_writew(bdf, PCI_MEMORY_LIMIT, limit >> 16);
+    }
+    if (entry->type == PCI_REGION_TYPE_PREFMEM) {
+        pci_config_writew(bdf, PCI_PREF_MEMORY_BASE, base >> 16);
+        pci_config_writew(bdf, PCI_PREF_MEMORY_LIMIT, limit >> 16);
         pci_config_writel(bdf, PCI_PREF_BASE_UPPER32, 0);
         pci_config_writel(bdf, PCI_PREF_LIMIT_UPPER32, 0);
     }
+    return;
+}
 
-    // Map regions on each device.
-    int bus;
+static void pci_bios_map_devices(struct pci_bus *busses)
+{
     struct pci_region_entry *entry, *next;
+
+    int bus;
     for (bus = 0; bus<=MaxPCIBus; bus++) {
         int type;
         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
-            list_foreach_entry_safe(busses[bus].r[type].list,
-                                      next, entry) {
-                if (!entry->this_bus) {
-                    entry->base = pci_bios_bus_get_addr(&busses[bus],
-                                                      entry->type, entry->size);
-                    pci_set_io_region_addr(entry->dev, entry->bar, entry->base);
-                    if (entry->is64bit)
-                        pci_set_io_region_addr(entry->dev, entry->bar, 0);
-
-                    dprintf(1, "PCI: map device bdf=%02x:%02x.%x \tbar %d"
-                                "\tsize\t0x%08x\tbase 0x%x type %s\n",
-                               pci_bdf_to_bus(entry->dev->bdf),
-                               pci_bdf_to_dev(entry->dev->bdf),
-                               pci_bdf_to_fn(entry->dev->bdf),
-                               entry->bar, entry->size, entry->base,
-                               region_type_name[entry->type]);
+            u32 size;
+            for (size = busses[bus].r[type].max; size > 0; size >>= 1) {
+                    list_foreach_entry_safe(busses[bus].r[type].list,
+                                              next, entry) {
+                        if (size == entry->size) {
+                            entry->base = busses[bus].r[type].base;
+                            busses[bus].r[type].base += size;
+                            pci_region_map_one_entry(entry);
+                            list_del(entry);
+                            free(entry);
+                    }
                 }
-                list_del(entry);
-                free(entry);
             }
         }
     }
Signature · 1 line
1.7.5.4
#8 lore raw

Re: [Qemu-devel] [PATCH 3/4] Switch from array based resource allocation to list

Quoted 2 lines · Alexey Korolev

In this patch instead of array based resource allocation approach we calculate resource addresses linked lists of pci_region_entry structures.

Thanks. I still think this migration can be done more seamlessly. I played with your patches a bit and came up with the attached patches that do just code movement - no alogorithm changes. See the attached.

Also, I think we should look to commit after the next SeaBIOS release.

-Kevin

Quoted 1 line

From 2f6e81d884dfbb01a12ddfb10a64bf87e864a19c Mon Sep 17 00:00:00 2001

From: Alexey Korolev <[email protected]>
Date: Wed, 28 Mar 2012 17:41:41 +1300
Subject: [PATCH 1/3] Added a pci_region_entry structure
To: [email protected]

In this patch the pci_region_entry structure is introduced. The pci_device->bars are removed. The information from pci_region_entry is used to program pci bars.

---
 src/pci.h     |    5 --
 src/pciinit.c |  115 +++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 79 insertions(+), 41 deletions(-)
src/pci.h +0 -5
index a2a5a4c..5598100 100644
@@ -51,11 +51,6 @@ struct pci_device {
     u8 prog_if, revision;
     u8 header_type;
     u8 secondary_bus;
-    struct {
-        u32 addr;
-        u32 size;
-        int is64;
-    } bars[PCI_NUM_REGIONS];
 
     // Local information on device.
     int have_driver;
src/pciinit.c +79 -36

178 lines not on this page · raw message

Signature · 1 line
1.7.6.5
Quoted 1 line

From 6b6f20425b14f7f99aefb28a8553363573544409 Mon Sep 17 00:00:00 2001

From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 00:39:20 -0400
Subject: [PATCH 2/3] Perform pci-to-pci bus bar assignment at same time as
normal bar assignment.
To: [email protected]

---
 src/pciinit.c |   55 +++++++++++++++++++------------------------------------
 1 files changed, 19 insertions(+), 36 deletions(-)
src/pciinit.c +19 -36

82 lines not on this page · raw message

Signature · 1 line
1.7.6.5
Quoted 1 line

From 91b8ba37b87187643b96dca9eb135998eeead44e Mon Sep 17 00:00:00 2001

From: Alexey Korolev <[email protected]>
Date: Wed, 28 Mar 2012 17:56:44 +1300
Subject: [PATCH 3/3] Get rid of size element of pci_bus->r structure
To: [email protected]

The 'size' element of pci_bus->r structure is no longer need as the information about bridge region size is already stored in pci_region_entry structure.

---
 src/pciinit.c |   20 ++++++++------------
 1 files changed, 8 insertions(+), 12 deletions(-)
src/pciinit.c +8 -12
index 5c37e08..f2c839a 100644
@@ -49,8 +49,6 @@ struct pci_bus {
         /* pci region stats */
         u32 count[32 - PCI_MEM_INDEX_SHIFT];
         u32 sum, max;
-        /* seconday bus region sizes */
-        u32 size;
         /* pci region assignments */
         u32 bases[32 - PCI_MEM_INDEX_SHIFT];
         u32 base;
@@ -436,21 +434,19 @@ static int pci_bios_check_devices(struct pci_bus *busses)
         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
             u32 limit = (type == PCI_REGION_TYPE_IO) ?
                 PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN;
-            s->r[type].size = s->r[type].sum;
-            if (s->r[type].size < limit)
-                s->r[type].size = limit;
-            s->r[type].size = pci_size_roundup(s->r[type].size);
+            u32 size = s->r[type].sum;
+            if (size < limit)
+                size = limit;
+            size = pci_size_roundup(size);
             struct pci_region_entry *entry = pci_region_create_entry(
-                parent, s->bus_dev, s->r[type].size, type, 0);
+                parent, s->bus_dev, size, type, 0);
             if (!entry)
                 return -1;
             entry->child_bus = s;
+            dprintf(1, "PCI: secondary bus %d size %x type %s\n",
+                      entry->dev->secondary_bus, size,
+                      region_type_name[entry->type]);
         }
-        dprintf(1, "PCI: secondary bus %d sizes: io %x, mem %x, prefmem %x\n",
-                secondary_bus,
-                s->r[PCI_REGION_TYPE_IO].size,
-                s->r[PCI_REGION_TYPE_MEM].size,
-                s->r[PCI_REGION_TYPE_PREFMEM].size);
     }
     return 0;
 }
Signature · 1 line
1.7.6.5
#9 lore raw

Re: [Qemu-devel] [PATCH 3/4] Switch from array based resource allocation to list

Quoted 9 lines · Kevin O'Connor
Quoted 2 lines · Alexey Korolev

In this patch instead of array based resource allocation approach we calculate resource addresses linked lists of pci_region_entry structures.

Thanks. I still think this migration can be done more seamlessly. I played with your patches a bit and came up with the attached patches that do just code movement - no alogorithm changes. See the attached.

Also, I think we should look to commit after the next SeaBIOS release.

Looking closer at your new allocation algorithm, I see that it effectively allocates largest sizes first. It occurred to me that an easy way to do this is to keep the pci_region_entry list sorted by size. See the patch below as an example (based on top of the previous email I sent).

-Kevin

src/pciinit.c +15 -44
index f2c839a..bfee178 100644
@@ -41,16 +41,13 @@ struct pci_region_entry {
     struct pci_bus *child_bus;
     struct pci_bus *parent_bus;
     struct pci_region_entry *next;
-    struct pci_region_entry **pprev;
 };
 
 struct pci_bus {
     struct {
         /* pci region stats */
-        u32 count[32 - PCI_MEM_INDEX_SHIFT];
         u32 sum, max;
         /* pci region assignments */
-        u32 bases[32 - PCI_MEM_INDEX_SHIFT];
         u32 base;
         struct pci_region_entry *list;
     } r[PCI_REGION_TYPE_COUNT];
@@ -379,11 +376,18 @@ pci_region_create_entry(struct pci_bus *bus, struct pci_device *dev,
     entry->is64 = is64;
     entry->type = type;
     entry->parent_bus = bus;
-    list_add_head(&bus->r[type].list, entry);
+    // Insert into list in sorted order.
+    struct pci_region_entry **pprev;
+    for (pprev = &bus->r[type].list; *pprev; pprev = &(*pprev)->next) {
+        struct pci_region_entry *pos = *pprev;
+        if (pos->size < size)
+            break;
+    }
+    entry->next = *pprev;
+    *pprev = entry;
 
     u32 index = pci_size_to_index(size, type);
     size = pci_index_to_size(index, type);
-    bus->r[type].count[index]++;
     bus->r[type].sum += size;
     if (bus->r[type].max < size)
         bus->r[type].max = size;
@@ -478,46 +482,14 @@ static int pci_bios_init_root_regions(struct pci_bus *bus, u32 start, u32 end)
  * BAR assignment
  ****************************************************************/
 
-static void pci_bios_init_bus_bases(struct pci_bus *bus)
-{
-    u32 base, newbase, size;
-    int type, i;
-
-    for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
-        dprintf(1, "  type %s max %x sum %x base %x\n", region_type_name[type],
-                bus->r[type].max, bus->r[type].sum, bus->r[type].base);
-        base = bus->r[type].base;
-        for (i = ARRAY_SIZE(bus->r[type].count)-1; i >= 0; i--) {
-            size = pci_index_to_size(i, type);
-            if (!bus->r[type].count[i])
-                continue;
-            newbase = base + size * bus->r[type].count[i];
-            dprintf(1, "    size %8x: %d bar(s), %8x -> %8x\n",
-                    size, bus->r[type].count[i], base, newbase - 1);
-            bus->r[type].bases[i] = base;
-            base = newbase;
-        }
-    }
-}
-
-static u32 pci_bios_bus_get_addr(struct pci_bus *bus, int type, u32 size)
-{
-    u32 index, addr;
-
-    index = pci_size_to_index(size, type);
-    addr = bus->r[type].bases[index];
-    bus->r[type].bases[index] += pci_index_to_size(index, type);
-    return addr;
-}
-
 #define PCI_IO_SHIFT            8
 #define PCI_MEMORY_SHIFT        16
 #define PCI_PREF_MEMORY_SHIFT   16
 
 static void pci_region_map_one_entry(struct pci_region_entry *entry)
 {
-    u32 addr = pci_bios_bus_get_addr(
-        entry->parent_bus, entry->type, entry->size);
+    u32 addr = entry->parent_bus->r[entry->type].base;
+    entry->parent_bus->r[entry->type].base += entry->size;
     if (!entry->child_bus) {
         dprintf(1, "PCI: map device bdf=%02x:%02x.%x"
                 "  bar %d, addr %08x, size %08x [%s]\n",
@@ -559,15 +531,14 @@ static void pci_bios_map_devices(struct pci_bus *busses)
     // Map regions on each device.
     int bus;
     for (bus = 0; bus<=MaxPCIBus; bus++) {
-        dprintf(1, "PCI: init bases bus %d\n", bus);
-        pci_bios_init_bus_bases(&busses[bus]);
         int type;
         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
-            struct pci_region_entry *entry, *next;
-            list_foreach_entry_safe(busses[bus].r[type].list, next, entry) {
+            struct pci_region_entry *entry = busses[bus].r[type].list;
+            while (entry) {
                 pci_region_map_one_entry(entry);
-                list_del(entry);
+                struct pci_region_entry *next = entry->next;
                 free(entry);
+                entry = next;
             }
         }
     }
#10 lore raw

Re: [Qemu-devel] [PATCH 3/4] Switch from array based resource allocation to list

Hi Kevin,

Thank you for the patches!
I've created a diff of final version of your changes over mine, to make it clear what has changed.

Rather than including the complete diff, I've just left relevant parts and added comments.

--- a/src/pciinit.c +++ b/src/pciinit.c@@ -12,8 +12,9 @@
........
@@ -34,25 +35,44 @@

 struct pci_region_entry {
     struct pci_device *dev;
     int bar;
-    u32 base;
     u32 size;
-    int is64bit;
+    int is64;
     enum pci_region_type type;

- struct pci_bus *this_bus;
+ struct pci_bus *child_bus;

Structure members naming was one of difficult things when I was writing the code. The child_bus might be a bit confusing as people may thing that it describes a child bus in the bus topology,in fact this element describes the bus this pci_region_entry is representing.

.......

+
+static int pci_size_to_index(u32 size, enum pci_region_type type) +{
+ int index = __fls(size);
+ int shift = (type == PCI_REGION_TYPE_IO) ?
+ PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
+
+ if (index < shift)
+ index = shift;
+ index -= shift;
+ return index;
+}
+
+static u32 pci_index_to_size(int index, enum pci_region_type type) +{
+ int shift = (type == PCI_REGION_TYPE_IO) ?
+ PCI_IO_INDEX_SHIFT : PCI_MEM_INDEX_SHIFT;
+
+ return 0x1 << (index + shift);
+}

The only purpose to have these functions is to define the minimum size of pci BAR. They are used only once.
What if we add size adjustment to pci_region_create_entry, or just create a function like pci_adjust_size(u32 size, enum pci_region_type type, int bridge)?

.........

-    list_add_head(&bus->r[type].list, entry);
     entry->parent_bus = bus;
-
+    // Insert into list in sorted order.
+    struct pci_region_entry **pprev;
+    for (pprev = &bus->r[type].list; *pprev; pprev = &(*pprev)->next) {
+        struct pci_region_entry *pos = *pprev;
+        if (pos->size < size)
+            break;
+    }
+    entry->next = *pprev;
+    *pprev = entry;

+
..........
static void pci_bios_map_devices(struct pci_bus *busses) {
- struct pci_region_entry *entry, *next;
-
+ // Map regions on each device.

     int bus;
     for (bus = 0; bus<=MaxPCIBus; bus++) {
         int type;
         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {

- u32 size;
- for (size = busses[bus].r[type].max; size > 0; size >>= 1) {
- list_foreach_entry_safe(busses[bus].r[type].list,
- next, entry) {
- if (size == entry->size) {

-                            entry->base = busses[bus].r[type].base;
-                            busses[bus].r[type].base += size;
-                            pci_region_map_one_entry(entry);
-                            list_del(entry);
-                            free(entry);

- }
- }
+ struct pci_region_entry *entry = busses[bus].r[type].list;
+ while (entry) {

+                pci_region_map_one_entry(entry);
+                struct pci_region_entry *next = entry->next;
+                free(entry);
+                entry = next;
             }
         }
     }

Right, instead of sorting entries by size in pci_bios_map_devices, the entries are sorted when they are created. This makes the implementation simpler.
Note: In case of 64bit BARs we have to migrate entries, so just sorting on create won't be enough, we should also add sorting when entries are migrated. This will add some more complexity, while in case of old implementation complexity will remain the same. I expect it shouldn't be that complicated, so any of these approaches are fine for me.

#11 lore raw

Re: [Qemu-devel] [PATCH 3/4] Switch from array based resource allocation to list

Quoted 7 lines · Alexey Korolev

Hi Kevin,

Thank you for the patches!
I've created a diff of final version of your changes over mine, to make it clear what has changed.

Rather than including the complete diff, I've just left relevant parts and added comments.

Yes - there isn't much difference between your patches and my patches. I was really just playing with your patch.

Quoted 4 lines

Structure members naming was one of difficult things when I was writing the code. The child_bus might be a bit confusing as people may thing that it describes a child bus in the bus topology,in fact this element describes the bus this pci_region_entry is representing.

On Sunday, it occurred to me that we really don't need either parent_bus or this_bus.

Quoted 1 line

+static int pci_size_to_index(u32 size, enum pci_region_type type)

[...]

Quoted 4 lines

The only purpose to have these functions is to define the minimum size of pci BAR. They are used only once.
What if we add size adjustment to pci_region_create_entry, or just create a function like pci_adjust_size(u32 size, enum pci_region_type type, int bridge)?

Agreed - the only thing it does is force a minimum size for memory bars as you pointed out in a previous email.

As above, I did play with this a little more on Sunday. I also added in two patches from Gerd's series and made alignment handling more explicit. I'm including the series here if you're interested. Again, I think this should wait until after the 1.7.0 release.

-Kevin

Quoted 1 line

From 1ce91ff107ae43cc7fee5f87809f92078b0394ff Mon Sep 17 00:00:00 2001

Message-Id: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Tue, 3 Apr 2012 23:30:39 -0400
Subject: [PATCH 0/7] *** SUBJECT HERE ***
To: [email protected]

*** BLURB HERE ***

Alexey Korolev (1):
pciinit: Get rid of size element of pci_bus->r structure

Kevin O'Connor (6):
pciinit: Add a pci_region_entry structure.
pciinit: Perform bus bar assignment at same time as normal bar
assignment.
pciinit: Use sorted order allocation scheme instead of array based
count scheme.
pciinit: Track region alignment explicitly.
pciinit: bridges can have two regions too
pciinit: 64bit support.

 src/pci.h     |    5 -
 src/pciinit.c |  351 ++++++++++++++++++++++++++-------------------------------
 2 files changed, 160 insertions(+), 196 deletions(-)
Signature · 1 line
1.7.6.5
Quoted 1 line

From 5c88fe3a0a6bfb35abccee88904de11e2ce1896b Mon Sep 17 00:00:00 2001

Message-Id: <5c88fe3a0a6bfb35abccee88904de11e2ce1896b.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 11:23:06 -0400
Subject: [PATCH 1/7] pciinit: Add a pci_region_entry structure.
To: [email protected]

In this patch the pci_region_entry structure is introduced. The pci_device->bars are removed. The information from pci_region_entry is used to program pci bars.

Original patch by: Alexey Korolev <[email protected]>

---
 src/pci.h     |    5 --
 src/pciinit.c |  115 +++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 80 insertions(+), 40 deletions(-)
src/pci.h +0 -5
index a2a5a4c..5598100 100644
@@ -51,11 +51,6 @@ struct pci_device {
     u8 prog_if, revision;
     u8 header_type;
     u8 secondary_bus;
-    struct {
-        u32 addr;
-        u32 size;
-        int is64;
-    } bars[PCI_NUM_REGIONS];
 
     // Local information on device.
     int have_driver;
src/pciinit.c +80 -35

179 lines not on this page · raw message

Signature · 1 line
1.7.6.5
Quoted 1 line

From 60e411a40bff65d338d2d05c4a431a4b52d1b99e Mon Sep 17 00:00:00 2001

Message-Id: <60e411a40bff65d338d2d05c4a431a4b52d1b99e.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 00:39:20 -0400
Subject: [PATCH 2/7] pciinit: Perform bus bar assignment at same time as
normal bar assignment.
To: [email protected]

---
 src/pciinit.c |   53 ++++++++++++++++++-----------------------------------
 1 files changed, 18 insertions(+), 35 deletions(-)
src/pciinit.c +18 -35

80 lines not on this page · raw message

Signature · 1 line
1.7.6.5
Quoted 1 line

From c48faf3b52f0c0fad9da2c6edf4215ae9301b5a9 Mon Sep 17 00:00:00 2001

Message-Id: <c48faf3b52f0c0fad9da2c6edf4215ae9301b5a9.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Alexey Korolev <[email protected]>
Date: Wed, 28 Mar 2012 17:56:44 +1300
Subject: [PATCH 3/7] pciinit: Get rid of size element of pci_bus->r structure
To: [email protected]

The 'size' element of pci_bus->r structure is no longer need as the information about bridge region size is already stored in pci_region_entry structure.

---
 src/pciinit.c |   20 ++++++++------------
 1 files changed, 8 insertions(+), 12 deletions(-)
src/pciinit.c +8 -12
index 79301fd..eb8c2d7 100644
@@ -45,8 +45,6 @@ struct pci_bus {
         /* pci region stats */
         u32 count[32 - PCI_MEM_INDEX_SHIFT];
         u32 sum, max;
-        /* seconday bus region sizes */
-        u32 size;
         /* pci region assignments */
         u32 bases[32 - PCI_MEM_INDEX_SHIFT];
         u32 base;
@@ -439,20 +437,18 @@ static int pci_bios_check_devices(struct pci_bus *busses)
         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
             u32 limit = (type == PCI_REGION_TYPE_IO) ?
                 PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN;
-            s->r[type].size = s->r[type].sum;
-            if (s->r[type].size < limit)
-                s->r[type].size = limit;
-            s->r[type].size = pci_size_roundup(s->r[type].size);
+            u32 size = s->r[type].sum;
+            if (size < limit)
+                size = limit;
+            size = pci_size_roundup(size);
             struct pci_region_entry *entry = pci_region_create_entry(
-                parent, s->bus_dev, -1, s->r[type].size, type, 0);
+                parent, s->bus_dev, -1, size, type, 0);
             if (!entry)
                 return -1;
+            dprintf(1, "PCI: secondary bus %d size %x type %s\n",
+                      entry->dev->secondary_bus, size,
+                      region_type_name[entry->type]);
         }
-        dprintf(1, "PCI: secondary bus %d sizes: io %x, mem %x, prefmem %x\n",
-                secondary_bus,
-                s->r[PCI_REGION_TYPE_IO].size,
-                s->r[PCI_REGION_TYPE_MEM].size,
-                s->r[PCI_REGION_TYPE_PREFMEM].size);
     }
     return 0;
 }
Signature · 1 line
1.7.6.5
Quoted 1 line

From 8c2cc80a563c5652b695c62c2002764c81359082 Mon Sep 17 00:00:00 2001

Message-Id: <8c2cc80a563c5652b695c62c2002764c81359082.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 10:58:43 -0400
Subject: [PATCH 4/7] pciinit: Use sorted order allocation scheme instead of
array based count scheme.
To: [email protected]

---
 src/pciinit.c |   71 +++++---------------------------------------------------
 1 files changed, 7 insertions(+), 64 deletions(-)
src/pciinit.c +7 -64

125 lines not on this page · raw message

Signature · 1 line
1.7.6.5
Quoted 1 line

From ee86fc205d405c0e3d5caae37a5e9bc057485d56 Mon Sep 17 00:00:00 2001

Message-Id: <ee86fc205d405c0e3d5caae37a5e9bc057485d56.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 12:30:32 -0400
Subject: [PATCH 5/7] pciinit: Track region alignment explicitly.
To: [email protected]

Don't round up bridge regions to the next highest size - instead track alignment explicitly. This should improve the memory layout for bridge regions.

Also, unused bridge regions will no longer be allocated any space.

---
 src/pciinit.c |   41 ++++++++++++++++++-----------------------
 1 files changed, 18 insertions(+), 23 deletions(-)
src/pciinit.c +18 -23

113 lines not on this page · raw message

Signature · 1 line
1.7.6.5
Quoted 1 line

From 5c4d5afafd6b3b032fa81250e9a32ccb89ed5739 Mon Sep 17 00:00:00 2001

Message-Id: <5c4d5afafd6b3b032fa81250e9a32ccb89ed5739.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 15:21:24 -0400
Subject: [PATCH 6/7] pciinit: bridges can have two regions too
To: [email protected]

Original patch by: Gerd Hoffmann <[email protected]>

---
 src/pciinit.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
src/pciinit.c +3 -2
index 6ef1be8..7d3f076 100644
@@ -370,13 +370,14 @@ static int pci_bios_check_devices(struct pci_bus *busses)
     // Calculate resources needed for regular (non-bus) devices.
     struct pci_device *pci;
     foreachpci(pci) {
+        int num_regions = PCI_NUM_REGIONS;
         if (pci->class == PCI_CLASS_BRIDGE_PCI) {
             busses[pci->secondary_bus].bus_dev = pci;
-            continue;
+            num_regions = 2;
         }
         struct pci_bus *bus = &busses[pci_bdf_to_bus(pci->bdf)];
         int i;
-        for (i = 0; i < PCI_NUM_REGIONS; i++) {
+        for (i = 0; i < num_regions; i++) {
             u32 val, size;
             pci_bios_get_bar(pci, i, &val, &size);
             if (val == 0)
Signature · 1 line
1.7.6.5
Quoted 1 line

From 1ce91ff107ae43cc7fee5f87809f92078b0394ff Mon Sep 17 00:00:00 2001

Message-Id: <1ce91ff107ae43cc7fee5f87809f92078b0394ff.1333510239.git.kevin@koconnor.net>
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Kevin O'Connor <[email protected]>
Date: Sun, 1 Apr 2012 16:09:59 -0400
Subject: [PATCH 7/7] pciinit: 64bit support.
To: [email protected]

Makes pciinit.c 64bit aware. Use 64bit everywhere. Support discovery and configuration of 64bit bars, with non-zero upper32 bits.

Original patch by: Gerd Hoffmann <[email protected]>

---
 src/pciinit.c |  116 ++++++++++++++++++++++++++++++---------------------------
 1 files changed, 61 insertions(+), 55 deletions(-)
src/pciinit.c +61 -55

253 lines not on this page · raw message

Signature · 1 line
1.7.6.5
#12 lore raw

PATCH 4/4 Get rid of size element of pci_bus->r structure

In reply to Alexey Korolev

The 'size' element of pci_bus->r structure is no longer need as the information about bridge region size is already stored in pci_region_entry structure.

---
 src/pciinit.c |   21 +++++++++------------
 1 files changed, 9 insertions(+), 12 deletions(-)
src/pciinit.c +9 -12
index 85fe823..f07153a 100644
@@ -48,7 +48,6 @@ struct pci_bus {
     struct {
         /* pci region stats */
         u32 sum, max;
-        u32 size;
         u32 base;
         struct pci_region_entry *list;
     } r[PCI_REGION_TYPE_COUNT];
@@ -414,24 +413,22 @@ static int pci_bios_check_devices(struct pci_bus *busses)
             continue;
         struct pci_bus *parent = &busses[pci_bdf_to_bus(s->bus_dev->bdf)];
         int type;
+        u32 size;
         for (type = 0; type < PCI_REGION_TYPE_COUNT; type++) {
             u32 limit = (type == PCI_REGION_TYPE_IO) ?
                 PCI_BRIDGE_IO_MIN : PCI_BRIDGE_MEM_MIN;
-            s->r[type].size = s->r[type].sum;
-            if (s->r[type].size < limit)
-                s->r[type].size = limit;
-            s->r[type].size = pci_size_roundup(s->r[type].size);
-            entry = pci_region_create_entry(parent, s->bus_dev,
-                                            s->r[type].size, type, 0);
+            size = s->r[type].sum;
+            if (size < limit)
+                size = limit;
+            size = pci_size_roundup(size);
+            entry = pci_region_create_entry(parent, s->bus_dev, size, type, 0);
             if (!entry)
                 return -1;
             entry->this_bus = s;
+            dprintf(1, "PCI: secondary bus %d size 0x%x type %s\n",
+                      entry->dev->secondary_bus, size,
+                      region_type_name[entry->type]);
         }
-        dprintf(1, "PCI: secondary bus %d sizes: io %x, mem %x, prefmem %x\n",
-                secondary_bus,
-                s->r[PCI_REGION_TYPE_IO].size,
-                s->r[PCI_REGION_TYPE_MEM].size,
-                s->r[PCI_REGION_TYPE_PREFMEM].size);
     }
     return 0;
 }
Signature · 1 line
1.7.5.4