ioremap.c 11 KB

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