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