]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/CpuIo2Smm/CpuIo2Smm.c
UefiCpuPkg: CpuIo2Smm: Move CpuIo2Smm driver to consume gMmst
[mirror_edk2.git] / UefiCpuPkg / CpuIo2Smm / CpuIo2Smm.c
CommitLineData
173eeac9 1/** @file\r
2 Produces the SMM CPU I/O Protocol.\r
3\r
7367cc6c 4Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
173eeac9 6\r
7**/\r
8\r
430fbbe0 9#include "CpuIo2Smm.h"\r
173eeac9 10\r
11//\r
12// Handle for the SMM CPU I/O Protocol\r
13//\r
14EFI_HANDLE mHandle = NULL;\r
15\r
16//\r
17// SMM CPU I/O Protocol instance\r
18//\r
19EFI_SMM_CPU_IO2_PROTOCOL mSmmCpuIo2 = {\r
20 {\r
21 CpuMemoryServiceRead,\r
22 CpuMemoryServiceWrite\r
23 },\r
24 {\r
25 CpuIoServiceRead,\r
26 CpuIoServiceWrite\r
27 }\r
28};\r
29\r
30//\r
31// Lookup table for increment values based on transfer widths\r
32//\r
33UINT8 mStride[] = {\r
34 1, // SMM_IO_UINT8\r
35 2, // SMM_IO_UINT16\r
36 4, // SMM_IO_UINT32\r
37 8 // SMM_IO_UINT64\r
38};\r
39\r
40/**\r
41 Check parameters to a SMM CPU I/O Protocol service request.\r
42\r
bc230a23 43 @param[in] MmioOperation TRUE for an MMIO operation, FALSE for I/O Port operation.\r
44 @param[in] Width Signifies the width of the I/O operations.\r
7367cc6c
LG
45 @param[in] Address The base address of the I/O operations. The caller is\r
46 responsible for aligning the Address if required.\r
bc230a23 47 @param[in] Count The number of I/O operations to perform.\r
7367cc6c
LG
48 @param[in] Buffer For read operations, the destination buffer to store\r
49 the results. For write operations, the source buffer\r
bc230a23 50 from which to write data.\r
51\r
52 @retval EFI_SUCCESS The data was read from or written to the device.\r
53 @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
54 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
7367cc6c 55\r
173eeac9 56**/\r
57EFI_STATUS\r
58CpuIoCheckParameter (\r
59 IN BOOLEAN MmioOperation,\r
60 IN EFI_SMM_IO_WIDTH Width,\r
61 IN UINT64 Address,\r
62 IN UINTN Count,\r
63 IN VOID *Buffer\r
64 )\r
65{\r
66 UINT64 MaxCount;\r
67 UINT64 Limit;\r
68\r
69 //\r
70 // Check to see if Buffer is NULL\r
71 //\r
72 if (Buffer == NULL) {\r
73 return EFI_INVALID_PARAMETER;\r
74 }\r
75\r
76 //\r
77 // Check to see if Width is in the valid range\r
78 //\r
3d78c020 79 if ((UINT32)Width > SMM_IO_UINT64) {\r
173eeac9 80 return EFI_INVALID_PARAMETER;\r
81 }\r
82\r
83 //\r
84 // Check to see if Width is in the valid range for I/O Port operations\r
85 //\r
86 if (!MmioOperation && (Width == SMM_IO_UINT64)) {\r
87 return EFI_INVALID_PARAMETER;\r
88 }\r
7367cc6c 89\r
173eeac9 90 //\r
7367cc6c 91 // Check to see if any address associated with this transfer exceeds the maximum\r
173eeac9 92 // allowed address. The maximum address implied by the parameters passed in is\r
93 // Address + Size * Count. If the following condition is met, then the transfer\r
94 // is not supported.\r
95 //\r
96 // Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1\r
97 //\r
7367cc6c 98 // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count\r
173eeac9 99 // can also be the maximum integer value supported by the CPU, this range\r
311004b2 100 // check must be adjusted to avoid all overflow conditions.\r
7367cc6c
LG
101 //\r
102 // The following form of the range check is equivalent but assumes that\r
173eeac9 103 // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).\r
104 //\r
105 Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);\r
106 if (Count == 0) {\r
107 if (Address > Limit) {\r
108 return EFI_UNSUPPORTED;\r
109 }\r
7367cc6c 110 } else {\r
173eeac9 111 MaxCount = RShiftU64 (Limit, Width);\r
112 if (MaxCount < (Count - 1)) {\r
113 return EFI_UNSUPPORTED;\r
114 }\r
115 if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {\r
116 return EFI_UNSUPPORTED;\r
117 }\r
118 }\r
7367cc6c 119\r
173eeac9 120 //\r
121 // Check to see if Address is aligned\r
122 //\r
8491e302 123 if ((Address & ((UINT64)mStride[Width] - 1)) != 0) {\r
173eeac9 124 return EFI_UNSUPPORTED;\r
125 }\r
126\r
127 return EFI_SUCCESS;\r
128}\r
129\r
130/**\r
131 Reads memory-mapped registers.\r
132\r
7367cc6c
LG
133 The I/O operations are carried out exactly as requested. The caller is\r
134 responsible for any alignment and I/O width issues that the bus, device,\r
bc230a23 135 platform, or type of I/O might require.\r
136\r
137 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
138 @param[in] Width Signifies the width of the I/O operations.\r
7367cc6c
LG
139 @param[in] Address The base address of the I/O operations. The caller is\r
140 responsible for aligning the Address if required.\r
bc230a23 141 @param[in] Count The number of I/O operations to perform.\r
7367cc6c
LG
142 @param[out] Buffer For read operations, the destination buffer to store\r
143 the results. For write operations, the source buffer\r
bc230a23 144 from which to write data.\r
145\r
146 @retval EFI_SUCCESS The data was read from or written to the device.\r
147 @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
148 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
7367cc6c 149 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
bc230a23 150 lack of resources\r
173eeac9 151\r
152**/\r
153EFI_STATUS\r
154EFIAPI\r
155CpuMemoryServiceRead (\r
156 IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
157 IN EFI_SMM_IO_WIDTH Width,\r
158 IN UINT64 Address,\r
159 IN UINTN Count,\r
160 OUT VOID *Buffer\r
161 )\r
162{\r
163 EFI_STATUS Status;\r
164 UINT8 Stride;\r
165 UINT8 *Uint8Buffer;\r
166\r
167 Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);\r
168 if (EFI_ERROR (Status)) {\r
169 return Status;\r
170 }\r
171\r
172 //\r
173 // Select loop based on the width of the transfer\r
174 //\r
175 Stride = mStride[Width];\r
176 for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
177 if (Width == SMM_IO_UINT8) {\r
178 *Uint8Buffer = MmioRead8 ((UINTN)Address);\r
179 } else if (Width == SMM_IO_UINT16) {\r
180 *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);\r
181 } else if (Width == SMM_IO_UINT32) {\r
182 *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);\r
183 } else if (Width == SMM_IO_UINT64) {\r
184 *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);\r
185 }\r
186 }\r
187 return EFI_SUCCESS;\r
188}\r
189\r
190/**\r
191 Writes memory-mapped registers.\r
192\r
7367cc6c
LG
193 The I/O operations are carried out exactly as requested. The caller is\r
194 responsible for any alignment and I/O width issues that the bus, device,\r
bc230a23 195 platform, or type of I/O might require.\r
196\r
197 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
198 @param[in] Width Signifies the width of the I/O operations.\r
7367cc6c
LG
199 @param[in] Address The base address of the I/O operations. The caller is\r
200 responsible for aligning the Address if required.\r
bc230a23 201 @param[in] Count The number of I/O operations to perform.\r
7367cc6c
LG
202 @param[in] Buffer For read operations, the destination buffer to store\r
203 the results. For write operations, the source buffer\r
bc230a23 204 from which to write data.\r
205\r
206 @retval EFI_SUCCESS The data was read from or written to the device.\r
207 @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
208 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
7367cc6c 209 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
bc230a23 210 lack of resources\r
173eeac9 211\r
212**/\r
213EFI_STATUS\r
214EFIAPI\r
215CpuMemoryServiceWrite (\r
216 IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
217 IN EFI_SMM_IO_WIDTH Width,\r
218 IN UINT64 Address,\r
219 IN UINTN Count,\r
220 IN VOID *Buffer\r
221 )\r
222{\r
223 EFI_STATUS Status;\r
224 UINT8 Stride;\r
225 UINT8 *Uint8Buffer;\r
226\r
227 Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);\r
228 if (EFI_ERROR (Status)) {\r
229 return Status;\r
230 }\r
231\r
232 //\r
233 // Select loop based on the width of the transfer\r
234 //\r
235 Stride = mStride[Width];\r
236 for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
237 if (Width == SMM_IO_UINT8) {\r
238 MmioWrite8 ((UINTN)Address, *Uint8Buffer);\r
239 } else if (Width == SMM_IO_UINT16) {\r
240 MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));\r
241 } else if (Width == SMM_IO_UINT32) {\r
242 MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));\r
243 } else if (Width == SMM_IO_UINT64) {\r
244 MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));\r
245 }\r
246 }\r
247 return EFI_SUCCESS;\r
248}\r
249\r
250/**\r
251 Reads I/O registers.\r
252\r
7367cc6c
LG
253 The I/O operations are carried out exactly as requested. The caller is\r
254 responsible for any alignment and I/O width issues that the bus, device,\r
bc230a23 255 platform, or type of I/O might require.\r
256\r
257 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
258 @param[in] Width Signifies the width of the I/O operations.\r
7367cc6c
LG
259 @param[in] Address The base address of the I/O operations. The caller is\r
260 responsible for aligning the Address if required.\r
bc230a23 261 @param[in] Count The number of I/O operations to perform.\r
7367cc6c
LG
262 @param[out] Buffer For read operations, the destination buffer to store\r
263 the results. For write operations, the source buffer\r
bc230a23 264 from which to write data.\r
265\r
266 @retval EFI_SUCCESS The data was read from or written to the device.\r
267 @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
268 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
7367cc6c 269 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
bc230a23 270 lack of resources\r
173eeac9 271\r
272**/\r
273EFI_STATUS\r
274EFIAPI\r
275CpuIoServiceRead (\r
276 IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
277 IN EFI_SMM_IO_WIDTH Width,\r
278 IN UINT64 Address,\r
279 IN UINTN Count,\r
280 OUT VOID *Buffer\r
281 )\r
282{\r
283 EFI_STATUS Status;\r
284 UINT8 Stride;\r
285 UINT8 *Uint8Buffer;\r
286\r
287 Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);\r
288 if (EFI_ERROR (Status)) {\r
289 return Status;\r
290 }\r
291\r
292 //\r
293 // Select loop based on the width of the transfer\r
294 //\r
295 Stride = mStride[Width];\r
296 for (Uint8Buffer = Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
297 if (Width == SMM_IO_UINT8) {\r
298 *Uint8Buffer = IoRead8 ((UINTN)Address);\r
299 } else if (Width == SMM_IO_UINT16) {\r
300 *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);\r
301 } else if (Width == SMM_IO_UINT32) {\r
302 *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);\r
303 }\r
304 }\r
305\r
306 return EFI_SUCCESS;\r
307}\r
308\r
309/**\r
310 Write I/O registers.\r
311\r
7367cc6c
LG
312 The I/O operations are carried out exactly as requested. The caller is\r
313 responsible for any alignment and I/O width issues that the bus, device,\r
bc230a23 314 platform, or type of I/O might require.\r
315\r
316 @param[in] This The EFI_SMM_CPU_IO2_PROTOCOL instance.\r
317 @param[in] Width Signifies the width of the I/O operations.\r
7367cc6c
LG
318 @param[in] Address The base address of the I/O operations. The caller is\r
319 responsible for aligning the Address if required.\r
bc230a23 320 @param[in] Count The number of I/O operations to perform.\r
7367cc6c
LG
321 @param[in] Buffer For read operations, the destination buffer to store\r
322 the results. For write operations, the source buffer\r
bc230a23 323 from which to write data.\r
324\r
325 @retval EFI_SUCCESS The data was read from or written to the device.\r
326 @retval EFI_UNSUPPORTED The Address is not valid for this system.\r
327 @retval EFI_INVALID_PARAMETER Width or Count, or both, were invalid.\r
7367cc6c 328 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
bc230a23 329 lack of resources\r
173eeac9 330\r
331**/\r
332EFI_STATUS\r
333EFIAPI\r
334CpuIoServiceWrite (\r
335 IN CONST EFI_SMM_CPU_IO2_PROTOCOL *This,\r
336 IN EFI_SMM_IO_WIDTH Width,\r
337 IN UINT64 Address,\r
338 IN UINTN Count,\r
339 IN VOID *Buffer\r
340 )\r
341{\r
342 EFI_STATUS Status;\r
343 UINT8 Stride;\r
344 UINT8 *Uint8Buffer;\r
345\r
346 //\r
347 // Make sure the parameters are valid\r
348 //\r
349 Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);\r
350 if (EFI_ERROR (Status)) {\r
351 return Status;\r
352 }\r
353\r
354 //\r
355 // Select loop based on the width of the transfer\r
356 //\r
357 Stride = mStride[Width];\r
358 for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += Stride, Uint8Buffer += Stride, Count--) {\r
359 if (Width == SMM_IO_UINT8) {\r
360 IoWrite8 ((UINTN)Address, *Uint8Buffer);\r
361 } else if (Width == SMM_IO_UINT16) {\r
362 IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));\r
363 } else if (Width == SMM_IO_UINT32) {\r
364 IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));\r
365 }\r
366 }\r
7367cc6c 367\r
173eeac9 368 return EFI_SUCCESS;\r
369}\r
370\r
371/**\r
372 The module Entry Point SmmCpuIoProtocol driver\r
373\r
bc230a23 374 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
375 @param[in] SystemTable A pointer to the EFI System Table.\r
173eeac9 376\r
bc230a23 377 @retval EFI_SUCCESS The entry point is executed successfully.\r
378 @retval Other Some error occurs when executing this entry point.\r
173eeac9 379\r
380**/\r
381EFI_STATUS\r
382EFIAPI\r
383SmmCpuIo2Initialize (\r
384 IN EFI_HANDLE ImageHandle,\r
385 IN EFI_SYSTEM_TABLE *SystemTable\r
386 )\r
430fbbe0 387{\r
173eeac9 388 EFI_STATUS Status;\r
389\r
390 //\r
391 // Copy the SMM CPU I/O Protocol instance into the System Management System Table\r
392 //\r
e414a3e9 393 CopyMem (&gMmst->MmIo, &mSmmCpuIo2, sizeof (mSmmCpuIo2));\r
173eeac9 394\r
395 //\r
e414a3e9 396 // Install the SMM CPU I/O Protocol into the MM protocol database\r
173eeac9 397 //\r
e414a3e9 398 Status = gMmst->MmInstallProtocolInterface (\r
173eeac9 399 &mHandle,\r
400 &gEfiSmmCpuIo2ProtocolGuid,\r
401 EFI_NATIVE_INTERFACE,\r
402 &mSmmCpuIo2\r
403 );\r
404 ASSERT_EFI_ERROR (Status);\r
7367cc6c 405\r
173eeac9 406 return Status;\r
407}\r