summaryrefslogtreecommitdiffstats
path: root/tools/pci.hpp
blob: 37cd736d746ebd9866831b89b650dd65076c5bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once

extern "C" {
#include <pci/pci.h>
} // extern "C"

#include <optional>
#include <vector>

namespace host_tool
{

/* The ASPEED AST2400 & AST2500 have the same VIDDID. */

/**
 * The PciDevice structure is a copy of the information to uniquely identify a
 * PCI device.
 */
struct PciDevice
{
    std::uint16_t vid;
    std::uint16_t did;
    std::uint8_t bus;
    std::uint8_t dev;
    std::uint8_t func;
    pciaddr_t bars[6];
};

/**
 * The PciFilter structure is a simple mechanism for filtering devices by their
 * vendor and/or device ids.
 */
struct PciFilter
{
    std::uint16_t vid;
    std::uint16_t did;
};

class PciUtilInterface
{
  public:
    virtual ~PciUtilInterface() = default;

    /**
     * Get a list of PCI devices from a system.
     *
     * @param[in] filter - optional filter for the list.
     * @return the list of devices.
     */
    virtual std::vector<PciDevice>
        getPciDevices(std::optional<PciFilter> filter = std::nullopt) = 0;
};

class PciUtilImpl : public PciUtilInterface
{
  public:
    PciUtilImpl()
    {
        pacc = pci_alloc();
        pci_init(pacc);
    }
    ~PciUtilImpl()
    {
        pci_cleanup(pacc);
    }

    std::vector<PciDevice>
        getPciDevices(std::optional<PciFilter> filter = std::nullopt) override;

  private:
    struct pci_access* pacc;
};

} // namespace host_tool
OpenPOWER on IntegriCloud