]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c
MdeModulePkg/PciHostBridge: Fix a bug that prevents PMEM access
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciHostBridgeDxe / PciRootBridgeIo.c
CommitLineData
4a50cf4e
RN
1/** @file\r
2\r
3 PCI Root Bridge Io Protocol code.\r
4\r
0edb7ec5 5Copyright (c) 1999 - 2018, Intel Corporation. All rights reserved.<BR>\r
4a50cf4e
RN
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
c15da8eb
JY
20extern EDKII_IOMMU_PROTOCOL *mIoMmuProtocol;\r
21\r
ccf66016 22#define NO_MAPPING (VOID *) (UINTN) -1\r
4a50cf4e
RN
23\r
24//\r
25// Lookup table for increment values based on transfer widths\r
26//\r
27UINT8 mInStride[] = {\r
28 1, // EfiPciWidthUint8\r
29 2, // EfiPciWidthUint16\r
30 4, // EfiPciWidthUint32\r
31 8, // EfiPciWidthUint64\r
32 0, // EfiPciWidthFifoUint8\r
33 0, // EfiPciWidthFifoUint16\r
34 0, // EfiPciWidthFifoUint32\r
35 0, // EfiPciWidthFifoUint64\r
36 1, // EfiPciWidthFillUint8\r
37 2, // EfiPciWidthFillUint16\r
38 4, // EfiPciWidthFillUint32\r
39 8 // EfiPciWidthFillUint64\r
40};\r
41\r
42//\r
43// Lookup table for increment values based on transfer widths\r
44//\r
45UINT8 mOutStride[] = {\r
46 1, // EfiPciWidthUint8\r
47 2, // EfiPciWidthUint16\r
48 4, // EfiPciWidthUint32\r
49 8, // EfiPciWidthUint64\r
50 1, // EfiPciWidthFifoUint8\r
51 2, // EfiPciWidthFifoUint16\r
52 4, // EfiPciWidthFifoUint32\r
53 8, // EfiPciWidthFifoUint64\r
54 0, // EfiPciWidthFillUint8\r
55 0, // EfiPciWidthFillUint16\r
56 0, // EfiPciWidthFillUint32\r
57 0 // EfiPciWidthFillUint64\r
58};\r
59\r
60/**\r
61 Construct the Pci Root Bridge instance.\r
62\r
63 @param Bridge The root bridge instance.\r
4a50cf4e
RN
64\r
65 @return The pointer to PCI_ROOT_BRIDGE_INSTANCE just created\r
66 or NULL if creation fails.\r
67**/\r
68PCI_ROOT_BRIDGE_INSTANCE *\r
69CreateRootBridge (\r
401f8cd1 70 IN PCI_ROOT_BRIDGE *Bridge\r
4a50cf4e
RN
71 )\r
72{\r
73 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
74 PCI_RESOURCE_TYPE Index;\r
75 CHAR16 *DevicePathStr;\r
401f8cd1 76 PCI_ROOT_BRIDGE_APERTURE *Aperture;\r
4a50cf4e
RN
77\r
78 DevicePathStr = NULL;\r
79\r
80 DEBUG ((EFI_D_INFO, "RootBridge: "));\r
81 DEBUG ((EFI_D_INFO, "%s\n", DevicePathStr = ConvertDevicePathToText (Bridge->DevicePath, FALSE, FALSE)));\r
3da82965
LE
82 DEBUG ((EFI_D_INFO, " Support/Attr: %lx / %lx\n", Bridge->Supports, Bridge->Attributes));\r
83 DEBUG ((EFI_D_INFO, " DmaAbove4G: %s\n", Bridge->DmaAbove4G ? L"Yes" : L"No"));\r
014b4720 84 DEBUG ((EFI_D_INFO, "NoExtConfSpace: %s\n", Bridge->NoExtendedConfigSpace ? L"Yes" : L"No"));\r
3da82965 85 DEBUG ((EFI_D_INFO, " AllocAttr: %lx (%s%s)\n", Bridge->AllocationAttributes,\r
4a50cf4e
RN
86 (Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM) != 0 ? L"CombineMemPMem " : L"",\r
87 (Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_MEM64_DECODE) != 0 ? L"Mem64Decode" : L""\r
88 ));\r
74d0a339
HG
89 DEBUG ((\r
90 EFI_D_INFO, " Bus: %lx - %lx Translation=%lx\n",\r
91 Bridge->Bus.Base, Bridge->Bus.Limit, Bridge->Bus.Translation\r
92 ));\r
93 //\r
94 // Translation for bus is not supported.\r
95 //\r
96 ASSERT (Bridge->Bus.Translation == 0);\r
97 if (Bridge->Bus.Translation != 0) {\r
98 return NULL;\r
99 }\r
100\r
101 DEBUG ((\r
102 DEBUG_INFO, " Io: %lx - %lx Translation=%lx\n",\r
103 Bridge->Io.Base, Bridge->Io.Limit, Bridge->Io.Translation\r
104 ));\r
105 DEBUG ((\r
106 DEBUG_INFO, " Mem: %lx - %lx Translation=%lx\n",\r
107 Bridge->Mem.Base, Bridge->Mem.Limit, Bridge->Mem.Translation\r
108 ));\r
109 DEBUG ((\r
110 DEBUG_INFO, " MemAbove4G: %lx - %lx Translation=%lx\n",\r
111 Bridge->MemAbove4G.Base, Bridge->MemAbove4G.Limit, Bridge->MemAbove4G.Translation\r
112 ));\r
113 DEBUG ((\r
114 DEBUG_INFO, " PMem: %lx - %lx Translation=%lx\n",\r
115 Bridge->PMem.Base, Bridge->PMem.Limit, Bridge->PMem.Translation\r
116 ));\r
117 DEBUG ((\r
118 DEBUG_INFO, " PMemAbove4G: %lx - %lx Translation=%lx\n",\r
119 Bridge->PMemAbove4G.Base, Bridge->PMemAbove4G.Limit, Bridge->PMemAbove4G.Translation\r
120 ));\r
4a50cf4e
RN
121\r
122 //\r
123 // Make sure Mem and MemAbove4G apertures are valid\r
124 //\r
f9607bef 125 if (Bridge->Mem.Base <= Bridge->Mem.Limit) {\r
4a50cf4e
RN
126 ASSERT (Bridge->Mem.Limit < SIZE_4GB);\r
127 if (Bridge->Mem.Limit >= SIZE_4GB) {\r
128 return NULL;\r
129 }\r
130 }\r
f9607bef 131 if (Bridge->MemAbove4G.Base <= Bridge->MemAbove4G.Limit) {\r
4a50cf4e
RN
132 ASSERT (Bridge->MemAbove4G.Base >= SIZE_4GB);\r
133 if (Bridge->MemAbove4G.Base < SIZE_4GB) {\r
134 return NULL;\r
135 }\r
136 }\r
f9607bef 137 if (Bridge->PMem.Base <= Bridge->PMem.Limit) {\r
4a50cf4e
RN
138 ASSERT (Bridge->PMem.Limit < SIZE_4GB);\r
139 if (Bridge->PMem.Limit >= SIZE_4GB) {\r
140 return NULL;\r
141 }\r
142 }\r
f9607bef 143 if (Bridge->PMemAbove4G.Base <= Bridge->PMemAbove4G.Limit) {\r
4a50cf4e
RN
144 ASSERT (Bridge->PMemAbove4G.Base >= SIZE_4GB);\r
145 if (Bridge->PMemAbove4G.Base < SIZE_4GB) {\r
146 return NULL;\r
147 }\r
148 }\r
149\r
401f8cd1
RN
150 //\r
151 // Ignore AllocationAttributes when resources were already assigned.\r
152 //\r
153 if (!Bridge->ResourceAssigned) {\r
154 if ((Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM) != 0) {\r
155 //\r
156 // If this bit is set, then the PCI Root Bridge does not\r
157 // support separate windows for Non-prefetchable and Prefetchable\r
158 // memory.\r
159 //\r
160 ASSERT (Bridge->PMem.Base > Bridge->PMem.Limit);\r
161 ASSERT (Bridge->PMemAbove4G.Base > Bridge->PMemAbove4G.Limit);\r
162 if ((Bridge->PMem.Base <= Bridge->PMem.Limit) ||\r
163 (Bridge->PMemAbove4G.Base <= Bridge->PMemAbove4G.Limit)\r
164 ) {\r
165 return NULL;\r
166 }\r
4a50cf4e 167 }\r
4a50cf4e 168\r
401f8cd1
RN
169 if ((Bridge->AllocationAttributes & EFI_PCI_HOST_BRIDGE_MEM64_DECODE) == 0) {\r
170 //\r
171 // If this bit is not set, then the PCI Root Bridge does not support\r
172 // 64 bit memory windows.\r
173 //\r
174 ASSERT (Bridge->MemAbove4G.Base > Bridge->MemAbove4G.Limit);\r
175 ASSERT (Bridge->PMemAbove4G.Base > Bridge->PMemAbove4G.Limit);\r
176 if ((Bridge->MemAbove4G.Base <= Bridge->MemAbove4G.Limit) ||\r
177 (Bridge->PMemAbove4G.Base <= Bridge->PMemAbove4G.Limit)\r
178 ) {\r
179 return NULL;\r
180 }\r
4a50cf4e
RN
181 }\r
182 }\r
183\r
184 RootBridge = AllocateZeroPool (sizeof (PCI_ROOT_BRIDGE_INSTANCE));\r
185 ASSERT (RootBridge != NULL);\r
186\r
187 RootBridge->Signature = PCI_ROOT_BRIDGE_SIGNATURE;\r
188 RootBridge->Supports = Bridge->Supports;\r
189 RootBridge->Attributes = Bridge->Attributes;\r
190 RootBridge->DmaAbove4G = Bridge->DmaAbove4G;\r
014b4720 191 RootBridge->NoExtendedConfigSpace = Bridge->NoExtendedConfigSpace;\r
4a50cf4e
RN
192 RootBridge->AllocationAttributes = Bridge->AllocationAttributes;\r
193 RootBridge->DevicePath = DuplicateDevicePath (Bridge->DevicePath);\r
194 RootBridge->DevicePathStr = DevicePathStr;\r
195 RootBridge->ConfigBuffer = AllocatePool (\r
196 TypeMax * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR)\r
197 );\r
198 ASSERT (RootBridge->ConfigBuffer != NULL);\r
199 InitializeListHead (&RootBridge->Maps);\r
200\r
201 CopyMem (&RootBridge->Bus, &Bridge->Bus, sizeof (PCI_ROOT_BRIDGE_APERTURE));\r
202 CopyMem (&RootBridge->Io, &Bridge->Io, sizeof (PCI_ROOT_BRIDGE_APERTURE));\r
203 CopyMem (&RootBridge->Mem, &Bridge->Mem, sizeof (PCI_ROOT_BRIDGE_APERTURE));\r
204 CopyMem (&RootBridge->MemAbove4G, &Bridge->MemAbove4G, sizeof (PCI_ROOT_BRIDGE_APERTURE));\r
814f4306
RN
205 CopyMem (&RootBridge->PMem, &Bridge->PMem, sizeof (PCI_ROOT_BRIDGE_APERTURE));\r
206 CopyMem (&RootBridge->PMemAbove4G, &Bridge->PMemAbove4G, sizeof (PCI_ROOT_BRIDGE_APERTURE));\r
4a50cf4e
RN
207\r
208 for (Index = TypeIo; Index < TypeMax; Index++) {\r
401f8cd1
RN
209 switch (Index) {\r
210 case TypeBus:\r
211 Aperture = &RootBridge->Bus;\r
212 break;\r
213 case TypeIo:\r
214 Aperture = &RootBridge->Io;\r
215 break;\r
216 case TypeMem32:\r
217 Aperture = &RootBridge->Mem;\r
218 break;\r
219 case TypeMem64:\r
220 Aperture = &RootBridge->MemAbove4G;\r
221 break;\r
222 case TypePMem32:\r
223 Aperture = &RootBridge->PMem;\r
224 break;\r
225 case TypePMem64:\r
226 Aperture = &RootBridge->PMemAbove4G;\r
227 break;\r
228 default:\r
229 ASSERT (FALSE);\r
26bd6437 230 Aperture = NULL;\r
401f8cd1
RN
231 break;\r
232 }\r
233 RootBridge->ResAllocNode[Index].Type = Index;\r
234 if (Bridge->ResourceAssigned && (Aperture->Limit >= Aperture->Base)) {\r
74d0a339
HG
235 //\r
236 // Base in ResAllocNode is a host address, while Base in Aperture is a\r
237 // device address.\r
238 //\r
239 RootBridge->ResAllocNode[Index].Base = TO_HOST_ADDRESS (Aperture->Base,\r
240 Aperture->Translation);\r
401f8cd1
RN
241 RootBridge->ResAllocNode[Index].Length = Aperture->Limit - Aperture->Base + 1;\r
242 RootBridge->ResAllocNode[Index].Status = ResAllocated;\r
243 } else {\r
244 RootBridge->ResAllocNode[Index].Base = 0;\r
245 RootBridge->ResAllocNode[Index].Length = 0;\r
246 RootBridge->ResAllocNode[Index].Status = ResNone;\r
247 }\r
4a50cf4e
RN
248 }\r
249\r
250 RootBridge->RootBridgeIo.SegmentNumber = Bridge->Segment;\r
4a50cf4e
RN
251 RootBridge->RootBridgeIo.PollMem = RootBridgeIoPollMem;\r
252 RootBridge->RootBridgeIo.PollIo = RootBridgeIoPollIo;\r
253 RootBridge->RootBridgeIo.Mem.Read = RootBridgeIoMemRead;\r
254 RootBridge->RootBridgeIo.Mem.Write = RootBridgeIoMemWrite;\r
255 RootBridge->RootBridgeIo.Io.Read = RootBridgeIoIoRead;\r
256 RootBridge->RootBridgeIo.Io.Write = RootBridgeIoIoWrite;\r
257 RootBridge->RootBridgeIo.CopyMem = RootBridgeIoCopyMem;\r
258 RootBridge->RootBridgeIo.Pci.Read = RootBridgeIoPciRead;\r
259 RootBridge->RootBridgeIo.Pci.Write = RootBridgeIoPciWrite;\r
260 RootBridge->RootBridgeIo.Map = RootBridgeIoMap;\r
261 RootBridge->RootBridgeIo.Unmap = RootBridgeIoUnmap;\r
262 RootBridge->RootBridgeIo.AllocateBuffer = RootBridgeIoAllocateBuffer;\r
263 RootBridge->RootBridgeIo.FreeBuffer = RootBridgeIoFreeBuffer;\r
264 RootBridge->RootBridgeIo.Flush = RootBridgeIoFlush;\r
265 RootBridge->RootBridgeIo.GetAttributes = RootBridgeIoGetAttributes;\r
266 RootBridge->RootBridgeIo.SetAttributes = RootBridgeIoSetAttributes;\r
267 RootBridge->RootBridgeIo.Configuration = RootBridgeIoConfiguration;\r
268\r
269 return RootBridge;\r
270}\r
271\r
272/**\r
273 Check parameters for IO,MMIO,PCI read/write services of PCI Root Bridge IO.\r
274\r
275 The I/O operations are carried out exactly as requested. The caller is\r
276 responsible for satisfying any alignment and I/O width restrictions that a PI\r
277 System on a platform might require. For example on some platforms, width\r
278 requests of EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other\r
279 hand, will be handled by the driver.\r
280\r
281 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
282\r
283 @param[in] OperationType I/O operation type: IO/MMIO/PCI.\r
284\r
285 @param[in] Width Signifies the width of the I/O or Memory operation.\r
286\r
287 @param[in] Address The base address of the I/O operation.\r
288\r
289 @param[in] Count The number of I/O operations to perform. The number\r
290 of bytes moved is Width size * Count, starting at\r
291 Address.\r
292\r
293 @param[in] Buffer For read operations, the destination buffer to\r
294 store the results. For write operations, the source\r
295 buffer from which to write data.\r
296\r
297 @retval EFI_SUCCESS The parameters for this request pass the\r
298 checks.\r
299\r
300 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.\r
301\r
302 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
303\r
666d05a0
RN
304 @retval EFI_INVALID_PARAMETER Address or Count is invalid.\r
305\r
4a50cf4e
RN
306 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.\r
307\r
308 @retval EFI_UNSUPPORTED The address range specified by Address, Width,\r
309 and Count is not valid for this PI system.\r
310**/\r
311EFI_STATUS\r
312RootBridgeIoCheckParameter (\r
313 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
314 IN OPERATION_TYPE OperationType,\r
315 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
316 IN UINT64 Address,\r
317 IN UINTN Count,\r
318 IN VOID *Buffer\r
319 )\r
320{\r
321 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
322 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *PciRbAddr;\r
323 UINT64 Base;\r
324 UINT64 Limit;\r
325 UINT32 Size;\r
666d05a0 326 UINT64 Length;\r
4a50cf4e
RN
327\r
328 //\r
329 // Check to see if Buffer is NULL\r
330 //\r
331 if (Buffer == NULL) {\r
332 return EFI_INVALID_PARAMETER;\r
333 }\r
334\r
335 //\r
336 // Check to see if Width is in the valid range\r
337 //\r
338 if ((UINT32) Width >= EfiPciWidthMaximum) {\r
339 return EFI_INVALID_PARAMETER;\r
340 }\r
341\r
342 //\r
666d05a0 343 // For FIFO type, the device address won't increase during the access,\r
4a50cf4e
RN
344 // so treat Count as 1\r
345 //\r
346 if (Width >= EfiPciWidthFifoUint8 && Width <= EfiPciWidthFifoUint64) {\r
347 Count = 1;\r
348 }\r
349\r
350 Width = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) (Width & 0x03);\r
351 Size = 1 << Width;\r
352\r
666d05a0
RN
353 //\r
354 // Make sure (Count * Size) doesn't exceed MAX_UINT64\r
355 //\r
356 if (Count > DivU64x32 (MAX_UINT64, Size)) {\r
357 return EFI_INVALID_PARAMETER;\r
358 }\r
359\r
4a50cf4e
RN
360 //\r
361 // Check to see if Address is aligned\r
362 //\r
363 if ((Address & (Size - 1)) != 0) {\r
364 return EFI_UNSUPPORTED;\r
365 }\r
366\r
666d05a0
RN
367 //\r
368 // Make sure (Address + Count * Size) doesn't exceed MAX_UINT64\r
369 //\r
370 Length = MultU64x32 (Count, Size);\r
371 if (Address > MAX_UINT64 - Length) {\r
372 return EFI_INVALID_PARAMETER;\r
373 }\r
374\r
4a50cf4e
RN
375 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
376\r
377 //\r
378 // Check to see if any address associated with this transfer exceeds the\r
379 // maximum allowed address. The maximum address implied by the parameters\r
380 // passed in is Address + Size * Count. If the following condition is met,\r
381 // then the transfer is not supported.\r
382 //\r
383 // Address + Size * Count > Limit + 1\r
384 //\r
385 // Since Limit can be the maximum integer value supported by the CPU and\r
386 // Count can also be the maximum integer value supported by the CPU, this\r
387 // range check must be adjusted to avoid all oveflow conditions.\r
388 //\r
389 if (OperationType == IoOperation) {\r
390 //\r
391 // Allow Legacy IO access\r
392 //\r
666d05a0 393 if (Address + Length <= 0x1000) {\r
4a50cf4e
RN
394 if ((RootBridge->Attributes & (\r
395 EFI_PCI_ATTRIBUTE_ISA_IO | EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO | EFI_PCI_ATTRIBUTE_VGA_IO |\r
396 EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO | EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO |\r
397 EFI_PCI_ATTRIBUTE_ISA_IO_16 | EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16 | EFI_PCI_ATTRIBUTE_VGA_IO_16)) != 0) {\r
398 return EFI_SUCCESS;\r
399 }\r
400 }\r
401 Base = RootBridge->Io.Base;\r
402 Limit = RootBridge->Io.Limit;\r
403 } else if (OperationType == MemOperation) {\r
404 //\r
405 // Allow Legacy MMIO access\r
406 //\r
666d05a0 407 if ((Address >= 0xA0000) && (Address + Length) <= 0xC0000) {\r
4a50cf4e
RN
408 if ((RootBridge->Attributes & EFI_PCI_ATTRIBUTE_VGA_MEMORY) != 0) {\r
409 return EFI_SUCCESS;\r
410 }\r
411 }\r
412 //\r
413 // By comparing the Address against Limit we know which range to be used\r
414 // for checking\r
415 //\r
bff6584d
RN
416 if ((Address >= RootBridge->Mem.Base) && (Address + Length <= RootBridge->Mem.Limit + 1)) {\r
417 Base = RootBridge->Mem.Base;\r
4a50cf4e 418 Limit = RootBridge->Mem.Limit;\r
bff6584d
RN
419 } else if ((Address >= RootBridge->PMem.Base) && (Address + Length <= RootBridge->PMem.Limit + 1)) {\r
420 Base = RootBridge->PMem.Base;\r
421 Limit = RootBridge->PMem.Limit;\r
422 } else if ((Address >= RootBridge->MemAbove4G.Base) && (Address + Length <= RootBridge->MemAbove4G.Limit + 1)) {\r
423 Base = RootBridge->MemAbove4G.Base;\r
4a50cf4e 424 Limit = RootBridge->MemAbove4G.Limit;\r
bff6584d
RN
425 } else {\r
426 Base = RootBridge->PMemAbove4G.Base;\r
427 Limit = RootBridge->PMemAbove4G.Limit;\r
4a50cf4e
RN
428 }\r
429 } else {\r
430 PciRbAddr = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS*) &Address;\r
431 if (PciRbAddr->Bus < RootBridge->Bus.Base ||\r
432 PciRbAddr->Bus > RootBridge->Bus.Limit) {\r
433 return EFI_INVALID_PARAMETER;\r
434 }\r
435\r
436 if (PciRbAddr->Device > PCI_MAX_DEVICE ||\r
437 PciRbAddr->Function > PCI_MAX_FUNC) {\r
438 return EFI_INVALID_PARAMETER;\r
439 }\r
440\r
441 if (PciRbAddr->ExtendedRegister != 0) {\r
442 Address = PciRbAddr->ExtendedRegister;\r
443 } else {\r
444 Address = PciRbAddr->Register;\r
445 }\r
446 Base = 0;\r
014b4720 447 Limit = RootBridge->NoExtendedConfigSpace ? 0xFF : 0xFFF;\r
4a50cf4e
RN
448 }\r
449\r
450 if (Address < Base) {\r
451 return EFI_INVALID_PARAMETER;\r
452 }\r
453\r
666d05a0 454 if (Address + Length > Limit + 1) {\r
4a50cf4e
RN
455 return EFI_INVALID_PARAMETER;\r
456 }\r
457\r
458 return EFI_SUCCESS;\r
459}\r
460\r
74d0a339
HG
461/**\r
462 Use address to match apertures of memory type and then get the corresponding\r
463 translation.\r
464\r
465 @param RootBridge The root bridge instance.\r
466 @param Address The address used to match aperture.\r
467 @param Translation Pointer containing the output translation.\r
468\r
469 @return EFI_SUCCESS Get translation successfully.\r
470 @return EFI_INVALID_PARAMETER No matched memory aperture; the input Address\r
471 must be invalid.\r
472**/\r
473EFI_STATUS\r
474RootBridgeIoGetMemTranslationByAddress (\r
475 IN PCI_ROOT_BRIDGE_INSTANCE *RootBridge,\r
476 IN UINT64 Address,\r
477 IN OUT UINT64 *Translation\r
478 )\r
479{\r
480 if (Address >= RootBridge->Mem.Base && Address <= RootBridge->Mem.Limit) {\r
481 *Translation = RootBridge->Mem.Translation;\r
482 } else if (Address >= RootBridge->PMem.Base && Address <= RootBridge->PMem.Limit) {\r
483 *Translation = RootBridge->PMem.Translation;\r
484 } else if (Address >= RootBridge->MemAbove4G.Base && Address <= RootBridge->MemAbove4G.Limit) {\r
485 *Translation = RootBridge->MemAbove4G.Translation;\r
486 } else if (Address >= RootBridge->PMemAbove4G.Base && Address <= RootBridge->PMemAbove4G.Limit) {\r
487 *Translation = RootBridge->PMemAbove4G.Translation;\r
488 } else {\r
489 return EFI_INVALID_PARAMETER;\r
490 }\r
491\r
492 return EFI_SUCCESS;\r
493}\r
494\r
0edb7ec5
RN
495/**\r
496 Return the result of (Multiplicand * Multiplier / Divisor).\r
497\r
498 @param Multiplicand A 64-bit unsigned value.\r
499 @param Multiplier A 64-bit unsigned value.\r
500 @param Divisor A 32-bit unsigned value.\r
501 @param Remainder A pointer to a 32-bit unsigned value. This parameter is\r
502 optional and may be NULL.\r
503\r
504 @return Multiplicand * Multiplier / Divisor.\r
505**/\r
506UINT64\r
507MultThenDivU64x64x32 (\r
508 IN UINT64 Multiplicand,\r
509 IN UINT64 Multiplier,\r
510 IN UINT32 Divisor,\r
511 OUT UINT32 *Remainder OPTIONAL\r
512 )\r
513{\r
514 UINT64 Uint64;\r
515 UINT32 LocalRemainder;\r
516 UINT32 Uint32;\r
517 if (Multiplicand > DivU64x64Remainder (MAX_UINT64, Multiplier, NULL)) {\r
518 //\r
519 // Make sure Multiplicand is the bigger one.\r
520 //\r
521 if (Multiplicand < Multiplier) {\r
522 Uint64 = Multiplicand;\r
523 Multiplicand = Multiplier;\r
524 Multiplier = Uint64;\r
525 }\r
526 //\r
527 // Because Multiplicand * Multiplier overflows,\r
528 // Multiplicand * Multiplier / Divisor\r
529 // = (2 * Multiplicand' + 1) * Multiplier / Divisor\r
530 // = 2 * (Multiplicand' * Multiplier / Divisor) + Multiplier / Divisor\r
531 //\r
532 Uint64 = MultThenDivU64x64x32 (RShiftU64 (Multiplicand, 1), Multiplier, Divisor, &LocalRemainder);\r
533 Uint64 = LShiftU64 (Uint64, 1);\r
534 Uint32 = 0;\r
535 if ((Multiplicand & 0x1) == 1) {\r
536 Uint64 += DivU64x32Remainder (Multiplier, Divisor, &Uint32);\r
537 }\r
538 return Uint64 + DivU64x32Remainder (Uint32 + LShiftU64 (LocalRemainder, 1), Divisor, Remainder);\r
539 } else {\r
540 return DivU64x32Remainder (MultU64x64 (Multiplicand, Multiplier), Divisor, Remainder);\r
541 }\r
542}\r
543\r
544/**\r
545 Return the elapsed tick count from CurrentTick.\r
546\r
547 @param CurrentTick On input, the previous tick count.\r
548 On output, the current tick count.\r
549 @param StartTick The value the performance counter starts with when it\r
550 rolls over.\r
551 @param EndTick The value that the performance counter ends with before\r
552 it rolls over.\r
553\r
554 @return The elapsed tick count from CurrentTick.\r
555**/\r
556UINT64\r
557GetElapsedTick (\r
558 UINT64 *CurrentTick,\r
559 UINT64 StartTick,\r
560 UINT64 EndTick\r
561 )\r
562{\r
563 UINT64 PreviousTick;\r
d1102dba 564\r
0edb7ec5
RN
565 PreviousTick = *CurrentTick;\r
566 *CurrentTick = GetPerformanceCounter();\r
567 if (StartTick < EndTick) {\r
568 return *CurrentTick - PreviousTick;\r
569 } else {\r
570 return PreviousTick - *CurrentTick;\r
571 }\r
572}\r
573\r
4a50cf4e
RN
574/**\r
575 Polls an address in memory mapped I/O space until an exit condition is met,\r
576 or a timeout occurs.\r
577\r
578 This function provides a standard way to poll a PCI memory location. A PCI\r
579 memory read operation is performed at the PCI memory address specified by\r
580 Address for the width specified by Width. The result of this PCI memory read\r
581 operation is stored in Result. This PCI memory read operation is repeated\r
582 until either a timeout of Delay 100 ns units has expired, or (Result & Mask)\r
583 is equal to Value.\r
584\r
585 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
586 @param[in] Width Signifies the width of the memory operations.\r
587 @param[in] Address The base address of the memory operations. The caller\r
588 is responsible for aligning Address if required.\r
589 @param[in] Mask Mask used for the polling criteria. Bytes above Width\r
590 in Mask are ignored. The bits in the bytes below Width\r
591 which are zero in Mask are ignored when polling the\r
592 memory address.\r
593 @param[in] Value The comparison value used for the polling exit\r
594 criteria.\r
595 @param[in] Delay The number of 100 ns units to poll. Note that timer\r
596 available may be of poorer granularity.\r
597 @param[out] Result Pointer to the last value read from the memory\r
598 location.\r
599\r
600 @retval EFI_SUCCESS The last data returned from the access matched\r
601 the poll exit criteria.\r
602 @retval EFI_INVALID_PARAMETER Width is invalid.\r
603 @retval EFI_INVALID_PARAMETER Result is NULL.\r
604 @retval EFI_TIMEOUT Delay expired before a match occurred.\r
605 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
606 lack of resources.\r
607**/\r
4a50cf4e
RN
608EFI_STATUS\r
609EFIAPI\r
610RootBridgeIoPollMem (\r
611 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
612 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
613 IN UINT64 Address,\r
614 IN UINT64 Mask,\r
615 IN UINT64 Value,\r
616 IN UINT64 Delay,\r
617 OUT UINT64 *Result\r
618 )\r
619{\r
620 EFI_STATUS Status;\r
621 UINT64 NumberOfTicks;\r
622 UINT32 Remainder;\r
0edb7ec5
RN
623 UINT64 StartTick;\r
624 UINT64 EndTick;\r
625 UINT64 CurrentTick;\r
626 UINT64 ElapsedTick;\r
627 UINT64 Frequency;\r
4a50cf4e
RN
628\r
629 if (Result == NULL) {\r
630 return EFI_INVALID_PARAMETER;\r
631 }\r
632\r
633 if ((UINT32)Width > EfiPciWidthUint64) {\r
634 return EFI_INVALID_PARAMETER;\r
635 }\r
636\r
637 //\r
638 // No matter what, always do a single poll.\r
639 //\r
640 Status = This->Mem.Read (This, Width, Address, 1, Result);\r
641 if (EFI_ERROR (Status)) {\r
642 return Status;\r
643 }\r
644\r
645 if ((*Result & Mask) == Value) {\r
646 return EFI_SUCCESS;\r
647 }\r
648\r
649 if (Delay == 0) {\r
650 return EFI_SUCCESS;\r
651\r
652 } else {\r
4a50cf4e 653 //\r
0edb7ec5 654 // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)\r
4a50cf4e 655 //\r
0edb7ec5
RN
656 Frequency = GetPerformanceCounterProperties (&StartTick, &EndTick);\r
657 NumberOfTicks = MultThenDivU64x64x32 (Frequency, Delay, (UINT32)EFI_TIMER_PERIOD_SECONDS(1), &Remainder);\r
658 if (Remainder >= (UINTN)EFI_TIMER_PERIOD_SECONDS(1) / 2) {\r
659 NumberOfTicks++;\r
4a50cf4e 660 }\r
0edb7ec5
RN
661 for ( ElapsedTick = 0, CurrentTick = GetPerformanceCounter()\r
662 ; ElapsedTick <= NumberOfTicks\r
663 ; ElapsedTick += GetElapsedTick (&CurrentTick, StartTick, EndTick)\r
664 ) {\r
4a50cf4e
RN
665 Status = This->Mem.Read (This, Width, Address, 1, Result);\r
666 if (EFI_ERROR (Status)) {\r
667 return Status;\r
668 }\r
669\r
670 if ((*Result & Mask) == Value) {\r
671 return EFI_SUCCESS;\r
672 }\r
4a50cf4e
RN
673 }\r
674 }\r
675 return EFI_TIMEOUT;\r
676}\r
677\r
678/**\r
679 Reads from the I/O space of a PCI Root Bridge. Returns when either the\r
680 polling exit criteria is satisfied or after a defined duration.\r
681\r
682 This function provides a standard way to poll a PCI I/O location. A PCI I/O\r
683 read operation is performed at the PCI I/O address specified by Address for\r
684 the width specified by Width.\r
685 The result of this PCI I/O read operation is stored in Result. This PCI I/O\r
686 read operation is repeated until either a timeout of Delay 100 ns units has\r
687 expired, or (Result & Mask) is equal to Value.\r
688\r
689 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
690 @param[in] Width Signifies the width of the I/O operations.\r
691 @param[in] Address The base address of the I/O operations. The caller is\r
692 responsible for aligning Address if required.\r
693 @param[in] Mask Mask used for the polling criteria. Bytes above Width in\r
694 Mask are ignored. The bits in the bytes below Width\r
695 which are zero in Mask are ignored when polling the I/O\r
696 address.\r
697 @param[in] Value The comparison value used for the polling exit criteria.\r
698 @param[in] Delay The number of 100 ns units to poll. Note that timer\r
699 available may be of poorer granularity.\r
700 @param[out] Result Pointer to the last value read from the memory location.\r
701\r
702 @retval EFI_SUCCESS The last data returned from the access matched\r
703 the poll exit criteria.\r
704 @retval EFI_INVALID_PARAMETER Width is invalid.\r
705 @retval EFI_INVALID_PARAMETER Result is NULL.\r
706 @retval EFI_TIMEOUT Delay expired before a match occurred.\r
707 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
708 lack of resources.\r
709**/\r
710EFI_STATUS\r
711EFIAPI\r
712RootBridgeIoPollIo (\r
713 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
714 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
715 IN UINT64 Address,\r
716 IN UINT64 Mask,\r
717 IN UINT64 Value,\r
718 IN UINT64 Delay,\r
719 OUT UINT64 *Result\r
720 )\r
721{\r
722 EFI_STATUS Status;\r
723 UINT64 NumberOfTicks;\r
724 UINT32 Remainder;\r
0edb7ec5
RN
725 UINT64 StartTick;\r
726 UINT64 EndTick;\r
727 UINT64 CurrentTick;\r
728 UINT64 ElapsedTick;\r
729 UINT64 Frequency;\r
4a50cf4e
RN
730\r
731 //\r
732 // No matter what, always do a single poll.\r
733 //\r
734\r
735 if (Result == NULL) {\r
736 return EFI_INVALID_PARAMETER;\r
737 }\r
738\r
739 if ((UINT32)Width > EfiPciWidthUint64) {\r
740 return EFI_INVALID_PARAMETER;\r
741 }\r
742\r
743 Status = This->Io.Read (This, Width, Address, 1, Result);\r
744 if (EFI_ERROR (Status)) {\r
745 return Status;\r
746 }\r
747 if ((*Result & Mask) == Value) {\r
748 return EFI_SUCCESS;\r
749 }\r
750\r
751 if (Delay == 0) {\r
752 return EFI_SUCCESS;\r
753\r
754 } else {\r
4a50cf4e 755 //\r
0edb7ec5 756 // NumberOfTicks = Frenquency * Delay / EFI_TIMER_PERIOD_SECONDS(1)\r
4a50cf4e 757 //\r
0edb7ec5
RN
758 Frequency = GetPerformanceCounterProperties (&StartTick, &EndTick);\r
759 NumberOfTicks = MultThenDivU64x64x32 (Frequency, Delay, (UINT32)EFI_TIMER_PERIOD_SECONDS(1), &Remainder);\r
760 if (Remainder >= (UINTN)EFI_TIMER_PERIOD_SECONDS(1) / 2) {\r
761 NumberOfTicks++;\r
4a50cf4e 762 }\r
0edb7ec5
RN
763 for ( ElapsedTick = 0, CurrentTick = GetPerformanceCounter()\r
764 ; ElapsedTick <= NumberOfTicks\r
765 ; ElapsedTick += GetElapsedTick (&CurrentTick, StartTick, EndTick)\r
766 ) {\r
4a50cf4e
RN
767 Status = This->Io.Read (This, Width, Address, 1, Result);\r
768 if (EFI_ERROR (Status)) {\r
769 return Status;\r
770 }\r
771\r
772 if ((*Result & Mask) == Value) {\r
773 return EFI_SUCCESS;\r
774 }\r
4a50cf4e
RN
775 }\r
776 }\r
777 return EFI_TIMEOUT;\r
778}\r
779\r
780/**\r
781 Enables a PCI driver to access PCI controller registers in the PCI root\r
782 bridge memory space.\r
783\r
784 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI\r
785 controller registers in the PCI root bridge memory space.\r
786 The memory operations are carried out exactly as requested. The caller is\r
787 responsible for satisfying any alignment and memory width restrictions that a\r
788 PCI Root Bridge on a platform might require.\r
789\r
790 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
791 @param[in] Width Signifies the width of the memory operation.\r
792 @param[in] Address The base address of the memory operation. The caller\r
793 is responsible for aligning the Address if required.\r
794 @param[in] Count The number of memory operations to perform. Bytes\r
795 moved is Width size * Count, starting at Address.\r
796 @param[out] Buffer For read operations, the destination buffer to store\r
797 the results. For write operations, the source buffer\r
798 to write data from.\r
799\r
800 @retval EFI_SUCCESS The data was read from or written to the PCI\r
801 root bridge.\r
802 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.\r
803 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
804 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
805 lack of resources.\r
806**/\r
807EFI_STATUS\r
808EFIAPI\r
809RootBridgeIoMemRead (\r
810 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
811 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
812 IN UINT64 Address,\r
813 IN UINTN Count,\r
814 OUT VOID *Buffer\r
815 )\r
816{\r
817 EFI_STATUS Status;\r
74d0a339
HG
818 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
819 UINT64 Translation;\r
4a50cf4e
RN
820\r
821 Status = RootBridgeIoCheckParameter (This, MemOperation, Width, Address,\r
822 Count, Buffer);\r
823 if (EFI_ERROR (Status)) {\r
824 return Status;\r
825 }\r
74d0a339
HG
826\r
827 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
828 Status = RootBridgeIoGetMemTranslationByAddress (RootBridge, Address, &Translation);\r
829 if (EFI_ERROR (Status)) {\r
830 return Status;\r
831 }\r
832\r
833 // Address passed to CpuIo->Mem.Read needs to be a host address instead of\r
834 // device address.\r
835 return mCpuIo->Mem.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,\r
836 TO_HOST_ADDRESS (Address, Translation), Count, Buffer);\r
4a50cf4e
RN
837}\r
838\r
839/**\r
840 Enables a PCI driver to access PCI controller registers in the PCI root\r
841 bridge memory space.\r
842\r
843 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI\r
844 controller registers in the PCI root bridge memory space.\r
845 The memory operations are carried out exactly as requested. The caller is\r
846 responsible for satisfying any alignment and memory width restrictions that a\r
847 PCI Root Bridge on a platform might require.\r
848\r
849 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
850 @param[in] Width Signifies the width of the memory operation.\r
851 @param[in] Address The base address of the memory operation. The caller\r
852 is responsible for aligning the Address if required.\r
853 @param[in] Count The number of memory operations to perform. Bytes\r
854 moved is Width size * Count, starting at Address.\r
855 @param[in] Buffer For read operations, the destination buffer to store\r
856 the results. For write operations, the source buffer\r
857 to write data from.\r
858\r
859 @retval EFI_SUCCESS The data was read from or written to the PCI\r
860 root bridge.\r
861 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.\r
862 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
863 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
864 lack of resources.\r
865**/\r
866EFI_STATUS\r
867EFIAPI\r
868RootBridgeIoMemWrite (\r
869 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
870 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
871 IN UINT64 Address,\r
872 IN UINTN Count,\r
873 IN VOID *Buffer\r
874 )\r
875{\r
876 EFI_STATUS Status;\r
74d0a339
HG
877 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
878 UINT64 Translation;\r
4a50cf4e
RN
879\r
880 Status = RootBridgeIoCheckParameter (This, MemOperation, Width, Address,\r
881 Count, Buffer);\r
882 if (EFI_ERROR (Status)) {\r
883 return Status;\r
884 }\r
74d0a339
HG
885\r
886 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
887 Status = RootBridgeIoGetMemTranslationByAddress (RootBridge, Address, &Translation);\r
888 if (EFI_ERROR (Status)) {\r
889 return Status;\r
890 }\r
891\r
892 // Address passed to CpuIo->Mem.Write needs to be a host address instead of\r
893 // device address.\r
894 return mCpuIo->Mem.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,\r
895 TO_HOST_ADDRESS (Address, Translation), Count, Buffer);\r
4a50cf4e
RN
896}\r
897\r
898/**\r
899 Enables a PCI driver to access PCI controller registers in the PCI root\r
900 bridge I/O space.\r
901\r
902 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
903 @param[in] Width Signifies the width of the memory operations.\r
904 @param[in] Address The base address of the I/O operation. The caller is\r
905 responsible for aligning the Address if required.\r
906 @param[in] Count The number of I/O operations to perform. Bytes moved\r
907 is Width size * Count, starting at Address.\r
908 @param[out] Buffer For read operations, the destination buffer to store\r
909 the results. For write operations, the source buffer\r
910 to write data from.\r
911\r
912 @retval EFI_SUCCESS The data was read from or written to the PCI\r
913 root bridge.\r
914 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.\r
915 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
916 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
917 lack of resources.\r
918**/\r
919EFI_STATUS\r
920EFIAPI\r
921RootBridgeIoIoRead (\r
922 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
923 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
924 IN UINT64 Address,\r
925 IN UINTN Count,\r
926 OUT VOID *Buffer\r
927 )\r
928{\r
929 EFI_STATUS Status;\r
74d0a339
HG
930 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
931\r
4a50cf4e
RN
932 Status = RootBridgeIoCheckParameter (\r
933 This, IoOperation, Width,\r
934 Address, Count, Buffer\r
935 );\r
936 if (EFI_ERROR (Status)) {\r
937 return Status;\r
938 }\r
74d0a339
HG
939\r
940 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
941\r
942 // Address passed to CpuIo->Io.Read needs to be a host address instead of\r
943 // device address.\r
944 return mCpuIo->Io.Read (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,\r
945 TO_HOST_ADDRESS (Address, RootBridge->Io.Translation), Count, Buffer);\r
4a50cf4e
RN
946}\r
947\r
948/**\r
949 Enables a PCI driver to access PCI controller registers in the PCI root\r
950 bridge I/O space.\r
951\r
952 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
953 @param[in] Width Signifies the width of the memory operations.\r
954 @param[in] Address The base address of the I/O operation. The caller is\r
955 responsible for aligning the Address if required.\r
956 @param[in] Count The number of I/O operations to perform. Bytes moved\r
957 is Width size * Count, starting at Address.\r
958 @param[in] Buffer For read operations, the destination buffer to store\r
959 the results. For write operations, the source buffer\r
960 to write data from.\r
961\r
962 @retval EFI_SUCCESS The data was read from or written to the PCI\r
963 root bridge.\r
964 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.\r
965 @retval EFI_INVALID_PARAMETER Buffer is NULL.\r
966 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
967 lack of resources.\r
968**/\r
969EFI_STATUS\r
970EFIAPI\r
971RootBridgeIoIoWrite (\r
972 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
973 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
974 IN UINT64 Address,\r
975 IN UINTN Count,\r
976 IN VOID *Buffer\r
977 )\r
978{\r
979 EFI_STATUS Status;\r
74d0a339
HG
980 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
981\r
4a50cf4e
RN
982 Status = RootBridgeIoCheckParameter (\r
983 This, IoOperation, Width,\r
984 Address, Count, Buffer\r
985 );\r
986 if (EFI_ERROR (Status)) {\r
987 return Status;\r
988 }\r
74d0a339
HG
989\r
990 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
991\r
992 // Address passed to CpuIo->Io.Write needs to be a host address instead of\r
993 // device address.\r
994 return mCpuIo->Io.Write (mCpuIo, (EFI_CPU_IO_PROTOCOL_WIDTH) Width,\r
995 TO_HOST_ADDRESS (Address, RootBridge->Io.Translation), Count, Buffer);\r
4a50cf4e
RN
996}\r
997\r
998/**\r
999 Enables a PCI driver to copy one region of PCI root bridge memory space to\r
1000 another region of PCI root bridge memory space.\r
1001\r
1002 The CopyMem() function enables a PCI driver to copy one region of PCI root\r
1003 bridge memory space to another region of PCI root bridge memory space. This\r
1004 is especially useful for video scroll operation on a memory mapped video\r
1005 buffer.\r
1006 The memory operations are carried out exactly as requested. The caller is\r
1007 responsible for satisfying any alignment and memory width restrictions that a\r
1008 PCI root bridge on a platform might require.\r
1009\r
1010 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL\r
1011 instance.\r
1012 @param[in] Width Signifies the width of the memory operations.\r
1013 @param[in] DestAddress The destination address of the memory operation. The\r
1014 caller is responsible for aligning the DestAddress if\r
1015 required.\r
1016 @param[in] SrcAddress The source address of the memory operation. The caller\r
1017 is responsible for aligning the SrcAddress if\r
1018 required.\r
1019 @param[in] Count The number of memory operations to perform. Bytes\r
1020 moved is Width size * Count, starting at DestAddress\r
1021 and SrcAddress.\r
1022\r
1023 @retval EFI_SUCCESS The data was copied from one memory region\r
1024 to another memory region.\r
1025 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.\r
1026 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
1027 lack of resources.\r
1028**/\r
1029EFI_STATUS\r
1030EFIAPI\r
1031RootBridgeIoCopyMem (\r
1032 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1033 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
1034 IN UINT64 DestAddress,\r
1035 IN UINT64 SrcAddress,\r
1036 IN UINTN Count\r
1037 )\r
1038{\r
1039 EFI_STATUS Status;\r
1040 BOOLEAN Forward;\r
1041 UINTN Stride;\r
1042 UINTN Index;\r
1043 UINT64 Result;\r
1044\r
1045 if ((UINT32) Width > EfiPciWidthUint64) {\r
1046 return EFI_INVALID_PARAMETER;\r
1047 }\r
1048\r
1049 if (DestAddress == SrcAddress) {\r
1050 return EFI_SUCCESS;\r
1051 }\r
1052\r
1053 Stride = (UINTN) (1 << Width);\r
1054\r
1055 Forward = TRUE;\r
1056 if ((DestAddress > SrcAddress) &&\r
1057 (DestAddress < (SrcAddress + Count * Stride))) {\r
1058 Forward = FALSE;\r
1059 SrcAddress = SrcAddress + (Count - 1) * Stride;\r
1060 DestAddress = DestAddress + (Count - 1) * Stride;\r
1061 }\r
1062\r
1063 for (Index = 0; Index < Count; Index++) {\r
1064 Status = RootBridgeIoMemRead (\r
1065 This,\r
1066 Width,\r
1067 SrcAddress,\r
1068 1,\r
1069 &Result\r
1070 );\r
1071 if (EFI_ERROR (Status)) {\r
1072 return Status;\r
1073 }\r
1074 Status = RootBridgeIoMemWrite (\r
1075 This,\r
1076 Width,\r
1077 DestAddress,\r
1078 1,\r
1079 &Result\r
1080 );\r
1081 if (EFI_ERROR (Status)) {\r
1082 return Status;\r
1083 }\r
1084 if (Forward) {\r
1085 SrcAddress += Stride;\r
1086 DestAddress += Stride;\r
1087 } else {\r
1088 SrcAddress -= Stride;\r
1089 DestAddress -= Stride;\r
1090 }\r
1091 }\r
1092 return EFI_SUCCESS;\r
1093}\r
1094\r
1095\r
1096/**\r
1097 PCI configuration space access.\r
1098\r
1099 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL\r
1100 @param Read TRUE indicating it's a read operation.\r
1101 @param Width Signifies the width of the memory operation.\r
1102 @param Address The address within the PCI configuration space\r
1103 for the PCI controller.\r
1104 @param Count The number of PCI configuration operations\r
1105 to perform.\r
1106 @param Buffer The destination buffer to store the results.\r
1107\r
1108 @retval EFI_SUCCESS The data was read/written from/to the PCI root bridge.\r
1109 @retval EFI_INVALID_PARAMETER Invalid parameters found.\r
1110**/\r
1111EFI_STATUS\r
1112EFIAPI\r
1113RootBridgeIoPciAccess (\r
1114 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1115 IN BOOLEAN Read,\r
1116 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
1117 IN UINT64 Address,\r
1118 IN UINTN Count,\r
1119 IN OUT VOID *Buffer\r
1120 )\r
1121{\r
1122 EFI_STATUS Status;\r
1123 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1124 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress;\r
1125 UINT8 *Uint8Buffer;\r
1126 UINT8 InStride;\r
1127 UINT8 OutStride;\r
1128 UINTN Size;\r
1129\r
1130 Status = RootBridgeIoCheckParameter (This, PciOperation, Width, Address, Count, Buffer);\r
1131 if (EFI_ERROR (Status)) {\r
1132 return Status;\r
1133 }\r
1134\r
1135 //\r
1136 // Read Pci configuration space\r
1137 //\r
1138 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
1139 CopyMem (&PciAddress, &Address, sizeof (PciAddress));\r
1140\r
1141 if (PciAddress.ExtendedRegister == 0) {\r
1142 PciAddress.ExtendedRegister = PciAddress.Register;\r
1143 }\r
1144\r
1145 Address = PCI_SEGMENT_LIB_ADDRESS (\r
1146 RootBridge->RootBridgeIo.SegmentNumber,\r
1147 PciAddress.Bus,\r
1148 PciAddress.Device,\r
1149 PciAddress.Function,\r
1150 PciAddress.ExtendedRegister\r
1151 );\r
1152\r
1153 //\r
1154 // Select loop based on the width of the transfer\r
1155 //\r
1156 InStride = mInStride[Width];\r
1157 OutStride = mOutStride[Width];\r
1158 Size = (UINTN) (1 << (Width & 0x03));\r
1159 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {\r
1160 if (Read) {\r
1161 PciSegmentReadBuffer (Address, Size, Uint8Buffer);\r
1162 } else {\r
1163 PciSegmentWriteBuffer (Address, Size, Uint8Buffer);\r
1164 }\r
1165 }\r
1166 return EFI_SUCCESS;\r
1167}\r
1168\r
1169/**\r
1170 Allows read from PCI configuration space.\r
1171\r
1172 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL\r
1173 @param Width Signifies the width of the memory operation.\r
1174 @param Address The address within the PCI configuration space\r
1175 for the PCI controller.\r
1176 @param Count The number of PCI configuration operations\r
1177 to perform.\r
1178 @param Buffer The destination buffer to store the results.\r
1179\r
1180 @retval EFI_SUCCESS The data was read from the PCI root bridge.\r
1181 @retval EFI_INVALID_PARAMETER Invalid parameters found.\r
1182**/\r
1183EFI_STATUS\r
1184EFIAPI\r
1185RootBridgeIoPciRead (\r
1186 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1187 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
1188 IN UINT64 Address,\r
1189 IN UINTN Count,\r
1190 IN OUT VOID *Buffer\r
1191 )\r
1192{\r
1193 return RootBridgeIoPciAccess (This, TRUE, Width, Address, Count, Buffer);\r
1194}\r
1195\r
1196/**\r
1197 Allows write to PCI configuration space.\r
1198\r
1199 @param This A pointer to EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL\r
1200 @param Width Signifies the width of the memory operation.\r
1201 @param Address The address within the PCI configuration space\r
1202 for the PCI controller.\r
1203 @param Count The number of PCI configuration operations\r
1204 to perform.\r
1205 @param Buffer The source buffer to get the results.\r
1206\r
1207 @retval EFI_SUCCESS The data was written to the PCI root bridge.\r
1208 @retval EFI_INVALID_PARAMETER Invalid parameters found.\r
1209**/\r
1210EFI_STATUS\r
1211EFIAPI\r
1212RootBridgeIoPciWrite (\r
1213 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1214 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,\r
1215 IN UINT64 Address,\r
1216 IN UINTN Count,\r
1217 IN OUT VOID *Buffer\r
1218 )\r
1219{\r
1220 return RootBridgeIoPciAccess (This, FALSE, Width, Address, Count, Buffer);\r
1221}\r
1222\r
1223/**\r
4a50cf4e
RN
1224 Provides the PCI controller-specific address needed to access\r
1225 system memory for DMA.\r
1226\r
1227 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1228 @param Operation Indicate if the bus master is going to read or write\r
1229 to system memory.\r
1230 @param HostAddress The system memory address to map on the PCI controller.\r
1231 @param NumberOfBytes On input the number of bytes to map.\r
1232 On output the number of bytes that were mapped.\r
1233 @param DeviceAddress The resulting map address for the bus master PCI\r
1234 controller to use to access the system memory's HostAddress.\r
1235 @param Mapping The value to pass to Unmap() when the bus master DMA\r
1236 operation is complete.\r
1237\r
1238 @retval EFI_SUCCESS Success.\r
1239 @retval EFI_INVALID_PARAMETER Invalid parameters found.\r
1240 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
1241 @retval EFI_DEVICE_ERROR The System hardware could not map the requested address.\r
1242 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to lack of resources.\r
4a50cf4e
RN
1243**/\r
1244EFI_STATUS\r
1245EFIAPI\r
1246RootBridgeIoMap (\r
1247 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1248 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION Operation,\r
1249 IN VOID *HostAddress,\r
1250 IN OUT UINTN *NumberOfBytes,\r
1251 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
1252 OUT VOID **Mapping\r
1253 )\r
1254{\r
1255 EFI_STATUS Status;\r
1256 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1257 EFI_PHYSICAL_ADDRESS PhysicalAddress;\r
1258 MAP_INFO *MapInfo;\r
1259\r
1260 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||\r
1261 Mapping == NULL) {\r
1262 return EFI_INVALID_PARAMETER;\r
1263 }\r
1264\r
1265 //\r
1266 // Make sure that Operation is valid\r
1267 //\r
1268 if ((UINT32) Operation >= EfiPciOperationMaximum) {\r
1269 return EFI_INVALID_PARAMETER;\r
1270 }\r
1271\r
1272 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
1273\r
c15da8eb
JY
1274 if (mIoMmuProtocol != NULL) {\r
1275 if (!RootBridge->DmaAbove4G) {\r
1276 //\r
1277 // Clear 64bit support\r
1278 //\r
1279 if (Operation > EfiPciOperationBusMasterCommonBuffer) {\r
1280 Operation = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION) (Operation - EfiPciOperationBusMasterRead64);\r
1281 }\r
1282 }\r
1283 Status = mIoMmuProtocol->Map (\r
1284 mIoMmuProtocol,\r
7a85e847 1285 (EDKII_IOMMU_OPERATION) Operation,\r
c15da8eb
JY
1286 HostAddress,\r
1287 NumberOfBytes,\r
1288 DeviceAddress,\r
1289 Mapping\r
1290 );\r
1291 return Status;\r
1292 }\r
1293\r
4a50cf4e 1294 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress;\r
e58a71d9
AB
1295 if ((!RootBridge->DmaAbove4G ||\r
1296 (Operation != EfiPciOperationBusMasterRead64 &&\r
1297 Operation != EfiPciOperationBusMasterWrite64 &&\r
1298 Operation != EfiPciOperationBusMasterCommonBuffer64)) &&\r
1299 ((PhysicalAddress + *NumberOfBytes) > SIZE_4GB)) {\r
1300\r
4a50cf4e 1301 //\r
e58a71d9
AB
1302 // If the root bridge or the device cannot handle performing DMA above\r
1303 // 4GB but any part of the DMA transfer being mapped is above 4GB, then\r
4a50cf4e
RN
1304 // map the DMA transfer to a buffer below 4GB.\r
1305 //\r
1306\r
1307 if (Operation == EfiPciOperationBusMasterCommonBuffer ||\r
1308 Operation == EfiPciOperationBusMasterCommonBuffer64) {\r
1309 //\r
1310 // Common Buffer operations can not be remapped. If the common buffer\r
1311 // if above 4GB, then it is not possible to generate a mapping, so return\r
1312 // an error.\r
1313 //\r
1314 return EFI_UNSUPPORTED;\r
1315 }\r
1316\r
1317 //\r
1318 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r
1319 // called later.\r
1320 //\r
1321 MapInfo = AllocatePool (sizeof (MAP_INFO));\r
1322 if (MapInfo == NULL) {\r
1323 *NumberOfBytes = 0;\r
1324 return EFI_OUT_OF_RESOURCES;\r
1325 }\r
1326\r
1327 //\r
1328 // Initialize the MAP_INFO structure\r
1329 //\r
1330 MapInfo->Signature = MAP_INFO_SIGNATURE;\r
1331 MapInfo->Operation = Operation;\r
1332 MapInfo->NumberOfBytes = *NumberOfBytes;\r
1333 MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);\r
1334 MapInfo->HostAddress = PhysicalAddress;\r
1335 MapInfo->MappedHostAddress = SIZE_4GB - 1;\r
1336\r
1337 //\r
1338 // Allocate a buffer below 4GB to map the transfer to.\r
1339 //\r
1340 Status = gBS->AllocatePages (\r
1341 AllocateMaxAddress,\r
1342 EfiBootServicesData,\r
1343 MapInfo->NumberOfPages,\r
1344 &MapInfo->MappedHostAddress\r
1345 );\r
1346 if (EFI_ERROR (Status)) {\r
1347 FreePool (MapInfo);\r
1348 *NumberOfBytes = 0;\r
1349 return Status;\r
1350 }\r
1351\r
1352 //\r
1353 // If this is a read operation from the Bus Master's point of view,\r
1354 // then copy the contents of the real buffer into the mapped buffer\r
1355 // so the Bus Master can read the contents of the real buffer.\r
1356 //\r
1357 if (Operation == EfiPciOperationBusMasterRead ||\r
1358 Operation == EfiPciOperationBusMasterRead64) {\r
1359 CopyMem (\r
1360 (VOID *) (UINTN) MapInfo->MappedHostAddress,\r
1361 (VOID *) (UINTN) MapInfo->HostAddress,\r
1362 MapInfo->NumberOfBytes\r
1363 );\r
1364 }\r
1365\r
1366 InsertTailList (&RootBridge->Maps, &MapInfo->Link);\r
1367\r
1368 //\r
1369 // The DeviceAddress is the address of the maped buffer below 4GB\r
1370 //\r
1371 *DeviceAddress = MapInfo->MappedHostAddress;\r
1372 //\r
1373 // Return a pointer to the MAP_INFO structure in Mapping\r
1374 //\r
1375 *Mapping = MapInfo;\r
1376 } else {\r
1377 //\r
1378 // If the root bridge CAN handle performing DMA above 4GB or\r
1379 // the transfer is below 4GB, so the DeviceAddress is simply the\r
1380 // HostAddress\r
1381 //\r
1382 *DeviceAddress = PhysicalAddress;\r
1383 *Mapping = NO_MAPPING;\r
1384 }\r
1385\r
1386 return EFI_SUCCESS;\r
1387}\r
1388\r
1389/**\r
1390 Completes the Map() operation and releases any corresponding resources.\r
1391\r
1392 The Unmap() function completes the Map() operation and releases any\r
1393 corresponding resources.\r
1394 If the operation was an EfiPciOperationBusMasterWrite or\r
1395 EfiPciOperationBusMasterWrite64, the data is committed to the target system\r
1396 memory.\r
1397 Any resources used for the mapping are freed.\r
1398\r
1399 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1400 @param[in] Mapping The mapping value returned from Map().\r
1401\r
1402 @retval EFI_SUCCESS The range was unmapped.\r
1403 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().\r
1404 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
1405**/\r
1406EFI_STATUS\r
1407EFIAPI\r
1408RootBridgeIoUnmap (\r
1409 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1410 IN VOID *Mapping\r
1411 )\r
1412{\r
1413 MAP_INFO *MapInfo;\r
1414 LIST_ENTRY *Link;\r
1415 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
c15da8eb
JY
1416 EFI_STATUS Status;\r
1417\r
1418 if (mIoMmuProtocol != NULL) {\r
1419 Status = mIoMmuProtocol->Unmap (\r
1420 mIoMmuProtocol,\r
1421 Mapping\r
1422 );\r
1423 return Status;\r
1424 }\r
4a50cf4e
RN
1425\r
1426 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
c15da8eb 1427\r
4a50cf4e
RN
1428 //\r
1429 // See if the Map() operation associated with this Unmap() required a mapping\r
1430 // buffer. If a mapping buffer was not required, then this function simply\r
1431 // returns EFI_SUCCESS.\r
1432 //\r
1433 if (Mapping == NO_MAPPING) {\r
1434 return EFI_SUCCESS;\r
1435 }\r
1436\r
1437 MapInfo = NO_MAPPING;\r
1438 for (Link = GetFirstNode (&RootBridge->Maps)\r
1439 ; !IsNull (&RootBridge->Maps, Link)\r
1440 ; Link = GetNextNode (&RootBridge->Maps, Link)\r
1441 ) {\r
1442 MapInfo = MAP_INFO_FROM_LINK (Link);\r
1443 if (MapInfo == Mapping) {\r
1444 break;\r
1445 }\r
1446 }\r
1447 //\r
1448 // Mapping is not a valid value returned by Map()\r
1449 //\r
1450 if (MapInfo != Mapping) {\r
1451 return EFI_INVALID_PARAMETER;\r
1452 }\r
1453 RemoveEntryList (&MapInfo->Link);\r
1454\r
1455 //\r
1456 // If this is a write operation from the Bus Master's point of view,\r
1457 // then copy the contents of the mapped buffer into the real buffer\r
1458 // so the processor can read the contents of the real buffer.\r
1459 //\r
1460 if (MapInfo->Operation == EfiPciOperationBusMasterWrite ||\r
1461 MapInfo->Operation == EfiPciOperationBusMasterWrite64) {\r
1462 CopyMem (\r
1463 (VOID *) (UINTN) MapInfo->HostAddress,\r
1464 (VOID *) (UINTN) MapInfo->MappedHostAddress,\r
1465 MapInfo->NumberOfBytes\r
1466 );\r
1467 }\r
1468\r
1469 //\r
1470 // Free the mapped buffer and the MAP_INFO structure.\r
1471 //\r
1472 gBS->FreePages (MapInfo->MappedHostAddress, MapInfo->NumberOfPages);\r
1473 FreePool (Mapping);\r
1474 return EFI_SUCCESS;\r
1475}\r
1476\r
1477/**\r
1478 Allocates pages that are suitable for an EfiPciOperationBusMasterCommonBuffer\r
1479 or EfiPciOperationBusMasterCommonBuffer64 mapping.\r
1480\r
1481 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1482 @param Type This parameter is not used and must be ignored.\r
1483 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
1484 EfiRuntimeServicesData.\r
1485 @param Pages The number of pages to allocate.\r
1486 @param HostAddress A pointer to store the base system memory address of the\r
1487 allocated range.\r
1488 @param Attributes The requested bit mask of attributes for the allocated\r
1489 range. Only the attributes\r
1490 EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE,\r
1491 EFI_PCI_ATTRIBUTE_MEMORY_CACHED, and\r
1492 EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE may be used with this\r
1493 function.\r
1494\r
1495 @retval EFI_SUCCESS The requested memory pages were allocated.\r
1496 @retval EFI_INVALID_PARAMETER MemoryType is invalid.\r
1497 @retval EFI_INVALID_PARAMETER HostAddress is NULL.\r
1498 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal\r
1499 attribute bits are MEMORY_WRITE_COMBINE,\r
1500 MEMORY_CACHED, and DUAL_ADDRESS_CYCLE.\r
1501 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
1502**/\r
1503EFI_STATUS\r
1504EFIAPI\r
1505RootBridgeIoAllocateBuffer (\r
1506 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1507 IN EFI_ALLOCATE_TYPE Type,\r
1508 IN EFI_MEMORY_TYPE MemoryType,\r
1509 IN UINTN Pages,\r
1510 OUT VOID **HostAddress,\r
1511 IN UINT64 Attributes\r
1512 )\r
1513{\r
1514 EFI_STATUS Status;\r
1515 EFI_PHYSICAL_ADDRESS PhysicalAddress;\r
1516 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1517 EFI_ALLOCATE_TYPE AllocateType;\r
1518\r
1519 //\r
1520 // Validate Attributes\r
1521 //\r
1522 if ((Attributes & EFI_PCI_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {\r
1523 return EFI_UNSUPPORTED;\r
1524 }\r
1525\r
1526 //\r
1527 // Check for invalid inputs\r
1528 //\r
1529 if (HostAddress == NULL) {\r
1530 return EFI_INVALID_PARAMETER;\r
1531 }\r
1532\r
1533 //\r
1534 // The only valid memory types are EfiBootServicesData and\r
1535 // EfiRuntimeServicesData\r
1536 //\r
1537 if (MemoryType != EfiBootServicesData &&\r
1538 MemoryType != EfiRuntimeServicesData) {\r
1539 return EFI_INVALID_PARAMETER;\r
1540 }\r
1541\r
1542 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
1543\r
c15da8eb
JY
1544 if (mIoMmuProtocol != NULL) {\r
1545 if (!RootBridge->DmaAbove4G) {\r
1546 //\r
1547 // Clear DUAL_ADDRESS_CYCLE\r
1548 //\r
8df95dd0 1549 Attributes &= ~((UINT64) EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE);\r
c15da8eb
JY
1550 }\r
1551 Status = mIoMmuProtocol->AllocateBuffer (\r
1552 mIoMmuProtocol,\r
1553 Type,\r
1554 MemoryType,\r
1555 Pages,\r
1556 HostAddress,\r
1557 Attributes\r
1558 );\r
1559 return Status;\r
1560 }\r
1561\r
4a50cf4e 1562 AllocateType = AllocateAnyPages;\r
e58a71d9
AB
1563 if (!RootBridge->DmaAbove4G ||\r
1564 (Attributes & EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {\r
4a50cf4e
RN
1565 //\r
1566 // Limit allocations to memory below 4GB\r
1567 //\r
1568 AllocateType = AllocateMaxAddress;\r
1569 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1);\r
1570 }\r
1571 Status = gBS->AllocatePages (\r
1572 AllocateType,\r
1573 MemoryType,\r
1574 Pages,\r
1575 &PhysicalAddress\r
1576 );\r
1577 if (!EFI_ERROR (Status)) {\r
1578 *HostAddress = (VOID *) (UINTN) PhysicalAddress;\r
1579 }\r
1580\r
1581 return Status;\r
1582}\r
1583\r
1584/**\r
1585 Frees memory that was allocated with AllocateBuffer().\r
1586\r
1587 The FreeBuffer() function frees memory that was allocated with\r
1588 AllocateBuffer().\r
1589\r
1590 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1591 @param Pages The number of pages to free.\r
1592 @param HostAddress The base system memory address of the allocated range.\r
1593\r
1594 @retval EFI_SUCCESS The requested memory pages were freed.\r
1595 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r
1596 Pages was not allocated with AllocateBuffer().\r
1597**/\r
1598EFI_STATUS\r
1599EFIAPI\r
1600RootBridgeIoFreeBuffer (\r
1601 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1602 IN UINTN Pages,\r
1603 OUT VOID *HostAddress\r
1604 )\r
1605{\r
c15da8eb
JY
1606 EFI_STATUS Status;\r
1607\r
1608 if (mIoMmuProtocol != NULL) {\r
1609 Status = mIoMmuProtocol->FreeBuffer (\r
1610 mIoMmuProtocol,\r
1611 Pages,\r
1612 HostAddress\r
1613 );\r
1614 return Status;\r
1615 }\r
1616\r
4a50cf4e
RN
1617 return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages);\r
1618}\r
1619\r
1620/**\r
1621 Flushes all PCI posted write transactions from a PCI host bridge to system\r
1622 memory.\r
1623\r
1624 The Flush() function flushes any PCI posted write transactions from a PCI\r
1625 host bridge to system memory. Posted write transactions are generated by PCI\r
1626 bus masters when they perform write transactions to target addresses in\r
1627 system memory.\r
1628 This function does not flush posted write transactions from any PCI bridges.\r
1629 A PCI controller specific action must be taken to guarantee that the posted\r
1630 write transactions have been flushed from the PCI controller and from all the\r
1631 PCI bridges into the PCI host bridge. This is typically done with a PCI read\r
1632 transaction from the PCI controller prior to calling Flush().\r
1633\r
1634 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1635\r
1636 @retval EFI_SUCCESS The PCI posted write transactions were flushed\r
1637 from the PCI host bridge to system memory.\r
1638 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed\r
1639 from the PCI host bridge due to a hardware error.\r
1640**/\r
1641EFI_STATUS\r
1642EFIAPI\r
1643RootBridgeIoFlush (\r
1644 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This\r
1645 )\r
1646{\r
1647 return EFI_SUCCESS;\r
1648}\r
1649\r
1650/**\r
1651 Gets the attributes that a PCI root bridge supports setting with\r
1652 SetAttributes(), and the attributes that a PCI root bridge is currently\r
1653 using.\r
1654\r
1655 The GetAttributes() function returns the mask of attributes that this PCI\r
1656 root bridge supports and the mask of attributes that the PCI root bridge is\r
1657 currently using.\r
1658\r
1659 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1660 @param Supported A pointer to the mask of attributes that this PCI root\r
1661 bridge supports setting with SetAttributes().\r
1662 @param Attributes A pointer to the mask of attributes that this PCI root\r
1663 bridge is currently using.\r
1664\r
1665 @retval EFI_SUCCESS If Supports is not NULL, then the attributes\r
1666 that the PCI root bridge supports is returned\r
1667 in Supports. If Attributes is not NULL, then\r
1668 the attributes that the PCI root bridge is\r
1669 currently using is returned in Attributes.\r
1670 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.\r
1671**/\r
1672EFI_STATUS\r
1673EFIAPI\r
1674RootBridgeIoGetAttributes (\r
1675 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1676 OUT UINT64 *Supported,\r
1677 OUT UINT64 *Attributes\r
1678 )\r
1679{\r
1680 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1681\r
1682 if (Attributes == NULL && Supported == NULL) {\r
1683 return EFI_INVALID_PARAMETER;\r
1684 }\r
1685\r
1686 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
1687 //\r
1688 // Set the return value for Supported and Attributes\r
1689 //\r
1690 if (Supported != NULL) {\r
1691 *Supported = RootBridge->Supports;\r
1692 }\r
1693\r
1694 if (Attributes != NULL) {\r
1695 *Attributes = RootBridge->Attributes;\r
1696 }\r
1697\r
1698 return EFI_SUCCESS;\r
1699}\r
1700\r
1701/**\r
1702 Sets attributes for a resource range on a PCI root bridge.\r
1703\r
1704 The SetAttributes() function sets the attributes specified in Attributes for\r
1705 the PCI root bridge on the resource range specified by ResourceBase and\r
1706 ResourceLength. Since the granularity of setting these attributes may vary\r
1707 from resource type to resource type, and from platform to platform, the\r
1708 actual resource range and the one passed in by the caller may differ. As a\r
1709 result, this function may set the attributes specified by Attributes on a\r
1710 larger resource range than the caller requested. The actual range is returned\r
1711 in ResourceBase and ResourceLength. The caller is responsible for verifying\r
1712 that the actual range for which the attributes were set is acceptable.\r
1713\r
1714 @param This A pointer to the\r
1715 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
1716 @param Attributes The mask of attributes to set. If the\r
1717 attribute bit MEMORY_WRITE_COMBINE,\r
1718 MEMORY_CACHED, or MEMORY_DISABLE is set,\r
1719 then the resource range is specified by\r
1720 ResourceBase and ResourceLength. If\r
1721 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and\r
1722 MEMORY_DISABLE are not set, then\r
1723 ResourceBase and ResourceLength are ignored,\r
1724 and may be NULL.\r
1725 @param ResourceBase A pointer to the base address of the\r
1726 resource range to be modified by the\r
1727 attributes specified by Attributes.\r
1728 @param ResourceLength A pointer to the length of the resource\r
1729 range to be modified by the attributes\r
1730 specified by Attributes.\r
1731\r
1732 @retval EFI_SUCCESS The current configuration of this PCI root bridge\r
1733 was returned in Resources.\r
1734 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge\r
1735 could not be retrieved.\r
1736**/\r
1737EFI_STATUS\r
1738EFIAPI\r
1739RootBridgeIoSetAttributes (\r
1740 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1741 IN UINT64 Attributes,\r
1742 IN OUT UINT64 *ResourceBase,\r
1743 IN OUT UINT64 *ResourceLength\r
1744 )\r
1745{\r
1746 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1747\r
1748 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
1749\r
1750 if ((Attributes & (~RootBridge->Supports)) != 0) {\r
1751 return EFI_UNSUPPORTED;\r
1752 }\r
1753\r
1754 RootBridge->Attributes = Attributes;\r
1755 return EFI_SUCCESS;\r
1756}\r
1757\r
1758/**\r
1759 Retrieves the current resource settings of this PCI root bridge in the form\r
6fbaed1f 1760 of a set of ACPI resource descriptors.\r
4a50cf4e
RN
1761\r
1762 There are only two resource descriptor types from the ACPI Specification that\r
1763 may be used to describe the current resources allocated to a PCI root bridge.\r
6fbaed1f
DB
1764 These are the QWORD Address Space Descriptor, and the End Tag. The QWORD\r
1765 Address Space Descriptor can describe memory, I/O, and bus number ranges for\r
1766 dynamic or fixed resources. The configuration of a PCI root bridge is described\r
1767 with one or more QWORD Address Space Descriptors followed by an End Tag.\r
4a50cf4e
RN
1768\r
1769 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
6fbaed1f 1770 @param[out] Resources A pointer to the resource descriptors that\r
4a50cf4e 1771 describe the current configuration of this PCI root\r
6fbaed1f 1772 bridge. The storage for the resource\r
4a50cf4e
RN
1773 descriptors is allocated by this function. The\r
1774 caller must treat the return buffer as read-only\r
1775 data, and the buffer must not be freed by the\r
1776 caller.\r
1777\r
1778 @retval EFI_SUCCESS The current configuration of this PCI root bridge\r
1779 was returned in Resources.\r
1780 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge\r
1781 could not be retrieved.\r
1782**/\r
1783EFI_STATUS\r
1784EFIAPI\r
1785RootBridgeIoConfiguration (\r
1786 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,\r
1787 OUT VOID **Resources\r
1788 )\r
1789{\r
1790 PCI_RESOURCE_TYPE Index;\r
1791 PCI_ROOT_BRIDGE_INSTANCE *RootBridge;\r
1792 PCI_RES_NODE *ResAllocNode;\r
1793 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
1794 EFI_ACPI_END_TAG_DESCRIPTOR *End;\r
1795\r
1796 //\r
1797 // Get this instance of the Root Bridge.\r
1798 //\r
1799 RootBridge = ROOT_BRIDGE_FROM_THIS (This);\r
1800 ZeroMem (\r
1801 RootBridge->ConfigBuffer,\r
1802 TypeMax * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR)\r
1803 );\r
1804 Descriptor = RootBridge->ConfigBuffer;\r
1805 for (Index = TypeIo; Index < TypeMax; Index++) {\r
1806\r
1807 ResAllocNode = &RootBridge->ResAllocNode[Index];\r
1808\r
1809 if (ResAllocNode->Status != ResAllocated) {\r
1810 continue;\r
1811 }\r
1812\r
1813 Descriptor->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r
1814 Descriptor->Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;\r
74d0a339
HG
1815 // According to UEFI 2.7, RootBridgeIo->Configuration should return address\r
1816 // range in CPU view (host address), and ResAllocNode->Base is already a CPU\r
1817 // view address (host address).\r
4a50cf4e
RN
1818 Descriptor->AddrRangeMin = ResAllocNode->Base;\r
1819 Descriptor->AddrRangeMax = ResAllocNode->Base + ResAllocNode->Length - 1;\r
1820 Descriptor->AddrLen = ResAllocNode->Length;\r
74d0a339
HG
1821 Descriptor->AddrTranslationOffset = GetTranslationByResourceType (\r
1822 RootBridge,\r
1823 ResAllocNode->Type\r
1824 );\r
1825\r
4a50cf4e
RN
1826 switch (ResAllocNode->Type) {\r
1827\r
1828 case TypeIo:\r
1829 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;\r
1830 break;\r
1831\r
1832 case TypePMem32:\r
1833 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
1834 case TypeMem32:\r
1835 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1836 Descriptor->AddrSpaceGranularity = 32;\r
1837 break;\r
1838\r
1839 case TypePMem64:\r
1840 Descriptor->SpecificFlag = EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;\r
1841 case TypeMem64:\r
1842 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1843 Descriptor->AddrSpaceGranularity = 64;\r
1844 break;\r
1845\r
1846 case TypeBus:\r
1847 Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_BUS;\r
1848 break;\r
1849\r
1850 default:\r
1851 break;\r
1852 }\r
1853\r
1854 Descriptor++;\r
1855 }\r
1856 //\r
1857 // Terminate the entries.\r
1858 //\r
1859 End = (EFI_ACPI_END_TAG_DESCRIPTOR *) Descriptor;\r
1860 End->Desc = ACPI_END_TAG_DESCRIPTOR;\r
1861 End->Checksum = 0x0;\r
1862\r
1863 *Resources = RootBridge->ConfigBuffer;\r
1864 return EFI_SUCCESS;\r
1865}\r