pci_iomap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  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. #include <linux/export.h>
  9. #ifdef CONFIG_PCI
  10. /**
  11. * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
  12. * @dev: PCI device that owns the BAR
  13. * @bar: BAR number
  14. * @offset: map memory at the given offset in BAR
  15. * @maxlen: max length of the memory to map
  16. *
  17. * Using this function you will get a __iomem address to your device BAR.
  18. * You can access it using ioread*() and iowrite*(). These functions hide
  19. * the details if this is a MMIO or PIO address space and will just do what
  20. * you expect from them in the correct way.
  21. *
  22. * @maxlen specifies the maximum length to map. If you want to get access to
  23. * the complete BAR from offset to the end, pass %0 here.
  24. * */
  25. void __iomem *pci_iomap_range(struct pci_dev *dev,
  26. int bar,
  27. unsigned long offset,
  28. unsigned long maxlen)
  29. {
  30. resource_size_t start = pci_resource_start(dev, bar);
  31. resource_size_t len = pci_resource_len(dev, bar);
  32. unsigned long flags = pci_resource_flags(dev, bar);
  33. if (len <= offset || !start)
  34. return NULL;
  35. len -= offset;
  36. start += offset;
  37. if (maxlen && len > maxlen)
  38. len = maxlen;
  39. if (flags & IORESOURCE_IO)
  40. return __pci_ioport_map(dev, start, len);
  41. if (flags & IORESOURCE_MEM)
  42. return ioremap(start, len);
  43. /* What? */
  44. return NULL;
  45. }
  46. EXPORT_SYMBOL(pci_iomap_range);
  47. /**
  48. * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
  49. * @dev: PCI device that owns the BAR
  50. * @bar: BAR number
  51. * @offset: map memory at the given offset in BAR
  52. * @maxlen: max length of the memory to map
  53. *
  54. * Using this function you will get a __iomem address to your device BAR.
  55. * You can access it using ioread*() and iowrite*(). These functions hide
  56. * the details if this is a MMIO or PIO address space and will just do what
  57. * you expect from them in the correct way. When possible write combining
  58. * is used.
  59. *
  60. * @maxlen specifies the maximum length to map. If you want to get access to
  61. * the complete BAR from offset to the end, pass %0 here.
  62. * */
  63. void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
  64. int bar,
  65. unsigned long offset,
  66. unsigned long maxlen)
  67. {
  68. resource_size_t start = pci_resource_start(dev, bar);
  69. resource_size_t len = pci_resource_len(dev, bar);
  70. unsigned long flags = pci_resource_flags(dev, bar);
  71. if (flags & IORESOURCE_IO)
  72. return NULL;
  73. if (len <= offset || !start)
  74. return NULL;
  75. len -= offset;
  76. start += offset;
  77. if (maxlen && len > maxlen)
  78. len = maxlen;
  79. if (flags & IORESOURCE_MEM)
  80. return ioremap_wc(start, len);
  81. /* What? */
  82. return NULL;
  83. }
  84. EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
  85. /**
  86. * pci_iomap - create a virtual mapping cookie for a PCI BAR
  87. * @dev: PCI device that owns the BAR
  88. * @bar: BAR number
  89. * @maxlen: length of the memory to map
  90. *
  91. * Using this function you will get a __iomem address to your device BAR.
  92. * You can access it using ioread*() and iowrite*(). These functions hide
  93. * the details if this is a MMIO or PIO address space and will just do what
  94. * you expect from them in the correct way.
  95. *
  96. * @maxlen specifies the maximum length to map. If you want to get access to
  97. * the complete BAR without checking for its length first, pass %0 here.
  98. * */
  99. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  100. {
  101. return pci_iomap_range(dev, bar, 0, maxlen);
  102. }
  103. EXPORT_SYMBOL(pci_iomap);
  104. /**
  105. * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
  106. * @dev: PCI device that owns the BAR
  107. * @bar: BAR number
  108. * @maxlen: length of the memory to map
  109. *
  110. * Using this function you will get a __iomem address to your device BAR.
  111. * You can access it using ioread*() and iowrite*(). These functions hide
  112. * the details if this is a MMIO or PIO address space and will just do what
  113. * you expect from them in the correct way. When possible write combining
  114. * is used.
  115. *
  116. * @maxlen specifies the maximum length to map. If you want to get access to
  117. * the complete BAR without checking for its length first, pass %0 here.
  118. * */
  119. void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
  120. {
  121. return pci_iomap_wc_range(dev, bar, 0, maxlen);
  122. }
  123. EXPORT_SYMBOL_GPL(pci_iomap_wc);
  124. #endif /* CONFIG_PCI */