ioremap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * arch/sh/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. * This is needed for high PCI addresses that aren't mapped in the
  6. * 640k-1MB IO memory area on PC's
  7. *
  8. * (C) Copyright 1995 1996 Linus Torvalds
  9. * (C) Copyright 2005, 2006 Paul Mundt
  10. *
  11. * This file is subject to the terms and conditions of the GNU General
  12. * Public License. See the file "COPYING" in the main directory of this
  13. * archive for more details.
  14. */
  15. #include <linux/vmalloc.h>
  16. #include <linux/module.h>
  17. #include <linux/mm.h>
  18. #include <asm/io.h>
  19. #include <asm/page.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/addrspace.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  25. unsigned long size, unsigned long phys_addr, unsigned long flags)
  26. {
  27. unsigned long end;
  28. unsigned long pfn;
  29. pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW |
  30. _PAGE_DIRTY | _PAGE_ACCESSED |
  31. _PAGE_HW_SHARED | _PAGE_FLAGS_HARD | flags);
  32. address &= ~PMD_MASK;
  33. end = address + size;
  34. if (end > PMD_SIZE)
  35. end = PMD_SIZE;
  36. if (address >= end)
  37. BUG();
  38. pfn = phys_addr >> PAGE_SHIFT;
  39. do {
  40. if (!pte_none(*pte)) {
  41. printk("remap_area_pte: page already exists\n");
  42. BUG();
  43. }
  44. set_pte(pte, pfn_pte(pfn, pgprot));
  45. address += PAGE_SIZE;
  46. pfn++;
  47. pte++;
  48. } while (address && (address < end));
  49. }
  50. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
  51. unsigned long size, unsigned long phys_addr, unsigned long flags)
  52. {
  53. unsigned long end;
  54. address &= ~PGDIR_MASK;
  55. end = address + size;
  56. if (end > PGDIR_SIZE)
  57. end = PGDIR_SIZE;
  58. phys_addr -= address;
  59. if (address >= end)
  60. BUG();
  61. do {
  62. pte_t * pte = pte_alloc_kernel(pmd, address);
  63. if (!pte)
  64. return -ENOMEM;
  65. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  66. address = (address + PMD_SIZE) & PMD_MASK;
  67. pmd++;
  68. } while (address && (address < end));
  69. return 0;
  70. }
  71. int remap_area_pages(unsigned long address, unsigned long phys_addr,
  72. unsigned long size, unsigned long flags)
  73. {
  74. int error;
  75. pgd_t * dir;
  76. unsigned long end = address + size;
  77. phys_addr -= address;
  78. dir = pgd_offset_k(address);
  79. flush_cache_all();
  80. if (address >= end)
  81. BUG();
  82. do {
  83. pud_t *pud;
  84. pmd_t *pmd;
  85. error = -ENOMEM;
  86. pud = pud_alloc(&init_mm, dir, address);
  87. if (!pud)
  88. break;
  89. pmd = pmd_alloc(&init_mm, pud, address);
  90. if (!pmd)
  91. break;
  92. if (remap_area_pmd(pmd, address, end - address,
  93. phys_addr + address, flags))
  94. break;
  95. error = 0;
  96. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  97. dir++;
  98. } while (address && (address < end));
  99. flush_tlb_all();
  100. return error;
  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. void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
  112. unsigned long flags)
  113. {
  114. struct vm_struct * area;
  115. unsigned long offset, last_addr, addr, orig_addr;
  116. /* Don't allow wraparound or zero size */
  117. last_addr = phys_addr + size - 1;
  118. if (!size || last_addr < phys_addr)
  119. return NULL;
  120. /*
  121. * Don't remap the low PCI/ISA area, it's always mapped..
  122. */
  123. if (phys_addr >= 0xA0000 && last_addr < 0x100000)
  124. return (void __iomem *)phys_to_virt(phys_addr);
  125. /*
  126. * Don't allow anybody to remap normal RAM that we're using..
  127. */
  128. if (phys_addr < virt_to_phys(high_memory))
  129. return NULL;
  130. /*
  131. * Mappings have to be page-aligned
  132. */
  133. offset = phys_addr & ~PAGE_MASK;
  134. phys_addr &= PAGE_MASK;
  135. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  136. /*
  137. * Ok, go for it..
  138. */
  139. area = get_vm_area(size, VM_IOREMAP);
  140. if (!area)
  141. return NULL;
  142. area->phys_addr = phys_addr;
  143. orig_addr = addr = (unsigned long)area->addr;
  144. #ifdef CONFIG_32BIT
  145. /*
  146. * First try to remap through the PMB once a valid VMA has been
  147. * established. Smaller allocations (or the rest of the size
  148. * remaining after a PMB mapping due to the size not being
  149. * perfectly aligned on a PMB size boundary) are then mapped
  150. * through the UTLB using conventional page tables.
  151. *
  152. * PMB entries are all pre-faulted.
  153. */
  154. if (unlikely(size >= 0x1000000)) {
  155. unsigned long mapped = pmb_remap(addr, phys_addr, size, flags);
  156. if (likely(mapped)) {
  157. addr += mapped;
  158. phys_addr += mapped;
  159. size -= mapped;
  160. }
  161. }
  162. #endif
  163. if (likely(size))
  164. if (remap_area_pages(addr, phys_addr, size, flags)) {
  165. vunmap((void *)orig_addr);
  166. return NULL;
  167. }
  168. return (void __iomem *)(offset + (char *)orig_addr);
  169. }
  170. EXPORT_SYMBOL(__ioremap);
  171. void __iounmap(void __iomem *addr)
  172. {
  173. unsigned long vaddr = (unsigned long __force)addr;
  174. struct vm_struct *p;
  175. if (PXSEG(vaddr) < P3SEG)
  176. return;
  177. #ifdef CONFIG_32BIT
  178. /*
  179. * Purge any PMB entries that may have been established for this
  180. * mapping, then proceed with conventional VMA teardown.
  181. *
  182. * XXX: Note that due to the way that remove_vm_area() does
  183. * matching of the resultant VMA, we aren't able to fast-forward
  184. * the address past the PMB space until the end of the VMA where
  185. * the page tables reside. As such, unmap_vm_area() will be
  186. * forced to linearly scan over the area until it finds the page
  187. * tables where PTEs that need to be unmapped actually reside,
  188. * which is far from optimal. Perhaps we need to use a separate
  189. * VMA for the PMB mappings?
  190. * -- PFM.
  191. */
  192. pmb_unmap(vaddr);
  193. #endif
  194. p = remove_vm_area((void *)(vaddr & PAGE_MASK));
  195. if (!p) {
  196. printk(KERN_ERR "%s: bad address %p\n", __FUNCTION__, addr);
  197. return;
  198. }
  199. kfree(p);
  200. }
  201. EXPORT_SYMBOL(__iounmap);