winsys/amdgpu: add back multithreaded command submission
[mesa.git] / src / gallium / winsys / amdgpu / drm / amdgpu_cs.h
1 /*
2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
3 * Copyright © 2015 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Marek Olšák <maraeo@gmail.com>
30 */
31
32 #ifndef AMDGPU_CS_H
33 #define AMDGPU_CS_H
34
35 #include "amdgpu_bo.h"
36 #include "util/u_memory.h"
37
38 struct amdgpu_ctx {
39 struct amdgpu_winsys *ws;
40 amdgpu_context_handle ctx;
41 amdgpu_bo_handle user_fence_bo;
42 uint64_t *user_fence_cpu_address_base;
43 int refcount;
44 };
45
46 struct amdgpu_cs_buffer {
47 struct amdgpu_winsys_bo *bo;
48 uint64_t priority_usage;
49 enum radeon_bo_usage usage;
50 enum radeon_bo_domain domains;
51 };
52
53 struct amdgpu_ib {
54 struct radeon_winsys_cs base;
55
56 /* A buffer out of which new IBs are allocated. */
57 struct pb_buffer *big_ib_buffer;
58 uint8_t *ib_mapped;
59 unsigned used_ib_space;
60 };
61
62 enum {
63 IB_CONST_PREAMBLE = 0,
64 IB_CONST = 1, /* the const IB must be first */
65 IB_MAIN = 2,
66 IB_NUM
67 };
68
69 struct amdgpu_cs_context {
70 struct amdgpu_cs_request request;
71 struct amdgpu_cs_ib_info ib[IB_NUM];
72
73 /* Buffers. */
74 unsigned max_num_buffers;
75 unsigned num_buffers;
76 amdgpu_bo_handle *handles;
77 uint8_t *flags;
78 struct amdgpu_cs_buffer *buffers;
79
80 int buffer_indices_hashlist[4096];
81
82 uint64_t used_vram;
83 uint64_t used_gart;
84
85 unsigned max_dependencies;
86
87 struct pipe_fence_handle *fence;
88 };
89
90 struct amdgpu_cs {
91 struct amdgpu_ib main; /* must be first because this is inherited */
92 struct amdgpu_ib const_ib; /* optional constant engine IB */
93 struct amdgpu_ib const_preamble_ib;
94 struct amdgpu_ctx *ctx;
95 enum ring_type ring_type;
96
97 /* We flip between these two CS. While one is being consumed
98 * by the kernel in another thread, the other one is being filled
99 * by the pipe driver. */
100 struct amdgpu_cs_context csc1;
101 struct amdgpu_cs_context csc2;
102 /* The currently-used CS. */
103 struct amdgpu_cs_context *csc;
104 /* The CS being currently-owned by the other thread. */
105 struct amdgpu_cs_context *cst;
106
107 /* Flush CS. */
108 void (*flush_cs)(void *ctx, unsigned flags, struct pipe_fence_handle **fence);
109 void *flush_data;
110
111 pipe_semaphore flush_completed;
112 };
113
114 struct amdgpu_fence {
115 struct pipe_reference reference;
116
117 struct amdgpu_ctx *ctx; /* submission context */
118 struct amdgpu_cs_fence fence;
119 uint64_t *user_fence_cpu_address;
120
121 /* If the fence is unknown due to an IB still being submitted
122 * in the other thread. */
123 volatile int submission_in_progress; /* bool (int for atomicity) */
124 volatile int signalled; /* bool (int for atomicity) */
125 };
126
127 static inline void amdgpu_ctx_unref(struct amdgpu_ctx *ctx)
128 {
129 if (p_atomic_dec_zero(&ctx->refcount)) {
130 amdgpu_cs_ctx_free(ctx->ctx);
131 amdgpu_bo_free(ctx->user_fence_bo);
132 FREE(ctx);
133 }
134 }
135
136 static inline void amdgpu_fence_reference(struct pipe_fence_handle **dst,
137 struct pipe_fence_handle *src)
138 {
139 struct amdgpu_fence **rdst = (struct amdgpu_fence **)dst;
140 struct amdgpu_fence *rsrc = (struct amdgpu_fence *)src;
141
142 if (pipe_reference(&(*rdst)->reference, &rsrc->reference)) {
143 amdgpu_ctx_unref((*rdst)->ctx);
144 FREE(*rdst);
145 }
146 *rdst = rsrc;
147 }
148
149 int amdgpu_lookup_buffer(struct amdgpu_cs_context *cs, struct amdgpu_winsys_bo *bo);
150
151 static inline struct amdgpu_cs *
152 amdgpu_cs(struct radeon_winsys_cs *base)
153 {
154 return (struct amdgpu_cs*)base;
155 }
156
157 static inline boolean
158 amdgpu_bo_is_referenced_by_cs(struct amdgpu_cs *cs,
159 struct amdgpu_winsys_bo *bo)
160 {
161 int num_refs = bo->num_cs_references;
162 return num_refs == bo->ws->num_cs ||
163 (num_refs && amdgpu_lookup_buffer(cs->csc, bo) != -1);
164 }
165
166 static inline boolean
167 amdgpu_bo_is_referenced_by_cs_with_usage(struct amdgpu_cs *cs,
168 struct amdgpu_winsys_bo *bo,
169 enum radeon_bo_usage usage)
170 {
171 int index;
172
173 if (!bo->num_cs_references)
174 return FALSE;
175
176 index = amdgpu_lookup_buffer(cs->csc, bo);
177 if (index == -1)
178 return FALSE;
179
180 return (cs->csc->buffers[index].usage & usage) != 0;
181 }
182
183 static inline boolean
184 amdgpu_bo_is_referenced_by_any_cs(struct amdgpu_winsys_bo *bo)
185 {
186 return bo->num_cs_references != 0;
187 }
188
189 bool amdgpu_fence_wait(struct pipe_fence_handle *fence, uint64_t timeout,
190 bool absolute);
191 void amdgpu_cs_sync_flush(struct radeon_winsys_cs *rcs);
192 void amdgpu_cs_init_functions(struct amdgpu_winsys *ws);
193 void amdgpu_cs_submit_ib(struct amdgpu_cs *cs);
194
195 #endif