1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Implement the default iomap interfaces
4 *
5 * (C) Copyright 2004 Linus Torvalds
6 */
7 #include <linux/pci.h>
8 #include <linux/io.h>
9
10 #include <linux/export.h>
11
12 #include "pci.h" /* for pci_bar_index_is_valid() */
13
14 /**
15 * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
16 * @dev: PCI device that owns the BAR
17 * @bar: BAR number
18 * @offset: map memory at the given offset in BAR
19 * @maxlen: max length of the memory to map
20 *
21 * Using this function you will get a __iomem address to your device BAR.
22 * You can access it using ioread*() and iowrite*(). These functions hide
23 * the details if this is a MMIO or PIO address space and will just do what
24 * you expect from them in the correct way.
25 *
26 * @maxlen specifies the maximum length to map. If you want to get access to
27 * the complete BAR from offset to the end, pass %0 here.
28 *
29 * NOTE:
30 * This function is never managed, even if you initialized with
31 * pcim_enable_device().
32 * */
pci_iomap_range(struct pci_dev * dev,int bar,unsigned long offset,unsigned long maxlen)33 void __iomem *pci_iomap_range(struct pci_dev *dev,
34 int bar,
35 unsigned long offset,
36 unsigned long maxlen)
37 {
38 resource_size_t start, len;
39 unsigned long flags;
40
41 if (!pci_bar_index_is_valid(bar))
42 return NULL;
43
44 start = pci_resource_start(dev, bar);
45 len = pci_resource_len(dev, bar);
46 flags = pci_resource_flags(dev, bar);
47
48 if (len <= offset || !start)
49 return NULL;
50
51 len -= offset;
52 start += offset;
53 if (maxlen && len > maxlen)
54 len = maxlen;
55 if (flags & IORESOURCE_IO)
56 return __pci_ioport_map(dev, start, len);
57 if (flags & IORESOURCE_MEM)
58 return ioremap(start, len);
59 /* What? */
60 return NULL;
61 }
62 EXPORT_SYMBOL(pci_iomap_range);
63
64 /**
65 * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
66 * @dev: PCI device that owns the BAR
67 * @bar: BAR number
68 * @offset: map memory at the given offset in BAR
69 * @maxlen: max length of the memory to map
70 *
71 * Using this function you will get a __iomem address to your device BAR.
72 * You can access it using ioread*() and iowrite*(). These functions hide
73 * the details if this is a MMIO or PIO address space and will just do what
74 * you expect from them in the correct way. When possible write combining
75 * is used.
76 *
77 * @maxlen specifies the maximum length to map. If you want to get access to
78 * the complete BAR from offset to the end, pass %0 here.
79 *
80 * NOTE:
81 * This function is never managed, even if you initialized with
82 * pcim_enable_device().
83 * */
pci_iomap_wc_range(struct pci_dev * dev,int bar,unsigned long offset,unsigned long maxlen)84 void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
85 int bar,
86 unsigned long offset,
87 unsigned long maxlen)
88 {
89 resource_size_t start, len;
90 unsigned long flags;
91
92 if (!pci_bar_index_is_valid(bar))
93 return NULL;
94
95 start = pci_resource_start(dev, bar);
96 len = pci_resource_len(dev, bar);
97 flags = pci_resource_flags(dev, bar);
98
99 if (len <= offset || !start)
100 return NULL;
101 if (flags & IORESOURCE_IO)
102 return NULL;
103
104 len -= offset;
105 start += offset;
106 if (maxlen && len > maxlen)
107 len = maxlen;
108
109 if (flags & IORESOURCE_MEM)
110 return ioremap_wc(start, len);
111
112 /* What? */
113 return NULL;
114 }
115 EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
116
117 /**
118 * pci_iomap - create a virtual mapping cookie for a PCI BAR
119 * @dev: PCI device that owns the BAR
120 * @bar: BAR number
121 * @maxlen: length of the memory to map
122 *
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.
127 *
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.
130 *
131 * NOTE:
132 * This function is never managed, even if you initialized with
133 * pcim_enable_device(). If you need automatic cleanup, use pcim_iomap().
134 * */
pci_iomap(struct pci_dev * dev,int bar,unsigned long maxlen)135 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
136 {
137 return pci_iomap_range(dev, bar, 0, maxlen);
138 }
139 EXPORT_SYMBOL(pci_iomap);
140
141 /**
142 * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
143 * @dev: PCI device that owns the BAR
144 * @bar: BAR number
145 * @maxlen: length of the memory to map
146 *
147 * Using this function you will get a __iomem address to your device BAR.
148 * You can access it using ioread*() and iowrite*(). These functions hide
149 * the details if this is a MMIO or PIO address space and will just do what
150 * you expect from them in the correct way. When possible write combining
151 * is used.
152 *
153 * @maxlen specifies the maximum length to map. If you want to get access to
154 * the complete BAR without checking for its length first, pass %0 here.
155 *
156 * NOTE:
157 * This function is never managed, even if you initialized with
158 * pcim_enable_device().
159 * */
pci_iomap_wc(struct pci_dev * dev,int bar,unsigned long maxlen)160 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
161 {
162 return pci_iomap_wc_range(dev, bar, 0, maxlen);
163 }
164 EXPORT_SYMBOL_GPL(pci_iomap_wc);
165
166 /*
167 * pci_iounmap() somewhat illogically comes from lib/iomap.c for the
168 * CONFIG_GENERIC_IOMAP case, because that's the code that knows about
169 * the different IOMAP ranges.
170 *
171 * But if the architecture does not use the generic iomap code, and if
172 * it has _not_ defined its own private pci_iounmap function, we define
173 * it here.
174 *
175 * NOTE! This default implementation assumes that if the architecture
176 * support ioport mapping (HAS_IOPORT_MAP), the ioport mapping will
177 * be fixed to the range [ PCI_IOBASE, PCI_IOBASE+IO_SPACE_LIMIT [,
178 * and does not need unmapping with 'ioport_unmap()'.
179 *
180 * If you have different rules for your architecture, you need to
181 * implement your own pci_iounmap() that knows the rules for where
182 * and how IO vs MEM get mapped.
183 *
184 * This code is odd, and the ARCH_HAS/ARCH_WANTS #define logic comes
185 * from legacy <asm-generic/io.h> header file behavior. In particular,
186 * it would seem to make sense to do the iounmap(p) for the non-IO-space
187 * case here regardless, but that's not what the old header file code
188 * did. Probably incorrectly, but this is meant to be bug-for-bug
189 * compatible.
190 */
191 #if defined(ARCH_WANTS_GENERIC_PCI_IOUNMAP)
192
pci_iounmap(struct pci_dev * dev,void __iomem * p)193 void pci_iounmap(struct pci_dev *dev, void __iomem *p)
194 {
195 #ifdef ARCH_HAS_GENERIC_IOPORT_MAP
196 uintptr_t start = (uintptr_t) PCI_IOBASE;
197 uintptr_t addr = (uintptr_t) p;
198
199 if (addr >= start && addr < start + IO_SPACE_LIMIT)
200 return;
201 #endif
202 iounmap(p);
203 }
204 EXPORT_SYMBOL(pci_iounmap);
205
206 #endif /* ARCH_WANTS_GENERIC_PCI_IOUNMAP */
207