TianoCore EDK2 master
Loading...
Searching...
No Matches
NonDiscoverableDeviceRegistrationLib.c
Go to the documentation of this file.
1
8#include <PiDxe.h>
9
11
13#include <Library/DebugLib.h>
18
19#include <Protocol/DevicePath.h>
21
33 IN NON_DISCOVERABLE_DEVICE_TYPE Type
34 )
35{
36 switch (Type) {
37 case NonDiscoverableDeviceTypeAhci:
38 return &gEdkiiNonDiscoverableAhciDeviceGuid;
39
40 case NonDiscoverableDeviceTypeAmba:
41 return &gEdkiiNonDiscoverableAmbaDeviceGuid;
42
43 case NonDiscoverableDeviceTypeEhci:
44 return &gEdkiiNonDiscoverableEhciDeviceGuid;
45
46 case NonDiscoverableDeviceTypeNvme:
47 return &gEdkiiNonDiscoverableNvmeDeviceGuid;
48
49 case NonDiscoverableDeviceTypeOhci:
50 return &gEdkiiNonDiscoverableOhciDeviceGuid;
51
52 case NonDiscoverableDeviceTypeSdhci:
53 return &gEdkiiNonDiscoverableSdhciDeviceGuid;
54
55 case NonDiscoverableDeviceTypeUfs:
56 return &gEdkiiNonDiscoverableUfsDeviceGuid;
57
58 case NonDiscoverableDeviceTypeUhci:
59 return &gEdkiiNonDiscoverableUhciDeviceGuid;
60
61 case NonDiscoverableDeviceTypeXhci:
62 return &gEdkiiNonDiscoverableXhciDeviceGuid;
63
64 default:
65 return NULL;
66 }
67}
68
69#pragma pack (1)
70typedef struct {
71 VENDOR_DEVICE_PATH Vendor;
72 UINT64 BaseAddress;
73 UINT8 ResourceType;
76#pragma pack ()
77
101EFIAPI
103 IN NON_DISCOVERABLE_DEVICE_TYPE Type,
104 IN NON_DISCOVERABLE_DEVICE_DMA_TYPE DmaType,
106 IN OUT EFI_HANDLE *Handle OPTIONAL,
107 IN UINTN NumMmioResources,
108 ...
109 )
110{
113 EFI_HANDLE LocalHandle;
114 EFI_STATUS Status;
115 UINTN AllocSize;
116 UINTN Index;
117 VA_LIST Args;
120 UINTN Base, Size;
121
122 if ((Type >= NonDiscoverableDeviceTypeMax) ||
123 (DmaType >= NonDiscoverableDeviceDmaTypeMax) ||
124 (NumMmioResources == 0))
125 {
126 return EFI_INVALID_PARAMETER;
127 }
128
129 if (Handle == NULL) {
130 Handle = &LocalHandle;
131 LocalHandle = NULL;
132 }
133
134 AllocSize = sizeof *Device +
135 NumMmioResources * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) +
137 Device = (NON_DISCOVERABLE_DEVICE *)AllocateZeroPool (AllocSize);
138 if (Device == NULL) {
139 return EFI_OUT_OF_RESOURCES;
140 }
141
142 Device->Type = GetGuidFromType (Type);
143 ASSERT (Device->Type != NULL);
144
145 Device->DmaType = DmaType;
146 Device->Initialize = InitFunc;
147 Device->Resources = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(Device + 1);
148
149 VA_START (Args, NumMmioResources);
150 for (Index = 0; Index < NumMmioResources; Index++) {
151 Desc = &Device->Resources[Index];
152 Base = VA_ARG (Args, UINTN);
153 Size = VA_ARG (Args, UINTN);
154
155 Desc->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
156 Desc->Len = sizeof *Desc - 3;
157 Desc->AddrRangeMin = Base;
158 Desc->AddrLen = Size;
159 Desc->AddrRangeMax = Base + Size - 1;
160 Desc->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
161 Desc->AddrSpaceGranularity = ((EFI_PHYSICAL_ADDRESS)Base + Size > SIZE_4GB) ? 64 : 32;
162 Desc->AddrTranslationOffset = 0;
163 }
164
165 VA_END (Args);
166
167 End = (EFI_ACPI_END_TAG_DESCRIPTOR *)&Device->Resources[NumMmioResources];
168
169 End->Desc = ACPI_END_TAG_DESCRIPTOR;
170 End->Checksum = 0;
171
175 sizeof (*DevicePath)
176 );
177 if (DevicePath == NULL) {
178 Status = EFI_OUT_OF_RESOURCES;
179 goto FreeDevice;
180 }
181
182 CopyGuid (&DevicePath->Vendor.Guid, &gEdkiiNonDiscoverableDeviceProtocolGuid);
183
184 //
185 // Use the base address and type of the first region to
186 // make the device path unique
187 //
188 DevicePath->BaseAddress = Device->Resources[0].AddrRangeMin;
189 DevicePath->ResourceType = Device->Resources[0].ResType;
190
192 &DevicePath->Vendor,
193 sizeof (*DevicePath) - sizeof (DevicePath->End)
194 );
195 SetDevicePathEndNode (&DevicePath->End);
196
197 Status = gBS->InstallMultipleProtocolInterfaces (
198 Handle,
199 &gEdkiiNonDiscoverableDeviceProtocolGuid,
200 Device,
201 &gEfiDevicePathProtocolGuid,
202 DevicePath,
203 NULL
204 );
205 if (EFI_ERROR (Status)) {
206 goto FreeDevicePath;
207 }
208
209 return EFI_SUCCESS;
210
211FreeDevicePath:
212 FreePool (DevicePath);
213
214FreeDevice:
215 FreePool (Device);
216
217 return Status;
218}
UINT64 UINTN
PACKED struct @89 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
#define HARDWARE_DEVICE_PATH
Definition: DevicePath.h:68
#define HW_VENDOR_DP
Definition: DevicePath.h:133
UINT16 EFIAPI SetDevicePathNodeLength(IN OUT VOID *Node, IN UINTN Length)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI CreateDeviceNode(IN UINT8 NodeType, IN UINT8 NodeSubType, IN UINT16 NodeLength)
VOID EFIAPI SetDevicePathEndNode(OUT VOID *Node)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define VA_ARG(Marker, TYPE)
Definition: Base.h:679
#define VA_START(Marker, Parameter)
Definition: Base.h:661
CHAR8 * VA_LIST
Definition: Base.h:643
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define VA_END(Marker)
Definition: Base.h:691
STATIC CONST EFI_GUID * GetGuidFromType(IN NON_DISCOVERABLE_DEVICE_TYPE Type)
EFI_STATUS EFIAPI RegisterNonDiscoverableMmioDevice(IN NON_DISCOVERABLE_DEVICE_TYPE Type, IN NON_DISCOVERABLE_DEVICE_DMA_TYPE DmaType, IN NON_DISCOVERABLE_DEVICE_INIT InitFunc, IN OUT EFI_HANDLE *Handle OPTIONAL, IN UINTN NumMmioResources,...)
EFI_STATUS(EFIAPI * NON_DISCOVERABLE_DEVICE_INIT)(IN NON_DISCOVERABLE_DEVICE *This)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
Definition: Base.h:213