ioremap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/module.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/vmalloc.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/io.h>
  15. #include <asm/tlbflush.h>
  16. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  17. phys_t size, phys_t phys_addr, unsigned long flags)
  18. {
  19. phys_t end;
  20. unsigned long pfn;
  21. pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
  22. | __WRITEABLE | flags);
  23. address &= ~PMD_MASK;
  24. end = address + size;
  25. if (end > PMD_SIZE)
  26. end = PMD_SIZE;
  27. if (address >= end)
  28. BUG();
  29. pfn = phys_addr >> PAGE_SHIFT;
  30. do {
  31. if (!pte_none(*pte)) {
  32. printk("remap_area_pte: page already exists\n");
  33. BUG();
  34. }
  35. set_pte(pte, pfn_pte(pfn, pgprot));
  36. address += PAGE_SIZE;
  37. pfn++;
  38. pte++;
  39. } while (address && (address < end));
  40. }
  41. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
  42. phys_t size, phys_t phys_addr, unsigned long flags)
  43. {
  44. phys_t end;
  45. address &= ~PGDIR_MASK;
  46. end = address + size;
  47. if (end > PGDIR_SIZE)
  48. end = PGDIR_SIZE;
  49. phys_addr -= address;
  50. if (address >= end)
  51. BUG();
  52. do {
  53. pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
  54. if (!pte)
  55. return -ENOMEM;
  56. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  57. address = (address + PMD_SIZE) & PMD_MASK;
  58. pmd++;
  59. } while (address && (address < end));
  60. return 0;
  61. }
  62. static int remap_area_pages(unsigned long address, phys_t phys_addr,
  63. phys_t size, unsigned long flags)
  64. {
  65. int error;
  66. pgd_t * dir;
  67. unsigned long end = address + size;
  68. phys_addr -= address;
  69. dir = pgd_offset(&init_mm, address);
  70. flush_cache_all();
  71. if (address >= end)
  72. BUG();
  73. spin_lock(&init_mm.page_table_lock);
  74. do {
  75. pmd_t *pmd;
  76. pmd = pmd_alloc(&init_mm, dir, address);
  77. error = -ENOMEM;
  78. if (!pmd)
  79. break;
  80. if (remap_area_pmd(pmd, address, end - address,
  81. phys_addr + address, flags))
  82. break;
  83. error = 0;
  84. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  85. dir++;
  86. } while (address && (address < end));
  87. spin_unlock(&init_mm.page_table_lock);
  88. flush_tlb_all();
  89. return error;
  90. }
  91. /*
  92. * Allow physical addresses to be fixed up to help 36 bit peripherals.
  93. */
  94. phys_t __attribute__ ((weak))
  95. fixup_bigphys_addr(phys_t phys_addr, phys_t size)
  96. {
  97. return phys_addr;
  98. }
  99. /*
  100. * Generic mapping function (not visible outside):
  101. */
  102. /*
  103. * Remap an arbitrary physical address space into the kernel virtual
  104. * address space. Needed when the kernel wants to access high addresses
  105. * directly.
  106. *
  107. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  108. * have to convert them into an offset in a page-aligned mapping, but the
  109. * caller shouldn't need to know that small detail.
  110. */
  111. #define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
  112. void * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
  113. {
  114. struct vm_struct * area;
  115. unsigned long offset;
  116. phys_t last_addr;
  117. void * addr;
  118. phys_addr = fixup_bigphys_addr(phys_addr, size);
  119. /* Don't allow wraparound or zero size */
  120. last_addr = phys_addr + size - 1;
  121. if (!size || last_addr < phys_addr)
  122. return NULL;
  123. /*
  124. * Map uncached objects in the low 512mb of address space using KSEG1,
  125. * otherwise map using page tables.
  126. */
  127. if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
  128. flags == _CACHE_UNCACHED)
  129. return (void *) KSEG1ADDR(phys_addr);
  130. /*
  131. * Don't allow anybody to remap normal RAM that we're using..
  132. */
  133. if (phys_addr < virt_to_phys(high_memory)) {
  134. char *t_addr, *t_end;
  135. struct page *page;
  136. t_addr = __va(phys_addr);
  137. t_end = t_addr + (size - 1);
  138. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  139. if(!PageReserved(page))
  140. return NULL;
  141. }
  142. /*
  143. * Mappings have to be page-aligned
  144. */
  145. offset = phys_addr & ~PAGE_MASK;
  146. phys_addr &= PAGE_MASK;
  147. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  148. /*
  149. * Ok, go for it..
  150. */
  151. area = get_vm_area(size, VM_IOREMAP);
  152. if (!area)
  153. return NULL;
  154. addr = area->addr;
  155. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  156. vunmap(addr);
  157. return NULL;
  158. }
  159. return (void *) (offset + (char *)addr);
  160. }
  161. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
  162. void __iounmap(volatile void __iomem *addr)
  163. {
  164. struct vm_struct *p;
  165. if (IS_KSEG1(addr))
  166. return;
  167. p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
  168. if (!p) {
  169. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  170. return;
  171. }
  172. kfree(p);
  173. }
  174. EXPORT_SYMBOL(__ioremap);
  175. EXPORT_SYMBOL(__iounmap);