ioremap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P.
  4. * Bjorn Helgaas <[email protected]>
  5. */
  6. #include <linux/compiler.h>
  7. #include <linux/module.h>
  8. #include <linux/efi.h>
  9. #include <linux/io.h>
  10. #include <linux/mm.h>
  11. #include <linux/vmalloc.h>
  12. #include <asm/io.h>
  13. #include <asm/meminit.h>
  14. static inline void __iomem *
  15. __ioremap_uc(unsigned long phys_addr)
  16. {
  17. return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr);
  18. }
  19. void __iomem *
  20. early_ioremap (unsigned long phys_addr, unsigned long size)
  21. {
  22. u64 attr;
  23. attr = kern_mem_attribute(phys_addr, size);
  24. if (attr & EFI_MEMORY_WB)
  25. return (void __iomem *) phys_to_virt(phys_addr);
  26. return __ioremap_uc(phys_addr);
  27. }
  28. void __iomem *
  29. ioremap (unsigned long phys_addr, unsigned long size)
  30. {
  31. void __iomem *addr;
  32. struct vm_struct *area;
  33. unsigned long offset;
  34. pgprot_t prot;
  35. u64 attr;
  36. unsigned long gran_base, gran_size;
  37. unsigned long page_base;
  38. /*
  39. * For things in kern_memmap, we must use the same attribute
  40. * as the rest of the kernel. For more details, see
  41. * Documentation/ia64/aliasing.rst.
  42. */
  43. attr = kern_mem_attribute(phys_addr, size);
  44. if (attr & EFI_MEMORY_WB)
  45. return (void __iomem *) phys_to_virt(phys_addr);
  46. else if (attr & EFI_MEMORY_UC)
  47. return __ioremap_uc(phys_addr);
  48. /*
  49. * Some chipsets don't support UC access to memory. If
  50. * WB is supported for the whole granule, we prefer that.
  51. */
  52. gran_base = GRANULEROUNDDOWN(phys_addr);
  53. gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base;
  54. if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB)
  55. return (void __iomem *) phys_to_virt(phys_addr);
  56. /*
  57. * WB is not supported for the whole granule, so we can't use
  58. * the region 7 identity mapping. If we can safely cover the
  59. * area with kernel page table mappings, we can use those
  60. * instead.
  61. */
  62. page_base = phys_addr & PAGE_MASK;
  63. size = PAGE_ALIGN(phys_addr + size) - page_base;
  64. if (efi_mem_attribute(page_base, size) & EFI_MEMORY_WB) {
  65. prot = PAGE_KERNEL;
  66. /*
  67. * Mappings have to be page-aligned
  68. */
  69. offset = phys_addr & ~PAGE_MASK;
  70. phys_addr &= PAGE_MASK;
  71. /*
  72. * Ok, go for it..
  73. */
  74. area = get_vm_area(size, VM_IOREMAP);
  75. if (!area)
  76. return NULL;
  77. area->phys_addr = phys_addr;
  78. addr = (void __iomem *) area->addr;
  79. if (ioremap_page_range((unsigned long) addr,
  80. (unsigned long) addr + size, phys_addr, prot)) {
  81. vunmap((void __force *) addr);
  82. return NULL;
  83. }
  84. return (void __iomem *) (offset + (char __iomem *)addr);
  85. }
  86. return __ioremap_uc(phys_addr);
  87. }
  88. EXPORT_SYMBOL(ioremap);
  89. void __iomem *
  90. ioremap_uc(unsigned long phys_addr, unsigned long size)
  91. {
  92. if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
  93. return NULL;
  94. return __ioremap_uc(phys_addr);
  95. }
  96. EXPORT_SYMBOL(ioremap_uc);
  97. void
  98. early_iounmap (volatile void __iomem *addr, unsigned long size)
  99. {
  100. }
  101. void
  102. iounmap (volatile void __iomem *addr)
  103. {
  104. if (REGION_NUMBER(addr) == RGN_GATE)
  105. vunmap((void *) ((unsigned long) addr & PAGE_MASK));
  106. }
  107. EXPORT_SYMBOL(iounmap);