forked from notaz/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintel_gem.c
154 lines (138 loc) · 5.08 KB
/
intel_gem.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright © 2020 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "intel_gem.h"
#include "drm-uapi/i915_drm.h"
bool
intel_gem_supports_syncobj_wait(int fd)
{
int ret;
struct drm_syncobj_create create = {
.flags = 0,
};
ret = intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &create);
if (ret)
return false;
uint32_t syncobj = create.handle;
struct drm_syncobj_wait wait = {
.handles = (uint64_t)(uintptr_t)&create,
.count_handles = 1,
.timeout_nsec = 0,
.flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
};
ret = intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &wait);
struct drm_syncobj_destroy destroy = {
.handle = syncobj,
};
intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy);
/* If it timed out, then we have the ioctl and it supports the
* DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT flag.
*/
return ret == -1 && errno == ETIME;
}
int
intel_gem_count_engines(const struct drm_i915_query_engine_info *info,
enum drm_i915_gem_engine_class engine_class)
{
assert(info != NULL);
int count = 0;
for (int i = 0; i < info->num_engines; i++) {
if (info->engines[i].engine.engine_class == engine_class)
count++;
}
return count;
}
int
intel_gem_create_context_engines(int fd,
const struct drm_i915_query_engine_info *info,
int num_engines, uint16_t *engine_classes)
{
assert(info != NULL);
assert(num_engines <= 64);
I915_DEFINE_CONTEXT_PARAM_ENGINES(engines_param, 64);
engines_param.extensions = 0;
/* For each type of drm_i915_gem_engine_class of interest, we keep track of
* the previous engine instance used.
*/
int last_engine_idx[] = {
[I915_ENGINE_CLASS_RENDER] = -1,
[I915_ENGINE_CLASS_COPY] = -1,
[I915_ENGINE_CLASS_COMPUTE] = -1,
};
int i915_engine_counts[] = {
[I915_ENGINE_CLASS_RENDER] =
intel_gem_count_engines(info, I915_ENGINE_CLASS_RENDER),
[I915_ENGINE_CLASS_COPY] =
intel_gem_count_engines(info, I915_ENGINE_CLASS_COPY),
[I915_ENGINE_CLASS_COMPUTE] =
intel_gem_count_engines(info, I915_ENGINE_CLASS_COMPUTE),
};
/* For each queue, we look for the next instance that matches the class we
* need.
*/
for (int i = 0; i < num_engines; i++) {
uint16_t engine_class = engine_classes[i];
assert(engine_class == I915_ENGINE_CLASS_RENDER ||
engine_class == I915_ENGINE_CLASS_COPY ||
engine_class == I915_ENGINE_CLASS_COMPUTE);
if (i915_engine_counts[engine_class] <= 0) {
return -1;
}
/* Run through the engines reported by the kernel looking for the next
* matching instance. We loop in case we want to create multiple
* contexts on an engine instance.
*/
int engine_instance = -1;
for (int i = 0; i < info->num_engines; i++) {
int *idx = &last_engine_idx[engine_class];
if (++(*idx) >= info->num_engines)
*idx = 0;
if (info->engines[*idx].engine.engine_class == engine_class) {
engine_instance = info->engines[*idx].engine.engine_instance;
break;
}
}
if (engine_instance < 0) {
return -1;
}
engines_param.engines[i].engine_class = engine_class;
engines_param.engines[i].engine_instance = engine_instance;
}
uint32_t size = sizeof(engines_param.extensions);
size += sizeof(engines_param.engines[0]) * num_engines;
struct drm_i915_gem_context_create_ext_setparam set_engines = {
.base = {
.name = I915_CONTEXT_CREATE_EXT_SETPARAM,
},
.param = {
.param = I915_CONTEXT_PARAM_ENGINES,
.value = (uintptr_t)&engines_param,
.size = size,
}
};
struct drm_i915_gem_context_create_ext create = {
.flags = I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS,
.extensions = (uintptr_t)&set_engines,
};
if (intel_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &create) == -1)
return -1;
return create.ctx_id;
}