source: rtems/bsps/arm/shared/cache/cache-cp15.h @ 3200c300

5
Last change on this file since 3200c300 was 4cf93658, checked in by Sebastian Huber <sebastian.huber@…>, on 01/27/18 at 13:37:51

bsps: Rework cache manager implementation

The previous cache manager support used a single souce file
(cache_manager.c) which included an implementation header (cache_.h).
This required the use of specialized include paths to find the right
header file. Change this to include a generic implementation header
(cacheimpl.h) in specialized source files.

Use the following directories and files:

  • bsps/shared/cache
  • bsps/@RTEMS_CPU@/shared/cache
  • bsps/@RTEMS_CPU@/@RTEMS_BSP_FAMILY/start/cache.c

Update #3285.

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/**
2 * @ingroup arm_shared
3 *
4 * @brief Level 1 Cache definitions and functions.
5 *
6 * This file implements handling for the ARM Level 1 cache controller
7 */
8
9/*
10 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#ifndef LIBBSP_ARM_SHARED_CACHE_L1_H
24#define LIBBSP_ARM_SHARED_CACHE_L1_H
25
26#include <bsp.h>
27#include <libcpu/arm-cp15.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif /* __cplusplus */
32
33/* These two defines also ensure that the rtems_cache_* functions have bodies */
34#define ARM_CACHE_L1_CPU_DATA_ALIGNMENT 32
35#define ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT 32
36#define ARM_CACHE_L1_CPU_SUPPORT_PROVIDES_RANGE_FUNCTIONS
37
38#define ARM_CACHE_L1_CSS_ID_DATA \
39          (ARM_CP15_CACHE_CSS_ID_DATA | ARM_CP15_CACHE_CSS_LEVEL(0))
40#define ARM_CACHE_L1_CSS_ID_INSTRUCTION \
41          (ARM_CP15_CACHE_CSS_ID_INSTRUCTION | ARM_CP15_CACHE_CSS_LEVEL(0))
42#define ARM_CACHE_L1_DATA_LINE_MASK ( ARM_CACHE_L1_CPU_DATA_ALIGNMENT - 1 )
43#define ARM_CACHE_L1_INSTRUCTION_LINE_MASK \
44  ( ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT \
45    - 1 )
46
47/* Errata Handlers */
48static void arm_cache_l1_errata_764369_handler( void )
49{
50#ifdef RTEMS_SMP
51  _ARM_Data_synchronization_barrier();
52#endif
53}
54
55/*
56 * @param l1LineSize      Number of bytes in cache line expressed as power of
57 *                        2 value
58 * @param l1Associativity Associativity of cache. The associativity does not
59 *                        have to be a power of 2.
60 * qparam liNumSets       Number of sets in cache
61 * */
62
63static inline void arm_cache_l1_properties_for_level(
64  uint32_t *l1LineSize,
65  uint32_t *l1Associativity,
66  uint32_t *l1NumSets,
67  uint32_t level_and_inst_dat
68)
69{
70  uint32_t ccsidr;
71
72  ccsidr = arm_cp15_get_cache_size_id_for_level(level_and_inst_dat);
73
74  /* Cache line size in words + 2 -> bytes) */
75  *l1LineSize      = arm_ccsidr_get_line_power(ccsidr);
76  /* Number of Ways */
77  *l1Associativity = arm_ccsidr_get_associativity(ccsidr);
78  /* Number of Sets */
79  *l1NumSets       = arm_ccsidr_get_num_sets(ccsidr);
80}
81
82/*
83 * @param log_2_line_bytes The number of bytes per cache line expressed in log2
84 * @param associativity    The associativity of the cache beeing operated
85 * @param cache_level_idx  The level of the cache beeing operated minus 1 e.g 0
86 *                         for cache level 1
87 * @param set              Number of the set to operate on
88 * @param way              Number of the way to operate on
89 * */
90
91static inline uint32_t arm_cache_l1_get_set_way_param(
92  const uint32_t log_2_line_bytes,
93  const uint32_t associativity,
94  const uint32_t cache_level_idx,
95  const uint32_t set,
96  const uint32_t way )
97{
98  uint32_t way_shift = __builtin_clz( associativity - 1 );
99
100
101  return ( 0
102           | ( way
103    << way_shift ) | ( set << log_2_line_bytes ) | ( cache_level_idx << 1 ) );
104}
105
106static inline void arm_cache_l1_flush_1_data_line( const void *d_addr )
107{
108  /* Flush the Data cache */
109  arm_cp15_data_cache_clean_and_invalidate_line( d_addr );
110
111  /* Wait for L1 flush to complete */
112  _ARM_Data_synchronization_barrier();
113}
114
115static inline void arm_cache_l1_flush_entire_data( void )
116{
117  uint32_t l1LineSize, l1Associativity, l1NumSets;
118  uint32_t s, w;
119  uint32_t set_way_param;
120
121  /* ensure ordering with previous memory accesses */
122  _ARM_Data_memory_barrier();
123
124  /* Get the L1 cache properties */
125  arm_cache_l1_properties_for_level( &l1LineSize,
126                    &l1Associativity, &l1NumSets,
127                    ARM_CACHE_L1_CSS_ID_DATA);
128
129  for ( w = 0; w < l1Associativity; ++w ) {
130    for ( s = 0; s < l1NumSets; ++s ) {
131      set_way_param = arm_cache_l1_get_set_way_param(
132        l1LineSize,
133        l1Associativity,
134        0,
135        s,
136        w
137        );
138      arm_cp15_data_cache_clean_line_by_set_and_way( set_way_param );
139    }
140  }
141
142  /* Wait for L1 flush to complete */
143  _ARM_Data_synchronization_barrier();
144}
145
146static inline void arm_cache_l1_invalidate_entire_data( void )
147{
148  uint32_t l1LineSize, l1Associativity, l1NumSets;
149  uint32_t s, w;
150  uint32_t set_way_param;
151
152  /* ensure ordering with previous memory accesses */
153  _ARM_Data_memory_barrier();
154
155  /* Get the L1 cache properties */
156  arm_cache_l1_properties_for_level( &l1LineSize,
157                    &l1Associativity, &l1NumSets,
158                    ARM_CACHE_L1_CSS_ID_DATA);
159
160  for ( w = 0; w < l1Associativity; ++w ) {
161    for ( s = 0; s < l1NumSets; ++s ) {
162      set_way_param = arm_cache_l1_get_set_way_param(
163        l1LineSize,
164        l1Associativity,
165        0,
166        s,
167        w
168        );
169      arm_cp15_data_cache_invalidate_line_by_set_and_way( set_way_param );
170    }
171  }
172
173  /* Wait for L1 invalidate to complete */
174  _ARM_Data_synchronization_barrier();
175}
176
177static inline void arm_cache_l1_clean_and_invalidate_entire_data( void )
178{
179  uint32_t l1LineSize, l1Associativity, l1NumSets;
180  uint32_t s, w;
181  uint32_t set_way_param;
182
183  /* ensure ordering with previous memory accesses */
184  _ARM_Data_memory_barrier();
185
186
187  /* Get the L1 cache properties */
188  arm_cache_l1_properties_for_level( &l1LineSize,
189                    &l1Associativity, &l1NumSets,
190                    ARM_CACHE_L1_CSS_ID_DATA);
191
192  for ( w = 0; w < l1Associativity; ++w ) {
193    for ( s = 0; s < l1NumSets; ++s ) {
194      set_way_param = arm_cache_l1_get_set_way_param(
195        l1LineSize,
196        l1Associativity,
197        0,
198        s,
199        w
200        );
201      arm_cp15_data_cache_clean_and_invalidate_line_by_set_and_way(
202        set_way_param );
203    }
204  }
205
206  /* Wait for L1 invalidate to complete */
207  _ARM_Data_synchronization_barrier();
208}
209
210static inline void arm_cache_l1_flush_data_range(
211  const void *d_addr,
212  size_t      n_bytes
213)
214{
215  if ( n_bytes != 0 ) {
216    uint32_t       adx       = (uint32_t) d_addr
217                               & ~ARM_CACHE_L1_DATA_LINE_MASK;
218    const uint32_t ADDR_LAST =
219      (uint32_t)( (size_t) d_addr + n_bytes - 1 );
220
221    arm_cache_l1_errata_764369_handler();
222
223    for (; adx <= ADDR_LAST; adx += ARM_CACHE_L1_CPU_DATA_ALIGNMENT ) {
224      /* Store and invalidate the Data cache line */
225      arm_cp15_data_cache_clean_and_invalidate_line( (void*)adx );
226    }
227    /* Wait for L1 store to complete */
228    _ARM_Data_synchronization_barrier();
229  }
230}
231
232
233static inline void arm_cache_l1_invalidate_1_data_line(
234  const void *d_addr )
235{
236  /* Invalidate the data cache line */
237  arm_cp15_data_cache_invalidate_line( d_addr );
238
239  /* Wait for L1 invalidate to complete */
240  _ARM_Data_synchronization_barrier();
241}
242
243static inline void arm_cache_l1_freeze_data( void )
244{
245  /* To be implemented as needed, if supported by hardware at all */
246}
247
248static inline void arm_cache_l1_unfreeze_data( void )
249{
250  /* To be implemented as needed, if supported by hardware at all */
251}
252
253static inline void arm_cache_l1_invalidate_1_instruction_line(
254  const void *i_addr )
255{
256  /* Invalidate the Instruction cache line */
257  arm_cp15_instruction_cache_invalidate_line( i_addr );
258
259  /* Wait for L1 invalidate to complete */
260  _ARM_Data_synchronization_barrier();
261}
262
263static inline void arm_cache_l1_invalidate_data_range(
264  const void *d_addr,
265  size_t      n_bytes
266)
267{
268  if ( n_bytes != 0 ) {
269    uint32_t       adx = (uint32_t) d_addr
270                         & ~ARM_CACHE_L1_DATA_LINE_MASK;
271    const uint32_t end =
272      (uint32_t)( (size_t)d_addr + n_bytes -1);
273
274    arm_cache_l1_errata_764369_handler();
275   
276    /* Back starting address up to start of a line and invalidate until end */
277    for (;
278         adx <= end;
279         adx += ARM_CACHE_L1_CPU_DATA_ALIGNMENT ) {
280        /* Invalidate the Instruction cache line */
281        arm_cp15_data_cache_invalidate_line( (void*)adx );
282    }
283    /* Wait for L1 invalidate to complete */
284    _ARM_Data_synchronization_barrier();
285  }
286}
287
288static inline void arm_cache_l1_invalidate_instruction_range(
289  const void *i_addr,
290  size_t      n_bytes
291)
292{
293  if ( n_bytes != 0 ) {
294    uint32_t       adx = (uint32_t) i_addr
295                         & ~ARM_CACHE_L1_INSTRUCTION_LINE_MASK;
296    const uint32_t end =
297      (uint32_t)( (size_t)i_addr + n_bytes -1);
298
299    arm_cache_l1_errata_764369_handler();
300
301    /* Back starting address up to start of a line and invalidate until end */
302    for (;
303         adx <= end;
304         adx += ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT ) {
305        /* Invalidate the Instruction cache line */
306        arm_cp15_instruction_cache_invalidate_line( (void*)adx );
307    }
308    /* Wait for L1 invalidate to complete */
309    _ARM_Data_synchronization_barrier();
310  }
311}
312
313static inline void arm_cache_l1_invalidate_entire_instruction( void )
314{
315  uint32_t ctrl = arm_cp15_get_control();
316
317
318  #ifdef RTEMS_SMP
319
320  /* invalidate I-cache inner shareable */
321  arm_cp15_instruction_cache_inner_shareable_invalidate_all();
322
323  /* I+BTB cache invalidate */
324  arm_cp15_instruction_cache_invalidate();
325  #else /* RTEMS_SMP */
326  /* I+BTB cache invalidate */
327  arm_cp15_instruction_cache_invalidate();
328  #endif /* RTEMS_SMP */
329
330  if ( ( ctrl & ARM_CP15_CTRL_Z ) != 0 ) {
331    #if defined(__ARM_ARCH_7A__)
332    arm_cp15_branch_predictor_inner_shareable_invalidate_all();
333    #endif
334    #if defined(__ARM_ARCH_6KZ__) || defined(__ARM_ARCH_7A__)
335    arm_cp15_branch_predictor_invalidate_all();
336    #endif
337  }
338}
339
340static inline void arm_cache_l1_freeze_instruction( void )
341{
342  /* To be implemented as needed, if supported by hardware at all */
343}
344
345static inline void arm_cache_l1_unfreeze_instruction( void )
346{
347  /* To be implemented as needed, if supported by hardware at all */
348}
349
350static inline void arm_cache_l1_disable_data( void )
351{
352  /* Clean and invalidate the Data cache */
353  arm_cache_l1_flush_entire_data();
354
355  /* Disable the Data cache */
356  arm_cp15_set_control( arm_cp15_get_control() & ~ARM_CP15_CTRL_C );
357}
358
359static inline void arm_cache_l1_disable_instruction( void )
360{
361  /* Synchronize the processor */
362  _ARM_Data_synchronization_barrier();
363
364  /* Invalidate the Instruction cache */
365  arm_cache_l1_invalidate_entire_instruction();
366
367  /* Disable the Instruction cache */
368  arm_cp15_set_control( arm_cp15_get_control() & ~ARM_CP15_CTRL_I );
369}
370
371static inline size_t arm_cache_l1_get_data_cache_size( void )
372{
373  size_t   size;
374  uint32_t line_size     = 0;
375  uint32_t associativity = 0;
376  uint32_t num_sets      = 0;
377
378  arm_cache_l1_properties_for_level( &line_size,
379                    &associativity, &num_sets,
380                    ARM_CACHE_L1_CSS_ID_DATA);
381
382  size = (1 << line_size) * associativity * num_sets;
383
384  return size;
385}
386
387static inline size_t arm_cache_l1_get_instruction_cache_size( void )
388{
389  size_t   size;
390  uint32_t line_size     = 0;
391  uint32_t associativity = 0;
392  uint32_t num_sets      = 0;
393
394  arm_cache_l1_properties_for_level( &line_size,
395                    &associativity, &num_sets,
396                    ARM_CACHE_L1_CSS_ID_INSTRUCTION);
397
398  size = (1 << line_size) * associativity * num_sets;
399
400  return size;
401}
402
403#ifdef __cplusplus
404}
405#endif /* __cplusplus */
406
407#endif /* LIBBSP_ARM_SHARED_CACHE_L1_H */
Note: See TracBrowser for help on using the repository browser.