]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/ArmVirtualizationPkg/PciHostBridgeDxe/PciRootBridgeIo.c
ArmVirtualizationPkg/PciHostBridgeDxe: clone from PcAtChipsetPkg
[mirror_edk2.git] / ArmPlatformPkg / ArmVirtualizationPkg / PciHostBridgeDxe / PciRootBridgeIo.c
1 /** @file
2 PCI Root Bridge Io Protocol implementation
3
4 Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are
6 licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PciHostBridge.h"
16
17 typedef struct {
18 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR SpaceDesp[TypeMax];
19 EFI_ACPI_END_TAG_DESCRIPTOR EndDesp;
20 } RESOURCE_CONFIGURATION;
21
22 RESOURCE_CONFIGURATION Configuration = {
23 {{0x8A, 0x2B, 1, 0, 0, 0, 0, 0, 0, 0},
24 {0x8A, 0x2B, 0, 0, 0, 32, 0, 0, 0, 0},
25 {0x8A, 0x2B, 0, 0, 6, 32, 0, 0, 0, 0},
26 {0x8A, 0x2B, 0, 0, 0, 64, 0, 0, 0, 0},
27 {0x8A, 0x2B, 0, 0, 6, 64, 0, 0, 0, 0},
28 {0x8A, 0x2B, 2, 0, 0, 0, 0, 0, 0, 0}},
29 {0x79, 0}
30 };
31
32 //
33 // Protocol Member Function Prototypes
34 //
35
36 /**
37 Polls an address in memory mapped I/O space until an exit condition is met, or
38 a timeout occurs.
39
40 This function provides a standard way to poll a PCI memory location. A PCI memory read
41 operation is performed at the PCI memory address specified by Address for the width specified
42 by Width. The result of this PCI memory read operation is stored in Result. This PCI memory
43 read operation is repeated until either a timeout of Delay 100 ns units has expired, or (Result &
44 Mask) is equal to Value.
45
46 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
47 @param[in] Width Signifies the width of the memory operations.
48 @param[in] Address The base address of the memory operations. The caller is
49 responsible for aligning Address if required.
50 @param[in] Mask Mask used for the polling criteria. Bytes above Width in Mask
51 are ignored. The bits in the bytes below Width which are zero in
52 Mask are ignored when polling the memory address.
53 @param[in] Value The comparison value used for the polling exit criteria.
54 @param[in] Delay The number of 100 ns units to poll. Note that timer available may
55 be of poorer granularity.
56 @param[out] Result Pointer to the last value read from the memory location.
57
58 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
59 @retval EFI_INVALID_PARAMETER Width is invalid.
60 @retval EFI_INVALID_PARAMETER Result is NULL.
61 @retval EFI_TIMEOUT Delay expired before a match occurred.
62 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
63
64 **/
65 EFI_STATUS
66 EFIAPI
67 RootBridgeIoPollMem (
68 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
69 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
70 IN UINT64 Address,
71 IN UINT64 Mask,
72 IN UINT64 Value,
73 IN UINT64 Delay,
74 OUT UINT64 *Result
75 );
76
77 /**
78 Reads from the I/O space of a PCI Root Bridge. Returns when either the polling exit criteria is
79 satisfied or after a defined duration.
80
81 This function provides a standard way to poll a PCI I/O location. A PCI I/O read operation is
82 performed at the PCI I/O address specified by Address for the width specified by Width.
83 The result of this PCI I/O read operation is stored in Result. This PCI I/O read operation is
84 repeated until either a timeout of Delay 100 ns units has expired, or (Result & Mask) is equal
85 to Value.
86
87 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
88 @param[in] Width Signifies the width of the I/O operations.
89 @param[in] Address The base address of the I/O operations. The caller is responsible
90 for aligning Address if required.
91 @param[in] Mask Mask used for the polling criteria. Bytes above Width in Mask
92 are ignored. The bits in the bytes below Width which are zero in
93 Mask are ignored when polling the I/O address.
94 @param[in] Value The comparison value used for the polling exit criteria.
95 @param[in] Delay The number of 100 ns units to poll. Note that timer available may
96 be of poorer granularity.
97 @param[out] Result Pointer to the last value read from the memory location.
98
99 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
100 @retval EFI_INVALID_PARAMETER Width is invalid.
101 @retval EFI_INVALID_PARAMETER Result is NULL.
102 @retval EFI_TIMEOUT Delay expired before a match occurred.
103 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
104
105 **/
106 EFI_STATUS
107 EFIAPI
108 RootBridgeIoPollIo (
109 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
110 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
111 IN UINT64 Address,
112 IN UINT64 Mask,
113 IN UINT64 Value,
114 IN UINT64 Delay,
115 OUT UINT64 *Result
116 );
117
118 /**
119 Enables a PCI driver to access PCI controller registers in the PCI root bridge memory space.
120
121 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI controller
122 registers in the PCI root bridge memory space.
123 The memory operations are carried out exactly as requested. The caller is responsible for satisfying
124 any alignment and memory width restrictions that a PCI Root Bridge on a platform might require.
125
126 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
127 @param[in] Width Signifies the width of the memory operation.
128 @param[in] Address The base address of the memory operation. The caller is
129 responsible for aligning the Address if required.
130 @param[in] Count The number of memory operations to perform. Bytes moved is
131 Width size * Count, starting at Address.
132 @param[out] Buffer For read operations, the destination buffer to store the results. For
133 write operations, the source buffer to write data from.
134
135 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
136 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
137 @retval EFI_INVALID_PARAMETER Buffer is NULL.
138 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
139
140 **/
141 EFI_STATUS
142 EFIAPI
143 RootBridgeIoMemRead (
144 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
145 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
146 IN UINT64 Address,
147 IN UINTN Count,
148 OUT VOID *Buffer
149 );
150
151 /**
152 Enables a PCI driver to access PCI controller registers in the PCI root bridge memory space.
153
154 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI controller
155 registers in the PCI root bridge memory space.
156 The memory operations are carried out exactly as requested. The caller is responsible for satisfying
157 any alignment and memory width restrictions that a PCI Root Bridge on a platform might require.
158
159 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
160 @param[in] Width Signifies the width of the memory operation.
161 @param[in] Address The base address of the memory operation. The caller is
162 responsible for aligning the Address if required.
163 @param[in] Count The number of memory operations to perform. Bytes moved is
164 Width size * Count, starting at Address.
165 @param[in] Buffer For read operations, the destination buffer to store the results. For
166 write operations, the source buffer to write data from.
167
168 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
169 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
170 @retval EFI_INVALID_PARAMETER Buffer is NULL.
171 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
172 **/
173 EFI_STATUS
174 EFIAPI
175 RootBridgeIoMemWrite (
176 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
177 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
178 IN UINT64 Address,
179 IN UINTN Count,
180 IN VOID *Buffer
181 );
182
183 /**
184 Enables a PCI driver to access PCI controller registers in the PCI root bridge I/O space.
185
186 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
187 @param[in] Width Signifies the width of the memory operations.
188 @param[in] UserAddress The base address of the I/O operation. The caller is responsible for
189 aligning the Address if required.
190 @param[in] Count The number of I/O operations to perform. Bytes moved is Width
191 size * Count, starting at Address.
192 @param[out] UserBuffer For read operations, the destination buffer to store the results. For
193 write operations, the source buffer to write data from.
194
195 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
196 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
197 @retval EFI_INVALID_PARAMETER Buffer is NULL.
198 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
199
200 **/
201 EFI_STATUS
202 EFIAPI
203 RootBridgeIoIoRead (
204 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
205 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
206 IN UINT64 UserAddress,
207 IN UINTN Count,
208 OUT VOID *UserBuffer
209 );
210
211 /**
212 Enables a PCI driver to access PCI controller registers in the PCI root bridge I/O space.
213
214 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
215 @param[in] Width Signifies the width of the memory operations.
216 @param[in] UserAddress The base address of the I/O operation. The caller is responsible for
217 aligning the Address if required.
218 @param[in] Count The number of I/O operations to perform. Bytes moved is Width
219 size * Count, starting at Address.
220 @param[in] UserBuffer For read operations, the destination buffer to store the results. For
221 write operations, the source buffer to write data from.
222
223 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
224 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
225 @retval EFI_INVALID_PARAMETER Buffer is NULL.
226 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
227
228 **/
229 EFI_STATUS
230 EFIAPI
231 RootBridgeIoIoWrite (
232 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
233 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
234 IN UINT64 UserAddress,
235 IN UINTN Count,
236 IN VOID *UserBuffer
237 );
238
239 /**
240 Enables a PCI driver to copy one region of PCI root bridge memory space to another region of PCI
241 root bridge memory space.
242
243 The CopyMem() function enables a PCI driver to copy one region of PCI root bridge memory
244 space to another region of PCI root bridge memory space. This is especially useful for video scroll
245 operation on a memory mapped video buffer.
246 The memory operations are carried out exactly as requested. The caller is responsible for satisfying
247 any alignment and memory width restrictions that a PCI root bridge on a platform might require.
248
249 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL instance.
250 @param[in] Width Signifies the width of the memory operations.
251 @param[in] DestAddress The destination address of the memory operation. The caller is
252 responsible for aligning the DestAddress if required.
253 @param[in] SrcAddress The source address of the memory operation. The caller is
254 responsible for aligning the SrcAddress if required.
255 @param[in] Count The number of memory operations to perform. Bytes moved is
256 Width size * Count, starting at DestAddress and SrcAddress.
257
258 @retval EFI_SUCCESS The data was copied from one memory region to another memory region.
259 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
260 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
261
262 **/
263 EFI_STATUS
264 EFIAPI
265 RootBridgeIoCopyMem (
266 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
267 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
268 IN UINT64 DestAddress,
269 IN UINT64 SrcAddress,
270 IN UINTN Count
271 );
272
273 /**
274 Enables a PCI driver to access PCI controller registers in a PCI root bridge's configuration space.
275
276 The Pci.Read() and Pci.Write() functions enable a driver to access PCI configuration
277 registers for a PCI controller.
278 The PCI Configuration operations are carried out exactly as requested. The caller is responsible for
279 any alignment and PCI configuration width issues that a PCI Root Bridge on a platform might
280 require.
281
282 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
283 @param[in] Width Signifies the width of the memory operations.
284 @param[in] Address The address within the PCI configuration space for the PCI controller.
285 @param[in] Count The number of PCI configuration operations to perform. Bytes
286 moved is Width size * Count, starting at Address.
287 @param[out] Buffer For read operations, the destination buffer to store the results. For
288 write operations, the source buffer to write data from.
289
290 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
291 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
292 @retval EFI_INVALID_PARAMETER Buffer is NULL.
293 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
294
295 **/
296 EFI_STATUS
297 EFIAPI
298 RootBridgeIoPciRead (
299 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
300 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
301 IN UINT64 Address,
302 IN UINTN Count,
303 OUT VOID *Buffer
304 );
305
306 /**
307 Enables a PCI driver to access PCI controller registers in a PCI root bridge's configuration space.
308
309 The Pci.Read() and Pci.Write() functions enable a driver to access PCI configuration
310 registers for a PCI controller.
311 The PCI Configuration operations are carried out exactly as requested. The caller is responsible for
312 any alignment and PCI configuration width issues that a PCI Root Bridge on a platform might
313 require.
314
315 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
316 @param[in] Width Signifies the width of the memory operations.
317 @param[in] Address The address within the PCI configuration space for the PCI controller.
318 @param[in] Count The number of PCI configuration operations to perform. Bytes
319 moved is Width size * Count, starting at Address.
320 @param[in] Buffer For read operations, the destination buffer to store the results. For
321 write operations, the source buffer to write data from.
322
323 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
324 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
325 @retval EFI_INVALID_PARAMETER Buffer is NULL.
326 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
327
328 **/
329 EFI_STATUS
330 EFIAPI
331 RootBridgeIoPciWrite (
332 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
333 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
334 IN UINT64 Address,
335 IN UINTN Count,
336 IN VOID *Buffer
337 );
338
339 /**
340 Provides the PCI controller-specific addresses required to access system memory from a
341 DMA bus master.
342
343 The Map() function provides the PCI controller specific addresses needed to access system
344 memory. This function is used to map system memory for PCI bus master DMA accesses.
345
346 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
347 @param[in] Operation Indicates if the bus master is going to read or write to system memory.
348 @param[in] HostAddress The system memory address to map to the PCI controller.
349 @param[in, out] NumberOfBytes On input the number of bytes to map. On output the number of bytes that were mapped.
350 @param[out] DeviceAddress The resulting map address for the bus master PCI controller to use
351 to access the system memory's HostAddress.
352 @param[out] Mapping The value to pass to Unmap() when the bus master DMA operation is complete.
353
354 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
355 @retval EFI_INVALID_PARAMETER Operation is invalid.
356 @retval EFI_INVALID_PARAMETER HostAddress is NULL.
357 @retval EFI_INVALID_PARAMETER NumberOfBytes is NULL.
358 @retval EFI_INVALID_PARAMETER DeviceAddress is NULL.
359 @retval EFI_INVALID_PARAMETER Mapping is NULL.
360 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
361 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
362 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
363
364 **/
365 EFI_STATUS
366 EFIAPI
367 RootBridgeIoMap (
368 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
369 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION Operation,
370 IN VOID *HostAddress,
371 IN OUT UINTN *NumberOfBytes,
372 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
373 OUT VOID **Mapping
374 );
375
376 /**
377 Completes the Map() operation and releases any corresponding resources.
378
379 The Unmap() function completes the Map() operation and releases any corresponding resources.
380 If the operation was an EfiPciOperationBusMasterWrite or
381 EfiPciOperationBusMasterWrite64, the data is committed to the target system memory.
382 Any resources used for the mapping are freed.
383
384 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
385 @param[in] Mapping The mapping value returned from Map().
386
387 @retval EFI_SUCCESS The range was unmapped.
388 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
389 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
390
391 **/
392 EFI_STATUS
393 EFIAPI
394 RootBridgeIoUnmap (
395 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
396 IN VOID *Mapping
397 );
398
399 /**
400 Allocates pages that are suitable for an EfiPciOperationBusMasterCommonBuffer or
401 EfiPciOperationBusMasterCommonBuffer64 mapping.
402
403 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
404 @param Type This parameter is not used and must be ignored.
405 @param MemoryType The type of memory to allocate, EfiBootServicesData or EfiRuntimeServicesData.
406 @param Pages The number of pages to allocate.
407 @param HostAddress A pointer to store the base system memory address of the allocated range.
408 @param Attributes The requested bit mask of attributes for the allocated range. Only
409 the attributes EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE, EFI_PCI_ATTRIBUTE_MEMORY_CACHED,
410 and EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE may be used with this function.
411
412 @retval EFI_SUCCESS The requested memory pages were allocated.
413 @retval EFI_INVALID_PARAMETER MemoryType is invalid.
414 @retval EFI_INVALID_PARAMETER HostAddress is NULL.
415 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
416 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and DUAL_ADDRESS_CYCLE.
417 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
418
419 **/
420 EFI_STATUS
421 EFIAPI
422 RootBridgeIoAllocateBuffer (
423 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
424 IN EFI_ALLOCATE_TYPE Type,
425 IN EFI_MEMORY_TYPE MemoryType,
426 IN UINTN Pages,
427 OUT VOID **HostAddress,
428 IN UINT64 Attributes
429 );
430
431 /**
432 Frees memory that was allocated with AllocateBuffer().
433
434 The FreeBuffer() function frees memory that was allocated with AllocateBuffer().
435
436 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
437 @param Pages The number of pages to free.
438 @param HostAddress The base system memory address of the allocated range.
439
440 @retval EFI_SUCCESS The requested memory pages were freed.
441 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
442 was not allocated with AllocateBuffer().
443
444 **/
445 EFI_STATUS
446 EFIAPI
447 RootBridgeIoFreeBuffer (
448 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
449 IN UINTN Pages,
450 OUT VOID *HostAddress
451 );
452
453 /**
454 Flushes all PCI posted write transactions from a PCI host bridge to system memory.
455
456 The Flush() function flushes any PCI posted write transactions from a PCI host bridge to system
457 memory. Posted write transactions are generated by PCI bus masters when they perform write
458 transactions to target addresses in system memory.
459 This function does not flush posted write transactions from any PCI bridges. A PCI controller
460 specific action must be taken to guarantee that the posted write transactions have been flushed from
461 the PCI controller and from all the PCI bridges into the PCI host bridge. This is typically done with
462 a PCI read transaction from the PCI controller prior to calling Flush().
463
464 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
465
466 @retval EFI_SUCCESS The PCI posted write transactions were flushed from the PCI host
467 bridge to system memory.
468 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed from the PCI
469 host bridge due to a hardware error.
470
471 **/
472 EFI_STATUS
473 EFIAPI
474 RootBridgeIoFlush (
475 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This
476 );
477
478 /**
479 Gets the attributes that a PCI root bridge supports setting with SetAttributes(), and the
480 attributes that a PCI root bridge is currently using.
481
482 The GetAttributes() function returns the mask of attributes that this PCI root bridge supports
483 and the mask of attributes that the PCI root bridge is currently using.
484
485 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
486 @param Supported A pointer to the mask of attributes that this PCI root bridge
487 supports setting with SetAttributes().
488 @param Attributes A pointer to the mask of attributes that this PCI root bridge is
489 currently using.
490
491 @retval EFI_SUCCESS If Supports is not NULL, then the attributes that the PCI root
492 bridge supports is returned in Supports. If Attributes is
493 not NULL, then the attributes that the PCI root bridge is currently
494 using is returned in Attributes.
495 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.
496
497 **/
498 EFI_STATUS
499 EFIAPI
500 RootBridgeIoGetAttributes (
501 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
502 OUT UINT64 *Supported,
503 OUT UINT64 *Attributes
504 );
505
506 /**
507 Sets attributes for a resource range on a PCI root bridge.
508
509 The SetAttributes() function sets the attributes specified in Attributes for the PCI root
510 bridge on the resource range specified by ResourceBase and ResourceLength. Since the
511 granularity of setting these attributes may vary from resource type to resource type, and from
512 platform to platform, the actual resource range and the one passed in by the caller may differ. As a
513 result, this function may set the attributes specified by Attributes on a larger resource range
514 than the caller requested. The actual range is returned in ResourceBase and
515 ResourceLength. The caller is responsible for verifying that the actual range for which the
516 attributes were set is acceptable.
517
518 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
519 @param[in] Attributes The mask of attributes to set. If the attribute bit
520 MEMORY_WRITE_COMBINE, MEMORY_CACHED, or
521 MEMORY_DISABLE is set, then the resource range is specified by
522 ResourceBase and ResourceLength. If
523 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and
524 MEMORY_DISABLE are not set, then ResourceBase and
525 ResourceLength are ignored, and may be NULL.
526 @param[in, out] ResourceBase A pointer to the base address of the resource range to be modified
527 by the attributes specified by Attributes.
528 @param[in, out] ResourceLength A pointer to the length of the resource range to be modified by the
529 attributes specified by Attributes.
530
531 @retval EFI_SUCCESS The current configuration of this PCI root bridge was returned in Resources.
532 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge could not be retrieved.
533 @retval EFI_INVALID_PARAMETER Invalid pointer of EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
534
535 **/
536 EFI_STATUS
537 EFIAPI
538 RootBridgeIoSetAttributes (
539 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
540 IN UINT64 Attributes,
541 IN OUT UINT64 *ResourceBase,
542 IN OUT UINT64 *ResourceLength
543 );
544
545 /**
546 Retrieves the current resource settings of this PCI root bridge in the form of a set of ACPI 2.0
547 resource descriptors.
548
549 There are only two resource descriptor types from the ACPI Specification that may be used to
550 describe the current resources allocated to a PCI root bridge. These are the QWORD Address
551 Space Descriptor (ACPI 2.0 Section 6.4.3.5.1), and the End Tag (ACPI 2.0 Section 6.4.2.8). The
552 QWORD Address Space Descriptor can describe memory, I/O, and bus number ranges for dynamic
553 or fixed resources. The configuration of a PCI root bridge is described with one or more QWORD
554 Address Space Descriptors followed by an End Tag.
555
556 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
557 @param[out] Resources A pointer to the ACPI 2.0 resource descriptors that describe the
558 current configuration of this PCI root bridge. The storage for the
559 ACPI 2.0 resource descriptors is allocated by this function. The
560 caller must treat the return buffer as read-only data, and the buffer
561 must not be freed by the caller.
562
563 @retval EFI_SUCCESS The current configuration of this PCI root bridge was returned in Resources.
564 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge could not be retrieved.
565 @retval EFI_INVALID_PARAMETER Invalid pointer of EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
566
567 **/
568 EFI_STATUS
569 EFIAPI
570 RootBridgeIoConfiguration (
571 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
572 OUT VOID **Resources
573 );
574
575 //
576 // Memory Controller Pci Root Bridge Io Module Variables
577 //
578 EFI_METRONOME_ARCH_PROTOCOL *mMetronome;
579
580 //
581 // Lookup table for increment values based on transfer widths
582 //
583 UINT8 mInStride[] = {
584 1, // EfiPciWidthUint8
585 2, // EfiPciWidthUint16
586 4, // EfiPciWidthUint32
587 8, // EfiPciWidthUint64
588 0, // EfiPciWidthFifoUint8
589 0, // EfiPciWidthFifoUint16
590 0, // EfiPciWidthFifoUint32
591 0, // EfiPciWidthFifoUint64
592 1, // EfiPciWidthFillUint8
593 2, // EfiPciWidthFillUint16
594 4, // EfiPciWidthFillUint32
595 8 // EfiPciWidthFillUint64
596 };
597
598 //
599 // Lookup table for increment values based on transfer widths
600 //
601 UINT8 mOutStride[] = {
602 1, // EfiPciWidthUint8
603 2, // EfiPciWidthUint16
604 4, // EfiPciWidthUint32
605 8, // EfiPciWidthUint64
606 1, // EfiPciWidthFifoUint8
607 2, // EfiPciWidthFifoUint16
608 4, // EfiPciWidthFifoUint32
609 8, // EfiPciWidthFifoUint64
610 0, // EfiPciWidthFillUint8
611 0, // EfiPciWidthFillUint16
612 0, // EfiPciWidthFillUint32
613 0 // EfiPciWidthFillUint64
614 };
615
616 /**
617
618 Construct the Pci Root Bridge Io protocol
619
620 @param Protocol Point to protocol instance
621 @param HostBridgeHandle Handle of host bridge
622 @param Attri Attribute of host bridge
623 @param ResAperture ResourceAperture for host bridge
624
625 @retval EFI_SUCCESS Success to initialize the Pci Root Bridge.
626
627 **/
628 EFI_STATUS
629 RootBridgeConstructor (
630 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *Protocol,
631 IN EFI_HANDLE HostBridgeHandle,
632 IN UINT64 Attri,
633 IN PCI_ROOT_BRIDGE_RESOURCE_APERTURE *ResAperture
634 )
635 {
636 EFI_STATUS Status;
637 PCI_ROOT_BRIDGE_INSTANCE *PrivateData;
638 PCI_RESOURCE_TYPE Index;
639
640 PrivateData = DRIVER_INSTANCE_FROM_PCI_ROOT_BRIDGE_IO_THIS (Protocol);
641
642 //
643 // The host to pci bridge, the host memory and io addresses are
644 // direct mapped to pci addresses, so no need translate, set bases to 0.
645 //
646 PrivateData->MemBase = ResAperture->MemBase;
647 PrivateData->IoBase = ResAperture->IoBase;
648
649 //
650 // The host bridge only supports 32bit addressing for memory
651 // and standard IA32 16bit io
652 //
653 PrivateData->MemLimit = ResAperture->MemLimit;
654 PrivateData->IoLimit = ResAperture->IoLimit;
655
656 //
657 // Bus Aperture for this Root Bridge (Possible Range)
658 //
659 PrivateData->BusBase = ResAperture->BusBase;
660 PrivateData->BusLimit = ResAperture->BusLimit;
661
662 //
663 // Specific for this chipset
664 //
665 for (Index = TypeIo; Index < TypeMax; Index++) {
666 PrivateData->ResAllocNode[Index].Type = Index;
667 PrivateData->ResAllocNode[Index].Base = 0;
668 PrivateData->ResAllocNode[Index].Length = 0;
669 PrivateData->ResAllocNode[Index].Status = ResNone;
670 }
671
672 PrivateData->RootBridgeAttrib = Attri;
673
674 PrivateData->Supports = EFI_PCI_ATTRIBUTE_IDE_PRIMARY_IO | EFI_PCI_ATTRIBUTE_IDE_SECONDARY_IO | \
675 EFI_PCI_ATTRIBUTE_ISA_IO_16 | EFI_PCI_ATTRIBUTE_ISA_MOTHERBOARD_IO | \
676 EFI_PCI_ATTRIBUTE_VGA_MEMORY | \
677 EFI_PCI_ATTRIBUTE_VGA_IO_16 | EFI_PCI_ATTRIBUTE_VGA_PALETTE_IO_16;
678 PrivateData->Attributes = PrivateData->Supports;
679
680 Protocol->ParentHandle = HostBridgeHandle;
681
682 Protocol->PollMem = RootBridgeIoPollMem;
683 Protocol->PollIo = RootBridgeIoPollIo;
684
685 Protocol->Mem.Read = RootBridgeIoMemRead;
686 Protocol->Mem.Write = RootBridgeIoMemWrite;
687
688 Protocol->Io.Read = RootBridgeIoIoRead;
689 Protocol->Io.Write = RootBridgeIoIoWrite;
690
691 Protocol->CopyMem = RootBridgeIoCopyMem;
692
693 Protocol->Pci.Read = RootBridgeIoPciRead;
694 Protocol->Pci.Write = RootBridgeIoPciWrite;
695
696 Protocol->Map = RootBridgeIoMap;
697 Protocol->Unmap = RootBridgeIoUnmap;
698
699 Protocol->AllocateBuffer = RootBridgeIoAllocateBuffer;
700 Protocol->FreeBuffer = RootBridgeIoFreeBuffer;
701
702 Protocol->Flush = RootBridgeIoFlush;
703
704 Protocol->GetAttributes = RootBridgeIoGetAttributes;
705 Protocol->SetAttributes = RootBridgeIoSetAttributes;
706
707 Protocol->Configuration = RootBridgeIoConfiguration;
708
709 Protocol->SegmentNumber = 0;
710
711 Status = gBS->LocateProtocol (&gEfiMetronomeArchProtocolGuid, NULL, (VOID **)&mMetronome);
712 ASSERT_EFI_ERROR (Status);
713
714 return EFI_SUCCESS;
715 }
716
717 /**
718 Check parameters for IO,MMIO,PCI read/write services of PCI Root Bridge IO.
719
720 The I/O operations are carried out exactly as requested. The caller is responsible
721 for satisfying any alignment and I/O width restrictions that a PI System on a
722 platform might require. For example on some platforms, width requests of
723 EfiCpuIoWidthUint64 do not work. Misaligned buffers, on the other hand, will
724 be handled by the driver.
725
726 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
727 @param[in] OperationType I/O operation type: IO/MMIO/PCI.
728 @param[in] Width Signifies the width of the I/O or Memory operation.
729 @param[in] Address The base address of the I/O operation.
730 @param[in] Count The number of I/O operations to perform. The number of
731 bytes moved is Width size * Count, starting at Address.
732 @param[in] Buffer For read operations, the destination buffer to store the results.
733 For write operations, the source buffer from which to write data.
734
735 @retval EFI_SUCCESS The parameters for this request pass the checks.
736 @retval EFI_INVALID_PARAMETER Width is invalid for this PI system.
737 @retval EFI_INVALID_PARAMETER Buffer is NULL.
738 @retval EFI_UNSUPPORTED The Buffer is not aligned for the given Width.
739 @retval EFI_UNSUPPORTED The address range specified by Address, Width,
740 and Count is not valid for this PI system.
741
742 **/
743 EFI_STATUS
744 RootBridgeIoCheckParameter (
745 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
746 IN OPERATION_TYPE OperationType,
747 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
748 IN UINT64 Address,
749 IN UINTN Count,
750 IN VOID *Buffer
751 )
752 {
753 PCI_ROOT_BRIDGE_INSTANCE *PrivateData;
754 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *PciRbAddr;
755 UINT64 MaxCount;
756 UINT64 Base;
757 UINT64 Limit;
758
759 //
760 // Check to see if Buffer is NULL
761 //
762 if (Buffer == NULL) {
763 return EFI_INVALID_PARAMETER;
764 }
765
766 //
767 // Check to see if Width is in the valid range
768 //
769 if ((UINT32)Width >= EfiPciWidthMaximum) {
770 return EFI_INVALID_PARAMETER;
771 }
772
773 //
774 // For FIFO type, the target address won't increase during the access,
775 // so treat Count as 1
776 //
777 if (Width >= EfiPciWidthFifoUint8 && Width <= EfiPciWidthFifoUint64) {
778 Count = 1;
779 }
780
781 //
782 // Check to see if Width is in the valid range for I/O Port operations
783 //
784 Width = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) (Width & 0x03);
785 if ((OperationType != MemOperation) && (Width == EfiPciWidthUint64)) {
786 ASSERT (FALSE);
787 return EFI_INVALID_PARAMETER;
788 }
789
790 //
791 // Check to see if Address is aligned
792 //
793 if ((Address & (UINT64)(mInStride[Width] - 1)) != 0) {
794 return EFI_UNSUPPORTED;
795 }
796
797 PrivateData = DRIVER_INSTANCE_FROM_PCI_ROOT_BRIDGE_IO_THIS (This);
798
799 //
800 // Check to see if any address associated with this transfer exceeds the maximum
801 // allowed address. The maximum address implied by the parameters passed in is
802 // Address + Size * Count. If the following condition is met, then the transfer
803 // is not supported.
804 //
805 // Address + Size * Count > Limit + 1
806 //
807 // Since Limit can be the maximum integer value supported by the CPU and Count
808 // can also be the maximum integer value supported by the CPU, this range
809 // check must be adjusted to avoid all oveflow conditions.
810 //
811 // The following form of the range check is equivalent but assumes that
812 // Limit is of the form (2^n - 1).
813 //
814 if (OperationType == IoOperation) {
815 Base = PrivateData->IoBase;
816 Limit = PrivateData->IoLimit;
817 } else if (OperationType == MemOperation) {
818 Base = PrivateData->MemBase;
819 Limit = PrivateData->MemLimit;
820 } else {
821 PciRbAddr = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS*) &Address;
822 if (PciRbAddr->Bus < PrivateData->BusBase || PciRbAddr->Bus > PrivateData->BusLimit) {
823 return EFI_INVALID_PARAMETER;
824 }
825
826 if (PciRbAddr->Device > MAX_PCI_DEVICE_NUMBER || PciRbAddr->Function > MAX_PCI_FUNCTION_NUMBER) {
827 return EFI_INVALID_PARAMETER;
828 }
829
830 if (PciRbAddr->ExtendedRegister != 0) {
831 Address = PciRbAddr->ExtendedRegister;
832 } else {
833 Address = PciRbAddr->Register;
834 }
835 Base = 0;
836 Limit = MAX_PCI_REG_ADDRESS;
837 }
838
839 if (Address < Base) {
840 return EFI_INVALID_PARAMETER;
841 }
842
843 if (Count == 0) {
844 if (Address > Limit) {
845 return EFI_UNSUPPORTED;
846 }
847 } else {
848 MaxCount = RShiftU64 (Limit, Width);
849 if (MaxCount < (Count - 1)) {
850 return EFI_UNSUPPORTED;
851 }
852 if (Address > LShiftU64 (MaxCount - Count + 1, Width)) {
853 return EFI_UNSUPPORTED;
854 }
855 }
856
857 return EFI_SUCCESS;
858 }
859
860 /**
861 Internal help function for read and write memory space.
862
863 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
864 @param[in] Write Switch value for Read or Write.
865 @param[in] Width Signifies the width of the memory operations.
866 @param[in] UserAddress The address within the PCI configuration space for the PCI controller.
867 @param[in] Count The number of PCI configuration operations to perform. Bytes
868 moved is Width size * Count, starting at Address.
869 @param[in, out] UserBuffer For read operations, the destination buffer to store the results. For
870 write operations, the source buffer to write data from.
871
872 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
873 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
874 @retval EFI_INVALID_PARAMETER Buffer is NULL.
875 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
876
877 **/
878 EFI_STATUS
879 RootBridgeIoMemRW (
880 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
881 IN BOOLEAN Write,
882 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
883 IN UINT64 Address,
884 IN UINTN Count,
885 IN OUT VOID *Buffer
886 )
887 {
888 EFI_STATUS Status;
889 UINT8 InStride;
890 UINT8 OutStride;
891 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH OperationWidth;
892 UINT8 *Uint8Buffer;
893
894 Status = RootBridgeIoCheckParameter (This, MemOperation, Width, Address, Count, Buffer);
895 if (EFI_ERROR (Status)) {
896 return Status;
897 }
898
899 InStride = mInStride[Width];
900 OutStride = mOutStride[Width];
901 OperationWidth = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) (Width & 0x03);
902 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
903 if (Write) {
904 switch (OperationWidth) {
905 case EfiPciWidthUint8:
906 MmioWrite8 ((UINTN)Address, *Uint8Buffer);
907 break;
908 case EfiPciWidthUint16:
909 MmioWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
910 break;
911 case EfiPciWidthUint32:
912 MmioWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
913 break;
914 case EfiPciWidthUint64:
915 MmioWrite64 ((UINTN)Address, *((UINT64 *)Uint8Buffer));
916 break;
917 default:
918 //
919 // The RootBridgeIoCheckParameter call above will ensure that this
920 // path is not taken.
921 //
922 ASSERT (FALSE);
923 break;
924 }
925 } else {
926 switch (OperationWidth) {
927 case EfiPciWidthUint8:
928 *Uint8Buffer = MmioRead8 ((UINTN)Address);
929 break;
930 case EfiPciWidthUint16:
931 *((UINT16 *)Uint8Buffer) = MmioRead16 ((UINTN)Address);
932 break;
933 case EfiPciWidthUint32:
934 *((UINT32 *)Uint8Buffer) = MmioRead32 ((UINTN)Address);
935 break;
936 case EfiPciWidthUint64:
937 *((UINT64 *)Uint8Buffer) = MmioRead64 ((UINTN)Address);
938 break;
939 default:
940 //
941 // The RootBridgeIoCheckParameter call above will ensure that this
942 // path is not taken.
943 //
944 ASSERT (FALSE);
945 break;
946 }
947 }
948 }
949 return EFI_SUCCESS;
950 }
951
952 /**
953 Internal help function for read and write IO space.
954
955 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
956 @param[in] Write Switch value for Read or Write.
957 @param[in] Width Signifies the width of the memory operations.
958 @param[in] UserAddress The address within the PCI configuration space for the PCI controller.
959 @param[in] Count The number of PCI configuration operations to perform. Bytes
960 moved is Width size * Count, starting at Address.
961 @param[in, out] UserBuffer For read operations, the destination buffer to store the results. For
962 write operations, the source buffer to write data from.
963
964 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
965 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
966 @retval EFI_INVALID_PARAMETER Buffer is NULL.
967 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
968
969 **/
970 EFI_STATUS
971 RootBridgeIoIoRW (
972 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
973 IN BOOLEAN Write,
974 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
975 IN UINT64 Address,
976 IN UINTN Count,
977 IN OUT VOID *Buffer
978 )
979 {
980 EFI_STATUS Status;
981 UINT8 InStride;
982 UINT8 OutStride;
983 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH OperationWidth;
984 UINT8 *Uint8Buffer;
985
986 Status = RootBridgeIoCheckParameter (This, IoOperation, Width, Address, Count, Buffer);
987 if (EFI_ERROR (Status)) {
988 return Status;
989 }
990
991 InStride = mInStride[Width];
992 OutStride = mOutStride[Width];
993 OperationWidth = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) (Width & 0x03);
994
995 for (Uint8Buffer = Buffer; Count > 0; Address += InStride, Uint8Buffer += OutStride, Count--) {
996 if (Write) {
997 switch (OperationWidth) {
998 case EfiPciWidthUint8:
999 IoWrite8 ((UINTN)Address, *Uint8Buffer);
1000 break;
1001 case EfiPciWidthUint16:
1002 IoWrite16 ((UINTN)Address, *((UINT16 *)Uint8Buffer));
1003 break;
1004 case EfiPciWidthUint32:
1005 IoWrite32 ((UINTN)Address, *((UINT32 *)Uint8Buffer));
1006 break;
1007 default:
1008 //
1009 // The RootBridgeIoCheckParameter call above will ensure that this
1010 // path is not taken.
1011 //
1012 ASSERT (FALSE);
1013 break;
1014 }
1015 } else {
1016 switch (OperationWidth) {
1017 case EfiPciWidthUint8:
1018 *Uint8Buffer = IoRead8 ((UINTN)Address);
1019 break;
1020 case EfiPciWidthUint16:
1021 *((UINT16 *)Uint8Buffer) = IoRead16 ((UINTN)Address);
1022 break;
1023 case EfiPciWidthUint32:
1024 *((UINT32 *)Uint8Buffer) = IoRead32 ((UINTN)Address);
1025 break;
1026 default:
1027 //
1028 // The RootBridgeIoCheckParameter call above will ensure that this
1029 // path is not taken.
1030 //
1031 ASSERT (FALSE);
1032 break;
1033 }
1034 }
1035 }
1036 return EFI_SUCCESS;
1037 }
1038
1039 /**
1040 Internal help function for read and write PCI configuration space.
1041
1042 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1043 @param[in] Write Switch value for Read or Write.
1044 @param[in] Width Signifies the width of the memory operations.
1045 @param[in] UserAddress The address within the PCI configuration space for the PCI controller.
1046 @param[in] Count The number of PCI configuration operations to perform. Bytes
1047 moved is Width size * Count, starting at Address.
1048 @param[in, out] UserBuffer For read operations, the destination buffer to store the results. For
1049 write operations, the source buffer to write data from.
1050
1051 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1052 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1053 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1054 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1055
1056 **/
1057 EFI_STATUS
1058 RootBridgeIoPciRW (
1059 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1060 IN BOOLEAN Write,
1061 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1062 IN UINT64 Address,
1063 IN UINTN Count,
1064 IN OUT VOID *Buffer
1065 )
1066 {
1067 EFI_STATUS Status;
1068 UINT8 InStride;
1069 UINT8 OutStride;
1070 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH OperationWidth;
1071 UINT8 *Uint8Buffer;
1072 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS *PciRbAddr;
1073 UINTN PcieRegAddr;
1074
1075 Status = RootBridgeIoCheckParameter (This, PciOperation, Width, Address, Count, Buffer);
1076 if (EFI_ERROR (Status)) {
1077 return Status;
1078 }
1079
1080 PciRbAddr = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS*) &Address;
1081
1082 PcieRegAddr = (UINTN) PCI_LIB_ADDRESS (
1083 PciRbAddr->Bus,
1084 PciRbAddr->Device,
1085 PciRbAddr->Function,
1086 (PciRbAddr->ExtendedRegister != 0) ? \
1087 PciRbAddr->ExtendedRegister :
1088 PciRbAddr->Register
1089 );
1090
1091 InStride = mInStride[Width];
1092 OutStride = mOutStride[Width];
1093 OperationWidth = (EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH) (Width & 0x03);
1094 for (Uint8Buffer = Buffer; Count > 0; PcieRegAddr += InStride, Uint8Buffer += OutStride, Count--) {
1095 if (Write) {
1096 switch (OperationWidth) {
1097 case EfiPciWidthUint8:
1098 PciWrite8 (PcieRegAddr, *Uint8Buffer);
1099 break;
1100 case EfiPciWidthUint16:
1101 PciWrite16 (PcieRegAddr, *((UINT16 *)Uint8Buffer));
1102 break;
1103 case EfiPciWidthUint32:
1104 PciWrite32 (PcieRegAddr, *((UINT32 *)Uint8Buffer));
1105 break;
1106 default:
1107 //
1108 // The RootBridgeIoCheckParameter call above will ensure that this
1109 // path is not taken.
1110 //
1111 ASSERT (FALSE);
1112 break;
1113 }
1114 } else {
1115 switch (OperationWidth) {
1116 case EfiPciWidthUint8:
1117 *Uint8Buffer = PciRead8 (PcieRegAddr);
1118 break;
1119 case EfiPciWidthUint16:
1120 *((UINT16 *)Uint8Buffer) = PciRead16 (PcieRegAddr);
1121 break;
1122 case EfiPciWidthUint32:
1123 *((UINT32 *)Uint8Buffer) = PciRead32 (PcieRegAddr);
1124 break;
1125 default:
1126 //
1127 // The RootBridgeIoCheckParameter call above will ensure that this
1128 // path is not taken.
1129 //
1130 ASSERT (FALSE);
1131 break;
1132 }
1133 }
1134 }
1135
1136 return EFI_SUCCESS;
1137 }
1138
1139 /**
1140 Polls an address in memory mapped I/O space until an exit condition is met, or
1141 a timeout occurs.
1142
1143 This function provides a standard way to poll a PCI memory location. A PCI memory read
1144 operation is performed at the PCI memory address specified by Address for the width specified
1145 by Width. The result of this PCI memory read operation is stored in Result. This PCI memory
1146 read operation is repeated until either a timeout of Delay 100 ns units has expired, or (Result &
1147 Mask) is equal to Value.
1148
1149 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1150 @param[in] Width Signifies the width of the memory operations.
1151 @param[in] Address The base address of the memory operations. The caller is
1152 responsible for aligning Address if required.
1153 @param[in] Mask Mask used for the polling criteria. Bytes above Width in Mask
1154 are ignored. The bits in the bytes below Width which are zero in
1155 Mask are ignored when polling the memory address.
1156 @param[in] Value The comparison value used for the polling exit criteria.
1157 @param[in] Delay The number of 100 ns units to poll. Note that timer available may
1158 be of poorer granularity.
1159 @param[out] Result Pointer to the last value read from the memory location.
1160
1161 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
1162 @retval EFI_INVALID_PARAMETER Width is invalid.
1163 @retval EFI_INVALID_PARAMETER Result is NULL.
1164 @retval EFI_TIMEOUT Delay expired before a match occurred.
1165 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1166
1167 **/
1168 EFI_STATUS
1169 EFIAPI
1170 RootBridgeIoPollMem (
1171 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1172 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1173 IN UINT64 Address,
1174 IN UINT64 Mask,
1175 IN UINT64 Value,
1176 IN UINT64 Delay,
1177 OUT UINT64 *Result
1178 )
1179 {
1180 EFI_STATUS Status;
1181 UINT64 NumberOfTicks;
1182 UINT32 Remainder;
1183
1184 if (Result == NULL) {
1185 return EFI_INVALID_PARAMETER;
1186 }
1187
1188 if ((UINT32)Width > EfiPciWidthUint64) {
1189 return EFI_INVALID_PARAMETER;
1190 }
1191
1192 //
1193 // No matter what, always do a single poll.
1194 //
1195 Status = This->Mem.Read (This, Width, Address, 1, Result);
1196 if (EFI_ERROR (Status)) {
1197 return Status;
1198 }
1199 if ((*Result & Mask) == Value) {
1200 return EFI_SUCCESS;
1201 }
1202
1203 if (Delay == 0) {
1204 return EFI_SUCCESS;
1205
1206 } else {
1207
1208 //
1209 // Determine the proper # of metronome ticks to wait for polling the
1210 // location. The nuber of ticks is Roundup (Delay / mMetronome->TickPeriod)+1
1211 // The "+1" to account for the possibility of the first tick being short
1212 // because we started in the middle of a tick.
1213 //
1214 // BugBug: overriding mMetronome->TickPeriod with UINT32 until Metronome
1215 // protocol definition is updated.
1216 //
1217 NumberOfTicks = DivU64x32Remainder (Delay, (UINT32) mMetronome->TickPeriod, &Remainder);
1218 if (Remainder != 0) {
1219 NumberOfTicks += 1;
1220 }
1221 NumberOfTicks += 1;
1222
1223 while (NumberOfTicks != 0) {
1224
1225 mMetronome->WaitForTick (mMetronome, 1);
1226
1227 Status = This->Mem.Read (This, Width, Address, 1, Result);
1228 if (EFI_ERROR (Status)) {
1229 return Status;
1230 }
1231
1232 if ((*Result & Mask) == Value) {
1233 return EFI_SUCCESS;
1234 }
1235
1236 NumberOfTicks -= 1;
1237 }
1238 }
1239 return EFI_TIMEOUT;
1240 }
1241
1242 /**
1243 Reads from the I/O space of a PCI Root Bridge. Returns when either the polling exit criteria is
1244 satisfied or after a defined duration.
1245
1246 This function provides a standard way to poll a PCI I/O location. A PCI I/O read operation is
1247 performed at the PCI I/O address specified by Address for the width specified by Width.
1248 The result of this PCI I/O read operation is stored in Result. This PCI I/O read operation is
1249 repeated until either a timeout of Delay 100 ns units has expired, or (Result & Mask) is equal
1250 to Value.
1251
1252 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1253 @param[in] Width Signifies the width of the I/O operations.
1254 @param[in] Address The base address of the I/O operations. The caller is responsible
1255 for aligning Address if required.
1256 @param[in] Mask Mask used for the polling criteria. Bytes above Width in Mask
1257 are ignored. The bits in the bytes below Width which are zero in
1258 Mask are ignored when polling the I/O address.
1259 @param[in] Value The comparison value used for the polling exit criteria.
1260 @param[in] Delay The number of 100 ns units to poll. Note that timer available may
1261 be of poorer granularity.
1262 @param[out] Result Pointer to the last value read from the memory location.
1263
1264 @retval EFI_SUCCESS The last data returned from the access matched the poll exit criteria.
1265 @retval EFI_INVALID_PARAMETER Width is invalid.
1266 @retval EFI_INVALID_PARAMETER Result is NULL.
1267 @retval EFI_TIMEOUT Delay expired before a match occurred.
1268 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1269
1270 **/
1271 EFI_STATUS
1272 EFIAPI
1273 RootBridgeIoPollIo (
1274 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1275 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1276 IN UINT64 Address,
1277 IN UINT64 Mask,
1278 IN UINT64 Value,
1279 IN UINT64 Delay,
1280 OUT UINT64 *Result
1281 )
1282 {
1283 EFI_STATUS Status;
1284 UINT64 NumberOfTicks;
1285 UINT32 Remainder;
1286
1287 //
1288 // No matter what, always do a single poll.
1289 //
1290
1291 if (Result == NULL) {
1292 return EFI_INVALID_PARAMETER;
1293 }
1294
1295 if ((UINT32)Width > EfiPciWidthUint64) {
1296 return EFI_INVALID_PARAMETER;
1297 }
1298
1299 Status = This->Io.Read (This, Width, Address, 1, Result);
1300 if (EFI_ERROR (Status)) {
1301 return Status;
1302 }
1303 if ((*Result & Mask) == Value) {
1304 return EFI_SUCCESS;
1305 }
1306
1307 if (Delay == 0) {
1308 return EFI_SUCCESS;
1309
1310 } else {
1311
1312 //
1313 // Determine the proper # of metronome ticks to wait for polling the
1314 // location. The number of ticks is Roundup (Delay / mMetronome->TickPeriod)+1
1315 // The "+1" to account for the possibility of the first tick being short
1316 // because we started in the middle of a tick.
1317 //
1318 NumberOfTicks = DivU64x32Remainder (Delay, (UINT32)mMetronome->TickPeriod, &Remainder);
1319 if (Remainder != 0) {
1320 NumberOfTicks += 1;
1321 }
1322 NumberOfTicks += 1;
1323
1324 while (NumberOfTicks != 0) {
1325
1326 mMetronome->WaitForTick (mMetronome, 1);
1327
1328 Status = This->Io.Read (This, Width, Address, 1, Result);
1329 if (EFI_ERROR (Status)) {
1330 return Status;
1331 }
1332
1333 if ((*Result & Mask) == Value) {
1334 return EFI_SUCCESS;
1335 }
1336
1337 NumberOfTicks -= 1;
1338 }
1339 }
1340 return EFI_TIMEOUT;
1341 }
1342
1343 /**
1344 Enables a PCI driver to access PCI controller registers in the PCI root bridge memory space.
1345
1346 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI controller
1347 registers in the PCI root bridge memory space.
1348 The memory operations are carried out exactly as requested. The caller is responsible for satisfying
1349 any alignment and memory width restrictions that a PCI Root Bridge on a platform might require.
1350
1351 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1352 @param[in] Width Signifies the width of the memory operation.
1353 @param[in] Address The base address of the memory operation. The caller is
1354 responsible for aligning the Address if required.
1355 @param[in] Count The number of memory operations to perform. Bytes moved is
1356 Width size * Count, starting at Address.
1357 @param[out] Buffer For read operations, the destination buffer to store the results. For
1358 write operations, the source buffer to write data from.
1359
1360 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1361 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1362 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1363 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1364
1365 **/
1366 EFI_STATUS
1367 EFIAPI
1368 RootBridgeIoMemRead (
1369 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1370 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1371 IN UINT64 Address,
1372 IN UINTN Count,
1373 OUT VOID *Buffer
1374 )
1375 {
1376 return RootBridgeIoMemRW (This, FALSE, Width, Address, Count, Buffer);
1377 }
1378
1379 /**
1380 Enables a PCI driver to access PCI controller registers in the PCI root bridge memory space.
1381
1382 The Mem.Read(), and Mem.Write() functions enable a driver to access PCI controller
1383 registers in the PCI root bridge memory space.
1384 The memory operations are carried out exactly as requested. The caller is responsible for satisfying
1385 any alignment and memory width restrictions that a PCI Root Bridge on a platform might require.
1386
1387 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1388 @param[in] Width Signifies the width of the memory operation.
1389 @param[in] Address The base address of the memory operation. The caller is
1390 responsible for aligning the Address if required.
1391 @param[in] Count The number of memory operations to perform. Bytes moved is
1392 Width size * Count, starting at Address.
1393 @param[in] Buffer For read operations, the destination buffer to store the results. For
1394 write operations, the source buffer to write data from.
1395
1396 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1397 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1398 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1399 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1400 **/
1401 EFI_STATUS
1402 EFIAPI
1403 RootBridgeIoMemWrite (
1404 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1405 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1406 IN UINT64 Address,
1407 IN UINTN Count,
1408 IN VOID *Buffer
1409 )
1410 {
1411 return RootBridgeIoMemRW (This, TRUE, Width, Address, Count, Buffer);
1412 }
1413
1414 /**
1415 Enables a PCI driver to access PCI controller registers in the PCI root bridge I/O space.
1416
1417 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1418 @param[in] Width Signifies the width of the memory operations.
1419 @param[in] Address The base address of the I/O operation. The caller is responsible for
1420 aligning the Address if required.
1421 @param[in] Count The number of I/O operations to perform. Bytes moved is Width
1422 size * Count, starting at Address.
1423 @param[out] Buffer For read operations, the destination buffer to store the results. For
1424 write operations, the source buffer to write data from.
1425
1426 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1427 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1428 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1429 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1430
1431 **/
1432 EFI_STATUS
1433 EFIAPI
1434 RootBridgeIoIoRead (
1435 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1436 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1437 IN UINT64 Address,
1438 IN UINTN Count,
1439 OUT VOID *Buffer
1440 )
1441 {
1442 return RootBridgeIoIoRW (This, FALSE, Width, Address, Count, Buffer);
1443 }
1444
1445 /**
1446 Enables a PCI driver to access PCI controller registers in the PCI root bridge I/O space.
1447
1448 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1449 @param[in] Width Signifies the width of the memory operations.
1450 @param[in] Address The base address of the I/O operation. The caller is responsible for
1451 aligning the Address if required.
1452 @param[in] Count The number of I/O operations to perform. Bytes moved is Width
1453 size * Count, starting at Address.
1454 @param[in] Buffer For read operations, the destination buffer to store the results. For
1455 write operations, the source buffer to write data from.
1456
1457 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1458 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1459 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1460 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1461
1462 **/
1463 EFI_STATUS
1464 EFIAPI
1465 RootBridgeIoIoWrite (
1466 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1467 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1468 IN UINT64 Address,
1469 IN UINTN Count,
1470 IN VOID *Buffer
1471 )
1472 {
1473 return RootBridgeIoIoRW (This, TRUE, Width, Address, Count, Buffer);
1474 }
1475
1476 /**
1477 Enables a PCI driver to copy one region of PCI root bridge memory space to another region of PCI
1478 root bridge memory space.
1479
1480 The CopyMem() function enables a PCI driver to copy one region of PCI root bridge memory
1481 space to another region of PCI root bridge memory space. This is especially useful for video scroll
1482 operation on a memory mapped video buffer.
1483 The memory operations are carried out exactly as requested. The caller is responsible for satisfying
1484 any alignment and memory width restrictions that a PCI root bridge on a platform might require.
1485
1486 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL instance.
1487 @param[in] Width Signifies the width of the memory operations.
1488 @param[in] DestAddress The destination address of the memory operation. The caller is
1489 responsible for aligning the DestAddress if required.
1490 @param[in] SrcAddress The source address of the memory operation. The caller is
1491 responsible for aligning the SrcAddress if required.
1492 @param[in] Count The number of memory operations to perform. Bytes moved is
1493 Width size * Count, starting at DestAddress and SrcAddress.
1494
1495 @retval EFI_SUCCESS The data was copied from one memory region to another memory region.
1496 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1497 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1498
1499 **/
1500 EFI_STATUS
1501 EFIAPI
1502 RootBridgeIoCopyMem (
1503 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1504 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1505 IN UINT64 DestAddress,
1506 IN UINT64 SrcAddress,
1507 IN UINTN Count
1508 )
1509 {
1510 EFI_STATUS Status;
1511 BOOLEAN Direction;
1512 UINTN Stride;
1513 UINTN Index;
1514 UINT64 Result;
1515
1516 if ((UINT32)Width > EfiPciWidthUint64) {
1517 return EFI_INVALID_PARAMETER;
1518 }
1519
1520 if (DestAddress == SrcAddress) {
1521 return EFI_SUCCESS;
1522 }
1523
1524 Stride = (UINTN)(1 << Width);
1525
1526 Direction = TRUE;
1527 if ((DestAddress > SrcAddress) && (DestAddress < (SrcAddress + Count * Stride))) {
1528 Direction = FALSE;
1529 SrcAddress = SrcAddress + (Count-1) * Stride;
1530 DestAddress = DestAddress + (Count-1) * Stride;
1531 }
1532
1533 for (Index = 0;Index < Count;Index++) {
1534 Status = RootBridgeIoMemRead (
1535 This,
1536 Width,
1537 SrcAddress,
1538 1,
1539 &Result
1540 );
1541 if (EFI_ERROR (Status)) {
1542 return Status;
1543 }
1544 Status = RootBridgeIoMemWrite (
1545 This,
1546 Width,
1547 DestAddress,
1548 1,
1549 &Result
1550 );
1551 if (EFI_ERROR (Status)) {
1552 return Status;
1553 }
1554 if (Direction) {
1555 SrcAddress += Stride;
1556 DestAddress += Stride;
1557 } else {
1558 SrcAddress -= Stride;
1559 DestAddress -= Stride;
1560 }
1561 }
1562 return EFI_SUCCESS;
1563 }
1564
1565 /**
1566 Enables a PCI driver to access PCI controller registers in a PCI root bridge's configuration space.
1567
1568 The Pci.Read() and Pci.Write() functions enable a driver to access PCI configuration
1569 registers for a PCI controller.
1570 The PCI Configuration operations are carried out exactly as requested. The caller is responsible for
1571 any alignment and PCI configuration width issues that a PCI Root Bridge on a platform might
1572 require.
1573
1574 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1575 @param[in] Width Signifies the width of the memory operations.
1576 @param[in] Address The address within the PCI configuration space for the PCI controller.
1577 @param[in] Count The number of PCI configuration operations to perform. Bytes
1578 moved is Width size * Count, starting at Address.
1579 @param[out] Buffer For read operations, the destination buffer to store the results. For
1580 write operations, the source buffer to write data from.
1581
1582 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1583 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1584 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1585 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1586
1587 **/
1588 EFI_STATUS
1589 EFIAPI
1590 RootBridgeIoPciRead (
1591 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1592 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1593 IN UINT64 Address,
1594 IN UINTN Count,
1595 OUT VOID *Buffer
1596 )
1597 {
1598 return RootBridgeIoPciRW (This, FALSE, Width, Address, Count, Buffer);
1599 }
1600
1601 /**
1602 Enables a PCI driver to access PCI controller registers in a PCI root bridge's configuration space.
1603
1604 The Pci.Read() and Pci.Write() functions enable a driver to access PCI configuration
1605 registers for a PCI controller.
1606 The PCI Configuration operations are carried out exactly as requested. The caller is responsible for
1607 any alignment and PCI configuration width issues that a PCI Root Bridge on a platform might
1608 require.
1609
1610 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1611 @param[in] Width Signifies the width of the memory operations.
1612 @param[in] Address The address within the PCI configuration space for the PCI controller.
1613 @param[in] Count The number of PCI configuration operations to perform. Bytes
1614 moved is Width size * Count, starting at Address.
1615 @param[in] Buffer For read operations, the destination buffer to store the results. For
1616 write operations, the source buffer to write data from.
1617
1618 @retval EFI_SUCCESS The data was read from or written to the PCI root bridge.
1619 @retval EFI_INVALID_PARAMETER Width is invalid for this PCI root bridge.
1620 @retval EFI_INVALID_PARAMETER Buffer is NULL.
1621 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1622
1623 **/
1624 EFI_STATUS
1625 EFIAPI
1626 RootBridgeIoPciWrite (
1627 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1628 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_WIDTH Width,
1629 IN UINT64 Address,
1630 IN UINTN Count,
1631 IN VOID *Buffer
1632 )
1633 {
1634 return RootBridgeIoPciRW (This, TRUE, Width, Address, Count, Buffer);
1635 }
1636
1637 /**
1638 Provides the PCI controller-specific addresses required to access system memory from a
1639 DMA bus master.
1640
1641 The Map() function provides the PCI controller specific addresses needed to access system
1642 memory. This function is used to map system memory for PCI bus master DMA accesses.
1643
1644 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1645 @param[in] Operation Indicates if the bus master is going to read or write to system memory.
1646 @param[in] HostAddress The system memory address to map to the PCI controller.
1647 @param[in, out] NumberOfBytes On input the number of bytes to map. On output the number of bytes that were mapped.
1648 @param[out] DeviceAddress The resulting map address for the bus master PCI controller to use
1649 to access the system memory's HostAddress.
1650 @param[out] Mapping The value to pass to Unmap() when the bus master DMA operation is complete.
1651
1652 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
1653 @retval EFI_INVALID_PARAMETER Operation is invalid.
1654 @retval EFI_INVALID_PARAMETER HostAddress is NULL.
1655 @retval EFI_INVALID_PARAMETER NumberOfBytes is NULL.
1656 @retval EFI_INVALID_PARAMETER DeviceAddress is NULL.
1657 @retval EFI_INVALID_PARAMETER Mapping is NULL.
1658 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
1659 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
1660 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1661
1662 **/
1663 EFI_STATUS
1664 EFIAPI
1665 RootBridgeIoMap (
1666 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1667 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_OPERATION Operation,
1668 IN VOID *HostAddress,
1669 IN OUT UINTN *NumberOfBytes,
1670 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
1671 OUT VOID **Mapping
1672 )
1673 {
1674 EFI_STATUS Status;
1675 EFI_PHYSICAL_ADDRESS PhysicalAddress;
1676 MAP_INFO *MapInfo;
1677
1678 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL) {
1679 return EFI_INVALID_PARAMETER;
1680 }
1681
1682 //
1683 // Initialize the return values to their defaults
1684 //
1685 *Mapping = NULL;
1686
1687 //
1688 // Make sure that Operation is valid
1689 //
1690 if ((UINT32)Operation >= EfiPciOperationMaximum) {
1691 return EFI_INVALID_PARAMETER;
1692 }
1693
1694 //
1695 // Most PCAT like chipsets can not handle performing DMA above 4GB.
1696 // If any part of the DMA transfer being mapped is above 4GB, then
1697 // map the DMA transfer to a buffer below 4GB.
1698 //
1699 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress;
1700 if ((PhysicalAddress + *NumberOfBytes) > 0x100000000ULL) {
1701
1702 //
1703 // Common Buffer operations can not be remapped. If the common buffer
1704 // if above 4GB, then it is not possible to generate a mapping, so return
1705 // an error.
1706 //
1707 if (Operation == EfiPciOperationBusMasterCommonBuffer || Operation == EfiPciOperationBusMasterCommonBuffer64) {
1708 return EFI_UNSUPPORTED;
1709 }
1710
1711 //
1712 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
1713 // called later.
1714 //
1715 Status = gBS->AllocatePool (
1716 EfiBootServicesData,
1717 sizeof(MAP_INFO),
1718 (VOID **)&MapInfo
1719 );
1720 if (EFI_ERROR (Status)) {
1721 *NumberOfBytes = 0;
1722 return Status;
1723 }
1724
1725 //
1726 // Return a pointer to the MAP_INFO structure in Mapping
1727 //
1728 *Mapping = MapInfo;
1729
1730 //
1731 // Initialize the MAP_INFO structure
1732 //
1733 MapInfo->Operation = Operation;
1734 MapInfo->NumberOfBytes = *NumberOfBytes;
1735 MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES(*NumberOfBytes);
1736 MapInfo->HostAddress = PhysicalAddress;
1737 MapInfo->MappedHostAddress = 0x00000000ffffffff;
1738
1739 //
1740 // Allocate a buffer below 4GB to map the transfer to.
1741 //
1742 Status = gBS->AllocatePages (
1743 AllocateMaxAddress,
1744 EfiBootServicesData,
1745 MapInfo->NumberOfPages,
1746 &MapInfo->MappedHostAddress
1747 );
1748 if (EFI_ERROR (Status)) {
1749 gBS->FreePool (MapInfo);
1750 *NumberOfBytes = 0;
1751 return Status;
1752 }
1753
1754 //
1755 // If this is a read operation from the Bus Master's point of view,
1756 // then copy the contents of the real buffer into the mapped buffer
1757 // so the Bus Master can read the contents of the real buffer.
1758 //
1759 if (Operation == EfiPciOperationBusMasterRead || Operation == EfiPciOperationBusMasterRead64) {
1760 CopyMem (
1761 (VOID *)(UINTN)MapInfo->MappedHostAddress,
1762 (VOID *)(UINTN)MapInfo->HostAddress,
1763 MapInfo->NumberOfBytes
1764 );
1765 }
1766
1767 //
1768 // The DeviceAddress is the address of the maped buffer below 4GB
1769 //
1770 *DeviceAddress = MapInfo->MappedHostAddress;
1771 } else {
1772 //
1773 // The transfer is below 4GB, so the DeviceAddress is simply the HostAddress
1774 //
1775 *DeviceAddress = PhysicalAddress;
1776 }
1777
1778 return EFI_SUCCESS;
1779 }
1780
1781 /**
1782 Completes the Map() operation and releases any corresponding resources.
1783
1784 The Unmap() function completes the Map() operation and releases any corresponding resources.
1785 If the operation was an EfiPciOperationBusMasterWrite or
1786 EfiPciOperationBusMasterWrite64, the data is committed to the target system memory.
1787 Any resources used for the mapping are freed.
1788
1789 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1790 @param[in] Mapping The mapping value returned from Map().
1791
1792 @retval EFI_SUCCESS The range was unmapped.
1793 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
1794 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
1795
1796 **/
1797 EFI_STATUS
1798 EFIAPI
1799 RootBridgeIoUnmap (
1800 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1801 IN VOID *Mapping
1802 )
1803 {
1804 MAP_INFO *MapInfo;
1805
1806 //
1807 // See if the Map() operation associated with this Unmap() required a mapping buffer.
1808 // If a mapping buffer was not required, then this function simply returns EFI_SUCCESS.
1809 //
1810 if (Mapping != NULL) {
1811 //
1812 // Get the MAP_INFO structure from Mapping
1813 //
1814 MapInfo = (MAP_INFO *)Mapping;
1815
1816 //
1817 // If this is a write operation from the Bus Master's point of view,
1818 // then copy the contents of the mapped buffer into the real buffer
1819 // so the processor can read the contents of the real buffer.
1820 //
1821 if (MapInfo->Operation == EfiPciOperationBusMasterWrite || MapInfo->Operation == EfiPciOperationBusMasterWrite64) {
1822 CopyMem (
1823 (VOID *)(UINTN)MapInfo->HostAddress,
1824 (VOID *)(UINTN)MapInfo->MappedHostAddress,
1825 MapInfo->NumberOfBytes
1826 );
1827 }
1828
1829 //
1830 // Free the mapped buffer and the MAP_INFO structure.
1831 //
1832 gBS->FreePages (MapInfo->MappedHostAddress, MapInfo->NumberOfPages);
1833 gBS->FreePool (Mapping);
1834 }
1835 return EFI_SUCCESS;
1836 }
1837
1838 /**
1839 Allocates pages that are suitable for an EfiPciOperationBusMasterCommonBuffer or
1840 EfiPciOperationBusMasterCommonBuffer64 mapping.
1841
1842 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1843 @param Type This parameter is not used and must be ignored.
1844 @param MemoryType The type of memory to allocate, EfiBootServicesData or EfiRuntimeServicesData.
1845 @param Pages The number of pages to allocate.
1846 @param HostAddress A pointer to store the base system memory address of the allocated range.
1847 @param Attributes The requested bit mask of attributes for the allocated range. Only
1848 the attributes EFI_PCI_ATTRIBUTE_MEMORY_WRITE_COMBINE, EFI_PCI_ATTRIBUTE_MEMORY_CACHED,
1849 and EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE may be used with this function.
1850
1851 @retval EFI_SUCCESS The requested memory pages were allocated.
1852 @retval EFI_INVALID_PARAMETER MemoryType is invalid.
1853 @retval EFI_INVALID_PARAMETER HostAddress is NULL.
1854 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
1855 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and DUAL_ADDRESS_CYCLE.
1856 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
1857
1858 **/
1859 EFI_STATUS
1860 EFIAPI
1861 RootBridgeIoAllocateBuffer (
1862 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1863 IN EFI_ALLOCATE_TYPE Type,
1864 IN EFI_MEMORY_TYPE MemoryType,
1865 IN UINTN Pages,
1866 OUT VOID **HostAddress,
1867 IN UINT64 Attributes
1868 )
1869 {
1870 EFI_STATUS Status;
1871 EFI_PHYSICAL_ADDRESS PhysicalAddress;
1872
1873 //
1874 // Validate Attributes
1875 //
1876 if ((Attributes & EFI_PCI_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {
1877 return EFI_UNSUPPORTED;
1878 }
1879
1880 //
1881 // Check for invalid inputs
1882 //
1883 if (HostAddress == NULL) {
1884 return EFI_INVALID_PARAMETER;
1885 }
1886
1887 //
1888 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
1889 //
1890 if (MemoryType != EfiBootServicesData && MemoryType != EfiRuntimeServicesData) {
1891 return EFI_INVALID_PARAMETER;
1892 }
1893
1894 //
1895 // Limit allocations to memory below 4GB
1896 //
1897 PhysicalAddress = (EFI_PHYSICAL_ADDRESS)(0xffffffff);
1898
1899 Status = gBS->AllocatePages (AllocateMaxAddress, MemoryType, Pages, &PhysicalAddress);
1900 if (EFI_ERROR (Status)) {
1901 return Status;
1902 }
1903
1904 *HostAddress = (VOID *)(UINTN)PhysicalAddress;
1905
1906 return EFI_SUCCESS;
1907 }
1908
1909 /**
1910 Frees memory that was allocated with AllocateBuffer().
1911
1912 The FreeBuffer() function frees memory that was allocated with AllocateBuffer().
1913
1914 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1915 @param Pages The number of pages to free.
1916 @param HostAddress The base system memory address of the allocated range.
1917
1918 @retval EFI_SUCCESS The requested memory pages were freed.
1919 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
1920 was not allocated with AllocateBuffer().
1921
1922 **/
1923 EFI_STATUS
1924 EFIAPI
1925 RootBridgeIoFreeBuffer (
1926 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1927 IN UINTN Pages,
1928 OUT VOID *HostAddress
1929 )
1930 {
1931 return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages);
1932 }
1933
1934 /**
1935 Flushes all PCI posted write transactions from a PCI host bridge to system memory.
1936
1937 The Flush() function flushes any PCI posted write transactions from a PCI host bridge to system
1938 memory. Posted write transactions are generated by PCI bus masters when they perform write
1939 transactions to target addresses in system memory.
1940 This function does not flush posted write transactions from any PCI bridges. A PCI controller
1941 specific action must be taken to guarantee that the posted write transactions have been flushed from
1942 the PCI controller and from all the PCI bridges into the PCI host bridge. This is typically done with
1943 a PCI read transaction from the PCI controller prior to calling Flush().
1944
1945 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1946
1947 @retval EFI_SUCCESS The PCI posted write transactions were flushed from the PCI host
1948 bridge to system memory.
1949 @retval EFI_DEVICE_ERROR The PCI posted write transactions were not flushed from the PCI
1950 host bridge due to a hardware error.
1951
1952 **/
1953 EFI_STATUS
1954 EFIAPI
1955 RootBridgeIoFlush (
1956 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This
1957 )
1958 {
1959 //
1960 // not supported yet
1961 //
1962 return EFI_SUCCESS;
1963 }
1964
1965 /**
1966 Gets the attributes that a PCI root bridge supports setting with SetAttributes(), and the
1967 attributes that a PCI root bridge is currently using.
1968
1969 The GetAttributes() function returns the mask of attributes that this PCI root bridge supports
1970 and the mask of attributes that the PCI root bridge is currently using.
1971
1972 @param This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
1973 @param Supported A pointer to the mask of attributes that this PCI root bridge
1974 supports setting with SetAttributes().
1975 @param Attributes A pointer to the mask of attributes that this PCI root bridge is
1976 currently using.
1977
1978 @retval EFI_SUCCESS If Supports is not NULL, then the attributes that the PCI root
1979 bridge supports is returned in Supports. If Attributes is
1980 not NULL, then the attributes that the PCI root bridge is currently
1981 using is returned in Attributes.
1982 @retval EFI_INVALID_PARAMETER Both Supports and Attributes are NULL.
1983
1984 **/
1985 EFI_STATUS
1986 EFIAPI
1987 RootBridgeIoGetAttributes (
1988 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
1989 OUT UINT64 *Supported,
1990 OUT UINT64 *Attributes
1991 )
1992 {
1993 PCI_ROOT_BRIDGE_INSTANCE *PrivateData;
1994
1995 PrivateData = DRIVER_INSTANCE_FROM_PCI_ROOT_BRIDGE_IO_THIS(This);
1996
1997 if (Attributes == NULL && Supported == NULL) {
1998 return EFI_INVALID_PARAMETER;
1999 }
2000
2001 //
2002 // Set the return value for Supported and Attributes
2003 //
2004 if (Supported != NULL) {
2005 *Supported = PrivateData->Supports;
2006 }
2007
2008 if (Attributes != NULL) {
2009 *Attributes = PrivateData->Attributes;
2010 }
2011
2012 return EFI_SUCCESS;
2013 }
2014
2015 /**
2016 Sets attributes for a resource range on a PCI root bridge.
2017
2018 The SetAttributes() function sets the attributes specified in Attributes for the PCI root
2019 bridge on the resource range specified by ResourceBase and ResourceLength. Since the
2020 granularity of setting these attributes may vary from resource type to resource type, and from
2021 platform to platform, the actual resource range and the one passed in by the caller may differ. As a
2022 result, this function may set the attributes specified by Attributes on a larger resource range
2023 than the caller requested. The actual range is returned in ResourceBase and
2024 ResourceLength. The caller is responsible for verifying that the actual range for which the
2025 attributes were set is acceptable.
2026
2027 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
2028 @param[in] Attributes The mask of attributes to set. If the attribute bit
2029 MEMORY_WRITE_COMBINE, MEMORY_CACHED, or
2030 MEMORY_DISABLE is set, then the resource range is specified by
2031 ResourceBase and ResourceLength. If
2032 MEMORY_WRITE_COMBINE, MEMORY_CACHED, and
2033 MEMORY_DISABLE are not set, then ResourceBase and
2034 ResourceLength are ignored, and may be NULL.
2035 @param[in, out] ResourceBase A pointer to the base address of the resource range to be modified
2036 by the attributes specified by Attributes.
2037 @param[in, out] ResourceLength A pointer to the length of the resource range to be modified by the
2038 attributes specified by Attributes.
2039
2040 @retval EFI_SUCCESS The current configuration of this PCI root bridge was returned in Resources.
2041 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge could not be retrieved.
2042 @retval EFI_INVALID_PARAMETER Invalid pointer of EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
2043
2044 **/
2045 EFI_STATUS
2046 EFIAPI
2047 RootBridgeIoSetAttributes (
2048 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
2049 IN UINT64 Attributes,
2050 IN OUT UINT64 *ResourceBase,
2051 IN OUT UINT64 *ResourceLength
2052 )
2053 {
2054 PCI_ROOT_BRIDGE_INSTANCE *PrivateData;
2055
2056 PrivateData = DRIVER_INSTANCE_FROM_PCI_ROOT_BRIDGE_IO_THIS(This);
2057
2058 if (Attributes != 0) {
2059 if ((Attributes & (~(PrivateData->Supports))) != 0) {
2060 return EFI_UNSUPPORTED;
2061 }
2062 }
2063
2064 //
2065 // This is a generic driver for a PC-AT class system. It does not have any
2066 // chipset specific knowlegde, so none of the attributes can be set or
2067 // cleared. Any attempt to set attribute that are already set will succeed,
2068 // and any attempt to set an attribute that is not supported will fail.
2069 //
2070 if (Attributes & (~PrivateData->Attributes)) {
2071 return EFI_UNSUPPORTED;
2072 }
2073
2074 return EFI_SUCCESS;
2075 }
2076
2077 /**
2078 Retrieves the current resource settings of this PCI root bridge in the form of a set of ACPI 2.0
2079 resource descriptors.
2080
2081 There are only two resource descriptor types from the ACPI Specification that may be used to
2082 describe the current resources allocated to a PCI root bridge. These are the QWORD Address
2083 Space Descriptor (ACPI 2.0 Section 6.4.3.5.1), and the End Tag (ACPI 2.0 Section 6.4.2.8). The
2084 QWORD Address Space Descriptor can describe memory, I/O, and bus number ranges for dynamic
2085 or fixed resources. The configuration of a PCI root bridge is described with one or more QWORD
2086 Address Space Descriptors followed by an End Tag.
2087
2088 @param[in] This A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.
2089 @param[out] Resources A pointer to the ACPI 2.0 resource descriptors that describe the
2090 current configuration of this PCI root bridge. The storage for the
2091 ACPI 2.0 resource descriptors is allocated by this function. The
2092 caller must treat the return buffer as read-only data, and the buffer
2093 must not be freed by the caller.
2094
2095 @retval EFI_SUCCESS The current configuration of this PCI root bridge was returned in Resources.
2096 @retval EFI_UNSUPPORTED The current configuration of this PCI root bridge could not be retrieved.
2097 @retval EFI_INVALID_PARAMETER Invalid pointer of EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
2098
2099 **/
2100 EFI_STATUS
2101 EFIAPI
2102 RootBridgeIoConfiguration (
2103 IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *This,
2104 OUT VOID **Resources
2105 )
2106 {
2107 PCI_ROOT_BRIDGE_INSTANCE *PrivateData;
2108 UINTN Index;
2109
2110 PrivateData = DRIVER_INSTANCE_FROM_PCI_ROOT_BRIDGE_IO_THIS (This);
2111
2112 for (Index = 0; Index < TypeMax; Index++) {
2113 if (PrivateData->ResAllocNode[Index].Status == ResAllocated) {
2114 Configuration.SpaceDesp[Index].AddrRangeMin = PrivateData->ResAllocNode[Index].Base;
2115 Configuration.SpaceDesp[Index].AddrRangeMax = PrivateData->ResAllocNode[Index].Base + PrivateData->ResAllocNode[Index].Length - 1;
2116 Configuration.SpaceDesp[Index].AddrLen = PrivateData->ResAllocNode[Index].Length;
2117 }
2118 }
2119
2120 *Resources = &Configuration;
2121 return EFI_SUCCESS;
2122 }
2123