forked from notaz/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpps_device.cc
141 lines (115 loc) · 3.15 KB
/
pps_device.cc
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
133
134
135
136
137
138
139
140
141
/*
* Copyright © 2020 Collabora, Ltd.
* Author: Antonio Caggiano <[email protected]>
* Author: Rohan Garg <[email protected]>
* Author: Robert Beckett <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
#include "pps_device.h"
#include <cassert>
#include <fcntl.h>
#include <memory>
#include <unistd.h>
#include <xf86drm.h>
namespace pps
{
#define MAX_DRM_DEVICES 64
uint32_t DrmDevice::device_count()
{
drmDevicePtr devices[MAX_DRM_DEVICES] = {};
int num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
drmFreeDevices(devices, num_devices);
return static_cast<uint32_t>(num_devices);
}
/// @return The name of a DRM device, empty string in case of error
std::string query_drm_name(const int fd)
{
assert(fd && "Failed to query DrmDevice: invalid fd");
std::string name = "";
if (drmVersionPtr version = drmGetVersion(fd)) {
name = std::string(version->name, version->name_len);
drmFreeVersion(version);
}
return name;
}
/// @return A DRM device, nullopt in case of error
std::optional<DrmDevice> create_drm_device(int fd, int32_t gpu_num)
{
if (fd < 0 || gpu_num < 0) {
return std::nullopt;
}
// Try getting the name
std::string name = query_drm_name(fd);
if (name.empty()) {
return std::nullopt;
}
auto ret = DrmDevice();
ret.fd = fd;
ret.gpu_num = gpu_num;
ret.name = name;
return ret;
}
std::vector<DrmDevice> DrmDevice::create_all()
{
std::vector<DrmDevice> ret = {};
drmDevicePtr devices[MAX_DRM_DEVICES] = {};
int num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
if (num_devices <= 0) {
return ret;
}
for (int32_t gpu_num = 0; gpu_num < num_devices; gpu_num++) {
drmDevicePtr device = devices[gpu_num];
if ((device->available_nodes & (1 << DRM_NODE_RENDER))) {
int fd = open(device->nodes[DRM_NODE_RENDER], O_RDWR);
// If it can create a device, push it into the vector
if (auto drm_device = create_drm_device(fd, gpu_num)) {
ret.emplace_back(std::move(drm_device.value()));
}
}
}
drmFreeDevices(devices, num_devices);
return ret;
}
std::optional<DrmDevice> DrmDevice::create(int32_t gpu_num)
{
std::optional<DrmDevice> ret = std::nullopt;
if (gpu_num < 0) {
return ret;
}
drmDevicePtr devices[MAX_DRM_DEVICES] = {};
int num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES);
if (num_devices > 0 && gpu_num < num_devices) {
drmDevicePtr device = devices[gpu_num];
int fd = open(device->nodes[DRM_NODE_RENDER], O_RDWR);
ret = create_drm_device(fd, gpu_num);
}
drmFreeDevices(devices, num_devices);
return ret;
}
DrmDevice::DrmDevice(DrmDevice &&other)
: fd {other.fd}
, gpu_num {other.gpu_num}
, name {std::move(other.name)}
{
other.fd = -1;
other.gpu_num = -1;
}
DrmDevice &DrmDevice::operator=(DrmDevice &&other)
{
std::swap(fd, other.fd);
std::swap(gpu_num, other.gpu_num);
std::swap(name, other.name);
return *this;
}
DrmDevice::~DrmDevice()
{
if (fd >= 0) {
close(fd);
}
}
DrmDevice::operator bool() const
{
return !name.empty();
}
} // namespace pps