]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseCacheMaintenanceLib/x86Cache.c
1) Add a stringent check to make sure the package list for UpdateForm call must have...
[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
38bbd3d9 67 @return Address of cache invalidation.\r
e1f414b6 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
38bbd3d9 123 @return Address of cache invalidation.\r
e1f414b6 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
38bbd3d9 142 //\r
143 // Calculate the cache line alignment\r
144 // \r
e1f414b6 145 End = (Start + Length + (CACHE_LINE_SIZE - 1)) & ~(CACHE_LINE_SIZE - 1);\r
146 Start &= ~(CACHE_LINE_SIZE - 1);\r
147\r
148 do {\r
149 Start = (UINTN)AsmFlushCacheLine ((VOID*)Start) + CACHE_LINE_SIZE;\r
150 } while (Start != End);\r
151 return Address;\r
152}\r
153\r
154/**\r
155 Writes Back the entire data cache in cache coherency domain of the calling\r
156 CPU.\r
157\r
158 Writes Back the entire data cache in cache coherency domain of the calling\r
159 CPU. This function guarantees that all dirty cache lines are written back to\r
160 system memory. This function may also invalidate all the data cache lines in\r
161 the cache coherency domain of the calling CPU.\r
162\r
163**/\r
164VOID\r
165EFIAPI\r
166WriteBackDataCache (\r
167 VOID\r
168 )\r
169{\r
170 WriteBackInvalidateDataCache ();\r
171}\r
172\r
173/**\r
174 Writes Back a range of data cache lines in the cache coherency domain of the\r
175 calling CPU.\r
176\r
177 Writes Back the data cache lines specified by Address and Length. If Address\r
178 is not aligned on a cache line boundary, then entire data cache line\r
179 containing Address is written back. If Address + Length is not aligned on a\r
180 cache line boundary, then the entire data cache line containing Address +\r
181 Length -1 is written back. This function may choose to write back the entire\r
182 data cache if that is more efficient than writing back the specified range.\r
183 If Length is 0, the no data cache lines are written back. This function may\r
184 also invalidate all the data cache lines in the specified range of the cache\r
185 coherency domain of the calling CPU. Address is returned.\r
186\r
187 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().\r
188\r
189 @param Address The base address of the data cache lines to write back. If\r
190 the CPU is in a physical addressing mode, then Address is a\r
191 physical address. If the CPU is in a virtual addressing\r
192 mode, then Address is a virtual address.\r
193 @param Length The number of bytes to write back from the data cache.\r
194\r
38bbd3d9 195 @return Address of cache wrote in main memory.\r
e1f414b6 196\r
197**/\r
198VOID *\r
199EFIAPI\r
200WriteBackDataCacheRange (\r
201 IN VOID *Address,\r
202 IN UINTN Length\r
203 )\r
204{\r
205 return WriteBackInvalidateDataCacheRange (Address, Length);\r
206}\r
207\r
208/**\r
209 Invalidates the entire data cache in cache coherency domain of the calling\r
210 CPU.\r
211\r
212 Invalidates the entire data cache in cache coherency domain of the calling\r
213 CPU. This function must be used with care because dirty cache lines are not\r
214 written back to system memory. It is typically used for cache diagnostics. If\r
215 the CPU does not support invalidation of the entire data cache, then a write\r
216 back and invalidate operation should be performed on the entire data cache.\r
217\r
218**/\r
219VOID\r
220EFIAPI\r
221InvalidateDataCache (\r
222 VOID\r
223 )\r
224{\r
225 AsmInvd ();\r
226}\r
227\r
228/**\r
229 Invalidates a range of data cache lines in the cache coherency domain of the\r
230 calling CPU.\r
231\r
232 Invalidates the data cache lines specified by Address and Length. If Address\r
233 is not aligned on a cache line boundary, then entire data cache line\r
234 containing Address is invalidated. If Address + Length is not aligned on a\r
235 cache line boundary, then the entire data cache line containing Address +\r
236 Length -1 is invalidated. This function must never invalidate any cache lines\r
237 outside the specified range. If Length is 0, the no data cache lines are\r
238 invalidated. Address is returned. This function must be used with care\r
239 because dirty cache lines are not written back to system memory. It is\r
240 typically used for cache diagnostics. If the CPU does not support\r
241 invalidation of a data cache range, then a write back and invalidate\r
242 operation should be performed on the data cache range.\r
243\r
244 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().\r
245\r
246 @param Address The base address of the data cache lines to invalidate. If\r
247 the CPU is in a physical addressing mode, then Address is a\r
248 physical address. If the CPU is in a virtual addressing mode,\r
249 then Address is a virtual address.\r
250 @param Length The number of bytes to invalidate from the data cache.\r
251\r
38bbd3d9 252 @return Address of cache invalidation.\r
e1f414b6 253\r
254**/\r
255VOID *\r
256EFIAPI\r
257InvalidateDataCacheRange (\r
258 IN VOID *Address,\r
259 IN UINTN Length\r
260 )\r
261{\r
262 return WriteBackInvalidateDataCacheRange (Address, Length);\r
263}\r