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  struct armv8_cache_common *armv8_cache = &armv8->armv8_mmu.armv8_cache;
148  uint64_t linelen = armv8_cache->iminline;
149  target_addr_t va_line, va_end;
150  int retval;
151 
152  retval = armv8_i_cache_sanity_check(armv8);
153  if (retval != ERROR_OK)
154  return retval;
155 
156  retval = dpm->prepare(dpm);
157  if (retval != ERROR_OK)
158  goto done;
159 
160  va_line = va & (-linelen);
161  va_end = va + size;
162 
163  while (va_line < va_end) {
164  /* IC IVAU - Invalidate instruction cache by VA to PoU. */
165  retval = dpm->instr_write_data_r0_64(dpm,
166  armv8_opcode(armv8, ARMV8_OPC_ICIVAU), va_line);
167  if (retval != ERROR_OK)
168  goto done;
169  va_line += linelen;
170  }
171 
172  dpm->finish(dpm);
173  return retval;
174 
175 done:
176  LOG_ERROR("d-cache invalidate failed");
177  dpm->finish(dpm);
178 
179  return retval;
180 }
181 
183  struct armv8_cache_common *armv8_cache)
184 {
185  int cl;
186 
187  if (armv8_cache->info == -1) {
188  command_print(cmd, "cache not yet identified");
189  return ERROR_OK;
190  }
191 
192  for (cl = 0; cl < armv8_cache->loc; cl++) {
193  struct armv8_arch_cache *arch = &(armv8_cache->arch[cl]);
194 
195  if (arch->ctype & 1) {
197  "L%d I-Cache: linelen %" PRIu32
198  ", associativity %" PRIu32
199  ", nsets %" PRIu32
200  ", cachesize %" PRIu32 " KBytes",
201  cl+1,
202  arch->i_size.linelen,
203  arch->i_size.associativity,
204  arch->i_size.nsets,
205  arch->i_size.cachesize);
206  }
207 
208  if (arch->ctype >= 2) {
210  "L%d D-Cache: linelen %" PRIu32
211  ", associativity %" PRIu32
212  ", nsets %" PRIu32
213  ", cachesize %" PRIu32 " KBytes",
214  cl+1,
215  arch->d_u_size.linelen,
216  arch->d_u_size.associativity,
217  arch->d_u_size.nsets,
218  arch->d_u_size.cachesize);
219  }
220  }
221 
222  return ERROR_OK;
223 }
224 
226 {
228 }
229 
230 static int armv8_flush_all_data(struct target *target)
231 {
232  int retval = ERROR_FAIL;
233  /* check that armv8_cache is correctly identify */
234  struct armv8_common *armv8 = target_to_armv8(target);
235  if (armv8->armv8_mmu.armv8_cache.info == -1) {
236  LOG_ERROR("trying to flush un-identified cache");
237  return retval;
238  }
239 
240  if (target->smp) {
241  /* look if all the other target have been flushed in order to flush level
242  * 2 */
243  struct target_list *head;
245  struct target *curr = head->target;
246  if (curr->state == TARGET_HALTED) {
247  LOG_TARGET_INFO(curr, "Wait flushing data l1.");
248  retval = _armv8_flush_all_data(curr);
249  }
250  }
251  } else
252  retval = _armv8_flush_all_data(target);
253  return retval;
254 }
255 
256 static int get_cache_info(struct arm_dpm *dpm, int cl, int ct, uint32_t *cache_reg)
257 {
258  struct armv8_common *armv8 = dpm->arm->arch_info;
259  int retval = ERROR_OK;
260 
261  /* select cache level */
262  retval = dpm->instr_write_data_r0(dpm,
264  (cl << 1) | (ct == 1 ? 1 : 0));
265  if (retval != ERROR_OK)
266  goto done;
267 
268  retval = dpm->instr_read_data_r0(dpm,
270  cache_reg);
271  done:
272  return retval;
273 }
274 
275 static struct armv8_cachesize decode_cache_reg(uint32_t cache_reg)
276 {
277  struct armv8_cachesize size;
278  int i = 0;
279 
280  size.linelen = 16 << (cache_reg & 0x7);
281  size.associativity = ((cache_reg >> 3) & 0x3ff) + 1;
282  size.nsets = ((cache_reg >> 13) & 0x7fff) + 1;
283  size.cachesize = size.linelen * size.associativity * size.nsets / 1024;
284 
285  /* compute info for set way operation on cache */
286  size.index_shift = (cache_reg & 0x7) + 4;
287  size.index = (cache_reg >> 13) & 0x7fff;
288  size.way = ((cache_reg >> 3) & 0x3ff);
289 
290  if (size.way != 0)
291  while (((size.way << i) & 0x80000000) == 0)
292  i++;
293  size.way_shift = i;
294 
295  return size;
296 }
297 
299 {
300  /* read cache descriptor */
301  int retval = ERROR_FAIL;
302  struct arm *arm = &armv8->arm;
303  struct arm_dpm *dpm = armv8->arm.dpm;
304  uint32_t csselr, clidr, ctr;
305  uint32_t cache_reg;
306  int cl, ctype;
307  struct armv8_cache_common *cache = &(armv8->armv8_mmu.armv8_cache);
308 
309  retval = dpm->prepare(dpm);
310  if (retval != ERROR_OK)
311  goto done;
312 
313  /* check if we're in an unprivileged mode */
315  retval = armv8_dpm_modeswitch(dpm, ARMV8_64_EL1H);
316  if (retval != ERROR_OK)
317  return retval;
318  }
319 
320  /* retrieve CTR */
321  retval = dpm->instr_read_data_r0(dpm,
322  armv8_opcode(armv8, READ_REG_CTR), &ctr);
323  if (retval != ERROR_OK)
324  goto done;
325 
326  cache->iminline = 4UL << (ctr & 0xf);
327  cache->dminline = 4UL << ((ctr & 0xf0000) >> 16);
328  LOG_DEBUG("ctr %" PRIx32 " ctr.iminline %" PRIu32 " ctr.dminline %" PRIu32,
329  ctr, cache->iminline, cache->dminline);
330 
331  /* retrieve CLIDR */
332  retval = dpm->instr_read_data_r0(dpm,
333  armv8_opcode(armv8, READ_REG_CLIDR), &clidr);
334  if (retval != ERROR_OK)
335  goto done;
336 
337  cache->loc = (clidr & 0x7000000) >> 24;
338  LOG_DEBUG("Number of cache levels to PoC %" PRId32, cache->loc);
339 
340  /* retrieve selected cache for later restore
341  * MRC p15, 2,<Rd>, c0, c0, 0; Read CSSELR */
342  retval = dpm->instr_read_data_r0(dpm,
343  armv8_opcode(armv8, READ_REG_CSSELR), &csselr);
344  if (retval != ERROR_OK)
345  goto done;
346 
347  /* retrieve all available inner caches */
348  for (cl = 0; cl < cache->loc; clidr >>= 3, cl++) {
349 
350  /* isolate cache type at current level */
351  ctype = clidr & 7;
352 
353  /* skip reserved values */
354  if (ctype > CACHE_LEVEL_HAS_UNIFIED_CACHE)
355  continue;
356 
357  /* separate d or unified d/i cache at this level ? */
359  /* retrieve d-cache info */
360  retval = get_cache_info(dpm, cl, 0, &cache_reg);
361  if (retval != ERROR_OK)
362  goto done;
363  cache->arch[cl].d_u_size = decode_cache_reg(cache_reg);
364 
365  LOG_DEBUG("data/unified cache index %" PRIu32 " << %" PRIu32 ", way %" PRIu32 " << %" PRIu32,
366  cache->arch[cl].d_u_size.index,
367  cache->arch[cl].d_u_size.index_shift,
368  cache->arch[cl].d_u_size.way,
369  cache->arch[cl].d_u_size.way_shift);
370 
371  LOG_DEBUG("cacheline %" PRIu32 " bytes %" PRIu32 " KBytes asso %" PRIu32 " ways",
372  cache->arch[cl].d_u_size.linelen,
373  cache->arch[cl].d_u_size.cachesize,
374  cache->arch[cl].d_u_size.associativity);
375  }
376 
377  /* separate i-cache at this level ? */
378  if (ctype & CACHE_LEVEL_HAS_I_CACHE) {
379  /* retrieve i-cache info */
380  retval = get_cache_info(dpm, cl, 1, &cache_reg);
381  if (retval != ERROR_OK)
382  goto done;
383  cache->arch[cl].i_size = decode_cache_reg(cache_reg);
384 
385  LOG_DEBUG("instruction cache index %" PRIu32 " << %" PRIu32 ", way %" PRIu32 " << %" PRIu32,
386  cache->arch[cl].i_size.index,
387  cache->arch[cl].i_size.index_shift,
388  cache->arch[cl].i_size.way,
389  cache->arch[cl].i_size.way_shift);
390 
391  LOG_DEBUG("cacheline %" PRIu32 " bytes %" PRIu32 " KBytes asso %" PRIu32 " ways",
392  cache->arch[cl].i_size.linelen,
393  cache->arch[cl].i_size.cachesize,
394  cache->arch[cl].i_size.associativity);
395  }
396 
397  cache->arch[cl].ctype = ctype;
398  }
399 
400  /* restore selected cache */
401  dpm->instr_write_data_r0(dpm,
402  armv8_opcode(armv8, WRITE_REG_CSSELR), csselr);
403  if (retval != ERROR_OK)
404  goto done;
405 
406  armv8->armv8_mmu.armv8_cache.info = 1;
407 
408  /* if no l2 cache initialize l1 data cache flush function function */
414  }
415 
416 done:
418  dpm->finish(dpm);
419  return retval;
420 
421 }
@ 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:238
static unsigned int armv8_curel_from_core_mode(enum arm_mode core_mode)
Definition: armv8.h:307
static int armv8_handle_inner_cache_info_command(struct command_invocation *cmd, struct armv8_cache_common *armv8_cache)
Definition: armv8_cache.c:182
int armv8_identify_cache(struct armv8_common *armv8)
Definition: armv8_cache.c:298
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:256
#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:225
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:144
static int armv8_flush_all_data(struct target *target)
Definition: armv8_cache.c:230
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:275
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
@ 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:443
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_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
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:165
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:187
struct arm_dpm dpm
Definition: armv8.h:191
struct armv8_mmu_common armv8_mmu
Definition: armv8.h:209
struct armv8_cache_common armv8_cache
Definition: armv8.h:180
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