summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/gpu_info_util/SystemInfo_libpci.cpp
blob: 07c72872add3f30aba620f8bfe8d731ea1df4676 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Copyright (c) 2013-2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// SystemInfo_libpci.cpp: implementation of the libPCI-specific parts of SystemInfo.h

#include "gpu_info_util/SystemInfo_internal.h"

#include <dlfcn.h>
#include <pci/pci.h>
#include <unistd.h>

#include "common/angleutils.h"
#include "common/debug.h"

#if !defined(GPU_INFO_USE_LIBPCI)
#error SystemInfo_libpci.cpp compiled without GPU_INFO_USE_LIBPCI
#endif

namespace angle
{

namespace
{

struct LibPCI : private angle::NonCopyable
{
    LibPCI()
    {
        if (access("/sys/bus/pci/", F_OK) != 0 && access("/sys/bs/pci_express/", F_OK) != 0)
        {
            return;
        }

        mHandle = dlopen("libpci.so.3", RTLD_LAZY);

        if (mHandle == nullptr)
        {
            mHandle = dlopen("libpci.so", RTLD_LAZY);
        }

        if (mHandle == nullptr)
        {
            return;
        }

        mValid =
            (Alloc = reinterpret_cast<decltype(Alloc)>(dlsym(mHandle, "pci_alloc"))) != nullptr &&
            (Init = reinterpret_cast<decltype(Init)>(dlsym(mHandle, "pci_init"))) != nullptr &&
            (Cleanup = reinterpret_cast<decltype(Cleanup)>(dlsym(mHandle, "pci_cleanup"))) !=
                nullptr &&
            (ScanBus = reinterpret_cast<decltype(ScanBus)>(dlsym(mHandle, "pci_scan_bus"))) !=
                nullptr &&
            (FillInfo = reinterpret_cast<decltype(FillInfo)>(dlsym(mHandle, "pci_fill_info"))) !=
                nullptr &&
            (LookupName = reinterpret_cast<decltype(LookupName)>(
                 dlsym(mHandle, "pci_lookup_name"))) != nullptr;
    }

    bool IsValid() const { return mValid; }

    ~LibPCI()
    {
        if (mHandle != nullptr)
        {
            dlclose(mHandle);
        }
    }

    decltype(&::pci_alloc) Alloc            = nullptr;
    decltype(&::pci_init) Init              = nullptr;
    decltype(&::pci_cleanup) Cleanup        = nullptr;
    decltype(&::pci_scan_bus) ScanBus       = nullptr;
    decltype(&::pci_fill_info) FillInfo     = nullptr;
    decltype(&::pci_lookup_name) LookupName = nullptr;

  private:
    void *mHandle = nullptr;
    bool mValid   = false;
};

}  // anonymous namespace

// Adds an entry per PCI GPU found and fills the device and vendor ID.
bool GetPCIDevicesWithLibPCI(std::vector<GPUDeviceInfo> *devices)
{
    LibPCI pci;
    if (!pci.IsValid())
    {
        return false;
    }

    pci_access *access = pci.Alloc();
    ASSERT(access != nullptr);
    pci.Init(access);
    pci.ScanBus(access);

    for (pci_dev *device = access->devices; device != nullptr; device = device->next)
    {
        pci.FillInfo(device, PCI_FILL_IDENT | PCI_FILL_CLASS);

        // Skip non-GPU devices
        switch (device->device_class)
        {
            case PCI_CLASS_DISPLAY_VGA:
            case PCI_CLASS_DISPLAY_XGA:
            case PCI_CLASS_DISPLAY_3D:
                break;
            default:
                continue;
        }

        // Skip unknown devices
        if (device->vendor_id == 0 || device->device_id == 0)
        {
            continue;
        }

        GPUDeviceInfo info;
        info.vendorId = device->vendor_id;
        info.deviceId = device->device_id;

        devices->push_back(info);
    }

    pci.Cleanup(access);

    return true;
}
}