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

4.115
Last change on this file since 44fbca3 was 44fbca3, checked in by Sebastian Huber <sebastian.huber@…>, on 06/04/14 at 09:21:43

bsps/arm: Simplify L1 caches support

Delete superfluous/incorrect interrupt disable/enable.

  • Property mode set to 100644
File size: 12.1 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 <assert.h>
29#include <bsp.h>
30#include <libcpu/arm-cp15.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif /* __cplusplus */
35
36/* These two defines also ensure that the rtems_cache_* functions have bodies */
37#define ARM_CACHE_L1_CPU_DATA_ALIGNMENT 32
38#define ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT 32
39#define ARM_CACHE_L1_CPU_SUPPORT_PROVIDES_RANGE_FUNCTIONS
40
41#define ARM_CACHE_L1_CSS_ID_DATA 0
42#define ARM_CACHE_L1_CSS_ID_INSTRUCTION 1
43#define ARM_CACHE_L1_DATA_LINE_MASK ( ARM_CACHE_L1_CPU_DATA_ALIGNMENT - 1 )
44#define ARM_CACHE_L1_INSTRUCTION_LINE_MASK \
45  ( ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT \
46    - 1 )
47
48/* Errata Handlers */
49#define ARM_CACHE_L1_ERRATA_764369_HANDLER()                 \
50  if( arm_errata_is_applicable_processor_errata_764369() ) { \
51    _ARM_Data_synchronization_barrier();                     \
52  }
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_select( ARM_CACHE_L1_CSS_ID_INSTRUCTION );
305
306    ARM_CACHE_L1_ERRATA_764369_HANDLER();
307
308    /* Back starting address up to start of a line and invalidate until end */
309    for (;
310         adx <= end;
311         adx += ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT ) {
312        /* Invalidate the Instruction cache line */
313        arm_cp15_instruction_cache_invalidate_line( (void*)adx );
314    }
315    /* Wait for L1 invalidate to complete */
316    _ARM_Data_synchronization_barrier();
317
318    arm_cache_l1_select( ARM_CACHE_L1_CSS_ID_DATA );
319  }
320}
321
322static inline void arm_cache_l1_invalidate_entire_instruction( void )
323{
324  uint32_t ctrl = arm_cp15_get_control();
325
326
327  #ifdef RTEMS_SMP
328
329  /* invalidate I-cache inner shareable */
330  arm_cp15_instruction_cache_inner_shareable_invalidate_all();
331
332  /* I+BTB cache invalidate */
333  arm_cp15_instruction_cache_invalidate();
334  #else /* RTEMS_SMP */
335  /* I+BTB cache invalidate */
336  arm_cp15_instruction_cache_invalidate();
337  #endif /* RTEMS_SMP */
338
339  if ( ( ctrl & ARM_CP15_CTRL_Z ) == 0 ) {
340    arm_cp15_branch_predictor_inner_shareable_invalidate_all();
341    arm_cp15_branch_predictor_invalidate_all();
342  }
343}
344
345static inline void arm_cache_l1_freeze_instruction( void )
346{
347  /* To be implemented as needed, if supported by hardware at all */
348}
349
350static inline void arm_cache_l1_unfreeze_instruction( void )
351{
352  /* To be implemented as needed, if supported by hardware at all */
353}
354
355static inline void arm_cache_l1_enable_data( void )
356{
357  uint32_t ctrl;
358
359  arm_cache_l1_select( ARM_CACHE_L1_CSS_ID_DATA );
360
361  assert( ARM_CACHE_L1_CPU_DATA_ALIGNMENT == arm_cp15_get_data_cache_line_size() );
362
363  ctrl = arm_cp15_get_control();
364
365  /* Only enable the cache if it is disabled */
366  if ( !( ctrl & ARM_CP15_CTRL_C ) ) {
367    /* Clean and invalidate the Data cache */
368    arm_cache_l1_invalidate_entire_data();
369
370    /* Enable the Data cache */
371    ctrl |= ARM_CP15_CTRL_C;
372
373    arm_cp15_set_control( ctrl );
374  }
375}
376
377static inline void arm_cache_l1_disable_data( void )
378{
379  /* Clean and invalidate the Data cache */
380  arm_cache_l1_flush_entire_data();
381
382  /* Disable the Data cache */
383  arm_cp15_set_control( arm_cp15_get_control() & ~ARM_CP15_CTRL_C );
384}
385
386static inline void arm_cache_l1_disable_instruction( void )
387{
388  /* Synchronize the processor */
389  _ARM_Data_synchronization_barrier();
390
391  /* Invalidate the Instruction cache */
392  arm_cache_l1_invalidate_entire_instruction();
393
394  /* Disable the Instruction cache */
395  arm_cp15_set_control( arm_cp15_get_control() & ~ARM_CP15_CTRL_I );
396}
397
398static inline void arm_cache_l1_enable_instruction( void )
399{
400  uint32_t ctrl;
401
402  arm_cache_l1_select( ARM_CACHE_L1_CSS_ID_INSTRUCTION );
403
404  assert( ARM_CACHE_L1_CPU_INSTRUCTION_ALIGNMENT
405          == arm_cp15_get_data_cache_line_size() );
406
407  /* Enable Instruction cache only if it is disabled */
408  ctrl = arm_cp15_get_control();
409
410  if ( !( ctrl & ARM_CP15_CTRL_I ) ) {
411    /* Invalidate the Instruction cache */
412    arm_cache_l1_invalidate_entire_instruction();
413
414    /* Enable the Instruction cache */
415    ctrl |= ARM_CP15_CTRL_I;
416
417    arm_cp15_set_control( ctrl );
418  }
419
420  arm_cache_l1_select( ARM_CACHE_L1_CSS_ID_DATA );
421}
422
423static inline size_t arm_cache_l1_get_data_cache_size( void )
424{
425  size_t   size;
426  uint32_t line_size     = 0;
427  uint32_t associativity = 0;
428  uint32_t num_sets      = 0;
429  arm_cache_l1_properties( &line_size, &associativity,
430                           &num_sets );
431
432  size = (1 << line_size) * associativity * num_sets;
433
434  return size;
435}
436
437static inline size_t arm_cache_l1_get_instruction_cache_size( void )
438{
439  size_t   size;
440  uint32_t line_size     = 0;
441  uint32_t associativity = 0;
442  uint32_t num_sets      = 0;
443
444  arm_cache_l1_select( ARM_CACHE_L1_CSS_ID_INSTRUCTION );
445 
446  arm_cache_l1_properties( &line_size, &associativity,
447                           &num_sets );
448 
449  arm_cache_l1_select( ARM_CACHE_L1_CSS_ID_DATA );
450
451  size = (1 << line_size) * associativity * num_sets;
452 
453  return size;
454}
455
456#ifdef __cplusplus
457}
458#endif /* __cplusplus */
459
460#endif /* LIBBSP_ARM_SHARED_CACHE_L1_H */
Note: See TracBrowser for help on using the repository browser.