ioremap.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/cp15.h>
  29. #include <asm/cputype.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/pgalloc.h>
  33. #include <asm/tlbflush.h>
  34. #include <asm/sizes.h>
  35. #include <asm/mach/map.h>
  36. #include "mm.h"
  37. int ioremap_page(unsigned long virt, unsigned long phys,
  38. const struct mem_type *mtype)
  39. {
  40. return ioremap_page_range(virt, virt + PAGE_SIZE, phys,
  41. __pgprot(mtype->prot_pte));
  42. }
  43. EXPORT_SYMBOL(ioremap_page);
  44. void __check_kvm_seq(struct mm_struct *mm)
  45. {
  46. unsigned int seq;
  47. do {
  48. seq = init_mm.context.kvm_seq;
  49. memcpy(pgd_offset(mm, VMALLOC_START),
  50. pgd_offset_k(VMALLOC_START),
  51. sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
  52. pgd_index(VMALLOC_START)));
  53. mm->context.kvm_seq = seq;
  54. } while (seq != init_mm.context.kvm_seq);
  55. }
  56. #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
  57. /*
  58. * Section support is unsafe on SMP - If you iounmap and ioremap a region,
  59. * the other CPUs will not see this change until their next context switch.
  60. * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
  61. * which requires the new ioremap'd region to be referenced, the CPU will
  62. * reference the _old_ region.
  63. *
  64. * Note that get_vm_area_caller() allocates a guard 4K page, so we need to
  65. * mask the size back to 1MB aligned or we will overflow in the loop below.
  66. */
  67. static void unmap_area_sections(unsigned long virt, unsigned long size)
  68. {
  69. unsigned long addr = virt, end = virt + (size & ~(SZ_1M - 1));
  70. pgd_t *pgd;
  71. pud_t *pud;
  72. pmd_t *pmdp;
  73. flush_cache_vunmap(addr, end);
  74. pgd = pgd_offset_k(addr);
  75. pud = pud_offset(pgd, addr);
  76. pmdp = pmd_offset(pud, addr);
  77. do {
  78. pmd_t pmd = *pmdp;
  79. if (!pmd_none(pmd)) {
  80. /*
  81. * Clear the PMD from the page table, and
  82. * increment the kvm sequence so others
  83. * notice this change.
  84. *
  85. * Note: this is still racy on SMP machines.
  86. */
  87. pmd_clear(pmdp);
  88. init_mm.context.kvm_seq++;
  89. /*
  90. * Free the page table, if there was one.
  91. */
  92. if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
  93. pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
  94. }
  95. addr += PMD_SIZE;
  96. pmdp += 2;
  97. } while (addr < end);
  98. /*
  99. * Ensure that the active_mm is up to date - we want to
  100. * catch any use-after-iounmap cases.
  101. */
  102. if (current->active_mm->context.kvm_seq != init_mm.context.kvm_seq)
  103. __check_kvm_seq(current->active_mm);
  104. flush_tlb_kernel_range(virt, end);
  105. }
  106. static int
  107. remap_area_sections(unsigned long virt, unsigned long pfn,
  108. size_t size, const struct mem_type *type)
  109. {
  110. unsigned long addr = virt, end = virt + size;
  111. pgd_t *pgd;
  112. pud_t *pud;
  113. pmd_t *pmd;
  114. /*
  115. * Remove and free any PTE-based mapping, and
  116. * sync the current kernel mapping.
  117. */
  118. unmap_area_sections(virt, size);
  119. pgd = pgd_offset_k(addr);
  120. pud = pud_offset(pgd, addr);
  121. pmd = pmd_offset(pud, addr);
  122. do {
  123. pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  124. pfn += SZ_1M >> PAGE_SHIFT;
  125. pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  126. pfn += SZ_1M >> PAGE_SHIFT;
  127. flush_pmd_entry(pmd);
  128. addr += PMD_SIZE;
  129. pmd += 2;
  130. } while (addr < end);
  131. return 0;
  132. }
  133. static int
  134. remap_area_supersections(unsigned long virt, unsigned long pfn,
  135. size_t size, const struct mem_type *type)
  136. {
  137. unsigned long addr = virt, end = virt + size;
  138. pgd_t *pgd;
  139. pud_t *pud;
  140. pmd_t *pmd;
  141. /*
  142. * Remove and free any PTE-based mapping, and
  143. * sync the current kernel mapping.
  144. */
  145. unmap_area_sections(virt, size);
  146. pgd = pgd_offset_k(virt);
  147. pud = pud_offset(pgd, addr);
  148. pmd = pmd_offset(pud, addr);
  149. do {
  150. unsigned long super_pmd_val, i;
  151. super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
  152. PMD_SECT_SUPER;
  153. super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
  154. for (i = 0; i < 8; i++) {
  155. pmd[0] = __pmd(super_pmd_val);
  156. pmd[1] = __pmd(super_pmd_val);
  157. flush_pmd_entry(pmd);
  158. addr += PMD_SIZE;
  159. pmd += 2;
  160. }
  161. pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
  162. } while (addr < end);
  163. return 0;
  164. }
  165. #endif
  166. void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn,
  167. unsigned long offset, size_t size, unsigned int mtype, void *caller)
  168. {
  169. const struct mem_type *type;
  170. int err;
  171. unsigned long addr;
  172. struct vm_struct * area;
  173. #ifndef CONFIG_ARM_LPAE
  174. /*
  175. * High mappings must be supersection aligned
  176. */
  177. if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
  178. return NULL;
  179. #endif
  180. type = get_mem_type(mtype);
  181. if (!type)
  182. return NULL;
  183. /*
  184. * Page align the mapping size, taking account of any offset.
  185. */
  186. size = PAGE_ALIGN(offset + size);
  187. /*
  188. * Try to reuse one of the static mapping whenever possible.
  189. */
  190. read_lock(&vmlist_lock);
  191. for (area = vmlist; area; area = area->next) {
  192. if (!size || (sizeof(phys_addr_t) == 4 && pfn >= 0x100000))
  193. break;
  194. if (!(area->flags & VM_ARM_STATIC_MAPPING))
  195. continue;
  196. if ((area->flags & VM_ARM_MTYPE_MASK) != VM_ARM_MTYPE(mtype))
  197. continue;
  198. if (__phys_to_pfn(area->phys_addr) > pfn ||
  199. __pfn_to_phys(pfn) + size-1 > area->phys_addr + area->size-1)
  200. continue;
  201. /* we can drop the lock here as we know *area is static */
  202. read_unlock(&vmlist_lock);
  203. addr = (unsigned long)area->addr;
  204. addr += __pfn_to_phys(pfn) - area->phys_addr;
  205. return (void __iomem *) (offset + addr);
  206. }
  207. read_unlock(&vmlist_lock);
  208. /*
  209. * Don't allow RAM to be mapped - this causes problems with ARMv6+
  210. */
  211. if (WARN_ON(pfn_valid(pfn)))
  212. return NULL;
  213. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  214. if (!area)
  215. return NULL;
  216. addr = (unsigned long)area->addr;
  217. #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
  218. if (DOMAIN_IO == 0 &&
  219. (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
  220. cpu_is_xsc3()) && pfn >= 0x100000 &&
  221. !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
  222. area->flags |= VM_ARM_SECTION_MAPPING;
  223. err = remap_area_supersections(addr, pfn, size, type);
  224. } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
  225. area->flags |= VM_ARM_SECTION_MAPPING;
  226. err = remap_area_sections(addr, pfn, size, type);
  227. } else
  228. #endif
  229. err = ioremap_page_range(addr, addr + size, __pfn_to_phys(pfn),
  230. __pgprot(type->prot_pte));
  231. if (err) {
  232. vunmap((void *)addr);
  233. return NULL;
  234. }
  235. flush_cache_vmap(addr, addr + size);
  236. return (void __iomem *) (offset + addr);
  237. }
  238. void __iomem *__arm_ioremap_caller(unsigned long phys_addr, size_t size,
  239. unsigned int mtype, void *caller)
  240. {
  241. unsigned long last_addr;
  242. unsigned long offset = phys_addr & ~PAGE_MASK;
  243. unsigned long pfn = __phys_to_pfn(phys_addr);
  244. /*
  245. * Don't allow wraparound or zero size
  246. */
  247. last_addr = phys_addr + size - 1;
  248. if (!size || last_addr < phys_addr)
  249. return NULL;
  250. return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
  251. caller);
  252. }
  253. /*
  254. * Remap an arbitrary physical address space into the kernel virtual
  255. * address space. Needed when the kernel wants to access high addresses
  256. * directly.
  257. *
  258. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  259. * have to convert them into an offset in a page-aligned mapping, but the
  260. * caller shouldn't need to know that small detail.
  261. */
  262. void __iomem *
  263. __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  264. unsigned int mtype)
  265. {
  266. return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
  267. __builtin_return_address(0));
  268. }
  269. EXPORT_SYMBOL(__arm_ioremap_pfn);
  270. void __iomem *
  271. __arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
  272. {
  273. return __arm_ioremap_caller(phys_addr, size, mtype,
  274. __builtin_return_address(0));
  275. }
  276. EXPORT_SYMBOL(__arm_ioremap);
  277. /*
  278. * Remap an arbitrary physical address space into the kernel virtual
  279. * address space as memory. Needed when the kernel wants to execute
  280. * code in external memory. This is needed for reprogramming source
  281. * clocks that would affect normal memory for example. Please see
  282. * CONFIG_GENERIC_ALLOCATOR for allocating external memory.
  283. */
  284. void __iomem *
  285. __arm_ioremap_exec(unsigned long phys_addr, size_t size, bool cached)
  286. {
  287. unsigned int mtype;
  288. if (cached)
  289. mtype = MT_MEMORY;
  290. else
  291. mtype = MT_MEMORY_NONCACHED;
  292. return __arm_ioremap_caller(phys_addr, size, mtype,
  293. __builtin_return_address(0));
  294. }
  295. void __iounmap(volatile void __iomem *io_addr)
  296. {
  297. void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
  298. struct vm_struct *vm;
  299. read_lock(&vmlist_lock);
  300. for (vm = vmlist; vm; vm = vm->next) {
  301. if (vm->addr > addr)
  302. break;
  303. if (!(vm->flags & VM_IOREMAP))
  304. continue;
  305. /* If this is a static mapping we must leave it alone */
  306. if ((vm->flags & VM_ARM_STATIC_MAPPING) &&
  307. (vm->addr <= addr) && (vm->addr + vm->size > addr)) {
  308. read_unlock(&vmlist_lock);
  309. return;
  310. }
  311. #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
  312. /*
  313. * If this is a section based mapping we need to handle it
  314. * specially as the VM subsystem does not know how to handle
  315. * such a beast.
  316. */
  317. if ((vm->addr == addr) &&
  318. (vm->flags & VM_ARM_SECTION_MAPPING)) {
  319. unmap_area_sections((unsigned long)vm->addr, vm->size);
  320. break;
  321. }
  322. #endif
  323. }
  324. read_unlock(&vmlist_lock);
  325. vunmap(addr);
  326. }
  327. EXPORT_SYMBOL(__iounmap);