All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Krzysztof Wilczyński" <kw@linux.com>
To: Bjorn Helgaas <bhelgaas@google.com>
Cc: linux-pci@vger.kernel.org
Subject: [PATCH] PCI: Refactor pci_ioremap_bar() and pci_ioremap_wc_bar()
Date: Tue, 13 Jul 2021 10:24:36 +0000	[thread overview]
Message-ID: <20210713102436.304693-1-kw@linux.com> (raw)

Currently, functions pci_ioremap_bar() and pci_ioremap_wc_bar() share
similar implementation details as both functions were almost identical
in the past, especially when the latter was initially introduced in the
commit c43996f4001d ("PCI: Add pci_ioremap_wc_bar()") as somewhat exact
copy of the function pci_ioremap_bar().

However, function pci_ioremap_bar() received several updates that were
never introduced to the function pci_ioremap_wc_bar().

Thus, to align implementation of both functions and reduce the need to
duplicate code between them, introduce a new internal function called
__pci_ioremap_resource() as a helper with a shared codebase intended to
be called from functions pci_ioremap_bar() and pci_ioremap_wc_bar().

The  __pci_ioremap_resource() function will therefore include a check
for the IORESOURCE_UNSET flag that has previously been added to the
function pci_ioremap_bar() in the commit 646c0282df04 ("PCI: Fail
pci_ioremap_bar() on unassigned resources") and otherwise has been
missing from function pci_ioremap_wc_bar().

Additionally, function __pci_ioremap_resource() will retire the usage of
the WARN_ON() macro and replace it with pci_err() to show information
such as the driver name, the BAR number and resource details in case of
a failure, instead of printing a complete backtrace. The WARN_ON() has
already been replaced with pci_warn() in the commit 1f7bf3bfb5d6 ("PCI:
Show driver, BAR#, and resource on pci_ioremap_bar() failure") which
sadly didn't include an update to the function pci_ioremap_wc_bar() at
that time.

Finally, a direct use of functions ioremap() and ioremap_wc() in the
function __pci_ioremap_resource() will be replaced with calls to the
pci_iomap_range() and pci_iomap_wc_range() functions respectively.

Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
---
 drivers/pci/pci.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index e3bb0d073352..4bae55f0700b 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -206,7 +206,8 @@ int pci_status_get_and_clear_errors(struct pci_dev *pdev)
 EXPORT_SYMBOL_GPL(pci_status_get_and_clear_errors);
 
 #ifdef CONFIG_HAS_IOMEM
-void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
+static void __iomem *__pci_ioremap_resource(struct pci_dev *pdev, int bar,
+					    bool write_combine)
 {
 	struct resource *res = &pdev->resource[bar];
 
@@ -214,24 +215,25 @@ void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
 	 * Make sure the BAR is actually a memory resource, not an IO resource
 	 */
 	if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) {
-		pci_warn(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
+		pci_err(pdev, "can't ioremap BAR %d: %pR\n", bar, res);
 		return NULL;
 	}
-	return ioremap(res->start, resource_size(res));
+
+	if (write_combine)
+		return pci_iomap_wc_range(pdev, bar, 0, 0);
+
+	return pci_iomap_range(pdev, bar, 0, 0);
+}
+
+void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar)
+{
+	return __pci_ioremap_resource(pdev, bar, false);
 }
 EXPORT_SYMBOL_GPL(pci_ioremap_bar);
 
 void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar)
 {
-	/*
-	 * Make sure the BAR is actually a memory resource, not an IO resource
-	 */
-	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
-		WARN_ON(1);
-		return NULL;
-	}
-	return ioremap_wc(pci_resource_start(pdev, bar),
-			  pci_resource_len(pdev, bar));
+	return __pci_ioremap_resource(pdev, bar, true);
 }
 EXPORT_SYMBOL_GPL(pci_ioremap_wc_bar);
 #endif
-- 
2.32.0


             reply	other threads:[~2021-07-13 10:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-13 10:24 Krzysztof Wilczyński [this message]
2021-07-16 21:08 ` [PATCH] PCI: Refactor pci_ioremap_bar() and pci_ioremap_wc_bar() Bjorn Helgaas

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=20210713102436.304693-1-kw@linux.com \
    --to=kw@linux.com \
    --cc=bhelgaas@google.com \
    --cc=linux-pci@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.