blob: e1930dbab2dae09f2d83ec62b0eeb123cbe5320b [file] [log] [blame]
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +02001/*
2 * Implement the default iomap interfaces
3 *
4 * (C) Copyright 2004 Linus Torvalds
5 */
6#include <linux/pci.h>
7#include <linux/io.h>
8
9#include <linux/export.h>
10
11#ifdef CONFIG_PCI
12/**
Michael S. Tsirkineb29d8d2013-05-29 11:52:21 +093013 * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
14 * @dev: PCI device that owns the BAR
15 * @bar: BAR number
16 * @offset: map memory at the given offset in BAR
17 * @maxlen: max length of the memory to map
18 *
19 * Using this function you will get a __iomem address to your device BAR.
20 * You can access it using ioread*() and iowrite*(). These functions hide
21 * the details if this is a MMIO or PIO address space and will just do what
22 * you expect from them in the correct way.
23 *
24 * @maxlen specifies the maximum length to map. If you want to get access to
25 * the complete BAR from offset to the end, pass %0 here.
26 * */
27void __iomem *pci_iomap_range(struct pci_dev *dev,
28 int bar,
29 unsigned long offset,
30 unsigned long maxlen)
31{
32 resource_size_t start = pci_resource_start(dev, bar);
33 resource_size_t len = pci_resource_len(dev, bar);
34 unsigned long flags = pci_resource_flags(dev, bar);
35
36 if (len <= offset || !start)
37 return NULL;
38 len -= offset;
39 start += offset;
40 if (maxlen && len > maxlen)
41 len = maxlen;
42 if (flags & IORESOURCE_IO)
43 return __pci_ioport_map(dev, start, len);
Dan Williams92b19ff2015-08-10 23:07:06 -040044 if (flags & IORESOURCE_MEM)
45 return ioremap(start, len);
Michael S. Tsirkineb29d8d2013-05-29 11:52:21 +093046 /* What? */
47 return NULL;
48}
49EXPORT_SYMBOL(pci_iomap_range);
50
51/**
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +020052 * pci_iomap - create a virtual mapping cookie for a PCI BAR
53 * @dev: PCI device that owns the BAR
54 * @bar: BAR number
55 * @maxlen: length of the memory to map
56 *
57 * Using this function you will get a __iomem address to your device BAR.
58 * You can access it using ioread*() and iowrite*(). These functions hide
59 * the details if this is a MMIO or PIO address space and will just do what
60 * you expect from them in the correct way.
61 *
62 * @maxlen specifies the maximum length to map. If you want to get access to
63 * the complete BAR without checking for its length first, pass %0 here.
64 * */
65void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
66{
Michael S. Tsirkineb29d8d2013-05-29 11:52:21 +093067 return pci_iomap_range(dev, bar, 0, maxlen);
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +020068}
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +020069EXPORT_SYMBOL(pci_iomap);
70#endif /* CONFIG_PCI */