xref: /linux/lib/devres.c (revision 288440de9e5fdb4a3ff73864850f080c1250fc81)
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_UC,
12 	DEVM_IOREMAP_WC,
13 	DEVM_IOREMAP_NP,
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_node(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL,
33 				dev_to_node(dev));
34 	if (!ptr)
35 		return NULL;
36 
37 	switch (type) {
38 	case DEVM_IOREMAP:
39 		addr = ioremap(offset, size);
40 		break;
41 	case DEVM_IOREMAP_UC:
42 		addr = ioremap_uc(offset, size);
43 		break;
44 	case DEVM_IOREMAP_WC:
45 		addr = ioremap_wc(offset, size);
46 		break;
47 	case DEVM_IOREMAP_NP:
48 		addr = ioremap_np(offset, size);
49 		break;
50 	}
51 
52 	if (addr) {
53 		*ptr = addr;
54 		devres_add(dev, ptr);
55 	} else
56 		devres_free(ptr);
57 
58 	return addr;
59 }
60 
61 /**
62  * devm_ioremap - Managed ioremap()
63  * @dev: Generic device to remap IO address for
64  * @offset: Resource address to map
65  * @size: Size of map
66  *
67  * Managed ioremap().  Map is automatically unmapped on driver detach.
68  */
69 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
70 			   resource_size_t size)
71 {
72 	return __devm_ioremap(dev, offset, size, DEVM_IOREMAP);
73 }
74 EXPORT_SYMBOL(devm_ioremap);
75 
76 /**
77  * devm_ioremap_uc - Managed ioremap_uc()
78  * @dev: Generic device to remap IO address for
79  * @offset: Resource address to map
80  * @size: Size of map
81  *
82  * Managed ioremap_uc().  Map is automatically unmapped on driver detach.
83  */
84 void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
85 			      resource_size_t size)
86 {
87 	return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_UC);
88 }
89 EXPORT_SYMBOL_GPL(devm_ioremap_uc);
90 
91 /**
92  * devm_ioremap_wc - Managed ioremap_wc()
93  * @dev: Generic device to remap IO address for
94  * @offset: Resource address to map
95  * @size: Size of map
96  *
97  * Managed ioremap_wc().  Map is automatically unmapped on driver detach.
98  */
99 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
100 			      resource_size_t size)
101 {
102 	return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_WC);
103 }
104 EXPORT_SYMBOL(devm_ioremap_wc);
105 
106 /**
107  * devm_ioremap_np - Managed ioremap_np()
108  * @dev: Generic device to remap IO address for
109  * @offset: Resource address to map
110  * @size: Size of map
111  *
112  * Managed ioremap_np().  Map is automatically unmapped on driver detach.
113  */
114 void __iomem *devm_ioremap_np(struct device *dev, resource_size_t offset,
115 			      resource_size_t size)
116 {
117 	return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NP);
118 }
119 EXPORT_SYMBOL(devm_ioremap_np);
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 static 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 	char *pretty_name;
143 
144 	BUG_ON(!dev);
145 
146 	if (!res || resource_type(res) != IORESOURCE_MEM) {
147 		dev_err(dev, "invalid resource\n");
148 		return IOMEM_ERR_PTR(-EINVAL);
149 	}
150 
151 	if (type == DEVM_IOREMAP && res->flags & IORESOURCE_MEM_NONPOSTED)
152 		type = DEVM_IOREMAP_NP;
153 
154 	size = resource_size(res);
155 
156 	if (res->name)
157 		pretty_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s",
158 					     dev_name(dev), res->name);
159 	else
160 		pretty_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
161 	if (!pretty_name) {
162 		dev_err(dev, "can't generate pretty name for resource %pR\n", res);
163 		return IOMEM_ERR_PTR(-ENOMEM);
164 	}
165 
166 	if (!devm_request_mem_region(dev, res->start, size, pretty_name)) {
167 		dev_err(dev, "can't request region for resource %pR\n", res);
168 		return IOMEM_ERR_PTR(-EBUSY);
169 	}
170 
171 	dest_ptr = __devm_ioremap(dev, res->start, size, type);
172 	if (!dest_ptr) {
173 		dev_err(dev, "ioremap failed for resource %pR\n", res);
174 		devm_release_mem_region(dev, res->start, size);
175 		dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
176 	}
177 
178 	return dest_ptr;
179 }
180 
181 /**
182  * devm_ioremap_resource() - check, request region, and ioremap resource
183  * @dev: generic device to handle the resource for
184  * @res: resource to be handled
185  *
186  * Checks that a resource is a valid memory region, requests the memory
187  * region and ioremaps it. All operations are managed and will be undone
188  * on driver detach.
189  *
190  * Usage example:
191  *
192  *	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193  *	base = devm_ioremap_resource(&pdev->dev, res);
194  *	if (IS_ERR(base))
195  *		return PTR_ERR(base);
196  *
197  * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
198  * on failure.
199  */
200 void __iomem *devm_ioremap_resource(struct device *dev,
201 				    const struct resource *res)
202 {
203 	return __devm_ioremap_resource(dev, res, DEVM_IOREMAP);
204 }
205 EXPORT_SYMBOL(devm_ioremap_resource);
206 
207 /**
208  * devm_ioremap_resource_wc() - write-combined variant of
209  *				devm_ioremap_resource()
210  * @dev: generic device to handle the resource for
211  * @res: resource to be handled
212  *
213  * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code
214  * on failure.
215  */
216 void __iomem *devm_ioremap_resource_wc(struct device *dev,
217 				       const struct resource *res)
218 {
219 	return __devm_ioremap_resource(dev, res, DEVM_IOREMAP_WC);
220 }
221 
222 /*
223  * devm_of_iomap - Requests a resource and maps the memory mapped IO
224  *		   for a given device_node managed by a given device
225  *
226  * Checks that a resource is a valid memory region, requests the memory
227  * region and ioremaps it. All operations are managed and will be undone
228  * on driver detach of the device.
229  *
230  * This is to be used when a device requests/maps resources described
231  * by other device tree nodes (children or otherwise).
232  *
233  * @dev:	The device "managing" the resource
234  * @node:       The device-tree node where the resource resides
235  * @index:	index of the MMIO range in the "reg" property
236  * @size:	Returns the size of the resource (pass NULL if not needed)
237  *
238  * Usage example:
239  *
240  *	base = devm_of_iomap(&pdev->dev, node, 0, NULL);
241  *	if (IS_ERR(base))
242  *		return PTR_ERR(base);
243  *
244  * Please Note: This is not a one-to-one replacement for of_iomap() because the
245  * of_iomap() function does not track whether the region is already mapped.  If
246  * two drivers try to map the same memory, the of_iomap() function will succeed
247  * but the devm_of_iomap() function will return -EBUSY.
248  *
249  * Return: a pointer to the requested and mapped memory or an ERR_PTR() encoded
250  * error code on failure.
251  */
252 void __iomem *devm_of_iomap(struct device *dev, struct device_node *node, int index,
253 			    resource_size_t *size)
254 {
255 	struct resource res;
256 
257 	if (of_address_to_resource(node, index, &res))
258 		return IOMEM_ERR_PTR(-EINVAL);
259 	if (size)
260 		*size = resource_size(&res);
261 	return devm_ioremap_resource(dev, &res);
262 }
263 EXPORT_SYMBOL(devm_of_iomap);
264 
265 #ifdef CONFIG_HAS_IOPORT_MAP
266 /*
267  * Generic iomap devres
268  */
269 static void devm_ioport_map_release(struct device *dev, void *res)
270 {
271 	ioport_unmap(*(void __iomem **)res);
272 }
273 
274 static int devm_ioport_map_match(struct device *dev, void *res,
275 				 void *match_data)
276 {
277 	return *(void **)res == match_data;
278 }
279 
280 /**
281  * devm_ioport_map - Managed ioport_map()
282  * @dev: Generic device to map ioport for
283  * @port: Port to map
284  * @nr: Number of ports to map
285  *
286  * Managed ioport_map().  Map is automatically unmapped on driver
287  * detach.
288  *
289  * Return: a pointer to the remapped memory or NULL on failure.
290  */
291 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
292 			       unsigned int nr)
293 {
294 	void __iomem **ptr, *addr;
295 
296 	ptr = devres_alloc_node(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL,
297 				dev_to_node(dev));
298 	if (!ptr)
299 		return NULL;
300 
301 	addr = ioport_map(port, nr);
302 	if (addr) {
303 		*ptr = addr;
304 		devres_add(dev, ptr);
305 	} else
306 		devres_free(ptr);
307 
308 	return addr;
309 }
310 EXPORT_SYMBOL(devm_ioport_map);
311 
312 /**
313  * devm_ioport_unmap - Managed ioport_unmap()
314  * @dev: Generic device to unmap for
315  * @addr: Address to unmap
316  *
317  * Managed ioport_unmap().  @addr must have been mapped using
318  * devm_ioport_map().
319  */
320 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
321 {
322 	ioport_unmap(addr);
323 	WARN_ON(devres_destroy(dev, devm_ioport_map_release,
324 			       devm_ioport_map_match, (__force void *)addr));
325 }
326 EXPORT_SYMBOL(devm_ioport_unmap);
327 #endif /* CONFIG_HAS_IOPORT_MAP */
328 
329 #ifdef CONFIG_PCI
330 /*
331  * PCI iomap devres
332  */
333 #define PCIM_IOMAP_MAX	PCI_STD_NUM_BARS
334 
335 struct pcim_iomap_devres {
336 	void __iomem *table[PCIM_IOMAP_MAX];
337 };
338 
339 static void pcim_iomap_release(struct device *gendev, void *res)
340 {
341 	struct pci_dev *dev = to_pci_dev(gendev);
342 	struct pcim_iomap_devres *this = res;
343 	int i;
344 
345 	for (i = 0; i < PCIM_IOMAP_MAX; i++)
346 		if (this->table[i])
347 			pci_iounmap(dev, this->table[i]);
348 }
349 
350 /**
351  * pcim_iomap_table - access iomap allocation table
352  * @pdev: PCI device to access iomap table for
353  *
354  * Access iomap allocation table for @dev.  If iomap table doesn't
355  * exist and @pdev is managed, it will be allocated.  All iomaps
356  * recorded in the iomap table are automatically unmapped on driver
357  * detach.
358  *
359  * This function might sleep when the table is first allocated but can
360  * be safely called without context and guaranteed to succeed once
361  * allocated.
362  */
363 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
364 {
365 	struct pcim_iomap_devres *dr, *new_dr;
366 
367 	dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
368 	if (dr)
369 		return dr->table;
370 
371 	new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL,
372 				   dev_to_node(&pdev->dev));
373 	if (!new_dr)
374 		return NULL;
375 	dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
376 	return dr->table;
377 }
378 EXPORT_SYMBOL(pcim_iomap_table);
379 
380 /**
381  * pcim_iomap - Managed pcim_iomap()
382  * @pdev: PCI device to iomap for
383  * @bar: BAR to iomap
384  * @maxlen: Maximum length of iomap
385  *
386  * Managed pci_iomap().  Map is automatically unmapped on driver
387  * detach.
388  */
389 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
390 {
391 	void __iomem **tbl;
392 
393 	BUG_ON(bar >= PCIM_IOMAP_MAX);
394 
395 	tbl = (void __iomem **)pcim_iomap_table(pdev);
396 	if (!tbl || tbl[bar])	/* duplicate mappings not allowed */
397 		return NULL;
398 
399 	tbl[bar] = pci_iomap(pdev, bar, maxlen);
400 	return tbl[bar];
401 }
402 EXPORT_SYMBOL(pcim_iomap);
403 
404 /**
405  * pcim_iounmap - Managed pci_iounmap()
406  * @pdev: PCI device to iounmap for
407  * @addr: Address to unmap
408  *
409  * Managed pci_iounmap().  @addr must have been mapped using pcim_iomap().
410  */
411 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
412 {
413 	void __iomem **tbl;
414 	int i;
415 
416 	pci_iounmap(pdev, addr);
417 
418 	tbl = (void __iomem **)pcim_iomap_table(pdev);
419 	BUG_ON(!tbl);
420 
421 	for (i = 0; i < PCIM_IOMAP_MAX; i++)
422 		if (tbl[i] == addr) {
423 			tbl[i] = NULL;
424 			return;
425 		}
426 	WARN_ON(1);
427 }
428 EXPORT_SYMBOL(pcim_iounmap);
429 
430 /**
431  * pcim_iomap_regions - Request and iomap PCI BARs
432  * @pdev: PCI device to map IO resources for
433  * @mask: Mask of BARs to request and iomap
434  * @name: Name used when requesting regions
435  *
436  * Request and iomap regions specified by @mask.
437  */
438 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
439 {
440 	void __iomem * const *iomap;
441 	int i, rc;
442 
443 	iomap = pcim_iomap_table(pdev);
444 	if (!iomap)
445 		return -ENOMEM;
446 
447 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
448 		unsigned long len;
449 
450 		if (!(mask & (1 << i)))
451 			continue;
452 
453 		rc = -EINVAL;
454 		len = pci_resource_len(pdev, i);
455 		if (!len)
456 			goto err_inval;
457 
458 		rc = pci_request_region(pdev, i, name);
459 		if (rc)
460 			goto err_inval;
461 
462 		rc = -ENOMEM;
463 		if (!pcim_iomap(pdev, i, 0))
464 			goto err_region;
465 	}
466 
467 	return 0;
468 
469  err_region:
470 	pci_release_region(pdev, i);
471  err_inval:
472 	while (--i >= 0) {
473 		if (!(mask & (1 << i)))
474 			continue;
475 		pcim_iounmap(pdev, iomap[i]);
476 		pci_release_region(pdev, i);
477 	}
478 
479 	return rc;
480 }
481 EXPORT_SYMBOL(pcim_iomap_regions);
482 
483 /**
484  * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
485  * @pdev: PCI device to map IO resources for
486  * @mask: Mask of BARs to iomap
487  * @name: Name used when requesting regions
488  *
489  * Request all PCI BARs and iomap regions specified by @mask.
490  */
491 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
492 				   const char *name)
493 {
494 	int request_mask = ((1 << 6) - 1) & ~mask;
495 	int rc;
496 
497 	rc = pci_request_selected_regions(pdev, request_mask, name);
498 	if (rc)
499 		return rc;
500 
501 	rc = pcim_iomap_regions(pdev, mask, name);
502 	if (rc)
503 		pci_release_selected_regions(pdev, request_mask);
504 	return rc;
505 }
506 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
507 
508 /**
509  * pcim_iounmap_regions - Unmap and release PCI BARs
510  * @pdev: PCI device to map IO resources for
511  * @mask: Mask of BARs to unmap and release
512  *
513  * Unmap and release regions specified by @mask.
514  */
515 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
516 {
517 	void __iomem * const *iomap;
518 	int i;
519 
520 	iomap = pcim_iomap_table(pdev);
521 	if (!iomap)
522 		return;
523 
524 	for (i = 0; i < PCIM_IOMAP_MAX; i++) {
525 		if (!(mask & (1 << i)))
526 			continue;
527 
528 		pcim_iounmap(pdev, iomap[i]);
529 		pci_release_region(pdev, i);
530 	}
531 }
532 EXPORT_SYMBOL(pcim_iounmap_regions);
533 #endif /* CONFIG_PCI */
534 
535 static void devm_arch_phys_ac_add_release(struct device *dev, void *res)
536 {
537 	arch_phys_wc_del(*((int *)res));
538 }
539 
540 /**
541  * devm_arch_phys_wc_add - Managed arch_phys_wc_add()
542  * @dev: Managed device
543  * @base: Memory base address
544  * @size: Size of memory range
545  *
546  * Adds a WC MTRR using arch_phys_wc_add() and sets up a release callback.
547  * See arch_phys_wc_add() for more information.
548  */
549 int devm_arch_phys_wc_add(struct device *dev, unsigned long base, unsigned long size)
550 {
551 	int *mtrr;
552 	int ret;
553 
554 	mtrr = devres_alloc_node(devm_arch_phys_ac_add_release, sizeof(*mtrr), GFP_KERNEL,
555 				 dev_to_node(dev));
556 	if (!mtrr)
557 		return -ENOMEM;
558 
559 	ret = arch_phys_wc_add(base, size);
560 	if (ret < 0) {
561 		devres_free(mtrr);
562 		return ret;
563 	}
564 
565 	*mtrr = ret;
566 	devres_add(dev, mtrr);
567 
568 	return ret;
569 }
570 EXPORT_SYMBOL(devm_arch_phys_wc_add);
571 
572 struct arch_io_reserve_memtype_wc_devres {
573 	resource_size_t start;
574 	resource_size_t size;
575 };
576 
577 static void devm_arch_io_free_memtype_wc_release(struct device *dev, void *res)
578 {
579 	const struct arch_io_reserve_memtype_wc_devres *this = res;
580 
581 	arch_io_free_memtype_wc(this->start, this->size);
582 }
583 
584 /**
585  * devm_arch_io_reserve_memtype_wc - Managed arch_io_reserve_memtype_wc()
586  * @dev: Managed device
587  * @start: Memory base address
588  * @size: Size of memory range
589  *
590  * Reserves a memory range with WC caching using arch_io_reserve_memtype_wc()
591  * and sets up a release callback See arch_io_reserve_memtype_wc() for more
592  * information.
593  */
594 int devm_arch_io_reserve_memtype_wc(struct device *dev, resource_size_t start,
595 				    resource_size_t size)
596 {
597 	struct arch_io_reserve_memtype_wc_devres *dr;
598 	int ret;
599 
600 	dr = devres_alloc_node(devm_arch_io_free_memtype_wc_release, sizeof(*dr), GFP_KERNEL,
601 			       dev_to_node(dev));
602 	if (!dr)
603 		return -ENOMEM;
604 
605 	ret = arch_io_reserve_memtype_wc(start, size);
606 	if (ret < 0) {
607 		devres_free(dr);
608 		return ret;
609 	}
610 
611 	dr->start = start;
612 	dr->size = size;
613 	devres_add(dev, dr);
614 
615 	return ret;
616 }
617 EXPORT_SYMBOL(devm_arch_io_reserve_memtype_wc);
618