1 // SPDX-License-Identifier: GPL-2.0
3 * Implement the default iomap interfaces
5 * (C) Copyright 2004 Linus Torvalds
10 #include <linux/export.h>
14 * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
15 * @dev: PCI device that owns the BAR
17 * @offset: map memory at the given offset in BAR
18 * @maxlen: max length of the memory to map
20 * Using this function you will get a __iomem address to your device BAR.
21 * You can access it using ioread*() and iowrite*(). These functions hide
22 * the details if this is a MMIO or PIO address space and will just do what
23 * you expect from them in the correct way.
25 * @maxlen specifies the maximum length to map. If you want to get access to
26 * the complete BAR from offset to the end, pass %0 here.
28 void __iomem *pci_iomap_range(struct pci_dev *dev,
33 resource_size_t start = pci_resource_start(dev, bar);
34 resource_size_t len = pci_resource_len(dev, bar);
35 unsigned long flags = pci_resource_flags(dev, bar);
37 if (len <= offset || !start)
41 if (maxlen && len > maxlen)
43 if (flags & IORESOURCE_IO)
44 return __pci_ioport_map(dev, start, len);
45 if (flags & IORESOURCE_MEM)
46 return ioremap(start, len);
50 EXPORT_SYMBOL(pci_iomap_range);
53 * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
54 * @dev: PCI device that owns the BAR
56 * @offset: map memory at the given offset in BAR
57 * @maxlen: max length of the memory to map
59 * Using this function you will get a __iomem address to your device BAR.
60 * You can access it using ioread*() and iowrite*(). These functions hide
61 * the details if this is a MMIO or PIO address space and will just do what
62 * you expect from them in the correct way. When possible write combining
65 * @maxlen specifies the maximum length to map. If you want to get access to
66 * the complete BAR from offset to the end, pass %0 here.
68 void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
73 resource_size_t start = pci_resource_start(dev, bar);
74 resource_size_t len = pci_resource_len(dev, bar);
75 unsigned long flags = pci_resource_flags(dev, bar);
78 if (flags & IORESOURCE_IO)
81 if (len <= offset || !start)
86 if (maxlen && len > maxlen)
89 if (flags & IORESOURCE_MEM)
90 return ioremap_wc(start, len);
95 EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
98 * pci_iomap - create a virtual mapping cookie for a PCI BAR
99 * @dev: PCI device that owns the BAR
101 * @maxlen: length of the memory to map
103 * Using this function you will get a __iomem address to your device BAR.
104 * You can access it using ioread*() and iowrite*(). These functions hide
105 * the details if this is a MMIO or PIO address space and will just do what
106 * you expect from them in the correct way.
108 * @maxlen specifies the maximum length to map. If you want to get access to
109 * the complete BAR without checking for its length first, pass %0 here.
111 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
113 return pci_iomap_range(dev, bar, 0, maxlen);
115 EXPORT_SYMBOL(pci_iomap);
118 * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
119 * @dev: PCI device that owns the BAR
121 * @maxlen: length of the memory to map
123 * Using this function you will get a __iomem address to your device BAR.
124 * You can access it using ioread*() and iowrite*(). These functions hide
125 * the details if this is a MMIO or PIO address space and will just do what
126 * you expect from them in the correct way. When possible write combining
129 * @maxlen specifies the maximum length to map. If you want to get access to
130 * the complete BAR without checking for its length first, pass %0 here.
132 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
134 return pci_iomap_wc_range(dev, bar, 0, maxlen);
136 EXPORT_SYMBOL_GPL(pci_iomap_wc);
139 * pci_iounmap() somewhat illogically comes from lib/iomap.c for the
140 * CONFIG_GENERIC_IOMAP case, because that's the code that knows about
141 * the different IOMAP ranges.
143 * But if the architecture does not use the generic iomap code, and if
144 * it has _not_ defined it's own private pci_iounmap function, we define
147 * NOTE! This default implementation assumes that if the architecture
148 * support ioport mapping (HAS_IOPORT_MAP), the ioport mapping will
149 * be fixed to the range [ PCI_IOBASE, PCI_IOBASE+IO_SPACE_LIMIT [,
150 * and does not need unmapping with 'ioport_unmap()'.
152 * If you have different rules for your architecture, you need to
153 * implement your own pci_iounmap() that knows the rules for where
154 * and how IO vs MEM get mapped.
156 * This code is odd, and the ARCH_HAS/ARCH_WANTS #define logic comes
157 * from legacy <asm-generic/io.h> header file behavior. In particular,
158 * it would seem to make sense to do the iounmap(p) for the non-IO-space
159 * case here regardless, but that's not what the old header file code
160 * did. Probably incorrectly, but this is meant to be bug-for-bug
163 #if defined(ARCH_WANTS_GENERIC_PCI_IOUNMAP)
165 void pci_iounmap(struct pci_dev *dev, void __iomem *p)
167 #ifdef ARCH_HAS_GENERIC_IOPORT_MAP
168 uintptr_t start = (uintptr_t) PCI_IOBASE;
169 uintptr_t addr = (uintptr_t) p;
171 if (addr >= start && addr < start + IO_SPACE_LIMIT)
176 EXPORT_SYMBOL(pci_iounmap);
178 #endif /* ARCH_WANTS_GENERIC_PCI_IOUNMAP */
180 #endif /* CONFIG_PCI */