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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2021 Intel Corporation
*/
#include <errno.h>
#include <pthread.h>
#include "drmtest.h"
#include "gem_create.h"
#include "i915_drm.h"
#include "igt_core.h"
#include "igt_list.h"
#include "igt_map.h"
#include "ioctl_wrappers.h"
/**
* SECTION:gem_create
* @short_description: Helpers for dealing with objects creation
* @title: GEM Create
*
* This helper library contains functions used for handling creating gem
* objects.
*/
int __gem_create(int fd, uint64_t *size, uint32_t *handle)
{
struct drm_i915_gem_create create = {
.size = *size,
};
int err = 0;
if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) == 0) {
*handle = create.handle;
*size = create.size;
} else {
err = -errno;
igt_assume(err != 0);
}
errno = 0;
return err;
}
/**
* gem_create:
* @fd: open i915 drm file descriptor
* @size: desired size of the buffer
*
* This wraps the GEM_CREATE ioctl, which allocates a new gem buffer object of
* @size.
*
* Returns: The file-private handle of the created buffer object
*/
uint32_t gem_create(int fd, uint64_t size)
{
uint32_t handle;
igt_assert_eq(__gem_create(fd, &size, &handle), 0);
return handle;
}
int __gem_create_ext(int fd, uint64_t *size, uint32_t flags, uint32_t *handle,
struct i915_user_extension *ext)
{
struct drm_i915_gem_create_ext create = {
.size = *size,
.flags = flags,
.extensions = to_user_pointer(ext),
};
int err = 0;
if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &create) == 0) {
*handle = create.handle;
*size = create.size;
} else {
err = -errno;
igt_assume(err != 0);
}
errno = 0;
return err;
}
/**
* gem_create_ext:
* @fd: open i915 drm file descriptor
* @size: desired size of the buffer
* @flags: optional flags
* @ext: optional extensions chain
*
* This wraps the GEM_CREATE_EXT ioctl, which allocates a new gem buffer object
* of @size.
*
* Returns: The file-private handle of the created buffer object
*/
uint32_t gem_create_ext(int fd, uint64_t size, uint32_t flags,
struct i915_user_extension *ext)
{
uint32_t handle;
igt_assert_eq(__gem_create_ext(fd, &size, flags, &handle, ext), 0);
return handle;
}
static struct igt_map *pool;
static pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER;
struct pool_entry {
int fd;
uint32_t handle;
uint64_t size; /* requested bo size */
uint64_t bo_size; /* created bo size */
uint32_t region;
struct igt_list_head link;
};
struct pool_list {
uint64_t size;
struct igt_list_head list;
};
static struct pool_entry *find_or_create(int fd, struct pool_list *pl,
uint64_t size, uint32_t region)
{
struct pool_entry *pe;
bool found = false;
igt_list_for_each_entry(pe, &pl->list, link) {
if (pe->fd == fd && pe->size == size && pe->region == region &&
!gem_bo_busy(fd, pe->handle)) {
found = true;
break;
}
}
if (!found) {
pe = calloc(1, sizeof(*pe));
if (!pe)
goto out;
pe->fd = fd;
pe->bo_size = size;
if (__gem_create_in_memory_regions(fd, &pe->handle, &pe->bo_size, region)) {
free(pe);
pe = NULL;
goto out;
}
pe->size = size;
pe->region = region;
igt_list_add_tail(&pe->link, &pl->list);
}
out:
return pe;
}
/**
* gem_create_from_pool:
* @fd: open i915 drm file descriptor
* @size: pointer to size, on input it points to requested bo size,
* on output created bo size will be stored there
* @region: region in which bo should be created
*
* Function returns bo handle which is free to use (not busy). Internally
* it iterates over previously allocated bo and returns first free. If there
* are no free bo a new one is created.
*
* Returns: bo handle + created bo size (via pointer to size)
*/
uint32_t gem_create_from_pool(int fd, uint64_t *size, uint32_t region)
{
struct pool_list *pl;
struct pool_entry *pe;
pthread_mutex_lock(&pool_mutex);
pl = igt_map_search(pool, size);
if (!pl) {
pl = calloc(1, sizeof(*pl));
if (!pl)
goto out;
IGT_INIT_LIST_HEAD(&pl->list);
pl->size = *size;
igt_map_insert(pool, &pl->size, pl);
}
pe = find_or_create(fd, pl, *size, region);
out:
pthread_mutex_unlock(&pool_mutex);
igt_assert(pl && pe);
return pe->handle;
}
static void __pool_list_free_func(struct igt_map_entry *entry)
{
free(entry->data);
}
static void __destroy_pool(struct igt_map *map, pthread_mutex_t *mutex)
{
struct igt_map_entry *pos;
const struct pool_list *pl;
struct pool_entry *pe, *tmp;
if (!map)
return;
pthread_mutex_lock(mutex);
igt_map_foreach(map, pos) {
pl = pos->key;
igt_list_for_each_entry_safe(pe, tmp, &pl->list, link) {
gem_close(pe->fd, pe->handle);
igt_list_del(&pe->link);
free(pe);
}
}
pthread_mutex_unlock(mutex);
igt_map_destroy(map, __pool_list_free_func);
}
void gem_pool_dump(void)
{
struct igt_map_entry *pos;
const struct pool_list *pl;
struct pool_entry *pe;
if (!pool)
return;
pthread_mutex_lock(&pool_mutex);
igt_debug("[pool]\n");
igt_map_foreach(pool, pos) {
pl = pos->key;
igt_debug("bucket [%llx]\n", (long long) pl->size);
igt_list_for_each_entry(pe, &pl->list, link)
igt_debug(" - handle: %u, size: %llx, bo_size: %llx, region: %x\n",
pe->handle, (long long) pe->size,
(long long) pe->bo_size, pe->region);
}
pthread_mutex_unlock(&pool_mutex);
}
static int equal_pool(const void *a, const void *b)
{
struct pool_list *p1 = (struct pool_list *) a;
struct pool_list *p2 = (struct pool_list *) b;
return p1->size == p2->size;
}
/**
* gem_pool_init:
*
* Function initializes bo pool (kind of bo cache). Main purpose of it is to
* support working with softpin to achieve pipelined execution on gpu (without
* stalls).
*
* For example imagine code as follows:
*
* |[<!-- language="C" -->
* uint32_t bb = gem_create(fd, 4096);
* uint32_t *bbptr = gem_mmap__device_coherent(fd, bb, ...)
* uint32_t *cmd = bbptr;
* ...
* *cmd++ = ...gpu commands...
* ...
* *cmd++ = MI_BATCH_BUFFER_END;
* ...
* gem_execbuf(fd, execbuf); // bb is part of execbuf <--- first execbuf
*
* cmd = bbptr;
* ...
* *cmd++ = ... next gpu commands...
* ...
* *cmd++ = MI_BATCH_BUFFER_END;
* ...
* gem_execbuf(fd, execbuf); // bb is part of execbuf <--- second execbuf
* ]|
*
* Above code is prone to gpu hang because when bb was submitted to gpu
* we immediately started writing to it. If gpu started executing commands
* from first execbuf we're overwriting it leading to unpredicted behavior
* (partially execute from first and second commands or we get gpu hang).
* To avoid this we can sync after first execbuf but we will get stall
* in execution. For some tests it might be accepted but such "isolated"
* execution hides bugs (synchronization, cache flushes, etc).
*
* So, to achive pipelined execution we need to use another bb. If we would
* like to enqueue more work which is serialized we would need more bbs
* (depends on execution speed). Handling this manually is cumbersome as
* we need to track all bb and their status (busy or free).
*
* Solution to above is gem pool. It returns first handle of requested size
* which is not busy (or create a new one if there's none or all of bo are
* in use). Here's an example how to use it:
*
* |[<!-- language="C" -->
* uint64_t bbsize = 4096;
* uint32_t bb = gem_create_from_pool(fd, &bbsize, REGION_SMEM);
* uint32_t *bbptr = gem_mmap__device_coherent(fd, bb, ...)
* uint32_t *cmd = bbptr;
* ...
* *cmd++ = ...gpu commands...
* ...
* *cmd++ = MI_BATCH_BUFFER_END;
* gem_munmap(bbptr, bbsize);
* ...
* gem_execbuf(fd, execbuf); // bb is part of execbuf <--- first execbuf
*
* bbsize = 4096;
* bb = gem_create_from_pool(fd, &bbsize, REGION_SMEM);
* cmd = bbptr;
* ...
* *cmd++ = ... next gpu commands...
* ...
* *cmd++ = MI_BATCH_BUFFER_END;
* gem_munmap(bbptr, bbsize);
* ...
* gem_execbuf(fd, execbuf); // bb is part of execbuf <--- second execbuf
* ]|
*
* Assuming first execbuf is executed we will get new bb handle when we call
* gem_create_from_pool(). When test completes pool is freed automatically
* in igt core (all handles will be closed, memory will be freed and gem pool
* will be reinitialized for next test).
*
* Some explanation is needed why we need to put pointer to size instead of
* passing absolute value. On discrete regarding memory placement (region)
* object created in the memory can be bigger than requested. Especially when
* we use allocator to handle vm space and we allocate vma with requested
* size (which is smaller than bo created) we can overlap with next allocation
* and get -ENOSPC.
*/
void gem_pool_init(void)
{
pthread_mutex_init(&pool_mutex, NULL);
__destroy_pool(pool, &pool_mutex);
pool = igt_map_create(igt_map_hash_64, equal_pool);
}
igt_constructor {
gem_pool_init();
}
|