blob: 7a9664adce4360b36a87a13b637d787fcec97e38 [file] [log] [blame]
Guo Ren00a97302018-09-05 14:25:10 +08001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#include <linux/spinlock.h>
5#include <linux/smp.h>
Guo Ren761b4f62020-01-22 11:15:14 +08006#include <linux/mm.h>
Guo Ren00a97302018-09-05 14:25:10 +08007#include <asm/cache.h>
8#include <asm/barrier.h>
9
Guo Rendd7c9832020-03-31 22:15:42 +080010/* for L1-cache */
Guo Ren761b4f62020-01-22 11:15:14 +080011#define INS_CACHE (1 << 0)
Guo Rendd7c9832020-03-31 22:15:42 +080012#define DATA_CACHE (1 << 1)
Guo Ren761b4f62020-01-22 11:15:14 +080013#define CACHE_INV (1 << 4)
Guo Rendd7c9832020-03-31 22:15:42 +080014#define CACHE_CLR (1 << 5)
15#define CACHE_OMS (1 << 6)
Guo Ren761b4f62020-01-22 11:15:14 +080016
17void local_icache_inv_all(void *priv)
Guo Ren00a97302018-09-05 14:25:10 +080018{
Guo Ren761b4f62020-01-22 11:15:14 +080019 mtcr("cr17", INS_CACHE|CACHE_INV);
Guo Ren00a97302018-09-05 14:25:10 +080020 sync_is();
21}
22
Guo Ren761b4f62020-01-22 11:15:14 +080023#ifdef CONFIG_CPU_HAS_ICACHE_INS
Guo Ren00a97302018-09-05 14:25:10 +080024void icache_inv_range(unsigned long start, unsigned long end)
25{
26 unsigned long i = start & ~(L1_CACHE_BYTES - 1);
27
28 for (; i < end; i += L1_CACHE_BYTES)
29 asm volatile("icache.iva %0\n"::"r"(i):"memory");
30 sync_is();
31}
Guo Ren761b4f62020-01-22 11:15:14 +080032#else
Guo Rendd7c9832020-03-31 22:15:42 +080033struct cache_range {
34 unsigned long start;
35 unsigned long end;
36};
37
38static DEFINE_SPINLOCK(cache_lock);
39
40static inline void cache_op_line(unsigned long i, unsigned int val)
41{
42 mtcr("cr22", i);
43 mtcr("cr17", val);
44}
45
46void local_icache_inv_range(void *priv)
47{
48 struct cache_range *param = priv;
49 unsigned long i = param->start & ~(L1_CACHE_BYTES - 1);
50 unsigned long flags;
51
52 spin_lock_irqsave(&cache_lock, flags);
53
54 for (; i < param->end; i += L1_CACHE_BYTES)
55 cache_op_line(i, INS_CACHE | CACHE_INV | CACHE_OMS);
56
57 spin_unlock_irqrestore(&cache_lock, flags);
58
59 sync_is();
60}
61
Guo Ren761b4f62020-01-22 11:15:14 +080062void icache_inv_range(unsigned long start, unsigned long end)
Guo Ren00a97302018-09-05 14:25:10 +080063{
Guo Rendd7c9832020-03-31 22:15:42 +080064 struct cache_range param = { start, end };
65
66 if (irqs_disabled())
67 local_icache_inv_range(&param);
68 else
69 on_each_cpu(local_icache_inv_range, &param, 1);
Guo Ren761b4f62020-01-22 11:15:14 +080070}
71#endif
72
73inline void dcache_wb_line(unsigned long start)
74{
75 asm volatile("dcache.cval1 %0\n"::"r"(start):"memory");
Guo Ren00a97302018-09-05 14:25:10 +080076 sync_is();
77}
78
79void dcache_wb_range(unsigned long start, unsigned long end)
80{
81 unsigned long i = start & ~(L1_CACHE_BYTES - 1);
82
83 for (; i < end; i += L1_CACHE_BYTES)
84 asm volatile("dcache.cval1 %0\n"::"r"(i):"memory");
85 sync_is();
86}
87
Guo Ren00a97302018-09-05 14:25:10 +080088void cache_wbinv_range(unsigned long start, unsigned long end)
89{
Guo Ren9025fd42020-02-02 10:58:38 +080090 dcache_wb_range(start, end);
Guo Ren761b4f62020-01-22 11:15:14 +080091 icache_inv_range(start, end);
Guo Ren00a97302018-09-05 14:25:10 +080092}
93EXPORT_SYMBOL(cache_wbinv_range);
94
95void dma_wbinv_range(unsigned long start, unsigned long end)
96{
97 unsigned long i = start & ~(L1_CACHE_BYTES - 1);
98
99 for (; i < end; i += L1_CACHE_BYTES)
100 asm volatile("dcache.civa %0\n"::"r"(i):"memory");
101 sync_is();
102}
103
Guo Renae76f632019-07-30 17:16:28 +0800104void dma_inv_range(unsigned long start, unsigned long end)
105{
106 unsigned long i = start & ~(L1_CACHE_BYTES - 1);
107
108 for (; i < end; i += L1_CACHE_BYTES)
109 asm volatile("dcache.iva %0\n"::"r"(i):"memory");
110 sync_is();
111}
112
Guo Ren00a97302018-09-05 14:25:10 +0800113void dma_wb_range(unsigned long start, unsigned long end)
114{
115 unsigned long i = start & ~(L1_CACHE_BYTES - 1);
116
117 for (; i < end; i += L1_CACHE_BYTES)
Guo Renae76f632019-07-30 17:16:28 +0800118 asm volatile("dcache.cva %0\n"::"r"(i):"memory");
Guo Ren00a97302018-09-05 14:25:10 +0800119 sync_is();
120}