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