]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseCacheMaintenanceLib/x86Cache.c
9cb5589297d4eea4dc9ef1dc8a71363c0fbe5eec
[mirror_edk2.git] / MdePkg / Library / BaseCacheMaintenanceLib / x86Cache.c
1 /** @file
2 Cache Maintenance Functions.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name: x86Cache.c
14
15 **/
16
17 //
18 // Include common header file for this module.
19 //
20 #include <Base.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23
24 //
25 // This size must be at or below the smallest cache size possible among all
26 // supported processors
27 //
28 #define CACHE_LINE_SIZE 0x20
29
30 /**
31 Invalidates the entire instruction cache in cache coherency domain of the
32 calling CPU.
33
34 Invalidates the entire instruction cache in cache coherency domain of the
35 calling CPU.
36
37 **/
38 VOID
39 EFIAPI
40 InvalidateInstructionCache (
41 VOID
42 )
43 {
44 }
45
46 /**
47 Invalidates a range of instruction cache lines in the cache coherency domain
48 of the calling CPU.
49
50 Invalidates the instruction cache lines specified by Address and Length. If
51 Address is not aligned on a cache line boundary, then entire instruction
52 cache line containing Address is invalidated. If Address + Length is not
53 aligned on a cache line boundary, then the entire instruction cache line
54 containing Address + Length -1 is invalidated. This function may choose to
55 invalidate the entire instruction cache if that is more efficient than
56 invalidating the specified range. If Length is 0, the no instruction cache
57 lines are invalidated. Address is returned.
58
59 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().
60
61 @param Address The base address of the instruction cache lines to
62 invalidate. If the CPU is in a physical addressing mode, then
63 Address is a physical address. If the CPU is in a virtual
64 addressing mode, then Address is a virtual address.
65
66 @param Length The number of bytes to invalidate from the instruction cache.
67
68 @return Address
69
70 **/
71 VOID *
72 EFIAPI
73 InvalidateInstructionCacheRange (
74 IN VOID *Address,
75 IN UINTN Length
76 )
77 {
78 ASSERT (Length <= MAX_ADDRESS - (UINTN)Address + 1);
79 return Address;
80 }
81
82 /**
83 Writes Back and Invalidates the entire data cache in cache coherency domain
84 of the calling CPU.
85
86 Writes Back and Invalidates the entire data cache in cache coherency domain
87 of the calling CPU. This function guarantees that all dirty cache lines are
88 written back to system memory, and also invalidates all the data cache lines
89 in the cache coherency domain of the calling CPU.
90
91 **/
92 VOID
93 EFIAPI
94 WriteBackInvalidateDataCache (
95 VOID
96 )
97 {
98 AsmWbinvd ();
99 }
100
101 /**
102 Writes Back and Invalidates a range of data cache lines in the cache
103 coherency domain of the calling CPU.
104
105 Writes Back and Invalidate the data cache lines specified by Address and
106 Length. If Address is not aligned on a cache line boundary, then entire data
107 cache line containing Address is written back and invalidated. If Address +
108 Length is not aligned on a cache line boundary, then the entire data cache
109 line containing Address + Length -1 is written back and invalidated. This
110 function may choose to write back and invalidate the entire data cache if
111 that is more efficient than writing back and invalidating the specified
112 range. If Length is 0, the no data cache lines are written back and
113 invalidated. Address is returned.
114
115 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().
116
117 @param Address The base address of the data cache lines to write back and
118 invalidate. If the CPU is in a physical addressing mode, then
119 Address is a physical address. If the CPU is in a virtual
120 addressing mode, then Address is a virtual address.
121 @param Length The number of bytes to write back and invalidate from the
122 data cache.
123
124 @return Address
125
126 **/
127 VOID *
128 EFIAPI
129 WriteBackInvalidateDataCacheRange (
130 IN VOID *Address,
131 IN UINTN Length
132 )
133 {
134 UINTN Start, End;
135
136 ASSERT (Length <= MAX_ADDRESS - (UINTN)Address + 1);
137
138 if (Length == 0) {
139 return Address;
140 }
141
142 Start = (UINTN)Address;
143 End = (Start + Length + (CACHE_LINE_SIZE - 1)) & ~(CACHE_LINE_SIZE - 1);
144 Start &= ~(CACHE_LINE_SIZE - 1);
145
146 do {
147 Start = (UINTN)AsmFlushCacheLine ((VOID*)Start) + CACHE_LINE_SIZE;
148 } while (Start != End);
149 return Address;
150 }
151
152 /**
153 Writes Back the entire data cache in cache coherency domain of the calling
154 CPU.
155
156 Writes Back the entire data cache in cache coherency domain of the calling
157 CPU. This function guarantees that all dirty cache lines are written back to
158 system memory. This function may also invalidate all the data cache lines in
159 the cache coherency domain of the calling CPU.
160
161 **/
162 VOID
163 EFIAPI
164 WriteBackDataCache (
165 VOID
166 )
167 {
168 WriteBackInvalidateDataCache ();
169 }
170
171 /**
172 Writes Back a range of data cache lines in the cache coherency domain of the
173 calling CPU.
174
175 Writes Back the data cache lines specified by Address and Length. If Address
176 is not aligned on a cache line boundary, then entire data cache line
177 containing Address is written back. If Address + Length is not aligned on a
178 cache line boundary, then the entire data cache line containing Address +
179 Length -1 is written back. This function may choose to write back the entire
180 data cache if that is more efficient than writing back the specified range.
181 If Length is 0, the no data cache lines are written back. This function may
182 also invalidate all the data cache lines in the specified range of the cache
183 coherency domain of the calling CPU. Address is returned.
184
185 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().
186
187 @param Address The base address of the data cache lines to write back. If
188 the CPU is in a physical addressing mode, then Address is a
189 physical address. If the CPU is in a virtual addressing
190 mode, then Address is a virtual address.
191 @param Length The number of bytes to write back from the data cache.
192
193 @return Address
194
195 **/
196 VOID *
197 EFIAPI
198 WriteBackDataCacheRange (
199 IN VOID *Address,
200 IN UINTN Length
201 )
202 {
203 return WriteBackInvalidateDataCacheRange (Address, Length);
204 }
205
206 /**
207 Invalidates the entire data cache in cache coherency domain of the calling
208 CPU.
209
210 Invalidates the entire data cache in cache coherency domain of the calling
211 CPU. This function must be used with care because dirty cache lines are not
212 written back to system memory. It is typically used for cache diagnostics. If
213 the CPU does not support invalidation of the entire data cache, then a write
214 back and invalidate operation should be performed on the entire data cache.
215
216 **/
217 VOID
218 EFIAPI
219 InvalidateDataCache (
220 VOID
221 )
222 {
223 AsmInvd ();
224 }
225
226 /**
227 Invalidates a range of data cache lines in the cache coherency domain of the
228 calling CPU.
229
230 Invalidates the data cache lines specified by Address and Length. If Address
231 is not aligned on a cache line boundary, then entire data cache line
232 containing Address is invalidated. If Address + Length is not aligned on a
233 cache line boundary, then the entire data cache line containing Address +
234 Length -1 is invalidated. This function must never invalidate any cache lines
235 outside the specified range. If Length is 0, the no data cache lines are
236 invalidated. Address is returned. This function must be used with care
237 because dirty cache lines are not written back to system memory. It is
238 typically used for cache diagnostics. If the CPU does not support
239 invalidation of a data cache range, then a write back and invalidate
240 operation should be performed on the data cache range.
241
242 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().
243
244 @param Address The base address of the data cache lines to invalidate. If
245 the CPU is in a physical addressing mode, then Address is a
246 physical address. If the CPU is in a virtual addressing mode,
247 then Address is a virtual address.
248 @param Length The number of bytes to invalidate from the data cache.
249
250 @return Address
251
252 **/
253 VOID *
254 EFIAPI
255 InvalidateDataCacheRange (
256 IN VOID *Address,
257 IN UINTN Length
258 )
259 {
260 return WriteBackInvalidateDataCacheRange (Address, Length);
261 }