xref: /openbmc/linux/arch/parisc/kernel/cache.c (revision d35ac6ac)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1999-2006 Helge Deller <deller@gmx.de> (07-13-1999)
7  * Copyright (C) 1999 SuSE GmbH Nuernberg
8  * Copyright (C) 2000 Philipp Rumpf (prumpf@tux.org)
9  *
10  * Cache and TLB management
11  *
12  */
13 
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/seq_file.h>
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/sched/mm.h>
22 #include <asm/pdc.h>
23 #include <asm/cache.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
26 #include <asm/page.h>
27 #include <asm/processor.h>
28 #include <asm/sections.h>
29 #include <asm/shmparam.h>
30 #include <asm/mmu_context.h>
31 
32 int split_tlb __ro_after_init;
33 int dcache_stride __ro_after_init;
34 int icache_stride __ro_after_init;
35 EXPORT_SYMBOL(dcache_stride);
36 
37 void flush_dcache_page_asm(unsigned long phys_addr, unsigned long vaddr);
38 EXPORT_SYMBOL(flush_dcache_page_asm);
39 void purge_dcache_page_asm(unsigned long phys_addr, unsigned long vaddr);
40 void flush_icache_page_asm(unsigned long phys_addr, unsigned long vaddr);
41 
42 /* Internal implementation in arch/parisc/kernel/pacache.S */
43 void flush_data_cache_local(void *);  /* flushes local data-cache only */
44 void flush_instruction_cache_local(void); /* flushes local code-cache only */
45 
46 /* On some machines (i.e., ones with the Merced bus), there can be
47  * only a single PxTLB broadcast at a time; this must be guaranteed
48  * by software. We need a spinlock around all TLB flushes to ensure
49  * this.
50  */
51 DEFINE_SPINLOCK(pa_tlb_flush_lock);
52 
53 #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
54 int pa_serialize_tlb_flushes __ro_after_init;
55 #endif
56 
57 struct pdc_cache_info cache_info __ro_after_init;
58 #ifndef CONFIG_PA20
59 static struct pdc_btlb_info btlb_info __ro_after_init;
60 #endif
61 
62 DEFINE_STATIC_KEY_TRUE(parisc_has_cache);
63 DEFINE_STATIC_KEY_TRUE(parisc_has_dcache);
64 DEFINE_STATIC_KEY_TRUE(parisc_has_icache);
65 
66 static void cache_flush_local_cpu(void *dummy)
67 {
68 	if (static_branch_likely(&parisc_has_icache))
69 		flush_instruction_cache_local();
70 	if (static_branch_likely(&parisc_has_dcache))
71 		flush_data_cache_local(NULL);
72 }
73 
74 void flush_cache_all_local(void)
75 {
76 	cache_flush_local_cpu(NULL);
77 }
78 
79 void flush_cache_all(void)
80 {
81 	if (static_branch_likely(&parisc_has_cache))
82 		on_each_cpu(cache_flush_local_cpu, NULL, 1);
83 }
84 
85 static inline void flush_data_cache(void)
86 {
87 	if (static_branch_likely(&parisc_has_dcache))
88 		on_each_cpu(flush_data_cache_local, NULL, 1);
89 }
90 
91 
92 /* Kernel virtual address of pfn.  */
93 #define pfn_va(pfn)	__va(PFN_PHYS(pfn))
94 
95 void
96 __update_cache(pte_t pte)
97 {
98 	unsigned long pfn = pte_pfn(pte);
99 	struct page *page;
100 
101 	/* We don't have pte special.  As a result, we can be called with
102 	   an invalid pfn and we don't need to flush the kernel dcache page.
103 	   This occurs with FireGL card in C8000.  */
104 	if (!pfn_valid(pfn))
105 		return;
106 
107 	page = pfn_to_page(pfn);
108 	if (page_mapping_file(page) &&
109 	    test_bit(PG_dcache_dirty, &page->flags)) {
110 		flush_kernel_dcache_page_addr(pfn_va(pfn));
111 		clear_bit(PG_dcache_dirty, &page->flags);
112 	} else if (parisc_requires_coherency())
113 		flush_kernel_dcache_page_addr(pfn_va(pfn));
114 }
115 
116 void
117 show_cache_info(struct seq_file *m)
118 {
119 	char buf[32];
120 
121 	seq_printf(m, "I-cache\t\t: %ld KB\n",
122 		cache_info.ic_size/1024 );
123 	if (cache_info.dc_loop != 1)
124 		snprintf(buf, 32, "%lu-way associative", cache_info.dc_loop);
125 	seq_printf(m, "D-cache\t\t: %ld KB (%s%s, %s, alias=%d)\n",
126 		cache_info.dc_size/1024,
127 		(cache_info.dc_conf.cc_wt ? "WT":"WB"),
128 		(cache_info.dc_conf.cc_sh ? ", shared I/D":""),
129 		((cache_info.dc_loop == 1) ? "direct mapped" : buf),
130 		cache_info.dc_conf.cc_alias
131 	);
132 	seq_printf(m, "ITLB entries\t: %ld\n" "DTLB entries\t: %ld%s\n",
133 		cache_info.it_size,
134 		cache_info.dt_size,
135 		cache_info.dt_conf.tc_sh ? " - shared with ITLB":""
136 	);
137 
138 #ifndef CONFIG_PA20
139 	/* BTLB - Block TLB */
140 	if (btlb_info.max_size==0) {
141 		seq_printf(m, "BTLB\t\t: not supported\n" );
142 	} else {
143 		seq_printf(m,
144 		"BTLB fixed\t: max. %d pages, pagesize=%d (%dMB)\n"
145 		"BTLB fix-entr.\t: %d instruction, %d data (%d combined)\n"
146 		"BTLB var-entr.\t: %d instruction, %d data (%d combined)\n",
147 		btlb_info.max_size, (int)4096,
148 		btlb_info.max_size>>8,
149 		btlb_info.fixed_range_info.num_i,
150 		btlb_info.fixed_range_info.num_d,
151 		btlb_info.fixed_range_info.num_comb,
152 		btlb_info.variable_range_info.num_i,
153 		btlb_info.variable_range_info.num_d,
154 		btlb_info.variable_range_info.num_comb
155 		);
156 	}
157 #endif
158 }
159 
160 void __init
161 parisc_cache_init(void)
162 {
163 	if (pdc_cache_info(&cache_info) < 0)
164 		panic("parisc_cache_init: pdc_cache_info failed");
165 
166 #if 0
167 	printk("ic_size %lx dc_size %lx it_size %lx\n",
168 		cache_info.ic_size,
169 		cache_info.dc_size,
170 		cache_info.it_size);
171 
172 	printk("DC  base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
173 		cache_info.dc_base,
174 		cache_info.dc_stride,
175 		cache_info.dc_count,
176 		cache_info.dc_loop);
177 
178 	printk("dc_conf = 0x%lx  alias %d blk %d line %d shift %d\n",
179 		*(unsigned long *) (&cache_info.dc_conf),
180 		cache_info.dc_conf.cc_alias,
181 		cache_info.dc_conf.cc_block,
182 		cache_info.dc_conf.cc_line,
183 		cache_info.dc_conf.cc_shift);
184 	printk("	wt %d sh %d cst %d hv %d\n",
185 		cache_info.dc_conf.cc_wt,
186 		cache_info.dc_conf.cc_sh,
187 		cache_info.dc_conf.cc_cst,
188 		cache_info.dc_conf.cc_hv);
189 
190 	printk("IC  base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
191 		cache_info.ic_base,
192 		cache_info.ic_stride,
193 		cache_info.ic_count,
194 		cache_info.ic_loop);
195 
196 	printk("IT  base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx off_base 0x%lx off_stride 0x%lx off_count 0x%lx\n",
197 		cache_info.it_sp_base,
198 		cache_info.it_sp_stride,
199 		cache_info.it_sp_count,
200 		cache_info.it_loop,
201 		cache_info.it_off_base,
202 		cache_info.it_off_stride,
203 		cache_info.it_off_count);
204 
205 	printk("DT  base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx off_base 0x%lx off_stride 0x%lx off_count 0x%lx\n",
206 		cache_info.dt_sp_base,
207 		cache_info.dt_sp_stride,
208 		cache_info.dt_sp_count,
209 		cache_info.dt_loop,
210 		cache_info.dt_off_base,
211 		cache_info.dt_off_stride,
212 		cache_info.dt_off_count);
213 
214 	printk("ic_conf = 0x%lx  alias %d blk %d line %d shift %d\n",
215 		*(unsigned long *) (&cache_info.ic_conf),
216 		cache_info.ic_conf.cc_alias,
217 		cache_info.ic_conf.cc_block,
218 		cache_info.ic_conf.cc_line,
219 		cache_info.ic_conf.cc_shift);
220 	printk("	wt %d sh %d cst %d hv %d\n",
221 		cache_info.ic_conf.cc_wt,
222 		cache_info.ic_conf.cc_sh,
223 		cache_info.ic_conf.cc_cst,
224 		cache_info.ic_conf.cc_hv);
225 
226 	printk("D-TLB conf: sh %d page %d cst %d aid %d sr %d\n",
227 		cache_info.dt_conf.tc_sh,
228 		cache_info.dt_conf.tc_page,
229 		cache_info.dt_conf.tc_cst,
230 		cache_info.dt_conf.tc_aid,
231 		cache_info.dt_conf.tc_sr);
232 
233 	printk("I-TLB conf: sh %d page %d cst %d aid %d sr %d\n",
234 		cache_info.it_conf.tc_sh,
235 		cache_info.it_conf.tc_page,
236 		cache_info.it_conf.tc_cst,
237 		cache_info.it_conf.tc_aid,
238 		cache_info.it_conf.tc_sr);
239 #endif
240 
241 	split_tlb = 0;
242 	if (cache_info.dt_conf.tc_sh == 0 || cache_info.dt_conf.tc_sh == 2) {
243 		if (cache_info.dt_conf.tc_sh == 2)
244 			printk(KERN_WARNING "Unexpected TLB configuration. "
245 			"Will flush I/D separately (could be optimized).\n");
246 
247 		split_tlb = 1;
248 	}
249 
250 	/* "New and Improved" version from Jim Hull
251 	 *	(1 << (cc_block-1)) * (cc_line << (4 + cnf.cc_shift))
252 	 * The following CAFL_STRIDE is an optimized version, see
253 	 * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023625.html
254 	 * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023671.html
255 	 */
256 #define CAFL_STRIDE(cnf) (cnf.cc_line << (3 + cnf.cc_block + cnf.cc_shift))
257 	dcache_stride = CAFL_STRIDE(cache_info.dc_conf);
258 	icache_stride = CAFL_STRIDE(cache_info.ic_conf);
259 #undef CAFL_STRIDE
260 
261 #ifndef CONFIG_PA20
262 	if (pdc_btlb_info(&btlb_info) < 0) {
263 		memset(&btlb_info, 0, sizeof btlb_info);
264 	}
265 #endif
266 
267 	if ((boot_cpu_data.pdc.capabilities & PDC_MODEL_NVA_MASK) ==
268 						PDC_MODEL_NVA_UNSUPPORTED) {
269 		printk(KERN_WARNING "parisc_cache_init: Only equivalent aliasing supported!\n");
270 #if 0
271 		panic("SMP kernel required to avoid non-equivalent aliasing");
272 #endif
273 	}
274 }
275 
276 void disable_sr_hashing(void)
277 {
278 	int srhash_type, retval;
279 	unsigned long space_bits;
280 
281 	switch (boot_cpu_data.cpu_type) {
282 	case pcx: /* We shouldn't get this far.  setup.c should prevent it. */
283 		BUG();
284 		return;
285 
286 	case pcxs:
287 	case pcxt:
288 	case pcxt_:
289 		srhash_type = SRHASH_PCXST;
290 		break;
291 
292 	case pcxl:
293 		srhash_type = SRHASH_PCXL;
294 		break;
295 
296 	case pcxl2: /* pcxl2 doesn't support space register hashing */
297 		return;
298 
299 	default: /* Currently all PA2.0 machines use the same ins. sequence */
300 		srhash_type = SRHASH_PA20;
301 		break;
302 	}
303 
304 	disable_sr_hashing_asm(srhash_type);
305 
306 	retval = pdc_spaceid_bits(&space_bits);
307 	/* If this procedure isn't implemented, don't panic. */
308 	if (retval < 0 && retval != PDC_BAD_OPTION)
309 		panic("pdc_spaceid_bits call failed.\n");
310 	if (space_bits != 0)
311 		panic("SpaceID hashing is still on!\n");
312 }
313 
314 static inline void
315 __flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,
316 		   unsigned long physaddr)
317 {
318 	if (!static_branch_likely(&parisc_has_cache))
319 		return;
320 	preempt_disable();
321 	flush_dcache_page_asm(physaddr, vmaddr);
322 	if (vma->vm_flags & VM_EXEC)
323 		flush_icache_page_asm(physaddr, vmaddr);
324 	preempt_enable();
325 }
326 
327 static void flush_user_cache_page(struct vm_area_struct *vma, unsigned long vmaddr)
328 {
329 	unsigned long flags, space, pgd, prot;
330 #ifdef CONFIG_TLB_PTLOCK
331 	unsigned long pgd_lock;
332 #endif
333 
334 	vmaddr &= PAGE_MASK;
335 
336 	preempt_disable();
337 
338 	/* Set context for flush */
339 	local_irq_save(flags);
340 	prot = mfctl(8);
341 	space = mfsp(SR_USER);
342 	pgd = mfctl(25);
343 #ifdef CONFIG_TLB_PTLOCK
344 	pgd_lock = mfctl(28);
345 #endif
346 	switch_mm_irqs_off(NULL, vma->vm_mm, NULL);
347 	local_irq_restore(flags);
348 
349 	flush_user_dcache_range_asm(vmaddr, vmaddr + PAGE_SIZE);
350 	if (vma->vm_flags & VM_EXEC)
351 		flush_user_icache_range_asm(vmaddr, vmaddr + PAGE_SIZE);
352 	flush_tlb_page(vma, vmaddr);
353 
354 	/* Restore previous context */
355 	local_irq_save(flags);
356 #ifdef CONFIG_TLB_PTLOCK
357 	mtctl(pgd_lock, 28);
358 #endif
359 	mtctl(pgd, 25);
360 	mtsp(space, SR_USER);
361 	mtctl(prot, 8);
362 	local_irq_restore(flags);
363 
364 	preempt_enable();
365 }
366 
367 static inline pte_t *get_ptep(struct mm_struct *mm, unsigned long addr)
368 {
369 	pte_t *ptep = NULL;
370 	pgd_t *pgd = mm->pgd;
371 	p4d_t *p4d;
372 	pud_t *pud;
373 	pmd_t *pmd;
374 
375 	if (!pgd_none(*pgd)) {
376 		p4d = p4d_offset(pgd, addr);
377 		if (!p4d_none(*p4d)) {
378 			pud = pud_offset(p4d, addr);
379 			if (!pud_none(*pud)) {
380 				pmd = pmd_offset(pud, addr);
381 				if (!pmd_none(*pmd))
382 					ptep = pte_offset_map(pmd, addr);
383 			}
384 		}
385 	}
386 	return ptep;
387 }
388 
389 static inline bool pte_needs_flush(pte_t pte)
390 {
391 	return (pte_val(pte) & (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_NO_CACHE))
392 		== (_PAGE_PRESENT | _PAGE_ACCESSED);
393 }
394 
395 void flush_dcache_page(struct page *page)
396 {
397 	struct address_space *mapping = page_mapping_file(page);
398 	struct vm_area_struct *mpnt;
399 	unsigned long offset;
400 	unsigned long addr, old_addr = 0;
401 	unsigned long count = 0;
402 	unsigned long flags;
403 	pgoff_t pgoff;
404 
405 	if (mapping && !mapping_mapped(mapping)) {
406 		set_bit(PG_dcache_dirty, &page->flags);
407 		return;
408 	}
409 
410 	flush_kernel_dcache_page_addr(page_address(page));
411 
412 	if (!mapping)
413 		return;
414 
415 	pgoff = page->index;
416 
417 	/*
418 	 * We have carefully arranged in arch_get_unmapped_area() that
419 	 * *any* mappings of a file are always congruently mapped (whether
420 	 * declared as MAP_PRIVATE or MAP_SHARED), so we only need
421 	 * to flush one address here for them all to become coherent
422 	 * on machines that support equivalent aliasing
423 	 */
424 	flush_dcache_mmap_lock_irqsave(mapping, flags);
425 	vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {
426 		offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
427 		addr = mpnt->vm_start + offset;
428 		if (parisc_requires_coherency()) {
429 			bool needs_flush = false;
430 			pte_t *ptep;
431 
432 			ptep = get_ptep(mpnt->vm_mm, addr);
433 			if (ptep) {
434 				needs_flush = pte_needs_flush(*ptep);
435 				pte_unmap(ptep);
436 			}
437 			if (needs_flush)
438 				flush_user_cache_page(mpnt, addr);
439 		} else {
440 			/*
441 			 * The TLB is the engine of coherence on parisc:
442 			 * The CPU is entitled to speculate any page
443 			 * with a TLB mapping, so here we kill the
444 			 * mapping then flush the page along a special
445 			 * flush only alias mapping. This guarantees that
446 			 * the page is no-longer in the cache for any
447 			 * process and nor may it be speculatively read
448 			 * in (until the user or kernel specifically
449 			 * accesses it, of course)
450 			 */
451 			flush_tlb_page(mpnt, addr);
452 			if (old_addr == 0 || (old_addr & (SHM_COLOUR - 1))
453 					!= (addr & (SHM_COLOUR - 1))) {
454 				__flush_cache_page(mpnt, addr, page_to_phys(page));
455 				/*
456 				 * Software is allowed to have any number
457 				 * of private mappings to a page.
458 				 */
459 				if (!(mpnt->vm_flags & VM_SHARED))
460 					continue;
461 				if (old_addr)
462 					pr_err("INEQUIVALENT ALIASES 0x%lx and 0x%lx in file %pD\n",
463 						old_addr, addr, mpnt->vm_file);
464 				old_addr = addr;
465 			}
466 		}
467 		WARN_ON(++count == 4096);
468 	}
469 	flush_dcache_mmap_unlock_irqrestore(mapping, flags);
470 }
471 EXPORT_SYMBOL(flush_dcache_page);
472 
473 /* Defined in arch/parisc/kernel/pacache.S */
474 EXPORT_SYMBOL(flush_kernel_dcache_range_asm);
475 EXPORT_SYMBOL(flush_kernel_icache_range_asm);
476 
477 #define FLUSH_THRESHOLD 0x80000 /* 0.5MB */
478 static unsigned long parisc_cache_flush_threshold __ro_after_init = FLUSH_THRESHOLD;
479 
480 #define FLUSH_TLB_THRESHOLD (16*1024) /* 16 KiB minimum TLB threshold */
481 static unsigned long parisc_tlb_flush_threshold __ro_after_init = ~0UL;
482 
483 void __init parisc_setup_cache_timing(void)
484 {
485 	unsigned long rangetime, alltime;
486 	unsigned long size;
487 	unsigned long threshold, threshold2;
488 
489 	alltime = mfctl(16);
490 	flush_data_cache();
491 	alltime = mfctl(16) - alltime;
492 
493 	size = (unsigned long)(_end - _text);
494 	rangetime = mfctl(16);
495 	flush_kernel_dcache_range((unsigned long)_text, size);
496 	rangetime = mfctl(16) - rangetime;
497 
498 	printk(KERN_DEBUG "Whole cache flush %lu cycles, flushing %lu bytes %lu cycles\n",
499 		alltime, size, rangetime);
500 
501 	threshold = L1_CACHE_ALIGN((unsigned long)((uint64_t)size * alltime / rangetime));
502 	pr_info("Calculated flush threshold is %lu KiB\n",
503 		threshold/1024);
504 
505 	/*
506 	 * The threshold computed above isn't very reliable. The following
507 	 * heuristic works reasonably well on c8000/rp3440.
508 	 */
509 	threshold2 = cache_info.dc_size * num_online_cpus();
510 	parisc_cache_flush_threshold = threshold2;
511 	printk(KERN_INFO "Cache flush threshold set to %lu KiB\n",
512 		parisc_cache_flush_threshold/1024);
513 
514 	/* calculate TLB flush threshold */
515 
516 	/* On SMP machines, skip the TLB measure of kernel text which
517 	 * has been mapped as huge pages. */
518 	if (num_online_cpus() > 1 && !parisc_requires_coherency()) {
519 		threshold = max(cache_info.it_size, cache_info.dt_size);
520 		threshold *= PAGE_SIZE;
521 		threshold /= num_online_cpus();
522 		goto set_tlb_threshold;
523 	}
524 
525 	size = (unsigned long)_end - (unsigned long)_text;
526 	rangetime = mfctl(16);
527 	flush_tlb_kernel_range((unsigned long)_text, (unsigned long)_end);
528 	rangetime = mfctl(16) - rangetime;
529 
530 	alltime = mfctl(16);
531 	flush_tlb_all();
532 	alltime = mfctl(16) - alltime;
533 
534 	printk(KERN_INFO "Whole TLB flush %lu cycles, Range flush %lu bytes %lu cycles\n",
535 		alltime, size, rangetime);
536 
537 	threshold = PAGE_ALIGN((num_online_cpus() * size * alltime) / rangetime);
538 	printk(KERN_INFO "Calculated TLB flush threshold %lu KiB\n",
539 		threshold/1024);
540 
541 set_tlb_threshold:
542 	if (threshold > FLUSH_TLB_THRESHOLD)
543 		parisc_tlb_flush_threshold = threshold;
544 	else
545 		parisc_tlb_flush_threshold = FLUSH_TLB_THRESHOLD;
546 
547 	printk(KERN_INFO "TLB flush threshold set to %lu KiB\n",
548 		parisc_tlb_flush_threshold/1024);
549 }
550 
551 extern void purge_kernel_dcache_page_asm(unsigned long);
552 extern void clear_user_page_asm(void *, unsigned long);
553 extern void copy_user_page_asm(void *, void *, unsigned long);
554 
555 void flush_kernel_dcache_page_addr(const void *addr)
556 {
557 	unsigned long flags;
558 
559 	flush_kernel_dcache_page_asm(addr);
560 	purge_tlb_start(flags);
561 	pdtlb(SR_KERNEL, addr);
562 	purge_tlb_end(flags);
563 }
564 EXPORT_SYMBOL(flush_kernel_dcache_page_addr);
565 
566 static void flush_cache_page_if_present(struct vm_area_struct *vma,
567 	unsigned long vmaddr, unsigned long pfn)
568 {
569 	bool needs_flush = false;
570 	pte_t *ptep;
571 
572 	/*
573 	 * The pte check is racy and sometimes the flush will trigger
574 	 * a non-access TLB miss. Hopefully, the page has already been
575 	 * flushed.
576 	 */
577 	ptep = get_ptep(vma->vm_mm, vmaddr);
578 	if (ptep) {
579 		needs_flush = pte_needs_flush(*ptep);
580 		pte_unmap(ptep);
581 	}
582 	if (needs_flush)
583 		flush_cache_page(vma, vmaddr, pfn);
584 }
585 
586 void copy_user_highpage(struct page *to, struct page *from,
587 	unsigned long vaddr, struct vm_area_struct *vma)
588 {
589 	void *kto, *kfrom;
590 
591 	kfrom = kmap_local_page(from);
592 	kto = kmap_local_page(to);
593 	flush_cache_page_if_present(vma, vaddr, page_to_pfn(from));
594 	copy_page_asm(kto, kfrom);
595 	kunmap_local(kto);
596 	kunmap_local(kfrom);
597 }
598 
599 void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
600 		unsigned long user_vaddr, void *dst, void *src, int len)
601 {
602 	flush_cache_page_if_present(vma, user_vaddr, page_to_pfn(page));
603 	memcpy(dst, src, len);
604 	flush_kernel_dcache_range_asm((unsigned long)dst, (unsigned long)dst + len);
605 }
606 
607 void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
608 		unsigned long user_vaddr, void *dst, void *src, int len)
609 {
610 	flush_cache_page_if_present(vma, user_vaddr, page_to_pfn(page));
611 	memcpy(dst, src, len);
612 }
613 
614 /* __flush_tlb_range()
615  *
616  * returns 1 if all TLBs were flushed.
617  */
618 int __flush_tlb_range(unsigned long sid, unsigned long start,
619 		      unsigned long end)
620 {
621 	unsigned long flags;
622 
623 	if ((!IS_ENABLED(CONFIG_SMP) || !arch_irqs_disabled()) &&
624 	    end - start >= parisc_tlb_flush_threshold) {
625 		flush_tlb_all();
626 		return 1;
627 	}
628 
629 	/* Purge TLB entries for small ranges using the pdtlb and
630 	   pitlb instructions.  These instructions execute locally
631 	   but cause a purge request to be broadcast to other TLBs.  */
632 	while (start < end) {
633 		purge_tlb_start(flags);
634 		mtsp(sid, SR_TEMP1);
635 		pdtlb(SR_TEMP1, start);
636 		pitlb(SR_TEMP1, start);
637 		purge_tlb_end(flags);
638 		start += PAGE_SIZE;
639 	}
640 	return 0;
641 }
642 
643 static void flush_cache_pages(struct vm_area_struct *vma, unsigned long start, unsigned long end)
644 {
645 	unsigned long addr, pfn;
646 	pte_t *ptep;
647 
648 	for (addr = start; addr < end; addr += PAGE_SIZE) {
649 		bool needs_flush = false;
650 		/*
651 		 * The vma can contain pages that aren't present. Although
652 		 * the pte search is expensive, we need the pte to find the
653 		 * page pfn and to check whether the page should be flushed.
654 		 */
655 		ptep = get_ptep(vma->vm_mm, addr);
656 		if (ptep) {
657 			needs_flush = pte_needs_flush(*ptep);
658 			pfn = pte_pfn(*ptep);
659 			pte_unmap(ptep);
660 		}
661 		if (needs_flush) {
662 			if (parisc_requires_coherency()) {
663 				flush_user_cache_page(vma, addr);
664 			} else {
665 				if (WARN_ON(!pfn_valid(pfn)))
666 					return;
667 				__flush_cache_page(vma, addr, PFN_PHYS(pfn));
668 			}
669 		}
670 	}
671 }
672 
673 static inline unsigned long mm_total_size(struct mm_struct *mm)
674 {
675 	struct vm_area_struct *vma;
676 	unsigned long usize = 0;
677 	VMA_ITERATOR(vmi, mm, 0);
678 
679 	for_each_vma(vmi, vma) {
680 		if (usize >= parisc_cache_flush_threshold)
681 			break;
682 		usize += vma->vm_end - vma->vm_start;
683 	}
684 	return usize;
685 }
686 
687 void flush_cache_mm(struct mm_struct *mm)
688 {
689 	struct vm_area_struct *vma;
690 	VMA_ITERATOR(vmi, mm, 0);
691 
692 	/*
693 	 * Flushing the whole cache on each cpu takes forever on
694 	 * rp3440, etc. So, avoid it if the mm isn't too big.
695 	 *
696 	 * Note that we must flush the entire cache on machines
697 	 * with aliasing caches to prevent random segmentation
698 	 * faults.
699 	 */
700 	if (!parisc_requires_coherency()
701 	    ||  mm_total_size(mm) >= parisc_cache_flush_threshold) {
702 		if (WARN_ON(IS_ENABLED(CONFIG_SMP) && arch_irqs_disabled()))
703 			return;
704 		flush_tlb_all();
705 		flush_cache_all();
706 		return;
707 	}
708 
709 	/* Flush mm */
710 	for_each_vma(vmi, vma)
711 		flush_cache_pages(vma, vma->vm_start, vma->vm_end);
712 }
713 
714 void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
715 {
716 	if (!parisc_requires_coherency()
717 	    || end - start >= parisc_cache_flush_threshold) {
718 		if (WARN_ON(IS_ENABLED(CONFIG_SMP) && arch_irqs_disabled()))
719 			return;
720 		flush_tlb_range(vma, start, end);
721 		flush_cache_all();
722 		return;
723 	}
724 
725 	flush_cache_pages(vma, start, end);
726 }
727 
728 void flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr, unsigned long pfn)
729 {
730 	if (WARN_ON(!pfn_valid(pfn)))
731 		return;
732 	if (parisc_requires_coherency())
733 		flush_user_cache_page(vma, vmaddr);
734 	else
735 		__flush_cache_page(vma, vmaddr, PFN_PHYS(pfn));
736 }
737 
738 void flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
739 {
740 	if (!PageAnon(page))
741 		return;
742 
743 	if (parisc_requires_coherency()) {
744 		if (vma->vm_flags & VM_SHARED)
745 			flush_data_cache();
746 		else
747 			flush_user_cache_page(vma, vmaddr);
748 		return;
749 	}
750 
751 	flush_tlb_page(vma, vmaddr);
752 	preempt_disable();
753 	flush_dcache_page_asm(page_to_phys(page), vmaddr);
754 	preempt_enable();
755 }
756 
757 void flush_kernel_vmap_range(void *vaddr, int size)
758 {
759 	unsigned long start = (unsigned long)vaddr;
760 	unsigned long end = start + size;
761 
762 	if ((!IS_ENABLED(CONFIG_SMP) || !arch_irqs_disabled()) &&
763 	    (unsigned long)size >= parisc_cache_flush_threshold) {
764 		flush_tlb_kernel_range(start, end);
765 		flush_data_cache();
766 		return;
767 	}
768 
769 	flush_kernel_dcache_range_asm(start, end);
770 	flush_tlb_kernel_range(start, end);
771 }
772 EXPORT_SYMBOL(flush_kernel_vmap_range);
773 
774 void invalidate_kernel_vmap_range(void *vaddr, int size)
775 {
776 	unsigned long start = (unsigned long)vaddr;
777 	unsigned long end = start + size;
778 
779 	/* Ensure DMA is complete */
780 	asm_syncdma();
781 
782 	if ((!IS_ENABLED(CONFIG_SMP) || !arch_irqs_disabled()) &&
783 	    (unsigned long)size >= parisc_cache_flush_threshold) {
784 		flush_tlb_kernel_range(start, end);
785 		flush_data_cache();
786 		return;
787 	}
788 
789 	purge_kernel_dcache_range_asm(start, end);
790 	flush_tlb_kernel_range(start, end);
791 }
792 EXPORT_SYMBOL(invalidate_kernel_vmap_range);
793