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