1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _METAG_L2CACHE_H
3 #define _METAG_L2CACHE_H
5 #ifdef CONFIG_METAG_L2C
7 #include <asm/global_lock.h>
11 * Store the last known value of pfenable (we don't want prefetch enabled while
14 extern int l2c_pfenable;
16 /* defined in arch/metag/drivers/core-sysfs.c */
17 extern struct sysdev_class cache_sysclass;
19 static inline void wr_fence(void);
22 * Functions for reading of L2 cache configuration.
25 /* Get raw L2 config register (CORE_CONFIG3) */
26 static inline unsigned int meta_l2c_config(void)
28 const unsigned int *corecfg3 = (const unsigned int *)METAC_CORE_CONFIG3;
32 /* Get whether the L2 is present */
33 static inline int meta_l2c_is_present(void)
35 return meta_l2c_config() & METAC_CORECFG3_L2C_HAVE_L2C_BIT;
38 /* Get whether the L2 is configured for write-back instead of write-through */
39 static inline int meta_l2c_is_writeback(void)
41 return meta_l2c_config() & METAC_CORECFG3_L2C_MODE_BIT;
44 /* Get whether the L2 is unified instead of separated code/data */
45 static inline int meta_l2c_is_unified(void)
47 return meta_l2c_config() & METAC_CORECFG3_L2C_UNIFIED_BIT;
50 /* Get the L2 cache size in bytes */
51 static inline unsigned int meta_l2c_size(void)
54 if (!meta_l2c_is_present())
56 size_s = (meta_l2c_config() & METAC_CORECFG3_L2C_SIZE_BITS)
57 >> METAC_CORECFG3_L2C_SIZE_S;
58 /* L2CSIZE is in KiB */
59 return 1024 << size_s;
62 /* Get the number of ways in the L2 cache */
63 static inline unsigned int meta_l2c_ways(void)
66 if (!meta_l2c_is_present())
68 ways_s = (meta_l2c_config() & METAC_CORECFG3_L2C_NUM_WAYS_BITS)
69 >> METAC_CORECFG3_L2C_NUM_WAYS_S;
73 /* Get the line size of the L2 cache */
74 static inline unsigned int meta_l2c_linesize(void)
76 unsigned int line_size;
77 if (!meta_l2c_is_present())
79 line_size = (meta_l2c_config() & METAC_CORECFG3_L2C_LINE_SIZE_BITS)
80 >> METAC_CORECFG3_L2C_LINE_SIZE_S;
82 case METAC_CORECFG3_L2C_LINE_SIZE_64B:
89 /* Get the revision ID of the L2 cache */
90 static inline unsigned int meta_l2c_revision(void)
92 return (meta_l2c_config() & METAC_CORECFG3_L2C_REV_ID_BITS)
93 >> METAC_CORECFG3_L2C_REV_ID_S;
98 * Start an initialisation of the L2 cachelines and wait for completion.
99 * This should only be done in a LOCK1 or LOCK2 critical section while the L2
102 static inline void _meta_l2c_init(void)
104 metag_out32(SYSC_L2C_INIT_INIT, SYSC_L2C_INIT);
105 while (metag_in32(SYSC_L2C_INIT) == SYSC_L2C_INIT_IN_PROGRESS)
110 * Start a writeback of dirty L2 cachelines and wait for completion.
111 * This should only be done in a LOCK1 or LOCK2 critical section.
113 static inline void _meta_l2c_purge(void)
115 metag_out32(SYSC_L2C_PURGE_PURGE, SYSC_L2C_PURGE);
116 while (metag_in32(SYSC_L2C_PURGE) == SYSC_L2C_PURGE_IN_PROGRESS)
120 /* Set whether the L2 cache is enabled. */
121 static inline void _meta_l2c_enable(int enabled)
125 enable = metag_in32(SYSC_L2C_ENABLE);
127 enable |= SYSC_L2C_ENABLE_ENABLE_BIT;
129 enable &= ~SYSC_L2C_ENABLE_ENABLE_BIT;
130 metag_out32(enable, SYSC_L2C_ENABLE);
133 /* Set whether the L2 cache prefetch is enabled. */
134 static inline void _meta_l2c_pf_enable(int pfenabled)
138 enable = metag_in32(SYSC_L2C_ENABLE);
140 enable |= SYSC_L2C_ENABLE_PFENABLE_BIT;
142 enable &= ~SYSC_L2C_ENABLE_PFENABLE_BIT;
143 metag_out32(enable, SYSC_L2C_ENABLE);
146 /* Return whether the L2 cache is enabled */
147 static inline int _meta_l2c_is_enabled(void)
149 return metag_in32(SYSC_L2C_ENABLE) & SYSC_L2C_ENABLE_ENABLE_BIT;
152 /* Return whether the L2 cache prefetch is enabled */
153 static inline int _meta_l2c_pf_is_enabled(void)
155 return metag_in32(SYSC_L2C_ENABLE) & SYSC_L2C_ENABLE_PFENABLE_BIT;
159 /* Return whether the L2 cache is enabled */
160 static inline int meta_l2c_is_enabled(void)
165 * There is no need to lock at the moment, as the enable bit is never
166 * intermediately changed, so we will never see an intermediate result.
168 en = _meta_l2c_is_enabled();
174 * Ensure the L2 cache is disabled.
175 * Return whether the L2 was previously disabled.
177 int meta_l2c_disable(void);
180 * Ensure the L2 cache is enabled.
181 * Return whether the L2 was previously enabled.
183 int meta_l2c_enable(void);
185 /* Return whether the L2 cache prefetch is enabled */
186 static inline int meta_l2c_pf_is_enabled(void)
192 * Set whether the L2 cache prefetch is enabled.
193 * Return whether the L2 prefetch was previously enabled.
195 int meta_l2c_pf_enable(int pfenable);
198 * Flush the L2 cache.
199 * Return 1 if the L2 is disabled.
201 int meta_l2c_flush(void);
204 * Write back all dirty cache lines in the L2 cache.
205 * Return 1 if the L2 is disabled or there isn't any writeback.
207 static inline int meta_l2c_writeback(void)
212 /* no need to purge if it's not a writeback cache */
213 if (!meta_l2c_is_writeback())
217 * Purge only works if the L2 is enabled, and involves reading back to
218 * detect completion, so keep this operation atomic with other threads.
220 __global_lock1(flags);
221 en = meta_l2c_is_enabled();
226 __global_unlock1(flags);
231 #else /* CONFIG_METAG_L2C */
233 #define meta_l2c_config() 0
234 #define meta_l2c_is_present() 0
235 #define meta_l2c_is_writeback() 0
236 #define meta_l2c_is_unified() 0
237 #define meta_l2c_size() 0
238 #define meta_l2c_ways() 0
239 #define meta_l2c_linesize() 0
240 #define meta_l2c_revision() 0
242 #define meta_l2c_is_enabled() 0
243 #define _meta_l2c_pf_is_enabled() 0
244 #define meta_l2c_pf_is_enabled() 0
245 #define meta_l2c_disable() 1
246 #define meta_l2c_enable() 0
247 #define meta_l2c_pf_enable(X) 0
248 static inline int meta_l2c_flush(void)
252 static inline int meta_l2c_writeback(void)
257 #endif /* CONFIG_METAG_L2C */
259 #endif /* _METAG_L2CACHE_H */