ioremap.c 11 KB

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