ioremap.c 3.4 KB

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