]>
Commit | Line | Data |
---|---|---|
1652dc21 AB |
1 | /** @file\r |
2 | Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>\r | |
3 | \r | |
9d510e61 | 4 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
1652dc21 AB |
5 | \r |
6 | **/\r | |
7 | \r | |
8 | #include <PiDxe.h>\r | |
9 | \r | |
10 | #include <Guid/NonDiscoverableDevice.h>\r | |
11 | \r | |
12 | #include <Library/BaseMemoryLib.h>\r | |
13 | #include <Library/DebugLib.h>\r | |
14 | #include <Library/DevicePathLib.h>\r | |
15 | #include <Library/MemoryAllocationLib.h>\r | |
16 | #include <Library/NonDiscoverableDeviceRegistrationLib.h>\r | |
17 | #include <Library/UefiBootServicesTableLib.h>\r | |
18 | \r | |
19 | #include <Protocol/DevicePath.h>\r | |
20 | #include <Protocol/NonDiscoverableDevice.h>\r | |
21 | \r | |
9a882490 DB |
22 | /**\r |
23 | Get Guid form the type of non-discoverable device.\r | |
24 | \r | |
25 | @param[in] Type The type of non-discoverable device.\r | |
26 | \r | |
27 | @retval Return the Guid.\r | |
28 | \r | |
29 | **/\r | |
1652dc21 AB |
30 | STATIC\r |
31 | CONST EFI_GUID *\r | |
32 | GetGuidFromType (\r | |
33 | IN NON_DISCOVERABLE_DEVICE_TYPE Type\r | |
34 | )\r | |
35 | {\r | |
36 | switch (Type) {\r | |
37 | case NonDiscoverableDeviceTypeAhci:\r | |
38 | return &gEdkiiNonDiscoverableAhciDeviceGuid;\r | |
39 | \r | |
40 | case NonDiscoverableDeviceTypeAmba:\r | |
41 | return &gEdkiiNonDiscoverableAmbaDeviceGuid;\r | |
42 | \r | |
43 | case NonDiscoverableDeviceTypeEhci:\r | |
44 | return &gEdkiiNonDiscoverableEhciDeviceGuid;\r | |
45 | \r | |
46 | case NonDiscoverableDeviceTypeNvme:\r | |
47 | return &gEdkiiNonDiscoverableNvmeDeviceGuid;\r | |
48 | \r | |
49 | case NonDiscoverableDeviceTypeOhci:\r | |
50 | return &gEdkiiNonDiscoverableOhciDeviceGuid;\r | |
51 | \r | |
52 | case NonDiscoverableDeviceTypeSdhci:\r | |
53 | return &gEdkiiNonDiscoverableSdhciDeviceGuid;\r | |
54 | \r | |
55 | case NonDiscoverableDeviceTypeUfs:\r | |
56 | return &gEdkiiNonDiscoverableUfsDeviceGuid;\r | |
57 | \r | |
58 | case NonDiscoverableDeviceTypeUhci:\r | |
59 | return &gEdkiiNonDiscoverableUhciDeviceGuid;\r | |
60 | \r | |
61 | case NonDiscoverableDeviceTypeXhci:\r | |
62 | return &gEdkiiNonDiscoverableXhciDeviceGuid;\r | |
63 | \r | |
64 | default:\r | |
65 | return NULL;\r | |
66 | }\r | |
67 | }\r | |
68 | \r | |
69 | #pragma pack (1)\r | |
70 | typedef struct {\r | |
71 | VENDOR_DEVICE_PATH Vendor;\r | |
72 | UINT64 BaseAddress;\r | |
73 | UINT8 ResourceType;\r | |
74 | EFI_DEVICE_PATH_PROTOCOL End;\r | |
75 | } NON_DISCOVERABLE_DEVICE_PATH;\r | |
76 | #pragma pack ()\r | |
77 | \r | |
78 | /**\r | |
9a882490 | 79 | Register a non-discoverable MMIO device.\r |
1652dc21 | 80 | \r |
9a882490 | 81 | @param[in] Type The type of non-discoverable device\r |
1652dc21 AB |
82 | @param[in] DmaType Whether the device is DMA coherent\r |
83 | @param[in] InitFunc Initialization routine to be invoked when\r | |
84 | the device is enabled\r | |
85 | @param[in,out] Handle The handle onto which to install the\r | |
86 | non-discoverable device protocol.\r | |
87 | If Handle is NULL or *Handle is NULL, a\r | |
88 | new handle will be allocated.\r | |
89 | @param[in] NumMmioResources The number of UINTN base/size pairs that\r | |
90 | follow, each describing an MMIO region\r | |
91 | owned by the device\r | |
9a882490 DB |
92 | @param[in] ... The variable argument list which contains the\r |
93 | info about MmioResources.\r | |
1652dc21 AB |
94 | \r |
95 | @retval EFI_SUCCESS The registration succeeded.\r | |
96 | @retval EFI_INVALID_PARAMETER An invalid argument was given\r | |
97 | @retval Other The registration failed.\r | |
98 | \r | |
99 | **/\r | |
100 | EFI_STATUS\r | |
101 | EFIAPI\r | |
102 | RegisterNonDiscoverableMmioDevice (\r | |
103 | IN NON_DISCOVERABLE_DEVICE_TYPE Type,\r | |
104 | IN NON_DISCOVERABLE_DEVICE_DMA_TYPE DmaType,\r | |
105 | IN NON_DISCOVERABLE_DEVICE_INIT InitFunc,\r | |
106 | IN OUT EFI_HANDLE *Handle OPTIONAL,\r | |
107 | IN UINTN NumMmioResources,\r | |
108 | ...\r | |
109 | )\r | |
110 | {\r | |
111 | NON_DISCOVERABLE_DEVICE *Device;\r | |
112 | NON_DISCOVERABLE_DEVICE_PATH *DevicePath;\r | |
113 | EFI_HANDLE LocalHandle;\r | |
114 | EFI_STATUS Status;\r | |
115 | UINTN AllocSize;\r | |
116 | UINTN Index;\r | |
117 | VA_LIST Args;\r | |
118 | EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Desc;\r | |
119 | EFI_ACPI_END_TAG_DESCRIPTOR *End;\r | |
120 | UINTN Base, Size;\r | |
121 | \r | |
122 | if (Type >= NonDiscoverableDeviceTypeMax ||\r | |
123 | DmaType >= NonDiscoverableDeviceDmaTypeMax ||\r | |
124 | NumMmioResources == 0) {\r | |
125 | return EFI_INVALID_PARAMETER;\r | |
126 | }\r | |
127 | \r | |
128 | if (Handle == NULL) {\r | |
129 | Handle = &LocalHandle;\r | |
130 | LocalHandle = NULL;\r | |
131 | }\r | |
132 | \r | |
133 | AllocSize = sizeof *Device +\r | |
134 | NumMmioResources * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) +\r | |
135 | sizeof (EFI_ACPI_END_TAG_DESCRIPTOR);\r | |
136 | Device = (NON_DISCOVERABLE_DEVICE *)AllocateZeroPool (AllocSize);\r | |
137 | if (Device == NULL) {\r | |
138 | return EFI_OUT_OF_RESOURCES;\r | |
139 | }\r | |
140 | \r | |
141 | Device->Type = GetGuidFromType (Type);\r | |
142 | ASSERT (Device->Type != NULL);\r | |
143 | \r | |
144 | Device->DmaType = DmaType;\r | |
145 | Device->Initialize = InitFunc;\r | |
146 | Device->Resources = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(Device + 1);\r | |
147 | \r | |
148 | VA_START (Args, NumMmioResources);\r | |
149 | for (Index = 0; Index < NumMmioResources; Index++) {\r | |
150 | Desc = &Device->Resources [Index];\r | |
151 | Base = VA_ARG (Args, UINTN);\r | |
152 | Size = VA_ARG (Args, UINTN);\r | |
153 | \r | |
154 | Desc->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r | |
155 | Desc->Len = sizeof *Desc - 3;\r | |
156 | Desc->AddrRangeMin = Base;\r | |
157 | Desc->AddrLen = Size;\r | |
158 | Desc->AddrRangeMax = Base + Size - 1;\r | |
159 | Desc->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r | |
b494cf96 | 160 | Desc->AddrSpaceGranularity = ((EFI_PHYSICAL_ADDRESS)Base + Size > SIZE_4GB) ? 64 : 32;\r |
1652dc21 AB |
161 | Desc->AddrTranslationOffset = 0;\r |
162 | }\r | |
163 | VA_END (Args);\r | |
164 | \r | |
165 | End = (EFI_ACPI_END_TAG_DESCRIPTOR *)&Device->Resources [NumMmioResources];\r | |
166 | \r | |
167 | End->Desc = ACPI_END_TAG_DESCRIPTOR;\r | |
168 | End->Checksum = 0;\r | |
169 | \r | |
170 | DevicePath = (NON_DISCOVERABLE_DEVICE_PATH *)CreateDeviceNode (\r | |
171 | HARDWARE_DEVICE_PATH,\r | |
172 | HW_VENDOR_DP,\r | |
173 | sizeof (*DevicePath));\r | |
174 | if (DevicePath == NULL) {\r | |
175 | Status = EFI_OUT_OF_RESOURCES;\r | |
176 | goto FreeDevice;\r | |
177 | }\r | |
178 | \r | |
179 | CopyGuid (&DevicePath->Vendor.Guid, &gEdkiiNonDiscoverableDeviceProtocolGuid);\r | |
180 | \r | |
181 | //\r | |
182 | // Use the base address and type of the first region to\r | |
183 | // make the device path unique\r | |
184 | //\r | |
185 | DevicePath->BaseAddress = Device->Resources [0].AddrRangeMin;\r | |
186 | DevicePath->ResourceType = Device->Resources [0].ResType;\r | |
187 | \r | |
188 | SetDevicePathNodeLength (&DevicePath->Vendor,\r | |
189 | sizeof (*DevicePath) - sizeof (DevicePath->End));\r | |
190 | SetDevicePathEndNode (&DevicePath->End);\r | |
191 | \r | |
192 | Status = gBS->InstallMultipleProtocolInterfaces (Handle,\r | |
193 | &gEdkiiNonDiscoverableDeviceProtocolGuid, Device,\r | |
194 | &gEfiDevicePathProtocolGuid, DevicePath,\r | |
195 | NULL);\r | |
196 | if (EFI_ERROR (Status)) {\r | |
197 | goto FreeDevicePath;\r | |
198 | }\r | |
199 | return EFI_SUCCESS;\r | |
200 | \r | |
201 | FreeDevicePath:\r | |
202 | FreePool (DevicePath);\r | |
203 | \r | |
204 | FreeDevice:\r | |
205 | FreePool (Device);\r | |
206 | \r | |
207 | return Status;\r | |
208 | }\r |