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