]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridge.c
MdeModulePkg/PciHostBridgeLib: Add ResourceAssigned field
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciHostBridgeDxe / PciHostBridge.c
CommitLineData
4a50cf4e
RN
1/** @file\r
2\r
3 Provides the basic interfaces to abstract a PCI Host Bridge Resource Allocation.\r
4\r
5Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "PciHostBridge.h"\r
17#include "PciRootBridge.h"\r
18#include "PciHostResource.h"\r
19\r
20\r
21EFI_METRONOME_ARCH_PROTOCOL *mMetronome;\r
22EFI_CPU_IO2_PROTOCOL *mCpuIo;\r
23\r
24GLOBAL_REMOVE_IF_UNREFERENCED CHAR16 *mAcpiAddressSpaceTypeStr[] = {\r
25 L"Mem", L"I/O", L"Bus"\r
26};\r
27GLOBAL_REMOVE_IF_UNREFERENCED CHAR16 *mPciResourceTypeStr[] = {\r
28 L"I/O", L"Mem", L"PMem", L"Mem64", L"PMem64", L"Bus"\r
29};\r
30\r
6474f1f1
RN
31/**\r
32 Ensure the compatibility of an IO space descriptor with the IO aperture.\r
33\r
34 The IO space descriptor can come from the GCD IO space map, or it can\r
35 represent a gap between two neighboring IO space descriptors. In the latter\r
36 case, the GcdIoType field is expected to be EfiGcdIoTypeNonExistent.\r
37\r
38 If the IO space descriptor already has type EfiGcdIoTypeIo, then no action is\r
39 taken -- it is by definition compatible with the aperture.\r
40\r
41 Otherwise, the intersection of the IO space descriptor is calculated with the\r
42 aperture. If the intersection is the empty set (no overlap), no action is\r
43 taken; the IO space descriptor is compatible with the aperture.\r
44\r
45 Otherwise, the type of the descriptor is investigated again. If the type is\r
46 EfiGcdIoTypeNonExistent (representing a gap, or a genuine descriptor with\r
47 such a type), then an attempt is made to add the intersection as IO space to\r
48 the GCD IO space map. This ensures continuity for the aperture, and the\r
49 descriptor is deemed compatible with the aperture.\r
50\r
51 Otherwise, the IO space descriptor is incompatible with the IO aperture.\r
52\r
53 @param[in] Base Base address of the aperture.\r
54 @param[in] Length Length of the aperture.\r
55 @param[in] Descriptor The descriptor to ensure compatibility with the\r
56 aperture for.\r
57\r
58 @retval EFI_SUCCESS The descriptor is compatible. The GCD IO space\r
59 map may have been updated, for continuity\r
60 within the aperture.\r
61 @retval EFI_INVALID_PARAMETER The descriptor is incompatible.\r
62 @return Error codes from gDS->AddIoSpace().\r
63**/\r
64EFI_STATUS\r
65IntersectIoDescriptor (\r
66 IN UINT64 Base,\r
67 IN UINT64 Length,\r
68 IN CONST EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor\r
69 )\r
70{\r
71 UINT64 IntersectionBase;\r
72 UINT64 IntersectionEnd;\r
73 EFI_STATUS Status;\r
74\r
75 if (Descriptor->GcdIoType == EfiGcdIoTypeIo) {\r
76 return EFI_SUCCESS;\r
77 }\r
78\r
79 IntersectionBase = MAX (Base, Descriptor->BaseAddress);\r
80 IntersectionEnd = MIN (Base + Length,\r
81 Descriptor->BaseAddress + Descriptor->Length);\r
82 if (IntersectionBase >= IntersectionEnd) {\r
83 //\r
84 // The descriptor and the aperture don't overlap.\r
85 //\r
86 return EFI_SUCCESS;\r
87 }\r
88\r
89 if (Descriptor->GcdIoType == EfiGcdIoTypeNonExistent) {\r
90 Status = gDS->AddIoSpace (EfiGcdIoTypeIo, IntersectionBase,\r
91 IntersectionEnd - IntersectionBase);\r
92\r
93 DEBUG ((EFI_ERROR (Status) ? EFI_D_ERROR : EFI_D_VERBOSE,\r
94 "%a: %a: add [%Lx, %Lx): %r\n", gEfiCallerBaseName, __FUNCTION__,\r
95 IntersectionBase, IntersectionEnd, Status));\r
96 return Status;\r
97 }\r
98\r
99 DEBUG ((EFI_D_ERROR, "%a: %a: desc [%Lx, %Lx) type %u conflicts with "\r
100 "aperture [%Lx, %Lx)\n", gEfiCallerBaseName, __FUNCTION__,\r
101 Descriptor->BaseAddress, Descriptor->BaseAddress + Descriptor->Length,\r
102 (UINT32)Descriptor->GcdIoType, Base, Base + Length));\r
103 return EFI_INVALID_PARAMETER;\r
104}\r
105\r
106/**\r
107 Add IO space to GCD.\r
108 The routine checks the GCD database and only adds those which are\r
109 not added in the specified range to GCD.\r
110\r
111 @param Base Base address of the IO space.\r
112 @param Length Length of the IO space.\r
113\r
114 @retval EFI_SUCCES The IO space was added successfully.\r
115**/\r
116EFI_STATUS\r
117AddIoSpace (\r
118 IN UINT64 Base,\r
119 IN UINT64 Length\r
120 )\r
121{\r
122 EFI_STATUS Status;\r
123 UINTN Index;\r
124 UINTN NumberOfDescriptors;\r
125 EFI_GCD_IO_SPACE_DESCRIPTOR *IoSpaceMap;\r
126\r
127 Status = gDS->GetIoSpaceMap (&NumberOfDescriptors, &IoSpaceMap);\r
128 if (EFI_ERROR (Status)) {\r
129 DEBUG ((EFI_D_ERROR, "%a: %a: GetIoSpaceMap(): %r\n",\r
130 gEfiCallerBaseName, __FUNCTION__, Status));\r
131 return Status;\r
132 }\r
133\r
134 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
135 Status = IntersectIoDescriptor (Base, Length, &IoSpaceMap[Index]);\r
136 if (EFI_ERROR (Status)) {\r
137 goto FreeIoSpaceMap;\r
138 }\r
139 }\r
140\r
141 DEBUG_CODE (\r
142 //\r
143 // Make sure there are adjacent descriptors covering [Base, Base + Length).\r
144 // It is possible that they have not been merged; merging can be prevented\r
145 // by allocation.\r
146 //\r
147 UINT64 CheckBase;\r
148 EFI_STATUS CheckStatus;\r
149 EFI_GCD_IO_SPACE_DESCRIPTOR Descriptor;\r
150\r
151 for (CheckBase = Base;\r
152 CheckBase < Base + Length;\r
153 CheckBase = Descriptor.BaseAddress + Descriptor.Length) {\r
154 CheckStatus = gDS->GetIoSpaceDescriptor (CheckBase, &Descriptor);\r
155 ASSERT_EFI_ERROR (CheckStatus);\r
156 ASSERT (Descriptor.GcdIoType == EfiGcdIoTypeIo);\r
157 }\r
158 );\r
159\r
160FreeIoSpaceMap:\r
161 FreePool (IoSpaceMap);\r
162\r
163 return Status;\r
164}\r
165\r
166/**\r
167 Ensure the compatibility of a memory space descriptor with the MMIO aperture.\r
168\r
169 The memory space descriptor can come from the GCD memory space map, or it can\r
170 represent a gap between two neighboring memory space descriptors. In the\r
171 latter case, the GcdMemoryType field is expected to be\r
172 EfiGcdMemoryTypeNonExistent.\r
173\r
174 If the memory space descriptor already has type\r
175 EfiGcdMemoryTypeMemoryMappedIo, and its capabilities are a superset of the\r
176 required capabilities, then no action is taken -- it is by definition\r
177 compatible with the aperture.\r
178\r
179 Otherwise, the intersection of the memory space descriptor is calculated with\r
180 the aperture. If the intersection is the empty set (no overlap), no action is\r
181 taken; the memory space descriptor is compatible with the aperture.\r
182\r
183 Otherwise, the type of the descriptor is investigated again. If the type is\r
184 EfiGcdMemoryTypeNonExistent (representing a gap, or a genuine descriptor with\r
185 such a type), then an attempt is made to add the intersection as MMIO space\r
186 to the GCD memory space map, with the specified capabilities. This ensures\r
187 continuity for the aperture, and the descriptor is deemed compatible with the\r
188 aperture.\r
189\r
190 Otherwise, the memory space descriptor is incompatible with the MMIO\r
191 aperture.\r
192\r
193 @param[in] Base Base address of the aperture.\r
194 @param[in] Length Length of the aperture.\r
195 @param[in] Capabilities Capabilities required by the aperture.\r
196 @param[in] Descriptor The descriptor to ensure compatibility with the\r
197 aperture for.\r
198\r
199 @retval EFI_SUCCESS The descriptor is compatible. The GCD memory\r
200 space map may have been updated, for\r
201 continuity within the aperture.\r
202 @retval EFI_INVALID_PARAMETER The descriptor is incompatible.\r
203 @return Error codes from gDS->AddMemorySpace().\r
204**/\r
205EFI_STATUS\r
206IntersectMemoryDescriptor (\r
207 IN UINT64 Base,\r
208 IN UINT64 Length,\r
209 IN UINT64 Capabilities,\r
210 IN CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor\r
211 )\r
212{\r
213 UINT64 IntersectionBase;\r
214 UINT64 IntersectionEnd;\r
215 EFI_STATUS Status;\r
216\r
217 if (Descriptor->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo &&\r
218 (Descriptor->Capabilities & Capabilities) == Capabilities) {\r
219 return EFI_SUCCESS;\r
220 }\r
221\r
222 IntersectionBase = MAX (Base, Descriptor->BaseAddress);\r
223 IntersectionEnd = MIN (Base + Length,\r
224 Descriptor->BaseAddress + Descriptor->Length);\r
225 if (IntersectionBase >= IntersectionEnd) {\r
226 //\r
227 // The descriptor and the aperture don't overlap.\r
228 //\r
229 return EFI_SUCCESS;\r
230 }\r
231\r
232 if (Descriptor->GcdMemoryType == EfiGcdMemoryTypeNonExistent) {\r
233 Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo,\r
234 IntersectionBase, IntersectionEnd - IntersectionBase,\r
235 Capabilities);\r
236\r
237 DEBUG ((EFI_ERROR (Status) ? EFI_D_ERROR : EFI_D_VERBOSE,\r
238 "%a: %a: add [%Lx, %Lx): %r\n", gEfiCallerBaseName, __FUNCTION__,\r
239 IntersectionBase, IntersectionEnd, Status));\r
240 return Status;\r
241 }\r
242\r
243 DEBUG ((EFI_D_ERROR, "%a: %a: desc [%Lx, %Lx) type %u cap %Lx conflicts "\r
244 "with aperture [%Lx, %Lx) cap %Lx\n", gEfiCallerBaseName, __FUNCTION__,\r
245 Descriptor->BaseAddress, Descriptor->BaseAddress + Descriptor->Length,\r
246 (UINT32)Descriptor->GcdMemoryType, Descriptor->Capabilities,\r
247 Base, Base + Length, Capabilities));\r
248 return EFI_INVALID_PARAMETER;\r
249}\r
250\r
251/**\r
252 Add MMIO space to GCD.\r
253 The routine checks the GCD database and only adds those which are\r
254 not added in the specified range to GCD.\r
255\r
256 @param Base Base address of the MMIO space.\r
257 @param Length Length of the MMIO space.\r
258 @param Capabilities Capabilities of the MMIO space.\r
259\r
260 @retval EFI_SUCCES The MMIO space was added successfully.\r
261**/\r
262EFI_STATUS\r
263AddMemoryMappedIoSpace (\r
264 IN UINT64 Base,\r
265 IN UINT64 Length,\r
266 IN UINT64 Capabilities\r
267 )\r
268{\r
269 EFI_STATUS Status;\r
270 UINTN Index;\r
271 UINTN NumberOfDescriptors;\r
272 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemorySpaceMap;\r
273\r
274 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemorySpaceMap);\r
275 if (EFI_ERROR (Status)) {\r
276 DEBUG ((EFI_D_ERROR, "%a: %a: GetMemorySpaceMap(): %r\n",\r
277 gEfiCallerBaseName, __FUNCTION__, Status));\r
278 return Status;\r
279 }\r
280\r
281 for (Index = 0; Index < NumberOfDescriptors; Index++) {\r
282 Status = IntersectMemoryDescriptor (Base, Length, Capabilities,\r
283 &MemorySpaceMap[Index]);\r
284 if (EFI_ERROR (Status)) {\r
285 goto FreeMemorySpaceMap;\r
286 }\r
287 }\r
288\r
289 DEBUG_CODE (\r
290 //\r
291 // Make sure there are adjacent descriptors covering [Base, Base + Length).\r
292 // It is possible that they have not been merged; merging can be prevented\r
293 // by allocation and different capabilities.\r
294 //\r
295 UINT64 CheckBase;\r
296 EFI_STATUS CheckStatus;\r
297 EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor;\r
298\r
299 for (CheckBase = Base;\r
300 CheckBase < Base + Length;\r
301 CheckBase = Descriptor.BaseAddress + Descriptor.Length) {\r
302 CheckStatus = gDS->GetMemorySpaceDescriptor (CheckBase, &Descriptor);\r
303 ASSERT_EFI_ERROR (CheckStatus);\r
304 ASSERT (Descriptor.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo);\r
305 ASSERT ((Descriptor.Capabilities & Capabilities) == Capabilities);\r
306 }\r
307 );\r
308\r
309FreeMemorySpaceMap:\r
310 FreePool (MemorySpaceMap);\r
311\r
312 return Status;\r
313}\r
314\r
4a50cf4e
RN
315/**\r
316\r
317 Entry point of this driver.\r
318\r
319 @param ImageHandle Image handle of this driver.\r
320 @param SystemTable Pointer to standard EFI system table.\r
321\r
322 @retval EFI_SUCCESS Succeed.\r
323 @retval EFI_DEVICE_ERROR Fail to install PCI_ROOT_BRIDGE_IO protocol.\r
324\r
325**/\r
326EFI_STATUS\r
327EFIAPI\r
328InitializePciHostBridge (\r
329 IN EFI_HANDLE ImageHandle,\r
330 IN EFI_SYSTEM_TABLE *SystemTable\r
331 )\r
332{\r
333 EFI_STATUS Status;\r
334 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
335 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
336 PCI_ROOT_BRIDGE *RootBridges;\r
337 UINTN RootBridgeCount;\r
338 UINTN Index;\r
339 PCI_ROOT_BRIDGE_APERTURE *MemApertures[4];\r
340 UINTN MemApertureIndex;\r
341\r
342 RootBridges = PciHostBridgeGetRootBridges (&RootBridgeCount);\r
343 if ((RootBridges == NULL) || (RootBridgeCount == 0)) {\r
344 return EFI_UNSUPPORTED;\r
345 }\r
346\r
347 Status = gBS->LocateProtocol (&gEfiMetronomeArchProtocolGuid, NULL, (VOID **) &mMetronome);\r
348 ASSERT_EFI_ERROR (Status);\r
349 Status = gBS->LocateProtocol (&gEfiCpuIo2ProtocolGuid, NULL, (VOID **) &mCpuIo);\r
350 ASSERT_EFI_ERROR (Status);\r
351\r
352 //\r
353 // Most systems in the world including complex servers have only one Host Bridge.\r
354 //\r
355 HostBridge = AllocateZeroPool (sizeof (PCI_HOST_BRIDGE_INSTANCE));\r
356 ASSERT (HostBridge != NULL);\r
357\r
358 HostBridge->Signature = PCI_HOST_BRIDGE_SIGNATURE;\r
359 HostBridge->CanRestarted = TRUE;\r
360 InitializeListHead (&HostBridge->RootBridges);\r
361\r
362 HostBridge->ResAlloc.NotifyPhase = NotifyPhase;\r
363 HostBridge->ResAlloc.GetNextRootBridge = GetNextRootBridge;\r
364 HostBridge->ResAlloc.GetAllocAttributes = GetAttributes;\r
365 HostBridge->ResAlloc.StartBusEnumeration = StartBusEnumeration;\r
366 HostBridge->ResAlloc.SetBusNumbers = SetBusNumbers;\r
367 HostBridge->ResAlloc.SubmitResources = SubmitResources;\r
368 HostBridge->ResAlloc.GetProposedResources = GetProposedResources;\r
369 HostBridge->ResAlloc.PreprocessController = PreprocessController;\r
370\r
371 Status = gBS->InstallMultipleProtocolInterfaces (\r
372 &HostBridge->Handle,\r
373 &gEfiPciHostBridgeResourceAllocationProtocolGuid, &HostBridge->ResAlloc,\r
374 NULL\r
375 );\r
6474f1f1 376 ASSERT_EFI_ERROR (Status);\r
4a50cf4e
RN
377 if (EFI_ERROR (Status)) {\r
378 FreePool (HostBridge);\r
379 PciHostBridgeFreeRootBridges (RootBridges, RootBridgeCount);\r
380 return Status;\r
381 }\r
382\r
383 //\r
384 // Create Root Bridge Device Handle in this Host Bridge\r
385 //\r
386 for (Index = 0; Index < RootBridgeCount; Index++) {\r
387 //\r
388 // Create Root Bridge Handle Instance\r
389 //\r
390 RootBridge = CreateRootBridge (&RootBridges[Index], HostBridge->Handle);\r
391 ASSERT (RootBridge != NULL);\r
392 if (RootBridge == NULL) {\r
393 continue;\r
394 }\r
395\r
f9607bef 396 if (RootBridges[Index].Io.Base <= RootBridges[Index].Io.Limit) {\r
6474f1f1
RN
397 Status = AddIoSpace (\r
398 RootBridges[Index].Io.Base,\r
399 RootBridges[Index].Io.Limit - RootBridges[Index].Io.Base + 1\r
400 );\r
4a50cf4e
RN
401 ASSERT_EFI_ERROR (Status);\r
402 }\r
403\r
404 //\r
405 // Add all the Mem/PMem aperture to GCD\r
406 // Mem/PMem shouldn't overlap with each other\r
407 // Root bridge which needs to combine MEM and PMEM should only report\r
408 // the MEM aperture in Mem\r
409 //\r
410 MemApertures[0] = &RootBridges[Index].Mem;\r
411 MemApertures[1] = &RootBridges[Index].MemAbove4G;\r
412 MemApertures[2] = &RootBridges[Index].PMem;\r
413 MemApertures[3] = &RootBridges[Index].PMemAbove4G;\r
414\r
415 for (MemApertureIndex = 0; MemApertureIndex < sizeof (MemApertures) / sizeof (MemApertures[0]); MemApertureIndex++) {\r
f9607bef 416 if (MemApertures[MemApertureIndex]->Base <= MemApertures[MemApertureIndex]->Limit) {\r
6474f1f1
RN
417 Status = AddMemoryMappedIoSpace (\r
418 MemApertures[MemApertureIndex]->Base,\r
419 MemApertures[MemApertureIndex]->Limit - MemApertures[MemApertureIndex]->Base + 1,\r
420 EFI_MEMORY_UC\r
421 );\r
4a50cf4e
RN
422 ASSERT_EFI_ERROR (Status);\r
423 Status = gDS->SetMemorySpaceAttributes (\r
424 MemApertures[MemApertureIndex]->Base,\r
425 MemApertures[MemApertureIndex]->Limit - MemApertures[MemApertureIndex]->Base + 1,\r
426 EFI_MEMORY_UC\r
427 );\r
13be90fa
RN
428 if (EFI_ERROR (Status)) {\r
429 DEBUG ((DEBUG_WARN, "PciHostBridge driver failed to set EFI_MEMORY_UC to MMIO aperture - %r.\n", Status));\r
430 }\r
4a50cf4e
RN
431 }\r
432 }\r
433 //\r
434 // Insert Root Bridge Handle Instance\r
435 //\r
436 Status = gBS->InstallMultipleProtocolInterfaces (\r
437 &RootBridge->Handle,\r
438 &gEfiDevicePathProtocolGuid, RootBridge->DevicePath,\r
439 &gEfiPciRootBridgeIoProtocolGuid, &RootBridge->RootBridgeIo,\r
440 NULL\r
441 );\r
442 ASSERT_EFI_ERROR (Status);\r
443 InsertTailList (&HostBridge->RootBridges, &RootBridge->Link);\r
444 }\r
445 PciHostBridgeFreeRootBridges (RootBridges, RootBridgeCount);\r
446 return Status;\r
447}\r
448\r
449/**\r
450 This routine constructs the resource descriptors for all root bridges and call PciHostBridgeResourceConflict().\r
451\r
452 @param HostBridge The Host Bridge Instance where the resource adjustment happens.\r
453**/\r
454VOID\r
455ResourceConflict (\r
456 IN PCI_HOST_BRIDGE_INSTANCE *HostBridge\r
457 )\r
458{\r
459 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Resources;\r
460 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
461 EFI_ACPI_END_TAG_DESCRIPTOR *End;\r
462 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
463 LIST_ENTRY *Link;\r
464 UINTN RootBridgeCount;\r
465 PCI_RESOURCE_TYPE Index;\r
466 PCI_RES_NODE *ResAllocNode;\r
467\r
468 RootBridgeCount = 0;\r
469 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
470 ; !IsNull (&HostBridge->RootBridges, Link)\r
471 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
472 ) {\r
473 RootBridgeCount++;\r
474 }\r
475\r
476 Resources = AllocatePool (\r
477 RootBridgeCount * (TypeMax * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR)) +\r
478 sizeof (EFI_ACPI_END_TAG_DESCRIPTOR)\r
479 );\r
480 ASSERT (Resources != NULL);\r
481\r
482 for (Link = GetFirstNode (&HostBridge->RootBridges), Descriptor = Resources\r
483 ; !IsNull (&HostBridge->RootBridges, Link)\r
484 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
485 ) {\r
486 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
487 for (Index = TypeIo; Index < TypeMax; Index++) {\r
488 ResAllocNode = &RootBridge->ResAllocNode[Index];\r
489\r
490 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r
491 Descriptor->Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;\r
492 Descriptor->AddrRangeMin = ResAllocNode->Base;\r
493 Descriptor->AddrRangeMax = ResAllocNode->Alignment;\r
494 Descriptor->AddrLen = ResAllocNode->Length;\r
495 switch (ResAllocNode->Type) {\r
496\r
497 case TypeIo:\r
498 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;\r
499 break;\r
500\r
501 case TypePMem32:\r
502 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
503 case TypeMem32:\r
504 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
505 Descriptor->AddrSpaceGranularity = 32;\r
506 break;\r
507\r
508 case TypePMem64:\r
509 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
510 case TypeMem64:\r
511 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
512 Descriptor->AddrSpaceGranularity = 64;\r
513 break;\r
514\r
515 case TypeBus:\r
516 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_BUS;\r
517 break;\r
518\r
519 default:\r
520 break;\r
521 }\r
522\r
523 Descriptor++;\r
524 }\r
525 //\r
526 // Terminate the root bridge resources.\r
527 //\r
528 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) Descriptor;\r
529 End->Desc = ACPI_END_TAG_DESCRIPTOR;\r
530 End->Checksum = 0x0;\r
531\r
532 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) (End + 1);\r
533 }\r
534 //\r
535 // Terminate the host bridge resources.\r
536 //\r
537 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) Descriptor;\r
538 End->Desc = ACPI_END_TAG_DESCRIPTOR;\r
539 End->Checksum = 0x0;\r
540\r
541 DEBUG ((DEBUG_ERROR, "Call PciHostBridgeResourceConflict().\n"));\r
542 PciHostBridgeResourceConflict (HostBridge->Handle, Resources);\r
543 FreePool (Resources);\r
544}\r
545\r
63b90643
RN
546/**\r
547 Allocate Length of MMIO or IO resource with alignment BitsOfAlignment\r
548 from GCD range [BaseAddress, Limit).\r
549\r
550 @param Mmio TRUE for MMIO and FALSE for IO.\r
551 @param Length Length of the resource to allocate.\r
552 @param BitsOfAlignment Alignment of the resource to allocate.\r
553 @param BaseAddress The starting address the allocation is from.\r
554 @param Limit The ending address the allocation is to.\r
555\r
556 @retval The base address of the allocated resource or MAX_UINT64 if allocation\r
557 fails.\r
558**/\r
4a50cf4e
RN
559UINT64\r
560AllocateResource (\r
561 BOOLEAN Mmio,\r
562 UINT64 Length,\r
563 UINTN BitsOfAlignment,\r
564 UINT64 BaseAddress,\r
565 UINT64 Limit\r
566 )\r
567{\r
568 EFI_STATUS Status;\r
569\r
570 if (BaseAddress < Limit) {\r
571 //\r
572 // Have to make sure Aligment is handled since we are doing direct address allocation\r
573 //\r
574 BaseAddress = ALIGN_VALUE (BaseAddress, LShiftU64 (1, BitsOfAlignment));\r
575\r
576 while (BaseAddress + Length <= Limit + 1) {\r
577 if (Mmio) {\r
578 Status = gDS->AllocateMemorySpace (\r
579 EfiGcdAllocateAddress,\r
580 EfiGcdMemoryTypeMemoryMappedIo,\r
581 BitsOfAlignment,\r
582 Length,\r
583 &BaseAddress,\r
584 gImageHandle,\r
585 NULL\r
586 );\r
587 } else {\r
588 Status = gDS->AllocateIoSpace (\r
589 EfiGcdAllocateAddress,\r
590 EfiGcdIoTypeIo,\r
591 BitsOfAlignment,\r
592 Length,\r
593 &BaseAddress,\r
594 gImageHandle,\r
595 NULL\r
596 );\r
597 }\r
598\r
599 if (!EFI_ERROR (Status)) {\r
600 return BaseAddress;\r
601 }\r
602 BaseAddress += LShiftU64 (1, BitsOfAlignment);\r
603 }\r
604 }\r
605 return MAX_UINT64;\r
606}\r
63b90643 607\r
4a50cf4e
RN
608/**\r
609\r
610 Enter a certain phase of the PCI enumeration process.\r
611\r
612 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL instance.\r
613 @param Phase The phase during enumeration.\r
614\r
615 @retval EFI_SUCCESS Succeed.\r
616 @retval EFI_INVALID_PARAMETER Wrong phase parameter passed in.\r
617 @retval EFI_NOT_READY Resources have not been submitted yet.\r
618\r
619**/\r
620EFI_STATUS\r
621EFIAPI\r
622NotifyPhase (\r
623 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
624 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase\r
625 )\r
626{\r
627 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
628 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
629 LIST_ENTRY *Link;\r
630 EFI_PHYSICAL_ADDRESS BaseAddress;\r
4a50cf4e
RN
631 UINTN BitsOfAlignment;\r
632 UINT64 Alignment;\r
633 EFI_STATUS Status;\r
634 EFI_STATUS ReturnStatus;\r
635 PCI_RESOURCE_TYPE Index;\r
636 PCI_RESOURCE_TYPE Index1;\r
637 PCI_RESOURCE_TYPE Index2;\r
638 BOOLEAN ResNodeHandled[TypeMax];\r
639 UINT64 MaxAlignment;\r
640\r
641 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
642\r
643 switch (Phase) {\r
644 case EfiPciHostBridgeBeginEnumeration:\r
645 if (!HostBridge->CanRestarted) {\r
646 return EFI_NOT_READY;\r
647 }\r
648 //\r
649 // Reset Root Bridge\r
650 //\r
651 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
652 ; !IsNull (&HostBridge->RootBridges, Link)\r
653 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
654 ) {\r
655 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
656 for (Index = TypeIo; Index < TypeMax; Index++) {\r
657 RootBridge->ResAllocNode[Index].Type = Index;\r
658 RootBridge->ResAllocNode[Index].Base = 0;\r
659 RootBridge->ResAllocNode[Index].Length = 0;\r
660 RootBridge->ResAllocNode[Index].Status = ResNone;\r
661\r
662 RootBridge->ResourceSubmitted = FALSE;\r
663 }\r
664 }\r
665\r
666 HostBridge->CanRestarted = TRUE;\r
667 break;\r
668\r
669 case EfiPciHostBridgeBeginBusAllocation:\r
670 //\r
671 // No specific action is required here, can perform any chipset specific programing\r
672 //\r
673 HostBridge->CanRestarted = FALSE;\r
674 break;\r
675\r
676 case EfiPciHostBridgeEndBusAllocation:\r
677 //\r
678 // No specific action is required here, can perform any chipset specific programing\r
679 //\r
680 break;\r
681\r
682 case EfiPciHostBridgeBeginResourceAllocation:\r
683 //\r
684 // No specific action is required here, can perform any chipset specific programing\r
685 //\r
686 break;\r
687\r
688 case EfiPciHostBridgeAllocateResources:\r
689 ReturnStatus = EFI_SUCCESS;\r
690\r
691 //\r
692 // Make sure the resource for all root bridges has been submitted.\r
693 //\r
694 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
695 ; !IsNull (&HostBridge->RootBridges, Link)\r
696 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
697 ) {\r
698 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
699 if (!RootBridge->ResourceSubmitted) {\r
700 return EFI_NOT_READY;\r
701 }\r
702 }\r
703\r
704 DEBUG ((EFI_D_INFO, "PciHostBridge: NotifyPhase (AllocateResources)\n"));\r
705 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
706 ; !IsNull (&HostBridge->RootBridges, Link)\r
707 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
708 ) {\r
709 for (Index = TypeIo; Index < TypeBus; Index++) {\r
710 ResNodeHandled[Index] = FALSE;\r
711 }\r
712\r
713 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
714 DEBUG ((EFI_D_INFO, " RootBridge: %s\n", RootBridge->DevicePathStr));\r
715\r
716 for (Index1 = TypeIo; Index1 < TypeBus; Index1++) {\r
717 if (RootBridge->ResAllocNode[Index1].Status == ResNone) {\r
718 ResNodeHandled[Index1] = TRUE;\r
719 } else {\r
720 //\r
721 // Allocate the resource node with max alignment at first\r
722 //\r
723 MaxAlignment = 0;\r
724 Index = TypeMax;\r
725 for (Index2 = TypeIo; Index2 < TypeBus; Index2++) {\r
726 if (ResNodeHandled[Index2]) {\r
727 continue;\r
728 }\r
729 if (MaxAlignment <= RootBridge->ResAllocNode[Index2].Alignment) {\r
730 MaxAlignment = RootBridge->ResAllocNode[Index2].Alignment;\r
731 Index = Index2;\r
732 }\r
733 }\r
734\r
735 ASSERT (Index < TypeMax);\r
736 ResNodeHandled[Index] = TRUE;\r
4a50cf4e
RN
737 Alignment = RootBridge->ResAllocNode[Index].Alignment;\r
738 BitsOfAlignment = LowBitSet64 (Alignment + 1);\r
739 BaseAddress = MAX_UINT64;\r
740\r
741 switch (Index) {\r
742 case TypeIo:\r
743 BaseAddress = AllocateResource (\r
744 FALSE,\r
745 RootBridge->ResAllocNode[Index].Length,\r
746 MIN (15, BitsOfAlignment),\r
747 ALIGN_VALUE (RootBridge->Io.Base, Alignment + 1),\r
748 RootBridge->Io.Limit\r
749 );\r
750 break;\r
751\r
752 case TypeMem64:\r
753 BaseAddress = AllocateResource (\r
754 TRUE,\r
755 RootBridge->ResAllocNode[Index].Length,\r
756 MIN (63, BitsOfAlignment),\r
757 ALIGN_VALUE (RootBridge->MemAbove4G.Base, Alignment + 1),\r
758 RootBridge->MemAbove4G.Limit\r
759 );\r
760 if (BaseAddress != MAX_UINT64) {\r
761 break;\r
762 }\r
763 //\r
764 // If memory above 4GB is not available, try memory below 4GB\r
765 //\r
766\r
767 case TypeMem32:\r
768 BaseAddress = AllocateResource (\r
769 TRUE,\r
770 RootBridge->ResAllocNode[Index].Length,\r
771 MIN (31, BitsOfAlignment),\r
772 ALIGN_VALUE (RootBridge->Mem.Base, Alignment + 1),\r
773 RootBridge->Mem.Limit\r
774 );\r
775 break;\r
776\r
777 case TypePMem64:\r
778 BaseAddress = AllocateResource (\r
779 TRUE,\r
780 RootBridge->ResAllocNode[Index].Length,\r
781 MIN (63, BitsOfAlignment),\r
782 ALIGN_VALUE (RootBridge->PMemAbove4G.Base, Alignment + 1),\r
783 RootBridge->PMemAbove4G.Limit\r
784 );\r
785 if (BaseAddress != MAX_UINT64) {\r
786 break;\r
787 }\r
788 //\r
789 // If memory above 4GB is not available, try memory below 4GB\r
790 //\r
791 case TypePMem32:\r
792 BaseAddress = AllocateResource (\r
793 TRUE,\r
794 RootBridge->ResAllocNode[Index].Length,\r
795 MIN (31, BitsOfAlignment),\r
796 ALIGN_VALUE (RootBridge->PMem.Base, Alignment + 1),\r
797 RootBridge->PMem.Limit\r
798 );\r
799 break;\r
800\r
801 default:\r
802 ASSERT (FALSE);\r
803 break;\r
804 }\r
805\r
806 DEBUG ((DEBUG_INFO, " %s: Base/Length/Alignment = %lx/%lx/%lx - ",\r
807 mPciResourceTypeStr[Index], BaseAddress, RootBridge->ResAllocNode[Index].Length, Alignment));\r
808 if (BaseAddress != MAX_UINT64) {\r
809 RootBridge->ResAllocNode[Index].Base = BaseAddress;\r
810 RootBridge->ResAllocNode[Index].Status = ResAllocated;\r
811 DEBUG ((DEBUG_INFO, "Success\n"));\r
812 } else {\r
813 ReturnStatus = EFI_OUT_OF_RESOURCES;\r
814 DEBUG ((DEBUG_ERROR, "Out Of Resource!\n"));\r
815 }\r
816 }\r
817 }\r
818 }\r
819\r
820 if (ReturnStatus == EFI_OUT_OF_RESOURCES) {\r
821 ResourceConflict (HostBridge);\r
822 }\r
823\r
824 //\r
825 // Set resource to zero for nodes where allocation fails\r
826 //\r
827 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
828 ; !IsNull (&HostBridge->RootBridges, Link)\r
829 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
830 ) {\r
831 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
832 for (Index = TypeIo; Index < TypeBus; Index++) {\r
833 if (RootBridge->ResAllocNode[Index].Status != ResAllocated) {\r
834 RootBridge->ResAllocNode[Index].Length = 0;\r
835 }\r
836 }\r
837 }\r
838 return ReturnStatus;\r
839\r
840 case EfiPciHostBridgeSetResources:\r
841 //\r
842 // HostBridgeInstance->CanRestarted = FALSE;\r
843 //\r
844 break;\r
845\r
846 case EfiPciHostBridgeFreeResources:\r
847 //\r
848 // HostBridgeInstance->CanRestarted = FALSE;\r
849 //\r
850 ReturnStatus = EFI_SUCCESS;\r
851 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
852 ; !IsNull (&HostBridge->RootBridges, Link)\r
853 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
854 ) {\r
855 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
856 for (Index = TypeIo; Index < TypeBus; Index++) {\r
857 if (RootBridge->ResAllocNode[Index].Status == ResAllocated) {\r
858 switch (Index) {\r
859 case TypeIo:\r
860 Status = gDS->FreeIoSpace (RootBridge->ResAllocNode[Index].Base, RootBridge->ResAllocNode[Index].Length);\r
861 if (EFI_ERROR (Status)) {\r
862 ReturnStatus = Status;\r
863 }\r
864 break;\r
865\r
866 case TypeMem32:\r
867 case TypePMem32:\r
868 case TypeMem64:\r
869 case TypePMem64:\r
870 Status = gDS->FreeMemorySpace (RootBridge->ResAllocNode[Index].Base, RootBridge->ResAllocNode[Index].Length);\r
871 if (EFI_ERROR (Status)) {\r
872 ReturnStatus = Status;\r
873 }\r
874 break;\r
875\r
876 default:\r
877 ASSERT (FALSE);\r
878 break;\r
879 }\r
880\r
881 RootBridge->ResAllocNode[Index].Type = Index;\r
882 RootBridge->ResAllocNode[Index].Base = 0;\r
883 RootBridge->ResAllocNode[Index].Length = 0;\r
884 RootBridge->ResAllocNode[Index].Status = ResNone;\r
885 }\r
886 }\r
887\r
888 RootBridge->ResourceSubmitted = FALSE;\r
889 }\r
890\r
891 HostBridge->CanRestarted = TRUE;\r
892 return ReturnStatus;\r
893\r
894 case EfiPciHostBridgeEndResourceAllocation:\r
895 //\r
896 // The resource allocation phase is completed. No specific action is required\r
897 // here. This notification can be used to perform any chipset specific programming.\r
898 //\r
899 break;\r
900\r
901 case EfiPciHostBridgeEndEnumeration:\r
902 //\r
903 // The Host Bridge Enumeration is completed. No specific action is required here.\r
904 // This notification can be used to perform any chipset specific programming.\r
905 //\r
906 break;\r
907\r
908 default:\r
909 return EFI_INVALID_PARAMETER;\r
910 }\r
911\r
912 return EFI_SUCCESS;\r
913}\r
914\r
915/**\r
916\r
917 Return the device handle of the next PCI root bridge that is associated with\r
918 this Host Bridge.\r
919\r
920 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
921 @param RootBridgeHandle Returns the device handle of the next PCI Root Bridge.\r
922 On input, it holds the RootBridgeHandle returned by the most\r
923 recent call to GetNextRootBridge().The handle for the first\r
924 PCI Root Bridge is returned if RootBridgeHandle is NULL on input.\r
925\r
926 @retval EFI_SUCCESS Succeed.\r
927 @retval EFI_NOT_FOUND Next PCI root bridge not found.\r
928 @retval EFI_INVALID_PARAMETER Wrong parameter passed in.\r
929\r
930**/\r
931EFI_STATUS\r
932EFIAPI\r
933GetNextRootBridge (\r
934 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
935 IN OUT EFI_HANDLE *RootBridgeHandle\r
936 )\r
937{\r
938 BOOLEAN ReturnNext;\r
939 LIST_ENTRY *Link;\r
940 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
941 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
942\r
943 if (RootBridgeHandle == NULL) {\r
944 return EFI_INVALID_PARAMETER;\r
945 }\r
946\r
947 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
948 ReturnNext = (BOOLEAN) (*RootBridgeHandle == NULL);\r
949\r
950 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
951 ; !IsNull (&HostBridge->RootBridges, Link)\r
952 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
953 ) {\r
954 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
955 if (ReturnNext) {\r
956 *RootBridgeHandle = RootBridge->Handle;\r
957 return EFI_SUCCESS;\r
958 }\r
959\r
960 ReturnNext = (BOOLEAN) (*RootBridgeHandle == RootBridge->Handle);\r
961 }\r
962\r
963 if (ReturnNext) {\r
964 ASSERT (IsNull (&HostBridge->RootBridges, Link));\r
965 return EFI_NOT_FOUND;\r
966 } else {\r
967 return EFI_INVALID_PARAMETER;\r
968 }\r
969}\r
970\r
971/**\r
972\r
973 Returns the attributes of a PCI Root Bridge.\r
974\r
975 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
976 @param RootBridgeHandle The device handle of the PCI Root Bridge\r
977 that the caller is interested in.\r
978 @param Attributes The pointer to attributes of the PCI Root Bridge.\r
979\r
980 @retval EFI_SUCCESS Succeed.\r
981 @retval EFI_INVALID_PARAMETER Attributes parameter passed in is NULL or\r
982 RootBridgeHandle is not an EFI_HANDLE\r
983 that was returned on a previous call to\r
984 GetNextRootBridge().\r
985\r
986**/\r
987EFI_STATUS\r
988EFIAPI\r
989GetAttributes (\r
990 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
991 IN EFI_HANDLE RootBridgeHandle,\r
992 OUT UINT64 *Attributes\r
993 )\r
994{\r
995 LIST_ENTRY *Link;\r
996 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
997 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
998\r
999 if (Attributes == NULL) {\r
1000 return EFI_INVALID_PARAMETER;\r
1001 }\r
1002\r
1003 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
1004 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
1005 ; !IsNull (&HostBridge->RootBridges, Link)\r
1006 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
1007 ) {\r
1008 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
1009 if (RootBridgeHandle == RootBridge->Handle) {\r
1010 *Attributes = RootBridge->AllocationAttributes;\r
1011 return EFI_SUCCESS;\r
1012 }\r
1013 }\r
1014\r
1015 return EFI_INVALID_PARAMETER;\r
1016}\r
1017\r
1018/**\r
1019\r
1020 This is the request from the PCI enumerator to set up\r
1021 the specified PCI Root Bridge for bus enumeration process.\r
1022\r
1023 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
1024 @param RootBridgeHandle The PCI Root Bridge to be set up.\r
1025 @param Configuration Pointer to the pointer to the PCI bus resource descriptor.\r
1026\r
1027 @retval EFI_SUCCESS Succeed.\r
1028 @retval EFI_OUT_OF_RESOURCES Not enough pool to be allocated.\r
1029 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid handle.\r
1030\r
1031**/\r
1032EFI_STATUS\r
1033EFIAPI\r
1034StartBusEnumeration (\r
1035 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
1036 IN EFI_HANDLE RootBridgeHandle,\r
1037 OUT VOID **Configuration\r
1038 )\r
1039{\r
1040 LIST_ENTRY *Link;\r
1041 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
1042 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1043 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
1044 EFI_ACPI_END_TAG_DESCRIPTOR *End;\r
1045\r
1046 if (Configuration == NULL) {\r
1047 return EFI_INVALID_PARAMETER;\r
1048 }\r
1049\r
1050 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
1051 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
1052 ; !IsNull (&HostBridge->RootBridges, Link)\r
1053 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
1054 ) {\r
1055 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
1056 if (RootBridgeHandle == RootBridge->Handle) {\r
1057 *Configuration = AllocatePool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR));\r
1058 if (*Configuration == NULL) {\r
1059 return EFI_OUT_OF_RESOURCES;\r
1060 }\r
1061\r
1062 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) *Configuration;\r
1063 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r
1064 Descriptor->Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;\r
1065 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_BUS;\r
1066 Descriptor->GenFlag = 0;\r
1067 Descriptor->SpecificFlag = 0;\r
1068 Descriptor->AddrSpaceGranularity = 0;\r
1069 Descriptor->AddrRangeMin = RootBridge->Bus.Base;\r
1070 Descriptor->AddrRangeMax = 0;\r
1071 Descriptor->AddrTranslationOffset = 0;\r
1072 Descriptor->AddrLen = RootBridge->Bus.Limit - RootBridge->Bus.Base + 1;\r
1073\r
1074 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) (Descriptor + 1);\r
1075 End->Desc = ACPI_END_TAG_DESCRIPTOR;\r
1076 End->Checksum = 0x0;\r
1077\r
1078 return EFI_SUCCESS;\r
1079 }\r
1080 }\r
1081\r
1082 return EFI_INVALID_PARAMETER;\r
1083}\r
1084\r
1085/**\r
1086\r
1087 This function programs the PCI Root Bridge hardware so that\r
1088 it decodes the specified PCI bus range.\r
1089\r
1090 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
1091 @param RootBridgeHandle The PCI Root Bridge whose bus range is to be programmed.\r
1092 @param Configuration The pointer to the PCI bus resource descriptor.\r
1093\r
1094 @retval EFI_SUCCESS Succeed.\r
1095 @retval EFI_INVALID_PARAMETER Wrong parameters passed in.\r
1096\r
1097**/\r
1098EFI_STATUS\r
1099EFIAPI\r
1100SetBusNumbers (\r
1101 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
1102 IN EFI_HANDLE RootBridgeHandle,\r
1103 IN VOID *Configuration\r
1104 )\r
1105{\r
1106 LIST_ENTRY *Link;\r
1107 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
1108 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1109 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
1110 EFI_ACPI_END_TAG_DESCRIPTOR *End;\r
4a50cf4e
RN
1111\r
1112 if (Configuration == NULL) {\r
1113 return EFI_INVALID_PARAMETER;\r
1114 }\r
1115\r
1116 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration;\r
1117 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) (Descriptor + 1);\r
1118\r
1119 //\r
1120 // Check the Configuration is valid\r
1121 //\r
1122 if ((Descriptor->Desc != ACPI_ADDRESS_SPACE_DESCRIPTOR) ||\r
1123 (Descriptor->ResType != ACPI_ADDRESS_SPACE_TYPE_BUS) ||\r
1124 (End->Desc != ACPI_END_TAG_DESCRIPTOR)\r
1125 ) {\r
1126 return EFI_INVALID_PARAMETER;\r
1127 }\r
1128\r
1129 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
1130 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
1131 ; !IsNull (&HostBridge->RootBridges, Link)\r
1132 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
1133 ) {\r
1134 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
1135 if (RootBridgeHandle == RootBridge->Handle) {\r
4a50cf4e
RN
1136\r
1137 if (Descriptor->AddrLen == 0) {\r
1138 return EFI_INVALID_PARAMETER;\r
1139 }\r
1140\r
1141 if ((Descriptor->AddrRangeMin < RootBridge->Bus.Base) ||\r
1142 (Descriptor->AddrRangeMin + Descriptor->AddrLen - 1 > RootBridge->Bus.Limit)\r
1143 ) {\r
1144 return EFI_INVALID_PARAMETER;\r
1145 }\r
1146 //\r
1147 // Update the Bus Range\r
1148 //\r
1149 RootBridge->ResAllocNode[TypeBus].Base = Descriptor->AddrRangeMin;\r
1150 RootBridge->ResAllocNode[TypeBus].Length = Descriptor->AddrLen;\r
1151 RootBridge->ResAllocNode[TypeBus].Status = ResAllocated;\r
1152 return EFI_SUCCESS;\r
1153 }\r
1154 }\r
1155\r
1156 return EFI_INVALID_PARAMETER;\r
1157}\r
1158\r
1159/**\r
1160\r
1161 Submits the I/O and memory resource requirements for the specified PCI Root Bridge.\r
1162\r
1163 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
1164 @param RootBridgeHandle The PCI Root Bridge whose I/O and memory resource requirements.\r
1165 are being submitted.\r
1166 @param Configuration The pointer to the PCI I/O and PCI memory resource descriptor.\r
1167\r
1168 @retval EFI_SUCCESS Succeed.\r
1169 @retval EFI_INVALID_PARAMETER Wrong parameters passed in.\r
1170**/\r
1171EFI_STATUS\r
1172EFIAPI\r
1173SubmitResources (\r
1174 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
1175 IN EFI_HANDLE RootBridgeHandle,\r
1176 IN VOID *Configuration\r
1177 )\r
1178{\r
1179 LIST_ENTRY *Link;\r
1180 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
1181 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1182 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
1183 PCI_RESOURCE_TYPE Type;\r
1184\r
1185 //\r
1186 // Check the input parameter: Configuration\r
1187 //\r
1188 if (Configuration == NULL) {\r
1189 return EFI_INVALID_PARAMETER;\r
1190 }\r
1191\r
1192 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
1193 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
1194 ; !IsNull (&HostBridge->RootBridges, Link)\r
1195 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
1196 ) {\r
1197 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
1198 if (RootBridgeHandle == RootBridge->Handle) {\r
1199 DEBUG ((EFI_D_INFO, "PciHostBridge: SubmitResources for %s\n", RootBridge->DevicePathStr));\r
1200 //\r
1201 // Check the resource descriptors.\r
1202 // If the Configuration includes one or more invalid resource descriptors, all the resource\r
1203 // descriptors are ignored and the function returns EFI_INVALID_PARAMETER.\r
1204 //\r
1205 for (Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration; Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR; Descriptor++) {\r
1206 if (Descriptor->ResType > ACPI_ADDRESS_SPACE_TYPE_BUS) {\r
1207 return EFI_INVALID_PARAMETER;\r
1208 }\r
1209\r
1210 DEBUG ((EFI_D_INFO, " %s: Granularity/SpecificFlag = %ld / %02x%s\n",\r
1211 mAcpiAddressSpaceTypeStr[Descriptor->ResType], Descriptor->AddrSpaceGranularity, Descriptor->SpecificFlag,\r
1212 (Descriptor->SpecificFlag & EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE) != 0 ? L" (Prefetchable)" : L""\r
1213 ));\r
1214 DEBUG ((EFI_D_INFO, " Length/Alignment = 0x%lx / 0x%lx\n", Descriptor->AddrLen, Descriptor->AddrRangeMax));\r
1215 switch (Descriptor->ResType) {\r
1216 case ACPI_ADDRESS_SPACE_TYPE_MEM:\r
1217 if (Descriptor->AddrSpaceGranularity != 32 && Descriptor->AddrSpaceGranularity != 64) {\r
1218 return EFI_INVALID_PARAMETER;\r
1219 }\r
1220 if (Descriptor->AddrSpaceGranularity == 32 && Descriptor->AddrLen >= SIZE_4GB) {\r
1221 return EFI_INVALID_PARAMETER;\r
1222 }\r
1223 //\r
1224 // If the PCI root bridge does not support separate windows for nonprefetchable and\r
1225 // prefetchable memory, then the PCI bus driver needs to include requests for\r
1226 // prefetchable memory in the nonprefetchable memory pool.\r
1227 //\r
1228 if (((RootBridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM) != 0) &&\r
1229 ((Descriptor->SpecificFlag & EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE) != 0)\r
1230 ) {\r
1231 return EFI_INVALID_PARAMETER;\r
1232 }\r
1233 case ACPI_ADDRESS_SPACE_TYPE_IO:\r
1234 //\r
1235 // Check aligment, it should be of the form 2^n-1\r
1236 //\r
1237 if (GetPowerOfTwo64 (Descriptor->AddrRangeMax + 1) != (Descriptor->AddrRangeMax + 1)) {\r
1238 return EFI_INVALID_PARAMETER;\r
1239 }\r
1240 break;\r
1241 default:\r
1242 ASSERT (FALSE);\r
1243 break;\r
1244 }\r
1245 }\r
1246 if (Descriptor->Desc != ACPI_END_TAG_DESCRIPTOR) {\r
1247 return EFI_INVALID_PARAMETER;\r
1248 }\r
1249\r
1250 for (Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration; Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR; Descriptor++) {\r
1251 if (Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {\r
1252 if (Descriptor->AddrSpaceGranularity == 32) {\r
1253 if ((Descriptor->SpecificFlag & EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE) != 0) {\r
1254 Type = TypePMem32;\r
1255 } else {\r
1256 Type = TypeMem32;\r
1257 }\r
1258 } else {\r
1259 ASSERT (Descriptor->AddrSpaceGranularity == 64);\r
1260 if ((Descriptor->SpecificFlag & EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE) != 0) {\r
1261 Type = TypePMem64;\r
1262 } else {\r
1263 Type = TypeMem64;\r
1264 }\r
1265 }\r
1266 } else {\r
1267 ASSERT (Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_IO);\r
1268 Type = TypeIo;\r
1269 }\r
1270 RootBridge->ResAllocNode[Type].Length = Descriptor->AddrLen;\r
1271 RootBridge->ResAllocNode[Type].Alignment = Descriptor->AddrRangeMax;\r
1272 RootBridge->ResAllocNode[Type].Status = ResSubmitted;\r
1273 }\r
1274 RootBridge->ResourceSubmitted = TRUE;\r
1275 return EFI_SUCCESS;\r
1276 }\r
1277 }\r
1278\r
1279 return EFI_INVALID_PARAMETER;\r
1280}\r
1281\r
1282/**\r
1283\r
1284 This function returns the proposed resource settings for the specified\r
1285 PCI Root Bridge.\r
1286\r
1287 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
1288 @param RootBridgeHandle The PCI Root Bridge handle.\r
1289 @param Configuration The pointer to the pointer to the PCI I/O\r
1290 and memory resource descriptor.\r
1291\r
1292 @retval EFI_SUCCESS Succeed.\r
1293 @retval EFI_OUT_OF_RESOURCES Not enough pool to be allocated.\r
1294 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid handle.\r
1295\r
1296**/\r
1297EFI_STATUS\r
1298EFIAPI\r
1299GetProposedResources (\r
1300 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
1301 IN EFI_HANDLE RootBridgeHandle,\r
1302 OUT VOID **Configuration\r
1303 )\r
1304{\r
1305 LIST_ENTRY *Link;\r
1306 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
1307 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1308 UINTN Index;\r
1309 UINTN Number;\r
1310 VOID *Buffer;\r
1311 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
1312 EFI_ACPI_END_TAG_DESCRIPTOR *End;\r
1313 UINT64 ResStatus;\r
1314\r
1315 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
1316 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
1317 ; !IsNull (&HostBridge->RootBridges, Link)\r
1318 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
1319 ) {\r
1320 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
1321 if (RootBridgeHandle == RootBridge->Handle) {\r
1322 for (Index = 0, Number = 0; Index < TypeBus; Index++) {\r
1323 if (RootBridge->ResAllocNode[Index].Status != ResNone) {\r
1324 Number++;\r
1325 }\r
1326 }\r
1327\r
1328 Buffer = AllocateZeroPool (Number * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR));\r
1329 if (Buffer == NULL) {\r
1330 return EFI_OUT_OF_RESOURCES;\r
1331 }\r
1332\r
1333 Descriptor = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Buffer;\r
1334 for (Index = 0; Index < TypeBus; Index++) {\r
1335 ResStatus = RootBridge->ResAllocNode[Index].Status;\r
1336 if (ResStatus != ResNone) {\r
1337 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r
1338 Descriptor->Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;;\r
1339 Descriptor->GenFlag = 0;\r
1340 Descriptor->AddrRangeMin = RootBridge->ResAllocNode[Index].Base;\r
1341 Descriptor->AddrRangeMax = 0;\r
1342 Descriptor->AddrTranslationOffset = (ResStatus == ResAllocated) ? EFI_RESOURCE_SATISFIED : PCI_RESOURCE_LESS;\r
1343 Descriptor->AddrLen = RootBridge->ResAllocNode[Index].Length;\r
1344\r
1345 switch (Index) {\r
1346\r
1347 case TypeIo:\r
1348 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;\r
1349 break;\r
1350\r
1351 case TypePMem32:\r
1352 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
1353 case TypeMem32:\r
1354 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1355 Descriptor->AddrSpaceGranularity = 32;\r
1356 break;\r
1357\r
1358 case TypePMem64:\r
1359 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
1360 case TypeMem64:\r
1361 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1362 Descriptor->AddrSpaceGranularity = 64;\r
1363 break;\r
1364 }\r
1365\r
1366 Descriptor++;\r
1367 }\r
1368 }\r
1369 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) Descriptor;\r
1370 End->Desc = ACPI_END_TAG_DESCRIPTOR;\r
1371 End->Checksum = 0;\r
1372\r
1373 *Configuration = Buffer;\r
1374\r
1375 return EFI_SUCCESS;\r
1376 }\r
1377 }\r
1378\r
1379 return EFI_INVALID_PARAMETER;\r
1380}\r
1381\r
1382/**\r
1383\r
1384 This function is called for all the PCI controllers that the PCI\r
1385 bus driver finds. Can be used to Preprogram the controller.\r
1386\r
1387 @param This The EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_ PROTOCOL instance.\r
1388 @param RootBridgeHandle The PCI Root Bridge handle.\r
1389 @param PciAddress Address of the controller on the PCI bus.\r
1390 @param Phase The Phase during resource allocation.\r
1391\r
1392 @retval EFI_SUCCESS Succeed.\r
1393 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid handle.\r
1394\r
1395**/\r
1396EFI_STATUS\r
1397EFIAPI\r
1398PreprocessController (\r
1399 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *This,\r
1400 IN EFI_HANDLE RootBridgeHandle,\r
1401 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress,\r
1402 IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase\r
1403 )\r
1404{\r
1405 LIST_ENTRY *Link;\r
1406 PCI_HOST_BRIDGE_INSTANCE *HostBridge;\r
1407 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1408\r
1409 if ((UINT32) Phase > EfiPciBeforeResourceCollection) {\r
1410 return EFI_INVALID_PARAMETER;\r
1411 }\r
1412\r
1413 HostBridge = PCI_HOST_BRIDGE_FROM_THIS (This);\r
1414 for (Link = GetFirstNode (&HostBridge->RootBridges)\r
1415 ; !IsNull (&HostBridge->RootBridges, Link)\r
1416 ; Link = GetNextNode (&HostBridge->RootBridges, Link)\r
1417 ) {\r
1418 RootBridge = ROOT_BRIDGE_FROM_LINK (Link);\r
1419 if (RootBridgeHandle == RootBridge->Handle) {\r
1420 return EFI_SUCCESS;\r
1421 }\r
1422 }\r
1423\r
1424 return EFI_INVALID_PARAMETER;\r
1425}\r