3 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #ifndef _SD_BLOCK_IO_PEI_H_
9 #define _SD_BLOCK_IO_PEI_H_
13 #include <Ppi/SdMmcHostController.h>
14 #include <Ppi/BlockIo.h>
15 #include <Ppi/BlockIo2.h>
16 #include <Ppi/IoMmu.h>
17 #include <Ppi/EndOfPeiPhase.h>
19 #include <Library/DebugLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/TimerLib.h>
25 #include <Library/PeiServicesLib.h>
27 #include <IndustryStandard/Sd.h>
29 typedef struct _SD_PEIM_HC_PRIVATE_DATA SD_PEIM_HC_PRIVATE_DATA
;
30 typedef struct _SD_PEIM_HC_SLOT SD_PEIM_HC_SLOT
;
31 typedef struct _SD_TRB SD_TRB
;
36 #define SD_PEIM_SIG SIGNATURE_32 ('S', 'D', 'C', 'P')
37 #define SD_PEIM_SLOT_SIG SIGNATURE_32 ('S', 'D', 'C', 'S')
39 #define SD_PEIM_MAX_SLOTS 6
41 struct _SD_PEIM_HC_SLOT
{
43 EFI_PEI_BLOCK_IO2_MEDIA Media
;
46 SD_HC_SLOT_CAP Capability
;
48 BOOLEAN SectorAddressing
;
49 SD_PEIM_HC_PRIVATE_DATA
*Private
;
52 struct _SD_PEIM_HC_PRIVATE_DATA
{
54 SD_PEIM_MEM_POOL
*Pool
;
55 EFI_PEI_RECOVERY_BLOCK_IO_PPI BlkIoPpi
;
56 EFI_PEI_RECOVERY_BLOCK_IO2_PPI BlkIo2Ppi
;
57 EFI_PEI_PPI_DESCRIPTOR BlkIoPpiList
;
58 EFI_PEI_PPI_DESCRIPTOR BlkIo2PpiList
;
61 // EndOfPei callback is used to do the cleanups before exit of PEI phase.
63 EFI_PEI_NOTIFY_DESCRIPTOR EndOfPeiNotifyList
;
65 SD_PEIM_HC_SLOT Slot
[SD_PEIM_MAX_SLOTS
];
67 UINT8 TotalBlkIoDevices
;
70 #define SD_TIMEOUT MultU64x32((UINT64)(3), 1000000)
71 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, BlkIoPpi, SD_PEIM_SIG)
72 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, BlkIo2Ppi, SD_PEIM_SIG)
73 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, EndOfPeiNotifyList, SD_PEIM_SIG)
76 SD_PEIM_HC_SLOT
*Slot
;
79 SD_COMMAND_PACKET
*Packet
;
83 EFI_PHYSICAL_ADDRESS DataPhy
;
85 SD_HC_TRANSFER_MODE Mode
;
89 SD_HC_ADMA_DESC_LINE
*AdmaDesc
;
94 Gets the count of block I/O devices that one specific block driver detects.
96 This function is used for getting the count of block I/O devices that one
97 specific block driver detects. To the PEI ATAPI driver, it returns the number
98 of all the detected ATAPI devices it detects during the enumeration process.
99 To the PEI legacy floppy driver, it returns the number of all the legacy
100 devices it finds during its enumeration process. If no device is detected,
101 then the function will return zero.
103 @param[in] PeiServices General-purpose services that are available
105 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
107 @param[out] NumberBlockDevices The number of block I/O devices discovered.
109 @retval EFI_SUCCESS The operation performed successfully.
114 SdBlockIoPeimGetDeviceNo (
115 IN EFI_PEI_SERVICES
**PeiServices
,
116 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
117 OUT UINTN
*NumberBlockDevices
121 Gets a block device's media information.
123 This function will provide the caller with the specified block device's media
124 information. If the media changes, calling this function will update the media
125 information accordingly.
127 @param[in] PeiServices General-purpose services that are available to every
129 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
130 @param[in] DeviceIndex Specifies the block device to which the function wants
131 to talk. Because the driver that implements Block I/O
132 PPIs will manage multiple block devices, the PPIs that
133 want to talk to a single device must specify the
134 device index that was assigned during the enumeration
135 process. This index is a number from one to
137 @param[out] MediaInfo The media information of the specified block media.
138 The caller is responsible for the ownership of this
142 The MediaInfo structure describes an enumeration of possible block device
143 types. This enumeration exists because no device paths are actually passed
144 across interfaces that describe the type or class of hardware that is publishing
145 the block I/O interface. This enumeration will allow for policy decisions
146 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
147 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
148 by a given device type, they should be reported in ascending order; this
149 order also applies to nested partitions, such as legacy MBR, where the
150 outermost partitions would have precedence in the reporting order. The
151 same logic applies to systems such as IDE that have precedence relationships
152 like "Master/Slave" or "Primary/Secondary". The master device should be
153 reported first, the slave second.
155 @retval EFI_SUCCESS Media information about the specified block device
156 was obtained successfully.
157 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
163 SdBlockIoPeimGetMediaInfo (
164 IN EFI_PEI_SERVICES
**PeiServices
,
165 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
166 IN UINTN DeviceIndex
,
167 OUT EFI_PEI_BLOCK_IO_MEDIA
*MediaInfo
171 Reads the requested number of blocks from the specified block device.
173 The function reads the requested number of blocks from the device. All the
174 blocks are read, or an error is returned. If there is no media in the device,
175 the function returns EFI_NO_MEDIA.
177 @param[in] PeiServices General-purpose services that are available to
179 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
180 @param[in] DeviceIndex Specifies the block device to which the function wants
181 to talk. Because the driver that implements Block I/O
182 PPIs will manage multiple block devices, PPIs that
183 want to talk to a single device must specify the device
184 index that was assigned during the enumeration process.
185 This index is a number from one to NumberBlockDevices.
186 @param[in] StartLBA The starting logical block address (LBA) to read from
188 @param[in] BufferSize The size of the Buffer in bytes. This number must be
189 a multiple of the intrinsic block size of the device.
190 @param[out] Buffer A pointer to the destination buffer for the data.
191 The caller is responsible for the ownership of the
194 @retval EFI_SUCCESS The data was read correctly from the device.
195 @retval EFI_DEVICE_ERROR The device reported an error while attempting
196 to perform the read operation.
197 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
198 valid, or the buffer is not properly aligned.
199 @retval EFI_NO_MEDIA There is no media in the device.
200 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
201 the intrinsic block size of the device.
206 SdBlockIoPeimReadBlocks (
207 IN EFI_PEI_SERVICES
**PeiServices
,
208 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI
*This
,
209 IN UINTN DeviceIndex
,
210 IN EFI_PEI_LBA StartLBA
,
216 Gets the count of block I/O devices that one specific block driver detects.
218 This function is used for getting the count of block I/O devices that one
219 specific block driver detects. To the PEI ATAPI driver, it returns the number
220 of all the detected ATAPI devices it detects during the enumeration process.
221 To the PEI legacy floppy driver, it returns the number of all the legacy
222 devices it finds during its enumeration process. If no device is detected,
223 then the function will return zero.
225 @param[in] PeiServices General-purpose services that are available
227 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
229 @param[out] NumberBlockDevices The number of block I/O devices discovered.
231 @retval EFI_SUCCESS The operation performed successfully.
236 SdBlockIoPeimGetDeviceNo2 (
237 IN EFI_PEI_SERVICES
**PeiServices
,
238 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
239 OUT UINTN
*NumberBlockDevices
243 Gets a block device's media information.
245 This function will provide the caller with the specified block device's media
246 information. If the media changes, calling this function will update the media
247 information accordingly.
249 @param[in] PeiServices General-purpose services that are available to every
251 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
252 @param[in] DeviceIndex Specifies the block device to which the function wants
253 to talk. Because the driver that implements Block I/O
254 PPIs will manage multiple block devices, the PPIs that
255 want to talk to a single device must specify the
256 device index that was assigned during the enumeration
257 process. This index is a number from one to
259 @param[out] MediaInfo The media information of the specified block media.
260 The caller is responsible for the ownership of this
264 The MediaInfo structure describes an enumeration of possible block device
265 types. This enumeration exists because no device paths are actually passed
266 across interfaces that describe the type or class of hardware that is publishing
267 the block I/O interface. This enumeration will allow for policy decisions
268 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
269 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
270 by a given device type, they should be reported in ascending order; this
271 order also applies to nested partitions, such as legacy MBR, where the
272 outermost partitions would have precedence in the reporting order. The
273 same logic applies to systems such as IDE that have precedence relationships
274 like "Master/Slave" or "Primary/Secondary". The master device should be
275 reported first, the slave second.
277 @retval EFI_SUCCESS Media information about the specified block device
278 was obtained successfully.
279 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
285 SdBlockIoPeimGetMediaInfo2 (
286 IN EFI_PEI_SERVICES
**PeiServices
,
287 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
288 IN UINTN DeviceIndex
,
289 OUT EFI_PEI_BLOCK_IO2_MEDIA
*MediaInfo
293 Reads the requested number of blocks from the specified block device.
295 The function reads the requested number of blocks from the device. All the
296 blocks are read, or an error is returned. If there is no media in the device,
297 the function returns EFI_NO_MEDIA.
299 @param[in] PeiServices General-purpose services that are available to
301 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
302 @param[in] DeviceIndex Specifies the block device to which the function wants
303 to talk. Because the driver that implements Block I/O
304 PPIs will manage multiple block devices, PPIs that
305 want to talk to a single device must specify the device
306 index that was assigned during the enumeration process.
307 This index is a number from one to NumberBlockDevices.
308 @param[in] StartLBA The starting logical block address (LBA) to read from
310 @param[in] BufferSize The size of the Buffer in bytes. This number must be
311 a multiple of the intrinsic block size of the device.
312 @param[out] Buffer A pointer to the destination buffer for the data.
313 The caller is responsible for the ownership of the
316 @retval EFI_SUCCESS The data was read correctly from the device.
317 @retval EFI_DEVICE_ERROR The device reported an error while attempting
318 to perform the read operation.
319 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
320 valid, or the buffer is not properly aligned.
321 @retval EFI_NO_MEDIA There is no media in the device.
322 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
323 the intrinsic block size of the device.
328 SdBlockIoPeimReadBlocks2 (
329 IN EFI_PEI_SERVICES
**PeiServices
,
330 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI
*This
,
331 IN UINTN DeviceIndex
,
332 IN EFI_PEI_LBA StartLBA
,
338 Initialize the memory management pool for the host controller.
340 @param Private The Sd Peim driver private data.
342 @retval EFI_SUCCESS The memory pool is initialized.
343 @retval Others Fail to init the memory pool.
348 IN SD_PEIM_HC_PRIVATE_DATA
*Private
352 Release the memory management pool.
354 @param Pool The memory pool to free.
356 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
357 @retval EFI_SUCCESS The memory pool is freed.
362 IN SD_PEIM_MEM_POOL
*Pool
366 Allocate some memory from the host controller's memory pool
367 which can be used to communicate with host controller.
369 @param Pool The host controller's memory pool.
370 @param Size Size of the memory to allocate.
372 @return The allocated memory or NULL.
377 IN SD_PEIM_MEM_POOL
*Pool
,
382 Free the allocated memory back to the memory pool.
384 @param Pool The memory pool of the host controller.
385 @param Mem The memory to free.
386 @param Size The size of the memory to free.
391 IN SD_PEIM_MEM_POOL
*Pool
,
405 Provides the controller-specific addresses required to access system memory from a
408 @param Operation Indicates if the bus master is going to read or write to system memory.
409 @param HostAddress The system memory address to map to the PCI controller.
410 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
412 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
413 access the hosts HostAddress.
414 @param Mapping A resulting value to pass to Unmap().
416 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
417 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
418 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
419 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
420 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
425 IN EDKII_IOMMU_OPERATION Operation
,
426 IN VOID
*HostAddress
,
427 IN OUT UINTN
*NumberOfBytes
,
428 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
433 Completes the Map() operation and releases any corresponding resources.
435 @param Mapping The mapping value returned from Map().
437 @retval EFI_SUCCESS The range was unmapped.
438 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
439 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
447 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
448 OperationBusMasterCommonBuffer64 mapping.
450 @param Pages The number of pages to allocate.
451 @param HostAddress A pointer to store the base system memory address of the
453 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
454 access the hosts HostAddress.
455 @param Mapping A resulting value to pass to Unmap().
457 @retval EFI_SUCCESS The requested memory pages were allocated.
458 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
459 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
460 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
461 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
465 IoMmuAllocateBuffer (
467 OUT VOID
**HostAddress
,
468 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
473 Frees memory that was allocated with AllocateBuffer().
475 @param Pages The number of pages to free.
476 @param HostAddress The base system memory address of the allocated range.
477 @param Mapping The mapping value returned from Map().
479 @retval EFI_SUCCESS The requested memory pages were freed.
480 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
481 was not allocated with AllocateBuffer().
487 IN VOID
*HostAddress
,
492 One notified function to cleanup the allocated DMA buffers at the end of PEI.
494 @param[in] PeiServices Pointer to PEI Services Table.
495 @param[in] NotifyDescriptor Pointer to the descriptor for the Notification
496 event that caused this function to execute.
497 @param[in] Ppi Pointer to the PPI data associated with this function.
499 @retval EFI_SUCCESS The function completes successfully
504 SdBlockIoPeimEndOfPei (
505 IN EFI_PEI_SERVICES
**PeiServices
,
506 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,