mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/4] pci: refactor bar configuration
Date: Sat, 14 Apr 2018 01:30:51 +0200	[thread overview]
Message-ID: <20180413233053.15570-2-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20180413233053.15570-1-u.kleine-koenig@pengutronix.de>

Instead of doing nearly the same three times for the three different
memory types, use some helper variables and do the same in a more
generic way only once.

Apart from minor changes in the pr_debug output there is no semantic
change intended.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/pci/pci.c | 104 ++++++++++++++++++++++--------------------------------
 1 file changed, 43 insertions(+), 61 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 538903ee6607..87359b6de82c 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -169,8 +169,11 @@ static void setup_device(struct pci_dev *dev, int max_bar)
 			      cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY));
 
 	for (bar = 0; bar < max_bar; bar++) {
-		resource_size_t last_addr;
-		u32 orig, barval, size;
+		u32 orig, barval, size, mask;
+		const char *type;
+		int restype;
+		unsigned long resflags;
+		resource_size_t *last;
 
 		pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + bar * 4, &orig);
 		pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + bar * 4, 0xfffffffe);
@@ -183,68 +186,47 @@ static void setup_device(struct pci_dev *dev, int max_bar)
 		}
 
 		if (barval & 0x01) { /* IO */
-			size = pci_size(orig, barval, 0xfffffffe);
-			if (!size) {
-				pr_debug("pbar%d bad IO mask\n", bar);
-				continue;
-			}
-			pr_debug("pbar%d: barval=%08x io %d bytes\n",
-				 bar, barval, size);
-			if (ALIGN(last_io, size) + size >
-			    dev->bus->resource[PCI_BUS_RESOURCE_IO]->end) {
-				pr_debug("BAR does not fit within bus IO res\n");
-				return;
-			}
-			last_io = ALIGN(last_io, size);
-			pr_debug("pbar%d: allocated at 0x%08x\n", bar, last_io);
-			pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + bar * 4, last_io);
-			dev->resource[bar].flags = IORESOURCE_IO;
-			last_addr = last_io;
-			last_io += size;
+			mask = 0xfffffffe;
+			type = "IO";
+			restype = PCI_BUS_RESOURCE_IO;
+			resflags = IORESOURCE_IO;
+			last = &last_io;
 		} else if ((barval & PCI_BASE_ADDRESS_MEM_PREFETCH) &&
-		           last_mem_pref) /* prefetchable MEM */ {
-			size = pci_size(orig, barval, 0xfffffff0);
-			if (!size) {
-				pr_debug("pbar%d bad P-MEM mask\n", bar);
-				continue;
-			}
-			pr_debug("pbar%d: barval=%08x P memory %d bytes\n",
-				 bar, barval, size);
-			if (ALIGN(last_mem_pref, size) + size >
-			    dev->bus->resource[PCI_BUS_RESOURCE_MEM_PREF]->end) {
-				pr_debug("BAR does not fit within bus p-mem res\n");
-				return;
-			}
-			last_mem_pref = ALIGN(last_mem_pref, size);
-			pr_debug("pbar%d: allocated at 0x%08x\n", bar, last_mem_pref);
-			pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + bar * 4, last_mem_pref);
-			dev->resource[bar].flags = IORESOURCE_MEM |
-			                           IORESOURCE_PREFETCH;
-			last_addr = last_mem_pref;
-			last_mem_pref += size;
-		} else { /* non-prefetch MEM */
-			size = pci_size(orig, barval, 0xfffffff0);
-			if (!size) {
-				pr_debug("pbar%d bad NP-MEM mask\n", bar);
-				continue;
-			}
-			pr_debug("pbar%d: barval=%08x NP memory %d bytes\n",
-				 bar, barval, size);
-			if (ALIGN(last_mem, size) + size >
-			    dev->bus->resource[PCI_BUS_RESOURCE_MEM]->end) {
-				pr_debug("BAR does not fit within bus np-mem res\n");
-				return;
-			}
-			last_mem = ALIGN(last_mem, size);
-			pr_debug("pbar%d: allocated at 0x%08x\n", bar, last_mem);
-			pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + bar * 4, last_mem);
-			dev->resource[bar].flags = IORESOURCE_MEM;
-			last_addr = last_mem;
-			last_mem += size;
+			   last_mem_pref) { /* prefetchable MEM */
+			mask = 0xfffffff0;
+			type = "P";
+			restype = PCI_BUS_RESOURCE_MEM_PREF;
+			resflags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
+			last = &last_mem_pref;
+		} else { /* non-prefetchable MEM */
+			mask = 0xfffffff0;
+			type = "NP";
+			restype = PCI_BUS_RESOURCE_MEM;
+			resflags = IORESOURCE_MEM;
+			last = &last_mem;
 		}
 
-		dev->resource[bar].start = last_addr;
-		dev->resource[bar].end = last_addr + size - 1;
+		size = pci_size(orig, barval, mask);
+		if (!size) {
+			pr_debug("pbar%d bad %s mask\n", bar, type);
+			continue;
+		}
+
+		pr_debug("pbar%d: barval=%08x type=%s size=0x%x\n",
+			 bar, barval, type, size);
+
+		if (ALIGN(*last, size) + size > dev->bus->resource[restype]->end) {
+			pr_debug("BAR does not fit within bus %s res\n", type);
+			return;
+		}
+
+		*last = ALIGN(*last, size);
+		pr_debug("pbar%d: allocated at 0x%08x\n", bar, *last);
+		pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + bar * 4, *last);
+		dev->resource[bar].flags = resflags;
+		dev->resource[bar].start = *last;
+		*last += size;
+		dev->resource[bar].end = *last - 1;
 
 		if ((barval & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
 			dev->resource[bar].flags |= IORESOURCE_MEM_64;
-- 
2.16.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2018-04-13 23:31 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-13 23:30 [PATCH 1/4] pci: rename variable mask -> barval Uwe Kleine-König
2018-04-13 23:30 ` Uwe Kleine-König [this message]
2018-04-13 23:30 ` [PATCH 3/4] pci: improve debug output Uwe Kleine-König
2018-04-13 23:58   ` Uwe Kleine-König
2018-04-13 23:30 ` [PATCH 4/4] pci: don't hide hard to understand sanity check in size calculation Uwe Kleine-König
2018-04-14 16:25   ` Andrey Smirnov
2018-04-15 18:27     ` Uwe Kleine-König
2018-04-16 13:42       ` Andrey Smirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180413233053.15570-2-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox