1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ClassicCache |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
9 | * |
---|
10 | * embedded brains GmbH |
---|
11 | * Dornierstr. 4 |
---|
12 | * 82178 Puchheim |
---|
13 | * Germany |
---|
14 | * <rtems@embedded-brains.de> |
---|
15 | * |
---|
16 | * The license and distribution terms for this file may be |
---|
17 | * found in the file LICENSE in this distribution or at |
---|
18 | * http://www.rtems.org/license/LICENSE. |
---|
19 | */ |
---|
20 | |
---|
21 | #if HAVE_CONFIG_H |
---|
22 | #include "config.h" |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/malloc.h> |
---|
27 | #include <rtems/score/apimutex.h> |
---|
28 | #include <rtems/score/heapimpl.h> |
---|
29 | #include <rtems/score/sysstate.h> |
---|
30 | |
---|
31 | static Heap_Control cache_coherent_heap_instance; |
---|
32 | |
---|
33 | static Heap_Control *cache_coherent_heap; |
---|
34 | |
---|
35 | void *rtems_cache_coherent_allocate( |
---|
36 | size_t size, |
---|
37 | uintptr_t alignment, |
---|
38 | uintptr_t boundary |
---|
39 | ) |
---|
40 | { |
---|
41 | void *ptr; |
---|
42 | Heap_Control *heap; |
---|
43 | |
---|
44 | _RTEMS_Lock_allocator(); |
---|
45 | |
---|
46 | heap = cache_coherent_heap; |
---|
47 | if ( heap == NULL ) { |
---|
48 | heap = RTEMS_Malloc_Heap; |
---|
49 | } |
---|
50 | |
---|
51 | ptr = _Heap_Allocate_aligned_with_boundary( |
---|
52 | heap, |
---|
53 | size, |
---|
54 | alignment, |
---|
55 | boundary |
---|
56 | ); |
---|
57 | |
---|
58 | _RTEMS_Unlock_allocator(); |
---|
59 | |
---|
60 | return ptr; |
---|
61 | } |
---|
62 | |
---|
63 | void rtems_cache_coherent_free( void *ptr ) |
---|
64 | { |
---|
65 | Heap_Control *heap; |
---|
66 | |
---|
67 | _RTEMS_Lock_allocator(); |
---|
68 | |
---|
69 | heap = cache_coherent_heap; |
---|
70 | if ( heap != NULL ) { |
---|
71 | if ( _Heap_Free( heap, ptr ) ) { |
---|
72 | heap = NULL; |
---|
73 | } else { |
---|
74 | heap = RTEMS_Malloc_Heap; |
---|
75 | } |
---|
76 | } else { |
---|
77 | heap = RTEMS_Malloc_Heap; |
---|
78 | } |
---|
79 | |
---|
80 | if ( heap != NULL ) { |
---|
81 | _Heap_Free( heap, ptr ); |
---|
82 | } |
---|
83 | |
---|
84 | _RTEMS_Unlock_allocator(); |
---|
85 | } |
---|
86 | |
---|
87 | static void add_area( |
---|
88 | void *area_begin, |
---|
89 | uintptr_t area_size |
---|
90 | ) |
---|
91 | { |
---|
92 | Heap_Control *heap = cache_coherent_heap; |
---|
93 | |
---|
94 | if ( heap == NULL ) { |
---|
95 | bool ok; |
---|
96 | |
---|
97 | heap = &cache_coherent_heap_instance; |
---|
98 | |
---|
99 | ok = _Heap_Initialize( heap, area_begin, area_size, 0 ); |
---|
100 | if ( ok ) { |
---|
101 | cache_coherent_heap = heap; |
---|
102 | } |
---|
103 | } else { |
---|
104 | _Heap_Extend( heap, area_begin, area_size, 0 ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | void rtems_cache_coherent_add_area( |
---|
109 | void *area_begin, |
---|
110 | uintptr_t area_size |
---|
111 | ) |
---|
112 | { |
---|
113 | if ( _System_state_Is_up( _System_state_Get()) ) { |
---|
114 | _RTEMS_Lock_allocator(); |
---|
115 | |
---|
116 | add_area( area_begin, area_size ); |
---|
117 | |
---|
118 | _RTEMS_Unlock_allocator(); |
---|
119 | } else { |
---|
120 | add_area( area_begin, area_size ); |
---|
121 | } |
---|
122 | } |
---|