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  return dpm->finish(dpm);
96 
97 done:
98  LOG_ERROR("clean invalidate failed");
99  dpm->finish(dpm);
100 
101  return retval;
102 }
103 
105 {
106  struct arm_dpm *dpm = armv8->arm.dpm;
107  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
108  uint64_t linelen = armv8_cache->dminline;
109  target_addr_t va_line, va_end;
110  int retval;
111 
112  retval = armv8_d_cache_sanity_check(armv8);
113  if (retval != ERROR_OK)
114  return retval;
115 
116  retval = dpm->prepare(dpm);
117  if (retval != ERROR_OK)
118  goto done;
119 
120  va_line = va & (-linelen);
121  va_end = va + size;
122 
123  while (va_line < va_end) {
124  /* DC CIVAC */
125  /* Aarch32: DCCIMVAC: ARMV4_5_MCR(15, 0, 0, 7, 14, 1) */
126  retval = dpm->instr_write_data_r0_64(dpm,
127  armv8_opcode(armv8, ARMV8_OPC_DCCIVAC), va_line);
128  if (retval != ERROR_OK)
129  goto done;
130  va_line += linelen;
131  }
132 
133  dpm->finish(dpm);
134  return retval;
135 
136 done:
137  LOG_ERROR("d-cache invalidate failed");
138  dpm->finish(dpm);
139 
140  return retval;
141 }
142 
144 {
145  struct arm_dpm *dpm = armv8->arm.dpm;
146  int retval;
147 
148  retval = armv8_i_cache_sanity_check(armv8);
149  if (retval != ERROR_OK)
150  return retval;
151 
152  LOG_DEBUG("flushing cache");
153 
154  retval = dpm->prepare(dpm);
155  if (retval != ERROR_OK)
156  goto done;
157 
158  retval = dpm->instr_execute(dpm, armv8_opcode(armv8, ARMV8_OPC_ICIALLU));
159  if (retval != ERROR_OK)
160  goto done;
161 
162  dpm->finish(dpm);
163  LOG_DEBUG("flushing cache done");
164  return retval;
165 
166 done:
167  LOG_ERROR("i-cache invalidate failed");
168  dpm->finish(dpm);
169 
170  return retval;
171 }
172 
174 {
175  struct arm_dpm *dpm = armv8->arm.dpm;
176  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
177  uint64_t linelen = armv8_cache->iminline;
178  target_addr_t va_line, va_end;
179  int retval;
180 
181  retval = armv8_i_cache_sanity_check(armv8);
182  if (retval != ERROR_OK)
183  return retval;
184 
185  retval = dpm->prepare(dpm);
186  if (retval != ERROR_OK)
187  goto done;
188 
189  va_line = va & (-linelen);
190  va_end = va + size;
191 
192  while (va_line < va_end) {
193  /* IC IVAU - Invalidate instruction cache by VA to PoU. */
194  retval = dpm->instr_write_data_r0_64(dpm,
195  armv8_opcode(armv8, ARMV8_OPC_ICIVAU), va_line);
196  if (retval != ERROR_OK)
197  goto done;
198  va_line += linelen;
199  }
200 
201  dpm->finish(dpm);
202  return retval;
203 
204 done:
205  LOG_ERROR("d-cache invalidate failed");
206  dpm->finish(dpm);
207 
208  return retval;
209 }
210 
212  struct armv8_cache_common *armv8_cache)
213 {
214  int cl;
215 
216  if (!armv8_cache->info_valid) {
217  command_print(cmd, "cache not yet identified");
218  return ERROR_OK;
219  }
220 
221  for (cl = 0; cl < armv8_cache->loc; cl++) {
222  struct armv8_arch_cache *arch = &(armv8_cache->arch[cl]);
223 
224  if (arch->ctype & 1) {
226  "L%d I-Cache: linelen %" PRIu32
227  ", associativity %" PRIu32
228  ", nsets %" PRIu32
229  ", cachesize %" PRIu32 " KBytes",
230  cl+1,
231  arch->i_size.linelen,
232  arch->i_size.associativity,
233  arch->i_size.nsets,
234  arch->i_size.cachesize);
235  }
236 
237  if (arch->ctype >= 2) {
239  "L%d D-Cache: linelen %" PRIu32
240  ", associativity %" PRIu32
241  ", nsets %" PRIu32
242  ", cachesize %" PRIu32 " KBytes",
243  cl+1,
244  arch->d_u_size.linelen,
245  arch->d_u_size.associativity,
246  arch->d_u_size.nsets,
247  arch->d_u_size.cachesize);
248  }
249  }
250 
251  return ERROR_OK;
252 }
253 
255 {
257 }
258 
259 static int armv8_flush_all_data(struct target *target)
260 {
261  int retval = ERROR_FAIL;
262  /* check that armv8_cache is correctly identify */
263  struct armv8_common *armv8 = target_to_armv8(target);
264  if (!armv8->armv8_mmu.armv8_cache.info_valid) {
265  LOG_ERROR("trying to flush un-identified cache");
266  return retval;
267  }
268 
269  if (target->smp) {
270  /* look if all the other target have been flushed in order to flush level
271  * 2 */
272  struct target_list *head;
274  struct target *curr = head->target;
275  if (curr->state == TARGET_HALTED) {
276  LOG_TARGET_INFO(curr, "Wait flushing data l1.");
277  retval = _armv8_flush_all_data(curr);
278  }
279  }
280  } else
281  retval = _armv8_flush_all_data(target);
282  return retval;
283 }
284 
286 {
287  int retval = ERROR_FAIL;
288  /* check that armv8_cache is correctly identify */
289  struct armv8_common *armv8 = target_to_armv8(target);
290  if (!armv8->armv8_mmu.armv8_cache.info_valid) {
291  LOG_ERROR("trying to flush un-identified cache");
292  return retval;
293  }
294 
295  if (target->smp) {
296  /* look if all the other target have been flushed in order to flush icache */
297  struct target_list *head;
299  struct target *curr = head->target;
300  if (curr->state == TARGET_HALTED) {
301  LOG_TARGET_INFO(curr, "Wait flushing instruction l1.");
303  }
304  }
305  } else {
306  retval = armv8_cache_i_inner_clean_inval_all(armv8);
307  }
308  return retval;
309 }
310 
311 static int get_cache_info(struct arm_dpm *dpm, int cl, int ct, uint32_t *cache_reg)
312 {
313  struct armv8_common *armv8 = dpm->arm->arch_info;
314  int retval = ERROR_OK;
315 
316  /* select cache level */
317  retval = dpm->instr_write_data_r0(dpm,
319  (cl << 1) | (ct == 1 ? 1 : 0));
320  if (retval != ERROR_OK)
321  goto done;
322 
323  retval = dpm->instr_read_data_r0(dpm,
325  cache_reg);
326  done:
327  return retval;
328 }
329 
330 static struct armv8_cachesize decode_cache_reg(uint32_t cache_reg)
331 {
332  struct armv8_cachesize size;
333  int i = 0;
334 
335  size.linelen = 16 << (cache_reg & 0x7);
336  size.associativity = ((cache_reg >> 3) & 0x3ff) + 1;
337  size.nsets = ((cache_reg >> 13) & 0x7fff) + 1;
338  size.cachesize = size.linelen * size.associativity * size.nsets / 1024;
339 
340  /* compute info for set way operation on cache */
341  size.index_shift = (cache_reg & 0x7) + 4;
342  size.index = (cache_reg >> 13) & 0x7fff;
343  size.way = ((cache_reg >> 3) & 0x3ff);
344 
345  if (size.way != 0)
346  while (((size.way << i) & 0x80000000) == 0)
347  i++;
348  size.way_shift = i;
349 
350  return size;
351 }
352 
354 {
355  /* read cache descriptor */
356  int retval = ERROR_FAIL;
357  struct arm *arm = &armv8->arm;
358  struct arm_dpm *dpm = armv8->arm.dpm;
359  uint32_t csselr, clidr, ctr;
360  uint32_t cache_reg;
361  int cl, ctype;
362  struct armv8_cache_common *cache = &(armv8->armv8_mmu.armv8_cache);
363 
364  retval = dpm->prepare(dpm);
365  if (retval != ERROR_OK)
366  goto done;
367 
368  /* check if we're in an unprivileged mode */
370  retval = armv8_dpm_modeswitch(dpm, ARMV8_64_EL1H);
371  if (retval != ERROR_OK)
372  return retval;
373  }
374 
375  /* retrieve CTR */
376  retval = dpm->instr_read_data_r0(dpm,
377  armv8_opcode(armv8, READ_REG_CTR), &ctr);
378  if (retval != ERROR_OK)
379  goto done;
380 
381  cache->iminline = 4UL << (ctr & 0xf);
382  cache->dminline = 4UL << ((ctr & 0xf0000) >> 16);
383  LOG_DEBUG("ctr %" PRIx32 " ctr.iminline %" PRIu32 " ctr.dminline %" PRIu32,
384  ctr, cache->iminline, cache->dminline);
385 
386  /* retrieve CLIDR */
387  retval = dpm->instr_read_data_r0(dpm,
388  armv8_opcode(armv8, READ_REG_CLIDR), &clidr);
389  if (retval != ERROR_OK)
390  goto done;
391 
392  cache->loc = (clidr & 0x7000000) >> 24;
393  LOG_DEBUG("Number of cache levels to PoC %" PRId32, cache->loc);
394 
395  /* retrieve selected cache for later restore
396  * MRC p15, 2,<Rd>, c0, c0, 0; Read CSSELR */
397  retval = dpm->instr_read_data_r0(dpm,
398  armv8_opcode(armv8, READ_REG_CSSELR), &csselr);
399  if (retval != ERROR_OK)
400  goto done;
401 
402  /* retrieve all available inner caches */
403  for (cl = 0; cl < cache->loc; clidr >>= 3, cl++) {
404 
405  /* isolate cache type at current level */
406  ctype = clidr & 7;
407 
408  /* skip reserved values */
409  if (ctype > CACHE_LEVEL_HAS_UNIFIED_CACHE)
410  continue;
411 
412  /* separate d or unified d/i cache at this level ? */
414  /* retrieve d-cache info */
415  retval = get_cache_info(dpm, cl, 0, &cache_reg);
416  if (retval != ERROR_OK)
417  goto done;
418  cache->arch[cl].d_u_size = decode_cache_reg(cache_reg);
419 
420  LOG_DEBUG("data/unified cache index %" PRIu32 " << %" PRIu32 ", way %" PRIu32 " << %" PRIu32,
421  cache->arch[cl].d_u_size.index,
422  cache->arch[cl].d_u_size.index_shift,
423  cache->arch[cl].d_u_size.way,
424  cache->arch[cl].d_u_size.way_shift);
425 
426  LOG_DEBUG("cacheline %" PRIu32 " bytes %" PRIu32 " KBytes asso %" PRIu32 " ways",
427  cache->arch[cl].d_u_size.linelen,
428  cache->arch[cl].d_u_size.cachesize,
429  cache->arch[cl].d_u_size.associativity);
430  }
431 
432  /* separate i-cache at this level ? */
433  if (ctype & CACHE_LEVEL_HAS_I_CACHE) {
434  /* retrieve i-cache info */
435  retval = get_cache_info(dpm, cl, 1, &cache_reg);
436  if (retval != ERROR_OK)
437  goto done;
438  cache->arch[cl].i_size = decode_cache_reg(cache_reg);
439 
440  LOG_DEBUG("instruction cache index %" PRIu32 " << %" PRIu32 ", way %" PRIu32 " << %" PRIu32,
441  cache->arch[cl].i_size.index,
442  cache->arch[cl].i_size.index_shift,
443  cache->arch[cl].i_size.way,
444  cache->arch[cl].i_size.way_shift);
445 
446  LOG_DEBUG("cacheline %" PRIu32 " bytes %" PRIu32 " KBytes asso %" PRIu32 " ways",
447  cache->arch[cl].i_size.linelen,
448  cache->arch[cl].i_size.cachesize,
449  cache->arch[cl].i_size.associativity);
450  }
451 
452  cache->arch[cl].ctype = ctype;
453  }
454 
455  /* restore selected cache */
456  dpm->instr_write_data_r0(dpm,
457  armv8_opcode(armv8, WRITE_REG_CSSELR), csselr);
458  if (retval != ERROR_OK)
459  goto done;
460 
461  armv8->armv8_mmu.armv8_cache.info_valid = true;
462 
463  /* if no l2 cache initialize l1 data cache flush function function */
469  }
475  }
476 
477 done:
479  dpm->finish(dpm);
480  return retval;
481 
482 }
@ 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:234
static unsigned int armv8_curel_from_core_mode(enum arm_mode core_mode)
Definition: armv8.h:298
static int armv8_handle_inner_cache_info_command(struct command_invocation *cmd, struct armv8_cache_common *armv8_cache)
Definition: armv8_cache.c:211
int armv8_identify_cache(struct armv8_common *armv8)
Definition: armv8_cache.c:353
int armv8_cache_d_inner_flush_virt(struct armv8_common *armv8, target_addr_t va, size_t size)
Definition: armv8_cache.c:104
static int get_cache_info(struct arm_dpm *dpm, int cl, int ct, uint32_t *cache_reg)
Definition: armv8_cache.c:311
static int armv8_cache_i_inner_clean_inval_all(struct armv8_common *armv8)
Definition: armv8_cache.c:143
static int armv8_flush_all_instruction(struct target *target)
Definition: armv8_cache.c:285
#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:254
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:173
static int armv8_flush_all_data(struct target *target)
Definition: armv8_cache.c:259
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:330
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:389
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
void keep_alive(void)
Definition: log.c:437
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#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:176
void * arch_info
Definition: arm.h:252
enum arm_mode core_mode
Record the current core mode: SVC, USR, or some other mode.
Definition: arm.h:197
struct arm_dpm * dpm
Handle for the debug module, if one is present.
Definition: arm.h:214
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
bool d_u_cache_enabled
Definition: armv8.h:160
uint32_t dminline
Definition: armv8.h:157
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
bool i_cache_enabled
Definition: armv8.h:159
int(* flush_all_data_cache)(struct target *target)
Definition: armv8.h:164
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:205
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:227
Definition: target.h:119
enum target_state state
Definition: target.h:167
struct list_head * smp_targets
Definition: target.h:201
unsigned int smp
Definition: target.h:200
#define ERROR_TARGET_INVALID
Definition: target.h:814
@ TARGET_HALTED
Definition: target.h:58
uint64_t target_addr_t
Definition: types.h:279
uint8_t cmd
Definition: vdebug.c:1