1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/device.h>
3 #include <linux/pci.h>
4 #include "pci.h"
5 
6 /*
7  * On the state of PCI's devres implementation:
8  *
9  * The older devres API for PCI has two significant problems:
10  *
11  * 1. It is very strongly tied to the statically allocated mapping table in
12  *    struct pcim_iomap_devres below. This is mostly solved in the sense of the
13  *    pcim_ functions in this file providing things like ranged mapping by
14  *    bypassing this table, whereas the functions that were present in the old
15  *    API still enter the mapping addresses into the table for users of the old
16  *    API.
17  *
18  * 2. The region-request-functions in pci.c do become managed IF the device has
19  *    been enabled with pcim_enable_device() instead of pci_enable_device().
20  *    This resulted in the API becoming inconsistent: Some functions have an
21  *    obviously managed counter-part (e.g., pci_iomap() <-> pcim_iomap()),
22  *    whereas some don't and are never managed, while others don't and are
23  *    _sometimes_ managed (e.g. pci_request_region()).
24  *
25  *    Consequently, in the new API, region requests performed by the pcim_
26  *    functions are automatically cleaned up through the devres callback
27  *    pcim_addr_resource_release().
28  *
29  *    Users of pcim_enable_device() + pci_*region*() are redirected in
30  *    pci.c to the managed functions here in this file. This isn't exactly
31  *    perfect, but the only alternative way would be to port ALL drivers
32  *    using said combination to pcim_ functions.
33  *
34  * TODO:
35  * Remove the legacy table entirely once all calls to pcim_iomap_table() in
36  * the kernel have been removed.
37  */
38 
39 /*
40  * Legacy struct storing addresses to whole mapped BARs.
41  */
42 struct pcim_iomap_devres {
43 	void __iomem *table[PCI_NUM_RESOURCES];
44 };
45 
46 /* Used to restore the old INTx state on driver detach. */
47 struct pcim_intx_devres {
48 	int orig_intx;
49 };
50 
51 enum pcim_addr_devres_type {
52 	/* Default initializer. */
53 	PCIM_ADDR_DEVRES_TYPE_INVALID,
54 
55 	/* A requested region spanning an entire BAR. */
56 	PCIM_ADDR_DEVRES_TYPE_REGION,
57 
58 	/*
59 	 * A requested region spanning an entire BAR, and a mapping for
60 	 * the entire BAR.
61 	 */
62 	PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING,
63 
64 	/*
65 	 * A mapping within a BAR, either spanning the whole BAR or just a
66 	 * range.  Without a requested region.
67 	 */
68 	PCIM_ADDR_DEVRES_TYPE_MAPPING,
69 };
70 
71 /*
72  * This struct envelops IO or MEM addresses, i.e., mappings and region
73  * requests, because those are very frequently requested and released
74  * together.
75  */
76 struct pcim_addr_devres {
77 	enum pcim_addr_devres_type type;
78 	void __iomem *baseaddr;
79 	unsigned long offset;
80 	unsigned long len;
81 	int bar;
82 };
83 
pcim_addr_devres_clear(struct pcim_addr_devres * res)84 static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res)
85 {
86 	memset(res, 0, sizeof(*res));
87 	res->bar = -1;
88 }
89 
90 /*
91  * The following functions, __pcim_*_region*, exist as counterparts to the
92  * versions from pci.c - which, unfortunately, can be in "hybrid mode", i.e.,
93  * sometimes managed, sometimes not.
94  *
95  * To separate the APIs cleanly, we define our own, simplified versions here.
96  */
97 
98 /**
99  * __pcim_request_region_range - Request a ranged region
100  * @pdev: PCI device the region belongs to
101  * @bar: BAR the range is within
102  * @offset: offset from the BAR's start address
103  * @maxlen: length in bytes, beginning at @offset
104  * @name: name of the driver requesting the resource
105  * @req_flags: flags for the request, e.g., for kernel-exclusive requests
106  *
107  * Returns: 0 on success, a negative error code on failure.
108  *
109  * Request a range within a device's PCI BAR.  Sanity check the input.
110  */
__pcim_request_region_range(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long maxlen,const char * name,int req_flags)111 static int __pcim_request_region_range(struct pci_dev *pdev, int bar,
112 				       unsigned long offset,
113 				       unsigned long maxlen,
114 				       const char *name, int req_flags)
115 {
116 	resource_size_t start = pci_resource_start(pdev, bar);
117 	resource_size_t len = pci_resource_len(pdev, bar);
118 	unsigned long dev_flags = pci_resource_flags(pdev, bar);
119 
120 	if (start == 0 || len == 0) /* Unused BAR. */
121 		return 0;
122 	if (len <= offset)
123 		return -EINVAL;
124 
125 	start += offset;
126 	len -= offset;
127 
128 	if (len > maxlen && maxlen != 0)
129 		len = maxlen;
130 
131 	if (dev_flags & IORESOURCE_IO) {
132 		if (!request_region(start, len, name))
133 			return -EBUSY;
134 	} else if (dev_flags & IORESOURCE_MEM) {
135 		if (!__request_mem_region(start, len, name, req_flags))
136 			return -EBUSY;
137 	} else {
138 		/* That's not a device we can request anything on. */
139 		return -ENODEV;
140 	}
141 
142 	return 0;
143 }
144 
__pcim_release_region_range(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long maxlen)145 static void __pcim_release_region_range(struct pci_dev *pdev, int bar,
146 					unsigned long offset,
147 					unsigned long maxlen)
148 {
149 	resource_size_t start = pci_resource_start(pdev, bar);
150 	resource_size_t len = pci_resource_len(pdev, bar);
151 	unsigned long flags = pci_resource_flags(pdev, bar);
152 
153 	if (len <= offset || start == 0)
154 		return;
155 
156 	if (len == 0 || maxlen == 0) /* This an unused BAR. Do nothing. */
157 		return;
158 
159 	start += offset;
160 	len -= offset;
161 
162 	if (len > maxlen)
163 		len = maxlen;
164 
165 	if (flags & IORESOURCE_IO)
166 		release_region(start, len);
167 	else if (flags & IORESOURCE_MEM)
168 		release_mem_region(start, len);
169 }
170 
__pcim_request_region(struct pci_dev * pdev,int bar,const char * name,int flags)171 static int __pcim_request_region(struct pci_dev *pdev, int bar,
172 				 const char *name, int flags)
173 {
174 	unsigned long offset = 0;
175 	unsigned long len = pci_resource_len(pdev, bar);
176 
177 	return __pcim_request_region_range(pdev, bar, offset, len, name, flags);
178 }
179 
__pcim_release_region(struct pci_dev * pdev,int bar)180 static void __pcim_release_region(struct pci_dev *pdev, int bar)
181 {
182 	unsigned long offset = 0;
183 	unsigned long len = pci_resource_len(pdev, bar);
184 
185 	__pcim_release_region_range(pdev, bar, offset, len);
186 }
187 
pcim_addr_resource_release(struct device * dev,void * resource_raw)188 static void pcim_addr_resource_release(struct device *dev, void *resource_raw)
189 {
190 	struct pci_dev *pdev = to_pci_dev(dev);
191 	struct pcim_addr_devres *res = resource_raw;
192 
193 	switch (res->type) {
194 	case PCIM_ADDR_DEVRES_TYPE_REGION:
195 		__pcim_release_region(pdev, res->bar);
196 		break;
197 	case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING:
198 		pci_iounmap(pdev, res->baseaddr);
199 		__pcim_release_region(pdev, res->bar);
200 		break;
201 	case PCIM_ADDR_DEVRES_TYPE_MAPPING:
202 		pci_iounmap(pdev, res->baseaddr);
203 		break;
204 	default:
205 		break;
206 	}
207 }
208 
pcim_addr_devres_alloc(struct pci_dev * pdev)209 static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pdev)
210 {
211 	struct pcim_addr_devres *res;
212 
213 	res = devres_alloc_node(pcim_addr_resource_release, sizeof(*res),
214 				GFP_KERNEL, dev_to_node(&pdev->dev));
215 	if (res)
216 		pcim_addr_devres_clear(res);
217 	return res;
218 }
219 
220 /* Just for consistency and readability. */
pcim_addr_devres_free(struct pcim_addr_devres * res)221 static inline void pcim_addr_devres_free(struct pcim_addr_devres *res)
222 {
223 	devres_free(res);
224 }
225 
226 /*
227  * Used by devres to identify a pcim_addr_devres.
228  */
pcim_addr_resources_match(struct device * dev,void * a_raw,void * b_raw)229 static int pcim_addr_resources_match(struct device *dev,
230 				     void *a_raw, void *b_raw)
231 {
232 	struct pcim_addr_devres *a, *b;
233 
234 	a = a_raw;
235 	b = b_raw;
236 
237 	if (a->type != b->type)
238 		return 0;
239 
240 	switch (a->type) {
241 	case PCIM_ADDR_DEVRES_TYPE_REGION:
242 	case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING:
243 		return a->bar == b->bar;
244 	case PCIM_ADDR_DEVRES_TYPE_MAPPING:
245 		return a->baseaddr == b->baseaddr;
246 	default:
247 		return 0;
248 	}
249 }
250 
devm_pci_unmap_iospace(struct device * dev,void * ptr)251 static void devm_pci_unmap_iospace(struct device *dev, void *ptr)
252 {
253 	struct resource **res = ptr;
254 
255 	pci_unmap_iospace(*res);
256 }
257 
258 /**
259  * devm_pci_remap_iospace - Managed pci_remap_iospace()
260  * @dev: Generic device to remap IO address for
261  * @res: Resource describing the I/O space
262  * @phys_addr: physical address of range to be mapped
263  *
264  * Managed pci_remap_iospace().  Map is automatically unmapped on driver
265  * detach.
266  */
devm_pci_remap_iospace(struct device * dev,const struct resource * res,phys_addr_t phys_addr)267 int devm_pci_remap_iospace(struct device *dev, const struct resource *res,
268 			   phys_addr_t phys_addr)
269 {
270 	const struct resource **ptr;
271 	int error;
272 
273 	ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL);
274 	if (!ptr)
275 		return -ENOMEM;
276 
277 	error = pci_remap_iospace(res, phys_addr);
278 	if (error) {
279 		devres_free(ptr);
280 	} else	{
281 		*ptr = res;
282 		devres_add(dev, ptr);
283 	}
284 
285 	return error;
286 }
287 EXPORT_SYMBOL(devm_pci_remap_iospace);
288 
289 /**
290  * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace()
291  * @dev: Generic device to remap IO address for
292  * @offset: Resource address to map
293  * @size: Size of map
294  *
295  * Managed pci_remap_cfgspace().  Map is automatically unmapped on driver
296  * detach.
297  */
devm_pci_remap_cfgspace(struct device * dev,resource_size_t offset,resource_size_t size)298 void __iomem *devm_pci_remap_cfgspace(struct device *dev,
299 				      resource_size_t offset,
300 				      resource_size_t size)
301 {
302 	void __iomem **ptr, *addr;
303 
304 	ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
305 	if (!ptr)
306 		return NULL;
307 
308 	addr = pci_remap_cfgspace(offset, size);
309 	if (addr) {
310 		*ptr = addr;
311 		devres_add(dev, ptr);
312 	} else
313 		devres_free(ptr);
314 
315 	return addr;
316 }
317 EXPORT_SYMBOL(devm_pci_remap_cfgspace);
318 
319 /**
320  * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource
321  * @dev: generic device to handle the resource for
322  * @res: configuration space resource to be handled
323  *
324  * Checks that a resource is a valid memory region, requests the memory
325  * region and ioremaps with pci_remap_cfgspace() API that ensures the
326  * proper PCI configuration space memory attributes are guaranteed.
327  *
328  * All operations are managed and will be undone on driver detach.
329  *
330  * Returns a pointer to the remapped memory or an IOMEM_ERR_PTR() encoded error
331  * code on failure. Usage example::
332  *
333  *	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
334  *	base = devm_pci_remap_cfg_resource(&pdev->dev, res);
335  *	if (IS_ERR(base))
336  *		return PTR_ERR(base);
337  */
devm_pci_remap_cfg_resource(struct device * dev,struct resource * res)338 void __iomem *devm_pci_remap_cfg_resource(struct device *dev,
339 					  struct resource *res)
340 {
341 	resource_size_t size;
342 	const char *name;
343 	void __iomem *dest_ptr;
344 
345 	BUG_ON(!dev);
346 
347 	if (!res || resource_type(res) != IORESOURCE_MEM) {
348 		dev_err(dev, "invalid resource\n");
349 		return IOMEM_ERR_PTR(-EINVAL);
350 	}
351 
352 	size = resource_size(res);
353 
354 	if (res->name)
355 		name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev),
356 				      res->name);
357 	else
358 		name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
359 	if (!name)
360 		return IOMEM_ERR_PTR(-ENOMEM);
361 
362 	if (!devm_request_mem_region(dev, res->start, size, name)) {
363 		dev_err(dev, "can't request region for resource %pR\n", res);
364 		return IOMEM_ERR_PTR(-EBUSY);
365 	}
366 
367 	dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size);
368 	if (!dest_ptr) {
369 		dev_err(dev, "ioremap failed for resource %pR\n", res);
370 		devm_release_mem_region(dev, res->start, size);
371 		dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
372 	}
373 
374 	return dest_ptr;
375 }
376 EXPORT_SYMBOL(devm_pci_remap_cfg_resource);
377 
__pcim_clear_mwi(void * pdev_raw)378 static void __pcim_clear_mwi(void *pdev_raw)
379 {
380 	struct pci_dev *pdev = pdev_raw;
381 
382 	pci_clear_mwi(pdev);
383 }
384 
385 /**
386  * pcim_set_mwi - a device-managed pci_set_mwi()
387  * @pdev: the PCI device for which MWI is enabled
388  *
389  * Managed pci_set_mwi().
390  *
391  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
392  */
pcim_set_mwi(struct pci_dev * pdev)393 int pcim_set_mwi(struct pci_dev *pdev)
394 {
395 	int ret;
396 
397 	ret = devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev);
398 	if (ret != 0)
399 		return ret;
400 
401 	ret = pci_set_mwi(pdev);
402 	if (ret != 0)
403 		devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev);
404 
405 	return ret;
406 }
407 EXPORT_SYMBOL(pcim_set_mwi);
408 
mask_contains_bar(int mask,int bar)409 static inline bool mask_contains_bar(int mask, int bar)
410 {
411 	return mask & BIT(bar);
412 }
413 
pcim_intx_restore(struct device * dev,void * data)414 static void pcim_intx_restore(struct device *dev, void *data)
415 {
416 	struct pci_dev *pdev = to_pci_dev(dev);
417 	struct pcim_intx_devres *res = data;
418 
419 	pci_intx(pdev, res->orig_intx);
420 }
421 
save_orig_intx(struct pci_dev * pdev,struct pcim_intx_devres * res)422 static void save_orig_intx(struct pci_dev *pdev, struct pcim_intx_devres *res)
423 {
424 	u16 pci_command;
425 
426 	pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
427 	res->orig_intx = !(pci_command & PCI_COMMAND_INTX_DISABLE);
428 }
429 
430 /**
431  * pcim_intx - managed pci_intx()
432  * @pdev: the PCI device to operate on
433  * @enable: boolean: whether to enable or disable PCI INTx
434  *
435  * Returns: 0 on success, -ENOMEM on error.
436  *
437  * Enable/disable PCI INTx for device @pdev.
438  * Restore the original state on driver detach.
439  */
pcim_intx(struct pci_dev * pdev,int enable)440 int pcim_intx(struct pci_dev *pdev, int enable)
441 {
442 	struct pcim_intx_devres *res;
443 	struct device *dev = &pdev->dev;
444 
445 	/*
446 	 * pcim_intx() must only restore the INTx value that existed before the
447 	 * driver was loaded, i.e., before it called pcim_intx() for the
448 	 * first time.
449 	 */
450 	res = devres_find(dev, pcim_intx_restore, NULL, NULL);
451 	if (!res) {
452 		res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL);
453 		if (!res)
454 			return -ENOMEM;
455 
456 		save_orig_intx(pdev, res);
457 		devres_add(dev, res);
458 	}
459 
460 	pci_intx(pdev, enable);
461 
462 	return 0;
463 }
464 EXPORT_SYMBOL_GPL(pcim_intx);
465 
pcim_disable_device(void * pdev_raw)466 static void pcim_disable_device(void *pdev_raw)
467 {
468 	struct pci_dev *pdev = pdev_raw;
469 
470 	if (!pdev->pinned)
471 		pci_disable_device(pdev);
472 
473 	pdev->is_managed = false;
474 }
475 
476 /**
477  * pcim_enable_device - Managed pci_enable_device()
478  * @pdev: PCI device to be initialized
479  *
480  * Returns: 0 on success, negative error code on failure.
481  *
482  * Managed pci_enable_device(). Device will automatically be disabled on
483  * driver detach.
484  */
pcim_enable_device(struct pci_dev * pdev)485 int pcim_enable_device(struct pci_dev *pdev)
486 {
487 	int ret;
488 
489 	ret = devm_add_action(&pdev->dev, pcim_disable_device, pdev);
490 	if (ret != 0)
491 		return ret;
492 
493 	/*
494 	 * We prefer removing the action in case of an error over
495 	 * devm_add_action_or_reset() because the latter could theoretically be
496 	 * disturbed by users having pinned the device too soon.
497 	 */
498 	ret = pci_enable_device(pdev);
499 	if (ret != 0) {
500 		devm_remove_action(&pdev->dev, pcim_disable_device, pdev);
501 		return ret;
502 	}
503 
504 	pdev->is_managed = true;
505 
506 	return ret;
507 }
508 EXPORT_SYMBOL(pcim_enable_device);
509 
510 /**
511  * pcim_pin_device - Pin managed PCI device
512  * @pdev: PCI device to pin
513  *
514  * Pin managed PCI device @pdev. Pinned device won't be disabled on driver
515  * detach. @pdev must have been enabled with pcim_enable_device().
516  */
pcim_pin_device(struct pci_dev * pdev)517 void pcim_pin_device(struct pci_dev *pdev)
518 {
519 	pdev->pinned = true;
520 }
521 EXPORT_SYMBOL(pcim_pin_device);
522 
pcim_iomap_release(struct device * gendev,void * res)523 static void pcim_iomap_release(struct device *gendev, void *res)
524 {
525 	/*
526 	 * Do nothing. This is legacy code.
527 	 *
528 	 * Cleanup of the mappings is now done directly through the callbacks
529 	 * registered when creating them.
530 	 */
531 }
532 
533 /**
534  * pcim_iomap_table - access iomap allocation table (DEPRECATED)
535  * @pdev: PCI device to access iomap table for
536  *
537  * Returns:
538  * Const pointer to array of __iomem pointers on success, NULL on failure.
539  *
540  * Access iomap allocation table for @dev.  If iomap table doesn't
541  * exist and @pdev is managed, it will be allocated.  All iomaps
542  * recorded in the iomap table are automatically unmapped on driver
543  * detach.
544  *
545  * This function might sleep when the table is first allocated but can
546  * be safely called without context and guaranteed to succeed once
547  * allocated.
548  *
549  * This function is DEPRECATED. Do not use it in new code. Instead, obtain a
550  * mapping's address directly from one of the pcim_* mapping functions. For
551  * example:
552  * void __iomem \*mappy = pcim_iomap(pdev, bar, length);
553  */
pcim_iomap_table(struct pci_dev * pdev)554 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
555 {
556 	struct pcim_iomap_devres *dr, *new_dr;
557 
558 	dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
559 	if (dr)
560 		return dr->table;
561 
562 	new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL,
563 				   dev_to_node(&pdev->dev));
564 	if (!new_dr)
565 		return NULL;
566 	dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
567 	return dr->table;
568 }
569 EXPORT_SYMBOL(pcim_iomap_table);
570 
571 /*
572  * Fill the legacy mapping-table, so that drivers using the old API can
573  * still get a BAR's mapping address through pcim_iomap_table().
574  */
pcim_add_mapping_to_legacy_table(struct pci_dev * pdev,void __iomem * mapping,int bar)575 static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev,
576 					    void __iomem *mapping, int bar)
577 {
578 	void __iomem **legacy_iomap_table;
579 
580 	if (!pci_bar_index_is_valid(bar))
581 		return -EINVAL;
582 
583 	legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
584 	if (!legacy_iomap_table)
585 		return -ENOMEM;
586 
587 	/* The legacy mechanism doesn't allow for duplicate mappings. */
588 	WARN_ON(legacy_iomap_table[bar]);
589 
590 	legacy_iomap_table[bar] = mapping;
591 
592 	return 0;
593 }
594 
595 /*
596  * Remove a mapping. The table only contains whole-BAR mappings, so this will
597  * never interfere with ranged mappings.
598  */
pcim_remove_mapping_from_legacy_table(struct pci_dev * pdev,void __iomem * addr)599 static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev,
600 						  void __iomem *addr)
601 {
602 	int bar;
603 	void __iomem **legacy_iomap_table;
604 
605 	legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
606 	if (!legacy_iomap_table)
607 		return;
608 
609 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
610 		if (legacy_iomap_table[bar] == addr) {
611 			legacy_iomap_table[bar] = NULL;
612 			return;
613 		}
614 	}
615 }
616 
617 /*
618  * The same as pcim_remove_mapping_from_legacy_table(), but identifies the
619  * mapping by its BAR index.
620  */
pcim_remove_bar_from_legacy_table(struct pci_dev * pdev,int bar)621 static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, int bar)
622 {
623 	void __iomem **legacy_iomap_table;
624 
625 	if (!pci_bar_index_is_valid(bar))
626 		return;
627 
628 	legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev);
629 	if (!legacy_iomap_table)
630 		return;
631 
632 	legacy_iomap_table[bar] = NULL;
633 }
634 
635 /**
636  * pcim_iomap - Managed pcim_iomap()
637  * @pdev: PCI device to iomap for
638  * @bar: BAR to iomap
639  * @maxlen: Maximum length of iomap
640  *
641  * Returns: __iomem pointer on success, NULL on failure.
642  *
643  * Managed pci_iomap(). Map is automatically unmapped on driver detach. If
644  * desired, unmap manually only with pcim_iounmap().
645  *
646  * This SHOULD only be used once per BAR.
647  *
648  * NOTE:
649  * Contrary to the other pcim_* functions, this function does not return an
650  * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwards
651  * compatibility.
652  */
pcim_iomap(struct pci_dev * pdev,int bar,unsigned long maxlen)653 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
654 {
655 	void __iomem *mapping;
656 	struct pcim_addr_devres *res;
657 
658 	if (!pci_bar_index_is_valid(bar))
659 		return NULL;
660 
661 	res = pcim_addr_devres_alloc(pdev);
662 	if (!res)
663 		return NULL;
664 	res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
665 
666 	mapping = pci_iomap(pdev, bar, maxlen);
667 	if (!mapping)
668 		goto err_iomap;
669 	res->baseaddr = mapping;
670 
671 	if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0)
672 		goto err_table;
673 
674 	devres_add(&pdev->dev, res);
675 	return mapping;
676 
677 err_table:
678 	pci_iounmap(pdev, mapping);
679 err_iomap:
680 	pcim_addr_devres_free(res);
681 	return NULL;
682 }
683 EXPORT_SYMBOL(pcim_iomap);
684 
685 /**
686  * pcim_iounmap - Managed pci_iounmap()
687  * @pdev: PCI device to iounmap for
688  * @addr: Address to unmap
689  *
690  * Managed pci_iounmap(). @addr must have been mapped using a pcim_* mapping
691  * function.
692  */
pcim_iounmap(struct pci_dev * pdev,void __iomem * addr)693 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
694 {
695 	struct pcim_addr_devres res_searched;
696 
697 	pcim_addr_devres_clear(&res_searched);
698 	res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
699 	res_searched.baseaddr = addr;
700 
701 	if (devres_release(&pdev->dev, pcim_addr_resource_release,
702 			pcim_addr_resources_match, &res_searched) != 0) {
703 		/* Doesn't exist. User passed nonsense. */
704 		return;
705 	}
706 
707 	pcim_remove_mapping_from_legacy_table(pdev, addr);
708 }
709 EXPORT_SYMBOL(pcim_iounmap);
710 
711 /**
712  * pcim_iomap_region - Request and iomap a PCI BAR
713  * @pdev: PCI device to map IO resources for
714  * @bar: Index of a BAR to map
715  * @name: Name of the driver requesting the resource
716  *
717  * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure.
718  *
719  * Mapping and region will get automatically released on driver detach. If
720  * desired, release manually only with pcim_iounmap_region().
721  */
pcim_iomap_region(struct pci_dev * pdev,int bar,const char * name)722 void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar,
723 				       const char *name)
724 {
725 	int ret;
726 	struct pcim_addr_devres *res;
727 
728 	if (!pci_bar_index_is_valid(bar))
729 		return IOMEM_ERR_PTR(-EINVAL);
730 
731 	res = pcim_addr_devres_alloc(pdev);
732 	if (!res)
733 		return IOMEM_ERR_PTR(-ENOMEM);
734 
735 	res->type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING;
736 	res->bar = bar;
737 
738 	ret = __pcim_request_region(pdev, bar, name, 0);
739 	if (ret != 0)
740 		goto err_region;
741 
742 	res->baseaddr = pci_iomap(pdev, bar, 0);
743 	if (!res->baseaddr) {
744 		ret = -EINVAL;
745 		goto err_iomap;
746 	}
747 
748 	devres_add(&pdev->dev, res);
749 	return res->baseaddr;
750 
751 err_iomap:
752 	__pcim_release_region(pdev, bar);
753 err_region:
754 	pcim_addr_devres_free(res);
755 
756 	return IOMEM_ERR_PTR(ret);
757 }
758 EXPORT_SYMBOL(pcim_iomap_region);
759 
760 /**
761  * pcim_iounmap_region - Unmap and release a PCI BAR
762  * @pdev: PCI device to operate on
763  * @bar: Index of BAR to unmap and release
764  *
765  * Unmap a BAR and release its region manually. Only pass BARs that were
766  * previously mapped by pcim_iomap_region().
767  */
pcim_iounmap_region(struct pci_dev * pdev,int bar)768 void pcim_iounmap_region(struct pci_dev *pdev, int bar)
769 {
770 	struct pcim_addr_devres res_searched;
771 
772 	pcim_addr_devres_clear(&res_searched);
773 	res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING;
774 	res_searched.bar = bar;
775 
776 	devres_release(&pdev->dev, pcim_addr_resource_release,
777 			pcim_addr_resources_match, &res_searched);
778 }
779 EXPORT_SYMBOL(pcim_iounmap_region);
780 
781 /**
782  * pcim_iomap_regions - Request and iomap PCI BARs (DEPRECATED)
783  * @pdev: PCI device to map IO resources for
784  * @mask: Mask of BARs to request and iomap
785  * @name: Name of the driver requesting the resources
786  *
787  * Returns: 0 on success, negative error code on failure.
788  *
789  * Request and iomap regions specified by @mask.
790  *
791  * This function is DEPRECATED. Do not use it in new code.
792  * Use pcim_iomap_region() instead.
793  */
pcim_iomap_regions(struct pci_dev * pdev,int mask,const char * name)794 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
795 {
796 	int ret;
797 	int bar;
798 	void __iomem *mapping;
799 
800 	for (bar = 0; bar < DEVICE_COUNT_RESOURCE; bar++) {
801 		if (!mask_contains_bar(mask, bar))
802 			continue;
803 
804 		mapping = pcim_iomap_region(pdev, bar, name);
805 		if (IS_ERR(mapping)) {
806 			ret = PTR_ERR(mapping);
807 			goto err;
808 		}
809 		ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar);
810 		if (ret != 0)
811 			goto err;
812 	}
813 
814 	return 0;
815 
816 err:
817 	while (--bar >= 0) {
818 		pcim_iounmap_region(pdev, bar);
819 		pcim_remove_bar_from_legacy_table(pdev, bar);
820 	}
821 
822 	return ret;
823 }
824 EXPORT_SYMBOL(pcim_iomap_regions);
825 
_pcim_request_region(struct pci_dev * pdev,int bar,const char * name,int request_flags)826 static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name,
827 				int request_flags)
828 {
829 	int ret;
830 	struct pcim_addr_devres *res;
831 
832 	if (!pci_bar_index_is_valid(bar))
833 		return -EINVAL;
834 
835 	res = pcim_addr_devres_alloc(pdev);
836 	if (!res)
837 		return -ENOMEM;
838 	res->type = PCIM_ADDR_DEVRES_TYPE_REGION;
839 	res->bar = bar;
840 
841 	ret = __pcim_request_region(pdev, bar, name, request_flags);
842 	if (ret != 0) {
843 		pcim_addr_devres_free(res);
844 		return ret;
845 	}
846 
847 	devres_add(&pdev->dev, res);
848 	return 0;
849 }
850 
851 /**
852  * pcim_request_region - Request a PCI BAR
853  * @pdev: PCI device to request region for
854  * @bar: Index of BAR to request
855  * @name: Name of the driver requesting the resource
856  *
857  * Returns: 0 on success, a negative error code on failure.
858  *
859  * Request region specified by @bar.
860  *
861  * The region will automatically be released on driver detach. If desired,
862  * release manually only with pcim_release_region().
863  */
pcim_request_region(struct pci_dev * pdev,int bar,const char * name)864 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name)
865 {
866 	return _pcim_request_region(pdev, bar, name, 0);
867 }
868 EXPORT_SYMBOL(pcim_request_region);
869 
870 /**
871  * pcim_request_region_exclusive - Request a PCI BAR exclusively
872  * @pdev: PCI device to request region for
873  * @bar: Index of BAR to request
874  * @name: Name of the driver requesting the resource
875  *
876  * Returns: 0 on success, a negative error code on failure.
877  *
878  * Request region specified by @bar exclusively.
879  *
880  * The region will automatically be released on driver detach. If desired,
881  * release manually only with pcim_release_region().
882  */
pcim_request_region_exclusive(struct pci_dev * pdev,int bar,const char * name)883 int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name)
884 {
885 	return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE);
886 }
887 
888 /**
889  * pcim_release_region - Release a PCI BAR
890  * @pdev: PCI device to operate on
891  * @bar: Index of BAR to release
892  *
893  * Release a region manually that was previously requested by
894  * pcim_request_region().
895  */
pcim_release_region(struct pci_dev * pdev,int bar)896 void pcim_release_region(struct pci_dev *pdev, int bar)
897 {
898 	struct pcim_addr_devres res_searched;
899 
900 	pcim_addr_devres_clear(&res_searched);
901 	res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION;
902 	res_searched.bar = bar;
903 
904 	devres_release(&pdev->dev, pcim_addr_resource_release,
905 			pcim_addr_resources_match, &res_searched);
906 }
907 
908 
909 /**
910  * pcim_release_all_regions - Release all regions of a PCI-device
911  * @pdev: the PCI device
912  *
913  * Release all regions previously requested through pcim_request_region()
914  * or pcim_request_all_regions().
915  *
916  * Can be called from any context, i.e., not necessarily as a counterpart to
917  * pcim_request_all_regions().
918  */
pcim_release_all_regions(struct pci_dev * pdev)919 static void pcim_release_all_regions(struct pci_dev *pdev)
920 {
921 	int bar;
922 
923 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
924 		pcim_release_region(pdev, bar);
925 }
926 
927 /**
928  * pcim_request_all_regions - Request all regions
929  * @pdev: PCI device to map IO resources for
930  * @name: name of the driver requesting the resources
931  *
932  * Returns: 0 on success, negative error code on failure.
933  *
934  * Requested regions will automatically be released at driver detach. If
935  * desired, release individual regions with pcim_release_region() or all of
936  * them at once with pcim_release_all_regions().
937  */
pcim_request_all_regions(struct pci_dev * pdev,const char * name)938 int pcim_request_all_regions(struct pci_dev *pdev, const char *name)
939 {
940 	int ret;
941 	int bar;
942 
943 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
944 		ret = pcim_request_region(pdev, bar, name);
945 		if (ret != 0)
946 			goto err;
947 	}
948 
949 	return 0;
950 
951 err:
952 	pcim_release_all_regions(pdev);
953 
954 	return ret;
955 }
956 EXPORT_SYMBOL(pcim_request_all_regions);
957 
958 /**
959  * pcim_iounmap_regions - Unmap and release PCI BARs (DEPRECATED)
960  * @pdev: PCI device to map IO resources for
961  * @mask: Mask of BARs to unmap and release
962  *
963  * Unmap and release regions specified by @mask.
964  *
965  * This function is DEPRECATED. Do not use it in new code.
966  * Use pcim_iounmap_region() instead.
967  */
pcim_iounmap_regions(struct pci_dev * pdev,int mask)968 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
969 {
970 	int i;
971 
972 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
973 		if (!mask_contains_bar(mask, i))
974 			continue;
975 
976 		pcim_iounmap_region(pdev, i);
977 		pcim_remove_bar_from_legacy_table(pdev, i);
978 	}
979 }
980 EXPORT_SYMBOL(pcim_iounmap_regions);
981 
982 /**
983  * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR
984  * @pdev: PCI device to map IO resources for
985  * @bar: Index of the BAR
986  * @offset: Offset from the begin of the BAR
987  * @len: Length in bytes for the mapping
988  *
989  * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure.
990  *
991  * Creates a new IO-Mapping within the specified @bar, ranging from @offset to
992  * @offset + @len.
993  *
994  * The mapping will automatically get unmapped on driver detach. If desired,
995  * release manually only with pcim_iounmap().
996  */
pcim_iomap_range(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long len)997 void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar,
998 		unsigned long offset, unsigned long len)
999 {
1000 	void __iomem *mapping;
1001 	struct pcim_addr_devres *res;
1002 
1003 	if (!pci_bar_index_is_valid(bar))
1004 		return IOMEM_ERR_PTR(-EINVAL);
1005 
1006 	res = pcim_addr_devres_alloc(pdev);
1007 	if (!res)
1008 		return IOMEM_ERR_PTR(-ENOMEM);
1009 
1010 	mapping = pci_iomap_range(pdev, bar, offset, len);
1011 	if (!mapping) {
1012 		pcim_addr_devres_free(res);
1013 		return IOMEM_ERR_PTR(-EINVAL);
1014 	}
1015 
1016 	res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING;
1017 	res->baseaddr = mapping;
1018 
1019 	/*
1020 	 * Ranged mappings don't get added to the legacy-table, since the table
1021 	 * only ever keeps track of whole BARs.
1022 	 */
1023 
1024 	devres_add(&pdev->dev, res);
1025 	return mapping;
1026 }
1027 EXPORT_SYMBOL(pcim_iomap_range);
1028