Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 3 | extern "C" |
| 4 | { |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 5 | #include <pci/pci.h> |
| 6 | } // extern "C" |
| 7 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 8 | #include <cstdint> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 9 | #include <optional> |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace host_tool |
| 13 | { |
| 14 | |
| 15 | /* The ASPEED AST2400 & AST2500 have the same VIDDID. */ |
| 16 | |
| 17 | /** |
| 18 | * The PciDevice structure is a copy of the information to uniquely identify a |
| 19 | * PCI device. |
| 20 | */ |
| 21 | struct PciDevice |
| 22 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 23 | std::uint16_t vid; |
| 24 | std::uint16_t did; |
| 25 | std::uint8_t bus; |
| 26 | std::uint8_t dev; |
| 27 | std::uint8_t func; |
| 28 | pciaddr_t bars[6]; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * The PciFilter structure is a simple mechanism for filtering devices by their |
| 33 | * vendor and/or device ids. |
| 34 | */ |
| 35 | struct PciFilter |
| 36 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 37 | std::uint16_t vid; |
| 38 | std::uint16_t did; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class PciUtilInterface |
| 42 | { |
| 43 | public: |
| 44 | virtual ~PciUtilInterface() = default; |
| 45 | |
| 46 | /** |
| 47 | * Get a list of PCI devices from a system. |
| 48 | * |
| 49 | * @param[in] filter - optional filter for the list. |
| 50 | * @return the list of devices. |
| 51 | */ |
| 52 | virtual std::vector<PciDevice> |
| 53 | getPciDevices(std::optional<PciFilter> filter = std::nullopt) = 0; |
| 54 | }; |
| 55 | |
| 56 | class PciUtilImpl : public PciUtilInterface |
| 57 | { |
| 58 | public: |
| 59 | PciUtilImpl() |
| 60 | { |
| 61 | pacc = pci_alloc(); |
| 62 | pci_init(pacc); |
| 63 | } |
| 64 | ~PciUtilImpl() |
| 65 | { |
| 66 | pci_cleanup(pacc); |
| 67 | } |
| 68 | |
| 69 | std::vector<PciDevice> |
| 70 | getPciDevices(std::optional<PciFilter> filter = std::nullopt) override; |
| 71 | |
| 72 | private: |
| 73 | struct pci_access* pacc; |
| 74 | }; |
| 75 | |
| 76 | } // namespace host_tool |