blob: 37cd736d746ebd9866831b89b650dd65076c5bd8 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001#pragma once
2
3extern "C" {
4#include <pci/pci.h>
5} // extern "C"
6
7#include <optional>
8#include <vector>
9
10namespace 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 */
19struct PciDevice
20{
Patrick Venture24141612019-05-03 17:59:18 -070021 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 Ventureb5bf0fc2019-05-03 14:33:49 -070027};
28
29/**
30 * The PciFilter structure is a simple mechanism for filtering devices by their
31 * vendor and/or device ids.
32 */
33struct PciFilter
34{
Patrick Venture24141612019-05-03 17:59:18 -070035 std::uint16_t vid;
36 std::uint16_t did;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070037};
38
39class 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
54class 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