aboutsummaryrefslogtreecommitdiff
path: root/arch/metag/include/asm/l2cache.h
blob: bffbeaa4d93bfac4cf5abc42c9ac3d4e4d840e11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#ifndef _METAG_L2CACHE_H
#define _METAG_L2CACHE_H

#ifdef CONFIG_METAG_L2C

#include <asm/global_lock.h>
#include <asm/io.h>

/*
 * Store the last known value of pfenable (we don't want prefetch enabled while
 * L2 is off).
 */
extern int l2c_pfenable;

/* defined in arch/metag/drivers/core-sysfs.c */
extern struct sysdev_class cache_sysclass;

static inline void wr_fence(void);

/*
 * Functions for reading of L2 cache configuration.
 */

/* Get raw L2 config register (CORE_CONFIG3) */
static inline unsigned int meta_l2c_config(void)
{
	const unsigned int *corecfg3 = (const unsigned int *)METAC_CORE_CONFIG3;
	return *corecfg3;
}

/* Get whether the L2 is present */
static inline int meta_l2c_is_present(void)
{
	return meta_l2c_config() & METAC_CORECFG3_L2C_HAVE_L2C_BIT;
}

/* Get whether the L2 is configured for write-back instead of write-through */
static inline int meta_l2c_is_writeback(void)
{
	return meta_l2c_config() & METAC_CORECFG3_L2C_MODE_BIT;
}

/* Get whether the L2 is unified instead of separated code/data */
static inline int meta_l2c_is_unified(void)
{
	return meta_l2c_config() & METAC_CORECFG3_L2C_UNIFIED_BIT;
}

/* Get the L2 cache size in bytes */
static inline unsigned int meta_l2c_size(void)
{
	unsigned int size_s;
	if (!meta_l2c_is_present())
		return 0;
	size_s = (meta_l2c_config() & METAC_CORECFG3_L2C_SIZE_BITS)
			>> METAC_CORECFG3_L2C_SIZE_S;
	/* L2CSIZE is in KiB */
	return 1024 << size_s;
}

/* Get the number of ways in the L2 cache */
static inline unsigned int meta_l2c_ways(void)
{
	unsigned int ways_s;
	if (!meta_l2c_is_present())
		return 0;
	ways_s = (meta_l2c_config() & METAC_CORECFG3_L2C_NUM_WAYS_BITS)
			>> METAC_CORECFG3_L2C_NUM_WAYS_S;
	return 0x1 << ways_s;
}

/* Get the line size of the L2 cache */
static inline unsigned int meta_l2c_linesize(void)
{
	unsigned int line_size;
	if (!meta_l2c_is_present())
		return 0;
	line_size = (meta_l2c_config() & METAC_CORECFG3_L2C_LINE_SIZE_BITS)
			>> METAC_CORECFG3_L2C_LINE_SIZE_S;
	switch (line_size) {
	case METAC_CORECFG3_L2C_LINE_SIZE_64B:
		return 64;
	default:
		return 0;
	}
}

/* Get the revision ID of the L2 cache */
static inline unsigned int meta_l2c_revision(void)
{
	return (meta_l2c_config() & METAC_CORECFG3_L2C_REV_ID_BITS)
			>> METAC_CORECFG3_L2C_REV_ID_S;
}


/*
 * Start an initialisation of the L2 cachelines and wait for completion.
 * This should only be done in a LOCK1 or LOCK2 critical section while the L2
 * is disabled.
 */
static inline void _meta_l2c_init(void)
{
	metag_out32(SYSC_L2C_INIT_INIT, SYSC_L2C_INIT);
	while (metag_in32(SYSC_L2C_INIT) == SYSC_L2C_INIT_IN_PROGRESS)
		/* do nothing */;
}

/*
 * Start a writeback of dirty L2 cachelines and wait for completion.
 * This should only be done in a LOCK1 or LOCK2 critical section.
 */
static inline void _meta_l2c_purge(void)
{
	metag_out32(SYSC_L2C_PURGE_PURGE, SYSC_L2C_PURGE);
	while (metag_in32(SYSC_L2C_PURGE) == SYSC_L2C_PURGE_IN_PROGRESS)
		/* do nothing */;
}

/* Set whether the L2 cache is enabled. */
static inline void _meta_l2c_enable(int enabled)
{
	unsigned int enable;

	enable = metag_in32(SYSC_L2C_ENABLE);
	if (enabled)
		enable |= SYSC_L2C_ENABLE_ENABLE_BIT;
	else
		enable &= ~SYSC_L2C_ENABLE_ENABLE_BIT;
	metag_out32(enable, SYSC_L2C_ENABLE);
}

/* Set whether the L2 cache prefetch is enabled. */
static inline void _meta_l2c_pf_enable(int pfenabled)
{
	unsigned int enable;

	enable = metag_in32(SYSC_L2C_ENABLE);
	if (pfenabled)
		enable |= SYSC_L2C_ENABLE_PFENABLE_BIT;
	else
		enable &= ~SYSC_L2C_ENABLE_PFENABLE_BIT;
	metag_out32(enable, SYSC_L2C_ENABLE);
}

/* Return whether the L2 cache is enabled */
static inline int _meta_l2c_is_enabled(void)
{
	return metag_in32(SYSC_L2C_ENABLE) & SYSC_L2C_ENABLE_ENABLE_BIT;
}

/* Return whether the L2 cache prefetch is enabled */
static inline int _meta_l2c_pf_is_enabled(void)
{
	return metag_in32(SYSC_L2C_ENABLE) & SYSC_L2C_ENABLE_PFENABLE_BIT;
}


/* Return whether the L2 cache is enabled */
static inline int meta_l2c_is_enabled(void)
{
	int en;

	/*
	 * There is no need to lock at the moment, as the enable bit is never
	 * intermediately changed, so we will never see an intermediate result.
	 */
	en = _meta_l2c_is_enabled();

	return en;
}

/*
 * Ensure the L2 cache is disabled.
 * Return whether the L2 was previously disabled.
 */
int meta_l2c_disable(void);

/*
 * Ensure the L2 cache is enabled.
 * Return whether the L2 was previously enabled.
 */
int meta_l2c_enable(void);

/* Return whether the L2 cache prefetch is enabled */
static inline int meta_l2c_pf_is_enabled(void)
{
	return l2c_pfenable;
}

/*
 * Set whether the L2 cache prefetch is enabled.
 * Return whether the L2 prefetch was previously enabled.
 */
int meta_l2c_pf_enable(int pfenable);

/*
 * Flush the L2 cache.
 * Return 1 if the L2 is disabled.
 */
int meta_l2c_flush(void);

/*
 * Write back all dirty cache lines in the L2 cache.
 * Return 1 if the L2 is disabled or there isn't any writeback.
 */
static inline int meta_l2c_writeback(void)
{
	unsigned long flags;
	int en;

	/* no need to purge if it's not a writeback cache */
	if (!meta_l2c_is_writeback())
		return 1;

	/*
	 * Purge only works if the L2 is enabled, and involves reading back to
	 * detect completion, so keep this operation atomic with other threads.
	 */
	__global_lock1(flags);
	en = meta_l2c_is_enabled();
	if (likely(en)) {
		wr_fence();
		_meta_l2c_purge();
	}
	__global_unlock1(flags);

	return !en;
}

#else /* CONFIG_METAG_L2C */

#define meta_l2c_config()		0
#define meta_l2c_is_present()		0
#define meta_l2c_is_writeback()		0
#define meta_l2c_is_unified()		0
#define meta_l2c_size()			0
#define meta_l2c_ways()			0
#define meta_l2c_linesize()		0
#define meta_l2c_revision()		0

#define meta_l2c_is_enabled()		0
#define _meta_l2c_pf_is_enabled()	0
#define meta_l2c_pf_is_enabled()	0
#define meta_l2c_disable()		1
#define meta_l2c_enable()		0
#define meta_l2c_pf_enable(X)		0
static inline int meta_l2c_flush(void)
{
	return 1;
}
static inline int meta_l2c_writeback(void)
{
	return 1;
}

#endif /* CONFIG_METAG_L2C */

#endif /* _METAG_L2CACHE_H */