source: rtems/c/src/lib/libbsp/arm/shared/include/arm-cache-l1.h @ 579f16ef

5
Last change on this file since 579f16ef was 579f16ef, checked in by Sebastian Huber <sebastian.huber@…>, on 07/21/15 at 07:29:56

bsps/arm: Update due to API changes

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