ioremap.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * linux/arch/arm/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. *
  8. * Hacked for ARM by Phil Blundell <philb@gnu.org>
  9. * Hacked to allow all architectures to build, and various cleanups
  10. * by Russell King
  11. *
  12. * This allows a driver to remap an arbitrary region of bus memory into
  13. * virtual space. One should *only* use readl, writel, memcpy_toio and
  14. * so on with such remapped areas.
  15. *
  16. * Because the ARM only has a 32-bit address space we can't address the
  17. * whole of the (physical) PCI space at once. PCI huge-mode addressing
  18. * allows us to circumvent this restriction by splitting PCI space into
  19. * two 2GB chunks and mapping only one at a time into processor memory.
  20. * We use MMU protection domains to trap any attempt to access the bank
  21. * that is not currently mapped. (This isn't fully implemented yet.)
  22. */
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/mm.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/io.h>
  28. #include <asm/cputype.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/sizes.h>
  34. #include <asm/mach/map.h>
  35. #include "mm.h"
  36. /*
  37. * Used by ioremap() and iounmap() code to mark (super)section-mapped
  38. * I/O regions in vm_struct->flags field.
  39. */
  40. #define VM_ARM_SECTION_MAPPING 0x80000000
  41. int ioremap_page(unsigned long virt, unsigned long phys,
  42. const struct mem_type *mtype)
  43. {
  44. return ioremap_page_range(virt, virt + PAGE_SIZE, phys,
  45. __pgprot(mtype->prot_pte));
  46. }
  47. EXPORT_SYMBOL(ioremap_page);
  48. void __check_kvm_seq(struct mm_struct *mm)
  49. {
  50. unsigned int seq;
  51. do {
  52. seq = init_mm.context.kvm_seq;
  53. memcpy(pgd_offset(mm, VMALLOC_START),
  54. pgd_offset_k(VMALLOC_START),
  55. sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
  56. pgd_index(VMALLOC_START)));
  57. mm->context.kvm_seq = seq;
  58. } while (seq != init_mm.context.kvm_seq);
  59. }
  60. #ifndef CONFIG_SMP
  61. /*
  62. * Section support is unsafe on SMP - If you iounmap and ioremap a region,
  63. * the other CPUs will not see this change until their next context switch.
  64. * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
  65. * which requires the new ioremap'd region to be referenced, the CPU will
  66. * reference the _old_ region.
  67. *
  68. * Note that get_vm_area_caller() allocates a guard 4K page, so we need to
  69. * mask the size back to 1MB aligned or we will overflow in the loop below.
  70. */
  71. static void unmap_area_sections(unsigned long virt, unsigned long size)
  72. {
  73. unsigned long addr = virt, end = virt + (size & ~(SZ_1M - 1));
  74. pgd_t *pgd;
  75. flush_cache_vunmap(addr, end);
  76. pgd = pgd_offset_k(addr);
  77. do {
  78. pmd_t pmd, *pmdp = pmd_offset(pgd, addr);
  79. pmd = *pmdp;
  80. if (!pmd_none(pmd)) {
  81. /*
  82. * Clear the PMD from the page table, and
  83. * increment the kvm sequence so others
  84. * notice this change.
  85. *
  86. * Note: this is still racy on SMP machines.
  87. */
  88. pmd_clear(pmdp);
  89. init_mm.context.kvm_seq++;
  90. /*
  91. * Free the page table, if there was one.
  92. */
  93. if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
  94. pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
  95. }
  96. addr += PGDIR_SIZE;
  97. pgd++;
  98. } while (addr < end);
  99. /*
  100. * Ensure that the active_mm is up to date - we want to
  101. * catch any use-after-iounmap cases.
  102. */
  103. if (current->active_mm->context.kvm_seq != init_mm.context.kvm_seq)
  104. __check_kvm_seq(current->active_mm);
  105. flush_tlb_kernel_range(virt, end);
  106. }
  107. static int
  108. remap_area_sections(unsigned long virt, unsigned long pfn,
  109. size_t size, const struct mem_type *type)
  110. {
  111. unsigned long addr = virt, end = virt + size;
  112. pgd_t *pgd;
  113. /*
  114. * Remove and free any PTE-based mapping, and
  115. * sync the current kernel mapping.
  116. */
  117. unmap_area_sections(virt, size);
  118. pgd = pgd_offset_k(addr);
  119. do {
  120. pmd_t *pmd = pmd_offset(pgd, addr);
  121. pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  122. pfn += SZ_1M >> PAGE_SHIFT;
  123. pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  124. pfn += SZ_1M >> PAGE_SHIFT;
  125. flush_pmd_entry(pmd);
  126. addr += PGDIR_SIZE;
  127. pgd++;
  128. } while (addr < end);
  129. return 0;
  130. }
  131. static int
  132. remap_area_supersections(unsigned long virt, unsigned long pfn,
  133. size_t size, const struct mem_type *type)
  134. {
  135. unsigned long addr = virt, end = virt + size;
  136. pgd_t *pgd;
  137. /*
  138. * Remove and free any PTE-based mapping, and
  139. * sync the current kernel mapping.
  140. */
  141. unmap_area_sections(virt, size);
  142. pgd = pgd_offset_k(virt);
  143. do {
  144. unsigned long super_pmd_val, i;
  145. super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
  146. PMD_SECT_SUPER;
  147. super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
  148. for (i = 0; i < 8; i++) {
  149. pmd_t *pmd = pmd_offset(pgd, addr);
  150. pmd[0] = __pmd(super_pmd_val);
  151. pmd[1] = __pmd(super_pmd_val);
  152. flush_pmd_entry(pmd);
  153. addr += PGDIR_SIZE;
  154. pgd++;
  155. }
  156. pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
  157. } while (addr < end);
  158. return 0;
  159. }
  160. #endif
  161. void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn,
  162. unsigned long offset, size_t size, unsigned int mtype, void *caller)
  163. {
  164. const struct mem_type *type;
  165. int err;
  166. unsigned long addr;
  167. struct vm_struct * area;
  168. /*
  169. * High mappings must be supersection aligned
  170. */
  171. if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
  172. return NULL;
  173. /*
  174. * Don't allow RAM to be mapped - this causes problems with ARMv6+
  175. */
  176. if (pfn_valid(pfn)) {
  177. printk(KERN_WARNING "BUG: Your driver calls ioremap() on system memory. This leads\n"
  178. "to architecturally unpredictable behaviour on ARMv6+, and ioremap()\n"
  179. "will fail in the next kernel release. Please fix your driver.\n");
  180. WARN_ON(1);
  181. }
  182. type = get_mem_type(mtype);
  183. if (!type)
  184. return NULL;
  185. /*
  186. * Page align the mapping size, taking account of any offset.
  187. */
  188. size = PAGE_ALIGN(offset + size);
  189. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  190. if (!area)
  191. return NULL;
  192. addr = (unsigned long)area->addr;
  193. #ifndef CONFIG_SMP
  194. if (DOMAIN_IO == 0 &&
  195. (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
  196. cpu_is_xsc3()) && pfn >= 0x100000 &&
  197. !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
  198. area->flags |= VM_ARM_SECTION_MAPPING;
  199. err = remap_area_supersections(addr, pfn, size, type);
  200. } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
  201. area->flags |= VM_ARM_SECTION_MAPPING;
  202. err = remap_area_sections(addr, pfn, size, type);
  203. } else
  204. #endif
  205. err = ioremap_page_range(addr, addr + size, __pfn_to_phys(pfn),
  206. __pgprot(type->prot_pte));
  207. if (err) {
  208. vunmap((void *)addr);
  209. return NULL;
  210. }
  211. flush_cache_vmap(addr, addr + size);
  212. return (void __iomem *) (offset + addr);
  213. }
  214. void __iomem *__arm_ioremap_caller(unsigned long phys_addr, size_t size,
  215. unsigned int mtype, void *caller)
  216. {
  217. unsigned long last_addr;
  218. unsigned long offset = phys_addr & ~PAGE_MASK;
  219. unsigned long pfn = __phys_to_pfn(phys_addr);
  220. /*
  221. * Don't allow wraparound or zero size
  222. */
  223. last_addr = phys_addr + size - 1;
  224. if (!size || last_addr < phys_addr)
  225. return NULL;
  226. return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
  227. caller);
  228. }
  229. /*
  230. * Remap an arbitrary physical address space into the kernel virtual
  231. * address space. Needed when the kernel wants to access high addresses
  232. * directly.
  233. *
  234. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  235. * have to convert them into an offset in a page-aligned mapping, but the
  236. * caller shouldn't need to know that small detail.
  237. */
  238. void __iomem *
  239. __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  240. unsigned int mtype)
  241. {
  242. return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
  243. __builtin_return_address(0));
  244. }
  245. EXPORT_SYMBOL(__arm_ioremap_pfn);
  246. void __iomem *
  247. __arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
  248. {
  249. return __arm_ioremap_caller(phys_addr, size, mtype,
  250. __builtin_return_address(0));
  251. }
  252. EXPORT_SYMBOL(__arm_ioremap);
  253. void __iounmap(volatile void __iomem *io_addr)
  254. {
  255. void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
  256. #ifndef CONFIG_SMP
  257. struct vm_struct **p, *tmp;
  258. /*
  259. * If this is a section based mapping we need to handle it
  260. * specially as the VM subsystem does not know how to handle
  261. * such a beast. We need the lock here b/c we need to clear
  262. * all the mappings before the area can be reclaimed
  263. * by someone else.
  264. */
  265. write_lock(&vmlist_lock);
  266. for (p = &vmlist ; (tmp = *p) ; p = &tmp->next) {
  267. if ((tmp->flags & VM_IOREMAP) && (tmp->addr == addr)) {
  268. if (tmp->flags & VM_ARM_SECTION_MAPPING) {
  269. unmap_area_sections((unsigned long)tmp->addr,
  270. tmp->size);
  271. }
  272. break;
  273. }
  274. }
  275. write_unlock(&vmlist_lock);
  276. #endif
  277. vunmap(addr);
  278. }
  279. EXPORT_SYMBOL(__iounmap);