ioremap.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. * (C) Copyright 2001, 2002 Ralf Baechle
  8. */
  9. #include <linux/export.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/ioport.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/mm_types.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/io.h>
  19. #include <asm/tlbflush.h>
  20. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  21. phys_addr_t size, phys_addr_t phys_addr, unsigned long flags)
  22. {
  23. phys_addr_t end;
  24. unsigned long pfn;
  25. pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
  26. | __WRITEABLE | flags);
  27. address &= ~PMD_MASK;
  28. end = address + size;
  29. if (end > PMD_SIZE)
  30. end = PMD_SIZE;
  31. BUG_ON(address >= end);
  32. pfn = phys_addr >> PAGE_SHIFT;
  33. do {
  34. if (!pte_none(*pte)) {
  35. printk("remap_area_pte: page already exists\n");
  36. BUG();
  37. }
  38. set_pte(pte, pfn_pte(pfn, pgprot));
  39. address += PAGE_SIZE;
  40. pfn++;
  41. pte++;
  42. } while (address && (address < end));
  43. }
  44. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
  45. phys_addr_t size, phys_addr_t phys_addr, unsigned long flags)
  46. {
  47. phys_addr_t end;
  48. address &= ~PGDIR_MASK;
  49. end = address + size;
  50. if (end > PGDIR_SIZE)
  51. end = PGDIR_SIZE;
  52. phys_addr -= address;
  53. BUG_ON(address >= end);
  54. do {
  55. pte_t * pte = pte_alloc_kernel(pmd, address);
  56. if (!pte)
  57. return -ENOMEM;
  58. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  59. address = (address + PMD_SIZE) & PMD_MASK;
  60. pmd++;
  61. } while (address && (address < end));
  62. return 0;
  63. }
  64. static int remap_area_pages(unsigned long address, phys_addr_t phys_addr,
  65. phys_addr_t size, unsigned long flags)
  66. {
  67. int error;
  68. pgd_t * dir;
  69. unsigned long end = address + size;
  70. phys_addr -= address;
  71. dir = pgd_offset(&init_mm, address);
  72. flush_cache_all();
  73. BUG_ON(address >= end);
  74. do {
  75. pud_t *pud;
  76. pmd_t *pmd;
  77. error = -ENOMEM;
  78. pud = pud_alloc(&init_mm, dir, address);
  79. if (!pud)
  80. break;
  81. pmd = pmd_alloc(&init_mm, pud, address);
  82. if (!pmd)
  83. break;
  84. if (remap_area_pmd(pmd, address, end - address,
  85. phys_addr + address, flags))
  86. break;
  87. error = 0;
  88. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  89. dir++;
  90. } while (address && (address < end));
  91. flush_tlb_all();
  92. return error;
  93. }
  94. static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages,
  95. void *arg)
  96. {
  97. unsigned long i;
  98. for (i = 0; i < nr_pages; i++) {
  99. if (pfn_valid(start_pfn + i) &&
  100. !PageReserved(pfn_to_page(start_pfn + i)))
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. /*
  106. * Generic mapping function (not visible outside):
  107. */
  108. /*
  109. * Remap an arbitrary physical address space into the kernel virtual
  110. * address space. Needed when the kernel wants to access high addresses
  111. * directly.
  112. *
  113. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  114. * have to convert them into an offset in a page-aligned mapping, but the
  115. * caller shouldn't need to know that small detail.
  116. */
  117. #define IS_LOW512(addr) (!((phys_addr_t)(addr) & (phys_addr_t) ~0x1fffffffULL))
  118. void __iomem * __ioremap(phys_addr_t phys_addr, phys_addr_t size, unsigned long flags)
  119. {
  120. unsigned long offset, pfn, last_pfn;
  121. struct vm_struct * area;
  122. phys_addr_t last_addr;
  123. void * addr;
  124. phys_addr = fixup_bigphys_addr(phys_addr, size);
  125. /* Don't allow wraparound or zero size */
  126. last_addr = phys_addr + size - 1;
  127. if (!size || last_addr < phys_addr)
  128. return NULL;
  129. /*
  130. * Map uncached objects in the low 512mb of address space using KSEG1,
  131. * otherwise map using page tables.
  132. */
  133. if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
  134. flags == _CACHE_UNCACHED)
  135. return (void __iomem *) CKSEG1ADDR(phys_addr);
  136. /*
  137. * Don't allow anybody to remap RAM that may be allocated by the page
  138. * allocator, since that could lead to races & data clobbering.
  139. */
  140. pfn = PFN_DOWN(phys_addr);
  141. last_pfn = PFN_DOWN(last_addr);
  142. if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL,
  143. __ioremap_check_ram) == 1) {
  144. WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
  145. &phys_addr, &last_addr);
  146. return NULL;
  147. }
  148. /*
  149. * Mappings have to be page-aligned
  150. */
  151. offset = phys_addr & ~PAGE_MASK;
  152. phys_addr &= PAGE_MASK;
  153. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  154. /*
  155. * Ok, go for it..
  156. */
  157. area = get_vm_area(size, VM_IOREMAP);
  158. if (!area)
  159. return NULL;
  160. addr = area->addr;
  161. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  162. vunmap(addr);
  163. return NULL;
  164. }
  165. return (void __iomem *) (offset + (char *)addr);
  166. }
  167. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
  168. void __iounmap(const volatile void __iomem *addr)
  169. {
  170. struct vm_struct *p;
  171. if (IS_KSEG1(addr))
  172. return;
  173. p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
  174. if (!p)
  175. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  176. kfree(p);
  177. }
  178. EXPORT_SYMBOL(__ioremap);
  179. EXPORT_SYMBOL(__iounmap);