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