]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Drivers/ArmPciCpuIo2Dxe/ArmPciCpuIo2Dxe.c
ArmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / ArmPkg / Drivers / ArmPciCpuIo2Dxe / ArmPciCpuIo2Dxe.c
1 /** @file
2 Produces the CPU I/O 2 Protocol.
3
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiDxe.h>
12
13 #include <Protocol/CpuIo2.h>
14
15 #include <Library/BaseLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/IoLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20
21 #define MAX_IO_PORT_ADDRESS 0xFFFF
22
23 //
24 // Handle for the CPU I/O 2 Protocol
25 //
26 STATIC EFI_HANDLE mHandle = NULL;
27
28 //
29 // Lookup table for increment values based on transfer widths
30 //
31 STATIC CONST UINT8 mInStride[] = {
32 1, // EfiCpuIoWidthUint8
33 2, // EfiCpuIoWidthUint16
34 4, // EfiCpuIoWidthUint32
35 8, // EfiCpuIoWidthUint64
36 0, // EfiCpuIoWidthFifoUint8
37 0, // EfiCpuIoWidthFifoUint16
38 0, // EfiCpuIoWidthFifoUint32
39 0, // EfiCpuIoWidthFifoUint64
40 1, // EfiCpuIoWidthFillUint8
41 2, // EfiCpuIoWidthFillUint16
42 4, // EfiCpuIoWidthFillUint32
43 8 // EfiCpuIoWidthFillUint64
44 };
45
46 //
47 // Lookup table for increment values based on transfer widths
48 //
49 STATIC CONST UINT8 mOutStride[] = {
50 1, // EfiCpuIoWidthUint8
51 2, // EfiCpuIoWidthUint16
52 4, // EfiCpuIoWidthUint32
53 8, // EfiCpuIoWidthUint64
54 1, // EfiCpuIoWidthFifoUint8
55 2, // EfiCpuIoWidthFifoUint16
56 4, // EfiCpuIoWidthFifoUint32
57 8, // EfiCpuIoWidthFifoUint64
58 0, // EfiCpuIoWidthFillUint8
59 0, // EfiCpuIoWidthFillUint16
60 0, // EfiCpuIoWidthFillUint32
61 0 // EfiCpuIoWidthFillUint64
62 };
63
64 /**
65 Check parameters to a CPU I/O 2 Protocol service request.
66
67 The I/O operations are carried out exactly as requested. The caller is responsible
68 for satisfying any alignment and I/O width restrictions that a PI System on a
69 platform might require. For example on some platforms, width requests of
70 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
71 be handled by the driver.
72
73 @param[in] MmioOperation TRUE for an MMIO operation, FALSE for I/O Port operation.
74 @param[in] Width Signifies the width of the I/O or Memory operation.
75 @param[in] Address The base address of the I/O operation.
76 @param[in] Count The number of I/O operations to perform. The number of
77 bytes moved is Width size * Count, starting at Address.
78 @param[in] Buffer For read operations, the destination buffer to store the results.
79 For write operations, the source buffer from which to write data.
80
81 @retval EFI_SUCCESS The parameters for this request pass the checks.
82 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
83 @retval EFI_INVALID_PARAMETER Buffer is NULL.
84 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
85 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
86 and Count is not valid for this PI system.
87
88 **/
89 STATIC
90 EFI_STATUS
91 CpuIoCheckParameter (
92 IN BOOLEAN MmioOperation,
93 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
94 IN UINT64 Address,
95 IN UINTN Count,
96 IN VOID *Buffer
97 )
98 {
99 UINT64 MaxCount;
100 UINT64 Limit;
101
102 //
103 // Check to see if Buffer is NULL
104 //
105 if (Buffer == NULL) {
106 return EFI_INVALID_PARAMETER;
107 }
108
109 //
110 // Check to see if Width is in the valid range
111 //
112 if ((UINT32)Width >= EfiCpuIoWidthMaximum) {
113 return EFI_INVALID_PARAMETER;
114 }
115
116 //
117 // For FIFO type, the target address won't increase during the access,
118 // so treat Count as 1
119 //
120 if (Width >= EfiCpuIoWidthFifoUint8 && Width <= EfiCpuIoWidthFifoUint64) {
121 Count = 1;
122 }
123
124 //
125 // Check to see if Width is in the valid range for I/O Port operations
126 //
127 Width = (EFI_CPU_IO_PROTOCOL_WIDTH) (Width & 0x03);
128 if (!MmioOperation && (Width == EfiCpuIoWidthUint64)) {
129 return EFI_INVALID_PARAMETER;
130 }
131
132 //
133 // Check to see if Address is aligned
134 //
135 if ((Address & (UINT64)(mInStride[Width] - 1)) != 0) {
136 return EFI_UNSUPPORTED;
137 }
138
139 //
140 // Check to see if any address associated with this transfer exceeds the maximum
141 // allowed address. The maximum address implied by the parameters passed in is
142 // Address + Size * Count. If the following condition is met, then the transfer
143 // is not supported.
144 //
145 // Address + Size * Count > (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS) + 1
146 //
147 // Since MAX_ADDRESS can be the maximum integer value supported by the CPU and Count
148 // can also be the maximum integer value supported by the CPU, this range
149 // check must be adjusted to avoid all oveflow conditions.
150 //
151 // The following form of the range check is equivalent but assumes that
152 // MAX_ADDRESS and MAX_IO_PORT_ADDRESS are of the form (2^n - 1).
153 //
154 Limit = (MmioOperation ? MAX_ADDRESS : MAX_IO_PORT_ADDRESS);
155 if (Count == 0) {
156 if (Address > Limit) {
157 return EFI_UNSUPPORTED;
158 }
159 } else {
160 MaxCount = RShiftU64 (Limit, Width);
161 if (MaxCount < (Count - 1)) {
162 return EFI_UNSUPPORTED;
163 }
164 if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {
165 return EFI_UNSUPPORTED;
166 }
167 }
168
169 //
170 // Check to see if Buffer is aligned
171 //
172 if (((UINTN)Buffer & ((MIN (sizeof (UINTN), mInStride[Width]) - 1))) != 0) {
173 return EFI_UNSUPPORTED;
174 }
175
176 return EFI_SUCCESS;
177 }
178
179 /**
180 Reads memory-mapped registers.
181
182 The I/O operations are carried out exactly as requested. The caller is responsible
183 for satisfying any alignment and I/O width restrictions that a PI System on a
184 platform might require. For example on some platforms, width requests of
185 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
186 be handled by the driver.
187
188 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
189 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
190 each of the Count operations that is performed.
191
192 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
193 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
194 incremented for each of the Count operations that is performed. The read or
195 write operation is performed Count times on the same Address.
196
197 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
198 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
199 incremented for each of the Count operations that is performed. The read or
200 write operation is performed Count times from the first element of Buffer.
201
202 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
203 @param[in] Width Signifies the width of the I/O or Memory operation.
204 @param[in] Address The base address of the I/O operation.
205 @param[in] Count The number of I/O operations to perform. The number of
206 bytes moved is Width size * Count, starting at Address.
207 @param[out] Buffer For read operations, the destination buffer to store the results.
208 For write operations, the source buffer from which to write data.
209
210 @retval EFI_SUCCESS The data was read from or written to the PI system.
211 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
212 @retval EFI_INVALID_PARAMETER Buffer is NULL.
213 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
214 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
215 and Count is not valid for this PI system.
216
217 **/
218 STATIC
219 EFI_STATUS
220 EFIAPI
221 CpuMemoryServiceRead (
222 IN EFI_CPU_IO2_PROTOCOL *This,
223 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
224 IN UINT64 Address,
225 IN UINTN Count,
226 OUT VOID *Buffer
227 )
228 {
229 EFI_STATUS Status;
230 UINT8 InStride;
231 UINT8 OutStride;
232 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth;
233 UINT8 *Uint8Buffer;
234
235 Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
236 if (EFI_ERROR (Status)) {
237 return Status;
238 }
239
240 //
241 // Select loop based on the width of the transfer
242 //
243 InStride = mInStride[Width];
244 OutStride = mOutStride[Width];
245 OperationWidth = (EFI_CPU_IO_PROTOCOL_WIDTH) (Width & 0x03);
246 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
247 if (OperationWidth == EfiCpuIoWidthUint8) {
248 *Uint8Buffer = MmioRead8 ((UINTN)Address);
249 } else if (OperationWidth == EfiCpuIoWidthUint16) {
250 *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
251 } else if (OperationWidth == EfiCpuIoWidthUint32) {
252 *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
253 } else if (OperationWidth == EfiCpuIoWidthUint64) {
254 *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);
255 }
256 }
257 return EFI_SUCCESS;
258 }
259
260 /**
261 Writes memory-mapped registers.
262
263 The I/O operations are carried out exactly as requested. The caller is responsible
264 for satisfying any alignment and I/O width restrictions that a PI System on a
265 platform might require. For example on some platforms, width requests of
266 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
267 be handled by the driver.
268
269 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
270 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
271 each of the Count operations that is performed.
272
273 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
274 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
275 incremented for each of the Count operations that is performed. The read or
276 write operation is performed Count times on the same Address.
277
278 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
279 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
280 incremented for each of the Count operations that is performed. The read or
281 write operation is performed Count times from the first element of Buffer.
282
283 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
284 @param[in] Width Signifies the width of the I/O or Memory operation.
285 @param[in] Address The base address of the I/O operation.
286 @param[in] Count The number of I/O operations to perform. The number of
287 bytes moved is Width size * Count, starting at Address.
288 @param[in] Buffer For read operations, the destination buffer to store the results.
289 For write operations, the source buffer from which to write data.
290
291 @retval EFI_SUCCESS The data was read from or written to the PI system.
292 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
293 @retval EFI_INVALID_PARAMETER Buffer is NULL.
294 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
295 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
296 and Count is not valid for this PI system.
297
298 **/
299 STATIC
300 EFI_STATUS
301 EFIAPI
302 CpuMemoryServiceWrite (
303 IN EFI_CPU_IO2_PROTOCOL *This,
304 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
305 IN UINT64 Address,
306 IN UINTN Count,
307 IN VOID *Buffer
308 )
309 {
310 EFI_STATUS Status;
311 UINT8 InStride;
312 UINT8 OutStride;
313 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth;
314 UINT8 *Uint8Buffer;
315
316 Status = CpuIoCheckParameter (TRUE, Width, Address, Count, Buffer);
317 if (EFI_ERROR (Status)) {
318 return Status;
319 }
320
321 //
322 // Select loop based on the width of the transfer
323 //
324 InStride = mInStride[Width];
325 OutStride = mOutStride[Width];
326 OperationWidth = (EFI_CPU_IO_PROTOCOL_WIDTH) (Width & 0x03);
327 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
328 if (OperationWidth == EfiCpuIoWidthUint8) {
329 MmioWrite8 ((UINTN)Address, *Uint8Buffer);
330 } else if (OperationWidth == EfiCpuIoWidthUint16) {
331 MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
332 } else if (OperationWidth == EfiCpuIoWidthUint32) {
333 MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
334 } else if (OperationWidth == EfiCpuIoWidthUint64) {
335 MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));
336 }
337 }
338 return EFI_SUCCESS;
339 }
340
341 /**
342 Reads I/O registers.
343
344 The I/O operations are carried out exactly as requested. The caller is responsible
345 for satisfying any alignment and I/O width restrictions that a PI System on a
346 platform might require. For example on some platforms, width requests of
347 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
348 be handled by the driver.
349
350 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
351 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
352 each of the Count operations that is performed.
353
354 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
355 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
356 incremented for each of the Count operations that is performed. The read or
357 write operation is performed Count times on the same Address.
358
359 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
360 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
361 incremented for each of the Count operations that is performed. The read or
362 write operation is performed Count times from the first element of Buffer.
363
364 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
365 @param[in] Width Signifies the width of the I/O or Memory operation.
366 @param[in] Address The base address of the I/O operation.
367 @param[in] Count The number of I/O operations to perform. The number of
368 bytes moved is Width size * Count, starting at Address.
369 @param[out] Buffer For read operations, the destination buffer to store the results.
370 For write operations, the source buffer from which to write data.
371
372 @retval EFI_SUCCESS The data was read from or written to the PI system.
373 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
374 @retval EFI_INVALID_PARAMETER Buffer is NULL.
375 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
376 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
377 and Count is not valid for this PI system.
378
379 **/
380 STATIC
381 EFI_STATUS
382 EFIAPI
383 CpuIoServiceRead (
384 IN EFI_CPU_IO2_PROTOCOL *This,
385 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
386 IN UINT64 Address,
387 IN UINTN Count,
388 OUT VOID *Buffer
389 )
390 {
391 EFI_STATUS Status;
392 UINT8 InStride;
393 UINT8 OutStride;
394 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth;
395 UINT8 *Uint8Buffer;
396
397 Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
398 if (EFI_ERROR (Status)) {
399 return Status;
400 }
401
402 Address += PcdGet64 (PcdPciIoTranslation);
403
404 //
405 // Select loop based on the width of the transfer
406 //
407 InStride = mInStride[Width];
408 OutStride = mOutStride[Width];
409 OperationWidth = (EFI_CPU_IO_PROTOCOL_WIDTH) (Width & 0x03);
410
411 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
412 if (OperationWidth == EfiCpuIoWidthUint8) {
413 *Uint8Buffer = MmioRead8 ((UINTN)Address);
414 } else if (OperationWidth == EfiCpuIoWidthUint16) {
415 *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
416 } else if (OperationWidth == EfiCpuIoWidthUint32) {
417 *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
418 }
419 }
420
421 return EFI_SUCCESS;
422 }
423
424 /**
425 Write I/O registers.
426
427 The I/O operations are carried out exactly as requested. The caller is responsible
428 for satisfying any alignment and I/O width restrictions that a PI System on a
429 platform might require. For example on some platforms, width requests of
430 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
431 be handled by the driver.
432
433 If Width is EfiCpuIoWidthUint8, EfiCpuIoWidthUint16, EfiCpuIoWidthUint32,
434 or EfiCpuIoWidthUint64, then both Address and Buffer are incremented for
435 each of the Count operations that is performed.
436
437 If Width is EfiCpuIoWidthFifoUint8, EfiCpuIoWidthFifoUint16,
438 EfiCpuIoWidthFifoUint32, or EfiCpuIoWidthFifoUint64, then only Buffer is
439 incremented for each of the Count operations that is performed. The read or
440 write operation is performed Count times on the same Address.
441
442 If Width is EfiCpuIoWidthFillUint8, EfiCpuIoWidthFillUint16,
443 EfiCpuIoWidthFillUint32, or EfiCpuIoWidthFillUint64, then only Address is
444 incremented for each of the Count operations that is performed. The read or
445 write operation is performed Count times from the first element of Buffer.
446
447 @param[in] This A pointer to the EFI_CPU_IO2_PROTOCOL instance.
448 @param[in] Width Signifies the width of the I/O or Memory operation.
449 @param[in] Address The base address of the I/O operation.
450 @param[in] Count The number of I/O operations to perform. The number of
451 bytes moved is Width size * Count, starting at Address.
452 @param[in] Buffer For read operations, the destination buffer to store the results.
453 For write operations, the source buffer from which to write data.
454
455 @retval EFI_SUCCESS The data was read from or written to the PI system.
456 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
457 @retval EFI_INVALID_PARAMETER Buffer is NULL.
458 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
459 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
460 and Count is not valid for this PI system.
461
462 **/
463 STATIC
464 EFI_STATUS
465 EFIAPI
466 CpuIoServiceWrite (
467 IN EFI_CPU_IO2_PROTOCOL *This,
468 IN EFI_CPU_IO_PROTOCOL_WIDTH Width,
469 IN UINT64 Address,
470 IN UINTN Count,
471 IN VOID *Buffer
472 )
473 {
474 EFI_STATUS Status;
475 UINT8 InStride;
476 UINT8 OutStride;
477 EFI_CPU_IO_PROTOCOL_WIDTH OperationWidth;
478 UINT8 *Uint8Buffer;
479
480 //
481 // Make sure the parameters are valid
482 //
483 Status = CpuIoCheckParameter (FALSE, Width, Address, Count, Buffer);
484 if (EFI_ERROR (Status)) {
485 return Status;
486 }
487
488 Address += PcdGet64 (PcdPciIoTranslation);
489
490 //
491 // Select loop based on the width of the transfer
492 //
493 InStride = mInStride[Width];
494 OutStride = mOutStride[Width];
495 OperationWidth = (EFI_CPU_IO_PROTOCOL_WIDTH) (Width & 0x03);
496
497 for (Uint8Buffer = (UINT8 *)Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
498 if (OperationWidth == EfiCpuIoWidthUint8) {
499 MmioWrite8 ((UINTN)Address, *Uint8Buffer);
500 } else if (OperationWidth == EfiCpuIoWidthUint16) {
501 MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
502 } else if (OperationWidth == EfiCpuIoWidthUint32) {
503 MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
504 }
505 }
506
507 return EFI_SUCCESS;
508 }
509
510 //
511 // CPU I/O 2 Protocol instance
512 //
513 STATIC EFI_CPU_IO2_PROTOCOL mCpuIo2 = {
514 {
515 CpuMemoryServiceRead,
516 CpuMemoryServiceWrite
517 },
518 {
519 CpuIoServiceRead,
520 CpuIoServiceWrite
521 }
522 };
523
524
525 /**
526 The user Entry Point for module CpuIo2Dxe. The user code starts with this function.
527
528 @param[in] ImageHandle The firmware allocated handle for the EFI image.
529 @param[in] SystemTable A pointer to the EFI System Table.
530
531 @retval EFI_SUCCESS The entry point is executed successfully.
532 @retval other Some error occurs when executing this entry point.
533
534 **/
535 EFI_STATUS
536 EFIAPI
537 ArmPciCpuIo2Initialize (
538 IN EFI_HANDLE ImageHandle,
539 IN EFI_SYSTEM_TABLE *SystemTable
540 )
541 {
542 EFI_STATUS Status;
543
544 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiCpuIo2ProtocolGuid);
545 Status = gBS->InstallMultipleProtocolInterfaces (
546 &mHandle,
547 &gEfiCpuIo2ProtocolGuid, &mCpuIo2,
548 NULL
549 );
550 ASSERT_EFI_ERROR (Status);
551
552 return Status;
553 }