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 4/4] pci: don't hide hard to understand sanity check in size calculation
Date: Sat, 14 Apr 2018 01:30:53 +0200	[thread overview]
Message-ID: <20180413233053.15570-4-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20180413233053.15570-1-u.kleine-koenig@pengutronix.de>

pci_size() had a check that made it return 0 (i.e. an error) in some
cases. It took me a while to work out in which situations exactly this
check triggered: If writing 0xfffffffe to the BAR register doesn't
change it's value and the masked value isn't in the form 0b1....10...0.

This patch replaces the check in pci_size() by a similar check in
setup_device() where it is more expected to happen. This allows to
simplify pci_size() considerably.

The new check is a bit more strict as it doesn't consider the value the
BAR register holds initially and bails out iff the maximal base value
isn't in the expected format.

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

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 837e17aa8397..72689560d598 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -144,18 +144,10 @@ static struct pci_dev *alloc_pci_dev(void)
 	return dev;
 }
 
-static u32 pci_size(u32 base, u32 maxbase, u32 mask)
+static u32 pci_size(u32 maxbase)
 {
-	u32 size = maxbase & mask;
-	if (!size)
-		return 0;
-
-	size = (size & ~(size-1)) - 1;
-
-	if (base == maxbase && ((base | size) & mask) != mask)
-		return 0;
-
-	return size + 1;
+	/* isolate right-most set bit */
+	return maxbase & ~(maxbase - 1);
 }
 
 
@@ -169,7 +161,7 @@ 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++) {
-		u32 orig, barval, size, mask;
+		u32 orig, barval, maxbase, size, mask;
 		const char *type;
 		int restype;
 		unsigned long resflags;
@@ -206,13 +198,19 @@ static void setup_device(struct pci_dev *dev, int max_bar)
 			last = &last_mem;
 		}
 
-		size = pci_size(orig, barval, mask);
-		if (!size) {
-			pr_debug(" pbar%d: bad %s size (orig=%08x, barval=%08x, type=%s)\n",
-				 bar, type, orig, barval, type);
+		maxbase = barval & mask;
+
+		/*
+		 * if maxbase isn't in the form 0b1...10...0 there is something
+		 * fishy
+		 */
+		if ((maxbase | (maxbase - 1)) != (u32)-1 || maxbase == 0) {
+			pr_debug(" pbar%d: !! barval=%08x maxbase=0x%08x\n", bar, barval, maxbase);
 			continue;
 		}
 
+		size = pci_size(maxbase);
+
 		if (ALIGN(*last, size) + size > dev->bus->resource[restype]->end) {
 			pr_debug(" pbar%d: !! type=%s allocbase=0x%08x + size=0x%08x > resource_end=%pa (barval=%08x)\n",
 				 bar, type, ALIGN(*last, size), size, &dev->bus->resource[restype]->end, barval);
-- 
2.16.3


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

  parent 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 ` [PATCH 2/4] pci: refactor bar configuration Uwe Kleine-König
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 ` Uwe Kleine-König [this message]
2018-04-14 16:25   ` [PATCH 4/4] pci: don't hide hard to understand sanity check in size calculation 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-4-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