summaryrefslogtreecommitdiff
path: root/bsp/synopsys/embarc/inc/arc/arc_cache.h
blob: dfbc7df9a5952d73d8460e4a616d934dad7a1a84 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* ------------------------------------------
 * Copyright (c) 2016, Synopsys, Inc. All rights reserved.

 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:

 * 1) Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.

 * 2) Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.

 * 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * \version 2016.05
 * \date 2014-07-15
 * \author Wayne Ren([email protected])
--------------------------------------------- */

/**
 * \file
 * \ingroup	ARC_HAL_MISC_CACHE
 * \brief header file of cache module
 */

#ifndef _ARC_HAL_CACHE_H_
#define _ARC_HAL_CACHE_H_

#include "inc/embARC_toolchain.h"
#include "inc/arc/arc.h"
#include "inc/arc/arc_builtin.h"
#include "inc/arc/arc_exception.h"

/**
 * \name instruction cache control register related definition
 * \todo this definitions will be reviewed.
 * @{
 */
#define IC_CTRL_IC_ENABLE			0x0	/*!< enable instruction cache */
#define IC_CTRL_IC_DISABLE			0x1	/*!< disable instruction cache */
#define IC_CTRL_DIRECT_ACCESS			0x0	/*!< direct access mode */
#define IC_CTRL_INDIRECT_ACCESS			0x20	/*!< indirect access mode */
#define IC_CTRL_OP_SUCCEEDED			0x8	/*!< instruction cache operation succeeded */
/** @} */

/**
 * \name data cache control register related definition
 * \todo this definition will be reviewed.
 * @{
 */
#define IC_CTRL_I
#define DC_CTRL_DC_ENABLE			0x0	/*!< enable data cache */
#define DC_CTRL_DC_DISABLE			0x1	/*!< disable data cache */
#define DC_CTRL_INVALID_ONLY			0x0	/*!< invalid data cache only */
#define DC_CTRL_INVALID_FLUSH			0x40	/*!< invalid and flush data cache */
#define DC_CTRL_ENABLE_FLUSH_LOCKED		0x80	/*!< the locked data cache can be flushed */
#define DC_CTRL_DISABLE_FLUSH_LOCKED		0x0	/*!< the locked data cache cannot be flushed */
#define DC_CTRL_FLUSH_STATUS			0x100	/*!< flush status */
#define DC_CTRL_DIRECT_ACCESS			0x0	/*!< direct access mode  */
#define DC_CTRL_INDIRECT_ACCESS			0x20	/*!< indirect access mode */
#define DC_CTRL_OP_SUCCEEDED			0x4	/*!< data cache operation succeeded */
/** @} */

/**
 * \name instruction cache related inline function
 * @{
 */

/**
 * \brief  check whether instruction cache is available,
 * 0 for not available, >0 for available
 */
Inline uint8_t icache_available(void)
{
	return (_arc_aux_read(AUX_BCR_I_CACHE) & 0xF);
}

/**
 * \brief  enable instruction cache
 * \param[in] icache_en_mask operation mask
 */
Inline void icache_enable(uint32_t icache_en_mask)
{
	if (!icache_available()) return;
	_arc_aux_write(AUX_IC_CTRL, icache_en_mask);
}

/**
 * \brief  disable instruction cache
 */
Inline void icache_disable(void)
{
	if (!icache_available()) return;
	_arc_aux_write(AUX_IC_CTRL, IC_CTRL_IC_DISABLE);
}

/**
 * \brief  invalidate the entire instruction cache
 */
Inline void icache_invalidate(void)
{
	if (!icache_available()) return;
	/* invalidate the entire icache */
	_arc_aux_write(AUX_IC_IVIC, 0);
	Asm("nop_s");
	Asm("nop_s");
	Asm("nop_s");
}

/**
 * \brief  invalidate specific cache line
 * \param[in] address memory address
 */
Inline void icache_invalidate_line(uint32_t address)
{
	if (!icache_available()) return;
	_arc_aux_write(AUX_IC_IVIL, address);
	/* the 3 nops are required by ARCv2 ISA */
	Asm("nop_s");
	Asm("nop_s");
	Asm("nop_s");
}

/**
 * \brief  lock specific cache line
 * \param[in] address memory address
 * \return 0, succeeded, -1, failed
 */
Inline int32_t icache_lock_line(uint32_t address)
{
	if (!icache_available()) return -1;
	_arc_aux_write(AUX_IC_LIL, address);
	if(_arc_aux_read(AUX_IC_CTRL) & IC_CTRL_OP_SUCCEEDED) {
		return 0;
	} else {
		return -1;
	}
}

/**
 * \brief  set icache access mode
 * \param[in] mode, access mode, 1: indirect access 0:direct access
 */
Inline void icache_access_mode(uint32_t mode)
{
	if (!icache_available()) return;
	if (mode) {
		_arc_aux_write(AUX_IC_CTRL, _arc_aux_read(AUX_IC_CTRL) | IC_CTRL_INDIRECT_ACCESS);
	} else {
		_arc_aux_write(AUX_IC_CTRL, _arc_aux_read(AUX_IC_CTRL) & (~IC_CTRL_INDIRECT_ACCESS));
	}
}


/** @} */

/**
 * \name data cache related inline functions
 * @{
 */

/**
 * \brief  check whether data cache is available,
 * 0 for not available, >0 for available
 */
Inline uint8_t dcache_available(void)
{
	return (_arc_aux_read(AUX_BCR_D_CACHE) & 0xF);
}

/**
 * \brief 	invalidate the entire data cache
 */
Inline void dcache_invalidate(void)
{
	if (!dcache_available()) return;
	uint32_t status;

	status = cpu_lock_save();
	_arc_aux_write(AUX_DC_IVDC, 1);
	/* wait for flush completion */
	while (_arc_aux_read(AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
	cpu_unlock_restore(status);
}

/**
 * \brief invalidate the specific cache line
 * \param[in] address memory address
 */
Inline void dcache_invalidate_line(uint32_t address)
{
	if (!dcache_available()) return;
	_arc_aux_write(AUX_DC_IVDL, address);
	Asm("nop_s");
	Asm("nop_s");
	Asm("nop_s");
}

/**
 * \brief enable data cache
 * \param[in] dcache_en_mask operation mask
 */
Inline void dcache_enable(uint32_t dcache_en_mask)
{
	if (!dcache_available()) return;
	_arc_aux_write(AUX_DC_CTRL, dcache_en_mask);
}

/**
 * \brief disable data cache
 */
Inline void dcache_disable(void)
{
	if (!dcache_available()) return;
	_arc_aux_write(AUX_DC_CTRL, DC_CTRL_DC_DISABLE);
}

/**
 * \brief flush data cache
 */
Inline void dcache_flush(void)
{
	if (!dcache_available()) return;
	uint32_t status;

	status = cpu_lock_save();
	_arc_aux_write(AUX_DC_FLSH, 1);
	/* wait for flush completion */
	while (_arc_aux_read(AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
	cpu_unlock_restore(status);
}

/**
 * \brief  flush the specific data cache line
 * \param[in] address memory address
 */
Inline void dcache_flush_line(uint32_t address)
{
	if (!dcache_available()) return;

	uint32_t status;

	status = cpu_lock_save();
	_arc_aux_write(AUX_DC_FLDL, address);
	while (_arc_aux_read(AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
	cpu_unlock_restore(status);
}

/**
 * \brief  lock the specific data cache line
 * \param[in] address memory address
 * \return 0, succeeded, -1, failed
 */
Inline int dcache_lock_line(uint32_t address)
{
	if (!dcache_available()) return -1;
	_arc_aux_write(AUX_DC_LDL, address);
	if(_arc_aux_read(AUX_DC_CTRL) & DC_CTRL_OP_SUCCEEDED) {
		return 0;
	} else {
		return -1;
	}
}

/**
 * \brief  set dcache access mode
 * \param[in] mode, access mode, 1: indirect access 0:direct access
 */
Inline void dcache_access_mode(uint32_t mode)
{
	if (!dcache_available()) return;
	if (mode) {
		_arc_aux_write(AUX_DC_CTRL, _arc_aux_read(AUX_DC_CTRL) | DC_CTRL_INDIRECT_ACCESS);
	} else {
		_arc_aux_write(AUX_DC_CTRL, _arc_aux_read(AUX_DC_CTRL) & (~DC_CTRL_INDIRECT_ACCESS));
	}
}

/** @} */

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \name declarations of cache related functions
 * @{
 */
extern int32_t icache_invalidate_mlines(uint32_t start_addr, uint32_t size);
extern int32_t icache_lock_mlines(uint32_t start_addr, uint32_t size);
extern int32_t icache_direct_write(uint32_t cache_addr, uint32_t tag, uint32_t data);
extern int32_t icache_direct_read(uint32_t cache_addr, uint32_t *tag, uint32_t *data);
extern int32_t icache_indirect_read(uint32_t mem_addr, uint32_t *tag, uint32_t *data);
extern int32_t dcache_invalidate_mlines(uint32_t start_addr, uint32_t size);
extern int32_t dcache_flush_mlines(uint32_t start_addr, uint32_t size);
extern int32_t dcache_lock_mlines(uint32_t start_addr, uint32_t size);
extern int32_t dcache_direct_write(uint32_t cache_addr, uint32_t tag, uint32_t data);
extern int32_t dcache_direct_read(uint32_t cache_addr, uint32_t *tag, uint32_t *data);
extern int32_t dcache_indirect_read(uint32_t mem_addr, uint32_t *tag, uint32_t *data);
extern void arc_cache_init(void);

#ifdef __cplusplus
}
#endif

/** @} */
#endif	/* _ARC_HAL_CACHE_H_ */