blob: f421892a16ae173b2924387bcc6e080673c5cf42 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001#pragma once
2
Benjamin Fairc04c2c52020-06-05 09:14:44 -07003#include "internal/sys.hpp"
4
Patrick Venture9b37b092020-05-28 20:58:57 -07005extern "C"
6{
Benjamin Fairc04c2c52020-06-05 09:14:44 -07007#include <pciaccess.h>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07008} // extern "C"
9
Benjamin Fairc04c2c52020-06-05 09:14:44 -070010#include <linux/pci_regs.h>
11
Patrick Venture9b37b092020-05-28 20:58:57 -070012#include <cstdint>
Benjamin Fairc04c2c52020-06-05 09:14:44 -070013#include <memory>
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070014#include <optional>
15#include <vector>
16
Benjamin Fairc04c2c52020-06-05 09:14:44 -070017#ifndef PCI_STD_NUM_BARS
18#define PCI_STD_NUM_BARS 6
19#endif // !PCI_STD_NUM_BARS
20
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070021namespace 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 */
30struct PciDevice
31{
Patrick Venture24141612019-05-03 17:59:18 -070032 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 Fairc04c2c52020-06-05 09:14:44 -070037 pciaddr_t bars[PCI_STD_NUM_BARS];
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070038};
39
40/**
41 * The PciFilter structure is a simple mechanism for filtering devices by their
42 * vendor and/or device ids.
43 */
44struct PciFilter
45{
Patrick Venture24141612019-05-03 17:59:18 -070046 std::uint16_t vid;
47 std::uint16_t did;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070048};
49
50class 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
65class PciUtilImpl : public PciUtilInterface
66{
67 public:
Benjamin Fairc04c2c52020-06-05 09:14:44 -070068 static PciUtilImpl& getInstance()
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070069 {
Benjamin Fairc04c2c52020-06-05 09:14:44 -070070 static PciUtilImpl instance;
71 return instance;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070072 }
73
74 std::vector<PciDevice>
75 getPciDevices(std::optional<PciFilter> filter = std::nullopt) override;
76
Benjamin Fairc04c2c52020-06-05 09:14:44 -070077 PciUtilImpl(const PciUtilImpl&) = delete;
78 PciUtilImpl& operator=(const PciUtilImpl&) = delete;
79
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070080 private:
Benjamin Fairc04c2c52020-06-05 09:14:44 -070081 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 Ventureb5bf0fc2019-05-03 14:33:49 -070094};
95
96} // namespace host_tool