Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI: Add pci_iomap_wc() variants

PCI BARs tell us whether prefetching is safe, but they don't say
anything about write combining (WC). WC changes ordering rules
and allows writes to be collapsed, so it's not safe in general
to use it on a prefetchable region.

Add pci_iomap_wc() and pci_iomap_wc_range() so drivers can take
advantage of write combining when they know it's safe.

On architectures that don't fully support WC, e.g., x86 without
PAT, drivers for legacy framebuffers may get some of the benefit
by using arch_phys_wc_add() in addition to pci_iomap_wc(). But
arch_phys_wc_add() is unreliable and should be avoided in
general. On x86, it uses MTRRs, which are limited in number and
size, so the results will vary based on driver loading order.

The goals of adding pci_iomap_wc() are to:

- Give drivers an architecture-independent way to use WC so they can stop
using interfaces like mtrr_add() (on x86, pci_iomap_wc() uses
PAT when available).

- Move toward using _PAGE_CACHE_MODE_UC, not _PAGE_CACHE_MODE_UC_MINUS,
on x86 on ioremap_nocache() (see de33c442ed2a ("x86 PAT: fix
performance drop for glx, use UC minus for ioremap(), ioremap_nocache()
and pci_mmap_page_range()").

Signed-off-by: Luis R. Rodriguez <mcgrof@suse.com>
[ Move IORESOURCE_IO check up, space out statements for better readability. ]
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Cc: <roger.pau@citrix.com>
Cc: <syrjala@sci.fi>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Antonino Daplas <adaplas@gmail.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: Dave Airlie <airlied@redhat.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Davidlohr Bueso <dbueso@suse.de>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jean-Christophe Plagniol-Villard <plagnioj@jcrosoft.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Roger Pau Monné <roger.pau@citrix.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Stefan Bader <stefan.bader@canonical.com>
Cc: Suresh Siddha <sbsiddha@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tomi Valkeinen <tomi.valkeinen@ti.com>
Cc: Toshi Kani <toshi.kani@hp.com>
Cc: Ville Syrjälä <syrjala@sci.fi>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: airlied@linux.ie
Cc: benh@kernel.crashing.org
Cc: dan.j.williams@intel.com
Cc: david.vrabel@citrix.com
Cc: jbeulich@suse.com
Cc: konrad.wilk@oracle.com
Cc: linux-arch@vger.kernel.org
Cc: linux-fbdev@vger.kernel.org
Cc: linux-pci@vger.kernel.org
Cc: venkatesh.pallipadi@intel.com
Cc: vinod.koul@intel.com
Cc: xen-devel@lists.xensource.com
Link: http://lkml.kernel.org/r/1440443613-13696-6-git-send-email-mcgrof@do-not-panic.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Luis R. Rodriguez and committed by
Ingo Molnar
1b3d4200 f3adccbd

+80
+14
include/asm-generic/pci_iomap.h
··· 15 15 #ifdef CONFIG_PCI 16 16 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */ 17 17 extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max); 18 + extern void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long max); 18 19 extern void __iomem *pci_iomap_range(struct pci_dev *dev, int bar, 19 20 unsigned long offset, 20 21 unsigned long maxlen); 22 + extern void __iomem *pci_iomap_wc_range(struct pci_dev *dev, int bar, 23 + unsigned long offset, 24 + unsigned long maxlen); 21 25 /* Create a virtual mapping cookie for a port on a given PCI device. 22 26 * Do not call this directly, it exists to make it easier for architectures 23 27 * to override */ ··· 38 34 return NULL; 39 35 } 40 36 37 + static inline void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long max) 38 + { 39 + return NULL; 40 + } 41 41 static inline void __iomem *pci_iomap_range(struct pci_dev *dev, int bar, 42 42 unsigned long offset, 43 43 unsigned long maxlen) 44 + { 45 + return NULL; 46 + } 47 + static inline void __iomem *pci_iomap_wc_range(struct pci_dev *dev, int bar, 48 + unsigned long offset, 49 + unsigned long maxlen) 44 50 { 45 51 return NULL; 46 52 }
+66
lib/pci_iomap.c
··· 52 52 EXPORT_SYMBOL(pci_iomap_range); 53 53 54 54 /** 55 + * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR 56 + * @dev: PCI device that owns the BAR 57 + * @bar: BAR number 58 + * @offset: map memory at the given offset in BAR 59 + * @maxlen: max length of the memory to map 60 + * 61 + * Using this function you will get a __iomem address to your device BAR. 62 + * You can access it using ioread*() and iowrite*(). These functions hide 63 + * the details if this is a MMIO or PIO address space and will just do what 64 + * you expect from them in the correct way. When possible write combining 65 + * is used. 66 + * 67 + * @maxlen specifies the maximum length to map. If you want to get access to 68 + * the complete BAR from offset to the end, pass %0 here. 69 + * */ 70 + void __iomem *pci_iomap_wc_range(struct pci_dev *dev, 71 + int bar, 72 + unsigned long offset, 73 + unsigned long maxlen) 74 + { 75 + resource_size_t start = pci_resource_start(dev, bar); 76 + resource_size_t len = pci_resource_len(dev, bar); 77 + unsigned long flags = pci_resource_flags(dev, bar); 78 + 79 + 80 + if (flags & IORESOURCE_IO) 81 + return NULL; 82 + 83 + if (len <= offset || !start) 84 + return NULL; 85 + 86 + len -= offset; 87 + start += offset; 88 + if (maxlen && len > maxlen) 89 + len = maxlen; 90 + 91 + if (flags & IORESOURCE_MEM) 92 + return ioremap_wc(start, len); 93 + 94 + /* What? */ 95 + return NULL; 96 + } 97 + EXPORT_SYMBOL_GPL(pci_iomap_wc_range); 98 + 99 + /** 55 100 * pci_iomap - create a virtual mapping cookie for a PCI BAR 56 101 * @dev: PCI device that owns the BAR 57 102 * @bar: BAR number ··· 115 70 return pci_iomap_range(dev, bar, 0, maxlen); 116 71 } 117 72 EXPORT_SYMBOL(pci_iomap); 73 + 74 + /** 75 + * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR 76 + * @dev: PCI device that owns the BAR 77 + * @bar: BAR number 78 + * @maxlen: length of the memory to map 79 + * 80 + * Using this function you will get a __iomem address to your device BAR. 81 + * You can access it using ioread*() and iowrite*(). These functions hide 82 + * the details if this is a MMIO or PIO address space and will just do what 83 + * you expect from them in the correct way. When possible write combining 84 + * is used. 85 + * 86 + * @maxlen specifies the maximum length to map. If you want to get access to 87 + * the complete BAR without checking for its length first, pass %0 here. 88 + * */ 89 + void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen) 90 + { 91 + return pci_iomap_wc_range(dev, bar, 0, maxlen); 92 + } 93 + EXPORT_SYMBOL_GPL(pci_iomap_wc); 118 94 #endif /* CONFIG_PCI */