blob: 0edc531be61b53d527f00d185caeb44f638d2194 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001#pragma once
2
Patrick Venture9b37b092020-05-28 20:58:57 -07003extern "C"
4{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07005#include <pci/pci.h>
6} // extern "C"
7
Patrick Venture9b37b092020-05-28 20:58:57 -07008#include <cstdint>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07009#include <optional>
10#include <vector>
11
12namespace 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 */
21struct PciDevice
22{
Patrick Venture24141612019-05-03 17:59:18 -070023 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 Ventureb5bf0fc2019-05-03 14:33:49 -070029};
30
31/**
32 * The PciFilter structure is a simple mechanism for filtering devices by their
33 * vendor and/or device ids.
34 */
35struct PciFilter
36{
Patrick Venture24141612019-05-03 17:59:18 -070037 std::uint16_t vid;
38 std::uint16_t did;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070039};
40
41class 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
56class 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