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 <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 uint64_t priority_usage
;
52 uint32_t real_idx
; /* index of underlying real BO */
55 enum radeon_bo_usage usage
;
64 struct radeon_winsys_cs base
;
66 /* A buffer out of which new IBs are allocated. */
67 struct pb_buffer
*big_ib_buffer
;
69 unsigned used_ib_space
;
71 uint32_t *ptr_ib_size
;
72 bool ptr_ib_size_inside_ib
;
76 struct amdgpu_cs_context
{
77 struct drm_amdgpu_cs_chunk_ib ib
[IB_NUM
];
80 unsigned max_real_buffers
;
81 unsigned num_real_buffers
;
82 struct amdgpu_cs_buffer
*real_buffers
;
84 unsigned max_real_submit
;
85 amdgpu_bo_handle
*handles
;
88 unsigned num_slab_buffers
;
89 unsigned max_slab_buffers
;
90 struct amdgpu_cs_buffer
*slab_buffers
;
92 unsigned num_sparse_buffers
;
93 unsigned max_sparse_buffers
;
94 struct amdgpu_cs_buffer
*sparse_buffers
;
96 int buffer_indices_hashlist
[4096];
98 struct amdgpu_winsys_bo
*last_added_bo
;
99 unsigned last_added_bo_index
;
100 unsigned last_added_bo_usage
;
101 uint64_t last_added_bo_priority_usage
;
103 struct pipe_fence_handle
**fence_dependencies
;
104 unsigned num_fence_dependencies
;
105 unsigned max_fence_dependencies
;
107 struct pipe_fence_handle
**syncobj_to_signal
;
108 unsigned num_syncobj_to_signal
;
109 unsigned max_syncobj_to_signal
;
111 struct pipe_fence_handle
*fence
;
113 /* the error returned from cs_flush for non-async submissions */
118 struct amdgpu_ib main
; /* must be first because this is inherited */
119 struct amdgpu_ctx
*ctx
;
120 enum ring_type ring_type
;
121 struct drm_amdgpu_cs_chunk_fence fence_chunk
;
123 /* We flip between these two CS. While one is being consumed
124 * by the kernel in another thread, the other one is being filled
125 * by the pipe driver. */
126 struct amdgpu_cs_context csc1
;
127 struct amdgpu_cs_context csc2
;
128 /* The currently-used CS. */
129 struct amdgpu_cs_context
*csc
;
130 /* The CS being currently-owned by the other thread. */
131 struct amdgpu_cs_context
*cst
;
134 void (*flush_cs
)(void *ctx
, unsigned flags
, struct pipe_fence_handle
**fence
);
137 struct util_queue_fence flush_completed
;
138 struct pipe_fence_handle
*next_fence
;
141 struct amdgpu_fence
{
142 struct pipe_reference reference
;
143 /* If ctx == NULL, this fence is syncobj-based. */
146 struct amdgpu_winsys
*ws
;
147 struct amdgpu_ctx
*ctx
; /* submission context */
148 struct amdgpu_cs_fence fence
;
149 uint64_t *user_fence_cpu_address
;
151 /* If the fence has been submitted. This is unsignalled for deferred fences
152 * (cs->next_fence) and while an IB is still being submitted in the submit
154 struct util_queue_fence submitted
;
156 volatile int signalled
; /* bool (int for atomicity) */
159 static inline bool amdgpu_fence_is_syncobj(struct amdgpu_fence
*fence
)
161 return fence
->ctx
== NULL
;
164 static inline void amdgpu_ctx_unref(struct amdgpu_ctx
*ctx
)
166 if (p_atomic_dec_zero(&ctx
->refcount
)) {
167 amdgpu_cs_ctx_free(ctx
->ctx
);
168 amdgpu_bo_free(ctx
->user_fence_bo
);
173 static inline void amdgpu_fence_reference(struct pipe_fence_handle
**dst
,
174 struct pipe_fence_handle
*src
)
176 struct amdgpu_fence
**rdst
= (struct amdgpu_fence
**)dst
;
177 struct amdgpu_fence
*rsrc
= (struct amdgpu_fence
*)src
;
179 if (pipe_reference(&(*rdst
)->reference
, &rsrc
->reference
)) {
180 struct amdgpu_fence
*fence
= *rdst
;
182 if (amdgpu_fence_is_syncobj(fence
))
183 amdgpu_cs_destroy_syncobj(fence
->ws
->dev
, fence
->syncobj
);
185 amdgpu_ctx_unref(fence
->ctx
);
187 util_queue_fence_destroy(&fence
->submitted
);
193 int amdgpu_lookup_buffer(struct amdgpu_cs_context
*cs
, struct amdgpu_winsys_bo
*bo
);
195 static inline struct amdgpu_ib
*
196 amdgpu_ib(struct radeon_winsys_cs
*base
)
198 return (struct amdgpu_ib
*)base
;
201 static inline struct amdgpu_cs
*
202 amdgpu_cs(struct radeon_winsys_cs
*base
)
204 assert(amdgpu_ib(base
)->ib_type
== IB_MAIN
);
205 return (struct amdgpu_cs
*)base
;
208 #define get_container(member_ptr, container_type, container_member) \
209 (container_type *)((char *)(member_ptr) - offsetof(container_type, container_member))
211 static inline struct amdgpu_cs
*
212 amdgpu_cs_from_ib(struct amdgpu_ib
*ib
)
214 switch (ib
->ib_type
) {
216 return get_container(ib
, struct amdgpu_cs
, main
);
218 unreachable("bad ib_type");
223 amdgpu_bo_is_referenced_by_cs(struct amdgpu_cs
*cs
,
224 struct amdgpu_winsys_bo
*bo
)
226 int num_refs
= bo
->num_cs_references
;
227 return num_refs
== bo
->ws
->num_cs
||
228 (num_refs
&& amdgpu_lookup_buffer(cs
->csc
, bo
) != -1);
232 amdgpu_bo_is_referenced_by_cs_with_usage(struct amdgpu_cs
*cs
,
233 struct amdgpu_winsys_bo
*bo
,
234 enum radeon_bo_usage usage
)
237 struct amdgpu_cs_buffer
*buffer
;
239 if (!bo
->num_cs_references
)
242 index
= amdgpu_lookup_buffer(cs
->csc
, bo
);
246 buffer
= bo
->bo
? &cs
->csc
->real_buffers
[index
] :
247 bo
->sparse
? &cs
->csc
->sparse_buffers
[index
] :
248 &cs
->csc
->slab_buffers
[index
];
250 return (buffer
->usage
& usage
) != 0;
254 amdgpu_bo_is_referenced_by_any_cs(struct amdgpu_winsys_bo
*bo
)
256 return bo
->num_cs_references
!= 0;
259 bool amdgpu_fence_wait(struct pipe_fence_handle
*fence
, uint64_t timeout
,
261 void amdgpu_add_fences(struct amdgpu_winsys_bo
*bo
,
263 struct pipe_fence_handle
**fences
);
264 void amdgpu_cs_sync_flush(struct radeon_winsys_cs
*rcs
);
265 void amdgpu_cs_init_functions(struct amdgpu_winsys
*ws
);
266 void amdgpu_cs_submit_ib(void *job
, int thread_index
);