~mmach/netext73/mesa-ryzen

« back to all changes in this revision

Viewing changes to src/gallium/drivers/iris/iris_bufmgr.c

  • Committer: mmach
  • Date: 2023-11-02 21:31:35 UTC
  • Revision ID: netbit73@gmail.com-20231102213135-18d4tzh7tj0uz752
2023-11-02 22:11:57

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
#include "errno.h"
51
51
#include "common/intel_aux_map.h"
52
52
#include "common/intel_clflush.h"
 
53
#include "c99_alloca.h"
53
54
#include "dev/intel_debug.h"
54
55
#include "common/intel_gem.h"
55
56
#include "dev/intel_device_info.h"
 
57
#include "drm-uapi/dma-buf.h"
56
58
#include "isl/isl.h"
57
59
#include "util/os_mman.h"
58
60
#include "util/u_debug.h"
313
315
bucket_for_size(struct iris_bufmgr *bufmgr, uint64_t size,
314
316
                enum iris_heap heap, unsigned flags)
315
317
{
316
 
 
317
 
   /* Protected bo needs special handling during allocation.
318
 
    * Exported and scanout bos also need special handling during allocation
319
 
    * in Xe KMD.
320
 
    */
321
 
   if ((flags & BO_ALLOC_PROTECTED) ||
322
 
       ((flags & (BO_ALLOC_SHARED | BO_ALLOC_SCANOUT)) &&
323
 
        bufmgr->devinfo.kmd_type == INTEL_KMD_TYPE_XE))
 
318
   if (flags & BO_ALLOC_PROTECTED)
 
319
      return NULL;
 
320
 
 
321
   const struct intel_device_info *devinfo = &bufmgr->devinfo;
 
322
   if (devinfo->has_set_pat_uapi &&
 
323
       iris_pat_index_for_bo_flags(devinfo, flags) != devinfo->pat.writeback)
 
324
      return NULL;
 
325
 
 
326
   if (devinfo->kmd_type == INTEL_KMD_TYPE_XE &&
 
327
       (flags & (BO_ALLOC_SHARED | BO_ALLOC_SCANOUT)))
324
328
      return NULL;
325
329
 
326
330
   /* Calculating the pages and rounding up to the page size. */
442
446
   util_vma_heap_free(&bufmgr->vma_allocator[memzone], address, size);
443
447
}
444
448
 
 
449
/* Exports a BO's implicit synchronization state to a drm_syncobj, returning
 
450
 * its wrapping iris_syncobj. The drm_syncobj is created new and has to be
 
451
 * destroyed by the caller after the execbuf ioctl.
 
452
 */
 
453
struct iris_syncobj *
 
454
iris_bo_export_sync_state(struct iris_bo *bo)
 
455
{
 
456
   struct iris_bufmgr *bufmgr = bo->bufmgr;
 
457
   int drm_fd = iris_bufmgr_get_fd(bufmgr);
 
458
 
 
459
   struct iris_syncobj *iris_syncobj = iris_create_syncobj(bufmgr);
 
460
 
 
461
   struct dma_buf_export_sync_file export_sync_file_ioctl = {
 
462
      .flags = DMA_BUF_SYNC_RW, /* TODO */
 
463
      .fd = -1,
 
464
   };
 
465
   if (intel_ioctl(bo->real.prime_fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE,
 
466
                   &export_sync_file_ioctl)) {
 
467
      fprintf(stderr, "DMA_BUF_IOCTL_EXPORT_SYNC_FILE ioctl failed (%d)\n",
 
468
              errno);
 
469
      goto error_export;
 
470
   }
 
471
 
 
472
   int sync_file_fd = export_sync_file_ioctl.fd;
 
473
   assert(sync_file_fd >= 0);
 
474
 
 
475
   struct drm_syncobj_handle syncobj_import_ioctl = {
 
476
      .handle = iris_syncobj->handle,
 
477
      .flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE,
 
478
      .fd = sync_file_fd,
 
479
   };
 
480
   if (intel_ioctl(drm_fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE,
 
481
                   &syncobj_import_ioctl)) {
 
482
      fprintf(stderr, "DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE ioctl failed (%d)\n",
 
483
              errno);
 
484
   }
 
485
 
 
486
   close(sync_file_fd);
 
487
 
 
488
   return iris_syncobj;
 
489
error_export:
 
490
   iris_syncobj_destroy(bufmgr, iris_syncobj);
 
491
   return NULL;
 
492
}
 
493
 
 
494
/* Import the state of a sync_file_fd (which we should have gotten from
 
495
 * batch_syncobj_to_sync_file_fd) into a BO as its implicit synchronization
 
496
 * state.
 
497
 */
 
498
void
 
499
iris_bo_import_sync_state(struct iris_bo *bo, int sync_file_fd)
 
500
{
 
501
   struct dma_buf_import_sync_file import_sync_file_ioctl = {
 
502
      .flags = DMA_BUF_SYNC_WRITE,
 
503
      .fd = sync_file_fd,
 
504
   };
 
505
   if (intel_ioctl(bo->real.prime_fd, DMA_BUF_IOCTL_IMPORT_SYNC_FILE,
 
506
                   &import_sync_file_ioctl))
 
507
      fprintf(stderr, "DMA_BUF_IOCTL_IMPORT_SYNC_FILE ioctl failed (%d)\n",
 
508
              errno);
 
509
}
 
510
 
445
511
/* A timeout of 0 just checks for busyness. */
446
512
static int
447
513
iris_bo_wait_syncobj(struct iris_bo *bo, int64_t timeout_ns)
448
514
{
449
515
   int ret = 0;
450
516
   struct iris_bufmgr *bufmgr = bo->bufmgr;
 
517
   const bool is_external = iris_bo_is_real(bo) && bo->real.prime_fd != -1;
 
518
   struct iris_syncobj *external_implicit_syncobj = NULL;
451
519
 
452
 
   /* If we know it's idle, don't bother with the kernel round trip */
453
 
   if (bo->idle)
 
520
   /* If we know it's idle, don't bother with the kernel round trip.
 
521
    * Can't do that for Xe KMD with external BOs since we have to check the
 
522
    * implicit synchronization information.
 
523
    */
 
524
   if (!is_external && bo->idle)
454
525
      return 0;
455
526
 
456
527
   simple_mtx_lock(&bufmgr->bo_deps_lock);
457
528
 
458
 
   uint32_t handles[bo->deps_size * IRIS_BATCH_COUNT * 2];
 
529
   const int handles_len = bo->deps_size * IRIS_BATCH_COUNT * 2 + is_external;
 
530
   uint32_t *handles = handles_len <= 32 ?
 
531
                        (uint32_t *)alloca(handles_len * sizeof(*handles)) :
 
532
                        (uint32_t *)malloc(handles_len * sizeof(*handles));
459
533
   int handle_count = 0;
460
534
 
 
535
   if (is_external) {
 
536
      external_implicit_syncobj = iris_bo_export_sync_state(bo);
 
537
      if (external_implicit_syncobj)
 
538
         handles[handle_count++] = external_implicit_syncobj->handle;
 
539
   }
 
540
 
461
541
   for (int d = 0; d < bo->deps_size; d++) {
462
542
      for (int b = 0; b < IRIS_BATCH_COUNT; b++) {
463
543
         struct iris_syncobj *r = bo->deps[d].read_syncobjs[b];
499
579
   }
500
580
 
501
581
out:
 
582
   if (handles_len > 32)
 
583
      free(handles);
 
584
   if (external_implicit_syncobj)
 
585
      iris_syncobj_reference(bufmgr, &external_implicit_syncobj, NULL);
 
586
 
502
587
   simple_mtx_unlock(&bufmgr->bo_deps_lock);
503
588
   return ret;
504
589
}
749
834
      bo->bufmgr = bufmgr;
750
835
      bo->hash = _mesa_hash_pointer(bo);
751
836
      bo->gem_handle = 0;
752
 
      bo->address = slab->bo->address + i * entry_size;
 
837
      bo->address = intel_canonical_address(slab->bo->address + i * entry_size);
753
838
      bo->aux_map_address = 0;
754
839
      bo->index = -1;
755
840
      bo->refcount = 0;
1061
1146
   [IRIS_HEAP_DEVICE_LOCAL_PREFERRED] = "local-preferred",
1062
1147
};
1063
1148
 
 
1149
static enum iris_mmap_mode
 
1150
iris_bo_alloc_get_mmap_mode(struct iris_bufmgr *bufmgr, enum iris_heap heap,
 
1151
                            unsigned flags)
 
1152
{
 
1153
   if (bufmgr->devinfo.kmd_type == INTEL_KMD_TYPE_XE)
 
1154
      return iris_xe_bo_flags_to_mmap_mode(bufmgr, heap, flags);
 
1155
 
 
1156
   /* i915 */
 
1157
   const bool local = heap != IRIS_HEAP_SYSTEM_MEMORY;
 
1158
   const bool is_coherent = bufmgr->devinfo.has_llc ||
 
1159
                            (bufmgr->vram.size > 0 && !local) ||
 
1160
                            (flags & BO_ALLOC_COHERENT);
 
1161
   const bool is_scanout = (flags & BO_ALLOC_SCANOUT) != 0;
 
1162
   enum iris_mmap_mode mmap_mode;
 
1163
 
 
1164
   if (!intel_vram_all_mappable(&bufmgr->devinfo) && heap == IRIS_HEAP_DEVICE_LOCAL)
 
1165
      mmap_mode = IRIS_MMAP_NONE;
 
1166
   else if (!local && is_coherent && !is_scanout)
 
1167
      mmap_mode = IRIS_MMAP_WB;
 
1168
   else
 
1169
      mmap_mode = IRIS_MMAP_WC;
 
1170
 
 
1171
   return mmap_mode;
 
1172
}
 
1173
 
1064
1174
struct iris_bo *
1065
1175
iris_bo_alloc(struct iris_bufmgr *bufmgr,
1066
1176
              const char *name,
1072
1182
   struct iris_bo *bo;
1073
1183
   unsigned int page_size = getpagesize();
1074
1184
   enum iris_heap heap = flags_to_heap(bufmgr, flags);
1075
 
   bool local = heap != IRIS_HEAP_SYSTEM_MEMORY;
1076
1185
   struct bo_cache_bucket *bucket = bucket_for_size(bufmgr, size, heap, flags);
1077
1186
 
1078
1187
   if (memzone != IRIS_MEMZONE_OTHER || (flags & BO_ALLOC_COHERENT))
1088
1197
    */
1089
1198
   uint64_t bo_size =
1090
1199
      bucket ? bucket->size : MAX2(ALIGN(size, page_size), page_size);
1091
 
 
1092
 
   bool is_coherent = bufmgr->devinfo.has_llc ||
1093
 
                      (bufmgr->vram.size > 0 && !local) ||
1094
 
                      (flags & BO_ALLOC_COHERENT);
1095
 
   bool is_scanout = (flags & BO_ALLOC_SCANOUT) != 0;
1096
 
 
1097
 
   enum iris_mmap_mode mmap_mode;
1098
 
   if (!intel_vram_all_mappable(&bufmgr->devinfo) && heap == IRIS_HEAP_DEVICE_LOCAL)
1099
 
      mmap_mode = IRIS_MMAP_NONE;
1100
 
   else if (!local && is_coherent && !is_scanout)
1101
 
      mmap_mode = IRIS_MMAP_WB;
1102
 
   else
1103
 
      mmap_mode = IRIS_MMAP_WC;
 
1200
   enum iris_mmap_mode mmap_mode = iris_bo_alloc_get_mmap_mode(bufmgr, heap, flags);
1104
1201
 
1105
1202
   simple_mtx_lock(&bufmgr->lock);
1106
1203
 
1142
1239
   bo->real.protected = flags & BO_ALLOC_PROTECTED;
1143
1240
   bo->index = -1;
1144
1241
   bo->real.kflags = EXEC_OBJECT_SUPPORTS_48B_ADDRESS | EXEC_OBJECT_PINNED;
 
1242
   bo->real.prime_fd = -1;
1145
1243
 
1146
1244
   /* By default, capture all driver-internal buffers like shader kernels,
1147
1245
    * surface states, dynamic states, border colors, and so on.
1193
1291
   return iris_bo_close(bufmgr->fd, gem_handle);
1194
1292
}
1195
1293
 
 
1294
static enum iris_mmap_mode
 
1295
iris_bo_create_userptr_get_mmap_mode(struct iris_bufmgr *bufmgr)
 
1296
{
 
1297
   switch (bufmgr->devinfo.kmd_type) {
 
1298
   case INTEL_KMD_TYPE_I915:
 
1299
      return IRIS_MMAP_WB;
 
1300
   case INTEL_KMD_TYPE_XE:
 
1301
      return iris_xe_bo_flags_to_mmap_mode(bufmgr, IRIS_HEAP_SYSTEM_MEMORY, 0);
 
1302
   default:
 
1303
      return IRIS_MMAP_NONE;
 
1304
   }
 
1305
}
 
1306
 
1196
1307
struct iris_bo *
1197
1308
iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name,
1198
1309
                       void *ptr, size_t size,
1240
1351
   bo->real.userptr = true;
1241
1352
   bo->index = -1;
1242
1353
   bo->idle = true;
1243
 
   bo->real.mmap_mode = IRIS_MMAP_WB;
 
1354
   bo->real.heap = IRIS_HEAP_SYSTEM_MEMORY;
 
1355
   bo->real.mmap_mode = iris_bo_create_userptr_get_mmap_mode(bufmgr);
 
1356
   bo->real.prime_fd = -1;
1244
1357
 
1245
1358
   return bo;
1246
1359
 
1251
1364
   return NULL;
1252
1365
}
1253
1366
 
 
1367
static bool
 
1368
needs_prime_fd(struct iris_bufmgr *bufmgr)
 
1369
{
 
1370
   return bufmgr->devinfo.kmd_type == INTEL_KMD_TYPE_XE;
 
1371
}
 
1372
 
 
1373
static bool
 
1374
iris_bo_set_prime_fd(struct iris_bo *bo)
 
1375
{
 
1376
   struct iris_bufmgr *bufmgr = bo->bufmgr;
 
1377
 
 
1378
   if (needs_prime_fd(bufmgr) && bo->real.prime_fd == -1) {
 
1379
      if (drmPrimeHandleToFD(bufmgr->fd, bo->gem_handle,
 
1380
                             DRM_CLOEXEC | DRM_RDWR, &bo->real.prime_fd)) {
 
1381
         fprintf(stderr, "Failed to get prime fd for bo %s/%u\n",
 
1382
                 bo->name, bo->gem_handle);
 
1383
         return false;
 
1384
      }
 
1385
   }
 
1386
 
 
1387
   return true;
 
1388
}
 
1389
 
1254
1390
/**
1255
1391
 * Returns a iris_bo wrapping the given buffer object handle.
1256
1392
 *
1303
1439
   bo->gem_handle = open_arg.handle;
1304
1440
   bo->name = name;
1305
1441
   bo->real.global_name = handle;
 
1442
   bo->real.prime_fd = -1;
1306
1443
   bo->real.reusable = false;
1307
1444
   bo->real.imported = true;
1308
1445
   bo->real.mmap_mode = IRIS_MMAP_NONE;
1313
1450
   if (bo->address == 0ull)
1314
1451
      goto err_free;
1315
1452
 
 
1453
   if (!iris_bo_set_prime_fd(bo))
 
1454
      goto err_vm_alloc;
 
1455
 
1316
1456
   if (!bufmgr->kmd_backend->gem_vm_bind(bo))
1317
1457
      goto err_vm_alloc;
1318
1458
 
1369
1509
   else
1370
1510
      DBG("Unable to unbind vm of buf %u\n", bo->gem_handle);
1371
1511
 
 
1512
   if (bo->real.prime_fd != -1)
 
1513
      close(bo->real.prime_fd);
 
1514
 
1372
1515
   /* Close this object */
1373
1516
   if (iris_bufmgr_bo_close(bufmgr, bo->gem_handle) != 0) {
1374
1517
      DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
1799
1942
   /* GEM_SET_TILING is slightly broken and overwrites the input on the
1800
1943
    * error path, so we have to open code intel_ioctl().
1801
1944
    */
1802
 
   do {
1803
 
      struct drm_i915_gem_set_tiling set_tiling = {
1804
 
         .handle = bo->gem_handle,
1805
 
         .tiling_mode = tiling_mode,
1806
 
         .stride = surf->row_pitch_B,
1807
 
      };
1808
 
      ret = ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
1809
 
   } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
 
1945
   struct drm_i915_gem_set_tiling set_tiling = {
 
1946
      .handle = bo->gem_handle,
 
1947
      .tiling_mode = tiling_mode,
 
1948
      .stride = surf->row_pitch_B,
 
1949
   };
1810
1950
 
 
1951
   ret = intel_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
1811
1952
   if (ret) {
1812
1953
      DBG("gem_set_tiling failed for BO %u: %s\n",
1813
1954
          bo->gem_handle, strerror(errno));
1817
1958
}
1818
1959
 
1819
1960
struct iris_bo *
1820
 
iris_bo_import_dmabuf(struct iris_bufmgr *bufmgr, int prime_fd)
 
1961
iris_bo_import_dmabuf(struct iris_bufmgr *bufmgr, int prime_fd,
 
1962
                      const uint64_t modifier)
1821
1963
{
1822
1964
   uint32_t handle;
1823
1965
   struct iris_bo *bo;
1864
2006
   if (INTEL_DEBUG(DEBUG_CAPTURE_ALL))
1865
2007
      bo->real.kflags |= EXEC_OBJECT_CAPTURE;
1866
2008
   bo->gem_handle = handle;
1867
 
 
1868
 
   /* From the Bspec, Memory Compression - Gfx12:
1869
 
    *
1870
 
    *    The base address for the surface has to be 64K page aligned and the
1871
 
    *    surface is expected to be padded in the virtual domain to be 4 4K
1872
 
    *    pages.
1873
 
    *
1874
 
    * The dmabuf may contain a compressed surface. Align the BO to 64KB just
1875
 
    * in case. We always align to 64KB even on platforms where we don't need
1876
 
    * to, because it's a fairly reasonable thing to do anyway.
 
2009
   bo->real.prime_fd = needs_prime_fd(bufmgr) ? dup(prime_fd) : -1;
 
2010
 
 
2011
   uint64_t alignment = 1;
 
2012
 
 
2013
   /* When an aux map will be used, there is an alignment requirement on the
 
2014
    * main surface from the mapping granularity. Some planes of the image may
 
2015
    * have smaller alignment requirements, but this one should work for all.
1877
2016
    */
1878
 
   bo->address = vma_alloc(bufmgr, IRIS_MEMZONE_OTHER, bo->size, 64 * 1024);
 
2017
   if (bufmgr->devinfo.has_aux_map && isl_drm_modifier_has_aux(modifier))
 
2018
      alignment = intel_aux_map_get_alignment(bufmgr->aux_map_ctx);
 
2019
 
 
2020
   bo->address = vma_alloc(bufmgr, IRIS_MEMZONE_OTHER, bo->size, alignment);
1879
2021
   if (bo->address == 0ull)
1880
2022
      goto err_free;
1881
2023
 
1934
2076
   simple_mtx_lock(&bufmgr->lock);
1935
2077
   iris_bo_mark_exported_locked(bo);
1936
2078
   simple_mtx_unlock(&bufmgr->lock);
 
2079
 
 
2080
   iris_bo_set_prime_fd(bo);
1937
2081
}
1938
2082
 
1939
2083
int
1985
2129
         _mesa_hash_table_insert(bufmgr->name_table, &bo->real.global_name, bo);
1986
2130
      }
1987
2131
      simple_mtx_unlock(&bufmgr->lock);
 
2132
 
 
2133
      iris_bo_set_prime_fd(bo);
1988
2134
   }
1989
2135
 
1990
2136
   *name = bo->real.global_name;
2101
2247
   }
2102
2248
}
2103
2249
 
 
2250
static enum iris_mmap_mode
 
2251
iris_bo_alloc_aux_map_get_mmap_mode(struct iris_bufmgr *bufmgr,
 
2252
                                    enum iris_heap heap)
 
2253
{
 
2254
   switch (bufmgr->devinfo.kmd_type) {
 
2255
   case INTEL_KMD_TYPE_I915:
 
2256
      return heap != IRIS_HEAP_SYSTEM_MEMORY ||
 
2257
         bufmgr->devinfo.has_set_pat_uapi ?
 
2258
         IRIS_MMAP_WC : IRIS_MMAP_WB;
 
2259
   case INTEL_KMD_TYPE_XE:
 
2260
      return iris_xe_bo_flags_to_mmap_mode(bufmgr, heap, 0);
 
2261
   default:
 
2262
      return IRIS_MMAP_NONE;
 
2263
   }
 
2264
}
 
2265
 
2104
2266
static struct intel_buffer *
2105
2267
intel_aux_map_buffer_alloc(void *driver_ctx, uint32_t size)
2106
2268
{
2135
2297
   bo->index = -1;
2136
2298
   bo->real.kflags = EXEC_OBJECT_SUPPORTS_48B_ADDRESS | EXEC_OBJECT_PINNED |
2137
2299
                     EXEC_OBJECT_CAPTURE;
2138
 
   bo->real.mmap_mode =
2139
 
      bo->real.heap != IRIS_HEAP_SYSTEM_MEMORY ? IRIS_MMAP_WC : IRIS_MMAP_WB;
 
2300
   bo->real.mmap_mode = iris_bo_alloc_aux_map_get_mmap_mode(bufmgr,
 
2301
                                                            bo->real.heap);
 
2302
   bo->real.prime_fd = -1;
2140
2303
 
2141
2304
   buf->driver_bo = bo;
2142
2305
   buf->gpu = bo->address;
2172
2335
   bufmgr->sys.region = &devinfo->mem.sram.mem;
2173
2336
   bufmgr->sys.size = devinfo->mem.sram.mappable.size;
2174
2337
 
 
2338
   /* When the resizable bar feature is disabled,
 
2339
    * then vram.mappable.size is only 256MB.
 
2340
    * The second half of the total size is in the vram.unmappable.size
 
2341
    * variable.
 
2342
    */
2175
2343
   bufmgr->vram.region = &devinfo->mem.vram.mem;
2176
 
   bufmgr->vram.size = devinfo->mem.vram.mappable.size;
 
2344
   bufmgr->vram.size = devinfo->mem.vram.mappable.size +
 
2345
                       devinfo->mem.vram.unmappable.size;
2177
2346
 
2178
2347
   return true;
2179
2348
}