| 1 |
/* |
| 2 |
* Copyright © 2008 Jérôme Glisse |
| 3 |
* Copyright © 2010 Marek Olšák <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
This file replaces libdrm's radeon_cs_gem with our own implemention. |
| 10 |
It's optimized specifically for Radeon DRM. |
| 11 |
Adding buffers and space checking are faster and simpler than their |
| 12 |
counterparts in libdrm (the time complexity of all the functions |
| 13 |
is O(1) in nearly all scenarios, thanks to hashing). |
| 14 |
|
| 15 |
It works like this: |
| 16 |
|
| 17 |
cs_add_buffer(cs, buf, read_domain, write_domain) adds a new relocation and |
| 18 |
also adds the size of 'buf' to the used_gart and used_vram winsys variables |
| 19 |
based on the domains, which are simply or'd for the accounting purposes. |
| 20 |
The adding is skipped if the reloc is already present in the list, but it |
| 21 |
accounts any newly-referenced domains. |
| 22 |
|
| 23 |
cs_validate is then called, which just checks: |
| 24 |
used_vram/gart < vram/gart_size * 0.8 |
| 25 |
The 0.8 number allows for some memory fragmentation. If the validation |
| 26 |
fails, the pipe driver flushes CS and tries do the validation again, |
| 27 |
i.e. it validates only that one operation. If it fails again, it drops |
| 28 |
the operation on the floor and prints some nasty message to stderr. |
| 29 |
(done in the pipe driver) |
| 30 |
|
| 31 |
cs_write_reloc(cs, buf) just writes a reloc that has been added using |
| 32 |
cs_add_buffer. The read_domain and write_domain parameters have been removed, |
| 33 |
because we already specify them in cs_add_buffer. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "radeon_drm_cs.h" |
| 37 |
|
| 38 |
#include "util/u_memory.h" |
| 39 |
#include "util/os_time.h" |
| 40 |
|
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <stdint.h> |
| 44 |
#include <xf86drm.h> |
| 45 |
|
| 46 |
|
| 47 |
#define RELOC_DWORDS (sizeof(struct drm_radeon_cs_reloc) / sizeof(uint32_t)) |
| 48 |
|
| 49 |
static struct pipe_fence_handle *radeon_cs_create_fence(struct radeon_cmdbuf *rcs); |
| 50 |
static void radeon_fence_reference(struct radeon_winsys *ws, |
| 51 |
struct pipe_fence_handle **dst, |
| 52 |
struct pipe_fence_handle *src); |
| 53 |
|
| 54 |
static struct radeon_winsys_ctx *radeon_drm_ctx_create(struct radeon_winsys *ws, |
| 55 |
unsigned flags) |
| 56 |
{ |
| 57 |
struct radeon_ctx *ctx = CALLOC_STRUCT(radeon_ctx); |
| 58 |
if (!ctx) |
| 59 |
return NULL; |
| 60 |
|
| 61 |
ctx->ws = (struct radeon_drm_winsys*)ws; |
| 62 |
ctx->gpu_reset_counter = radeon_drm_get_gpu_reset_counter(ctx->ws); |
| 63 |
return (struct radeon_winsys_ctx*)ctx; |
| 64 |
} |
| 65 |
|
| 66 |
static void radeon_drm_ctx_destroy(struct radeon_winsys_ctx *ctx) |
| 67 |
{ |
| 68 |
FREE(ctx); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
radeon_drm_ctx_set_sw_reset_status(struct radeon_winsys_ctx *rwctx, enum pipe_reset_status status, |
| 73 |
const char *format, ...) |
| 74 |
{ |
| 75 |
/* TODO: we should do something better here */ |
| 76 |
va_list args; |
| 77 |
|
| 78 |
va_start(args, format); |
| 79 |
vfprintf(stderr, format, args); |
| 80 |
va_end(args); |
| 81 |
} |
| 82 |
|
| 83 |
static enum pipe_reset_status |
| 84 |
radeon_drm_ctx_query_reset_status(struct radeon_winsys_ctx *rctx, bool full_reset_only, |
| 85 |
bool *needs_reset, bool *reset_completed) |
| 86 |
{ |
| 87 |
struct radeon_ctx *ctx = (struct radeon_ctx*)rctx; |
| 88 |
|
| 89 |
unsigned latest = radeon_drm_get_gpu_reset_counter(ctx->ws); |
| 90 |
|
| 91 |
if (ctx->gpu_reset_counter == latest) { |
| 92 |
if (needs_reset) |
| 93 |
*needs_reset = false; |
| 94 |
if (reset_completed) |
| 95 |
*reset_completed = false; |
| 96 |
return PIPE_NO_RESET; |
| 97 |
} |
| 98 |
|
| 99 |
if (needs_reset) |
| 100 |
*needs_reset = true; |
| 101 |
if (reset_completed) |
| 102 |
*reset_completed = true; |
| 103 |
|
| 104 |
ctx->gpu_reset_counter = latest; |
| 105 |
return PIPE_UNKNOWN_CONTEXT_RESET; |
| 106 |
} |
| 107 |
|
| 108 |
static bool radeon_init_cs_context(struct radeon_cs_context *csc, |
| 109 |
struct radeon_drm_winsys *ws) |
| 110 |
{ |
| 111 |
int i; |
| 112 |
|
| 113 |
csc->fd = ws->fd; |
| 114 |
|
| 115 |
csc->chunks[0].chunk_id = RADEON_CHUNK_ID_IB; |
| 116 |
csc->chunks[0].length_dw = 0; |
| 117 |
csc->chunks[0].chunk_data = (uint64_t)(uintptr_t)csc->buf; |
| 118 |
csc->chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS; |
| 119 |
csc->chunks[1].length_dw = 0; |
| 120 |
csc->chunks[1].chunk_data = (uint64_t)(uintptr_t)csc->relocs; |
| 121 |
csc->chunks[2].chunk_id = RADEON_CHUNK_ID_FLAGS; |
| 122 |
csc->chunks[2].length_dw = 2; |
| 123 |
csc->chunks[2].chunk_data = (uint64_t)(uintptr_t)&csc->flags; |
| 124 |
|
| 125 |
csc->chunk_array[0] = (uint64_t)(uintptr_t)&csc->chunks[0]; |
| 126 |
csc->chunk_array[1] = (uint64_t)(uintptr_t)&csc->chunks[1]; |
| 127 |
csc->chunk_array[2] = (uint64_t)(uintptr_t)&csc->chunks[2]; |
| 128 |
|
| 129 |
csc->cs.chunks = (uint64_t)(uintptr_t)csc->chunk_array; |
| 130 |
|
| 131 |
for (i = 0; i < ARRAY_SIZE(csc->reloc_indices_hashlist); i++) { |
| 132 |
csc->reloc_indices_hashlist[i] = -1; |
| 133 |
} |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
static void radeon_cs_context_cleanup(struct radeon_winsys *rws, |
| 138 |
struct radeon_cs_context *csc) |
| 139 |
{ |
| 140 |
unsigned i; |
| 141 |
|
| 142 |
for (i = 0; i < csc->num_relocs; i++) { |
| 143 |
p_atomic_dec(&csc->relocs_bo[i].bo->num_cs_references); |
| 144 |
radeon_ws_bo_reference(rws, &csc->relocs_bo[i].bo, NULL); |
| 145 |
} |
| 146 |
for (i = 0; i < csc->num_slab_buffers; ++i) { |
| 147 |
p_atomic_dec(&csc->slab_buffers[i].bo->num_cs_references); |
| 148 |
radeon_ws_bo_reference(rws, &csc->slab_buffers[i].bo, NULL); |
| 149 |
} |
| 150 |
|
| 151 |
csc->num_relocs = 0; |
| 152 |
csc->num_validated_relocs = 0; |
| 153 |
csc->num_slab_buffers = 0; |
| 154 |
csc->chunks[0].length_dw = 0; |
| 155 |
csc->chunks[1].length_dw = 0; |
| 156 |
|
| 157 |
for (i = 0; i < ARRAY_SIZE(csc->reloc_indices_hashlist); i++) { |
| 158 |
csc->reloc_indices_hashlist[i] = -1; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
static void radeon_destroy_cs_context(struct radeon_winsys *rws, struct radeon_cs_context *csc) |
| 163 |
{ |
| 164 |
radeon_cs_context_cleanup(rws, csc); |
| 165 |
FREE(csc->slab_buffers); |
| 166 |
FREE(csc->relocs_bo); |
| 167 |
FREE(csc->relocs); |
| 168 |
} |
| 169 |
|
| 170 |
static bool |
| 171 |
radeon_drm_cs_create(struct radeon_cmdbuf *rcs, |
| 172 |
struct radeon_winsys_ctx *ctx, |
| 173 |
enum amd_ip_type ip_type, |
| 174 |
void (*flush)(void *ctx, unsigned flags, |
| 175 |
struct pipe_fence_handle **fence), |
| 176 |
void *flush_ctx) |
| 177 |
{ |
| 178 |
struct radeon_drm_winsys *ws = ((struct radeon_ctx*)ctx)->ws; |
| 179 |
struct radeon_drm_cs *cs; |
| 180 |
|
| 181 |
cs = CALLOC_STRUCT(radeon_drm_cs); |
| 182 |
if (!cs) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
util_queue_fence_init(&cs->flush_completed); |
| 186 |
|
| 187 |
cs->ws = ws; |
| 188 |
cs->flush_cs = flush; |
| 189 |
cs->flush_data = flush_ctx; |
| 190 |
|
| 191 |
if (!radeon_init_cs_context(&cs->csc1, cs->ws)) { |
| 192 |
FREE(cs); |
| 193 |
return false; |
| 194 |
} |
| 195 |
if (!radeon_init_cs_context(&cs->csc2, cs->ws)) { |
| 196 |
radeon_destroy_cs_context(&ws->base, &cs->csc1); |
| 197 |
FREE(cs); |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
/* Set the first command buffer as current. */ |
| 202 |
cs->csc = &cs->csc1; |
| 203 |
cs->cst = &cs->csc2; |
| 204 |
cs->ip_type = ip_type; |
| 205 |
|
| 206 |
memset(rcs, 0, sizeof(*rcs)); |
| 207 |
rcs->current.buf = cs->csc->buf; |
| 208 |
rcs->current.max_dw = ARRAY_SIZE(cs->csc->buf); |
| 209 |
rcs->priv = cs; |
| 210 |
|
| 211 |
p_atomic_inc(&ws->num_cs); |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
int radeon_lookup_buffer(struct radeon_winsys *rws, struct radeon_cs_context *csc, |
| 216 |
struct radeon_bo *bo) |
| 217 |
{ |
| 218 |
unsigned hash = bo->hash & (ARRAY_SIZE(csc->reloc_indices_hashlist)-1); |
| 219 |
struct radeon_bo_item *buffers; |
| 220 |
unsigned num_buffers; |
| 221 |
int i = csc->reloc_indices_hashlist[hash]; |
| 222 |
|
| 223 |
if (bo->handle) { |
| 224 |
buffers = csc->relocs_bo; |
| 225 |
num_buffers = csc->num_relocs; |
| 226 |
} else { |
| 227 |
buffers = csc->slab_buffers; |
| 228 |
num_buffers = csc->num_slab_buffers; |
| 229 |
} |
| 230 |
|
| 231 |
/* not found or found */ |
| 232 |
if (i == -1 || (i < num_buffers && buffers[i].bo == bo)) |
| 233 |
return i; |
| 234 |
|
| 235 |
/* Hash collision, look for the BO in the list of relocs linearly. */ |
| 236 |
for (i = num_buffers - 1; i >= 0; i--) { |
| 237 |
if (buffers[i].bo == bo) { |
| 238 |
/* Put this reloc in the hash list. |
| 239 |
* This will prevent additional hash collisions if there are |
| 240 |
* several consecutive lookup_buffer calls for the same buffer. |
| 241 |
* |
| 242 |
* Example: Assuming buffers A,B,C collide in the hash list, |
| 243 |
* the following sequence of relocs: |
| 244 |
* AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC |
| 245 |
* will collide here: ^ and here: ^, |
| 246 |
* meaning that we should get very few collisions in the end. */ |
| 247 |
csc->reloc_indices_hashlist[hash] = i; |
| 248 |
return i; |
| 249 |
} |
| 250 |
} |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
static unsigned radeon_lookup_or_add_real_buffer(struct radeon_drm_cs *cs, |
| 255 |
struct radeon_bo *bo) |
| 256 |
{ |
| 257 |
struct radeon_cs_context *csc = cs->csc; |
| 258 |
struct drm_radeon_cs_reloc *reloc; |
| 259 |
unsigned hash = bo->hash & (ARRAY_SIZE(csc->reloc_indices_hashlist)-1); |
| 260 |
int i = -1; |
| 261 |
|
| 262 |
i = radeon_lookup_buffer(&cs->ws->base, csc, bo); |
| 263 |
|
| 264 |
if (i >= 0) { |
| 265 |
/* For async DMA, every add_buffer call must add a buffer to the list |
| 266 |
* no matter how many duplicates there are. This is due to the fact |
| 267 |
* the DMA CS checker doesn't use NOP packets for offset patching, |
| 268 |
* but always uses the i-th buffer from the list to patch the i-th |
| 269 |
* offset. If there are N offsets in a DMA CS, there must also be N |
| 270 |
* buffers in the relocation list. |
| 271 |
* |
| 272 |
* This doesn't have to be done if virtual memory is enabled, |
| 273 |
* because there is no offset patching with virtual memory. |
| 274 |
*/ |
| 275 |
if (cs->ip_type != AMD_IP_SDMA || cs->ws->info.r600_has_virtual_memory) { |
| 276 |
return i; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/* New relocation, check if the backing array is large enough. */ |
| 281 |
if (csc->num_relocs >= csc->max_relocs) { |
| 282 |
uint32_t size; |
| 283 |
csc->max_relocs = MAX2(csc->max_relocs + 16, (unsigned)(csc->max_relocs * 1.3)); |
| 284 |
|
| 285 |
size = csc->max_relocs * sizeof(csc->relocs_bo[0]); |
| 286 |
csc->relocs_bo = realloc(csc->relocs_bo, size); |
| 287 |
|
| 288 |
size = csc->max_relocs * sizeof(struct drm_radeon_cs_reloc); |
| 289 |
csc->relocs = realloc(csc->relocs, size); |
| 290 |
|
| 291 |
csc->chunks[1].chunk_data = (uint64_t)(uintptr_t)csc->relocs; |
| 292 |
} |
| 293 |
|
| 294 |
/* Initialize the new relocation. */ |
| 295 |
csc->relocs_bo[csc->num_relocs].bo = NULL; |
| 296 |
csc->relocs_bo[csc->num_relocs].u.real.priority_usage = 0; |
| 297 |
radeon_ws_bo_reference(&cs->ws->base, &csc->relocs_bo[csc->num_relocs].bo, bo); |
| 298 |
p_atomic_inc(&bo->num_cs_references); |
| 299 |
reloc = &csc->relocs[csc->num_relocs]; |
| 300 |
reloc->handle = bo->handle; |
| 301 |
reloc->read_domains = 0; |
| 302 |
reloc->write_domain = 0; |
| 303 |
reloc->flags = 0; |
| 304 |
|
| 305 |
csc->reloc_indices_hashlist[hash] = csc->num_relocs; |
| 306 |
|
| 307 |
csc->chunks[1].length_dw += RELOC_DWORDS; |
| 308 |
|
| 309 |
return csc->num_relocs++; |
| 310 |
} |
| 311 |
|
| 312 |
static int radeon_lookup_or_add_slab_buffer(struct radeon_drm_cs *cs, |
| 313 |
struct radeon_bo *bo) |
| 314 |
{ |
| 315 |
struct radeon_cs_context *csc = cs->csc; |
| 316 |
unsigned hash; |
| 317 |
struct radeon_bo_item *item; |
| 318 |
int idx; |
| 319 |
int real_idx; |
| 320 |
|
| 321 |
idx = radeon_lookup_buffer(&cs->ws->base, csc, bo); |
| 322 |
if (idx >= 0) |
| 323 |
return idx; |
| 324 |
|
| 325 |
real_idx = radeon_lookup_or_add_real_buffer(cs, bo->u.slab.real); |
| 326 |
|
| 327 |
/* Check if the backing array is large enough. */ |
| 328 |
if (csc->num_slab_buffers >= csc->max_slab_buffers) { |
| 329 |
unsigned new_max = MAX2(csc->max_slab_buffers + 16, |
| 330 |
(unsigned)(csc->max_slab_buffers * 1.3)); |
| 331 |
struct radeon_bo_item *new_buffers = |
| 332 |
REALLOC(csc->slab_buffers, |
| 333 |
csc->max_slab_buffers * sizeof(*new_buffers), |
| 334 |
new_max * sizeof(*new_buffers)); |
| 335 |
if (!new_buffers) { |
| 336 |
fprintf(stderr, "radeon_lookup_or_add_slab_buffer: allocation failure\n"); |
| 337 |
return -1; |
| 338 |
} |
| 339 |
|
| 340 |
csc->max_slab_buffers = new_max; |
| 341 |
csc->slab_buffers = new_buffers; |
| 342 |
} |
| 343 |
|
| 344 |
/* Initialize the new relocation. */ |
| 345 |
idx = csc->num_slab_buffers++; |
| 346 |
item = &csc->slab_buffers[idx]; |
| 347 |
|
| 348 |
item->bo = NULL; |
| 349 |
item->u.slab.real_idx = real_idx; |
| 350 |
radeon_ws_bo_reference(&cs->ws->base, &item->bo, bo); |
| 351 |
p_atomic_inc(&bo->num_cs_references); |
| 352 |
|
| 353 |
hash = bo->hash & (ARRAY_SIZE(csc->reloc_indices_hashlist)-1); |
| 354 |
csc->reloc_indices_hashlist[hash] = idx; |
| 355 |
|
| 356 |
return idx; |
| 357 |
} |
| 358 |
|
| 359 |
static unsigned radeon_drm_cs_add_buffer(struct radeon_cmdbuf *rcs, |
| 360 |
struct pb_buffer_lean *buf, |
| 361 |
unsigned usage, |
| 362 |
enum radeon_bo_domain domains) |
| 363 |
{ |
| 364 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 365 |
struct radeon_bo *bo = (struct radeon_bo*)buf; |
| 366 |
enum radeon_bo_domain added_domains; |
| 367 |
|
| 368 |
/* If VRAM is just stolen system memory, allow both VRAM and |
| 369 |
* GTT, whichever has free space. If a buffer is evicted from |
| 370 |
* VRAM to GTT, it will stay there. |
| 371 |
*/ |
| 372 |
if (!cs->ws->info.has_dedicated_vram) |
| 373 |
domains |= RADEON_DOMAIN_GTT; |
| 374 |
|
| 375 |
enum radeon_bo_domain rd = usage & RADEON_USAGE_READ ? domains : 0; |
| 376 |
enum radeon_bo_domain wd = usage & RADEON_USAGE_WRITE ? domains : 0; |
| 377 |
struct drm_radeon_cs_reloc *reloc; |
| 378 |
int index; |
| 379 |
|
| 380 |
if (!bo->handle) { |
| 381 |
index = radeon_lookup_or_add_slab_buffer(cs, bo); |
| 382 |
if (index < 0) |
| 383 |
return 0; |
| 384 |
|
| 385 |
index = cs->csc->slab_buffers[index].u.slab.real_idx; |
| 386 |
} else { |
| 387 |
index = radeon_lookup_or_add_real_buffer(cs, bo); |
| 388 |
} |
| 389 |
|
| 390 |
reloc = &cs->csc->relocs[index]; |
| 391 |
added_domains = (rd | wd) & ~(reloc->read_domains | reloc->write_domain); |
| 392 |
reloc->read_domains |= rd; |
| 393 |
reloc->write_domain |= wd; |
| 394 |
|
| 395 |
/* The priority must be in [0, 15]. It's used by the kernel memory management. */ |
| 396 |
unsigned priority = usage & RADEON_ALL_PRIORITIES; |
| 397 |
unsigned bo_priority = util_last_bit(priority) / 2; |
| 398 |
reloc->flags = MAX2(reloc->flags, bo_priority); |
| 399 |
cs->csc->relocs_bo[index].u.real.priority_usage |= priority; |
| 400 |
|
| 401 |
if (added_domains & RADEON_DOMAIN_VRAM) |
| 402 |
rcs->used_vram_kb += bo->base.size / 1024; |
| 403 |
else if (added_domains & RADEON_DOMAIN_GTT) |
| 404 |
rcs->used_gart_kb += bo->base.size / 1024; |
| 405 |
|
| 406 |
return index; |
| 407 |
} |
| 408 |
|
| 409 |
static int radeon_drm_cs_lookup_buffer(struct radeon_cmdbuf *rcs, |
| 410 |
struct pb_buffer_lean *buf) |
| 411 |
{ |
| 412 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 413 |
|
| 414 |
return radeon_lookup_buffer(&cs->ws->base, cs->csc, (struct radeon_bo*)buf); |
| 415 |
} |
| 416 |
|
| 417 |
static bool radeon_drm_cs_validate(struct radeon_cmdbuf *rcs) |
| 418 |
{ |
| 419 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 420 |
bool status = |
| 421 |
rcs->used_gart_kb < cs->ws->info.gart_size_kb * 0.8 && |
| 422 |
rcs->used_vram_kb < cs->ws->info.vram_size_kb * 0.8; |
| 423 |
|
| 424 |
if (status) { |
| 425 |
cs->csc->num_validated_relocs = cs->csc->num_relocs; |
| 426 |
} else { |
| 427 |
/* Remove lately-added buffers. The validation failed with them |
| 428 |
* and the CS is about to be flushed because of that. Keep only |
| 429 |
* the already-validated buffers. */ |
| 430 |
unsigned i; |
| 431 |
|
| 432 |
for (i = cs->csc->num_validated_relocs; i < cs->csc->num_relocs; i++) { |
| 433 |
p_atomic_dec(&cs->csc->relocs_bo[i].bo->num_cs_references); |
| 434 |
radeon_ws_bo_reference(&cs->ws->base, &cs->csc->relocs_bo[i].bo, NULL); |
| 435 |
} |
| 436 |
cs->csc->num_relocs = cs->csc->num_validated_relocs; |
| 437 |
|
| 438 |
/* Flush if there are any relocs. Clean up otherwise. */ |
| 439 |
if (cs->csc->num_relocs) { |
| 440 |
cs->flush_cs(cs->flush_data, |
| 441 |
RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL); |
| 442 |
} else { |
| 443 |
radeon_cs_context_cleanup(&cs->ws->base, cs->csc); |
| 444 |
rcs->used_vram_kb = 0; |
| 445 |
rcs->used_gart_kb = 0; |
| 446 |
|
| 447 |
assert(rcs->current.cdw == 0); |
| 448 |
if (rcs->current.cdw != 0) { |
| 449 |
fprintf(stderr, "radeon: Unexpected error in %s.\n", __func__); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
return status; |
| 454 |
} |
| 455 |
|
| 456 |
static bool radeon_drm_cs_check_space(struct radeon_cmdbuf *rcs, unsigned dw) |
| 457 |
{ |
| 458 |
assert(rcs->current.cdw <= rcs->current.max_dw); |
| 459 |
return rcs->current.max_dw - rcs->current.cdw >= dw; |
| 460 |
} |
| 461 |
|
| 462 |
static unsigned radeon_drm_cs_get_buffer_list(struct radeon_cmdbuf *rcs, |
| 463 |
struct radeon_bo_list_item *list) |
| 464 |
{ |
| 465 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 466 |
int i; |
| 467 |
|
| 468 |
if (list) { |
| 469 |
for (i = 0; i < cs->csc->num_relocs; i++) { |
| 470 |
list[i].bo_size = cs->csc->relocs_bo[i].bo->base.size; |
| 471 |
list[i].vm_address = cs->csc->relocs_bo[i].bo->va; |
| 472 |
list[i].priority_usage = cs->csc->relocs_bo[i].u.real.priority_usage; |
| 473 |
} |
| 474 |
} |
| 475 |
return cs->csc->num_relocs; |
| 476 |
} |
| 477 |
|
| 478 |
void radeon_drm_cs_emit_ioctl_oneshot(void *job, void *gdata, int thread_index) |
| 479 |
{ |
| 480 |
struct radeon_drm_cs *cs = (struct radeon_drm_cs*)job; |
| 481 |
struct radeon_cs_context *csc = cs->cst; |
| 482 |
unsigned i; |
| 483 |
int r; |
| 484 |
|
| 485 |
r = drmCommandWriteRead(csc->fd, DRM_RADEON_CS, |
| 486 |
&csc->cs, sizeof(struct drm_radeon_cs)); |
| 487 |
if (r) { |
| 488 |
if (r == -ENOMEM) |
| 489 |
fprintf(stderr, "radeon: Not enough memory for command submission.\n"); |
| 490 |
else if (debug_get_bool_option("RADEON_DUMP_CS", false)) { |
| 491 |
unsigned i; |
| 492 |
|
| 493 |
fprintf(stderr, "radeon: The kernel rejected CS, dumping...\n"); |
| 494 |
for (i = 0; i < csc->chunks[0].length_dw; i++) { |
| 495 |
fprintf(stderr, "0x%08X\n", csc->buf[i]); |
| 496 |
} |
| 497 |
} else { |
| 498 |
fprintf(stderr, "radeon: The kernel rejected CS, " |
| 499 |
"see dmesg for more information (%i).\n", r); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
for (i = 0; i < csc->num_relocs; i++) |
| 504 |
p_atomic_dec(&csc->relocs_bo[i].bo->num_active_ioctls); |
| 505 |
for (i = 0; i < csc->num_slab_buffers; i++) |
| 506 |
p_atomic_dec(&csc->slab_buffers[i].bo->num_active_ioctls); |
| 507 |
|
| 508 |
radeon_cs_context_cleanup(&cs->ws->base, csc); |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* Make sure previous submission of this cs are completed |
| 513 |
*/ |
| 514 |
void radeon_drm_cs_sync_flush(struct radeon_cmdbuf *rcs) |
| 515 |
{ |
| 516 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 517 |
|
| 518 |
/* Wait for any pending ioctl of this CS to complete. */ |
| 519 |
if (util_queue_is_initialized(&cs->ws->cs_queue)) |
| 520 |
util_queue_fence_wait(&cs->flush_completed); |
| 521 |
} |
| 522 |
|
| 523 |
/* Add the given fence to a slab buffer fence list. |
| 524 |
* |
| 525 |
* There is a potential race condition when bo participates in submissions on |
| 526 |
* two or more threads simultaneously. Since we do not know which of the |
| 527 |
* submissions will be sent to the GPU first, we have to keep the fences |
| 528 |
* of all submissions. |
| 529 |
* |
| 530 |
* However, fences that belong to submissions that have already returned from |
| 531 |
* their respective ioctl do not have to be kept, because we know that they |
| 532 |
* will signal earlier. |
| 533 |
*/ |
| 534 |
static void radeon_bo_slab_fence(struct radeon_winsys *rws, struct radeon_bo *bo, |
| 535 |
struct radeon_bo *fence) |
| 536 |
{ |
| 537 |
unsigned dst; |
| 538 |
|
| 539 |
assert(fence->num_cs_references); |
| 540 |
|
| 541 |
/* Cleanup older fences */ |
| 542 |
dst = 0; |
| 543 |
for (unsigned src = 0; src < bo->u.slab.num_fences; ++src) { |
| 544 |
if (bo->u.slab.fences[src]->num_cs_references) { |
| 545 |
bo->u.slab.fences[dst] = bo->u.slab.fences[src]; |
| 546 |
dst++; |
| 547 |
} else { |
| 548 |
radeon_ws_bo_reference(rws, &bo->u.slab.fences[src], NULL); |
| 549 |
} |
| 550 |
} |
| 551 |
bo->u.slab.num_fences = dst; |
| 552 |
|
| 553 |
/* Check available space for the new fence */ |
| 554 |
if (bo->u.slab.num_fences >= bo->u.slab.max_fences) { |
| 555 |
unsigned new_max_fences = bo->u.slab.max_fences + 1; |
| 556 |
struct radeon_bo **new_fences = REALLOC(bo->u.slab.fences, |
| 557 |
bo->u.slab.max_fences * sizeof(*new_fences), |
| 558 |
new_max_fences * sizeof(*new_fences)); |
| 559 |
if (!new_fences) { |
| 560 |
fprintf(stderr, "radeon_bo_slab_fence: allocation failure, dropping fence\n"); |
| 561 |
return; |
| 562 |
} |
| 563 |
|
| 564 |
bo->u.slab.fences = new_fences; |
| 565 |
bo->u.slab.max_fences = new_max_fences; |
| 566 |
} |
| 567 |
|
| 568 |
/* Add the new fence */ |
| 569 |
bo->u.slab.fences[bo->u.slab.num_fences] = NULL; |
| 570 |
radeon_ws_bo_reference(rws, &bo->u.slab.fences[bo->u.slab.num_fences], fence); |
| 571 |
bo->u.slab.num_fences++; |
| 572 |
} |
| 573 |
|
| 574 |
static int radeon_drm_cs_flush(struct radeon_cmdbuf *rcs, |
| 575 |
unsigned flags, |
| 576 |
struct pipe_fence_handle **pfence) |
| 577 |
{ |
| 578 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 579 |
struct radeon_cs_context *tmp; |
| 580 |
|
| 581 |
switch (cs->ip_type) { |
| 582 |
case AMD_IP_SDMA: |
| 583 |
/* pad DMA ring to 8 DWs */ |
| 584 |
if (cs->ws->info.gfx_level <= GFX6) { |
| 585 |
while (rcs->current.cdw & 7) |
| 586 |
radeon_emit(rcs, 0xf0000000); /* NOP packet */ |
| 587 |
} else { |
| 588 |
while (rcs->current.cdw & 7) |
| 589 |
radeon_emit(rcs, 0x00000000); /* NOP packet */ |
| 590 |
} |
| 591 |
break; |
| 592 |
case AMD_IP_GFX: |
| 593 |
/* pad GFX ring to 8 DWs to meet CP fetch alignment requirements |
| 594 |
* r6xx, requires at least 4 dw alignment to avoid a hw bug. |
| 595 |
*/ |
| 596 |
if (cs->ws->info.gfx_ib_pad_with_type2) { |
| 597 |
while (rcs->current.cdw & 7) |
| 598 |
radeon_emit(rcs, 0x80000000); /* type2 nop packet */ |
| 599 |
} else { |
| 600 |
while (rcs->current.cdw & 7) |
| 601 |
radeon_emit(rcs, 0xffff1000); /* type3 nop packet */ |
| 602 |
} |
| 603 |
break; |
| 604 |
case AMD_IP_UVD: |
| 605 |
while (rcs->current.cdw & 15) |
| 606 |
radeon_emit(rcs, 0x80000000); /* type2 nop packet */ |
| 607 |
break; |
| 608 |
default: |
| 609 |
break; |
| 610 |
} |
| 611 |
|
| 612 |
if (rcs->current.cdw > rcs->current.max_dw) { |
| 613 |
fprintf(stderr, "radeon: command stream overflowed\n"); |
| 614 |
} |
| 615 |
|
| 616 |
if (pfence || cs->csc->num_slab_buffers) { |
| 617 |
struct pipe_fence_handle *fence; |
| 618 |
|
| 619 |
if (cs->next_fence) { |
| 620 |
fence = cs->next_fence; |
| 621 |
cs->next_fence = NULL; |
| 622 |
} else { |
| 623 |
fence = radeon_cs_create_fence(rcs); |
| 624 |
} |
| 625 |
|
| 626 |
if (fence) { |
| 627 |
if (pfence) |
| 628 |
radeon_fence_reference(&cs->ws->base, pfence, fence); |
| 629 |
|
| 630 |
mtx_lock(&cs->ws->bo_fence_lock); |
| 631 |
for (unsigned i = 0; i < cs->csc->num_slab_buffers; ++i) { |
| 632 |
struct radeon_bo *bo = cs->csc->slab_buffers[i].bo; |
| 633 |
p_atomic_inc(&bo->num_active_ioctls); |
| 634 |
radeon_bo_slab_fence(&cs->ws->base, bo, (struct radeon_bo *)fence); |
| 635 |
} |
| 636 |
mtx_unlock(&cs->ws->bo_fence_lock); |
| 637 |
|
| 638 |
radeon_fence_reference(&cs->ws->base, &fence, NULL); |
| 639 |
} |
| 640 |
} else { |
| 641 |
radeon_fence_reference(&cs->ws->base, &cs->next_fence, NULL); |
| 642 |
} |
| 643 |
|
| 644 |
radeon_drm_cs_sync_flush(rcs); |
| 645 |
|
| 646 |
/* Swap command streams. */ |
| 647 |
tmp = cs->csc; |
| 648 |
cs->csc = cs->cst; |
| 649 |
cs->cst = tmp; |
| 650 |
|
| 651 |
/* If the CS is not empty or overflowed, emit it in a separate thread. */ |
| 652 |
if (rcs->current.cdw && rcs->current.cdw <= rcs->current.max_dw && |
| 653 |
!cs->ws->noop_cs && !(flags & RADEON_FLUSH_NOOP)) { |
| 654 |
unsigned i, num_relocs; |
| 655 |
|
| 656 |
num_relocs = cs->cst->num_relocs; |
| 657 |
|
| 658 |
cs->cst->chunks[0].length_dw = rcs->current.cdw; |
| 659 |
|
| 660 |
for (i = 0; i < num_relocs; i++) { |
| 661 |
/* Update the number of active asynchronous CS ioctls for the buffer. */ |
| 662 |
p_atomic_inc(&cs->cst->relocs_bo[i].bo->num_active_ioctls); |
| 663 |
} |
| 664 |
|
| 665 |
switch (cs->ip_type) { |
| 666 |
case AMD_IP_SDMA: |
| 667 |
cs->cst->flags[0] = 0; |
| 668 |
cs->cst->flags[1] = RADEON_CS_RING_DMA; |
| 669 |
cs->cst->cs.num_chunks = 3; |
| 670 |
if (cs->ws->info.r600_has_virtual_memory) { |
| 671 |
cs->cst->flags[0] |= RADEON_CS_USE_VM; |
| 672 |
} |
| 673 |
break; |
| 674 |
|
| 675 |
case AMD_IP_UVD: |
| 676 |
cs->cst->flags[0] = 0; |
| 677 |
cs->cst->flags[1] = RADEON_CS_RING_UVD; |
| 678 |
cs->cst->cs.num_chunks = 3; |
| 679 |
break; |
| 680 |
|
| 681 |
case AMD_IP_VCE: |
| 682 |
cs->cst->flags[0] = 0; |
| 683 |
cs->cst->flags[1] = RADEON_CS_RING_VCE; |
| 684 |
cs->cst->cs.num_chunks = 3; |
| 685 |
break; |
| 686 |
|
| 687 |
default: |
| 688 |
case AMD_IP_GFX: |
| 689 |
case AMD_IP_COMPUTE: |
| 690 |
cs->cst->flags[0] = RADEON_CS_KEEP_TILING_FLAGS; |
| 691 |
cs->cst->flags[1] = RADEON_CS_RING_GFX; |
| 692 |
cs->cst->cs.num_chunks = 3; |
| 693 |
|
| 694 |
if (cs->ws->info.r600_has_virtual_memory) { |
| 695 |
cs->cst->flags[0] |= RADEON_CS_USE_VM; |
| 696 |
cs->cst->cs.num_chunks = 3; |
| 697 |
} |
| 698 |
if (flags & PIPE_FLUSH_END_OF_FRAME) { |
| 699 |
cs->cst->flags[0] |= RADEON_CS_END_OF_FRAME; |
| 700 |
cs->cst->cs.num_chunks = 3; |
| 701 |
} |
| 702 |
if (cs->ip_type == AMD_IP_COMPUTE) { |
| 703 |
cs->cst->flags[1] = RADEON_CS_RING_COMPUTE; |
| 704 |
cs->cst->cs.num_chunks = 3; |
| 705 |
} |
| 706 |
break; |
| 707 |
} |
| 708 |
|
| 709 |
if (util_queue_is_initialized(&cs->ws->cs_queue)) { |
| 710 |
util_queue_add_job(&cs->ws->cs_queue, cs, &cs->flush_completed, |
| 711 |
radeon_drm_cs_emit_ioctl_oneshot, NULL, 0); |
| 712 |
if (!(flags & PIPE_FLUSH_ASYNC)) |
| 713 |
radeon_drm_cs_sync_flush(rcs); |
| 714 |
} else { |
| 715 |
radeon_drm_cs_emit_ioctl_oneshot(cs, NULL, 0); |
| 716 |
} |
| 717 |
} else { |
| 718 |
radeon_cs_context_cleanup(&cs->ws->base, cs->cst); |
| 719 |
} |
| 720 |
|
| 721 |
/* Prepare a new CS. */ |
| 722 |
rcs->current.buf = cs->csc->buf; |
| 723 |
rcs->current.cdw = 0; |
| 724 |
rcs->used_vram_kb = 0; |
| 725 |
rcs->used_gart_kb = 0; |
| 726 |
|
| 727 |
if (cs->ip_type == AMD_IP_GFX) |
| 728 |
cs->ws->num_gfx_IBs++; |
| 729 |
else if (cs->ip_type == AMD_IP_SDMA) |
| 730 |
cs->ws->num_sdma_IBs++; |
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
static void radeon_drm_cs_destroy(struct radeon_cmdbuf *rcs) |
| 735 |
{ |
| 736 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 737 |
|
| 738 |
if (!cs) |
| 739 |
return; |
| 740 |
|
| 741 |
radeon_drm_cs_sync_flush(rcs); |
| 742 |
util_queue_fence_destroy(&cs->flush_completed); |
| 743 |
radeon_cs_context_cleanup(&cs->ws->base, &cs->csc1); |
| 744 |
radeon_cs_context_cleanup(&cs->ws->base, &cs->csc2); |
| 745 |
p_atomic_dec(&cs->ws->num_cs); |
| 746 |
radeon_destroy_cs_context(&cs->ws->base, &cs->csc1); |
| 747 |
radeon_destroy_cs_context(&cs->ws->base, &cs->csc2); |
| 748 |
radeon_fence_reference(&cs->ws->base, &cs->next_fence, NULL); |
| 749 |
FREE(cs); |
| 750 |
} |
| 751 |
|
| 752 |
static bool radeon_bo_is_referenced(struct radeon_cmdbuf *rcs, |
| 753 |
struct pb_buffer_lean *_buf, |
| 754 |
unsigned usage) |
| 755 |
{ |
| 756 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 757 |
struct radeon_bo *bo = (struct radeon_bo*)_buf; |
| 758 |
int index; |
| 759 |
|
| 760 |
if (!bo->num_cs_references) |
| 761 |
return false; |
| 762 |
|
| 763 |
index = radeon_lookup_buffer(&cs->ws->base, cs->csc, bo); |
| 764 |
if (index == -1) |
| 765 |
return false; |
| 766 |
|
| 767 |
if (!bo->handle) |
| 768 |
index = cs->csc->slab_buffers[index].u.slab.real_idx; |
| 769 |
|
| 770 |
if ((usage & RADEON_USAGE_WRITE) && cs->csc->relocs[index].write_domain) |
| 771 |
return true; |
| 772 |
if ((usage & RADEON_USAGE_READ) && cs->csc->relocs[index].read_domains) |
| 773 |
return true; |
| 774 |
|
| 775 |
return false; |
| 776 |
} |
| 777 |
|
| 778 |
/* FENCES */ |
| 779 |
|
| 780 |
static struct pipe_fence_handle *radeon_cs_create_fence(struct radeon_cmdbuf *rcs) |
| 781 |
{ |
| 782 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 783 |
struct pb_buffer_lean *fence; |
| 784 |
|
| 785 |
/* Create a fence, which is a dummy BO. */ |
| 786 |
fence = cs->ws->base.buffer_create(&cs->ws->base, 1, 1, |
| 787 |
RADEON_DOMAIN_GTT, |
| 788 |
RADEON_FLAG_NO_SUBALLOC |
| 789 |
| RADEON_FLAG_NO_INTERPROCESS_SHARING); |
| 790 |
if (!fence) |
| 791 |
return NULL; |
| 792 |
|
| 793 |
/* Add the fence as a dummy relocation. */ |
| 794 |
cs->ws->base.cs_add_buffer(rcs, fence, |
| 795 |
RADEON_USAGE_READWRITE | RADEON_PRIO_FENCE_TRACE, RADEON_DOMAIN_GTT); |
| 796 |
return (struct pipe_fence_handle*)fence; |
| 797 |
} |
| 798 |
|
| 799 |
static bool radeon_fence_wait(struct radeon_winsys *ws, |
| 800 |
struct pipe_fence_handle *fence, |
| 801 |
uint64_t timeout) |
| 802 |
{ |
| 803 |
return ws->buffer_wait(ws, (struct pb_buffer_lean*)fence, timeout, |
| 804 |
RADEON_USAGE_READWRITE); |
| 805 |
} |
| 806 |
|
| 807 |
static void radeon_fence_reference(struct radeon_winsys *ws, |
| 808 |
struct pipe_fence_handle **dst, |
| 809 |
struct pipe_fence_handle *src) |
| 810 |
{ |
| 811 |
radeon_bo_reference(ws, (struct pb_buffer_lean**)dst, (struct pb_buffer_lean*)src); |
| 812 |
} |
| 813 |
|
| 814 |
static struct pipe_fence_handle *radeon_drm_cs_get_next_fence(struct radeon_cmdbuf *rcs) |
| 815 |
{ |
| 816 |
struct radeon_drm_cs *cs = radeon_drm_cs(rcs); |
| 817 |
struct pipe_fence_handle *fence = NULL; |
| 818 |
|
| 819 |
if (cs->next_fence) { |
| 820 |
radeon_fence_reference(&cs->ws->base, &fence, cs->next_fence); |
| 821 |
return fence; |
| 822 |
} |
| 823 |
|
| 824 |
fence = radeon_cs_create_fence(rcs); |
| 825 |
if (!fence) |
| 826 |
return NULL; |
| 827 |
|
| 828 |
radeon_fence_reference(&cs->ws->base, &cs->next_fence, fence); |
| 829 |
return fence; |
| 830 |
} |
| 831 |
|
| 832 |
static void |
| 833 |
radeon_drm_cs_add_fence_dependency(struct radeon_cmdbuf *rcs, |
| 834 |
struct pipe_fence_handle *fence, |
| 835 |
uint64_t timeline_point) |
| 836 |
{ |
| 837 |
/* TODO: Handle the following unlikely multi-threaded scenario: |
| 838 |
* |
| 839 |
* Thread 1 / Context 1 Thread 2 / Context 2 |
| 840 |
* -------------------- -------------------- |
| 841 |
* f = cs_get_next_fence() |
| 842 |
* cs_add_fence_dependency(f) |
| 843 |
* cs_flush() |
| 844 |
* cs_flush() |
| 845 |
* |
| 846 |
* We currently assume that this does not happen because we don't support |
| 847 |
* asynchronous flushes on Radeon. |
| 848 |
*/ |
| 849 |
} |
| 850 |
|
| 851 |
void radeon_drm_cs_init_functions(struct radeon_drm_winsys *ws) |
| 852 |
{ |
| 853 |
ws->base.ctx_create = radeon_drm_ctx_create; |
| 854 |
ws->base.ctx_destroy = radeon_drm_ctx_destroy; |
| 855 |
ws->base.ctx_set_sw_reset_status = radeon_drm_ctx_set_sw_reset_status; |
| 856 |
ws->base.ctx_query_reset_status = radeon_drm_ctx_query_reset_status; |
| 857 |
ws->base.cs_create = radeon_drm_cs_create; |
| 858 |
ws->base.cs_destroy = radeon_drm_cs_destroy; |
| 859 |
ws->base.cs_add_buffer = radeon_drm_cs_add_buffer; |
| 860 |
ws->base.cs_lookup_buffer = radeon_drm_cs_lookup_buffer; |
| 861 |
ws->base.cs_validate = radeon_drm_cs_validate; |
| 862 |
ws->base.cs_check_space = radeon_drm_cs_check_space; |
| 863 |
ws->base.cs_get_buffer_list = radeon_drm_cs_get_buffer_list; |
| 864 |
ws->base.cs_flush = radeon_drm_cs_flush; |
| 865 |
ws->base.cs_get_next_fence = radeon_drm_cs_get_next_fence; |
| 866 |
ws->base.cs_is_buffer_referenced = radeon_bo_is_referenced; |
| 867 |
ws->base.cs_sync_flush = radeon_drm_cs_sync_flush; |
| 868 |
ws->base.cs_add_fence_dependency = radeon_drm_cs_add_fence_dependency; |
| 869 |
ws->base.fence_wait = radeon_fence_wait; |
| 870 |
ws->base.fence_reference = radeon_fence_reference; |
| 871 |
} |