mips32, drop unnecessary code in mips32_pracc.c
[openocd.git] / src / target / armv8_cache.c
blob7f610c953536714f3e36eecbce102929bb4f282c
1 /***************************************************************************
2 * Copyright (C) 2016 by Matthias Welwarsky *
3 * matthias.welwarsky@sysgo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include "armv8_cache.h"
24 #include "armv8_dpm.h"
25 #include "armv8_opcodes.h"
27 /* CLIDR cache types */
28 #define CACHE_LEVEL_HAS_UNIFIED_CACHE 0x4
29 #define CACHE_LEVEL_HAS_D_CACHE 0x2
30 #define CACHE_LEVEL_HAS_I_CACHE 0x1
32 static int armv8_d_cache_sanity_check(struct armv8_common *armv8)
34 struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
36 if (armv8_cache->d_u_cache_enabled)
37 return ERROR_OK;
39 return ERROR_TARGET_INVALID;
42 static int armv8_i_cache_sanity_check(struct armv8_common *armv8)
44 struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
46 if (armv8_cache->i_cache_enabled)
47 return ERROR_OK;
49 return ERROR_TARGET_INVALID;
52 static int armv8_cache_d_inner_flush_level(struct armv8_common *armv8, struct armv8_cachesize *size, int cl)
54 struct arm_dpm *dpm = armv8->arm.dpm;
55 int retval = ERROR_OK;
56 int32_t c_way, c_index = size->index;
58 LOG_DEBUG("cl %" PRId32, cl);
59 do {
60 c_way = size->way;
61 do {
62 uint32_t value = (c_index << size->index_shift)
63 | (c_way << size->way_shift) | (cl << 1);
65 * DC CISW - Clean and invalidate data cache
66 * line by Set/Way.
68 retval = dpm->instr_write_data_r0(dpm,
69 armv8_opcode(armv8, ARMV8_OPC_DCCISW), value);
70 if (retval != ERROR_OK)
71 goto done;
72 c_way -= 1;
73 } while (c_way >= 0);
74 c_index -= 1;
75 } while (c_index >= 0);
77 done:
78 return retval;
81 static int armv8_cache_d_inner_clean_inval_all(struct armv8_common *armv8)
83 struct armv8_cache_common *cache = &(armv8->armv8_mmu.armv8_cache);
84 struct arm_dpm *dpm = armv8->arm.dpm;
85 int cl;
86 int retval;
88 retval = armv8_d_cache_sanity_check(armv8);
89 if (retval != ERROR_OK)
90 return retval;
92 retval = dpm->prepare(dpm);
93 if (retval != ERROR_OK)
94 goto done;
96 for (cl = 0; cl < cache->loc; cl++) {
97 /* skip i-only caches */
98 if (cache->arch[cl].ctype < CACHE_LEVEL_HAS_D_CACHE)
99 continue;
101 armv8_cache_d_inner_flush_level(armv8, &cache->arch[cl].d_u_size, cl);
104 retval = dpm->finish(dpm);
105 return retval;
107 done:
108 LOG_ERROR("clean invalidate failed");
109 dpm->finish(dpm);
111 return retval;
114 int armv8_cache_d_inner_flush_virt(struct armv8_common *armv8, target_addr_t va, size_t size)
116 struct arm_dpm *dpm = armv8->arm.dpm;
117 struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
118 uint64_t linelen = armv8_cache->dminline;
119 target_addr_t va_line, va_end;
120 int retval;
122 retval = armv8_d_cache_sanity_check(armv8);
123 if (retval != ERROR_OK)
124 return retval;
126 retval = dpm->prepare(dpm);
127 if (retval != ERROR_OK)
128 goto done;
130 va_line = va & (-linelen);
131 va_end = va + size;
133 while (va_line < va_end) {
134 /* DC CIVAC */
135 /* Aarch32: DCCIMVAC: ARMV4_5_MCR(15, 0, 0, 7, 14, 1) */
136 retval = dpm->instr_write_data_r0_64(dpm,
137 armv8_opcode(armv8, ARMV8_OPC_DCCIVAC), va_line);
138 if (retval != ERROR_OK)
139 goto done;
140 va_line += linelen;
143 dpm->finish(dpm);
144 return retval;
146 done:
147 LOG_ERROR("d-cache invalidate failed");
148 dpm->finish(dpm);
150 return retval;
153 int armv8_cache_i_inner_inval_virt(struct armv8_common *armv8, target_addr_t va, size_t size)
155 struct arm_dpm *dpm = armv8->arm.dpm;
156 struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
157 uint64_t linelen = armv8_cache->iminline;
158 target_addr_t va_line, va_end;
159 int retval;
161 retval = armv8_i_cache_sanity_check(armv8);
162 if (retval != ERROR_OK)
163 return retval;
165 retval = dpm->prepare(dpm);
166 if (retval != ERROR_OK)
167 goto done;
169 va_line = va & (-linelen);
170 va_end = va + size;
172 while (va_line < va_end) {
173 /* IC IVAU - Invalidate instruction cache by VA to PoU. */
174 retval = dpm->instr_write_data_r0_64(dpm,
175 armv8_opcode(armv8, ARMV8_OPC_ICIVAU), va_line);
176 if (retval != ERROR_OK)
177 goto done;
178 va_line += linelen;
181 dpm->finish(dpm);
182 return retval;
184 done:
185 LOG_ERROR("d-cache invalidate failed");
186 dpm->finish(dpm);
188 return retval;
191 static int armv8_handle_inner_cache_info_command(struct command_context *cmd_ctx,
192 struct armv8_cache_common *armv8_cache)
194 int cl;
196 if (armv8_cache->info == -1) {
197 command_print(cmd_ctx, "cache not yet identified");
198 return ERROR_OK;
201 for (cl = 0; cl < armv8_cache->loc; cl++) {
202 struct armv8_arch_cache *arch = &(armv8_cache->arch[cl]);
204 if (arch->ctype & 1) {
205 command_print(cmd_ctx,
206 "L%d I-Cache: linelen %" PRIi32
207 ", associativity %" PRIi32
208 ", nsets %" PRIi32
209 ", cachesize %" PRId32 " KBytes",
210 cl+1,
211 arch->i_size.linelen,
212 arch->i_size.associativity,
213 arch->i_size.nsets,
214 arch->i_size.cachesize);
217 if (arch->ctype >= 2) {
218 command_print(cmd_ctx,
219 "L%d D-Cache: linelen %" PRIi32
220 ", associativity %" PRIi32
221 ", nsets %" PRIi32
222 ", cachesize %" PRId32 " KBytes",
223 cl+1,
224 arch->d_u_size.linelen,
225 arch->d_u_size.associativity,
226 arch->d_u_size.nsets,
227 arch->d_u_size.cachesize);
231 return ERROR_OK;
234 static int _armv8_flush_all_data(struct target *target)
236 return armv8_cache_d_inner_clean_inval_all(target_to_armv8(target));
239 static int armv8_flush_all_data(struct target *target)
241 int retval = ERROR_FAIL;
242 /* check that armv8_cache is correctly identify */
243 struct armv8_common *armv8 = target_to_armv8(target);
244 if (armv8->armv8_mmu.armv8_cache.info == -1) {
245 LOG_ERROR("trying to flush un-identified cache");
246 return retval;
249 if (target->smp) {
250 /* look if all the other target have been flushed in order to flush level
251 * 2 */
252 struct target_list *head;
253 struct target *curr;
254 head = target->head;
255 while (head != (struct target_list *)NULL) {
256 curr = head->target;
257 if (curr->state == TARGET_HALTED) {
258 LOG_INFO("Wait flushing data l1 on core %" PRId32, curr->coreid);
259 retval = _armv8_flush_all_data(curr);
261 head = head->next;
263 } else
264 retval = _armv8_flush_all_data(target);
265 return retval;
268 static int get_cache_info(struct arm_dpm *dpm, int cl, int ct, uint32_t *cache_reg)
270 struct armv8_common *armv8 = dpm->arm->arch_info;
271 int retval = ERROR_OK;
273 /* select cache level */
274 retval = dpm->instr_write_data_r0(dpm,
275 armv8_opcode(armv8, WRITE_REG_CSSELR),
276 (cl << 1) | (ct == 1 ? 1 : 0));
277 if (retval != ERROR_OK)
278 goto done;
280 retval = dpm->instr_read_data_r0(dpm,
281 armv8_opcode(armv8, READ_REG_CCSIDR),
282 cache_reg);
283 done:
284 return retval;
287 static struct armv8_cachesize decode_cache_reg(uint32_t cache_reg)
289 struct armv8_cachesize size;
290 int i = 0;
292 size.linelen = 16 << (cache_reg & 0x7);
293 size.associativity = ((cache_reg >> 3) & 0x3ff) + 1;
294 size.nsets = ((cache_reg >> 13) & 0x7fff) + 1;
295 size.cachesize = size.linelen * size.associativity * size.nsets / 1024;
297 /* compute info for set way operation on cache */
298 size.index_shift = (cache_reg & 0x7) + 4;
299 size.index = (cache_reg >> 13) & 0x7fff;
300 size.way = ((cache_reg >> 3) & 0x3ff);
302 while (((size.way << i) & 0x80000000) == 0)
303 i++;
304 size.way_shift = i;
306 return size;
309 int armv8_identify_cache(struct armv8_common *armv8)
311 /* read cache descriptor */
312 int retval = ERROR_FAIL;
313 struct arm_dpm *dpm = armv8->arm.dpm;
314 uint32_t csselr, clidr, ctr;
315 uint32_t cache_reg;
316 int cl, ctype;
317 struct armv8_cache_common *cache = &(armv8->armv8_mmu.armv8_cache);
319 retval = dpm->prepare(dpm);
320 if (retval != ERROR_OK)
321 goto done;
323 /* retrieve CTR */
324 retval = dpm->instr_read_data_r0(dpm,
325 armv8_opcode(armv8, READ_REG_CTR), &ctr);
326 if (retval != ERROR_OK)
327 goto done;
329 cache->iminline = 4UL << (ctr & 0xf);
330 cache->dminline = 4UL << ((ctr & 0xf0000) >> 16);
331 LOG_DEBUG("ctr %" PRIx32 " ctr.iminline %" PRId32 " ctr.dminline %" PRId32,
332 ctr, cache->iminline, cache->dminline);
334 /* retrieve CLIDR */
335 retval = dpm->instr_read_data_r0(dpm,
336 armv8_opcode(armv8, READ_REG_CLIDR), &clidr);
337 if (retval != ERROR_OK)
338 goto done;
340 cache->loc = (clidr & 0x7000000) >> 24;
341 LOG_DEBUG("Number of cache levels to PoC %" PRId32, cache->loc);
343 /* retrieve selected cache for later restore
344 * MRC p15, 2,<Rd>, c0, c0, 0; Read CSSELR */
345 retval = dpm->instr_read_data_r0(dpm,
346 armv8_opcode(armv8, READ_REG_CSSELR), &csselr);
347 if (retval != ERROR_OK)
348 goto done;
350 /* retrieve all available inner caches */
351 for (cl = 0; cl < cache->loc; clidr >>= 3, cl++) {
353 /* isolate cache type at current level */
354 ctype = clidr & 7;
356 /* skip reserved values */
357 if (ctype > CACHE_LEVEL_HAS_UNIFIED_CACHE)
358 continue;
360 /* separate d or unified d/i cache at this level ? */
361 if (ctype & (CACHE_LEVEL_HAS_UNIFIED_CACHE | CACHE_LEVEL_HAS_D_CACHE)) {
362 /* retrieve d-cache info */
363 retval = get_cache_info(dpm, cl, 0, &cache_reg);
364 if (retval != ERROR_OK)
365 goto done;
366 cache->arch[cl].d_u_size = decode_cache_reg(cache_reg);
368 LOG_DEBUG("data/unified cache index %d << %d, way %d << %d",
369 cache->arch[cl].d_u_size.index,
370 cache->arch[cl].d_u_size.index_shift,
371 cache->arch[cl].d_u_size.way,
372 cache->arch[cl].d_u_size.way_shift);
374 LOG_DEBUG("cacheline %d bytes %d KBytes asso %d ways",
375 cache->arch[cl].d_u_size.linelen,
376 cache->arch[cl].d_u_size.cachesize,
377 cache->arch[cl].d_u_size.associativity);
380 /* separate i-cache at this level ? */
381 if (ctype & CACHE_LEVEL_HAS_I_CACHE) {
382 /* retrieve i-cache info */
383 retval = get_cache_info(dpm, cl, 1, &cache_reg);
384 if (retval != ERROR_OK)
385 goto done;
386 cache->arch[cl].i_size = decode_cache_reg(cache_reg);
388 LOG_DEBUG("instruction cache index %d << %d, way %d << %d",
389 cache->arch[cl].i_size.index,
390 cache->arch[cl].i_size.index_shift,
391 cache->arch[cl].i_size.way,
392 cache->arch[cl].i_size.way_shift);
394 LOG_DEBUG("cacheline %d bytes %d KBytes asso %d ways",
395 cache->arch[cl].i_size.linelen,
396 cache->arch[cl].i_size.cachesize,
397 cache->arch[cl].i_size.associativity);
400 cache->arch[cl].ctype = ctype;
403 /* restore selected cache */
404 dpm->instr_write_data_r0(dpm,
405 armv8_opcode(armv8, WRITE_REG_CSSELR), csselr);
406 if (retval != ERROR_OK)
407 goto done;
409 armv8->armv8_mmu.armv8_cache.info = 1;
411 /* if no l2 cache initialize l1 data cache flush function function */
412 if (armv8->armv8_mmu.armv8_cache.flush_all_data_cache == NULL) {
413 armv8->armv8_mmu.armv8_cache.display_cache_info =
414 armv8_handle_inner_cache_info_command;
415 armv8->armv8_mmu.armv8_cache.flush_all_data_cache =
416 armv8_flush_all_data;
419 done:
420 dpm->finish(dpm);
421 return retval;