xref: /linux-6.15/lib/devres.c (revision b1425189)
1 #include <linux/err.h>
2 #include <linux/pci.h>
3 #include <linux/io.h>
4 #include <linux/gfp.h>
5 #include <linux/export.h>
6 
7 void devm_ioremap_release(struct device *dev, void *res)
8 {
9 	iounmap(*(void __iomem **)res);
10 }
11 
12 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
13 {
14 	return *(void **)res == match_data;
15 }
16 
17 /**
18  * devm_ioremap - Managed ioremap()
19  * @dev: Generic device to remap IO address for
20  * @offset: BUS offset to map
21  * @size: Size of map
22  *
23  * Managed ioremap().  Map is automatically unmapped on driver detach.
24  */
25 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
26 			   unsigned long size)
27 {
28 	void __iomem **ptr, *addr;
29 
30 	ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
31 	if (!ptr)
32 		return NULL;
33 
34 	addr = ioremap(offset, size);
35 	if (addr) {
36 		*ptr = addr;
37 		devres_add(dev, ptr);
38 	} else
39 		devres_free(ptr);
40 
41 	return addr;
42 }
43 EXPORT_SYMBOL(devm_ioremap);
44 
45 /**
46  * devm_ioremap_nocache - Managed ioremap_nocache()
47  * @dev: Generic device to remap IO address for
48  * @offset: BUS offset to map
49  * @size: Size of map
50  *
51  * Managed ioremap_nocache().  Map is automatically unmapped on driver
52  * detach.
53  */
54 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
55 				   unsigned long 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_nocache(offset, size);
64 	if (addr) {
65 		*ptr = addr;
66 		devres_add(dev, ptr);
67 	} else
68 		devres_free(ptr);
69 
70 	return addr;
71 }
72 EXPORT_SYMBOL(devm_ioremap_nocache);
73 
74 /**
75  * devm_iounmap - Managed iounmap()
76  * @dev: Generic device to unmap for
77  * @addr: Address to unmap
78  *
79  * Managed iounmap().  @addr must have been mapped using devm_ioremap*().
80  */
81 void devm_iounmap(struct device *dev, void __iomem *addr)
82 {
83 	WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
84 			       (__force void *)addr));
85 	iounmap(addr);
86 }
87 EXPORT_SYMBOL(devm_iounmap);
88 
89 #define IOMEM_ERR_PTR(err) (__force void __iomem *)ERR_PTR(err)
90 
91 /**
92  * devm_ioremap_resource() - check, request region, and ioremap resource
93  * @dev: generic device to handle the resource for
94  * @res: resource to be handled
95  *
96  * Checks that a resource is a valid memory region, requests the memory region
97  * and ioremaps it either as cacheable or as non-cacheable memory depending on
98  * the resource's flags. All operations are managed and will be undone on
99  * driver detach.
100  *
101  * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
102  * on failure. Usage example:
103  *
104  *	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105  *	base = devm_ioremap_resource(&pdev->dev, res);
106  *	if (IS_ERR(base))
107  *		return PTR_ERR(base);
108  */
109 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res)
110 {
111 	resource_size_t size;
112 	const char *name;
113 	void __iomem *dest_ptr;
114 
115 	BUG_ON(!dev);
116 
117 	if (!res || resource_type(res) != IORESOURCE_MEM) {
118 		dev_err(dev, "invalid resource\n");
119 		return IOMEM_ERR_PTR(-EINVAL);
120 	}
121 
122 	size = resource_size(res);
123 	name = res->name ?: dev_name(dev);
124 
125 	if (!devm_request_mem_region(dev, res->start, size, name)) {
126 		dev_err(dev, "can't request region for resource %pR\n", res);
127 		return IOMEM_ERR_PTR(-EBUSY);
128 	}
129 
130 	if (res->flags & IORESOURCE_CACHEABLE)
131 		dest_ptr = devm_ioremap(dev, res->start, size);
132 	else
133 		dest_ptr = devm_ioremap_nocache(dev, res->start, size);
134 
135 	if (!dest_ptr) {
136 		dev_err(dev, "ioremap failed for resource %pR\n", res);
137 		devm_release_mem_region(dev, res->start, size);
138 		dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
139 	}
140 
141 	return dest_ptr;
142 }
143 EXPORT_SYMBOL(devm_ioremap_resource);
144 
145 #ifdef CONFIG_HAS_IOPORT_MAP
146 /*
147  * Generic iomap devres
148  */
149 static void devm_ioport_map_release(struct device *dev, void *res)
150 {
151 	ioport_unmap(*(void __iomem **)res);
152 }
153 
154 static int devm_ioport_map_match(struct device *dev, void *res,
155 				 void *match_data)
156 {
157 	return *(void **)res == match_data;
158 }
159 
160 /**
161  * devm_ioport_map - Managed ioport_map()
162  * @dev: Generic device to map ioport for
163  * @port: Port to map
164  * @nr: Number of ports to map
165  *
166  * Managed ioport_map().  Map is automatically unmapped on driver
167  * detach.
168  */
169 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
170 			       unsigned int nr)
171 {
172 	void __iomem **ptr, *addr;
173 
174 	ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
175 	if (!ptr)
176 		return NULL;
177 
178 	addr = ioport_map(port, nr);
179 	if (addr) {
180 		*ptr = addr;
181 		devres_add(dev, ptr);
182 	} else
183 		devres_free(ptr);
184 
185 	return addr;
186 }
187 EXPORT_SYMBOL(devm_ioport_map);
188 
189 /**
190  * devm_ioport_unmap - Managed ioport_unmap()
191  * @dev: Generic device to unmap for
192  * @addr: Address to unmap
193  *
194  * Managed ioport_unmap().  @addr must have been mapped using
195  * devm_ioport_map().
196  */
197 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
198 {
199 	ioport_unmap(addr);
200 	WARN_ON(devres_destroy(dev, devm_ioport_map_release,
201 			       devm_ioport_map_match, (__force void *)addr));
202 }
203 EXPORT_SYMBOL(devm_ioport_unmap);
204 #endif /* CONFIG_HAS_IOPORT_MAP */
205 
206 #ifdef CONFIG_PCI
207 /*
208  * PCI iomap devres
209  */
210 #define PCIM_IOMAP_MAX	PCI_ROM_RESOURCE
211 
212 struct pcim_iomap_devres {
213 	void __iomem *table[PCIM_IOMAP_MAX];
214 };
215 
216 static void pcim_iomap_release(struct device *gendev, void *res)
217 {
218 	struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
219 	struct pcim_iomap_devres *this = res;
220 	int i;
221 
222 	for (i = 0; i < PCIM_IOMAP_MAX; i++)
223 		if (this->table[i])
224 			pci_iounmap(dev, this->table[i]);
225 }
226 
227 /**
228  * pcim_iomap_table - access iomap allocation table
229  * @pdev: PCI device to access iomap table for
230  *
231  * Access iomap allocation table for @dev.  If iomap table doesn't
232  * exist and @pdev is managed, it will be allocated.  All iomaps
233  * recorded in the iomap table are automatically unmapped on driver
234  * detach.
235  *
236  * This function might sleep when the table is first allocated but can
237  * be safely called without context and guaranteed to succed once
238  * allocated.
239  */
240 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
241 {
242 	struct pcim_iomap_devres *dr, *new_dr;
243 
244 	dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
245 	if (dr)
246 		return dr->table;
247 
248 	new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
249 	if (!new_dr)
250 		return NULL;
251 	dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
252 	return dr->table;
253 }
254 EXPORT_SYMBOL(pcim_iomap_table);
255 
256 /**
257  * pcim_iomap - Managed pcim_iomap()
258  * @pdev: PCI device to iomap for
259  * @bar: BAR to iomap
260  * @maxlen: Maximum length of iomap
261  *
262  * Managed pci_iomap().  Map is automatically unmapped on driver
263  * detach.
264  */
265 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
266 {
267 	void __iomem **tbl;
268 
269 	BUG_ON(bar >= PCIM_IOMAP_MAX);
270 
271 	tbl = (void __iomem **)pcim_iomap_table(pdev);
272 	if (!tbl || tbl[bar])	/* duplicate mappings not allowed */
273 		return NULL;
274 
275 	tbl[bar] = pci_iomap(pdev, bar, maxlen);
276 	return tbl[bar];
277 }
278 EXPORT_SYMBOL(pcim_iomap);
279 
280 /**
281  * pcim_iounmap - Managed pci_iounmap()
282  * @pdev: PCI device to iounmap for
283  * @addr: Address to unmap
284  *
285  * Managed pci_iounmap().  @addr must have been mapped using pcim_iomap().
286  */
287 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
288 {
289 	void __iomem **tbl;
290 	int i;
291 
292 	pci_iounmap(pdev, addr);
293 
294 	tbl = (void __iomem **)pcim_iomap_table(pdev);
295 	BUG_ON(!tbl);
296 
297 	for (i = 0; i < PCIM_IOMAP_MAX; i++)
298 		if (tbl[i] == addr) {
299 			tbl[i] = NULL;
300 			return;
301 		}
302 	WARN_ON(1);
303 }
304 EXPORT_SYMBOL(pcim_iounmap);
305 
306 /**
307  * pcim_iomap_regions - Request and iomap PCI BARs
308  * @pdev: PCI device to map IO resources for
309  * @mask: Mask of BARs to request and iomap
310  * @name: Name used when requesting regions
311  *
312  * Request and iomap regions specified by @mask.
313  */
314 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
315 {
316 	void __iomem * const *iomap;
317 	int i, rc;
318 
319 	iomap = pcim_iomap_table(pdev);
320 	if (!iomap)
321 		return -ENOMEM;
322 
323 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
324 		unsigned long len;
325 
326 		if (!(mask & (1 << i)))
327 			continue;
328 
329 		rc = -EINVAL;
330 		len = pci_resource_len(pdev, i);
331 		if (!len)
332 			goto err_inval;
333 
334 		rc = pci_request_region(pdev, i, name);
335 		if (rc)
336 			goto err_inval;
337 
338 		rc = -ENOMEM;
339 		if (!pcim_iomap(pdev, i, 0))
340 			goto err_region;
341 	}
342 
343 	return 0;
344 
345  err_region:
346 	pci_release_region(pdev, i);
347  err_inval:
348 	while (--i >= 0) {
349 		if (!(mask & (1 << i)))
350 			continue;
351 		pcim_iounmap(pdev, iomap[i]);
352 		pci_release_region(pdev, i);
353 	}
354 
355 	return rc;
356 }
357 EXPORT_SYMBOL(pcim_iomap_regions);
358 
359 /**
360  * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
361  * @pdev: PCI device to map IO resources for
362  * @mask: Mask of BARs to iomap
363  * @name: Name used when requesting regions
364  *
365  * Request all PCI BARs and iomap regions specified by @mask.
366  */
367 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
368 				   const char *name)
369 {
370 	int request_mask = ((1 << 6) - 1) & ~mask;
371 	int rc;
372 
373 	rc = pci_request_selected_regions(pdev, request_mask, name);
374 	if (rc)
375 		return rc;
376 
377 	rc = pcim_iomap_regions(pdev, mask, name);
378 	if (rc)
379 		pci_release_selected_regions(pdev, request_mask);
380 	return rc;
381 }
382 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
383 
384 /**
385  * pcim_iounmap_regions - Unmap and release PCI BARs
386  * @pdev: PCI device to map IO resources for
387  * @mask: Mask of BARs to unmap and release
388  *
389  * Unmap and release regions specified by @mask.
390  */
391 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
392 {
393 	void __iomem * const *iomap;
394 	int i;
395 
396 	iomap = pcim_iomap_table(pdev);
397 	if (!iomap)
398 		return;
399 
400 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
401 		if (!(mask & (1 << i)))
402 			continue;
403 
404 		pcim_iounmap(pdev, iomap[i]);
405 		pci_release_region(pdev, i);
406 	}
407 }
408 EXPORT_SYMBOL(pcim_iounmap_regions);
409 #endif /* CONFIG_PCI */
410