blob: 40a8b12a8b6b9237f34a63d90f4566a2b24fd154 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2#include <linux/err.h>
3#include <linux/pci.h>
4#include <linux/io.h>
5#include <linux/gfp.h>
6#include <linux/export.h>
7
8void devm_ioremap_release(struct device *dev, void *res)
9{
10 iounmap(*(void __iomem **)res);
11}
12
13static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
14{
15 return *(void **)res == match_data;
16}
17
18/**
19 * devm_ioremap - Managed ioremap()
20 * @dev: Generic device to remap IO address for
21 * @offset: Resource address to map
22 * @size: Size of map
23 *
24 * Managed ioremap(). Map is automatically unmapped on driver detach.
25 */
26void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
27 resource_size_t size)
28{
29 void __iomem **ptr, *addr;
30
31 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
32 if (!ptr)
33 return NULL;
34
35 addr = ioremap(offset, size);
36 if (addr) {
37 *ptr = addr;
38 devres_add(dev, ptr);
39 } else
40 devres_free(ptr);
41
42 return addr;
43}
44EXPORT_SYMBOL(devm_ioremap);
45
46/**
47 * devm_ioremap_nocache - Managed ioremap_nocache()
48 * @dev: Generic device to remap IO address for
49 * @offset: Resource address to map
50 * @size: Size of map
51 *
52 * Managed ioremap_nocache(). Map is automatically unmapped on driver
53 * detach.
54 */
55void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
56 resource_size_t size)
57{
58 void __iomem **ptr, *addr;
59
60 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
61 if (!ptr)
62 return NULL;
63
64 addr = ioremap_nocache(offset, size);
65 if (addr) {
66 *ptr = addr;
67 devres_add(dev, ptr);
68 } else
69 devres_free(ptr);
70
71 return addr;
72}
73EXPORT_SYMBOL(devm_ioremap_nocache);
74
75/**
76 * devm_ioremap_wc - Managed ioremap_wc()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
79 * @size: Size of map
80 *
81 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
82 */
83void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
84 resource_size_t size)
85{
86 void __iomem **ptr, *addr;
87
88 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
89 if (!ptr)
90 return NULL;
91
92 addr = ioremap_wc(offset, size);
93 if (addr) {
94 *ptr = addr;
95 devres_add(dev, ptr);
96 } else
97 devres_free(ptr);
98
99 return addr;
100}
101EXPORT_SYMBOL(devm_ioremap_wc);
102
103/**
104 * devm_iounmap - Managed iounmap()
105 * @dev: Generic device to unmap for
106 * @addr: Address to unmap
107 *
108 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
109 */
110void devm_iounmap(struct device *dev, void __iomem *addr)
111{
112 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
113 (__force void *)addr));
114 iounmap(addr);
115}
116EXPORT_SYMBOL(devm_iounmap);
117
118/**
119 * devm_ioremap_resource() - check, request region, and ioremap resource
120 * @dev: generic device to handle the resource for
121 * @res: resource to be handled
122 *
123 * Checks that a resource is a valid memory region, requests the memory
124 * region and ioremaps it. All operations are managed and will be undone
125 * on driver detach.
126 *
127 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
128 * on failure. Usage example:
129 *
130 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 * base = devm_ioremap_resource(&pdev->dev, res);
132 * if (IS_ERR(base))
133 * return PTR_ERR(base);
134 */
135void __iomem *devm_ioremap_resource(struct device *dev,
136 const struct resource *res)
137{
138 resource_size_t size;
139 const char *name;
140 void __iomem *dest_ptr;
141
142 BUG_ON(!dev);
143
144 if (!res || resource_type(res) != IORESOURCE_MEM) {
145 dev_err(dev, "invalid resource\n");
146 return IOMEM_ERR_PTR(-EINVAL);
147 }
148
149 size = resource_size(res);
150 name = res->name ?: dev_name(dev);
151
152 if (!devm_request_mem_region(dev, res->start, size, name)) {
153 dev_err(dev, "can't request region for resource %pR\n", res);
154 return IOMEM_ERR_PTR(-EBUSY);
155 }
156
157 dest_ptr = devm_ioremap(dev, res->start, size);
158 if (!dest_ptr) {
159 dev_err(dev, "ioremap failed for resource %pR\n", res);
160 devm_release_mem_region(dev, res->start, size);
161 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
162 }
163
164 return dest_ptr;
165}
166EXPORT_SYMBOL(devm_ioremap_resource);
167
168#ifdef CONFIG_HAS_IOPORT_MAP
169/*
170 * Generic iomap devres
171 */
172static void devm_ioport_map_release(struct device *dev, void *res)
173{
174 ioport_unmap(*(void __iomem **)res);
175}
176
177static int devm_ioport_map_match(struct device *dev, void *res,
178 void *match_data)
179{
180 return *(void **)res == match_data;
181}
182
183/**
184 * devm_ioport_map - Managed ioport_map()
185 * @dev: Generic device to map ioport for
186 * @port: Port to map
187 * @nr: Number of ports to map
188 *
189 * Managed ioport_map(). Map is automatically unmapped on driver
190 * detach.
191 */
192void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
193 unsigned int nr)
194{
195 void __iomem **ptr, *addr;
196
197 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
198 if (!ptr)
199 return NULL;
200
201 addr = ioport_map(port, nr);
202 if (addr) {
203 *ptr = addr;
204 devres_add(dev, ptr);
205 } else
206 devres_free(ptr);
207
208 return addr;
209}
210EXPORT_SYMBOL(devm_ioport_map);
211
212/**
213 * devm_ioport_unmap - Managed ioport_unmap()
214 * @dev: Generic device to unmap for
215 * @addr: Address to unmap
216 *
217 * Managed ioport_unmap(). @addr must have been mapped using
218 * devm_ioport_map().
219 */
220void devm_ioport_unmap(struct device *dev, void __iomem *addr)
221{
222 ioport_unmap(addr);
223 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
224 devm_ioport_map_match, (__force void *)addr));
225}
226EXPORT_SYMBOL(devm_ioport_unmap);
227#endif /* CONFIG_HAS_IOPORT_MAP */
228
229#ifdef CONFIG_PCI
230/*
231 * PCI iomap devres
232 */
233#define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
234
235struct pcim_iomap_devres {
236 void __iomem *table[PCIM_IOMAP_MAX];
237};
238
239static void pcim_iomap_release(struct device *gendev, void *res)
240{
241 struct pci_dev *dev = to_pci_dev(gendev);
242 struct pcim_iomap_devres *this = res;
243 int i;
244
245 for (i = 0; i < PCIM_IOMAP_MAX; i++)
246 if (this->table[i])
247 pci_iounmap(dev, this->table[i]);
248}
249
250/**
251 * pcim_iomap_table - access iomap allocation table
252 * @pdev: PCI device to access iomap table for
253 *
254 * Access iomap allocation table for @dev. If iomap table doesn't
255 * exist and @pdev is managed, it will be allocated. All iomaps
256 * recorded in the iomap table are automatically unmapped on driver
257 * detach.
258 *
259 * This function might sleep when the table is first allocated but can
260 * be safely called without context and guaranteed to succed once
261 * allocated.
262 */
263void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
264{
265 struct pcim_iomap_devres *dr, *new_dr;
266
267 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
268 if (dr)
269 return dr->table;
270
271 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
272 if (!new_dr)
273 return NULL;
274 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
275 return dr->table;
276}
277EXPORT_SYMBOL(pcim_iomap_table);
278
279/**
280 * pcim_iomap - Managed pcim_iomap()
281 * @pdev: PCI device to iomap for
282 * @bar: BAR to iomap
283 * @maxlen: Maximum length of iomap
284 *
285 * Managed pci_iomap(). Map is automatically unmapped on driver
286 * detach.
287 */
288void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
289{
290 void __iomem **tbl;
291
292 BUG_ON(bar >= PCIM_IOMAP_MAX);
293
294 tbl = (void __iomem **)pcim_iomap_table(pdev);
295 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
296 return NULL;
297
298 tbl[bar] = pci_iomap(pdev, bar, maxlen);
299 return tbl[bar];
300}
301EXPORT_SYMBOL(pcim_iomap);
302
303/**
304 * pcim_iounmap - Managed pci_iounmap()
305 * @pdev: PCI device to iounmap for
306 * @addr: Address to unmap
307 *
308 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
309 */
310void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
311{
312 void __iomem **tbl;
313 int i;
314
315 pci_iounmap(pdev, addr);
316
317 tbl = (void __iomem **)pcim_iomap_table(pdev);
318 BUG_ON(!tbl);
319
320 for (i = 0; i < PCIM_IOMAP_MAX; i++)
321 if (tbl[i] == addr) {
322 tbl[i] = NULL;
323 return;
324 }
325 WARN_ON(1);
326}
327EXPORT_SYMBOL(pcim_iounmap);
328
329/**
330 * pcim_iomap_regions - Request and iomap PCI BARs
331 * @pdev: PCI device to map IO resources for
332 * @mask: Mask of BARs to request and iomap
333 * @name: Name used when requesting regions
334 *
335 * Request and iomap regions specified by @mask.
336 */
337int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
338{
339 void __iomem * const *iomap;
340 int i, rc;
341
342 iomap = pcim_iomap_table(pdev);
343 if (!iomap)
344 return -ENOMEM;
345
346 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
347 unsigned long len;
348
349 if (!(mask & (1 << i)))
350 continue;
351
352 rc = -EINVAL;
353 len = pci_resource_len(pdev, i);
354 if (!len)
355 goto err_inval;
356
357 rc = pci_request_region(pdev, i, name);
358 if (rc)
359 goto err_inval;
360
361 rc = -ENOMEM;
362 if (!pcim_iomap(pdev, i, 0))
363 goto err_region;
364 }
365
366 return 0;
367
368 err_region:
369 pci_release_region(pdev, i);
370 err_inval:
371 while (--i >= 0) {
372 if (!(mask & (1 << i)))
373 continue;
374 pcim_iounmap(pdev, iomap[i]);
375 pci_release_region(pdev, i);
376 }
377
378 return rc;
379}
380EXPORT_SYMBOL(pcim_iomap_regions);
381
382/**
383 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
384 * @pdev: PCI device to map IO resources for
385 * @mask: Mask of BARs to iomap
386 * @name: Name used when requesting regions
387 *
388 * Request all PCI BARs and iomap regions specified by @mask.
389 */
390int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
391 const char *name)
392{
393 int request_mask = ((1 << 6) - 1) & ~mask;
394 int rc;
395
396 rc = pci_request_selected_regions(pdev, request_mask, name);
397 if (rc)
398 return rc;
399
400 rc = pcim_iomap_regions(pdev, mask, name);
401 if (rc)
402 pci_release_selected_regions(pdev, request_mask);
403 return rc;
404}
405EXPORT_SYMBOL(pcim_iomap_regions_request_all);
406
407/**
408 * pcim_iounmap_regions - Unmap and release PCI BARs
409 * @pdev: PCI device to map IO resources for
410 * @mask: Mask of BARs to unmap and release
411 *
412 * Unmap and release regions specified by @mask.
413 */
414void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
415{
416 void __iomem * const *iomap;
417 int i;
418
419 iomap = pcim_iomap_table(pdev);
420 if (!iomap)
421 return;
422
423 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
424 if (!(mask & (1 << i)))
425 continue;
426
427 pcim_iounmap(pdev, iomap[i]);
428 pci_release_region(pdev, i);
429 }
430}
431EXPORT_SYMBOL(pcim_iounmap_regions);
432#endif /* CONFIG_PCI */