1b26e8f27SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-or-later
2b26e8f27SChristophe Leroy
3b26e8f27SChristophe Leroy #include <linux/highmem.h>
4b26e8f27SChristophe Leroy #include <linux/kprobes.h>
5b26e8f27SChristophe Leroy
6b26e8f27SChristophe Leroy /**
7b26e8f27SChristophe Leroy * flush_coherent_icache() - if a CPU has a coherent icache, flush it
8b26e8f27SChristophe Leroy * Return true if the cache was flushed, false otherwise
9b26e8f27SChristophe Leroy */
flush_coherent_icache(void)10131637a1SChristophe Leroy static inline bool flush_coherent_icache(void)
11b26e8f27SChristophe Leroy {
12b26e8f27SChristophe Leroy /*
13b26e8f27SChristophe Leroy * For a snooping icache, we still need a dummy icbi to purge all the
14b26e8f27SChristophe Leroy * prefetched instructions from the ifetch buffers. We also need a sync
1587c78b61SMichael Ellerman * before the icbi to order the actual stores to memory that might
16b26e8f27SChristophe Leroy * have modified instructions with the icbi.
17b26e8f27SChristophe Leroy */
18b26e8f27SChristophe Leroy if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
19b26e8f27SChristophe Leroy mb(); /* sync */
20131637a1SChristophe Leroy icbi((void *)PAGE_OFFSET);
21b26e8f27SChristophe Leroy mb(); /* sync */
22b26e8f27SChristophe Leroy isync();
23b26e8f27SChristophe Leroy return true;
24b26e8f27SChristophe Leroy }
25b26e8f27SChristophe Leroy
26b26e8f27SChristophe Leroy return false;
27b26e8f27SChristophe Leroy }
28b26e8f27SChristophe Leroy
29b26e8f27SChristophe Leroy /**
30b26e8f27SChristophe Leroy * invalidate_icache_range() - Flush the icache by issuing icbi across an address range
31b26e8f27SChristophe Leroy * @start: the start address
32b26e8f27SChristophe Leroy * @stop: the stop address (exclusive)
33b26e8f27SChristophe Leroy */
invalidate_icache_range(unsigned long start,unsigned long stop)34b26e8f27SChristophe Leroy static void invalidate_icache_range(unsigned long start, unsigned long stop)
35b26e8f27SChristophe Leroy {
36b26e8f27SChristophe Leroy unsigned long shift = l1_icache_shift();
37b26e8f27SChristophe Leroy unsigned long bytes = l1_icache_bytes();
38b26e8f27SChristophe Leroy char *addr = (char *)(start & ~(bytes - 1));
39b26e8f27SChristophe Leroy unsigned long size = stop - (unsigned long)addr + (bytes - 1);
40b26e8f27SChristophe Leroy unsigned long i;
41b26e8f27SChristophe Leroy
42b26e8f27SChristophe Leroy for (i = 0; i < size >> shift; i++, addr += bytes)
43b26e8f27SChristophe Leroy icbi(addr);
44b26e8f27SChristophe Leroy
45b26e8f27SChristophe Leroy mb(); /* sync */
46b26e8f27SChristophe Leroy isync();
47b26e8f27SChristophe Leroy }
48b26e8f27SChristophe Leroy
49b26e8f27SChristophe Leroy /**
50b26e8f27SChristophe Leroy * flush_icache_range: Write any modified data cache blocks out to memory
51b26e8f27SChristophe Leroy * and invalidate the corresponding blocks in the instruction cache
52b26e8f27SChristophe Leroy *
53b26e8f27SChristophe Leroy * Generic code will call this after writing memory, before executing from it.
54b26e8f27SChristophe Leroy *
55b26e8f27SChristophe Leroy * @start: the start address
56b26e8f27SChristophe Leroy * @stop: the stop address (exclusive)
57b26e8f27SChristophe Leroy */
flush_icache_range(unsigned long start,unsigned long stop)58b26e8f27SChristophe Leroy void flush_icache_range(unsigned long start, unsigned long stop)
59b26e8f27SChristophe Leroy {
60131637a1SChristophe Leroy if (flush_coherent_icache())
61b26e8f27SChristophe Leroy return;
62b26e8f27SChristophe Leroy
63b26e8f27SChristophe Leroy clean_dcache_range(start, stop);
64b26e8f27SChristophe Leroy
65b26e8f27SChristophe Leroy if (IS_ENABLED(CONFIG_44x)) {
66b26e8f27SChristophe Leroy /*
67b26e8f27SChristophe Leroy * Flash invalidate on 44x because we are passed kmapped
68b26e8f27SChristophe Leroy * addresses and this doesn't work for userspace pages due to
69b26e8f27SChristophe Leroy * the virtually tagged icache.
70b26e8f27SChristophe Leroy */
71b26e8f27SChristophe Leroy iccci((void *)start);
72b26e8f27SChristophe Leroy mb(); /* sync */
73b26e8f27SChristophe Leroy isync();
74b26e8f27SChristophe Leroy } else
75b26e8f27SChristophe Leroy invalidate_icache_range(start, stop);
76b26e8f27SChristophe Leroy }
77b26e8f27SChristophe Leroy EXPORT_SYMBOL(flush_icache_range);
78b26e8f27SChristophe Leroy
7952d49043SChristophe Leroy #ifdef CONFIG_HIGHMEM
80b26e8f27SChristophe Leroy /**
81b26e8f27SChristophe Leroy * flush_dcache_icache_phys() - Flush a page by it's physical address
82b26e8f27SChristophe Leroy * @physaddr: the physical address of the page
83b26e8f27SChristophe Leroy */
flush_dcache_icache_phys(unsigned long physaddr)84b26e8f27SChristophe Leroy static void flush_dcache_icache_phys(unsigned long physaddr)
85b26e8f27SChristophe Leroy {
86b26e8f27SChristophe Leroy unsigned long bytes = l1_dcache_bytes();
87b26e8f27SChristophe Leroy unsigned long nb = PAGE_SIZE / bytes;
88b26e8f27SChristophe Leroy unsigned long addr = physaddr & PAGE_MASK;
89b26e8f27SChristophe Leroy unsigned long msr, msr0;
90b26e8f27SChristophe Leroy unsigned long loop1 = addr, loop2 = addr;
91b26e8f27SChristophe Leroy
92b26e8f27SChristophe Leroy msr0 = mfmsr();
93b26e8f27SChristophe Leroy msr = msr0 & ~MSR_DR;
94b26e8f27SChristophe Leroy /*
95b26e8f27SChristophe Leroy * This must remain as ASM to prevent potential memory accesses
96b26e8f27SChristophe Leroy * while the data MMU is disabled
97b26e8f27SChristophe Leroy */
98b26e8f27SChristophe Leroy asm volatile(
99b26e8f27SChristophe Leroy " mtctr %2;\n"
100b26e8f27SChristophe Leroy " mtmsr %3;\n"
101b26e8f27SChristophe Leroy " isync;\n"
102b26e8f27SChristophe Leroy "0: dcbst 0, %0;\n"
103b26e8f27SChristophe Leroy " addi %0, %0, %4;\n"
104b26e8f27SChristophe Leroy " bdnz 0b;\n"
105b26e8f27SChristophe Leroy " sync;\n"
106b26e8f27SChristophe Leroy " mtctr %2;\n"
107b26e8f27SChristophe Leroy "1: icbi 0, %1;\n"
108b26e8f27SChristophe Leroy " addi %1, %1, %4;\n"
109b26e8f27SChristophe Leroy " bdnz 1b;\n"
110b26e8f27SChristophe Leroy " sync;\n"
111b26e8f27SChristophe Leroy " mtmsr %5;\n"
112b26e8f27SChristophe Leroy " isync;\n"
113b26e8f27SChristophe Leroy : "+&r" (loop1), "+&r" (loop2)
114b26e8f27SChristophe Leroy : "r" (nb), "r" (msr), "i" (bytes), "r" (msr0)
115b26e8f27SChristophe Leroy : "ctr", "memory");
116b26e8f27SChristophe Leroy }
NOKPROBE_SYMBOL(flush_dcache_icache_phys)117b26e8f27SChristophe Leroy NOKPROBE_SYMBOL(flush_dcache_icache_phys)
11852d49043SChristophe Leroy #else
11952d49043SChristophe Leroy static void flush_dcache_icache_phys(unsigned long physaddr)
12052d49043SChristophe Leroy {
12152d49043SChristophe Leroy }
12252d49043SChristophe Leroy #endif
123b26e8f27SChristophe Leroy
124bf26e0bbSChristophe Leroy /**
125bf26e0bbSChristophe Leroy * __flush_dcache_icache(): Flush a particular page from the data cache to RAM.
126bf26e0bbSChristophe Leroy * Note: this is necessary because the instruction cache does *not*
127bf26e0bbSChristophe Leroy * snoop from the data cache.
128bf26e0bbSChristophe Leroy *
129bf26e0bbSChristophe Leroy * @p: the address of the page to flush
130bf26e0bbSChristophe Leroy */
131bf26e0bbSChristophe Leroy static void __flush_dcache_icache(void *p)
132bf26e0bbSChristophe Leroy {
13367b8e6afSChristophe Leroy unsigned long addr = (unsigned long)p & PAGE_MASK;
134bf26e0bbSChristophe Leroy
135bf26e0bbSChristophe Leroy clean_dcache_range(addr, addr + PAGE_SIZE);
136bf26e0bbSChristophe Leroy
137bf26e0bbSChristophe Leroy /*
138bf26e0bbSChristophe Leroy * We don't flush the icache on 44x. Those have a virtual icache and we
139bf26e0bbSChristophe Leroy * don't have access to the virtual address here (it's not the page
140bf26e0bbSChristophe Leroy * vaddr but where it's mapped in user space). The flushing of the
141bf26e0bbSChristophe Leroy * icache on these is handled elsewhere, when a change in the address
142bf26e0bbSChristophe Leroy * space occurs, before returning to user space.
143bf26e0bbSChristophe Leroy */
144bf26e0bbSChristophe Leroy
145bf26e0bbSChristophe Leroy if (mmu_has_feature(MMU_FTR_TYPE_44x))
146bf26e0bbSChristophe Leroy return;
147bf26e0bbSChristophe Leroy
148bf26e0bbSChristophe Leroy invalidate_icache_range(addr, addr + PAGE_SIZE);
149bf26e0bbSChristophe Leroy }
150bf26e0bbSChristophe Leroy
flush_dcache_icache_folio(struct folio * folio)151*9fee28baSMatthew Wilcox (Oracle) void flush_dcache_icache_folio(struct folio *folio)
152b26e8f27SChristophe Leroy {
153*9fee28baSMatthew Wilcox (Oracle) unsigned int i, nr = folio_nr_pages(folio);
154b26e8f27SChristophe Leroy
155e618c7aeSChristophe Leroy if (flush_coherent_icache())
156e618c7aeSChristophe Leroy return;
157b26e8f27SChristophe Leroy
158*9fee28baSMatthew Wilcox (Oracle) if (!folio_test_highmem(folio)) {
159*9fee28baSMatthew Wilcox (Oracle) void *addr = folio_address(folio);
160*9fee28baSMatthew Wilcox (Oracle) for (i = 0; i < nr; i++)
161*9fee28baSMatthew Wilcox (Oracle) __flush_dcache_icache(addr + i * PAGE_SIZE);
16252d49043SChristophe Leroy } else if (IS_ENABLED(CONFIG_BOOKE) || sizeof(phys_addr_t) > sizeof(void *)) {
163*9fee28baSMatthew Wilcox (Oracle) for (i = 0; i < nr; i++) {
164*9fee28baSMatthew Wilcox (Oracle) void *start = kmap_local_folio(folio, i * PAGE_SIZE);
1657e9ab144SChristophe Leroy
166b26e8f27SChristophe Leroy __flush_dcache_icache(start);
1677e9ab144SChristophe Leroy kunmap_local(start);
168*9fee28baSMatthew Wilcox (Oracle) }
169b26e8f27SChristophe Leroy } else {
170*9fee28baSMatthew Wilcox (Oracle) unsigned long pfn = folio_pfn(folio);
171*9fee28baSMatthew Wilcox (Oracle) for (i = 0; i < nr; i++)
172*9fee28baSMatthew Wilcox (Oracle) flush_dcache_icache_phys((pfn + i) * PAGE_SIZE);
173b26e8f27SChristophe Leroy }
174b26e8f27SChristophe Leroy }
175*9fee28baSMatthew Wilcox (Oracle) EXPORT_SYMBOL(flush_dcache_icache_folio);
176b26e8f27SChristophe Leroy
clear_user_page(void * page,unsigned long vaddr,struct page * pg)177b26e8f27SChristophe Leroy void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
178b26e8f27SChristophe Leroy {
179b26e8f27SChristophe Leroy clear_page(page);
180b26e8f27SChristophe Leroy
181b26e8f27SChristophe Leroy /*
182b26e8f27SChristophe Leroy * We shouldn't have to do this, but some versions of glibc
183b26e8f27SChristophe Leroy * require it (ld.so assumes zero filled pages are icache clean)
184b26e8f27SChristophe Leroy * - Anton
185b26e8f27SChristophe Leroy */
186b26e8f27SChristophe Leroy flush_dcache_page(pg);
187b26e8f27SChristophe Leroy }
188b26e8f27SChristophe Leroy EXPORT_SYMBOL(clear_user_page);
189b26e8f27SChristophe Leroy
copy_user_page(void * vto,void * vfrom,unsigned long vaddr,struct page * pg)190b26e8f27SChristophe Leroy void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
191b26e8f27SChristophe Leroy struct page *pg)
192b26e8f27SChristophe Leroy {
193b26e8f27SChristophe Leroy copy_page(vto, vfrom);
194b26e8f27SChristophe Leroy
195b26e8f27SChristophe Leroy /*
196b26e8f27SChristophe Leroy * We should be able to use the following optimisation, however
197b26e8f27SChristophe Leroy * there are two problems.
198b26e8f27SChristophe Leroy * Firstly a bug in some versions of binutils meant PLT sections
199b26e8f27SChristophe Leroy * were not marked executable.
200b26e8f27SChristophe Leroy * Secondly the first word in the GOT section is blrl, used
201b26e8f27SChristophe Leroy * to establish the GOT address. Until recently the GOT was
202b26e8f27SChristophe Leroy * not marked executable.
203b26e8f27SChristophe Leroy * - Anton
204b26e8f27SChristophe Leroy */
205b26e8f27SChristophe Leroy #if 0
206b26e8f27SChristophe Leroy if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
207b26e8f27SChristophe Leroy return;
208b26e8f27SChristophe Leroy #endif
209b26e8f27SChristophe Leroy
210b26e8f27SChristophe Leroy flush_dcache_page(pg);
211b26e8f27SChristophe Leroy }
212b26e8f27SChristophe Leroy
flush_icache_user_page(struct vm_area_struct * vma,struct page * page,unsigned long addr,int len)213b26e8f27SChristophe Leroy void flush_icache_user_page(struct vm_area_struct *vma, struct page *page,
214b26e8f27SChristophe Leroy unsigned long addr, int len)
215b26e8f27SChristophe Leroy {
2167e9ab144SChristophe Leroy void *maddr;
217b26e8f27SChristophe Leroy
2187e9ab144SChristophe Leroy maddr = kmap_local_page(page) + (addr & ~PAGE_MASK);
2197e9ab144SChristophe Leroy flush_icache_range((unsigned long)maddr, (unsigned long)maddr + len);
2207e9ab144SChristophe Leroy kunmap_local(maddr);
221b26e8f27SChristophe Leroy }
222