GNU Linux-libre 5.4.200-gnu1
[releases.git] / lib / devres.c
1 // 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 #include <linux/of_address.h>
8
9 enum devm_ioremap_type {
10         DEVM_IOREMAP = 0,
11         DEVM_IOREMAP_NC,
12         DEVM_IOREMAP_UC,
13         DEVM_IOREMAP_WC,
14 };
15
16 void devm_ioremap_release(struct device *dev, void *res)
17 {
18         iounmap(*(void __iomem **)res);
19 }
20
21 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
22 {
23         return *(void **)res == match_data;
24 }
25
26 static 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;
43         case DEVM_IOREMAP_UC:
44                 addr = ioremap_uc(offset, size);
45                 break;
46         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
60 /**
61  * devm_ioremap - Managed ioremap()
62  * @dev: Generic device to remap IO address for
63  * @offset: Resource address to map
64  * @size: Size of map
65  *
66  * Managed ioremap().  Map is automatically unmapped on driver detach.
67  */
68 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
69                            resource_size_t size)
70 {
71         return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
72 }
73 EXPORT_SYMBOL(devm_ioremap);
74
75 /**
76  * 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  */
83 void __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 }
88 EXPORT_SYMBOL_GPL(devm_ioremap_uc);
89
90 /**
91  * devm_ioremap_nocache - Managed ioremap_nocache()
92  * @dev: Generic device to remap IO address for
93  * @offset: Resource address to map
94  * @size: Size of map
95  *
96  * Managed ioremap_nocache().  Map is automatically unmapped on driver
97  * detach.
98  */
99 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
100                                    resource_size_t size)
101 {
102         return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NC);
103 }
104 EXPORT_SYMBOL(devm_ioremap_nocache);
105
106 /**
107  * devm_ioremap_wc - Managed ioremap_wc()
108  * @dev: Generic device to remap IO address for
109  * @offset: Resource address to map
110  * @size: Size of map
111  *
112  * Managed ioremap_wc().  Map is automatically unmapped on driver detach.
113  */
114 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
115                               resource_size_t size)
116 {
117         return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
118 }
119 EXPORT_SYMBOL(devm_ioremap_wc);
120
121 /**
122  * 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  */
128 void devm_iounmap(struct device *dev, void __iomem *addr)
129 {
130         WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
131                                (__force void *)addr));
132         iounmap(addr);
133 }
134 EXPORT_SYMBOL(devm_iounmap);
135
136 /**
137  * devm_ioremap_resource() - check, request region, and ioremap resource
138  * @dev: generic device to handle the resource for
139  * @res: resource to be handled
140  *
141  * Checks that a resource is a valid memory region, requests the memory
142  * region and ioremaps it. All operations are managed and will be undone
143  * on driver detach.
144  *
145  * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
146  * on failure. Usage example:
147  *
148  *      res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149  *      base = devm_ioremap_resource(&pdev->dev, res);
150  *      if (IS_ERR(base))
151  *              return PTR_ERR(base);
152  */
153 void __iomem *devm_ioremap_resource(struct device *dev,
154                                     const struct resource *res)
155 {
156         resource_size_t size;
157         void __iomem *dest_ptr;
158         char *pretty_name;
159
160         BUG_ON(!dev);
161
162         if (!res || resource_type(res) != IORESOURCE_MEM) {
163                 dev_err(dev, "invalid resource\n");
164                 return IOMEM_ERR_PTR(-EINVAL);
165         }
166
167         size = resource_size(res);
168
169         if (res->name)
170                 pretty_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
171                                              dev_name(dev), res->name);
172         else
173                 pretty_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
174         if (!pretty_name)
175                 return IOMEM_ERR_PTR(-ENOMEM);
176
177         if (!devm_request_mem_region(dev, res->start, size, pretty_name)) {
178                 dev_err(dev, "can't request region for resource %pR\n", res);
179                 return IOMEM_ERR_PTR(-EBUSY);
180         }
181
182         dest_ptr = devm_ioremap(dev, res->start, size);
183         if (!dest_ptr) {
184                 dev_err(dev, "ioremap failed for resource %pR\n", res);
185                 devm_release_mem_region(dev, res->start, size);
186                 dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
187         }
188
189         return dest_ptr;
190 }
191 EXPORT_SYMBOL(devm_ioremap_resource);
192
193 /*
194  * devm_of_iomap - Requests a resource and maps the memory mapped IO
195  *                 for a given device_node managed by a given device
196  *
197  * Checks that a resource is a valid memory region, requests the memory
198  * region and ioremaps it. All operations are managed and will be undone
199  * on driver detach of the device.
200  *
201  * This is to be used when a device requests/maps resources described
202  * by other device tree nodes (children or otherwise).
203  *
204  * @dev:        The device "managing" the resource
205  * @node:       The device-tree node where the resource resides
206  * @index:      index of the MMIO range in the "reg" property
207  * @size:       Returns the size of the resource (pass NULL if not needed)
208  * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
209  * error code on failure. Usage example:
210  *
211  *      base = devm_of_iomap(&pdev->dev, node, 0, NULL);
212  *      if (IS_ERR(base))
213  *              return PTR_ERR(base);
214  */
215 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
216                             resource_size_t *size)
217 {
218         struct resource res;
219
220         if (of_address_to_resource(node, index, &res))
221                 return IOMEM_ERR_PTR(-EINVAL);
222         if (size)
223                 *size = resource_size(&res);
224         return devm_ioremap_resource(dev, &res);
225 }
226 EXPORT_SYMBOL(devm_of_iomap);
227
228 #ifdef CONFIG_HAS_IOPORT_MAP
229 /*
230  * Generic iomap devres
231  */
232 static void devm_ioport_map_release(struct device *dev, void *res)
233 {
234         ioport_unmap(*(void __iomem **)res);
235 }
236
237 static int devm_ioport_map_match(struct device *dev, void *res,
238                                  void *match_data)
239 {
240         return *(void **)res == match_data;
241 }
242
243 /**
244  * devm_ioport_map - Managed ioport_map()
245  * @dev: Generic device to map ioport for
246  * @port: Port to map
247  * @nr: Number of ports to map
248  *
249  * Managed ioport_map().  Map is automatically unmapped on driver
250  * detach.
251  */
252 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
253                                unsigned int nr)
254 {
255         void __iomem **ptr, *addr;
256
257         ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
258         if (!ptr)
259                 return NULL;
260
261         addr = ioport_map(port, nr);
262         if (addr) {
263                 *ptr = addr;
264                 devres_add(dev, ptr);
265         } else
266                 devres_free(ptr);
267
268         return addr;
269 }
270 EXPORT_SYMBOL(devm_ioport_map);
271
272 /**
273  * devm_ioport_unmap - Managed ioport_unmap()
274  * @dev: Generic device to unmap for
275  * @addr: Address to unmap
276  *
277  * Managed ioport_unmap().  @addr must have been mapped using
278  * devm_ioport_map().
279  */
280 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
281 {
282         ioport_unmap(addr);
283         WARN_ON(devres_destroy(dev, devm_ioport_map_release,
284                                devm_ioport_map_match, (__force void *)addr));
285 }
286 EXPORT_SYMBOL(devm_ioport_unmap);
287 #endif /* CONFIG_HAS_IOPORT_MAP */
288
289 #ifdef CONFIG_PCI
290 /*
291  * PCI iomap devres
292  */
293 #define PCIM_IOMAP_MAX  PCI_ROM_RESOURCE
294
295 struct pcim_iomap_devres {
296         void __iomem *table[PCIM_IOMAP_MAX];
297 };
298
299 static void pcim_iomap_release(struct device *gendev, void *res)
300 {
301         struct pci_dev *dev = to_pci_dev(gendev);
302         struct pcim_iomap_devres *this = res;
303         int i;
304
305         for (i = 0; i < PCIM_IOMAP_MAX; i++)
306                 if (this->table[i])
307                         pci_iounmap(dev, this->table[i]);
308 }
309
310 /**
311  * pcim_iomap_table - access iomap allocation table
312  * @pdev: PCI device to access iomap table for
313  *
314  * Access iomap allocation table for @dev.  If iomap table doesn't
315  * exist and @pdev is managed, it will be allocated.  All iomaps
316  * recorded in the iomap table are automatically unmapped on driver
317  * detach.
318  *
319  * This function might sleep when the table is first allocated but can
320  * be safely called without context and guaranteed to succed once
321  * allocated.
322  */
323 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
324 {
325         struct pcim_iomap_devres *dr, *new_dr;
326
327         dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
328         if (dr)
329                 return dr->table;
330
331         new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
332         if (!new_dr)
333                 return NULL;
334         dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
335         return dr->table;
336 }
337 EXPORT_SYMBOL(pcim_iomap_table);
338
339 /**
340  * pcim_iomap - Managed pcim_iomap()
341  * @pdev: PCI device to iomap for
342  * @bar: BAR to iomap
343  * @maxlen: Maximum length of iomap
344  *
345  * Managed pci_iomap().  Map is automatically unmapped on driver
346  * detach.
347  */
348 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
349 {
350         void __iomem **tbl;
351
352         BUG_ON(bar >= PCIM_IOMAP_MAX);
353
354         tbl = (void __iomem **)pcim_iomap_table(pdev);
355         if (!tbl || tbl[bar])   /* duplicate mappings not allowed */
356                 return NULL;
357
358         tbl[bar] = pci_iomap(pdev, bar, maxlen);
359         return tbl[bar];
360 }
361 EXPORT_SYMBOL(pcim_iomap);
362
363 /**
364  * pcim_iounmap - Managed pci_iounmap()
365  * @pdev: PCI device to iounmap for
366  * @addr: Address to unmap
367  *
368  * Managed pci_iounmap().  @addr must have been mapped using pcim_iomap().
369  */
370 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
371 {
372         void __iomem **tbl;
373         int i;
374
375         pci_iounmap(pdev, addr);
376
377         tbl = (void __iomem **)pcim_iomap_table(pdev);
378         BUG_ON(!tbl);
379
380         for (i = 0; i < PCIM_IOMAP_MAX; i++)
381                 if (tbl[i] == addr) {
382                         tbl[i] = NULL;
383                         return;
384                 }
385         WARN_ON(1);
386 }
387 EXPORT_SYMBOL(pcim_iounmap);
388
389 /**
390  * pcim_iomap_regions - Request and iomap PCI BARs
391  * @pdev: PCI device to map IO resources for
392  * @mask: Mask of BARs to request and iomap
393  * @name: Name used when requesting regions
394  *
395  * Request and iomap regions specified by @mask.
396  */
397 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
398 {
399         void __iomem * const *iomap;
400         int i, rc;
401
402         iomap = pcim_iomap_table(pdev);
403         if (!iomap)
404                 return -ENOMEM;
405
406         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
407                 unsigned long len;
408
409                 if (!(mask & (1 << i)))
410                         continue;
411
412                 rc = -EINVAL;
413                 len = pci_resource_len(pdev, i);
414                 if (!len)
415                         goto err_inval;
416
417                 rc = pci_request_region(pdev, i, name);
418                 if (rc)
419                         goto err_inval;
420
421                 rc = -ENOMEM;
422                 if (!pcim_iomap(pdev, i, 0))
423                         goto err_region;
424         }
425
426         return 0;
427
428  err_region:
429         pci_release_region(pdev, i);
430  err_inval:
431         while (--i >= 0) {
432                 if (!(mask & (1 << i)))
433                         continue;
434                 pcim_iounmap(pdev, iomap[i]);
435                 pci_release_region(pdev, i);
436         }
437
438         return rc;
439 }
440 EXPORT_SYMBOL(pcim_iomap_regions);
441
442 /**
443  * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
444  * @pdev: PCI device to map IO resources for
445  * @mask: Mask of BARs to iomap
446  * @name: Name used when requesting regions
447  *
448  * Request all PCI BARs and iomap regions specified by @mask.
449  */
450 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
451                                    const char *name)
452 {
453         int request_mask = ((1 << 6) - 1) & ~mask;
454         int rc;
455
456         rc = pci_request_selected_regions(pdev, request_mask, name);
457         if (rc)
458                 return rc;
459
460         rc = pcim_iomap_regions(pdev, mask, name);
461         if (rc)
462                 pci_release_selected_regions(pdev, request_mask);
463         return rc;
464 }
465 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
466
467 /**
468  * pcim_iounmap_regions - Unmap and release PCI BARs
469  * @pdev: PCI device to map IO resources for
470  * @mask: Mask of BARs to unmap and release
471  *
472  * Unmap and release regions specified by @mask.
473  */
474 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
475 {
476         void __iomem * const *iomap;
477         int i;
478
479         iomap = pcim_iomap_table(pdev);
480         if (!iomap)
481                 return;
482
483         for (i = 0; i < PCIM_IOMAP_MAX; i++) {
484                 if (!(mask & (1 << i)))
485                         continue;
486
487                 pcim_iounmap(pdev, iomap[i]);
488                 pci_release_region(pdev, i);
489         }
490 }
491 EXPORT_SYMBOL(pcim_iounmap_regions);
492 #endif /* CONFIG_PCI */