ioremap.c 10 KB

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