ioremap.c 12 KB

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