blob: a14c727128c1c176c1c1d032fd4a9e66b855bafc [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,
12 DEVM_IOREMAP_WC,
13};
14
Emil Medveb41e5ff2008-05-03 06:34:04 +100015void devm_ioremap_release(struct device *dev, void *res)
Al Viro5ea81762007-02-11 15:41:31 +000016{
17 iounmap(*(void __iomem **)res);
18}
19
20static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
21{
22 return *(void **)res == match_data;
23}
24
Yisheng Xie1b723412018-01-29 19:48:16 +080025static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
26 resource_size_t size,
27 enum devm_ioremap_type type)
28{
29 void __iomem **ptr, *addr = NULL;
30
31 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
32 if (!ptr)
33 return NULL;
34
35 switch (type) {
36 case DEVM_IOREMAP:
37 addr = ioremap(offset, size);
38 break;
39 case DEVM_IOREMAP_NC:
40 addr = ioremap_nocache(offset, size);
41 break;
42 case DEVM_IOREMAP_WC:
43 addr = ioremap_wc(offset, size);
44 break;
45 }
46
47 if (addr) {
48 *ptr = addr;
49 devres_add(dev, ptr);
50 } else
51 devres_free(ptr);
52
53 return addr;
54}
55
Al Viro5ea81762007-02-11 15:41:31 +000056/**
57 * devm_ioremap - Managed ioremap()
58 * @dev: Generic device to remap IO address for
Lorenzo Pieralisi65247542017-04-19 17:48:54 +010059 * @offset: Resource address to map
Al Viro5ea81762007-02-11 15:41:31 +000060 * @size: Size of map
61 *
62 * Managed ioremap(). Map is automatically unmapped on driver detach.
63 */
Kumar Gala4f452e82008-04-29 10:25:48 -050064void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
Cristian Stoica5559b7b2014-10-07 18:25:43 +030065 resource_size_t size)
Al Viro5ea81762007-02-11 15:41:31 +000066{
Yisheng Xie1b723412018-01-29 19:48:16 +080067 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
Al Viro5ea81762007-02-11 15:41:31 +000068}
69EXPORT_SYMBOL(devm_ioremap);
70
71/**
72 * devm_ioremap_nocache - Managed ioremap_nocache()
73 * @dev: Generic device to remap IO address for
Lorenzo Pieralisi65247542017-04-19 17:48:54 +010074 * @offset: Resource address to map
Al Viro5ea81762007-02-11 15:41:31 +000075 * @size: Size of map
76 *
77 * Managed ioremap_nocache(). Map is automatically unmapped on driver
78 * detach.
79 */
Kumar Gala4f452e82008-04-29 10:25:48 -050080void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
Cristian Stoica5559b7b2014-10-07 18:25:43 +030081 resource_size_t size)
Al Viro5ea81762007-02-11 15:41:31 +000082{
Yisheng Xie1b723412018-01-29 19:48:16 +080083 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NC);
Al Viro5ea81762007-02-11 15:41:31 +000084}
85EXPORT_SYMBOL(devm_ioremap_nocache);
86
87/**
Abhilash Kesavan34644522015-02-06 19:15:27 +053088 * devm_ioremap_wc - Managed ioremap_wc()
89 * @dev: Generic device to remap IO address for
Lorenzo Pieralisi65247542017-04-19 17:48:54 +010090 * @offset: Resource address to map
Abhilash Kesavan34644522015-02-06 19:15:27 +053091 * @size: Size of map
92 *
93 * Managed ioremap_wc(). Map is automatically unmapped on driver detach.
94 */
95void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
96 resource_size_t size)
97{
Yisheng Xie1b723412018-01-29 19:48:16 +080098 return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
Abhilash Kesavan34644522015-02-06 19:15:27 +053099}
100EXPORT_SYMBOL(devm_ioremap_wc);
101
102/**
Al Viro5ea81762007-02-11 15:41:31 +0000103 * devm_iounmap - Managed iounmap()
104 * @dev: Generic device to unmap for
105 * @addr: Address to unmap
106 *
107 * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
108 */
109void devm_iounmap(struct device *dev, void __iomem *addr)
110{
Al Viro5ea81762007-02-11 15:41:31 +0000111 WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
Steven Rostedtb104d6a2014-04-03 14:49:07 -0700112 (__force void *)addr));
Maxin B Johnae891a12011-07-25 17:12:59 -0700113 iounmap(addr);
Al Viro5ea81762007-02-11 15:41:31 +0000114}
115EXPORT_SYMBOL(devm_iounmap);
116
Bartosz Golaszewski6e924822019-10-22 10:43:12 +0200117static void __iomem *
118__devm_ioremap_resource(struct device *dev, const struct resource *res,
119 enum devm_ioremap_type type)
120{
121 resource_size_t size;
122 void __iomem *dest_ptr;
123
124 BUG_ON(!dev);
125
126 if (!res || resource_type(res) != IORESOURCE_MEM) {
127 dev_err(dev, "invalid resource\n");
128 return IOMEM_ERR_PTR(-EINVAL);
129 }
130
131 size = resource_size(res);
132
133 if (!devm_request_mem_region(dev, res->start, size, dev_name(dev))) {
134 dev_err(dev, "can't request region for resource %pR\n", res);
135 return IOMEM_ERR_PTR(-EBUSY);
136 }
137
138 dest_ptr = __devm_ioremap(dev, res->start, size, type);
139 if (!dest_ptr) {
140 dev_err(dev, "ioremap failed for resource %pR\n", res);
141 devm_release_mem_region(dev, res->start, size);
142 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
143 }
144
145 return dest_ptr;
146}
147
Wolfram Sang72f8c0b2011-10-25 15:16:47 +0200148/**
Thierry Reding75096572013-01-21 11:08:54 +0100149 * devm_ioremap_resource() - check, request region, and ioremap resource
150 * @dev: generic device to handle the resource for
151 * @res: resource to be handled
152 *
Dan Williams92b19ff2015-08-10 23:07:06 -0400153 * Checks that a resource is a valid memory region, requests the memory
154 * region and ioremaps it. All operations are managed and will be undone
155 * on driver detach.
Thierry Reding75096572013-01-21 11:08:54 +0100156 *
157 * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
158 * on failure. Usage example:
159 *
160 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
161 * base = devm_ioremap_resource(&pdev->dev, res);
162 * if (IS_ERR(base))
163 * return PTR_ERR(base);
164 */
Arnd Bergmanneef778c2019-07-04 15:14:45 -0700165void __iomem *devm_ioremap_resource(struct device *dev,
166 const struct resource *res)
Thierry Reding75096572013-01-21 11:08:54 +0100167{
Bartosz Golaszewski6e924822019-10-22 10:43:12 +0200168 return __devm_ioremap_resource(dev, res, DEVM_IOREMAP);
Thierry Reding75096572013-01-21 11:08:54 +0100169}
170EXPORT_SYMBOL(devm_ioremap_resource);
171
Benjamin Herrenschmidtd5e83822018-06-05 13:21:26 +1000172/*
173 * devm_of_iomap - Requests a resource and maps the memory mapped IO
174 * for a given device_node managed by a given device
175 *
176 * Checks that a resource is a valid memory region, requests the memory
177 * region and ioremaps it. All operations are managed and will be undone
178 * on driver detach of the device.
179 *
180 * This is to be used when a device requests/maps resources described
181 * by other device tree nodes (children or otherwise).
182 *
183 * @dev: The device "managing" the resource
184 * @node: The device-tree node where the resource resides
185 * @index: index of the MMIO range in the "reg" property
186 * @size: Returns the size of the resource (pass NULL if not needed)
187 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
188 * error code on failure. Usage example:
189 *
190 * base = devm_of_iomap(&pdev->dev, node, 0, NULL);
191 * if (IS_ERR(base))
192 * return PTR_ERR(base);
193 */
194void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
195 resource_size_t *size)
196{
197 struct resource res;
198
199 if (of_address_to_resource(node, index, &res))
200 return IOMEM_ERR_PTR(-EINVAL);
201 if (size)
202 *size = resource_size(&res);
203 return devm_ioremap_resource(dev, &res);
204}
205EXPORT_SYMBOL(devm_of_iomap);
206
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700207#ifdef CONFIG_HAS_IOPORT_MAP
Al Viro5ea81762007-02-11 15:41:31 +0000208/*
209 * Generic iomap devres
210 */
211static void devm_ioport_map_release(struct device *dev, void *res)
212{
213 ioport_unmap(*(void __iomem **)res);
214}
215
216static int devm_ioport_map_match(struct device *dev, void *res,
217 void *match_data)
218{
219 return *(void **)res == match_data;
220}
221
222/**
223 * devm_ioport_map - Managed ioport_map()
224 * @dev: Generic device to map ioport for
225 * @port: Port to map
226 * @nr: Number of ports to map
227 *
228 * Managed ioport_map(). Map is automatically unmapped on driver
229 * detach.
230 */
Fabian Frederick5cbb00c2014-05-23 22:30:50 +0200231void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
Al Viro5ea81762007-02-11 15:41:31 +0000232 unsigned int nr)
233{
234 void __iomem **ptr, *addr;
235
236 ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
237 if (!ptr)
238 return NULL;
239
240 addr = ioport_map(port, nr);
241 if (addr) {
242 *ptr = addr;
243 devres_add(dev, ptr);
244 } else
245 devres_free(ptr);
246
247 return addr;
248}
249EXPORT_SYMBOL(devm_ioport_map);
250
251/**
252 * devm_ioport_unmap - Managed ioport_unmap()
253 * @dev: Generic device to unmap for
254 * @addr: Address to unmap
255 *
256 * Managed ioport_unmap(). @addr must have been mapped using
257 * devm_ioport_map().
258 */
259void devm_ioport_unmap(struct device *dev, void __iomem *addr)
260{
261 ioport_unmap(addr);
262 WARN_ON(devres_destroy(dev, devm_ioport_map_release,
Steven Rostedtb104d6a2014-04-03 14:49:07 -0700263 devm_ioport_map_match, (__force void *)addr));
Al Viro5ea81762007-02-11 15:41:31 +0000264}
265EXPORT_SYMBOL(devm_ioport_unmap);
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700266#endif /* CONFIG_HAS_IOPORT_MAP */
Al Viro5ea81762007-02-11 15:41:31 +0000267
268#ifdef CONFIG_PCI
269/*
270 * PCI iomap devres
271 */
272#define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
273
274struct pcim_iomap_devres {
275 void __iomem *table[PCIM_IOMAP_MAX];
276};
277
278static void pcim_iomap_release(struct device *gendev, void *res)
279{
Geliang Tang20af74e2015-12-27 18:46:05 +0800280 struct pci_dev *dev = to_pci_dev(gendev);
Al Viro5ea81762007-02-11 15:41:31 +0000281 struct pcim_iomap_devres *this = res;
282 int i;
283
284 for (i = 0; i < PCIM_IOMAP_MAX; i++)
285 if (this->table[i])
286 pci_iounmap(dev, this->table[i]);
287}
288
289/**
290 * pcim_iomap_table - access iomap allocation table
291 * @pdev: PCI device to access iomap table for
292 *
293 * Access iomap allocation table for @dev. If iomap table doesn't
294 * exist and @pdev is managed, it will be allocated. All iomaps
295 * recorded in the iomap table are automatically unmapped on driver
296 * detach.
297 *
298 * This function might sleep when the table is first allocated but can
299 * be safely called without context and guaranteed to succed once
300 * allocated.
301 */
Fabian Frederick5cbb00c2014-05-23 22:30:50 +0200302void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
Al Viro5ea81762007-02-11 15:41:31 +0000303{
304 struct pcim_iomap_devres *dr, *new_dr;
305
306 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
307 if (dr)
308 return dr->table;
309
310 new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
311 if (!new_dr)
312 return NULL;
313 dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
314 return dr->table;
315}
316EXPORT_SYMBOL(pcim_iomap_table);
317
318/**
319 * pcim_iomap - Managed pcim_iomap()
320 * @pdev: PCI device to iomap for
321 * @bar: BAR to iomap
322 * @maxlen: Maximum length of iomap
323 *
324 * Managed pci_iomap(). Map is automatically unmapped on driver
325 * detach.
326 */
Fabian Frederick5cbb00c2014-05-23 22:30:50 +0200327void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
Al Viro5ea81762007-02-11 15:41:31 +0000328{
329 void __iomem **tbl;
330
331 BUG_ON(bar >= PCIM_IOMAP_MAX);
332
333 tbl = (void __iomem **)pcim_iomap_table(pdev);
334 if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
335 return NULL;
336
337 tbl[bar] = pci_iomap(pdev, bar, maxlen);
338 return tbl[bar];
339}
340EXPORT_SYMBOL(pcim_iomap);
341
342/**
343 * pcim_iounmap - Managed pci_iounmap()
344 * @pdev: PCI device to iounmap for
345 * @addr: Address to unmap
346 *
347 * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
348 */
349void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
350{
351 void __iomem **tbl;
352 int i;
353
354 pci_iounmap(pdev, addr);
355
356 tbl = (void __iomem **)pcim_iomap_table(pdev);
357 BUG_ON(!tbl);
358
359 for (i = 0; i < PCIM_IOMAP_MAX; i++)
360 if (tbl[i] == addr) {
361 tbl[i] = NULL;
362 return;
363 }
364 WARN_ON(1);
365}
366EXPORT_SYMBOL(pcim_iounmap);
367
368/**
369 * pcim_iomap_regions - Request and iomap PCI BARs
370 * @pdev: PCI device to map IO resources for
371 * @mask: Mask of BARs to request and iomap
372 * @name: Name used when requesting regions
373 *
374 * Request and iomap regions specified by @mask.
375 */
Yinghai Lufb7ebfe2012-01-04 15:50:02 -0800376int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
Al Viro5ea81762007-02-11 15:41:31 +0000377{
378 void __iomem * const *iomap;
379 int i, rc;
380
381 iomap = pcim_iomap_table(pdev);
382 if (!iomap)
383 return -ENOMEM;
384
385 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
386 unsigned long len;
387
388 if (!(mask & (1 << i)))
389 continue;
390
391 rc = -EINVAL;
392 len = pci_resource_len(pdev, i);
393 if (!len)
394 goto err_inval;
395
396 rc = pci_request_region(pdev, i, name);
397 if (rc)
Frederik Deweerdtfb4d64e2007-02-16 01:27:15 -0800398 goto err_inval;
Al Viro5ea81762007-02-11 15:41:31 +0000399
400 rc = -ENOMEM;
401 if (!pcim_iomap(pdev, i, 0))
Frederik Deweerdtfb4d64e2007-02-16 01:27:15 -0800402 goto err_region;
Al Viro5ea81762007-02-11 15:41:31 +0000403 }
404
405 return 0;
406
Al Viro5ea81762007-02-11 15:41:31 +0000407 err_region:
408 pci_release_region(pdev, i);
409 err_inval:
410 while (--i >= 0) {
Frederik Deweerdtfb4d64e2007-02-16 01:27:15 -0800411 if (!(mask & (1 << i)))
412 continue;
Al Viro5ea81762007-02-11 15:41:31 +0000413 pcim_iounmap(pdev, iomap[i]);
414 pci_release_region(pdev, i);
415 }
416
417 return rc;
418}
419EXPORT_SYMBOL(pcim_iomap_regions);
Tejun Heoec04b072007-03-09 19:45:58 +0900420
421/**
Tejun Heo916fbfb2008-03-12 15:26:34 +0900422 * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
423 * @pdev: PCI device to map IO resources for
424 * @mask: Mask of BARs to iomap
425 * @name: Name used when requesting regions
426 *
427 * Request all PCI BARs and iomap regions specified by @mask.
428 */
Yinghai Lufb7ebfe2012-01-04 15:50:02 -0800429int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
Tejun Heo916fbfb2008-03-12 15:26:34 +0900430 const char *name)
431{
432 int request_mask = ((1 << 6) - 1) & ~mask;
433 int rc;
434
435 rc = pci_request_selected_regions(pdev, request_mask, name);
436 if (rc)
437 return rc;
438
439 rc = pcim_iomap_regions(pdev, mask, name);
440 if (rc)
441 pci_release_selected_regions(pdev, request_mask);
442 return rc;
443}
444EXPORT_SYMBOL(pcim_iomap_regions_request_all);
445
446/**
Tejun Heoec04b072007-03-09 19:45:58 +0900447 * pcim_iounmap_regions - Unmap and release PCI BARs
448 * @pdev: PCI device to map IO resources for
449 * @mask: Mask of BARs to unmap and release
450 *
Kulikov Vasiliy4d45ada2010-07-10 14:07:41 +0400451 * Unmap and release regions specified by @mask.
Tejun Heoec04b072007-03-09 19:45:58 +0900452 */
Yinghai Lufb7ebfe2012-01-04 15:50:02 -0800453void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
Tejun Heoec04b072007-03-09 19:45:58 +0900454{
455 void __iomem * const *iomap;
456 int i;
457
458 iomap = pcim_iomap_table(pdev);
459 if (!iomap)
460 return;
461
Dan Carpenter1f35d042015-09-21 19:21:51 +0300462 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
Tejun Heoec04b072007-03-09 19:45:58 +0900463 if (!(mask & (1 << i)))
464 continue;
465
466 pcim_iounmap(pdev, iomap[i]);
467 pci_release_region(pdev, i);
468 }
469}
470EXPORT_SYMBOL(pcim_iounmap_regions);
Wolfram Sang571806a2011-10-25 15:03:42 +0200471#endif /* CONFIG_PCI */