1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
12bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#include "intel_gem.h"
24bf215546Sopenharmony_ci#include "drm-uapi/i915_drm.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cibool
27bf215546Sopenharmony_ciintel_gem_supports_syncobj_wait(int fd)
28bf215546Sopenharmony_ci{
29bf215546Sopenharmony_ci   int ret;
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci   struct drm_syncobj_create create = {
32bf215546Sopenharmony_ci      .flags = 0,
33bf215546Sopenharmony_ci   };
34bf215546Sopenharmony_ci   ret = intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &create);
35bf215546Sopenharmony_ci   if (ret)
36bf215546Sopenharmony_ci      return false;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   uint32_t syncobj = create.handle;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   struct drm_syncobj_wait wait = {
41bf215546Sopenharmony_ci      .handles = (uint64_t)(uintptr_t)&create,
42bf215546Sopenharmony_ci      .count_handles = 1,
43bf215546Sopenharmony_ci      .timeout_nsec = 0,
44bf215546Sopenharmony_ci      .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT,
45bf215546Sopenharmony_ci   };
46bf215546Sopenharmony_ci   ret = intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &wait);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   struct drm_syncobj_destroy destroy = {
49bf215546Sopenharmony_ci      .handle = syncobj,
50bf215546Sopenharmony_ci   };
51bf215546Sopenharmony_ci   intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy);
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   /* If it timed out, then we have the ioctl and it supports the
54bf215546Sopenharmony_ci    * DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT flag.
55bf215546Sopenharmony_ci    */
56bf215546Sopenharmony_ci   return ret == -1 && errno == ETIME;
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ciint
60bf215546Sopenharmony_ciintel_gem_count_engines(const struct drm_i915_query_engine_info *info,
61bf215546Sopenharmony_ci                        enum drm_i915_gem_engine_class engine_class)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci   assert(info != NULL);
64bf215546Sopenharmony_ci   int count = 0;
65bf215546Sopenharmony_ci   for (int i = 0; i < info->num_engines; i++) {
66bf215546Sopenharmony_ci      if (info->engines[i].engine.engine_class == engine_class)
67bf215546Sopenharmony_ci         count++;
68bf215546Sopenharmony_ci   }
69bf215546Sopenharmony_ci   return count;
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ciint
73bf215546Sopenharmony_ciintel_gem_create_context_engines(int fd,
74bf215546Sopenharmony_ci                                 const struct drm_i915_query_engine_info *info,
75bf215546Sopenharmony_ci                                 int num_engines, uint16_t *engine_classes)
76bf215546Sopenharmony_ci{
77bf215546Sopenharmony_ci   assert(info != NULL);
78bf215546Sopenharmony_ci   const size_t engine_inst_sz = 2 * sizeof(__u16); /* 1 class, 1 instance */
79bf215546Sopenharmony_ci   const size_t engines_param_size =
80bf215546Sopenharmony_ci      sizeof(__u64) /* extensions */ + num_engines * engine_inst_sz;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   void *engines_param = malloc(engines_param_size);
83bf215546Sopenharmony_ci   assert(engines_param);
84bf215546Sopenharmony_ci   *(__u64*)engines_param = 0;
85bf215546Sopenharmony_ci   __u16 *class_inst_ptr = (__u16*)(((__u64*)engines_param) + 1);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   /* For each type of drm_i915_gem_engine_class of interest, we keep track of
88bf215546Sopenharmony_ci    * the previous engine instance used.
89bf215546Sopenharmony_ci    */
90bf215546Sopenharmony_ci   int last_engine_idx[] = {
91bf215546Sopenharmony_ci      [I915_ENGINE_CLASS_RENDER] = -1,
92bf215546Sopenharmony_ci      [I915_ENGINE_CLASS_COPY] = -1,
93bf215546Sopenharmony_ci      [I915_ENGINE_CLASS_COMPUTE] = -1,
94bf215546Sopenharmony_ci   };
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   int i915_engine_counts[] = {
97bf215546Sopenharmony_ci      [I915_ENGINE_CLASS_RENDER] =
98bf215546Sopenharmony_ci         intel_gem_count_engines(info, I915_ENGINE_CLASS_RENDER),
99bf215546Sopenharmony_ci      [I915_ENGINE_CLASS_COPY] =
100bf215546Sopenharmony_ci         intel_gem_count_engines(info, I915_ENGINE_CLASS_COPY),
101bf215546Sopenharmony_ci      [I915_ENGINE_CLASS_COMPUTE] =
102bf215546Sopenharmony_ci         intel_gem_count_engines(info, I915_ENGINE_CLASS_COMPUTE),
103bf215546Sopenharmony_ci   };
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   /* For each queue, we look for the next instance that matches the class we
106bf215546Sopenharmony_ci    * need.
107bf215546Sopenharmony_ci    */
108bf215546Sopenharmony_ci   for (int i = 0; i < num_engines; i++) {
109bf215546Sopenharmony_ci      uint16_t engine_class = engine_classes[i];
110bf215546Sopenharmony_ci      assert(engine_class == I915_ENGINE_CLASS_RENDER ||
111bf215546Sopenharmony_ci             engine_class == I915_ENGINE_CLASS_COPY ||
112bf215546Sopenharmony_ci             engine_class == I915_ENGINE_CLASS_COMPUTE);
113bf215546Sopenharmony_ci      if (i915_engine_counts[engine_class] <= 0) {
114bf215546Sopenharmony_ci         free(engines_param);
115bf215546Sopenharmony_ci         return -1;
116bf215546Sopenharmony_ci      }
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci      /* Run through the engines reported by the kernel looking for the next
119bf215546Sopenharmony_ci       * matching instance. We loop in case we want to create multiple
120bf215546Sopenharmony_ci       * contexts on an engine instance.
121bf215546Sopenharmony_ci       */
122bf215546Sopenharmony_ci      int engine_instance = -1;
123bf215546Sopenharmony_ci      for (int i = 0; i < info->num_engines; i++) {
124bf215546Sopenharmony_ci         int *idx = &last_engine_idx[engine_class];
125bf215546Sopenharmony_ci         if (++(*idx) >= info->num_engines)
126bf215546Sopenharmony_ci            *idx = 0;
127bf215546Sopenharmony_ci         if (info->engines[*idx].engine.engine_class == engine_class) {
128bf215546Sopenharmony_ci            engine_instance = info->engines[*idx].engine.engine_instance;
129bf215546Sopenharmony_ci            break;
130bf215546Sopenharmony_ci         }
131bf215546Sopenharmony_ci      }
132bf215546Sopenharmony_ci      if (engine_instance < 0) {
133bf215546Sopenharmony_ci         free(engines_param);
134bf215546Sopenharmony_ci         return -1;
135bf215546Sopenharmony_ci      }
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci      *class_inst_ptr++ = engine_class;
138bf215546Sopenharmony_ci      *class_inst_ptr++ = engine_instance;
139bf215546Sopenharmony_ci   }
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   assert((uintptr_t)engines_param + engines_param_size ==
142bf215546Sopenharmony_ci          (uintptr_t)class_inst_ptr);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   struct drm_i915_gem_context_create_ext_setparam set_engines = {
145bf215546Sopenharmony_ci      .base = {
146bf215546Sopenharmony_ci         .name = I915_CONTEXT_CREATE_EXT_SETPARAM,
147bf215546Sopenharmony_ci      },
148bf215546Sopenharmony_ci      .param = {
149bf215546Sopenharmony_ci         .param = I915_CONTEXT_PARAM_ENGINES,
150bf215546Sopenharmony_ci         .value = (uintptr_t)engines_param,
151bf215546Sopenharmony_ci         .size = engines_param_size,
152bf215546Sopenharmony_ci      }
153bf215546Sopenharmony_ci   };
154bf215546Sopenharmony_ci   struct drm_i915_gem_context_create_ext create = {
155bf215546Sopenharmony_ci      .flags = I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS,
156bf215546Sopenharmony_ci      .extensions = (uintptr_t)&set_engines,
157bf215546Sopenharmony_ci   };
158bf215546Sopenharmony_ci   int ret = intel_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, &create);
159bf215546Sopenharmony_ci   free(engines_param);
160bf215546Sopenharmony_ci   if (ret == -1)
161bf215546Sopenharmony_ci      return -1;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   return create.ctx_id;
164bf215546Sopenharmony_ci}
165