]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/CpuRuntimeDxe/CpuIo.c
fixing build errors
[mirror_edk2.git] / UnixPkg / CpuRuntimeDxe / CpuIo.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 CpuIo.c
15
16 Abstract:
17
18 This is the code that publishes the CPU I/O Protocol.
19 The intent herein is to have a single I/O service that can load
20 as early as possible, extend into runtime, and be layered upon by
21 the implementations of architectural protocols and the PCI Root
22 Bridge I/O Protocol.
23
24 --*/
25 #include "PiDxe.h"
26 #include <Protocol/Cpu.h>
27 #include <Protocol/DataHub.h>
28 #include <Guid/DataHubRecords.h>
29 #include <Protocol/CpuIo.h>
30 #include <Protocol/FrameworkHii.h>
31
32 #include <Library/BaseLib.h>
33 #include <Library/DebugLib.h>
34 #include <Library/HiiLib.h>
35 #include <Library/UefiLib.h>
36 #include <Library/UefiDriverEntryPoint.h>
37 #include <Library/BaseMemoryLib.h>
38 #include <Library/MemoryAllocationLib.h>
39 #include <Library/UefiBootServicesTableLib.h>
40 #include <CpuDriver.h>
41
42 #define IA32_MAX_IO_ADDRESS 0xFFFF
43 #define IA32_MAX_MEM_ADDRESS 0xFFFFFFFF
44
45 EFI_CPU_IO_PROTOCOL mCpuIoProtocol;
46
47 EFI_STATUS
48 CpuIoCheckAddressRange (
49 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
50 IN UINT64 Address,
51 IN UINTN Count,
52 IN VOID *Buffer,
53 IN UINT64 Limit
54 );
55
56 EFI_STATUS
57 EFIAPI
58 CpuMemoryServiceRead (
59 IN EFI_CPU_IO_PROTOCOL *This,
60 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
61 IN UINT64 Address,
62 IN UINTN Count,
63 IN OUT VOID *Buffer
64 )
65 /*++
66
67 Routine Description:
68
69 Perform the Memory Access Read service for the CPU I/O Protocol
70
71 Arguments:
72
73 Pointer to an instance of the CPU I/O Protocol
74 Width of the Memory Access
75 Address of the Memory access
76 Count of the number of accesses to perform
77 Pointer to the buffer to read or write from memory
78
79 Returns:
80
81 Status
82
83 EFI_SUCCESS - The data was read from or written to the EFI
84 System.
85 EFI_INVALID_PARAMETER - Width is invalid for this EFI System.
86 EFI_INVALID_PARAMETER - Buffer is NULL.
87 EFI_UNSUPPORTED - The Buffer is not aligned for the given Width.
88 EFI_UNSUPPORTED - The address range specified by Address, Width,
89 and Count is not valid for this EFI System.
90
91 --*/
92 // TODO: This - add argument and description to function comment
93 {
94 EFI_STATUS Status;
95
96 if (!Buffer) {
97 return EFI_INVALID_PARAMETER;
98 }
99
100 Status = CpuIoCheckAddressRange (Width, Address, Count, Buffer, IA32_MAX_MEM_ADDRESS);
101 if (EFI_ERROR (Status)) {
102 return Status;
103 }
104
105 //
106 // Do nothing for Nt32 version
107 //
108 return EFI_SUCCESS;
109 }
110
111 EFI_STATUS
112 EFIAPI
113 CpuMemoryServiceWrite (
114 IN EFI_CPU_IO_PROTOCOL *This,
115 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
116 IN UINT64 Address,
117 IN UINTN Count,
118 IN OUT VOID *Buffer
119 )
120 /*++
121
122 Routine Description:
123
124 Perform the Memory Access Read service for the CPU I/O Protocol
125
126 Arguments:
127
128 Pointer to an instance of the CPU I/O Protocol
129 Width of the Memory Access
130 Address of the Memory access
131 Count of the number of accesses to perform
132 Pointer to the buffer to read or write from memory
133
134 Returns:
135
136 Status
137
138 EFI_SUCCESS - The data was read from or written to the EFI System.
139 EFI_INVALID_PARAMETER - Width is invalid for this EFI System.
140 EFI_INVALID_PARAMETER - Buffer is NULL.
141 EFI_UNSUPPORTED - The Buffer is not aligned for the given Width.
142 EFI_UNSUPPORTED - The address range specified by Address, Width, and
143 Count is not valid for this EFI System.
144
145 --*/
146 // TODO: This - add argument and description to function comment
147 {
148 EFI_STATUS Status;
149
150 if (!Buffer) {
151 return EFI_INVALID_PARAMETER;
152 }
153
154 Status = CpuIoCheckAddressRange (Width, Address, Count, Buffer, IA32_MAX_MEM_ADDRESS);
155 if (EFI_ERROR (Status)) {
156 return Status;
157 }
158
159 //
160 // Do nothing for Nt32 version
161 //
162 return EFI_SUCCESS;
163 }
164
165 EFI_STATUS
166 EFIAPI
167 CpuIoServiceRead (
168 IN EFI_CPU_IO_PROTOCOL *This,
169 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
170 IN UINT64 UserAddress,
171 IN UINTN Count,
172 IN OUT VOID *UserBuffer
173 )
174 /*++
175
176 Routine Description:
177
178 This is the service that implements the I/O read
179
180 Arguments:
181
182 Pointer to an instance of the CPU I/O Protocol
183 Width of the Memory Access
184 Address of the I/O access
185 Count of the number of accesses to perform
186 Pointer to the buffer to read or write from I/O space
187
188 Returns:
189
190 Status
191 EFI_SUCCESS - The data was read from or written to the EFI System.
192 EFI_INVALID_PARAMETER - Width is invalid for this EFI System.
193 EFI_INVALID_PARAMETER - Buffer is NULL.
194 EFI_UNSUPPORTED - The Buffer is not aligned for the given Width.
195 EFI_UNSUPPORTED - The address range specified by Address, Width, and
196 Count is not valid for this EFI System.
197 --*/
198 // TODO: This - add argument and description to function comment
199 // TODO: UserAddress - add argument and description to function comment
200 // TODO: UserBuffer - add argument and description to function comment
201 {
202 UINTN Address;
203 EFI_STATUS Status;
204
205 if (!UserBuffer) {
206 return EFI_INVALID_PARAMETER;
207 }
208
209 Address = (UINTN) UserAddress;
210
211 if (Width >= EfiCpuIoWidthMaximum) {
212 return EFI_INVALID_PARAMETER;
213 }
214
215 Status = CpuIoCheckAddressRange (Width, Address, Count, UserBuffer, IA32_MAX_IO_ADDRESS);
216 if (EFI_ERROR (Status)) {
217 return Status;
218 }
219
220 //
221 // Do nothing for Nt32 version
222 //
223 return EFI_SUCCESS;
224 }
225
226 EFI_STATUS
227 EFIAPI
228 CpuIoServiceWrite (
229 IN EFI_CPU_IO_PROTOCOL *This,
230 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
231 IN UINT64 UserAddress,
232 IN UINTN Count,
233 IN OUT VOID *UserBuffer
234 )
235 /*++
236
237 Routine Description:
238
239
240 This is the service that implements the I/O Write
241
242 Arguments:
243
244 Pointer to an instance of the CPU I/O Protocol
245 Width of the Memory Access
246 Address of the I/O access
247 Count of the number of accesses to perform
248 Pointer to the buffer to read or write from I/O space
249
250 Returns:
251
252 Status
253
254 Status
255 EFI_SUCCESS - The data was read from or written to the EFI System.
256 EFI_INVALID_PARAMETER - Width is invalid for this EFI System.
257 EFI_INVALID_PARAMETER - Buffer is NULL.
258 EFI_UNSUPPORTED - The Buffer is not aligned for the given Width.
259 EFI_UNSUPPORTED - The address range specified by Address, Width, and
260 Count is not valid for this EFI System.
261
262 --*/
263 // TODO: This - add argument and description to function comment
264 // TODO: UserAddress - add argument and description to function comment
265 // TODO: UserBuffer - add argument and description to function comment
266 {
267 UINTN Address;
268 EFI_STATUS Status;
269
270 if (!UserBuffer) {
271 return EFI_INVALID_PARAMETER;
272 }
273
274 Address = (UINTN) UserAddress;
275
276 if (Width >= EfiCpuIoWidthMaximum) {
277 return EFI_INVALID_PARAMETER;
278 }
279
280 Status = CpuIoCheckAddressRange (Width, Address, Count, UserBuffer, IA32_MAX_IO_ADDRESS);
281 if (EFI_ERROR (Status)) {
282 return Status;
283 }
284
285 //
286 // Do nothing for Nt32 version
287 //
288 return EFI_SUCCESS;
289 }
290
291
292 EFI_STATUS
293 CpuIoCheckAddressRange (
294 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
295 IN UINT64 Address,
296 IN UINTN Count,
297 IN VOID *Buffer,
298 IN UINT64 Limit
299 )
300 /*++
301
302 Routine Description:
303
304 TODO: Add function description
305
306 Arguments:
307
308 Width - TODO: add argument description
309 Address - TODO: add argument description
310 Count - TODO: add argument description
311 Buffer - TODO: add argument description
312 Limit - TODO: add argument description
313
314 Returns:
315
316 EFI_UNSUPPORTED - TODO: Add description for return value
317 EFI_UNSUPPORTED - TODO: Add description for return value
318 EFI_UNSUPPORTED - TODO: Add description for return value
319 EFI_SUCCESS - TODO: Add description for return value
320
321 --*/
322 {
323 UINTN AlignMask;
324
325 if (Address > Limit) {
326 return EFI_UNSUPPORTED;
327 }
328
329 //
330 // For FiFo type, the target address won't increase during the access, so treat count as 1
331 //
332 if (Width >= EfiCpuIoWidthFifoUint8 && Width <= EfiCpuIoWidthFifoUint64) {
333 Count = 1;
334 }
335
336 Width = Width & 0x03;
337 if (Address - 1 + (1 << Width) * Count > Limit) {
338 return EFI_UNSUPPORTED;
339 }
340
341 AlignMask = (1 << Width) - 1;
342 if ((UINTN) Buffer & AlignMask) {
343 return EFI_UNSUPPORTED;
344 }
345
346 return EFI_SUCCESS;
347 }
348
349