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