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