iomem.c 4.6 KB

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