Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Based on arch/arm/mm/flush.c |
| 4 | * |
| 5 | * Copyright (C) 1995-2002 Russell King |
| 6 | * Copyright (C) 2012 ARM Ltd. |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/pagemap.h> |
| 12 | |
| 13 | #include <asm/cacheflush.h> |
Will Deacon | 02f7760 | 2017-03-10 20:32:23 +0000 | [diff] [blame] | 14 | #include <asm/cache.h> |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 15 | #include <asm/tlbflush.h> |
| 16 | |
Pratyush Anand | 9842cea | 2016-11-02 14:40:46 +0530 | [diff] [blame] | 17 | void sync_icache_aliases(void *kaddr, unsigned long len) |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 18 | { |
| 19 | unsigned long addr = (unsigned long)kaddr; |
| 20 | |
| 21 | if (icache_is_aliasing()) { |
| 22 | __clean_dcache_area_pou(kaddr, len); |
| 23 | __flush_icache_all(); |
| 24 | } else { |
Catalin Marinas | 132fdc3 | 2019-01-24 17:28:37 +0000 | [diff] [blame] | 25 | /* |
| 26 | * Don't issue kick_all_cpus_sync() after I-cache invalidation |
| 27 | * for user mappings. |
| 28 | */ |
| 29 | __flush_icache_range(addr, addr + len); |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 33 | static void flush_ptrace_access(struct vm_area_struct *vma, struct page *page, |
| 34 | unsigned long uaddr, void *kaddr, |
| 35 | unsigned long len) |
| 36 | { |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 37 | if (vma->vm_flags & VM_EXEC) |
| 38 | sync_icache_aliases(kaddr, len); |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Copy user data from/to a page which is mapped into a different processes |
| 43 | * address space. Really, we want to allow our "user space" model to handle |
| 44 | * this. |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 45 | */ |
| 46 | void copy_to_user_page(struct vm_area_struct *vma, struct page *page, |
| 47 | unsigned long uaddr, void *dst, const void *src, |
| 48 | unsigned long len) |
| 49 | { |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 50 | memcpy(dst, src, len); |
| 51 | flush_ptrace_access(vma, page, uaddr, dst, len); |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Shaokun Zhang | 907e21c | 2018-04-17 20:03:09 +0800 | [diff] [blame] | 54 | void __sync_icache_dcache(pte_t pte) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 55 | { |
Catalin Marinas | 7249b79 | 2013-05-01 16:34:22 +0100 | [diff] [blame] | 56 | struct page *page = pte_page(pte); |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 57 | |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 58 | if (!test_and_set_bit(PG_dcache_clean, &page->flags)) |
| 59 | sync_icache_aliases(page_address(page), |
| 60 | PAGE_SIZE << compound_order(page)); |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 61 | } |
Ben Hutchings | c515710 | 2018-07-12 00:18:22 +0100 | [diff] [blame] | 62 | EXPORT_SYMBOL_GPL(__sync_icache_dcache); |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
Catalin Marinas | b5b6c9e | 2013-05-01 12:23:05 +0100 | [diff] [blame] | 65 | * This function is called when a page has been modified by the kernel. Mark |
| 66 | * it as dirty for later flushing when mapped in user space (if executable, |
| 67 | * see __sync_icache_dcache). |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 68 | */ |
| 69 | void flush_dcache_page(struct page *page) |
| 70 | { |
Catalin Marinas | b5b6c9e | 2013-05-01 12:23:05 +0100 | [diff] [blame] | 71 | if (test_bit(PG_dcache_clean, &page->flags)) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 72 | clear_bit(PG_dcache_clean, &page->flags); |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL(flush_dcache_page); |
| 75 | |
| 76 | /* |
| 77 | * Additional functions defined in assembly. |
| 78 | */ |
Will Deacon | bedbeec | 2018-07-06 16:21:17 +0100 | [diff] [blame] | 79 | EXPORT_SYMBOL(__flush_icache_range); |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 80 | |
| 81 | #ifdef CONFIG_ARCH_HAS_PMEM_API |
Arnd Bergmann | caf5ef7 | 2017-08-10 16:52:31 +0200 | [diff] [blame] | 82 | void arch_wb_cache_pmem(void *addr, size_t size) |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 83 | { |
| 84 | /* Ensure order against any prior non-cacheable writes */ |
| 85 | dmb(osh); |
| 86 | __clean_dcache_area_pop(addr, size); |
| 87 | } |
| 88 | EXPORT_SYMBOL_GPL(arch_wb_cache_pmem); |
| 89 | |
Arnd Bergmann | caf5ef7 | 2017-08-10 16:52:31 +0200 | [diff] [blame] | 90 | void arch_invalidate_pmem(void *addr, size_t size) |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 91 | { |
| 92 | __inval_dcache_area(addr, size); |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(arch_invalidate_pmem); |
| 95 | #endif |