blob: f56070cf970b6a4ff9c760e598dceec489f2ec59 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thierry Redingf4a18312013-01-22 22:24:46 +01002#include <linux/err.h>
Al Viro5ea81762007-02-11 15:41:31 +00003#include <linux/pci.h>
4#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/gfp.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05006#include <linux/export.h>
Benjamin Herrenschmidtd5e83822018-06-05 13:21:26 +10007#include <linux/of_address.h>
Al Viro5ea81762007-02-11 15:41:31 +00008
Yisheng Xie1b723412018-01-29 19:48:16 +08009enum devm_ioremap_type {
10 DEVM_IOREMAP = 0,
11 DEVM_IOREMAP_NC,
Tuowen Zhaoe537654b2019-10-16 15:06:28 -060012 DEVM_IOREMAP_UC,
Yisheng Xie1b723412018-01-29 19:48:16 +080013 DEVM_IOREMAP_WC,
14};
15
Emil Medveb41e5ff2008-05-03 06:34:04 +100016void devm_ioremap_release(struct device *dev, void *res)
Al Viro5ea81762007-02-11 15:41:31 +000017{
18 iounmap(*(void __iomem **)res);
19}
20
21static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
22{
23 return *(void **)res == match_data;
24}
25
Yisheng Xie1b723412018-01-29 19:48:16 +080026static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
27 resource_size_t size,
28 enum devm_ioremap_type type)
29{
30 void __iomem **ptr, *addr = NULL;
31
32 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
33 if (!ptr)
34 return NULL;
35
36 switch (type) {
37 case DEVM_IOREMAP:
38 addr = ioremap(offset, size);
39 break;
40 case DEVM_IOREMAP_NC:
41 addr = ioremap_nocache(offset, size);
42 break;
Tuowen Zhaoe537654b2019-10-16 15:06:28 -060043 case DEVM_IOREMAP_UC:
44 addr = ioremap_uc(offset, size);
45 break;
Yisheng Xie1b723412018-01-29 19:48:16 +080046 case DEVM_IOREMAP_WC:
47 addr = ioremap_wc(offset, size);
48 break;
49 }
50
51 if (addr) {
52 *ptr = addr;
53 devres_add(dev, ptr);
54 } else
55 devres_free(ptr);
56
57 return addr;
58}
59
Al Viro5ea81762007-02-11 15:41:31 +000060/**
61 * devm_ioremap - Managed ioremap()
62 * @dev: Generic device to remap IO address for
Lorenzo Pieralisi65247542017-04-19 17:48:54 +010063 * @offset: Resource address to map
Al Viro5ea81762007-02-11 15:41:31 +000064 * @size: Size of map
65 *
66 * Managed ioremap(). Map is automatically unmapped on driver detach.
67 */
Kumar Gala4f452e82008-04-29 10:25:48 -050068void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
Cristian Stoica5559b7b2014-10-07 18:25:43 +030069 resource_size_t size)
Al Viro5ea81762007-02-11 15:41:31 +000070{
Yisheng Xie1b723412018-01-29 19:48:16 +080071 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
Al Viro5ea81762007-02-11 15:41:31 +000072}
73EXPORT_SYMBOL(devm_ioremap);
74
75/**
Tuowen Zhaoe537654b2019-10-16 15:06:28 -060076 * devm_ioremap_uc - Managed ioremap_uc()
77 * @dev: Generic device to remap IO address for
78 * @offset: Resource address to map
79 * @size: Size of map
80 *
81 * Managed ioremap_uc(). Map is automatically unmapped on driver detach.
82 */
83void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
84 resource_size_t size)
85{
86 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_UC);
87}
88EXPORT_SYMBOL_GPL(devm_ioremap_uc);
89
90/**
Al Viro5ea81762007-02-11 15:41:31 +000091 * devm_ioremap_nocache - Managed ioremap_nocache()
92 * @dev: Generic device to remap IO address for
Lorenzo Pieralisi65247542017-04-19 17:48:54 +010093 * @offset: Resource address to map
Al Viro5ea81762007-02-11 15:41:31 +000094 * @size: Size of map
95 *
96 * Managed ioremap_nocache(). Map is automatically unmapped on driver
97 * detach.
98 */
Kumar Gala4f452e82008-04-29 10:25:48 -050099void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
Cristian Stoica5559b7b2014-10-07 18:25:43 +0300100 resource_size_t size)
Al Viro5ea81762007-02-11 15:41:31 +0000101{
Yisheng Xie1b723412018-01-29 19:48:16 +0800102 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NC);
Al Viro5ea81762007-02-11 15:41:31 +0000103}
104EXPORT_SYMBOL(devm_ioremap_nocache);
105
106/**
Abhilash Kesavan34644522015-02-06 19:15:27 +0530107 * devm_ioremap_wc - Managed ioremap_wc()
108 * @dev: Generic device to remap IO address for
Lorenzo Pieralisi65247542017-04-19 17:48:54 +0100109 * @offset: Resource address to map
Abhilash Kesavan34644522015-02-06 19:15:27 +0530110 * @size: Size of map
111 *
112 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
113 */
114void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
115 resource_size_t size)
116{
Yisheng Xie1b723412018-01-29 19:48:16 +0800117 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
Abhilash Kesavan34644522015-02-06 19:15:27 +0530118}
119EXPORT_SYMBOL(devm_ioremap_wc);
120
121/**
Al Viro5ea81762007-02-11 15:41:31 +0000122 * devm_iounmap - Managed iounmap()
123 * @dev: Generic device to unmap for
124 * @addr: Address to unmap
125 *
126 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
127 */
128void devm_iounmap(struct device *dev, void __iomem *addr)
129{
Al Viro5ea81762007-02-11 15:41:31 +0000130 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
Steven Rostedtb104d6a2014-04-03 14:49:07 -0700131 (__force void *)addr));
Maxin B Johnae891a12011-07-25 17:12:59 -0700132 iounmap(addr);
Al Viro5ea81762007-02-11 15:41:31 +0000133}
134EXPORT_SYMBOL(devm_iounmap);
135
Bartosz Golaszewski6e924822019-10-22 10:43:12 +0200136static void __iomem *
137__devm_ioremap_resource(struct device *dev, const struct resource *res,
138 enum devm_ioremap_type type)
139{
140 resource_size_t size;
141 void __iomem *dest_ptr;
142
143 BUG_ON(!dev);
144
145 if (!res || resource_type(res) != IORESOURCE_MEM) {
146 dev_err(dev, "invalid resource\n");
147 return IOMEM_ERR_PTR(-EINVAL);
148 }
149
150 size = resource_size(res);
151
152 if (!devm_request_mem_region(dev, res->start, size, dev_name(dev))) {
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, type);
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}
166
Wolfram Sang72f8c0b2011-10-25 15:16:47 +0200167/**
Thierry Reding75096572013-01-21 11:08:54 +0100168 * devm_ioremap_resource() - check, request region, and ioremap resource
169 * @dev: generic device to handle the resource for
170 * @res: resource to be handled
171 *
Dan Williams92b19ff2015-08-10 23:07:06 -0400172 * Checks that a resource is a valid memory region, requests the memory
173 * region and ioremaps it. All operations are managed and will be undone
174 * on driver detach.
Thierry Reding75096572013-01-21 11:08:54 +0100175 *
176 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
177 * on failure. Usage example:
178 *
179 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 * base = devm_ioremap_resource(&pdev->dev, res);
181 * if (IS_ERR(base))
182 * return PTR_ERR(base);
183 */
Arnd Bergmanneef778c2019-07-04 15:14:45 -0700184void __iomem *devm_ioremap_resource(struct device *dev,
185 const struct resource *res)
Thierry Reding75096572013-01-21 11:08:54 +0100186{
Bartosz Golaszewski6e924822019-10-22 10:43:12 +0200187 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP);
Thierry Reding75096572013-01-21 11:08:54 +0100188}
189EXPORT_SYMBOL(devm_ioremap_resource);
190
Bartosz Golaszewskib873af62019-10-22 10:43:13 +0200191/**
192 * devm_ioremap_resource_wc() - write-combined variant of
193 * devm_ioremap_resource()
194 * @dev: generic device to handle the resource for
195 * @res: resource to be handled
196 *
197 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
198 * on failure. Usage example:
199 */
200void __iomem *devm_ioremap_resource_wc(struct device *dev,
201 const struct resource *res)
202{
203 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP_WC);
204}
205
Benjamin Herrenschmidtd5e83822018-06-05 13:21:26 +1000206/*
207 * devm_of_iomap - Requests a resource and maps the memory mapped IO
208 * for a given device_node managed by a given device
209 *
210 * Checks that a resource is a valid memory region, requests the memory
211 * region and ioremaps it. All operations are managed and will be undone
212 * on driver detach of the device.
213 *
214 * This is to be used when a device requests/maps resources described
215 * by other device tree nodes (children or otherwise).
216 *
217 * @dev: The device "managing" the resource
218 * @node: The device-tree node where the resource resides
219 * @index: index of the MMIO range in the "reg" property
220 * @size: Returns the size of the resource (pass NULL if not needed)
221 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
222 * error code on failure. Usage example:
223 *
224 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
225 * if (IS_ERR(base))
226 * return PTR_ERR(base);
227 */
228void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
229 resource_size_t *size)
230{
231 struct resource res;
232
233 if (of_address_to_resource(node, index, &res))
234 return IOMEM_ERR_PTR(-EINVAL);
235 if (size)
236 *size = resource_size(&res);
237 return devm_ioremap_resource(dev, &res);
238}
239EXPORT_SYMBOL(devm_of_iomap);
240
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700241#ifdef CONFIG_HAS_IOPORT_MAP
Al Viro5ea81762007-02-11 15:41:31 +0000242/*
243 * Generic iomap devres
244 */
245static void devm_ioport_map_release(struct device *dev, void *res)
246{
247 ioport_unmap(*(void __iomem **)res);
248}
249
250static int devm_ioport_map_match(struct device *dev, void *res,
251 void *match_data)
252{
253 return *(void **)res == match_data;
254}
255
256/**
257 * devm_ioport_map - Managed ioport_map()
258 * @dev: Generic device to map ioport for
259 * @port: Port to map
260 * @nr: Number of ports to map
261 *
262 * Managed ioport_map(). Map is automatically unmapped on driver
263 * detach.
264 */
Fabian Frederick5cbb00c2014-05-23 22:30:50 +0200265void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
Al Viro5ea81762007-02-11 15:41:31 +0000266 unsigned int nr)
267{
268 void __iomem **ptr, *addr;
269
270 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
271 if (!ptr)
272 return NULL;
273
274 addr = ioport_map(port, nr);
275 if (addr) {
276 *ptr = addr;
277 devres_add(dev, ptr);
278 } else
279 devres_free(ptr);
280
281 return addr;
282}
283EXPORT_SYMBOL(devm_ioport_map);
284
285/**
286 * devm_ioport_unmap - Managed ioport_unmap()
287 * @dev: Generic device to unmap for
288 * @addr: Address to unmap
289 *
290 * Managed ioport_unmap(). @addr must have been mapped using
291 * devm_ioport_map().
292 */
293void devm_ioport_unmap(struct device *dev, void __iomem *addr)
294{
295 ioport_unmap(addr);
296 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
Steven Rostedtb104d6a2014-04-03 14:49:07 -0700297 devm_ioport_map_match, (__force void *)addr));
Al Viro5ea81762007-02-11 15:41:31 +0000298}
299EXPORT_SYMBOL(devm_ioport_unmap);
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700300#endif /* CONFIG_HAS_IOPORT_MAP */
Al Viro5ea81762007-02-11 15:41:31 +0000301
302#ifdef CONFIG_PCI
303/*
304 * PCI iomap devres
305 */
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300306#define PCIM_IOMAP_MAX PCI_STD_NUM_BARS
Al Viro5ea81762007-02-11 15:41:31 +0000307
308struct pcim_iomap_devres {
309 void __iomem *table[PCIM_IOMAP_MAX];
310};
311
312static void pcim_iomap_release(struct device *gendev, void *res)
313{
Geliang Tang20af74e2015-12-27 18:46:05 +0800314 struct pci_dev *dev = to_pci_dev(gendev);
Al Viro5ea81762007-02-11 15:41:31 +0000315 struct pcim_iomap_devres *this = res;
316 int i;
317
318 for (i = 0; i < PCIM_IOMAP_MAX; i++)
319 if (this->table[i])
320 pci_iounmap(dev, this->table[i]);
321}
322
323/**
324 * pcim_iomap_table - access iomap allocation table
325 * @pdev: PCI device to access iomap table for
326 *
327 * Access iomap allocation table for @dev. If iomap table doesn't
328 * exist and @pdev is managed, it will be allocated. All iomaps
329 * recorded in the iomap table are automatically unmapped on driver
330 * detach.
331 *
332 * This function might sleep when the table is first allocated but can
333 * be safely called without context and guaranteed to succed once
334 * allocated.
335 */
Fabian Frederick5cbb00c2014-05-23 22:30:50 +0200336void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
Al Viro5ea81762007-02-11 15:41:31 +0000337{
338 struct pcim_iomap_devres *dr, *new_dr;
339
340 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
341 if (dr)
342 return dr->table;
343
344 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
345 if (!new_dr)
346 return NULL;
347 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
348 return dr->table;
349}
350EXPORT_SYMBOL(pcim_iomap_table);
351
352/**
353 * pcim_iomap - Managed pcim_iomap()
354 * @pdev: PCI device to iomap for
355 * @bar: BAR to iomap
356 * @maxlen: Maximum length of iomap
357 *
358 * Managed pci_iomap(). Map is automatically unmapped on driver
359 * detach.
360 */
Fabian Frederick5cbb00c2014-05-23 22:30:50 +0200361void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
Al Viro5ea81762007-02-11 15:41:31 +0000362{
363 void __iomem **tbl;
364
365 BUG_ON(bar >= PCIM_IOMAP_MAX);
366
367 tbl = (void __iomem **)pcim_iomap_table(pdev);
368 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
369 return NULL;
370
371 tbl[bar] = pci_iomap(pdev, bar, maxlen);
372 return tbl[bar];
373}
374EXPORT_SYMBOL(pcim_iomap);
375
376/**
377 * pcim_iounmap - Managed pci_iounmap()
378 * @pdev: PCI device to iounmap for
379 * @addr: Address to unmap
380 *
381 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
382 */
383void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
384{
385 void __iomem **tbl;
386 int i;
387
388 pci_iounmap(pdev, addr);
389
390 tbl = (void __iomem **)pcim_iomap_table(pdev);
391 BUG_ON(!tbl);
392
393 for (i = 0; i < PCIM_IOMAP_MAX; i++)
394 if (tbl[i] == addr) {
395 tbl[i] = NULL;
396 return;
397 }
398 WARN_ON(1);
399}
400EXPORT_SYMBOL(pcim_iounmap);
401
402/**
403 * pcim_iomap_regions - Request and iomap PCI BARs
404 * @pdev: PCI device to map IO resources for
405 * @mask: Mask of BARs to request and iomap
406 * @name: Name used when requesting regions
407 *
408 * Request and iomap regions specified by @mask.
409 */
Yinghai Lufb7ebfe2012-01-04 15:50:02 -0800410int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
Al Viro5ea81762007-02-11 15:41:31 +0000411{
412 void __iomem * const *iomap;
413 int i, rc;
414
415 iomap = pcim_iomap_table(pdev);
416 if (!iomap)
417 return -ENOMEM;
418
419 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
420 unsigned long len;
421
422 if (!(mask & (1 << i)))
423 continue;
424
425 rc = -EINVAL;
426 len = pci_resource_len(pdev, i);
427 if (!len)
428 goto err_inval;
429
430 rc = pci_request_region(pdev, i, name);
431 if (rc)
Frederik Deweerdtfb4d64e2007-02-16 01:27:15 -0800432 goto err_inval;
Al Viro5ea81762007-02-11 15:41:31 +0000433
434 rc = -ENOMEM;
435 if (!pcim_iomap(pdev, i, 0))
Frederik Deweerdtfb4d64e2007-02-16 01:27:15 -0800436 goto err_region;
Al Viro5ea81762007-02-11 15:41:31 +0000437 }
438
439 return 0;
440
Al Viro5ea81762007-02-11 15:41:31 +0000441 err_region:
442 pci_release_region(pdev, i);
443 err_inval:
444 while (--i >= 0) {
Frederik Deweerdtfb4d64e2007-02-16 01:27:15 -0800445 if (!(mask & (1 << i)))
446 continue;
Al Viro5ea81762007-02-11 15:41:31 +0000447 pcim_iounmap(pdev, iomap[i]);
448 pci_release_region(pdev, i);
449 }
450
451 return rc;
452}
453EXPORT_SYMBOL(pcim_iomap_regions);
Tejun Heoec04b072007-03-09 19:45:58 +0900454
455/**
Tejun Heo916fbfb2008-03-12 15:26:34 +0900456 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
457 * @pdev: PCI device to map IO resources for
458 * @mask: Mask of BARs to iomap
459 * @name: Name used when requesting regions
460 *
461 * Request all PCI BARs and iomap regions specified by @mask.
462 */
Yinghai Lufb7ebfe2012-01-04 15:50:02 -0800463int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
Tejun Heo916fbfb2008-03-12 15:26:34 +0900464 const char *name)
465{
466 int request_mask = ((1 << 6) - 1) & ~mask;
467 int rc;
468
469 rc = pci_request_selected_regions(pdev, request_mask, name);
470 if (rc)
471 return rc;
472
473 rc = pcim_iomap_regions(pdev, mask, name);
474 if (rc)
475 pci_release_selected_regions(pdev, request_mask);
476 return rc;
477}
478EXPORT_SYMBOL(pcim_iomap_regions_request_all);
479
480/**
Tejun Heoec04b072007-03-09 19:45:58 +0900481 * pcim_iounmap_regions - Unmap and release PCI BARs
482 * @pdev: PCI device to map IO resources for
483 * @mask: Mask of BARs to unmap and release
484 *
Kulikov Vasiliy4d45ada2010-07-10 14:07:41 +0400485 * Unmap and release regions specified by @mask.
Tejun Heoec04b072007-03-09 19:45:58 +0900486 */
Yinghai Lufb7ebfe2012-01-04 15:50:02 -0800487void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
Tejun Heoec04b072007-03-09 19:45:58 +0900488{
489 void __iomem * const *iomap;
490 int i;
491
492 iomap = pcim_iomap_table(pdev);
493 if (!iomap)
494 return;
495
Dan Carpenter1f35d042015-09-21 19:21:51 +0300496 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
Tejun Heoec04b072007-03-09 19:45:58 +0900497 if (!(mask & (1 << i)))
498 continue;
499
500 pcim_iounmap(pdev, iomap[i]);
501 pci_release_region(pdev, i);
502 }
503}
504EXPORT_SYMBOL(pcim_iounmap_regions);
Wolfram Sang571806a2011-10-25 15:03:42 +0200505#endif /* CONFIG_PCI */