Back to home page

LXR

 
 

    


0001 /*
0002  * Implement the default iomap interfaces
0003  *
0004  * (C) Copyright 2004 Linus Torvalds
0005  */
0006 #include <linux/pci.h>
0007 #include <linux/io.h>
0008 
0009 #include <linux/export.h>
0010 
0011 #ifdef CONFIG_PCI
0012 /**
0013  * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
0014  * @dev: PCI device that owns the BAR
0015  * @bar: BAR number
0016  * @offset: map memory at the given offset in BAR
0017  * @maxlen: max length of the memory to map
0018  *
0019  * Using this function you will get a __iomem address to your device BAR.
0020  * You can access it using ioread*() and iowrite*(). These functions hide
0021  * the details if this is a MMIO or PIO address space and will just do what
0022  * you expect from them in the correct way.
0023  *
0024  * @maxlen specifies the maximum length to map. If you want to get access to
0025  * the complete BAR from offset to the end, pass %0 here.
0026  * */
0027 void __iomem *pci_iomap_range(struct pci_dev *dev,
0028                   int bar,
0029                   unsigned long offset,
0030                   unsigned long maxlen)
0031 {
0032     resource_size_t start = pci_resource_start(dev, bar);
0033     resource_size_t len = pci_resource_len(dev, bar);
0034     unsigned long flags = pci_resource_flags(dev, bar);
0035 
0036     if (len <= offset || !start)
0037         return NULL;
0038     len -= offset;
0039     start += offset;
0040     if (maxlen && len > maxlen)
0041         len = maxlen;
0042     if (flags & IORESOURCE_IO)
0043         return __pci_ioport_map(dev, start, len);
0044     if (flags & IORESOURCE_MEM)
0045         return ioremap(start, len);
0046     /* What? */
0047     return NULL;
0048 }
0049 EXPORT_SYMBOL(pci_iomap_range);
0050 
0051 /**
0052  * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
0053  * @dev: PCI device that owns the BAR
0054  * @bar: BAR number
0055  * @offset: map memory at the given offset in BAR
0056  * @maxlen: max length of the memory to map
0057  *
0058  * Using this function you will get a __iomem address to your device BAR.
0059  * You can access it using ioread*() and iowrite*(). These functions hide
0060  * the details if this is a MMIO or PIO address space and will just do what
0061  * you expect from them in the correct way. When possible write combining
0062  * is used.
0063  *
0064  * @maxlen specifies the maximum length to map. If you want to get access to
0065  * the complete BAR from offset to the end, pass %0 here.
0066  * */
0067 void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
0068                  int bar,
0069                  unsigned long offset,
0070                  unsigned long maxlen)
0071 {
0072     resource_size_t start = pci_resource_start(dev, bar);
0073     resource_size_t len = pci_resource_len(dev, bar);
0074     unsigned long flags = pci_resource_flags(dev, bar);
0075 
0076 
0077     if (flags & IORESOURCE_IO)
0078         return NULL;
0079 
0080     if (len <= offset || !start)
0081         return NULL;
0082 
0083     len -= offset;
0084     start += offset;
0085     if (maxlen && len > maxlen)
0086         len = maxlen;
0087 
0088     if (flags & IORESOURCE_MEM)
0089         return ioremap_wc(start, len);
0090 
0091     /* What? */
0092     return NULL;
0093 }
0094 EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
0095 
0096 /**
0097  * pci_iomap - create a virtual mapping cookie for a PCI BAR
0098  * @dev: PCI device that owns the BAR
0099  * @bar: BAR number
0100  * @maxlen: length of the memory to map
0101  *
0102  * Using this function you will get a __iomem address to your device BAR.
0103  * You can access it using ioread*() and iowrite*(). These functions hide
0104  * the details if this is a MMIO or PIO address space and will just do what
0105  * you expect from them in the correct way.
0106  *
0107  * @maxlen specifies the maximum length to map. If you want to get access to
0108  * the complete BAR without checking for its length first, pass %0 here.
0109  * */
0110 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
0111 {
0112     return pci_iomap_range(dev, bar, 0, maxlen);
0113 }
0114 EXPORT_SYMBOL(pci_iomap);
0115 
0116 /**
0117  * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
0118  * @dev: PCI device that owns the BAR
0119  * @bar: BAR number
0120  * @maxlen: length of the memory to map
0121  *
0122  * Using this function you will get a __iomem address to your device BAR.
0123  * You can access it using ioread*() and iowrite*(). These functions hide
0124  * the details if this is a MMIO or PIO address space and will just do what
0125  * you expect from them in the correct way. When possible write combining
0126  * is used.
0127  *
0128  * @maxlen specifies the maximum length to map. If you want to get access to
0129  * the complete BAR without checking for its length first, pass %0 here.
0130  * */
0131 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
0132 {
0133     return pci_iomap_wc_range(dev, bar, 0, maxlen);
0134 }
0135 EXPORT_SYMBOL_GPL(pci_iomap_wc);
0136 #endif /* CONFIG_PCI */