Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 3 | #include "internal/sys.hpp" |
| 4 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 5 | extern "C" |
| 6 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 7 | #include <pciaccess.h> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 8 | } // extern "C" |
| 9 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 10 | #include <linux/pci_regs.h> |
| 11 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 12 | #include <cstdint> |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 13 | #include <memory> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 14 | #include <optional> |
| 15 | #include <vector> |
| 16 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 17 | #ifndef PCI_STD_NUM_BARS |
| 18 | #define PCI_STD_NUM_BARS 6 |
| 19 | #endif // !PCI_STD_NUM_BARS |
| 20 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 21 | namespace host_tool |
| 22 | { |
| 23 | |
| 24 | /* The ASPEED AST2400 & AST2500 have the same VIDDID. */ |
| 25 | |
| 26 | /** |
| 27 | * The PciDevice structure is a copy of the information to uniquely identify a |
| 28 | * PCI device. |
| 29 | */ |
| 30 | struct PciDevice |
| 31 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 32 | std::uint16_t vid; |
| 33 | std::uint16_t did; |
| 34 | std::uint8_t bus; |
| 35 | std::uint8_t dev; |
| 36 | std::uint8_t func; |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 37 | pciaddr_t bars[PCI_STD_NUM_BARS]; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * The PciFilter structure is a simple mechanism for filtering devices by their |
| 42 | * vendor and/or device ids. |
| 43 | */ |
| 44 | struct PciFilter |
| 45 | { |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 46 | std::uint16_t vid; |
| 47 | std::uint16_t did; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | class PciUtilInterface |
| 51 | { |
| 52 | public: |
| 53 | virtual ~PciUtilInterface() = default; |
| 54 | |
| 55 | /** |
| 56 | * Get a list of PCI devices from a system. |
| 57 | * |
| 58 | * @param[in] filter - optional filter for the list. |
| 59 | * @return the list of devices. |
| 60 | */ |
| 61 | virtual std::vector<PciDevice> |
| 62 | getPciDevices(std::optional<PciFilter> filter = std::nullopt) = 0; |
| 63 | }; |
| 64 | |
| 65 | class PciUtilImpl : public PciUtilInterface |
| 66 | { |
| 67 | public: |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 68 | static PciUtilImpl& getInstance() |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 69 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 70 | static PciUtilImpl instance; |
| 71 | return instance; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | std::vector<PciDevice> |
| 75 | getPciDevices(std::optional<PciFilter> filter = std::nullopt) override; |
| 76 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 77 | PciUtilImpl(const PciUtilImpl&) = delete; |
| 78 | PciUtilImpl& operator=(const PciUtilImpl&) = delete; |
| 79 | |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 80 | private: |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 81 | PciUtilImpl() |
| 82 | { |
| 83 | int ret = pci_system_init(); |
| 84 | if (ret) |
| 85 | { |
| 86 | throw internal::errnoException("pci_system_init"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | ~PciUtilImpl() |
| 91 | { |
| 92 | pci_system_cleanup(); |
| 93 | } |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace host_tool |