xref: /kernel/linux/linux-5.10/arch/csky/mm/cachev2.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
58c2ecf20Sopenharmony_ci#include <linux/smp.h>
68c2ecf20Sopenharmony_ci#include <linux/mm.h>
78c2ecf20Sopenharmony_ci#include <asm/cache.h>
88c2ecf20Sopenharmony_ci#include <asm/barrier.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/* for L1-cache */
118c2ecf20Sopenharmony_ci#define INS_CACHE		(1 << 0)
128c2ecf20Sopenharmony_ci#define DATA_CACHE		(1 << 1)
138c2ecf20Sopenharmony_ci#define CACHE_INV		(1 << 4)
148c2ecf20Sopenharmony_ci#define CACHE_CLR		(1 << 5)
158c2ecf20Sopenharmony_ci#define CACHE_OMS		(1 << 6)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_civoid local_icache_inv_all(void *priv)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	mtcr("cr17", INS_CACHE|CACHE_INV);
208c2ecf20Sopenharmony_ci	sync_is();
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_HAS_ICACHE_INS
248c2ecf20Sopenharmony_civoid icache_inv_range(unsigned long start, unsigned long end)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	unsigned long i = start & ~(L1_CACHE_BYTES - 1);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	for (; i < end; i += L1_CACHE_BYTES)
298c2ecf20Sopenharmony_ci		asm volatile("icache.iva %0\n"::"r"(i):"memory");
308c2ecf20Sopenharmony_ci	sync_is();
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci#else
338c2ecf20Sopenharmony_cistruct cache_range {
348c2ecf20Sopenharmony_ci	unsigned long start;
358c2ecf20Sopenharmony_ci	unsigned long end;
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(cache_lock);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic inline void cache_op_line(unsigned long i, unsigned int val)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	mtcr("cr22", i);
438c2ecf20Sopenharmony_ci	mtcr("cr17", val);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_civoid local_icache_inv_range(void *priv)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct cache_range *param = priv;
498c2ecf20Sopenharmony_ci	unsigned long i = param->start & ~(L1_CACHE_BYTES - 1);
508c2ecf20Sopenharmony_ci	unsigned long flags;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	spin_lock_irqsave(&cache_lock, flags);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	for (; i < param->end; i += L1_CACHE_BYTES)
558c2ecf20Sopenharmony_ci		cache_op_line(i, INS_CACHE | CACHE_INV | CACHE_OMS);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&cache_lock, flags);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	sync_is();
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_civoid icache_inv_range(unsigned long start, unsigned long end)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct cache_range param = { start, end };
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (irqs_disabled())
678c2ecf20Sopenharmony_ci		local_icache_inv_range(&param);
688c2ecf20Sopenharmony_ci	else
698c2ecf20Sopenharmony_ci		on_each_cpu(local_icache_inv_range, &param, 1);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci#endif
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciinline void dcache_wb_line(unsigned long start)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	asm volatile("dcache.cval1 %0\n"::"r"(start):"memory");
768c2ecf20Sopenharmony_ci	sync_is();
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_civoid dcache_wb_range(unsigned long start, unsigned long end)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	unsigned long i = start & ~(L1_CACHE_BYTES - 1);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	for (; i < end; i += L1_CACHE_BYTES)
848c2ecf20Sopenharmony_ci		asm volatile("dcache.cval1 %0\n"::"r"(i):"memory");
858c2ecf20Sopenharmony_ci	sync_is();
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_civoid cache_wbinv_range(unsigned long start, unsigned long end)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	dcache_wb_range(start, end);
918c2ecf20Sopenharmony_ci	icache_inv_range(start, end);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cache_wbinv_range);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_civoid dma_wbinv_range(unsigned long start, unsigned long end)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	unsigned long i = start & ~(L1_CACHE_BYTES - 1);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	for (; i < end; i += L1_CACHE_BYTES)
1008c2ecf20Sopenharmony_ci		asm volatile("dcache.civa %0\n"::"r"(i):"memory");
1018c2ecf20Sopenharmony_ci	sync_is();
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_civoid dma_inv_range(unsigned long start, unsigned long end)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	unsigned long i = start & ~(L1_CACHE_BYTES - 1);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	for (; i < end; i += L1_CACHE_BYTES)
1098c2ecf20Sopenharmony_ci		asm volatile("dcache.iva %0\n"::"r"(i):"memory");
1108c2ecf20Sopenharmony_ci	sync_is();
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_civoid dma_wb_range(unsigned long start, unsigned long end)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	unsigned long i = start & ~(L1_CACHE_BYTES - 1);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	for (; i < end; i += L1_CACHE_BYTES)
1188c2ecf20Sopenharmony_ci		asm volatile("dcache.cva %0\n"::"r"(i):"memory");
1198c2ecf20Sopenharmony_ci	sync_is();
1208c2ecf20Sopenharmony_ci}
121