xref: /aosp_15_r20/external/mesa3d/src/intel/common/intel_gem.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "intel_gem.h"
24 
25 #include "i915/intel_engine.h"
26 #include "i915/intel_gem.h"
27 #include "xe/intel_gem.h"
28 
29 #include "util/os_time.h"
30 
31 bool
intel_gem_supports_syncobj_wait(int fd)32 intel_gem_supports_syncobj_wait(int fd)
33 {
34    int ret;
35 
36    struct drm_syncobj_create create = {
37       .flags = 0,
38    };
39    ret = intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &create);
40    if (ret)
41       return false;
42 
43    uint32_t syncobj = create.handle;
44 
45    struct drm_syncobj_wait wait = {
46       .handles = (uint64_t)(uintptr_t)&create,
47       .count_handles = 1,
48       .timeout_nsec = 0,
49       .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
50    };
51    ret = intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &wait);
52 
53    struct drm_syncobj_destroy destroy = {
54       .handle = syncobj,
55    };
56    intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy);
57 
58    /* If it timed out, then we have the ioctl and it supports the
59     * DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT flag.
60     */
61    return ret == -1 && errno == ETIME;
62 }
63 
64 bool
intel_gem_create_context(int fd,uint32_t * context_id)65 intel_gem_create_context(int fd, uint32_t *context_id)
66 {
67    return i915_gem_create_context(fd, context_id);
68 }
69 
70 bool
intel_gem_destroy_context(int fd,uint32_t context_id)71 intel_gem_destroy_context(int fd, uint32_t context_id)
72 {
73    return i915_gem_destroy_context(fd, context_id);
74 }
75 
76 bool
intel_gem_create_context_engines(int fd,enum intel_gem_create_context_flags flags,const struct intel_query_engine_info * info,int num_engines,enum intel_engine_class * engine_classes,uint32_t vm_id,uint32_t * context_id)77 intel_gem_create_context_engines(int fd,
78                                  enum intel_gem_create_context_flags flags,
79                                  const struct intel_query_engine_info *info,
80                                  int num_engines, enum intel_engine_class *engine_classes,
81                                  uint32_t vm_id,
82                                  uint32_t *context_id)
83 {
84    return i915_gem_create_context_engines(fd, flags, info, num_engines,
85                                           engine_classes, vm_id, context_id);
86 }
87 
88 bool
intel_gem_set_context_param(int fd,uint32_t context,uint32_t param,uint64_t value)89 intel_gem_set_context_param(int fd, uint32_t context, uint32_t param,
90                             uint64_t value)
91 {
92    return i915_gem_set_context_param(fd, context, param, value);
93 }
94 
95 bool
intel_gem_get_context_param(int fd,uint32_t context,uint32_t param,uint64_t * value)96 intel_gem_get_context_param(int fd, uint32_t context, uint32_t param,
97                             uint64_t *value)
98 {
99    return i915_gem_get_context_param(fd, context, param, value);
100 }
101 
102 bool
intel_gem_read_render_timestamp(int fd,enum intel_kmd_type kmd_type,uint64_t * value)103 intel_gem_read_render_timestamp(int fd,
104                                 enum intel_kmd_type kmd_type,
105                                 uint64_t *value)
106 {
107    switch (kmd_type) {
108    case INTEL_KMD_TYPE_I915:
109       return i915_gem_read_render_timestamp(fd, value);
110    case INTEL_KMD_TYPE_XE:
111       return xe_gem_read_render_timestamp(fd, value);
112    default:
113       unreachable("Missing");
114       return false;
115    }
116 }
117 
118 bool
intel_gem_read_correlate_cpu_gpu_timestamp(int fd,enum intel_kmd_type kmd_type,enum intel_engine_class engine_class,uint16_t engine_instance,clockid_t cpu_clock_id,uint64_t * cpu_timestamp,uint64_t * gpu_timestamp,uint64_t * cpu_delta)119 intel_gem_read_correlate_cpu_gpu_timestamp(int fd,
120                                            enum intel_kmd_type kmd_type,
121                                            enum intel_engine_class engine_class,
122                                            uint16_t engine_instance,
123                                            clockid_t cpu_clock_id,
124                                            uint64_t *cpu_timestamp,
125                                            uint64_t *gpu_timestamp,
126                                            uint64_t *cpu_delta)
127 {
128    switch (kmd_type) {
129    case INTEL_KMD_TYPE_I915:
130       return false;
131    case INTEL_KMD_TYPE_XE:
132       return xe_gem_read_correlate_cpu_gpu_timestamp(fd, engine_class,
133                                                      engine_instance,
134                                                      cpu_clock_id,
135                                                      cpu_timestamp,
136                                                      gpu_timestamp,
137                                                      cpu_delta);
138    default:
139       unreachable("Missing");
140       return false;
141    }
142 }
143 
144 bool
intel_gem_create_context_ext(int fd,enum intel_gem_create_context_flags flags,uint32_t * ctx_id)145 intel_gem_create_context_ext(int fd, enum intel_gem_create_context_flags flags,
146                              uint32_t *ctx_id)
147 {
148    return i915_gem_create_context_ext(fd, flags, ctx_id);
149 }
150 
151 bool
intel_gem_supports_protected_context(int fd,enum intel_kmd_type kmd_type)152 intel_gem_supports_protected_context(int fd, enum intel_kmd_type kmd_type)
153 {
154    switch (kmd_type) {
155    case INTEL_KMD_TYPE_I915:
156       return i915_gem_supports_protected_context(fd);
157    case INTEL_KMD_TYPE_XE:
158       /* TODO: so far Xe don't have support for protected contexts/engines */
159       return false;
160    default:
161       unreachable("Missing");
162       return false;
163    }
164 }
165 
166 bool
intel_gem_wait_on_get_param(int fd,uint32_t param,int target_val,uint32_t timeout_ms)167 intel_gem_wait_on_get_param(int fd, uint32_t param, int target_val,
168                             uint32_t timeout_ms)
169 {
170    int64_t start_time = os_time_get();
171    int64_t end_time = start_time + (timeout_ms * 1000);
172    int val = -1;
173 
174    errno = 0;
175    do {
176       if (!intel_gem_get_param(fd, param, &val))
177          break;
178    } while (val != target_val && !os_time_timeout(start_time, end_time, os_time_get()));
179 
180    if (errno || val != target_val)
181       return false;
182 
183    return true;
184 }
185 
186 bool
intel_gem_get_param(int fd,uint32_t param,int * value)187 intel_gem_get_param(int fd, uint32_t param, int *value)
188 {
189    return i915_gem_get_param(fd, param, value);
190 }
191 
192 bool
intel_gem_can_render_on_fd(int fd,enum intel_kmd_type kmd_type)193 intel_gem_can_render_on_fd(int fd, enum intel_kmd_type kmd_type)
194 {
195    switch (kmd_type) {
196    case INTEL_KMD_TYPE_I915:
197       return i915_gem_can_render_on_fd(fd);
198    case INTEL_KMD_TYPE_XE:
199       return xe_gem_can_render_on_fd(fd);
200    default:
201       unreachable("Missing");
202       return false;
203    }
204 }
205