xref: /linux/arch/sh/mm/cache-sh3.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1*c456cfc2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * arch/sh/mm/cache-sh3.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1999, 2000  Niibe Yutaka
61da177e4SLinus Torvalds  * Copyright (C) 2002 Paul Mundt
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/threads.h>
131da177e4SLinus Torvalds #include <asm/addrspace.h>
141da177e4SLinus Torvalds #include <asm/page.h>
151da177e4SLinus Torvalds #include <asm/processor.h>
161da177e4SLinus Torvalds #include <asm/cache.h>
171da177e4SLinus Torvalds #include <asm/io.h>
187c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
191da177e4SLinus Torvalds #include <asm/mmu_context.h>
201da177e4SLinus Torvalds #include <asm/cacheflush.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * Write back the dirty D-caches, but not invalidate them.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Is this really worth it, or should we just alias this routine
261da177e4SLinus Torvalds  * to __flush_purge_region too?
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * START: Virtual Address (U0, P1, or P3)
291da177e4SLinus Torvalds  * SIZE: Size of the region.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
sh3__flush_wback_region(void * start,int size)3279f1c9daSPaul Mundt static void sh3__flush_wback_region(void *start, int size)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds 	unsigned long v, j;
351da177e4SLinus Torvalds 	unsigned long begin, end;
361da177e4SLinus Torvalds 	unsigned long flags;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
391da177e4SLinus Torvalds 	end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
401da177e4SLinus Torvalds 		& ~(L1_CACHE_BYTES-1);
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	for (v = begin; v < end; v+=L1_CACHE_BYTES) {
431da177e4SLinus Torvalds 		unsigned long addrstart = CACHE_OC_ADDRESS_ARRAY;
4411c19656SPaul Mundt 		for (j = 0; j < current_cpu_data.dcache.ways; j++) {
451da177e4SLinus Torvalds 			unsigned long data, addr, p;
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds 			p = __pa(v);
4811c19656SPaul Mundt 			addr = addrstart | (v & current_cpu_data.dcache.entry_mask);
491da177e4SLinus Torvalds 			local_irq_save(flags);
509d56dd3bSPaul Mundt 			data = __raw_readl(addr);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 			if ((data & CACHE_PHYSADDR_MASK) ==
531da177e4SLinus Torvalds 			    (p & CACHE_PHYSADDR_MASK)) {
541da177e4SLinus Torvalds 				data &= ~SH_CACHE_UPDATED;
559d56dd3bSPaul Mundt 				__raw_writel(data, addr);
561da177e4SLinus Torvalds 				local_irq_restore(flags);
571da177e4SLinus Torvalds 				break;
581da177e4SLinus Torvalds 			}
591da177e4SLinus Torvalds 			local_irq_restore(flags);
6011c19656SPaul Mundt 			addrstart += current_cpu_data.dcache.way_incr;
611da177e4SLinus Torvalds 		}
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * Write back the dirty D-caches and invalidate them.
671da177e4SLinus Torvalds  *
681da177e4SLinus Torvalds  * START: Virtual Address (U0, P1, or P3)
691da177e4SLinus Torvalds  * SIZE: Size of the region.
701da177e4SLinus Torvalds  */
sh3__flush_purge_region(void * start,int size)7179f1c9daSPaul Mundt static void sh3__flush_purge_region(void *start, int size)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	unsigned long v;
741da177e4SLinus Torvalds 	unsigned long begin, end;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
771da177e4SLinus Torvalds 	end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
781da177e4SLinus Torvalds 		& ~(L1_CACHE_BYTES-1);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	for (v = begin; v < end; v+=L1_CACHE_BYTES) {
811da177e4SLinus Torvalds 		unsigned long data, addr;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 		data = (v & 0xfffffc00); /* _Virtual_ address, ~U, ~V */
841da177e4SLinus Torvalds 		addr = CACHE_OC_ADDRESS_ARRAY |
8511c19656SPaul Mundt 			(v & current_cpu_data.dcache.entry_mask) | SH_CACHE_ASSOC;
869d56dd3bSPaul Mundt 		__raw_writel(data, addr);
871da177e4SLinus Torvalds 	}
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
sh3_cache_init(void)9079f1c9daSPaul Mundt void __init sh3_cache_init(void)
9179f1c9daSPaul Mundt {
9279f1c9daSPaul Mundt 	__flush_wback_region = sh3__flush_wback_region;
9379f1c9daSPaul Mundt 	__flush_purge_region = sh3__flush_purge_region;
9479f1c9daSPaul Mundt 
951da177e4SLinus Torvalds 	/*
961da177e4SLinus Torvalds 	 * No write back please
971da177e4SLinus Torvalds 	 *
9879f1c9daSPaul Mundt 	 * Except I don't think there's any way to avoid the writeback.
9979f1c9daSPaul Mundt 	 * So we just alias it to sh3__flush_purge_region(). dwmw2.
1001da177e4SLinus Torvalds 	 */
10179f1c9daSPaul Mundt 	__flush_invalidate_region = sh3__flush_purge_region;
10279f1c9daSPaul Mundt }
103