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