ioremap.c 13 KB

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