xref: /openbmc/phosphor-ipmi-flash/tools/pci.hpp (revision 9b37b095)
1 #pragma once
2 
3 extern "C"
4 {
5 #include <pci/pci.h>
6 } // extern "C"
7 
8 #include <cstdint>
9 #include <optional>
10 #include <vector>
11 
12 namespace 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  */
21 struct PciDevice
22 {
23     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];
29 };
30 
31 /**
32  * The PciFilter structure is a simple mechanism for filtering devices by their
33  * vendor and/or device ids.
34  */
35 struct PciFilter
36 {
37     std::uint16_t vid;
38     std::uint16_t did;
39 };
40 
41 class 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 
56 class 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
77