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