diff --git a/drivers/pci/pci_auto.c b/drivers/pci/pci_auto.c
index c7968926a1..69c801fc62 100644
--- a/drivers/pci/pci_auto.c
+++ b/drivers/pci/pci_auto.c
@@ -14,6 +14,8 @@
 #include <log.h>
 #include <pci.h>
 #include <time.h>
+#include <linux/compiler.h>
+#include <linux/list.h>
 #include "pci_internal.h"
 
 /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
@@ -21,6 +23,69 @@
 #define CONFIG_SYS_PCI_CACHE_LINE_SIZE	8
 #endif
 
+struct pci_dev_resource {
+	struct list_head list;
+	int bar;
+	pci_size_t bar_size;
+	int found_mem64;
+	struct pci_region *bar_res;
+};
+
+/* Sort resources by bar size */
+static void pdev_sort_resources(struct pci_dev_resource *new,
+				struct list_head *head)
+{
+	struct pci_dev_resource *dev_res;
+	struct list_head *n;
+
+	/* Fallback is smallest one or list is empty */
+	n = head;
+	list_for_each_entry(dev_res, head, list) {
+		if (new->bar_size > dev_res->bar_size) {
+			n = &dev_res->list;
+			break;
+		}
+	}
+
+	/* Insert it just before n */
+	list_add_tail(&new->list, n);
+}
+
+static void pdev_assign_resources(struct udevice *dev, struct list_head *head)
+{
+	struct pci_dev_resource *dev_res;
+	int bar;
+	pci_addr_t bar_value;
+	int ret = 0;
+
+	list_for_each_entry(dev_res, head, list) {
+		ret = pciauto_region_allocate(dev_res->bar_res, dev_res->bar_size,
+					      &bar_value, dev_res->found_mem64);
+		if (ret)
+			printf("PCI: Failed autoconfig bar %x\n", dev_res->bar);
+
+		bar = dev_res->bar;
+		if (!ret) {
+			/* Write it out and update our limit */
+			dm_pci_write_config32(dev, bar, (u32)bar_value);
+
+			if (dev_res->found_mem64) {
+				bar += 4;
+				if (IS_ENABLED(CONFIG_SYS_PCI_64BIT))
+					dm_pci_write_config32(dev, bar,
+							      (u32)(bar_value >> 32));
+				else
+					/*
+					 * If we are a 64-bit decoder then increment to
+					 * the upper 32 bits of the bar and force it to
+					 * locate in the lower 4GB of memory.
+					 */
+					dm_pci_write_config32(dev, bar, 0x00000000);
+			}
+		}
+	}
+}
+
 static void dm_pciauto_setup_device(struct udevice *dev,
 				    struct pci_region *mem,
 				    struct pci_region *prefetch,
@@ -37,6 +102,10 @@ static void dm_pciauto_setup_device(struct udevice *dev,
 	struct pci_region *bar_res = NULL;
 	int found_mem64 = 0;
 	u16 class;
+	LIST_HEAD(head);
+	struct pci_dev_resource pdev_resource[6];
+
+	memset(pdev_resource, 0x0, sizeof(pdev_resource));
 
 	dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
 	cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
@@ -64,8 +133,6 @@ static void dm_pciauto_setup_device(struct udevice *dev,
 
 	for (bar = PCI_BASE_ADDRESS_0;
 	     bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
-		int ret = 0;
-
 		/* Tickle the BAR and get the response */
 		dm_pci_write_config32(dev, bar, 0xffffffff);
 		dm_pci_read_config32(dev, bar, &bar_response);
@@ -118,30 +185,15 @@ static void dm_pciauto_setup_device(struct udevice *dev,
 			      (unsigned long long)bar_size);
 		}
 
-		ret = pciauto_region_allocate(bar_res, bar_size,
-					      &bar_value, found_mem64);
-		if (ret)
-			printf("PCI: Failed autoconfig bar %x\n", bar);
-
-		if (!ret) {
-			/* Write it out and update our limit */
-			dm_pci_write_config32(dev, bar, (u32)bar_value);
+		INIT_LIST_HEAD(&pdev_resource[bar_nr].list);
+		pdev_resource[bar_nr].bar = bar;
+		pdev_resource[bar_nr].bar_size = bar_size;
+		pdev_resource[bar_nr].bar_res = bar_res;
+		pdev_resource[bar_nr].found_mem64 = found_mem64;
+		pdev_sort_resources(&pdev_resource[bar_nr], &head);
 
-			if (found_mem64) {
-				bar += 4;
-#ifdef CONFIG_SYS_PCI_64BIT
-				dm_pci_write_config32(dev, bar,
-						      (u32)(bar_value >> 32));
-#else
-				/*
-				 * If we are a 64-bit decoder then increment to
-				 * the upper 32 bits of the bar and force it to
-				 * locate in the lower 4GB of memory.
-				 */
-				dm_pci_write_config32(dev, bar, 0x00000000);
-#endif
-			}
-		}
+		if (found_mem64)
+			bar += 4;
 
 		cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
 			PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
@@ -151,6 +203,8 @@ static void dm_pciauto_setup_device(struct udevice *dev,
 		bar_nr++;
 	}
 
+	pdev_assign_resources(dev, &head);
+
 	/* Configure the expansion ROM address */
 	if (header_type == PCI_HEADER_TYPE_NORMAL ||
 	    header_type == PCI_HEADER_TYPE_BRIDGE) {
