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