]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseCacheMaintenanceLib/x86Cache.c
Removed CommonHeader.h generated file from the MdePkg.
[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
13 Module Name: x86Cache.c\r
14\r
15**/\r
16\r
17//\r
18// Include common header file for this module.\r
19//\r
f734a10a
A
20#include <Base.h>\r
21#include <Library/BaseLib.h>\r
22#include <Library/DebugLib.h>\r
e1f414b6 23\r
24//\r
25// This size must be at or below the smallest cache size possible among all\r
26// supported processors\r
27//\r
28#define CACHE_LINE_SIZE 0x20\r
29\r
30/**\r
31 Invalidates the entire instruction cache in cache coherency domain of the\r
32 calling CPU.\r
33\r
34 Invalidates the entire instruction cache in cache coherency domain of the\r
35 calling CPU.\r
36\r
37**/\r
38VOID\r
39EFIAPI\r
40InvalidateInstructionCache (\r
41 VOID\r
42 )\r
43{\r
44}\r
45\r
46/**\r
47 Invalidates a range of instruction cache lines in the cache coherency domain\r
48 of the calling CPU.\r
49\r
50 Invalidates the instruction cache lines specified by Address and Length. If\r
51 Address is not aligned on a cache line boundary, then entire instruction\r
52 cache line containing Address is invalidated. If Address + Length is not\r
53 aligned on a cache line boundary, then the entire instruction cache line\r
54 containing Address + Length -1 is invalidated. This function may choose to\r
55 invalidate the entire instruction cache if that is more efficient than\r
56 invalidating the specified range. If Length is 0, the no instruction cache\r
57 lines are invalidated. Address is returned.\r
58\r
59 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().\r
60\r
61 @param Address The base address of the instruction cache lines to\r
62 invalidate. If the CPU is in a physical addressing mode, then\r
63 Address is a physical address. If the CPU is in a virtual\r
64 addressing mode, then Address is a virtual address.\r
65\r
66 @param Length The number of bytes to invalidate from the instruction cache.\r
67\r
68 @return Address\r
69\r
70**/\r
71VOID *\r
72EFIAPI\r
73InvalidateInstructionCacheRange (\r
74 IN VOID *Address,\r
75 IN UINTN Length\r
76 )\r
77{\r
78 ASSERT (Length <= MAX_ADDRESS - (UINTN)Address + 1);\r
79 return Address;\r
80}\r
81\r
82/**\r
83 Writes Back and Invalidates the entire data cache in cache coherency domain\r
84 of the calling CPU.\r
85\r
86 Writes Back and Invalidates the entire data cache in cache coherency domain\r
87 of the calling CPU. This function guarantees that all dirty cache lines are\r
88 written back to system memory, and also invalidates all the data cache lines\r
89 in the cache coherency domain of the calling CPU.\r
90\r
91**/\r
92VOID\r
93EFIAPI\r
94WriteBackInvalidateDataCache (\r
95 VOID\r
96 )\r
97{\r
98 AsmWbinvd ();\r
99}\r
100\r
101/**\r
102 Writes Back and Invalidates a range of data cache lines in the cache\r
103 coherency domain of the calling CPU.\r
104\r
105 Writes Back and Invalidate the data cache lines specified by Address and\r
106 Length. If Address is not aligned on a cache line boundary, then entire data\r
107 cache line containing Address is written back and invalidated. If Address +\r
108 Length is not aligned on a cache line boundary, then the entire data cache\r
109 line containing Address + Length -1 is written back and invalidated. This\r
110 function may choose to write back and invalidate the entire data cache if\r
111 that is more efficient than writing back and invalidating the specified\r
112 range. If Length is 0, the no data cache lines are written back and\r
113 invalidated. Address is returned.\r
114\r
115 If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().\r
116\r
117 @param Address The base address of the data cache lines to write back and\r
118 invalidate. If the CPU is in a physical addressing mode, then\r
119 Address is a physical address. If the CPU is in a virtual\r
120 addressing mode, then Address is a virtual address.\r
121 @param Length The number of bytes to write back and invalidate from the\r
122 data cache.\r
123\r
124 @return Address\r
125\r
126**/\r
127VOID *\r
128EFIAPI\r
129WriteBackInvalidateDataCacheRange (\r
130 IN VOID *Address,\r
131 IN UINTN Length\r
132 )\r
133{\r
134 UINTN Start, End;\r
135\r
136 ASSERT (Length <= MAX_ADDRESS - (UINTN)Address + 1);\r
137\r
138 if (Length == 0) {\r
139 return Address;\r
140 }\r
141\r
142 Start = (UINTN)Address;\r
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
193 @return Address\r
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
250 @return Address\r
251\r
252**/\r
253VOID *\r
254EFIAPI\r
255InvalidateDataCacheRange (\r
256 IN VOID *Address,\r
257 IN UINTN Length\r
258 )\r
259{\r
260 return WriteBackInvalidateDataCacheRange (Address, Length);\r
261}\r