ioremap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. if (pmd_set_huge(pmd, phys_addr + addr, prot))
  77. continue;
  78. }
  79. if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, prot))
  80. return -ENOMEM;
  81. } while (pmd++, addr = next, addr != end);
  82. return 0;
  83. }
  84. static inline int ioremap_pud_range(pgd_t *pgd, unsigned long addr,
  85. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  86. {
  87. pud_t *pud;
  88. unsigned long next;
  89. phys_addr -= addr;
  90. pud = pud_alloc(&init_mm, pgd, addr);
  91. if (!pud)
  92. return -ENOMEM;
  93. do {
  94. next = pud_addr_end(addr, end);
  95. if (ioremap_pud_enabled() &&
  96. ((next - addr) == PUD_SIZE) &&
  97. IS_ALIGNED(phys_addr + addr, PUD_SIZE)) {
  98. if (pud_set_huge(pud, phys_addr + addr, prot))
  99. continue;
  100. }
  101. if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, prot))
  102. return -ENOMEM;
  103. } while (pud++, addr = next, addr != end);
  104. return 0;
  105. }
  106. int ioremap_page_range(unsigned long addr,
  107. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  108. {
  109. pgd_t *pgd;
  110. unsigned long start;
  111. unsigned long next;
  112. int err;
  113. BUG_ON(addr >= end);
  114. start = addr;
  115. phys_addr -= addr;
  116. pgd = pgd_offset_k(addr);
  117. do {
  118. next = pgd_addr_end(addr, end);
  119. err = ioremap_pud_range(pgd, addr, next, phys_addr+addr, prot);
  120. if (err)
  121. break;
  122. } while (pgd++, addr = next, addr != end);
  123. flush_cache_vmap(start, end);
  124. return err;
  125. }
  126. EXPORT_SYMBOL_GPL(ioremap_page_range);