OpenOCD
armv8_cache.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2016 by Matthias Welwarsky *
5  * matthias.welwarsky@sysgo.com *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "armv8_cache.h"
13 #include "armv8_dpm.h"
14 #include "armv8_opcodes.h"
15 #include "smp.h"
16 
17 /* CLIDR cache types */
18 #define CACHE_LEVEL_HAS_UNIFIED_CACHE 0x4
19 #define CACHE_LEVEL_HAS_D_CACHE 0x2
20 #define CACHE_LEVEL_HAS_I_CACHE 0x1
21 
22 static int armv8_d_cache_sanity_check(struct armv8_common *armv8)
23 {
24  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
25 
26  if (armv8_cache->d_u_cache_enabled)
27  return ERROR_OK;
28 
29  return ERROR_TARGET_INVALID;
30 }
31 
32 static int armv8_i_cache_sanity_check(struct armv8_common *armv8)
33 {
34  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
35 
36  if (armv8_cache->i_cache_enabled)
37  return ERROR_OK;
38 
39  return ERROR_TARGET_INVALID;
40 }
41 
42 static int armv8_cache_d_inner_flush_level(struct armv8_common *armv8, struct armv8_cachesize *size, int cl)
43 {
44  struct arm_dpm *dpm = armv8->arm.dpm;
45  int retval = ERROR_OK;
46  int32_t c_way, c_index = size->index;
47 
48  LOG_DEBUG("cl %" PRId32, cl);
49  do {
50  c_way = size->way;
51  do {
52  uint32_t value = (c_index << size->index_shift)
53  | (c_way << size->way_shift) | (cl << 1);
54  /*
55  * DC CISW - Clean and invalidate data cache
56  * line by Set/Way.
57  */
58  retval = dpm->instr_write_data_r0(dpm,
59  armv8_opcode(armv8, ARMV8_OPC_DCCISW), value);
60  if (retval != ERROR_OK)
61  goto done;
62  c_way -= 1;
63  } while (c_way >= 0);
64  keep_alive();
65  c_index -= 1;
66  } while (c_index >= 0);
67 
68  done:
69  return retval;
70 }
71 
73 {
74  struct armv8_cache_common *cache = &(armv8->armv8_mmu.armv8_cache);
75  struct arm_dpm *dpm = armv8->arm.dpm;
76  int cl;
77  int retval;
78 
79  retval = armv8_d_cache_sanity_check(armv8);
80  if (retval != ERROR_OK)
81  return retval;
82 
83  retval = dpm->prepare(dpm);
84  if (retval != ERROR_OK)
85  goto done;
86 
87  for (cl = 0; cl < cache->loc; cl++) {
88  /* skip i-only caches */
89  if (cache->arch[cl].ctype < CACHE_LEVEL_HAS_D_CACHE)
90  continue;
91 
92  armv8_cache_d_inner_flush_level(armv8, &cache->arch[cl].d_u_size, cl);
93  }
94 
95  retval = dpm->finish(dpm);
96  return retval;
97 
98 done:
99  LOG_ERROR("clean invalidate failed");
100  dpm->finish(dpm);
101 
102  return retval;
103 }
104 
106 {
107  struct arm_dpm *dpm = armv8->arm.dpm;
108  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
109  uint64_t linelen = armv8_cache->dminline;
110  target_addr_t va_line, va_end;
111  int retval;
112 
113  retval = armv8_d_cache_sanity_check(armv8);
114  if (retval != ERROR_OK)
115  return retval;
116 
117  retval = dpm->prepare(dpm);
118  if (retval != ERROR_OK)
119  goto done;
120 
121  va_line = va & (-linelen);
122  va_end = va + size;
123 
124  while (va_line < va_end) {
125  /* DC CIVAC */
126  /* Aarch32: DCCIMVAC: ARMV4_5_MCR(15, 0, 0, 7, 14, 1) */
127  retval = dpm->instr_write_data_r0_64(dpm,
128  armv8_opcode(armv8, ARMV8_OPC_DCCIVAC), va_line);
129  if (retval != ERROR_OK)
130  goto done;
131  va_line += linelen;
132  }
133 
134  dpm->finish(dpm);
135  return retval;
136 
137 done:
138  LOG_ERROR("d-cache invalidate failed");
139  dpm->finish(dpm);
140 
141  return retval;
142 }
143 
145 {
146  struct arm_dpm *dpm = armv8->arm.dpm;
147  int retval;
148 
149  retval = armv8_i_cache_sanity_check(armv8);
150  if (retval != ERROR_OK)
151  return retval;
152 
153  LOG_DEBUG("flushing cache");
154 
155  retval = dpm->prepare(dpm);
156  if (retval != ERROR_OK)
157  goto done;
158 
159  retval = dpm->instr_execute(dpm, armv8_opcode(armv8, ARMV8_OPC_ICIALLU));
160  if (retval != ERROR_OK)
161  goto done;
162 
163  dpm->finish(dpm);
164  LOG_DEBUG("flushing cache done");
165  return retval;
166 
167 done:
168  LOG_ERROR("i-cache invalidate failed");
169  dpm->finish(dpm);
170 
171  return retval;
172 }
173 
175 {
176  struct arm_dpm *dpm = armv8->arm.dpm;
177  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
178  uint64_t linelen = armv8_cache->iminline;
179  target_addr_t va_line, va_end;
180  int retval;
181 
182  retval = armv8_i_cache_sanity_check(armv8);
183  if (retval != ERROR_OK)
184  return retval;
185 
186  retval = dpm->prepare(dpm);
187  if (retval != ERROR_OK)
188  goto done;
189 
190  va_line = va & (-linelen);
191  va_end = va + size;
192 
193  while (va_line < va_end) {
194  /* IC IVAU - Invalidate instruction cache by VA to PoU. */
195  retval = dpm->instr_write_data_r0_64(dpm,
196  armv8_opcode(armv8, ARMV8_OPC_ICIVAU), va_line);
197  if (retval != ERROR_OK)
198  goto done;
199  va_line += linelen;
200  }
201 
202  dpm->finish(dpm);
203  return retval;
204 
205 done:
206  LOG_ERROR("d-cache invalidate failed");
207  dpm->finish(dpm);
208 
209  return retval;
210 }
211 
213  struct armv8_cache_common *armv8_cache)
214 {
215  int cl;
216 
217  if (armv8_cache->info == -1) {
218  command_print(cmd, "cache not yet identified");
219  return ERROR_OK;
220  }
221 
222  for (cl = 0; cl < armv8_cache->loc; cl++) {
223  struct armv8_arch_cache *arch = &(armv8_cache->arch[cl]);
224 
225  if (arch->ctype & 1) {
227  "L%d I-Cache: linelen %" PRIu32
228  ", associativity %" PRIu32
229  ", nsets %" PRIu32
230  ", cachesize %" PRIu32 " KBytes",
231  cl+1,
232  arch->i_size.linelen,
233  arch->i_size.associativity,
234  arch->i_size.nsets,
235  arch->i_size.cachesize);
236  }
237 
238  if (arch->ctype >= 2) {
240  "L%d D-Cache: linelen %" PRIu32
241  ", associativity %" PRIu32
242  ", nsets %" PRIu32
243  ", cachesize %" PRIu32 " KBytes",
244  cl+1,
245  arch->d_u_size.linelen,
246  arch->d_u_size.associativity,
247  arch->d_u_size.nsets,
248  arch->d_u_size.cachesize);
249  }
250  }
251 
252  return ERROR_OK;
253 }
254 
256 {
258 }
259 
260 static int armv8_flush_all_data(struct target *target)
261 {
262  int retval = ERROR_FAIL;
263  /* check that armv8_cache is correctly identify */
264  struct armv8_common *armv8 = target_to_armv8(target);
265  if (armv8->armv8_mmu.armv8_cache.info == -1) {
266  LOG_ERROR("trying to flush un-identified cache");
267  return retval;
268  }
269 
270  if (target->smp) {
271  /* look if all the other target have been flushed in order to flush level
272  * 2 */
273  struct target_list *head;
275  struct target *curr = head->target;
276  if (curr->state == TARGET_HALTED) {
277  LOG_TARGET_INFO(curr, "Wait flushing data l1.");
278  retval = _armv8_flush_all_data(curr);
279  }
280  }
281  } else
282  retval = _armv8_flush_all_data(target);
283  return retval;
284 }
285 
287 {
288  int retval = ERROR_FAIL;
289  /* check that armv8_cache is correctly identify */
290  struct armv8_common *armv8 = target_to_armv8(target);
291  if (armv8->armv8_mmu.armv8_cache.info == -1) {
292  LOG_ERROR("trying to flush un-identified cache");
293  return retval;
294  }
295 
296  if (target->smp) {
297  /* look if all the other target have been flushed in order to flush icache */
298  struct target_list *head;
300  struct target *curr = head->target;
301  if (curr->state == TARGET_HALTED) {
302  LOG_TARGET_INFO(curr, "Wait flushing instruction l1.");
304  }
305  }
306  } else {
307  retval = armv8_cache_i_inner_clean_inval_all(armv8);
308  }
309  return retval;
310 }
311 
312 static int get_cache_info(struct arm_dpm *dpm, int cl, int ct, uint32_t *cache_reg)
313 {
314  struct armv8_common *armv8 = dpm->arm->arch_info;
315  int retval = ERROR_OK;
316 
317  /* select cache level */
318  retval = dpm->instr_write_data_r0(dpm,
320  (cl << 1) | (ct == 1 ? 1 : 0));
321  if (retval != ERROR_OK)
322  goto done;
323 
324  retval = dpm->instr_read_data_r0(dpm,
326  cache_reg);
327  done:
328  return retval;
329 }
330 
331 static struct armv8_cachesize decode_cache_reg(uint32_t cache_reg)
332 {
333  struct armv8_cachesize size;
334  int i = 0;
335 
336  size.linelen = 16 << (cache_reg & 0x7);
337  size.associativity = ((cache_reg >> 3) & 0x3ff) + 1;
338  size.nsets = ((cache_reg >> 13) & 0x7fff) + 1;
339  size.cachesize = size.linelen * size.associativity * size.nsets / 1024;
340 
341  /* compute info for set way operation on cache */
342  size.index_shift = (cache_reg & 0x7) + 4;
343  size.index = (cache_reg >> 13) & 0x7fff;
344  size.way = ((cache_reg >> 3) & 0x3ff);
345 
346  if (size.way != 0)
347  while (((size.way << i) & 0x80000000) == 0)
348  i++;
349  size.way_shift = i;
350 
351  return size;
352 }
353 
355 {
356  /* read cache descriptor */
357  int retval = ERROR_FAIL;
358  struct arm *arm = &armv8->arm;
359  struct arm_dpm *dpm = armv8->arm.dpm;
360  uint32_t csselr, clidr, ctr;
361  uint32_t cache_reg;
362  int cl, ctype;
363  struct armv8_cache_common *cache = &(armv8->armv8_mmu.armv8_cache);
364 
365  retval = dpm->prepare(dpm);
366  if (retval != ERROR_OK)
367  goto done;
368 
369  /* check if we're in an unprivileged mode */
371  retval = armv8_dpm_modeswitch(dpm, ARMV8_64_EL1H);
372  if (retval != ERROR_OK)
373  return retval;
374  }
375 
376  /* retrieve CTR */
377  retval = dpm->instr_read_data_r0(dpm,
378  armv8_opcode(armv8, READ_REG_CTR), &ctr);
379  if (retval != ERROR_OK)
380  goto done;
381 
382  cache->iminline = 4UL << (ctr & 0xf);
383  cache->dminline = 4UL << ((ctr & 0xf0000) >> 16);
384  LOG_DEBUG("ctr %" PRIx32 " ctr.iminline %" PRIu32 " ctr.dminline %" PRIu32,
385  ctr, cache->iminline, cache->dminline);
386 
387  /* retrieve CLIDR */
388  retval = dpm->instr_read_data_r0(dpm,
389  armv8_opcode(armv8, READ_REG_CLIDR), &clidr);
390  if (retval != ERROR_OK)
391  goto done;
392 
393  cache->loc = (clidr & 0x7000000) >> 24;
394  LOG_DEBUG("Number of cache levels to PoC %" PRId32, cache->loc);
395 
396  /* retrieve selected cache for later restore
397  * MRC p15, 2,<Rd>, c0, c0, 0; Read CSSELR */
398  retval = dpm->instr_read_data_r0(dpm,
399  armv8_opcode(armv8, READ_REG_CSSELR), &csselr);
400  if (retval != ERROR_OK)
401  goto done;
402 
403  /* retrieve all available inner caches */
404  for (cl = 0; cl < cache->loc; clidr >>= 3, cl++) {
405 
406  /* isolate cache type at current level */
407  ctype = clidr & 7;
408 
409  /* skip reserved values */
410  if (ctype > CACHE_LEVEL_HAS_UNIFIED_CACHE)
411  continue;
412 
413  /* separate d or unified d/i cache at this level ? */
415  /* retrieve d-cache info */
416  retval = get_cache_info(dpm, cl, 0, &cache_reg);
417  if (retval != ERROR_OK)
418  goto done;
419  cache->arch[cl].d_u_size = decode_cache_reg(cache_reg);
420 
421  LOG_DEBUG("data/unified cache index %" PRIu32 " << %" PRIu32 ", way %" PRIu32 " << %" PRIu32,
422  cache->arch[cl].d_u_size.index,
423  cache->arch[cl].d_u_size.index_shift,
424  cache->arch[cl].d_u_size.way,
425  cache->arch[cl].d_u_size.way_shift);
426 
427  LOG_DEBUG("cacheline %" PRIu32 " bytes %" PRIu32 " KBytes asso %" PRIu32 " ways",
428  cache->arch[cl].d_u_size.linelen,
429  cache->arch[cl].d_u_size.cachesize,
430  cache->arch[cl].d_u_size.associativity);
431  }
432 
433  /* separate i-cache at this level ? */
434  if (ctype & CACHE_LEVEL_HAS_I_CACHE) {
435  /* retrieve i-cache info */
436  retval = get_cache_info(dpm, cl, 1, &cache_reg);
437  if (retval != ERROR_OK)
438  goto done;
439  cache->arch[cl].i_size = decode_cache_reg(cache_reg);
440 
441  LOG_DEBUG("instruction cache index %" PRIu32 " << %" PRIu32 ", way %" PRIu32 " << %" PRIu32,
442  cache->arch[cl].i_size.index,
443  cache->arch[cl].i_size.index_shift,
444  cache->arch[cl].i_size.way,
445  cache->arch[cl].i_size.way_shift);
446 
447  LOG_DEBUG("cacheline %" PRIu32 " bytes %" PRIu32 " KBytes asso %" PRIu32 " ways",
448  cache->arch[cl].i_size.linelen,
449  cache->arch[cl].i_size.cachesize,
450  cache->arch[cl].i_size.associativity);
451  }
452 
453  cache->arch[cl].ctype = ctype;
454  }
455 
456  /* restore selected cache */
457  dpm->instr_write_data_r0(dpm,
458  armv8_opcode(armv8, WRITE_REG_CSSELR), csselr);
459  if (retval != ERROR_OK)
460  goto done;
461 
462  armv8->armv8_mmu.armv8_cache.info = 1;
463 
464  /* if no l2 cache initialize l1 data cache flush function function */
470  }
476  }
477 
478 done:
480  dpm->finish(dpm);
481  return retval;
482 
483 }
@ ARM_MODE_ANY
Definition: arm.h:106
@ ARMV8_64_EL1H
Definition: arm.h:100
static struct armv8_common * target_to_armv8(struct target *target)
Definition: armv8.h:239
static unsigned int armv8_curel_from_core_mode(enum arm_mode core_mode)
Definition: armv8.h:308
static int armv8_handle_inner_cache_info_command(struct command_invocation *cmd, struct armv8_cache_common *armv8_cache)
Definition: armv8_cache.c:212
int armv8_identify_cache(struct armv8_common *armv8)
Definition: armv8_cache.c:354
int armv8_cache_d_inner_flush_virt(struct armv8_common *armv8, target_addr_t va, size_t size)
Definition: armv8_cache.c:105
static int get_cache_info(struct arm_dpm *dpm, int cl, int ct, uint32_t *cache_reg)
Definition: armv8_cache.c:312
static int armv8_cache_i_inner_clean_inval_all(struct armv8_common *armv8)
Definition: armv8_cache.c:144
static int armv8_flush_all_instruction(struct target *target)
Definition: armv8_cache.c:286
#define CACHE_LEVEL_HAS_UNIFIED_CACHE
Definition: armv8_cache.c:18
#define CACHE_LEVEL_HAS_I_CACHE
Definition: armv8_cache.c:20
static int _armv8_flush_all_data(struct target *target)
Definition: armv8_cache.c:255
static int armv8_d_cache_sanity_check(struct armv8_common *armv8)
Definition: armv8_cache.c:22
static int armv8_cache_d_inner_clean_inval_all(struct armv8_common *armv8)
Definition: armv8_cache.c:72
static int armv8_cache_d_inner_flush_level(struct armv8_common *armv8, struct armv8_cachesize *size, int cl)
Definition: armv8_cache.c:42
int armv8_cache_i_inner_inval_virt(struct armv8_common *armv8, target_addr_t va, size_t size)
Definition: armv8_cache.c:174
static int armv8_flush_all_data(struct target *target)
Definition: armv8_cache.c:260
static int armv8_i_cache_sanity_check(struct armv8_common *armv8)
Definition: armv8_cache.c:32
#define CACHE_LEVEL_HAS_D_CACHE
Definition: armv8_cache.c:19
static struct armv8_cachesize decode_cache_reg(uint32_t cache_reg)
Definition: armv8_cache.c:331
int armv8_dpm_modeswitch(struct arm_dpm *dpm, enum arm_mode mode)
Definition: armv8_dpm.c:538
#define SYSTEM_CUREL_EL1
Definition: armv8_opcodes.h:15
armv8_opcode
@ ARMV8_OPC_DCCISW
@ READ_REG_CCSIDR
@ READ_REG_CTR
@ WRITE_REG_CSSELR
@ ARMV8_OPC_ICIALLU
@ READ_REG_CLIDR
@ ARMV8_OPC_DCCIVAC
@ ARMV8_OPC_ICIVAU
@ READ_REG_CSSELR
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:444
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
void keep_alive(void)
Definition: log.c:426
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:153
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
#define foreach_smp_target(pos, head)
Definition: smp.h:15
This wraps an implementation of DPM primitives.
Definition: arm_dpm.h:47
int(* instr_write_data_r0_64)(struct arm_dpm *dpm, uint32_t opcode, uint64_t data)
Runs one instruction, writing data to R0 before execution.
Definition: arm_dpm.h:82
int(* instr_execute)(struct arm_dpm *dpm, uint32_t opcode)
Runs one instruction.
Definition: arm_dpm.h:60
int(* instr_write_data_r0)(struct arm_dpm *dpm, uint32_t opcode, uint32_t data)
Runs one instruction, writing data to R0 before execution.
Definition: arm_dpm.h:72
struct arm * arm
Definition: arm_dpm.h:48
int(* finish)(struct arm_dpm *dpm)
Invoke after a series of instruction operations.
Definition: arm_dpm.h:57
int(* prepare)(struct arm_dpm *dpm)
Invoke before a series of instruction operations.
Definition: arm_dpm.h:54
int(* instr_read_data_r0)(struct arm_dpm *dpm, uint32_t opcode, uint32_t *data)
Runs one instruction, reading data from r0 after execution.
Definition: arm_dpm.h:98
Represents a generic ARM core, with standard application registers.
Definition: arm.h:175
void * arch_info
Definition: arm.h:251
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:196
struct arm_dpm * dpm
Handle for the debug module, if one is present.
Definition: arm.h:213
struct armv8_cachesize d_u_size
Definition: armv8.h:149
struct armv8_cachesize i_size
Definition: armv8.h:150
uint32_t iminline
Definition: armv8.h:156
uint32_t dminline
Definition: armv8.h:157
int d_u_cache_enabled
Definition: armv8.h:160
int(* invalidate_all_instruction_cache)(struct target *target)
Definition: armv8.h:165
struct armv8_arch_cache arch[6]
Definition: armv8.h:158
int(* display_cache_info)(struct command_invocation *cmd, struct armv8_cache_common *armv8_cache)
Definition: armv8.h:166
int(* flush_all_data_cache)(struct target *target)
Definition: armv8.h:164
int i_cache_enabled
Definition: armv8.h:159
uint32_t way_shift
Definition: armv8.h:143
uint32_t associativity
Definition: armv8.h:136
uint32_t index
Definition: armv8.h:140
uint32_t index_shift
Definition: armv8.h:141
uint32_t way
Definition: armv8.h:142
uint32_t nsets
Definition: armv8.h:137
uint32_t linelen
Definition: armv8.h:135
uint32_t cachesize
Definition: armv8.h:138
struct arm arm
Definition: armv8.h:188
struct arm_dpm dpm
Definition: armv8.h:192
struct armv8_mmu_common armv8_mmu
Definition: armv8.h:210
struct armv8_cache_common armv8_cache
Definition: armv8.h:181
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
struct target * target
Definition: target.h:214
Definition: target.h:116
enum target_state state
Definition: target.h:157
struct list_head * smp_targets
Definition: target.h:188
unsigned int smp
Definition: target.h:187
#define ERROR_TARGET_INVALID
Definition: target.h:787
@ TARGET_HALTED
Definition: target.h:56
uint64_t target_addr_t
Definition: types.h:335
uint8_t cmd
Definition: vdebug.c:1