]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/SdBlockIoPei/SdBlockIoPei.h
MdeModulePkg/AtaAtapiPassThru: Revert patch to disable Bus Master
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / SdBlockIoPei / SdBlockIoPei.h
1 /** @file
2
3 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
4 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 **/
13
14 #ifndef _SD_BLOCK_IO_PEI_H_
15 #define _SD_BLOCK_IO_PEI_H_
16
17 #include <PiPei.h>
18
19 #include <Ppi/SdMmcHostController.h>
20 #include <Ppi/BlockIo.h>
21 #include <Ppi/BlockIo2.h>
22 #include <Ppi/IoMmu.h>
23 #include <Ppi/EndOfPeiPhase.h>
24
25 #include <Library/DebugLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/IoLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/PeiServicesLib.h>
32
33 #include <IndustryStandard/Sd.h>
34
35 typedef struct _SD_PEIM_HC_PRIVATE_DATA SD_PEIM_HC_PRIVATE_DATA;
36 typedef struct _SD_PEIM_HC_SLOT SD_PEIM_HC_SLOT;
37 typedef struct _SD_TRB SD_TRB;
38
39 #include "SdHci.h"
40 #include "SdHcMem.h"
41
42 #define SD_PEIM_SIG SIGNATURE_32 ('S', 'D', 'C', 'P')
43 #define SD_PEIM_SLOT_SIG SIGNATURE_32 ('S', 'D', 'C', 'S')
44
45 #define SD_PEIM_MAX_SLOTS 6
46
47 struct _SD_PEIM_HC_SLOT {
48 UINT32 Signature;
49 EFI_PEI_BLOCK_IO2_MEDIA Media;
50
51 UINTN SdHcBase;
52 SD_HC_SLOT_CAP Capability;
53 SD_CSD Csd;
54 BOOLEAN SectorAddressing;
55 SD_PEIM_HC_PRIVATE_DATA *Private;
56 };
57
58 struct _SD_PEIM_HC_PRIVATE_DATA {
59 UINT32 Signature;
60 SD_PEIM_MEM_POOL *Pool;
61 EFI_PEI_RECOVERY_BLOCK_IO_PPI BlkIoPpi;
62 EFI_PEI_RECOVERY_BLOCK_IO2_PPI BlkIo2Ppi;
63 EFI_PEI_PPI_DESCRIPTOR BlkIoPpiList;
64 EFI_PEI_PPI_DESCRIPTOR BlkIo2PpiList;
65
66 //
67 // EndOfPei callback is used to do the cleanups before exit of PEI phase.
68 //
69 EFI_PEI_NOTIFY_DESCRIPTOR EndOfPeiNotifyList;
70
71 SD_PEIM_HC_SLOT Slot[SD_PEIM_MAX_SLOTS];
72 UINT8 SlotNum;
73 UINT8 TotalBlkIoDevices;
74 };
75
76 #define SD_TIMEOUT MultU64x32((UINT64)(3), 1000000)
77 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, BlkIoPpi, SD_PEIM_SIG)
78 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS2(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, BlkIo2Ppi, SD_PEIM_SIG)
79 #define GET_SD_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY(a) CR (a, SD_PEIM_HC_PRIVATE_DATA, EndOfPeiNotifyList, SD_PEIM_SIG)
80
81 struct _SD_TRB {
82 SD_PEIM_HC_SLOT *Slot;
83 UINT16 BlockSize;
84
85 SD_COMMAND_PACKET *Packet;
86 VOID *Data;
87 UINT32 DataLen;
88 BOOLEAN Read;
89 EFI_PHYSICAL_ADDRESS DataPhy;
90 VOID *DataMap;
91 SD_HC_TRANSFER_MODE Mode;
92
93 UINT64 Timeout;
94
95 SD_HC_ADMA_DESC_LINE *AdmaDesc;
96 UINTN AdmaDescSize;
97 };
98
99 /**
100 Gets the count of block I/O devices that one specific block driver detects.
101
102 This function is used for getting the count of block I/O devices that one
103 specific block driver detects. To the PEI ATAPI driver, it returns the number
104 of all the detected ATAPI devices it detects during the enumeration process.
105 To the PEI legacy floppy driver, it returns the number of all the legacy
106 devices it finds during its enumeration process. If no device is detected,
107 then the function will return zero.
108
109 @param[in] PeiServices General-purpose services that are available
110 to every PEIM.
111 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
112 instance.
113 @param[out] NumberBlockDevices The number of block I/O devices discovered.
114
115 @retval EFI_SUCCESS The operation performed successfully.
116
117 **/
118 EFI_STATUS
119 EFIAPI
120 SdBlockIoPeimGetDeviceNo (
121 IN EFI_PEI_SERVICES **PeiServices,
122 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
123 OUT UINTN *NumberBlockDevices
124 );
125
126 /**
127 Gets a block device's media information.
128
129 This function will provide the caller with the specified block device's media
130 information. If the media changes, calling this function will update the media
131 information accordingly.
132
133 @param[in] PeiServices General-purpose services that are available to every
134 PEIM
135 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
136 @param[in] DeviceIndex Specifies the block device to which the function wants
137 to talk. Because the driver that implements Block I/O
138 PPIs will manage multiple block devices, the PPIs that
139 want to talk to a single device must specify the
140 device index that was assigned during the enumeration
141 process. This index is a number from one to
142 NumberBlockDevices.
143 @param[out] MediaInfo The media information of the specified block media.
144 The caller is responsible for the ownership of this
145 data structure.
146
147 @par Note:
148 The MediaInfo structure describes an enumeration of possible block device
149 types. This enumeration exists because no device paths are actually passed
150 across interfaces that describe the type or class of hardware that is publishing
151 the block I/O interface. This enumeration will allow for policy decisions
152 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
153 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
154 by a given device type, they should be reported in ascending order; this
155 order also applies to nested partitions, such as legacy MBR, where the
156 outermost partitions would have precedence in the reporting order. The
157 same logic applies to systems such as IDE that have precedence relationships
158 like "Master/Slave" or "Primary/Secondary". The master device should be
159 reported first, the slave second.
160
161 @retval EFI_SUCCESS Media information about the specified block device
162 was obtained successfully.
163 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
164 error.
165
166 **/
167 EFI_STATUS
168 EFIAPI
169 SdBlockIoPeimGetMediaInfo (
170 IN EFI_PEI_SERVICES **PeiServices,
171 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
172 IN UINTN DeviceIndex,
173 OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo
174 );
175
176 /**
177 Reads the requested number of blocks from the specified block device.
178
179 The function reads the requested number of blocks from the device. All the
180 blocks are read, or an error is returned. If there is no media in the device,
181 the function returns EFI_NO_MEDIA.
182
183 @param[in] PeiServices General-purpose services that are available to
184 every PEIM.
185 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
186 @param[in] DeviceIndex Specifies the block device to which the function wants
187 to talk. Because the driver that implements Block I/O
188 PPIs will manage multiple block devices, PPIs that
189 want to talk to a single device must specify the device
190 index that was assigned during the enumeration process.
191 This index is a number from one to NumberBlockDevices.
192 @param[in] StartLBA The starting logical block address (LBA) to read from
193 on the device
194 @param[in] BufferSize The size of the Buffer in bytes. This number must be
195 a multiple of the intrinsic block size of the device.
196 @param[out] Buffer A pointer to the destination buffer for the data.
197 The caller is responsible for the ownership of the
198 buffer.
199
200 @retval EFI_SUCCESS The data was read correctly from the device.
201 @retval EFI_DEVICE_ERROR The device reported an error while attempting
202 to perform the read operation.
203 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
204 valid, or the buffer is not properly aligned.
205 @retval EFI_NO_MEDIA There is no media in the device.
206 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
207 the intrinsic block size of the device.
208
209 **/
210 EFI_STATUS
211 EFIAPI
212 SdBlockIoPeimReadBlocks (
213 IN EFI_PEI_SERVICES **PeiServices,
214 IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This,
215 IN UINTN DeviceIndex,
216 IN EFI_PEI_LBA StartLBA,
217 IN UINTN BufferSize,
218 OUT VOID *Buffer
219 );
220
221 /**
222 Gets the count of block I/O devices that one specific block driver detects.
223
224 This function is used for getting the count of block I/O devices that one
225 specific block driver detects. To the PEI ATAPI driver, it returns the number
226 of all the detected ATAPI devices it detects during the enumeration process.
227 To the PEI legacy floppy driver, it returns the number of all the legacy
228 devices it finds during its enumeration process. If no device is detected,
229 then the function will return zero.
230
231 @param[in] PeiServices General-purpose services that are available
232 to every PEIM.
233 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI
234 instance.
235 @param[out] NumberBlockDevices The number of block I/O devices discovered.
236
237 @retval EFI_SUCCESS The operation performed successfully.
238
239 **/
240 EFI_STATUS
241 EFIAPI
242 SdBlockIoPeimGetDeviceNo2 (
243 IN EFI_PEI_SERVICES **PeiServices,
244 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
245 OUT UINTN *NumberBlockDevices
246 );
247
248 /**
249 Gets a block device's media information.
250
251 This function will provide the caller with the specified block device's media
252 information. If the media changes, calling this function will update the media
253 information accordingly.
254
255 @param[in] PeiServices General-purpose services that are available to every
256 PEIM
257 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
258 @param[in] DeviceIndex Specifies the block device to which the function wants
259 to talk. Because the driver that implements Block I/O
260 PPIs will manage multiple block devices, the PPIs that
261 want to talk to a single device must specify the
262 device index that was assigned during the enumeration
263 process. This index is a number from one to
264 NumberBlockDevices.
265 @param[out] MediaInfo The media information of the specified block media.
266 The caller is responsible for the ownership of this
267 data structure.
268
269 @par Note:
270 The MediaInfo structure describes an enumeration of possible block device
271 types. This enumeration exists because no device paths are actually passed
272 across interfaces that describe the type or class of hardware that is publishing
273 the block I/O interface. This enumeration will allow for policy decisions
274 in the Recovery PEIM, such as "Try to recover from legacy floppy first,
275 LS-120 second, CD-ROM third." If there are multiple partitions abstracted
276 by a given device type, they should be reported in ascending order; this
277 order also applies to nested partitions, such as legacy MBR, where the
278 outermost partitions would have precedence in the reporting order. The
279 same logic applies to systems such as IDE that have precedence relationships
280 like "Master/Slave" or "Primary/Secondary". The master device should be
281 reported first, the slave second.
282
283 @retval EFI_SUCCESS Media information about the specified block device
284 was obtained successfully.
285 @retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
286 error.
287
288 **/
289 EFI_STATUS
290 EFIAPI
291 SdBlockIoPeimGetMediaInfo2 (
292 IN EFI_PEI_SERVICES **PeiServices,
293 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
294 IN UINTN DeviceIndex,
295 OUT EFI_PEI_BLOCK_IO2_MEDIA *MediaInfo
296 );
297
298 /**
299 Reads the requested number of blocks from the specified block device.
300
301 The function reads the requested number of blocks from the device. All the
302 blocks are read, or an error is returned. If there is no media in the device,
303 the function returns EFI_NO_MEDIA.
304
305 @param[in] PeiServices General-purpose services that are available to
306 every PEIM.
307 @param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO2_PPI instance.
308 @param[in] DeviceIndex Specifies the block device to which the function wants
309 to talk. Because the driver that implements Block I/O
310 PPIs will manage multiple block devices, PPIs that
311 want to talk to a single device must specify the device
312 index that was assigned during the enumeration process.
313 This index is a number from one to NumberBlockDevices.
314 @param[in] StartLBA The starting logical block address (LBA) to read from
315 on the device
316 @param[in] BufferSize The size of the Buffer in bytes. This number must be
317 a multiple of the intrinsic block size of the device.
318 @param[out] Buffer A pointer to the destination buffer for the data.
319 The caller is responsible for the ownership of the
320 buffer.
321
322 @retval EFI_SUCCESS The data was read correctly from the device.
323 @retval EFI_DEVICE_ERROR The device reported an error while attempting
324 to perform the read operation.
325 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
326 valid, or the buffer is not properly aligned.
327 @retval EFI_NO_MEDIA There is no media in the device.
328 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
329 the intrinsic block size of the device.
330
331 **/
332 EFI_STATUS
333 EFIAPI
334 SdBlockIoPeimReadBlocks2 (
335 IN EFI_PEI_SERVICES **PeiServices,
336 IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This,
337 IN UINTN DeviceIndex,
338 IN EFI_PEI_LBA StartLBA,
339 IN UINTN BufferSize,
340 OUT VOID *Buffer
341 );
342
343 /**
344 Initialize the memory management pool for the host controller.
345
346 @param Private The Sd Peim driver private data.
347
348 @retval EFI_SUCCESS The memory pool is initialized.
349 @retval Others Fail to init the memory pool.
350
351 **/
352 EFI_STATUS
353 SdPeimInitMemPool (
354 IN SD_PEIM_HC_PRIVATE_DATA *Private
355 );
356
357 /**
358 Release the memory management pool.
359
360 @param Pool The memory pool to free.
361
362 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
363 @retval EFI_SUCCESS The memory pool is freed.
364
365 **/
366 EFI_STATUS
367 SdPeimFreeMemPool (
368 IN SD_PEIM_MEM_POOL *Pool
369 );
370
371 /**
372 Allocate some memory from the host controller's memory pool
373 which can be used to communicate with host controller.
374
375 @param Pool The host controller's memory pool.
376 @param Size Size of the memory to allocate.
377
378 @return The allocated memory or NULL.
379
380 **/
381 VOID *
382 SdPeimAllocateMem (
383 IN SD_PEIM_MEM_POOL *Pool,
384 IN UINTN Size
385 );
386
387 /**
388 Free the allocated memory back to the memory pool.
389
390 @param Pool The memory pool of the host controller.
391 @param Mem The memory to free.
392 @param Size The size of the memory to free.
393
394 **/
395 VOID
396 SdPeimFreeMem (
397 IN SD_PEIM_MEM_POOL *Pool,
398 IN VOID *Mem,
399 IN UINTN Size
400 );
401
402 /**
403 Initialize IOMMU.
404 **/
405 VOID
406 IoMmuInit (
407 VOID
408 );
409
410 /**
411 Provides the controller-specific addresses required to access system memory from a
412 DMA bus master.
413
414 @param Operation Indicates if the bus master is going to read or write to system memory.
415 @param HostAddress The system memory address to map to the PCI controller.
416 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
417 that were mapped.
418 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
419 access the hosts HostAddress.
420 @param Mapping A resulting value to pass to Unmap().
421
422 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
423 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
424 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
425 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
426 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
427
428 **/
429 EFI_STATUS
430 IoMmuMap (
431 IN EDKII_IOMMU_OPERATION Operation,
432 IN VOID *HostAddress,
433 IN OUT UINTN *NumberOfBytes,
434 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
435 OUT VOID **Mapping
436 );
437
438 /**
439 Completes the Map() operation and releases any corresponding resources.
440
441 @param Mapping The mapping value returned from Map().
442
443 @retval EFI_SUCCESS The range was unmapped.
444 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
445 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
446 **/
447 EFI_STATUS
448 IoMmuUnmap (
449 IN VOID *Mapping
450 );
451
452 /**
453 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
454 OperationBusMasterCommonBuffer64 mapping.
455
456 @param Pages The number of pages to allocate.
457 @param HostAddress A pointer to store the base system memory address of the
458 allocated range.
459 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
460 access the hosts HostAddress.
461 @param Mapping A resulting value to pass to Unmap().
462
463 @retval EFI_SUCCESS The requested memory pages were allocated.
464 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
465 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
466 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
467 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
468
469 **/
470 EFI_STATUS
471 IoMmuAllocateBuffer (
472 IN UINTN Pages,
473 OUT VOID **HostAddress,
474 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
475 OUT VOID **Mapping
476 );
477
478 /**
479 Frees memory that was allocated with AllocateBuffer().
480
481 @param Pages The number of pages to free.
482 @param HostAddress The base system memory address of the allocated range.
483 @param Mapping The mapping value returned from Map().
484
485 @retval EFI_SUCCESS The requested memory pages were freed.
486 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
487 was not allocated with AllocateBuffer().
488
489 **/
490 EFI_STATUS
491 IoMmuFreeBuffer (
492 IN UINTN Pages,
493 IN VOID *HostAddress,
494 IN VOID *Mapping
495 );
496
497 /**
498 One notified function to cleanup the allocated DMA buffers at the end of PEI.
499
500 @param[in] PeiServices Pointer to PEI Services Table.
501 @param[in] NotifyDescriptor Pointer to the descriptor for the Notification
502 event that caused this function to execute.
503 @param[in] Ppi Pointer to the PPI data associated with this function.
504
505 @retval EFI_SUCCESS The function completes successfully
506
507 **/
508 EFI_STATUS
509 EFIAPI
510 SdBlockIoPeimEndOfPei (
511 IN EFI_PEI_SERVICES **PeiServices,
512 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
513 IN VOID *Ppi
514 );
515
516 #endif