[PATCH] pci: introduce an ioremap_pcibar(pdev, barnr) function

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Arjan van de Ven
Date: Friday, September 26, 2008 - 4:36 pm

From abb4ec6b5b7f22904668fb5de5fe0d7a3a7fd2d5 Mon Sep 17 00:00:00 2001
From: Arjan van de Ven <arjan@linux.intel.com>
Date: Fri, 26 Sep 2008 16:34:52 -0700
Subject: [PATCH] pci: introduce an ioremap_pcibar(pdev, barnr) function

A common thing in many PCI drivers is to ioremap() an entire bar.
This is a slightly fragile thing right now, needing both an address and a size,
and many driver writers do.. various things there.

This patch introduces an ioremap_pcibar() function taking just a PCI device struct
and the bar number as arguments, and figures this all out itself, in one place.
In addition, we can add various sanity checks to this function (the patch already
checks to make sure that the bar in question really is a MEM bar; few to no drivers
do that sort of thing).

Hopefully with this type of API we get less chance of mistakes in drivers with
ioremap() operations.

Signed-off-by: Arjan van de Ven <arjan@linux.intel.com>
---
 include/linux/pci.h |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/include/linux/pci.h b/include/linux/pci.h
index e48614b..65c1dbc 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1122,5 +1122,18 @@ static inline void pci_mmcfg_early_init(void) { }
 static inline void pci_mmcfg_late_init(void) { }
 #endif
 
+static inline void * ioremap_pcibar(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_nocache(pci_resource_start(pdev, bar),
+				     pci_resource_len(pdev, bar));
+}
+
 #endif /* __KERNEL__ */
 #endif /* LINUX_PCI_H */
-- 
1.5.5.1



-- 
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] pci: introduce an ioremap_pcibar(pdev, barnr) function, Arjan van de Ven, (Fri Sep 26, 4:36 pm)
[PATCH] pci: introduce users of ioremap_pcibar(), Arjan van de Ven, (Fri Sep 26, 4:37 pm)
Re: [PATCH] pci: introduce an ioremap_pcibar(pdev, barnr) ..., Arjan van de Ven, (Sat Sep 27, 8:35 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Grant Grundler, (Mon Sep 29, 12:26 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Arjan van de Ven, (Mon Sep 29, 6:42 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Grant Grundler, (Mon Sep 29, 10:10 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Arjan van de Ven, (Mon Sep 29, 10:23 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Arjan van de Ven, (Tue Sep 30, 3:30 pm)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Grant Grundler, (Tue Sep 30, 10:24 pm)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Rolf Eike Beer, (Wed Oct 1, 3:33 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Kyle McMartin, (Wed Oct 1, 5:42 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Matthew Wilcox, (Wed Oct 1, 5:57 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Kyle McMartin, (Wed Oct 1, 6:07 am)
Re: [PATCH] pci: introduce users of ioremap_pcibar(), Arjan van de Ven, (Wed Oct 1, 6:53 am)