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