2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
3 * Copyright © 2015 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
31 #include "amdgpu_bo.h"
32 #include "util/u_memory.h"
33 #include "drm-uapi/amdgpu_drm.h"
36 struct amdgpu_winsys
*ws
;
37 amdgpu_context_handle ctx
;
38 amdgpu_bo_handle user_fence_bo
;
39 uint64_t *user_fence_cpu_address_base
;
41 unsigned initial_num_total_rejected_cs
;
42 unsigned num_rejected_cs
;
45 struct amdgpu_cs_buffer
{
46 struct amdgpu_winsys_bo
*bo
;
49 uint32_t priority_usage
;
52 uint32_t real_idx
; /* index of underlying real BO */
55 enum radeon_bo_usage usage
;
65 struct radeon_cmdbuf base
;
67 /* A buffer out of which new IBs are allocated. */
68 struct pb_buffer
*big_ib_buffer
;
70 unsigned used_ib_space
;
72 /* The maximum seen size from cs_check_space. If the driver does
73 * cs_check_space and flush, the newly allocated IB should have at least
76 unsigned max_check_space_size
;
79 uint32_t *ptr_ib_size
;
80 bool ptr_ib_size_inside_ib
;
84 struct amdgpu_fence_list
{
85 struct pipe_fence_handle
**list
;
90 struct amdgpu_cs_context
{
91 struct drm_amdgpu_cs_chunk_ib ib
[IB_NUM
];
94 unsigned max_real_buffers
;
95 unsigned num_real_buffers
;
96 struct amdgpu_cs_buffer
*real_buffers
;
98 unsigned num_slab_buffers
;
99 unsigned max_slab_buffers
;
100 struct amdgpu_cs_buffer
*slab_buffers
;
102 unsigned num_sparse_buffers
;
103 unsigned max_sparse_buffers
;
104 struct amdgpu_cs_buffer
*sparse_buffers
;
106 int buffer_indices_hashlist
[4096];
108 struct amdgpu_winsys_bo
*last_added_bo
;
109 unsigned last_added_bo_index
;
110 unsigned last_added_bo_usage
;
111 uint32_t last_added_bo_priority_usage
;
113 struct amdgpu_fence_list fence_dependencies
;
114 struct amdgpu_fence_list syncobj_dependencies
;
115 struct amdgpu_fence_list syncobj_to_signal
;
117 /* The compute IB uses the dependencies above + these: */
118 struct amdgpu_fence_list compute_fence_dependencies
;
119 struct amdgpu_fence_list compute_start_fence_dependencies
;
121 struct pipe_fence_handle
*fence
;
123 /* the error returned from cs_flush for non-async submissions */
126 /* TMZ: will this command be submitted using the TMZ flag */
131 struct amdgpu_ib main
; /* must be first because this is inherited */
132 struct amdgpu_ib compute_ib
; /* optional parallel compute IB */
133 struct amdgpu_ctx
*ctx
;
134 enum ring_type ring_type
;
135 struct drm_amdgpu_cs_chunk_fence fence_chunk
;
137 /* We flip between these two CS. While one is being consumed
138 * by the kernel in another thread, the other one is being filled
139 * by the pipe driver. */
140 struct amdgpu_cs_context csc1
;
141 struct amdgpu_cs_context csc2
;
142 /* The currently-used CS. */
143 struct amdgpu_cs_context
*csc
;
144 /* The CS being currently-owned by the other thread. */
145 struct amdgpu_cs_context
*cst
;
148 void (*flush_cs
)(void *ctx
, unsigned flags
, struct pipe_fence_handle
**fence
);
150 bool stop_exec_on_failure
;
152 struct util_queue_fence flush_completed
;
153 struct pipe_fence_handle
*next_fence
;
156 struct amdgpu_fence
{
157 struct pipe_reference reference
;
158 /* If ctx == NULL, this fence is syncobj-based. */
161 struct amdgpu_winsys
*ws
;
162 struct amdgpu_ctx
*ctx
; /* submission context */
163 struct amdgpu_cs_fence fence
;
164 uint64_t *user_fence_cpu_address
;
166 /* If the fence has been submitted. This is unsignalled for deferred fences
167 * (cs->next_fence) and while an IB is still being submitted in the submit
169 struct util_queue_fence submitted
;
171 volatile int signalled
; /* bool (int for atomicity) */
174 static inline bool amdgpu_fence_is_syncobj(struct amdgpu_fence
*fence
)
176 return fence
->ctx
== NULL
;
179 static inline void amdgpu_ctx_unref(struct amdgpu_ctx
*ctx
)
181 if (p_atomic_dec_zero(&ctx
->refcount
)) {
182 amdgpu_cs_ctx_free(ctx
->ctx
);
183 amdgpu_bo_free(ctx
->user_fence_bo
);
188 static inline void amdgpu_fence_reference(struct pipe_fence_handle
**dst
,
189 struct pipe_fence_handle
*src
)
191 struct amdgpu_fence
**adst
= (struct amdgpu_fence
**)dst
;
192 struct amdgpu_fence
*asrc
= (struct amdgpu_fence
*)src
;
194 if (pipe_reference(&(*adst
)->reference
, &asrc
->reference
)) {
195 struct amdgpu_fence
*fence
= *adst
;
197 if (amdgpu_fence_is_syncobj(fence
))
198 amdgpu_cs_destroy_syncobj(fence
->ws
->dev
, fence
->syncobj
);
200 amdgpu_ctx_unref(fence
->ctx
);
202 util_queue_fence_destroy(&fence
->submitted
);
208 int amdgpu_lookup_buffer(struct amdgpu_cs_context
*cs
, struct amdgpu_winsys_bo
*bo
);
210 static inline struct amdgpu_ib
*
211 amdgpu_ib(struct radeon_cmdbuf
*base
)
213 return (struct amdgpu_ib
*)base
;
216 static inline struct amdgpu_cs
*
217 amdgpu_cs(struct radeon_cmdbuf
*base
)
219 assert(amdgpu_ib(base
)->ib_type
== IB_MAIN
);
220 return (struct amdgpu_cs
*)base
;
223 #define get_container(member_ptr, container_type, container_member) \
224 (container_type *)((char *)(member_ptr) - offsetof(container_type, container_member))
226 static inline struct amdgpu_cs
*
227 amdgpu_cs_from_ib(struct amdgpu_ib
*ib
)
229 switch (ib
->ib_type
) {
231 return get_container(ib
, struct amdgpu_cs
, main
);
232 case IB_PARALLEL_COMPUTE
:
233 return get_container(ib
, struct amdgpu_cs
, compute_ib
);
235 unreachable("bad ib_type");
240 amdgpu_bo_is_referenced_by_cs(struct amdgpu_cs
*cs
,
241 struct amdgpu_winsys_bo
*bo
)
243 int num_refs
= bo
->num_cs_references
;
244 return num_refs
== bo
->ws
->num_cs
||
245 (num_refs
&& amdgpu_lookup_buffer(cs
->csc
, bo
) != -1);
249 amdgpu_bo_is_referenced_by_cs_with_usage(struct amdgpu_cs
*cs
,
250 struct amdgpu_winsys_bo
*bo
,
251 enum radeon_bo_usage usage
)
254 struct amdgpu_cs_buffer
*buffer
;
256 if (!bo
->num_cs_references
)
259 index
= amdgpu_lookup_buffer(cs
->csc
, bo
);
263 buffer
= bo
->bo
? &cs
->csc
->real_buffers
[index
] :
264 bo
->sparse
? &cs
->csc
->sparse_buffers
[index
] :
265 &cs
->csc
->slab_buffers
[index
];
267 return (buffer
->usage
& usage
) != 0;
271 amdgpu_bo_is_referenced_by_any_cs(struct amdgpu_winsys_bo
*bo
)
273 return bo
->num_cs_references
!= 0;
276 bool amdgpu_fence_wait(struct pipe_fence_handle
*fence
, uint64_t timeout
,
278 void amdgpu_add_fences(struct amdgpu_winsys_bo
*bo
,
280 struct pipe_fence_handle
**fences
);
281 void amdgpu_cs_sync_flush(struct radeon_cmdbuf
*rcs
);
282 void amdgpu_cs_init_functions(struct amdgpu_screen_winsys
*ws
);
283 void amdgpu_cs_submit_ib(void *job
, int thread_index
);