]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/SdBlockIoPei/SdBlockIoPei.h
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / SdBlockIoPei / SdBlockIoPei.h
1 /** @file
2
3 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6 **/
7
8 #ifndef _SD_BLOCK_IO_PEI_H_
9 #define _SD_BLOCK_IO_PEI_H_
10
11 #include <PiPei.h>
12
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>
18
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>
26
27 #include <IndustryStandard/Sd.h>
28
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;
32
33 #include "SdHci.h"
34 #include "SdHcMem.h"
35
36 #define SD_PEIM_SIG SIGNATURE_32 ('S', 'D', 'C', 'P')
37 #define SD_PEIM_SLOT_SIG SIGNATURE_32 ('S', 'D', 'C', 'S')
38
39 #define SD_PEIM_MAX_SLOTS 6
40
41 struct _SD_PEIM_HC_SLOT {
42 UINT32 Signature;
43 EFI_PEI_BLOCK_IO2_MEDIA Media;
44
45 UINTN SdHcBase;
46 SD_HC_SLOT_CAP Capability;
47 SD_CSD Csd;
48 BOOLEAN SectorAddressing;
49 SD_PEIM_HC_PRIVATE_DATA *Private;
50 };
51
52 struct _SD_PEIM_HC_PRIVATE_DATA {
53 UINT32 Signature;
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;
59
60 //
61 // EndOfPei callback is used to do the cleanups before exit of PEI phase.
62 //
63 EFI_PEI_NOTIFY_DESCRIPTOR EndOfPeiNotifyList;
64
65 SD_PEIM_HC_SLOT Slot[SD_PEIM_MAX_SLOTS];
66 UINT8 SlotNum;
67 UINT8 TotalBlkIoDevices;
68 };
69
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)
74
75 struct _SD_TRB {
76 SD_PEIM_HC_SLOT *Slot;
77 UINT16 BlockSize;
78
79 SD_COMMAND_PACKET *Packet;
80 VOID *Data;
81 UINT32 DataLen;
82 BOOLEAN Read;
83 EFI_PHYSICAL_ADDRESS DataPhy;
84 VOID *DataMap;
85 SD_HC_TRANSFER_MODE Mode;
86
87 UINT64 Timeout;
88
89 SD_HC_ADMA_DESC_LINE *AdmaDesc;
90 UINTN AdmaDescSize;
91 };
92
93 /**
94 Gets the count of block I/O devices that one specific block driver detects.
95
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.
102
103 @param[in] PeiServices General-purpose services that are available
104 to every PEIM.
105 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
106 instance.
107 @param[out] NumberBlockDevices The number of block I/O devices discovered.
108
109 @retval EFI_SUCCESS The operation performed successfully.
110
111 **/
112 EFI_STATUS
113 EFIAPI
114 SdBlockIoPeimGetDeviceNo (
115 IN EFI_PEI_SERVICES **PeiServices,
116 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
117 OUT UINTN *NumberBlockDevices
118 );
119
120 /**
121 Gets a block device's media information.
122
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.
126
127 @param[in] PeiServices General-purpose services that are available to every
128 PEIM
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
136 NumberBlockDevices.
137 @param[out] MediaInfo The media information of the specified block media.
138 The caller is responsible for the ownership of this
139 data structure.
140
141 @par Note:
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.
154
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
158 error.
159
160 **/
161 EFI_STATUS
162 EFIAPI
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
168 );
169
170 /**
171 Reads the requested number of blocks from the specified block device.
172
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.
176
177 @param[in] PeiServices General-purpose services that are available to
178 every PEIM.
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
187 on the device
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
192 buffer.
193
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.
202
203 **/
204 EFI_STATUS
205 EFIAPI
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,
211 IN UINTN BufferSize,
212 OUT VOID *Buffer
213 );
214
215 /**
216 Gets the count of block I/O devices that one specific block driver detects.
217
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.
224
225 @param[in] PeiServices General-purpose services that are available
226 to every PEIM.
227 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
228 instance.
229 @param[out] NumberBlockDevices The number of block I/O devices discovered.
230
231 @retval EFI_SUCCESS The operation performed successfully.
232
233 **/
234 EFI_STATUS
235 EFIAPI
236 SdBlockIoPeimGetDeviceNo2 (
237 IN EFI_PEI_SERVICES **PeiServices,
238 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
239 OUT UINTN *NumberBlockDevices
240 );
241
242 /**
243 Gets a block device's media information.
244
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.
248
249 @param[in] PeiServices General-purpose services that are available to every
250 PEIM
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
258 NumberBlockDevices.
259 @param[out] MediaInfo The media information of the specified block media.
260 The caller is responsible for the ownership of this
261 data structure.
262
263 @par Note:
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.
276
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
280 error.
281
282 **/
283 EFI_STATUS
284 EFIAPI
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
290 );
291
292 /**
293 Reads the requested number of blocks from the specified block device.
294
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.
298
299 @param[in] PeiServices General-purpose services that are available to
300 every PEIM.
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
309 on the device
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
314 buffer.
315
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.
324
325 **/
326 EFI_STATUS
327 EFIAPI
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,
333 IN UINTN BufferSize,
334 OUT VOID *Buffer
335 );
336
337 /**
338 Initialize the memory management pool for the host controller.
339
340 @param Private The Sd Peim driver private data.
341
342 @retval EFI_SUCCESS The memory pool is initialized.
343 @retval Others Fail to init the memory pool.
344
345 **/
346 EFI_STATUS
347 SdPeimInitMemPool (
348 IN SD_PEIM_HC_PRIVATE_DATA *Private
349 );
350
351 /**
352 Release the memory management pool.
353
354 @param Pool The memory pool to free.
355
356 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
357 @retval EFI_SUCCESS The memory pool is freed.
358
359 **/
360 EFI_STATUS
361 SdPeimFreeMemPool (
362 IN SD_PEIM_MEM_POOL *Pool
363 );
364
365 /**
366 Allocate some memory from the host controller's memory pool
367 which can be used to communicate with host controller.
368
369 @param Pool The host controller's memory pool.
370 @param Size Size of the memory to allocate.
371
372 @return The allocated memory or NULL.
373
374 **/
375 VOID *
376 SdPeimAllocateMem (
377 IN SD_PEIM_MEM_POOL *Pool,
378 IN UINTN Size
379 );
380
381 /**
382 Free the allocated memory back to the memory pool.
383
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.
387
388 **/
389 VOID
390 SdPeimFreeMem (
391 IN SD_PEIM_MEM_POOL *Pool,
392 IN VOID *Mem,
393 IN UINTN Size
394 );
395
396 /**
397 Initialize IOMMU.
398 **/
399 VOID
400 IoMmuInit (
401 VOID
402 );
403
404 /**
405 Provides the controller-specific addresses required to access system memory from a
406 DMA bus master.
407
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
411 that were mapped.
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().
415
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.
421
422 **/
423 EFI_STATUS
424 IoMmuMap (
425 IN EDKII_IOMMU_OPERATION Operation,
426 IN VOID *HostAddress,
427 IN OUT UINTN *NumberOfBytes,
428 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
429 OUT VOID **Mapping
430 );
431
432 /**
433 Completes the Map() operation and releases any corresponding resources.
434
435 @param Mapping The mapping value returned from Map().
436
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.
440 **/
441 EFI_STATUS
442 IoMmuUnmap (
443 IN VOID *Mapping
444 );
445
446 /**
447 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
448 OperationBusMasterCommonBuffer64 mapping.
449
450 @param Pages The number of pages to allocate.
451 @param HostAddress A pointer to store the base system memory address of the
452 allocated range.
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().
456
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.
462
463 **/
464 EFI_STATUS
465 IoMmuAllocateBuffer (
466 IN UINTN Pages,
467 OUT VOID **HostAddress,
468 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
469 OUT VOID **Mapping
470 );
471
472 /**
473 Frees memory that was allocated with AllocateBuffer().
474
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().
478
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().
482
483 **/
484 EFI_STATUS
485 IoMmuFreeBuffer (
486 IN UINTN Pages,
487 IN VOID *HostAddress,
488 IN VOID *Mapping
489 );
490
491 /**
492 One notified function to cleanup the allocated DMA buffers at the end of PEI.
493
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.
498
499 @retval EFI_SUCCESS The function completes successfully
500
501 **/
502 EFI_STATUS
503 EFIAPI
504 SdBlockIoPeimEndOfPei (
505 IN EFI_PEI_SERVICES **PeiServices,
506 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
507 IN VOID *Ppi
508 );
509
510 #endif