]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Bus/Pci/PciBusDxe/PciHotPlugSupport.c
MdeModulePkg/PciBus: Correct typos
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciBusDxe / PciHotPlugSupport.c
... / ...
CommitLineData
1/** @file\r
2 PCI Hot Plug support functions implementation for PCI Bus module..\r
3\r
4Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PciBus.h"\r
16\r
17EFI_PCI_HOT_PLUG_INIT_PROTOCOL *gPciHotPlugInit = NULL;\r
18EFI_HPC_LOCATION *gPciRootHpcPool = NULL;\r
19UINTN gPciRootHpcCount = 0;\r
20ROOT_HPC_DATA *gPciRootHpcData = NULL;\r
21\r
22\r
23/**\r
24 Event notification function to set Hot Plug controller status.\r
25\r
26 @param Event The event that invoke this function.\r
27 @param Context The calling context, pointer to ROOT_HPC_DATA.\r
28\r
29**/\r
30VOID\r
31EFIAPI\r
32PciHPCInitialized (\r
33 IN EFI_EVENT Event,\r
34 IN VOID *Context\r
35 )\r
36{\r
37 ROOT_HPC_DATA *HpcData;\r
38\r
39 HpcData = (ROOT_HPC_DATA *) Context;\r
40 HpcData->Initialized = TRUE;\r
41}\r
42\r
43/**\r
44 Compare two device paths to check if they are exactly same.\r
45\r
46 @param DevicePath1 A pointer to the first device path data structure.\r
47 @param DevicePath2 A pointer to the second device path data structure.\r
48\r
49 @retval TRUE They are same.\r
50 @retval FALSE They are not same.\r
51\r
52**/\r
53BOOLEAN\r
54EfiCompareDevicePath (\r
55 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath1,\r
56 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath2\r
57 )\r
58{\r
59 UINTN Size1;\r
60 UINTN Size2;\r
61\r
62 Size1 = GetDevicePathSize (DevicePath1);\r
63 Size2 = GetDevicePathSize (DevicePath2);\r
64\r
65 if (Size1 != Size2) {\r
66 return FALSE;\r
67 }\r
68\r
69 if (CompareMem (DevicePath1, DevicePath2, Size1) != 0) {\r
70 return FALSE;\r
71 }\r
72\r
73 return TRUE;\r
74}\r
75\r
76/**\r
77 Check hot plug support and initialize root hot plug private data.\r
78\r
79 If Hot Plug is supported by the platform, call PCI Hot Plug Init protocol\r
80 to get PCI Hot Plug controller's information and constructor the root hot plug\r
81 private data structure.\r
82\r
83 @retval EFI_SUCCESS They are same.\r
84 @retval EFI_UNSUPPORTED No PCI Hot Plug controller on the platform.\r
85 @retval EFI_OUT_OF_RESOURCES No memory to constructor root hot plug private\r
86 data structure.\r
87\r
88**/\r
89EFI_STATUS\r
90InitializeHotPlugSupport (\r
91 VOID\r
92 )\r
93{\r
94 EFI_STATUS Status;\r
95 EFI_HPC_LOCATION *HpcList;\r
96 UINTN HpcCount;\r
97\r
98 //\r
99 // Locate the PciHotPlugInit Protocol\r
100 // If it doesn't exist, that means there is no\r
101 // hot plug controller supported on the platform\r
102 // the PCI Bus driver is running on. HotPlug Support\r
103 // is an optional feature, so absence of the protocol\r
104 // won't incur the penalty.\r
105 //\r
106 Status = gBS->LocateProtocol (\r
107 &gEfiPciHotPlugInitProtocolGuid,\r
108 NULL,\r
109 (VOID **) &gPciHotPlugInit\r
110 );\r
111\r
112 if (EFI_ERROR (Status)) {\r
113 return EFI_UNSUPPORTED;\r
114 }\r
115\r
116 Status = gPciHotPlugInit->GetRootHpcList (\r
117 gPciHotPlugInit,\r
118 &HpcCount,\r
119 &HpcList\r
120 );\r
121\r
122 if (!EFI_ERROR (Status)) {\r
123\r
124 gPciRootHpcPool = HpcList;\r
125 gPciRootHpcCount = HpcCount;\r
126 gPciRootHpcData = AllocateZeroPool (sizeof (ROOT_HPC_DATA) * gPciRootHpcCount);\r
127 if (gPciRootHpcData == NULL) {\r
128 return EFI_OUT_OF_RESOURCES;\r
129 }\r
130 }\r
131\r
132 return EFI_SUCCESS;\r
133}\r
134\r
135/**\r
136 Test whether device path is for root pci hot plug bus.\r
137\r
138 @param HpbDevicePath A pointer to device path data structure to be tested.\r
139 @param HpIndex If HpIndex is not NULL, return the index of root hot\r
140 plug in global array when TRUE is returned.\r
141\r
142 @retval TRUE The device path is for root pci hot plug bus.\r
143 @retval FALSE The device path is not for root pci hot plug bus.\r
144\r
145**/\r
146BOOLEAN\r
147IsRootPciHotPlugBus (\r
148 IN EFI_DEVICE_PATH_PROTOCOL *HpbDevicePath,\r
149 OUT UINTN *HpIndex OPTIONAL\r
150 )\r
151{\r
152 UINTN Index;\r
153\r
154 for (Index = 0; Index < gPciRootHpcCount; Index++) {\r
155\r
156 if (EfiCompareDevicePath (gPciRootHpcPool[Index].HpbDevicePath, HpbDevicePath)) {\r
157\r
158 if (HpIndex != NULL) {\r
159 *HpIndex = Index;\r
160 }\r
161\r
162 return TRUE;\r
163 }\r
164 }\r
165\r
166 return FALSE;\r
167}\r
168\r
169/**\r
170 Test whether device path is for root pci hot plug controller.\r
171\r
172 @param HpcDevicePath A pointer to device path data structure to be tested.\r
173 @param HpIndex If HpIndex is not NULL, return the index of root hot\r
174 plug in global array when TRUE is returned.\r
175\r
176 @retval TRUE The device path is for root pci hot plug controller.\r
177 @retval FALSE The device path is not for root pci hot plug controller.\r
178\r
179**/\r
180BOOLEAN\r
181IsRootPciHotPlugController (\r
182 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,\r
183 OUT UINTN *HpIndex\r
184 )\r
185{\r
186 UINTN Index;\r
187\r
188 for (Index = 0; Index < gPciRootHpcCount; Index++) {\r
189\r
190 if (EfiCompareDevicePath (gPciRootHpcPool[Index].HpcDevicePath, HpcDevicePath)) {\r
191\r
192 if (HpIndex != NULL) {\r
193 *HpIndex = Index;\r
194 }\r
195\r
196 return TRUE;\r
197 }\r
198 }\r
199\r
200 return FALSE;\r
201}\r
202\r
203/**\r
204 Creating event object for PCI Hot Plug controller.\r
205\r
206 @param HpIndex Index of hot plug device in global array.\r
207 @param Event The returned event that invoke this function.\r
208\r
209 @return Status of create event.\r
210\r
211**/\r
212EFI_STATUS\r
213CreateEventForHpc (\r
214 IN UINTN HpIndex,\r
215 OUT EFI_EVENT *Event\r
216 )\r
217{\r
218 EFI_STATUS Status;\r
219\r
220 Status = gBS->CreateEvent (\r
221 EVT_NOTIFY_SIGNAL,\r
222 TPL_CALLBACK,\r
223 PciHPCInitialized,\r
224 gPciRootHpcData + HpIndex,\r
225 &((gPciRootHpcData + HpIndex)->Event)\r
226 );\r
227\r
228 if (!EFI_ERROR (Status)) {\r
229 *Event = (gPciRootHpcData + HpIndex)->Event;\r
230 }\r
231\r
232 return Status;\r
233}\r
234\r
235/**\r
236 Wait for all root PCI Hot Plug controller finished initializing.\r
237\r
238 @param TimeoutInMicroSeconds Microseconds to wait for all root HPCs' initialization.\r
239\r
240 @retval EFI_SUCCESS All HPCs initialization finished.\r
241 @retval EFI_TIMEOUT Not ALL HPCs initialization finished in Microseconds.\r
242\r
243**/\r
244EFI_STATUS\r
245AllRootHPCInitialized (\r
246 IN UINTN TimeoutInMicroSeconds\r
247 )\r
248{\r
249 UINT32 Delay;\r
250 UINTN Index;\r
251\r
252 Delay = (UINT32) ((TimeoutInMicroSeconds / 30) + 1);\r
253\r
254 do {\r
255 for (Index = 0; Index < gPciRootHpcCount; Index++) {\r
256\r
257 if (gPciRootHpcData[Index].Found && !gPciRootHpcData[Index].Initialized) {\r
258 break;\r
259 }\r
260 }\r
261\r
262 if (Index == gPciRootHpcCount) {\r
263 return EFI_SUCCESS;\r
264 }\r
265\r
266 //\r
267 // Stall for 30 microseconds..\r
268 //\r
269 gBS->Stall (30);\r
270\r
271 Delay--;\r
272\r
273 } while (Delay > 0);\r
274\r
275 return EFI_TIMEOUT;\r
276}\r
277\r
278/**\r
279 Check whether PCI-PCI bridge has PCI Hot Plug capability register block.\r
280\r
281 @param PciIoDevice A Pointer to the PCI-PCI bridge.\r
282\r
283 @retval TRUE PCI device is HPC.\r
284 @retval FALSE PCI device is not HPC.\r
285\r
286**/\r
287BOOLEAN\r
288IsSHPC (\r
289 IN PCI_IO_DEVICE *PciIoDevice\r
290 )\r
291{\r
292\r
293 EFI_STATUS Status;\r
294 UINT8 Offset;\r
295\r
296 if (PciIoDevice == NULL) {\r
297 return FALSE;\r
298 }\r
299\r
300 Offset = 0;\r
301 Status = LocateCapabilityRegBlock (\r
302 PciIoDevice,\r
303 EFI_PCI_CAPABILITY_ID_SHPC,\r
304 &Offset,\r
305 NULL\r
306 );\r
307\r
308 //\r
309 // If the PCI-PCI bridge has the hot plug controller build-in,\r
310 // then return TRUE;\r
311 //\r
312 if (!EFI_ERROR (Status)) {\r
313 return TRUE;\r
314 }\r
315\r
316 return FALSE;\r
317}\r
318\r
319/**\r
320 Check whether PciIoDevice supports PCIe hotplug.\r
321\r
322 This is equivalent to the following condition:\r
323 - the device is either a PCIe switch downstream port or a root port,\r
324 - and the device has the SlotImplemented bit set in its PCIe capability\r
325 register,\r
326 - and the device has the HotPlugCapable bit set in its slot capabilities\r
327 register.\r
328\r
329 @param[in] PciIoDevice The device being checked.\r
330\r
331 @retval TRUE PciIoDevice is a PCIe port that accepts a hot-plugged device.\r
332 @retval FALSE Otherwise.\r
333\r
334**/\r
335BOOLEAN\r
336SupportsPcieHotplug (\r
337 IN PCI_IO_DEVICE *PciIoDevice\r
338 )\r
339{\r
340 UINT32 Offset;\r
341 EFI_STATUS Status;\r
342 PCI_REG_PCIE_CAPABILITY Capability;\r
343 PCI_REG_PCIE_SLOT_CAPABILITY SlotCapability;\r
344\r
345 if (PciIoDevice == NULL) {\r
346 return FALSE;\r
347 }\r
348\r
349 //\r
350 // Read the PCI Express Capabilities Register\r
351 //\r
352 if (!PciIoDevice->IsPciExp) {\r
353 return FALSE;\r
354 }\r
355 Offset = PciIoDevice->PciExpressCapabilityOffset +\r
356 OFFSET_OF (PCI_CAPABILITY_PCIEXP, Capability);\r
357 Status = PciIoDevice->PciIo.Pci.Read (\r
358 &PciIoDevice->PciIo,\r
359 EfiPciIoWidthUint16,\r
360 Offset,\r
361 1,\r
362 &Capability\r
363 );\r
364 if (EFI_ERROR (Status)) {\r
365 return FALSE;\r
366 }\r
367\r
368 //\r
369 // Check the contents of the register\r
370 //\r
371 switch (Capability.Bits.DevicePortType) {\r
372 case PCIE_DEVICE_PORT_TYPE_ROOT_PORT:\r
373 case PCIE_DEVICE_PORT_TYPE_DOWNSTREAM_PORT:\r
374 break;\r
375 default:\r
376 return FALSE;\r
377 }\r
378 if (!Capability.Bits.SlotImplemented) {\r
379 return FALSE;\r
380 }\r
381\r
382 //\r
383 // Read the Slot Capabilities Register\r
384 //\r
385 Offset = PciIoDevice->PciExpressCapabilityOffset +\r
386 OFFSET_OF (PCI_CAPABILITY_PCIEXP, SlotCapability);\r
387 Status = PciIoDevice->PciIo.Pci.Read (\r
388 &PciIoDevice->PciIo,\r
389 EfiPciIoWidthUint32,\r
390 Offset,\r
391 1,\r
392 &SlotCapability\r
393 );\r
394 if (EFI_ERROR (Status)) {\r
395 return FALSE;\r
396 }\r
397\r
398 //\r
399 // Check the contents of the register\r
400 //\r
401 if (SlotCapability.Bits.HotPlugCapable) {\r
402 return TRUE;\r
403 }\r
404 return FALSE;\r
405}\r
406\r
407/**\r
408 Get resource padding if the specified PCI bridge is a hot plug bus.\r
409\r
410 @param PciIoDevice PCI bridge instance.\r
411\r
412**/\r
413VOID\r
414GetResourcePaddingForHpb (\r
415 IN PCI_IO_DEVICE *PciIoDevice\r
416 )\r
417{\r
418 EFI_STATUS Status;\r
419 EFI_HPC_STATE State;\r
420 UINT64 PciAddress;\r
421 EFI_HPC_PADDING_ATTRIBUTES Attributes;\r
422 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptors;\r
423\r
424 if (IsPciHotPlugBus (PciIoDevice)) {\r
425 //\r
426 // If PCI-PCI bridge device is PCI Hot Plug bus.\r
427 //\r
428 PciAddress = EFI_PCI_ADDRESS (PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->FunctionNumber, 0);\r
429 Status = gPciHotPlugInit->GetResourcePadding (\r
430 gPciHotPlugInit,\r
431 PciIoDevice->DevicePath,\r
432 PciAddress,\r
433 &State,\r
434 (VOID **) &Descriptors,\r
435 &Attributes\r
436 );\r
437\r
438 if (EFI_ERROR (Status)) {\r
439 return;\r
440 }\r
441\r
442 if ((State & EFI_HPC_STATE_ENABLED) != 0 && (State & EFI_HPC_STATE_INITIALIZED) != 0) {\r
443 PciIoDevice->ResourcePaddingDescriptors = Descriptors;\r
444 PciIoDevice->PaddingAttributes = Attributes;\r
445 }\r
446\r
447 return;\r
448 }\r
449}\r
450\r
451/**\r
452 Test whether PCI device is hot plug bus.\r
453\r
454 @param PciIoDevice PCI device instance.\r
455\r
456 @retval TRUE PCI device is a hot plug bus.\r
457 @retval FALSE PCI device is not a hot plug bus.\r
458\r
459**/\r
460BOOLEAN\r
461IsPciHotPlugBus (\r
462 PCI_IO_DEVICE *PciIoDevice\r
463 )\r
464{\r
465 if (IsSHPC (PciIoDevice)) {\r
466 //\r
467 // If the PPB has the hot plug controller build-in,\r
468 // then return TRUE;\r
469 //\r
470 return TRUE;\r
471 }\r
472\r
473 if (SupportsPcieHotplug (PciIoDevice)) {\r
474 //\r
475 // If the PPB is a PCIe root complex port or a switch downstream port, and\r
476 // implements a hot-plug capable slot, then also return TRUE.\r
477 //\r
478 return TRUE;\r
479 }\r
480\r
481 //\r
482 // Otherwise, see if it is a Root HPC\r
483 //\r
484 if(IsRootPciHotPlugBus (PciIoDevice->DevicePath, NULL)) {\r
485 return TRUE;\r
486 }\r
487\r
488 return FALSE;\r
489}\r
490\r