ioremap.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 (WARN_ON(pfn_valid(pfn)))
  177. return NULL;
  178. type = get_mem_type(mtype);
  179. if (!type)
  180. return NULL;
  181. /*
  182. * Page align the mapping size, taking account of any offset.
  183. */
  184. size = PAGE_ALIGN(offset + size);
  185. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  186. if (!area)
  187. return NULL;
  188. addr = (unsigned long)area->addr;
  189. #ifndef CONFIG_SMP
  190. if (DOMAIN_IO == 0 &&
  191. (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
  192. cpu_is_xsc3()) && pfn >= 0x100000 &&
  193. !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
  194. area->flags |= VM_ARM_SECTION_MAPPING;
  195. err = remap_area_supersections(addr, pfn, size, type);
  196. } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
  197. area->flags |= VM_ARM_SECTION_MAPPING;
  198. err = remap_area_sections(addr, pfn, size, type);
  199. } else
  200. #endif
  201. err = ioremap_page_range(addr, addr + size, __pfn_to_phys(pfn),
  202. __pgprot(type->prot_pte));
  203. if (err) {
  204. vunmap((void *)addr);
  205. return NULL;
  206. }
  207. flush_cache_vmap(addr, addr + size);
  208. return (void __iomem *) (offset + addr);
  209. }
  210. void __iomem *__arm_ioremap_caller(unsigned long phys_addr, size_t size,
  211. unsigned int mtype, void *caller)
  212. {
  213. unsigned long last_addr;
  214. unsigned long offset = phys_addr & ~PAGE_MASK;
  215. unsigned long pfn = __phys_to_pfn(phys_addr);
  216. /*
  217. * Don't allow wraparound or zero size
  218. */
  219. last_addr = phys_addr + size - 1;
  220. if (!size || last_addr < phys_addr)
  221. return NULL;
  222. return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
  223. caller);
  224. }
  225. /*
  226. * Remap an arbitrary physical address space into the kernel virtual
  227. * address space. Needed when the kernel wants to access high addresses
  228. * directly.
  229. *
  230. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  231. * have to convert them into an offset in a page-aligned mapping, but the
  232. * caller shouldn't need to know that small detail.
  233. */
  234. void __iomem *
  235. __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  236. unsigned int mtype)
  237. {
  238. return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
  239. __builtin_return_address(0));
  240. }
  241. EXPORT_SYMBOL(__arm_ioremap_pfn);
  242. void __iomem *
  243. __arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
  244. {
  245. return __arm_ioremap_caller(phys_addr, size, mtype,
  246. __builtin_return_address(0));
  247. }
  248. EXPORT_SYMBOL(__arm_ioremap);
  249. void __iounmap(volatile void __iomem *io_addr)
  250. {
  251. void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
  252. #ifndef CONFIG_SMP
  253. struct vm_struct **p, *tmp;
  254. /*
  255. * If this is a section based mapping we need to handle it
  256. * specially as the VM subsystem does not know how to handle
  257. * such a beast. We need the lock here b/c we need to clear
  258. * all the mappings before the area can be reclaimed
  259. * by someone else.
  260. */
  261. write_lock(&vmlist_lock);
  262. for (p = &vmlist ; (tmp = *p) ; p = &tmp->next) {
  263. if ((tmp->flags & VM_IOREMAP) && (tmp->addr == addr)) {
  264. if (tmp->flags & VM_ARM_SECTION_MAPPING) {
  265. unmap_area_sections((unsigned long)tmp->addr,
  266. tmp->size);
  267. }
  268. break;
  269. }
  270. }
  271. write_unlock(&vmlist_lock);
  272. #endif
  273. vunmap(addr);
  274. }
  275. EXPORT_SYMBOL(__iounmap);