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