ioremap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <linux/pci.h>
  19. #include <asm/io.h>
  20. #include <asm/page.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/addrspace.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlbflush.h>
  25. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  26. unsigned long size, unsigned long phys_addr, unsigned long flags)
  27. {
  28. unsigned long end;
  29. unsigned long pfn;
  30. pgprot_t pgprot = __pgprot(pgprot_val(PAGE_KERNEL_NOCACHE) | flags);
  31. address &= ~PMD_MASK;
  32. end = address + size;
  33. if (end > PMD_SIZE)
  34. end = PMD_SIZE;
  35. if (address >= end)
  36. BUG();
  37. pfn = phys_addr >> PAGE_SHIFT;
  38. do {
  39. if (!pte_none(*pte)) {
  40. printk("remap_area_pte: page already exists\n");
  41. BUG();
  42. }
  43. set_pte(pte, pfn_pte(pfn, pgprot));
  44. address += PAGE_SIZE;
  45. pfn++;
  46. pte++;
  47. } while (address && (address < end));
  48. }
  49. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
  50. unsigned long size, unsigned long phys_addr, unsigned long flags)
  51. {
  52. unsigned long end;
  53. address &= ~PGDIR_MASK;
  54. end = address + size;
  55. if (end > PGDIR_SIZE)
  56. end = PGDIR_SIZE;
  57. phys_addr -= address;
  58. if (address >= end)
  59. BUG();
  60. do {
  61. pte_t * pte = pte_alloc_kernel(pmd, address);
  62. if (!pte)
  63. return -ENOMEM;
  64. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  65. address = (address + PMD_SIZE) & PMD_MASK;
  66. pmd++;
  67. } while (address && (address < end));
  68. return 0;
  69. }
  70. int remap_area_pages(unsigned long address, unsigned long phys_addr,
  71. unsigned long size, unsigned long flags)
  72. {
  73. int error;
  74. pgd_t * dir;
  75. unsigned long end = address + size;
  76. phys_addr -= address;
  77. dir = pgd_offset_k(address);
  78. flush_cache_all();
  79. if (address >= end)
  80. BUG();
  81. do {
  82. pud_t *pud;
  83. pmd_t *pmd;
  84. error = -ENOMEM;
  85. pud = pud_alloc(&init_mm, dir, address);
  86. if (!pud)
  87. break;
  88. pmd = pmd_alloc(&init_mm, pud, address);
  89. if (!pmd)
  90. break;
  91. if (remap_area_pmd(pmd, address, end - address,
  92. phys_addr + address, flags))
  93. break;
  94. error = 0;
  95. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  96. dir++;
  97. } while (address && (address < end));
  98. flush_tlb_all();
  99. return error;
  100. }
  101. /*
  102. * Remap an arbitrary physical address space into the kernel virtual
  103. * address space. Needed when the kernel wants to access high addresses
  104. * directly.
  105. *
  106. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  107. * have to convert them into an offset in a page-aligned mapping, but the
  108. * caller shouldn't need to know that small detail.
  109. */
  110. void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
  111. unsigned long flags)
  112. {
  113. struct vm_struct * area;
  114. unsigned long offset, last_addr, addr, orig_addr;
  115. /* Don't allow wraparound or zero size */
  116. last_addr = phys_addr + size - 1;
  117. if (!size || last_addr < phys_addr)
  118. return NULL;
  119. /*
  120. * Don't remap the low PCI/ISA area, it's always mapped..
  121. */
  122. if (phys_addr >= 0xA0000 && last_addr < 0x100000)
  123. return (void __iomem *)phys_to_virt(phys_addr);
  124. /*
  125. * If we're on an SH7751 or SH7780 PCI controller, PCI memory is
  126. * mapped at the end of the address space (typically 0xfd000000)
  127. * in a non-translatable area, so mapping through page tables for
  128. * this area is not only pointless, but also fundamentally
  129. * broken. Just return the physical address instead.
  130. *
  131. * For boards that map a small PCI memory aperture somewhere in
  132. * P1/P2 space, ioremap() will already do the right thing,
  133. * and we'll never get this far.
  134. */
  135. if (is_pci_memaddr(phys_addr) && is_pci_memaddr(last_addr))
  136. return (void __iomem *)phys_addr;
  137. /*
  138. * Don't allow anybody to remap normal RAM that we're using..
  139. */
  140. if (phys_addr < virt_to_phys(high_memory))
  141. return NULL;
  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. area->phys_addr = phys_addr;
  155. orig_addr = addr = (unsigned long)area->addr;
  156. #ifdef CONFIG_32BIT
  157. /*
  158. * First try to remap through the PMB once a valid VMA has been
  159. * established. Smaller allocations (or the rest of the size
  160. * remaining after a PMB mapping due to the size not being
  161. * perfectly aligned on a PMB size boundary) are then mapped
  162. * through the UTLB using conventional page tables.
  163. *
  164. * PMB entries are all pre-faulted.
  165. */
  166. if (unlikely(size >= 0x1000000)) {
  167. unsigned long mapped = pmb_remap(addr, phys_addr, size, flags);
  168. if (likely(mapped)) {
  169. addr += mapped;
  170. phys_addr += mapped;
  171. size -= mapped;
  172. }
  173. }
  174. #endif
  175. if (likely(size))
  176. if (remap_area_pages(addr, phys_addr, size, flags)) {
  177. vunmap((void *)orig_addr);
  178. return NULL;
  179. }
  180. return (void __iomem *)(offset + (char *)orig_addr);
  181. }
  182. EXPORT_SYMBOL(__ioremap);
  183. void __iounmap(void __iomem *addr)
  184. {
  185. unsigned long vaddr = (unsigned long __force)addr;
  186. struct vm_struct *p;
  187. if (PXSEG(vaddr) < P3SEG || is_pci_memaddr(vaddr))
  188. return;
  189. #ifdef CONFIG_32BIT
  190. /*
  191. * Purge any PMB entries that may have been established for this
  192. * mapping, then proceed with conventional VMA teardown.
  193. *
  194. * XXX: Note that due to the way that remove_vm_area() does
  195. * matching of the resultant VMA, we aren't able to fast-forward
  196. * the address past the PMB space until the end of the VMA where
  197. * the page tables reside. As such, unmap_vm_area() will be
  198. * forced to linearly scan over the area until it finds the page
  199. * tables where PTEs that need to be unmapped actually reside,
  200. * which is far from optimal. Perhaps we need to use a separate
  201. * VMA for the PMB mappings?
  202. * -- PFM.
  203. */
  204. pmb_unmap(vaddr);
  205. #endif
  206. p = remove_vm_area((void *)(vaddr & PAGE_MASK));
  207. if (!p) {
  208. printk(KERN_ERR "%s: bad address %p\n", __FUNCTION__, addr);
  209. return;
  210. }
  211. kfree(p);
  212. }
  213. EXPORT_SYMBOL(__iounmap);