2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 #include <amdgpu_drm.h>
30 #include "amdgpu_id.h"
31 #include "radv_radeon_winsys.h"
32 #include "radv_amdgpu_cs.h"
33 #include "radv_amdgpu_bo.h"
36 struct radv_amdgpu_cs
{
37 struct radeon_winsys_cs base
;
38 struct radv_amdgpu_winsys
*ws
;
40 struct amdgpu_cs_ib_info ib
;
42 struct radeon_winsys_bo
*ib_buffer
;
44 unsigned max_num_buffers
;
46 amdgpu_bo_handle
*handles
;
49 struct radeon_winsys_bo
**old_ib_buffers
;
50 unsigned num_old_ib_buffers
;
51 unsigned max_num_old_ib_buffers
;
52 unsigned *ib_size_ptr
;
56 int buffer_hash_table
[1024];
59 static inline struct radv_amdgpu_cs
*
60 radv_amdgpu_cs(struct radeon_winsys_cs
*base
)
62 return (struct radv_amdgpu_cs
*)base
;
66 static struct radeon_winsys_fence
*radv_amdgpu_create_fence()
68 struct radv_amdgpu_cs_fence
*fence
= calloc(1, sizeof(struct amdgpu_cs_fence
));
69 return (struct radeon_winsys_fence
*)fence
;
72 static void radv_amdgpu_destroy_fence(struct radeon_winsys_fence
*_fence
)
74 struct amdgpu_cs_fence
*fence
= (struct amdgpu_cs_fence
*)_fence
;
78 static bool radv_amdgpu_fence_wait(struct radeon_winsys
*_ws
,
79 struct radeon_winsys_fence
*_fence
,
83 struct amdgpu_cs_fence
*fence
= (struct amdgpu_cs_fence
*)_fence
;
84 unsigned flags
= absolute
? AMDGPU_QUERY_FENCE_TIMEOUT_IS_ABSOLUTE
: 0;
88 /* Now use the libdrm query. */
89 r
= amdgpu_cs_query_fence_status(fence
,
95 fprintf(stderr
, "amdgpu: radv_amdgpu_cs_query_fence_status failed.\n");
105 static void radv_amdgpu_cs_destroy(struct radeon_winsys_cs
*rcs
)
107 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(rcs
);
110 cs
->ws
->base
.buffer_destroy(cs
->ib_buffer
);
114 for (unsigned i
= 0; i
< cs
->num_old_ib_buffers
; ++i
)
115 cs
->ws
->base
.buffer_destroy(cs
->old_ib_buffers
[i
]);
117 free(cs
->old_ib_buffers
);
119 free(cs
->priorities
);
123 static boolean
radv_amdgpu_init_cs(struct radv_amdgpu_cs
*cs
,
124 enum ring_type ring_type
)
126 for (int i
= 0; i
< ARRAY_SIZE(cs
->buffer_hash_table
); ++i
)
127 cs
->buffer_hash_table
[i
] = -1;
132 static struct radeon_winsys_cs
*
133 radv_amdgpu_cs_create(struct radeon_winsys
*ws
,
134 enum ring_type ring_type
)
136 struct radv_amdgpu_cs
*cs
;
137 uint32_t ib_size
= 20 * 1024 * 4;
138 cs
= calloc(1, sizeof(struct radv_amdgpu_cs
));
142 cs
->ws
= radv_amdgpu_winsys(ws
);
143 radv_amdgpu_init_cs(cs
, RING_GFX
);
145 if (cs
->ws
->use_ib_bos
) {
146 cs
->ib_buffer
= ws
->buffer_create(ws
, ib_size
, 0,
148 RADEON_FLAG_CPU_ACCESS
);
149 if (!cs
->ib_buffer
) {
154 cs
->ib_mapped
= ws
->buffer_map(cs
->ib_buffer
);
155 if (!cs
->ib_mapped
) {
156 ws
->buffer_destroy(cs
->ib_buffer
);
161 cs
->ib
.ib_mc_address
= radv_amdgpu_winsys_bo(cs
->ib_buffer
)->va
;
162 cs
->base
.buf
= (uint32_t *)cs
->ib_mapped
;
163 cs
->base
.max_dw
= ib_size
/ 4 - 4;
164 cs
->ib_size_ptr
= &cs
->ib
.size
;
167 ws
->cs_add_buffer(&cs
->base
, cs
->ib_buffer
, 8);
169 cs
->base
.buf
= malloc(16384);
170 cs
->base
.max_dw
= 4096;
180 static void radv_amdgpu_cs_grow(struct radeon_winsys_cs
*_cs
, size_t min_size
)
182 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(_cs
);
183 uint64_t ib_size
= MAX2(min_size
* 4 + 16, cs
->base
.max_dw
* 4 * 2);
185 /* max that fits in the chain size field. */
186 ib_size
= MIN2(ib_size
, 0xfffff);
193 if (!cs
->ws
->use_ib_bos
) {
194 uint32_t *new_buf
= realloc(cs
->base
.buf
, ib_size
);
196 cs
->base
.buf
= new_buf
;
197 cs
->base
.max_dw
= ib_size
/ 4;
205 while (!cs
->base
.cdw
|| (cs
->base
.cdw
& 7) != 4)
206 cs
->base
.buf
[cs
->base
.cdw
++] = 0xffff1000;
208 *cs
->ib_size_ptr
|= cs
->base
.cdw
+ 4;
210 if (cs
->num_old_ib_buffers
== cs
->max_num_old_ib_buffers
) {
211 cs
->max_num_old_ib_buffers
= MAX2(1, cs
->max_num_old_ib_buffers
* 2);
212 cs
->old_ib_buffers
= realloc(cs
->old_ib_buffers
,
213 cs
->max_num_old_ib_buffers
* sizeof(void*));
216 cs
->old_ib_buffers
[cs
->num_old_ib_buffers
++] = cs
->ib_buffer
;
218 cs
->ib_buffer
= cs
->ws
->base
.buffer_create(&cs
->ws
->base
, ib_size
, 0,
220 RADEON_FLAG_CPU_ACCESS
);
222 if (!cs
->ib_buffer
) {
225 cs
->ib_buffer
= cs
->old_ib_buffers
[--cs
->num_old_ib_buffers
];
228 cs
->ib_mapped
= cs
->ws
->base
.buffer_map(cs
->ib_buffer
);
229 if (!cs
->ib_mapped
) {
230 cs
->ws
->base
.buffer_destroy(cs
->ib_buffer
);
233 cs
->ib_buffer
= cs
->old_ib_buffers
[--cs
->num_old_ib_buffers
];
236 cs
->ws
->base
.cs_add_buffer(&cs
->base
, cs
->ib_buffer
, 8);
238 cs
->base
.buf
[cs
->base
.cdw
++] = PKT3(PKT3_INDIRECT_BUFFER_CIK
, 2, 0);
239 cs
->base
.buf
[cs
->base
.cdw
++] = radv_amdgpu_winsys_bo(cs
->ib_buffer
)->va
;
240 cs
->base
.buf
[cs
->base
.cdw
++] = radv_amdgpu_winsys_bo(cs
->ib_buffer
)->va
>> 32;
241 cs
->ib_size_ptr
= cs
->base
.buf
+ cs
->base
.cdw
;
242 cs
->base
.buf
[cs
->base
.cdw
++] = S_3F2_CHAIN(1) | S_3F2_VALID(1);
244 cs
->base
.buf
= (uint32_t *)cs
->ib_mapped
;
246 cs
->base
.max_dw
= ib_size
/ 4 - 4;
250 static bool radv_amdgpu_cs_finalize(struct radeon_winsys_cs
*_cs
)
252 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(_cs
);
254 if (cs
->ws
->use_ib_bos
) {
255 while (!cs
->base
.cdw
|| (cs
->base
.cdw
& 7) != 0)
256 cs
->base
.buf
[cs
->base
.cdw
++] = 0xffff1000;
258 *cs
->ib_size_ptr
|= cs
->base
.cdw
;
260 cs
->is_chained
= false;
266 static void radv_amdgpu_cs_reset(struct radeon_winsys_cs
*_cs
)
268 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(_cs
);
272 for (unsigned i
= 0; i
< cs
->num_buffers
; ++i
) {
273 unsigned hash
= ((uintptr_t)cs
->handles
[i
] >> 6) &
274 (ARRAY_SIZE(cs
->buffer_hash_table
) - 1);
275 cs
->buffer_hash_table
[hash
] = -1;
280 if (cs
->ws
->use_ib_bos
) {
281 cs
->ws
->base
.cs_add_buffer(&cs
->base
, cs
->ib_buffer
, 8);
283 for (unsigned i
= 0; i
< cs
->num_old_ib_buffers
; ++i
)
284 cs
->ws
->base
.buffer_destroy(cs
->old_ib_buffers
[i
]);
286 cs
->num_old_ib_buffers
= 0;
287 cs
->ib
.ib_mc_address
= radv_amdgpu_winsys_bo(cs
->ib_buffer
)->va
;
288 cs
->ib_size_ptr
= &cs
->ib
.size
;
293 static int radv_amdgpu_cs_find_buffer(struct radv_amdgpu_cs
*cs
,
296 unsigned hash
= ((uintptr_t)bo
>> 6) & (ARRAY_SIZE(cs
->buffer_hash_table
) - 1);
297 int index
= cs
->buffer_hash_table
[hash
];
302 if (cs
->handles
[index
] == bo
)
305 for (unsigned i
= 0; i
< cs
->num_buffers
; ++i
) {
306 if (cs
->handles
[i
] == bo
) {
307 cs
->buffer_hash_table
[hash
] = i
;
315 static void radv_amdgpu_cs_add_buffer_internal(struct radv_amdgpu_cs
*cs
,
320 int index
= radv_amdgpu_cs_find_buffer(cs
, bo
);
323 cs
->priorities
[index
] = MAX2(cs
->priorities
[index
], priority
);
327 if (cs
->num_buffers
== cs
->max_num_buffers
) {
328 unsigned new_count
= MAX2(1, cs
->max_num_buffers
* 2);
329 cs
->handles
= realloc(cs
->handles
, new_count
* sizeof(amdgpu_bo_handle
));
330 cs
->priorities
= realloc(cs
->priorities
, new_count
* sizeof(uint8_t));
331 cs
->max_num_buffers
= new_count
;
334 cs
->handles
[cs
->num_buffers
] = bo
;
335 cs
->priorities
[cs
->num_buffers
] = priority
;
337 hash
= ((uintptr_t)bo
>> 6) & (ARRAY_SIZE(cs
->buffer_hash_table
) - 1);
338 cs
->buffer_hash_table
[hash
] = cs
->num_buffers
;
343 static void radv_amdgpu_cs_add_buffer(struct radeon_winsys_cs
*_cs
,
344 struct radeon_winsys_bo
*_bo
,
347 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(_cs
);
348 struct radv_amdgpu_winsys_bo
*bo
= radv_amdgpu_winsys_bo(_bo
);
350 radv_amdgpu_cs_add_buffer_internal(cs
, bo
->bo
, priority
);
353 static void radv_amdgpu_cs_execute_secondary(struct radeon_winsys_cs
*_parent
,
354 struct radeon_winsys_cs
*_child
)
356 struct radv_amdgpu_cs
*parent
= radv_amdgpu_cs(_parent
);
357 struct radv_amdgpu_cs
*child
= radv_amdgpu_cs(_child
);
359 for (unsigned i
= 0; i
< child
->num_buffers
; ++i
) {
360 radv_amdgpu_cs_add_buffer_internal(parent
, child
->handles
[i
],
361 child
->priorities
[i
]);
364 if (parent
->ws
->use_ib_bos
) {
365 if (parent
->base
.cdw
+ 4 > parent
->base
.max_dw
)
366 radv_amdgpu_cs_grow(&parent
->base
, 4);
368 parent
->base
.buf
[parent
->base
.cdw
++] = PKT3(PKT3_INDIRECT_BUFFER_CIK
, 2, 0);
369 parent
->base
.buf
[parent
->base
.cdw
++] = child
->ib
.ib_mc_address
;
370 parent
->base
.buf
[parent
->base
.cdw
++] = child
->ib
.ib_mc_address
>> 32;
371 parent
->base
.buf
[parent
->base
.cdw
++] = child
->ib
.size
;
373 if (parent
->base
.cdw
+ child
->base
.cdw
> parent
->base
.max_dw
)
374 radv_amdgpu_cs_grow(&parent
->base
, child
->base
.cdw
);
376 memcpy(parent
->base
.buf
+ parent
->base
.cdw
, child
->base
.buf
, 4 * child
->base
.cdw
);
377 parent
->base
.cdw
+= child
->base
.cdw
;
381 static int radv_amdgpu_create_bo_list(struct radv_amdgpu_winsys
*ws
,
382 struct radeon_winsys_cs
**cs_array
,
384 struct radv_amdgpu_winsys_bo
*extra_bo
,
385 amdgpu_bo_list_handle
*bo_list
)
388 if (ws
->debug_all_bos
) {
389 struct radv_amdgpu_winsys_bo
*bo
;
390 amdgpu_bo_handle
*handles
;
393 pthread_mutex_lock(&ws
->global_bo_list_lock
);
395 handles
= malloc(sizeof(handles
[0]) * ws
->num_buffers
);
397 pthread_mutex_unlock(&ws
->global_bo_list_lock
);
401 LIST_FOR_EACH_ENTRY(bo
, &ws
->global_bo_list
, global_list_item
) {
402 assert(num
< ws
->num_buffers
);
403 handles
[num
++] = bo
->bo
;
406 r
= amdgpu_bo_list_create(ws
->dev
, ws
->num_buffers
,
410 pthread_mutex_unlock(&ws
->global_bo_list_lock
);
411 } else if (count
== 1 && !extra_bo
) {
412 struct radv_amdgpu_cs
*cs
= (struct radv_amdgpu_cs
*)cs_array
[0];
413 r
= amdgpu_bo_list_create(ws
->dev
, cs
->num_buffers
, cs
->handles
,
414 cs
->priorities
, bo_list
);
416 unsigned total_buffer_count
= !!extra_bo
;
417 unsigned unique_bo_count
= !!extra_bo
;
418 for (unsigned i
= 0; i
< count
; ++i
) {
419 struct radv_amdgpu_cs
*cs
= (struct radv_amdgpu_cs
*)cs_array
[i
];
420 total_buffer_count
+= cs
->num_buffers
;
423 amdgpu_bo_handle
*handles
= malloc(sizeof(amdgpu_bo_handle
) * total_buffer_count
);
424 uint8_t *priorities
= malloc(sizeof(uint8_t) * total_buffer_count
);
425 if (!handles
|| !priorities
) {
432 handles
[0] = extra_bo
->bo
;
436 for (unsigned i
= 0; i
< count
; ++i
) {
437 struct radv_amdgpu_cs
*cs
= (struct radv_amdgpu_cs
*)cs_array
[i
];
438 for (unsigned j
= 0; j
< cs
->num_buffers
; ++j
) {
440 for (unsigned k
= 0; k
< unique_bo_count
; ++k
) {
441 if (handles
[k
] == cs
->handles
[j
]) {
443 priorities
[k
] = MAX2(priorities
[k
],
449 handles
[unique_bo_count
] = cs
->handles
[j
];
450 priorities
[unique_bo_count
] = cs
->priorities
[j
];
455 r
= amdgpu_bo_list_create(ws
->dev
, unique_bo_count
, handles
,
456 priorities
, bo_list
);
465 static int radv_amdgpu_winsys_cs_submit_chained(struct radeon_winsys_ctx
*_ctx
,
466 struct radeon_winsys_cs
**cs_array
,
468 struct radeon_winsys_fence
*_fence
)
471 struct radv_amdgpu_ctx
*ctx
= radv_amdgpu_ctx(_ctx
);
472 struct amdgpu_cs_fence
*fence
= (struct amdgpu_cs_fence
*)_fence
;
473 struct radv_amdgpu_cs
*cs0
= radv_amdgpu_cs(cs_array
[0]);
474 amdgpu_bo_list_handle bo_list
;
475 struct amdgpu_cs_request request
= {0};
477 for (unsigned i
= cs_count
; i
--;) {
478 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(cs_array
[i
]);
480 if (cs
->is_chained
) {
481 *cs
->ib_size_ptr
-= 4;
482 cs
->is_chained
= false;
485 if (i
+ 1 < cs_count
) {
486 struct radv_amdgpu_cs
*next
= radv_amdgpu_cs(cs_array
[i
+ 1]);
487 assert(cs
->base
.cdw
+ 4 <= cs
->base
.max_dw
);
489 cs
->is_chained
= true;
490 *cs
->ib_size_ptr
+= 4;
492 cs
->base
.buf
[cs
->base
.cdw
+ 0] = PKT3(PKT3_INDIRECT_BUFFER_CIK
, 2, 0);
493 cs
->base
.buf
[cs
->base
.cdw
+ 1] = next
->ib
.ib_mc_address
;
494 cs
->base
.buf
[cs
->base
.cdw
+ 2] = next
->ib
.ib_mc_address
>> 32;
495 cs
->base
.buf
[cs
->base
.cdw
+ 3] = S_3F2_CHAIN(1) | S_3F2_VALID(1) | next
->ib
.size
;
499 r
= radv_amdgpu_create_bo_list(cs0
->ws
, cs_array
, cs_count
, NULL
, &bo_list
);
501 fprintf(stderr
, "amdgpu: Failed to created the BO list for submission\n");
505 request
.ip_type
= AMDGPU_HW_IP_GFX
;
506 request
.number_of_ibs
= 1;
507 request
.ibs
= &cs0
->ib
;
508 request
.resources
= bo_list
;
510 r
= amdgpu_cs_submit(ctx
->ctx
, 0, &request
, 1);
513 fprintf(stderr
, "amdgpu: Not enough memory for command submission.\n");
515 fprintf(stderr
, "amdgpu: The CS has been rejected, "
516 "see dmesg for more information.\n");
519 amdgpu_bo_list_destroy(bo_list
);
522 fence
->context
= ctx
->ctx
;
523 fence
->ip_type
= request
.ip_type
;
524 fence
->ip_instance
= request
.ip_instance
;
525 fence
->ring
= request
.ring
;
526 fence
->fence
= request
.seq_no
;
528 ctx
->last_seq_no
= request
.seq_no
;
533 static int radv_amdgpu_winsys_cs_submit_fallback(struct radeon_winsys_ctx
*_ctx
,
534 struct radeon_winsys_cs
**cs_array
,
536 struct radeon_winsys_fence
*_fence
)
539 struct radv_amdgpu_ctx
*ctx
= radv_amdgpu_ctx(_ctx
);
540 struct amdgpu_cs_fence
*fence
= (struct amdgpu_cs_fence
*)_fence
;
541 amdgpu_bo_list_handle bo_list
;
542 struct amdgpu_cs_request request
;
546 for (unsigned i
= 0; i
< cs_count
;) {
547 struct radv_amdgpu_cs
*cs0
= radv_amdgpu_cs(cs_array
[i
]);
548 struct amdgpu_cs_ib_info ibs
[AMDGPU_CS_MAX_IBS_PER_SUBMIT
];
549 unsigned cnt
= MIN2(AMDGPU_CS_MAX_IBS_PER_SUBMIT
, cs_count
- i
);
551 memset(&request
, 0, sizeof(request
));
553 r
= radv_amdgpu_create_bo_list(cs0
->ws
, &cs_array
[i
], cnt
, NULL
, &bo_list
);
555 fprintf(stderr
, "amdgpu: Failed to created the BO list for submission\n");
559 request
.ip_type
= AMDGPU_HW_IP_GFX
;
560 request
.resources
= bo_list
;
561 request
.number_of_ibs
= cnt
;
564 for (unsigned j
= 0; j
< cnt
; ++j
) {
565 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(cs_array
[i
+ j
]);
568 if (cs
->is_chained
) {
569 *cs
->ib_size_ptr
-= 4;
570 cs
->is_chained
= false;
574 r
= amdgpu_cs_submit(ctx
->ctx
, 0, &request
, 1);
577 fprintf(stderr
, "amdgpu: Not enough memory for command submission.\n");
579 fprintf(stderr
, "amdgpu: The CS has been rejected, "
580 "see dmesg for more information.\n");
583 amdgpu_bo_list_destroy(bo_list
);
591 fence
->context
= ctx
->ctx
;
592 fence
->ip_type
= request
.ip_type
;
593 fence
->ip_instance
= request
.ip_instance
;
594 fence
->ring
= request
.ring
;
595 fence
->fence
= request
.seq_no
;
597 ctx
->last_seq_no
= request
.seq_no
;
602 static int radv_amdgpu_winsys_cs_submit_sysmem(struct radeon_winsys_ctx
*_ctx
,
603 struct radeon_winsys_cs
**cs_array
,
605 struct radeon_winsys_fence
*_fence
)
608 struct radv_amdgpu_ctx
*ctx
= radv_amdgpu_ctx(_ctx
);
609 struct amdgpu_cs_fence
*fence
= (struct amdgpu_cs_fence
*)_fence
;
610 struct radv_amdgpu_cs
*cs0
= radv_amdgpu_cs(cs_array
[0]);
611 struct radeon_winsys
*ws
= (struct radeon_winsys
*)cs0
->ws
;
612 amdgpu_bo_list_handle bo_list
;
613 struct amdgpu_cs_request request
;
614 uint32_t pad_word
= 0xffff1000U
;
616 if (radv_amdgpu_winsys(ws
)->family
== FAMILY_SI
)
617 pad_word
= 0x80000000;
621 for (unsigned i
= 0; i
< cs_count
;) {
622 struct amdgpu_cs_ib_info ib
= {0};
623 struct radeon_winsys_bo
*bo
= NULL
;
628 while (i
+ cnt
< cs_count
&& 0xffff8 - size
>= radv_amdgpu_cs(cs_array
[i
+ cnt
])->base
.cdw
) {
629 size
+= radv_amdgpu_cs(cs_array
[i
+ cnt
])->base
.cdw
;
635 bo
= ws
->buffer_create(ws
, 4 * size
, 4096, RADEON_DOMAIN_GTT
, RADEON_FLAG_CPU_ACCESS
);
636 ptr
= ws
->buffer_map(bo
);
638 for (unsigned j
= 0; j
< cnt
; ++j
) {
639 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(cs_array
[i
+ j
]);
640 memcpy(ptr
, cs
->base
.buf
, 4 * cs
->base
.cdw
);
645 while(!size
|| (size
& 7)) {
650 memset(&request
, 0, sizeof(request
));
653 r
= radv_amdgpu_create_bo_list(cs0
->ws
, &cs_array
[i
], cnt
,
654 (struct radv_amdgpu_winsys_bo
*)bo
, &bo_list
);
656 fprintf(stderr
, "amdgpu: Failed to created the BO list for submission\n");
661 ib
.ib_mc_address
= ws
->buffer_get_va(bo
);
663 request
.ip_type
= AMDGPU_HW_IP_GFX
;
664 request
.resources
= bo_list
;
665 request
.number_of_ibs
= 1;
668 r
= amdgpu_cs_submit(ctx
->ctx
, 0, &request
, 1);
671 fprintf(stderr
, "amdgpu: Not enough memory for command submission.\n");
673 fprintf(stderr
, "amdgpu: The CS has been rejected, "
674 "see dmesg for more information.\n");
677 amdgpu_bo_list_destroy(bo_list
);
679 ws
->buffer_destroy(bo
);
686 fence
->context
= ctx
->ctx
;
687 fence
->ip_type
= request
.ip_type
;
688 fence
->ip_instance
= request
.ip_instance
;
689 fence
->ring
= request
.ring
;
690 fence
->fence
= request
.seq_no
;
692 ctx
->last_seq_no
= request
.seq_no
;
697 static int radv_amdgpu_winsys_cs_submit(struct radeon_winsys_ctx
*_ctx
,
698 struct radeon_winsys_cs
**cs_array
,
701 struct radeon_winsys_fence
*_fence
)
703 struct radv_amdgpu_cs
*cs
= radv_amdgpu_cs(cs_array
[0]);
704 if (!cs
->ws
->use_ib_bos
) {
705 return radv_amdgpu_winsys_cs_submit_sysmem(_ctx
, cs_array
,
707 } else if (can_patch
&& cs_count
> AMDGPU_CS_MAX_IBS_PER_SUBMIT
&& false) {
708 return radv_amdgpu_winsys_cs_submit_chained(_ctx
, cs_array
,
711 return radv_amdgpu_winsys_cs_submit_fallback(_ctx
, cs_array
,
716 static struct radeon_winsys_ctx
*radv_amdgpu_ctx_create(struct radeon_winsys
*_ws
)
718 struct radv_amdgpu_winsys
*ws
= radv_amdgpu_winsys(_ws
);
719 struct radv_amdgpu_ctx
*ctx
= CALLOC_STRUCT(radv_amdgpu_ctx
);
724 r
= amdgpu_cs_ctx_create(ws
->dev
, &ctx
->ctx
);
726 fprintf(stderr
, "amdgpu: radv_amdgpu_cs_ctx_create failed. (%i)\n", r
);
730 return (struct radeon_winsys_ctx
*)ctx
;
735 static void radv_amdgpu_ctx_destroy(struct radeon_winsys_ctx
*rwctx
)
737 struct radv_amdgpu_ctx
*ctx
= (struct radv_amdgpu_ctx
*)rwctx
;
738 amdgpu_cs_ctx_free(ctx
->ctx
);
742 static bool radv_amdgpu_ctx_wait_idle(struct radeon_winsys_ctx
*rwctx
)
744 struct radv_amdgpu_ctx
*ctx
= (struct radv_amdgpu_ctx
*)rwctx
;
746 if (ctx
->last_seq_no
) {
748 struct amdgpu_cs_fence fence
;
750 fence
.context
= ctx
->ctx
;
751 fence
.ip_type
= RING_GFX
;
752 fence
.ip_instance
= 0;
754 fence
.fence
= ctx
->last_seq_no
;
756 int ret
= amdgpu_cs_query_fence_status(&fence
, 1000000000ull, 0,
766 void radv_amdgpu_cs_init_functions(struct radv_amdgpu_winsys
*ws
)
768 ws
->base
.ctx_create
= radv_amdgpu_ctx_create
;
769 ws
->base
.ctx_destroy
= radv_amdgpu_ctx_destroy
;
770 ws
->base
.ctx_wait_idle
= radv_amdgpu_ctx_wait_idle
;
771 ws
->base
.cs_create
= radv_amdgpu_cs_create
;
772 ws
->base
.cs_destroy
= radv_amdgpu_cs_destroy
;
773 ws
->base
.cs_grow
= radv_amdgpu_cs_grow
;
774 ws
->base
.cs_finalize
= radv_amdgpu_cs_finalize
;
775 ws
->base
.cs_reset
= radv_amdgpu_cs_reset
;
776 ws
->base
.cs_add_buffer
= radv_amdgpu_cs_add_buffer
;
777 ws
->base
.cs_execute_secondary
= radv_amdgpu_cs_execute_secondary
;
778 ws
->base
.cs_submit
= radv_amdgpu_winsys_cs_submit
;
779 ws
->base
.create_fence
= radv_amdgpu_create_fence
;
780 ws
->base
.destroy_fence
= radv_amdgpu_destroy_fence
;
781 ws
->base
.fence_wait
= radv_amdgpu_fence_wait
;