ioremap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Re-map IO memory to kernel address space so that we can access it.
  4. * This is needed for high PCI addresses that aren't mapped in the
  5. * 640k-1MB IO memory area on PC's
  6. *
  7. * (C) Copyright 1995 1996 Linus Torvalds
  8. */
  9. #include <linux/vmalloc.h>
  10. #include <linux/mm.h>
  11. #include <linux/sched.h>
  12. #include <linux/io.h>
  13. #include <linux/export.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/pgtable.h>
  16. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  17. static int __read_mostly ioremap_p4d_capable;
  18. static int __read_mostly ioremap_pud_capable;
  19. static int __read_mostly ioremap_pmd_capable;
  20. static int __read_mostly ioremap_huge_disabled;
  21. static int __init set_nohugeiomap(char *str)
  22. {
  23. ioremap_huge_disabled = 1;
  24. return 0;
  25. }
  26. early_param("nohugeiomap", set_nohugeiomap);
  27. void __init ioremap_huge_init(void)
  28. {
  29. if (!ioremap_huge_disabled) {
  30. if (arch_ioremap_pud_supported())
  31. ioremap_pud_capable = 1;
  32. if (arch_ioremap_pmd_supported())
  33. ioremap_pmd_capable = 1;
  34. }
  35. }
  36. static inline int ioremap_p4d_enabled(void)
  37. {
  38. return ioremap_p4d_capable;
  39. }
  40. static inline int ioremap_pud_enabled(void)
  41. {
  42. return ioremap_pud_capable;
  43. }
  44. static inline int ioremap_pmd_enabled(void)
  45. {
  46. return ioremap_pmd_capable;
  47. }
  48. #else /* !CONFIG_HAVE_ARCH_HUGE_VMAP */
  49. static inline int ioremap_p4d_enabled(void) { return 0; }
  50. static inline int ioremap_pud_enabled(void) { return 0; }
  51. static inline int ioremap_pmd_enabled(void) { return 0; }
  52. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */
  53. static int ioremap_pte_range(pmd_t *pmd, unsigned long addr,
  54. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  55. {
  56. pte_t *pte;
  57. u64 pfn;
  58. pfn = phys_addr >> PAGE_SHIFT;
  59. pte = pte_alloc_kernel(pmd, addr);
  60. if (!pte)
  61. return -ENOMEM;
  62. do {
  63. BUG_ON(!pte_none(*pte));
  64. set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
  65. pfn++;
  66. } while (pte++, addr += PAGE_SIZE, addr != end);
  67. return 0;
  68. }
  69. static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr,
  70. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  71. {
  72. pmd_t *pmd;
  73. unsigned long next;
  74. phys_addr -= addr;
  75. pmd = pmd_alloc(&init_mm, pud, addr);
  76. if (!pmd)
  77. return -ENOMEM;
  78. do {
  79. next = pmd_addr_end(addr, end);
  80. if (ioremap_pmd_enabled() &&
  81. ((next - addr) == PMD_SIZE) &&
  82. IS_ALIGNED(phys_addr + addr, PMD_SIZE) &&
  83. pmd_free_pte_page(pmd, addr)) {
  84. if (pmd_set_huge(pmd, phys_addr + addr, prot))
  85. continue;
  86. }
  87. if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, prot))
  88. return -ENOMEM;
  89. } while (pmd++, addr = next, addr != end);
  90. return 0;
  91. }
  92. static inline int ioremap_pud_range(p4d_t *p4d, unsigned long addr,
  93. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  94. {
  95. pud_t *pud;
  96. unsigned long next;
  97. phys_addr -= addr;
  98. pud = pud_alloc(&init_mm, p4d, addr);
  99. if (!pud)
  100. return -ENOMEM;
  101. do {
  102. next = pud_addr_end(addr, end);
  103. if (ioremap_pud_enabled() &&
  104. ((next - addr) == PUD_SIZE) &&
  105. IS_ALIGNED(phys_addr + addr, PUD_SIZE) &&
  106. pud_free_pmd_page(pud, addr)) {
  107. if (pud_set_huge(pud, phys_addr + addr, prot))
  108. continue;
  109. }
  110. if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, prot))
  111. return -ENOMEM;
  112. } while (pud++, addr = next, addr != end);
  113. return 0;
  114. }
  115. static inline int ioremap_p4d_range(pgd_t *pgd, unsigned long addr,
  116. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  117. {
  118. p4d_t *p4d;
  119. unsigned long next;
  120. phys_addr -= addr;
  121. p4d = p4d_alloc(&init_mm, pgd, addr);
  122. if (!p4d)
  123. return -ENOMEM;
  124. do {
  125. next = p4d_addr_end(addr, end);
  126. if (ioremap_p4d_enabled() &&
  127. ((next - addr) == P4D_SIZE) &&
  128. IS_ALIGNED(phys_addr + addr, P4D_SIZE)) {
  129. if (p4d_set_huge(p4d, phys_addr + addr, prot))
  130. continue;
  131. }
  132. if (ioremap_pud_range(p4d, addr, next, phys_addr + addr, prot))
  133. return -ENOMEM;
  134. } while (p4d++, addr = next, addr != end);
  135. return 0;
  136. }
  137. int ioremap_page_range(unsigned long addr,
  138. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  139. {
  140. pgd_t *pgd;
  141. unsigned long start;
  142. unsigned long next;
  143. int err;
  144. might_sleep();
  145. BUG_ON(addr >= end);
  146. start = addr;
  147. phys_addr -= addr;
  148. pgd = pgd_offset_k(addr);
  149. do {
  150. next = pgd_addr_end(addr, end);
  151. err = ioremap_p4d_range(pgd, addr, next, phys_addr+addr, prot);
  152. if (err)
  153. break;
  154. } while (pgd++, addr = next, addr != end);
  155. flush_cache_vmap(start, end);
  156. return err;
  157. }