~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/intel/vulkan/anv_gem.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2015 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 (including the next
12
 
 * paragraph) shall be included in all copies or substantial portions of the
13
 
 * Software.
14
 
 *
15
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 
 * IN THE SOFTWARE.
22
 
 */
23
 
 
24
 
#include <sys/ioctl.h>
25
 
#include <sys/types.h>
26
 
#include <sys/mman.h>
27
 
#include <string.h>
28
 
#include <errno.h>
29
 
#include <unistd.h>
30
 
#include <fcntl.h>
31
 
 
32
 
#include "anv_private.h"
33
 
#include "common/intel_defines.h"
34
 
#include "common/intel_gem.h"
35
 
 
36
 
/**
37
 
 * Wrapper around DRM_IOCTL_I915_GEM_CREATE.
38
 
 *
39
 
 * Return gem handle, or 0 on failure. Gem handles are never 0.
40
 
 */
41
 
uint32_t
42
 
anv_gem_create(struct anv_device *device, uint64_t size)
43
 
{
44
 
   struct drm_i915_gem_create gem_create = {
45
 
      .size = size,
46
 
   };
47
 
 
48
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create);
49
 
   if (ret != 0) {
50
 
      /* FIXME: What do we do if this fails? */
51
 
      return 0;
52
 
   }
53
 
 
54
 
   return gem_create.handle;
55
 
}
56
 
 
57
 
void
58
 
anv_gem_close(struct anv_device *device, uint32_t gem_handle)
59
 
{
60
 
   struct drm_gem_close close = {
61
 
      .handle = gem_handle,
62
 
   };
63
 
 
64
 
   intel_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
65
 
}
66
 
 
67
 
uint32_t
68
 
anv_gem_create_regions(struct anv_device *device, uint64_t anv_bo_size,
69
 
                       uint32_t num_regions,
70
 
                       struct drm_i915_gem_memory_class_instance *regions)
71
 
{
72
 
   struct drm_i915_gem_create_ext_memory_regions ext_regions = {
73
 
      .base = { .name = I915_GEM_CREATE_EXT_MEMORY_REGIONS },
74
 
      .num_regions = num_regions,
75
 
      .regions = (uintptr_t)regions,
76
 
   };
77
 
 
78
 
   struct drm_i915_gem_create_ext gem_create = {
79
 
      .size = anv_bo_size,
80
 
      .extensions = (uintptr_t) &ext_regions,
81
 
   };
82
 
 
83
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE_EXT,
84
 
                         &gem_create);
85
 
   if (ret != 0) {
86
 
      return 0;
87
 
   }
88
 
 
89
 
   return gem_create.handle;
90
 
}
91
 
 
92
 
/**
93
 
 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
94
 
 */
95
 
static void*
96
 
anv_gem_mmap_offset(struct anv_device *device, uint32_t gem_handle,
97
 
                    uint64_t offset, uint64_t size, uint32_t flags)
98
 
{
99
 
   struct drm_i915_gem_mmap_offset gem_mmap = {
100
 
      .handle = gem_handle,
101
 
      .flags = device->info.has_local_mem ? I915_MMAP_OFFSET_FIXED :
102
 
         (flags & I915_MMAP_WC) ? I915_MMAP_OFFSET_WC : I915_MMAP_OFFSET_WB,
103
 
   };
104
 
   assert(offset == 0);
105
 
 
106
 
   /* Get the fake offset back */
107
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET, &gem_mmap);
108
 
   if (ret != 0)
109
 
      return MAP_FAILED;
110
 
 
111
 
   /* And map it */
112
 
   void *map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
113
 
                    device->fd, gem_mmap.offset);
114
 
   return map;
115
 
}
116
 
 
117
 
static void*
118
 
anv_gem_mmap_legacy(struct anv_device *device, uint32_t gem_handle,
119
 
                    uint64_t offset, uint64_t size, uint32_t flags)
120
 
{
121
 
   assert(!device->info.has_local_mem);
122
 
 
123
 
   struct drm_i915_gem_mmap gem_mmap = {
124
 
      .handle = gem_handle,
125
 
      .offset = offset,
126
 
      .size = size,
127
 
      .flags = flags,
128
 
   };
129
 
 
130
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap);
131
 
   if (ret != 0)
132
 
      return MAP_FAILED;
133
 
 
134
 
   return (void *)(uintptr_t) gem_mmap.addr_ptr;
135
 
}
136
 
 
137
 
/**
138
 
 * Wrapper around DRM_IOCTL_I915_GEM_MMAP. Returns MAP_FAILED on error.
139
 
 */
140
 
void*
141
 
anv_gem_mmap(struct anv_device *device, uint32_t gem_handle,
142
 
             uint64_t offset, uint64_t size, uint32_t flags)
143
 
{
144
 
   void *map;
145
 
   if (device->physical->has_mmap_offset)
146
 
      map = anv_gem_mmap_offset(device, gem_handle, offset, size, flags);
147
 
   else
148
 
      map = anv_gem_mmap_legacy(device, gem_handle, offset, size, flags);
149
 
 
150
 
   if (map != MAP_FAILED)
151
 
      VG(VALGRIND_MALLOCLIKE_BLOCK(map, size, 0, 1));
152
 
 
153
 
   return map;
154
 
}
155
 
 
156
 
/* This is just a wrapper around munmap, but it also notifies valgrind that
157
 
 * this map is no longer valid.  Pair this with anv_gem_mmap().
158
 
 */
159
 
void
160
 
anv_gem_munmap(struct anv_device *device, void *p, uint64_t size)
161
 
{
162
 
   VG(VALGRIND_FREELIKE_BLOCK(p, 0));
163
 
   munmap(p, size);
164
 
}
165
 
 
166
 
uint32_t
167
 
anv_gem_userptr(struct anv_device *device, void *mem, size_t size)
168
 
{
169
 
   struct drm_i915_gem_userptr userptr = {
170
 
      .user_ptr = (__u64)((unsigned long) mem),
171
 
      .user_size = size,
172
 
      .flags = 0,
173
 
   };
174
 
 
175
 
   if (device->physical->has_userptr_probe)
176
 
      userptr.flags |= I915_USERPTR_PROBE;
177
 
 
178
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
179
 
   if (ret == -1)
180
 
      return 0;
181
 
 
182
 
   return userptr.handle;
183
 
}
184
 
 
185
 
int
186
 
anv_gem_set_caching(struct anv_device *device,
187
 
                    uint32_t gem_handle, uint32_t caching)
188
 
{
189
 
   struct drm_i915_gem_caching gem_caching = {
190
 
      .handle = gem_handle,
191
 
      .caching = caching,
192
 
   };
193
 
 
194
 
   return intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
195
 
}
196
 
 
197
 
int
198
 
anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
199
 
                   uint32_t read_domains, uint32_t write_domain)
200
 
{
201
 
   struct drm_i915_gem_set_domain gem_set_domain = {
202
 
      .handle = gem_handle,
203
 
      .read_domains = read_domains,
204
 
      .write_domain = write_domain,
205
 
   };
206
 
 
207
 
   return intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &gem_set_domain);
208
 
}
209
 
 
210
 
/**
211
 
 * Returns 0, 1, or negative to indicate error
212
 
 */
213
 
int
214
 
anv_gem_busy(struct anv_device *device, uint32_t gem_handle)
215
 
{
216
 
   struct drm_i915_gem_busy busy = {
217
 
      .handle = gem_handle,
218
 
   };
219
 
 
220
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
221
 
   if (ret < 0)
222
 
      return ret;
223
 
 
224
 
   return busy.busy != 0;
225
 
}
226
 
 
227
 
/**
228
 
 * On error, \a timeout_ns holds the remaining time.
229
 
 */
230
 
int
231
 
anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
232
 
{
233
 
   struct drm_i915_gem_wait wait = {
234
 
      .bo_handle = gem_handle,
235
 
      .timeout_ns = *timeout_ns,
236
 
      .flags = 0,
237
 
   };
238
 
 
239
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
240
 
   *timeout_ns = wait.timeout_ns;
241
 
 
242
 
   return ret;
243
 
}
244
 
 
245
 
int
246
 
anv_gem_execbuffer(struct anv_device *device,
247
 
                   struct drm_i915_gem_execbuffer2 *execbuf)
248
 
{
249
 
   if (execbuf->flags & I915_EXEC_FENCE_OUT)
250
 
      return intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2_WR, execbuf);
251
 
   else
252
 
      return intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
253
 
}
254
 
 
255
 
/** Return -1 on error. */
256
 
int
257
 
anv_gem_get_tiling(struct anv_device *device, uint32_t gem_handle)
258
 
{
259
 
   struct drm_i915_gem_get_tiling get_tiling = {
260
 
      .handle = gem_handle,
261
 
   };
262
 
 
263
 
   /* FIXME: On discrete platforms we don't have DRM_IOCTL_I915_GEM_GET_TILING
264
 
    * anymore, so we will need another way to get the tiling. Apparently this
265
 
    * is only used in Android code, so we may need some other way to
266
 
    * communicate the tiling mode.
267
 
    */
268
 
   if (intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling)) {
269
 
      assert(!"Failed to get BO tiling");
270
 
      return -1;
271
 
   }
272
 
 
273
 
   return get_tiling.tiling_mode;
274
 
}
275
 
 
276
 
int
277
 
anv_gem_set_tiling(struct anv_device *device,
278
 
                   uint32_t gem_handle, uint32_t stride, uint32_t tiling)
279
 
{
280
 
   int ret;
281
 
 
282
 
   /* On discrete platforms we don't have DRM_IOCTL_I915_GEM_SET_TILING. So
283
 
    * nothing needs to be done.
284
 
    */
285
 
   if (!device->info.has_tiling_uapi)
286
 
      return 0;
287
 
 
288
 
   /* set_tiling overwrites the input on the error path, so we have to open
289
 
    * code intel_ioctl.
290
 
    */
291
 
   do {
292
 
      struct drm_i915_gem_set_tiling set_tiling = {
293
 
         .handle = gem_handle,
294
 
         .tiling_mode = tiling,
295
 
         .stride = stride,
296
 
      };
297
 
 
298
 
      ret = ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
299
 
   } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
300
 
 
301
 
   return ret;
302
 
}
303
 
 
304
 
int
305
 
anv_gem_get_param(int fd, uint32_t param)
306
 
{
307
 
   int tmp;
308
 
 
309
 
   drm_i915_getparam_t gp = {
310
 
      .param = param,
311
 
      .value = &tmp,
312
 
   };
313
 
 
314
 
   int ret = intel_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
315
 
   if (ret == 0)
316
 
      return tmp;
317
 
 
318
 
   return 0;
319
 
}
320
 
 
321
 
bool
322
 
anv_gem_has_context_priority(int fd, int priority)
323
 
{
324
 
   return !anv_gem_set_context_param(fd, 0, I915_CONTEXT_PARAM_PRIORITY,
325
 
                                     priority);
326
 
}
327
 
 
328
 
int
329
 
anv_gem_create_context(struct anv_device *device)
330
 
{
331
 
   struct drm_i915_gem_context_create create = { 0 };
332
 
 
333
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
334
 
   if (ret == -1)
335
 
      return -1;
336
 
 
337
 
   return create.ctx_id;
338
 
}
339
 
 
340
 
int
341
 
anv_gem_destroy_context(struct anv_device *device, int context)
342
 
{
343
 
   struct drm_i915_gem_context_destroy destroy = {
344
 
      .ctx_id = context,
345
 
   };
346
 
 
347
 
   return intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &destroy);
348
 
}
349
 
 
350
 
int
351
 
anv_gem_set_context_param(int fd, int context, uint32_t param, uint64_t value)
352
 
{
353
 
   struct drm_i915_gem_context_param p = {
354
 
      .ctx_id = context,
355
 
      .param = param,
356
 
      .value = value,
357
 
   };
358
 
   int err = 0;
359
 
 
360
 
   if (intel_ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &p))
361
 
      err = -errno;
362
 
   return err;
363
 
}
364
 
 
365
 
int
366
 
anv_gem_context_get_reset_stats(int fd, int context,
367
 
                                uint32_t *active, uint32_t *pending)
368
 
{
369
 
   struct drm_i915_reset_stats stats = {
370
 
      .ctx_id = context,
371
 
   };
372
 
 
373
 
   int ret = intel_ioctl(fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats);
374
 
   if (ret == 0) {
375
 
      *active = stats.batch_active;
376
 
      *pending = stats.batch_pending;
377
 
   }
378
 
 
379
 
   return ret;
380
 
}
381
 
 
382
 
int
383
 
anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
384
 
{
385
 
   struct drm_prime_handle args = {
386
 
      .handle = gem_handle,
387
 
      .flags = DRM_CLOEXEC | DRM_RDWR,
388
 
   };
389
 
 
390
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
391
 
   if (ret == -1)
392
 
      return -1;
393
 
 
394
 
   return args.fd;
395
 
}
396
 
 
397
 
uint32_t
398
 
anv_gem_fd_to_handle(struct anv_device *device, int fd)
399
 
{
400
 
   struct drm_prime_handle args = {
401
 
      .fd = fd,
402
 
   };
403
 
 
404
 
   int ret = intel_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
405
 
   if (ret == -1)
406
 
      return 0;
407
 
 
408
 
   return args.handle;
409
 
}
410
 
 
411
 
int
412
 
anv_gem_reg_read(int fd, uint32_t offset, uint64_t *result)
413
 
{
414
 
   struct drm_i915_reg_read args = {
415
 
      .offset = offset
416
 
   };
417
 
 
418
 
   int ret = intel_ioctl(fd, DRM_IOCTL_I915_REG_READ, &args);
419
 
 
420
 
   *result = args.val;
421
 
   return ret;
422
 
}
423
 
 
424
 
struct drm_i915_query_engine_info *
425
 
anv_gem_get_engine_info(int fd)
426
 
{
427
 
   return intel_i915_query_alloc(fd, DRM_I915_QUERY_ENGINE_INFO, NULL);
428
 
}