ioremap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Based on arch/arm/mm/ioremap.c
  3. *
  4. * (C) Copyright 1995 1996 Linus Torvalds
  5. * Hacked for ARM by Phil Blundell <philb@gnu.org>
  6. * Hacked to allow all architectures to build, and various cleanups
  7. * by Russell King
  8. * Copyright (C) 2012 ARM Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/export.h>
  23. #include <linux/mm.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/io.h>
  26. #include <asm/fixmap.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/pgalloc.h>
  29. static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
  30. pgprot_t prot, void *caller)
  31. {
  32. unsigned long last_addr;
  33. unsigned long offset = phys_addr & ~PAGE_MASK;
  34. int err;
  35. unsigned long addr;
  36. struct vm_struct *area;
  37. /*
  38. * Page align the mapping address and size, taking account of any
  39. * offset.
  40. */
  41. phys_addr &= PAGE_MASK;
  42. size = PAGE_ALIGN(size + offset);
  43. /*
  44. * Don't allow wraparound, zero size or outside PHYS_MASK.
  45. */
  46. last_addr = phys_addr + size - 1;
  47. if (!size || last_addr < phys_addr || (last_addr & ~PHYS_MASK))
  48. return NULL;
  49. /*
  50. * Don't allow RAM to be mapped.
  51. */
  52. if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr))))
  53. return NULL;
  54. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  55. if (!area)
  56. return NULL;
  57. addr = (unsigned long)area->addr;
  58. err = ioremap_page_range(addr, addr + size, phys_addr, prot);
  59. if (err) {
  60. vunmap((void *)addr);
  61. return NULL;
  62. }
  63. return (void __iomem *)(offset + addr);
  64. }
  65. void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot)
  66. {
  67. return __ioremap_caller(phys_addr, size, prot,
  68. __builtin_return_address(0));
  69. }
  70. EXPORT_SYMBOL(__ioremap);
  71. void __iounmap(volatile void __iomem *io_addr)
  72. {
  73. unsigned long addr = (unsigned long)io_addr & PAGE_MASK;
  74. /*
  75. * We could get an address outside vmalloc range in case
  76. * of ioremap_cache() reusing a RAM mapping.
  77. */
  78. if (VMALLOC_START <= addr && addr < VMALLOC_END)
  79. vunmap((void *)addr);
  80. }
  81. EXPORT_SYMBOL(__iounmap);
  82. void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
  83. {
  84. /* For normal memory we already have a cacheable mapping. */
  85. if (pfn_valid(__phys_to_pfn(phys_addr)))
  86. return (void __iomem *)__phys_to_virt(phys_addr);
  87. return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
  88. __builtin_return_address(0));
  89. }
  90. EXPORT_SYMBOL(ioremap_cache);
  91. static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
  92. #if CONFIG_ARM64_PGTABLE_LEVELS > 2
  93. static pte_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss;
  94. #endif
  95. #if CONFIG_ARM64_PGTABLE_LEVELS > 3
  96. static pte_t bm_pud[PTRS_PER_PUD] __page_aligned_bss;
  97. #endif
  98. static inline pud_t * __init early_ioremap_pud(unsigned long addr)
  99. {
  100. pgd_t *pgd;
  101. pgd = pgd_offset_k(addr);
  102. BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
  103. return pud_offset(pgd, addr);
  104. }
  105. static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
  106. {
  107. pud_t *pud = early_ioremap_pud(addr);
  108. BUG_ON(pud_none(*pud) || pud_bad(*pud));
  109. return pmd_offset(pud, addr);
  110. }
  111. static inline pte_t * __init early_ioremap_pte(unsigned long addr)
  112. {
  113. pmd_t *pmd = early_ioremap_pmd(addr);
  114. BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
  115. return pte_offset_kernel(pmd, addr);
  116. }
  117. void __init early_ioremap_init(void)
  118. {
  119. pgd_t *pgd;
  120. pud_t *pud;
  121. pmd_t *pmd;
  122. unsigned long addr = fix_to_virt(FIX_BTMAP_BEGIN);
  123. pgd = pgd_offset_k(addr);
  124. pgd_populate(&init_mm, pgd, bm_pud);
  125. pud = pud_offset(pgd, addr);
  126. pud_populate(&init_mm, pud, bm_pmd);
  127. pmd = pmd_offset(pud, addr);
  128. pmd_populate_kernel(&init_mm, pmd, bm_pte);
  129. /*
  130. * The boot-ioremap range spans multiple pmds, for which
  131. * we are not prepared:
  132. */
  133. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  134. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  135. if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
  136. WARN_ON(1);
  137. pr_warn("pmd %p != %p\n",
  138. pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
  139. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  140. fix_to_virt(FIX_BTMAP_BEGIN));
  141. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  142. fix_to_virt(FIX_BTMAP_END));
  143. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  144. pr_warn("FIX_BTMAP_BEGIN: %d\n",
  145. FIX_BTMAP_BEGIN);
  146. }
  147. early_ioremap_setup();
  148. }
  149. void __init __early_set_fixmap(enum fixed_addresses idx,
  150. phys_addr_t phys, pgprot_t flags)
  151. {
  152. unsigned long addr = __fix_to_virt(idx);
  153. pte_t *pte;
  154. if (idx >= __end_of_fixed_addresses) {
  155. BUG();
  156. return;
  157. }
  158. pte = early_ioremap_pte(addr);
  159. if (pgprot_val(flags))
  160. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
  161. else {
  162. pte_clear(&init_mm, addr, pte);
  163. flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
  164. }
  165. }