ioremap_32.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/io.h>
  3. #include <linux/slab.h>
  4. #include <linux/vmalloc.h>
  5. #include <mm/mmu_decl.h>
  6. void __iomem *ioremap_wt(phys_addr_t addr, unsigned long size)
  7. {
  8. pgprot_t prot = pgprot_cached_wthru(PAGE_KERNEL);
  9. return __ioremap_caller(addr, size, prot, __builtin_return_address(0));
  10. }
  11. EXPORT_SYMBOL(ioremap_wt);
  12. void __iomem *
  13. __ioremap_caller(phys_addr_t addr, unsigned long size, pgprot_t prot, void *caller)
  14. {
  15. unsigned long v;
  16. phys_addr_t p, offset;
  17. int err;
  18. /*
  19. * Choose an address to map it to.
  20. * Once the vmalloc system is running, we use it.
  21. * Before then, we use space going down from IOREMAP_TOP
  22. * (ioremap_bot records where we're up to).
  23. */
  24. p = addr & PAGE_MASK;
  25. offset = addr & ~PAGE_MASK;
  26. size = PAGE_ALIGN(addr + size) - p;
  27. /*
  28. * If the address lies within the first 16 MB, assume it's in ISA
  29. * memory space
  30. */
  31. if (p < 16 * 1024 * 1024)
  32. p += _ISA_MEM_BASE;
  33. #ifndef CONFIG_CRASH_DUMP
  34. /*
  35. * Don't allow anybody to remap normal RAM that we're using.
  36. * mem_init() sets high_memory so only do the check after that.
  37. */
  38. if (slab_is_available() && p <= virt_to_phys(high_memory - 1) &&
  39. page_is_ram(__phys_to_pfn(p))) {
  40. pr_warn("%s(): phys addr 0x%llx is RAM lr %ps\n", __func__,
  41. (unsigned long long)p, __builtin_return_address(0));
  42. return NULL;
  43. }
  44. #endif
  45. if (size == 0)
  46. return NULL;
  47. /*
  48. * Is it already mapped? Perhaps overlapped by a previous
  49. * mapping.
  50. */
  51. v = p_block_mapped(p);
  52. if (v)
  53. return (void __iomem *)v + offset;
  54. if (slab_is_available())
  55. return do_ioremap(p, offset, size, prot, caller);
  56. /*
  57. * Should check if it is a candidate for a BAT mapping
  58. */
  59. pr_warn("ioremap() called early from %pS. Use early_ioremap() instead\n", caller);
  60. err = early_ioremap_range(ioremap_bot - size - PAGE_SIZE, p, size, prot);
  61. if (err)
  62. return NULL;
  63. ioremap_bot -= size + PAGE_SIZE;
  64. return (void __iomem *)ioremap_bot + offset;
  65. }
  66. void iounmap(volatile void __iomem *addr)
  67. {
  68. /*
  69. * If mapped by BATs then there is nothing to do.
  70. * Calling vfree() generates a benign warning.
  71. */
  72. if (v_block_mapped((unsigned long)addr))
  73. return;
  74. if (addr > high_memory && (unsigned long)addr < ioremap_bot)
  75. vunmap((void *)(PAGE_MASK & (unsigned long)addr));
  76. }
  77. EXPORT_SYMBOL(iounmap);