]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/PciBusDxe/PciIo.c
EdkCompatibilityPkg RuntimeDxe Lib: Fix GCC warning
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciBusDxe / PciIo.c
CommitLineData
9060e3ec 1/** @file\r
2 EFI PCI IO protocol functions implementation for PCI Bus module.\r
3\r
cd5ebaa0
HT
4Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
9060e3ec 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PciBus.h"\r
16\r
17//\r
18// Pci Io Protocol Interface\r
19//\r
20EFI_PCI_IO_PROTOCOL mPciIoInterface = {\r
21 PciIoPollMem,\r
22 PciIoPollIo,\r
23 {\r
24 PciIoMemRead,\r
25 PciIoMemWrite\r
26 },\r
27 {\r
28 PciIoIoRead,\r
29 PciIoIoWrite\r
30 },\r
31 {\r
32 PciIoConfigRead,\r
33 PciIoConfigWrite\r
34 },\r
35 PciIoCopyMem,\r
36 PciIoMap,\r
37 PciIoUnmap,\r
38 PciIoAllocateBuffer,\r
39 PciIoFreeBuffer,\r
40 PciIoFlush,\r
41 PciIoGetLocation,\r
42 PciIoAttributes,\r
43 PciIoGetBarAttributes,\r
44 PciIoSetBarAttributes,\r
45 0,\r
46 NULL\r
47};\r
48\r
49/**\r
50 Report a error Status code of PCI bus driver controller.\r
51\r
52 @param PciIoDevice Pci device instance.\r
53 @param Code Status code value.\r
54\r
55**/\r
56EFI_STATUS\r
57ReportErrorStatusCode (\r
58 IN PCI_IO_DEVICE *PciIoDevice,\r
59 IN EFI_STATUS_CODE_VALUE Code\r
60 )\r
61{\r
62 return REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
63 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
64 Code,\r
65 PciIoDevice->DevicePath\r
66 );\r
67}\r
68\r
69/**\r
70 Initializes a PCI I/O Instance.\r
71\r
72 @param PciIoDevice Pci device instance.\r
73\r
74**/\r
75VOID\r
76InitializePciIoInstance (\r
77 IN PCI_IO_DEVICE *PciIoDevice\r
78 )\r
79{\r
80 CopyMem (&PciIoDevice->PciIo, &mPciIoInterface, sizeof (EFI_PCI_IO_PROTOCOL));\r
81}\r
82\r
83/**\r
84 Verifies access to a PCI Base Address Register (BAR).\r
85\r
86 @param PciIoDevice Pci device instance.\r
87 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
88 base address for the memory or I/O operation to perform.\r
89 @param Type Operation type could be memory or I/O.\r
90 @param Width Signifies the width of the memory or I/O operations.\r
91 @param Count The number of memory or I/O operations to perform.\r
92 @param Offset The offset within the PCI configuration space for the PCI controller.\r
93\r
94 @retval EFI_INVALID_PARAMETER Invalid Width/BarIndex or Bar type.\r
95 @retval EFI_SUCCESS Successfully verified.\r
96\r
97**/\r
98EFI_STATUS\r
99PciIoVerifyBarAccess (\r
100 IN PCI_IO_DEVICE *PciIoDevice,\r
101 IN UINT8 BarIndex,\r
102 IN PCI_BAR_TYPE Type,\r
103 IN IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
104 IN IN UINTN Count,\r
105 IN UINT64 *Offset\r
106 )\r
107{\r
108 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
109 return EFI_INVALID_PARAMETER;\r
110 }\r
111\r
112 if (BarIndex == EFI_PCI_IO_PASS_THROUGH_BAR) {\r
113 return EFI_SUCCESS;\r
114 }\r
115\r
116 //\r
117 // BarIndex 0-5 is legal\r
118 //\r
119 if (BarIndex >= PCI_MAX_BAR) {\r
120 return EFI_INVALID_PARAMETER;\r
121 }\r
122\r
123 if (!CheckBarType (PciIoDevice, BarIndex, Type)) {\r
124 return EFI_INVALID_PARAMETER;\r
125 }\r
126\r
127 //\r
128 // If Width is EfiPciIoWidthFifoUintX then convert to EfiPciIoWidthUintX\r
129 // If Width is EfiPciIoWidthFillUintX then convert to EfiPciIoWidthUintX\r
130 //\r
815119f3 131 if (Width >= EfiPciIoWidthFifoUint8 && Width <= EfiPciIoWidthFifoUint64) {\r
9060e3ec 132 Count = 1;\r
133 }\r
134\r
135 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & 0x03);\r
136\r
137 if ((*Offset + Count * (UINTN)(1 << Width)) - 1 >= PciIoDevice->PciBar[BarIndex].Length) {\r
138 return EFI_INVALID_PARAMETER;\r
139 }\r
140\r
141 *Offset = *Offset + PciIoDevice->PciBar[BarIndex].BaseAddress;\r
142\r
143 return EFI_SUCCESS;\r
144}\r
145\r
146/**\r
147 Verifies access to a PCI Configuration Header.\r
148\r
149 @param PciIoDevice Pci device instance.\r
150 @param Width Signifies the width of the memory or I/O operations.\r
151 @param Count The number of memory or I/O operations to perform.\r
152 @param Offset The offset within the PCI configuration space for the PCI controller.\r
153\r
154 @retval EFI_INVALID_PARAMETER Invalid Width\r
155 @retval EFI_UNSUPPORTED Offset overflowed.\r
156 @retval EFI_SUCCESS Successfully verified.\r
157\r
158**/\r
159EFI_STATUS\r
160PciIoVerifyConfigAccess (\r
161 IN PCI_IO_DEVICE *PciIoDevice,\r
162 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
163 IN UINTN Count,\r
164 IN UINT64 *Offset\r
165 )\r
166{\r
167 UINT64 ExtendOffset;\r
168\r
169 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
170 return EFI_INVALID_PARAMETER;\r
171 }\r
172\r
173 //\r
174 // If Width is EfiPciIoWidthFillUintX then convert to EfiPciIoWidthUintX\r
175 //\r
176 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & 0x03);\r
177\r
178 if (PciIoDevice->IsPciExp) {\r
179 if ((*Offset + Count * (UINTN)(1 << Width)) - 1 >= PCI_EXP_MAX_CONFIG_OFFSET) {\r
180 return EFI_UNSUPPORTED;\r
181 }\r
182\r
183 ExtendOffset = LShiftU64 (*Offset, 32);\r
184 *Offset = EFI_PCI_ADDRESS (PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->FunctionNumber, 0);\r
185 *Offset = (*Offset) | ExtendOffset;\r
186\r
187 } else {\r
188 if ((*Offset + Count * (UINTN)(1 << Width)) - 1 >= PCI_MAX_CONFIG_OFFSET) {\r
189 return EFI_UNSUPPORTED;\r
190 }\r
191\r
192 *Offset = EFI_PCI_ADDRESS (PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->FunctionNumber, *Offset);\r
193 }\r
194\r
195 return EFI_SUCCESS;\r
196}\r
197\r
198/**\r
199 Reads from the memory space of a PCI controller. Returns either when the polling exit criteria is\r
200 satisfied or after a defined duration.\r
201\r
202 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
203 @param Width Signifies the width of the memory or I/O operations.\r
204 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
205 base address for the memory operation to perform.\r
206 @param Offset The offset within the selected BAR to start the memory operation.\r
207 @param Mask Mask used for the polling criteria.\r
208 @param Value The comparison value used for the polling exit criteria.\r
209 @param Delay The number of 100 ns units to poll.\r
210 @param Result Pointer to the last value read from the memory location.\r
211\r
212 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.\r
213 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
214 @retval EFI_UNSUPPORTED Offset is not valid for the BarIndex of this PCI controller.\r
215 @retval EFI_TIMEOUT Delay expired before a match occurred.\r
216 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
217 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
218\r
219**/\r
220EFI_STATUS\r
221EFIAPI\r
222PciIoPollMem (\r
223 IN EFI_PCI_IO_PROTOCOL *This,\r
224 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
225 IN UINT8 BarIndex,\r
226 IN UINT64 Offset,\r
227 IN UINT64 Mask,\r
228 IN UINT64 Value,\r
229 IN UINT64 Delay,\r
230 OUT UINT64 *Result\r
231 )\r
232{\r
233 EFI_STATUS Status;\r
234 PCI_IO_DEVICE *PciIoDevice;\r
235\r
236 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
237\r
238 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
239 return EFI_INVALID_PARAMETER;\r
240 }\r
241\r
242 Status = PciIoVerifyBarAccess (PciIoDevice, BarIndex, PciBarTypeMem, Width, 1, &Offset);\r
243 if (EFI_ERROR (Status)) {\r
244 return EFI_UNSUPPORTED;\r
245 }\r
246\r
247 if (Width > EfiPciIoWidthUint64) {\r
248 return EFI_INVALID_PARAMETER;\r
249 }\r
250\r
aeeb84ba 251 //\r
252 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
253 // \r
254 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
255 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
256 Status = PciIoMemRead (This, Width, BarIndex, Offset, 1, Result);\r
257 if (EFI_ERROR (Status)) {\r
258 return Status;\r
259 }\r
260 if ((*Result & Mask) == Value || Delay == 0) {\r
261 return EFI_SUCCESS;\r
262 }\r
263 do {\r
264 //\r
265 // Stall 10 us = 100 * 100ns\r
266 //\r
267 gBS->Stall (10);\r
268\r
269 Status = PciIoMemRead (This, Width, BarIndex, Offset, 1, Result);\r
270 if (EFI_ERROR (Status)) {\r
271 return Status;\r
272 }\r
273 if ((*Result & Mask) == Value) {\r
274 return EFI_SUCCESS;\r
275 }\r
276 if (Delay <= 100) {\r
277 return EFI_TIMEOUT;\r
278 }\r
279 Delay -= 100;\r
280 } while (TRUE);\r
281 }\r
282 }\r
283 \r
9060e3ec 284 Status = PciIoDevice->PciRootBridgeIo->PollMem (\r
285 PciIoDevice->PciRootBridgeIo,\r
286 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
287 Offset,\r
288 Mask,\r
289 Value,\r
290 Delay,\r
291 Result\r
292 );\r
293\r
294 if (EFI_ERROR (Status)) {\r
295 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
296 }\r
297\r
298 return Status;\r
299}\r
300\r
301/**\r
302 Reads from the memory space of a PCI controller. Returns either when the polling exit criteria is\r
303 satisfied or after a defined duration.\r
304\r
305 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
306 @param Width Signifies the width of the memory or I/O operations.\r
307 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
308 base address for the memory operation to perform.\r
309 @param Offset The offset within the selected BAR to start the memory operation.\r
310 @param Mask Mask used for the polling criteria.\r
311 @param Value The comparison value used for the polling exit criteria.\r
312 @param Delay The number of 100 ns units to poll.\r
313 @param Result Pointer to the last value read from the memory location.\r
314\r
315 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.\r
316 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
317 @retval EFI_UNSUPPORTED Offset is not valid for the BarIndex of this PCI controller.\r
318 @retval EFI_TIMEOUT Delay expired before a match occurred.\r
319 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
320 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
321\r
322**/\r
323EFI_STATUS\r
324EFIAPI\r
325PciIoPollIo (\r
326 IN EFI_PCI_IO_PROTOCOL *This,\r
327 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
328 IN UINT8 BarIndex,\r
329 IN UINT64 Offset,\r
330 IN UINT64 Mask,\r
331 IN UINT64 Value,\r
332 IN UINT64 Delay,\r
333 OUT UINT64 *Result\r
334 )\r
335{\r
336 EFI_STATUS Status;\r
337 PCI_IO_DEVICE *PciIoDevice;\r
338\r
339 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
340\r
341 if (Width < 0 || Width > EfiPciIoWidthUint64) {\r
342 return EFI_INVALID_PARAMETER;\r
343 }\r
344\r
345 Status = PciIoVerifyBarAccess (PciIoDevice, BarIndex, PciBarTypeIo, Width, 1, &Offset);\r
346 if (EFI_ERROR (Status)) {\r
347 return EFI_UNSUPPORTED;\r
348 }\r
349\r
aeeb84ba 350 //\r
351 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
352 // \r
353 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
354 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
355 Status = PciIoIoRead (This, Width, BarIndex, Offset, 1, Result);\r
356 if (EFI_ERROR (Status)) {\r
357 return Status;\r
358 }\r
359 if ((*Result & Mask) == Value || Delay == 0) {\r
360 return EFI_SUCCESS;\r
361 }\r
362 do {\r
363 //\r
364 // Stall 10 us = 100 * 100ns\r
365 //\r
366 gBS->Stall (10);\r
367\r
368 Status = PciIoIoRead (This, Width, BarIndex, Offset, 1, Result);\r
369 if (EFI_ERROR (Status)) {\r
370 return Status;\r
371 }\r
372 if ((*Result & Mask) == Value) {\r
373 return EFI_SUCCESS;\r
374 }\r
375 if (Delay <= 100) {\r
376 return EFI_TIMEOUT;\r
377 }\r
378 Delay -= 100;\r
379 } while (TRUE);\r
380 }\r
381 }\r
382 \r
9060e3ec 383 Status = PciIoDevice->PciRootBridgeIo->PollIo (\r
384 PciIoDevice->PciRootBridgeIo,\r
385 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
386 Offset,\r
387 Mask,\r
388 Value,\r
389 Delay,\r
390 Result\r
391 );\r
392\r
393 if (EFI_ERROR (Status)) {\r
394 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
395 }\r
396\r
397 return Status;\r
398}\r
399\r
400/**\r
401 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.\r
402\r
403 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
404 @param Width Signifies the width of the memory or I/O operations.\r
405 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
406 base address for the memory or I/O operation to perform.\r
407 @param Offset The offset within the selected BAR to start the memory or I/O operation.\r
408 @param Count The number of memory or I/O operations to perform.\r
409 @param Buffer For read operations, the destination buffer to store the results. For write\r
410 operations, the source buffer to write data from.\r
411\r
412 @retval EFI_SUCCESS The data was read from or written to the PCI controller.\r
413 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
414 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not\r
415 valid for the PCI BAR specified by BarIndex.\r
416 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
417 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
418\r
419**/\r
420EFI_STATUS\r
421EFIAPI\r
422PciIoMemRead (\r
423 IN EFI_PCI_IO_PROTOCOL *This,\r
424 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
425 IN UINT8 BarIndex,\r
426 IN UINT64 Offset,\r
427 IN UINTN Count,\r
428 IN OUT VOID *Buffer\r
429 )\r
430{\r
431 EFI_STATUS Status;\r
432 PCI_IO_DEVICE *PciIoDevice;\r
433\r
434 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
435\r
436 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
437 return EFI_INVALID_PARAMETER;\r
438 }\r
439\r
440 if (Buffer == NULL) {\r
441 return EFI_INVALID_PARAMETER;\r
442 }\r
443\r
444 Status = PciIoVerifyBarAccess (PciIoDevice, BarIndex, PciBarTypeMem, Width, Count, &Offset);\r
445 if (EFI_ERROR (Status)) {\r
446 return EFI_UNSUPPORTED;\r
447 }\r
448\r
aeeb84ba 449 //\r
450 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
451 // \r
452 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
453 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 454 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 455 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 456 }\r
457 } \r
458 \r
459\r
9060e3ec 460 Status = PciIoDevice->PciRootBridgeIo->Mem.Read (\r
461 PciIoDevice->PciRootBridgeIo,\r
462 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
463 Offset,\r
464 Count,\r
465 Buffer\r
466 );\r
467\r
468 if (EFI_ERROR (Status)) {\r
469 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_READ_ERROR);\r
470 }\r
471\r
472 return Status;\r
473}\r
474\r
475/**\r
476 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.\r
477\r
478 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
479 @param Width Signifies the width of the memory or I/O operations.\r
480 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
481 base address for the memory or I/O operation to perform.\r
482 @param Offset The offset within the selected BAR to start the memory or I/O operation.\r
483 @param Count The number of memory or I/O operations to perform.\r
484 @param Buffer For read operations, the destination buffer to store the results. For write\r
485 operations, the source buffer to write data from.\r
486\r
487 @retval EFI_SUCCESS The data was read from or written to the PCI controller.\r
488 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
489 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not\r
490 valid for the PCI BAR specified by BarIndex.\r
491 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
492 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
493\r
494**/\r
495EFI_STATUS\r
496EFIAPI\r
497PciIoMemWrite (\r
498 IN EFI_PCI_IO_PROTOCOL *This,\r
499 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
500 IN UINT8 BarIndex,\r
501 IN UINT64 Offset,\r
502 IN UINTN Count,\r
503 IN OUT VOID *Buffer\r
504 )\r
505{\r
506 EFI_STATUS Status;\r
507 PCI_IO_DEVICE *PciIoDevice;\r
508\r
509 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
510\r
511 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
512 return EFI_INVALID_PARAMETER;\r
513 }\r
514\r
515 if (Buffer == NULL) {\r
516 return EFI_INVALID_PARAMETER;\r
517 }\r
518\r
519 Status = PciIoVerifyBarAccess (PciIoDevice, BarIndex, PciBarTypeMem, Width, Count, &Offset);\r
520 if (EFI_ERROR (Status)) {\r
521 return EFI_UNSUPPORTED;\r
522 }\r
523\r
aeeb84ba 524 //\r
525 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
526 // \r
527 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
528 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 529 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 530 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 531 }\r
532 }\r
533\r
9060e3ec 534 Status = PciIoDevice->PciRootBridgeIo->Mem.Write (\r
535 PciIoDevice->PciRootBridgeIo,\r
536 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
537 Offset,\r
538 Count,\r
539 Buffer\r
540 );\r
541\r
542 if (EFI_ERROR (Status)) {\r
543 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_WRITE_ERROR);\r
544 }\r
545\r
546 return Status;\r
547}\r
548\r
549/**\r
550 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.\r
551\r
552 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
553 @param Width Signifies the width of the memory or I/O operations.\r
554 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
555 base address for the memory or I/O operation to perform.\r
556 @param Offset The offset within the selected BAR to start the memory or I/O operation.\r
557 @param Count The number of memory or I/O operations to perform.\r
558 @param Buffer For read operations, the destination buffer to store the results. For write\r
559 operations, the source buffer to write data from.\r
560\r
561 @retval EFI_SUCCESS The data was read from or written to the PCI controller.\r
562 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
563 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not\r
564 valid for the PCI BAR specified by BarIndex.\r
565 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
566 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
567\r
568**/\r
569EFI_STATUS\r
570EFIAPI\r
571PciIoIoRead (\r
572 IN EFI_PCI_IO_PROTOCOL *This,\r
573 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
574 IN UINT8 BarIndex,\r
575 IN UINT64 Offset,\r
576 IN UINTN Count,\r
577 IN OUT VOID *Buffer\r
578 )\r
579{\r
580 EFI_STATUS Status;\r
581 PCI_IO_DEVICE *PciIoDevice;\r
582\r
583 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
584\r
585 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
586 return EFI_INVALID_PARAMETER;\r
587 }\r
588\r
589 if (Buffer == NULL) {\r
590 return EFI_INVALID_PARAMETER;\r
591 }\r
592\r
593 Status = PciIoVerifyBarAccess (PciIoDevice, BarIndex, PciBarTypeIo, Width, Count, &Offset);\r
594 if (EFI_ERROR (Status)) {\r
595 return EFI_UNSUPPORTED;\r
596 }\r
597\r
aeeb84ba 598 //\r
599 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
600 // \r
601 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
602 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 603 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 604 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 605 }\r
606 } \r
607\r
9060e3ec 608 Status = PciIoDevice->PciRootBridgeIo->Io.Read (\r
609 PciIoDevice->PciRootBridgeIo,\r
610 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
611 Offset,\r
612 Count,\r
613 Buffer\r
614 );\r
615\r
616 if (EFI_ERROR (Status)) {\r
617 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_READ_ERROR);\r
618 }\r
619\r
620 return Status;\r
621}\r
622\r
623/**\r
624 Enable a PCI driver to access PCI controller registers in the PCI memory or I/O space.\r
625\r
626 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
627 @param Width Signifies the width of the memory or I/O operations.\r
628 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
629 base address for the memory or I/O operation to perform.\r
630 @param Offset The offset within the selected BAR to start the memory or I/O operation.\r
631 @param Count The number of memory or I/O operations to perform.\r
632 @param Buffer For read operations, the destination buffer to store the results. For write\r
633 operations, the source buffer to write data from.\r
634\r
635 @retval EFI_SUCCESS The data was read from or written to the PCI controller.\r
636 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
637 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not\r
638 valid for the PCI BAR specified by BarIndex.\r
639 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
640 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
641\r
642**/\r
643EFI_STATUS\r
644EFIAPI\r
645PciIoIoWrite (\r
646 IN EFI_PCI_IO_PROTOCOL *This,\r
647 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
648 IN UINT8 BarIndex,\r
649 IN UINT64 Offset,\r
650 IN UINTN Count,\r
651 IN OUT VOID *Buffer\r
652 )\r
653{\r
654 EFI_STATUS Status;\r
655 PCI_IO_DEVICE *PciIoDevice;\r
656\r
657 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
658\r
659 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
660 return EFI_INVALID_PARAMETER;\r
661 }\r
662\r
663 if (Buffer == NULL) {\r
664 return EFI_INVALID_PARAMETER;\r
665 }\r
666\r
667 Status = PciIoVerifyBarAccess (PciIoDevice, BarIndex, PciBarTypeIo, Width, Count, &Offset);\r
668 if (EFI_ERROR (Status)) {\r
669 return EFI_UNSUPPORTED;\r
670 }\r
671\r
aeeb84ba 672 //\r
673 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
674 // \r
675 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
676 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 677 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 678 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 679 }\r
680 } \r
681\r
9060e3ec 682 Status = PciIoDevice->PciRootBridgeIo->Io.Write (\r
683 PciIoDevice->PciRootBridgeIo,\r
684 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
685 Offset,\r
686 Count,\r
687 Buffer\r
688 );\r
689\r
690 if (EFI_ERROR (Status)) {\r
691 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_WRITE_ERROR);\r
692 }\r
693\r
694 return Status;\r
695}\r
696\r
697/**\r
698 Enable a PCI driver to access PCI controller registers in PCI configuration space.\r
699\r
700 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
701 @param Width Signifies the width of the memory operations.\r
702 @param Offset The offset within the PCI configuration space for the PCI controller.\r
703 @param Count The number of PCI configuration operations to perform.\r
704 @param Buffer For read operations, the destination buffer to store the results. For write\r
705 operations, the source buffer to write data from.\r
706\r
707\r
708 @retval EFI_SUCCESS The data was read from or written to the PCI controller.\r
709 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not\r
710 valid for the PCI configuration header of the PCI controller.\r
711 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
712 @retval EFI_INVALID_PARAMETER Buffer is NULL or Width is invalid.\r
713\r
714**/\r
715EFI_STATUS\r
716EFIAPI\r
717PciIoConfigRead (\r
718 IN EFI_PCI_IO_PROTOCOL *This,\r
719 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
720 IN UINT32 Offset,\r
721 IN UINTN Count,\r
722 IN OUT VOID *Buffer\r
723 )\r
724{\r
725 EFI_STATUS Status;\r
726 PCI_IO_DEVICE *PciIoDevice;\r
727 UINT64 Address;\r
728\r
729 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
730\r
731 Address = Offset;\r
732 Status = PciIoVerifyConfigAccess (PciIoDevice, Width, Count, &Address);\r
733 if (EFI_ERROR (Status)) {\r
734 return Status;\r
735 }\r
aeeb84ba 736 \r
737 //\r
738 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
739 // \r
740 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
741 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 742 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 743 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 744 }\r
745 } \r
9060e3ec 746\r
747 Status = PciIoDevice->PciRootBridgeIo->Pci.Read (\r
748 PciIoDevice->PciRootBridgeIo,\r
749 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
750 Address,\r
751 Count,\r
752 Buffer\r
753 );\r
754\r
755 if (EFI_ERROR (Status)) {\r
756 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_READ_ERROR);\r
757 }\r
758\r
759 return Status;\r
760}\r
761\r
762/**\r
763 Enable a PCI driver to access PCI controller registers in PCI configuration space.\r
764\r
765 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
766 @param Width Signifies the width of the memory operations.\r
767 @param Offset The offset within the PCI configuration space for the PCI controller.\r
768 @param Count The number of PCI configuration operations to perform.\r
769 @param Buffer For read operations, the destination buffer to store the results. For write\r
770 operations, the source buffer to write data from.\r
771\r
772\r
773 @retval EFI_SUCCESS The data was read from or written to the PCI controller.\r
774 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not\r
775 valid for the PCI configuration header of the PCI controller.\r
776 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
777 @retval EFI_INVALID_PARAMETER Buffer is NULL or Width is invalid.\r
778\r
779**/\r
780EFI_STATUS\r
781EFIAPI\r
782PciIoConfigWrite (\r
783 IN EFI_PCI_IO_PROTOCOL *This,\r
784 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
785 IN UINT32 Offset,\r
786 IN UINTN Count,\r
787 IN OUT VOID *Buffer\r
788 )\r
789{\r
790 EFI_STATUS Status;\r
791 PCI_IO_DEVICE *PciIoDevice;\r
792 UINT64 Address;\r
793\r
794 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
795\r
796 Address = Offset;\r
797 Status = PciIoVerifyConfigAccess (PciIoDevice, Width, Count, &Address);\r
798 if (EFI_ERROR (Status)) {\r
799 return Status;\r
800 }\r
801\r
aeeb84ba 802 //\r
803 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
804 // \r
805 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
806 if ((Offset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 807 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 808 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 809 }\r
810 } \r
811 \r
9060e3ec 812 Status = PciIoDevice->PciRootBridgeIo->Pci.Write (\r
813 PciIoDevice->PciRootBridgeIo,\r
814 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
815 Address,\r
816 Count,\r
817 Buffer\r
818 );\r
819\r
820 if (EFI_ERROR (Status)) {\r
821 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_WRITE_ERROR);\r
822 }\r
823\r
824 return Status;\r
825}\r
826\r
827/**\r
828 Enables a PCI driver to copy one region of PCI memory space to another region of PCI\r
829 memory space.\r
830\r
831 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
832 @param Width Signifies the width of the memory operations.\r
833 @param DestBarIndex The BAR index in the standard PCI Configuration header to use as the\r
834 base address for the memory operation to perform.\r
835 @param DestOffset The destination offset within the BAR specified by DestBarIndex to\r
836 start the memory writes for the copy operation.\r
837 @param SrcBarIndex The BAR index in the standard PCI Configuration header to use as the\r
838 base address for the memory operation to perform.\r
839 @param SrcOffset The source offset within the BAR specified by SrcBarIndex to start\r
840 the memory reads for the copy operation.\r
841 @param Count The number of memory operations to perform. Bytes moved is Width\r
842 size * Count, starting at DestOffset and SrcOffset.\r
843\r
844 @retval EFI_SUCCESS The data was copied from one memory region to another memory region.\r
845 @retval EFI_UNSUPPORTED DestBarIndex not valid for this PCI controller.\r
846 @retval EFI_UNSUPPORTED SrcBarIndex not valid for this PCI controller.\r
847 @retval EFI_UNSUPPORTED The address range specified by DestOffset, Width, and Count\r
848 is not valid for the PCI BAR specified by DestBarIndex.\r
849 @retval EFI_UNSUPPORTED The address range specified by SrcOffset, Width, and Count is\r
850 not valid for the PCI BAR specified by SrcBarIndex.\r
851 @retval EFI_INVALID_PARAMETER Width is invalid.\r
852 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
853\r
854**/\r
855EFI_STATUS\r
856EFIAPI\r
857PciIoCopyMem (\r
858 IN EFI_PCI_IO_PROTOCOL *This,\r
859 IN EFI_PCI_IO_PROTOCOL_WIDTH Width,\r
860 IN UINT8 DestBarIndex,\r
861 IN UINT64 DestOffset,\r
862 IN UINT8 SrcBarIndex,\r
863 IN UINT64 SrcOffset,\r
864 IN UINTN Count\r
865 )\r
866{\r
867 EFI_STATUS Status;\r
868 PCI_IO_DEVICE *PciIoDevice;\r
869\r
870 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
871\r
872 if (Width < 0 || Width >= EfiPciIoWidthMaximum) {\r
873 return EFI_INVALID_PARAMETER;\r
874 }\r
875\r
876 if (Width == EfiPciIoWidthFifoUint8 ||\r
877 Width == EfiPciIoWidthFifoUint16 ||\r
878 Width == EfiPciIoWidthFifoUint32 ||\r
879 Width == EfiPciIoWidthFifoUint64 ||\r
880 Width == EfiPciIoWidthFillUint8 ||\r
881 Width == EfiPciIoWidthFillUint16 ||\r
882 Width == EfiPciIoWidthFillUint32 ||\r
883 Width == EfiPciIoWidthFillUint64) {\r
884 return EFI_INVALID_PARAMETER;\r
885 }\r
886\r
887 Status = PciIoVerifyBarAccess (PciIoDevice, DestBarIndex, PciBarTypeMem, Width, Count, &DestOffset);\r
888 if (EFI_ERROR (Status)) {\r
889 return EFI_UNSUPPORTED;\r
890 }\r
891\r
892 Status = PciIoVerifyBarAccess (PciIoDevice, SrcBarIndex, PciBarTypeMem, Width, Count, &SrcOffset);\r
893 if (EFI_ERROR (Status)) {\r
894 return EFI_UNSUPPORTED;\r
895 }\r
896\r
aeeb84ba 897 //\r
898 // If request is not aligned, then convert request to EfiPciIoWithXXXUint8\r
899 // \r
900 if (FeaturePcdGet (PcdUnalignedPciIoEnable)) {\r
901 if ((SrcOffset & ((1 << (Width & 0x03)) - 1)) != 0 || (DestOffset & ((1 << (Width & 0x03)) - 1)) != 0) {\r
aeeb84ba 902 Count *= (UINTN)(1 << (Width & 0x03));\r
c1450e16 903 Width = (EFI_PCI_IO_PROTOCOL_WIDTH) (Width & (~0x03));\r
aeeb84ba 904 }\r
905 } \r
906\r
9060e3ec 907 Status = PciIoDevice->PciRootBridgeIo->CopyMem (\r
908 PciIoDevice->PciRootBridgeIo,\r
909 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) Width,\r
910 DestOffset,\r
911 SrcOffset,\r
912 Count\r
913 );\r
914\r
915 if (EFI_ERROR (Status)) {\r
916 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
917 }\r
918\r
919 return Status;\r
920}\r
921\r
922/**\r
923 Provides the PCI controller-specific addresses needed to access system memory.\r
924\r
925 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
926 @param Operation Indicates if the bus master is going to read or write to system memory.\r
927 @param HostAddress The system memory address to map to the PCI controller.\r
928 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
929 that were mapped.\r
930 @param DeviceAddress The resulting map address for the bus master PCI controller to use to\r
931 access the hosts HostAddress.\r
932 @param Mapping A resulting value to pass to Unmap().\r
933\r
934 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
935 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
936 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
937 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
938 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
939\r
940**/\r
941EFI_STATUS\r
942EFIAPI\r
943PciIoMap (\r
944 IN EFI_PCI_IO_PROTOCOL *This,\r
945 IN EFI_PCI_IO_PROTOCOL_OPERATION Operation,\r
946 IN VOID *HostAddress,\r
947 IN OUT UINTN *NumberOfBytes,\r
948 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
949 OUT VOID **Mapping\r
950 )\r
951{\r
952 EFI_STATUS Status;\r
953 PCI_IO_DEVICE *PciIoDevice;\r
954\r
955 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
956\r
957 if (Operation < 0 || Operation >= EfiPciIoOperationMaximum) {\r
958 return EFI_INVALID_PARAMETER;\r
959 }\r
960\r
961 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL) {\r
962 return EFI_INVALID_PARAMETER;\r
963 }\r
964\r
965 if ((PciIoDevice->Attributes & EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE) != 0) {\r
966 Operation = (EFI_PCI_IO_PROTOCOL_OPERATION) (Operation + EfiPciOperationBusMasterRead64);\r
967 }\r
968\r
969 Status = PciIoDevice->PciRootBridgeIo->Map (\r
970 PciIoDevice->PciRootBridgeIo,\r
971 (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION) Operation,\r
972 HostAddress,\r
973 NumberOfBytes,\r
974 DeviceAddress,\r
975 Mapping\r
976 );\r
977\r
978 if (EFI_ERROR (Status)) {\r
979 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
980 }\r
981\r
982 return Status;\r
983}\r
984\r
985/**\r
986 Completes the Map() operation and releases any corresponding resources.\r
987\r
988 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
989 @param Mapping The mapping value returned from Map().\r
990\r
991 @retval EFI_SUCCESS The range was unmapped.\r
992 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
993\r
994**/\r
995EFI_STATUS\r
996EFIAPI\r
997PciIoUnmap (\r
998 IN EFI_PCI_IO_PROTOCOL *This,\r
999 IN VOID *Mapping\r
1000 )\r
1001{\r
1002 EFI_STATUS Status;\r
1003 PCI_IO_DEVICE *PciIoDevice;\r
1004\r
1005 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1006\r
1007 Status = PciIoDevice->PciRootBridgeIo->Unmap (\r
1008 PciIoDevice->PciRootBridgeIo,\r
1009 Mapping\r
1010 );\r
1011\r
1012 if (EFI_ERROR (Status)) {\r
1013 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
1014 }\r
1015\r
1016 return Status;\r
1017}\r
1018\r
1019/**\r
1020 Allocates pages that are suitable for an EfiPciIoOperationBusMasterCommonBuffer\r
1021 mapping.\r
1022\r
1023 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1024 @param Type This parameter is not used and must be ignored.\r
1025 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
1026 EfiRuntimeServicesData.\r
1027 @param Pages The number of pages to allocate.\r
1028 @param HostAddress A pointer to store the base system memory address of the\r
1029 allocated range.\r
1030 @param Attributes The requested bit mask of attributes for the allocated range.\r
1031\r
1032 @retval EFI_SUCCESS The requested memory pages were allocated.\r
1033 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
1034 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
1035 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1036 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
1037\r
1038**/\r
1039EFI_STATUS\r
1040EFIAPI\r
1041PciIoAllocateBuffer (\r
1042 IN EFI_PCI_IO_PROTOCOL *This,\r
1043 IN EFI_ALLOCATE_TYPE Type,\r
1044 IN EFI_MEMORY_TYPE MemoryType,\r
1045 IN UINTN Pages,\r
1046 OUT VOID **HostAddress,\r
1047 IN UINT64 Attributes\r
1048 )\r
1049{\r
1050 EFI_STATUS Status;\r
1051 PCI_IO_DEVICE *PciIoDevice;\r
1052\r
1053 if ((Attributes &\r
1054 (~(EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE | EFI_PCI_ATTRIBUTE_MEMORY_CACHED))) != 0){\r
1055 return EFI_UNSUPPORTED;\r
1056 }\r
1057\r
1058 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1059\r
1060 if ((PciIoDevice->Attributes & EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE) != 0) {\r
1061 Attributes |= EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE;\r
1062 }\r
1063\r
1064 Status = PciIoDevice->PciRootBridgeIo->AllocateBuffer (\r
1065 PciIoDevice->PciRootBridgeIo,\r
1066 Type,\r
1067 MemoryType,\r
1068 Pages,\r
1069 HostAddress,\r
1070 Attributes\r
1071 );\r
1072\r
1073 if (EFI_ERROR (Status)) {\r
1074 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
1075 }\r
1076\r
1077 return Status;\r
1078}\r
1079\r
1080/**\r
1081 Frees memory that was allocated with AllocateBuffer().\r
1082\r
1083 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1084 @param Pages The number of pages to free.\r
1085 @param HostAddress The base system memory address of the allocated range.\r
1086\r
1087 @retval EFI_SUCCESS The requested memory pages were freed.\r
1088 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
1089 was not allocated with AllocateBuffer().\r
1090\r
1091**/\r
1092EFI_STATUS\r
1093EFIAPI\r
1094PciIoFreeBuffer (\r
1095 IN EFI_PCI_IO_PROTOCOL *This,\r
1096 IN UINTN Pages,\r
1097 IN VOID *HostAddress\r
1098 )\r
1099{\r
1100 EFI_STATUS Status;\r
1101 PCI_IO_DEVICE *PciIoDevice;\r
1102\r
1103 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1104\r
1105 Status = PciIoDevice->PciRootBridgeIo->FreeBuffer (\r
1106 PciIoDevice->PciRootBridgeIo,\r
1107 Pages,\r
1108 HostAddress\r
1109 );\r
1110\r
1111 if (EFI_ERROR (Status)) {\r
1112 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
1113 }\r
1114\r
1115 return Status;\r
1116}\r
1117\r
1118/**\r
1119 Flushes all PCI posted write transactions from a PCI host bridge to system memory.\r
1120\r
1121 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1122\r
1123 @retval EFI_SUCCESS The PCI posted write transactions were flushed from the PCI host\r
1124 bridge to system memory.\r
1125 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed from the PCI\r
1126 host bridge due to a hardware error.\r
1127\r
1128**/\r
1129EFI_STATUS\r
1130EFIAPI\r
1131PciIoFlush (\r
1132 IN EFI_PCI_IO_PROTOCOL *This\r
1133 )\r
1134{\r
1135 EFI_STATUS Status;\r
1136 PCI_IO_DEVICE *PciIoDevice;\r
1137\r
1138 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1139\r
1140 Status = PciIoDevice->PciRootBridgeIo->Flush (\r
1141 PciIoDevice->PciRootBridgeIo\r
1142 );\r
1143 if (EFI_ERROR (Status)) {\r
1144 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
1145 }\r
1146\r
1147 return Status;\r
1148}\r
1149\r
1150/**\r
1151 Retrieves this PCI controller's current PCI bus number, device number, and function number.\r
1152\r
1153 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1154 @param SegmentNumber The PCI controller's current PCI segment number.\r
1155 @param BusNumber The PCI controller's current PCI bus number.\r
1156 @param DeviceNumber The PCI controller's current PCI device number.\r
1157 @param FunctionNumber The PCI controller's current PCI function number.\r
1158\r
1159 @retval EFI_SUCCESS The PCI controller location was returned.\r
1160 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1161\r
1162**/\r
1163EFI_STATUS\r
1164EFIAPI\r
1165PciIoGetLocation (\r
1166 IN EFI_PCI_IO_PROTOCOL *This,\r
1167 OUT UINTN *Segment,\r
1168 OUT UINTN *Bus,\r
1169 OUT UINTN *Device,\r
1170 OUT UINTN *Function\r
1171 )\r
1172{\r
1173 PCI_IO_DEVICE *PciIoDevice;\r
1174\r
1175 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1176\r
1177 if (Segment == NULL || Bus == NULL || Device == NULL || Function == NULL) {\r
1178 return EFI_INVALID_PARAMETER;\r
1179 }\r
1180\r
1181 *Segment = PciIoDevice->PciRootBridgeIo->SegmentNumber;\r
1182 *Bus = PciIoDevice->BusNumber;\r
1183 *Device = PciIoDevice->DeviceNumber;\r
1184 *Function = PciIoDevice->FunctionNumber;\r
1185\r
1186 return EFI_SUCCESS;\r
1187}\r
1188\r
1189/**\r
1190 Check BAR type for PCI resource.\r
1191\r
1192 @param PciIoDevice PCI device instance.\r
1193 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
1194 base address for the memory or I/O operation to perform.\r
1195 @param BarType Memory or I/O.\r
1196\r
1197 @retval TRUE Pci device's bar type is same with input BarType.\r
1198 @retval TRUE Pci device's bar type is not same with input BarType.\r
1199\r
1200**/\r
1201BOOLEAN\r
1202CheckBarType (\r
1203 IN PCI_IO_DEVICE *PciIoDevice,\r
1204 IN UINT8 BarIndex,\r
1205 IN PCI_BAR_TYPE BarType\r
1206 )\r
1207{\r
1208 switch (BarType) {\r
1209\r
1210 case PciBarTypeMem:\r
1211\r
1212 if (PciIoDevice->PciBar[BarIndex].BarType != PciBarTypeMem32 &&\r
1213 PciIoDevice->PciBar[BarIndex].BarType != PciBarTypePMem32 &&\r
1214 PciIoDevice->PciBar[BarIndex].BarType != PciBarTypePMem64 &&\r
1215 PciIoDevice->PciBar[BarIndex].BarType != PciBarTypeMem64 ) {\r
1216 return FALSE;\r
1217 }\r
1218\r
1219 return TRUE;\r
1220\r
1221 case PciBarTypeIo:\r
1222 if (PciIoDevice->PciBar[BarIndex].BarType != PciBarTypeIo32 &&\r
1223 PciIoDevice->PciBar[BarIndex].BarType != PciBarTypeIo16){\r
1224 return FALSE;\r
1225 }\r
1226\r
1227 return TRUE;\r
1228\r
1229 default:\r
1230 break;\r
1231 }\r
1232\r
1233 return FALSE;\r
1234}\r
1235\r
1236/**\r
1237 Set/Disable new attributes to a Root Bridge.\r
1238\r
1239 @param PciIoDevice Pci device instance.\r
1240 @param Attributes New attribute want to be set.\r
1241 @param Operation Set or Disable.\r
1242\r
1243 @retval EFI_UNSUPPORTED If root bridge does not support change attribute.\r
1244 @retval EFI_SUCCESS Successfully set new attributs.\r
1245\r
1246**/\r
1247EFI_STATUS\r
1248ModifyRootBridgeAttributes (\r
1249 IN PCI_IO_DEVICE *PciIoDevice,\r
1250 IN UINT64 Attributes,\r
1251 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation\r
1252 )\r
1253{\r
1254 UINT64 PciRootBridgeSupports;\r
1255 UINT64 PciRootBridgeAttributes;\r
1256 UINT64 NewPciRootBridgeAttributes;\r
1257 EFI_STATUS Status;\r
1258\r
1259 //\r
1260 // Get the current attributes of this PCI device's PCI Root Bridge\r
1261 //\r
1262 Status = PciIoDevice->PciRootBridgeIo->GetAttributes (\r
1263 PciIoDevice->PciRootBridgeIo,\r
1264 &PciRootBridgeSupports,\r
1265 &PciRootBridgeAttributes\r
1266 );\r
1267 if (EFI_ERROR (Status)) {\r
1268 return EFI_UNSUPPORTED;\r
1269 }\r
1270\r
7afa5ea8 1271 //\r
1272 // Mask off EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE & \r
1273 // EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM, which are not supported by PCI root bridge.\r
1274 //\r
1275 Attributes &= ~(UINT64)(EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE |\r
1276 EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM);\r
1277\r
9060e3ec 1278 //\r
1279 // Record the new attribute of the Root Bridge\r
1280 //\r
1281 if (Operation == EfiPciIoAttributeOperationEnable) {\r
1282 NewPciRootBridgeAttributes = PciRootBridgeAttributes | Attributes;\r
1283 } else {\r
1284 NewPciRootBridgeAttributes = PciRootBridgeAttributes & (~Attributes);\r
1285 }\r
1286\r
1287 //\r
1288 // Call the PCI Root Bridge to attempt to modify the attributes\r
1289 //\r
1290 if ((NewPciRootBridgeAttributes ^ PciRootBridgeAttributes) != 0) {\r
1291\r
1292 Status = PciIoDevice->PciRootBridgeIo->SetAttributes (\r
1293 PciIoDevice->PciRootBridgeIo,\r
1294 NewPciRootBridgeAttributes,\r
1295 NULL,\r
1296 NULL\r
1297 );\r
1298 if (EFI_ERROR (Status)) {\r
1299 //\r
1300 // The PCI Root Bridge could not modify the attributes, so return the error.\r
1301 //\r
1302 return EFI_UNSUPPORTED;\r
1303 }\r
1304 }\r
1305\r
1306 //\r
1307 // Also update the attributes for this Root Bridge structure\r
1308 //\r
1309 PciIoDevice->Attributes = NewPciRootBridgeAttributes;\r
1310\r
1311 return EFI_SUCCESS;\r
1312}\r
1313\r
1314/**\r
1315 Check whether this device can be enable/disable to snoop.\r
1316\r
1317 @param PciIoDevice Pci device instance.\r
1318 @param Operation Enable/Disable.\r
1319\r
1320 @retval EFI_UNSUPPORTED Pci device is not GFX device or not support snoop.\r
1321 @retval EFI_SUCCESS Snoop can be supported.\r
1322\r
1323**/\r
1324EFI_STATUS\r
1325SupportPaletteSnoopAttributes (\r
1326 IN PCI_IO_DEVICE *PciIoDevice,\r
1327 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation\r
1328 )\r
1329{\r
1330 PCI_IO_DEVICE *Temp;\r
1331 UINT16 VGACommand;\r
1332\r
1333 //\r
1334 // Snoop attribute can be only modified by GFX\r
1335 //\r
1336 if (!IS_PCI_GFX (&PciIoDevice->Pci)) {\r
1337 return EFI_UNSUPPORTED;\r
1338 }\r
1339\r
1340 //\r
1341 // Get the boot VGA on the same segement\r
1342 //\r
1343 Temp = ActiveVGADeviceOnTheSameSegment (PciIoDevice);\r
1344\r
1345 if (Temp == NULL) {\r
1346 //\r
1347 // If there is no VGA device on the segement, set\r
1348 // this graphics card to decode the palette range\r
1349 //\r
1350 return EFI_SUCCESS;\r
1351 }\r
1352\r
1353 //\r
1354 // Check these two agents are on the same path\r
1355 //\r
1356 if (!PciDevicesOnTheSamePath (Temp, PciIoDevice)) {\r
1357 //\r
1358 // they are not on the same path, so snoop can be enabled or disabled\r
1359 //\r
1360 return EFI_SUCCESS;\r
1361 }\r
1362 //\r
1363 // Check if they are on the same bus\r
1364 //\r
1365 if (Temp->Parent == PciIoDevice->Parent) {\r
1366\r
1367 PCI_READ_COMMAND_REGISTER (Temp, &VGACommand);\r
1368\r
1369 //\r
1370 // If they are on the same bus, either one can\r
1371 // be set to snoop, the other set to decode\r
1372 //\r
1373 if ((VGACommand & EFI_PCI_COMMAND_VGA_PALETTE_SNOOP) != 0) {\r
1374 //\r
1375 // VGA has set to snoop, so GFX can be only set to disable snoop\r
1376 //\r
1377 if (Operation == EfiPciIoAttributeOperationEnable) {\r
1378 return EFI_UNSUPPORTED;\r
1379 }\r
1380 } else {\r
1381 //\r
1382 // VGA has disabled to snoop, so GFX can be only enabled\r
1383 //\r
1384 if (Operation == EfiPciIoAttributeOperationDisable) {\r
1385 return EFI_UNSUPPORTED;\r
1386 }\r
1387 }\r
1388\r
1389 return EFI_SUCCESS;\r
1390 }\r
1391\r
1392 //\r
1393 // If they are on the same path but on the different bus\r
1394 // The first agent is set to snoop, the second one set to\r
1395 // decode\r
1396 //\r
1397\r
1398 if (Temp->BusNumber < PciIoDevice->BusNumber) {\r
1399 //\r
1400 // GFX should be set to decode\r
1401 //\r
1402 if (Operation == EfiPciIoAttributeOperationDisable) {\r
1403 PCI_ENABLE_COMMAND_REGISTER (Temp, EFI_PCI_COMMAND_VGA_PALETTE_SNOOP);\r
1404 Temp->Attributes |= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP;\r
1405 } else {\r
1406 return EFI_UNSUPPORTED;\r
1407 }\r
1408\r
1409 } else {\r
1410 //\r
1411 // GFX should be set to snoop\r
1412 //\r
1413 if (Operation == EfiPciIoAttributeOperationEnable) {\r
1414 PCI_DISABLE_COMMAND_REGISTER (Temp, EFI_PCI_COMMAND_VGA_PALETTE_SNOOP);\r
1415 Temp->Attributes &= (~EFI_PCI_COMMAND_VGA_PALETTE_SNOOP);\r
1416 } else {\r
1417 return EFI_UNSUPPORTED;\r
1418 }\r
1419\r
1420 }\r
1421\r
1422 return EFI_SUCCESS;\r
1423}\r
1424\r
1425/**\r
1426 Performs an operation on the attributes that this PCI controller supports. The operations include\r
1427 getting the set of supported attributes, retrieving the current attributes, setting the current\r
1428 attributes, enabling attributes, and disabling attributes.\r
1429\r
1430 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1431 @param Operation The operation to perform on the attributes for this PCI controller.\r
1432 @param Attributes The mask of attributes that are used for Set, Enable, and Disable\r
1433 operations.\r
1434 @param Result A pointer to the result mask of attributes that are returned for the Get\r
1435 and Supported operations.\r
1436\r
1437 @retval EFI_SUCCESS The operation on the PCI controller's attributes was completed.\r
1438 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1439 @retval EFI_UNSUPPORTED one or more of the bits set in\r
1440 Attributes are not supported by this PCI controller or one of\r
1441 its parent bridges when Operation is Set, Enable or Disable.\r
1442\r
1443**/\r
1444EFI_STATUS\r
1445EFIAPI\r
1446PciIoAttributes (\r
1447 IN EFI_PCI_IO_PROTOCOL * This,\r
1448 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation,\r
1449 IN UINT64 Attributes,\r
1450 OUT UINT64 *Result OPTIONAL\r
1451 )\r
1452{\r
1453 EFI_STATUS Status;\r
1454\r
1455 PCI_IO_DEVICE *PciIoDevice;\r
1456 PCI_IO_DEVICE *UpStreamBridge;\r
1457 PCI_IO_DEVICE *Temp;\r
1458\r
1459 UINT64 Supports;\r
1460 UINT64 UpStreamAttributes;\r
1461 UINT16 BridgeControl;\r
1462 UINT16 Command;\r
1463\r
1464 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1465\r
1466 switch (Operation) {\r
1467 case EfiPciIoAttributeOperationGet:\r
1468 if (Result == NULL) {\r
1469 return EFI_INVALID_PARAMETER;\r
1470 }\r
1471\r
1472 *Result = PciIoDevice->Attributes;\r
1473 return EFI_SUCCESS;\r
1474\r
1475 case EfiPciIoAttributeOperationSupported:\r
1476 if (Result == NULL) {\r
1477 return EFI_INVALID_PARAMETER;\r
1478 }\r
1479\r
1480 *Result = PciIoDevice->Supports;\r
1481 return EFI_SUCCESS;\r
1482\r
1483 case EfiPciIoAttributeOperationSet:\r
1484 Status = PciIoDevice->PciIo.Attributes (\r
1485 &(PciIoDevice->PciIo),\r
1486 EfiPciIoAttributeOperationEnable,\r
1487 Attributes,\r
1488 NULL\r
1489 );\r
1490 if (EFI_ERROR (Status)) {\r
1491 return EFI_UNSUPPORTED;\r
1492 }\r
1493\r
1494 Status = PciIoDevice->PciIo.Attributes (\r
1495 &(PciIoDevice->PciIo),\r
1496 EfiPciIoAttributeOperationDisable,\r
1497 (~Attributes) & (PciIoDevice->Supports),\r
1498 NULL\r
1499 );\r
1500 if (EFI_ERROR (Status)) {\r
1501 return EFI_UNSUPPORTED;\r
1502 }\r
1503\r
1504 return EFI_SUCCESS;\r
1505\r
1506 case EfiPciIoAttributeOperationEnable:\r
1507 case EfiPciIoAttributeOperationDisable:\r
1508 break;\r
1509\r
1510 default:\r
1511 return EFI_INVALID_PARAMETER;\r
1512 }\r
1513 //\r
1514 // Just a trick for ENABLE attribute\r
1515 // EFI_PCI_DEVICE_ENABLE is not defined in UEFI spec, which is the internal usage.\r
1516 // So, this logic doesn't confrom to UEFI spec, which should be removed.\r
1517 // But this trick logic is still kept for some binary drivers that depend on it.\r
1518 //\r
1519 if ((Attributes & EFI_PCI_DEVICE_ENABLE) == EFI_PCI_DEVICE_ENABLE) {\r
1520 Attributes &= (PciIoDevice->Supports);\r
1521\r
1522 //\r
1523 // Raise the EFI_P_PC_ENABLE Status code\r
1524 //\r
1525 REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
1526 EFI_PROGRESS_CODE,\r
1527 EFI_IO_BUS_PCI | EFI_P_PC_ENABLE,\r
1528 PciIoDevice->DevicePath\r
1529 );\r
1530 }\r
1531\r
e0ee9d93 1532 //\r
1533 // Check VGA and VGA16, they can not be set at the same time\r
1534 //\r
1535 if (((Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_IO) != 0 &&\r
1536 (Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_IO_16) != 0) ||\r
1537 ((Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_IO) != 0 &&\r
1538 (Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16) != 0) ||\r
1539 ((Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO) != 0 &&\r
1540 (Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_IO_16) != 0) ||\r
1541 ((Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO) != 0 &&\r
1542 (Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16) != 0) ) {\r
1543 return EFI_UNSUPPORTED;\r
1544 }\r
1545\r
1546 //\r
1547 // workaround for PCI drivers which always set ISA_IO or VGA_IO attribute without detecting support of\r
1548 // ISA_IO/ISA_IO_16 or VGA_IO/VGA_IO_16 to maintain backward-compatibility.\r
1549 //\r
1550 if (((Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_IO) != 0) && \r
1551 ((PciIoDevice->Supports & (EFI_PCI_IO_ATTRIBUTE_VGA_IO | EFI_PCI_IO_ATTRIBUTE_VGA_IO_16)) \\r
1552 == EFI_PCI_IO_ATTRIBUTE_VGA_IO_16)) {\r
1553 Attributes &= ~(UINT64)EFI_PCI_IO_ATTRIBUTE_VGA_IO;\r
1554 Attributes |= EFI_PCI_IO_ATTRIBUTE_VGA_IO_16;\r
1555 }\r
1556\r
1557 if (((Attributes & EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO) != 0) && \r
1558 ((PciIoDevice->Supports & (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO | EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16)) \\r
1559 == EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16)) {\r
1560 Attributes &= ~(UINT64)EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO;\r
1561 Attributes |= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16;\r
1562 }\r
1563\r
9060e3ec 1564 //\r
1565 // If no attributes can be supported, then return.\r
1566 // Otherwise, set the attributes that it can support.\r
1567 //\r
1568 Supports = (PciIoDevice->Supports) & Attributes;\r
1569 if (Supports != Attributes) {\r
1570 return EFI_UNSUPPORTED;\r
1571 }\r
1572\r
1573 //\r
1574 // For Root Bridge, just call RootBridgeIo to set attributes;\r
1575 //\r
1576 if (PciIoDevice->Parent == NULL) {\r
1577 Status = ModifyRootBridgeAttributes (PciIoDevice, Attributes, Operation);\r
1578 return Status;\r
1579 }\r
1580\r
1581 Command = 0;\r
1582 BridgeControl = 0;\r
1583\r
9060e3ec 1584 //\r
1585 // For PPB & P2C, set relevant attribute bits\r
1586 //\r
1587 if (IS_PCI_BRIDGE (&PciIoDevice->Pci) || IS_CARDBUS_BRIDGE (&PciIoDevice->Pci)) {\r
1588\r
1589 if ((Attributes & (EFI_PCI_IO_ATTRIBUTE_VGA_IO | EFI_PCI_IO_ATTRIBUTE_VGA_IO_16)) != 0) {\r
1590 BridgeControl |= EFI_PCI_BRIDGE_CONTROL_VGA;\r
1591 }\r
1592\r
1593 if ((Attributes & EFI_PCI_IO_ATTRIBUTE_ISA_IO) != 0) {\r
1594 BridgeControl |= EFI_PCI_BRIDGE_CONTROL_ISA;\r
1595 }\r
1596\r
1597 if ((Attributes & (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO | EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16)) != 0) {\r
1598 Command |= EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO;\r
1599 }\r
1600\r
1601 if ((Attributes & (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 | EFI_PCI_IO_ATTRIBUTE_VGA_IO_16)) != 0) {\r
1602 BridgeControl |= EFI_PCI_BRIDGE_CONTROL_VGA_16;\r
1603 }\r
1604\r
1605 } else {\r
1606 //\r
1607 // Do with the attributes on VGA\r
1608 // Only for VGA's legacy resource, we just can enable once.\r
1609 //\r
1610 if ((Attributes &\r
1611 (EFI_PCI_IO_ATTRIBUTE_VGA_IO |\r
1612 EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 |\r
1613 EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY)) != 0) {\r
1614 //\r
1615 // Check if a VGA has been enabled before enabling a new one\r
1616 //\r
1617 if (Operation == EfiPciIoAttributeOperationEnable) {\r
1618 //\r
1619 // Check if there have been an active VGA device on the same segment\r
1620 //\r
1621 Temp = ActiveVGADeviceOnTheSameSegment (PciIoDevice);\r
1622 if (Temp != NULL && Temp != PciIoDevice) {\r
1623 //\r
1624 // An active VGA has been detected, so can not enable another\r
1625 //\r
1626 return EFI_UNSUPPORTED;\r
1627 }\r
1628 }\r
1629 }\r
1630\r
1631 //\r
1632 // Do with the attributes on GFX\r
1633 //\r
1634 if ((Attributes & (EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO | EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16)) != 0) {\r
1635\r
1636 if (Operation == EfiPciIoAttributeOperationEnable) {\r
1637 //\r
1638 // Check if snoop can be enabled in current configuration\r
1639 //\r
1640 Status = SupportPaletteSnoopAttributes (PciIoDevice, Operation);\r
1641\r
1642 if (EFI_ERROR (Status)) {\r
1643\r
1644 //\r
1645 // Enable operation is forbidden, so mask the bit in attributes\r
1646 // so as to keep consistent with the actual Status\r
1647 //\r
1648 // Attributes &= (~EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO);\r
1649 //\r
1650 //\r
1651 //\r
1652 return EFI_UNSUPPORTED;\r
1653\r
1654 }\r
1655 }\r
1656\r
1657 //\r
1658 // It can be supported, so get ready to set the bit\r
1659 //\r
1660 Command |= EFI_PCI_COMMAND_VGA_PALETTE_SNOOP;\r
1661 }\r
1662 }\r
1663\r
1664 if ((Attributes & EFI_PCI_IO_ATTRIBUTE_IO) != 0) {\r
1665 Command |= EFI_PCI_COMMAND_IO_SPACE;\r
1666 }\r
1667\r
1668 if ((Attributes & EFI_PCI_IO_ATTRIBUTE_MEMORY) != 0) {\r
1669 Command |= EFI_PCI_COMMAND_MEMORY_SPACE;\r
1670 }\r
1671\r
1672 if ((Attributes & EFI_PCI_IO_ATTRIBUTE_BUS_MASTER) != 0) {\r
1673 Command |= EFI_PCI_COMMAND_BUS_MASTER;\r
1674 }\r
1675 //\r
1676 // The upstream bridge should be also set to revelant attribute\r
1677 // expect for IO, Mem and BusMaster\r
1678 //\r
1679 UpStreamAttributes = Attributes &\r
1680 (~(EFI_PCI_IO_ATTRIBUTE_IO |\r
1681 EFI_PCI_IO_ATTRIBUTE_MEMORY |\r
1682 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER\r
1683 )\r
1684 );\r
1685 UpStreamBridge = PciIoDevice->Parent;\r
1686\r
1687 if (Operation == EfiPciIoAttributeOperationEnable) {\r
1688 //\r
1689 // Enable relevant attributes to command register and bridge control register\r
1690 //\r
1691 Status = PCI_ENABLE_COMMAND_REGISTER (PciIoDevice, Command);\r
1692 if (BridgeControl != 0) {\r
1693 Status = PCI_ENABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice, BridgeControl);\r
1694 }\r
1695\r
1696 PciIoDevice->Attributes |= Attributes;\r
1697\r
1698 //\r
1699 // Enable attributes of the upstream bridge\r
1700 //\r
1701 Status = UpStreamBridge->PciIo.Attributes (\r
1702 &(UpStreamBridge->PciIo),\r
1703 EfiPciIoAttributeOperationEnable,\r
1704 UpStreamAttributes,\r
1705 NULL\r
1706 );\r
1707 } else {\r
1708\r
1709 //\r
1710 // Disable relevant attributes to command register and bridge control register\r
1711 //\r
1712 Status = PCI_DISABLE_COMMAND_REGISTER (PciIoDevice, Command);\r
1713 if (BridgeControl != 0) {\r
1714 Status = PCI_DISABLE_BRIDGE_CONTROL_REGISTER (PciIoDevice, BridgeControl);\r
1715 }\r
1716\r
1717 PciIoDevice->Attributes &= (~Attributes);\r
1718 Status = EFI_SUCCESS;\r
1719\r
1720 }\r
1721\r
1722 if (EFI_ERROR (Status)) {\r
1723 ReportErrorStatusCode (PciIoDevice, EFI_IO_BUS_PCI | EFI_IOB_EC_CONTROLLER_ERROR);\r
1724 }\r
1725\r
1726 return Status;\r
1727}\r
1728\r
1729/**\r
1730 Gets the attributes that this PCI controller supports setting on a BAR using\r
1731 SetBarAttributes(), and retrieves the list of resource descriptors for a BAR.\r
1732\r
1733 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1734 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
1735 base address for resource range. The legal range for this field is 0..5.\r
1736 @param Supports A pointer to the mask of attributes that this PCI controller supports\r
1737 setting for this BAR with SetBarAttributes().\r
1738 @param Resources A pointer to the ACPI 2.0 resource descriptors that describe the current\r
1739 configuration of this BAR of the PCI controller.\r
1740\r
1741 @retval EFI_SUCCESS If Supports is not NULL, then the attributes that the PCI\r
1742 controller supports are returned in Supports. If Resources\r
1743 is not NULL, then the ACPI 2.0 resource descriptors that the PCI\r
1744 controller is currently using are returned in Resources.\r
1745 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.\r
1746 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
1747 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to allocate\r
1748 Resources.\r
1749\r
1750**/\r
1751EFI_STATUS\r
1752EFIAPI\r
1753PciIoGetBarAttributes (\r
1754 IN EFI_PCI_IO_PROTOCOL * This,\r
1755 IN UINT8 BarIndex,\r
1756 OUT UINT64 *Supports, OPTIONAL\r
1757 OUT VOID **Resources OPTIONAL\r
1758 )\r
1759{\r
1760\r
1761 UINT8 *Configuration;\r
1762 UINT8 NumConfig;\r
1763 PCI_IO_DEVICE *PciIoDevice;\r
1764 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Ptr;\r
1765 EFI_ACPI_END_TAG_DESCRIPTOR *PtrEnd;\r
1766\r
1767 NumConfig = 0;\r
1768\r
1769 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1770\r
1771 if (Supports == NULL && Resources == NULL) {\r
1772 return EFI_INVALID_PARAMETER;\r
1773 }\r
1774\r
1775 if (BarIndex >= PCI_MAX_BAR) {\r
1776 return EFI_UNSUPPORTED;\r
1777 }\r
1778\r
1779 //\r
1780 // This driver does not support modifications to the WRITE_COMBINE or\r
1781 // CACHED attributes for BAR ranges.\r
1782 //\r
1783 if (Supports != NULL) {\r
1784 *Supports = PciIoDevice->Supports & EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED & EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE;\r
1785 }\r
1786\r
1787 if (Resources != NULL) {\r
1788\r
1789 if (PciIoDevice->PciBar[BarIndex].BarType != PciBarTypeUnknown) {\r
1790 NumConfig = 1;\r
1791 }\r
1792\r
1793 Configuration = AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) * NumConfig + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR));\r
1794 if (Configuration == NULL) {\r
1795 return EFI_OUT_OF_RESOURCES;\r
1796 }\r
1797\r
1798 Ptr = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) Configuration;\r
1799\r
1800 if (NumConfig == 1) {\r
1801 Ptr->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;\r
c9325700 1802 Ptr->Len = (UINT16) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3);\r
9060e3ec 1803\r
1804 Ptr->AddrRangeMin = PciIoDevice->PciBar[BarIndex].BaseAddress;\r
1805 Ptr->AddrLen = PciIoDevice->PciBar[BarIndex].Length;\r
1806 Ptr->AddrRangeMax = PciIoDevice->PciBar[BarIndex].Alignment;\r
1807\r
1808 switch (PciIoDevice->PciBar[BarIndex].BarType) {\r
1809 case PciBarTypeIo16:\r
1810 case PciBarTypeIo32:\r
1811 //\r
1812 // Io\r
1813 //\r
1814 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;\r
1815 break;\r
1816\r
1817 case PciBarTypeMem32:\r
1818 //\r
1819 // Mem\r
1820 //\r
1821 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1822 //\r
1823 // 32 bit\r
1824 //\r
1825 Ptr->AddrSpaceGranularity = 32;\r
1826 break;\r
1827\r
1828 case PciBarTypePMem32:\r
1829 //\r
1830 // Mem\r
1831 //\r
1832 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1833 //\r
1834 // prefechable\r
1835 //\r
1836 Ptr->SpecificFlag = 0x6;\r
1837 //\r
1838 // 32 bit\r
1839 //\r
1840 Ptr->AddrSpaceGranularity = 32;\r
1841 break;\r
1842\r
1843 case PciBarTypeMem64:\r
1844 //\r
1845 // Mem\r
1846 //\r
1847 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1848 //\r
1849 // 64 bit\r
1850 //\r
1851 Ptr->AddrSpaceGranularity = 64;\r
1852 break;\r
1853\r
1854 case PciBarTypePMem64:\r
1855 //\r
1856 // Mem\r
1857 //\r
1858 Ptr->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;\r
1859 //\r
1860 // prefechable\r
1861 //\r
1862 Ptr->SpecificFlag = 0x6;\r
1863 //\r
1864 // 64 bit\r
1865 //\r
1866 Ptr->AddrSpaceGranularity = 64;\r
1867 break;\r
1868\r
1869 default:\r
1870 break;\r
1871 }\r
1872\r
1873 Ptr = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *) ((UINT8 *) Ptr + sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR));\r
1874 }\r
1875\r
1876 //\r
1877 // put the checksum\r
1878 //\r
1879 PtrEnd = (EFI_ACPI_END_TAG_DESCRIPTOR *) ((UINT8 *) Ptr);\r
1880 PtrEnd->Desc = ACPI_END_TAG_DESCRIPTOR;\r
1881 PtrEnd->Checksum = 0;\r
1882\r
1883 *Resources = Configuration;\r
1884 }\r
1885\r
1886 return EFI_SUCCESS;\r
1887}\r
1888\r
1889/**\r
1890 Sets the attributes for a range of a BAR on a PCI controller.\r
1891\r
1892 @param This A pointer to the EFI_PCI_IO_PROTOCOL instance.\r
1893 @param Attributes The mask of attributes to set for the resource range specified by\r
1894 BarIndex, Offset, and Length.\r
1895 @param BarIndex The BAR index of the standard PCI Configuration header to use as the\r
1896 base address for resource range. The legal range for this field is 0..5.\r
1897 @param Offset A pointer to the BAR relative base address of the resource range to be\r
1898 modified by the attributes specified by Attributes.\r
1899 @param Length A pointer to the length of the resource range to be modified by the\r
1900 attributes specified by Attributes.\r
1901\r
1902 @retval EFI_SUCCESS The set of attributes specified by Attributes for the resource\r
1903 range specified by BarIndex, Offset, and Length were\r
1904 set on the PCI controller, and the actual resource range is returned\r
1905 in Offset and Length.\r
1906 @retval EFI_INVALID_PARAMETER Offset or Length is NULL.\r
1907 @retval EFI_UNSUPPORTED BarIndex not valid for this PCI controller.\r
1908 @retval EFI_OUT_OF_RESOURCES There are not enough resources to set the attributes on the\r
1909 resource range specified by BarIndex, Offset, and\r
1910 Length.\r
1911\r
1912**/\r
1913EFI_STATUS\r
1914EFIAPI\r
1915PciIoSetBarAttributes (\r
1916 IN EFI_PCI_IO_PROTOCOL *This,\r
1917 IN UINT64 Attributes,\r
1918 IN UINT8 BarIndex,\r
1919 IN OUT UINT64 *Offset,\r
1920 IN OUT UINT64 *Length\r
1921 )\r
1922{\r
1923 EFI_STATUS Status;\r
1924 PCI_IO_DEVICE *PciIoDevice;\r
1925 UINT64 NonRelativeOffset;\r
1926 UINT64 Supports;\r
1927\r
1928 PciIoDevice = PCI_IO_DEVICE_FROM_PCI_IO_THIS (This);\r
1929\r
1930 //\r
1931 // Make sure Offset and Length are not NULL\r
1932 //\r
1933 if (Offset == NULL || Length == NULL) {\r
1934 return EFI_INVALID_PARAMETER;\r
1935 }\r
1936\r
1937 if (PciIoDevice->PciBar[BarIndex].BarType == PciBarTypeUnknown) {\r
1938 return EFI_UNSUPPORTED;\r
1939 }\r
1940 //\r
1941 // This driver does not support setting the WRITE_COMBINE or the CACHED attributes.\r
1942 // If Attributes is not 0, then return EFI_UNSUPPORTED.\r
1943 //\r
1944 Supports = PciIoDevice->Supports & EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED & EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE;\r
1945\r
1946 if (Attributes != (Attributes & Supports)) {\r
1947 return EFI_UNSUPPORTED;\r
1948 }\r
1949 //\r
1950 // Attributes must be supported. Make sure the BAR range describd by BarIndex, Offset, and\r
1951 // Length are valid for this PCI device.\r
1952 //\r
1953 NonRelativeOffset = *Offset;\r
1954 Status = PciIoVerifyBarAccess (\r
1955 PciIoDevice,\r
1956 BarIndex,\r
1957 PciBarTypeMem,\r
1958 EfiPciIoWidthUint8,\r
1959 (UINT32) *Length,\r
1960 &NonRelativeOffset\r
1961 );\r
1962 if (EFI_ERROR (Status)) {\r
1963 return EFI_UNSUPPORTED;\r
1964 }\r
1965\r
1966 return EFI_SUCCESS;\r
1967}\r
1968\r
1969/**\r
1970 Program parent bridge's attribute recurrently.\r
1971\r
1972 @param PciIoDevice Child Pci device instance\r
1973 @param Operation The operation to perform on the attributes for this PCI controller.\r
1974 @param Attributes The mask of attributes that are used for Set, Enable, and Disable\r
1975 operations.\r
1976\r
1977 @retval EFI_SUCCESS The operation on the PCI controller's attributes was completed.\r
1978 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
1979 @retval EFI_UNSUPPORTED one or more of the bits set in\r
1980 Attributes are not supported by this PCI controller or one of\r
1981 its parent bridges when Operation is Set, Enable or Disable.\r
1982\r
1983**/\r
1984EFI_STATUS\r
1985UpStreamBridgesAttributes (\r
1986 IN PCI_IO_DEVICE *PciIoDevice,\r
1987 IN EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION Operation,\r
1988 IN UINT64 Attributes\r
1989 )\r
1990{\r
1991 PCI_IO_DEVICE *Parent;\r
1992 EFI_PCI_IO_PROTOCOL *PciIo;\r
1993\r
1994 Parent = PciIoDevice->Parent;\r
1995\r
1996 while (Parent != NULL && IS_PCI_BRIDGE (&Parent->Pci)) {\r
1997\r
1998 //\r
1999 // Get the PciIo Protocol\r
2000 //\r
2001 PciIo = &Parent->PciIo;\r
2002\r
2003 PciIo->Attributes (PciIo, Operation, Attributes, NULL);\r
2004\r
2005 Parent = Parent->Parent;\r
2006 }\r
2007\r
2008 return EFI_SUCCESS;\r
2009}\r
2010\r
2011/**\r
2012 Test whether two Pci devices has same parent bridge.\r
2013\r
2014 @param PciDevice1 The first pci device for testing.\r
2015 @param PciDevice2 The second pci device for testing.\r
2016\r
2017 @retval TRUE Two Pci device has the same parent bridge.\r
2018 @retval FALSE Two Pci device has not the same parent bridge.\r
2019\r
2020**/\r
2021BOOLEAN\r
2022PciDevicesOnTheSamePath (\r
2023 IN PCI_IO_DEVICE *PciDevice1,\r
2024 IN PCI_IO_DEVICE *PciDevice2\r
2025 )\r
2026{\r
2027 BOOLEAN Existed1;\r
2028 BOOLEAN Existed2;\r
2029\r
2030 if (PciDevice1->Parent == PciDevice2->Parent) {\r
2031 return TRUE;\r
2032 }\r
2033\r
2034 Existed1 = PciDeviceExisted (PciDevice1->Parent, PciDevice2);\r
2035 Existed2 = PciDeviceExisted (PciDevice2->Parent, PciDevice1);\r
2036\r
2037 return (BOOLEAN) (Existed1 || Existed2);\r
2038}\r
2039\r