ioremap.c 12 KB

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