5 * Look for a PCI capability
7 * @v pci PCI device to query
8 * @v cap Capability code
9 * @ret address Address of capability, or 0 if not found
11 * Determine whether or not a device supports a given PCI capability.
12 * Returns the address of the requested capability structure within
13 * the device's PCI configuration space, or 0 if the device does not
16 int pci_find_capability ( struct pci_device *pci, int cap ) {
22 pci_read_config_word ( pci, PCI_STATUS, &status );
23 if ( ! ( status & PCI_STATUS_CAP_LIST ) )
26 pci_read_config_byte ( pci, PCI_HEADER_TYPE, &hdr_type );
27 switch ( hdr_type & 0x7F ) {
28 case PCI_HEADER_TYPE_NORMAL:
29 case PCI_HEADER_TYPE_BRIDGE:
31 pci_read_config_byte ( pci, PCI_CAPABILITY_LIST, &pos );
33 case PCI_HEADER_TYPE_CARDBUS:
34 pci_read_config_byte ( pci, PCI_CB_CAPABILITY_LIST, &pos );
37 while ( ttl-- && pos >= 0x40 ) {
39 pci_read_config_byte ( pci, pos + PCI_CAP_LIST_ID, &id );
40 DBG ( "PCI Capability: %d\n", id );
45 pci_read_config_byte ( pci, pos + PCI_CAP_LIST_NEXT, &pos );
51 * Find the size of a PCI BAR
54 * @v reg PCI register number
57 * It should not be necessary for any Etherboot code to call this
60 unsigned long pci_bar_size ( struct pci_device *pci, unsigned int reg ) {
63 /* Save the original bar */
64 pci_read_config_dword ( pci, reg, &start );
65 /* Compute which bits can be set */
66 pci_write_config_dword ( pci, reg, ~0 );
67 pci_read_config_dword ( pci, reg, &size );
68 /* Restore the original size */
69 pci_write_config_dword ( pci, reg, start );
70 /* Find the significant bits */
71 if ( start & PCI_BASE_ADDRESS_SPACE_IO ) {
72 size &= PCI_BASE_ADDRESS_IO_MASK;
74 size &= PCI_BASE_ADDRESS_MEM_MASK;
76 /* Find the lowest bit set */
77 size = size & ~( size - 1 );