iomem.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #include <linux/device.h>
  3. #include <linux/types.h>
  4. #include <linux/io.h>
  5. #include <linux/mm.h>
  6. #ifndef ioremap_cache
  7. /* temporary while we convert existing ioremap_cache users to memremap */
  8. __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
  9. {
  10. return ioremap(offset, size);
  11. }
  12. #endif
  13. #ifndef arch_memremap_wb
  14. static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
  15. {
  16. return (__force void *)ioremap_cache(offset, size);
  17. }
  18. #endif
  19. #ifndef arch_memremap_can_ram_remap
  20. static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
  21. unsigned long flags)
  22. {
  23. return true;
  24. }
  25. #endif
  26. static void *try_ram_remap(resource_size_t offset, size_t size,
  27. unsigned long flags)
  28. {
  29. unsigned long pfn = PHYS_PFN(offset);
  30. /* In the simple case just return the existing linear address */
  31. if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)) &&
  32. arch_memremap_can_ram_remap(offset, size, flags))
  33. return __va(offset);
  34. return NULL; /* fallback to arch_memremap_wb */
  35. }
  36. /**
  37. * memremap() - remap an iomem_resource as cacheable memory
  38. * @offset: iomem resource start address
  39. * @size: size of remap
  40. * @flags: any of MEMREMAP_WB, MEMREMAP_WT, MEMREMAP_WC,
  41. * MEMREMAP_ENC, MEMREMAP_DEC
  42. *
  43. * memremap() is "ioremap" for cases where it is known that the resource
  44. * being mapped does not have i/o side effects and the __iomem
  45. * annotation is not applicable. In the case of multiple flags, the different
  46. * mapping types will be attempted in the order listed below until one of
  47. * them succeeds.
  48. *
  49. * MEMREMAP_WB - matches the default mapping for System RAM on
  50. * the architecture. This is usually a read-allocate write-back cache.
  51. * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
  52. * memremap() will bypass establishing a new mapping and instead return
  53. * a pointer into the direct map.
  54. *
  55. * MEMREMAP_WT - establish a mapping whereby writes either bypass the
  56. * cache or are written through to memory and never exist in a
  57. * cache-dirty state with respect to program visibility. Attempts to
  58. * map System RAM with this mapping type will fail.
  59. *
  60. * MEMREMAP_WC - establish a writecombine mapping, whereby writes may
  61. * be coalesced together (e.g. in the CPU's write buffers), but is otherwise
  62. * uncached. Attempts to map System RAM with this mapping type will fail.
  63. */
  64. void *memremap(resource_size_t offset, size_t size, unsigned long flags)
  65. {
  66. int is_ram = region_intersects(offset, size,
  67. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  68. void *addr = NULL;
  69. if (!flags)
  70. return NULL;
  71. if (is_ram == REGION_MIXED) {
  72. WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
  73. &offset, (unsigned long) size);
  74. return NULL;
  75. }
  76. /* Try all mapping types requested until one returns non-NULL */
  77. if (flags & MEMREMAP_WB) {
  78. /*
  79. * MEMREMAP_WB is special in that it can be satisfied
  80. * from the direct map. Some archs depend on the
  81. * capability of memremap() to autodetect cases where
  82. * the requested range is potentially in System RAM.
  83. */
  84. if (is_ram == REGION_INTERSECTS)
  85. addr = try_ram_remap(offset, size, flags);
  86. if (!addr)
  87. addr = arch_memremap_wb(offset, size);
  88. }
  89. /*
  90. * If we don't have a mapping yet and other request flags are
  91. * present then we will be attempting to establish a new virtual
  92. * address mapping. Enforce that this mapping is not aliasing
  93. * System RAM.
  94. */
  95. if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
  96. WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
  97. &offset, (unsigned long) size);
  98. return NULL;
  99. }
  100. if (!addr && (flags & MEMREMAP_WT))
  101. addr = ioremap_wt(offset, size);
  102. if (!addr && (flags & MEMREMAP_WC))
  103. addr = ioremap_wc(offset, size);
  104. return addr;
  105. }
  106. EXPORT_SYMBOL(memremap);
  107. void memunmap(void *addr)
  108. {
  109. if (is_ioremap_addr(addr))
  110. iounmap((void __iomem *) addr);
  111. }
  112. EXPORT_SYMBOL(memunmap);
  113. static void devm_memremap_release(struct device *dev, void *res)
  114. {
  115. memunmap(*(void **)res);
  116. }
  117. static int devm_memremap_match(struct device *dev, void *res, void *match_data)
  118. {
  119. return *(void **)res == match_data;
  120. }
  121. void *devm_memremap(struct device *dev, resource_size_t offset,
  122. size_t size, unsigned long flags)
  123. {
  124. void **ptr, *addr;
  125. ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
  126. dev_to_node(dev));
  127. if (!ptr)
  128. return ERR_PTR(-ENOMEM);
  129. addr = memremap(offset, size, flags);
  130. if (addr) {
  131. *ptr = addr;
  132. devres_add(dev, ptr);
  133. } else {
  134. devres_free(ptr);
  135. return ERR_PTR(-ENXIO);
  136. }
  137. return addr;
  138. }
  139. EXPORT_SYMBOL(devm_memremap);
  140. void devm_memunmap(struct device *dev, void *addr)
  141. {
  142. WARN_ON(devres_release(dev, devm_memremap_release,
  143. devm_memremap_match, addr));
  144. }
  145. EXPORT_SYMBOL(devm_memunmap);