ioremap.c 10 KB

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