1 // SPDX-License-Identifier: GPL-2.0
3 * Implement the default iomap interfaces
5 * (C) Copyright 2004 Linus Torvalds
10 #include <linux/export.h>
13 * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
14 * @dev: PCI device that owns the BAR
16 * @offset: map memory at the given offset in BAR
17 * @maxlen: max length of the memory to map
19 * Using this function you will get a __iomem address to your device BAR.
20 * You can access it using ioread*() and iowrite*(). These functions hide
21 * the details if this is a MMIO or PIO address space and will just do what
22 * you expect from them in the correct way.
24 * @maxlen specifies the maximum length to map. If you want to get access to
25 * the complete BAR from offset to the end, pass %0 here.
27 void __iomem *pci_iomap_range(struct pci_dev *dev,
32 resource_size_t start = pci_resource_start(dev, bar);
33 resource_size_t len = pci_resource_len(dev, bar);
34 unsigned long flags = pci_resource_flags(dev, bar);
36 if (len <= offset || !start)
40 if (maxlen && len > maxlen)
42 if (flags & IORESOURCE_IO)
43 return __pci_ioport_map(dev, start, len);
44 if (flags & IORESOURCE_MEM)
45 return ioremap(start, len);
49 EXPORT_SYMBOL(pci_iomap_range);
52 * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
53 * @dev: PCI device that owns the BAR
55 * @offset: map memory at the given offset in BAR
56 * @maxlen: max length of the memory to map
58 * Using this function you will get a __iomem address to your device BAR.
59 * You can access it using ioread*() and iowrite*(). These functions hide
60 * the details if this is a MMIO or PIO address space and will just do what
61 * you expect from them in the correct way. When possible write combining
64 * @maxlen specifies the maximum length to map. If you want to get access to
65 * the complete BAR from offset to the end, pass %0 here.
67 void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
72 resource_size_t start = pci_resource_start(dev, bar);
73 resource_size_t len = pci_resource_len(dev, bar);
74 unsigned long flags = pci_resource_flags(dev, bar);
77 if (flags & IORESOURCE_IO)
80 if (len <= offset || !start)
85 if (maxlen && len > maxlen)
88 if (flags & IORESOURCE_MEM)
89 return ioremap_wc(start, len);
94 EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
97 * pci_iomap - create a virtual mapping cookie for a PCI BAR
98 * @dev: PCI device that owns the BAR
100 * @maxlen: length of the memory to map
102 * Using this function you will get a __iomem address to your device BAR.
103 * You can access it using ioread*() and iowrite*(). These functions hide
104 * the details if this is a MMIO or PIO address space and will just do what
105 * you expect from them in the correct way.
107 * @maxlen specifies the maximum length to map. If you want to get access to
108 * the complete BAR without checking for its length first, pass %0 here.
110 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
112 return pci_iomap_range(dev, bar, 0, maxlen);
114 EXPORT_SYMBOL(pci_iomap);
117 * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
118 * @dev: PCI device that owns the BAR
120 * @maxlen: length of the memory to map
122 * Using this function you will get a __iomem address to your device BAR.
123 * You can access it using ioread*() and iowrite*(). These functions hide
124 * the details if this is a MMIO or PIO address space and will just do what
125 * you expect from them in the correct way. When possible write combining
128 * @maxlen specifies the maximum length to map. If you want to get access to
129 * the complete BAR without checking for its length first, pass %0 here.
131 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
133 return pci_iomap_wc_range(dev, bar, 0, maxlen);
135 EXPORT_SYMBOL_GPL(pci_iomap_wc);
138 * pci_iounmap() somewhat illogically comes from lib/iomap.c for the
139 * CONFIG_GENERIC_IOMAP case, because that's the code that knows about
140 * the different IOMAP ranges.
142 * But if the architecture does not use the generic iomap code, and if
143 * it has _not_ defined it's own private pci_iounmap function, we define
146 * NOTE! This default implementation assumes that if the architecture
147 * support ioport mapping (HAS_IOPORT_MAP), the ioport mapping will
148 * be fixed to the range [ PCI_IOBASE, PCI_IOBASE+IO_SPACE_LIMIT [,
149 * and does not need unmapping with 'ioport_unmap()'.
151 * If you have different rules for your architecture, you need to
152 * implement your own pci_iounmap() that knows the rules for where
153 * and how IO vs MEM get mapped.
155 * This code is odd, and the ARCH_HAS/ARCH_WANTS #define logic comes
156 * from legacy <asm-generic/io.h> header file behavior. In particular,
157 * it would seem to make sense to do the iounmap(p) for the non-IO-space
158 * case here regardless, but that's not what the old header file code
159 * did. Probably incorrectly, but this is meant to be bug-for-bug
162 #if defined(ARCH_WANTS_GENERIC_PCI_IOUNMAP)
164 void pci_iounmap(struct pci_dev *dev, void __iomem *p)
166 #ifdef ARCH_HAS_GENERIC_IOPORT_MAP
167 uintptr_t start = (uintptr_t) PCI_IOBASE;
168 uintptr_t addr = (uintptr_t) p;
170 if (addr >= start && addr < start + IO_SPACE_LIMIT)
175 EXPORT_SYMBOL(pci_iounmap);
177 #endif /* ARCH_WANTS_GENERIC_PCI_IOUNMAP */