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